Skip to content

Commit 48c348f

Browse files
committed
Dept of second thoughts: don't try to push LIMIT below a SRF.
If we have Limit->Result->Sort, the Result might be projecting a tlist that contains a set-returning function. If so, it's possible for the SRF to sometimes return zero rows, which means we could need to fetch more than N rows from the Sort in order to satisfy LIMIT N. So top-N sorting cannot be used in this scenario.
1 parent 1fc2d60 commit 48c348f

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/backend/executor/nodeLimit.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "executor/executor.h"
2525
#include "executor/nodeLimit.h"
26+
#include "nodes/nodeFuncs.h"
2627

2728
static void recompute_limits(LimitState *node);
2829
static void pass_down_bound(LimitState *node, PlanState *child_node);
@@ -344,7 +345,19 @@ pass_down_bound(LimitState *node, PlanState *child_node)
344345
}
345346
else if (IsA(child_node, ResultState))
346347
{
347-
if (outerPlanState(child_node))
348+
/*
349+
* An extra consideration here is that if the Result is projecting
350+
* a targetlist that contains any SRFs, we can't assume that every
351+
* input tuple generates an output tuple, so a Sort underneath
352+
* might need to return more than N tuples to satisfy LIMIT N.
353+
* So we cannot use bounded sort.
354+
*
355+
* If Result supported qual checking, we'd have to punt on seeing
356+
* a qual, too. Note that having a resconstantqual is not a
357+
* showstopper: if that fails we're not getting any rows at all.
358+
*/
359+
if (outerPlanState(child_node) &&
360+
!expression_returns_set((Node *) child_node->plan->targetlist))
348361
pass_down_bound(node, outerPlanState(child_node));
349362
}
350363
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy