Querying the Next Page After a Composite Cursor

A query can continue beyond the last displayed pair of sort keys, under the same order and filter as earlier pages.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace equal timestamps
  2. Check ordinary insertions
  3. Compare against the cursor using the actual sort directions
  4. Verify transitions within ties as well as across days

Trace equal timestamps

For ascending time and ID, after time T and ID 7 continue with later times or time T and ID above 7. Cursor pagination explains why both fields matter.

Check ordinary insertions

An inserted row before the cursor does not shift that boundary the way an offset may. Later changes to sort fields can still alter observed pages. See SQL order rules.[1]

Compare against the cursor using the actual sort directions

With newest day first and ID ascending within a day, rows after day five and ID twenty include older days or the same day with a larger ID. A tuple comparison using the same direction for both columns does not express that mixed order. See cursor pagination.

Cursor (day=5, id=20)
Eligible: (5,22), (4,3)
Ineligible: (5,18), (6,1)
Predicate: day < 5 OR (day = 5 AND id > 20)

Verify transitions within ties as well as across days

Query and displayed order must agree. If sort fields can be null, define their order explicitly and adjust the continuation. Test several pages from a fixed fixture before testing concurrent updates.

References

  1. PostgreSQL Global Development Group, Queries ↩