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 1qiygN-008l4H-ON for pgsql-hackers@arkaria.postgresql.org; Wed, 20 Sep 2023 15:00:04 +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 1qiygM-000iwk-CI for pgsql-hackers@arkaria.postgresql.org; Wed, 20 Sep 2023 15:00:02 +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 1qiygL-000iwG-AR for pgsql-hackers@lists.postgresql.org; Wed, 20 Sep 2023 15:00:02 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qiygE-005dxD-7g for pgsql-hackers@postgresql.org; Wed, 20 Sep 2023 15:00:00 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 8BAD95C01F3; Wed, 20 Sep 2023 10:59:52 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Wed, 20 Sep 2023 10:59:52 -0400 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:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1695221992; x= 1695308392; bh=SdkADxjjvGUO53pyxzlvkylOCJwuZFWbtpFW39mHJmo=; b=a qkZBvH4Dh/rnDIcNe4IuYj94bvHz29mWI9pILdc/bIpwcaoOCAVNWCD7asfPIc8b 1Shna9aWr1cm8oB0j/XM3btsk4kRXyskF7DllLwsGrnwcSJ375e53aMApnMic5i1 U9MCK5jPOd8MMKUHDOwHsEDTrYOYBMptWu1laG1VQhKAThDlnFJca3XUQDZoXGoy 7tO41A/f7fpj18I1gqRp52U1JiitJc+VoyTymYS4zMJcp+pEJyvrqW7FXM9WYiUA jftQCXSFGzdbTsXCJ0BortoZgltn8azvSAUq0OEJuZL7GYrIup65A9ONMBHq4ZJR H/N/3AjPsvmh+vXv9/Tww== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudekfedgkeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffhjeejhffgieeuueekjeek fffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 20 Sep 2023 10:59:51 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id C2690C5; Wed, 20 Sep 2023 16:59:49 +0200 (CEST) Date: Wed, 20 Sep 2023 16:59:49 +0200 From: Alvaro Herrera To: Amul Sul Cc: PostgreSQL-development Subject: Re: Dump-restore loosing 'attnotnull' bit for DEFERRABLE PRIMARY KEY column(s). Message-ID: <202309201459.jhuhi2pns3re@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 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 On 2023-Sep-20, Amul Sul wrote: > On the latest master head, I can see a $subject bug that seems to be related > commit #b0e96f311985: > > Here is the table definition: > create table foo(i int, j int, CONSTRAINT pk PRIMARY KEY(i) DEFERRABLE); Interesting, thanks for the report. Your attribution to that commit is correct. The table is dumped like this: CREATE TABLE public.foo ( i integer CONSTRAINT pgdump_throwaway_notnull_0 NOT NULL NO INHERIT, j integer ); ALTER TABLE ONLY public.foo ADD CONSTRAINT pk PRIMARY KEY (i) DEFERRABLE; ALTER TABLE ONLY public.foo DROP CONSTRAINT pgdump_throwaway_notnull_0; so the problem here is that the deferrable PK is not considered a reason to keep attnotnull set, so we produce a throwaway constraint that we then drop. This is already bogus, but what is more bogus is the fact that the backend accepts the DROP CONSTRAINT at all. The pg_dump failing should be easy to fix, but fixing the backend to error out sounds more critical. So, the reason for this behavior is that RelationGetIndexList doesn't want to list an index that isn't marked indimmediate as a primary key. I can easily hack around that by doing diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index 7234cb3da6..971d9c8738 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -4794,7 +4794,6 @@ RelationGetIndexList(Relation relation) * check them. */ if (!index->indisunique || - !index->indimmediate || !heap_attisnull(htup, Anum_pg_index_indpred, NULL)) continue; @@ -4821,6 +4820,9 @@ RelationGetIndexList(Relation relation) relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)) pkeyIndex = index->indexrelid; + if (!index->indimmediate) + continue; + if (!index->indisvalid) continue; But of course this is not great, since it impacts unrelated bits of code that are relying on relation->pkindex or RelationGetIndexAttrBitmap having their current behavior with non-immediate index. I think a real solution is to stop relying on RelationGetIndexAttrBitmap in ATExecDropNotNull(). (And, again, pg_dump needs some patch as well to avoid printing a throwaway NOT NULL constraint at this point.) -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/