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 1rhU21-00Bh8P-3E for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Mar 2024 12:36: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 1rhU1y-00Gdjc-KC for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Mar 2024 12:36:27 +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 1rhU1x-00GdjT-4J for pgsql-hackers@lists.postgresql.org; Tue, 05 Mar 2024 12:36:26 +0000 Received: from wfout1-smtp.messagingengine.com ([64.147.123.144]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rhU1t-002tRZ-4w for pgsql-hackers@postgresql.org; Tue, 05 Mar 2024 12:36:25 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfout.west.internal (Postfix) with ESMTP id C26521C000B0; Tue, 5 Mar 2024 07:36:15 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Tue, 05 Mar 2024 07:36:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1709642174; x= 1709728574; bh=ZDA64SfHR2LzWRAWlBzR02c0XVDCi0U4u02sMkSSfS8=; b=r sf9uE6VkSfkXuu/0j0jR3Nu2VCFZHnnFqKSRxwwlg0sYleml2WQQRoV6i4gXy+Jl z2X5TRTFBJGOZHU9ZwB9SSHiPhghMaas0Zo2v8jeiBYn6u6aXGH/utuMZLnVFIB9 elvSEVWibekWg0zrH7YzQ3DK0K9IfXCP4p2VQEtor6xwD/bPRHig0aBpqdJbd1zi Lb+F3czqJFZG6F+Nq6g4V/hf3wNdXkKyZiT4IVdoWpC+ydhk492Jv2Nj/8tKSEBh K10q2rjsjDdE8X1o77l/VEld70oqYp2A3Wp+LXu9zVTaUv5POZRyxlKcN+Ks9Ing YoDOpTK0+mKlA0ly0BpBw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrheelgdefkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfggtggugfgjsehmkeerredttdejnecuhfhrohhmpeetlhhvrghr ohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqe enucggtffrrghtthgvrhhnpeduleekkefgtddttedtkefguddvieffleetgeejiefhteeh keevfeettdduvdfhueenucffohhmrghinhepvghnthgvrhhprhhishgvuggsrdgtohhmne cuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlvhhh vghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 5 Mar 2024 07:36:14 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1709642171; bh=lFQszZaQq3TqQaHz4YYHhyjabyy5f76UQsh/iR3Uo/g=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=HZB4uFEfXwTuOoCJDblPgaohB3cux1Q4oPcH+ggbHeB1Ow9g/KWUYWDsQCP3lexzs YNj43PeUQi6d8E8aS+q5WJnC8tBTwq7wpozhH4F54mi/AohJdMSzUkcq4heNhcBpXj OnNrqe940EbGzQAjC4pYCdHvIibL9LGQUdlqILIPYhcHCcVgx/PEvJ/+o4NJTYdka1 04ny8xIWNdjsztFuOiVQlaxnlN6gXUrlCNjWeiXHbWGvDIn1BIKE7dApPZPDodx5JA dQ1VP9648SHM0qbC8DYjFoAp9kIuwChXjc4JLoWFG68TD2mPhaCCyYZ/bKCm12lCHx wQttY49jNsX1Q== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id B50DCCD5; Tue, 5 Mar 2024 13:36:11 +0100 (CET) Date: Tue, 5 Mar 2024 13:36:11 +0100 From: Alvaro Herrera To: Dean Rasheed Cc: Aleksander Alekseev , PostgreSQL-development , Amul Sul , vignesh C Subject: Re: Dump-restore loosing 'attnotnull' bit for DEFERRABLE PRIMARY KEY column(s). Message-ID: <202403051236.to67sdlvgxk6@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="qek6jpjdi3ls6ksm" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --qek6jpjdi3ls6ksm Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2024-Mar-04, Dean Rasheed wrote: > I don't think that this is the right fix. ISTM that the real issue is > that dropping a NOT NULL constraint should not mark the column as > nullable if it is part of a PK, whether or not that PK is deferrable > -- a deferrable PK still marks a column as not nullable. Yeah. As I said upthread, a good fix seems to require no longer relying on RelationGetIndexAttrBitmap to obtain the columns in the primary key, because that function does not include deferred primary keys. I came up with the attached POC, which seems to fix the reported problem, but of course it needs more polish, a working test case, and verifying whether the new function should be used in more places -- in particular, whether it can be used to revert the changes to RelationGetIndexList that b0e96f311985 did. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ "La persona que no quería pecar / estaba obligada a sentarse en duras y empinadas sillas / desprovistas, por cierto de blandos atenuantes" (Patricio Vogel) --qek6jpjdi3ls6ksm Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-Don-t-lose-attnotnull-if-a-deferred-PK-supports-it.patch" From cc7032c3f62949d20bc98d4b3c70cb2cb8a0dd5e Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Tue, 5 Mar 2024 13:32:50 +0100 Subject: [PATCH] Don't lose attnotnull if a deferred PK supports it When dropping a NOT NULL constraint on a column that has a deferred primary key, we would reset attnotnull, but that's bogus. XXX this patch is nowhere near final. Reported-by: Amul Sul Discussion: https://postgr.es/m/CAAJ_b94QonkgsbDXofakHDnORQNgafd1y3Oa5QXfpQNJyXyQ7A@mail.gmail.com --- src/backend/commands/tablecmds.c | 71 ++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index c61f9305c2..84c7871dda 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -12704,6 +12704,73 @@ ATExecDropConstraint(Relation rel, const char *constrName, table_close(conrel, RowExclusiveLock); } +/* + * dropconstraint_getpkcols -- subroutine for dropconstraint_internal + * + * This is a workaround to the fact that RelationGetIndexAttrBitmap does + * not consider a DEFERRABLE PRIMARY KEY to be a real primary key. Maybe + * this code should be elsewhere. + */ +static Bitmapset * +dropconstraint_getpkcols(Relation rel) +{ + Relation indrel; + SysScanDesc indscan; + ScanKeyData skey; + HeapTuple htup; + Bitmapset *b = NULL; + + /* + * We try first to obtain a list of PK columns from the cache; if there + * is one, we're done. + */ + b = RelationGetIndexAttrBitmap(rel, INDEX_ATTR_BITMAP_PRIMARY_KEY); + if (b != NULL) + return b; + + /* Prepare to scan pg_index for entries having indrelid = this rel. */ + ScanKeyInit(&skey, + Anum_pg_index_indrelid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(RelationGetRelid(rel))); + + indrel = table_open(IndexRelationId, AccessShareLock); + indscan = systable_beginscan(indrel, IndexIndrelidIndexId, true, + NULL, 1, &skey); + + while (HeapTupleIsValid(htup = systable_getnext(indscan))) + { + Form_pg_index index = (Form_pg_index) GETSTRUCT(htup); + + /* + * Ignore any indexes that are currently being dropped and those that + * aren't a primary key. + */ + if (!index->indislive) + continue; + if (!index->indisprimary) + continue; + + /* + * If this primary key was IMMEDIATE, it would have been returned + * by RelationGetIndexAttrBitmap. + */ + Assert(!index->indimmediate); + + for (int i = 0; i < index->indnatts; i++) + { + int attrnum = index->indkey.values[i]; + + b = bms_add_member(b, attrnum - FirstLowInvalidHeapAttributeNumber); + } + } + + systable_endscan(indscan); + table_close(indrel, AccessShareLock); + + return b; +} + /* * Remove a constraint, using its pg_constraint tuple * @@ -12837,9 +12904,7 @@ dropconstraint_internal(Relation rel, HeapTuple constraintTup, DropBehavior beha * We want to test columns for their presence in the primary key, but * only if we're not dropping it. */ - pkcols = dropping_pk ? NULL : - RelationGetIndexAttrBitmap(rel, - INDEX_ATTR_BITMAP_PRIMARY_KEY); + pkcols = dropping_pk ? NULL : dropconstraint_getpkcols(rel); ircols = RelationGetIndexAttrBitmap(rel, INDEX_ATTR_BITMAP_IDENTITY_KEY); foreach(lc, unconstrained_cols) -- 2.39.2 --qek6jpjdi3ls6ksm--