PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeCtescan.h File Reference
#include "nodes/execnodes.h"
Include dependency graph for nodeCtescan.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

CteScanStateExecInitCteScan (CteScan *node, EState *estate, int eflags)
 
void ExecEndCteScan (CteScanState *node)
 
void ExecReScanCteScan (CteScanState *node)
 

Function Documentation

◆ ExecEndCteScan()

void ExecEndCteScan ( CteScanState node)
extern

Definition at line 289 of file nodeCtescan.c.

290{
291 /*
292 * If I am the leader, free the tuplestore.
293 */
294 if (node->leader == node)
295 {
297 node->cte_table = NULL;
298 }
299}
static int fb(int x)
Tuplestorestate * cte_table
Definition execnodes.h:2023
struct CteScanState * leader
Definition execnodes.h:2021
void tuplestore_end(Tuplestorestate *state)
Definition tuplestore.c:493

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

Referenced by ExecEndNode().

◆ ExecInitCteScan()

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

Definition at line 176 of file nodeCtescan.c.

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

References Assert, castNode, CteScan::cteParam, CteScan::ctePlanId, DatumGetPointer(), EState::es_param_exec_vals, EState::es_subplanstates, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecCteScan(), ExecGetResultType(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), fb(), innerPlan, list_nth(), makeNode, outerPlan, PointerGetDatum(), CteScan::scan, TTSOpsMinimalTuple, tuplestore_alloc_read_pointer(), tuplestore_begin_heap(), tuplestore_rescan(), tuplestore_select_read_pointer(), tuplestore_set_eflags(), and work_mem.

Referenced by ExecInitNode().

◆ ExecReScanCteScan()

void ExecReScanCteScan ( CteScanState node)
extern

Definition at line 308 of file nodeCtescan.c.

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

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

Referenced by ExecReScan().