Re: not null constraints, again

From: Tender Wang <tndrwang(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Cc: jian he <jian(dot)universality(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: not null constraints, again
Date: 2025-04-17 17:01:14
Message-ID: CAHewXNnoacxOeRsuae_Ja7UKjnLgiO7ttc2OUHNssfgGawT9Hw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I found an inconsistent behavior in v17.4 and v18 after not-null
constraints were committed.

create table t1(a int not null);
ALTER TABLE t1 ADD CONSTRAINT d PRIMARY KEY(a), ALTER a DROP NOT NULL;

in v17.4, ALTER TABLE successes, but in v18, it reports below error:
ERROR: primary key column "a" is not marked NOT NULL

But if I separate the 'ALTER TABLE' command, there are no errors.
postgres=# create table t1(a int not null);
CREATE TABLE
postgres=# ALTER TABLE t1 ADD CONSTRAINT d PRIMARY KEY(a), ALTER a DROP
NOT NULL;
ERROR: primary key column "a" is not marked NOT NULL
postgres=# ALTER TABLE t1 ALTER a DROP NOT NULL;
ALTER TABLE
postgres=# ALTER TABLE t1 ADD CONSTRAINT d PRIMARY KEY(a);
ALTER TABLE

in v17.4, we first drop not null, but when run add primary key constraint,
set_notnull command will be added in transformIndexConstraint().
But in v18, the adding of set_notnull command logic in
transformIndexConstraint() has been removed. And when
call ATPrepAddPrimaryKey(),
the column has not-null constraint, so will not add not-null again.

Is the above error expected in v18? If so, we had better add more words to
the document. If it's not, we should fix it.

BTW, the document doesn't state the order in which the commands are
executed when users specify more than one manipulation in a single ALTER
TABLE command.

--
Thanks,
Tender Wang

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Álvaro Herrera 2025-04-17 18:17:10 Re: not null constraints, again
Previous Message Thomas Munro 2025-04-17 15:54:31 Re: Changing shared_buffers without restart