#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"
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
(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,
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
/*
* 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));
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 &&
*/
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;
/*
{
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;
* 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
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);
}
insert into parted_conflict_1 values (40, 'cuarenta')
on conflict (a) do update set b = excluded.b;
drop table parted_conflict;
+
+-- test whole-row Vars in ON CONFLICT expressions
+create table parted_conflict (a int, b text, c int) partition by range (a);
+create table parted_conflict_1 (drp text, c int, a int, b text);
+alter table parted_conflict_1 drop column drp;
+create unique index on parted_conflict (a, b);
+alter table parted_conflict attach partition parted_conflict_1 for values from (0) to (1000);
+truncate parted_conflict;
+insert into parted_conflict values (50, 'cincuenta', 1);
+insert into parted_conflict values (50, 'cincuenta', 2)
+ on conflict (a, b) do update set (a, b, c) = row(excluded.*)
+ where parted_conflict = (50, text 'cincuenta', 1) and
+ excluded = (50, text 'cincuenta', 2);
+
+-- should see (50, 'cincuenta', 2)
+select * from parted_conflict order by a;
+
+drop table parted_conflict;