Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * nodeTidrangescan.c
4 : * Routines to support TID range scans of relations
5 : *
6 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/executor/nodeTidrangescan.c
12 : *
13 : *-------------------------------------------------------------------------
14 : */
15 : #include "postgres.h"
16 :
17 : #include "access/relscan.h"
18 : #include "access/sysattr.h"
19 : #include "access/tableam.h"
20 : #include "catalog/pg_operator.h"
21 : #include "executor/executor.h"
22 : #include "executor/nodeTidrangescan.h"
23 : #include "nodes/nodeFuncs.h"
24 : #include "utils/rel.h"
25 :
26 :
27 : /*
28 : * It's sufficient to check varattno to identify the CTID variable, as any
29 : * Var in the relation scan qual must be for our table. (Even if it's a
30 : * parameterized scan referencing some other table's CTID, the other table's
31 : * Var would have become a Param by the time it gets here.)
32 : */
33 : #define IsCTIDVar(node) \
34 : ((node) != NULL && \
35 : IsA((node), Var) && \
36 : ((Var *) (node))->varattno == SelfItemPointerAttributeNumber)
37 :
38 : typedef enum
39 : {
40 : TIDEXPR_UPPER_BOUND,
41 : TIDEXPR_LOWER_BOUND,
42 : } TidExprType;
43 :
44 : /* Upper or lower range bound for scan */
45 : typedef struct TidOpExpr
46 : {
47 : TidExprType exprtype; /* type of op; lower or upper */
48 : ExprState *exprstate; /* ExprState for a TID-yielding subexpr */
49 : bool inclusive; /* whether op is inclusive */
50 : } TidOpExpr;
51 :
52 : /*
53 : * For the given 'expr', build and return an appropriate TidOpExpr taking into
54 : * account the expr's operator and operand order.
55 : */
56 : static TidOpExpr *
57 1970 : MakeTidOpExpr(OpExpr *expr, TidRangeScanState *tidstate)
58 : {
59 1970 : Node *arg1 = get_leftop((Expr *) expr);
60 1970 : Node *arg2 = get_rightop((Expr *) expr);
61 1970 : ExprState *exprstate = NULL;
62 1970 : bool invert = false;
63 : TidOpExpr *tidopexpr;
64 :
65 1970 : if (IsCTIDVar(arg1))
66 1934 : exprstate = ExecInitExpr((Expr *) arg2, &tidstate->ss.ps);
67 36 : else if (IsCTIDVar(arg2))
68 : {
69 36 : exprstate = ExecInitExpr((Expr *) arg1, &tidstate->ss.ps);
70 36 : invert = true;
71 : }
72 : else
73 0 : elog(ERROR, "could not identify CTID variable");
74 :
75 1970 : tidopexpr = (TidOpExpr *) palloc(sizeof(TidOpExpr));
76 1970 : tidopexpr->inclusive = false; /* for now */
77 :
78 1970 : switch (expr->opno)
79 : {
80 24 : case TIDLessEqOperator:
81 24 : tidopexpr->inclusive = true;
82 : /* fall through */
83 118 : case TIDLessOperator:
84 118 : tidopexpr->exprtype = invert ? TIDEXPR_LOWER_BOUND : TIDEXPR_UPPER_BOUND;
85 118 : break;
86 1788 : case TIDGreaterEqOperator:
87 1788 : tidopexpr->inclusive = true;
88 : /* fall through */
89 1852 : case TIDGreaterOperator:
90 1852 : tidopexpr->exprtype = invert ? TIDEXPR_UPPER_BOUND : TIDEXPR_LOWER_BOUND;
91 1852 : break;
92 0 : default:
93 0 : elog(ERROR, "could not identify CTID operator");
94 : }
95 :
96 1970 : tidopexpr->exprstate = exprstate;
97 :
98 1970 : return tidopexpr;
99 : }
100 :
101 : /*
102 : * Extract the qual subexpressions that yield TIDs to search for,
103 : * and compile them into ExprStates if they're ordinary expressions.
104 : */
105 : static void
106 1940 : TidExprListCreate(TidRangeScanState *tidrangestate)
107 : {
108 1940 : TidRangeScan *node = (TidRangeScan *) tidrangestate->ss.ps.plan;
109 1940 : List *tidexprs = NIL;
110 : ListCell *l;
111 :
112 3910 : foreach(l, node->tidrangequals)
113 : {
114 1970 : OpExpr *opexpr = lfirst(l);
115 : TidOpExpr *tidopexpr;
116 :
117 1970 : if (!IsA(opexpr, OpExpr))
118 0 : elog(ERROR, "could not identify CTID expression");
119 :
120 1970 : tidopexpr = MakeTidOpExpr(opexpr, tidrangestate);
121 1970 : tidexprs = lappend(tidexprs, tidopexpr);
122 : }
123 :
124 1940 : tidrangestate->trss_tidexprs = tidexprs;
125 1940 : }
126 :
127 : /* ----------------------------------------------------------------
128 : * TidRangeEval
129 : *
130 : * Compute and set node's block and offset range to scan by evaluating
131 : * node->trss_tidexprs. Returns false if we detect the range cannot
132 : * contain any tuples. Returns true if it's possible for the range to
133 : * contain tuples. We don't bother validating that trss_mintid is less
134 : * than or equal to trss_maxtid, as the scan_set_tidrange() table AM
135 : * function will handle that.
136 : * ----------------------------------------------------------------
137 : */
138 : static bool
139 1922 : TidRangeEval(TidRangeScanState *node)
140 : {
141 1922 : ExprContext *econtext = node->ss.ps.ps_ExprContext;
142 : ItemPointerData lowerBound;
143 : ItemPointerData upperBound;
144 : ListCell *l;
145 :
146 : /*
147 : * Set the upper and lower bounds to the absolute limits of the range of
148 : * the ItemPointer type. Below we'll try to narrow this range on either
149 : * side by looking at the TidOpExprs.
150 : */
151 1922 : ItemPointerSet(&lowerBound, 0, 0);
152 1922 : ItemPointerSet(&upperBound, InvalidBlockNumber, PG_UINT16_MAX);
153 :
154 3856 : foreach(l, node->trss_tidexprs)
155 : {
156 1940 : TidOpExpr *tidopexpr = (TidOpExpr *) lfirst(l);
157 : ItemPointer itemptr;
158 : bool isNull;
159 :
160 : /* Evaluate this bound. */
161 : itemptr = (ItemPointer)
162 1940 : DatumGetPointer(ExecEvalExprSwitchContext(tidopexpr->exprstate,
163 : econtext,
164 : &isNull));
165 :
166 : /* If the bound is NULL, *nothing* matches the qual. */
167 1940 : if (isNull)
168 6 : return false;
169 :
170 1934 : if (tidopexpr->exprtype == TIDEXPR_LOWER_BOUND)
171 : {
172 : ItemPointerData lb;
173 :
174 1792 : ItemPointerCopy(itemptr, &lb);
175 :
176 : /*
177 : * Normalize non-inclusive ranges to become inclusive. The
178 : * resulting ItemPointer here may not be a valid item pointer.
179 : */
180 1792 : if (!tidopexpr->inclusive)
181 46 : ItemPointerInc(&lb);
182 :
183 : /* Check if we can narrow the range using this qual */
184 1792 : if (ItemPointerCompare(&lb, &lowerBound) > 0)
185 1792 : ItemPointerCopy(&lb, &lowerBound);
186 : }
187 :
188 142 : else if (tidopexpr->exprtype == TIDEXPR_UPPER_BOUND)
189 : {
190 : ItemPointerData ub;
191 :
192 142 : ItemPointerCopy(itemptr, &ub);
193 :
194 : /*
195 : * Normalize non-inclusive ranges to become inclusive. The
196 : * resulting ItemPointer here may not be a valid item pointer.
197 : */
198 142 : if (!tidopexpr->inclusive)
199 64 : ItemPointerDec(&ub);
200 :
201 : /* Check if we can narrow the range using this qual */
202 142 : if (ItemPointerCompare(&ub, &upperBound) < 0)
203 142 : ItemPointerCopy(&ub, &upperBound);
204 : }
205 : }
206 :
207 1916 : ItemPointerCopy(&lowerBound, &node->trss_mintid);
208 1916 : ItemPointerCopy(&upperBound, &node->trss_maxtid);
209 :
210 1916 : return true;
211 : }
212 :
213 : /* ----------------------------------------------------------------
214 : * TidRangeNext
215 : *
216 : * Retrieve a tuple from the TidRangeScan node's currentRelation
217 : * using the TIDs in the TidRangeScanState information.
218 : *
219 : * ----------------------------------------------------------------
220 : */
221 : static TupleTableSlot *
222 8600 : TidRangeNext(TidRangeScanState *node)
223 : {
224 : TableScanDesc scandesc;
225 : EState *estate;
226 : ScanDirection direction;
227 : TupleTableSlot *slot;
228 :
229 : /*
230 : * extract necessary information from TID scan node
231 : */
232 8600 : scandesc = node->ss.ss_currentScanDesc;
233 8600 : estate = node->ss.ps.state;
234 8600 : slot = node->ss.ss_ScanTupleSlot;
235 8600 : direction = estate->es_direction;
236 :
237 8600 : if (!node->trss_inScan)
238 : {
239 : /* First time through, compute TID range to scan */
240 1922 : if (!TidRangeEval(node))
241 6 : return NULL;
242 :
243 1916 : if (scandesc == NULL)
244 : {
245 1850 : scandesc = table_beginscan_tidrange(node->ss.ss_currentRelation,
246 : estate->es_snapshot,
247 : &node->trss_mintid,
248 : &node->trss_maxtid);
249 1850 : node->ss.ss_currentScanDesc = scandesc;
250 : }
251 : else
252 : {
253 : /* rescan with the updated TID range */
254 66 : table_rescan_tidrange(scandesc, &node->trss_mintid,
255 : &node->trss_maxtid);
256 : }
257 :
258 1916 : node->trss_inScan = true;
259 : }
260 :
261 : /* Fetch the next tuple. */
262 8594 : if (!table_scan_getnextslot_tidrange(scandesc, direction, slot))
263 : {
264 166 : node->trss_inScan = false;
265 166 : ExecClearTuple(slot);
266 : }
267 :
268 8578 : return slot;
269 : }
270 :
271 : /*
272 : * TidRangeRecheck -- access method routine to recheck a tuple in EvalPlanQual
273 : */
274 : static bool
275 0 : TidRangeRecheck(TidRangeScanState *node, TupleTableSlot *slot)
276 : {
277 0 : return true;
278 : }
279 :
280 : /* ----------------------------------------------------------------
281 : * ExecTidRangeScan(node)
282 : *
283 : * Scans the relation using tids and returns the next qualifying tuple.
284 : * We call the ExecScan() routine and pass it the appropriate
285 : * access method functions.
286 : *
287 : * Conditions:
288 : * -- the "cursor" maintained by the AMI is positioned at the tuple
289 : * returned previously.
290 : *
291 : * Initial States:
292 : * -- the relation indicated is opened for TID range scanning.
293 : * ----------------------------------------------------------------
294 : */
295 : static TupleTableSlot *
296 8600 : ExecTidRangeScan(PlanState *pstate)
297 : {
298 8600 : TidRangeScanState *node = castNode(TidRangeScanState, pstate);
299 :
300 8600 : return ExecScan(&node->ss,
301 : (ExecScanAccessMtd) TidRangeNext,
302 : (ExecScanRecheckMtd) TidRangeRecheck);
303 : }
304 :
305 : /* ----------------------------------------------------------------
306 : * ExecReScanTidRangeScan(node)
307 : * ----------------------------------------------------------------
308 : */
309 : void
310 66 : ExecReScanTidRangeScan(TidRangeScanState *node)
311 : {
312 : /* mark scan as not in progress, and tid range list as not computed yet */
313 66 : node->trss_inScan = false;
314 :
315 : /*
316 : * We must wait until TidRangeNext before calling table_rescan_tidrange.
317 : */
318 66 : ExecScanReScan(&node->ss);
319 66 : }
320 :
321 : /* ----------------------------------------------------------------
322 : * ExecEndTidRangeScan
323 : *
324 : * Releases any storage allocated through C routines.
325 : * Returns nothing.
326 : * ----------------------------------------------------------------
327 : */
328 : void
329 202 : ExecEndTidRangeScan(TidRangeScanState *node)
330 : {
331 202 : TableScanDesc scan = node->ss.ss_currentScanDesc;
332 :
333 202 : if (scan != NULL)
334 112 : table_endscan(scan);
335 202 : }
336 :
337 : /* ----------------------------------------------------------------
338 : * ExecInitTidRangeScan
339 : *
340 : * Initializes the tid range scan's state information, creates
341 : * scan keys, and opens the scan relation.
342 : *
343 : * Parameters:
344 : * node: TidRangeScan node produced by the planner.
345 : * estate: the execution state initialized in InitPlan.
346 : * ----------------------------------------------------------------
347 : */
348 : TidRangeScanState *
349 1940 : ExecInitTidRangeScan(TidRangeScan *node, EState *estate, int eflags)
350 : {
351 : TidRangeScanState *tidrangestate;
352 : Relation currentRelation;
353 :
354 : /*
355 : * create state structure
356 : */
357 1940 : tidrangestate = makeNode(TidRangeScanState);
358 1940 : tidrangestate->ss.ps.plan = (Plan *) node;
359 1940 : tidrangestate->ss.ps.state = estate;
360 1940 : tidrangestate->ss.ps.ExecProcNode = ExecTidRangeScan;
361 :
362 : /*
363 : * Miscellaneous initialization
364 : *
365 : * create expression context for node
366 : */
367 1940 : ExecAssignExprContext(estate, &tidrangestate->ss.ps);
368 :
369 : /*
370 : * mark scan as not in progress, and TID range as not computed yet
371 : */
372 1940 : tidrangestate->trss_inScan = false;
373 :
374 : /*
375 : * open the scan relation
376 : */
377 1940 : currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
378 :
379 1940 : tidrangestate->ss.ss_currentRelation = currentRelation;
380 1940 : tidrangestate->ss.ss_currentScanDesc = NULL; /* no table scan here */
381 :
382 : /*
383 : * get the scan type from the relation descriptor.
384 : */
385 1940 : ExecInitScanTupleSlot(estate, &tidrangestate->ss,
386 : RelationGetDescr(currentRelation),
387 : table_slot_callbacks(currentRelation));
388 :
389 : /*
390 : * Initialize result type and projection.
391 : */
392 1940 : ExecInitResultTypeTL(&tidrangestate->ss.ps);
393 1940 : ExecAssignScanProjectionInfo(&tidrangestate->ss);
394 :
395 : /*
396 : * initialize child expressions
397 : */
398 1940 : tidrangestate->ss.ps.qual =
399 1940 : ExecInitQual(node->scan.plan.qual, (PlanState *) tidrangestate);
400 :
401 1940 : TidExprListCreate(tidrangestate);
402 :
403 : /*
404 : * all done.
405 : */
406 1940 : return tidrangestate;
407 : }
|