Predict the output
The client saw rows up to (ts=3, id=7). What does the next page return?
CREATE TABLE posts (id int PRIMARY KEY, ts int NOT NULL);
INSERT INTO posts VALUES (9, 4), (8, 3), (7, 3), (6, 3), (5, 2);
SELECT id FROM posts
WHERE (ts, id) < (3, 7)
ORDER BY ts DESC, id DESC
LIMIT 2;Hint 1 of 1
Compare tuples left to right.
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 6 then 5
Correct. (3, 6) and (2, 5) are the next keys below (3, 7) in descending order.
A row comparison like (ts, id) < (3, 7) compares ts first, then id when ts ties. That matches the ORDER BY, so the next page starts right after the last row the client saw.
Why not the others?
- B
5only. Comparing on ts alone would skip id 6, which shares ts = 3. - C
8then6. (3, 8) is greater than (3, 7), so it was on an earlier page. - D
7then6. The cursor row itself is excluded by the strict <.
| id |
|---|
| 6 |
| 5 |
(2 rows)