Sequences
About
A sequence in PostgreSQL is a special database object that generates a monotonically increasing (or decreasing) series of unique numeric values.
It can be customized (step, min, max, etc.)
Sequences are not rolledback in transactions, once you call nextval(), the value is gone forever, even if your transaction fails.
This is by design — so that sequence numbers are always unique even under concurrency.
Creating
CREATE SEQUENCE IF NOT EXISTS seq_name;-- Specify the type of sequence (smallint | int | bigint)
CREATE SEQUENCE IF NOT EXISTS seq_name AS bigint
START WITH 1000
INCREMENT BY 10
MINVALUE 1000
MAXVALUE 1000000
CACHE 100
-- When the value reaches the maxvalue or minvalue it starts over
CYCLE
;Advance sequence
Get current value
Set sequence to specific value
Deleting
List all sequences
Sharing Sequence
Share sequence between tables like:
Last updated