All we need is an easy explanation of the problem, so here it is.
I want to generate and write 128-bit k-ordered Flake IDs to a column from clients connected to my PostgreSQL database. These IDs are essentially a large globally-unique numbers that grow with time, similar to a monotonically increasing ID but without any coordination.
The two most common query patterns would look this:
- Sort by the Flake ID
SELECT * ORDER BY flake_id
- Group by another column, and then select the maximum Flake ID in each group.
SELECT max(flake_id) GROUP BY some_other_column
There seem to be a few possible ways to do this:
bigintwith two columns. Simple to order by, but unclear how to select maximum across two words after a group-by.
byteawith 16 bytes.
uuidhappen to be 128-bit, and testing yields ORDER BY results, but there is no
maxfunction that can be applied to it
- encoded as a string, and stored in
texthaven’t tested it but a
maxfunction seems nonsensical unless it uses a lexicographic order. It also seems a bit dirty to use a string to encode what is order-able on its own numerically.
There’s a bit of choice paralysis in the options and how they interact with indexes to perform the above query patterns most efficiently.
I am looking for insight into the ideal data-type considering the above query patterns, and how it would interact with the relevant indexes.
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
If you want to go with two
bigints, create a composite type for
CREATE TYPE pair (a bigint, b bigint);
Your first query then can remain as it is, and your second query could be rewritten to
SELECT DISTINCT ON (some_other_column) flake_id ORDER BY some_other_column, flake_id DESC;
But why don’t you use the obvious data type
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂