-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathexecScan.h
More file actions
295 lines (259 loc) · 8.32 KB
/
Copy pathexecScan.h
File metadata and controls
295 lines (259 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*-------------------------------------------------------------------------
* execScan.h
* Inline-able support functions for Scan nodes
*
* Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/include/executor/execScan.h
*-------------------------------------------------------------------------
*/
#ifndef EXECSCAN_H
#define EXECSCAN_H
#include "miscadmin.h"
#include "executor/executor.h"
#include "nodes/execnodes.h"
/*
* ExecScanFetch -- check interrupts & fetch next potential tuple
*
* This routine substitutes a test tuple if inside an EvalPlanQual recheck.
* Otherwise, it simply executes the access method's next-tuple routine.
*
* The pg_attribute_always_inline attribute allows the compiler to inline
* this function into its caller. When EPQState is NULL, the EvalPlanQual
* logic is completely eliminated at compile time, avoiding unnecessary
* run-time checks and code for cases where EPQ is not required.
*/
static pg_attribute_always_inline TupleTableSlot *
ExecScanFetch(ScanState *node,
EPQState *epqstate,
ExecScanAccessMtd accessMtd,
ExecScanRecheckMtd recheckMtd)
{
CHECK_FOR_INTERRUPTS();
if (epqstate != NULL)
{
/*
* We are inside an EvalPlanQual recheck. Return the test tuple if
* one is available, after rechecking any access-method-specific
* conditions.
*/
Index scanrelid = ((Scan *) node->ps.plan)->scanrelid;
if (scanrelid == 0)
{
/*
* This is a ForeignScan or CustomScan which has pushed down a
* join to the remote side. If it is a descendant node in the EPQ
* recheck plan tree, run the recheck method function. Otherwise,
* run the access method function below.
*/
if (bms_is_member(epqstate->epqParam, node->ps.plan->extParam))
{
/*
* The recheck method is responsible not only for rechecking
* the scan/join quals but also for storing the correct tuple
* in the slot.
*/
TupleTableSlot *slot = node->ss_ScanTupleSlot;
if (!(*recheckMtd) (node, slot))
ExecClearTuple(slot); /* would not be returned by scan */
return slot;
}
}
else if (epqstate->relsubs_done[scanrelid - 1])
{
/*
* Return empty slot, as either there is no EPQ tuple for this rel
* or we already returned it.
*/
TupleTableSlot *slot = node->ss_ScanTupleSlot;
return ExecClearTuple(slot);
}
else if (epqstate->relsubs_slot[scanrelid - 1] != NULL)
{
/*
* Return replacement tuple provided by the EPQ caller.
*/
TupleTableSlot *slot = epqstate->relsubs_slot[scanrelid - 1];
Assert(epqstate->relsubs_rowmark[scanrelid - 1] == NULL);
/* Mark to remember that we shouldn't return it again */
epqstate->relsubs_done[scanrelid - 1] = true;
/* Return empty slot if we haven't got a test tuple */
if (TupIsNull(slot))
return NULL;
/* Check if it meets the access-method conditions */
if (!(*recheckMtd) (node, slot))
return ExecClearTuple(slot); /* would not be returned by
* scan */
return slot;
}
else if (epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
{
/*
* Fetch and return replacement tuple using a non-locking rowmark.
*/
TupleTableSlot *slot = node->ss_ScanTupleSlot;
/* Mark to remember that we shouldn't return more */
epqstate->relsubs_done[scanrelid - 1] = true;
if (!EvalPlanQualFetchRowMark(epqstate, scanrelid, slot))
return NULL;
/* Return empty slot if we haven't got a test tuple */
if (TupIsNull(slot))
return NULL;
/* Check if it meets the access-method conditions */
if (!(*recheckMtd) (node, slot))
return ExecClearTuple(slot); /* would not be returned by
* scan */
return slot;
}
}
/*
* Run the node-type-specific access method function to get the next tuple
*/
return (*accessMtd) (node);
}
/* ----------------------------------------------------------------
* ExecScanExtended
* Scans the relation using the specified 'access method' and returns the
* next tuple. Optionally checks the tuple against 'qual' and applies
* 'projInfo' if provided.
*
* The 'recheck method' validates an arbitrary tuple of the relation against
* conditions enforced by the access method.
*
* This function is an alternative to ExecScan, used when callers may omit
* 'qual' or 'projInfo'. The pg_attribute_always_inline attribute allows the
* compiler to eliminate non-relevant branches at compile time, avoiding
* run-time checks in those cases.
*
* Conditions:
* -- The AMI "cursor" is positioned at the previously returned tuple.
*
* Initial States:
* -- The relation is opened for scanning, with the "cursor"
* positioned before the first qualifying tuple.
* ----------------------------------------------------------------
*/
static pg_attribute_always_inline TupleTableSlot *
ExecScanExtended(ScanState *node,
ExecScanAccessMtd accessMtd, /* function returning a tuple */
ExecScanRecheckMtd recheckMtd,
EPQState *epqstate,
ExprState *qual,
ProjectionInfo *projInfo)
{
ExprContext *econtext = node->ps.ps_ExprContext;
/* interrupt checks are in ExecScanFetch */
/*
* If we have neither a qual to check nor a projection to do, just skip
* all the overhead and return the raw scan tuple.
*/
if (!qual && !projInfo)
{
ResetExprContext(econtext);
return ExecScanFetch(node, epqstate, accessMtd, recheckMtd);
}
/*
* Reset per-tuple memory context to free any expression evaluation
* storage allocated in the previous tuple cycle.
*/
ResetExprContext(econtext);
/*
* get a tuple from the access method. Loop until we obtain a tuple that
* passes the qualification.
*/
for (;;)
{
TupleTableSlot *slot;
slot = ExecScanFetch(node, epqstate, accessMtd, recheckMtd);
/*
* if the slot returned by the accessMtd contains NULL, then it means
* there is nothing more to scan so we just return an empty slot,
* being careful to use the projection result slot so it has correct
* tupleDesc.
*/
if (TupIsNull(slot))
{
if (projInfo)
return ExecClearTuple(projInfo->pi_state.resultslot);
else
return slot;
}
/*
* place the current tuple into the expr context
*/
econtext->ecxt_scantuple = slot;
/*
* For Oracle ROWNUM compatibility: pre-increment ROWNUM before the
* qual check so that ROWNUM conditions (like ROWNUM <= 5) see the
* correct value. If the row fails the qual, we'll revert the
* increment. This matches Oracle's behavior where ROWNUM is assigned
* to each candidate row before checking the WHERE clause.
*/
if (node->ps.state)
node->ps.state->es_rownum++;
/*
* check that the current tuple satisfies the qual-clause
*
* check for non-null qual here to avoid a function call to ExecQual()
* when the qual is null ... saves only a few cycles, but they add up
* ...
*/
if (qual == NULL || ExecQual(qual, econtext))
{
/*
* Found a satisfactory scan tuple.
* The ROWNUM increment is already done.
*/
if (projInfo)
{
TupleTableSlot *result;
/*
* Form a projection tuple, store it in the result tuple slot
* and return it.
*/
result = ExecProject(projInfo);
/*
* If the projection contains ROWNUM expressions, materialize
* the virtual tuple to preserve the ROWNUM values as constants.
* This prevents re-evaluation when the tuple is read by outer
* queries (e.g., in subqueries with ORDER BY).
*
* Oracle materializes ROWNUM values in SELECT lists, so when
* a subquery projects ROWNUM, the value must be captured NOW
* and not re-evaluated later in different contexts.
*/
if (projInfo->pi_needsMaterialization)
{
ExecMaterializeSlot(result);
}
return result;
}
else
{
/*
* Here, we aren't projecting, so just return scan tuple.
*/
return slot;
}
}
else
{
/*
* Row failed qual check. Revert the ROWNUM increment so that
* only rows that pass quals consume ROWNUM values. This matches
* Oracle's behavior where ROWNUM is only assigned to rows that
* are actually "selected".
*/
if (node->ps.state)
node->ps.state->es_rownum--;
InstrCountFiltered1(node, 1);
}
/*
* Tuple fails qual, so free per-tuple memory and try again.
*/
ResetExprContext(econtext);
}
}
#endif /* EXECSCAN_H */