Postgres 10 brings an implementation of the SQL standard GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY
feature, loosely known as identity column. This feature supplants Postgres’ own SERIAL
pseudo-type, eliminating its issues/problems. For more info, see here and here.
One of the main reasons for GENERATED…
being superior to SERIAL
is that the sequence number generator is handled in the background transparently. We can add the column, adjust the current sequence number, and drop the column, all without referring explicitly to the sequence.
CREATE TABLE tbl (
col BIGINT
GENERATED ALWAYS AS IDENTITY
PRIMARY KEY ,
col2 text
)
;
And…
ALTER TABLE tbl
ALTER COLUMN col
RESTART WITH 1000
;
And…
ALTER TABLE tbl
ALTER COLUMN col
DROP IDENTITY IF EXISTS
;
Or…
ALTER TABLE tbl
ALTER COLUMN col
DROP COLUMN IF EXISTS
;
Note how in none of that example code did we refer to a specific sequence-generating object.
I presume this feature is implemented using the existing Sequence feature in Postgres.
➠ How do we query this background sequence? How do we ask what the last/next value generated will be? What is the equivalent of the Sequence manipulation functions?