v43-0001-refactor-calculate_partition_bound_for_merge.no-cfbot

application/octet-stream

Filename: v43-0001-refactor-calculate_partition_bound_for_merge.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: Add SPLIT PARTITION/MERGE PARTITIONS commands
From 98efd403ffb4f5f949579855529942463afcb845 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Thu, 12 Jun 2025 15:28:05 +0800
Subject: [PATCH v43 1/1] refactor calculate_partition_bound_for_merge

---
 src/backend/commands/tablecmds.c      |  3 +-
 src/backend/parser/parse_utilcmd.c    |  5 ++-
 src/backend/partitioning/partbounds.c | 53 +++++++++++----------------
 src/include/partitioning/partbounds.h |  1 -
 4 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 92bed4a2dd7..213bf4f0516 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -22216,6 +22216,7 @@ createTableConstraints(Relation modelRel, Relation newRel)
 		bool		found_whole_row;
 		CookedConstraint *cooked;
 
+		Assert(!ccnoinherit);
 		ccbin_node = map_variable_attnos(stringToNode(ccbin),
 										 1, 0,
 										 attmap,
@@ -22261,7 +22262,7 @@ createTableConstraints(Relation modelRel, Relation newRel)
 		List	   *nnconstraints;
 
 		nnconstraints = RelationGetNotNullConstraints(RelationGetRelid(modelRel),
-													  false, true);
+													  false, false);
 
 		Assert(list_length(nnconstraints) > 0);
 
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index 7815e8d2455..e8040a1fb58 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -3614,8 +3614,9 @@ transformPartitionCmdForMerge(CreateStmtContext *cxt, PartitionCmd *partcmd)
 	partcmd->bound->location = -1;
 	partcmd->bound->is_default = isDefaultPart;
 	if (!isDefaultPart)
-		calculate_partition_bound_for_merge(parent, partcmd->partlist,
-											partOids, partcmd->bound,
+		calculate_partition_bound_for_merge(parent,
+											partOids,
+											partcmd->bound,
 											cxt->pstate);
 }
 
diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c
index cb719f1ff57..26982f13a89 100644
--- a/src/backend/partitioning/partbounds.c
+++ b/src/backend/partitioning/partbounds.c
@@ -4994,18 +4994,18 @@ satisfies_hash_partition(PG_FUNCTION_ARGS)
  * DEFAULT partition.
  *
  * parent:			partitioned table
- * first_name:		name of first partition
+ * first_name:		Oid of first partition
  * first_bound:		bound of first partition
- * second_name:		name of second partition
+ * second_name:		Oid of second partition
  * second_bound:	bound of second partition
  * defaultPart:		true if one of split partitions is DEFAULT
  * pstate:			pointer to ParseState struct for determining error position
  */
 static void
 check_two_partitions_bounds_range(Relation parent,
-								  RangeVar *first_name,
+								  Oid first_name,
 								  PartitionBoundSpec *first_bound,
-								  RangeVar *second_name,
+								  Oid second_name,
 								  PartitionBoundSpec *second_bound,
 								  bool defaultPart,
 								  ParseState *pstate)
@@ -5034,20 +5034,20 @@ check_two_partitions_bounds_range(Relation parent,
 		PartitionRangeDatum *datum = linitial(second_bound->lowerdatums);
 
 		ereport(ERROR,
-				(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
-				 errmsg("lower bound of partition \"%s\" conflicts with upper bound of previous partition \"%s\"",
-						second_name->relname, first_name->relname),
-				 parser_errposition(pstate, datum->location)));
+				errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+				errmsg("lower bound of partition \"%s\" conflicts with upper bound of previous partition \"%s\"",
+						get_rel_name(second_name), get_rel_name(first_name)),
+				parser_errposition(pstate, datum->location));
 	}
 }
 
 /*
  * get_partition_bound_spec
  *
- * Returns description of partition with Oid "partOid" and name "name".
+ * Returns the PartitionBoundSpec for the partition with the given OID partOid
  */
 static PartitionBoundSpec *
-get_partition_bound_spec(Oid partOid, RangeVar *name)
+get_partition_bound_spec(Oid partOid)
 {
 	HeapTuple	tuple;
 	Datum		datum;
@@ -5057,21 +5057,20 @@ get_partition_bound_spec(Oid partOid, RangeVar *name)
 	/* Try fetching the tuple from the catcache, for speed. */
 	tuple = SearchSysCache1(RELOID, partOid);
 	if (!HeapTupleIsValid(tuple))
-		elog(ERROR, "cache lookup failed for relation \"%s\"",
-			 name->relname);
+		elog(ERROR, "cache lookup failed for relation %u", partOid);
 
 	datum = SysCacheGetAttr(RELOID, tuple,
 							Anum_pg_class_relpartbound,
 							&isnull);
 	if (isnull)
-		elog(ERROR, "partition bound for relation \"%s\" is null",
-			 name->relname);
+		elog(ERROR, "partition bound for relation %u is null",
+			 partOid);
 
 	boundspec = stringToNode(TextDatumGetCString(datum));
 
 	if (!IsA(boundspec, PartitionBoundSpec))
-		elog(ERROR, "expected PartitionBoundSpec for relation \"%s\"",
-			 name->relname);
+		elog(ERROR, "expected PartitionBoundSpec for relation %u",
+			 partOid);
 
 	ReleaseSysCache(tuple);
 	return boundspec;
@@ -5084,14 +5083,12 @@ get_partition_bound_spec(Oid partOid, RangeVar *name)
  * partitions to be merged.
  *
  * parent:			partitioned table
- * partNames:		names of partitions to be merged
  * partOids:		Oids of partitions to be merged
  * spec (out):		bounds specification of the merged partition
  * pstate:			pointer to ParseState struct for determine error position
  */
 void
 calculate_partition_bound_for_merge(Relation parent,
-									List *partNames,
 									List *partOids,
 									PartitionBoundSpec *spec,
 									ParseState *pstate)
@@ -5117,10 +5114,10 @@ calculate_partition_bound_for_merge(Relation parent,
 				 * Create array of lower bounds and list of
 				 * PartitionBoundSpec.
 				 */
-				for (i = 0; i < nparts; i++)
+				foreach_oid(partoid, partOids)
 				{
-					bound = get_partition_bound_spec(list_nth_oid(partOids, i),
-													 (RangeVar *) list_nth(partNames, i));
+					bound = get_partition_bound_spec(partoid);
+					i = foreach_current_index(partoid);
 
 					lower_bounds[i] = make_one_partition_rbound(key, i, bound->lowerdatums, true);
 					bounds = lappend(bounds, bound);
@@ -5137,9 +5134,9 @@ calculate_partition_bound_for_merge(Relation parent,
 					int			prev_index = lower_bounds[i - 1]->index;
 
 					check_two_partitions_bounds_range(parent,
-													  (RangeVar *) list_nth(partNames, prev_index),
+													  list_nth_oid(partOids, prev_index),
 													  (PartitionBoundSpec *) list_nth(bounds, prev_index),
-													  (RangeVar *) list_nth(partNames, index),
+													  list_nth_oid(partOids, index),
 													  (PartitionBoundSpec *) list_nth(bounds, index),
 													  false, pstate);
 				}
@@ -5165,16 +5162,10 @@ calculate_partition_bound_for_merge(Relation parent,
 
 		case PARTITION_STRATEGY_LIST:
 			{
-				ListCell   *listptr,
-						   *listptr2;
-
 				/* Consolidate bounds for all partitions in the list. */
-				forboth(listptr, partOids, listptr2, partNames)
+				foreach_oid(partoid, partOids)
 				{
-					RangeVar   *name = (RangeVar *) lfirst(listptr2);
-					Oid			curOid = lfirst_oid(listptr);
-
-					bound = get_partition_bound_spec(curOid, name);
+					bound = get_partition_bound_spec(partoid);
 					spec->listdatums = list_concat(spec->listdatums, bound->listdatums);
 				}
 				break;
diff --git a/src/include/partitioning/partbounds.h b/src/include/partitioning/partbounds.h
index 690d2596190..a432c2d27ff 100644
--- a/src/include/partitioning/partbounds.h
+++ b/src/include/partitioning/partbounds.h
@@ -144,7 +144,6 @@ extern int	partition_hash_bsearch(PartitionBoundInfo boundinfo,
 								   int modulus, int remainder);
 
 extern void calculate_partition_bound_for_merge(Relation parent,
-												List *partNames,
 												List *partOids,
 												PartitionBoundSpec *spec,
 												ParseState *pstate);
-- 
2.34.1