1 /*-------------------------------------------------------------------------
4 * Routines to support direct tid scans of relations
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/executor/nodeTidscan.c
13 *-------------------------------------------------------------------------
18 * ExecTidScan scans a relation using tids
19 * ExecInitTidScan creates and initializes state info.
20 * ExecReScanTidScan rescans the tid relation.
21 * ExecEndTidScan releases all storage.
25 #include "access/sysattr.h"
26 #include "catalog/pg_type.h"
27 #include "executor/execdebug.h"
28 #include "executor/nodeTidscan.h"
29 #include "optimizer/clauses.h"
30 #include "storage/bufmgr.h"
31 #include "utils/array.h"
32 #include "utils/rel.h"
35 #define IsCTIDVar(node) \
38 ((Var *) (node))->varattno == SelfItemPointerAttributeNumber && \
39 ((Var *) (node))->varlevelsup == 0)
41 /* one element in tss_tidexprs */
42 typedef struct TidExpr
44 ExprState *exprstate; /* ExprState for a TID-yielding subexpr */
45 bool isarray; /* if true, it yields tid[] not just tid */
46 CurrentOfExpr *cexpr; /* alternatively, we can have CURRENT OF */
49 static void TidExprListCreate(TidScanState *tidstate);
50 static void TidListEval(TidScanState *tidstate);
51 static int itemptr_comparator(const void *a, const void *b);
52 static TupleTableSlot *TidNext(TidScanState *node);
56 * Extract the qual subexpressions that yield TIDs to search for,
57 * and compile them into ExprStates if they're ordinary expressions.
59 * CURRENT OF is a special case that we can't compile usefully;
60 * just drop it into the TidExpr list as-is.
63 TidExprListCreate(TidScanState *tidstate)
65 TidScan *node = (TidScan *) tidstate->ss.ps.plan;
68 tidstate->tss_tidexprs = NIL;
69 tidstate->tss_isCurrentOf = false;
71 foreach(l, node->tidquals)
73 Expr *expr = (Expr *) lfirst(l);
74 TidExpr *tidexpr = (TidExpr *) palloc0(sizeof(TidExpr));
76 if (is_opclause(expr))
81 arg1 = get_leftop(expr);
82 arg2 = get_rightop(expr);
84 tidexpr->exprstate = ExecInitExpr((Expr *) arg2,
86 else if (IsCTIDVar(arg2))
87 tidexpr->exprstate = ExecInitExpr((Expr *) arg1,
90 elog(ERROR, "could not identify CTID variable");
91 tidexpr->isarray = false;
93 else if (expr && IsA(expr, ScalarArrayOpExpr))
95 ScalarArrayOpExpr *saex = (ScalarArrayOpExpr *) expr;
97 Assert(IsCTIDVar(linitial(saex->args)));
98 tidexpr->exprstate = ExecInitExpr(lsecond(saex->args),
100 tidexpr->isarray = true;
102 else if (expr && IsA(expr, CurrentOfExpr))
104 CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
106 tidexpr->cexpr = cexpr;
107 tidstate->tss_isCurrentOf = true;
110 elog(ERROR, "could not identify CTID expression");
112 tidstate->tss_tidexprs = lappend(tidstate->tss_tidexprs, tidexpr);
115 /* CurrentOfExpr could never appear OR'd with something else */
116 Assert(list_length(tidstate->tss_tidexprs) == 1 ||
117 !tidstate->tss_isCurrentOf);
121 * Compute the list of TIDs to be visited, by evaluating the expressions
124 * (The result is actually an array, not a list.)
127 TidListEval(TidScanState *tidstate)
129 ExprContext *econtext = tidstate->ss.ps.ps_ExprContext;
131 ItemPointerData *tidList;
137 * We silently discard any TIDs that are out of range at the time of scan
138 * start. (Since we hold at least AccessShareLock on the table, it won't
139 * be possible for someone to truncate away the blocks we intend to
142 nblocks = RelationGetNumberOfBlocks(tidstate->ss.ss_currentRelation);
145 * We initialize the array with enough slots for the case that all quals
146 * are simple OpExprs or CurrentOfExprs. If there are any
147 * ScalarArrayOpExprs, we may have to enlarge the array.
149 numAllocTids = list_length(tidstate->tss_tidexprs);
150 tidList = (ItemPointerData *)
151 palloc(numAllocTids * sizeof(ItemPointerData));
154 foreach(l, tidstate->tss_tidexprs)
156 TidExpr *tidexpr = (TidExpr *) lfirst(l);
160 if (tidexpr->exprstate && !tidexpr->isarray)
162 itemptr = (ItemPointer)
163 DatumGetPointer(ExecEvalExprSwitchContext(tidexpr->exprstate,
167 ItemPointerIsValid(itemptr) &&
168 ItemPointerGetBlockNumber(itemptr) < nblocks)
170 if (numTids >= numAllocTids)
173 tidList = (ItemPointerData *)
175 numAllocTids * sizeof(ItemPointerData));
177 tidList[numTids++] = *itemptr;
180 else if (tidexpr->exprstate && tidexpr->isarray)
183 ArrayType *itemarray;
189 arraydatum = ExecEvalExprSwitchContext(tidexpr->exprstate,
194 itemarray = DatumGetArrayTypeP(arraydatum);
195 deconstruct_array(itemarray,
196 TIDOID, sizeof(ItemPointerData), false, 's',
197 &ipdatums, &ipnulls, &ndatums);
198 if (numTids + ndatums > numAllocTids)
200 numAllocTids = numTids + ndatums;
201 tidList = (ItemPointerData *)
203 numAllocTids * sizeof(ItemPointerData));
205 for (i = 0; i < ndatums; i++)
209 itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
210 if (ItemPointerIsValid(itemptr) &&
211 ItemPointerGetBlockNumber(itemptr) < nblocks)
212 tidList[numTids++] = *itemptr;
220 ItemPointerData cursor_tid;
222 Assert(tidexpr->cexpr);
223 if (execCurrentOf(tidexpr->cexpr, econtext,
224 RelationGetRelid(tidstate->ss.ss_currentRelation),
227 if (numTids >= numAllocTids)
230 tidList = (ItemPointerData *)
232 numAllocTids * sizeof(ItemPointerData));
234 tidList[numTids++] = cursor_tid;
240 * Sort the array of TIDs into order, and eliminate duplicates.
241 * Eliminating duplicates is necessary since we want OR semantics across
242 * the list. Sorting makes it easier to detect duplicates, and as a bonus
243 * ensures that we will visit the heap in the most efficient way.
250 /* CurrentOfExpr could never appear OR'd with something else */
251 Assert(!tidstate->tss_isCurrentOf);
253 qsort((void *) tidList, numTids, sizeof(ItemPointerData),
256 for (i = 1; i < numTids; i++)
258 if (!ItemPointerEquals(&tidList[lastTid], &tidList[i]))
259 tidList[++lastTid] = tidList[i];
261 numTids = lastTid + 1;
264 tidstate->tss_TidList = tidList;
265 tidstate->tss_NumTids = numTids;
266 tidstate->tss_TidPtr = -1;
270 * qsort comparator for ItemPointerData items
273 itemptr_comparator(const void *a, const void *b)
275 const ItemPointerData *ipa = (const ItemPointerData *) a;
276 const ItemPointerData *ipb = (const ItemPointerData *) b;
277 BlockNumber ba = ItemPointerGetBlockNumber(ipa);
278 BlockNumber bb = ItemPointerGetBlockNumber(ipb);
279 OffsetNumber oa = ItemPointerGetOffsetNumber(ipa);
280 OffsetNumber ob = ItemPointerGetOffsetNumber(ipb);
293 /* ----------------------------------------------------------------
296 * Retrieve a tuple from the TidScan node's currentRelation
297 * using the tids in the TidScanState information.
299 * ----------------------------------------------------------------
301 static TupleTableSlot *
302 TidNext(TidScanState *node)
305 ScanDirection direction;
307 Relation heapRelation;
309 TupleTableSlot *slot;
310 Buffer buffer = InvalidBuffer;
311 ItemPointerData *tidList;
316 * extract necessary information from tid scan node
318 estate = node->ss.ps.state;
319 direction = estate->es_direction;
320 snapshot = estate->es_snapshot;
321 heapRelation = node->ss.ss_currentRelation;
322 slot = node->ss.ss_ScanTupleSlot;
325 * First time through, compute the list of TIDs to be visited
327 if (node->tss_TidList == NULL)
330 tidList = node->tss_TidList;
331 numTids = node->tss_NumTids;
334 * We use node->tss_htup as the tuple pointer; note this can't just be a
335 * local variable here, as the scan tuple slot will keep a pointer to it.
337 tuple = &(node->tss_htup);
340 * Initialize or advance scan position, depending on direction.
342 bBackward = ScanDirectionIsBackward(direction);
345 if (node->tss_TidPtr < 0)
347 /* initialize for backward scan */
348 node->tss_TidPtr = numTids - 1;
355 if (node->tss_TidPtr < 0)
357 /* initialize for forward scan */
358 node->tss_TidPtr = 0;
364 while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
366 tuple->t_self = tidList[node->tss_TidPtr];
369 * For WHERE CURRENT OF, the tuple retrieved from the cursor might
370 * since have been updated; if so, we should fetch the version that is
371 * current according to our snapshot.
373 if (node->tss_isCurrentOf)
374 heap_get_latest_tid(heapRelation, snapshot, &tuple->t_self);
376 if (heap_fetch(heapRelation, snapshot, tuple, &buffer, false, NULL))
379 * store the scanned tuple in the scan tuple slot of the scan
380 * state. Eventually we will only do this and not return a tuple.
381 * Note: we pass 'false' because tuples returned by amgetnext are
382 * pointers onto disk pages and were not created with palloc() and
383 * so should not be pfree()'d.
385 ExecStoreTuple(tuple, /* tuple to store */
386 slot, /* slot to store in */
387 buffer, /* buffer associated with tuple */
388 false); /* don't pfree */
391 * At this point we have an extra pin on the buffer, because
392 * ExecStoreTuple incremented the pin count. Drop our local pin.
394 ReleaseBuffer(buffer);
398 /* Bad TID or failed snapshot qual; try next */
406 * if we get here it means the tid scan failed so we are at the end of the
409 return ExecClearTuple(slot);
413 * TidRecheck -- access method routine to recheck a tuple in EvalPlanQual
416 TidRecheck(TidScanState *node, TupleTableSlot *slot)
419 * XXX shouldn't we check here to make sure tuple matches TID list? In
420 * runtime-key case this is not certain, is it? However, in the WHERE
421 * CURRENT OF case it might not match anyway ...
427 /* ----------------------------------------------------------------
430 * Scans the relation using tids and returns
431 * the next qualifying tuple in the direction specified.
432 * We call the ExecScan() routine and pass it the appropriate
433 * access method functions.
436 * -- the "cursor" maintained by the AMI is positioned at the tuple
437 * returned previously.
440 * -- the relation indicated is opened for scanning so that the
441 * "cursor" is positioned before the first qualifying tuple.
443 * ----------------------------------------------------------------
446 ExecTidScan(TidScanState *node)
448 return ExecScan(&node->ss,
449 (ExecScanAccessMtd) TidNext,
450 (ExecScanRecheckMtd) TidRecheck);
453 /* ----------------------------------------------------------------
454 * ExecReScanTidScan(node)
455 * ----------------------------------------------------------------
458 ExecReScanTidScan(TidScanState *node)
460 if (node->tss_TidList)
461 pfree(node->tss_TidList);
462 node->tss_TidList = NULL;
463 node->tss_NumTids = 0;
464 node->tss_TidPtr = -1;
466 ExecScanReScan(&node->ss);
469 /* ----------------------------------------------------------------
472 * Releases any storage allocated through C routines.
474 * ----------------------------------------------------------------
477 ExecEndTidScan(TidScanState *node)
480 * Free the exprcontext
482 ExecFreeExprContext(&node->ss.ps);
485 * clear out tuple table slots
487 ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
488 ExecClearTuple(node->ss.ss_ScanTupleSlot);
491 * close the heap relation.
493 ExecCloseScanRelation(node->ss.ss_currentRelation);
496 /* ----------------------------------------------------------------
499 * Initializes the tid scan's state information, creates
500 * scan keys, and opens the base and tid relations.
503 * node: TidNode node produced by the planner.
504 * estate: the execution state initialized in InitPlan.
505 * ----------------------------------------------------------------
508 ExecInitTidScan(TidScan *node, EState *estate, int eflags)
510 TidScanState *tidstate;
511 Relation currentRelation;
514 * create state structure
516 tidstate = makeNode(TidScanState);
517 tidstate->ss.ps.plan = (Plan *) node;
518 tidstate->ss.ps.state = estate;
521 * Miscellaneous initialization
523 * create expression context for node
525 ExecAssignExprContext(estate, &tidstate->ss.ps);
528 * initialize child expressions
530 tidstate->ss.ps.qual =
531 ExecInitQual(node->scan.plan.qual, (PlanState *) tidstate);
533 TidExprListCreate(tidstate);
536 * tuple table initialization
538 ExecInitResultTupleSlot(estate, &tidstate->ss.ps);
539 ExecInitScanTupleSlot(estate, &tidstate->ss);
542 * mark tid list as not computed yet
544 tidstate->tss_TidList = NULL;
545 tidstate->tss_NumTids = 0;
546 tidstate->tss_TidPtr = -1;
549 * open the base relation and acquire appropriate lock on it.
551 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
553 tidstate->ss.ss_currentRelation = currentRelation;
554 tidstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
557 * get the scan type from the relation descriptor.
559 ExecAssignScanType(&tidstate->ss, RelationGetDescr(currentRelation));
562 * Initialize result tuple type and projection info.
564 ExecAssignResultTypeFromTL(&tidstate->ss.ps);
565 ExecAssignScanProjectionInfo(&tidstate->ss);