pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix timing issue with ALTER TABLE's validate constraint
Date: Tue, 14 Jul 2020 05:02:51 +0000
Message-ID: <E1jvD5f-0007pz-L0@gemulon.postgresql.org> (raw)

Fix timing issue with ALTER TABLE's validate constraint

An ALTER TABLE to validate a foreign key in which another subcommand
already caused a pending table rewrite could fail due to ALTER TABLE
attempting to validate the foreign key before the actual table rewrite
takes place.  This situation could result in an error such as:

ERROR:  could not read block 0 in file "base/nnnnn/nnnnn": read only 0 of 8192 bytes

The failure here was due to the SPI call which validates the foreign key
trying to access an index which is yet to be rebuilt.

Similarly, we also incorrectly tried to validate CHECK constraints before
the heap had been rewritten.

The fix for both is to delay constraint validation until phase 3, after
the table has been rewritten.  For CHECK constraints this means a slight
behavioral change.  Previously ALTER TABLE VALIDATE CONSTRAINT on
inheritance tables would be validated from the bottom up.  This was
different from the order of evaluation when a new CHECK constraint was
added.  The changes made here aligns the VALIDATE CONSTRAINT evaluation
order for inheritance tables to be the same as ADD CONSTRAINT, which is
generally top-down.

Reported-by: Nazli Ugur Koyluoglu, using SQLancer
Discussion: https://postgr.es/m/CAApHDvp%3DZXv8wiRyk_0rWr00skhGkt8vXDrHJYXRMft3TjkxCA%40mail.gmail.com
Backpatch-through: 9.5 (all supported versions)

Branch
------
REL_13_STABLE

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

Modified Files
--------------
src/backend/commands/tablecmds.c          | 155 ++++++++++--------------------
src/test/regress/expected/alter_table.out |  22 ++++-
src/test/regress/sql/alter_table.sql      |  22 +++++
3 files changed, 93 insertions(+), 106 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1jvD5f-0007pz-L0@gemulon.postgresql.org>
Permalink:  ../E1jvD5f-0007pz-L0@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1jvD5f-0007pz-L0@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: drowley@postgresql.org
  Subject: Re: pgsql: Fix timing issue with ALTER TABLE's validate constraint
  In-Reply-To: <E1jvD5f-0007pz-L0@gemulon.postgresql.org>
  References: <E1jvD5f-0007pz-L0@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