Commit 48c348f8 authored by Tom Lane's avatar Tom Lane

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.
parent 1fc2d60d
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "executor/executor.h" #include "executor/executor.h"
#include "executor/nodeLimit.h" #include "executor/nodeLimit.h"
#include "nodes/nodeFuncs.h"
static void recompute_limits(LimitState *node); static void recompute_limits(LimitState *node);
static void pass_down_bound(LimitState *node, PlanState *child_node); static void pass_down_bound(LimitState *node, PlanState *child_node);
...@@ -344,7 +345,19 @@ pass_down_bound(LimitState *node, PlanState *child_node) ...@@ -344,7 +345,19 @@ pass_down_bound(LimitState *node, PlanState *child_node)
} }
else if (IsA(child_node, ResultState)) else if (IsA(child_node, ResultState))
{ {
if (outerPlanState(child_node)) /*
* An extra consideration here is that if the Result is projecting
* a targetlist that contains any SRFs, we can't assume that every
* input tuple generates an output tuple, so a Sort underneath
* might need to return more than N tuples to satisfy LIMIT N.
* So we cannot use bounded sort.
*
* If Result supported qual checking, we'd have to punt on seeing
* a qual, too. Note that having a resconstantqual is not a
* showstopper: if that fails we're not getting any rows at all.
*/
if (outerPlanState(child_node) &&
!expression_returns_set((Node *) child_node->plan->targetlist))
pass_down_bound(node, outerPlanState(child_node)); pass_down_bound(node, outerPlanState(child_node));
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment