1 /*-------------------------------------------------------------------------
4 * This code provides support for generalized relation scans. ExecScan
5 * is passed a node and a pointer to a function to "do the right thing"
6 * and return a tuple from the relation. ExecScan then does the tedious
7 * stuff - checking the qualification and projecting the tuple
10 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
15 * src/backend/executor/execScan.c
17 *-------------------------------------------------------------------------
21 #include "executor/executor.h"
22 #include "miscadmin.h"
23 #include "utils/memutils.h"
28 * ExecScanFetch -- check interrupts & fetch next potential tuple
30 * This routine is concerned with substituting a test tuple if we are
31 * inside an EvalPlanQual recheck. If we aren't, just execute
32 * the access method's next-tuple routine.
34 static inline TupleTableSlot *
35 ExecScanFetch(ScanState *node,
36 ExecScanAccessMtd accessMtd,
37 ExecScanRecheckMtd recheckMtd)
39 EState *estate = node->ps.state;
41 CHECK_FOR_INTERRUPTS();
43 if (estate->es_epqTupleSlot != NULL)
46 * We are inside an EvalPlanQual recheck. Return the test tuple if
47 * one is available, after rechecking any access-method-specific
50 Index scanrelid = ((Scan *) node->ps.plan)->scanrelid;
54 TupleTableSlot *slot = node->ss_ScanTupleSlot;
57 * This is a ForeignScan or CustomScan which has pushed down a
58 * join to the remote side. The recheck method is responsible not
59 * only for rechecking the scan/join quals but also for storing
60 * the correct tuple in the slot.
62 if (!(*recheckMtd) (node, slot))
63 ExecClearTuple(slot); /* would not be returned by scan */
66 else if (estate->es_epqTupleSlot[scanrelid - 1] != NULL)
68 TupleTableSlot *slot = node->ss_ScanTupleSlot;
70 /* Return empty slot if we already returned a tuple */
71 if (estate->es_epqScanDone[scanrelid - 1])
72 return ExecClearTuple(slot);
73 /* Else mark to remember that we shouldn't return more */
74 estate->es_epqScanDone[scanrelid - 1] = true;
76 slot = estate->es_epqTupleSlot[scanrelid - 1];
78 /* Return empty slot if we haven't got a test tuple */
82 /* Check if it meets the access-method conditions */
83 if (!(*recheckMtd) (node, slot))
84 return ExecClearTuple(slot); /* would not be returned by
92 * Run the node-type-specific access method function to get the next tuple
94 return (*accessMtd) (node);
97 /* ----------------------------------------------------------------
100 * Scans the relation using the 'access method' indicated and
101 * returns the next qualifying tuple.
102 * The access method returns the next tuple and ExecScan() is
103 * responsible for checking the tuple returned against the qual-clause.
105 * A 'recheck method' must also be provided that can check an
106 * arbitrary tuple of the relation against any qual conditions
107 * that are implemented internal to the access method.
110 * -- the "cursor" maintained by the AMI is positioned at the tuple
111 * returned previously.
114 * -- the relation indicated is opened for scanning so that the
115 * "cursor" is positioned before the first qualifying tuple.
116 * ----------------------------------------------------------------
119 ExecScan(ScanState *node,
120 ExecScanAccessMtd accessMtd, /* function returning a tuple */
121 ExecScanRecheckMtd recheckMtd)
123 ExprContext *econtext;
125 ProjectionInfo *projInfo;
128 * Fetch data from node
130 qual = node->ps.qual;
131 projInfo = node->ps.ps_ProjInfo;
132 econtext = node->ps.ps_ExprContext;
134 /* interrupt checks are in ExecScanFetch */
137 * If we have neither a qual to check nor a projection to do, just skip
138 * all the overhead and return the raw scan tuple.
140 if (!qual && !projInfo)
142 ResetExprContext(econtext);
143 return ExecScanFetch(node, accessMtd, recheckMtd);
147 * Reset per-tuple memory context to free any expression evaluation
148 * storage allocated in the previous tuple cycle.
150 ResetExprContext(econtext);
153 * get a tuple from the access method. Loop until we obtain a tuple that
154 * passes the qualification.
158 TupleTableSlot *slot;
160 slot = ExecScanFetch(node, accessMtd, recheckMtd);
163 * if the slot returned by the accessMtd contains NULL, then it means
164 * there is nothing more to scan so we just return an empty slot,
165 * being careful to use the projection result slot so it has correct
171 return ExecClearTuple(projInfo->pi_state.resultslot);
177 * place the current tuple into the expr context
179 econtext->ecxt_scantuple = slot;
182 * check that the current tuple satisfies the qual-clause
184 * check for non-null qual here to avoid a function call to ExecQual()
185 * when the qual is null ... saves only a few cycles, but they add up
188 if (qual == NULL || ExecQual(qual, econtext))
191 * Found a satisfactory scan tuple.
196 * Form a projection tuple, store it in the result tuple slot
199 return ExecProject(projInfo);
204 * Here, we aren't projecting, so just return scan tuple.
210 InstrCountFiltered1(node, 1);
213 * Tuple fails qual, so free per-tuple memory and try again.
215 ResetExprContext(econtext);
220 * ExecAssignScanProjectionInfo
221 * Set up projection info for a scan node, if necessary.
223 * We can avoid a projection step if the requested tlist exactly matches
224 * the underlying tuple type. If so, we just set ps_ProjInfo to NULL.
225 * Note that this case occurs not only for simple "SELECT * FROM ...", but
226 * also in most cases where there are joins or other processing nodes above
227 * the scan node, because the planner will preferentially generate a matching
230 * The scan slot's descriptor must have been set already.
233 ExecAssignScanProjectionInfo(ScanState *node)
235 Scan *scan = (Scan *) node->ps.plan;
236 TupleDesc tupdesc = node->ss_ScanTupleSlot->tts_tupleDescriptor;
238 ExecConditionalAssignProjectionInfo(&node->ps, tupdesc, scan->scanrelid);
242 * ExecAssignScanProjectionInfoWithVarno
243 * As above, but caller can specify varno expected in Vars in the tlist.
246 ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
248 TupleDesc tupdesc = node->ss_ScanTupleSlot->tts_tupleDescriptor;
250 ExecConditionalAssignProjectionInfo(&node->ps, tupdesc, varno);
256 * This must be called within the ReScan function of any plan node type
257 * that uses ExecScan().
260 ExecScanReScan(ScanState *node)
262 EState *estate = node->ps.state;
265 * We must clear the scan tuple so that observers (e.g., execCurrent.c)
266 * can tell that this plan node is not positioned on a tuple.
268 ExecClearTuple(node->ss_ScanTupleSlot);
270 /* Rescan EvalPlanQual tuple if we're inside an EvalPlanQual recheck */
271 if (estate->es_epqScanDone != NULL)
273 Index scanrelid = ((Scan *) node->ps.plan)->scanrelid;
276 estate->es_epqScanDone[scanrelid - 1] = false;
283 * If an FDW or custom scan provider has replaced the join with a
284 * scan, there are multiple RTIs; reset the epqScanDone flag for
287 if (IsA(node->ps.plan, ForeignScan))
288 relids = ((ForeignScan *) node->ps.plan)->fs_relids;
289 else if (IsA(node->ps.plan, CustomScan))
290 relids = ((CustomScan *) node->ps.plan)->custom_relids;
292 elog(ERROR, "unexpected scan node: %d",
293 (int) nodeTag(node->ps.plan));
295 while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
298 estate->es_epqScanDone[rtindex - 1] = false;