Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - repeating rows from LIMIT OFFSET

I noticed some repeating rows in a paginated recordset.

When I run this query:

SELECT "students".*  FROM "students"  ORDER BY "students"."status" asc  LIMIT 3 OFFSET 0 

I get:

    | id | name  | status |     | 1  | foo   | active |     | 12 | alice | active |     | 4  | bob   | active | 

Next query:

SELECT "students".*  FROM "students"  ORDER BY "students"."status" asc  LIMIT 3 OFFSET 3 

I get:

    | id | name  | status |     | 1  | foo   | active |     | 6  | cindy | active |     | 2  | dylan | active | 

Why does "foo" appear in both queries?

like image 599
keewooi Avatar asked Nov 27 '12 09:11

keewooi


People also ask

How does limit and offset work PostgreSQL?

If a limit count is given, no more than that many rows will be returned (but possibly fewer, if the query itself yields fewer rows). LIMIT ALL is the same as omitting the LIMIT clause, as is LIMIT with a NULL argument. OFFSET says to skip that many rows before beginning to return rows.

How do I limit rows in PostgreSQL?

The PostgreSQL LIMIT clause is used to get a subset of rows generated by a query. It is an optional clause of the SELECT statement. The LIMIT clause can be used with the OFFSET clause to skip a specific number of rows before returning the query for the LIMIT clause.

How do you use limit and offset in query?

The limit option allows you to limit the number of rows returned from a query, while offset allows you to omit a specified number of rows before the beginning of the result set. Using both limit and offset skips both rows as well as limit the rows returned.


2 Answers

Why does "foo" appear in both queries?

Because all rows that are returned have the same value for the status column. In that case the database is free to return the rows in any order it wants.

If you want a reproducable ordering you need to add a second column to your order by statement to make it consistent. E.g. the ID column:

SELECT students.*  FROM students  ORDER BY students.status asc,           students.id asc 

If two rows have the same value for the status column, they will be sorted by the id.

like image 116
a_horse_with_no_name Avatar answered Sep 28 '22 01:09

a_horse_with_no_name


For more details from PostgreSQL documentation (http://www.postgresql.org/docs/8.3/static/queries-limit.html) :

When using LIMIT, it is important to use an ORDER BY clause that constrains the result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows. You might be asking for the tenth through twentieth rows, but tenth through twentieth in what ordering? The ordering is unknown, unless you specified ORDER BY.

The query optimizer takes LIMIT into account when generating a query plan, so you are very likely to get different plans (yielding different row orders) depending on what you give for LIMIT and OFFSET. Thus, using different LIMIT/OFFSET values to select different subsets of a query result will give inconsistent results unless you enforce a predictable result ordering with ORDER BY. This is not a bug; it is an inherent consequence of the fact that SQL does not promise to deliver the results of a query in any particular order unless ORDER BY is used to constrain the order.

like image 43
Ahmed MANSOUR Avatar answered Sep 28 '22 00:09

Ahmed MANSOUR