Re: bug, ALTER TABLE call ATPostAlterTypeCleanup twice for the same relation

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: David Rowley <dgrowleyml(at)gmail(dot)com>, amul sul <sulamul(at)gmail(dot)com>, Peter Eisentraut <peter(at)eisentraut(dot)org>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: bug, ALTER TABLE call ATPostAlterTypeCleanup twice for the same relation
Date: 2026-08-03 06:00:33
Message-ID: D5AB9959-35FA-4DF0-B29F-FC9B34506B00@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Aug 2, 2026, at 19:52, jian he <jian(dot)universality(at)gmail(dot)com> wrote:
>
> Hi.
>
> ATPostAlterTypeCleanup->performMultipleDeletions drops the
> objects referenced by changedConstraintOids, changedIndexOids, and similar
> fields of the AlteredTableInfo, it makes sense to set those pointers
> to NULL afterward.
> The attached patch implements this approach.
>
> Another reason why I prefer this approach:
> Imagine some other random ALTER TABLE command that also needs to
> rebuild whole-row dependent objects for the table,
> then ATPostAlterTypeCleanup would also need to be called for that
> AlterTablePass.
> Ideally, we should be able to call ATPostAlterTypeCleanup for any kind
> of AlterTablePass.
>
> The commit message is below:
> ---------------------------------
> ATPostAlterTypeCleanup() is called twice when a single ALTER TABLE contains both
> ALTER COLUMN SET DATA TYPE and ALTER COLUMN SET EXPRESSION.
> The first call drops the objects listed in tab->changedConstraintOids,
> tab->changedIndexOids and tab->changedStatisticsOids via
> performMultipleDeletions(), but left those lists untouched. The second call
> would drop the same OIDs again, failing with errors like
> "cache lookup failed".
>
> Fix by resetting the changed-object lists (and the replica identity and CLUSTER
> index markings, which would otherwise queue duplicate subcommands) at the end of
> ATPostAlterTypeCleanup(), so the second invocation only processes objects
> registered by the SET EXPRESSION pass.
> --------------------------------
>
>
>
> --
> jian
> https://www.enterprisedb.com/
> <v6-0001-Fix-ALTER-TABLE-when-ALTER-TYPE-and-SET-EXPRESSION-are-used-toget.patch>

Since my last round of review of this patch, I have done a lot of work on ALTER TABLE and thus gained a better understanding of the ALTER TABLE infra. I now agree that your approach (as in v6) is correct. The ALTER TYPE pass remembers and drops its dependencies, and SET EXPRESSION remembers and drops any additional ones.

A small comment on the commit message:
```
The first call drops the objects … but left those lists untouched.
```

I think “left” should be “leaves” to keep the tense consistent with “drops”.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2026-08-03 06:07:14 Re: POC: PLpgSQL FOREACH IN JSON ARRAY
Previous Message Siddharth Kothari 2026-08-03 06:00:21 Re: [PATCH] Add RetrieveInstrumentation hook for CustomScan providers