Subject: [PATCH v6 2/2] Add object names to partition integrity violations

From: Chris Bandy <[email-masked]>
Date: Fri, 6 Mar 2020 21:02:52 -0600

All errors of SQLSTATE class 23 should include the name of an object
associated with the error.
---
 src/backend/commands/tablecmds.c               |  6 ++++--
 src/backend/executor/execMain.c                |  3 ++-
 src/backend/executor/execPartition.c           |  3 ++-
 src/backend/partitioning/partbounds.c          |  3 ++-
 src/backend/utils/adt/ri_triggers.c            |  3 ++-
 src/test/regress/expected/integrity_errors.out | 30 +++++++++++++-------------
 6 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 729025470d..8e35c5bd1a 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5343,12 +5343,14 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
 					ereport(ERROR,
 							(errcode(ERRCODE_CHECK_VIOLATION),
 							 errmsg("updated partition constraint for default partition \"%s\" would be violated by some row",
-									RelationGetRelationName(oldrel))));
+									RelationGetRelationName(oldrel)),
+							 errtable(oldrel)));
 				else
 					ereport(ERROR,
 							(errcode(ERRCODE_CHECK_VIOLATION),
 							 errmsg("partition constraint of relation \"%s\" is violated by some row",
-									RelationGetRelationName(oldrel))));
+									RelationGetRelationName(oldrel)),
+							 errtable(oldrel)));
 			}
 
 			/* Write the tuple out to the new relation */
diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index 28130fbc2b..4fdffad6f3 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -1878,7 +1878,8 @@ ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo,
 			(errcode(ERRCODE_CHECK_VIOLATION),
 			 errmsg("new row for relation \"%s\" violates partition constraint",
 					RelationGetRelationName(resultRelInfo->ri_RelationDesc)),
-			 val_desc ? errdetail("Failing row contains %s.", val_desc) : 0));
+			 val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
+			 errtable(resultRelInfo->ri_RelationDesc)));
 }
 
 /*
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index ef74ad85ff..fb6ce49056 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -346,7 +346,8 @@ ExecFindPartition(ModifyTableState *mtstate,
 							RelationGetRelationName(rel)),
 					 val_desc ?
 					 errdetail("Partition key of the failing row contains %s.",
-							   val_desc) : 0));
+							   val_desc) : 0,
+					 errtable(rel)));
 		}
 
 		if (partdesc->is_leaf[partidx])
diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c
index 35953f23fa..4c47f54a57 100644
--- a/src/backend/partitioning/partbounds.c
+++ b/src/backend/partitioning/partbounds.c
@@ -1366,7 +1366,8 @@ check_default_partition_contents(Relation parent, Relation default_rel,
 				ereport(ERROR,
 						(errcode(ERRCODE_CHECK_VIOLATION),
 						 errmsg("updated partition constraint for default partition \"%s\" would be violated by some row",
-								RelationGetRelationName(default_rel))));
+								RelationGetRelationName(default_rel)),
+						 errtable(default_rel)));
 
 			ResetExprContext(econtext);
 			CHECK_FOR_INTERRUPTS();
diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c
index 4ab7cda110..bb49e80d16 100644
--- a/src/backend/utils/adt/ri_triggers.c
+++ b/src/backend/utils/adt/ri_triggers.c
@@ -2452,7 +2452,8 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo,
 						NameStr(riinfo->conname)),
 				 errdetail("Key (%s)=(%s) is still referenced from table \"%s\".",
 						   key_names.data, key_values.data,
-						   RelationGetRelationName(fk_rel))));
+						   RelationGetRelationName(fk_rel)),
+				 errtableconstraint(fk_rel, NameStr(riinfo->conname))));
 	else if (onfk)
 		ereport(ERROR,
 				(errcode(ERRCODE_FOREIGN_KEY_VIOLATION),
diff --git a/src/test/regress/expected/integrity_errors.out b/src/test/regress/expected/integrity_errors.out
index e75e6b722f..14796a99c9 100644
--- a/src/test/regress/expected/integrity_errors.out
+++ b/src/test/regress/expected/integrity_errors.out
@@ -316,8 +316,8 @@ err_sqlstate   | 23514
 err_message    | no partition of relation "ivpt1" found for row
 err_detail     | Partition key of the failing row contains (y) = (10).
 err_datatype   | 
-err_schema     | 
-err_table      | 
+err_schema     | public
+err_table      | ivpt1
 err_column     | 
 err_constraint | 
 
@@ -330,8 +330,8 @@ err_sqlstate   | 23514
 err_message    | no partition of relation "ivpt1" found for row
 err_detail     | Partition key of the failing row contains (y) = (10).
 err_datatype   | 
-err_schema     | 
-err_table      | 
+err_schema     | public
+err_table      | ivpt1
 err_column     | 
 err_constraint | 
 
@@ -342,8 +342,8 @@ err_sqlstate   | 23514
 err_message    | new row for relation "ivpt1_p1" violates partition constraint
 err_detail     | Failing row contains (10, 10).
 err_datatype   | 
-err_schema     | 
-err_table      | 
+err_schema     | public
+err_table      | ivpt1_p1
 err_column     | 
 err_constraint | 
 
@@ -357,8 +357,8 @@ err_sqlstate   | 23514
 err_message    | partition constraint of relation "ivpt1_p2" is violated by some row
 err_detail     | 
 err_datatype   | 
-err_schema     | 
-err_table      | 
+err_schema     | public
+err_table      | ivpt1_p2
 err_column     | 
 err_constraint | 
 
@@ -372,8 +372,8 @@ err_sqlstate   | 23514
 err_message    | updated partition constraint for default partition "ivpt1_default" would be violated by some row
 err_detail     | 
 err_datatype   | 
-err_schema     | 
-err_table      | 
+err_schema     | public
+err_table      | ivpt1_default
 err_column     | 
 err_constraint | 
 
@@ -385,8 +385,8 @@ err_sqlstate   | 23514
 err_message    | updated partition constraint for default partition "ivpt1_default" would be violated by some row
 err_detail     | 
 err_datatype   | 
-err_schema     | 
-err_table      | 
+err_schema     | public
+err_table      | ivpt1_default
 err_column     | 
 err_constraint | 
 
@@ -400,9 +400,9 @@ err_sqlstate   | 23503
 err_message    | removing partition "ivpt1_default" violates foreign key constraint "ivpt2_x_y_fkey2"
 err_detail     | Key (x, y)=(10, 10) is still referenced from table "ivpt2".
 err_datatype   | 
-err_schema     | 
-err_table      | 
+err_schema     | public
+err_table      | ivpt2
 err_column     | 
-err_constraint | 
+err_constraint | ivpt2_x_y_fkey2
 
 DROP TABLE ivpt1, ivpt1_p2, ivpt1_p2, ivpt1_p3, ivpt2;
-- 
2.11.0

--------------7610F20B8BD8B09665E6DC43--