v52-0001-refactor-relpersistence-related-issue.no-cfbot

application/octet-stream

Filename: v52-0001-refactor-relpersistence-related-issue.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: Add SPLIT PARTITION/MERGE PARTITIONS commands
From a1c779ff030b9b2ffaeb15806cafe0c7fed34f43 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Thu, 21 Aug 2025 14:28:33 +0800
Subject: [PATCH v52 1/1] refactor relpersistence related issue

---
 src/backend/commands/tablecmds.c              |  1 -
 src/test/regress/expected/partition_merge.out | 60 +++++++++++--------
 src/test/regress/sql/partition_merge.sql      | 51 ++++++++--------
 3 files changed, 62 insertions(+), 50 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index cc24a92c504..0d520745670 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -22793,7 +22793,6 @@ ATExecMergePartitions(List **wqueue, AlteredTableInfo *tab, Relation rel,
 	 * stmt->relation as RELPERSISTENCE_TEMP if a temporary namespace is
 	 * selected.
 	 */
-	cmd->name->relpersistence = rel->rd_rel->relpersistence;
 	RangeVarGetAndCheckCreationNamespace(cmd->name, NoLock, &existingRelid);
 
 	/*
diff --git a/src/test/regress/expected/partition_merge.out b/src/test/regress/expected/partition_merge.out
index 5159fd37d82..00ed2516c68 100644
--- a/src/test/regress/expected/partition_merge.out
+++ b/src/test/regress/expected/partition_merge.out
@@ -656,28 +656,6 @@ DROP TABLE t3;
 DROP TABLE t2;
 DROP TABLE t1;
 --
--- Try to MERGE partitions of temporary table.
---
-CREATE TEMP TABLE t (i int) PARTITION BY RANGE (i);
-CREATE TEMP TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
-CREATE TEMP TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
-EXECUTE get_partition_info('{t}');
-  oid   | relpersistence | relkind | inhdetachpending |        pg_get_expr         
---------+----------------+---------+------------------+----------------------------
- tp_0_1 | t              | r       | f                | FOR VALUES FROM (0) TO (1)
- tp_1_2 | t              | r       | f                | FOR VALUES FROM (1) TO (2)
-(2 rows)
-
-ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
--- Partition should be temporary.
-EXECUTE get_partition_info('{t}');
-  oid   | relpersistence | relkind | inhdetachpending |        pg_get_expr         
---------+----------------+---------+------------------+----------------------------
- tp_0_2 | t              | r       | f                | FOR VALUES FROM (0) TO (2)
-(1 row)
-
-DROP TABLE t;
---
 -- Check the partition index name if the partition name is the same as one
 -- of the merged partitions.
 --
@@ -703,8 +681,37 @@ Not-null constraints:
 
 DROP TABLE t;
 --
+-- Try to MERGE partitions of temporary table.
+--
+BEGIN;
+SHOW search_path;
+           search_path           
+---------------------------------
+ partitions_merge_schema, public
+(1 row)
+
+CREATE TEMP TABLE t (i int) PARTITION BY RANGE (i) ON COMMIT DROP;
+CREATE TEMP TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
+CREATE TEMP TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
+CREATE TEMP TABLE tp_2_3 PARTITION OF t FOR VALUES FROM (2) TO (3);
+CREATE TEMP TABLE tp_3_4 PARTITION OF t FOR VALUES FROM (3) TO (4);
+ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO pg_temp.tp_0_2;
+ALTER TABLE t MERGE PARTITIONS (tp_0_2, tp_2_3) INTO pg_temp.tp_0_3;
+-- Partition should be temporary.
+EXECUTE get_partition_info('{t}');
+  oid   | relpersistence | relkind | inhdetachpending |        pg_get_expr         
+--------+----------------+---------+------------------+----------------------------
+ tp_0_3 | t              | r       | f                | FOR VALUES FROM (0) TO (3)
+ tp_3_4 | t              | r       | f                | FOR VALUES FROM (3) TO (4)
+(2 rows)
+
+ALTER TABLE t MERGE PARTITIONS (tp_0_3, tp_3_4) INTO tp_0_4; --error
+ERROR:  cannot create a permanent relation as partition of temporary relation "t"
+ROLLBACK;
+--
 -- Try mixing permanent and temporary partitions.
 --
+BEGIN;
 SET search_path = partitions_merge_schema, pg_temp, public;
 CREATE TABLE t (i int) PARTITION BY RANGE (i);
 CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
@@ -722,17 +729,19 @@ EXECUTE get_partition_info('{t}');
  tp_1_2 | p              | r       | f                | FOR VALUES FROM (1) TO (2)
 (2 rows)
 
+SAVEPOINT s;
 SET search_path = pg_temp, partitions_merge_schema, public;
 -- Can't merge persistent partitions into a temporary partition
 ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
 ERROR:  cannot create a temporary relation as partition of permanent relation "t"
+ROLLBACK TO SAVEPOINT s;
 SET search_path = partitions_merge_schema, public;
 -- Can't merge persistent partitions into a temporary partition
 ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO pg_temp.tp_0_2;
 ERROR:  cannot create a temporary relation as partition of permanent relation "t"
-DROP TABLE t;
-SET search_path = pg_temp, partitions_merge_schema, public;
+ROLLBACK;
 BEGIN;
+SET search_path = pg_temp, partitions_merge_schema, public;
 CREATE TABLE t (i int) PARTITION BY RANGE (i);
 CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
 CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
@@ -749,11 +758,12 @@ EXECUTE get_partition_info('{t}');
  tp_1_2 | t              | r       | f                | FOR VALUES FROM (1) TO (2)
 (2 rows)
 
-DEALLOCATE get_partition_info;
 SET search_path = partitions_merge_schema, pg_temp, public;
 -- Can't merge temporary partitions into a persistent partition
 ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
+ERROR:  cannot create a permanent relation as partition of temporary relation "t"
 ROLLBACK;
+DEALLOCATE get_partition_info;
 -- Check the new partition inherits parent's tablespace
 SET search_path = partitions_merge_schema, public;
 CREATE TABLE t (i int PRIMARY KEY USING INDEX TABLESPACE regress_tblspace)
diff --git a/src/test/regress/sql/partition_merge.sql b/src/test/regress/sql/partition_merge.sql
index bb5386fe381..0812d33186c 100644
--- a/src/test/regress/sql/partition_merge.sql
+++ b/src/test/regress/sql/partition_merge.sql
@@ -449,21 +449,6 @@ DROP TABLE t3;
 DROP TABLE t2;
 DROP TABLE t1;
 
---
--- Try to MERGE partitions of temporary table.
---
-CREATE TEMP TABLE t (i int) PARTITION BY RANGE (i);
-CREATE TEMP TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
-CREATE TEMP TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
-
-EXECUTE get_partition_info('{t}');
-
-ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
-
--- Partition should be temporary.
-EXECUTE get_partition_info('{t}');
-
-DROP TABLE t;
 
 --
 -- Check the partition index name if the partition name is the same as one
@@ -483,48 +468,66 @@ ALTER TABLE t MERGE PARTITIONS (tp_1_2, tp_0_1) INTO tp_1_2;
 
 DROP TABLE t;
 
+--
+-- Try to MERGE partitions of temporary table.
+--
+BEGIN;
+SHOW search_path;
+CREATE TEMP TABLE t (i int) PARTITION BY RANGE (i) ON COMMIT DROP;
+CREATE TEMP TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
+CREATE TEMP TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
+CREATE TEMP TABLE tp_2_3 PARTITION OF t FOR VALUES FROM (2) TO (3);
+CREATE TEMP TABLE tp_3_4 PARTITION OF t FOR VALUES FROM (3) TO (4);
+
+ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO pg_temp.tp_0_2;
+ALTER TABLE t MERGE PARTITIONS (tp_0_2, tp_2_3) INTO pg_temp.tp_0_3;
+
+-- Partition should be temporary.
+EXECUTE get_partition_info('{t}');
+
+ALTER TABLE t MERGE PARTITIONS (tp_0_3, tp_3_4) INTO tp_0_4; --error
+ROLLBACK;
+
 --
 -- Try mixing permanent and temporary partitions.
 --
+BEGIN;
 SET search_path = partitions_merge_schema, pg_temp, public;
 CREATE TABLE t (i int) PARTITION BY RANGE (i);
 CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
 CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
 
 SELECT c.oid::pg_catalog.regclass, c.relpersistence FROM pg_catalog.pg_class c WHERE c.oid = 't'::regclass;
-
 EXECUTE get_partition_info('{t}');
+SAVEPOINT s;
 
 SET search_path = pg_temp, partitions_merge_schema, public;
-
 -- Can't merge persistent partitions into a temporary partition
 ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
 
+ROLLBACK TO SAVEPOINT s;
 SET search_path = partitions_merge_schema, public;
-
 -- Can't merge persistent partitions into a temporary partition
 ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO pg_temp.tp_0_2;
-DROP TABLE t;
-
-SET search_path = pg_temp, partitions_merge_schema, public;
+ROLLBACK;
 
 BEGIN;
+SET search_path = pg_temp, partitions_merge_schema, public;
 CREATE TABLE t (i int) PARTITION BY RANGE (i);
 CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
 CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
 
 SELECT c.oid::pg_catalog.regclass, c.relpersistence FROM pg_catalog.pg_class c WHERE c.oid = 't'::regclass;
-
 EXECUTE get_partition_info('{t}');
 
-DEALLOCATE get_partition_info;
-
 SET search_path = partitions_merge_schema, pg_temp, public;
 
 -- Can't merge temporary partitions into a persistent partition
 ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
 ROLLBACK;
 
+DEALLOCATE get_partition_info;
+
 -- Check the new partition inherits parent's tablespace
 SET search_path = partitions_merge_schema, public;
 CREATE TABLE t (i int PRIMARY KEY USING INDEX TABLESPACE regress_tblspace)
-- 
2.34.1