Postgres LIMIT/OFFSET strange behaviour Postgres LIMIT/OFFSET strange behaviour postgresql postgresql

Postgres LIMIT/OFFSET strange behaviour


There is no "bug" at work here. You specified the following ordering, which was used when LIMIT and OFFSET were being applied:

ORDER BY anon_1.rating_points DESC

However, in the case two or more records are tied with the same rating_points values, Postgres makes no guarantee about what the ordering will be. This is why you are seeing a user with anon_id_1 of 20 apparently moving around. Postgres has done nothing wrong; it has honored your request to order by the rating_points, but you never told it what to do in the case of tie.

To resolve this, you could add a second condition to the ORDER BY:

ORDER BY    anon_1.rating_points DESC,    anon_id_1

This would break the tie with regard to ordering, and, assuming anon_id_1 is a primary key, the results would appear to be stable after making this change.