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 1uVvQl-0063CA-4m for pgsql-bugs@arkaria.postgresql.org; Sun, 29 Jun 2025 17:03:03 +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 1uVvPi-00CFSF-WC for pgsql-bugs@arkaria.postgresql.org; Sun, 29 Jun 2025 17:01:59 +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 1uVvPi-00CFS7-Me for pgsql-bugs@lists.postgresql.org; Sun, 29 Jun 2025 17:01:59 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uVvPh-004ckZ-0x for pgsql-bugs@lists.postgresql.org; Sun, 29 Jun 2025 17:01:58 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 55TH1rTf2482013; Sun, 29 Jun 2025 13:01:53 -0400 From: Tom Lane To: jian he cc: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18970: Atempt to alter type of table column used in row type with check leads to assertion failure In-reply-to: References: <18970-a7d1cfe1f8d5d8d9@postgresql.org> Comments: In-reply-to jian he message dated "Sun, 29 Jun 2025 13:28:05 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2482011.1751216513.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 29 Jun 2025 13:01:53 -0400 Message-ID: <2482012.1751216513@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk jian he writes: > On Sun, Jun 29, 2025 at 1:35=E2=80=AFAM PG Bug reporting form > wrote: >> CREATE TABLE t1(a int); >> CREATE TABLE t2(b t1 CHECK ((b).a IS NOT NULL)); >> ALTER TABLE t1 ALTER COLUMN a TYPE numeric; >> triggers >> 2025-06-28 06:52:21.201 UTC [2233016] LOG: statement: ALTER TABLE t1 A= LTER >> COLUMN a TYPE numeric; >> TRAP: failed Assert("lockmode !=3D NoLock || IsBootstrapProcessingMode(= ) || >> CheckRelationLockedByMe(r, AccessShareLock, true)"), File: "relation.c"= , >> Line: 67, PID: 2233016 > in ATPostAlterTypeCleanup > /* > * When rebuilding an FK constraint that references the table we= 're > * modifying, we might not yet have any lock on the FK's table, = so get > * one now. We'll need AccessExclusiveLock for the DROP CONSTRA= INT > * step, so there's no value in asking for anything weaker. > */ > if (relid !=3D tab->relid && contype =3D=3D CONSTRAINT_FOREIGN) > LockRelationOid(relid, AccessExclusiveLock); > we can change to > if (relid !=3D tab->relid) > LockRelationOid(relid, AccessExclusiveLock); > obviously, the comments need to be updated. Yeah, I came to the same conclusion after studying it for awhile. The problem is that because of the "(b).a" column reference in the CHECK expression, we record a direct dependency of t2's CHECK constraint on t1.a, and ATPostAlterTypeCleanup is evidently not expecting that. > When altering the data type of a column in one relation causes a constra= int of > another table rebuild, the other table should be locked with > AccessExclusiveLock. I wonder if there might be cases where a lesser lock is sufficient. However, this is apparently a very edgy edge case, so it's probably not worth obsessing over the lock level too much. It's somewhat annoying that we're just going to fail later. I thought about whether find_composite_type_dependencies ought to be run sooner, so that we'd error out before potentially doing a lot of work. But I think the design idea is that someday find_composite_type_dependencies would actually propagate the changes, in which case its current placement is correct. regards, tom lane