pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Disallow direct change of NO INHERIT of not-null constraints
Date: Thu, 02 May 2024 15:28:35 +0000
Message-ID: <E1s2YMM-0019v8-QJ@gemulon.postgresql.org> (raw)

Disallow direct change of NO INHERIT of not-null constraints

We support changing NO INHERIT constraint to INHERIT for constraints in
child relations when adding a constraint to some ancestor relation, and
also during pg_upgrade's schema restore; but other than those special
cases, command ALTER TABLE ADD CONSTRAINT should not be allowed to
change an existing constraint from NO INHERIT to INHERIT, as that would
require to process child relations so that they also acquire an
appropriate constraint, which we may not be in a position to do.  (It'd
also be surprising behavior.)

It is conceivable that we want to allow ALTER TABLE SET NOT NULL to make
such a change; but in that case some more code is needed to implement it
correctly, so for now I've made that throw the same error message.

Also, during the prep phase of ALTER TABLE ADD CONSTRAINT, acquire locks
on all descendant tables; otherwise we might operate on child tables on
which no locks are held, particularly in the mode where a primary key
causes not-null constraints to be created on children.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Discussion: https://postgr.es/m/7d923a66-55f0-3395-cd40-81c142b5448b@gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/d45597f72fe53a53f6271d5ba4e7acf8fc9308a1

Modified Files
--------------
doc/src/sgml/ref/alter_table.sgml     |  2 +-
src/backend/catalog/heap.c            | 20 +++++++++++++++-----
src/backend/catalog/pg_constraint.c   | 15 +++++++++++----
src/backend/commands/tablecmds.c      | 17 +++++++++++++++--
src/include/catalog/pg_constraint.h   |  2 +-
src/test/regress/expected/inherit.out | 30 +++++++++++++++++++++++++++++-
src/test/regress/sql/inherit.sql      | 20 ++++++++++++++++++++
7 files changed, 92 insertions(+), 14 deletions(-)



Message-ID: <E1s2YMM-0019v8-QJ@gemulon.postgresql.org>
Permalink:  ../E1s2YMM-0019v8-QJ@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1s2YMM-0019v8-QJ@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: alvherre@alvh.no-ip.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Disallow direct change of NO INHERIT of not-null constraints
  In-Reply-To: <E1s2YMM-0019v8-QJ@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox