mirror of
https://github.com/postgres/postgres.git
synced 2025-11-03 09:13:20 +03:00
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
This commit is contained in:
@@ -2678,6 +2678,26 @@ DROP TABLE range_parted2;
|
||||
SELECT * from part_rp;
|
||||
DROP TABLE part_rp;
|
||||
|
||||
-- concurrent detach
|
||||
CREATE TABLE range_parted2 (
|
||||
a int
|
||||
) PARTITION BY RANGE(a);
|
||||
CREATE TABLE part_rp PARTITION OF range_parted2 FOR VALUES FROM (0) to (100);
|
||||
BEGIN;
|
||||
-- doesn't work in a partition block
|
||||
ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
|
||||
COMMIT;
|
||||
CREATE TABLE part_rpd PARTITION OF range_parted2 DEFAULT;
|
||||
-- doesn't work if there's a default partition
|
||||
ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
|
||||
-- doesn't work for the default partition
|
||||
ALTER TABLE range_parted2 DETACH PARTITION part_rpd CONCURRENTLY;
|
||||
DROP TABLE part_rpd;
|
||||
-- works fine
|
||||
ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
|
||||
\d+ range_parted2
|
||||
DROP TABLE range_parted2;
|
||||
|
||||
-- Check ALTER TABLE commands for partitioned tables and partitions
|
||||
|
||||
-- cannot add/drop column to/from *only* the parent
|
||||
|
||||
Reference in New Issue
Block a user