PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeCtescan.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeCtescan.c
4  * routines to handle CteScan nodes.
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/executor/nodeCtescan.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "executor/execdebug.h"
19 #include "executor/nodeCtescan.h"
20 #include "miscadmin.h"
21 
23 
24 /* ----------------------------------------------------------------
25  * CteScanNext
26  *
27  * This is a workhorse for ExecCteScan
28  * ----------------------------------------------------------------
29  */
30 static TupleTableSlot *
32 {
33  EState *estate;
34  ScanDirection dir;
35  bool forward;
36  Tuplestorestate *tuplestorestate;
37  bool eof_tuplestore;
38  TupleTableSlot *slot;
39 
40  /*
41  * get state info from node
42  */
43  estate = node->ss.ps.state;
44  dir = estate->es_direction;
45  forward = ScanDirectionIsForward(dir);
46  tuplestorestate = node->leader->cte_table;
47  tuplestore_select_read_pointer(tuplestorestate, node->readptr);
48  slot = node->ss.ss_ScanTupleSlot;
49 
50  /*
51  * If we are not at the end of the tuplestore, or are going backwards, try
52  * to fetch a tuple from tuplestore.
53  */
54  eof_tuplestore = tuplestore_ateof(tuplestorestate);
55 
56  if (!forward && eof_tuplestore)
57  {
58  if (!node->leader->eof_cte)
59  {
60  /*
61  * When reversing direction at tuplestore EOF, the first
62  * gettupleslot call will fetch the last-added tuple; but we want
63  * to return the one before that, if possible. So do an extra
64  * fetch.
65  */
66  if (!tuplestore_advance(tuplestorestate, forward))
67  return NULL; /* the tuplestore must be empty */
68  }
69  eof_tuplestore = false;
70  }
71 
72  /*
73  * If we can fetch another tuple from the tuplestore, return it.
74  *
75  * Note: we have to use copy=true in the tuplestore_gettupleslot call,
76  * because we are sharing the tuplestore with other nodes that might write
77  * into the tuplestore before we get called again.
78  */
79  if (!eof_tuplestore)
80  {
81  if (tuplestore_gettupleslot(tuplestorestate, forward, true, slot))
82  return slot;
83  if (forward)
84  eof_tuplestore = true;
85  }
86 
87  /*
88  * If necessary, try to fetch another row from the CTE query.
89  *
90  * Note: the eof_cte state variable exists to short-circuit further calls
91  * of the CTE plan. It's not optional, unfortunately, because some plan
92  * node types are not robust about being called again when they've already
93  * returned NULL.
94  */
95  if (eof_tuplestore && !node->leader->eof_cte)
96  {
97  TupleTableSlot *cteslot;
98 
99  /*
100  * We can only get here with forward==true, so no need to worry about
101  * which direction the subplan will go.
102  */
103  cteslot = ExecProcNode(node->cteplanstate);
104  if (TupIsNull(cteslot))
105  {
106  node->leader->eof_cte = true;
107  return NULL;
108  }
109 
110  /*
111  * Append a copy of the returned tuple to tuplestore. NOTE: because
112  * our read pointer is certainly in EOF state, its read position will
113  * move forward over the added tuple. This is what we want. Also,
114  * any other readers will *not* move past the new tuple, which is what
115  * they want.
116  */
117  tuplestore_puttupleslot(tuplestorestate, cteslot);
118 
119  /*
120  * We MUST copy the CTE query's output tuple into our own slot. This
121  * is because other CteScan nodes might advance the CTE query before
122  * we are called again, and our output tuple must stay stable over
123  * that.
124  */
125  return ExecCopySlot(slot, cteslot);
126  }
127 
128  /*
129  * Nothing left ...
130  */
131  return ExecClearTuple(slot);
132 }
133 
134 /*
135  * CteScanRecheck -- access method routine to recheck a tuple in EvalPlanQual
136  */
137 static bool
139 {
140  /* nothing to check */
141  return true;
142 }
143 
144 /* ----------------------------------------------------------------
145  * ExecCteScan(node)
146  *
147  * Scans the CTE sequentially and returns the next qualifying tuple.
148  * We call the ExecScan() routine and pass it the appropriate
149  * access method functions.
150  * ----------------------------------------------------------------
151  */
154 {
155  return ExecScan(&node->ss,
158 }
159 
160 
161 /* ----------------------------------------------------------------
162  * ExecInitCteScan
163  * ----------------------------------------------------------------
164  */
165 CteScanState *
166 ExecInitCteScan(CteScan *node, EState *estate, int eflags)
167 {
168  CteScanState *scanstate;
169  ParamExecData *prmdata;
170 
171  /* check for unsupported flags */
172  Assert(!(eflags & EXEC_FLAG_MARK));
173 
174  /*
175  * For the moment we have to force the tuplestore to allow REWIND, because
176  * we might be asked to rescan the CTE even though upper levels didn't
177  * tell us to be prepared to do it efficiently. Annoying, since this
178  * prevents truncation of the tuplestore. XXX FIXME
179  */
180  eflags |= EXEC_FLAG_REWIND;
181 
182  /*
183  * CteScan should not have any children.
184  */
185  Assert(outerPlan(node) == NULL);
186  Assert(innerPlan(node) == NULL);
187 
188  /*
189  * create new CteScanState for node
190  */
191  scanstate = makeNode(CteScanState);
192  scanstate->ss.ps.plan = (Plan *) node;
193  scanstate->ss.ps.state = estate;
194  scanstate->eflags = eflags;
195  scanstate->cte_table = NULL;
196  scanstate->eof_cte = false;
197 
198  /*
199  * Find the already-initialized plan for the CTE query.
200  */
201  scanstate->cteplanstate = (PlanState *) list_nth(estate->es_subplanstates,
202  node->ctePlanId - 1);
203 
204  /*
205  * The Param slot associated with the CTE query is used to hold a pointer
206  * to the CteState of the first CteScan node that initializes for this
207  * CTE. This node will be the one that holds the shared state for all the
208  * CTEs, particularly the shared tuplestore.
209  */
210  prmdata = &(estate->es_param_exec_vals[node->cteParam]);
211  Assert(prmdata->execPlan == NULL);
212  Assert(!prmdata->isnull);
213  scanstate->leader = castNode(CteScanState, DatumGetPointer(prmdata->value));
214  if (scanstate->leader == NULL)
215  {
216  /* I am the leader */
217  prmdata->value = PointerGetDatum(scanstate);
218  scanstate->leader = scanstate;
219  scanstate->cte_table = tuplestore_begin_heap(true, false, work_mem);
220  tuplestore_set_eflags(scanstate->cte_table, scanstate->eflags);
221  scanstate->readptr = 0;
222  }
223  else
224  {
225  /* Not the leader */
226  /* Create my own read pointer, and ensure it is at start */
227  scanstate->readptr =
229  scanstate->eflags);
231  scanstate->readptr);
232  tuplestore_rescan(scanstate->leader->cte_table);
233  }
234 
235  /*
236  * Miscellaneous initialization
237  *
238  * create expression context for node
239  */
240  ExecAssignExprContext(estate, &scanstate->ss.ps);
241 
242  /*
243  * initialize child expressions
244  */
245  scanstate->ss.ps.qual =
246  ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
247 
248  /*
249  * tuple table initialization
250  */
251  ExecInitResultTupleSlot(estate, &scanstate->ss.ps);
252  ExecInitScanTupleSlot(estate, &scanstate->ss);
253 
254  /*
255  * The scan tuple type (ie, the rowtype we expect to find in the work
256  * table) is the same as the result rowtype of the CTE query.
257  */
258  ExecAssignScanType(&scanstate->ss,
259  ExecGetResultType(scanstate->cteplanstate));
260 
261  /*
262  * Initialize result tuple type and projection info.
263  */
264  ExecAssignResultTypeFromTL(&scanstate->ss.ps);
265  ExecAssignScanProjectionInfo(&scanstate->ss);
266 
267  return scanstate;
268 }
269 
270 /* ----------------------------------------------------------------
271  * ExecEndCteScan
272  *
273  * frees any storage allocated through C routines.
274  * ----------------------------------------------------------------
275  */
276 void
278 {
279  /*
280  * Free exprcontext
281  */
282  ExecFreeExprContext(&node->ss.ps);
283 
284  /*
285  * clean out the tuple table
286  */
289 
290  /*
291  * If I am the leader, free the tuplestore.
292  */
293  if (node->leader == node)
294  {
295  tuplestore_end(node->cte_table);
296  node->cte_table = NULL;
297  }
298 }
299 
300 /* ----------------------------------------------------------------
301  * ExecReScanCteScan
302  *
303  * Rescans the relation.
304  * ----------------------------------------------------------------
305  */
306 void
308 {
309  Tuplestorestate *tuplestorestate = node->leader->cte_table;
310 
312 
313  ExecScanReScan(&node->ss);
314 
315  /*
316  * Clear the tuplestore if a new scan of the underlying CTE is required.
317  * This implicitly resets all the tuplestore's read pointers. Note that
318  * multiple CTE nodes might redundantly clear the tuplestore; that's OK,
319  * and not unduly expensive. We'll stop taking this path as soon as
320  * somebody has attempted to read something from the underlying CTE
321  * (thereby causing its chgParam to be cleared).
322  */
323  if (node->leader->cteplanstate->chgParam != NULL)
324  {
325  tuplestore_clear(tuplestorestate);
326  node->leader->eof_cte = false;
327  }
328  else
329  {
330  /*
331  * Else, just rewind my own pointer. Either the underlying CTE
332  * doesn't need a rescan (and we can re-read what's in the tuplestore
333  * now), or somebody else already took care of it.
334  */
335  tuplestore_select_read_pointer(tuplestorestate, node->readptr);
336  tuplestore_rescan(tuplestorestate);
337  }
338 }
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1233
static bool CteScanRecheck(CteScanState *node, TupleTableSlot *slot)
Definition: nodeCtescan.c:138
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
Definition: tuplestore.c:708
List * qual
Definition: plannodes.h:145
void * execPlan
Definition: params.h:99
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:398
Plan plan
Definition: plannodes.h:328
bool tuplestore_advance(Tuplestorestate *state, bool forward)
Definition: tuplestore.c:1110
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
void tuplestore_set_eflags(Tuplestorestate *state, int eflags)
Definition: tuplestore.c:359
#define PointerGetDatum(X)
Definition: postgres.h:562
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:121
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
CteScanState * ExecInitCteScan(CteScan *node, EState *estate, int eflags)
Definition: nodeCtescan.c:166
struct CteScanState * leader
Definition: execnodes.h:1472
ScanState ss
Definition: execnodes.h:1467
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1078
EState * state
Definition: execnodes.h:834
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:516
ScanDirection es_direction
Definition: execnodes.h:428
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:440
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:389
PlanState ps
Definition: execnodes.h:1075
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:390
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:861
ParamExecData * es_param_exec_vals
Definition: execnodes.h:463
int cteParam
Definition: plannodes.h:540
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:235
bool isnull
Definition: params.h:101
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
Scan scan
Definition: plannodes.h:538
void tuplestore_clear(Tuplestorestate *state)
Definition: tuplestore.c:418
#define innerPlan(node)
Definition: plannodes.h:173
void * list_nth(const List *list, int n)
Definition: list.c:410
int ctePlanId
Definition: plannodes.h:539
ScanDirection
Definition: sdir.h:22
#define TupIsNull(slot)
Definition: tuptable.h:138
#define EXEC_FLAG_REWIND
Definition: executor.h:59
Bitmapset * chgParam
Definition: execnodes.h:856
#define outerPlan(node)
Definition: plannodes.h:174
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
void ExecEndCteScan(CteScanState *node)
Definition: nodeCtescan.c:277
int work_mem
Definition: globals.c:113
Plan * plan
Definition: execnodes.h:832
TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: execTuples.c:795
#define makeNode(_type_)
Definition: nodes.h:557
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define EXEC_FLAG_MARK
Definition: executor.h:61
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:418
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
List * es_subplanstates
Definition: execnodes.h:483
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:469
ExprState * qual
Definition: execnodes.h:846
#define DatumGetPointer(X)
Definition: postgres.h:555
int tuplestore_alloc_read_pointer(Tuplestorestate *state, int eflags)
Definition: tuplestore.c:383
PlanState * cteplanstate
Definition: execnodes.h:1470
TupleTableSlot * ExecCteScan(CteScanState *node)
Definition: nodeCtescan.c:153
static TupleTableSlot * CteScanNext(CteScanState *node)
Definition: nodeCtescan.c:31
void ExecScanReScan(ScanState *node)
Definition: execScan.c:327
void ExecReScanCteScan(CteScanState *node)
Definition: nodeCtescan.c:307
void tuplestore_select_read_pointer(Tuplestorestate *state, int ptr)
Definition: tuplestore.c:473
Datum value
Definition: params.h:100
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:540
bool tuplestore_ateof(Tuplestorestate *state)
Definition: tuplestore.c:557
Tuplestorestate * cte_table
Definition: execnodes.h:1474