pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chao Li <li.evan.chao@gmail.com>
To: jian he <jian.universality@gmail.com>
Cc: David Rowley <dgrowleyml@gmail.com>
Cc: amul sul <sulamul@gmail.com>
Cc: Peter Eisentraut <peter@eisentraut.org>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Re: bug, ALTER TABLE call ATPostAlterTypeCleanup twice for the same relation
Date: Tue, 21 Oct 2025 15:47:18 +0800
Message-ID: <ACAE1349-334E-43C3-AAE6-FC249E3C237D@gmail.com> (raw)
In-Reply-To: <CACJufxH1gCqCXHr7CNZBOUqffrHAC9kPg5wcDKZe4MersbUEYg@mail.gmail.com>
References: <CACJufxHZsgn3zM5g-x7YmtFGzNDnRwR07S+GYfiUs+tZ45MDDw@mail.gmail.com>
	<CAApHDvp7NidshSiw0kqQMgtRupYw7=A5aCM0ubhxC7D0MzvTVg@mail.gmail.com>
	<CACJufxG-tHh1ZtqAP7XTcvuJbKgsJupXBnA6BJGoVKP8pKCq_A@mail.gmail.com>
	<CAEoWx2nEXYMm+UhXaPdBfG2yqMoRMHiujGMrye9mnP6_jP26cQ@mail.gmail.com>
	<CACJufxE10Cfe6-JzPGArL01k0B_n8dacz+fvmUSp9Rcn2Wo+1g@mail.gmail.com>
	<CACJufxH1gCqCXHr7CNZBOUqffrHAC9kPg5wcDKZe4MersbUEYg@mail.gmail.com>



> On Oct 13, 2025, at 16:08, jian he <jian.universality@gmail.com> wrote:
> 
> On Wed, Oct 1, 2025 at 11:04 AM jian he <jian.universality@gmail.com> wrote:
>> 
>> we can simply change from
>> 
>>            if (pass == AT_PASS_ALTER_TYPE || pass == AT_PASS_SET_EXPRESSION)
>>                ATPostAlterTypeCleanup(wqueue, tab, lockmode);
>> 
>> to
>>            if (pass == AT_PASS_SET_EXPRESSION)
>>                ATPostAlterTypeCleanup(wqueue, tab, lockmode);
>>            else if (pass == AT_PASS_ALTER_TYPE &&
>>                     tab->subcmds[AT_PASS_SET_EXPRESSION] == NIL)
>>                ATPostAlterTypeCleanup(wqueue, tab, lockmode);
> 
> That will not work if AT_PASS_ALTER_TYPE triggers the creation of a new
> AT_PASS_SET_EXPRESSION entry.
> For example, consider:
> CREATE TABLE gtest25 (a int, b int GENERATED ALWAYS AS (a * 2) STORED);
> 
> If we alter the type of column "a", the column b’s generation
> expression would need
> to be rebuilt (which is currently not supported).  In that case, the current
> logic would not be able to handle it.
> 
> I think the correct fix is within ATPostAlterTypeCleanup.
> at the end of ATPostAlterTypeCleanup, we already delete these changed
> objects via
> ``performMultipleDeletions(objects, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);``
> 
> we can just list_free these (the below) objects too:
>    tab->changedConstraintOids
>    tab->changedConstraintDefs
>    tab->changedIndexOids
>    tab->changedIndexDefs
>    tab->changedStatisticsOids
>    tab->changedStatisticsDefs
> 
> since this information would become stale if those objects have
> already been dropped.
> <v5-0001-avoid-call-ATPostAlterTypeCleanup-twice.patch>

Now I fully understand what your concern is, so that I understand your solution also. So, I take back my previous reply and re-reply.

Your concern comes from an assumed solution to support altering a column’s type when it has a dependent generated column. Your assumed solution is like:

1. SQL: ALTER TABLE gtest25 ALTER COLUMN a TYPE bigint; # generated column depends on a
2. Before alter column a’s type, b’s expression is remembered
3. After alter column a’s type, ATPostAlterTypeClean() is called as there is no SET EXPRESSION. ATPostAlterTypeClean() will add a SET EXPRESSION subcmd to rebuild b
4. In SET_EXPRESSION PASS, b is recreated
5. ATPostAlterTypeClean() is called again, if we don’t cleanup those tab->changedXXX lists, then remembered objects will be rebuilt again, which may lead to errors. What’s why your solution of cleaning up table->changedXXX works.

In my opinion, in step 2, we don’t have follow the same mechanism to remember generated expressions as constraints. We can directly check if b has SET EXPRESSION cmd, if yes, we don’t need to do anything; otherwise, we get the b’s expression and inject a SET EXPRESSION subcmd for b. In this case, your worried problem will not exist. With this approach, ATPostAlterTypeClean() will always be called only once, so that execution path is simpler.

But anyway, we haven’t decided to support that yet. So for the current bug, your solution of:

>>            if (pass == AT_PASS_SET_EXPRESSION)
>>                ATPostAlterTypeCleanup(wqueue, tab, lockmode);
>>            else if (pass == AT_PASS_ALTER_TYPE &&
>>                     tab->subcmds[AT_PASS_SET_EXPRESSION] == NIL)
>>                ATPostAlterTypeCleanup(wqueue, tab, lockmode);

should work.

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









view thread (14+ messages)  latest in thread

Message-ID: <ACAE1349-334E-43C3-AAE6-FC249E3C237D@gmail.com>
Permalink:  ../ACAE1349-334E-43C3-AAE6-FC249E3C237D@gmail.com/
Also on:    postgresql.org/message-id/ACAE1349-334E-43C3-AAE6-FC249E3C237D@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: li.evan.chao@gmail.com, jian.universality@gmail.com, dgrowleyml@gmail.com, sulamul@gmail.com, peter@eisentraut.org
  Subject: Re: bug, ALTER TABLE call ATPostAlterTypeCleanup twice for the same relation
  In-Reply-To: <ACAE1349-334E-43C3-AAE6-FC249E3C237D@gmail.com>
  References: <CACJufxHZsgn3zM5g-x7YmtFGzNDnRwR07S+GYfiUs+tZ45MDDw@mail.gmail.com> <CAApHDvp7NidshSiw0kqQMgtRupYw7=A5aCM0ubhxC7D0MzvTVg@mail.gmail.com> <CACJufxG-tHh1ZtqAP7XTcvuJbKgsJupXBnA6BJGoVKP8pKCq_A@mail.gmail.com> <CAEoWx2nEXYMm+UhXaPdBfG2yqMoRMHiujGMrye9mnP6_jP26cQ@mail.gmail.com> <CACJufxE10Cfe6-JzPGArL01k0B_n8dacz+fvmUSp9Rcn2Wo+1g@mail.gmail.com> <CACJufxH1gCqCXHr7CNZBOUqffrHAC9kPg5wcDKZe4MersbUEYg@mail.gmail.com> <ACAE1349-334E-43C3-AAE6-FC249E3C237D@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox