DRAFTThis page is not published. Only visible in development mode.

Sequences

A sequence is a monotonic counter the engine maintains for ID generation. You rarely create one directly: declaring a column with { auto_increment } gives that column its own sequence, and the engine draws from it on every insert.

Auto-increment columns

Any integer column can auto-increment. Writers simply omit the column and each row receives the next value - no coordination, no SELECT MAX, no application-side counters. Run the snippets on this page in order:

$ Auto-Increment Assigns IDs at Insert
$ ctrl+enter to run

Repositioning a sequence

The sequence behind a column is addressed as namespace::table::column. alter sequence ... set value N repositions it; the next insert draws N + 1. This is how imports preserve existing IDs and how ID ranges are carved out:

$ Reposition a Sequence with ALTER SEQUENCE
$ ctrl+enter to run

Reading generated IDs back

The row's generated ID is often needed immediately - to reference it, to return it to a client. returning hands it back from the insert itself, in the same statement:

$ Read Generated IDs Back with RETURNING
$ ctrl+enter to run
Where else sequences appear
The catalog uses sequences internally to assign IDs to every object it manages - the IDs you see in DDL results and in system catalog queries come from the same machinery.