]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeTidscan.c
Support UPDATE/DELETE WHERE CURRENT OF cursor_name, per SQL standard.
[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-2007, 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.54 2007/06/11 01:16:22 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         ItemPointerData *tidList;
58         int                     numAllocTids;
59         int                     numTids;
60         ListCell   *l;
61
62         /*
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.
66          */
67         numAllocTids = list_length(evalList);
68         tidList = (ItemPointerData *)
69                 palloc(numAllocTids * sizeof(ItemPointerData));
70         numTids = 0;
71
72         foreach(l, evalList)
73         {
74                 ExprState  *exstate = (ExprState *) lfirst(l);
75                 Expr       *expr = exstate->expr;
76                 ItemPointer itemptr;
77                 bool            isNull;
78
79                 if (is_opclause(expr))
80                 {
81                         FuncExprState *fexstate = (FuncExprState *) exstate;
82                         Node       *arg1;
83                         Node       *arg2;
84
85                         arg1 = get_leftop(expr);
86                         arg2 = get_rightop(expr);
87                         if (IsCTIDVar(arg1))
88                                 exstate = (ExprState *) lsecond(fexstate->args);
89                         else if (IsCTIDVar(arg2))
90                                 exstate = (ExprState *) linitial(fexstate->args);
91                         else
92                                 elog(ERROR, "could not identify CTID variable");
93
94                         itemptr = (ItemPointer)
95                                 DatumGetPointer(ExecEvalExprSwitchContext(exstate,
96                                                                                                                   econtext,
97                                                                                                                   &isNull,
98                                                                                                                   NULL));
99                         if (!isNull && ItemPointerIsValid(itemptr))
100                         {
101                                 if (numTids >= numAllocTids)
102                                 {
103                                         numAllocTids *= 2;
104                                         tidList = (ItemPointerData *)
105                                                 repalloc(tidList,
106                                                                  numAllocTids * sizeof(ItemPointerData));
107                                 }
108                                 tidList[numTids++] = *itemptr;
109                         }
110                 }
111                 else if (expr && IsA(expr, ScalarArrayOpExpr))
112                 {
113                         ScalarArrayOpExprState *saexstate = (ScalarArrayOpExprState *) exstate;
114                         Datum           arraydatum;
115                         ArrayType  *itemarray;
116                         Datum      *ipdatums;
117                         bool       *ipnulls;
118                         int                     ndatums;
119                         int                     i;
120
121                         exstate = (ExprState *) lsecond(saexstate->fxprstate.args);
122                         arraydatum = ExecEvalExprSwitchContext(exstate,
123                                                                                                    econtext,
124                                                                                                    &isNull,
125                                                                                                    NULL);
126                         if (isNull)
127                                 continue;
128                         itemarray = DatumGetArrayTypeP(arraydatum);
129                         deconstruct_array(itemarray,
130                                                           TIDOID, SizeOfIptrData, false, 's',
131                                                           &ipdatums, &ipnulls, &ndatums);
132                         if (numTids + ndatums > numAllocTids)
133                         {
134                                 numAllocTids = numTids + ndatums;
135                                 tidList = (ItemPointerData *)
136                                         repalloc(tidList,
137                                                          numAllocTids * sizeof(ItemPointerData));
138                         }
139                         for (i = 0; i < ndatums; i++)
140                         {
141                                 if (!ipnulls[i])
142                                 {
143                                         itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
144                                         if (ItemPointerIsValid(itemptr))
145                                                 tidList[numTids++] = *itemptr;
146                                 }
147                         }
148                         pfree(ipdatums);
149                         pfree(ipnulls);
150                 }
151                 else if (expr && IsA(expr, CurrentOfExpr))
152                 {
153                         CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
154                         ItemPointerData cursor_tid;
155
156                         if (execCurrentOf(cexpr->cursor_name,
157                                                   RelationGetRelid(tidstate->ss.ss_currentRelation),
158                                                           &cursor_tid))
159                         {
160                                 if (numTids >= numAllocTids)
161                                 {
162                                         numAllocTids *= 2;
163                                         tidList = (ItemPointerData *)
164                                                 repalloc(tidList,
165                                                                  numAllocTids * sizeof(ItemPointerData));
166                                 }
167                                 tidList[numTids++] = cursor_tid;
168                         }
169                 }
170                 else
171                         elog(ERROR, "could not identify CTID expression");
172         }
173
174         /*
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.
179          */
180         if (numTids > 1)
181         {
182                 int                     lastTid;
183                 int                     i;
184
185                 qsort((void *) tidList, numTids, sizeof(ItemPointerData),
186                           itemptr_comparator);
187                 lastTid = 0;
188                 for (i = 1; i < numTids; i++)
189                 {
190                         if (!ItemPointerEquals(&tidList[lastTid], &tidList[i]))
191                                 tidList[++lastTid] = tidList[i];
192                 }
193                 numTids = lastTid + 1;
194         }
195
196         tidstate->tss_TidList = tidList;
197         tidstate->tss_NumTids = numTids;
198         tidstate->tss_TidPtr = -1;
199 }
200
201 /*
202  * qsort comparator for ItemPointerData items
203  */
204 static int
205 itemptr_comparator(const void *a, const void *b)
206 {
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);
213
214         if (ba < bb)
215                 return -1;
216         if (ba > bb)
217                 return 1;
218         if (oa < ob)
219                 return -1;
220         if (oa > ob)
221                 return 1;
222         return 0;
223 }
224
225 /* ----------------------------------------------------------------
226  *              TidNext
227  *
228  *              Retrieve a tuple from the TidScan node's currentRelation
229  *              using the tids in the TidScanState information.
230  *
231  * ----------------------------------------------------------------
232  */
233 static TupleTableSlot *
234 TidNext(TidScanState *node)
235 {
236         EState     *estate;
237         ScanDirection direction;
238         Snapshot        snapshot;
239         Relation        heapRelation;
240         HeapTuple       tuple;
241         TupleTableSlot *slot;
242         Index           scanrelid;
243         Buffer          buffer = InvalidBuffer;
244         ItemPointerData *tidList;
245         int                     numTids;
246         bool            bBackward;
247
248         /*
249          * extract necessary information from tid scan node
250          */
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;
257
258         /*
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...
263          */
264         if (estate->es_evTuple != NULL &&
265                 estate->es_evTuple[scanrelid - 1] != NULL)
266         {
267                 if (estate->es_evTupleNull[scanrelid - 1])
268                         return ExecClearTuple(slot);
269
270                 /*
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?
273                  */
274
275                 ExecStoreTuple(estate->es_evTuple[scanrelid - 1],
276                                            slot, InvalidBuffer, false);
277
278                 /* Flag for the next call that no more tuples */
279                 estate->es_evTupleNull[scanrelid - 1] = true;
280                 return slot;
281         }
282
283         /*
284          * First time through, compute the list of TIDs to be visited
285          */
286         if (node->tss_TidList == NULL)
287                 TidListCreate(node);
288
289         tidList = node->tss_TidList;
290         numTids = node->tss_NumTids;
291
292         tuple = &(node->tss_htup);
293
294         /*
295          * Initialize or advance scan position, depending on direction.
296          */
297         bBackward = ScanDirectionIsBackward(direction);
298         if (bBackward)
299         {
300                 if (node->tss_TidPtr < 0)
301                 {
302                         /* initialize for backward scan */
303                         node->tss_TidPtr = numTids - 1;
304                 }
305                 else
306                         node->tss_TidPtr--;
307         }
308         else
309         {
310                 if (node->tss_TidPtr < 0)
311                 {
312                         /* initialize for forward scan */
313                         node->tss_TidPtr = 0;
314                 }
315                 else
316                         node->tss_TidPtr++;
317         }
318
319         while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
320         {
321                 tuple->t_self = tidList[node->tss_TidPtr];
322                 if (heap_fetch(heapRelation, snapshot, tuple, &buffer, false, NULL))
323                 {
324                         /*
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.
330                          */
331                         ExecStoreTuple(tuple,           /* tuple to store */
332                                                    slot,        /* slot to store in */
333                                                    buffer,              /* buffer associated with tuple  */
334                                                    false);              /* don't pfree */
335
336                         /*
337                          * At this point we have an extra pin on the buffer, because
338                          * ExecStoreTuple incremented the pin count. Drop our local pin.
339                          */
340                         ReleaseBuffer(buffer);
341
342                         return slot;
343                 }
344                 /* Bad TID or failed snapshot qual; try next */
345                 if (bBackward)
346                         node->tss_TidPtr--;
347                 else
348                         node->tss_TidPtr++;
349         }
350
351         /*
352          * if we get here it means the tid scan failed so we are at the end of the
353          * scan..
354          */
355         return ExecClearTuple(slot);
356 }
357
358 /* ----------------------------------------------------------------
359  *              ExecTidScan(node)
360  *
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.
365  *
366  *              Conditions:
367  *                -- the "cursor" maintained by the AMI is positioned at the tuple
368  *                       returned previously.
369  *
370  *              Initial States:
371  *                -- the relation indicated is opened for scanning so that the
372  *                       "cursor" is positioned before the first qualifying tuple.
373  *                -- tidPtr is -1.
374  * ----------------------------------------------------------------
375  */
376 TupleTableSlot *
377 ExecTidScan(TidScanState *node)
378 {
379         /*
380          * use TidNext as access method
381          */
382         return ExecScan(&node->ss, (ExecScanAccessMtd) TidNext);
383 }
384
385 /* ----------------------------------------------------------------
386  *              ExecTidReScan(node)
387  * ----------------------------------------------------------------
388  */
389 void
390 ExecTidReScan(TidScanState *node, ExprContext *exprCtxt)
391 {
392         EState     *estate;
393         Index           scanrelid;
394
395         estate = node->ss.ps.state;
396         scanrelid = ((TidScan *) node->ss.ps.plan)->scan.scanrelid;
397
398         node->ss.ps.ps_TupFromTlist = false;
399
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;
404
405         /* If this is re-scanning of PlanQual ... */
406         if (estate->es_evTuple != NULL &&
407                 estate->es_evTuple[scanrelid - 1] != NULL)
408         {
409                 estate->es_evTupleNull[scanrelid - 1] = false;
410                 return;
411         }
412
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;
418 }
419
420 /* ----------------------------------------------------------------
421  *              ExecEndTidScan
422  *
423  *              Releases any storage allocated through C routines.
424  *              Returns nothing.
425  * ----------------------------------------------------------------
426  */
427 void
428 ExecEndTidScan(TidScanState *node)
429 {
430         /*
431          * Free the exprcontext
432          */
433         ExecFreeExprContext(&node->ss.ps);
434
435         /*
436          * clear out tuple table slots
437          */
438         ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
439         ExecClearTuple(node->ss.ss_ScanTupleSlot);
440
441         /*
442          * close the heap relation.
443          */
444         ExecCloseScanRelation(node->ss.ss_currentRelation);
445 }
446
447 /* ----------------------------------------------------------------
448  *              ExecTidMarkPos
449  *
450  *              Marks scan position by marking the current tid.
451  *              Returns nothing.
452  * ----------------------------------------------------------------
453  */
454 void
455 ExecTidMarkPos(TidScanState *node)
456 {
457         node->tss_MarkTidPtr = node->tss_TidPtr;
458 }
459
460 /* ----------------------------------------------------------------
461  *              ExecTidRestrPos
462  *
463  *              Restores scan position by restoring the current tid.
464  *              Returns nothing.
465  *
466  *              XXX Assumes previously marked scan position belongs to current tid
467  * ----------------------------------------------------------------
468  */
469 void
470 ExecTidRestrPos(TidScanState *node)
471 {
472         node->tss_TidPtr = node->tss_MarkTidPtr;
473 }
474
475 /* ----------------------------------------------------------------
476  *              ExecInitTidScan
477  *
478  *              Initializes the tid scan's state information, creates
479  *              scan keys, and opens the base and tid relations.
480  *
481  *              Parameters:
482  *                node: TidNode node produced by the planner.
483  *                estate: the execution state initialized in InitPlan.
484  * ----------------------------------------------------------------
485  */
486 TidScanState *
487 ExecInitTidScan(TidScan *node, EState *estate, int eflags)
488 {
489         TidScanState *tidstate;
490         Relation        currentRelation;
491
492         /*
493          * create state structure
494          */
495         tidstate = makeNode(TidScanState);
496         tidstate->ss.ps.plan = (Plan *) node;
497         tidstate->ss.ps.state = estate;
498
499         /*
500          * Miscellaneous initialization
501          *
502          * create expression context for node
503          */
504         ExecAssignExprContext(estate, &tidstate->ss.ps);
505
506         tidstate->ss.ps.ps_TupFromTlist = false;
507
508         /*
509          * initialize child expressions
510          */
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);
517
518         tidstate->tss_tidquals = (List *)
519                 ExecInitExpr((Expr *) node->tidquals,
520                                          (PlanState *) tidstate);
521
522 #define TIDSCAN_NSLOTS 2
523
524         /*
525          * tuple table initialization
526          */
527         ExecInitResultTupleSlot(estate, &tidstate->ss.ps);
528         ExecInitScanTupleSlot(estate, &tidstate->ss);
529
530         /*
531          * mark tid list as not computed yet
532          */
533         tidstate->tss_TidList = NULL;
534         tidstate->tss_NumTids = 0;
535         tidstate->tss_TidPtr = -1;
536
537         /*
538          * open the base relation and acquire appropriate lock on it.
539          */
540         currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
541
542         tidstate->ss.ss_currentRelation = currentRelation;
543         tidstate->ss.ss_currentScanDesc = NULL;         /* no heap scan here */
544
545         /*
546          * get the scan type from the relation descriptor.
547          */
548         ExecAssignScanType(&tidstate->ss, RelationGetDescr(currentRelation));
549
550         /*
551          * Initialize result tuple type and projection info.
552          */
553         ExecAssignResultTypeFromTL(&tidstate->ss.ps);
554         ExecAssignScanProjectionInfo(&tidstate->ss);
555
556         /*
557          * all done.
558          */
559         return tidstate;
560 }
561
562 int
563 ExecCountSlotsTidScan(TidScan *node)
564 {
565         return ExecCountSlotsNode(outerPlan((Plan *) node)) +
566                 ExecCountSlotsNode(innerPlan((Plan *) node)) + TIDSCAN_NSLOTS;
567 }