Re: Support NOT VALID / VALIDATE constraint options for named NOT NULL constraints
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: Robert Haas <robertmhaas@gmail.com>,
Rushabh Lathia <rushabh.lathia@gmail.com>, Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>, PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2025-04-02T13:51:31Z
Lists: pgsql-hackers
Attachments
- v6-0001-ensure-pg_dump-table-constraint-info-remain-the-s.patch (text/x-patch) patch v6-0001
- scratch147.sql (application/sql)
> > ------------------------------------------------------------------------
> > i think your patch messed up with pg_constraint.conislocal.
> > for example:
> >
> > CREATE TABLE parted (id bigint default 1,id_abc bigint) PARTITION BY LIST (id);
> > alter TABLE parted add CONSTRAINT dummy_constr not null id not valid;
> > CREATE TABLE parted_1 (id bigint default 1,id_abc bigint);
> > alter TABLE parted_1 add CONSTRAINT dummy_constr not null id;
> > ALTER TABLE parted ATTACH PARTITION parted_1 FOR VALUES IN ('1');
>
> It's still not clear to me what to do to fix this problem. But while
> trying to understand it, I had the chance to rework the pg_dump code
> somewhat, so here it is. Feel free to propose fixes on top of this.
we need special code for handing parent is invalid, child is valid
(table inheritance or partitioning).
To do that we need to check if these valid children have invalid
parent constraints or not.
Therefore another ExecuteSqlQuery query execution is needed.
it may cause performance problems for pg_dump, I guess.
There may be other simple ways to do it.
just sharing what i comp up with.
I also attached a sql test file. i tested all the check constraints,
not-null constraints
where parent is invalid while child is valid scarenio.
then i use
``
create table after_dump as
select conrelid::regclass::text, conname, convalidated, coninhcount,
conislocal, conparentid, contype
from pg_constraint
where conrelid::regclass::text = ANY('{...}';
``
to query before and after dumping whether pg_constraint information
remains as is or not.
The attached patch is based on v6-0001-NOT-NULL-NOT-VALID.patch.
It resolves the issue described in [1] and [2], (sql example demo)
where pg_dump restore failed to retain all pg_constraint properties in cases
where the child constraint was valid while the parent was invalid.
[1] https://postgr.es/m/CACJufxECVsdWSC4J0wo2LF-+QoacsfX_Scv-NGzQxWjzPF1coA@mail.gmail.com
[2] https://postgr.es/m/CACJufxGnXTj59WM_qqH_JNQ2xC8HQNbJdhAiXnCS2vr3j_17GA@mail.gmail.com
Commits
-
Allow NOT NULL constraints to be added as NOT VALID
- a379061a22a8 18.0 landed
-
Add missing deparsing of [NO] IDENT to XMLSERIALIZE()
- 984410b92326 18.0 cited
-
backend launchers void * arguments for binary data
- 7202d72787d3 18.0 cited
-
Add pg_constraint rows for not-null constraints
- 14e87ffa5c54 18.0 cited
-
Create the infrastructure for planner support functions.
- 1fb57af92069 12.0 cited
-
Add prokind column, replacing proisagg and proiswindow
- fd1a421fe661 11.0 cited