Re: not null constraints, again

jian he <jian.universality@gmail.com>

From: jian he <jian.universality@gmail.com>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: Tender Wang <tndrwang@gmail.com>, Pg Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2024-10-03T07:16:43Z
Lists: pgsql-hackers
I thought SearchSysCacheCopyAttNum is expensive.
Relation->rd_att is enough for checking attnotnull.

What do you think of the following refactoring of set_attnotnull?

static void
set_attnotnull(List **wqueue, Relation rel, AttrNumber attnum,
               LOCKMODE lockmode)
{
    Oid            reloid = RelationGetRelid(rel);
    HeapTuple    tuple;
    Form_pg_attribute attForm;
    Form_pg_attribute attr;
    TupleDesc    tupleDesc;
    CheckAlterTableIsSafe(rel);
    tupleDesc = RelationGetDescr(rel);
    attr = TupleDescAttr(tupleDesc, attnum - 1);
    if (attr->attisdropped)
        return;
    if (!attr->attnotnull)
    {
        Relation    attr_rel;
        attr_rel = table_open(AttributeRelationId, RowExclusiveLock);
        tuple = SearchSysCacheCopyAttNum(reloid, attnum);
        if (!HeapTupleIsValid(tuple))
            elog(ERROR, "cache lookup failed for attribute %d of relation %u",
                attnum, reloid);
        attForm = (Form_pg_attribute) GETSTRUCT(tuple);
        attForm->attnotnull = true;
        CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple);
        if (wqueue && !NotNullImpliedByRelConstraints(rel, attForm))
        {
            AlteredTableInfo *tab;
            tab = ATGetQueueEntry(wqueue, rel);
            tab->verify_new_notnull = true;
        }
        CommandCounterIncrement();
        heap_freetuple(tuple);
        table_close(attr_rel, RowExclusiveLock);
    }
}



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