Contents
ALTER SEQUENCE
An SQL command to modify the definition of a sequence
ALTER SEQUENCE
is a DDL command to modify the definition of a sequence.
ALTER SEQUENCE
was added in PostgreSQL 7.4.
Change history
- PostgreSQL 15
- syntax
SET [ LOGGED | UNLOGGED ]
added (commit 344d62fb)
- syntax
- PostgreSQL 14
ALTER SEQUENCE ... OWNER TO CURRENT_ROLE
syntax added (commit 45b98057)
- PostgreSQL 10
- PostgreSQL 9.5
ALTER SEQUENCE ... OWNER TO { CURRENT_USER | SESSION_USER }
syntax added (commit 31eae602)
- PostgreSQL 9.2
ALTER SEQUENCE IF EXISTS ...
syntax added (commit b8a91d9d)
- PostgreSQL 8.4
- PostgreSQL 8.3
ALTER SEQUENCE ... RENAME TO ...
syntax added (commit a5589813)
- PostgreSQL 8.2
ALTER SEQUENCE ... OWNED BY ...
syntax added (commit 2b2a5072)
- PostgreSQL 8.1
ALTER SEQUENCE ... SET SCHEMA ...
syntax added (commit 35508d1c)
- PostgreSQL 7.4
- added (commit 5f65225f)
Examples
Basic usage example for ALTER SEQUENCE
:
postgres=# ALTER SEQUENCE foo_id_seq RENAME TO bar_id_seq; ALTER SEQUENCE
Changing the sequence data type:
postgres=# ALTER SEQUENCE bar_id_seq AS integer; ALTER SEQUENCE
The data type cannot be changed if the sequence's current value exceeds the maximum value of the new data type:
postgres=# CREATE SEQUENCE type_test_seq AS bigint; CREATE SEQUENCE postgres=# ALTER SEQUENCE type_test_seq RESTART 32768; ALTER SEQUENCE postgres=# ALTER SEQUENCE type_test_seq AS smallint; ERROR: RESTART value (32768) cannot be greater than MAXVALUE (32767)
References
- PostgreSQL documentation: ALTER SEQUENCE