Re: using index or check in ALTER TABLE SET NOT NULL

From: Stephen Frost <sfrost(at)snowman(dot)net>
To: Sergei Kornilov <sk(at)zsrv(dot)org>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: using index or check in ALTER TABLE SET NOT NULL
Date: 2018-01-23 15:37:11
Message-ID: 20180123153711.GI2416@tamriel.snowman.net
Views: Raw Message | Whole Thread | Download mbox
Thread:
Lists: pgsql-hackers

Greetings Sergei,

* Sergei Kornilov (sk(at)zsrv(dot)org) wrote:
> I update patch and also rebase to current head. I hope now it is better aligned with project style

Thanks for updating it and continuing to work on it. I haven't done a
full review but there were a few things below that I thought could be
improved-

> @@ -5863,8 +5864,11 @@ ATExecSetNotNull(AlteredTableInfo *tab, Relation rel,
>
> CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple);
>
> - /* Tell Phase 3 it needs to test the constraint */
> - tab->new_notnull = true;
> + if (!NotNullImpliedByRelConstraints(rel, (Form_pg_attribute) GETSTRUCT(tuple)))
> + {
> + /* Tell Phase 3 it needs to test the constraint */
> + tab->verify_new_notnull = true;
> + }
>
> ObjectAddressSubSet(address, RelationRelationId,
> RelationGetRelid(rel), attnum);

This could really use some additional comments, imv. In particular, we
need to make it clear that verify_new_notnull only moves from the
initial 'false' value to 'true', since we could be asked to add multiple
NOT NULL constraints and if any of them aren't already covered by an
existing CHECK constraint then we need to perform the full table check.

> @@ -13618,15 +13662,14 @@ ComputePartitionAttrs(Relation rel, List *partParams, AttrNumber *partattrs,
> }
>
> /*
> - * PartConstraintImpliedByRelConstraint
> - * Does scanrel's existing constraints imply the partition constraint?
> + * ConstraintImpliedByRelConstraint
> + * Does scanrel's existing constraints imply given constraint
> *
> * Existing constraints includes its check constraints and column-level
> * NOT NULL constraints and partConstraint describes the partition constraint.
> */
> bool
> -PartConstraintImpliedByRelConstraint(Relation scanrel,
> - List *partConstraint)
> +ConstraintImpliedByRelConstraint(Relation scanrel, List *partConstraint)
> {
> List *existConstraint = NIL;
> TupleConstr *constr = RelationGetDescr(scanrel)->constr;

I would also rename 'partConstraint' since this function is no longer,
necessairly, working with a partition's constraint.

This could also really use some regression tests and I'd be sure to
include tests of adding multiple NOT NULL constraints, sometimes where
they're all covered by existing CHECK constraints and other times when
only one or the other is (and there's existing NULL values in the other
column), etc.

Thanks!

Stephen

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2018-01-23 15:45:37 Re: [HACKERS] Refactor handling of database attributes between pg_dump and pg_dumpall
Previous Message Daniel Gustafsson 2018-01-23 15:22:22 Re: [HACKERS] Refactoring identifier checks to consistently use strcmp