v31-0003-Additional-patch-for-ALTER-TABLE-.-MERGE-PARTITI.patch

text/plain

Filename: v31-0003-Additional-patch-for-ALTER-TABLE-.-MERGE-PARTITI.patch
Type: text/plain
Part: 2
Message: Re: Add SPLIT PARTITION/MERGE PARTITIONS commands

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v31-0003
Subject: Additional patch for ALTER TABLE ... MERGE PARTITIONS ... command
File+
src/backend/commands/tablecmds.c 33 34
src/test/regress/expected/partition_merge.out 1 1
From 233c0c414bc1d952d98b2eef4810f881e6afb24d Mon Sep 17 00:00:00 2001
From: Koval Dmitry <d.koval@postgrespro.ru>
Date: Fri, 5 Apr 2024 14:15:29 +0300
Subject: [PATCH v31 3/3] Additional patch for ALTER TABLE ... MERGE PARTITIONS
 ... command

---
 src/backend/commands/tablecmds.c              | 67 +++++++++----------
 src/test/regress/expected/partition_merge.out |  2 +-
 2 files changed, 34 insertions(+), 35 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 582890a302..e79aeef920 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -21431,7 +21431,6 @@ ATExecMergePartitions(List **wqueue, AlteredTableInfo *tab, Relation rel,
 	ListCell   *listptr;
 	List	   *mergingPartitionsList = NIL;
 	Oid			defaultPartOid;
-	char		tmpRelName[NAMEDATALEN];
 	RangeVar   *mergePartName = cmd->name;
 	bool		isSameName = false;
 
@@ -21461,12 +21460,17 @@ ATExecMergePartitions(List **wqueue, AlteredTableInfo *tab, Relation rel,
 		 * function transformPartitionCmdForMerge().
 		 */
 		if (equal(name, cmd->name))
+		{
 			/* One new partition can have the same name as merged partition. */
 			isSameName = true;
-
-		/* Store a next merging partition into the list. */
-		mergingPartitionsList = lappend(mergingPartitionsList,
-										mergingPartition);
+			newPartRel = mergingPartition;
+		}
+		else
+		{
+			/* Store a next merging partition into the list. */
+			mergingPartitionsList = lappend(mergingPartitionsList,
+											mergingPartition);
+		}
 	}
 
 	/* Detach all merged partitions. */
@@ -21482,26 +21486,34 @@ ATExecMergePartitions(List **wqueue, AlteredTableInfo *tab, Relation rel,
 		DetachPartitionFinalize(rel, mergingPartition, false, defaultPartOid);
 	}
 
-	/* Create table for new partition, use partitioned table as model. */
 	if (isSameName)
 	{
-		/* Create partition table with generated temparary name. */
-		sprintf(tmpRelName, "merge-%u-%X-tmp", RelationGetRelid(rel), MyProcPid);
-		mergePartName = makeRangeVar(get_namespace_name(RelationGetNamespace(rel)),
-									 tmpRelName, -1);
+		/* Detach partition that we re-use for merged partition. */
+
+		/* Remove the pg_inherits row first. */
+		RemoveInheritance(newPartRel, rel, false);
+		/* Do the final part of detaching. */
+		DetachPartitionFinalize(rel, newPartRel, false, defaultPartOid);
+
+		/* Make these updates visible */
+		CommandCounterIncrement();
 	}
-	createPartitionTable(mergePartName,
-						 makeRangeVar(get_namespace_name(RelationGetNamespace(rel)),
-									  RelationGetRelationName(rel), -1),
-						 context);
+	else
+	{
+		/* Create table for new partition, use partitioned table as model. */
+		createPartitionTable(mergePartName,
+							 makeRangeVar(get_namespace_name(RelationGetNamespace(rel)),
+										  RelationGetRelationName(rel), -1),
+							 context);
 
-	/*
-	 * Open the new partition and acquire exclusive lock on it.  This will
-	 * stop all the operations with partitioned table.  This might seem
-	 * excessive, but this is the way we make sure nobody is planning queries
-	 * involving merging partitions.
-	 */
-	newPartRel = table_openrv(mergePartName, AccessExclusiveLock);
+		/*
+		 * Open the new partition and acquire exclusive lock on it.  This will
+		 * stop all the operations with partitioned table.  This might seem
+		 * excessive, but this is the way we make sure nobody is planning
+		 * queries involving merging partitions.
+		 */
+		newPartRel = table_openrv(mergePartName, AccessExclusiveLock);
+	}
 
 	/* Copy data from merged partitions to new partition. */
 	moveMergedTablesRows(rel, mergingPartitionsList, newPartRel);
@@ -21529,19 +21541,6 @@ ATExecMergePartitions(List **wqueue, AlteredTableInfo *tab, Relation rel,
 		performDeletion(&object, DROP_RESTRICT, 0);
 	}
 	list_free(mergingPartitionsList);
-
-	/* Rename new partition if it is needed. */
-	if (isSameName)
-	{
-		/*
-		 * We must bump the command counter to make the new partition tuple
-		 * visible for rename.
-		 */
-		CommandCounterIncrement();
-		/* Rename partition. */
-		RenameRelationInternal(RelationGetRelid(newPartRel),
-							   cmd->name->relname, false, false);
-	}
 	/* Keep the lock until commit. */
 	table_close(newPartRel, NoLock);
 }
diff --git a/src/test/regress/expected/partition_merge.out b/src/test/regress/expected/partition_merge.out
index 2ba0ec47d9..f8dfbfcf41 100644
--- a/src/test/regress/expected/partition_merge.out
+++ b/src/test/regress/expected/partition_merge.out
@@ -211,13 +211,13 @@ ALTER TABLE sales_range MERGE PARTITIONS (sales_jan2022, sales_mar2022, sales_ot
 select * from sales_others;
  salesman_id | salesman_name | sales_amount | sales_date 
 -------------+---------------+--------------+------------
+          14 | Smith         |          510 | 05-04-2022
            1 | May           |         1000 | 01-31-2022
           10 | Halder        |          350 | 01-28-2022
           13 | Gandi         |          377 | 01-09-2022
            7 | Li            |          175 | 03-08-2022
            9 | Muller        |          250 | 03-11-2022
           12 | Plato         |          350 | 03-19-2022
-          14 | Smith         |          510 | 05-04-2022
 (7 rows)
 
 -- show partitions with conditions:
-- 
2.40.1.windows.1