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 1v9ZCq-002bA1-GC for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Oct 2025 01:24:31 +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 1v9ZCp-00GZCA-AE for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Oct 2025 01:24:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v9ZCp-00GZBv-15 for pgsql-hackers@lists.postgresql.org; Fri, 17 Oct 2025 01:24:30 +0000 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v9ZCl-002cty-1C for pgsql-hackers@postgresql.org; Fri, 17 Oct 2025 01:24:29 +0000 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-7930132f59aso1917023b3a.0 for ; Thu, 16 Oct 2025 18:24:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760664265; x=1761269065; darn=postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=SfrUFVRqk/7gy2Nh7MQniRrP6b3wFJBEBpM4lWrDLVI=; b=JouwZutQnv0jZ3mpYIP1JKTQc+eUitj5KF0USexmUxXenfPoGhlFdVxbMNLzkYAhck U6d0bboSUiq5hiGapbvBDt3ygQ5/2aCo/guJZIMtAJg24CLMA0mpJO4TKJyfzQhciGlR w4/+8sPLni8wUMXwlyTaxApgwI6WzPLTqyzG03Jktcw2GebFYK9fUjZB+RboGHeoh/EC L5/DUkFNU2tcVihKK1fNy/uv3KnBZBem4uFA/T4bEj1W4X+PyRgI4RFAVuIk2mHwVa1f BeiIr9vDsC61X6k5jWcf25kxRjYP3m3r3IVfEmGeazk3utvfJd1sCoZPDN5AsI6PpVvE bLig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760664265; x=1761269065; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SfrUFVRqk/7gy2Nh7MQniRrP6b3wFJBEBpM4lWrDLVI=; b=W6j3Ldtrejg5Eo2BzHOcxFi8PubjMMWnk+1LIw8y2Q43boCuv3L7P+HsQhE7+QvkOF DMSw2wl3EYtovn/B/jMJzwZVK/iS9Y4Ir1Upjm1FsuYJrGGzljn0gYloYeCIXp5YzipM X7Q3ojLRgmn8Z9yKJGUYK8B54+zkHvm7TSa3Vodq24frg161z+c88IgW8z5Z++DDNFU9 vj5/j4KFqDlzN29BMtg67ovfYrtMlpwA44r+s1HgvVoNTtE3me+Q39ByLkVIR43A1Qgn dEKGtsNqGBLuW4yVggDQkLn7qd2EzyMIygx7OoHOTxEKVzlAPXxOrSuB4TXK3XpQ8TUr CrWQ== X-Forwarded-Encrypted: i=1; AJvYcCUMToY5PomDJ/Aqo8mGhXUa77VF5Lg7g/ioqDemNw1ZKqQzcLRgCihhIbfeHqKdp+Up0kE4BM8ihQpD9aNU@postgresql.org X-Gm-Message-State: AOJu0Yy9FRGaSJum1bnIgVhNP0GRZpVJnpM6qnBcsz6pjETOXTWzG1bL Wdo9UTcfdf6YU7uC+pbMPuMBJyOCeUjdwFdlqhFj8fBR3A0lhsxOFsDG X-Gm-Gg: ASbGncsrxSX3moBIasjBfX+9XSd4npWpQ2nQ+3pDkyuw1OxME5sad9wgDkmZO58A7PF n3OheCyfogZ7v7161tF/GsBqqrFANKoRFeELfrV1QDVS3cJCq3Juc7x6j+ac5w4myi35K4zCotN 56oGrKrVeY6yreVpkRo4cPU8tPUA6TsZ88H11m09sg6Xb910Q4RYkq7rZFxjgw85/rdAvYxIGOj 4Z/8lvsp0Zr0osHOrHn6+HEKLF9DzLMqD58R10SfpzWPLa9wpxM8lmYCQmPirioax9PLTR0TkgS g98hSZ2dtwMMlDv2Ryt62D/KZTy8bQA9ItoIfYK0U58VrsCzl4KZctCI1/69cdGni+AspYhEo14 a6E9A+bQ2OxAHa4TJWWTfT8P8jnFMAxQtkHtRY1nDQ+xr/zi87luA5cvyPxvcN9aepr1RWMV0ZS dbR20Z9WP0+Vg= X-Google-Smtp-Source: AGHT+IEWXqewQ4Tj6ghPiybmhXmvp7K95DHGsMFucM6pvTyKfa5ueiPOKx4N6wql9/ovlIMljDErzQ== X-Received: by 2002:a05:6a21:33a6:b0:2e9:d6ce:e125 with SMTP id adf61e73a8af0-334a8485516mr2527411637.5.1760664265303; Thu, 16 Oct 2025 18:24:25 -0700 (PDT) Received: from smtpclient.apple ([170.178.170.211]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b6a22bb1c97sm4141227a12.32.2025.10.16.18.24.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Oct 2025 18:24:24 -0700 (PDT) Content-Type: text/plain; charset=utf-8 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 From: Chao Li In-Reply-To: Date: Fri, 17 Oct 2025 09:23:46 +0800 Cc: David Rowley , amul sul , Peter Eisentraut , PostgreSQL-development Content-Transfer-Encoding: quoted-printable Message-Id: <173872F1-0EE6-45C5-BB1B-D8DF2E9930F2@gmail.com> References: To: jian he X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Oct 13, 2025, at 16:08, jian he = wrote: >=20 > On Wed, Oct 1, 2025 at 11:04=E2=80=AFAM jian he = wrote: >>=20 >> we can simply change from >>=20 >> if (pass =3D=3D AT_PASS_ALTER_TYPE || pass =3D=3D = AT_PASS_SET_EXPRESSION) >> ATPostAlterTypeCleanup(wqueue, tab, lockmode); >>=20 >> to >> if (pass =3D=3D AT_PASS_SET_EXPRESSION) >> ATPostAlterTypeCleanup(wqueue, tab, lockmode); >> else if (pass =3D=3D AT_PASS_ALTER_TYPE && >> tab->subcmds[AT_PASS_SET_EXPRESSION] =3D=3D NIL) >> ATPostAlterTypeCleanup(wqueue, tab, lockmode); >=20 > 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); >=20 > If we alter the type of column "a", the column b=E2=80=99s 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. >=20 > 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);`` >=20 > we can just list_free these (the below) objects too: > tab->changedConstraintOids > tab->changedConstraintDefs > tab->changedIndexOids > tab->changedIndexDefs > tab->changedStatisticsOids > tab->changedStatisticsDefs >=20 > since this information would become stale if those objects have > already been dropped. > I think the correct solution should be my proposed v4 that has a = fundamental difference from your current change is that: * The current implementation as well as your current change, they do = ATPostAlterTypeCleanup() in either ALTER_TYPE pass or SET_EXPRESSION = pass. Say a table has columns a and b, and a constraint (a+b>5), then I = alter both columns types in a single command, then it will alter a=E2=80=99= s type, then rebuild the constant, and alter b=E2=80=99s type, then = rebuild the constraint again. For the first rebuild, because c=E2=80=99s = type has not been updated, the rebuild may potentially fail. * My v4 defers ATPostAlterTypeCleanup() to a later pass, which allows = all "set data type=E2=80=9D and =E2=80=9Cset expression=E2=80=9D to = finish, then start to rebuild constraints. My v4 does the cleanup in = next pass of SET_EXPRESSION, which might not be the best solution, = instead, it=E2=80=99s just a quick PoC to demo my idea. Perhaps, we may = add a new pass. But to handle the complicated case that I described above, v4 is still = not enough. All =E2=80=9CchangedXXXXX=E2=80=9D stuffs should be moved to = an upper level of tab. Still using the same example, the constrain = depends on both columns an and b, so we should remember the constraint = only once. WDYT? Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/