|
|
|
|
|
by aidos
1129 days ago
|
|
I use multi-column indexes for things like, “find me the most recent version of this template for this customer”. I really wish pg had a way to do partial indexes with limits so I could create a partial index that stores, for example, only the most recent version of something (I find this comes up a lot). |
|
Select the ID with the max sync_token. Easiest for Postgres to optimize, assuming you have a primary key of (id, sync_token).
Define a view using DISTINCT ON. Convenient for ad-hoc querying. Postgres usually figures out it can use the primary key to avoid a full-table scan. For tricky predicates, I use a trigger to track the most recent resource in a separate table. This is a hacky version of incremental view maintenance. [1][1]: https://wiki.postgresql.org/wiki/Incremental_View_Maintenanc...