Dropping a composite attribute causes data integrity violations
Hi,
A composite datum records the OID and typmod of its row type and nothing
about the shape it was built with, so every value is read back against
whatever the type looks like currently at read time. Nothing prevents
the below for example:
CREATE TYPE ct AS (a int, b int);
CREATE TABLE t (v ct);
CREATE UNIQUE INDEX t_v ON t (v);
INSERT INTO t VALUES (ROW(1, 2)::ct), (ROW(1, 3)::ct); -- accepted
ALTER TYPE ct DROP ATTRIBUTE b; -- accepted today! this violates UNIQUE
SELECT count(DISTINCT v) FROM t;
1
The rows are on disk and were committed before the ALTER; no cursor or
plan is involved, and the incorrect reading survives a restart.
INSERT INTO t VALUES (ROW(1)::ct);
ERROR: duplicate key value violates unique constraint "t_v"
DETAIL: Key (v)=((1)) already exists.
REINDEX INDEX t_v;
ERROR: could not create unique index "t_v"
DETAIL: Key (v)=((1)) is duplicated.
Worse, a plain pg_dump of that database fails to restore for the
same reason.
The check for this exists. On the same table, before the drop, retyping
the attribute is refused:
ALTER TYPE ct ALTER ATTRIBUTE b TYPE bigint;
ERROR: cannot alter type "ct" because column "t.v" uses it
but the call is gated on a rewrite being queued,
if (tab->newvals != NIL || tab->rewrite > 0)
find_composite_type_dependencies(...);
and a drop produces neither, so it walks past.
The same applies to ALTER TABLE ... DROP COLUMN, since a table's row type
is a composite type too, and there too the retyping form is already
refused.
The attached patch calls the existing function from the drop path, after
the column is checked for droppability and before recursion to
inheritance children, so both calls and every level are covered.
Regression, isolation and pg_upgrade suites pass.
I added new tests and had to modify a couple of existing tests because of
this
behavior change.
Regards,
Nikhil
---
Nikhil Sontakke
PlanetScale Postgres Core Team
Hi,
I added this to the open commitfest at
https://commitfest.postgresql.org/patch/7169/
Also, please find attached v2, which addresses additional required test
changes.
Regards,
Nikhil
On Mon, Aug 17, 2026 at 12:33 PM Nikhil Sontakke <nikhil@planetscale.com>
wrote:
Show quoted text
Hi,
A composite datum records the OID and typmod of its row type and nothing
about the shape it was built with, so every value is read back against
whatever the type looks like currently at read time. Nothing prevents
the below for example:CREATE TYPE ct AS (a int, b int);
CREATE TABLE t (v ct);
CREATE UNIQUE INDEX t_v ON t (v);
INSERT INTO t VALUES (ROW(1, 2)::ct), (ROW(1, 3)::ct); -- acceptedALTER TYPE ct DROP ATTRIBUTE b; -- accepted today! this violates UNIQUE
SELECT count(DISTINCT v) FROM t;
1The rows are on disk and were committed before the ALTER; no cursor or
plan is involved, and the incorrect reading survives a restart.INSERT INTO t VALUES (ROW(1)::ct);
ERROR: duplicate key value violates unique constraint "t_v"
DETAIL: Key (v)=((1)) already exists.REINDEX INDEX t_v;
ERROR: could not create unique index "t_v"
DETAIL: Key (v)=((1)) is duplicated.Worse, a plain pg_dump of that database fails to restore for the
same reason.The check for this exists. On the same table, before the drop, retyping
the attribute is refused:ALTER TYPE ct ALTER ATTRIBUTE b TYPE bigint;
ERROR: cannot alter type "ct" because column "t.v" uses itbut the call is gated on a rewrite being queued,
if (tab->newvals != NIL || tab->rewrite > 0)
find_composite_type_dependencies(...);and a drop produces neither, so it walks past.
The same applies to ALTER TABLE ... DROP COLUMN, since a table's row type
is a composite type too, and there too the retyping form is already
refused.The attached patch calls the existing function from the drop path, after
the column is checked for droppability and before recursion to
inheritance children, so both calls and every level are covered.
Regression, isolation and pg_upgrade suites pass.I added new tests and had to modify a couple of existing tests because of
this
behavior change.Regards,
Nikhil
---
Nikhil Sontakke
PlanetScale Postgres Core Team
Hi Nikhil,
Nikhil Sontakke <nikhil@planetscale.com> wrote:
The attached patch calls the existing function from the drop path, after
the column is checked for droppability and before recursion to
inheritance children, so both calls and every level are covered.
I reviewed v2 on master at 9e17d25e79d. It applies cleanly, builds
without warnings, and make check and contrib/test_decoding pass. The
new tests in alter_table fail on master without the tablecmds.c change,
so they do exercise the fix. Both of your cases, ALTER TYPE ... DROP
ATTRIBUTE with a column of the type and ALTER TABLE ... DROP COLUMN with
a column of the table's row type, are refused, and the stored values
stay distinct.
One gap remains: find_composite_type_dependencies() only looks for
columns of the row type, so the drop still goes through when a value of
the type is stored as a constant inside an expression, with the same
effect you describe for columns. On v2:
create type s1 as (a int, b int);
create view vs1 as select '(1,2)'::s1 = '(1,3)'::s1 as eq; -- f
alter type s1 drop attribute b; -- accepted
select eq from vs1; -- t
A CHECK constraint is worse, because the table stops accepting rows:
create type s3 as (a int, b int);
create table ts3 (i int check ('(1,2)'::s3 <> '(1,3)'::s3));
alter type s3 drop attribute b; -- accepted
insert into ts3 values (1);
ERROR: new row for relation "ts3" violates check constraint "ts3_check"
The same happens with a column DEFAULT, a function parameter default,
and with ALTER TABLE ... DROP COLUMN when the table's row type is used
the same way.
On pgsql-bugs, in a thread about a related problem [1]/messages/by-id/CA+bCEdDvsVApd+AADX=5uyj6Q5C3r5aruWNzOFaYqaFqLwE_5g@mail.gmail.com, I posted a
patch that makes find_composite_type_dependencies() look inside stored
expressions for constants of the row type (pg_attrdef, pg_constraint,
pg_policy, pg_proc, pg_rewrite, pg_statistic_ext, pg_trigger, and index
and partition key expressions). It was written for ALTER COLUMN TYPE,
but because your patch calls the same function from the drop path, the
two compose: with both applied, all six stored-expression cases are
refused and the stored values keep their meaning, while a function that
only takes the type as a parameter, or a view that selects NULL::s1,
is still allowed. The two patches apply cleanly together, and make
check and test_decoding pass with both. Neither covers all of this on
its own.
Testing them together also showed a problem in mine rather than in
yours: for a stand-alone composite type my new messages said
cannot alter table "s1" because rule _RETURN on view vs1 stores a
constant of its row type
where the existing ones, which your patch reaches, correctly say
"cannot alter type". That was already wrong in my v1 on its own (ALTER
TYPE ... ALTER ATTRIBUTE ... TYPE shows it too), and v2, posted on that
thread, fixes it. With your v2 and mine, the drops above report
"cannot alter type".
Heikki, I've added you because d78040a469b touched this call site "to
make backpatching future patches a little easier", in case this
overlaps with what you have in mind.
The SQL for all the cases above, with controls, is attached.
[1]: /messages/by-id/CA+bCEdDvsVApd+AADX=5uyj6Q5C3r5aruWNzOFaYqaFqLwE_5g@mail.gmail.com
Regards,
Manu
Attachments:
composite_drop_scenarios.sql.txttext/plain; charset=US-ASCII; name=composite_drop_scenarios.sql.txtDownload
Hi Manuel,
(cc Heikki, Andres for questions since changes are related to recent
CVE-2026-6460 fixes)
One gap remains: find_composite_type_dependencies() only looks for
columns of the row type, so the drop still goes through when a value of
the type is stored as a constant inside an expression, with the same
effect you describe for columns. On v2:create type s1 as (a int, b int);
create view vs1 as select '(1,2)'::s1 = '(1,3)'::s1 as eq; -- f
alter type s1 drop attribute b; --
accepted
select eq from vs1; -- tA CHECK constraint is worse, because the table stops accepting rows:
create type s3 as (a int, b int);
create table ts3 (i int check ('(1,2)'::s3 <> '(1,3)'::s3));
alter type s3 drop attribute b; --
accepted
insert into ts3 values (1);
ERROR: new row for relation "ts3" violates check constraint
"ts3_check"The same happens with a column DEFAULT, a function parameter default,
and with ALTER TABLE ... DROP COLUMN when the table's row type is used
the same way.On pgsql-bugs, in a thread about a related problem [1], I posted a
patch that makes find_composite_type_dependencies() look inside stored
expressions for constants of the row type (pg_attrdef, pg_constraint,
pg_policy, pg_proc, pg_rewrite, pg_statistic_ext, pg_trigger, and index
and partition key expressions). It was written for ALTER COLUMN TYPE,
but because your patch calls the same function from the drop path, the
two compose: with both applied, all six stored-expression cases are
refused and the stored values keep their meaning, while a function that
only takes the type as a parameter, or a view that selects NULL::s1,
is still allowed. The two patches apply cleanly together, and make
check and test_decoding pass with both. Neither covers all of this on
its own.Testing them together also showed a problem in mine rather than in
yours: for a stand-alone composite type my new messages saidcannot alter table "s1" because rule _RETURN on view vs1 stores a
constant of its row typewhere the existing ones, which your patch reaches, correctly say
"cannot alter type". That was already wrong in my v1 on its own (ALTER
TYPE ... ALTER ATTRIBUTE ... TYPE shows it too), and v2, posted on that
thread, fixes it. With your v2 and mine, the drops above report
"cannot alter type".Heikki, I've added you because d78040a469b touched this call site "to
make backpatching future patches a little easier", in case this
overlaps with what you have in mind.The SQL for all the cases above, with controls, is attached.
[1]
/messages/by-id/CA+bCEdDvsVApd+AADX=5uyj6Q5C3r5aruWNzOFaYqaFqLwE_5g@mail.gmail.com
Thanks for the review and for the scenario script.
I've rebased onto current master, which now includes the
CVE-2026-6460 commits. That changes the picture enough that I've added
Heikki and Andres. v3 is attached.
On master without the v3 patch, all of your scenarios still go through:
N1/N2 (stored columns), S1-S6 (constants in views, CHECK, DEFAULT,
function parameter default, table row type in a view) and both
controls. The original example still ends with a table that has
duplicates under its own unique index and can't be REINDEXed.
Attribute versioning doesn't catch this, and as far as I can tell
that's by design. README.attver.md says a drop changes no version
because the dropped attribute keeps attlen/attalign, so old values stay
safe to deform.
The good news is that the stricter find_composite_type_dependencies()
from Heikki's commits (0b0f5c5af11, 882eb502595, 1ff5607e95f,
3a98781ba12 and friends) makes the gap you pointed out go away. With
the single call from ATExecDropColumn() that v2 already added, all of
N1, N2 and S1-S6 are now refused with specific errors, so I don't think
your separate patch is needed for these cases anymore, at least on master.
The code change in v3 is the same as in v2. What's new are the tests.
Heikki and Andres, I'd like your opinion on this part. Some of the
tests added with the CVE fix store a value and then drop a column of
its row type, which this v3 patch now refuses.
The README already notes that preventing unsafe deforming doesn't
guarantee stored data stays usable. This is a case where the data stays
readable but silently becomes incorrect. Importantly, it makes DROP
consistent
with ALTER ... TYPE, which is already refused in exactly these situations.
So, the open questions for Heikki and Andres are whether they are okay with
refusing the drop via this v3 patch. Plus, there are questions around
back-patching.
Regards,
Nikhil
---
Nikhil Sontakke
PlanetScale
Hi Nikhil,
On Fri, Nov 13, 2026 at 4:46 PM Nikhil Sontakke
<nikhil@planetscale.com> wrote:
On master without the v3 patch, all of your scenarios still go through:
N1/N2 (stored columns), S1-S6 (constants in views, CHECK, DEFAULT,
function parameter default, table row type in a view) and both
controls.
[...]
With the single call from ATExecDropColumn() that v2 already added,
all of N1, N2 and S1-S6 are now refused with specific errors
That reproduces here on master at 77bba5454f9. Without v3 all ten
scenarios in the script are accepted and REINDEX of N1's unique index
fails. With v3 the eight are refused with "cannot alter type/table ...
uses it", as are 18 more stored cases I tried. C1 is still allowed, but
C2 (NULL::c2 in a view) is now refused, as ALTER ... TYPE already is.
Heikki and Andres, I'd like your opinion on this part.
With only the tablecmds.c change, master's alter_table (attdrop_*,
test_typex, test_tbl3), rowtypes (compositetable_drop), oldest_xmin and
009_composite_types fail. Only attdrop_*, compositetable_drop and 009
came with this month's security fixes. With v3's versions of those, a
same-version run of 009 no longer upgrades a stored value whose dropped
attribute held data, and compositetable_drop no longer drops the column
during the UPDATE.
My earlier patch is no longer needed for these cases, on master or on
REL_18_STABLE. There the ten drops also go through without v3, and v3
applies with git am and refuses the same ones.
Regards,
Manu
Hi Manu,
Thanks for testing/reproducing this. V4 attached.
same-version run of 009 no longer upgrades a stored value whose dropped
attribute held data, and
Fixed. When the server refuses the drop, 009 now keeps the datum in a
PL/pgSQL variable across the drop and stores it afterwards, so the
stored datum still holds the dropped attribute's data (29 bytes vs 25
for a fresh ROW(1)).
compositetable_drop no longer drops the column
during the UPDATE.
That can't happen to a stored value any more, so that test now checks
the refusal.
My earlier patch is no longer needed for these cases, on master or on
REL_18_STABLE. There the ten drops also go through without v3, and v3
applies with git am and refuses the same ones.
v3 missed columns dropped by cascading from their datatype:
CREATE DOMAIN d AS int;
CREATE TYPE ct AS (a int, b d);
CREATE TABLE t (c ct UNIQUE);
INSERT INTO t VALUES (ROW(1,2)), (ROW(1,3));
DROP DOMAIN d CASCADE; -- accepted by v3; t now has duplicates
v4 checks such columns in deleteObjectsInList(), after all objects are
deleted, so that anything storing the row type that the same command
drops doesn't count. Checking in RemoveAttributeById() would wrongly
refuse e.g. DROP DOMAIN d CASCADE that also drops an older partitioned
table storing ct, since deletion goes in reverse OID order.
v4 also applies cleanly to REL_18_STABLE and passes there, tests
included. Again, whether to back-patch is the committers' decision.
Regards,
Nikhil
---
Nikhil Sontakke
PlanetScale
Hello
On Mon, 16 Nov 2026, Nikhil Sontakke <nikhil.sontakke@enterprisedb.com> wrote:
v3 missed columns dropped by cascading from their datatype:
This new check can cause temp tablespace cleanup to fail:
CREATE DOMAIN pg_temp.d AS int;
CREATE TYPE ct AS (a int, b pg_temp.d);
CREATE TABLE t (c ct UNIQUE);
INSERT INTO t VALUES (ROW(1,2)), (ROW(1,3));
DISCARD TEMP;
-- ERROR: cannot alter type "ct" because column "t.c" uses it
At session exit we even get a FATAL in the log, and it also causes issues for the next session that gets the same tablespace.
With a temp table instead of a domain, it breaks autovacuum completely for the database:
LOG: autovacuum: dropping orphan temp table "postgres.pg_temp_0.tt"
ERROR: cannot alter type "ct" because column "p.c" uses it
And even in the previous version, BEGIN ATOMIC functions using the row type now block DROP COLUMN, even with CASCADE:
CREATE TABLE person (id int, first text, middle text, last text);
CREATE FUNCTION full_name(person) RETURNS text
BEGIN ATOMIC SELECT $1.first || ' ' || $1.last; END;
ALTER TABLE person DROP COLUMN middle;
ALTER TABLE person DROP COLUMN middle CASCADE; -- also errors
Note that the function doesn't use middle, so I think even the first error is a bug, but the second with CASCADE is definitely: without the patch, if the body does use middle, the alter drops the function. With the patch, it errors instead.
Hi Zsolt,
Thanks for the review. v5 attached.
This new check can cause temp tablespace cleanup to fail:
This needs a permanent composite type with an attribute of a temporary
type, stored in a permanent table, which is quite contrived, who'd do it?
:-D.
Still, internal deletions like temp namespace cleanup and autovacuum's
orphan
temp table removal must not fail, and the fix is small: v5 skips the
check for PERFORM_DELETION_INTERNAL deletions, so DISCARD TEMP, session
exit, ON COMMIT DROP and autovacuum behave as before.
And even in the previous version, BEGIN ATOMIC functions using the row
type now block DROP COLUMN, even with CASCADE:
Agreed, both are bugs. find_composite_type_dependencies() was written
for ALTER COLUMN TYPE, where any use of the type in a SQL body is a
problem. For a drop, only values of the type held in the body matter;
references to the type or its fields don't, and a use of the dropped
column is already a column dependency.
v5 adds find_composite_type_dependencies_for_drop(), which differs only
in that a function's argument defaults and SQL body block the drop just
if they contain a constant or ROW() constructor of the type. Your
example now behaves as on master: dropping an unused column works, and
dropping a used one gives the usual dependency error, or drops the
function with CASCADE. A body like SELECT '(1,2)'::t = '(1,3)'::t still
blocks it. Views and other expressions are unchanged, and already only
block for such values.
v5 also runs the check for every dropped column after all deletions,
instead of up front in ATExecDropColumn(), so that a function or view
holding such a value no longer blocks a drop whose CASCADE removes it.
The restriction is now documented under ALTER TABLE ... DROP COLUMN and
ALTER TYPE ... DROP ATTRIBUTE.
All of these, including a ROW() constructor in a SQL body, are in the
regression tests. Full meson suite passes on master; v5 also applies
cleanly to REL_18_STABLE.
Regards,
Nikhil
---
Nikhil Sontakke
PlanetScale