From d5e70837fb911284daa6108106c1a108c6063dda Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@postgresql.org>
Date: Thu, 7 May 2026 18:48:43 +0700
Subject: [PATCH v7] Message improvements

---
 src/backend/partitioning/partbounds.c         | 20 +++++++++----------
 src/test/regress/expected/partition_merge.out |  4 ++--
 src/test/regress/expected/partition_split.out | 20 +++++++++----------
 3 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c
index 9b4277a4987..061154c634f 100644
--- a/src/backend/partitioning/partbounds.c
+++ b/src/backend/partitioning/partbounds.c
@@ -4990,8 +4990,8 @@ satisfies_hash_partition(PG_FUNCTION_ARGS)
  * second_name:		name of the second partition
  * second_bound:	bound of the second partition
  * defaultPart:		true if one of the new partitions is DEFAULT
- * is_merge:		true indicates the operation is MERGE PARTITIONS;
- * 					false indicates the operation is SPLIT PARTITION.
+ * splitPartOid:	OID of the partition being split, or InvalidOid for
+ * 					MERGE PARTITIONS
  * pstate:			pointer to ParseState struct for determining error position
  */
 static void
@@ -5001,7 +5001,7 @@ check_two_partitions_bounds_range(Relation parent,
 								  RangeVar *second_name,
 								  PartitionBoundSpec *second_bound,
 								  bool defaultPart,
-								  bool is_merge,
+								  Oid splitPartOid,
 								  ParseState *pstate)
 {
 	PartitionKey key = RelationGetPartitionKey(parent);
@@ -5027,23 +5027,23 @@ check_two_partitions_bounds_range(Relation parent,
 	{
 		PartitionRangeDatum *datum = linitial(second_bound->lowerdatums);
 
-		if (is_merge)
+		if (!OidIsValid(splitPartOid))
 			ereport(ERROR,
 					errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
 					errmsg("cannot merge partition \"%s\" together with partition \"%s\"",
 						   second_name->relname, first_name->relname),
 					errdetail("The lower bound of partition \"%s\" is not equal to the upper bound of partition \"%s\".",
 							  second_name->relname, first_name->relname),
-					errhint("ALTER TABLE ... MERGE PARTITIONS requires the partition bounds to be adjacent."),
+					errhint("ALTER TABLE ... MERGE PARTITIONS requires the old partition bounds to be adjacent."),
 					parser_errposition(pstate, datum->location));
 		else
 			ereport(ERROR,
 					errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
-					errmsg("cannot split to partition \"%s\" together with partition \"%s\"",
-						   second_name->relname, first_name->relname),
+					errmsg("cannot split partition \"%s\"",
+						   get_rel_name(splitPartOid)),
 					errdetail("The lower bound of partition \"%s\" is not equal to the upper bound of partition \"%s\".",
 							  second_name->relname, first_name->relname),
-					errhint("ALTER TABLE ... SPLIT PARTITION requires the partition bounds to be adjacent."),
+					errhint("ALTER TABLE ... SPLIT PARTITION requires the new partition bounds to be adjacent."),
 					parser_errposition(pstate, datum->location));
 	}
 }
@@ -5147,7 +5147,7 @@ calculate_partition_bound_for_merge(Relation parent,
 													  (RangeVar *) list_nth(partNames, index),
 													  (PartitionBoundSpec *) list_nth(bounds, index),
 													  false,
-													  true,
+													  InvalidOid,
 													  pstate);
 				}
 
@@ -5850,7 +5850,7 @@ check_partitions_for_split(Relation parent,
 			check_two_partitions_bounds_range(parent, spsPrev->name, spsPrev->bound,
 											  sps->name, sps->bound,
 											  createDefaultPart,
-											  false,
+											  splitPartOid,
 											  pstate);
 
 		spsPrev = sps;
diff --git a/src/test/regress/expected/partition_merge.out b/src/test/regress/expected/partition_merge.out
index d3818f1bf9b..807a90ba366 100644
--- a/src/test/regress/expected/partition_merge.out
+++ b/src/test/regress/expected/partition_merge.out
@@ -35,13 +35,13 @@ HINT:  ALTER TABLE ... MERGE PARTITIONS can only merge partitions that don't hav
 ALTER TABLE sales_range MERGE PARTITIONS (sales_jan2022, sales_mar2022) INTO sales_jan_mar2022;
 ERROR:  cannot merge partition "sales_mar2022" together with partition "sales_jan2022"
 DETAIL:  The lower bound of partition "sales_mar2022" is not equal to the upper bound of partition "sales_jan2022".
-HINT:  ALTER TABLE ... MERGE PARTITIONS requires the partition bounds to be adjacent.
+HINT:  ALTER TABLE ... MERGE PARTITIONS requires the old partition bounds to be adjacent.
 -- ERROR
 -- (space between sections sales_dec2021 and sales_jan2022)
 ALTER TABLE sales_range MERGE PARTITIONS (sales_dec2021, sales_jan2022, sales_feb2022) INTO sales_dec_jan_feb2022;
 ERROR:  cannot merge partition "sales_jan2022" together with partition "sales_dec2021"
 DETAIL:  The lower bound of partition "sales_jan2022" is not equal to the upper bound of partition "sales_dec2021".
-HINT:  ALTER TABLE ... MERGE PARTITIONS requires the partition bounds to be adjacent.
+HINT:  ALTER TABLE ... MERGE PARTITIONS requires the old partition bounds to be adjacent.
 -- ERROR
 ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022, partitions_merge_schema.sales_feb2022) INTO sales_feb_mar_apr2022;
 ERROR:  partition with name "sales_feb2022" is already used
diff --git a/src/test/regress/expected/partition_split.out b/src/test/regress/expected/partition_split.out
index 961b37953c8..081f948ebda 100644
--- a/src/test/regress/expected/partition_split.out
+++ b/src/test/regress/expected/partition_split.out
@@ -103,11 +103,11 @@ ALTER TABLE sales_range SPLIT PARTITION sales_feb_mar_apr2022 INTO
   (PARTITION sales_feb2022 FOR VALUES FROM ('2022-02-01') TO ('2022-03-01'),
    PARTITION sales_mar2022 FOR VALUES FROM ('2022-02-01') TO ('2022-04-01'),
    PARTITION sales_apr2022 FOR VALUES FROM ('2022-04-01') TO ('2022-05-01'));
-ERROR:  cannot split to partition "sales_mar2022" together with partition "sales_feb2022"
+ERROR:  cannot split partition "sales_feb_mar_apr2022"
 LINE 3:    PARTITION sales_mar2022 FOR VALUES FROM ('2022-02-01') TO...
                                                     ^
 DETAIL:  The lower bound of partition "sales_mar2022" is not equal to the upper bound of partition "sales_feb2022".
-HINT:  ALTER TABLE ... SPLIT PARTITION requires the partition bounds to be adjacent.
+HINT:  ALTER TABLE ... SPLIT PARTITION requires the new partition bounds to be adjacent.
 -- Tests for spaces between partitions, them should be executed without DEFAULT partition
 ALTER TABLE sales_range DETACH PARTITION sales_others;
 -- ERROR
@@ -462,11 +462,11 @@ ALTER TABLE sales_range SPLIT PARTITION sales_others INTO
    PARTITION sales_error FOR VALUES FROM ('2021-12-30') TO ('2022-02-01'),
    PARTITION sales_feb2022 FOR VALUES FROM ('2022-02-01') TO ('2022-03-01'),
    PARTITION sales_others DEFAULT);
-ERROR:  cannot split to partition "sales_error" together with partition "sales_dec2021"
+ERROR:  cannot split partition "sales_others"
 LINE 3:    PARTITION sales_error FOR VALUES FROM ('2021-12-30') TO (...
                                                   ^
 DETAIL:  The lower bound of partition "sales_error" is not equal to the upper bound of partition "sales_dec2021".
-HINT:  ALTER TABLE ... SPLIT PARTITION requires the partition bounds to be adjacent.
+HINT:  ALTER TABLE ... SPLIT PARTITION requires the new partition bounds to be adjacent.
 -- sales_error intersects with sales_feb2022 (upper bound)
 -- ERROR
 ALTER TABLE sales_range SPLIT PARTITION sales_others INTO
@@ -474,11 +474,11 @@ ALTER TABLE sales_range SPLIT PARTITION sales_others INTO
    PARTITION sales_error FOR VALUES FROM ('2022-01-01') TO ('2022-02-02'),
    PARTITION sales_feb2022 FOR VALUES FROM ('2022-02-01') TO ('2022-03-01'),
    PARTITION sales_others DEFAULT);
-ERROR:  cannot split to partition "sales_feb2022" together with partition "sales_error"
+ERROR:  cannot split partition "sales_others"
 LINE 4:    PARTITION sales_feb2022 FOR VALUES FROM ('2022-02-01') TO...
                                                     ^
 DETAIL:  The lower bound of partition "sales_feb2022" is not equal to the upper bound of partition "sales_error".
-HINT:  ALTER TABLE ... SPLIT PARTITION requires the partition bounds to be adjacent.
+HINT:  ALTER TABLE ... SPLIT PARTITION requires the new partition bounds to be adjacent.
 -- sales_error intersects with sales_dec2021 (inside bound)
 -- ERROR
 ALTER TABLE sales_range SPLIT PARTITION sales_others INTO
@@ -486,11 +486,11 @@ ALTER TABLE sales_range SPLIT PARTITION sales_others INTO
    PARTITION sales_error FOR VALUES FROM ('2021-12-10') TO ('2021-12-20'),
    PARTITION sales_feb2022 FOR VALUES FROM ('2022-02-01') TO ('2022-03-01'),
    PARTITION sales_others DEFAULT);
-ERROR:  cannot split to partition "sales_error" together with partition "sales_dec2021"
+ERROR:  cannot split partition "sales_others"
 LINE 3:    PARTITION sales_error FOR VALUES FROM ('2021-12-10') TO (...
                                                   ^
 DETAIL:  The lower bound of partition "sales_error" is not equal to the upper bound of partition "sales_dec2021".
-HINT:  ALTER TABLE ... SPLIT PARTITION requires the partition bounds to be adjacent.
+HINT:  ALTER TABLE ... SPLIT PARTITION requires the new partition bounds to be adjacent.
 -- sales_error intersects with sales_dec2021 (exactly the same bounds)
 -- ERROR
 ALTER TABLE sales_range SPLIT PARTITION sales_others INTO
@@ -498,11 +498,11 @@ ALTER TABLE sales_range SPLIT PARTITION sales_others INTO
    PARTITION sales_error FOR VALUES FROM ('2021-12-01') TO ('2022-01-01'),
    PARTITION sales_feb2022 FOR VALUES FROM ('2022-02-01') TO ('2022-03-01'),
    PARTITION sales_others DEFAULT);
-ERROR:  cannot split to partition "sales_error" together with partition "sales_dec2021"
+ERROR:  cannot split partition "sales_others"
 LINE 3:    PARTITION sales_error FOR VALUES FROM ('2021-12-01') TO (...
                                                   ^
 DETAIL:  The lower bound of partition "sales_error" is not equal to the upper bound of partition "sales_dec2021".
-HINT:  ALTER TABLE ... SPLIT PARTITION requires the partition bounds to be adjacent.
+HINT:  ALTER TABLE ... SPLIT PARTITION requires the new partition bounds to be adjacent.
 -- ERROR
 ALTER TABLE sales_range SPLIT PARTITION sales_others INTO
   (PARTITION sales_dec2021 FOR VALUES FROM ('2021-12-01') TO ('2022-01-01'),
-- 
2.54.0

