Skip to content

Commit 85823b2

Browse files
committed
add parent`s columns to column list for update trigger
1 parent 310d76b commit 85823b2

File tree

1 file changed

+29
-66
lines changed

1 file changed

+29
-66
lines changed

src/pl_funcs.c

Lines changed: 29 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static void pathman_update_trigger_func_move_tuple(Relation source_rel,
113113
HeapTuple old_tuple,
114114
HeapTuple new_tuple);
115115

116+
static void collect_update_trigger_columns(Oid relid, List **columns);
116117
static Oid find_target_partition(Relation source_rel, HeapTuple tuple);
117118
static Oid find_topmost_parent(Oid partition);
118119
static Oid find_deepest_partition(Oid parent, Relation source_rel, HeapTuple tuple);
@@ -1087,30 +1088,12 @@ Datum
10871088
pathman_update_trigger_func(PG_FUNCTION_ARGS)
10881089
{
10891090
TriggerData *trigdata = (TriggerData *) fcinfo->context;
1090-
10911091
Relation source_rel;
1092-
1093-
// Oid parent_relid,
10941092
Oid source_relid,
10951093
target_relid;
1096-
10971094
HeapTuple old_tuple,
10981095
new_tuple;
10991096

1100-
// Datum value;
1101-
// Oid value_type;
1102-
// bool isnull;
1103-
// ExprDoneCond itemIsDone;
1104-
1105-
// Oid *parts;
1106-
// int nparts;
1107-
1108-
// ExprContext *econtext;
1109-
// ExprState *expr_state;
1110-
// MemoryContext old_mcxt;
1111-
// PartParentSearch parent_search;
1112-
// const PartRelationInfo *prel;
1113-
11141097
/* Handle user calls */
11151098
if (!CALLED_AS_TRIGGER(fcinfo))
11161099
elog(ERROR, "this function should not be called directly");
@@ -1132,54 +1115,11 @@ pathman_update_trigger_func(PG_FUNCTION_ARGS)
11321115
old_tuple = trigdata->tg_trigtuple;
11331116
new_tuple = trigdata->tg_newtuple;
11341117

1135-
// /* Find parent relation and partitioning info */
1136-
// parent_relid = get_parent_of_partition(source_relid, &parent_search);
1137-
// if (parent_search != PPS_ENTRY_PART_PARENT)
1138-
// elog(ERROR, "relation \"%s\" is not a partition",
1139-
// RelationGetRelationName(source_rel));
1140-
1141-
// /* Fetch partition dispatch info */
1142-
// prel = get_pathman_relation_info(parent_relid);
1143-
// shout_if_prel_is_invalid(parent_relid, prel, PT_ANY);
1144-
1145-
// /* Execute partitioning expression */
1146-
// econtext = CreateStandaloneExprContext();
1147-
// old_mcxt = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
1148-
// expr_state = pathman_update_trigger_build_expr_state(prel,
1149-
// source_rel,
1150-
// new_tuple,
1151-
// &value_type);
1152-
// value = ExecEvalExpr(expr_state, econtext, &isnull, &itemIsDone);
1153-
// MemoryContextSwitchTo(old_mcxt);
1154-
1155-
// if (isnull)
1156-
// elog(ERROR, ERR_PART_ATTR_NULL);
1157-
1158-
// if (itemIsDone != ExprSingleResult)
1159-
// elog(ERROR, ERR_PART_ATTR_MULTIPLE_RESULTS);
1160-
1161-
// /* Search for matching partitions */
1162-
// parts = find_partitions_for_value(value, value_type, prel, &nparts);
1163-
1164-
1165-
// /* We can free expression context now */
1166-
// FreeExprContext(econtext, false);
1167-
1168-
// if (nparts > 1)
1169-
// elog(ERROR, ERR_PART_ATTR_MULTIPLE);
1170-
// else if (nparts == 0)
1171-
// {
1172-
// target_relid = create_partitions_for_value(PrelParentRelid(prel),
1173-
// value, value_type);
1174-
1175-
// /* get_pathman_relation_info() will refresh this entry */
1176-
// invalidate_pathman_relation_info(PrelParentRelid(prel), NULL);
1177-
// }
1178-
// else target_relid = parts[0];
1179-
1180-
// pfree(parts);
1118+
/* Find (or create) target partition */
11811119
target_relid = find_target_partition(source_rel, new_tuple);
11821120

1121+
/* TODO: check for InvalidOid */
1122+
11831123
/* Convert tuple if target partition has changed */
11841124
if (target_relid != source_relid)
11851125
{
@@ -1549,7 +1489,7 @@ create_update_triggers(PG_FUNCTION_ARGS)
15491489
const char *trigname;
15501490
const PartRelationInfo *prel;
15511491
uint32 i;
1552-
List *columns;
1492+
List *columns = NIL;
15531493

15541494
/* Check that table is partitioned */
15551495
prel = get_pathman_relation_info(parent);
@@ -1559,7 +1499,8 @@ create_update_triggers(PG_FUNCTION_ARGS)
15591499
trigname = build_update_trigger_name_internal(parent);
15601500

15611501
/* Create trigger for parent */
1562-
columns = PrelExpressionColumnNames(prel);
1502+
// columns = PrelExpressionColumnNames(prel);
1503+
collect_update_trigger_columns(parent, &columns);
15631504
create_single_update_trigger_internal(parent, trigname, columns);
15641505

15651506
/* Fetch children array */
@@ -1572,6 +1513,28 @@ create_update_triggers(PG_FUNCTION_ARGS)
15721513
PG_RETURN_VOID();
15731514
}
15741515

1516+
static void
1517+
collect_update_trigger_columns(Oid relid, List **columns)
1518+
{
1519+
const PartRelationInfo *prel;
1520+
Oid parent;
1521+
PartParentSearch parent_search;
1522+
1523+
prel = get_pathman_relation_info(relid);
1524+
if (!prel)
1525+
return;
1526+
1527+
/* Collect columns from current level */
1528+
*columns = list_concat(*columns, PrelExpressionColumnNames(prel));
1529+
1530+
/* Collect columns from parent */
1531+
parent = get_parent_of_partition(relid, &parent_search);
1532+
if (parent_search != PPS_ENTRY_PART_PARENT)
1533+
return;
1534+
1535+
collect_update_trigger_columns(parent, columns);
1536+
}
1537+
15751538
/* Create an UPDATE trigger for partition */
15761539
Datum
15771540
create_single_update_trigger(PG_FUNCTION_ARGS)

0 commit comments

Comments
 (0)