Hello,

I'm writing telemetry data into a table partitioned by time. When there is no partition for a particular date, my app notices the constraint violation, creates the partition, and retries the insert.

I'm used to handling constraint violations by observing the constraint name in the error fields. However, this error had none. I set out to add the name to the error field, but after a bit of reading my impression is that partition constraints are more like a property of a table.

I've attached a patch that adds the schema and table name fields to errors for my use case:

- Insert data into a partitioned table for which there is no partition.
- Insert data directly into an incorrect partition.

Thanks,
Chris
>From 8261b366c49b2d04baeb882d39dfa626b9315889 Mon Sep 17 00:00:00 2001
From: Chris Bandy <bandy.ch...@gmail.com>
Date: Sat, 29 Feb 2020 12:47:56 -0600
Subject: [PATCH] Add schema and table names to partition error

---
 src/backend/executor/execMain.c      | 3 ++-
 src/backend/executor/execPartition.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git src/backend/executor/execMain.c src/backend/executor/execMain.c
index ee5c3a60ff..7cb486b211 100644
--- src/backend/executor/execMain.c
+++ 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 src/backend/executor/execPartition.c src/backend/executor/execPartition.c
index c13b1d3501..a5542b92c7 100644
--- src/backend/executor/execPartition.c
+++ src/backend/executor/execPartition.c
@@ -345,7 +345,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])
-- 
2.11.0

Reply via email to