Skip to content

CREATE, ALTER and DROP SERIAL

View as Markdown

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 DBA can change or drop a serial. A serial owned by PUBLIC can be changed by everyone.
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: MINVALUE for an ascending serial, MAXVALUE for 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) or MAXVALUE (descending). With NOCYCLE (the default), asking for a number past the limit fails with Next 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, comment
FROM 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.

serial_name.NEXT_VALUE -- or serial_name.NEXTVAL
serial_name.CURRENT_VALUE -- or serial_name.CURRVAL
  • NEXT_VALUE advances the serial and returns the new number. The first call after CREATE SERIAL returns 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; -- 10004

Each 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 as s.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 as s.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); -- 101
SELECT SERIAL_NEXT_VALUE(batch_seq, 10); -- 110: numbers 101 to 110 are yours
SELECT SERIAL_NEXT_VALUE(batch_seq, 10); -- 120: numbers 111 to 120
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 [IF EXISTS] serial_name
DROP SERIAL IF EXISTS batch_seq;