2000-09-29 14:21:41 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* nodeSubqueryscan.c
|
|
|
|
|
* Support routines for scanning subqueries (subselects in rangetable).
|
|
|
|
|
*
|
2000-10-05 15:11:39 -04:00
|
|
|
* This is just enough different from sublinks (nodeSubplan.c) to mean that
|
|
|
|
|
* we need two sets of code. Ought to look at trying to unify the cases.
|
|
|
|
|
*
|
|
|
|
|
*
|
2008-01-01 14:46:01 -05:00
|
|
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
2000-09-29 14:21:41 -04:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* IDENTIFICATION
|
2008-01-01 14:46:01 -05:00
|
|
|
* $PostgreSQL: pgsql/src/backend/executor/nodeSubqueryscan.c,v 1.39 2008/01/01 19:45:49 momjian Exp $
|
2000-09-29 14:21:41 -04:00
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
* INTERFACE ROUTINES
|
|
|
|
|
* ExecSubqueryScan scans a subquery.
|
|
|
|
|
* ExecSubqueryNext retrieve next tuple in sequential order.
|
|
|
|
|
* ExecInitSubqueryScan creates and initializes a subqueryscan node.
|
|
|
|
|
* ExecEndSubqueryScan releases any storage allocated.
|
|
|
|
|
* ExecSubqueryReScan rescans the relation
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
|
|
#include "executor/execdebug.h"
|
|
|
|
|
#include "executor/nodeSubqueryscan.h"
|
|
|
|
|
|
2002-12-05 10:50:39 -05:00
|
|
|
static TupleTableSlot *SubqueryNext(SubqueryScanState *node);
|
2000-09-29 14:21:41 -04:00
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* Scan Support
|
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* SubqueryNext
|
|
|
|
|
*
|
|
|
|
|
* This is a workhorse for ExecSubqueryScan
|
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
static TupleTableSlot *
|
2002-12-05 10:50:39 -05:00
|
|
|
SubqueryNext(SubqueryScanState *node)
|
2000-09-29 14:21:41 -04:00
|
|
|
{
|
|
|
|
|
TupleTableSlot *slot;
|
|
|
|
|
|
|
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* We need not support EvalPlanQual here, since we are not scanning a real
|
|
|
|
|
* relation.
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
|
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2006-12-26 16:37:20 -05:00
|
|
|
* Get the next tuple from the sub-query.
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
slot = ExecProcNode(node->subplan);
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2005-05-22 18:30:20 -04:00
|
|
|
/*
|
|
|
|
|
* We just overwrite our ScanTupleSlot with the subplan's result slot,
|
|
|
|
|
* rather than expending the cycles for ExecCopySlot().
|
|
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
node->ss.ss_ScanTupleSlot = slot;
|
2000-09-29 14:21:41 -04:00
|
|
|
|
|
|
|
|
return slot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* ExecSubqueryScan(node)
|
|
|
|
|
*
|
|
|
|
|
* Scans the subquery sequentially and returns the next qualifying
|
|
|
|
|
* tuple.
|
|
|
|
|
* It calls the ExecScan() routine and passes it the access method
|
|
|
|
|
* which retrieve tuples sequentially.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
TupleTableSlot *
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecSubqueryScan(SubqueryScanState *node)
|
2000-09-29 14:21:41 -04:00
|
|
|
{
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
|
|
|
|
* use SubqueryNext as access method
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
return ExecScan(&node->ss, (ExecScanAccessMtd) SubqueryNext);
|
2000-09-29 14:21:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* ExecInitSubqueryScan
|
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
SubqueryScanState *
|
2006-02-27 23:10:28 -05:00
|
|
|
ExecInitSubqueryScan(SubqueryScan *node, EState *estate, int eflags)
|
2000-09-29 14:21:41 -04:00
|
|
|
{
|
|
|
|
|
SubqueryScanState *subquerystate;
|
|
|
|
|
|
2006-02-27 23:10:28 -05:00
|
|
|
/* check for unsupported flags */
|
|
|
|
|
Assert(!(eflags & EXEC_FLAG_MARK));
|
|
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2007-11-15 16:14:46 -05:00
|
|
|
* SubqueryScan should not have any "normal" children. Also, if planner
|
2007-02-22 17:00:26 -05:00
|
|
|
* left anything in subrtable, it's fishy.
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
Assert(outerPlan(node) == NULL);
|
|
|
|
|
Assert(innerPlan(node) == NULL);
|
2007-02-22 17:00:26 -05:00
|
|
|
Assert(node->subrtable == NIL);
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2002-12-05 10:50:39 -05:00
|
|
|
* create state structure
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
|
|
|
|
subquerystate = makeNode(SubqueryScanState);
|
2002-12-05 10:50:39 -05:00
|
|
|
subquerystate->ss.ps.plan = (Plan *) node;
|
|
|
|
|
subquerystate->ss.ps.state = estate;
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
|
|
|
|
* Miscellaneous initialization
|
2000-09-29 14:21:41 -04:00
|
|
|
*
|
2001-03-22 01:16:21 -05:00
|
|
|
* create expression context for node
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecAssignExprContext(estate, &subquerystate->ss.ps);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* initialize child expressions
|
|
|
|
|
*/
|
|
|
|
|
subquerystate->ss.ps.targetlist = (List *)
|
2002-12-13 14:46:01 -05:00
|
|
|
ExecInitExpr((Expr *) node->scan.plan.targetlist,
|
2002-12-05 10:50:39 -05:00
|
|
|
(PlanState *) subquerystate);
|
|
|
|
|
subquerystate->ss.ps.qual = (List *)
|
2002-12-13 14:46:01 -05:00
|
|
|
ExecInitExpr((Expr *) node->scan.plan.qual,
|
2002-12-05 10:50:39 -05:00
|
|
|
(PlanState *) subquerystate);
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2005-05-22 18:30:20 -04:00
|
|
|
#define SUBQUERYSCAN_NSLOTS 2
|
2001-03-22 01:16:21 -05:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* tuple table initialization
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecInitResultTupleSlot(estate, &subquerystate->ss.ps);
|
2005-05-22 18:30:20 -04:00
|
|
|
ExecInitScanTupleSlot(estate, &subquerystate->ss);
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
|
|
|
|
* initialize subquery
|
2002-12-15 11:17:59 -05:00
|
|
|
*/
|
2007-02-26 20:11:26 -05:00
|
|
|
subquerystate->subplan = ExecInitNode(node->subplan, estate, eflags);
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2002-12-05 10:50:39 -05:00
|
|
|
subquerystate->ss.ps.ps_TupFromTlist = false;
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2005-05-22 18:30:20 -04:00
|
|
|
/*
|
2007-02-26 20:11:26 -05:00
|
|
|
* Initialize scan tuple type (needed by ExecAssignScanProjectionInfo)
|
2005-05-22 18:30:20 -04:00
|
|
|
*/
|
|
|
|
|
ExecAssignScanType(&subquerystate->ss,
|
2007-02-26 20:11:26 -05:00
|
|
|
ExecGetResultType(subquerystate->subplan));
|
2005-05-22 18:30:20 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2003-01-12 17:01:38 -05:00
|
|
|
* Initialize result tuple type and projection info.
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecAssignResultTypeFromTL(&subquerystate->ss.ps);
|
2005-05-22 18:30:20 -04:00
|
|
|
ExecAssignScanProjectionInfo(&subquerystate->ss);
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2002-12-05 10:50:39 -05:00
|
|
|
return subquerystate;
|
2000-09-29 14:21:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ExecCountSlotsSubqueryScan(SubqueryScan *node)
|
|
|
|
|
{
|
2007-02-26 20:11:26 -05:00
|
|
|
Assert(outerPlan(node) == NULL);
|
|
|
|
|
Assert(innerPlan(node) == NULL);
|
|
|
|
|
return ExecCountSlotsNode(node->subplan) +
|
2001-10-25 01:50:21 -04:00
|
|
|
SUBQUERYSCAN_NSLOTS;
|
2000-09-29 14:21:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* ExecEndSubqueryScan
|
|
|
|
|
*
|
|
|
|
|
* frees any storage allocated through C routines.
|
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
void
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecEndSubqueryScan(SubqueryScanState *node)
|
2000-09-29 14:21:41 -04:00
|
|
|
{
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2002-12-15 11:17:59 -05:00
|
|
|
* Free the exprcontext
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecFreeExprContext(&node->ss.ps);
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2002-12-05 10:50:39 -05:00
|
|
|
* clean out the upper tuple table
|
2000-09-29 14:21:41 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
2005-10-14 22:49:52 -04:00
|
|
|
node->ss.ss_ScanTupleSlot = NULL; /* not ours to clear */
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2000-09-29 14:21:41 -04:00
|
|
|
* close down subquery
|
|
|
|
|
*/
|
2007-02-26 20:11:26 -05:00
|
|
|
ExecEndNode(node->subplan);
|
2000-09-29 14:21:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* ExecSubqueryReScan
|
|
|
|
|
*
|
|
|
|
|
* Rescans the relation.
|
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
void
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecSubqueryReScan(SubqueryScanState *node, ExprContext *exprCtxt)
|
2000-09-29 14:21:41 -04:00
|
|
|
{
|
|
|
|
|
EState *estate;
|
|
|
|
|
|
2002-12-05 10:50:39 -05:00
|
|
|
estate = node->ss.ps.state;
|
2000-09-29 14:21:41 -04:00
|
|
|
|
2001-05-08 15:47:02 -04:00
|
|
|
/*
|
|
|
|
|
* ExecReScan doesn't know about my subplan, so I have to do
|
2005-10-14 22:49:52 -04:00
|
|
|
* changed-parameter signaling myself. This is just as well, because the
|
|
|
|
|
* subplan has its own memory context in which its chgParam state lives.
|
2001-05-08 15:47:02 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
if (node->ss.ps.chgParam != NULL)
|
2003-02-08 19:30:41 -05:00
|
|
|
UpdateChangedParamSet(node->subplan, node->ss.ps.chgParam);
|
2001-10-25 01:50:21 -04:00
|
|
|
|
2001-05-08 15:47:02 -04:00
|
|
|
/*
|
|
|
|
|
* if chgParam of subnode is not null then plan will be re-scanned by
|
|
|
|
|
* first ExecProcNode.
|
|
|
|
|
*/
|
|
|
|
|
if (node->subplan->chgParam == NULL)
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecReScan(node->subplan, NULL);
|
2000-10-05 15:11:39 -04:00
|
|
|
|
2002-12-05 10:50:39 -05:00
|
|
|
node->ss.ss_ScanTupleSlot = NULL;
|
2006-12-26 14:26:46 -05:00
|
|
|
node->ss.ps.ps_TupFromTlist = false;
|
2000-09-29 14:21:41 -04:00
|
|
|
}
|