PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeNamedtuplestorescan.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * nodeNamedtuplestorescan.c
4 * routines to handle NamedTuplestoreScan nodes.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/executor/nodeNamedtuplestorescan.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
18#include "executor/executor.h"
21#include "utils/tuplestore.h"
22
24
25/* ----------------------------------------------------------------
26 * NamedTuplestoreScanNext
27 *
28 * This is a workhorse for ExecNamedTuplestoreScan
29 * ----------------------------------------------------------------
30 */
31static TupleTableSlot *
33{
34 TupleTableSlot *slot;
35
36 /* We intentionally do not support backward scan. */
38
39 /*
40 * Get the next tuple from tuplestore. Return NULL if no more tuples.
41 */
42 slot = node->ss.ss_ScanTupleSlot;
44 (void) tuplestore_gettupleslot(node->relation, true, false, slot);
45 return slot;
46}
47
48/*
49 * NamedTuplestoreScanRecheck -- access method routine to recheck a tuple in
50 * EvalPlanQual
51 */
52static bool
54{
55 /* nothing to check */
56 return true;
57}
58
59/* ----------------------------------------------------------------
60 * ExecNamedTuplestoreScan(node)
61 *
62 * Scans the CTE sequentially and returns the next qualifying tuple.
63 * We call the ExecScan() routine and pass it the appropriate
64 * access method functions.
65 * ----------------------------------------------------------------
66 */
67static TupleTableSlot *
76
77
78/* ----------------------------------------------------------------
79 * ExecInitNamedTuplestoreScan
80 * ----------------------------------------------------------------
81 */
84{
87
88 /* check for unsupported flags */
90
91 /*
92 * NamedTuplestoreScan should not have any children.
93 */
94 Assert(outerPlan(node) == NULL);
95 Assert(innerPlan(node) == NULL);
96
97 /*
98 * create new NamedTuplestoreScanState for node
99 */
101 scanstate->ss.ps.plan = (Plan *) node;
102 scanstate->ss.ps.state = estate;
103 scanstate->ss.ps.ExecProcNode = ExecNamedTuplestoreScan;
104
105 enr = get_ENR(estate->es_queryEnv, node->enrname);
106 if (!enr)
107 elog(ERROR, "executor could not find named tuplestore \"%s\"",
108 node->enrname);
109
110 Assert(enr->reldata);
111 scanstate->relation = (Tuplestorestate *) enr->reldata;
112 scanstate->tupdesc = ENRMetadataGetTupDesc(&(enr->md));
113 scanstate->readptr =
115
116 /*
117 * The new read pointer copies its position from read pointer 0, which
118 * could be anywhere, so explicitly rewind it.
119 */
121 tuplestore_rescan(scanstate->relation);
122
123 /*
124 * XXX: Should we add a function to free that read pointer when done?
125 *
126 * This was attempted, but it did not improve performance or memory usage
127 * in any tested cases.
128 */
129
130 /*
131 * Miscellaneous initialization
132 *
133 * create expression context for node
134 */
135 ExecAssignExprContext(estate, &scanstate->ss.ps);
136
137 /*
138 * The scan tuple type is specified for the tuplestore.
139 */
140 ExecInitScanTupleSlot(estate, &scanstate->ss, scanstate->tupdesc,
142
143 /*
144 * Initialize result type and projection.
145 */
148
149 /*
150 * initialize child expressions
151 */
152 scanstate->ss.ps.qual =
153 ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
154
155 return scanstate;
156}
157
158/* ----------------------------------------------------------------
159 * ExecReScanNamedTuplestoreScan
160 *
161 * Rescans the relation.
162 * ----------------------------------------------------------------
163 */
164void
166{
167 Tuplestorestate *tuplestorestate = node->relation;
168
169 if (node->ss.ps.ps_ResultTupleSlot)
171
172 ExecScanReScan(&node->ss);
173
174 /*
175 * Rewind my own pointer.
176 */
177 tuplestore_select_read_pointer(tuplestorestate, node->readptr);
178 tuplestore_rescan(tuplestorestate);
179}
#define Assert(condition)
Definition c.h:945
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition execExpr.c:250
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition execScan.c:47
void ExecAssignScanProjectionInfo(ScanState *node)
Definition execScan.c:81
void ExecScanReScan(ScanState *node)
Definition execScan.c:108
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
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition execUtils.c:490
#define EXEC_FLAG_BACKWARD
Definition executor.h:70
#define EXEC_FLAG_REWIND
Definition executor.h:69
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition executor.h:583
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition executor.h:582
#define EXEC_FLAG_MARK
Definition executor.h:71
static TupleTableSlot * ExecNamedTuplestoreScan(PlanState *pstate)
static TupleTableSlot * NamedTuplestoreScanNext(NamedTuplestoreScanState *node)
static bool NamedTuplestoreScanRecheck(NamedTuplestoreScanState *node, TupleTableSlot *slot)
void ExecReScanNamedTuplestoreScan(NamedTuplestoreScanState *node)
NamedTuplestoreScanState * ExecInitNamedTuplestoreScan(NamedTuplestoreScan *node, EState *estate, int eflags)
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
#define innerPlan(node)
Definition plannodes.h:264
#define outerPlan(node)
Definition plannodes.h:265
static int fb(int x)
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
EphemeralNamedRelation get_ENR(QueryEnvironment *queryEnv, const char *name)
#define ScanDirectionIsForward(direction)
Definition sdir.h:64
QueryEnvironment * es_queryEnv
Definition execnodes.h:719
ScanDirection es_direction
Definition execnodes.h:671
Tuplestorestate * relation
Definition execnodes.h:2042
EState * state
Definition execnodes.h:1179
TupleTableSlot * ps_ResultTupleSlot
Definition execnodes.h:1215
TupleTableSlot * ss_ScanTupleSlot
Definition execnodes.h:1636
PlanState ps
Definition execnodes.h:1633
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
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
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:476