Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v35LY-00BC2C-DC for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Sep 2025 04:18:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1v35LW-00GgzO-4y for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Sep 2025 04:18:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v35LV-00GgzD-JK for pgsql-hackers@lists.postgresql.org; Mon, 29 Sep 2025 04:18:42 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v35LT-000RVR-0F for pgsql-hackers@postgresql.org; Mon, 29 Sep 2025 04:18:41 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-271d1305ad7so55067205ad.2 for ; Sun, 28 Sep 2025 21:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759119519; x=1759724319; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=d0HuPrzRcr+V3kcE8yiDGocxKo7EJdhh045GmqgaR/I=; b=MwixJP0uBy75Z9oP51NuS9w/d9kChWkIT3LWalVAgCACIN3HmoZWHlQE5gNWFR5EEy rtBd9A90yALUMSUtobQk6nuP3QO4CXnLO/8HjzeO+wSTG2+yEFB9HqKhczJxcPZjg6Li CwjbMCSKOAjHJK2xLrsJ/qfRXPmX8Xy9LMu5SSz12YdaVbXasKui2R1Z/DcOHcIHFPUd 1lMkJY54h6Rmvsa/vM3hbV2skcS4nj5Xhp4RvXxQMeMviwRXp0kpIRzINsl+owNet/vP UhIzHkdsXFEj+L7RKF9N7VQZRRV0nHoe3PEEY4/T8M9HgMoo8UUog3N8LJoDCsoMRX8l xCJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759119519; x=1759724319; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=d0HuPrzRcr+V3kcE8yiDGocxKo7EJdhh045GmqgaR/I=; b=S5hX9/CcsWridfPG8pUNFVraBdaNOi/x/Voh937f0ItMPNTRJA4Uy/M2SA11YRMct+ qqzbaw5bT3eebNalknQtfp5prgamSXGQpjx1jdnbcsRJRwzjIYnNgZIy7lN2KRlw5R2u 0stLxtBYbtBjYr/SQMYexzAJWOcGkodVksobsl/o7SyMTyHbSVLAm8r3jW4bjIrkU0y8 DBu/vaz2ATPtYK2JcE2wj6YzrO9qLIS6BkwekfUOTt76TAeoIUefR3vzshOAxb5NaYNk uPACJ51lbJcTu/VswnwkBiPJDR5Z3QyDuaX8bPSYM1r8O8VDO87obdAc/2Qax224sVbH zoKw== X-Forwarded-Encrypted: i=1; AJvYcCVL+LAOm6tyq9xm9HYlhU62EVI3Rrj6cl4b2OeostN6CxRYvNV8muSy54N7Wl7SZ8IEGLOuMfMRDyz8fL8y@postgresql.org X-Gm-Message-State: AOJu0YzKTBboV2hNQdqI+faKBc9x1utPqdpFNId6ic5FBAwk8ahyTWpn L0RjvI6/ZzRthbghEzVv0Sm9JtPtijKqOb7Ap4DYhJ22vlFVhA7UlAsj X-Gm-Gg: ASbGncvTTKJoGCaJitHP6BDVKbxw/K7W7MuCRt9Lv7qG6SV+4C7BKCeRm1WPIgsZasR FOd6RA3AYIanVDQQc5MSeRtqBSfP1slZjnRVY0EWHwteOBHL7nESP7PkPBsnfj2HWjwlkGjs9+h nGCJG7DBaS6Wf8g/ptCaPDDkOuS5mCDuVxqLOPn3FCF1/fCDfDymqURYkD9KmBrEzghyFRAPXr1 HVw5iVdo6CiZc0xnRLJgspBvwvRksm4HKZfR4V5Cyod+vzXEQ+koOlIxGGODRn0inczi7qxrtiO W0DOncxnS+mph+hVg/gR0rN0Acej+7R9ZxeV3fvTwCJfRzwe/6CHExemb4HOmeChFpESQo5Pskv Czv7iJbSViHz93rnCjOEm0uHiFOpIkjXcgXkQxZ8MIZQ= X-Google-Smtp-Source: AGHT+IFMD02XbGxcGm0+uVuJ/kdMv9qrAIZKskopzL5s1nIk/+d0SANWuHdV6YyGAXTOYK2zzEpD9g== X-Received: by 2002:a17:903:2407:b0:24c:ca55:6d90 with SMTP id d9443c01a7336-27ed4a7448emr175834725ad.61.1759119518488; Sun, 28 Sep 2025 21:18:38 -0700 (PDT) Received: from smtpclient.apple ([142.171.105.12]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-27ed66d3fe7sm118005945ad.7.2025.09.28.21.18.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Sep 2025 21:18:37 -0700 (PDT) From: Chao Li Message-Id: <1D4E38CE-E665-43DD-82C2-532CD9A4E2FA@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_0F6C8831-BB19-4566-9C64-3276A45F7892" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: bug, ALTER TABLE call ATPostAlterTypeCleanup twice for the same relation Date: Mon, 29 Sep 2025 12:18:21 +0800 In-Reply-To: Cc: amul sul , Peter Eisentraut , PostgreSQL-development To: David Rowley , jian he References: X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_0F6C8831-BB19-4566-9C64-3276A45F7892 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Sep 29, 2025, at 05:36, David Rowley wrote: >=20 > On Sat, 27 Sept 2025 at 21:54, jian he = wrote: >> if (pass =3D=3D AT_PASS_ALTER_TYPE || pass =3D=3D = AT_PASS_SET_EXPRESSION) >> - ATPostAlterTypeCleanup(wqueue, tab, lockmode); >> + { >> + if (!list_member_oid(relids, tab->relid)) >> + { >> + ATPostAlterTypeCleanup(wqueue, tab, lockmode); >> + relids =3D lappend_oid(relids, tab->relid); >> + } >> + } >=20 >=20 > drop table if exists gtest25; > CREATE TABLE gtest25 (a0 int, z int, a int, b int GENERATED ALWAYS AS > (a * 2 + a0) STORED); > alter table gtest25 add constraint check_z check (z > 0); > alter table gtest25 add constraint cc check (b > 0); >=20 > select oid, conname,conbin from pg_constraint where > conrelid=3D'gtest25'::regclass; >=20 > alter table gtest25 alter column z set data type numeric, ALTER > COLUMN b SET EXPRESSION AS (z + 0); > select oid, conname,conbin from pg_constraint where > conrelid=3D'gtest25'::regclass; >=20 > and that's certainly wrong as if I separate the ALTER TABLE into two > separate commands, then "cc" does get recreated. >=20 > I do wonder what the exact reason was that AT_PASS_ALTER_TYPE and > AT_PASS_SET_EXPRESSION are separate passes. I'd have expected that's > maybe so that it's possible to alter the type of a column used within > a generated column, but looking at the following error message makes > me think I might not be correct in that thinking: >=20 > create table test1 (a int, b int generated always as (a + 1) stored); > alter table test1 alter column a set data type bigint; > ERROR: cannot alter type of a column used by a generated column > DETAIL: Column "a" is used by generated column "b". >=20 > There's probably some good explanation for the separate pass, but I'm > not sure of it for now. I'm including in the author and committer of > the patch which added this code to see if we can figure this out. >=20 Based on the comment of ATPostAlterTypeCleanup(), it says =E2=80=9Ccleanup= after we=E2=80=99ve finished all the ALTER TYPE or SET EXPRESSION = operations for a particular relation=E2=80=9D, I tried this dirty fix: ``` diff --git a/src/backend/commands/tablecmds.c = b/src/backend/commands/tablecmds.c index fc89352b661..5c9c6d2a7db 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -5296,6 +5296,8 @@ ATRewriteCatalogs(List **wqueue, LOCKMODE = lockmode, AlterTableUtilityContext *context) { ListCell *ltab; + bool needAlterTypeCleanup =3D false; + AlteredTableInfo *tabToCleanup =3D NULL; /* * We process all the tables "in parallel", one pass at a time. = This is @@ -5313,6 +5315,13 @@ ATRewriteCatalogs(List **wqueue, LOCKMODE = lockmode, List *subcmds =3D tab->subcmds[pass]; ListCell *lcmd; + if (pass =3D=3D AT_PASS_OLD_INDEX && = needAlterTypeCleanup) + { + ATPostAlterTypeCleanup(wqueue, = tabToCleanup, lockmode); + needAlterTypeCleanup =3D false; + tabToCleanup =3D NULL; + } + if (subcmds =3D=3D NIL) continue; @@ -5334,7 +5343,10 @@ ATRewriteCatalogs(List **wqueue, LOCKMODE = lockmode, * done only once if multiple columns of a table = are altered). */ if (pass =3D=3D AT_PASS_ALTER_TYPE || pass =3D=3D = AT_PASS_SET_EXPRESSION) - ATPostAlterTypeCleanup(wqueue, tab, = lockmode); + { + needAlterTypeCleanup =3D true; + tabToCleanup =3D tab; + } if (tab->rel) { ``` It postpones the cleanup to after all =E2=80=9Calter type=E2=80=9D and = =E2=80=9Cset expression=E2=80=9D. With this fix, David=E2=80=99s test = case also passes: ``` evantest=3D# select oid, conname,conbin from pg_constraint where evantest-# conrelid=3D'gtest25'::regclass; oid | conname | = = = conbin = -------+---------+--------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= ------------------------------------------------------------------------ 32778 | check_z | {OPEXPR :opno 521 :opfuncid 147 :opresulttype 16 = :opretset false :opcollid 0 :inputcollid 0 :args ({VAR :varno 1 = :varattno 2 :vartype 23 :vartypmod -1 :varcollid 0 :varnullingrels (b) = :varlevelsup 0 :varreturningtype 0 :varnosyn 1 :varattnosyn 2 :location = -1} {CONST :consttype 23 :consttypmod -1 :constcollid 0 :constlen 4 = :constbyval true :constisnull false :location -1 :constvalue 4 [ 0 0 0 0 = 0 0 0 0 ]}) :location -1} 32779 | cc | {OPEXPR :opno 521 :opfuncid 147 :opresulttype 16 = :opretset false :opcollid 0 :inputcollid 0 :args ({VAR :varno 1 = :varattno 4 :vartype 23 :vartypmod -1 :varcollid 0 :varnullingrels (b) = :varlevelsup 0 :varreturningtype 0 :varnosyn 1 :varattnosyn 4 :location = -1} {CONST :consttype 23 :consttypmod -1 :constcollid 0 :constlen 4 = :constbyval true :constisnull false :location -1 :constvalue 4 [ 0 0 0 0 = 0 0 0 0 ]}) :location -1} (2 rows) evantest=3D# alter table gtest25 alter column z set data type numeric, = ALTER evantest-# COLUMN b SET EXPRESSION AS (z + 0); ALTER TABLE evantest=3D# select oid, conname,conbin from pg_constraint where evantest-# conrelid=3D'gtest25'::regclass; oid | conname | = = = = conbin = -------+---------+--------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= - 32781 | check_z | {OPEXPR :opno 1756 :opfuncid 1720 :opresulttype 16 = :opretset false :opcollid 0 :inputcollid 0 :args ({VAR :varno 1 = :varattno 2 :vartype 1700 :vartypmod -1 :varcollid 0 :varnullingrels (b) = :varlevelsup 0 :varreturningtype 0 :varnosyn 1 :varattnosyn 2 :location = -1} {FUNCEXPR :funcid 1740 :funcresulttype 1700 :funcretset false = :funcvariadic false :funcformat 2 :funccollid 0 :inputcollid 0 :args = ({CONST :consttype 23 :consttypmod -1 :constcollid 0 :constlen 4 = :constbyval true :constisnull false :location -1 :constvalue 4 [ 0 0 0 0 = 0 0 0 0 ]}) :location -1}) :location -1} 32782 | cc | {OPEXPR :opno 521 :opfuncid 147 :opresulttype 16 = :opretset false :opcollid 0 :inputcollid 0 :args ({VAR :varno 1 = :varattno 4 :vartype 23 :vartypmod -1 :varcollid 0 :varnullingrels (b) = :varlevelsup 0 :varreturningtype 0 :varnosyn 1 :varattnosyn 4 :location = -1} {CONST :consttype 23 :consttypmod -1 :constcollid 0 :constlen 4 = :constbyval true :constisnull false :location -1 :constvalue 4 [ 0 0 0 0 = 0 0 0 0 ]}) :location -1} (2 rows) evantest=3D# ``` We can see both check_z and check_cc got rebuilt. This fix uses the last =E2=80=9Ctab=E2=80=9D with the cleanup function. = In that way, TBH, I haven=E2=80=99t dig deeper enough to see if anything = is missed in cleanup. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_0F6C8831-BB19-4566-9C64-3276A45F7892 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

On Sep 29, 2025, at 05:36, David Rowley = <dgrowleyml@gmail.com> wrote:

On Sat, 27 Sept 2025 at = 21:54, jian he <jian.universality@gmail.com> wrote:
if (pass =3D=3D AT_PASS_ALTER_TYPE || pass =3D=3D = AT_PASS_SET_EXPRESSION)
- ATPostAlterTypeCleanup(wqueue, tab, = lockmode);
+ {
+ if (!list_member_oid(relids, tab->relid))
+ = {
+ ATPostAlterTypeCleanup(wqueue, tab, lockmode);
+ relids =3D = lappend_oid(relids, tab->relid);
+ }
+ = }


drop table if exists gtest25;
CREATE TABLE = gtest25 (a0 int, z int, a int, b int GENERATED ALWAYS AS
(a * 2 + a0) = STORED);
alter table gtest25 add constraint check_z check (z > = 0);
alter table gtest25 add constraint cc check (b > = 0);

select oid, conname,conbin from pg_constraint = where
conrelid=3D'gtest25'::regclass;

alter table gtest25 =  alter column z set data type numeric, ALTER
COLUMN b SET = EXPRESSION AS (z + 0);
select oid, conname,conbin from pg_constraint = where
conrelid=3D'gtest25'::regclass;

and that's certainly = wrong as if I separate the ALTER TABLE into two
separate commands, = then "cc" does get recreated.

I do wonder what the exact reason = was that AT_PASS_ALTER_TYPE and
AT_PASS_SET_EXPRESSION are separate = passes. I'd have expected that's
maybe so that it's possible to alter = the type of a column used within
a generated column, but looking at = the following error message makes
me think I might not be correct in = that thinking:

create table test1 (a int, b int generated always = as (a + 1) stored);
alter table test1 alter column a set data type = bigint;
ERROR:  cannot alter type of a column used by a = generated column
DETAIL:  Column "a" is used by generated column = "b".

There's probably some good explanation for the separate = pass, but I'm
not sure of it for now. I'm including in the author and = committer of
the patch which added this code to see if we can figure = this out.


Based on the = comment of ATPostAlterTypeCleanup(), it says =E2=80=9Ccleanup after = we=E2=80=99ve finished all the ALTER TYPE or SET EXPRESSION operations = for a particular relation=E2=80=9D, I tried this dirty = fix:

```
diff --git = a/src/backend/commands/tablecmds.c = b/src/backend/commands/tablecmds.c
index = fc89352b661..5c9c6d2a7db 100644
--- = a/src/backend/commands/tablecmds.c
+++ = b/src/backend/commands/tablecmds.c
@@ -5296,6 +5296,8 @@ = ATRewriteCatalogs(List **wqueue, LOCKMODE lockmode,
  =                     =             AlterTableUtilityContext = *context)
 {
        = ListCell   *ltab;
+       bool   =      needAlterTypeCleanup =3D false;
+   =     AlteredTableInfo *tabToCleanup =3D = NULL;

        = /*
         * We process all the = tables "in parallel", one pass at a time.  This is
@@ = -5313,6 +5315,13 @@ ATRewriteCatalogs(List **wqueue, LOCKMODE = lockmode,
              =           List       *subcmds =3D = tab->subcmds[pass];
          =               ListCell   = *lcmd;

+           =             if (pass =3D=3D = AT_PASS_OLD_INDEX && needAlterTypeCleanup)
+   =                     = {
+                 =               = ATPostAlterTypeCleanup(wqueue, tabToCleanup, lockmode);
+ =                     =           needAlterTypeCleanup =3D = false;
+               =                 tabToCleanup =3D = NULL;
+               =         }
+
      =                   if = (subcmds =3D=3D NIL)
            =                     = continue;

@@ -5334,7 +5343,10 @@ = ATRewriteCatalogs(List **wqueue, LOCKMODE lockmode,
  =                     =    * done only once if multiple columns of a table are = altered).
              =            */
    =                     if = (pass =3D=3D AT_PASS_ALTER_TYPE || pass =3D=3D = AT_PASS_SET_EXPRESSION)
-           =                     = ATPostAlterTypeCleanup(wqueue, tab, lockmode);
+     =                   = {
+                 =               needAlterTypeCleanup =3D = true;
+               =                 tabToCleanup =3D = tab;
+                 =       }

      =                   if = (tab->rel)
              =           = {
```

It postpones the cleanup = to after all =E2=80=9Calter type=E2=80=9D and =E2=80=9Cset = expression=E2=80=9D. With this fix, David=E2=80=99s test case also = passes:

```
evantest=3D# select = oid, conname,conbin from pg_constraint where
evantest-# = conrelid=3D'gtest25'::regclass;
  oid  | conname | =                     =                     =                     =                     =                     =                     =                     =                     =                     =                     =         = conbin
-------+---------+---------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= ---------------
 32778 | check_z | {OPEXPR :opno 521 = :opfuncid 147 :opresulttype 16 :opretset false :opcollid 0 :inputcollid = 0 :args ({VAR :varno 1 :varattno 2 :vartype 23 :vartypmod -1 :varcollid = 0 :varnullingrels (b) :varlevelsup 0 :varreturningtype 0 :varnosyn 1 = :varattnosyn 2 :location -1} {CONST :consttype 23 :consttypmod -1 = :constcollid 0 :constlen 4 :constbyval true :constisnull false :location = -1 :constvalue 4 [ 0 0 0 0 0 0 0 0 ]}) :location = -1}
 32779 | cc      | {OPEXPR :opno 521 = :opfuncid 147 :opresulttype 16 :opretset false :opcollid 0 :inputcollid = 0 :args ({VAR :varno 1 :varattno 4 :vartype 23 :vartypmod -1 :varcollid = 0 :varnullingrels (b) :varlevelsup 0 :varreturningtype 0 :varnosyn 1 = :varattnosyn 4 :location -1} {CONST :consttype 23 :consttypmod -1 = :constcollid 0 :constlen 4 :constbyval true :constisnull false :location = -1 :constvalue 4 [ 0 0 0 0 0 0 0 0 ]}) :location -1}
(2 = rows)

evantest=3D# alter table gtest25 =  alter column z set data type numeric, ALTER
evantest-# = COLUMN b SET EXPRESSION AS (z + 0);
ALTER = TABLE
evantest=3D# select oid, conname,conbin from = pg_constraint where
evantest-# = conrelid=3D'gtest25'::regclass;
  oid  | conname | =                     =                     =                     =                     =                     =                     =                     =                     =                     =                     =                     =                     =                     =                     =   =  conbin
-------+---------+---------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= ------------------------
 32781 | check_z | {OPEXPR :opno = 1756 :opfuncid 1720 :opresulttype 16 :opretset false :opcollid 0 = :inputcollid 0 :args ({VAR :varno 1 :varattno 2 :vartype 1700 :vartypmod = -1 :varcollid 0 :varnullingrels (b) :varlevelsup 0 :varreturningtype 0 = :varnosyn 1 :varattnosyn 2 :location -1} {FUNCEXPR :funcid 1740 = :funcresulttype 1700 :funcretset false :funcvariadic false :funcformat 2 = :funccollid 0 :inputcollid 0 :args ({CONST :consttype 23 :consttypmod -1 = :constcollid 0 :constlen 4 :constbyval true :constisnull false :location = -1 :constvalue 4 [ 0 0 0 0 0 0 0 0 ]}) :location -1}) :location = -1}
 32782 | cc      | {OPEXPR :opno 521 = :opfuncid 147 :opresulttype 16 :opretset false :opcollid 0 :inputcollid = 0 :args ({VAR :varno 1 :varattno 4 :vartype 23 :vartypmod -1 :varcollid = 0 :varnullingrels (b) :varlevelsup 0 :varreturningtype 0 :varnosyn 1 = :varattnosyn 4 :location -1} {CONST :consttype 23 :consttypmod -1 = :constcollid 0 :constlen 4 :constbyval true :constisnull false :location = -1 :constvalue 4 [ 0 0 0 0 0 0 0 0 ]}) :location -1}
(2 = rows)

evantest=3D#
```
=
We can see both check_z and check_cc got = rebuilt.

This fix uses the last =E2=80=9Ctab=E2=80= =9D with the cleanup function. In that way, TBH, I haven=E2=80=99t dig = deeper enough to see if anything is missed in = cleanup.

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




= --Apple-Mail=_0F6C8831-BB19-4566-9C64-3276A45F7892--