Skip to content

[Bug]: nextval() inside a rolled-back transaction re-issues the same value (sequence advancement is transactional) #30896

Closed as not planned

Description

@XuPeng-SH

Summary

nextval() inside a transaction that rolls back reuses the issued value. The same value is handed out twice, so "unique id from a sequence" can collide.

Repro (main@3ad9adbb4c, fresh instance)

create sequence s2 increment by 1 start with 1;
select nextval('s2');            -- 1

begin;
select nextval('s2');            -- 2   (issued inside the txn)
rollback;

select nextval('s2');            -- 2   <-- SAME VALUE RE-ISSUED

Second observation (committed path behaves monotonically, so this is specifically rollback):

begin; select nextval('s2'); commit;   -- 3
select nextval('s2');                  -- 4
alter sequence s2 increment by 5;
select nextval('s2');                  -- 9   (4 + 5, consistent)

Expected

PostgreSQL (and any sequence semantics MO's nextval mirrors) treats sequence advancement as non-transactional: a rolled-back transaction does not return the value, so nextval never repeats. MySQL has no sequences, so there is no MySQL fallback to match.

If MO intends transactional sequences instead, that must be documented, because the current behaviour silently produces duplicate values for an idiom (nextval for surrogate keys) where uniqueness is the entire point.

Impact

  • Two rows created in different transactions can receive the same primary key, failing later with 1062 or, worse, silently keying different rows to the same value when the column is not constrained.
  • Any application that retries a rolled-back transaction will hit this immediately.

Notes

  • Reproduced twice on a fresh data directory; not environment-dependent.
  • Related area: 309f618995 (preserve sequence issued state through cross-session ALTER SEQUENCE) — that fix preserves state across ALTER, but the rollback path still rewinds the issued counter.

Verification suggestion

begin; nextval; rollback; then compare the two values: they must differ. Also worth a BVT that a rolled-back nextval never collides with a committed one.

Activity

  1. added
    kind/bug
    on Oct 23, 2026
  2. XuPeng-SH commented on Oct 24, 2026

    @XuPeng-SH
    ContributorAuthor

    Tracking consolidated into #30359. The canonical issue now includes this report's independent scope and acceptance criteria. This is administrative consolidation, not a claim that the defect is fixed; the original reproduction and discussion are preserved here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions