Re: Support NOT VALID / VALIDATE constraint options for named NOT NULL constraints - Mailing list pgsql-hackers

From Alvaro Herrera
Subject Re: Support NOT VALID / VALIDATE constraint options for named NOT NULL constraints
Date
Msg-id 202503211450.4lps7qf3jutr@alvherre.pgsql
Whole thread Raw
In response to Re: Support NOT VALID / VALIDATE constraint options for named NOT NULL constraints  (jian he <jian.universality@gmail.com>)
List pgsql-hackers
On 2025-Mar-21, jian he wrote:

> * if partitioned table have valid not-null, then partition with
> invalid not-null can not attach to the partition tree.

Correct.

>   if partitioned table have not valid not-null, we *can* attach a
> valid not-null to the partition tree.

Also correct.

>   (inheritance hierarchy behaves the same).

Good -- it should! :-)

> this part does not require a lot of code changes.
> However, to make the pg_dump working with partitioned table we need to
> tweak AdjustNotNullInheritance a little bit.

Hmm, well, modifying a function to suite what we need it to do is part
of code patching :-)

> i changed this accordingly.
> ALTER TABLE .. ALTER COLUMN .. SET NOT NULL
> will validate not-null and set attnotnull, attinvalidnotnull accordingly.

Okay.


> i basically model NOT NULL NOT VALID == CHECK (x IS NOT NULL).
> i think your idea may need more refactoring?
> all the "if (attr->attnotnull" need change to "if (attr->attnotnull &&
> attr->attnotnullvalid)"
> or am i missing something?

In some places, yes we will need to change like that.  However, many
places do not need to change like that.  In particular, (most?) client
applications do not necessarily need that change, and to me, that's the
most important part, because we do not control external applications,
and --as I said upthread-- we do not have the luxury of breaking them.

> Anyway, I will just share my idea first, and will explore your idea later.

Thank you.

> in my attached patch, you will only create an not-null not valid
> pg_constraint entry
> If `if (constr->contype == CONSTR_NOTNULL && constr->skip_validation)`
> in ATAddCheckNNConstraint conditions are satisfied.
> 
> 
> imho, my approach is less bug-prone, almost no need to refactor current code.

I'll give this a look ... probably won't have time today ...  however,
IMO the consideration of external applications (ORMs, LibreOffice, admin
GUIs, etc) not breaking is the most important thing to keep in mind.
You can go over the code found by codesearch.debian.net when searching
for `attnotnull` to see the sort of code that would be affected.

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
"¿Qué importan los años?  Lo que realmente importa es comprobar que
a fin de cuentas la mejor edad de la vida es estar vivo"  (Mafalda)



pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: Update Unicode data to Unicode 16.0.0
Next
From: Andres Freund
Date:
Subject: Re: Snapshot related assert failure on skink