2000-10-26 23:38:24 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* nodeLimit.c
|
|
|
|
* Routines to handle limiting of query results where appropriate
|
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
2000-10-26 23:38:24 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/executor/nodeLimit.c
|
2000-10-26 23:38:24 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* INTERFACE ROUTINES
|
|
|
|
* ExecLimit - extract a limited range of tuples
|
|
|
|
* ExecInitLimit - initialize node and subnodes..
|
|
|
|
* ExecEndLimit - shutdown node and subnodes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "executor/executor.h"
|
|
|
|
#include "executor/nodeLimit.h"
|
2017-07-26 02:37:17 +02:00
|
|
|
#include "miscadmin.h"
|
2010-11-18 17:53:49 +01:00
|
|
|
#include "nodes/nodeFuncs.h"
|
2000-10-26 23:38:24 +02:00
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
static void recompute_limits(LimitState *node);
|
2017-08-29 19:12:23 +02:00
|
|
|
static int64 compute_tuples_needed(LimitState *node);
|
2000-10-26 23:38:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecLimit
|
|
|
|
*
|
|
|
|
* This is a very simple node which just performs LIMIT/OFFSET
|
|
|
|
* filtering on the stream of tuples returned by a subplan.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2017-07-17 09:33:49 +02:00
|
|
|
static TupleTableSlot * /* return: a tuple or NULL */
|
|
|
|
ExecLimit(PlanState *pstate)
|
2000-10-26 23:38:24 +02:00
|
|
|
{
|
2017-07-17 09:33:49 +02:00
|
|
|
LimitState *node = castNode(LimitState, pstate);
|
2020-04-07 22:22:13 +02:00
|
|
|
ExprContext *econtext = node->ps.ps_ExprContext;
|
2000-10-26 23:38:24 +02:00
|
|
|
ScanDirection direction;
|
|
|
|
TupleTableSlot *slot;
|
2002-12-05 16:50:39 +01:00
|
|
|
PlanState *outerPlan;
|
2000-10-26 23:38:24 +02:00
|
|
|
|
2017-07-26 02:37:17 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* get information from the node
|
2000-10-26 23:38:24 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
direction = node->ps.state->es_direction;
|
|
|
|
outerPlan = outerPlanState(node);
|
2000-10-26 23:38:24 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-11-22 23:10:01 +01:00
|
|
|
* The main logic is a simple state machine.
|
2000-10-26 23:38:24 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
switch (node->lstate)
|
2000-10-26 23:38:24 +02:00
|
|
|
{
|
2002-11-22 23:10:01 +01:00
|
|
|
case LIMIT_INITIAL:
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
2007-05-17 21:35:08 +02:00
|
|
|
* First call for this node, so compute limit/offset. (We can't do
|
|
|
|
* this any earlier, because parameters from upper nodes will not
|
2007-11-15 22:14:46 +01:00
|
|
|
* be set during ExecInitLimit.) This also sets position = 0 and
|
|
|
|
* changes the state to LIMIT_RESCAN.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
2007-05-17 21:35:08 +02:00
|
|
|
recompute_limits(node);
|
|
|
|
|
|
|
|
/* FALL THRU */
|
|
|
|
|
|
|
|
case LIMIT_RESCAN:
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
2007-05-17 21:35:08 +02:00
|
|
|
* If backwards scan, just return NULL without changing state.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
2007-05-17 21:35:08 +02:00
|
|
|
if (!ScanDirectionIsForward(direction))
|
|
|
|
return NULL;
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
|
|
|
* Check for empty window; if so, treat like empty subplan.
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
if (node->count <= 0 && !node->noCount)
|
2002-11-22 23:10:01 +01:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
node->lstate = LIMIT_EMPTY;
|
2000-10-26 23:38:24 +02:00
|
|
|
return NULL;
|
2002-11-22 23:10:01 +01:00
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2000-10-26 23:38:24 +02:00
|
|
|
/*
|
2002-11-22 23:10:01 +01:00
|
|
|
* Fetch rows from subplan until we reach position > offset.
|
2000-10-26 23:38:24 +02:00
|
|
|
*/
|
2002-11-22 23:10:01 +01:00
|
|
|
for (;;)
|
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
slot = ExecProcNode(outerPlan);
|
2002-11-22 23:10:01 +01:00
|
|
|
if (TupIsNull(slot))
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The subplan returns too few tuples for us to produce
|
|
|
|
* any output at all.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
node->lstate = LIMIT_EMPTY;
|
2002-11-22 23:10:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-04-07 22:22:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tuple at limit is needed for comparation in subsequent
|
|
|
|
* execution to detect ties.
|
|
|
|
*/
|
|
|
|
if (node->limitOption == LIMIT_OPTION_WITH_TIES &&
|
|
|
|
node->position - node->offset == node->count - 1)
|
|
|
|
{
|
|
|
|
ExecCopySlot(node->last_slot, slot);
|
|
|
|
}
|
2002-12-05 16:50:39 +01:00
|
|
|
node->subSlot = slot;
|
|
|
|
if (++node->position > node->offset)
|
2002-11-22 23:10:01 +01:00
|
|
|
break;
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
|
|
|
* Okay, we have the first tuple of the window.
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
node->lstate = LIMIT_INWINDOW;
|
2002-11-22 23:10:01 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LIMIT_EMPTY:
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
|
|
|
* The subplan is known to return no tuples (or not more than
|
2014-05-06 18:12:18 +02:00
|
|
|
* OFFSET tuples, in general). So we return no tuples.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
case LIMIT_INWINDOW:
|
2000-10-26 23:38:24 +02:00
|
|
|
if (ScanDirectionIsForward(direction))
|
|
|
|
{
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
2020-04-07 22:22:13 +02:00
|
|
|
* Forwards scan, so check for stepping off end of window. At
|
|
|
|
* the end of the window, the behavior depends on whether WITH
|
|
|
|
* TIES was specified: in that case, we need to change the
|
|
|
|
* state machine to LIMIT_WINDOWTIES. If not (nothing was
|
|
|
|
* specified, or ONLY was) return NULL without advancing the
|
|
|
|
* subplan or the position variable but change the state
|
|
|
|
* machine to record having done so
|
2019-11-18 09:47:41 +01:00
|
|
|
*
|
|
|
|
* Once at the end, ideally, we can shut down parallel
|
|
|
|
* resources but that would destroy the parallel context which
|
|
|
|
* would be required for rescans. To do that, we need to find
|
|
|
|
* a way to pass down more information about whether rescans
|
|
|
|
* are possible.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
if (!node->noCount &&
|
2011-08-02 09:47:17 +02:00
|
|
|
node->position - node->offset >= node->count)
|
2002-11-22 23:10:01 +01:00
|
|
|
{
|
2020-04-07 22:22:13 +02:00
|
|
|
if (node->limitOption == LIMIT_OPTION_COUNT)
|
|
|
|
{
|
|
|
|
node->lstate = LIMIT_WINDOWEND;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->lstate = LIMIT_WINDOWEND_TIES;
|
|
|
|
/* fall-through */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Get next tuple from subplan, if any.
|
|
|
|
*/
|
|
|
|
slot = ExecProcNode(outerPlan);
|
|
|
|
if (TupIsNull(slot))
|
|
|
|
{
|
|
|
|
node->lstate = LIMIT_SUBPLANEOF;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tuple at limit is needed for comparation in subsequent
|
|
|
|
* execution to detect ties.
|
|
|
|
*/
|
|
|
|
if (node->limitOption == LIMIT_OPTION_WITH_TIES &&
|
|
|
|
node->position - node->offset == node->count - 1)
|
|
|
|
{
|
|
|
|
ExecCopySlot(node->last_slot, slot);
|
|
|
|
}
|
|
|
|
node->subSlot = slot;
|
|
|
|
node->position++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Backwards scan, so check for stepping off start of window.
|
|
|
|
* As above, change only state-machine status if so.
|
|
|
|
*/
|
|
|
|
if (node->position <= node->offset + 1)
|
|
|
|
{
|
|
|
|
node->lstate = LIMIT_WINDOWSTART;
|
2002-11-22 23:10:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
2020-04-07 22:22:13 +02:00
|
|
|
* Get previous tuple from subplan; there should be one!
|
|
|
|
*/
|
|
|
|
slot = ExecProcNode(outerPlan);
|
|
|
|
if (TupIsNull(slot))
|
|
|
|
elog(ERROR, "LIMIT subplan failed to run backwards");
|
|
|
|
node->subSlot = slot;
|
|
|
|
node->position--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LIMIT_WINDOWEND_TIES:
|
|
|
|
if (ScanDirectionIsForward(direction))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Advance the subplan until we find the first row with
|
|
|
|
* different ORDER BY pathkeys.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
slot = ExecProcNode(outerPlan);
|
2002-11-22 23:10:01 +01:00
|
|
|
if (TupIsNull(slot))
|
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
node->lstate = LIMIT_SUBPLANEOF;
|
2002-11-22 23:10:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-04-07 22:22:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test if the new tuple and the last tuple match. If so we
|
|
|
|
* return the tuple.
|
|
|
|
*/
|
|
|
|
econtext->ecxt_innertuple = slot;
|
|
|
|
econtext->ecxt_outertuple = node->last_slot;
|
|
|
|
if (ExecQualAndReset(node->eqfunction, econtext))
|
|
|
|
{
|
|
|
|
node->subSlot = slot;
|
|
|
|
node->position++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->lstate = LIMIT_WINDOWEND;
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Backwards scan, so check for stepping off start of window.
|
2020-04-07 22:22:13 +02:00
|
|
|
* Change only state-machine status if so.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
if (node->position <= node->offset + 1)
|
2002-11-22 23:10:01 +01:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
node->lstate = LIMIT_WINDOWSTART;
|
2002-11-22 23:10:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
2020-04-07 22:22:13 +02:00
|
|
|
* Get previous tuple from subplan; there should be one! And
|
|
|
|
* change state-machine status.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
slot = ExecProcNode(outerPlan);
|
2002-11-22 23:10:01 +01:00
|
|
|
if (TupIsNull(slot))
|
2003-07-21 19:05:12 +02:00
|
|
|
elog(ERROR, "LIMIT subplan failed to run backwards");
|
2002-12-05 16:50:39 +01:00
|
|
|
node->subSlot = slot;
|
|
|
|
node->position--;
|
2020-04-07 22:22:13 +02:00
|
|
|
node->lstate = LIMIT_INWINDOW;
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
2002-11-22 23:10:01 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LIMIT_SUBPLANEOF:
|
|
|
|
if (ScanDirectionIsForward(direction))
|
|
|
|
return NULL;
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Backing up from subplan EOF, so re-fetch previous tuple; there
|
|
|
|
* should be one! Note previous tuple must be in window.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
slot = ExecProcNode(outerPlan);
|
2002-11-22 23:10:01 +01:00
|
|
|
if (TupIsNull(slot))
|
2003-07-21 19:05:12 +02:00
|
|
|
elog(ERROR, "LIMIT subplan failed to run backwards");
|
2002-12-05 16:50:39 +01:00
|
|
|
node->subSlot = slot;
|
|
|
|
node->lstate = LIMIT_INWINDOW;
|
2002-11-22 23:10:01 +01:00
|
|
|
/* position does not change 'cause we didn't advance it before */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIMIT_WINDOWEND:
|
|
|
|
if (ScanDirectionIsForward(direction))
|
|
|
|
return NULL;
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
2020-04-07 22:22:13 +02:00
|
|
|
* We already past one position to detect ties so re-fetch
|
|
|
|
* previous tuple; there should be one! Note previous tuple must
|
|
|
|
* be in window.
|
2002-11-22 23:10:01 +01:00
|
|
|
*/
|
2020-04-07 22:22:13 +02:00
|
|
|
if (node->limitOption == LIMIT_OPTION_WITH_TIES)
|
|
|
|
{
|
|
|
|
slot = ExecProcNode(outerPlan);
|
|
|
|
if (TupIsNull(slot))
|
|
|
|
elog(ERROR, "LIMIT subplan failed to run backwards");
|
|
|
|
node->subSlot = slot;
|
|
|
|
node->lstate = LIMIT_INWINDOW;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Backing up from window end: simply re-return the last tuple
|
|
|
|
* fetched from the subplan.
|
|
|
|
*/
|
|
|
|
slot = node->subSlot;
|
|
|
|
node->lstate = LIMIT_INWINDOW;
|
|
|
|
/* position does not change 'cause we didn't advance it before */
|
|
|
|
}
|
2002-11-22 23:10:01 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LIMIT_WINDOWSTART:
|
|
|
|
if (!ScanDirectionIsForward(direction))
|
|
|
|
return NULL;
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/*
|
|
|
|
* Advancing after having backed off window start: simply
|
|
|
|
* re-return the last tuple fetched from the subplan.
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
slot = node->subSlot;
|
|
|
|
node->lstate = LIMIT_INWINDOW;
|
2002-11-22 23:10:01 +01:00
|
|
|
/* position does not change 'cause we didn't change it before */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-07-21 19:05:12 +02:00
|
|
|
elog(ERROR, "impossible LIMIT state: %d",
|
2002-12-05 16:50:39 +01:00
|
|
|
(int) node->lstate);
|
2002-11-22 23:10:01 +01:00
|
|
|
slot = NULL; /* keep compiler quiet */
|
2000-10-26 23:38:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/* Return the current tuple */
|
|
|
|
Assert(!TupIsNull(slot));
|
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
return slot;
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-05-17 21:35:08 +02:00
|
|
|
* Evaluate the limit/offset expressions --- done at startup or rescan.
|
2000-10-26 23:38:24 +02:00
|
|
|
*
|
|
|
|
* This is also a handy place to reset the current-position state info.
|
|
|
|
*/
|
|
|
|
static void
|
2002-12-05 16:50:39 +01:00
|
|
|
recompute_limits(LimitState *node)
|
2000-10-26 23:38:24 +02:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
ExprContext *econtext = node->ps.ps_ExprContext;
|
2006-12-03 22:40:07 +01:00
|
|
|
Datum val;
|
2000-10-26 23:38:24 +02:00
|
|
|
bool isNull;
|
2006-07-26 21:31:51 +02:00
|
|
|
|
2000-10-26 23:38:24 +02:00
|
|
|
if (node->limitOffset)
|
|
|
|
{
|
2006-12-03 22:40:07 +01:00
|
|
|
val = ExecEvalExprSwitchContext(node->limitOffset,
|
|
|
|
econtext,
|
2017-01-19 23:12:38 +01:00
|
|
|
&isNull);
|
2000-10-26 23:38:24 +02:00
|
|
|
/* Interpret NULL offset as no offset */
|
|
|
|
if (isNull)
|
2002-12-05 16:50:39 +01:00
|
|
|
node->offset = 0;
|
2006-12-03 22:40:07 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
node->offset = DatumGetInt64(val);
|
|
|
|
if (node->offset < 0)
|
2008-03-10 04:37:59 +01:00
|
|
|
ereport(ERROR,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
(errcode(ERRCODE_INVALID_ROW_COUNT_IN_RESULT_OFFSET_CLAUSE),
|
|
|
|
errmsg("OFFSET must not be negative")));
|
2006-12-03 22:40:07 +01:00
|
|
|
}
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No OFFSET supplied */
|
2002-12-05 16:50:39 +01:00
|
|
|
node->offset = 0;
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node->limitCount)
|
|
|
|
{
|
2006-12-03 22:40:07 +01:00
|
|
|
val = ExecEvalExprSwitchContext(node->limitCount,
|
|
|
|
econtext,
|
2017-01-19 23:12:38 +01:00
|
|
|
&isNull);
|
2000-11-05 01:15:54 +01:00
|
|
|
/* Interpret NULL count as no count (LIMIT ALL) */
|
2000-10-26 23:38:24 +02:00
|
|
|
if (isNull)
|
2006-12-03 22:40:07 +01:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
node->count = 0;
|
2006-12-03 22:40:07 +01:00
|
|
|
node->noCount = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->count = DatumGetInt64(val);
|
|
|
|
if (node->count < 0)
|
2008-03-10 04:37:59 +01:00
|
|
|
ereport(ERROR,
|
2009-03-04 11:55:00 +01:00
|
|
|
(errcode(ERRCODE_INVALID_ROW_COUNT_IN_LIMIT_CLAUSE),
|
2008-03-10 04:37:59 +01:00
|
|
|
errmsg("LIMIT must not be negative")));
|
2006-12-03 22:40:07 +01:00
|
|
|
node->noCount = false;
|
|
|
|
}
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No COUNT supplied */
|
2002-12-05 16:50:39 +01:00
|
|
|
node->count = 0;
|
|
|
|
node->noCount = true;
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
/* Reset position to start-of-scan */
|
2002-12-05 16:50:39 +01:00
|
|
|
node->position = 0;
|
|
|
|
node->subSlot = NULL;
|
2007-05-04 03:13:45 +02:00
|
|
|
|
2007-05-17 21:35:08 +02:00
|
|
|
/* Set state-machine state */
|
|
|
|
node->lstate = LIMIT_RESCAN;
|
|
|
|
|
2017-08-29 19:12:23 +02:00
|
|
|
/*
|
|
|
|
* Notify child node about limit. Note: think not to "optimize" by
|
|
|
|
* skipping ExecSetTupleBound if compute_tuples_needed returns < 0. We
|
|
|
|
* must update the child node anyway, in case this is a rescan and the
|
|
|
|
* previous time we got a different result.
|
|
|
|
*/
|
|
|
|
ExecSetTupleBound(compute_tuples_needed(node), outerPlanState(node));
|
2010-11-18 06:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-08-29 19:12:23 +02:00
|
|
|
* Compute the maximum number of tuples needed to satisfy this Limit node.
|
|
|
|
* Return a negative value if there is not a determinable limit.
|
2010-11-18 06:30:10 +01:00
|
|
|
*/
|
2017-08-29 19:12:23 +02:00
|
|
|
static int64
|
|
|
|
compute_tuples_needed(LimitState *node)
|
2010-11-18 06:30:10 +01:00
|
|
|
{
|
2020-04-07 22:22:13 +02:00
|
|
|
if ((node->noCount) || (node->limitOption == LIMIT_OPTION_WITH_TIES))
|
2017-08-29 19:12:23 +02:00
|
|
|
return -1;
|
|
|
|
/* Note: if this overflows, we'll return a negative value, which is OK */
|
|
|
|
return node->count + node->offset;
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecInitLimit
|
|
|
|
*
|
|
|
|
* This initializes the limit node state structures and
|
|
|
|
* the node's subplan.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
LimitState *
|
2006-02-28 05:10:28 +01:00
|
|
|
ExecInitLimit(Limit *node, EState *estate, int eflags)
|
2000-10-26 23:38:24 +02:00
|
|
|
{
|
|
|
|
LimitState *limitstate;
|
|
|
|
Plan *outerPlan;
|
|
|
|
|
2006-02-28 05:10:28 +01:00
|
|
|
/* check for unsupported flags */
|
|
|
|
Assert(!(eflags & EXEC_FLAG_MARK));
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* create state structure
|
2000-10-26 23:38:24 +02:00
|
|
|
*/
|
|
|
|
limitstate = makeNode(LimitState);
|
2002-12-05 16:50:39 +01:00
|
|
|
limitstate->ps.plan = (Plan *) node;
|
|
|
|
limitstate->ps.state = estate;
|
2017-07-17 09:33:49 +02:00
|
|
|
limitstate->ps.ExecProcNode = ExecLimit;
|
2002-12-05 16:50:39 +01:00
|
|
|
|
2002-11-22 23:10:01 +01:00
|
|
|
limitstate->lstate = LIMIT_INITIAL;
|
2000-10-26 23:38:24 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Miscellaneous initialization
|
2000-10-26 23:38:24 +02:00
|
|
|
*
|
2001-03-22 07:16:21 +01:00
|
|
|
* Limit nodes never call ExecQual or ExecProject, but they need an
|
|
|
|
* exprcontext anyway to evaluate the limit/offset parameters in.
|
2000-10-26 23:38:24 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecAssignExprContext(estate, &limitstate->ps);
|
|
|
|
|
2018-02-17 06:17:38 +01:00
|
|
|
/*
|
|
|
|
* initialize outer plan
|
|
|
|
*/
|
|
|
|
outerPlan = outerPlan(node);
|
|
|
|
outerPlanState(limitstate) = ExecInitNode(outerPlan, estate, eflags);
|
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
/*
|
|
|
|
* initialize child expressions
|
|
|
|
*/
|
2002-12-13 20:46:01 +01:00
|
|
|
limitstate->limitOffset = ExecInitExpr((Expr *) node->limitOffset,
|
2002-12-05 16:50:39 +01:00
|
|
|
(PlanState *) limitstate);
|
2002-12-13 20:46:01 +01:00
|
|
|
limitstate->limitCount = ExecInitExpr((Expr *) node->limitCount,
|
2002-12-05 16:50:39 +01:00
|
|
|
(PlanState *) limitstate);
|
2020-04-07 22:22:13 +02:00
|
|
|
limitstate->limitOption = node->limitOption;
|
2000-10-26 23:38:24 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
Don't require return slots for nodes without projection.
In a lot of nodes the return slot is not required. That can either be
because the node doesn't do any projection (say an Append node), or
because the node does perform projections but the projection is
optimized away because the projection would yield an identical row.
Slots aren't that small, especially for wide rows, so it's worthwhile
to avoid creating them. It's not possible to just skip creating the
slot - it's currently used to determine the tuple descriptor returned
by ExecGetResultType(). So separate the determination of the result
type from the slot creation. The work previously done internally
ExecInitResultTupleSlotTL() can now also be done separately with
ExecInitResultTypeTL() and ExecInitResultSlot(). That way nodes that
aren't guaranteed to need a result slot, can use
ExecInitResultTypeTL() to determine the result type of the node, and
ExecAssignScanProjectionInfo() (via
ExecConditionalAssignProjectionInfo()) determines that a result slot
is needed, it is created with ExecInitResultSlot().
Besides the advantage of avoiding to create slots that then are
unused, this is necessary preparation for later patches around tuple
table slot abstraction. In particular separating the return descriptor
and slot is a prerequisite to allow JITing of tuple deforming with
knowledge of the underlying tuple format, and to avoid unnecessarily
creating JITed tuple deforming for virtual slots.
This commit removes a redundant argument from
ExecInitResultTupleSlotTL(). While this commit touches a lot of the
relevant lines anyway, it'd normally still not worthwhile to cause
breakage, except that aforementioned later commits will touch *all*
ExecInitResultTupleSlotTL() callers anyway (but fits worse
thematically).
Author: Andres Freund
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-10 02:19:39 +01:00
|
|
|
* Initialize result type.
|
2000-10-26 23:38:24 +02:00
|
|
|
*/
|
Don't require return slots for nodes without projection.
In a lot of nodes the return slot is not required. That can either be
because the node doesn't do any projection (say an Append node), or
because the node does perform projections but the projection is
optimized away because the projection would yield an identical row.
Slots aren't that small, especially for wide rows, so it's worthwhile
to avoid creating them. It's not possible to just skip creating the
slot - it's currently used to determine the tuple descriptor returned
by ExecGetResultType(). So separate the determination of the result
type from the slot creation. The work previously done internally
ExecInitResultTupleSlotTL() can now also be done separately with
ExecInitResultTypeTL() and ExecInitResultSlot(). That way nodes that
aren't guaranteed to need a result slot, can use
ExecInitResultTypeTL() to determine the result type of the node, and
ExecAssignScanProjectionInfo() (via
ExecConditionalAssignProjectionInfo()) determines that a result slot
is needed, it is created with ExecInitResultSlot().
Besides the advantage of avoiding to create slots that then are
unused, this is necessary preparation for later patches around tuple
table slot abstraction. In particular separating the return descriptor
and slot is a prerequisite to allow JITing of tuple deforming with
knowledge of the underlying tuple format, and to avoid unnecessarily
creating JITed tuple deforming for virtual slots.
This commit removes a redundant argument from
ExecInitResultTupleSlotTL(). While this commit touches a lot of the
relevant lines anyway, it'd normally still not worthwhile to cause
breakage, except that aforementioned later commits will touch *all*
ExecInitResultTupleSlotTL() callers anyway (but fits worse
thematically).
Author: Andres Freund
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-10 02:19:39 +01:00
|
|
|
ExecInitResultTypeTL(&limitstate->ps);
|
2000-10-26 23:38:24 +02:00
|
|
|
|
Introduce notion of different types of slots (without implementing them).
Upcoming work intends to allow pluggable ways to introduce new ways of
storing table data. Accessing those table access methods from the
executor requires TupleTableSlots to be carry tuples in the native
format of such storage methods; otherwise there'll be a significant
conversion overhead.
Different access methods will require different data to store tuples
efficiently (just like virtual, minimal, heap already require fields
in TupleTableSlot). To allow that without requiring additional pointer
indirections, we want to have different structs (embedding
TupleTableSlot) for different types of slots. Thus different types of
slots are needed, which requires adapting creators of slots.
The slot that most efficiently can represent a type of tuple in an
executor node will often depend on the type of slot a child node
uses. Therefore we need to track the type of slot is returned by
nodes, so parent slots can create slots based on that.
Relatedly, JIT compilation of tuple deforming needs to know which type
of slot a certain expression refers to, so it can create an
appropriate deforming function for the type of tuple in the slot.
But not all nodes will only return one type of slot, e.g. an append
node will potentially return different types of slots for each of its
subplans.
Therefore add function that allows to query the type of a node's
result slot, and whether it'll always be the same type (whether it's
fixed). This can be queried using ExecGetResultSlotOps().
The scan, result, inner, outer type of slots are automatically
inferred from ExecInitScanTupleSlot(), ExecInitResultSlot(),
left/right subtrees respectively. If that's not correct for a node,
that can be overwritten using new fields in PlanState.
This commit does not introduce the actually abstracted implementation
of different kind of TupleTableSlots, that will be left for a followup
commit. The different types of slots introduced will, for now, still
use the same backing implementation.
While this already partially invalidates the big comment in
tuptable.h, it seems to make more sense to update it later, when the
different TupleTableSlot implementations actually exist.
Author: Ashutosh Bapat and Andres Freund, with changes by Amit Khandekar
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-16 07:00:30 +01:00
|
|
|
limitstate->ps.resultopsset = true;
|
|
|
|
limitstate->ps.resultops = ExecGetResultSlotOps(outerPlanState(limitstate),
|
|
|
|
&limitstate->ps.resultopsfixed);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* limit nodes do no projections, so initialize projection info for this
|
|
|
|
* node appropriately
|
2000-10-26 23:38:24 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
limitstate->ps.ps_ProjInfo = NULL;
|
2000-10-26 23:38:24 +02:00
|
|
|
|
2020-04-07 22:22:13 +02:00
|
|
|
/*
|
|
|
|
* Initialize the equality evaluation, to detect ties.
|
|
|
|
*/
|
|
|
|
if (node->limitOption == LIMIT_OPTION_WITH_TIES)
|
|
|
|
{
|
|
|
|
TupleDesc desc;
|
|
|
|
const TupleTableSlotOps *ops;
|
|
|
|
|
|
|
|
desc = ExecGetResultType(outerPlanState(limitstate));
|
|
|
|
ops = ExecGetResultSlotOps(outerPlanState(limitstate), NULL);
|
|
|
|
|
|
|
|
limitstate->last_slot = ExecInitExtraTupleSlot(estate, desc, ops);
|
|
|
|
limitstate->eqfunction = execTuplesMatchPrepare(desc,
|
|
|
|
node->uniqNumCols,
|
|
|
|
node->uniqColIdx,
|
|
|
|
node->uniqOperators,
|
|
|
|
node->uniqCollations,
|
|
|
|
&limitstate->ps);
|
|
|
|
}
|
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
return limitstate;
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecEndLimit
|
|
|
|
*
|
|
|
|
* This shuts down the subplan and frees resources allocated
|
|
|
|
* to this node.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecEndLimit(LimitState *node)
|
2000-10-26 23:38:24 +02:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecFreeExprContext(&node->ps);
|
2002-12-15 17:17:59 +01:00
|
|
|
ExecEndNode(outerPlanState(node));
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-07-12 19:01:06 +02:00
|
|
|
ExecReScanLimit(LimitState *node)
|
2000-10-26 23:38:24 +02:00
|
|
|
{
|
2007-05-17 21:35:08 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* Recompute limit/offset in case parameters changed, and reset the state
|
|
|
|
* machine. We must do this before rescanning our child node, in case
|
|
|
|
* it's a Sort that we are passing the parameters down to.
|
2007-05-17 21:35:08 +02:00
|
|
|
*/
|
|
|
|
recompute_limits(node);
|
2000-10-26 23:38:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if chgParam of subnode is not null then plan will be re-scanned by
|
|
|
|
* first ExecProcNode.
|
|
|
|
*/
|
2010-07-12 19:01:06 +02:00
|
|
|
if (node->ps.lefttree->chgParam == NULL)
|
|
|
|
ExecReScan(node->ps.lefttree);
|
2000-10-26 23:38:24 +02:00
|
|
|
}
|