1 /*-------------------------------------------------------------------------
4 * miscellanious executor access method routines
6 * Copyright (c) 1994, Regents of the University of California
8 * $Id: execAmi.c,v 1.33 1999/03/23 16:50:47 momjian Exp $
10 *-------------------------------------------------------------------------
15 * ExecOpenScanR \ / amopen
16 * ExecBeginScan \ / ambeginscan
17 * ExecCloseR \ / amclose
18 * ExecInsert \ executor interface / aminsert
19 * ExecReScanNode / to access methods \ amrescan
20 * ExecReScanR / \ amrescan
21 * ExecMarkPos / \ ammarkpos
22 * ExecRestrPos / \ amrestpos
24 * ExecCreatR function to create temporary relations
31 #include "executor/executor.h"
32 #include "storage/smgr.h"
33 #include "utils/mcxt.h"
34 #include "executor/nodeSeqscan.h"
35 #include "executor/nodeIndexscan.h"
36 #include "executor/nodeSort.h"
37 #include "executor/nodeMaterial.h"
38 #include "executor/nodeNestloop.h"
39 #include "executor/nodeHashjoin.h"
40 #include "executor/nodeHash.h"
42 #include "executor/nodeGroup.h"
44 #include "executor/nodeAgg.h"
45 #include "executor/nodeGroup.h"
46 #include "executor/nodeResult.h"
47 #include "executor/nodeUnique.h"
48 #include "executor/nodeMergejoin.h"
49 #include "executor/nodeAppend.h"
50 #include "executor/nodeSubplan.h"
51 #include "executor/execdebug.h"
52 #include "optimizer/internal.h" /* for _NONAME_RELATION_ID_ */
53 #include "access/genam.h"
54 #include "access/heapam.h"
55 #include "catalog/heap.h"
57 static Pointer ExecBeginScan(Relation relation, int nkeys, ScanKey skeys,
58 bool isindex, ScanDirection dir, Snapshot snapshot);
59 static Relation ExecOpenR(Oid relationOid, bool isindex);
61 /* ----------------------------------------------------------------
66 * relation -- relation to be opened and scanned.
67 * nkeys -- number of keys
68 * skeys -- keys to restrict scanning
69 * isindex -- if this is true, the relation is the relid of
70 * an index relation, else it is an index into the
72 * Returns the relation as(relDesc scanDesc)
73 * If this structure is changed, need to modify the access macros
74 * defined in execInt.h.
75 * ----------------------------------------------------------------
78 ExecOpenScanR(Oid relOid,
84 Relation *returnRelation, /* return */
85 Pointer *returnScanDesc) /* return */
91 * note: scanDesc returned by ExecBeginScan can be either
92 * a HeapScanDesc or an IndexScanDesc so for now we
93 * make it a Pointer. There should be a better scan
94 * abstraction someday -cim 9/9/89
97 relation = ExecOpenR(relOid, isindex);
98 scanDesc = ExecBeginScan(relation,
105 if (returnRelation != NULL)
106 *returnRelation = relation;
107 if (scanDesc != NULL)
108 *returnScanDesc = scanDesc;
111 /* ----------------------------------------------------------------
114 * returns a relation descriptor given an object id.
115 * ----------------------------------------------------------------
118 ExecOpenR(Oid relationOid, bool isindex)
122 relation = (Relation) NULL;
125 * open the relation with the correct call depending
126 * on whether this is a heap relation or an index relation.
130 relation = index_open(relationOid);
132 relation = heap_open(relationOid);
134 if (relation == NULL)
135 elog(DEBUG, "ExecOpenR: relation == NULL, heap_open failed.");
140 /* ----------------------------------------------------------------
143 * beginscans a relation in current direction.
145 * XXX fix parameters to AMbeginscan (and btbeginscan)
146 * currently we need to pass a flag stating whether
147 * or not the scan should begin at an endpoint of
148 * the relation.. Right now we always pass false
150 * ----------------------------------------------------------------
153 ExecBeginScan(Relation relation,
165 * open the appropriate type of scan.
167 * Note: ambeginscan()'s second arg is a boolean indicating
168 * that the scan should be done in reverse.. That is,
169 * if you pass it true, then the scan is backward.
174 scanDesc = (Pointer) index_beginscan(relation,
175 false, /* see above comment */
181 scanDesc = (Pointer) heap_beginscan(relation,
182 ScanDirectionIsBackward(dir),
188 if (scanDesc == NULL)
189 elog(DEBUG, "ExecBeginScan: scanDesc = NULL, heap_beginscan failed.");
195 /* ----------------------------------------------------------------
198 * closes the relation and scan descriptor for a scan or sort
199 * node. Also closes index relations and scans for index scans.
202 * closes the relation indicated in 'relID'
203 * ----------------------------------------------------------------
206 ExecCloseR(Plan *node)
208 CommonScanState *state;
210 HeapScanDesc scanDesc;
213 * shut down the heap scan and close the heap relation
216 switch (nodeTag(node))
220 state = ((SeqScan *) node)->scanstate;
224 state = ((IndexScan *) node)->scan.scanstate;
228 state = &(((Material *) node)->matstate->csstate);
232 state = &(((Sort *) node)->sortstate->csstate);
236 state = &(((Agg *) node)->aggstate->csstate);
240 elog(DEBUG, "ExecCloseR: not a scan, material, or sort node!");
244 relation = state->css_currentRelation;
245 scanDesc = state->css_currentScanDesc;
247 if (scanDesc != NULL)
248 heap_endscan(scanDesc);
250 if (relation != NULL)
251 heap_close(relation);
254 * if this is an index scan then we have to take care
255 * of the index relations as well..
258 if (nodeTag(node) == T_IndexScan)
260 IndexScan *iscan = (IndexScan *) node;
261 IndexScanState *indexstate;
263 RelationPtr indexRelationDescs;
264 IndexScanDescPtr indexScanDescs;
267 indexstate = iscan->indxstate;
268 numIndices = indexstate->iss_NumIndices;
269 indexRelationDescs = indexstate->iss_RelationDescs;
270 indexScanDescs = indexstate->iss_ScanDescs;
272 for (i = 0; i < numIndices; i++)
275 * shut down each of the scans and
276 * close each of the index relations
279 if (indexScanDescs[i] != NULL)
280 index_endscan(indexScanDescs[i]);
282 if (indexRelationDescs[i] != NULL)
283 index_close(indexRelationDescs[i]);
288 /* ----------------------------------------------------------------
291 * XXX this should be extended to cope with all the node types..
293 * takes the new expression context as an argument, so that
294 * index scans needn't have their scan keys updated separately
296 * ----------------------------------------------------------------
299 ExecReScan(Plan *node, ExprContext *exprCtxt, Plan *parent)
302 if (node->chgParam != NULL) /* Wow! */
306 foreach(lst, node->initPlan)
308 Plan *splan = ((SubPlan *) lfirst(lst))->plan;
310 if (splan->extParam != NULL) /* don't care about child
312 SetChangedParamList(splan, node->chgParam);
313 if (splan->chgParam != NULL)
314 ExecReScanSetParamPlan((SubPlan *) lfirst(lst), node);
316 foreach(lst, node->subPlan)
318 Plan *splan = ((SubPlan *) lfirst(lst))->plan;
320 if (splan->extParam != NULL)
321 SetChangedParamList(splan, node->chgParam);
323 /* Well. Now set chgParam for left/right trees. */
324 if (node->lefttree != NULL)
325 SetChangedParamList(node->lefttree, node->chgParam);
326 if (node->righttree != NULL)
327 SetChangedParamList(node->righttree, node->chgParam);
330 switch (nodeTag(node))
333 ExecSeqReScan((SeqScan *) node, exprCtxt, parent);
337 ExecIndexReScan((IndexScan *) node, exprCtxt, parent);
341 ExecMaterialReScan((Material *) node, exprCtxt, parent);
345 ExecReScanNestLoop((NestLoop *) node, exprCtxt, parent);
349 ExecReScanHashJoin((HashJoin *) node, exprCtxt, parent);
353 ExecReScanHash((Hash *) node, exprCtxt, parent);
357 ExecReScanAgg((Agg *) node, exprCtxt, parent);
361 ExecReScanGroup((Group *) node, exprCtxt, parent);
365 ExecReScanResult((Result *) node, exprCtxt, parent);
369 ExecReScanUnique((Unique *) node, exprCtxt, parent);
373 ExecReScanSort((Sort *) node, exprCtxt, parent);
377 ExecReScanMergeJoin((MergeJoin *) node, exprCtxt, parent);
381 ExecReScanAppend((Append *) node, exprCtxt, parent);
385 elog(ERROR, "ExecReScan: node type %u not supported", nodeTag(node));
389 if (node->chgParam != NULL)
391 freeList(node->chgParam);
392 node->chgParam = NULL;
396 /* ----------------------------------------------------------------
399 * XXX this does not do the right thing with indices yet.
400 * ----------------------------------------------------------------
403 ExecReScanR(Relation relDesc, /* LLL relDesc unused */
404 HeapScanDesc scanDesc,
405 ScanDirection direction,
406 int nkeys, /* LLL nkeys unused */
409 if (scanDesc != NULL)
410 heap_rescan(scanDesc, /* scan desc */
411 ScanDirectionIsBackward(direction), /* backward flag */
412 skeys); /* scan keys */
417 /* ----------------------------------------------------------------
420 * Marks the current scan position.
422 * XXX Needs to be extended to include all the node types.
423 * ----------------------------------------------------------------
426 ExecMarkPos(Plan *node)
428 switch (nodeTag(node))
431 ExecSeqMarkPos((SeqScan *) node);
435 ExecIndexMarkPos((IndexScan *) node);
439 ExecSortMarkPos((Sort *) node);
443 elog(DEBUG, "ExecMarkPos: node type %u not supported", nodeTag(node));
449 /* ----------------------------------------------------------------
452 * restores the scan position previously saved with ExecMarkPos()
453 * ----------------------------------------------------------------
456 ExecRestrPos(Plan *node)
458 switch (nodeTag(node))
461 ExecSeqRestrPos((SeqScan *) node);
465 ExecIndexRestrPos((IndexScan *) node);
469 ExecSortRestrPos((Sort *) node);
473 elog(DEBUG, "ExecRestrPos: node type %u not supported", nodeTag(node));
478 /* ----------------------------------------------------------------
482 * Creates a relation.
485 * attrType -- type information on the attributes.
486 * accessMtd -- access methods used to access the created relation.
487 * relation -- optional. Either an index to the range table or
488 * negative number indicating a temporary relation.
489 * A temporary relation is assume if this field is absent.
490 * ----------------------------------------------------------------
494 ExecCreatR(TupleDesc tupType,
499 EU3_printf("ExecCreatR: %s type=%d oid=%d\n",
500 "entering: ", tupType, relationOid);
501 CXT1_printf("ExecCreatR: context is %d\n", CurrentMemoryContext);
505 if (relationOid == _NONAME_RELATION_ID_)
508 * create a temporary relation
509 * (currently the planner always puts a _NONAME_RELATION_ID
510 * in the relation argument so we expect this to be the case although
511 * it's possible that someday we'll get the name from
512 * from the range table.. -cim 10/12/89)
517 * heap_create creates a name if the argument to heap_create is
520 relDesc = heap_create(NULL, tupType, true, false);
525 * use a relation from the range table
528 elog(DEBUG, "ExecCreatR: %s",
529 "stuff using range table id's is not functional");
533 elog(DEBUG, "ExecCreatR: failed to create relation.");
535 EU1_printf("ExecCreatR: returning relDesc=%d\n", relDesc);