aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor
diff options
context:
space:
mode:
authorAlvaro Herrera <alvherre@alvh.no-ip.org>2018-04-16 15:50:57 -0300
committerAlvaro Herrera <alvherre@alvh.no-ip.org>2018-04-16 15:52:28 -0300
commit158b7bc6d77948d2f474dc9f2777c87f81d1365a (patch)
tree8c8db23a6754aedb011b81689066904c9fa8cb87 /src/backend/executor
parent3a2d6365984d2cd98ee03467cd901785941cf772 (diff)
downloadpostgresql-158b7bc6d77948d2f474dc9f2777c87f81d1365a.tar.gz
postgresql-158b7bc6d77948d2f474dc9f2777c87f81d1365a.zip
Ignore whole-rows in INSERT/CONFLICT with partitioned tables
We had an Assert() preventing whole-row expressions from being used in the SET clause of INSERT ON CONFLICT, but it seems unnecessary, given some tests, so remove it. Add a new test to exercise the case. Still at ExecInitPartitionInfo, we used map_partition_varattnos (which constructs an attribute map, then calls map_variable_attnos) using the same two relations many times in different expressions and with different parameters. Constructing the map over and over is a waste. To avoid this repeated work, construct the map once, and use map_variable_attnos() directly instead. Author: Amit Langote, per comments by me (Álvaro) Discussion: https://postgr.es/m/20180326142016.m4st5e34chrzrknk@alvherre.pgsql
Diffstat (limited to 'src/backend/executor')
-rw-r--r--src/backend/executor/execPartition.c121
1 files changed, 91 insertions, 30 deletions
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 218645d43b8..23a74bc3d98 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -24,6 +24,7 @@
#include "nodes/makefuncs.h"
#include "partitioning/partbounds.h"
#include "partitioning/partprune.h"
+#include "rewrite/rewriteManip.h"
#include "utils/lsyscache.h"
#include "utils/partcache.h"
#include "utils/rel.h"
@@ -307,8 +308,12 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
Relation rootrel = resultRelInfo->ri_RelationDesc,
partrel;
+ Relation firstResultRel = mtstate->resultRelInfo[0].ri_RelationDesc;
ResultRelInfo *leaf_part_rri;
MemoryContext oldContext;
+ AttrNumber *part_attnos = NULL;
+ bool found_whole_row;
+ bool equalTupdescs;
/*
* We locked all the partitions in ExecSetupPartitionTupleRouting
@@ -356,6 +361,10 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
(node != NULL &&
node->onConflictAction != ONCONFLICT_NONE));
+ /* if tuple descs are identical, we don't need to map the attrs */
+ equalTupdescs = equalTupleDescs(RelationGetDescr(partrel),
+ RelationGetDescr(firstResultRel));
+
/*
* Build WITH CHECK OPTION constraints for the partition. Note that we
* didn't build the withCheckOptionList for partitions within the planner,
@@ -369,7 +378,6 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
List *wcoExprs = NIL;
ListCell *ll;
int firstVarno = mtstate->resultRelInfo[0].ri_RangeTableIndex;
- Relation firstResultRel = mtstate->resultRelInfo[0].ri_RelationDesc;
/*
* In the case of INSERT on a partitioned table, there is only one
@@ -397,8 +405,22 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
/*
* Convert Vars in it to contain this partition's attribute numbers.
*/
- wcoList = map_partition_varattnos(wcoList, firstVarno,
- partrel, firstResultRel, NULL);
+ if (!equalTupdescs)
+ {
+ part_attnos =
+ convert_tuples_by_name_map(RelationGetDescr(partrel),
+ RelationGetDescr(firstResultRel),
+ gettext_noop("could not convert row type"));
+ wcoList = (List *)
+ map_variable_attnos((Node *) wcoList,
+ firstVarno, 0,
+ part_attnos,
+ RelationGetDescr(firstResultRel)->natts,
+ RelationGetForm(partrel)->reltype,
+ &found_whole_row);
+ /* We ignore the value of found_whole_row. */
+ }
+
foreach(ll, wcoList)
{
WithCheckOption *wco = castNode(WithCheckOption, lfirst(ll));
@@ -425,7 +447,6 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
ExprContext *econtext;
List *returningList;
int firstVarno = mtstate->resultRelInfo[0].ri_RangeTableIndex;
- Relation firstResultRel = mtstate->resultRelInfo[0].ri_RelationDesc;
/* See the comment above for WCO lists. */
Assert((node->operation == CMD_INSERT &&
@@ -443,12 +464,26 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
*/
returningList = linitial(node->returningLists);
- /*
- * Convert Vars in it to contain this partition's attribute numbers.
- */
- returningList = map_partition_varattnos(returningList, firstVarno,
- partrel, firstResultRel,
- NULL);
+ if (!equalTupdescs)
+ {
+ /*
+ * Convert Vars in it to contain this partition's attribute numbers.
+ */
+ if (part_attnos == NULL)
+ part_attnos =
+ convert_tuples_by_name_map(RelationGetDescr(partrel),
+ RelationGetDescr(firstResultRel),
+ gettext_noop("could not convert row type"));
+ returningList = (List *)
+ map_variable_attnos((Node *) returningList,
+ firstVarno, 0,
+ part_attnos,
+ RelationGetDescr(firstResultRel)->natts,
+ RelationGetForm(partrel)->reltype,
+ &found_whole_row);
+ /* We ignore the value of found_whole_row. */
+ }
+
leaf_part_rri->ri_returningList = returningList;
/*
@@ -473,7 +508,6 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
{
TupleConversionMap *map = proute->parent_child_tupconv_maps[partidx];
int firstVarno = mtstate->resultRelInfo[0].ri_RangeTableIndex;
- Relation firstResultRel = mtstate->resultRelInfo[0].ri_RelationDesc;
TupleDesc partrelDesc = RelationGetDescr(partrel);
ExprContext *econtext = mtstate->ps.ps_ExprContext;
ListCell *lc;
@@ -549,17 +583,33 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
* target relation (firstVarno).
*/
onconflset = (List *) copyObject((Node *) node->onConflictSet);
- onconflset =
- map_partition_varattnos(onconflset, INNER_VAR, partrel,
- firstResultRel, &found_whole_row);
- Assert(!found_whole_row);
- onconflset =
- map_partition_varattnos(onconflset, firstVarno, partrel,
- firstResultRel, &found_whole_row);
- Assert(!found_whole_row);
-
- /* Finally, adjust this tlist to match the partition. */
- onconflset = adjust_partition_tlist(onconflset, map);
+ if (!equalTupdescs)
+ {
+ if (part_attnos == NULL)
+ part_attnos =
+ convert_tuples_by_name_map(RelationGetDescr(partrel),
+ RelationGetDescr(firstResultRel),
+ gettext_noop("could not convert row type"));
+ onconflset = (List *)
+ map_variable_attnos((Node *) onconflset,
+ INNER_VAR, 0,
+ part_attnos,
+ RelationGetDescr(firstResultRel)->natts,
+ RelationGetForm(partrel)->reltype,
+ &found_whole_row);
+ /* We ignore the value of found_whole_row. */
+ onconflset = (List *)
+ map_variable_attnos((Node *) onconflset,
+ firstVarno, 0,
+ part_attnos,
+ RelationGetDescr(firstResultRel)->natts,
+ RelationGetForm(partrel)->reltype,
+ &found_whole_row);
+ /* We ignore the value of found_whole_row. */
+
+ /* Finally, adjust this tlist to match the partition. */
+ onconflset = adjust_partition_tlist(onconflset, map);
+ }
/*
* Build UPDATE SET's projection info. The user of this
@@ -587,14 +637,25 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
List *clause;
clause = copyObject((List *) node->onConflictWhere);
- clause = map_partition_varattnos(clause, INNER_VAR,
- partrel, firstResultRel,
- &found_whole_row);
- Assert(!found_whole_row);
- clause = map_partition_varattnos(clause, firstVarno,
- partrel, firstResultRel,
- &found_whole_row);
- Assert(!found_whole_row);
+ if (!equalTupdescs)
+ {
+ clause = (List *)
+ map_variable_attnos((Node *) clause,
+ INNER_VAR, 0,
+ part_attnos,
+ RelationGetDescr(firstResultRel)->natts,
+ RelationGetForm(partrel)->reltype,
+ &found_whole_row);
+ /* We ignore the value of found_whole_row. */
+ clause = (List *)
+ map_variable_attnos((Node *) clause,
+ firstVarno, 0,
+ part_attnos,
+ RelationGetDescr(firstResultRel)->natts,
+ RelationGetForm(partrel)->reltype,
+ &found_whole_row);
+ /* We ignore the value of found_whole_row. */
+ }
leaf_part_rri->ri_onConflict->oc_WhereClause =
ExecInitQual((List *) clause, &mtstate->ps);
}