1 /*-------------------------------------------------------------------------
4 * Routines to support direct tid scans of relations
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/executor/nodeTidscan.c,v 1.59 2008/04/30 23:28:32 tgl Exp $
13 *-------------------------------------------------------------------------
18 * ExecTidScan scans a relation using tids
19 * ExecInitTidScan creates and initializes state info.
20 * ExecTidReScan 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 "catalog/pg_type.h"
29 #include "executor/execdebug.h"
30 #include "executor/nodeTidscan.h"
31 #include "optimizer/clauses.h"
32 #include "utils/array.h"
35 #define IsCTIDVar(node) \
38 ((Var *) (node))->varattno == SelfItemPointerAttributeNumber && \
39 ((Var *) (node))->varlevelsup == 0)
41 static void TidListCreate(TidScanState *tidstate);
42 static int itemptr_comparator(const void *a, const void *b);
43 static TupleTableSlot *TidNext(TidScanState *node);
47 * Compute the list of TIDs to be visited, by evaluating the expressions
50 * (The result is actually an array, not a list.)
53 TidListCreate(TidScanState *tidstate)
55 List *evalList = tidstate->tss_tidquals;
56 ExprContext *econtext = tidstate->ss.ps.ps_ExprContext;
58 ItemPointerData *tidList;
64 * We silently discard any TIDs that are out of range at the time of
65 * scan start. (Since we hold at least AccessShareLock on the table,
66 * it won't be possible for someone to truncate away the blocks we
69 nblocks = RelationGetNumberOfBlocks(tidstate->ss.ss_currentRelation);
72 * We initialize the array with enough slots for the case that all quals
73 * are simple OpExprs or CurrentOfExprs. If there are any
74 * ScalarArrayOpExprs, we may have to enlarge the array.
76 numAllocTids = list_length(evalList);
77 tidList = (ItemPointerData *)
78 palloc(numAllocTids * sizeof(ItemPointerData));
80 tidstate->tss_isCurrentOf = false;
84 ExprState *exstate = (ExprState *) lfirst(l);
85 Expr *expr = exstate->expr;
89 if (is_opclause(expr))
91 FuncExprState *fexstate = (FuncExprState *) exstate;
95 arg1 = get_leftop(expr);
96 arg2 = get_rightop(expr);
98 exstate = (ExprState *) lsecond(fexstate->args);
99 else if (IsCTIDVar(arg2))
100 exstate = (ExprState *) linitial(fexstate->args);
102 elog(ERROR, "could not identify CTID variable");
104 itemptr = (ItemPointer)
105 DatumGetPointer(ExecEvalExprSwitchContext(exstate,
110 ItemPointerIsValid(itemptr) &&
111 ItemPointerGetBlockNumber(itemptr) < nblocks)
113 if (numTids >= numAllocTids)
116 tidList = (ItemPointerData *)
118 numAllocTids * sizeof(ItemPointerData));
120 tidList[numTids++] = *itemptr;
123 else if (expr && IsA(expr, ScalarArrayOpExpr))
125 ScalarArrayOpExprState *saexstate = (ScalarArrayOpExprState *) exstate;
127 ArrayType *itemarray;
133 exstate = (ExprState *) lsecond(saexstate->fxprstate.args);
134 arraydatum = ExecEvalExprSwitchContext(exstate,
140 itemarray = DatumGetArrayTypeP(arraydatum);
141 deconstruct_array(itemarray,
142 TIDOID, SizeOfIptrData, false, 's',
143 &ipdatums, &ipnulls, &ndatums);
144 if (numTids + ndatums > numAllocTids)
146 numAllocTids = numTids + ndatums;
147 tidList = (ItemPointerData *)
149 numAllocTids * sizeof(ItemPointerData));
151 for (i = 0; i < ndatums; i++)
155 itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
156 if (ItemPointerIsValid(itemptr) &&
157 ItemPointerGetBlockNumber(itemptr) < nblocks)
158 tidList[numTids++] = *itemptr;
164 else if (expr && IsA(expr, CurrentOfExpr))
166 CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
167 ItemPointerData cursor_tid;
169 if (execCurrentOf(cexpr, econtext,
170 RelationGetRelid(tidstate->ss.ss_currentRelation),
173 if (numTids >= numAllocTids)
176 tidList = (ItemPointerData *)
178 numAllocTids * sizeof(ItemPointerData));
180 tidList[numTids++] = cursor_tid;
181 tidstate->tss_isCurrentOf = true;
185 elog(ERROR, "could not identify CTID expression");
189 * Sort the array of TIDs into order, and eliminate duplicates.
190 * Eliminating duplicates is necessary since we want OR semantics across
191 * the list. Sorting makes it easier to detect duplicates, and as a bonus
192 * ensures that we will visit the heap in the most efficient way.
199 /* CurrentOfExpr could never appear OR'd with something else */
200 Assert(!tidstate->tss_isCurrentOf);
202 qsort((void *) tidList, numTids, sizeof(ItemPointerData),
205 for (i = 1; i < numTids; i++)
207 if (!ItemPointerEquals(&tidList[lastTid], &tidList[i]))
208 tidList[++lastTid] = tidList[i];
210 numTids = lastTid + 1;
213 tidstate->tss_TidList = tidList;
214 tidstate->tss_NumTids = numTids;
215 tidstate->tss_TidPtr = -1;
219 * qsort comparator for ItemPointerData items
222 itemptr_comparator(const void *a, const void *b)
224 const ItemPointerData *ipa = (const ItemPointerData *) a;
225 const ItemPointerData *ipb = (const ItemPointerData *) b;
226 BlockNumber ba = ItemPointerGetBlockNumber(ipa);
227 BlockNumber bb = ItemPointerGetBlockNumber(ipb);
228 OffsetNumber oa = ItemPointerGetOffsetNumber(ipa);
229 OffsetNumber ob = ItemPointerGetOffsetNumber(ipb);
242 /* ----------------------------------------------------------------
245 * Retrieve a tuple from the TidScan node's currentRelation
246 * using the tids in the TidScanState information.
248 * ----------------------------------------------------------------
250 static TupleTableSlot *
251 TidNext(TidScanState *node)
254 ScanDirection direction;
256 Relation heapRelation;
258 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;
273 scanrelid = ((TidScan *) node->ss.ps.plan)->scan.scanrelid;
276 * Check if we are evaluating PlanQual for tuple of this relation.
277 * Additional checking is not good, but no other way for now. We could
278 * introduce new nodes for this case and handle TidScan --> NewNode
279 * switching in Init/ReScan plan...
281 if (estate->es_evTuple != NULL &&
282 estate->es_evTuple[scanrelid - 1] != NULL)
284 if (estate->es_evTupleNull[scanrelid - 1])
285 return ExecClearTuple(slot);
288 * XXX shouldn't we check here to make sure tuple matches TID list? In
289 * runtime-key case this is not certain, is it? However, in the WHERE
290 * CURRENT OF case it might not match anyway ...
293 ExecStoreTuple(estate->es_evTuple[scanrelid - 1],
294 slot, InvalidBuffer, false);
296 /* Flag for the next call that no more tuples */
297 estate->es_evTupleNull[scanrelid - 1] = true;
302 * First time through, compute the list of TIDs to be visited
304 if (node->tss_TidList == NULL)
307 tidList = node->tss_TidList;
308 numTids = node->tss_NumTids;
310 tuple = &(node->tss_htup);
313 * Initialize or advance scan position, depending on direction.
315 bBackward = ScanDirectionIsBackward(direction);
318 if (node->tss_TidPtr < 0)
320 /* initialize for backward scan */
321 node->tss_TidPtr = numTids - 1;
328 if (node->tss_TidPtr < 0)
330 /* initialize for forward scan */
331 node->tss_TidPtr = 0;
337 while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
339 tuple->t_self = tidList[node->tss_TidPtr];
342 * For WHERE CURRENT OF, the tuple retrieved from the cursor might
343 * since have been updated; if so, we should fetch the version that is
344 * current according to our snapshot.
346 if (node->tss_isCurrentOf)
347 heap_get_latest_tid(heapRelation, snapshot, &tuple->t_self);
349 if (heap_fetch(heapRelation, snapshot, tuple, &buffer, false, NULL))
352 * store the scanned tuple in the scan tuple slot of the scan
353 * state. Eventually we will only do this and not return a tuple.
354 * Note: we pass 'false' because tuples returned by amgetnext are
355 * pointers onto disk pages and were not created with palloc() and
356 * so should not be pfree()'d.
358 ExecStoreTuple(tuple, /* tuple to store */
359 slot, /* slot to store in */
360 buffer, /* buffer associated with tuple */
361 false); /* don't pfree */
364 * At this point we have an extra pin on the buffer, because
365 * ExecStoreTuple incremented the pin count. Drop our local pin.
367 ReleaseBuffer(buffer);
371 /* Bad TID or failed snapshot qual; try next */
379 * if we get here it means the tid scan failed so we are at the end of the
382 return ExecClearTuple(slot);
385 /* ----------------------------------------------------------------
388 * Scans the relation using tids and returns
389 * the next qualifying tuple in the direction specified.
390 * It calls ExecScan() and passes it the access methods which returns
391 * the next tuple using the tids.
394 * -- the "cursor" maintained by the AMI is positioned at the tuple
395 * returned previously.
398 * -- the relation indicated is opened for scanning so that the
399 * "cursor" is positioned before the first qualifying tuple.
401 * ----------------------------------------------------------------
404 ExecTidScan(TidScanState *node)
407 * use TidNext as access method
409 return ExecScan(&node->ss, (ExecScanAccessMtd) TidNext);
412 /* ----------------------------------------------------------------
413 * ExecTidReScan(node)
414 * ----------------------------------------------------------------
417 ExecTidReScan(TidScanState *node, ExprContext *exprCtxt)
422 estate = node->ss.ps.state;
423 scanrelid = ((TidScan *) node->ss.ps.plan)->scan.scanrelid;
425 node->ss.ps.ps_TupFromTlist = false;
427 /* If we are being passed an outer tuple, save it for runtime key calc */
428 if (exprCtxt != NULL)
429 node->ss.ps.ps_ExprContext->ecxt_outertuple =
430 exprCtxt->ecxt_outertuple;
432 /* If this is re-scanning of PlanQual ... */
433 if (estate->es_evTuple != NULL &&
434 estate->es_evTuple[scanrelid - 1] != NULL)
436 estate->es_evTupleNull[scanrelid - 1] = false;
440 if (node->tss_TidList)
441 pfree(node->tss_TidList);
442 node->tss_TidList = NULL;
443 node->tss_NumTids = 0;
444 node->tss_TidPtr = -1;
447 /* ----------------------------------------------------------------
450 * Releases any storage allocated through C routines.
452 * ----------------------------------------------------------------
455 ExecEndTidScan(TidScanState *node)
458 * Free the exprcontext
460 ExecFreeExprContext(&node->ss.ps);
463 * clear out tuple table slots
465 ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
466 ExecClearTuple(node->ss.ss_ScanTupleSlot);
469 * close the heap relation.
471 ExecCloseScanRelation(node->ss.ss_currentRelation);
474 /* ----------------------------------------------------------------
477 * Marks scan position by marking the current tid.
479 * ----------------------------------------------------------------
482 ExecTidMarkPos(TidScanState *node)
484 node->tss_MarkTidPtr = node->tss_TidPtr;
487 /* ----------------------------------------------------------------
490 * Restores scan position by restoring the current tid.
493 * XXX Assumes previously marked scan position belongs to current tid
494 * ----------------------------------------------------------------
497 ExecTidRestrPos(TidScanState *node)
499 node->tss_TidPtr = node->tss_MarkTidPtr;
502 /* ----------------------------------------------------------------
505 * Initializes the tid scan's state information, creates
506 * scan keys, and opens the base and tid relations.
509 * node: TidNode node produced by the planner.
510 * estate: the execution state initialized in InitPlan.
511 * ----------------------------------------------------------------
514 ExecInitTidScan(TidScan *node, EState *estate, int eflags)
516 TidScanState *tidstate;
517 Relation currentRelation;
520 * create state structure
522 tidstate = makeNode(TidScanState);
523 tidstate->ss.ps.plan = (Plan *) node;
524 tidstate->ss.ps.state = estate;
527 * Miscellaneous initialization
529 * create expression context for node
531 ExecAssignExprContext(estate, &tidstate->ss.ps);
533 tidstate->ss.ps.ps_TupFromTlist = false;
536 * initialize child expressions
538 tidstate->ss.ps.targetlist = (List *)
539 ExecInitExpr((Expr *) node->scan.plan.targetlist,
540 (PlanState *) tidstate);
541 tidstate->ss.ps.qual = (List *)
542 ExecInitExpr((Expr *) node->scan.plan.qual,
543 (PlanState *) tidstate);
545 tidstate->tss_tidquals = (List *)
546 ExecInitExpr((Expr *) node->tidquals,
547 (PlanState *) tidstate);
549 #define TIDSCAN_NSLOTS 2
552 * tuple table initialization
554 ExecInitResultTupleSlot(estate, &tidstate->ss.ps);
555 ExecInitScanTupleSlot(estate, &tidstate->ss);
558 * mark tid list as not computed yet
560 tidstate->tss_TidList = NULL;
561 tidstate->tss_NumTids = 0;
562 tidstate->tss_TidPtr = -1;
565 * open the base relation and acquire appropriate lock on it.
567 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
569 tidstate->ss.ss_currentRelation = currentRelation;
570 tidstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
573 * get the scan type from the relation descriptor.
575 ExecAssignScanType(&tidstate->ss, RelationGetDescr(currentRelation));
578 * Initialize result tuple type and projection info.
580 ExecAssignResultTypeFromTL(&tidstate->ss.ps);
581 ExecAssignScanProjectionInfo(&tidstate->ss);
590 ExecCountSlotsTidScan(TidScan *node)
592 return ExecCountSlotsNode(outerPlan((Plan *) node)) +
593 ExecCountSlotsNode(innerPlan((Plan *) node)) + TIDSCAN_NSLOTS;