1 /*-------------------------------------------------------------------------
4 * Routines to support direct tid scans of relations
6 * Portions Copyright (c) 1996-2007, 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.55 2007/06/11 22:22:40 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;
57 ItemPointerData *tidList;
63 * We initialize the array with enough slots for the case that all quals
64 * are simple OpExprs or CurrentOfExprs. If there are any
65 * ScalarArrayOpExprs, we may have to enlarge the array.
67 numAllocTids = list_length(evalList);
68 tidList = (ItemPointerData *)
69 palloc(numAllocTids * sizeof(ItemPointerData));
74 ExprState *exstate = (ExprState *) lfirst(l);
75 Expr *expr = exstate->expr;
79 if (is_opclause(expr))
81 FuncExprState *fexstate = (FuncExprState *) exstate;
85 arg1 = get_leftop(expr);
86 arg2 = get_rightop(expr);
88 exstate = (ExprState *) lsecond(fexstate->args);
89 else if (IsCTIDVar(arg2))
90 exstate = (ExprState *) linitial(fexstate->args);
92 elog(ERROR, "could not identify CTID variable");
94 itemptr = (ItemPointer)
95 DatumGetPointer(ExecEvalExprSwitchContext(exstate,
99 if (!isNull && ItemPointerIsValid(itemptr))
101 if (numTids >= numAllocTids)
104 tidList = (ItemPointerData *)
106 numAllocTids * sizeof(ItemPointerData));
108 tidList[numTids++] = *itemptr;
111 else if (expr && IsA(expr, ScalarArrayOpExpr))
113 ScalarArrayOpExprState *saexstate = (ScalarArrayOpExprState *) exstate;
115 ArrayType *itemarray;
121 exstate = (ExprState *) lsecond(saexstate->fxprstate.args);
122 arraydatum = ExecEvalExprSwitchContext(exstate,
128 itemarray = DatumGetArrayTypeP(arraydatum);
129 deconstruct_array(itemarray,
130 TIDOID, SizeOfIptrData, false, 's',
131 &ipdatums, &ipnulls, &ndatums);
132 if (numTids + ndatums > numAllocTids)
134 numAllocTids = numTids + ndatums;
135 tidList = (ItemPointerData *)
137 numAllocTids * sizeof(ItemPointerData));
139 for (i = 0; i < ndatums; i++)
143 itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
144 if (ItemPointerIsValid(itemptr))
145 tidList[numTids++] = *itemptr;
151 else if (expr && IsA(expr, CurrentOfExpr))
153 CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
154 ItemPointerData cursor_tid;
156 if (execCurrentOf(cexpr, econtext,
157 RelationGetRelid(tidstate->ss.ss_currentRelation),
160 if (numTids >= numAllocTids)
163 tidList = (ItemPointerData *)
165 numAllocTids * sizeof(ItemPointerData));
167 tidList[numTids++] = cursor_tid;
171 elog(ERROR, "could not identify CTID expression");
175 * Sort the array of TIDs into order, and eliminate duplicates.
176 * Eliminating duplicates is necessary since we want OR semantics across
177 * the list. Sorting makes it easier to detect duplicates, and as a bonus
178 * ensures that we will visit the heap in the most efficient way.
185 qsort((void *) tidList, numTids, sizeof(ItemPointerData),
188 for (i = 1; i < numTids; i++)
190 if (!ItemPointerEquals(&tidList[lastTid], &tidList[i]))
191 tidList[++lastTid] = tidList[i];
193 numTids = lastTid + 1;
196 tidstate->tss_TidList = tidList;
197 tidstate->tss_NumTids = numTids;
198 tidstate->tss_TidPtr = -1;
202 * qsort comparator for ItemPointerData items
205 itemptr_comparator(const void *a, const void *b)
207 const ItemPointerData *ipa = (const ItemPointerData *) a;
208 const ItemPointerData *ipb = (const ItemPointerData *) b;
209 BlockNumber ba = ItemPointerGetBlockNumber(ipa);
210 BlockNumber bb = ItemPointerGetBlockNumber(ipb);
211 OffsetNumber oa = ItemPointerGetOffsetNumber(ipa);
212 OffsetNumber ob = ItemPointerGetOffsetNumber(ipb);
225 /* ----------------------------------------------------------------
228 * Retrieve a tuple from the TidScan node's currentRelation
229 * using the tids in the TidScanState information.
231 * ----------------------------------------------------------------
233 static TupleTableSlot *
234 TidNext(TidScanState *node)
237 ScanDirection direction;
239 Relation heapRelation;
241 TupleTableSlot *slot;
243 Buffer buffer = InvalidBuffer;
244 ItemPointerData *tidList;
249 * extract necessary information from tid scan node
251 estate = node->ss.ps.state;
252 direction = estate->es_direction;
253 snapshot = estate->es_snapshot;
254 heapRelation = node->ss.ss_currentRelation;
255 slot = node->ss.ss_ScanTupleSlot;
256 scanrelid = ((TidScan *) node->ss.ps.plan)->scan.scanrelid;
259 * Check if we are evaluating PlanQual for tuple of this relation.
260 * Additional checking is not good, but no other way for now. We could
261 * introduce new nodes for this case and handle TidScan --> NewNode
262 * switching in Init/ReScan plan...
264 if (estate->es_evTuple != NULL &&
265 estate->es_evTuple[scanrelid - 1] != NULL)
267 if (estate->es_evTupleNull[scanrelid - 1])
268 return ExecClearTuple(slot);
271 * XXX shouldn't we check here to make sure tuple matches TID list? In
272 * runtime-key case this is not certain, is it?
275 ExecStoreTuple(estate->es_evTuple[scanrelid - 1],
276 slot, InvalidBuffer, false);
278 /* Flag for the next call that no more tuples */
279 estate->es_evTupleNull[scanrelid - 1] = true;
284 * First time through, compute the list of TIDs to be visited
286 if (node->tss_TidList == NULL)
289 tidList = node->tss_TidList;
290 numTids = node->tss_NumTids;
292 tuple = &(node->tss_htup);
295 * Initialize or advance scan position, depending on direction.
297 bBackward = ScanDirectionIsBackward(direction);
300 if (node->tss_TidPtr < 0)
302 /* initialize for backward scan */
303 node->tss_TidPtr = numTids - 1;
310 if (node->tss_TidPtr < 0)
312 /* initialize for forward scan */
313 node->tss_TidPtr = 0;
319 while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
321 tuple->t_self = tidList[node->tss_TidPtr];
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);
358 /* ----------------------------------------------------------------
361 * Scans the relation using tids and returns
362 * the next qualifying tuple in the direction specified.
363 * It calls ExecScan() and passes it the access methods which returns
364 * the next tuple using the tids.
367 * -- the "cursor" maintained by the AMI is positioned at the tuple
368 * returned previously.
371 * -- the relation indicated is opened for scanning so that the
372 * "cursor" is positioned before the first qualifying tuple.
374 * ----------------------------------------------------------------
377 ExecTidScan(TidScanState *node)
380 * use TidNext as access method
382 return ExecScan(&node->ss, (ExecScanAccessMtd) TidNext);
385 /* ----------------------------------------------------------------
386 * ExecTidReScan(node)
387 * ----------------------------------------------------------------
390 ExecTidReScan(TidScanState *node, ExprContext *exprCtxt)
395 estate = node->ss.ps.state;
396 scanrelid = ((TidScan *) node->ss.ps.plan)->scan.scanrelid;
398 node->ss.ps.ps_TupFromTlist = false;
400 /* If we are being passed an outer tuple, save it for runtime key calc */
401 if (exprCtxt != NULL)
402 node->ss.ps.ps_ExprContext->ecxt_outertuple =
403 exprCtxt->ecxt_outertuple;
405 /* If this is re-scanning of PlanQual ... */
406 if (estate->es_evTuple != NULL &&
407 estate->es_evTuple[scanrelid - 1] != NULL)
409 estate->es_evTupleNull[scanrelid - 1] = false;
413 if (node->tss_TidList)
414 pfree(node->tss_TidList);
415 node->tss_TidList = NULL;
416 node->tss_NumTids = 0;
417 node->tss_TidPtr = -1;
420 /* ----------------------------------------------------------------
423 * Releases any storage allocated through C routines.
425 * ----------------------------------------------------------------
428 ExecEndTidScan(TidScanState *node)
431 * Free the exprcontext
433 ExecFreeExprContext(&node->ss.ps);
436 * clear out tuple table slots
438 ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
439 ExecClearTuple(node->ss.ss_ScanTupleSlot);
442 * close the heap relation.
444 ExecCloseScanRelation(node->ss.ss_currentRelation);
447 /* ----------------------------------------------------------------
450 * Marks scan position by marking the current tid.
452 * ----------------------------------------------------------------
455 ExecTidMarkPos(TidScanState *node)
457 node->tss_MarkTidPtr = node->tss_TidPtr;
460 /* ----------------------------------------------------------------
463 * Restores scan position by restoring the current tid.
466 * XXX Assumes previously marked scan position belongs to current tid
467 * ----------------------------------------------------------------
470 ExecTidRestrPos(TidScanState *node)
472 node->tss_TidPtr = node->tss_MarkTidPtr;
475 /* ----------------------------------------------------------------
478 * Initializes the tid scan's state information, creates
479 * scan keys, and opens the base and tid relations.
482 * node: TidNode node produced by the planner.
483 * estate: the execution state initialized in InitPlan.
484 * ----------------------------------------------------------------
487 ExecInitTidScan(TidScan *node, EState *estate, int eflags)
489 TidScanState *tidstate;
490 Relation currentRelation;
493 * create state structure
495 tidstate = makeNode(TidScanState);
496 tidstate->ss.ps.plan = (Plan *) node;
497 tidstate->ss.ps.state = estate;
500 * Miscellaneous initialization
502 * create expression context for node
504 ExecAssignExprContext(estate, &tidstate->ss.ps);
506 tidstate->ss.ps.ps_TupFromTlist = false;
509 * initialize child expressions
511 tidstate->ss.ps.targetlist = (List *)
512 ExecInitExpr((Expr *) node->scan.plan.targetlist,
513 (PlanState *) tidstate);
514 tidstate->ss.ps.qual = (List *)
515 ExecInitExpr((Expr *) node->scan.plan.qual,
516 (PlanState *) tidstate);
518 tidstate->tss_tidquals = (List *)
519 ExecInitExpr((Expr *) node->tidquals,
520 (PlanState *) tidstate);
522 #define TIDSCAN_NSLOTS 2
525 * tuple table initialization
527 ExecInitResultTupleSlot(estate, &tidstate->ss.ps);
528 ExecInitScanTupleSlot(estate, &tidstate->ss);
531 * mark tid list as not computed yet
533 tidstate->tss_TidList = NULL;
534 tidstate->tss_NumTids = 0;
535 tidstate->tss_TidPtr = -1;
538 * open the base relation and acquire appropriate lock on it.
540 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
542 tidstate->ss.ss_currentRelation = currentRelation;
543 tidstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
546 * get the scan type from the relation descriptor.
548 ExecAssignScanType(&tidstate->ss, RelationGetDescr(currentRelation));
551 * Initialize result tuple type and projection info.
553 ExecAssignResultTypeFromTL(&tidstate->ss.ps);
554 ExecAssignScanProjectionInfo(&tidstate->ss);
563 ExecCountSlotsTidScan(TidScan *node)
565 return ExecCountSlotsNode(outerPlan((Plan *) node)) +
566 ExecCountSlotsNode(innerPlan((Plan *) node)) + TIDSCAN_NSLOTS;