What is a sequence in PostgreSQL?

Answer

A sequence is a database object that generates unique numeric values, typically used for auto-incrementing primary keys. SERIAL creates an implicit sequence; you can also create one explicitly: CREATE SEQUENCE my_seq START 1 INCREMENT 1;. Get next value: SELECT nextval('my_seq');. Current value: SELECT currval('my_seq'); (only valid after calling nextval in the same session). Reset: ALTER SEQUENCE my_seq RESTART WITH 1;. Sequences are non-transactional — even if a transaction rolls back, the sequence value is not rolled back, which means gaps in IDs are normal and expected. This prevents bottlenecks from locking. The modern alternative to SERIAL is GENERATED ALWAYS AS IDENTITY.