BUG #19903: ALTER COLUMN TYPE: "unexpected object depending on column" internal error via partition key

Поиск
Список
Период
Сортировка
Искать

BUG #19903: ALTER COLUMN TYPE: "unexpected object depending on column" internal error via partition key

От:
PG Bug reporting form <noreply@postgresql.org>
Дата:
The following bug has been logged on the website:

Bug reference:      19903
Logged by:          Zheng Hacker
Email address:      hackerzheng666@gmail.com
PostgreSQL version: 19rc2
Operating system:   Linux x86_64 (Ubuntu 22.04)
Description:        

Tested on git master (20devel, commit 70c4a57b64d3; the code in question is
unchanged at master 81b2179 as of 2026-11-14). Not tested on 19rc2 or older
branches; the version selected in the form is only the closest listed one.

ALTER COLUMN TYPE on a column of a table that is also used as a composite
type raises an internal (XX000) error if the partition key expression of
another table selects a field of that composite type:

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:  unexpected object depending on column: table atref

The same dependency through an index expression or a CHECK constraint gives
the expected user error:

  index expression : ERROR:  cannot alter table "attbl" because index
atref_1_numeric_length_b_idx uses its row type
  CHECK constraint : ERROR:  cannot alter table "attbl" because constraint
atref_check on table atref uses its row type

In ATExecAlterColumnType() (tablecmds.c), the scan over the column's
pg_depend entries has a "case RelationRelationId" branch that handles
indexes and sequences and raises elog(ERROR, "unexpected object depending on
column: %s") for every other relation kind. A partitioned table whose
partition key expression selects a field of the altered composite type
depends on that column through its pg_depend entry for the relation itself,
so it ends up in that branch.

Expected: a user-facing error in the same style as the index and constraint
cases, for example "cannot alter table "attbl" because partition key of
table "atref" uses its row type".

(Found with a fuzzer; reduced by hand. Checked on current master only. A
bare partition key (('(1)'::attbl).a) is rejected earlier with "cannot use
constant expression as partition key", hence the "+ length(b)".)

Related: BUG #19559 reports the same "unexpected object depending on column"
elog from the same dependency scan for a property graph; this report is the
partition-key-expression case, which falls in the RelationRelationId branch.




Re: BUG #19903: ALTER COLUMN TYPE: "unexpected object depending on column" internal error via partition key

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
Вложения:
On Mon, Nov 16, 2026 at 10:55 AM PG Bug reporting form
 wrote:
> ALTER COLUMN TYPE on a column of a table that is also used as a composite
> type raises an internal (XX000) error if the partition key expression of
> another table selects a field of that composite type:
>
> 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:  unexpected object depending on column: table atref

Yeah, that's not great. Here's what my LLM thinks:

RememberAllDependentForRebuilding() expects the only relations that
depend directly on a column to be indexes and sequences. A partition
key can depend on one too, because selecting a field from a composite
value records a dependency on that column. That's been so since 2017,
so all supported branches have this.

Just ignoring partitioned tables there won't do, though. In the
reporter's example the rowtype check that produces the index and CHECK
messages runs later and would still reject the ALTER. But a key that
gets at the field through a function result doesn't use the rowtype at
all:

    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;

With partitioned tables skipped, that ALTER succeeds, and afterwards an
INSERT into atref from a new session fails with "attribute 1 has wrong
type". We can't rebuild a partition key, so the column dependency is
all that stands in the way, and the error belongs here.

The attached reports "cannot alter type of a column used in a partition
key expression", with a detail naming the partitioned table. I prefer
that to the "uses its row type" wording suggested in the report because
it's also accurate for the function case. As 42b041243c (Tom Lane,
2024-04-28) did for SQL function bodies, it lets ALTER COLUMN SET
EXPRESSION through, since that can't change the column's type.

Since this only changes cases that fail today, I think it should go
back to 14. Branches before 17 have no SET EXPRESSION, so that part
drops out there.

-- 
Robert Haas
Databricks

Re: BUG #19903: ALTER COLUMN TYPE: "unexpected object depending on column" internal error via partition key

От:
Andres Freund <andres@anarazel.de>
Дата:
Hi,

On 2026-11-15 04:47:59 +0000, PG Bug reporting form wrote:
> ALTER COLUMN TYPE on a column of a table that is also used as a composite
> type raises an internal (XX000) error if the partition key expression of
> another table selects a field of that composite type:

> 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:  unexpected object depending on column: table atref
>
> (Found with a fuzzer; reduced by hand. Checked on current master only. A
> bare partition key (('(1)'::attbl).a) is rejected earlier with "cannot use
> constant expression as partition key", hence the "+ length(b)".)

Maybe I'm just tired, but that's not something anybody actually cares about
right?  I guess we can dress this up to have a nicer error message, but also,
who cares?

I think we want to do something here, it might be better to focus on rejecting
such absurd partition by clauses...

Greetings,

Andres Freund


Re: BUG #19903: ALTER COLUMN TYPE: "unexpected object depending on column" internal error via partition key

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Mon, Nov 16, 2026 at 5:53 PM Andres Freund  wrote:
> Maybe I'm just tired, but that's not something anybody actually cares about
> right?  I guess we can dress this up to have a nicer error message, but also,
> who cares?
>
> I think we want to do something here, it might be better to focus on rejecting
> such absurd partition by clauses...

The LLM-generated patch I attached before gives a nice error message
for ALTER COLUMN TYPE, which I agree is nice but not critical, but it
also makes ALTER COLUMN .. SET EXPRESSION succeed instead of failing.
If that case is safe, we probably want to allow it.

-- 
Robert Haas
Databricks


Re: BUG #19903: ALTER COLUMN TYPE: "unexpected object depending on column" internal error via partition key

От:
Andres Freund <andres@anarazel.de>
Дата:
Hi,

On 2026-11-16 18:01:01 -0500, Robert Haas wrote:
> On Mon, Nov 16, 2026 at 5:53 PM Andres Freund  wrote:
> > Maybe I'm just tired, but that's not something anybody actually cares about
> > right?  I guess we can dress this up to have a nicer error message, but also,
> > who cares?
> >
> > I think we want to do something here, it might be better to focus on rejecting
> > such absurd partition by clauses...
>
> The LLM-generated patch I attached before gives a nice error message
> for ALTER COLUMN TYPE, which I agree is nice but not critical, but it
> also makes ALTER COLUMN .. SET EXPRESSION succeed instead of failing.
> If that case is safe, we probably want to allow it.

I am very hesitant to expand what is allowed around any of this without very
careful analysis. It's not at all hard to imagine that allowing such a change
would add security issues. In this case it also might be breaking
partitioning, afaict such a SET EXPRESSION, were it allowed, could change what
partition a row belongs in, no?

Even if it is correct, I would vote strongly against allowing more in the back
branches.


Generally around all of this allowing more IMO is an anti-goal.  The amount of
useless corner-case crap that we allow has cost me way way too much time over
the last months.

Nobody can tell me that it's useful to have partitioning expressions of this
kind:

-- Check if the rowtype is used in PARTITION BY expression of another table.
CREATE TABLE gtest31_1 (a int GENERATED ALWAYS AS (123) VIRTUAL);
CREATE TABLE gtest31_2 (b text) PARTITION BY RANGE ((('(1)'::gtest31_1).a + length(b)));


Greetings,

Andres


FAQ