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/sysattr.h"
28 #include "catalog/pg_type.h"
29 #include "executor/execdebug.h"
30 #include "executor/nodeTidscan.h"
31 #include "optimizer/clauses.h"
32 #include "storage/bufmgr.h"
33 #include "utils/rel.h"
36 #define IsCTIDVar(node) \
39 ((Var *) (node))->varattno == SelfItemPointerAttributeNumber && \
40 ((Var *) (node))->varlevelsup == 0)
42 static void TidListCreate(TidScanState *tidstate);
43 static int itemptr_comparator(const void *a, const void *b);
44 static TupleTableSlot *TidNext(TidScanState *node);
48 * Compute the list of TIDs to be visited, by evaluating the expressions
51 * (The result is actually an array, not a list.)
54 TidListCreate(TidScanState *tidstate)
56 List *evalList = tidstate->tss_tidquals;
57 ExprContext *econtext = tidstate->ss.ps.ps_ExprContext;
59 ItemPointerData *tidList;
65 * We silently discard any TIDs that are out of range at the time of scan
66 * start. (Since we hold at least AccessShareLock on the table, it won't
67 * be possible for someone to truncate away the blocks we intend to
70 nblocks = RelationGetNumberOfBlocks(tidstate->ss.ss_currentRelation);
73 * We initialize the array with enough slots for the case that all quals
74 * are simple OpExprs or CurrentOfExprs. If there are any
75 * ScalarArrayOpExprs, we may have to enlarge the array.
77 numAllocTids = list_length(evalList);
78 tidList = (ItemPointerData *)
79 palloc(numAllocTids * sizeof(ItemPointerData));
81 tidstate->tss_isCurrentOf = false;
85 ExprState *exstate = (ExprState *) lfirst(l);
86 Expr *expr = exstate->expr;
90 if (is_opclause(expr))
92 FuncExprState *fexstate = (FuncExprState *) exstate;
96 arg1 = get_leftop(expr);
97 arg2 = get_rightop(expr);
99 exstate = (ExprState *) lsecond(fexstate->args);
100 else if (IsCTIDVar(arg2))
101 exstate = (ExprState *) linitial(fexstate->args);
103 elog(ERROR, "could not identify CTID variable");
105 itemptr = (ItemPointer)
106 DatumGetPointer(ExecEvalExprSwitchContext(exstate,
111 ItemPointerIsValid(itemptr) &&
112 ItemPointerGetBlockNumber(itemptr) < nblocks)
114 if (numTids >= numAllocTids)
117 tidList = (ItemPointerData *)
119 numAllocTids * sizeof(ItemPointerData));
121 tidList[numTids++] = *itemptr;
124 else if (expr && IsA(expr, ScalarArrayOpExpr))
126 ScalarArrayOpExprState *saexstate = (ScalarArrayOpExprState *) exstate;
128 ArrayType *itemarray;
134 exstate = (ExprState *) lsecond(saexstate->fxprstate.args);
135 arraydatum = ExecEvalExprSwitchContext(exstate,
141 itemarray = DatumGetArrayTypeP(arraydatum);
142 deconstruct_array(itemarray,
143 TIDOID, SizeOfIptrData, false, 's',
144 &ipdatums, &ipnulls, &ndatums);
145 if (numTids + ndatums > numAllocTids)
147 numAllocTids = numTids + ndatums;
148 tidList = (ItemPointerData *)
150 numAllocTids * sizeof(ItemPointerData));
152 for (i = 0; i < ndatums; i++)
156 itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
157 if (ItemPointerIsValid(itemptr) &&
158 ItemPointerGetBlockNumber(itemptr) < nblocks)
159 tidList[numTids++] = *itemptr;
165 else if (expr && IsA(expr, CurrentOfExpr))
167 CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
168 ItemPointerData cursor_tid;
170 if (execCurrentOf(cexpr, econtext,
171 RelationGetRelid(tidstate->ss.ss_currentRelation),
174 if (numTids >= numAllocTids)
177 tidList = (ItemPointerData *)
179 numAllocTids * sizeof(ItemPointerData));
181 tidList[numTids++] = cursor_tid;
182 tidstate->tss_isCurrentOf = true;
186 elog(ERROR, "could not identify CTID expression");
190 * Sort the array of TIDs into order, and eliminate duplicates.
191 * Eliminating duplicates is necessary since we want OR semantics across
192 * the list. Sorting makes it easier to detect duplicates, and as a bonus
193 * ensures that we will visit the heap in the most efficient way.
200 /* CurrentOfExpr could never appear OR'd with something else */
201 Assert(!tidstate->tss_isCurrentOf);
203 qsort((void *) tidList, numTids, sizeof(ItemPointerData),
206 for (i = 1; i < numTids; i++)
208 if (!ItemPointerEquals(&tidList[lastTid], &tidList[i]))
209 tidList[++lastTid] = tidList[i];
211 numTids = lastTid + 1;
214 tidstate->tss_TidList = tidList;
215 tidstate->tss_NumTids = numTids;
216 tidstate->tss_TidPtr = -1;
220 * qsort comparator for ItemPointerData items
223 itemptr_comparator(const void *a, const void *b)
225 const ItemPointerData *ipa = (const ItemPointerData *) a;
226 const ItemPointerData *ipb = (const ItemPointerData *) b;
227 BlockNumber ba = ItemPointerGetBlockNumber(ipa);
228 BlockNumber bb = ItemPointerGetBlockNumber(ipb);
229 OffsetNumber oa = ItemPointerGetOffsetNumber(ipa);
230 OffsetNumber ob = ItemPointerGetOffsetNumber(ipb);
243 /* ----------------------------------------------------------------
246 * Retrieve a tuple from the TidScan node's currentRelation
247 * using the tids in the TidScanState information.
249 * ----------------------------------------------------------------
251 static TupleTableSlot *
252 TidNext(TidScanState *node)
255 ScanDirection direction;
257 Relation heapRelation;
259 TupleTableSlot *slot;
260 Buffer buffer = InvalidBuffer;
261 ItemPointerData *tidList;
266 * extract necessary information from tid scan node
268 estate = node->ss.ps.state;
269 direction = estate->es_direction;
270 snapshot = estate->es_snapshot;
271 heapRelation = node->ss.ss_currentRelation;
272 slot = node->ss.ss_ScanTupleSlot;
275 * First time through, compute the list of TIDs to be visited
277 if (node->tss_TidList == NULL)
280 tidList = node->tss_TidList;
281 numTids = node->tss_NumTids;
283 tuple = &(node->tss_htup);
286 * Initialize or advance scan position, depending on direction.
288 bBackward = ScanDirectionIsBackward(direction);
291 if (node->tss_TidPtr < 0)
293 /* initialize for backward scan */
294 node->tss_TidPtr = numTids - 1;
301 if (node->tss_TidPtr < 0)
303 /* initialize for forward scan */
304 node->tss_TidPtr = 0;
310 while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
312 tuple->t_self = tidList[node->tss_TidPtr];
315 * For WHERE CURRENT OF, the tuple retrieved from the cursor might
316 * since have been updated; if so, we should fetch the version that is
317 * current according to our snapshot.
319 if (node->tss_isCurrentOf)
320 heap_get_latest_tid(heapRelation, snapshot, &tuple->t_self);
322 if (heap_fetch(heapRelation, snapshot, tuple, &buffer, false, NULL))
325 * store the scanned tuple in the scan tuple slot of the scan
326 * state. Eventually we will only do this and not return a tuple.
327 * Note: we pass 'false' because tuples returned by amgetnext are
328 * pointers onto disk pages and were not created with palloc() and
329 * so should not be pfree()'d.
331 ExecStoreTuple(tuple, /* tuple to store */
332 slot, /* slot to store in */
333 buffer, /* buffer associated with tuple */
334 false); /* don't pfree */
337 * At this point we have an extra pin on the buffer, because
338 * ExecStoreTuple incremented the pin count. Drop our local pin.
340 ReleaseBuffer(buffer);
344 /* Bad TID or failed snapshot qual; try next */
352 * if we get here it means the tid scan failed so we are at the end of the
355 return ExecClearTuple(slot);
359 * TidRecheck -- access method routine to recheck a tuple in EvalPlanQual
362 TidRecheck(TidScanState *node, TupleTableSlot *slot)
365 * XXX shouldn't we check here to make sure tuple matches TID list? In
366 * runtime-key case this is not certain, is it? However, in the WHERE
367 * CURRENT OF case it might not match anyway ...
373 /* ----------------------------------------------------------------
376 * Scans the relation using tids and returns
377 * the next qualifying tuple in the direction specified.
378 * We call the ExecScan() routine and pass it the appropriate
379 * access method functions.
382 * -- the "cursor" maintained by the AMI is positioned at the tuple
383 * returned previously.
386 * -- the relation indicated is opened for scanning so that the
387 * "cursor" is positioned before the first qualifying tuple.
389 * ----------------------------------------------------------------
392 ExecTidScan(TidScanState *node)
394 return ExecScan(&node->ss,
395 (ExecScanAccessMtd) TidNext,
396 (ExecScanRecheckMtd) TidRecheck);
399 /* ----------------------------------------------------------------
400 * ExecReScanTidScan(node)
401 * ----------------------------------------------------------------
404 ExecReScanTidScan(TidScanState *node)
406 if (node->tss_TidList)
407 pfree(node->tss_TidList);
408 node->tss_TidList = NULL;
409 node->tss_NumTids = 0;
410 node->tss_TidPtr = -1;
412 ExecScanReScan(&node->ss);
415 /* ----------------------------------------------------------------
418 * Releases any storage allocated through C routines.
420 * ----------------------------------------------------------------
423 ExecEndTidScan(TidScanState *node)
426 * Free the exprcontext
428 ExecFreeExprContext(&node->ss.ps);
431 * clear out tuple table slots
433 ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
434 ExecClearTuple(node->ss.ss_ScanTupleSlot);
437 * close the heap relation.
439 ExecCloseScanRelation(node->ss.ss_currentRelation);
442 /* ----------------------------------------------------------------
445 * Marks scan position by marking the current tid.
447 * ----------------------------------------------------------------
450 ExecTidMarkPos(TidScanState *node)
452 node->tss_MarkTidPtr = node->tss_TidPtr;
455 /* ----------------------------------------------------------------
458 * Restores scan position by restoring the current tid.
461 * XXX Assumes previously marked scan position belongs to current tid
462 * ----------------------------------------------------------------
465 ExecTidRestrPos(TidScanState *node)
467 node->tss_TidPtr = node->tss_MarkTidPtr;
470 /* ----------------------------------------------------------------
473 * Initializes the tid scan's state information, creates
474 * scan keys, and opens the base and tid relations.
477 * node: TidNode node produced by the planner.
478 * estate: the execution state initialized in InitPlan.
479 * ----------------------------------------------------------------
482 ExecInitTidScan(TidScan *node, EState *estate, int eflags)
484 TidScanState *tidstate;
485 Relation currentRelation;
488 * create state structure
490 tidstate = makeNode(TidScanState);
491 tidstate->ss.ps.plan = (Plan *) node;
492 tidstate->ss.ps.state = estate;
495 * Miscellaneous initialization
497 * create expression context for node
499 ExecAssignExprContext(estate, &tidstate->ss.ps);
501 tidstate->ss.ps.ps_TupFromTlist = false;
504 * initialize child expressions
506 tidstate->ss.ps.targetlist = (List *)
507 ExecInitExpr((Expr *) node->scan.plan.targetlist,
508 (PlanState *) tidstate);
509 tidstate->ss.ps.qual = (List *)
510 ExecInitExpr((Expr *) node->scan.plan.qual,
511 (PlanState *) tidstate);
513 tidstate->tss_tidquals = (List *)
514 ExecInitExpr((Expr *) node->tidquals,
515 (PlanState *) tidstate);
518 * tuple table initialization
520 ExecInitResultTupleSlot(estate, &tidstate->ss.ps);
521 ExecInitScanTupleSlot(estate, &tidstate->ss);
524 * mark tid list as not computed yet
526 tidstate->tss_TidList = NULL;
527 tidstate->tss_NumTids = 0;
528 tidstate->tss_TidPtr = -1;
531 * open the base relation and acquire appropriate lock on it.
533 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
535 tidstate->ss.ss_currentRelation = currentRelation;
536 tidstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
539 * get the scan type from the relation descriptor.
541 ExecAssignScanType(&tidstate->ss, RelationGetDescr(currentRelation));
544 * Initialize result tuple type and projection info.
546 ExecAssignResultTypeFromTL(&tidstate->ss.ps);
547 ExecAssignScanProjectionInfo(&tidstate->ss);