From 5f213c9867cc04d5c09f5bf4749d63113113dafa Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Mon, 16 Nov 2026 14:54:36 -0500 Subject: [PATCH] Throw a more on-point error for partition keys depending on columns. A partition key expression that selects a field of another table's rowtype, such as (('(1)'::othertab).a + length(b)), depends on that column, but ALTER COLUMN TYPE didn't expect any partitioned table to depend on a column and failed with "unexpected object depending on column". Such an ALTER has to be refused, because the partition key can't be rebuilt, so throw a user-facing error instead. ALTER COLUMN SET EXPRESSION tolerates the dependency, since it doesn't change the column's type, as commit 42b041243c00fb20023c983357e7f1ffd3710fff arranged for SQL function bodies. Reported-by: Zheng Hacker Discussion: https://postgr.es/m/19903-06341b8535c78fc1@postgresql.org Backpatch-through: 14 --- src/backend/commands/tablecmds.c | 19 +++++++++++++++++++ .../regress/expected/alter_column_type.out | 16 +++++++++++++++- .../regress/expected/generated_stored.out | 1 - .../regress/expected/generated_virtual.out | 1 - src/test/regress/sql/alter_column_type.sql | 12 ++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index b670092945..f872558137 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -16266,6 +16266,25 @@ RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype, */ Assert(foundObject.objectSubId == 0); } + else if (relKind == RELKIND_PARTITIONED_TABLE) + { + /* + * A partition key expression can depend on a column + * of another table by selecting a field of that + * table's rowtype. We have no way to rebuild a + * partition key, so changing the column's type has to + * be rejected. Changing its generation expression is + * harmless, though. + */ + Assert(foundObject.objectSubId == 0); + if (subtype == AT_AlterColumnType) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot alter type of a column used in a partition key expression"), + errdetail("Partition key of %s depends on column \"%s\".", + getObjectDescription(&foundObject, false), + colName))); + } else { /* Not expecting any other direct dependencies... */ diff --git a/src/test/regress/expected/alter_column_type.out b/src/test/regress/expected/alter_column_type.out index 2388cd0859..6a2c3be37d 100644 --- a/src/test/regress/expected/alter_column_type.out +++ b/src/test/regress/expected/alter_column_type.out @@ -101,8 +101,22 @@ create table atref(b text) partition by range ((('(1)'::attbl).a + length(b))); create table atref_part(b text); alter table atref attach partition atref_part for values from (1) to (2); alter table attbl alter column a type bigint; -- someday this should work -ERROR: unexpected object depending on column: table atref +ERROR: cannot alter type of a column used in a partition key expression +DETAIL: Partition key of table atref depends on column "a". drop table atref, attbl; +-- in PARTITION BY clause, selecting a field of a function's result. The +-- expression doesn't depend on the rowtype, only on the function and the +-- column. +create table attbl(a numeric); +create function atref_func(text) returns attbl immutable language sql + as $$ select row(length($1))::public.attbl $$; +create table atref(b text) partition by range (((atref_func(b)).a)); +alter table attbl alter column a type bigint; -- fails +ERROR: cannot alter type of a column used in a partition key expression +DETAIL: Partition key of table atref depends on column "a". +drop table atref; +drop function atref_func; +drop table attbl; -- in PARTITION BY clause, when the type is the expression's return type, but -- the exprssion doesn't otherwise depend on it CREATE TYPE atcomp AS (a bytea); diff --git a/src/test/regress/expected/generated_stored.out b/src/test/regress/expected/generated_stored.out index d7a18e478f..ca6d7a635c 100644 --- a/src/test/regress/expected/generated_stored.out +++ b/src/test/regress/expected/generated_stored.out @@ -1515,7 +1515,6 @@ CREATE TABLE gtest31_2 (b text) PARTITION BY RANGE ((('(1)'::gtest31_1).a + leng ALTER TABLE gtest31_1 ALTER COLUMN a TYPE bigint; ERROR: cannot alter table "gtest31_1" because table gtest31_2 uses its row type ALTER TABLE gtest31_1 ALTER COLUMN a SET EXPRESSION AS (124); -ERROR: unexpected object depending on column: table gtest31_2 DROP TABLE gtest31_1, gtest31_2; -- triggers CREATE TABLE gtest26 ( diff --git a/src/test/regress/expected/generated_virtual.out b/src/test/regress/expected/generated_virtual.out index b14e10fb5a..0cbbac944b 100644 --- a/src/test/regress/expected/generated_virtual.out +++ b/src/test/regress/expected/generated_virtual.out @@ -1409,7 +1409,6 @@ CREATE TABLE gtest31_2 (b text) PARTITION BY RANGE ((('(1)'::gtest31_1).a + leng ALTER TABLE gtest31_1 ALTER COLUMN a TYPE bigint; ERROR: cannot alter table "gtest31_1" because table gtest31_2 uses its row type ALTER TABLE gtest31_1 ALTER COLUMN a SET EXPRESSION AS (124); -ERROR: unexpected object depending on column: table gtest31_2 DROP TABLE gtest31_1, gtest31_2; -- triggers CREATE TABLE gtest26 ( diff --git a/src/test/regress/sql/alter_column_type.sql b/src/test/regress/sql/alter_column_type.sql index d821126a2b..e00cc8a190 100644 --- a/src/test/regress/sql/alter_column_type.sql +++ b/src/test/regress/sql/alter_column_type.sql @@ -92,6 +92,18 @@ alter table atref attach partition atref_part for values from (1) to (2); alter table attbl alter column a type bigint; -- someday this should work drop table atref, attbl; +-- in PARTITION BY clause, selecting a field of a function's result. The +-- expression doesn't depend on the rowtype, only on the function and the +-- column. +create table attbl(a numeric); +create function atref_func(text) returns attbl immutable language sql + as $$ select row(length($1))::public.attbl $$; +create table atref(b text) partition by range (((atref_func(b)).a)); +alter table attbl alter column a type bigint; -- fails +drop table atref; +drop function atref_func; +drop table attbl; + -- in PARTITION BY clause, when the type is the expression's return type, but -- the exprssion doesn't otherwise depend on it CREATE TYPE atcomp AS (a bytea); -- 2.53.0