CREATE, ALTER and DROP SERIAL
A serial (a sequence in other databases) generates unique numbers, such as invoice or ticket numbers. Unlike an AUTO_INCREMENT column, a serial is an object of its own: it is not tied to a table and can feed several tables. Numbers are handed out safely to many sessions at once. Do not expect a serial to be free of gaps: cached numbers, for example, are lost when the server stops.
- Any user, including
PUBLIC, can create a serial. Once it exists, every user can read its numbers. - Only its owner and
DBAcan change or drop a serial. A serial owned byPUBLICcan be changed by everyone.
CREATE SERIAL
Section titled “CREATE SERIAL”CREATE SERIAL serial_name [START WITH initial_value] [INCREMENT BY interval] [MINVALUE min | NOMINVALUE] [MAXVALUE max | NOMAXVALUE] [CYCLE | NOCYCLE] [CACHE cached_count | NOCACHE] [COMMENT 'comment']- serial_name: see Identifiers.
- START WITH: the first number. Default:
MINVALUEfor an ascending serial,MAXVALUEfor a descending one. - INCREMENT BY: the step, any integer except 0 (default 1). A negative step makes a descending serial. Its absolute value must be smaller than
MAXVALUE - MINVALUE. - MINVALUE / NOMINVALUE: the lowest number.
NOMINVALUE(the default) means 1 for an ascending serial and -1036 for a descending one. - MAXVALUE / NOMAXVALUE: the highest number.
NOMAXVALUE(the default) means 1037 for an ascending serial and -1 for a descending one. - CYCLE: after the last number, start again from
MINVALUE(ascending) orMAXVALUE(descending). With NOCYCLE (the default), asking for a number past the limit fails withNext value exceeds MAX(MIN) value. - CACHE n: keep the next n numbers in memory, which is faster under heavy use. Numbers still in the cache when the server stops are lost, leaving a gap. NOCACHE (the default) writes every number to disk.
- COMMENT: a description of the serial.
All values can have up to 38 digits.
CREATE SERIAL invoice_seq START WITH 10000 INCREMENT BY 2 MAXVALUE 20000 CACHE 3 COMMENT 'invoice numbers';
SELECT name, current_val, increment_val, max_val, min_val, cyclic, started, cached_num, commentFROM db_serial WHERE name = 'invoice_seq';| name | current_val | increment_val | max_val | min_val | cyclic | started | cached_num | comment |
|---|---|---|---|---|---|---|---|---|
| invoice_seq | 10000 | 2 | 20000 | 10000 | 0 | 0 | 3 | invoice numbers |
The db_serial catalog view lists all serials. It is read-only; use ALTER SERIAL to change a serial.
Using a serial
Section titled “Using a serial”serial_name.NEXT_VALUE -- or serial_name.NEXTVALserial_name.CURRENT_VALUE -- or serial_name.CURRVAL- NEXT_VALUE advances the serial and returns the new number. The first call after
CREATE SERIALreturns the start value. - CURRENT_VALUE returns the current number without advancing.
CREATE TABLE invoice (invoice_no INT PRIMARY KEY, order_id INT NOT NULL);INSERT INTO invoice VALUES (invoice_seq.NEXT_VALUE, 455);INSERT INTO invoice VALUES (invoice_seq.NEXT_VALUE, 456);INSERT INTO invoice VALUES (invoice_seq.NEXTVAL, 457);
SELECT * FROM invoice ORDER BY invoice_no;| invoice_no | order_id |
|---|---|
| 10000 | 455 |
| 10002 | 456 |
| 10004 | 457 |
SELECT invoice_seq.CURRENT_VALUE; -- 10004Each reference to NEXT_VALUE advances the serial, even within one statement: SELECT s.NEXT_VALUE, s.NEXT_VALUE returns two different numbers.
A countdown from 3 to 1 that starts over:
CREATE SERIAL countdown START WITH 3 INCREMENT BY -1 MINVALUE 1 MAXVALUE 3 CYCLE;-- successive countdown.NEXT_VALUE calls return 3, 2, 1, 3, 2, ...SERIAL_CURRENT_VALUE and SERIAL_NEXT_VALUE
Section titled “SERIAL_CURRENT_VALUE and SERIAL_NEXT_VALUE”SERIAL_CURRENT_VALUE(serial_name)SERIAL_NEXT_VALUE(serial_name, count)Both return NUMERIC(38,0).
SERIAL_CURRENT_VALUE(s)is the same ass.CURRENT_VALUE.SERIAL_NEXT_VALUE(s, count)reserves count numbers in one call and returns the last of them.SERIAL_NEXT_VALUE(s, 1)is the same ass.NEXT_VALUE.
Reserving a block of numbers at once is much faster than calling NEXT_VALUE repeatedly. The first reserved number is last − (count − 1) × increment:
CREATE SERIAL batch_seq START WITH 101;
SELECT SERIAL_CURRENT_VALUE(batch_seq); -- 101SELECT SERIAL_NEXT_VALUE(batch_seq, 10); -- 110: numbers 101 to 110 are yoursSELECT SERIAL_NEXT_VALUE(batch_seq, 10); -- 120: numbers 111 to 120ALTER SERIAL
Section titled “ALTER SERIAL”ALTER SERIAL serial_name [INCREMENT BY interval] [START WITH initial_value] [MINVALUE min | NOMINVALUE] [MAXVALUE max | NOMAXVALUE] [CYCLE | NOCYCLE] [CACHE cached_count | NOCACHE] [COMMENT 'comment']The options have the same meaning as in CREATE SERIAL. After START WITH, the next NEXT_VALUE returns the new start value:
ALTER SERIAL batch_seq START WITH 500 INCREMENT BY 5;SELECT batch_seq.NEXT_VALUE; -- 500
ALTER SERIAL batch_seq CACHE 20;ALTER SERIAL batch_seq NOCACHE COMMENT 'batches';DROP SERIAL
Section titled “DROP SERIAL”DROP SERIAL [IF EXISTS] serial_nameDROP SERIAL IF EXISTS batch_seq;See also
Section titled “See also”- AUTO_INCREMENT
- System catalog: the
db_serialview