Re: NOT NULL NOT ENFORCED - Mailing list pgsql-hackers

From jian he
Subject Re: NOT NULL NOT ENFORCED
Date
Msg-id CACJufxFQqA-BV58_nZViFX=WAQc4kn4SJk0gE7Te+kQFdNf9aQ@mail.gmail.com
Whole thread Raw
In response to Re: NOT NULL NOT ENFORCED  (Álvaro Herrera <alvherre@kurilemu.de>)
Responses Re: NOT NULL NOT ENFORCED
List pgsql-hackers
On Thu, Sep 4, 2025 at 8:00 PM Álvaro Herrera <alvherre@kurilemu.de> wrote:
>
> > @@ -9937,9 +9962,9 @@ ATAddCheckNNConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
> >                * If adding a valid not-null constraint, set the pg_attribute flag
> >                * and tell phase 3 to verify existing rows, if needed.  For an
> >                * invalid constraint, just set attnotnull, without queueing
> > -              * verification.
> > +              * verification. For not enforced not-null, no need set attnotnull.
> >                */
> > -             if (constr->contype == CONSTR_NOTNULL)
> > +             if (constr->contype == CONSTR_NOTNULL && ccon->is_enforced)
> >                       set_attnotnull(wqueue, rel, ccon->attnum,
> >                                                  !constr->skip_validation,
> >                                                  !constr->skip_validation);
>
> Didn't we decide that attnotnull meant whether a constraint exists at
> all, without making a judgement on whether it's enforced or valid?  The
> important change should be in CheckNNConstraintFetch() which should
> determine attnullability in a way that allows executor know whether the
> column is nullable or not.  I admit we might want to handle this
> differently for unenforced constraints, but we should discuss that and
> make sure that's what we want.
>

In CheckNNConstraintFetch, I changed it to
"""
        if (conform->contype == CONSTRAINT_NOTNULL)
        {
            if (!conform->convalidated && conform->conenforced)
            {
                AttrNumber    attnum;
                attnum = extractNotNullColumn(htup);
                Assert(relation->rd_att->compact_attrs[attnum -
1].attnullability ==
                       ATTNULLABLE_UNKNOWN);
                relation->rd_att->compact_attrs[attnum - 1].attnullability =
                    ATTNULLABLE_INVALID;
            }
            continue;
        }
"""

set pg_attribute.attnotnull to true for not-valid not-null is still
useful for INSERT/UPDATE.
set pg_attribute.attnotnull to true for not-enforced not-null
constraints doesn't have real benefits, IMHO.

If we let pg_attribute.attnotnull to true for not-enforced not-null,
then do we need to change the definition of
TupleConstr->has_not_null?



pgsql-hackers by date:

Previous
From: Ilia Evdokimov
Date:
Subject: Re: Use merge-based matching for MCVs in eqjoinsel
Next
From: Ilia Evdokimov
Date:
Subject: Re: Use merge-based matching for MCVs in eqjoinsel