PostgreSQL sequences allocate values without promising gapless numbering
Summary
nextval advances a PostgreSQL sequence atomically, so concurrent callers receive distinct allocated values. Allocation is not rolled back merely because the calling transaction aborts. [S1]
Details
currval reports the last value obtained for that sequence in the current session, not the latest value across all sessions. CREATE SEQUENCE makes the generator used by these functions. Since failed transactions and conflicts can consume values, a sequence is not a gapless counter. setval also changes sequence state outside ordinary transaction rollback behavior. [S1, S2]
Claims
- id: C1 claim: "nextval allocates distinct values atomically across concurrent callers." source: S1 evidence: "each will safely receive a distinct sequence value"
- id: C2 claim: "Aborted transactions do not reclaim a nextval allocation." source: S1 evidence: "not reclaimed for re-use if the calling transaction later aborts"
- id: C3 claim: "CREATE SEQUENCE creates a sequence generator." source: S2 evidence: "CREATE SEQUENCE creates a new sequence number generator"
Sources
- [S1] Sequence manipulation functions. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/functions-sequence.html. PostgreSQL; license: https://www.postgresql.org/about/licence/. Accessed 2026-11-05.
- [S2] CREATE SEQUENCE. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/sql-createsequence.html. PostgreSQL; license: https://www.postgresql.org/about/licence/. Accessed 2026-11-05.
Attribution and notices
This note is CC BY 4.0. Source material has been condensed and restated; short evidence excerpts retain their source license.
PostgreSQL Database Management System (also known as Postgres, formerly as Postgres95) Portions Copyright © 1996-2026, The PostgreSQL Global Development Group Portions Copyright © 1994, The Regents of the University of California Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
License: CC-BY-4.0 · Original synthesis by iinventedlight, drafted by Librarian with AI assistance; based on the attributed sources
Cite this
Librarian. n.d.. "PostgreSQL sequences allocate values without promising gapless numbering." iinventedlight Library, iil:note:gfnwrhq9. https://iinventedlight.com/library/notes/postgres-sequences/. License: CC-BY-4.0.
@misc{iil_note_gfnwrhq9,
author = {{Librarian}},
title = {{PostgreSQL sequences allocate values without promising gapless numbering}},
howpublished = {iinventedlight Library},
url = {https://iinventedlight.com/library/notes/postgres-sequences/},
note = {Permanent ID iil:note:gfnwrhq9. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/postgres-sequences.md.