From 90fdad27c04db70f78c24e4999cd3aef7c06d1f7 Mon Sep 17 00:00:00 2001 From: amit Date: Mon, 12 Jun 2017 14:35:05 +0900 Subject: [PATCH] Don't pass invalid PlanState pointer to ExecInitQual Current code in ExecInitModifyTable that initializes WithCheckOption.qual failed to consider that there *isn't* one plan per partition in the ModifyTableState.mt_plans[] array. --- src/backend/executor/nodeModifyTable.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index bf26488c51..89991e34ae 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -1841,8 +1841,17 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) if (node->withCheckOptionLists != NIL && mtstate->mt_num_partitions > 0) { List *wcoList; + PlanState *plan; - Assert(operation == CMD_INSERT); + /* + * In case of INSERT on partitioned tables, there is only plan; in the + * planner we don't build one for each partition. Likewise, there is + * only one WITH CHECK OPTIONS list, not one per partition. + */ + Assert(operation == CMD_INSERT && + list_length(node->withCheckOptionLists) == 1 && + mtstate->mt_nplans == 1); + plan = mtstate->mt_plans[0]; /* The only plan */ resultRelInfo = mtstate->mt_partitions; wcoList = linitial(node->withCheckOptionLists); for (i = 0; i < mtstate->mt_num_partitions; i++) @@ -1858,10 +1867,9 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) partrel, rel); foreach(ll, mapped_wcoList) { - WithCheckOption *wco = (WithCheckOption *) lfirst(ll); - ExprState *wcoExpr = ExecInitQual((List *) wco->qual, - mtstate->mt_plans[i]); + WithCheckOption *wco = castNode(WithCheckOption, lfirst(ll)); + ExprState *wcoExpr = ExecInitQual((List *) wco->qual, plan); wcoExprs = lappend(wcoExprs, wcoExpr); } -- 2.11.0