Global sequences¶
New feature
This feature is new and experimental. Make sure to test it before deploying to production.
The open source edition of PgDog can generate unique, BIGINT primary keys in two ways:
However, both methods produce gaps in the numbers, while the unique ID generates very large 64-bit numbers, which may not work with all applications, e.g., JavaScript apps that pass identifiers directly.
Global sequences are powered by Raft and are guaranteed to produce sequential integers, starting at 1, just like regular PostgreSQL sequences.
How it works¶
This feature requires PgDog to be connected to the control plane. Once configured, the following functions will begin to work automatically:
| Function | Description |
|---|---|
pgdog.nextval(name) |
Return a monotonically increasing integer for the given sequence name. |
Usage¶
The sequence functions can be called in any query, including INSERT, UPDATE, and DELETE statements. The sequence values can also be automatically injected into INSERT queries targeting omnisharded tables:
The name of the sequence is automatically derived from the table and column names of the table targeted by the INSERT statement. For example, enabling this feature will produce the following rewrite:
Under the hood¶
All sequence values are stored in the Raft log, which makes them durable, just like PostgreSQL sequences. For this reason, enabling Raft in the control plane is required for this feature to work.
Performance¶
Due to their distributed and durable nature, global sequences are slower to generate numbers than Unique ID and sharded sequences, and should be used for infrequent writes into tables that otherwise would not be able to support BIGINT numbers.