Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e8b0e6b

Browse files
committed
Rewrite ExecPartitionCheckEmitError for clarity
The original was hard to follow and failed to comply with DRY principle. Discussion: https://postgr.es/m/20181206222221.g5witbsklvqthjll@alvherre.pgsql
1 parent f7ea1a4 commit e8b0e6b

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/backend/executor/execMain.c

+21-15
Original file line numberDiff line numberDiff line change
@@ -1837,25 +1837,26 @@ ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo,
18371837
TupleTableSlot *slot,
18381838
EState *estate)
18391839
{
1840-
Relation rel = resultRelInfo->ri_RelationDesc;
1841-
Relation orig_rel = rel;
1842-
TupleDesc tupdesc = RelationGetDescr(rel);
1840+
Oid root_relid;
1841+
TupleDesc tupdesc;
18431842
char *val_desc;
18441843
Bitmapset *modifiedCols;
1845-
Bitmapset *insertedCols;
1846-
Bitmapset *updatedCols;
18471844

18481845
/*
1849-
* Need to first convert the tuple to the root partitioned table's row
1850-
* type. For details, check similar comments in ExecConstraints().
1846+
* If the tuple has been routed, it's been converted to the partition's
1847+
* rowtype, which might differ from the root table's. We must convert it
1848+
* back to the root table's rowtype so that val_desc in the error message
1849+
* matches the input tuple.
18511850
*/
18521851
if (resultRelInfo->ri_PartitionRoot)
18531852
{
1854-
TupleDesc old_tupdesc = RelationGetDescr(rel);
1853+
TupleDesc old_tupdesc;
18551854
AttrNumber *map;
18561855

1857-
rel = resultRelInfo->ri_PartitionRoot;
1858-
tupdesc = RelationGetDescr(rel);
1856+
root_relid = RelationGetRelid(resultRelInfo->ri_PartitionRoot);
1857+
tupdesc = RelationGetDescr(resultRelInfo->ri_PartitionRoot);
1858+
1859+
old_tupdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
18591860
/* a reverse map */
18601861
map = convert_tuples_by_name_map_if_req(old_tupdesc, tupdesc,
18611862
gettext_noop("could not convert row type"));
@@ -1868,19 +1869,24 @@ ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo,
18681869
slot = execute_attr_map_slot(map, slot,
18691870
MakeTupleTableSlot(tupdesc, &TTSOpsVirtual));
18701871
}
1872+
else
1873+
{
1874+
root_relid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
1875+
tupdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
1876+
}
1877+
1878+
modifiedCols = bms_add_members(GetInsertedColumns(resultRelInfo, estate),
1879+
GetUpdatedColumns(resultRelInfo, estate));
18711880

1872-
insertedCols = GetInsertedColumns(resultRelInfo, estate);
1873-
updatedCols = GetUpdatedColumns(resultRelInfo, estate);
1874-
modifiedCols = bms_union(insertedCols, updatedCols);
1875-
val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
1881+
val_desc = ExecBuildSlotValueDescription(root_relid,
18761882
slot,
18771883
tupdesc,
18781884
modifiedCols,
18791885
64);
18801886
ereport(ERROR,
18811887
(errcode(ERRCODE_CHECK_VIOLATION),
18821888
errmsg("new row for relation \"%s\" violates partition constraint",
1883-
RelationGetRelationName(orig_rel)),
1889+
RelationGetRelationName(resultRelInfo->ri_RelationDesc)),
18841890
val_desc ? errdetail("Failing row contains %s.", val_desc) : 0));
18851891
}
18861892

0 commit comments

Comments
 (0)