Strange ordering bug (is it a bug?) when ordering two columns with identical values Strange ordering bug (is it a bug?) when ordering two columns with identical values postgresql postgresql

Strange ordering bug (is it a bug?) when ordering two columns with identical values


This is not a bug. The limit and offset happen after ordering and it is not deterministic which rows are selected in one case vs another. In general you want to have a tiebreaker so that your ordering is stable and deterministic (I prefer to use unique tiebreakers even when I don't have limit or offset issues in order to ensure the query is the same each time it is run).

If you are doing pagination, add the primary key or surrogate key to the sort as a tiebreaker. That is really the best way.