Skip to content

Commit a0efda8

Browse files
committed
Remove faulty support for MergeAppend plan with WHERE CURRENT OF.
Somebody extended search_plan_tree() to treat MergeAppend exactly like Append, which is 100% wrong, because unlike Append we can't assume that only one input node is actively returning tuples. Hence a cursor using a MergeAppend across a UNION ALL or inheritance tree could falsely match a WHERE CURRENT OF query at a row that isn't actually the cursor's current output row, but coincidentally has the same TID (in a different table) as the current output row. Delete the faulty code; this means that such a case will now return an error like 'cursor "foo" is not a simply updatable scan of table "bar"', instead of silently misbehaving. Users should not find that surprising though, as the same cursor query could have failed that way already depending on the chosen plan. (It would fail like that if the sort were done with an explicit Sort node instead of MergeAppend.) Expand the clearly-inadequate commentary to be more explicit about what this code is doing, in hopes of forestalling future mistakes. It's been like this for awhile, so back-patch to all supported branches. Discussion: https://postgr.es/m/482865.1611075182@sss.pgh.pa.us
1 parent f18aa1b commit a0efda8

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/backend/executor/execCurrent.c

+27-26
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ fetch_cursor_param_value(ExprContext *econtext, int paramId)
299299
* Search through a PlanState tree for a scan node on the specified table.
300300
* Return NULL if not found or multiple candidates.
301301
*
302+
* CAUTION: this function is not charged simply with finding some candidate
303+
* scan, but with ensuring that that scan returned the plan tree's current
304+
* output row. That's why we must reject multiple-match cases.
305+
*
302306
* If a candidate is found, set *pending_rescan to true if that candidate
303307
* or any node above it has a pending rescan action, i.e. chgParam != NULL.
304308
* That indicates that we shouldn't consider the node to be positioned on a
@@ -317,7 +321,9 @@ search_plan_tree(PlanState *node, Oid table_oid,
317321
switch (nodeTag(node))
318322
{
319323
/*
320-
* Relation scan nodes can all be treated alike. Note that
324+
* Relation scan nodes can all be treated alike: check to see if
325+
* they are scanning the specified table.
326+
*
321327
* ForeignScan and CustomScan might not have a currentRelation, in
322328
* which case we just ignore them. (We dare not descend to any
323329
* child plan nodes they might have, since we do not know the
@@ -342,8 +348,26 @@ search_plan_tree(PlanState *node, Oid table_oid,
342348
}
343349

344350
/*
345-
* For Append, we must look through the members; watch out for
346-
* multiple matches (possible if it was from UNION ALL)
351+
* For Append, we can check each input node. It is safe to
352+
* descend to the inputs because only the input that resulted in
353+
* the Append's current output node could be positioned on a tuple
354+
* at all; the other inputs are either at EOF or not yet started.
355+
* Hence, if the desired table is scanned by some
356+
* currently-inactive input node, we will find that node but then
357+
* our caller will realize that it didn't emit the tuple of
358+
* interest.
359+
*
360+
* We do need to watch out for multiple matches (possible if
361+
* Append was from UNION ALL rather than an inheritance tree).
362+
*
363+
* Note: we can NOT descend through MergeAppend similarly, since
364+
* its inputs are likely all active, and we don't know which one
365+
* returned the current output tuple. (Perhaps that could be
366+
* fixed if we were to let this code know more about MergeAppend's
367+
* internal state, but it does not seem worth the trouble. Users
368+
* should not expect plans for ORDER BY queries to be considered
369+
* simply-updatable, since they won't be if the sorting is
370+
* implemented by a Sort node.)
347371
*/
348372
case T_AppendState:
349373
{
@@ -365,29 +389,6 @@ search_plan_tree(PlanState *node, Oid table_oid,
365389
break;
366390
}
367391

368-
/*
369-
* Similarly for MergeAppend
370-
*/
371-
case T_MergeAppendState:
372-
{
373-
MergeAppendState *mstate = (MergeAppendState *) node;
374-
int i;
375-
376-
for (i = 0; i < mstate->ms_nplans; i++)
377-
{
378-
ScanState *elem = search_plan_tree(mstate->mergeplans[i],
379-
table_oid,
380-
pending_rescan);
381-
382-
if (!elem)
383-
continue;
384-
if (result)
385-
return NULL; /* multiple matches */
386-
result = elem;
387-
}
388-
break;
389-
}
390-
391392
/*
392393
* Result and Limit can be descended through (these are safe
393394
* because they always return their input's current row)

0 commit comments

Comments
 (0)