bbirk | 1 year ago | on: Keyset cursors, not offsets, for Postgres pagination
bbirk's comments
bbirk | 3 years ago | on: TimescaleDB 2.7 vs. PostgreSQL 14
bbirk | 4 years ago | on: Lesser-known Postgres features
select user_table.user_id, tmp.post_id, tmp.text, tmp.timestamp
from user_table
left outer join lateral (
select post_id, text, timestamp
from post
where post.user_id = user_table.user_id
order by timestamp
limit 1
) tmp on true
order by user_id
limit 30;
bbirk | 4 years ago | on: Random identifiers are poorly compressible
If you have only a single master DBMS server, then 64-bit integers should be sufficient when using this strategy. Otherwise, if you are using multi-master in a distributed environment, you could achieve lock/communication-free identifier generation by instead using a 128 bit integer and for example include the server identifier inside those 128 bits, similarly to snowflake, before encrypting it and exposing it to the user.
Side note, since the identifiers are sequential there is anther benefit in that, like snowflake, when you order by (created_date, user_id), it wouldn't incur any performance cost over just ordering by user_id.
If anyone knows anyone who uses this or why it would be a bad idea I would love to know.
bbirk | 5 years ago | on: Problems with JPA/Hibernate
bbirk | 5 years ago | on: Cover Your Tracks
With the reverse pagination solution here, the order of the items is inverted. For example if you order by some incrementing value, and page size is 5, then page 1 has items 1,2,3,4,5, in that order, the next page, page 2, has items 6,7,8,9,10, and when you press previous page you get items 5,4,3,2,1 instead of the original order. You can reverse them in the client when you use previous pagination, but this is inconvenient. You can implement this in sql by wrapping it in another order by with the correct order. There is a page on select (sql) on wikipedia that covers this kind of pagination for most relational databases[1].
>Designing interfaces
You mentioned that you don't have a concept of link to page 7 when you use cursor pagination, but you could always combine offset and cursor pagination, like: "6 pages (as offset) after item with lastupdate=2024-01-01". The performance downsides of using offset are not that big with small offsets, and you can add some maximum offset in the backend to make sure the performance is good enough. This means you can also add a feature where the user can jump 5 pages ahead instead of having to jump one page at a time like with the github example, without most of the performance overhead of using pure offset pagination.
>So, I think it's reasonable to return cursors that traverse in just one direction
Being able to traverse backwards is genuinely useful in a lot of cases, and as a developer it is your job to figure out a technical solution to the user needs. Again, have a look at the pagination wikipedia page to see how you can implement it in sql.
[1] https://en.wikipedia.org/wiki/Select_(SQL)#Method_with_filte...