pgsql: Better handle indirect constraint drops

From: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Better handle indirect constraint drops
Date: 2024-04-19 10:38:15
Message-ID: E1rxldH-002pB7-7m@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Better handle indirect constraint drops

It is possible for certain cases to remove not-null constraints without
maintaining the attnotnull in its correct state; for example if you drop
a column that's part of the primary key, and the other columns of the PK don't
have not-null constraints, then we should reset the attnotnull flags for
those other columns; up to this commit, we didn't. Handle those cases
better by doing the attnotnull reset in RemoveConstraintById() instead
of in dropconstraint_internal().

However, there are some cases where we must not do so. For example if
those other columns are in replica identity indexes or are generated
identity columns, we must keep attnotnull set, even though it results in
the catalog inconsistency that no not-null constraint supports that.

Because the attnotnull reset now happens in more places than before, for
instance when a column of the primary key changes type, we need an
additional trick to reinstate it as necessary. Introduce a new
alter-table pass that does this, which needs simply reschedule some
AT_SetAttNotNull subcommands that were already being generated and
ignored.

Because of the exceptions in which attnotnull is not reset noted above,
we also include a pg_dump hack to include a not-null constraint when the
attnotnull flag is set even if no pg_constraint row exists. This part
is undesirable but necessary, because failing to handle the case can
result in unrestorable dumps.

Reported-by: Tender Wang <tndrwang(at)gmail(dot)com>
Co-authored-by: Tender Wang <tndrwang(at)gmail(dot)com>
Reviewed-by: jian he <jian(dot)universality(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAHewXN=hMbNa3d43NOR=OCgdgpTt18S-1fmueCoEGesyeK4bqw@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/0cd711271d42b0888d36f8eda50e1092c2fed4b3

Modified Files
--------------
src/backend/catalog/pg_constraint.c | 117 +++++++++++++++++++++++++-
src/backend/commands/tablecmds.c | 135 +++++++++++++++---------------
src/bin/pg_dump/pg_dump.c | 30 +++++--
src/test/regress/expected/constraints.out | 78 +++++++++++++++++
src/test/regress/sql/constraints.sql | 39 +++++++++
5 files changed, 323 insertions(+), 76 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alvaro Herrera 2024-04-19 11:59:31 Re: pgsql: Fix restore of not-null constraints with inheritance
Previous Message Dean Rasheed 2024-04-19 08:41:34 pgsql: Use macro NUM_MERGE_MATCH_KINDS instead of '3' in MERGE code.