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: Tender Wang <tndrwang@gmail.com>, Pg Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2024-10-01T20:12:45Z
Lists: pgsql-hackers

Attachments

On 2024-Oct-01, jian he wrote:

> ATExecDropInherit
>     /*
>      * If the parent has a primary key, then we decrement counts for all NOT
>      * NULL constraints
>      */
>     ObjectAddressSet(address, RelationRelationId,
>                      RelationGetRelid(parent_rel));
> 
> only not-null constraint,
> with ALTER TABLE NO INHERIT we still decrement counts for not-null constraints.
> I feel the comment is in the wrong place?

Yeah, I think it's uselessly left over after removing some code that was
there.  I dropped it.

> please check the attached function MergeConstraintsIntoExisting refactoring
> 1. make it error check more confined within CONSTRAINT_CHECK and
> CONSTRAINT_NOTNULL.

I had already rewritten this, as I mentioned in an earlier response; I
just made the error check apply regardless of constraint type.  Note
that in your patch, you left a useless comment in place.

> 2. since get_attname will do system cache search, we can just use
> Relation->rd_att and TupleDescAttr

Hmm, you're right, but I think we can do even better than that: we
should use an AttrMap to map the column numbers from parent to child
without having the compare column names.  This should be much faster.

I also re-verified pg_upgrade from earlier releases (I tried 15 and 17).
There was a problem whereby we'd bogusly end up with some constraints
having islocal=true, because the UPDATE pg_constraint that tries to flip
it false doesn't have a constraint name to reference.   I made it use
'conkey' in the WHERE for that case; with that, the tests pass.

I pushed this branch to my github clone [1] here, and Cirrus is running
it here [2].
[1] https://github.com/alvherre/postgres/commits/notnull-init-18/
[2] https://cirrus-ci.com/build/6343292823011328

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
"Nunca confiaré en un traidor.  Ni siquiera si el traidor lo he creado yo"
(Barón Vladimir Harkonnen)

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