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.54 2000/10/26 21:35:15 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 an index into the
66 * Returns the relation as(relDesc scanDesc)
67 * If this structure is changed, need to modify the access macros
68 * defined in execInt.h.
69 * ----------------------------------------------------------------
72 ExecOpenScanR(Oid relOid,
78 Relation *returnRelation, /* return */
79 Pointer *returnScanDesc) /* return */
85 * note: scanDesc returned by ExecBeginScan can be either
86 * a HeapScanDesc or an IndexScanDesc so for now we
87 * make it a Pointer. There should be a better scan
88 * abstraction someday -cim 9/9/89
93 * open the relation with the correct call depending
94 * on whether this is a heap relation or an index relation.
96 * Do not lock the rel here; beginscan will acquire AccessShareLock.
100 relation = index_open(relOid);
102 relation = heap_open(relOid, NoLock);
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,
142 * open the appropriate type of scan.
144 * Note: ambeginscan()'s second arg is a boolean indicating
145 * that the scan should be done in reverse.. That is,
146 * if you pass it true, then the scan is backward.
151 scanDesc = (Pointer) index_beginscan(relation,
152 false, /* see above comment */
158 scanDesc = (Pointer) heap_beginscan(relation,
159 ScanDirectionIsBackward(dir),
165 if (scanDesc == NULL)
166 elog(DEBUG, "ExecBeginScan: scanDesc = NULL, heap_beginscan failed.");
171 /* ----------------------------------------------------------------
174 * closes the relation and scan descriptor for a scan node.
175 * Also closes index relations and scans for index scans.
176 * ----------------------------------------------------------------
179 ExecCloseR(Plan *node)
181 CommonScanState *state;
183 HeapScanDesc scanDesc;
186 * shut down the heap scan and close the heap relation
189 switch (nodeTag(node))
193 state = ((SeqScan *) node)->scanstate;
197 state = ((IndexScan *) node)->scan.scanstate;
201 state = ((TidScan *) node)->scan.scanstate;
205 elog(DEBUG, "ExecCloseR: not a scan node!");
209 relation = state->css_currentRelation;
210 scanDesc = state->css_currentScanDesc;
212 if (scanDesc != NULL)
213 heap_endscan(scanDesc);
216 * endscan released AccessShareLock acquired by beginscan. If we are
217 * holding any stronger locks on the rel, they should be held till end
218 * of xact. Therefore, we need only close the rel and not release
221 if (relation != NULL)
222 heap_close(relation, NoLock);
225 * if this is an index scan then we have to take care
226 * of the index relations as well..
229 if (IsA(node, IndexScan))
231 IndexScan *iscan = (IndexScan *) node;
232 IndexScanState *indexstate = iscan->indxstate;
234 RelationPtr indexRelationDescs;
235 IndexScanDescPtr indexScanDescs;
238 numIndices = indexstate->iss_NumIndices;
239 indexRelationDescs = indexstate->iss_RelationDescs;
240 indexScanDescs = indexstate->iss_ScanDescs;
242 for (i = 0; i < numIndices; i++)
245 * shut down each of the scans and
246 * close each of the index relations
249 if (indexScanDescs[i] != NULL)
250 index_endscan(indexScanDescs[i]);
252 if (indexRelationDescs[i] != NULL)
253 index_close(indexRelationDescs[i]);
258 /* ----------------------------------------------------------------
261 * XXX this should be extended to cope with all the node types..
263 * takes the new expression context as an argument, so that
264 * index scans needn't have their scan keys updated separately
266 * ----------------------------------------------------------------
269 ExecReScan(Plan *node, ExprContext *exprCtxt, Plan *parent)
272 if (node->chgParam != NULL) /* Wow! */
276 foreach(lst, node->initPlan)
278 Plan *splan = ((SubPlan *) lfirst(lst))->plan;
280 if (splan->extParam != NULL) /* don't care about child
282 SetChangedParamList(splan, node->chgParam);
283 if (splan->chgParam != NULL)
284 ExecReScanSetParamPlan((SubPlan *) lfirst(lst), node);
286 foreach(lst, node->subPlan)
288 Plan *splan = ((SubPlan *) lfirst(lst))->plan;
290 if (splan->extParam != NULL)
291 SetChangedParamList(splan, node->chgParam);
293 /* Well. Now set chgParam for left/right trees. */
294 if (node->lefttree != NULL)
295 SetChangedParamList(node->lefttree, node->chgParam);
296 if (node->righttree != NULL)
297 SetChangedParamList(node->righttree, node->chgParam);
300 switch (nodeTag(node))
303 ExecSeqReScan((SeqScan *) node, exprCtxt, parent);
307 ExecIndexReScan((IndexScan *) node, exprCtxt, parent);
311 ExecTidReScan((TidScan *) node, exprCtxt, parent);
315 ExecSubqueryReScan((SubqueryScan *) node, exprCtxt, parent);
319 ExecMaterialReScan((Material *) node, exprCtxt, parent);
323 ExecReScanNestLoop((NestLoop *) node, exprCtxt, parent);
327 ExecReScanHashJoin((HashJoin *) node, exprCtxt, parent);
331 ExecReScanHash((Hash *) node, exprCtxt, parent);
335 ExecReScanAgg((Agg *) node, exprCtxt, parent);
339 ExecReScanGroup((Group *) node, exprCtxt, parent);
343 ExecReScanResult((Result *) node, exprCtxt, parent);
347 ExecReScanUnique((Unique *) node, exprCtxt, parent);
351 ExecReScanSetOp((SetOp *) node, exprCtxt, parent);
355 ExecReScanLimit((Limit *) node, exprCtxt, parent);
359 ExecReScanSort((Sort *) node, exprCtxt, parent);
363 ExecReScanMergeJoin((MergeJoin *) node, exprCtxt, parent);
367 ExecReScanAppend((Append *) node, exprCtxt, parent);
371 elog(ERROR, "ExecReScan: node type %d not supported",
376 if (node->chgParam != NULL)
378 freeList(node->chgParam);
379 node->chgParam = NULL;
383 /* ----------------------------------------------------------------
386 * XXX this does not do the right thing with indices yet.
387 * ----------------------------------------------------------------
390 ExecReScanR(Relation relDesc, /* LLL relDesc unused */
391 HeapScanDesc scanDesc,
392 ScanDirection direction,
393 int nkeys, /* LLL nkeys unused */
396 if (scanDesc != NULL)
397 heap_rescan(scanDesc, /* scan desc */
398 ScanDirectionIsBackward(direction), /* backward flag */
399 skeys); /* scan keys */
404 /* ----------------------------------------------------------------
407 * Marks the current scan position.
409 * XXX Needs to be extended to include all the node types,
410 * or at least all the ones that can be directly below a mergejoin.
411 * ----------------------------------------------------------------
414 ExecMarkPos(Plan *node)
416 switch (nodeTag(node))
419 ExecSeqMarkPos((SeqScan *) node);
423 ExecIndexMarkPos((IndexScan *) node);
427 ExecMaterialMarkPos((Material *) node);
431 ExecSortMarkPos((Sort *) node);
435 ExecTidMarkPos((TidScan *) node);
439 /* don't make hard error unless caller asks to restore... */
440 elog(DEBUG, "ExecMarkPos: node type %d not supported",
446 /* ----------------------------------------------------------------
449 * restores the scan position previously saved with ExecMarkPos()
451 * XXX Needs to be extended to include all the node types,
452 * or at least all the ones that can be directly below a mergejoin.
453 * ----------------------------------------------------------------
456 ExecRestrPos(Plan *node)
458 switch (nodeTag(node))
461 ExecSeqRestrPos((SeqScan *) node);
465 ExecIndexRestrPos((IndexScan *) node);
469 ExecMaterialRestrPos((Material *) node);
473 ExecSortRestrPos((Sort *) node);
477 elog(ERROR, "ExecRestrPos: node type %d not supported",