PostgreSQL Source Code  git master
nodeSubqueryscan.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeSubqueryscan.c
4  * Support routines for scanning subqueries (subselects in rangetable).
5  *
6  * This is just enough different from sublinks (nodeSubplan.c) to mean that
7  * we need two sets of code. Ought to look at trying to unify the cases.
8  *
9  *
10  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  * src/backend/executor/nodeSubqueryscan.c
16  *
17  *-------------------------------------------------------------------------
18  */
19 /*
20  * INTERFACE ROUTINES
21  * ExecSubqueryScan scans a subquery.
22  * ExecSubqueryNext retrieve next tuple in sequential order.
23  * ExecInitSubqueryScan creates and initializes a subqueryscan node.
24  * ExecEndSubqueryScan releases any storage allocated.
25  * ExecReScanSubqueryScan rescans the relation
26  *
27  */
28 #include "postgres.h"
29 
30 #include "executor/executor.h"
32 
34 
35 /* ----------------------------------------------------------------
36  * Scan Support
37  * ----------------------------------------------------------------
38  */
39 /* ----------------------------------------------------------------
40  * SubqueryNext
41  *
42  * This is a workhorse for ExecSubqueryScan
43  * ----------------------------------------------------------------
44  */
45 static TupleTableSlot *
47 {
48  TupleTableSlot *slot;
49 
50  /*
51  * Get the next tuple from the sub-query.
52  */
53  slot = ExecProcNode(node->subplan);
54 
55  /*
56  * We just return the subplan's result slot, rather than expending extra
57  * cycles for ExecCopySlot(). (Our own ScanTupleSlot is used only for
58  * EvalPlanQual rechecks.)
59  */
60  return slot;
61 }
62 
63 /*
64  * SubqueryRecheck -- access method routine to recheck a tuple in EvalPlanQual
65  */
66 static bool
68 {
69  /* nothing to check */
70  return true;
71 }
72 
73 /* ----------------------------------------------------------------
74  * ExecSubqueryScan(node)
75  *
76  * Scans the subquery sequentially and returns the next qualifying
77  * tuple.
78  * We call the ExecScan() routine and pass it the appropriate
79  * access method functions.
80  * ----------------------------------------------------------------
81  */
82 static TupleTableSlot *
84 {
86 
87  return ExecScan(&node->ss,
90 }
91 
92 /* ----------------------------------------------------------------
93  * ExecInitSubqueryScan
94  * ----------------------------------------------------------------
95  */
97 ExecInitSubqueryScan(SubqueryScan *node, EState *estate, int eflags)
98 {
99  SubqueryScanState *subquerystate;
100 
101  /* check for unsupported flags */
102  Assert(!(eflags & EXEC_FLAG_MARK));
103 
104  /* SubqueryScan should not have any "normal" children */
105  Assert(outerPlan(node) == NULL);
106  Assert(innerPlan(node) == NULL);
107 
108  /*
109  * create state structure
110  */
111  subquerystate = makeNode(SubqueryScanState);
112  subquerystate->ss.ps.plan = (Plan *) node;
113  subquerystate->ss.ps.state = estate;
114  subquerystate->ss.ps.ExecProcNode = ExecSubqueryScan;
115 
116  /*
117  * Miscellaneous initialization
118  *
119  * create expression context for node
120  */
121  ExecAssignExprContext(estate, &subquerystate->ss.ps);
122 
123  /*
124  * initialize subquery
125  */
126  subquerystate->subplan = ExecInitNode(node->subplan, estate, eflags);
127 
128  /*
129  * Initialize scan slot and type (needed by ExecAssignScanProjectionInfo)
130  */
131  ExecInitScanTupleSlot(estate, &subquerystate->ss,
132  ExecGetResultType(subquerystate->subplan),
133  ExecGetResultSlotOps(subquerystate->subplan, NULL));
134 
135  /*
136  * The slot used as the scantuple isn't the slot above (outside of EPQ),
137  * but the one from the node below.
138  */
139  subquerystate->ss.ps.scanopsset = true;
140  subquerystate->ss.ps.scanops = ExecGetResultSlotOps(subquerystate->subplan,
141  &subquerystate->ss.ps.scanopsfixed);
142  subquerystate->ss.ps.resultopsset = true;
143  subquerystate->ss.ps.resultops = subquerystate->ss.ps.scanops;
144  subquerystate->ss.ps.resultopsfixed = subquerystate->ss.ps.scanopsfixed;
145 
146  /*
147  * Initialize result type and projection.
148  */
149  ExecInitResultTypeTL(&subquerystate->ss.ps);
150  ExecAssignScanProjectionInfo(&subquerystate->ss);
151 
152  /*
153  * initialize child expressions
154  */
155  subquerystate->ss.ps.qual =
156  ExecInitQual(node->scan.plan.qual, (PlanState *) subquerystate);
157 
158  return subquerystate;
159 }
160 
161 /* ----------------------------------------------------------------
162  * ExecEndSubqueryScan
163  *
164  * frees any storage allocated through C routines.
165  * ----------------------------------------------------------------
166  */
167 void
169 {
170  /*
171  * close down subquery
172  */
173  ExecEndNode(node->subplan);
174 }
175 
176 /* ----------------------------------------------------------------
177  * ExecReScanSubqueryScan
178  *
179  * Rescans the relation.
180  * ----------------------------------------------------------------
181  */
182 void
184 {
185  ExecScanReScan(&node->ss);
186 
187  /*
188  * ExecReScan doesn't know about my subplan, so I have to do
189  * changed-parameter signaling myself. This is just as well, because the
190  * subplan has its own memory context in which its chgParam state lives.
191  */
192  if (node->ss.ps.chgParam != NULL)
194 
195  /*
196  * if chgParam of subnode is not null then plan will be re-scanned by
197  * first ExecProcNode.
198  */
199  if (node->subplan->chgParam == NULL)
200  ExecReScan(node->subplan);
201 }
#define Assert(condition)
Definition: c.h:858
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:220
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:156
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:270
void ExecScanReScan(ScanState *node)
Definition: execScan.c:297
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1898
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1842
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:493
const TupleTableSlotOps * ExecGetResultSlotOps(PlanState *planstate, bool *isfixed)
Definition: execUtils.c:502
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:483
void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
Definition: execUtils.c:844
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:473
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:474
#define EXEC_FLAG_MARK
Definition: executor.h:69
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:269
void ExecEndSubqueryScan(SubqueryScanState *node)
SubqueryScanState * ExecInitSubqueryScan(SubqueryScan *node, EState *estate, int eflags)
static TupleTableSlot * SubqueryNext(SubqueryScanState *node)
static TupleTableSlot * ExecSubqueryScan(PlanState *pstate)
void ExecReScanSubqueryScan(SubqueryScanState *node)
static bool SubqueryRecheck(SubqueryScanState *node, TupleTableSlot *slot)
#define makeNode(_type_)
Definition: nodes.h:155
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
#define innerPlan(node)
Definition: plannodes.h:181
#define outerPlan(node)
Definition: plannodes.h:182
const TupleTableSlotOps * resultops
Definition: execnodes.h:1194
bool resultopsset
Definition: execnodes.h:1202
ExprState * qual
Definition: execnodes.h:1138
const TupleTableSlotOps * scanops
Definition: execnodes.h:1191
Plan * plan
Definition: execnodes.h:1117
EState * state
Definition: execnodes.h:1119
Bitmapset * chgParam
Definition: execnodes.h:1149
bool scanopsset
Definition: execnodes.h:1199
bool scanopsfixed
Definition: execnodes.h:1195
bool resultopsfixed
Definition: execnodes.h:1198
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1123
PlanState ps
Definition: execnodes.h:1564
PlanState * subplan
Definition: execnodes.h:1877
Plan * subplan
Definition: plannodes.h:601