From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix relcache inconsistency hazard in partition detach
Date: Thu, 22 Apr 2021 19:17:19 +0000
Message-ID: <E1lZepD-0004r9-4G@gemulon.postgresql.org> (raw)
Fix relcache inconsistency hazard in partition detach
During queries coming from ri_triggers.c, we need to omit partitions
that are marked pending detach -- otherwise, the RI query is tricked
into allowing a row into the referencing table whose corresponding row
is in the detached partition. Which is bogus: once the detach operation
completes, the row becomes an orphan.
However, the code was not doing that in repeatable-read transactions,
because relcache kept a copy of the partition descriptor that included
the partition, and used it in the RI query. This commit changes the
partdesc cache code to only keep descriptors that aren't dependent on
a snapshot (namely: those where no detached partition exist, and those
where detached partitions are included). When a partdesc-without-
detached-partitions is requested, we create one afresh each time; also,
those partdescs are stored in PortalContext instead of
CacheMemoryContext.
find_inheritance_children gets a new output *detached_exist boolean,
which indicates whether any partition marked pending-detach is found.
Its "include_detached" input flag is changed to "omit_detached", because
that name captures desired the semantics more naturally.
CreatePartitionDirectory() and RelationGetPartitionDesc() arguments are
identically renamed.
This was noticed because a buildfarm member that runs with relcache
clobbering, which would not keep the improperly cached partdesc, broke
one test, which led us to realize that the expected output of that test
was bogus. This commit also corrects that expected output.
Author: Amit Langote <amitlangote09@gmail.com>
Author: Álvaro Herrera <alvherre@alvh.no-ip.org>
Discussion: https://postgr.es/m/3269784.1617215412@sss.pgh.pa.us
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/8aba9322511f718f12b618470d8c07f0ee5f0700
Modified Files
--------------
src/backend/catalog/heap.c | 2 +-
src/backend/catalog/pg_inherits.c | 63 ++++++++-----
src/backend/commands/indexcmds.c | 4 +-
src/backend/commands/tablecmds.c | 40 ++++-----
src/backend/commands/trigger.c | 6 +-
src/backend/executor/execPartition.c | 19 ++--
src/backend/optimizer/util/plancat.c | 2 +-
src/backend/partitioning/partbounds.c | 6 +-
src/backend/partitioning/partdesc.c | 100 ++++++++++++++-------
src/include/catalog/pg_inherits.h | 4 +-
src/include/partitioning/partdesc.h | 14 ++-
.../expected/detach-partition-concurrently-4.out | 1 +
12 files changed, 160 insertions(+), 101 deletions(-)
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: alvherre@alvh.no-ip.org
Subject: Re: pgsql: Fix relcache inconsistency hazard in partition detach
In-Reply-To: <E1lZepD-0004r9-4G@gemulon.postgresql.org>
References: <E1lZepD-0004r9-4G@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