Report
ALTER COLUMN TYPE, ALTER TYPE ... ALTER ATTRIBUTE ... TYPE and ALTER COLUMN SET EXPRESSION fail with internal error "unexpected object depending on column" when another table's partition key selects a field of the altered rowtype
- Severity
- 35
- Blamed commit
- f3ea3e3e820b6b7512e48660bf984603418d53ff
On 10 branches
REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
When a partitioned table's partition key expression selects a field of a table's rowtype (or of a standalone composite type), the partitioned table records a pg_depend entry on that column. ALTER TABLE ... ALTER COLUMN TYPE, ALTER TYPE ... ALTER ATTRIBUTE ... TYPE, and ALTER COLUMN ... SET EXPRESSION then fail with an internal XX000 error instead of a user-facing one. The field may be reached through a rowtype constant or through a function returning the rowtype; the latter is not caught by any later rowtype check either.
Reproduction
create table attbl(a numeric);
create table atref(b text)
partition by range ((('(1)'::attbl).a + length(b)));
alter table attbl alter column a type bigint;
-- ERROR: XX000: unexpected object depending on column:
-- table atref
Function-mediated variant (no rowtype constant):
create table attbl(a numeric);
create function mk(text) returns attbl immutable language sql
as $$ select row(length($1))::public.attbl $$;
create table atref(b text) partition by range (((mk(b)).a));
alter table attbl alter column a type bigint;
-- same error
Standalone composite type:
create type ct as (a numeric);
create function mk(text) returns ct immutable language sql
as $$ select row(length($1))::public.ct $$;
create table atref(b text) partition by range (((mk(b)).a));
alter type ct alter attribute a type bigint;
-- same error
SET EXPRESSION on a generated column:
create table attbl(x int, a int generated always as (x*2) stored);
create function mk(text) returns attbl immutable language sql
as $$ select row(length($1), 0)::public.attbl $$;
create table atref(b text)
partition by range (((mk(b)).a + length(b)));
alter table attbl alter column a set expression as (x*3);
-- same error
Expected: a user-facing error (e.g. "cannot alter type of a column used in a partition key expression"), as for indexes and CHECK constraints that use the rowtype; SET EXPRESSION, which cannot change the column type, could be allowed.
Analysis
RememberAllDependentForRebuilding() in src/backend/commands/tablecmds.c handles RelationRelationId dependents only for indexes and sequences and raises elog(ERROR, "unexpected object depending on column") for any other relkind. A partitioned table whose partition key contains a FieldSelect on the altered rowtype has such a dependency: find_expr_references_walker() records a column dependency for FieldSelect since commit f3ea3e3e820b6b7512e48660bf984603418d53ff ("Fix some oversights in expression dependency recording"), which is when this path became reachable. For tables with storage, find_composite_type_dependencies() runs only later at rewrite time, and it never sees the function-mediated form, so the column dependency is the only guard; the error therefore belongs in RememberAllDependentForRebuilding().
Notes
Tested on master 7fc2f0423320e02beb8c44b863f1827604c47c75. Related to the pgsql-bugs thread for BUG #19903, where a patch has been proposed. The same elog is reached for property graphs via a different branch (BUG #19559).