Identity column silently stores a wrapped value when its sequence is wider than column

From: Nikhil Sontakke <nikhil(at)planetscale(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Cc: Peter Eisentraut <peter(at)eisentraut(dot)org>
Subject: Identity column silently stores a wrapped value when its sequence is wider than column
Date: 2026-12-04 08:10:04
Message-ID: CA+UBoq1wdW6-h2NyH3Ms1x9qTaTS8XWym5SfLBafNPLfoMN3Qw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

An identity column can store a wrapped-around value with no error once
its sequence's range is wider than the column's type. Here it is on
master:

CREATE TABLE t (id int GENERATED ALWAYS AS IDENTITY);
ALTER SEQUENCE t_id_seq AS bigint MAXVALUE 3000000000;
SELECT setval('t_id_seq', 2147483647);
INSERT INTO t DEFAULT VALUES RETURNING id;
id
-------------
-2147483648

A smallint column wraps to -32768 the same way.

ExecEvalNextValueExpr() casts the int64 from nextval_internal() to the
column type without a range check:

case INT4OID:
*op->resvalue = Int32GetDatum((int32) newval);

That cast is only safe if the sequence's bounds fit the column, and
nothing enforces that. ALTER SEQUENCE ... AS on an identity sequence
accepts any type, and ALTER SEQUENCE ... MAXVALUE accepts anything the
sequence's own type allows.

CREATE TABLE ... LIKE ... INCLUDING IDENTITY gets there without the user
ever changing the sequence's type. transformTableLikeClause() passes
InvalidOid as the type to generateSerialExtraStmts(), and
sequence_options() doesn't return the type, so the copied sequence is
bigint while keeping the int MAXVALUE:

CREATE TABLE src (id int GENERATED ALWAYS AS IDENTITY);
CREATE TABLE cpy (LIKE src INCLUDING IDENTITY);

SELECT seqrelid::regclass, seqtypid::regtype, seqmax FROM pg_sequence;
seqrelid | seqtypid | seqmax
--------------+----------+------------
src_id_seq | integer | 2147483647
cpy_id_seq | bigint | 2147483647

From there, raising MAXVALUE is accepted, and the column wraps as above:

ALTER SEQUENCE cpy_id_seq MAXVALUE 3000000000;
SELECT setval('cpy_id_seq', 2147483647);
INSERT INTO cpy DEFAULT VALUES RETURNING id; -- -2147483648

The mismatch also shows up in dump/restore.

AFAICS, there are multiple ways to fix this:

1. Range-check in ExecEvalNextValueExpr() and raise "integer out of
range" / "smallint out of range". This fixes every route to the
mismatch, including catalogs that already have it, and the cost is
one comparison per nextval.

2. Have ALTER SEQUENCE reject AS and MINVALUE/MAXVALUE on an identity
sequence when the result falls outside the column type's range. It is
probably simplest to check the bounds rather than the type.

3. Make LIKE ... INCLUDING IDENTITY pass the source sequence's seqtypid
to generateSerialExtraStmts(), so the copy matches the original.

IMHO, 1 is enough to stop wrong data from being stored and seems
back-patchable.
2 and 3 keep the catalogs consistent. I can cook up a patch along the
lines of 1 if nobody objects to the approach (or whichever approach we
settle on).

All of the above reproduces identically on master and on 14.25. The
NextValueExpr cast and the LIKE code date from identity columns in v10,
so I expect every supported branch is affected.

Regards,
---
Nikhil Sontakke
PlanetScale

Browse pgsql-hackers by date

  From Date Subject
Next Message Jakub Wartak 2026-12-04 08:12:52 Re: add --transfer-mode to pg_upgrade
Previous Message Tender Wang 2026-12-04 06:58:22 Hash join costing can overestimate inner MCV frequency with extended statistics