1)Create Sequence:
CREATE [ TEMPORARY | TEMP ] SEQUENCE [ IF NOT EXISTS ]name
[ ASdata_type
] [ INCREMENT [ BY ]increment
] [ MINVALUEminvalue
| NO MINVALUE ] [ MAXVALUEmaxvalue
| NO MAXVALUE ] [ START [ WITH ]start
] [ CACHEcache
] [ [ NO ] CYCLE ] [ OWNED BY {table_name
.column_name
| NONE } ] CREATE SEQUENCE serial START 101;
2) Select the next number from this sequence:
SELECT nextval('serial'); nextval --------- 101 SELECT nextval('serial'); nextval --------- 102
3)Sequence Manipulation Functions:
-- Return value most recently obtained withnextval
select currval('serial') ; ----- 102 (1 row) --Return value most recently obtained withnextval
for any sequence select lastval(); lastval --------- 102 (1 row) -- Advance sequence and return new value select nextval('serial') ; ----- 103 (1 row) -- Set sequence's current value select setval('serial', 1); setval -------- 1 (1 row) --setval SELECT setval('serial', 42); Next nextval will return 43 SELECT setval('serial', 42, true); Same as above SELECT setval('serial', 42, false); Next nextval will return 42
4) Sequence naming:
nextval('serial') operates on sequence serial nextval('SERIAL') operates on sequence serial nextval('"Serial"') operates on sequence Serial
5) Alter sequence:
ALTER SEQUENCE [ IF EXISTS ]name
[ ASdata_type
] [ INCREMENT [ BY ]increment
] [ MINVALUEminvalue
| NO MINVALUE ] [ MAXVALUEmaxvalue
| NO MAXVALUE ] [ START [ WITH ]start
] [ RESTART [ [ WITH ]restart
] ] [ CACHEcache
] [ [ NO ] CYCLE ] [ OWNED BY {table_name
.column_name
| NONE } ] ALTER SEQUENCE [ IF EXISTS ]name
OWNER TO {new_owner
| CURRENT_USER | SESSION_USER } ALTER SEQUENCE [ IF EXISTS ]name
RENAME TOnew_name
ALTER SEQUENCE [ IF EXISTS ]name
SET SCHEMAnew_schema
6) Grant Privileges On Sequence:
GRANT { { USAGE | SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] } ON { SEQUENCEsequence_name
[, ...] | ALL SEQUENCES IN SCHEMAschema_name
[, ...] } TOrole_specification
[, ...] [ WITH GRANT OPTION ]
whererole_specification
can be: [ GROUP ]role_name
| PUBLIC | CURRENT_USER | SESSION_USER
7)Drop sequence:
DROP SEQUENCE [ IF EXISTS ] name
[, ...] [ CASCADE | RESTRICT ]
DROP SEQUENCE serial;
8) Views about sequences:
PG_SEQUENCES:
Name | Type | References | Description |
---|---|---|---|
schemaname | name |
| Name of schema containing sequence |
sequencename | name |
| Name of sequence |
sequenceowner | name |
| Name of sequence’s owner |
data_type | regtype |
| Data type of the sequence |
start_value | bigint | Start value of the sequence | |
min_value | bigint | Minimum value of the sequence | |
max_value | bigint | Maximum value of the sequence | |
increment_by | bigint | Increment value of the sequence | |
cycle | boolean | Whether the sequence cycles | |
cache_size | bigint | Cache size of the sequence | |
last_value | bigint | The last sequence value written to disk. If caching is used, this value can be greater than the last value handed out from the sequence. Null if the sequence has not been read from yet. Also, if the current user does not have USAGE or SELECT privilege on the sequence, the value is null. |
PG_SEQUENCE:
pg_sequence
Columns
Name | Type | References | Description |
---|---|---|---|
seqrelid |
oid |
|
The OID of the pg_class entry for this sequence |
seqtypid |
oid |
|
Data type of the sequence |
seqstart |
int8 |
Start value of the sequence | |
seqincrement |
int8 |
Increment value of the sequence | |
seqmax |
int8 |
Maximum value of the sequence | |
seqmin |
int8 |
Minimum value of the sequence | |
seqcache |
int8 |
Cache size of the sequence | |
seqcycle |
bool |
Whether the sequence cycles |