Remove assertion for ALTER TABLE .. DETACH PARTITION CONCURRENTLY
authorMichael Paquier <michael@paquier.xyz>
Wed, 22 Dec 2021 06:38:00 +0000 (15:38 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 22 Dec 2021 06:38:00 +0000 (15:38 +0900)
One code path related to this flavor of ALTER TABLE was checking that
the relation to detach has to be a normal table or a partitioned table,
which would fail if using the command with a different relation kind.

Views, sequences and materialized views cannot be part of a partition
tree, so these would cause the command to fail anyway, but the assertion
was triggered.  Foreign tables can be part of a partition tree, and
again the assertion would have failed.  The simplest solution is just to
remove this assertion, so as we get the same failure as the
non-concurrent code path.

While on it, add a regression test in postgres_fdw for the concurrent
partition detach of a foreign table, as per a suggestion from Alexander
Lakhin.

Issue introduced in 71f4c8c.

Reported-by: Alexander Lakhin
Author: Michael Paquier, Alexander Lakhin
Reviewed-by: Peter Eisentraut, Kyotaro Horiguchi
Discussion: https://postgr.es/m/17339-a9e09aaf38a3457a@postgresql.org
Backpatch-through: 14

contrib/postgres_fdw/expected/postgres_fdw.out
contrib/postgres_fdw/sql/postgres_fdw.sql
src/backend/commands/tablecmds.c

index 5196e4797a6ccb6c737c90184f3939bb3052770d..7720ab9c58185aaa6302948476ccc79a133ffe8d 100644 (file)
@@ -6485,6 +6485,9 @@ CREATE FOREIGN TABLE foreign_tbl (a int, b int)
   SERVER loopback OPTIONS (table_name 'child_tbl');
 CREATE TABLE parent_tbl (a int, b int) PARTITION BY RANGE(a);
 ALTER TABLE parent_tbl ATTACH PARTITION foreign_tbl FOR VALUES FROM (0) TO (100);
+-- Detach and re-attach once, to stress the concurrent detach case.
+ALTER TABLE parent_tbl DETACH PARTITION foreign_tbl CONCURRENTLY;
+ALTER TABLE parent_tbl ATTACH PARTITION foreign_tbl FOR VALUES FROM (0) TO (100);
 CREATE VIEW rw_view AS SELECT * FROM parent_tbl
   WHERE a < b WITH CHECK OPTION;
 \d+ rw_view
index 666d21962aac7c2a30d2bdba4adc84076ebc0736..beeac8af1ede4ad26a35c196a49d01154eb3c252 100644 (file)
@@ -1490,6 +1490,9 @@ CREATE FOREIGN TABLE foreign_tbl (a int, b int)
 
 CREATE TABLE parent_tbl (a int, b int) PARTITION BY RANGE(a);
 ALTER TABLE parent_tbl ATTACH PARTITION foreign_tbl FOR VALUES FROM (0) TO (100);
+-- Detach and re-attach once, to stress the concurrent detach case.
+ALTER TABLE parent_tbl DETACH PARTITION foreign_tbl CONCURRENTLY;
+ALTER TABLE parent_tbl ATTACH PARTITION foreign_tbl FOR VALUES FROM (0) TO (100);
 
 CREATE VIEW rw_view AS SELECT * FROM parent_tbl
   WHERE a < b WITH CHECK OPTION;
index bf42587e383440c3adea0202a48110751259b5f8..45e59e3d5c6d00d6e3d3838f43d2ff6bb7b10a87 100644 (file)
@@ -15013,8 +15013,6 @@ MarkInheritDetached(Relation child_rel, Relation parent_rel)
    HeapTuple   inheritsTuple;
    bool        found = false;
 
-   Assert(child_rel->rd_rel->relkind == RELKIND_RELATION ||
-          child_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
    Assert(parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
 
    /*