PostgreSQL Source Code git master
nodeTidrangescan.c
Go to the documentation of this file.
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"
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
38typedef enum
39{
43
44/* Upper or lower range bound for scan */
45typedef 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 */
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 */
56static TidOpExpr *
58{
59 Node *arg1 = get_leftop((Expr *) expr);
60 Node *arg2 = get_rightop((Expr *) expr);
61 ExprState *exprstate = NULL;
62 bool invert = false;
63 TidOpExpr *tidopexpr;
64
65 if (IsCTIDVar(arg1))
66 exprstate = ExecInitExpr((Expr *) arg2, &tidstate->ss.ps);
67 else if (IsCTIDVar(arg2))
68 {
69 exprstate = ExecInitExpr((Expr *) arg1, &tidstate->ss.ps);
70 invert = true;
71 }
72 else
73 elog(ERROR, "could not identify CTID variable");
74
75 tidopexpr = (TidOpExpr *) palloc(sizeof(TidOpExpr));
76 tidopexpr->inclusive = false; /* for now */
77
78 switch (expr->opno)
79 {
80 case TIDLessEqOperator:
81 tidopexpr->inclusive = true;
82 /* fall through */
83 case TIDLessOperator:
84 tidopexpr->exprtype = invert ? TIDEXPR_LOWER_BOUND : TIDEXPR_UPPER_BOUND;
85 break;
86 case TIDGreaterEqOperator:
87 tidopexpr->inclusive = true;
88 /* fall through */
89 case TIDGreaterOperator:
90 tidopexpr->exprtype = invert ? TIDEXPR_UPPER_BOUND : TIDEXPR_LOWER_BOUND;
91 break;
92 default:
93 elog(ERROR, "could not identify CTID operator");
94 }
95
96 tidopexpr->exprstate = exprstate;
97
98 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 */
105static void
107{
108 TidRangeScan *node = (TidRangeScan *) tidrangestate->ss.ps.plan;
109 List *tidexprs = NIL;
110 ListCell *l;
111
112 foreach(l, node->tidrangequals)
113 {
114 OpExpr *opexpr = lfirst(l);
115 TidOpExpr *tidopexpr;
116
117 if (!IsA(opexpr, OpExpr))
118 elog(ERROR, "could not identify CTID expression");
119
120 tidopexpr = MakeTidOpExpr(opexpr, tidrangestate);
121 tidexprs = lappend(tidexprs, tidopexpr);
122 }
123
124 tidrangestate->trss_tidexprs = tidexprs;
125}
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 */
138static bool
140{
141 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 ItemPointerSet(&lowerBound, 0, 0);
153
154 foreach(l, node->trss_tidexprs)
155 {
156 TidOpExpr *tidopexpr = (TidOpExpr *) lfirst(l);
157 ItemPointer itemptr;
158 bool isNull;
159
160 /* Evaluate this bound. */
161 itemptr = (ItemPointer)
163 econtext,
164 &isNull));
165
166 /* If the bound is NULL, *nothing* matches the qual. */
167 if (isNull)
168 return false;
169
170 if (tidopexpr->exprtype == TIDEXPR_LOWER_BOUND)
171 {
173
174 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 if (!tidopexpr->inclusive)
181 ItemPointerInc(&lb);
182
183 /* Check if we can narrow the range using this qual */
184 if (ItemPointerCompare(&lb, &lowerBound) > 0)
185 ItemPointerCopy(&lb, &lowerBound);
186 }
187
188 else if (tidopexpr->exprtype == TIDEXPR_UPPER_BOUND)
189 {
191
192 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 if (!tidopexpr->inclusive)
199 ItemPointerDec(&ub);
200
201 /* Check if we can narrow the range using this qual */
202 if (ItemPointerCompare(&ub, &upperBound) < 0)
203 ItemPointerCopy(&ub, &upperBound);
204 }
205 }
206
207 ItemPointerCopy(&lowerBound, &node->trss_mintid);
208 ItemPointerCopy(&upperBound, &node->trss_maxtid);
209
210 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 */
221static TupleTableSlot *
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 scandesc = node->ss.ss_currentScanDesc;
233 estate = node->ss.ps.state;
234 slot = node->ss.ss_ScanTupleSlot;
235 direction = estate->es_direction;
236
237 if (!node->trss_inScan)
238 {
239 /* First time through, compute TID range to scan */
240 if (!TidRangeEval(node))
241 return NULL;
242
243 if (scandesc == NULL)
244 {
246 estate->es_snapshot,
247 &node->trss_mintid,
248 &node->trss_maxtid);
249 node->ss.ss_currentScanDesc = scandesc;
250 }
251 else
252 {
253 /* rescan with the updated TID range */
254 table_rescan_tidrange(scandesc, &node->trss_mintid,
255 &node->trss_maxtid);
256 }
257
258 node->trss_inScan = true;
259 }
260
261 /* Fetch the next tuple. */
262 if (!table_scan_getnextslot_tidrange(scandesc, direction, slot))
263 {
264 node->trss_inScan = false;
265 ExecClearTuple(slot);
266 }
267
268 return slot;
269}
270
271/*
272 * TidRangeRecheck -- access method routine to recheck a tuple in EvalPlanQual
273 */
274static bool
276{
277 if (!TidRangeEval(node))
278 return false;
279
281
282 /* Recheck the ctid is still within range */
283 if (ItemPointerCompare(&slot->tts_tid, &node->trss_mintid) < 0 ||
284 ItemPointerCompare(&slot->tts_tid, &node->trss_maxtid) > 0)
285 return false;
286
287 return true;
288}
289
290/* ----------------------------------------------------------------
291 * ExecTidRangeScan(node)
292 *
293 * Scans the relation using tids and returns the next qualifying tuple.
294 * We call the ExecScan() routine and pass it the appropriate
295 * access method functions.
296 *
297 * Conditions:
298 * -- the "cursor" maintained by the AMI is positioned at the tuple
299 * returned previously.
300 *
301 * Initial States:
302 * -- the relation indicated is opened for TID range scanning.
303 * ----------------------------------------------------------------
304 */
305static TupleTableSlot *
307{
309
310 return ExecScan(&node->ss,
313}
314
315/* ----------------------------------------------------------------
316 * ExecReScanTidRangeScan(node)
317 * ----------------------------------------------------------------
318 */
319void
321{
322 /* mark scan as not in progress, and tid range list as not computed yet */
323 node->trss_inScan = false;
324
325 /*
326 * We must wait until TidRangeNext before calling table_rescan_tidrange.
327 */
328 ExecScanReScan(&node->ss);
329}
330
331/* ----------------------------------------------------------------
332 * ExecEndTidRangeScan
333 *
334 * Releases any storage allocated through C routines.
335 * Returns nothing.
336 * ----------------------------------------------------------------
337 */
338void
340{
342
343 if (scan != NULL)
344 table_endscan(scan);
345}
346
347/* ----------------------------------------------------------------
348 * ExecInitTidRangeScan
349 *
350 * Initializes the tid range scan's state information, creates
351 * scan keys, and opens the scan relation.
352 *
353 * Parameters:
354 * node: TidRangeScan node produced by the planner.
355 * estate: the execution state initialized in InitPlan.
356 * ----------------------------------------------------------------
357 */
359ExecInitTidRangeScan(TidRangeScan *node, EState *estate, int eflags)
360{
361 TidRangeScanState *tidrangestate;
362 Relation currentRelation;
363
364 /*
365 * create state structure
366 */
367 tidrangestate = makeNode(TidRangeScanState);
368 tidrangestate->ss.ps.plan = (Plan *) node;
369 tidrangestate->ss.ps.state = estate;
370 tidrangestate->ss.ps.ExecProcNode = ExecTidRangeScan;
371
372 /*
373 * Miscellaneous initialization
374 *
375 * create expression context for node
376 */
377 ExecAssignExprContext(estate, &tidrangestate->ss.ps);
378
379 /*
380 * mark scan as not in progress, and TID range as not computed yet
381 */
382 tidrangestate->trss_inScan = false;
383
384 /*
385 * open the scan relation
386 */
387 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
388
389 tidrangestate->ss.ss_currentRelation = currentRelation;
390 tidrangestate->ss.ss_currentScanDesc = NULL; /* no table scan here */
391
392 /*
393 * get the scan type from the relation descriptor.
394 */
395 ExecInitScanTupleSlot(estate, &tidrangestate->ss,
396 RelationGetDescr(currentRelation),
397 table_slot_callbacks(currentRelation));
398
399 /*
400 * Initialize result type and projection.
401 */
402 ExecInitResultTypeTL(&tidrangestate->ss.ps);
403 ExecAssignScanProjectionInfo(&tidrangestate->ss);
404
405 /*
406 * initialize child expressions
407 */
408 tidrangestate->ss.ps.qual =
409 ExecInitQual(node->scan.plan.qual, (PlanState *) tidrangestate);
410
411 TidExprListCreate(tidrangestate);
412
413 /*
414 * all done.
415 */
416 return tidrangestate;
417}
418
419/* ----------------------------------------------------------------
420 * Parallel Scan Support
421 * ----------------------------------------------------------------
422 */
423
424/* ----------------------------------------------------------------
425 * ExecTidRangeScanEstimate
426 *
427 * Compute the amount of space we'll need in the parallel
428 * query DSM, and inform pcxt->estimator about our needs.
429 * ----------------------------------------------------------------
430 */
431void
433{
434 EState *estate = node->ss.ps.state;
435
436 node->trss_pscanlen =
438 estate->es_snapshot);
441}
442
443/* ----------------------------------------------------------------
444 * ExecTidRangeScanInitializeDSM
445 *
446 * Set up a parallel TID range scan descriptor.
447 * ----------------------------------------------------------------
448 */
449void
451{
452 EState *estate = node->ss.ps.state;
454
455 pscan = shm_toc_allocate(pcxt->toc, node->trss_pscanlen);
457 pscan,
458 estate->es_snapshot);
459 shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, pscan);
460 node->ss.ss_currentScanDesc =
462 pscan);
463}
464
465/* ----------------------------------------------------------------
466 * ExecTidRangeScanReInitializeDSM
467 *
468 * Reset shared state before beginning a fresh scan.
469 * ----------------------------------------------------------------
470 */
471void
473 ParallelContext *pcxt)
474{
476
477 pscan = node->ss.ss_currentScanDesc->rs_parallel;
479}
480
481/* ----------------------------------------------------------------
482 * ExecTidRangeScanInitializeWorker
483 *
484 * Copy relevant information from TOC into planstate.
485 * ----------------------------------------------------------------
486 */
487void
490{
492
493 pscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
494 node->ss.ss_currentScanDesc =
496 pscan);
497}
#define InvalidBlockNumber
Definition: block.h:33
#define PG_UINT16_MAX
Definition: c.h:597
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:143
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:229
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)
Definition: execTuples.c:2000
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1944
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:485
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:742
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:580
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:579
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:436
Assert(PointerIsAligned(start, uint64))
void ItemPointerDec(ItemPointer pointer)
Definition: itemptr.c:114
int32 ItemPointerCompare(const ItemPointerData *arg1, const ItemPointerData *arg2)
Definition: itemptr.c:51
void ItemPointerInc(ItemPointer pointer)
Definition: itemptr.c:84
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135
ItemPointerData * ItemPointer
Definition: itemptr.h:49
static void ItemPointerCopy(const ItemPointerData *fromPointer, ItemPointerData *toPointer)
Definition: itemptr.h:172
static bool ItemPointerIsValid(const ItemPointerData *pointer)
Definition: itemptr.h:83
List * lappend(List *list, void *datum)
Definition: list.c:339
void * palloc(Size size)
Definition: mcxt.c:1365
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:95
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:83
static void TidExprListCreate(TidRangeScanState *tidrangestate)
void ExecTidRangeScanEstimate(TidRangeScanState *node, ParallelContext *pcxt)
void ExecReScanTidRangeScan(TidRangeScanState *node)
void ExecEndTidRangeScan(TidRangeScanState *node)
static bool TidRangeEval(TidRangeScanState *node)
void ExecTidRangeScanInitializeWorker(TidRangeScanState *node, ParallelWorkerContext *pwcxt)
static TidOpExpr * MakeTidOpExpr(OpExpr *expr, TidRangeScanState *tidstate)
TidRangeScanState * ExecInitTidRangeScan(TidRangeScan *node, EState *estate, int eflags)
void ExecTidRangeScanInitializeDSM(TidRangeScanState *node, ParallelContext *pcxt)
void ExecTidRangeScanReInitializeDSM(TidRangeScanState *node, ParallelContext *pcxt)
static TupleTableSlot * TidRangeNext(TidRangeScanState *node)
#define IsCTIDVar(node)
struct TidOpExpr TidOpExpr
TidExprType
@ TIDEXPR_LOWER_BOUND
@ TIDEXPR_UPPER_BOUND
static TupleTableSlot * ExecTidRangeScan(PlanState *pstate)
static bool TidRangeRecheck(TidRangeScanState *node, TupleTableSlot *slot)
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define makeNode(_type_)
Definition: nodes.h:161
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
#define RelationGetDescr(relation)
Definition: rel.h:541
ScanDirection
Definition: sdir.h:25
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
ScanDirection es_direction
Definition: execnodes.h:659
Snapshot es_snapshot
Definition: execnodes.h:660
Definition: pg_list.h:54
Definition: nodes.h:135
Oid opno
Definition: primnodes.h:850
shm_toc_estimator estimator
Definition: parallel.h:41
shm_toc * toc
Definition: parallel.h:44
ExprState * qual
Definition: execnodes.h:1186
Plan * plan
Definition: execnodes.h:1165
EState * state
Definition: execnodes.h:1167
ExprContext * ps_ExprContext
Definition: execnodes.h:1204
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1171
int plan_node_id
Definition: plannodes.h:227
Relation ss_currentRelation
Definition: execnodes.h:1622
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1624
PlanState ps
Definition: execnodes.h:1621
struct TableScanDescData * ss_currentScanDesc
Definition: execnodes.h:1623
Index scanrelid
Definition: plannodes.h:523
struct ParallelTableScanDescData * rs_parallel
Definition: relscan.h:66
ExprState * exprstate
TidExprType exprtype
ItemPointerData trss_maxtid
Definition: execnodes.h:1941
List * trss_tidexprs
Definition: execnodes.h:1939
ItemPointerData trss_mintid
Definition: execnodes.h:1940
List * tidrangequals
Definition: plannodes.h:722
ItemPointerData tts_tid
Definition: tuptable.h:128
TableScanDesc table_beginscan_parallel_tidrange(Relation relation, ParallelTableScanDesc pscan)
Definition: tableam.c:192
Size table_parallelscan_estimate(Relation rel, Snapshot snapshot)
Definition: tableam.c:131
void table_parallelscan_initialize(Relation rel, ParallelTableScanDesc pscan, Snapshot snapshot)
Definition: tableam.c:146
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition: tableam.c:59
static void table_rescan_tidrange(TableScanDesc sscan, ItemPointer mintid, ItemPointer maxtid)
Definition: tableam.h:1070
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:985
static TableScanDesc table_beginscan_tidrange(Relation rel, Snapshot snapshot, ItemPointer mintid, ItemPointer maxtid)
Definition: tableam.h:1049
static bool table_scan_getnextslot_tidrange(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition: tableam.h:1086
static void table_parallelscan_reinitialize(Relation rel, ParallelTableScanDesc pscan)
Definition: tableam.h:1149
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:457