]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeTidscan.c
Fix nodeTidscan.c to not trigger an error if the block number portion of
[postgresql] / src / backend / executor / nodeTidscan.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeTidscan.c
4  *        Routines to support direct tid scans of relations
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/executor/nodeTidscan.c,v 1.59 2008/04/30 23:28:32 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  * INTERFACE ROUTINES
17  *
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.
24  */
25 #include "postgres.h"
26
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"
33
34
35 #define IsCTIDVar(node)  \
36         ((node) != NULL && \
37          IsA((node), Var) && \
38          ((Var *) (node))->varattno == SelfItemPointerAttributeNumber && \
39          ((Var *) (node))->varlevelsup == 0)
40
41 static void TidListCreate(TidScanState *tidstate);
42 static int      itemptr_comparator(const void *a, const void *b);
43 static TupleTableSlot *TidNext(TidScanState *node);
44
45
46 /*
47  * Compute the list of TIDs to be visited, by evaluating the expressions
48  * for them.
49  *
50  * (The result is actually an array, not a list.)
51  */
52 static void
53 TidListCreate(TidScanState *tidstate)
54 {
55         List       *evalList = tidstate->tss_tidquals;
56         ExprContext *econtext = tidstate->ss.ps.ps_ExprContext;
57         BlockNumber nblocks;
58         ItemPointerData *tidList;
59         int                     numAllocTids;
60         int                     numTids;
61         ListCell   *l;
62
63         /*
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
67          * intend to visit.)
68          */
69         nblocks = RelationGetNumberOfBlocks(tidstate->ss.ss_currentRelation);
70
71         /*
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.
75          */
76         numAllocTids = list_length(evalList);
77         tidList = (ItemPointerData *)
78                 palloc(numAllocTids * sizeof(ItemPointerData));
79         numTids = 0;
80         tidstate->tss_isCurrentOf = false;
81
82         foreach(l, evalList)
83         {
84                 ExprState  *exstate = (ExprState *) lfirst(l);
85                 Expr       *expr = exstate->expr;
86                 ItemPointer itemptr;
87                 bool            isNull;
88
89                 if (is_opclause(expr))
90                 {
91                         FuncExprState *fexstate = (FuncExprState *) exstate;
92                         Node       *arg1;
93                         Node       *arg2;
94
95                         arg1 = get_leftop(expr);
96                         arg2 = get_rightop(expr);
97                         if (IsCTIDVar(arg1))
98                                 exstate = (ExprState *) lsecond(fexstate->args);
99                         else if (IsCTIDVar(arg2))
100                                 exstate = (ExprState *) linitial(fexstate->args);
101                         else
102                                 elog(ERROR, "could not identify CTID variable");
103
104                         itemptr = (ItemPointer)
105                                 DatumGetPointer(ExecEvalExprSwitchContext(exstate,
106                                                                                                                   econtext,
107                                                                                                                   &isNull,
108                                                                                                                   NULL));
109                         if (!isNull &&
110                                 ItemPointerIsValid(itemptr) &&
111                                 ItemPointerGetBlockNumber(itemptr) < nblocks)
112                         {
113                                 if (numTids >= numAllocTids)
114                                 {
115                                         numAllocTids *= 2;
116                                         tidList = (ItemPointerData *)
117                                                 repalloc(tidList,
118                                                                  numAllocTids * sizeof(ItemPointerData));
119                                 }
120                                 tidList[numTids++] = *itemptr;
121                         }
122                 }
123                 else if (expr && IsA(expr, ScalarArrayOpExpr))
124                 {
125                         ScalarArrayOpExprState *saexstate = (ScalarArrayOpExprState *) exstate;
126                         Datum           arraydatum;
127                         ArrayType  *itemarray;
128                         Datum      *ipdatums;
129                         bool       *ipnulls;
130                         int                     ndatums;
131                         int                     i;
132
133                         exstate = (ExprState *) lsecond(saexstate->fxprstate.args);
134                         arraydatum = ExecEvalExprSwitchContext(exstate,
135                                                                                                    econtext,
136                                                                                                    &isNull,
137                                                                                                    NULL);
138                         if (isNull)
139                                 continue;
140                         itemarray = DatumGetArrayTypeP(arraydatum);
141                         deconstruct_array(itemarray,
142                                                           TIDOID, SizeOfIptrData, false, 's',
143                                                           &ipdatums, &ipnulls, &ndatums);
144                         if (numTids + ndatums > numAllocTids)
145                         {
146                                 numAllocTids = numTids + ndatums;
147                                 tidList = (ItemPointerData *)
148                                         repalloc(tidList,
149                                                          numAllocTids * sizeof(ItemPointerData));
150                         }
151                         for (i = 0; i < ndatums; i++)
152                         {
153                                 if (!ipnulls[i])
154                                 {
155                                         itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
156                                         if (ItemPointerIsValid(itemptr) &&
157                                                 ItemPointerGetBlockNumber(itemptr) < nblocks)
158                                                 tidList[numTids++] = *itemptr;
159                                 }
160                         }
161                         pfree(ipdatums);
162                         pfree(ipnulls);
163                 }
164                 else if (expr && IsA(expr, CurrentOfExpr))
165                 {
166                         CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
167                         ItemPointerData cursor_tid;
168
169                         if (execCurrentOf(cexpr, econtext,
170                                                    RelationGetRelid(tidstate->ss.ss_currentRelation),
171                                                           &cursor_tid))
172                         {
173                                 if (numTids >= numAllocTids)
174                                 {
175                                         numAllocTids *= 2;
176                                         tidList = (ItemPointerData *)
177                                                 repalloc(tidList,
178                                                                  numAllocTids * sizeof(ItemPointerData));
179                                 }
180                                 tidList[numTids++] = cursor_tid;
181                                 tidstate->tss_isCurrentOf = true;
182                         }
183                 }
184                 else
185                         elog(ERROR, "could not identify CTID expression");
186         }
187
188         /*
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.
193          */
194         if (numTids > 1)
195         {
196                 int                     lastTid;
197                 int                     i;
198
199                 /* CurrentOfExpr could never appear OR'd with something else */
200                 Assert(!tidstate->tss_isCurrentOf);
201
202                 qsort((void *) tidList, numTids, sizeof(ItemPointerData),
203                           itemptr_comparator);
204                 lastTid = 0;
205                 for (i = 1; i < numTids; i++)
206                 {
207                         if (!ItemPointerEquals(&tidList[lastTid], &tidList[i]))
208                                 tidList[++lastTid] = tidList[i];
209                 }
210                 numTids = lastTid + 1;
211         }
212
213         tidstate->tss_TidList = tidList;
214         tidstate->tss_NumTids = numTids;
215         tidstate->tss_TidPtr = -1;
216 }
217
218 /*
219  * qsort comparator for ItemPointerData items
220  */
221 static int
222 itemptr_comparator(const void *a, const void *b)
223 {
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);
230
231         if (ba < bb)
232                 return -1;
233         if (ba > bb)
234                 return 1;
235         if (oa < ob)
236                 return -1;
237         if (oa > ob)
238                 return 1;
239         return 0;
240 }
241
242 /* ----------------------------------------------------------------
243  *              TidNext
244  *
245  *              Retrieve a tuple from the TidScan node's currentRelation
246  *              using the tids in the TidScanState information.
247  *
248  * ----------------------------------------------------------------
249  */
250 static TupleTableSlot *
251 TidNext(TidScanState *node)
252 {
253         EState     *estate;
254         ScanDirection direction;
255         Snapshot        snapshot;
256         Relation        heapRelation;
257         HeapTuple       tuple;
258         TupleTableSlot *slot;
259         Index           scanrelid;
260         Buffer          buffer = InvalidBuffer;
261         ItemPointerData *tidList;
262         int                     numTids;
263         bool            bBackward;
264
265         /*
266          * extract necessary information from tid scan node
267          */
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;
274
275         /*
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...
280          */
281         if (estate->es_evTuple != NULL &&
282                 estate->es_evTuple[scanrelid - 1] != NULL)
283         {
284                 if (estate->es_evTupleNull[scanrelid - 1])
285                         return ExecClearTuple(slot);
286
287                 /*
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 ...
291                  */
292
293                 ExecStoreTuple(estate->es_evTuple[scanrelid - 1],
294                                            slot, InvalidBuffer, false);
295
296                 /* Flag for the next call that no more tuples */
297                 estate->es_evTupleNull[scanrelid - 1] = true;
298                 return slot;
299         }
300
301         /*
302          * First time through, compute the list of TIDs to be visited
303          */
304         if (node->tss_TidList == NULL)
305                 TidListCreate(node);
306
307         tidList = node->tss_TidList;
308         numTids = node->tss_NumTids;
309
310         tuple = &(node->tss_htup);
311
312         /*
313          * Initialize or advance scan position, depending on direction.
314          */
315         bBackward = ScanDirectionIsBackward(direction);
316         if (bBackward)
317         {
318                 if (node->tss_TidPtr < 0)
319                 {
320                         /* initialize for backward scan */
321                         node->tss_TidPtr = numTids - 1;
322                 }
323                 else
324                         node->tss_TidPtr--;
325         }
326         else
327         {
328                 if (node->tss_TidPtr < 0)
329                 {
330                         /* initialize for forward scan */
331                         node->tss_TidPtr = 0;
332                 }
333                 else
334                         node->tss_TidPtr++;
335         }
336
337         while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
338         {
339                 tuple->t_self = tidList[node->tss_TidPtr];
340
341                 /*
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.
345                  */
346                 if (node->tss_isCurrentOf)
347                         heap_get_latest_tid(heapRelation, snapshot, &tuple->t_self);
348
349                 if (heap_fetch(heapRelation, snapshot, tuple, &buffer, false, NULL))
350                 {
351                         /*
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.
357                          */
358                         ExecStoreTuple(tuple,           /* tuple to store */
359                                                    slot,        /* slot to store in */
360                                                    buffer,              /* buffer associated with tuple  */
361                                                    false);              /* don't pfree */
362
363                         /*
364                          * At this point we have an extra pin on the buffer, because
365                          * ExecStoreTuple incremented the pin count. Drop our local pin.
366                          */
367                         ReleaseBuffer(buffer);
368
369                         return slot;
370                 }
371                 /* Bad TID or failed snapshot qual; try next */
372                 if (bBackward)
373                         node->tss_TidPtr--;
374                 else
375                         node->tss_TidPtr++;
376         }
377
378         /*
379          * if we get here it means the tid scan failed so we are at the end of the
380          * scan..
381          */
382         return ExecClearTuple(slot);
383 }
384
385 /* ----------------------------------------------------------------
386  *              ExecTidScan(node)
387  *
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.
392  *
393  *              Conditions:
394  *                -- the "cursor" maintained by the AMI is positioned at the tuple
395  *                       returned previously.
396  *
397  *              Initial States:
398  *                -- the relation indicated is opened for scanning so that the
399  *                       "cursor" is positioned before the first qualifying tuple.
400  *                -- tidPtr is -1.
401  * ----------------------------------------------------------------
402  */
403 TupleTableSlot *
404 ExecTidScan(TidScanState *node)
405 {
406         /*
407          * use TidNext as access method
408          */
409         return ExecScan(&node->ss, (ExecScanAccessMtd) TidNext);
410 }
411
412 /* ----------------------------------------------------------------
413  *              ExecTidReScan(node)
414  * ----------------------------------------------------------------
415  */
416 void
417 ExecTidReScan(TidScanState *node, ExprContext *exprCtxt)
418 {
419         EState     *estate;
420         Index           scanrelid;
421
422         estate = node->ss.ps.state;
423         scanrelid = ((TidScan *) node->ss.ps.plan)->scan.scanrelid;
424
425         node->ss.ps.ps_TupFromTlist = false;
426
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;
431
432         /* If this is re-scanning of PlanQual ... */
433         if (estate->es_evTuple != NULL &&
434                 estate->es_evTuple[scanrelid - 1] != NULL)
435         {
436                 estate->es_evTupleNull[scanrelid - 1] = false;
437                 return;
438         }
439
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;
445 }
446
447 /* ----------------------------------------------------------------
448  *              ExecEndTidScan
449  *
450  *              Releases any storage allocated through C routines.
451  *              Returns nothing.
452  * ----------------------------------------------------------------
453  */
454 void
455 ExecEndTidScan(TidScanState *node)
456 {
457         /*
458          * Free the exprcontext
459          */
460         ExecFreeExprContext(&node->ss.ps);
461
462         /*
463          * clear out tuple table slots
464          */
465         ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
466         ExecClearTuple(node->ss.ss_ScanTupleSlot);
467
468         /*
469          * close the heap relation.
470          */
471         ExecCloseScanRelation(node->ss.ss_currentRelation);
472 }
473
474 /* ----------------------------------------------------------------
475  *              ExecTidMarkPos
476  *
477  *              Marks scan position by marking the current tid.
478  *              Returns nothing.
479  * ----------------------------------------------------------------
480  */
481 void
482 ExecTidMarkPos(TidScanState *node)
483 {
484         node->tss_MarkTidPtr = node->tss_TidPtr;
485 }
486
487 /* ----------------------------------------------------------------
488  *              ExecTidRestrPos
489  *
490  *              Restores scan position by restoring the current tid.
491  *              Returns nothing.
492  *
493  *              XXX Assumes previously marked scan position belongs to current tid
494  * ----------------------------------------------------------------
495  */
496 void
497 ExecTidRestrPos(TidScanState *node)
498 {
499         node->tss_TidPtr = node->tss_MarkTidPtr;
500 }
501
502 /* ----------------------------------------------------------------
503  *              ExecInitTidScan
504  *
505  *              Initializes the tid scan's state information, creates
506  *              scan keys, and opens the base and tid relations.
507  *
508  *              Parameters:
509  *                node: TidNode node produced by the planner.
510  *                estate: the execution state initialized in InitPlan.
511  * ----------------------------------------------------------------
512  */
513 TidScanState *
514 ExecInitTidScan(TidScan *node, EState *estate, int eflags)
515 {
516         TidScanState *tidstate;
517         Relation        currentRelation;
518
519         /*
520          * create state structure
521          */
522         tidstate = makeNode(TidScanState);
523         tidstate->ss.ps.plan = (Plan *) node;
524         tidstate->ss.ps.state = estate;
525
526         /*
527          * Miscellaneous initialization
528          *
529          * create expression context for node
530          */
531         ExecAssignExprContext(estate, &tidstate->ss.ps);
532
533         tidstate->ss.ps.ps_TupFromTlist = false;
534
535         /*
536          * initialize child expressions
537          */
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);
544
545         tidstate->tss_tidquals = (List *)
546                 ExecInitExpr((Expr *) node->tidquals,
547                                          (PlanState *) tidstate);
548
549 #define TIDSCAN_NSLOTS 2
550
551         /*
552          * tuple table initialization
553          */
554         ExecInitResultTupleSlot(estate, &tidstate->ss.ps);
555         ExecInitScanTupleSlot(estate, &tidstate->ss);
556
557         /*
558          * mark tid list as not computed yet
559          */
560         tidstate->tss_TidList = NULL;
561         tidstate->tss_NumTids = 0;
562         tidstate->tss_TidPtr = -1;
563
564         /*
565          * open the base relation and acquire appropriate lock on it.
566          */
567         currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
568
569         tidstate->ss.ss_currentRelation = currentRelation;
570         tidstate->ss.ss_currentScanDesc = NULL;         /* no heap scan here */
571
572         /*
573          * get the scan type from the relation descriptor.
574          */
575         ExecAssignScanType(&tidstate->ss, RelationGetDescr(currentRelation));
576
577         /*
578          * Initialize result tuple type and projection info.
579          */
580         ExecAssignResultTypeFromTL(&tidstate->ss.ps);
581         ExecAssignScanProjectionInfo(&tidstate->ss);
582
583         /*
584          * all done.
585          */
586         return tidstate;
587 }
588
589 int
590 ExecCountSlotsTidScan(TidScan *node)
591 {
592         return ExecCountSlotsNode(outerPlan((Plan *) node)) +
593                 ExecCountSlotsNode(innerPlan((Plan *) node)) + TIDSCAN_NSLOTS;
594 }