Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

CREATE SEQUENCE

Creates a catalog relation of kind sequence:

CREATE SEQUENCE [IF NOT EXISTS] name
  [START [WITH] value] [INCREMENT [BY] value]
  [MINVALUE value] [MAXVALUE value] [CYCLE | NO CYCLE] [CACHE value]
ALTER SEQUENCE name RESTART [WITH] value
ALTER SEQUENCE name CACHE value

Sequences can be ascending or descending: a positive increment generates increasing values and a negative increment generates decreasing values. Without explicit bounds, descending sequences use MAXVALUE -1, MINVALUE -9223372036854775808, and start at -1; ascending sequences start at 1. The nextval, currval, and setval functions are available, and bounds and cycling are persisted in the catalog.

CACHE n reserves n values per block. The default is 1 for explicit sequences and 32 for sequences created automatically by serial/bigserial. Reserved values may be skipped after a crash or restart; ROLLBACK also does not return consumed values. Change the cache with ALTER SEQUENCE name CACHE n.