Re: Support NOT VALID / VALIDATE constraint options for named NOT NULL constraints
Rushabh Lathia <rushabh.lathia@gmail.com>
From: Rushabh Lathia <rushabh.lathia@gmail.com>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: jian he <jian.universality@gmail.com>, PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2025-04-07T08:28:22Z
Lists: pgsql-hackers
Attachments
- 0002-Remove-attnotnullvalid-again.patch (application/octet-stream) patch 0002
- 0001-Allow-NOT-NULL-constraints-to-be-added-as-NOT-VALID.patch (application/octet-stream) patch 0001
- 0003-Fix-relation-attribute-loop-correctly-and-also-set-t.patch (application/octet-stream) patch 0003
Thanks Alvaro.
On Mon, Apr 7, 2025 at 2:02 AM Alvaro Herrera <alvherre@alvh.no-ip.org>
wrote:
> On 2025-Apr-05, jian he wrote:
>
> > hi.
> > + /* FIXME use CompactAttribute */
> > Form_pg_attribute att = TupleDescAttr(relation->rd_att,
> i - 1);
> > if (att->attnotnull && att->attnotnullvalid &&
> > !att->attisdropped)
> > {
> > NullTest *ntest = makeNode(NullTest);
> > ntest->arg = (Expr *) makeVar(varno,
> > i,
> > att->atttypid,
> > att->atttypmod,
> > att->attcollation,
> > 0);
> > ntest->nulltesttype = IS_NOT_NULL;
> >
> > CompactAttribute doesn't have {atttypmod, attcollation} information,
> > now it is impossible to use CompactAttribute here,
> > so I removed this FIXME in get_relation_constraints.
>
> Ah, good point. In this new patch, I now consult both
> TupleDescCompactAttr (to consult attnullability) and then TupleDescAttr
> (to get collation etc), because now the information we need is split
> between those two places. It feels a bit nasty TBH.
>
> > i am uncomfortable with the change in
> > 'CREATE TABLE dump_test.test_table_generated'
> > so I only added 'CONSTRAINT NOT NULL / INVALID' tests in
> > 002_pg_dump.pl.
> > so I only added a test case 'CONSTRAINT NOT NULL / INVALID'
> > to 002_pg_dump.pl.
>
> Yeah, good call.
>
> > v7-0001 commit message explains what kind of problem
> > MergeWithExistingConstraint is trying to fix.
>
> Umm. I have done away with this again (and the parts in 0002 where you
> handle the equivalent issue for not-null constraints), because it is
> still not clear to me exactly what fails if you don't have them. I
> suggest that we should deal with this separately, and that a patch to
> deal with it should include a test case that fails if the code fix is
> not present.
>
> > v7-0002 bullet points summary about NOT NULL NOT VALID added to the
> > commit message.
>
> Thanks.
>
> > add a test for CREATE TABLE LIKE.
> > CREATE TABLE LIKE will copy the invalid not-null constraint and will
> become
> > valid, i think this is what we want.
>
> Yep, thanks.
>
> I removed the postgres_fdw changes. What I wrote was untested, and
> failed as soon as I added a trivial test. Needs more thought.
>
>
> I also did some more polish, and as I said in another email, it seems to
> me that this approach is also wrong, and that a better approach is to
> determine the value of CompactAttribute->attnullability using the
> pg_constraint scan at the time when the tupdesc is built. I coded this,
> and it almost works, but there are some spots that fail because some
> tuple descriptors are not built correctly. I'm not sure what to do with
> this at this stage; I would love to see this patch across the finish
> line, but it seems a little late now.
>
>
> Note: naturally, patch 0002 in this series would be squashed with 0001
> for commit. Patch 0003 is not for commit, just to show what the
> problems are. If you run the regression tests without 0003, there are
> surprisingly very few failures, and it's very clear that they are
> because of tuple descriptors that don't have the attnullability flag set
> correctly.
>
I reviewed the patch and found that the regression was failing because the
loop over
attributes in RelationBuildTupleDesc() were not executed correctly.
After applying the fix, I noticed one more test was failing, which turned
out to be related
to nullability handling for temporary tables.
Please find attached the 0003 patch, which addresses both issues.
Thanks,
>
> --
> Álvaro Herrera 48°01'N 7°57'E —
> https://www.EnterpriseDB.com/
> "La grandeza es una experiencia transitoria. Nunca es consistente.
> Depende en gran parte de la imaginación humana creadora de mitos"
> (Irulan)
>
--
Rushabh Lathia
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