Re: not null constraints, again

Alvaro Herrera <alvherre@alvh.no-ip.org>

From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: jian he <jian.universality@gmail.com>
Cc: Pg Hackers <pgsql-hackers@lists.postgresql.org>, Tender Wang <tndrwang@gmail.com>
Date: 2024-11-07T19:16:32Z
Lists: pgsql-hackers

Attachments

On 2024-Nov-07, jian he wrote:

> drop table if exists part, part0 cascade;
> create table part (a int not null) partition by range (a);
> create table part0 (a int not null);
> alter table part attach partition part0 for values from (0) to (1000);
> alter table ONLY part0 add primary key(a);
> alter table part alter column a drop not null;
> 
> as the example shows that part0 not-null constraint is still there.
> that means:
> 
> perform <literal>DROP NOT NULL</literal> on the parent table
> will not drop the <literal>NOT NULL</literal> constraint from all partitions.
> 
> so we need rephrase the following sentence:
> 
> To drop the <literal>NOT NULL</literal> constraint from all the
>       partitions, perform <literal>DROP NOT NULL</literal> on the parent
>       table.
> 
> to address this kind of corner case?

I've been mulling over this and I'm not very sure I want to change the
docs over this point.  I think it's fine to leave it as is; otherwise it
becomes too verbose for a very esoteric corner case that has (what I
think is) an obvious explanation: the primary key in the child table
requires that the not-null constraint remains, so it does.  Do you
disagree?

Here's v11, which I intended to commit today, but didn't get around to.
CI is happy with it, so I'll probably do it tomorrow first thing.

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
"I'm impressed how quickly you are fixing this obscure issue. I came from 
MS SQL and it would be hard for me to put into words how much of a better job
you all are doing on [PostgreSQL]."
 Steve Midgley, http://archives.postgresql.org/pgsql-sql/2008-08/msg00000.php

Commits

  1. Suppress "may be used uninitialized" warnings from older compilers.

  2. Elide not-null constraint checks on child tables during PK creation

  3. Remove unnecessary code to handle CONSTR_NOTNULL

  4. Silence compilers about extractNotNullColumn()

  5. Add pg_constraint rows for not-null constraints