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 1rR12j-004mlS-Tq for pgsql-hackers@arkaria.postgresql.org; Sat, 20 Jan 2024 02:25:10 +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 1rR12h-00Btw2-Ra for pgsql-hackers@arkaria.postgresql.org; Sat, 20 Jan 2024 02:25:07 +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 1rR12h-00Btvt-Du for pgsql-hackers@lists.postgresql.org; Sat, 20 Jan 2024 02:25:07 +0000 Received: from mail-yw1-x112e.google.com ([2607:f8b0:4864:20::112e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rR12e-002PYK-Ld for pgsql-hackers@postgresql.org; Sat, 20 Jan 2024 02:25:05 +0000 Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-5ffaabdcccbso4132687b3.0 for ; Fri, 19 Jan 2024 18:25:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705717504; x=1706322304; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=+RvjmZkFna8afVxxFq47z/ts3OJrGVkT747iURRvDaw=; b=BxK1YbWJugcPbTgveJRHkFIO3+BfSzkqWyIIRLpNC4g8GHZLMWAWKapy7oq8dBneKv lvZutGjYwdHv/KTarJ7KZ0/DsEcGHOmir57Nh7cMWHrybtXuxhFZy2EhOKAnbpol/Bqn 6lJ6OuAMe5QSbzJzGa78yfsGVBfD/XXXbl2WGlY+WhEa61dg5ZIIHMYzqhkLigQFOCw8 8ZYr7+/5rnAFNsVcXxf7rghEqTutuq2A9A70e2Zx/4+2PlYzJwavoD3ZAZf4EEDXA1aV eYkP1mw0uFRXmkLwSTHL7J4YL2ttLPOtj6OK/uwtECvS/S/pjf1IiJF7CFmis6awrVje jGKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705717504; x=1706322304; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+RvjmZkFna8afVxxFq47z/ts3OJrGVkT747iURRvDaw=; b=Pg2VDeFTRUcz+kTLPYf3d9nOqMsn0fz5gvVxiuezb4hEnQ31edE904Rg1hLvdYxbm8 LbjXR8s3gaRUWPjtMh2h79QBsYFXRbRPUV2dTdHv2WJ62eoX5pnJ1Z0tHaOoV382wjfh fye+ehFlZLdyHSwsT9jGK41lw6xnmu3qces6FA57+/Lbti3HsZCI8qnhmkP71cEZdjMh sXvZT289ZvKs/1noXlfI2EqYsHwin3CDozgeuJXJboDGz7dx3vQha4QB8MOSRE1c1Trl nsMye+0lntoXhzu0xyBN5lNtb6kIPbNNin8Kc/j9ozr+sHdCO3guCkbtL6qUbryn5hjh pqow== X-Gm-Message-State: AOJu0Ywf4k7Qhc3Pn1ZebKC9IbeiXtCkct0AcYOJee4nvtf89+tB+yoc Qmq/Gei1IHJPgbkNPT3UhGHqtECzHIldelR9kMQpEN5X6Nl3VHN8cBLUj3Q0DEkr8gQ9IZOMh6m Sq2vfkxzMwtptpba3wgwitaVq1U8= X-Google-Smtp-Source: AGHT+IEcVme4+nr764Dos6C6FT1YT8R7mjGUdndH8HviZ2K5KwEH2Qa32i75ikdJpzgt+4aBwiMqanuvnbLkbTPABNU= X-Received: by 2002:a0d:f441:0:b0:5f7:7d5c:2302 with SMTP id d62-20020a0df441000000b005f77d5c2302mr909993ywf.102.1705717503641; Fri, 19 Jan 2024 18:25:03 -0800 (PST) MIME-Version: 1.0 References: <202309201459.jhuhi2pns3re@alvherre.pgsql> In-Reply-To: From: vignesh C Date: Sat, 20 Jan 2024 07:54:52 +0530 Message-ID: Subject: Re: Dump-restore loosing 'attnotnull' bit for DEFERRABLE PRIMARY KEY column(s). To: Amul Sul Cc: Alvaro Herrera , PostgreSQL-development Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 22 Sept 2023 at 18:45, Amul Sul wrote: > > > > On Wed, Sep 20, 2023 at 8:29=E2=80=AFPM Alvaro Herrera wrote: >> >> On 2023-Sep-20, Amul Sul wrote: >> >> > On the latest master head, I can see a $subject bug that seems to be r= elated >> > 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/ut= ils/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_indp= red, NULL)) >> continue; >> >> @@ -4821,6 +4820,9 @@ RelationGetIndexList(Relation relation) >> relation->rd_rel->relkind =3D=3D RELKIN= D_PARTITIONED_TABLE)) >> pkeyIndex =3D 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. > > > True, but still wondering why would relation->rd_pkattr skipped for a > deferrable primary key, which seems to be a bit incorrect to me since it > sensing that relation doesn't have PK at all. Anyway, that is unrelated. > >> >> 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.) > > > I might not have understood this, but I think, if it is ok to skip throwa= way NOT > NULL for deferrable PK then that would be enough for the reported issue > to be fixed. I quickly tried with the attached patch which looks suffici= ent > to skip that, but, TBH, I haven't thought carefully about this change. I did not see any test addition for this, can we add it? Regards, Vignesh