Re: not null constraints, again
Tender Wang <tndrwang@gmail.com>
From: Tender Wang <tndrwang@gmail.com>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: jian he <jian.universality@gmail.com>, Pg Hackers <pgsql-hackers@lists.postgresql.org>, Tom Lane <tgl@sss.pgh.pa.us>
Date: 2025-04-17T17:01:14Z
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
Commits
-
Suppress "may be used uninitialized" warnings from older compilers.
- fc5e966f73f0 18.0 landed
-
Elide not-null constraint checks on child tables during PK creation
- 11ff192b5bb7 18.0 landed
-
Remove unnecessary code to handle CONSTR_NOTNULL
- 5b291d1c9c09 18.0 landed
-
Silence compilers about extractNotNullColumn()
- ff239c3bf4e8 18.0 landed
-
Add pg_constraint rows for not-null constraints
- 14e87ffa5c54 18.0 landed