Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where clause parentheses with comma separated values greater than

This is a very very noob and silly question but I can't seem to find what the following means in Postgres:

select * 
from table 
where (last_updated, id) > (timestamp with time zone '2019-03-28 23:30:22.496+00:00', 0)

Basically what does (last_updated, id) > (timestamp with time zone '2019-03-28 23:30:22.496+00:00', 0) mean ? what's it comparing ? changing second value doesn't seem to have effects on the results for some reason.

like image 805
voidMainReturn Avatar asked Oct 18 '25 08:10

voidMainReturn


1 Answers

Comparing tuples uses lexographical order, meaning that the second value is only used in case the first value ties.

So if your row has a timestamp of exactly that cutoff value, then the id needs to be greater than 0.

Hunch: This is used for cursor-based paging, where the second page starts off right after the last value on the first page (according to the sort criteria, here a timestamp), with id used as a tie-breaker (for when the last entry on the previous page and the first entry on the next page have the same sorting value --- probably unlikely for timestamps, but very likely if you order by salary or such).

like image 72
Thilo Avatar answered Oct 20 '25 21:10

Thilo