1 /*-------------------------------------------------------------------------
4 * miscellaneous executor access method routines
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $Id: execAmi.c,v 1.55 2000/11/08 22:09:57 tgl Exp $
11 *-------------------------------------------------------------------------
16 * ExecOpenScanR \ / amopen
17 * ExecBeginScan \ / ambeginscan
18 * ExecCloseR \ / amclose
19 * ExecInsert \ executor interface / aminsert
20 * ExecReScanR / to access methods \ amrescan
21 * ExecMarkPos / \ ammarkpos
22 * ExecRestrPos / \ amrestpos
28 #include "access/genam.h"
29 #include "access/heapam.h"
30 #include "catalog/heap.h"
31 #include "executor/execdebug.h"
32 #include "executor/nodeAgg.h"
33 #include "executor/nodeAppend.h"
34 #include "executor/nodeGroup.h"
35 #include "executor/nodeGroup.h"
36 #include "executor/nodeHash.h"
37 #include "executor/nodeHashjoin.h"
38 #include "executor/nodeIndexscan.h"
39 #include "executor/nodeTidscan.h"
40 #include "executor/nodeLimit.h"
41 #include "executor/nodeMaterial.h"
42 #include "executor/nodeMergejoin.h"
43 #include "executor/nodeNestloop.h"
44 #include "executor/nodeResult.h"
45 #include "executor/nodeSeqscan.h"
46 #include "executor/nodeSetOp.h"
47 #include "executor/nodeSort.h"
48 #include "executor/nodeSubplan.h"
49 #include "executor/nodeSubqueryscan.h"
50 #include "executor/nodeUnique.h"
52 static Pointer ExecBeginScan(Relation relation, int nkeys, ScanKey skeys,
53 bool isindex, ScanDirection dir, Snapshot snapshot);
55 /* ----------------------------------------------------------------
60 * relation -- relation to be opened and scanned.
61 * nkeys -- number of keys
62 * skeys -- keys to restrict scanning
63 * isindex -- if this is true, the relation is the relid of
64 * an index relation, else it is a heap relation.
65 * Returns the relation as(relDesc scanDesc)
66 * ----------------------------------------------------------------
69 ExecOpenScanR(Oid relOid,
75 Relation *returnRelation, /* return */
76 Pointer *returnScanDesc) /* return */
82 * note: scanDesc returned by ExecBeginScan can be either
83 * a HeapScanDesc or an IndexScanDesc so for now we
84 * make it a Pointer. There should be a better scan
85 * abstraction someday -cim 9/9/89
90 * Open the relation with the correct call depending
91 * on whether this is a heap relation or an index relation.
93 * For a table, acquire AccessShareLock for the duration of the query
94 * execution. For indexes, acquire no lock here; the index machinery
95 * does its own locks and unlocks. (We rely on having some kind of
96 * lock on the parent table to ensure the index won't go away!)
100 relation = index_open(relOid);
102 relation = heap_open(relOid, AccessShareLock);
104 scanDesc = ExecBeginScan(relation,
111 if (returnRelation != NULL)
112 *returnRelation = relation;
113 if (scanDesc != NULL)
114 *returnScanDesc = scanDesc;
117 /* ----------------------------------------------------------------
120 * beginscans a relation in current direction.
122 * XXX fix parameters to AMbeginscan (and btbeginscan)
123 * currently we need to pass a flag stating whether
124 * or not the scan should begin at an endpoint of
125 * the relation.. Right now we always pass false
127 * ----------------------------------------------------------------
130 ExecBeginScan(Relation relation,
140 * open the appropriate type of scan.
142 * Note: ambeginscan()'s second arg is a boolean indicating
143 * that the scan should be done in reverse.. That is,
144 * if you pass it true, then the scan is backward.
149 scanDesc = (Pointer) index_beginscan(relation,
150 false, /* see above comment */
156 scanDesc = (Pointer) heap_beginscan(relation,
157 ScanDirectionIsBackward(dir),
163 if (scanDesc == NULL)
164 elog(DEBUG, "ExecBeginScan: scanDesc = NULL, heap_beginscan failed.");
169 /* ----------------------------------------------------------------
172 * closes the relation and scan descriptor for a scan node.
173 * Also closes index relations and scans for index scans.
174 * ----------------------------------------------------------------
177 ExecCloseR(Plan *node)
179 CommonScanState *state;
181 HeapScanDesc scanDesc;
184 * get state for node and shut down the heap scan, if any
187 switch (nodeTag(node))
190 state = ((SeqScan *) node)->scanstate;
194 state = ((IndexScan *) node)->scan.scanstate;
198 state = ((TidScan *) node)->scan.scanstate;
202 elog(DEBUG, "ExecCloseR: not a scan node!");
206 relation = state->css_currentRelation;
207 scanDesc = state->css_currentScanDesc;
209 if (scanDesc != NULL)
210 heap_endscan(scanDesc);
213 * if this is an index scan then we have to take care
214 * of the index relations as well.
217 if (IsA(node, IndexScan))
219 IndexScan *iscan = (IndexScan *) node;
220 IndexScanState *indexstate = iscan->indxstate;
222 RelationPtr indexRelationDescs;
223 IndexScanDescPtr indexScanDescs;
226 numIndices = indexstate->iss_NumIndices;
227 indexRelationDescs = indexstate->iss_RelationDescs;
228 indexScanDescs = indexstate->iss_ScanDescs;
230 for (i = 0; i < numIndices; i++)
233 * shut down each of the index scans and
234 * close each of the index relations
237 if (indexScanDescs[i] != NULL)
238 index_endscan(indexScanDescs[i]);
240 if (indexRelationDescs[i] != NULL)
241 index_close(indexRelationDescs[i]);
246 * Finally, close the heap relation.
248 * Currently, we do not release the AccessShareLock acquired by
249 * ExecOpenScanR. This lock should be held till end of transaction.
250 * (There is a faction that considers this too much locking, however.)
252 if (relation != NULL)
253 heap_close(relation, NoLock);
256 /* ----------------------------------------------------------------
259 * XXX this should be extended to cope with all the node types..
261 * takes the new expression context as an argument, so that
262 * index scans needn't have their scan keys updated separately
264 * ----------------------------------------------------------------
267 ExecReScan(Plan *node, ExprContext *exprCtxt, Plan *parent)
270 if (node->chgParam != NULL) /* Wow! */
274 foreach(lst, node->initPlan)
276 Plan *splan = ((SubPlan *) lfirst(lst))->plan;
278 if (splan->extParam != NULL) /* don't care about child
280 SetChangedParamList(splan, node->chgParam);
281 if (splan->chgParam != NULL)
282 ExecReScanSetParamPlan((SubPlan *) lfirst(lst), node);
284 foreach(lst, node->subPlan)
286 Plan *splan = ((SubPlan *) lfirst(lst))->plan;
288 if (splan->extParam != NULL)
289 SetChangedParamList(splan, node->chgParam);
291 /* Well. Now set chgParam for left/right trees. */
292 if (node->lefttree != NULL)
293 SetChangedParamList(node->lefttree, node->chgParam);
294 if (node->righttree != NULL)
295 SetChangedParamList(node->righttree, node->chgParam);
298 switch (nodeTag(node))
301 ExecSeqReScan((SeqScan *) node, exprCtxt, parent);
305 ExecIndexReScan((IndexScan *) node, exprCtxt, parent);
309 ExecTidReScan((TidScan *) node, exprCtxt, parent);
313 ExecSubqueryReScan((SubqueryScan *) node, exprCtxt, parent);
317 ExecMaterialReScan((Material *) node, exprCtxt, parent);
321 ExecReScanNestLoop((NestLoop *) node, exprCtxt, parent);
325 ExecReScanHashJoin((HashJoin *) node, exprCtxt, parent);
329 ExecReScanHash((Hash *) node, exprCtxt, parent);
333 ExecReScanAgg((Agg *) node, exprCtxt, parent);
337 ExecReScanGroup((Group *) node, exprCtxt, parent);
341 ExecReScanResult((Result *) node, exprCtxt, parent);
345 ExecReScanUnique((Unique *) node, exprCtxt, parent);
349 ExecReScanSetOp((SetOp *) node, exprCtxt, parent);
353 ExecReScanLimit((Limit *) node, exprCtxt, parent);
357 ExecReScanSort((Sort *) node, exprCtxt, parent);
361 ExecReScanMergeJoin((MergeJoin *) node, exprCtxt, parent);
365 ExecReScanAppend((Append *) node, exprCtxt, parent);
369 elog(ERROR, "ExecReScan: node type %d not supported",
374 if (node->chgParam != NULL)
376 freeList(node->chgParam);
377 node->chgParam = NULL;
381 /* ----------------------------------------------------------------
384 * XXX this does not do the right thing with indices yet.
385 * ----------------------------------------------------------------
388 ExecReScanR(Relation relDesc, /* LLL relDesc unused */
389 HeapScanDesc scanDesc,
390 ScanDirection direction,
391 int nkeys, /* LLL nkeys unused */
394 if (scanDesc != NULL)
395 heap_rescan(scanDesc, /* scan desc */
396 ScanDirectionIsBackward(direction), /* backward flag */
397 skeys); /* scan keys */
402 /* ----------------------------------------------------------------
405 * Marks the current scan position.
407 * XXX Needs to be extended to include all the node types,
408 * or at least all the ones that can be directly below a mergejoin.
409 * ----------------------------------------------------------------
412 ExecMarkPos(Plan *node)
414 switch (nodeTag(node))
417 ExecSeqMarkPos((SeqScan *) node);
421 ExecIndexMarkPos((IndexScan *) node);
425 ExecMaterialMarkPos((Material *) node);
429 ExecSortMarkPos((Sort *) node);
433 ExecTidMarkPos((TidScan *) node);
437 /* don't make hard error unless caller asks to restore... */
438 elog(DEBUG, "ExecMarkPos: node type %d not supported",
444 /* ----------------------------------------------------------------
447 * restores the scan position previously saved with ExecMarkPos()
449 * XXX Needs to be extended to include all the node types,
450 * or at least all the ones that can be directly below a mergejoin.
451 * ----------------------------------------------------------------
454 ExecRestrPos(Plan *node)
456 switch (nodeTag(node))
459 ExecSeqRestrPos((SeqScan *) node);
463 ExecIndexRestrPos((IndexScan *) node);
467 ExecMaterialRestrPos((Material *) node);
471 ExecSortRestrPos((Sort *) node);
475 elog(ERROR, "ExecRestrPos: node type %d not supported",