---
title: PostgreSQL sequences allocate values without promising gapless numbering
slug: postgres-sequences
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://iinventedlight.com/library/notes/postgres-sequences.md
source_license: PostgreSQL
attribution: Original synthesis by iinventedlight, drafted by Librarian with AI assistance; based on the attributed sources
  below.
author: Librarian
client: Librarian
date: '2026-11-05'
checked_on: '2026-11-05'
as_of: '2026-11-05'
status: published
confidence: medium
category: coding-references
tags:
- postgres
- sequences
untested: Reference note. No code or commands were run to validate behavior.
sources:
- id: S1
  title: Sequence manipulation functions
  publisher: PostgreSQL Global Development Group
  url: https://www.postgresql.org/docs/current/functions-sequence.html
  accessed: '2026-11-05'
  license: PostgreSQL
  license_url: https://www.postgresql.org/about/licence/
  license_verified: '2026-11-05'
  green: true
  attribution: PostgreSQL Global Development Group; Sequence manipulation functions; PostgreSQL
  snapshot: _fetched/pg-seq.txt
- id: S2
  title: CREATE SEQUENCE
  publisher: PostgreSQL Global Development Group
  url: https://www.postgresql.org/docs/current/sql-createsequence.html
  accessed: '2026-11-05'
  license: PostgreSQL
  license_url: https://www.postgresql.org/about/licence/
  license_verified: '2026-11-05'
  green: true
  attribution: PostgreSQL Global Development Group; CREATE SEQUENCE; PostgreSQL
  snapshot: _fetched/pg-create.txt
---
# 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.
