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: ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY
Date: Thu, 25 Mar 2021 21:02:42 +0000
Message-ID: <E1lPX7q-0001lJ-NO@gemulon.postgresql.org> (raw)

ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY

Allow a partition be detached from its partitioned table without
blocking concurrent queries, by running in two transactions and only
requiring ShareUpdateExclusive in the partitioned table.

Because it runs in two transactions, it cannot be used in a transaction
block.  This is the main reason to use dedicated syntax: so that users
can choose to use the original mode if they need it.  But also, it
doesn't work when a default partition exists (because an exclusive lock
would still need to be obtained on it, in order to change its partition
constraint.)

In case the second transaction is cancelled or a crash occurs, there's
ALTER TABLE .. DETACH PARTITION .. FINALIZE, which executes the final
steps.

The main trick to make this work is the addition of column
pg_inherits.inhdetachpending, initially false; can only be set true in
the first part of this command.  Once that is committed, concurrent
transactions that use a PartitionDirectory will include or ignore
partitions so marked: in optimizer they are ignored if the row is marked
committed for the snapshot; in executor they are always included.  As a
result, and because of the way PartitionDirectory caches partition
descriptors, queries that were planned before the detach will see the
rows in the detached partition and queries that are planned after the
detach, won't.

A CHECK constraint is created that duplicates the partition constraint.
This is probably not strictly necessary, and some users will prefer to
remove it afterwards, but if the partition is re-attached to a
partitioned table, the constraint needn't be rechecked.

Author: Álvaro Herrera <alvherre@alvh.no-ip.org>
Reviewed-by: Amit Langote <amitlangote09@gmail.com>
Reviewed-by: Justin Pryzby <pryzby@telsasoft.com>
Discussion: https://postgr.es/m/20200803234854.GA24158@alvherre.pgsql

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/71f4c8c6f74ba021e55d35b1128d22fb8c6e1629

Modified Files
--------------
doc/src/sgml/catalogs.sgml                         |  10 +
doc/src/sgml/ref/alter_table.sgml                  |  26 +-
src/backend/catalog/heap.c                         |  20 +-
src/backend/catalog/index.c                        |   4 +-
src/backend/catalog/partition.c                    |  38 +-
src/backend/catalog/pg_inherits.c                  | 114 ++++-
src/backend/commands/indexcmds.c                   |   6 +-
src/backend/commands/tablecmds.c                   | 550 +++++++++++++++++----
src/backend/commands/trigger.c                     |   5 +-
src/backend/executor/execPartition.c               |  29 +-
src/backend/nodes/copyfuncs.c                      |   1 +
src/backend/nodes/equalfuncs.c                     |   1 +
src/backend/optimizer/util/plancat.c               |   8 +-
src/backend/parser/gram.y                          |  23 +-
src/backend/partitioning/partbounds.c              |   6 +-
src/backend/partitioning/partdesc.c                |  21 +-
src/backend/tcop/utility.c                         |  19 +
src/backend/utils/adt/ri_triggers.c                |   6 +-
src/backend/utils/cache/partcache.c                |  12 +-
src/bin/psql/describe.c                            |  42 +-
src/include/catalog/catversion.h                   |   2 +-
src/include/catalog/partition.h                    |   2 +-
src/include/catalog/pg_inherits.h                  |   8 +-
src/include/nodes/parsenodes.h                     |   2 +
src/include/parser/kwlist.h                        |   1 +
src/include/partitioning/partdesc.h                |   5 +-
src/include/utils/snapmgr.h                        |   1 +
.../expected/detach-partition-concurrently-1.out   | 234 +++++++++
.../expected/detach-partition-concurrently-2.out   |  66 +++
.../expected/detach-partition-concurrently-3.out   | 282 +++++++++++
.../expected/detach-partition-concurrently-4.out   | 351 +++++++++++++
src/test/isolation/isolation_schedule              |   4 +
.../specs/detach-partition-concurrently-1.spec     |  69 +++
.../specs/detach-partition-concurrently-2.spec     |  41 ++
.../specs/detach-partition-concurrently-3.spec     |  66 +++
.../specs/detach-partition-concurrently-4.spec     |  74 +++
src/test/modules/delay_execution/Makefile          |   3 +-
.../expected/partition-removal-1.out               | 175 +++++++
.../delay_execution/specs/partition-removal-1.spec |  58 +++
src/test/regress/expected/alter_table.out          |  29 ++
src/test/regress/sql/alter_table.sql               |  20 +
41 files changed, 2266 insertions(+), 168 deletions(-)



Message-ID: <E1lPX7q-0001lJ-NO@gemulon.postgresql.org>
Permalink:  ../E1lPX7q-0001lJ-NO@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1lPX7q-0001lJ-NO@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: alvherre@alvh.no-ip.org
  Subject: Re: pgsql: ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY
  In-Reply-To: <E1lPX7q-0001lJ-NO@gemulon.postgresql.org>
  References: <E1lPX7q-0001lJ-NO@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