1 /*-------------------------------------------------------------------------
4 * Routines to support direct tid scans of relations
6 * Portions Copyright (c) 1996-2011, 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.
22 * ExecTidMarkPos marks scan position.
23 * ExecTidRestrPos restores scan position.
27 #include "access/heapam.h"
28 #include "access/sysattr.h"
29 #include "catalog/pg_type.h"
30 #include "executor/execdebug.h"
31 #include "executor/nodeTidscan.h"
32 #include "optimizer/clauses.h"
33 #include "storage/bufmgr.h"
34 #include "utils/array.h"
35 #include "utils/rel.h"
38 #define IsCTIDVar(node) \
41 ((Var *) (node))->varattno == SelfItemPointerAttributeNumber && \
42 ((Var *) (node))->varlevelsup == 0)
44 static void TidListCreate(TidScanState *tidstate);
45 static int itemptr_comparator(const void *a, const void *b);
46 static TupleTableSlot *TidNext(TidScanState *node);
50 * Compute the list of TIDs to be visited, by evaluating the expressions
53 * (The result is actually an array, not a list.)
56 TidListCreate(TidScanState *tidstate)
58 List *evalList = tidstate->tss_tidquals;
59 ExprContext *econtext = tidstate->ss.ps.ps_ExprContext;
61 ItemPointerData *tidList;
67 * We silently discard any TIDs that are out of range at the time of scan
68 * start. (Since we hold at least AccessShareLock on the table, it won't
69 * be possible for someone to truncate away the blocks we intend to
72 nblocks = RelationGetNumberOfBlocks(tidstate->ss.ss_currentRelation);
75 * We initialize the array with enough slots for the case that all quals
76 * are simple OpExprs or CurrentOfExprs. If there are any
77 * ScalarArrayOpExprs, we may have to enlarge the array.
79 numAllocTids = list_length(evalList);
80 tidList = (ItemPointerData *)
81 palloc(numAllocTids * sizeof(ItemPointerData));
83 tidstate->tss_isCurrentOf = false;
87 ExprState *exstate = (ExprState *) lfirst(l);
88 Expr *expr = exstate->expr;
92 if (is_opclause(expr))
94 FuncExprState *fexstate = (FuncExprState *) exstate;
98 arg1 = get_leftop(expr);
99 arg2 = get_rightop(expr);
101 exstate = (ExprState *) lsecond(fexstate->args);
102 else if (IsCTIDVar(arg2))
103 exstate = (ExprState *) linitial(fexstate->args);
105 elog(ERROR, "could not identify CTID variable");
107 itemptr = (ItemPointer)
108 DatumGetPointer(ExecEvalExprSwitchContext(exstate,
113 ItemPointerIsValid(itemptr) &&
114 ItemPointerGetBlockNumber(itemptr) < nblocks)
116 if (numTids >= numAllocTids)
119 tidList = (ItemPointerData *)
121 numAllocTids * sizeof(ItemPointerData));
123 tidList[numTids++] = *itemptr;
126 else if (expr && IsA(expr, ScalarArrayOpExpr))
128 ScalarArrayOpExprState *saexstate = (ScalarArrayOpExprState *) exstate;
130 ArrayType *itemarray;
136 exstate = (ExprState *) lsecond(saexstate->fxprstate.args);
137 arraydatum = ExecEvalExprSwitchContext(exstate,
143 itemarray = DatumGetArrayTypeP(arraydatum);
144 deconstruct_array(itemarray,
145 TIDOID, SizeOfIptrData, false, 's',
146 &ipdatums, &ipnulls, &ndatums);
147 if (numTids + ndatums > numAllocTids)
149 numAllocTids = numTids + ndatums;
150 tidList = (ItemPointerData *)
152 numAllocTids * sizeof(ItemPointerData));
154 for (i = 0; i < ndatums; i++)
158 itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
159 if (ItemPointerIsValid(itemptr) &&
160 ItemPointerGetBlockNumber(itemptr) < nblocks)
161 tidList[numTids++] = *itemptr;
167 else if (expr && IsA(expr, CurrentOfExpr))
169 CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
170 ItemPointerData cursor_tid;
172 if (execCurrentOf(cexpr, econtext,
173 RelationGetRelid(tidstate->ss.ss_currentRelation),
176 if (numTids >= numAllocTids)
179 tidList = (ItemPointerData *)
181 numAllocTids * sizeof(ItemPointerData));
183 tidList[numTids++] = cursor_tid;
184 tidstate->tss_isCurrentOf = true;
188 elog(ERROR, "could not identify CTID expression");
192 * Sort the array of TIDs into order, and eliminate duplicates.
193 * Eliminating duplicates is necessary since we want OR semantics across
194 * the list. Sorting makes it easier to detect duplicates, and as a bonus
195 * ensures that we will visit the heap in the most efficient way.
202 /* CurrentOfExpr could never appear OR'd with something else */
203 Assert(!tidstate->tss_isCurrentOf);
205 qsort((void *) tidList, numTids, sizeof(ItemPointerData),
208 for (i = 1; i < numTids; i++)
210 if (!ItemPointerEquals(&tidList[lastTid], &tidList[i]))
211 tidList[++lastTid] = tidList[i];
213 numTids = lastTid + 1;
216 tidstate->tss_TidList = tidList;
217 tidstate->tss_NumTids = numTids;
218 tidstate->tss_TidPtr = -1;
222 * qsort comparator for ItemPointerData items
225 itemptr_comparator(const void *a, const void *b)
227 const ItemPointerData *ipa = (const ItemPointerData *) a;
228 const ItemPointerData *ipb = (const ItemPointerData *) b;
229 BlockNumber ba = ItemPointerGetBlockNumber(ipa);
230 BlockNumber bb = ItemPointerGetBlockNumber(ipb);
231 OffsetNumber oa = ItemPointerGetOffsetNumber(ipa);
232 OffsetNumber ob = ItemPointerGetOffsetNumber(ipb);
245 /* ----------------------------------------------------------------
248 * Retrieve a tuple from the TidScan node's currentRelation
249 * using the tids in the TidScanState information.
251 * ----------------------------------------------------------------
253 static TupleTableSlot *
254 TidNext(TidScanState *node)
257 ScanDirection direction;
259 Relation heapRelation;
261 TupleTableSlot *slot;
262 Buffer buffer = InvalidBuffer;
263 ItemPointerData *tidList;
268 * extract necessary information from tid scan node
270 estate = node->ss.ps.state;
271 direction = estate->es_direction;
272 snapshot = estate->es_snapshot;
273 heapRelation = node->ss.ss_currentRelation;
274 slot = node->ss.ss_ScanTupleSlot;
277 * First time through, compute the list of TIDs to be visited
279 if (node->tss_TidList == NULL)
282 tidList = node->tss_TidList;
283 numTids = node->tss_NumTids;
285 tuple = &(node->tss_htup);
288 * Initialize or advance scan position, depending on direction.
290 bBackward = ScanDirectionIsBackward(direction);
293 if (node->tss_TidPtr < 0)
295 /* initialize for backward scan */
296 node->tss_TidPtr = numTids - 1;
303 if (node->tss_TidPtr < 0)
305 /* initialize for forward scan */
306 node->tss_TidPtr = 0;
312 while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
314 tuple->t_self = tidList[node->tss_TidPtr];
317 * For WHERE CURRENT OF, the tuple retrieved from the cursor might
318 * since have been updated; if so, we should fetch the version that is
319 * current according to our snapshot.
321 if (node->tss_isCurrentOf)
322 heap_get_latest_tid(heapRelation, snapshot, &tuple->t_self);
324 if (heap_fetch(heapRelation, snapshot, tuple, &buffer, false, NULL))
327 * store the scanned tuple in the scan tuple slot of the scan
328 * state. Eventually we will only do this and not return a tuple.
329 * Note: we pass 'false' because tuples returned by amgetnext are
330 * pointers onto disk pages and were not created with palloc() and
331 * so should not be pfree()'d.
333 ExecStoreTuple(tuple, /* tuple to store */
334 slot, /* slot to store in */
335 buffer, /* buffer associated with tuple */
336 false); /* don't pfree */
339 * At this point we have an extra pin on the buffer, because
340 * ExecStoreTuple incremented the pin count. Drop our local pin.
342 ReleaseBuffer(buffer);
346 /* Bad TID or failed snapshot qual; try next */
354 * if we get here it means the tid scan failed so we are at the end of the
357 return ExecClearTuple(slot);
361 * TidRecheck -- access method routine to recheck a tuple in EvalPlanQual
364 TidRecheck(TidScanState *node, TupleTableSlot *slot)
367 * XXX shouldn't we check here to make sure tuple matches TID list? In
368 * runtime-key case this is not certain, is it? However, in the WHERE
369 * CURRENT OF case it might not match anyway ...
375 /* ----------------------------------------------------------------
378 * Scans the relation using tids and returns
379 * the next qualifying tuple in the direction specified.
380 * We call the ExecScan() routine and pass it the appropriate
381 * access method functions.
384 * -- the "cursor" maintained by the AMI is positioned at the tuple
385 * returned previously.
388 * -- the relation indicated is opened for scanning so that the
389 * "cursor" is positioned before the first qualifying tuple.
391 * ----------------------------------------------------------------
394 ExecTidScan(TidScanState *node)
396 return ExecScan(&node->ss,
397 (ExecScanAccessMtd) TidNext,
398 (ExecScanRecheckMtd) TidRecheck);
401 /* ----------------------------------------------------------------
402 * ExecReScanTidScan(node)
403 * ----------------------------------------------------------------
406 ExecReScanTidScan(TidScanState *node)
408 if (node->tss_TidList)
409 pfree(node->tss_TidList);
410 node->tss_TidList = NULL;
411 node->tss_NumTids = 0;
412 node->tss_TidPtr = -1;
414 ExecScanReScan(&node->ss);
417 /* ----------------------------------------------------------------
420 * Releases any storage allocated through C routines.
422 * ----------------------------------------------------------------
425 ExecEndTidScan(TidScanState *node)
428 * Free the exprcontext
430 ExecFreeExprContext(&node->ss.ps);
433 * clear out tuple table slots
435 ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
436 ExecClearTuple(node->ss.ss_ScanTupleSlot);
439 * close the heap relation.
441 ExecCloseScanRelation(node->ss.ss_currentRelation);
444 /* ----------------------------------------------------------------
447 * Marks scan position by marking the current tid.
449 * ----------------------------------------------------------------
452 ExecTidMarkPos(TidScanState *node)
454 node->tss_MarkTidPtr = node->tss_TidPtr;
457 /* ----------------------------------------------------------------
460 * Restores scan position by restoring the current tid.
463 * XXX Assumes previously marked scan position belongs to current tid
464 * ----------------------------------------------------------------
467 ExecTidRestrPos(TidScanState *node)
469 node->tss_TidPtr = node->tss_MarkTidPtr;
472 /* ----------------------------------------------------------------
475 * Initializes the tid scan's state information, creates
476 * scan keys, and opens the base and tid relations.
479 * node: TidNode node produced by the planner.
480 * estate: the execution state initialized in InitPlan.
481 * ----------------------------------------------------------------
484 ExecInitTidScan(TidScan *node, EState *estate, int eflags)
486 TidScanState *tidstate;
487 Relation currentRelation;
490 * create state structure
492 tidstate = makeNode(TidScanState);
493 tidstate->ss.ps.plan = (Plan *) node;
494 tidstate->ss.ps.state = estate;
497 * Miscellaneous initialization
499 * create expression context for node
501 ExecAssignExprContext(estate, &tidstate->ss.ps);
503 tidstate->ss.ps.ps_TupFromTlist = false;
506 * initialize child expressions
508 tidstate->ss.ps.targetlist = (List *)
509 ExecInitExpr((Expr *) node->scan.plan.targetlist,
510 (PlanState *) tidstate);
511 tidstate->ss.ps.qual = (List *)
512 ExecInitExpr((Expr *) node->scan.plan.qual,
513 (PlanState *) tidstate);
515 tidstate->tss_tidquals = (List *)
516 ExecInitExpr((Expr *) node->tidquals,
517 (PlanState *) tidstate);
520 * tuple table initialization
522 ExecInitResultTupleSlot(estate, &tidstate->ss.ps);
523 ExecInitScanTupleSlot(estate, &tidstate->ss);
526 * mark tid list as not computed yet
528 tidstate->tss_TidList = NULL;
529 tidstate->tss_NumTids = 0;
530 tidstate->tss_TidPtr = -1;
533 * open the base relation and acquire appropriate lock on it.
535 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
537 tidstate->ss.ss_currentRelation = currentRelation;
538 tidstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
541 * get the scan type from the relation descriptor.
543 ExecAssignScanType(&tidstate->ss, RelationGetDescr(currentRelation));
546 * Initialize result tuple type and projection info.
548 ExecAssignResultTypeFromTL(&tidstate->ss.ps);
549 ExecAssignScanProjectionInfo(&tidstate->ss);