mirror of
https://github.com/postgres/postgres.git
synced 2026-02-10 06:13:34 -05:00
Formerly, set_subquery_pathlist and other creators of plans for subqueries saved only the rangetable and rowMarks lists from the lower-level PlannerInfo. But there's no reason not to remember the whole PlannerInfo, and indeed this turns out to simplify matters in a number of places. The immediate reason for doing this was so that the subroot will still be accessible when we're trying to extract column statistics out of an already-planned subquery. But now that I've done it, it seems like a good code-beautification effort in its own right. I also chose to get rid of the transient subrtable and subrowmark fields in SubqueryScan nodes, in favor of having setrefs.c look up the subquery's RelOptInfo. That required changing all the APIs in setrefs.c to pass PlannerInfo not PlannerGlobal, which was a large but quite mechanical transformation. One side-effect not foreseen at the beginning is that this finally broke inheritance_planner's assumption that replanning the same subquery RTE N times would necessarily give interchangeable results each time. That assumption was always pretty risky, but now we really have to make a separate RTE for each instance so that there's a place to carry the separate subroots.
209 lines
5.5 KiB
C
209 lines
5.5 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* nodeSubqueryscan.c
|
|
* Support routines for scanning subqueries (subselects in rangetable).
|
|
*
|
|
* 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.
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
*
|
|
* IDENTIFICATION
|
|
* src/backend/executor/nodeSubqueryscan.c
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
/*
|
|
* 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.
|
|
* ExecReScanSubqueryScan rescans the relation
|
|
*
|
|
*/
|
|
#include "postgres.h"
|
|
|
|
#include "executor/execdebug.h"
|
|
#include "executor/nodeSubqueryscan.h"
|
|
|
|
static TupleTableSlot *SubqueryNext(SubqueryScanState *node);
|
|
|
|
/* ----------------------------------------------------------------
|
|
* Scan Support
|
|
* ----------------------------------------------------------------
|
|
*/
|
|
/* ----------------------------------------------------------------
|
|
* SubqueryNext
|
|
*
|
|
* This is a workhorse for ExecSubqueryScan
|
|
* ----------------------------------------------------------------
|
|
*/
|
|
static TupleTableSlot *
|
|
SubqueryNext(SubqueryScanState *node)
|
|
{
|
|
TupleTableSlot *slot;
|
|
|
|
/*
|
|
* Get the next tuple from the sub-query.
|
|
*/
|
|
slot = ExecProcNode(node->subplan);
|
|
|
|
/*
|
|
* We just return the subplan's result slot, rather than expending extra
|
|
* cycles for ExecCopySlot(). (Our own ScanTupleSlot is used only for
|
|
* EvalPlanQual rechecks.)
|
|
*/
|
|
return slot;
|
|
}
|
|
|
|
/*
|
|
* SubqueryRecheck -- access method routine to recheck a tuple in EvalPlanQual
|
|
*/
|
|
static bool
|
|
SubqueryRecheck(SubqueryScanState *node, TupleTableSlot *slot)
|
|
{
|
|
/* nothing to check */
|
|
return true;
|
|
}
|
|
|
|
/* ----------------------------------------------------------------
|
|
* ExecSubqueryScan(node)
|
|
*
|
|
* Scans the subquery sequentially and returns the next qualifying
|
|
* tuple.
|
|
* We call the ExecScan() routine and pass it the appropriate
|
|
* access method functions.
|
|
* ----------------------------------------------------------------
|
|
*/
|
|
TupleTableSlot *
|
|
ExecSubqueryScan(SubqueryScanState *node)
|
|
{
|
|
return ExecScan(&node->ss,
|
|
(ExecScanAccessMtd) SubqueryNext,
|
|
(ExecScanRecheckMtd) SubqueryRecheck);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------
|
|
* ExecInitSubqueryScan
|
|
* ----------------------------------------------------------------
|
|
*/
|
|
SubqueryScanState *
|
|
ExecInitSubqueryScan(SubqueryScan *node, EState *estate, int eflags)
|
|
{
|
|
SubqueryScanState *subquerystate;
|
|
|
|
/* check for unsupported flags */
|
|
Assert(!(eflags & EXEC_FLAG_MARK));
|
|
|
|
/* SubqueryScan should not have any "normal" children */
|
|
Assert(outerPlan(node) == NULL);
|
|
Assert(innerPlan(node) == NULL);
|
|
|
|
/*
|
|
* create state structure
|
|
*/
|
|
subquerystate = makeNode(SubqueryScanState);
|
|
subquerystate->ss.ps.plan = (Plan *) node;
|
|
subquerystate->ss.ps.state = estate;
|
|
|
|
/*
|
|
* Miscellaneous initialization
|
|
*
|
|
* create expression context for node
|
|
*/
|
|
ExecAssignExprContext(estate, &subquerystate->ss.ps);
|
|
|
|
/*
|
|
* initialize child expressions
|
|
*/
|
|
subquerystate->ss.ps.targetlist = (List *)
|
|
ExecInitExpr((Expr *) node->scan.plan.targetlist,
|
|
(PlanState *) subquerystate);
|
|
subquerystate->ss.ps.qual = (List *)
|
|
ExecInitExpr((Expr *) node->scan.plan.qual,
|
|
(PlanState *) subquerystate);
|
|
|
|
/*
|
|
* tuple table initialization
|
|
*/
|
|
ExecInitResultTupleSlot(estate, &subquerystate->ss.ps);
|
|
ExecInitScanTupleSlot(estate, &subquerystate->ss);
|
|
|
|
/*
|
|
* initialize subquery
|
|
*/
|
|
subquerystate->subplan = ExecInitNode(node->subplan, estate, eflags);
|
|
|
|
subquerystate->ss.ps.ps_TupFromTlist = false;
|
|
|
|
/*
|
|
* Initialize scan tuple type (needed by ExecAssignScanProjectionInfo)
|
|
*/
|
|
ExecAssignScanType(&subquerystate->ss,
|
|
ExecGetResultType(subquerystate->subplan));
|
|
|
|
/*
|
|
* Initialize result tuple type and projection info.
|
|
*/
|
|
ExecAssignResultTypeFromTL(&subquerystate->ss.ps);
|
|
ExecAssignScanProjectionInfo(&subquerystate->ss);
|
|
|
|
return subquerystate;
|
|
}
|
|
|
|
/* ----------------------------------------------------------------
|
|
* ExecEndSubqueryScan
|
|
*
|
|
* frees any storage allocated through C routines.
|
|
* ----------------------------------------------------------------
|
|
*/
|
|
void
|
|
ExecEndSubqueryScan(SubqueryScanState *node)
|
|
{
|
|
/*
|
|
* Free the exprcontext
|
|
*/
|
|
ExecFreeExprContext(&node->ss.ps);
|
|
|
|
/*
|
|
* clean out the upper tuple table
|
|
*/
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
|
ExecClearTuple(node->ss.ss_ScanTupleSlot);
|
|
|
|
/*
|
|
* close down subquery
|
|
*/
|
|
ExecEndNode(node->subplan);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------
|
|
* ExecReScanSubqueryScan
|
|
*
|
|
* Rescans the relation.
|
|
* ----------------------------------------------------------------
|
|
*/
|
|
void
|
|
ExecReScanSubqueryScan(SubqueryScanState *node)
|
|
{
|
|
ExecScanReScan(&node->ss);
|
|
|
|
/*
|
|
* ExecReScan doesn't know about my subplan, so I have to do
|
|
* changed-parameter signaling myself. This is just as well, because the
|
|
* subplan has its own memory context in which its chgParam state lives.
|
|
*/
|
|
if (node->ss.ps.chgParam != NULL)
|
|
UpdateChangedParamSet(node->subplan, node->ss.ps.chgParam);
|
|
|
|
/*
|
|
* if chgParam of subnode is not null then plan will be re-scanned by
|
|
* first ExecProcNode.
|
|
*/
|
|
if (node->subplan->chgParam == NULL)
|
|
ExecReScan(node->subplan);
|
|
}
|