PostgreSQL Source Code  git master
nodeCtescan.c File Reference
#include "postgres.h"
#include "executor/executor.h"
#include "executor/nodeCtescan.h"
#include "miscadmin.h"
Include dependency graph for nodeCtescan.c:

Go to the source code of this file.

Functions

static TupleTableSlotCteScanNext (CteScanState *node)
 
static bool CteScanRecheck (CteScanState *node, TupleTableSlot *slot)
 
static TupleTableSlotExecCteScan (PlanState *pstate)
 
CteScanStateExecInitCteScan (CteScan *node, EState *estate, int eflags)
 
void ExecEndCteScan (CteScanState *node)
 
void ExecReScanCteScan (CteScanState *node)
 

Function Documentation

◆ CteScanNext()

static TupleTableSlot * CteScanNext ( CteScanState node)
static

Definition at line 31 of file nodeCtescan.c.

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  * There are corner cases where the subplan could change which
112  * tuplestore read pointer is active, so be sure to reselect ours
113  * before storing the tuple we got.
114  */
115  tuplestore_select_read_pointer(tuplestorestate, node->readptr);
116 
117  /*
118  * Append a copy of the returned tuple to tuplestore. NOTE: because
119  * our read pointer is certainly in EOF state, its read position will
120  * move forward over the added tuple. This is what we want. Also,
121  * any other readers will *not* move past the new tuple, which is what
122  * they want.
123  */
124  tuplestore_puttupleslot(tuplestorestate, cteslot);
125 
126  /*
127  * We MUST copy the CTE query's output tuple into our own slot. This
128  * is because other CteScan nodes might advance the CTE query before
129  * we are called again, and our output tuple must stay stable over
130  * that.
131  */
132  return ExecCopySlot(slot, cteslot);
133  }
134 
135  /*
136  * Nothing left ...
137  */
138  return ExecClearTuple(slot);
139 }
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:269
#define ScanDirectionIsForward(direction)
Definition: sdir.h:64
ScanDirection
Definition: sdir.h:25
Tuplestorestate * cte_table
Definition: execnodes.h:1988
ScanState ss
Definition: execnodes.h:1981
PlanState * cteplanstate
Definition: execnodes.h:1984
struct CteScanState * leader
Definition: execnodes.h:1986
ScanDirection es_direction
Definition: execnodes.h:621
EState * state
Definition: execnodes.h:1118
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1559
PlanState ps
Definition: execnodes.h:1556
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
Definition: tuplestore.c:708
void tuplestore_select_read_pointer(Tuplestorestate *state, int ptr)
Definition: tuplestore.c:473
bool tuplestore_advance(Tuplestorestate *state, bool forward)
Definition: tuplestore.c:1110
bool tuplestore_ateof(Tuplestorestate *state)
Definition: tuplestore.c:557
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:454
static TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: tuptable.h:509
#define TupIsNull(slot)
Definition: tuptable.h:306

References CteScanState::cte_table, CteScanState::cteplanstate, CteScanState::eof_cte, EState::es_direction, ExecClearTuple(), ExecCopySlot(), ExecProcNode(), CteScanState::leader, ScanState::ps, CteScanState::readptr, ScanDirectionIsForward, CteScanState::ss, ScanState::ss_ScanTupleSlot, PlanState::state, TupIsNull, tuplestore_advance(), tuplestore_ateof(), tuplestore_gettupleslot(), tuplestore_puttupleslot(), and tuplestore_select_read_pointer().

Referenced by ExecCteScan().

◆ CteScanRecheck()

static bool CteScanRecheck ( CteScanState node,
TupleTableSlot slot 
)
static

Definition at line 145 of file nodeCtescan.c.

146 {
147  /* nothing to check */
148  return true;
149 }

Referenced by ExecCteScan().

◆ ExecCteScan()

static TupleTableSlot* ExecCteScan ( PlanState pstate)
static

Definition at line 160 of file nodeCtescan.c.

161 {
162  CteScanState *node = castNode(CteScanState, pstate);
163 
164  return ExecScan(&node->ss,
167 }
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:156
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:473
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:474
static bool CteScanRecheck(CteScanState *node, TupleTableSlot *slot)
Definition: nodeCtescan.c:145
static TupleTableSlot * CteScanNext(CteScanState *node)
Definition: nodeCtescan.c:31
#define castNode(_type_, nodeptr)
Definition: nodes.h:176

References castNode, CteScanNext(), CteScanRecheck(), ExecScan(), and CteScanState::ss.

Referenced by ExecInitCteScan().

◆ ExecEndCteScan()

void ExecEndCteScan ( CteScanState node)

Definition at line 288 of file nodeCtescan.c.

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 }
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453

References CteScanState::cte_table, CteScanState::leader, and tuplestore_end().

Referenced by ExecEndNode().

◆ ExecInitCteScan()

CteScanState* ExecInitCteScan ( CteScan node,
EState estate,
int  eflags 
)

Definition at line 175 of file nodeCtescan.c.

176 {
177  CteScanState *scanstate;
178  ParamExecData *prmdata;
179 
180  /* check for unsupported flags */
181  Assert(!(eflags & EXEC_FLAG_MARK));
182 
183  /*
184  * For the moment we have to force the tuplestore to allow REWIND, because
185  * we might be asked to rescan the CTE even though upper levels didn't
186  * tell us to be prepared to do it efficiently. Annoying, since this
187  * prevents truncation of the tuplestore. XXX FIXME
188  *
189  * Note: if we are in an EPQ recheck plan tree, it's likely that no access
190  * to the tuplestore is needed at all, making this even more annoying.
191  * It's not worth improving that as long as all the read pointers would
192  * have REWIND anyway, but if we ever improve this logic then that aspect
193  * should be considered too.
194  */
195  eflags |= EXEC_FLAG_REWIND;
196 
197  /*
198  * CteScan should not have any children.
199  */
200  Assert(outerPlan(node) == NULL);
201  Assert(innerPlan(node) == NULL);
202 
203  /*
204  * create new CteScanState for node
205  */
206  scanstate = makeNode(CteScanState);
207  scanstate->ss.ps.plan = (Plan *) node;
208  scanstate->ss.ps.state = estate;
209  scanstate->ss.ps.ExecProcNode = ExecCteScan;
210  scanstate->eflags = eflags;
211  scanstate->cte_table = NULL;
212  scanstate->eof_cte = false;
213 
214  /*
215  * Find the already-initialized plan for the CTE query.
216  */
217  scanstate->cteplanstate = (PlanState *) list_nth(estate->es_subplanstates,
218  node->ctePlanId - 1);
219 
220  /*
221  * The Param slot associated with the CTE query is used to hold a pointer
222  * to the CteState of the first CteScan node that initializes for this
223  * CTE. This node will be the one that holds the shared state for all the
224  * CTEs, particularly the shared tuplestore.
225  */
226  prmdata = &(estate->es_param_exec_vals[node->cteParam]);
227  Assert(prmdata->execPlan == NULL);
228  Assert(!prmdata->isnull);
229  scanstate->leader = castNode(CteScanState, DatumGetPointer(prmdata->value));
230  if (scanstate->leader == NULL)
231  {
232  /* I am the leader */
233  prmdata->value = PointerGetDatum(scanstate);
234  scanstate->leader = scanstate;
235  scanstate->cte_table = tuplestore_begin_heap(true, false, work_mem);
236  tuplestore_set_eflags(scanstate->cte_table, scanstate->eflags);
237  scanstate->readptr = 0;
238  }
239  else
240  {
241  /* Not the leader */
242  /* Create my own read pointer, and ensure it is at start */
243  scanstate->readptr =
245  scanstate->eflags);
247  scanstate->readptr);
248  tuplestore_rescan(scanstate->leader->cte_table);
249  }
250 
251  /*
252  * Miscellaneous initialization
253  *
254  * create expression context for node
255  */
256  ExecAssignExprContext(estate, &scanstate->ss.ps);
257 
258  /*
259  * The scan tuple type (ie, the rowtype we expect to find in the work
260  * table) is the same as the result rowtype of the CTE query.
261  */
262  ExecInitScanTupleSlot(estate, &scanstate->ss,
263  ExecGetResultType(scanstate->cteplanstate),
265 
266  /*
267  * Initialize result type and projection.
268  */
269  ExecInitResultTypeTL(&scanstate->ss.ps);
270  ExecAssignScanProjectionInfo(&scanstate->ss);
271 
272  /*
273  * initialize child expressions
274  */
275  scanstate->ss.ps.qual =
276  ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
277 
278  return scanstate;
279 }
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:220
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:270
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1898
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1842
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:493
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:483
#define EXEC_FLAG_REWIND
Definition: executor.h:67
#define EXEC_FLAG_MARK
Definition: executor.h:69
int work_mem
Definition: globals.c:128
Assert(fmt[strlen(fmt) - 1] !='\n')
static TupleTableSlot * ExecCteScan(PlanState *pstate)
Definition: nodeCtescan.c:160
#define makeNode(_type_)
Definition: nodes.h:155
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define innerPlan(node)
Definition: plannodes.h:181
#define outerPlan(node)
Definition: plannodes.h:182
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
int ctePlanId
Definition: plannodes.h:641
int cteParam
Definition: plannodes.h:642
Scan scan
Definition: plannodes.h:640
ParamExecData * es_param_exec_vals
Definition: execnodes.h:660
List * es_subplanstates
Definition: execnodes.h:680
bool isnull
Definition: params.h:150
Datum value
Definition: params.h:149
void * execPlan
Definition: params.h:148
ExprState * qual
Definition: execnodes.h:1137
Plan * plan
Definition: execnodes.h:1116
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1122
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1233
int tuplestore_alloc_read_pointer(Tuplestorestate *state, int eflags)
Definition: tuplestore.c:383
void tuplestore_set_eflags(Tuplestorestate *state, int eflags)
Definition: tuplestore.c:359

References Assert(), castNode, CteScanState::cte_table, CteScan::cteParam, CteScan::ctePlanId, CteScanState::cteplanstate, DatumGetPointer(), CteScanState::eflags, CteScanState::eof_cte, EState::es_param_exec_vals, EState::es_subplanstates, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecCteScan(), ExecGetResultType(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ParamExecData::execPlan, PlanState::ExecProcNode, innerPlan, ParamExecData::isnull, CteScanState::leader, list_nth(), makeNode, outerPlan, PlanState::plan, PointerGetDatum(), ScanState::ps, PlanState::qual, CteScanState::readptr, CteScan::scan, CteScanState::ss, PlanState::state, TTSOpsMinimalTuple, tuplestore_alloc_read_pointer(), tuplestore_begin_heap(), tuplestore_rescan(), tuplestore_select_read_pointer(), tuplestore_set_eflags(), ParamExecData::value, and work_mem.

Referenced by ExecInitNode().

◆ ExecReScanCteScan()

void ExecReScanCteScan ( CteScanState node)

Definition at line 307 of file nodeCtescan.c.

308 {
309  Tuplestorestate *tuplestorestate = node->leader->cte_table;
310 
311  if (node->ss.ps.ps_ResultTupleSlot)
313 
314  ExecScanReScan(&node->ss);
315 
316  /*
317  * Clear the tuplestore if a new scan of the underlying CTE is required.
318  * This implicitly resets all the tuplestore's read pointers. Note that
319  * multiple CTE nodes might redundantly clear the tuplestore; that's OK,
320  * and not unduly expensive. We'll stop taking this path as soon as
321  * somebody has attempted to read something from the underlying CTE
322  * (thereby causing its chgParam to be cleared).
323  */
324  if (node->leader->cteplanstate->chgParam != NULL)
325  {
326  tuplestore_clear(tuplestorestate);
327  node->leader->eof_cte = false;
328  }
329  else
330  {
331  /*
332  * Else, just rewind my own pointer. Either the underlying CTE
333  * doesn't need a rescan (and we can re-read what's in the tuplestore
334  * now), or somebody else already took care of it.
335  */
336  tuplestore_select_read_pointer(tuplestorestate, node->readptr);
337  tuplestore_rescan(tuplestorestate);
338  }
339 }
void ExecScanReScan(ScanState *node)
Definition: execScan.c:297
Bitmapset * chgParam
Definition: execnodes.h:1148
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1154
void tuplestore_clear(Tuplestorestate *state)
Definition: tuplestore.c:418

References PlanState::chgParam, CteScanState::cte_table, CteScanState::cteplanstate, CteScanState::eof_cte, ExecClearTuple(), ExecScanReScan(), CteScanState::leader, ScanState::ps, PlanState::ps_ResultTupleSlot, CteScanState::readptr, CteScanState::ss, tuplestore_clear(), tuplestore_rescan(), and tuplestore_select_read_pointer().

Referenced by ExecReScan().