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 1rpOXj-006VH4-60 for pgsql-hackers@arkaria.postgresql.org; Wed, 27 Mar 2024 08:21:55 +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 1rpOXg-00Bqa5-Az for pgsql-hackers@arkaria.postgresql.org; Wed, 27 Mar 2024 08:21:52 +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 1rpOXf-00BqZx-UD for pgsql-hackers@lists.postgresql.org; Wed, 27 Mar 2024 08:21:52 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rpOXc-006j6u-Qi for pgsql-hackers@lists.postgresql.org; Wed, 27 Mar 2024 08:21:51 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-51381021af1so9769985e87.0 for ; Wed, 27 Mar 2024 01:21:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711527707; x=1712132507; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=rMBJD4VzeBmt7J5UMODuriEu8WDHlMV+jbLzrLKyGYk=; b=M72sYRi561oZ54Ww2lT5rMbTzdMMqipVBytJdoAhXiUl7OHpsjLP2nDtqe+uoXUb90 Xv8OUuk8+7XJlvP6jazeHgILjmrWBw11v7KZn+bByiwGzEx9rfju+7wSPpHFAuP1fIkU Nu9t6o0KHhjIgnNv28Xq5mzbSEGOUDvIKy1pI/GKS7mkj9qI6uD6OzoMpRRyZ8eoeOe2 ldEwsdj/TuIzlCT2UbCecPbFtPLljvYWywH/gjwEssdAZuB+1NvPomCvGTWvtg9AP7I+ wyYH5nXKmeT+V+GVoo4QPVU2LuQU/y1f5ufF6+szqvyieDuaf1StioHu6WYx4UG9wgXh rKvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711527707; x=1712132507; h=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=rMBJD4VzeBmt7J5UMODuriEu8WDHlMV+jbLzrLKyGYk=; b=BWZ4j+IKyIr6BtgryCF5zSZy2PUaB3IWWCij5j2hurqRbHwzE+kAIX5YB2W1yqc9aB DEYK1DICXPhWcON9j5YU4hc5V4HNiuBxspFg5WsYXM95oLnrtSj5UjeX9eq2sewJk5YA WajsZL/+IWObTISOImtzSy7jLo/+MYyi4geE5KOiuKYTI0Y5PauWOI6o/V0vf7xH7wcU vJhY2JfDw/cFJmA0Hx0iPdUXNsldfho7auLD/UxC9Oe3LPGCynjxhIQlYYbsHJ6YjAuK f6q2LR+BGHyXmkELA5YjLXmsLi6oqOjmqc+/zmLTlrHIANC0994Zz6/L5ACeRA9RXPMU oENA== X-Gm-Message-State: AOJu0Yyiit0jkO7CHPNKhE7aJOBRdAWI0FeoEf+R4bQycVL1ykwIWmOn CBIl9cI9ehalnF1UQhNecEYYjE6BLOVo0yYxF30/8e+Pch3pc98vGlVs7f93gFvCcrgh2qPcJEi ltxbZ/BZMD82M+wDuXTFWHWdVreM= X-Google-Smtp-Source: AGHT+IEYiOrXwxjsM3bAT1cps6nBscP/M/mo1/q9/IN0/Ryj4UB2d5qyufjVMpczvHltxJKD7hwvjfK3tQty6ewxEbw= X-Received: by 2002:ac2:4dae:0:b0:515:7686:6068 with SMTP id h14-20020ac24dae000000b0051576866068mr1620743lfe.55.1711527706605; Wed, 27 Mar 2024 01:21:46 -0700 (PDT) MIME-Version: 1.0 References: <202403261525.ciahntfgj25o@alvherre.pgsql> In-Reply-To: <202403261525.ciahntfgj25o@alvherre.pgsql> From: Tender Wang Date: Wed, 27 Mar 2024 16:21:35 +0800 Message-ID: Subject: Re: Can't find not null constraint, but \d+ shows that To: Alvaro Herrera Cc: PostgreSQL Hackers Content-Type: multipart/alternative; boundary="000000000000f9bfa30614a018b4" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000f9bfa30614a018b4 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Alvaro Herrera =E4=BA=8E2024=E5=B9=B43=E6=9C=8826= =E6=97=A5=E5=91=A8=E4=BA=8C 23:25=E5=86=99=E9=81=93=EF=BC=9A > On 2024-Mar-26, Tender Wang wrote: > > > postgres=3D# CREATE TABLE t1(c0 int, c1 int); > > postgres=3D# ALTER TABLE t1 ADD CONSTRAINT Q PRIMARY KEY(c0, c1); > > postgres=3D# ALTER TABLE t1 DROP c1; > > > > postgres=3D# ALTER TABLE t1 ALTER c0 DROP NOT NULL; > > ERROR: could not find not-null constraint on column "c0", relation "t1= " > > Ooh, hah, what happens here is that we drop the PK constraint > indirectly, so we only go via doDeletion rather than the tablecmds.c > code, so we don't check the attnotnull flags that the PK was protecting. > > > The attached patch is my workaround solution. Look forward your apply. > > Yeah, this is not a very good approach -- I think you're just guessing > that the column is marked NOT NULL because a PK was dropped in the > past -- but really what this catalog state is, is corrupted contents > because the PK drop was mishandled. At least in theory there are other > ways to drop a constraint other than dropping one of its columns (for > example, maybe this could happen if you drop a collation that the PK > depends on). The right approach is to ensure that the PK drop always > does the dance that ATExecDropConstraint does. A good fix probably just > moves some code from dropconstraint_internal to RemoveConstraintById. > I think again, below solutin maybe looks more better: i. move some code from dropconstraint_internal to RemoveConstraintById, not change the RemoveConstraintById interface. Ensure that the PK drop always does the dance that ATExecDropConstraint does. ii. After i phase, the attnotnull of some column of primary key may be reset to false as I provided example in last email. We can set attnotnull to true again in some place. --=20 Tender Wang OpenPie: https://en.openpie.com/ --000000000000f9bfa30614a018b4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
Alvaro Herrera <alvherre@alvh.no-ip.org> =E4=BA=8E2024=E5= =B9=B43=E6=9C=8826=E6=97=A5=E5=91=A8=E4=BA=8C 23:25=E5=86=99=E9=81=93=EF=BC= =9A
On 2024-Mar-= 26, Tender Wang wrote:

> postgres=3D# CREATE TABLE t1(c0 int, c1 int);
> postgres=3D# ALTER TABLE=C2=A0 t1 ADD CONSTRAINT Q PRIMARY KEY(c0, c1)= ;
> postgres=3D# ALTER TABLE=C2=A0 t1 DROP c1;
>
> postgres=3D# ALTER TABLE=C2=A0 t1=C2=A0 ALTER c0 DROP NOT NULL;
> ERROR:=C2=A0 could not find not-null constraint on column "c0&quo= t;, relation "t1"

Ooh, hah, what happens here is that we drop the PK constraint
indirectly, so we only go via doDeletion rather than the tablecmds.c
code, so we don't check the attnotnull flags that the PK was protecting= .

> The attached patch is my workaround solution.=C2=A0 Look forward your = apply.

Yeah, this is not a very good approach -- I think you're just guessing<= br> that the column is marked NOT NULL because a PK was dropped in the
past -- but really what this catalog state is, is corrupted contents
because the PK drop was mishandled.=C2=A0 At least in theory there are othe= r
ways to drop a constraint other than dropping one of its columns (for
example, maybe this could happen if you drop a collation that the PK
depends on).=C2=A0 The right approach is to ensure that the PK drop always<= br> does the dance that ATExecDropConstraint=C2=A0does.=C2=A0 A good fix probab= ly just
moves some code from dropconstraint_internal to RemoveConstraintById.

I think again, below solutin maybe looks mor= e better:
i. move some code from=C2=A0 dropconstraint_internal to= RemoveConstraintById,=C2=A0
=C2=A0 not change the RemoveConstrai= ntById interface. Ensure that the PK drop always
=C2=A0 does the = dance that ATExecDropConstraint does.

ii. After i = phase, the attnotnull of some column of primary key=C2=A0 may be reset to f= alse as I provided example in last email.
=C2=A0 =C2=A0We can set= attnotnull to true again in some place.

<= div>
--
Tender Wang
OpenPie:=C2=A0=C2=A0https://en.openpie.com/
--000000000000f9bfa30614a018b4--