execAmi.c 13.2 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * execAmi.c--
4
 *	  miscellanious executor access method routines
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
M
 
Marc G. Fournier 已提交
10
 *	  $Header: /cvsroot/pgsql/src/backend/executor/execAmi.c,v 1.26 1998/12/14 05:18:49 scrappy Exp $
11 12 13 14
 *
 *-------------------------------------------------------------------------
 */
/*
15
 *	 INTERFACE ROUTINES
16
 *
17 18 19 20 21 22 23 24
 *		ExecOpenScanR	\							  / amopen
 *		ExecBeginScan	 \							 /	ambeginscan
 *		ExecCloseR		  \							/	amclose
 *		ExecInsert		   \  executor interface   /	aminsert
 *		ExecReScanNode	   /  to access methods    \	amrescan
 *		ExecReScanR		  /							\	amrescan
 *		ExecMarkPos		 /							 \	ammarkpos
 *		ExecRestrPos	/							  \ amrestpos
25
 *
26
 *		ExecCreatR		function to create temporary relations
27 28
 *
 */
29
#include <stdio.h>				/* for sprintf() */
30 31 32

#include "postgres.h"

33 34
#include "executor/executor.h"
#include "storage/smgr.h"
35
#include "utils/mcxt.h"
36 37 38 39
#include "executor/nodeSeqscan.h"
#include "executor/nodeIndexscan.h"
#include "executor/nodeSort.h"
#include "executor/nodeTee.h"
V
Vadim B. Mikheev 已提交
40 41 42 43 44
#include "executor/nodeMaterial.h"
#include "executor/nodeNestloop.h"
#include "executor/nodeHashjoin.h"
#include "executor/nodeHash.h"
#include "executor/nodeAgg.h"
B
Bruce Momjian 已提交
45
#include "executor/nodeGroup.h"
V
Vadim B. Mikheev 已提交
46
#include "executor/nodeResult.h"
47
#include "executor/nodeUnique.h"
V
Vadim B. Mikheev 已提交
48
#include "executor/nodeMergejoin.h"
B
Bruce Momjian 已提交
49
#include "executor/nodeAppend.h"
V
Vadim B. Mikheev 已提交
50
#include "executor/nodeSubplan.h"
M
Marc G. Fournier 已提交
51
#include "executor/execdebug.h"
52
#include "optimizer/internal.h" /* for _TEMP_RELATION_ID_ */
M
Marc G. Fournier 已提交
53 54 55
#include "access/genam.h"
#include "access/heapam.h"
#include "catalog/heap.h"
56

57
static Pointer ExecBeginScan(Relation relation, int nkeys, ScanKey skeys,
58
			  bool isindex, ScanDirection dir, Snapshot snapshot);
59 60
static Relation ExecOpenR(Oid relationOid, bool isindex);

61
/* ----------------------------------------------------------------
62
 *		ExecOpenScanR
63 64
 *
 * old comments:
65 66 67 68 69 70 71 72 73 74
 *		Parameters:
 *		  relation -- relation to be opened and scanned.
 *		  nkeys    -- number of keys
 *		  skeys    -- keys to restrict scanning
 *			 isindex  -- if this is true, the relation is the relid of
 *						 an index relation, else it is an index into the
 *						 range table.
 *		Returns the relation as(relDesc scanDesc)
 *		   If this structure is changed, need to modify the access macros
 *		defined in execInt.h.
75 76 77 78
 * ----------------------------------------------------------------
 */
void
ExecOpenScanR(Oid relOid,
79 80 81 82
			  int nkeys,
			  ScanKey skeys,
			  bool isindex,
			  ScanDirection dir,
83
			  Snapshot snapshot,
84 85
			  Relation *returnRelation, /* return */
			  Pointer *returnScanDesc)	/* return */
86
{
87 88
	Relation	relation;
	Pointer		scanDesc;
89 90 91 92 93 94 95 96 97 98 99 100 101

	/* ----------------
	 *	note: scanDesc returned by ExecBeginScan can be either
	 *		  a HeapScanDesc or an IndexScanDesc so for now we
	 *		  make it a Pointer.  There should be a better scan
	 *		  abstraction someday -cim 9/9/89
	 * ----------------
	 */
	relation = ExecOpenR(relOid, isindex);
	scanDesc = ExecBeginScan(relation,
							 nkeys,
							 skeys,
							 isindex,
102 103
							 dir,
							 snapshot);
104 105 106 107 108

	if (returnRelation != NULL)
		*returnRelation = relation;
	if (scanDesc != NULL)
		*returnScanDesc = scanDesc;
109
}
110

111
/* ----------------------------------------------------------------
112
 *		ExecOpenR
113
 *
114
 *		returns a relation descriptor given an object id.
115 116
 * ----------------------------------------------------------------
 */
117
static Relation
118 119
ExecOpenR(Oid relationOid, bool isindex)
{
120
	Relation	relation;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

	relation = (Relation) NULL;

	/* ----------------
	 *	open the relation with the correct call depending
	 *	on whether this is a heap relation or an index relation.
	 * ----------------
	 */
	if (isindex)
		relation = index_open(relationOid);
	else
		relation = heap_open(relationOid);

	if (relation == NULL)
		elog(DEBUG, "ExecOpenR: relation == NULL, heap_open failed.");

	return relation;
138
}
139

140
/* ----------------------------------------------------------------
141
 *		ExecBeginScan
142
 *
143
 *		beginscans a relation in current direction.
144
 *
145 146 147 148 149
 *		XXX fix parameters to AMbeginscan (and btbeginscan)
 *				currently we need to pass a flag stating whether
 *				or not the scan should begin at an endpoint of
 *				the relation.. Right now we always pass false
 *				-cim 9/14/89
150 151
 * ----------------------------------------------------------------
 */
152
static Pointer
153
ExecBeginScan(Relation relation,
154 155 156
			  int nkeys,
			  ScanKey skeys,
			  bool isindex,
157 158
			  ScanDirection dir,
			  Snapshot snapshot)
159
{
160
	Pointer		scanDesc;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

	scanDesc = NULL;

	/* ----------------
	 *	open the appropriate type of scan.
	 *
	 *	Note: ambeginscan()'s second arg is a boolean indicating
	 *		  that the scan should be done in reverse..  That is,
	 *		  if you pass it true, then the scan is backward.
	 * ----------------
	 */
	if (isindex)
	{
		scanDesc = (Pointer) index_beginscan(relation,
											 false,		/* see above comment */
											 nkeys,
											 skeys);
	}
	else
	{
		scanDesc = (Pointer) heap_beginscan(relation,
											ScanDirectionIsBackward(dir),
183
											snapshot,
184 185 186 187 188 189 190 191 192
											nkeys,
											skeys);
	}

	if (scanDesc == NULL)
		elog(DEBUG, "ExecBeginScan: scanDesc = NULL, heap_beginscan failed.");


	return scanDesc;
193
}
194

195
/* ----------------------------------------------------------------
196
 *		ExecCloseR
197
 *
198 199
 *		closes the relation and scan descriptor for a scan or sort
 *		node.  Also closes index relations and scans for index scans.
200 201
 *
 * old comments
202
 *		closes the relation indicated in 'relID'
203 204 205
 * ----------------------------------------------------------------
 */
void
206
ExecCloseR(Plan *node)
207
{
208
	CommonScanState *state;
209 210
	Relation	relation;
	HeapScanDesc scanDesc;
211 212 213 214 215 216 217 218

	/* ----------------
	 *	shut down the heap scan and close the heap relation
	 * ----------------
	 */
	switch (nodeTag(node))
	{

219 220 221
		case T_SeqScan:
			state = ((SeqScan *) node)->scanstate;
			break;
222

223 224 225
		case T_IndexScan:
			state = ((IndexScan *) node)->scan.scanstate;
			break;
226

227 228 229
		case T_Material:
			state = &(((Material *) node)->matstate->csstate);
			break;
230

231 232 233
		case T_Sort:
			state = &(((Sort *) node)->sortstate->csstate);
			break;
234

235 236 237
		case T_Agg:
			state = &(((Agg *) node)->aggstate->csstate);
			break;
238

239 240 241
		default:
			elog(DEBUG, "ExecCloseR: not a scan, material, or sort node!");
			return;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	}

	relation = state->css_currentRelation;
	scanDesc = state->css_currentScanDesc;

	if (scanDesc != NULL)
		heap_endscan(scanDesc);

	if (relation != NULL)
		heap_close(relation);

	/* ----------------
	 *	if this is an index scan then we have to take care
	 *	of the index relations as well..
	 * ----------------
	 */
	if (nodeTag(node) == T_IndexScan)
	{
260
		IndexScan  *iscan = (IndexScan *) node;
261
		IndexScanState *indexstate;
262 263
		int			numIndices;
		RelationPtr indexRelationDescs;
264
		IndexScanDescPtr indexScanDescs;
265
		int			i;
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

		indexstate = iscan->indxstate;
		numIndices = indexstate->iss_NumIndices;
		indexRelationDescs = indexstate->iss_RelationDescs;
		indexScanDescs = indexstate->iss_ScanDescs;

		for (i = 0; i < numIndices; i++)
		{
			/* ----------------
			 *	shut down each of the scans and
			 *	close each of the index relations
			 * ----------------
			 */
			if (indexScanDescs[i] != NULL)
				index_endscan(indexScanDescs[i]);

			if (indexRelationDescs[i] != NULL)
				index_close(indexRelationDescs[i]);
		}
285 286
	}
}
287

288
/* ----------------------------------------------------------------
289
 *		ExecReScan
290
 *
291
 *		XXX this should be extended to cope with all the node types..
292
 *
293 294 295
 *		takes the new expression context as an argument, so that
 *		index scans needn't have their scan keys updated separately
 *		- marcel 09/20/94
296 297 298
 * ----------------------------------------------------------------
 */
void
299
ExecReScan(Plan *node, ExprContext *exprCtxt, Plan *parent)
300
{
V
Vadim B. Mikheev 已提交
301

302
	if (node->chgParam != NULL) /* Wow! */
V
Vadim B. Mikheev 已提交
303
	{
304 305 306
		List	   *lst;

		foreach(lst, node->initPlan)
V
Vadim B. Mikheev 已提交
307
		{
308 309 310 311 312 313 314
			Plan	   *splan = ((SubPlan *) lfirst(lst))->plan;

			if (splan->extParam != NULL)		/* don't care about child
												 * locParam */
				SetChangedParamList(splan, node->chgParam);
			if (splan->chgParam != NULL)
				ExecReScanSetParamPlan((SubPlan *) lfirst(lst), node);
V
Vadim B. Mikheev 已提交
315
		}
316
		foreach(lst, node->subPlan)
V
Vadim B. Mikheev 已提交
317
		{
318 319 320 321
			Plan	   *splan = ((SubPlan *) lfirst(lst))->plan;

			if (splan->extParam != NULL)
				SetChangedParamList(splan, node->chgParam);
V
Vadim B. Mikheev 已提交
322 323
		}
		/* Well. Now set chgParam for left/right trees. */
324 325 326 327
		if (node->lefttree != NULL)
			SetChangedParamList(node->lefttree, node->chgParam);
		if (node->righttree != NULL)
			SetChangedParamList(node->righttree, node->chgParam);
V
Vadim B. Mikheev 已提交
328 329
	}

330 331
	switch (nodeTag(node))
	{
V
Vadim B. Mikheev 已提交
332
		case T_SeqScan:
333
			ExecSeqReScan((SeqScan *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
334
			break;
335 336 337

		case T_IndexScan:
			ExecIndexReScan((IndexScan *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
338
			break;
339 340

		case T_Material:
341
			ExecMaterialReScan((Material *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
342
			break;
343

V
Vadim B. Mikheev 已提交
344
		case T_NestLoop:
345
			ExecReScanNestLoop((NestLoop *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
346 347 348
			break;

		case T_HashJoin:
349
			ExecReScanHashJoin((HashJoin *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
350 351 352
			break;

		case T_Hash:
353
			ExecReScanHash((Hash *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
354 355 356
			break;

		case T_Agg:
357
			ExecReScanAgg((Agg *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
358
			break;
359

B
Bruce Momjian 已提交
360 361 362 363
		case T_Group:
			ExecReScanGroup((Group *) node, exprCtxt, parent);
			break;

V
Vadim B. Mikheev 已提交
364
		case T_Result:
365
			ExecReScanResult((Result *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
366 367
			break;

368
		case T_Unique:
369
			ExecReScanUnique((Unique *) node, exprCtxt, parent);
370 371 372
			break;

		case T_Sort:
373
			ExecReScanSort((Sort *) node, exprCtxt, parent);
374 375
			break;

V
Vadim B. Mikheev 已提交
376 377 378 379
		case T_MergeJoin:
			ExecReScanMergeJoin((MergeJoin *) node, exprCtxt, parent);
			break;

380 381 382 383
		case T_Append:
			ExecReScanAppend((Append *) node, exprCtxt, parent);
			break;

384
/*
V
Vadim B. Mikheev 已提交
385
 * Tee is never used
386 387 388
		case T_Tee:
			ExecTeeReScan((Tee *) node, exprCtxt, parent);
			break;
V
Vadim B. Mikheev 已提交
389
 */
390
		default:
V
Vadim B. Mikheev 已提交
391
			elog(ERROR, "ExecReScan: node type %u not supported", nodeTag(node));
392
			return;
393
	}
394 395

	if (node->chgParam != NULL)
V
Vadim B. Mikheev 已提交
396
	{
397
		freeList(node->chgParam);
V
Vadim B. Mikheev 已提交
398 399
		node->chgParam = NULL;
	}
400
}
401

402
/* ----------------------------------------------------------------
403
 *		ExecReScanR
404
 *
405
 *		XXX this does not do the right thing with indices yet.
406 407 408
 * ----------------------------------------------------------------
 */
HeapScanDesc
409
ExecReScanR(Relation relDesc,	/* LLL relDesc unused  */
410 411 412 413
			HeapScanDesc scanDesc,
			ScanDirection direction,
			int nkeys,			/* LLL nkeys unused  */
			ScanKey skeys)
414
{
415 416 417 418 419 420
	if (scanDesc != NULL)
		heap_rescan(scanDesc,	/* scan desc */
					ScanDirectionIsBackward(direction), /* backward flag */
					skeys);		/* scan keys */

	return scanDesc;
421
}
422

423
/* ----------------------------------------------------------------
424
 *		ExecMarkPos
425
 *
426
 *		Marks the current scan position.
427
 *
428
 *		XXX Needs to be extended to include all the node types.
429 430 431
 * ----------------------------------------------------------------
 */
void
432
ExecMarkPos(Plan *node)
433
{
434 435
	switch (nodeTag(node))
	{
436
			case T_SeqScan:
437 438
			ExecSeqMarkPos((SeqScan *) node);
			break;
439

440 441 442
		case T_IndexScan:
			ExecIndexMarkPos((IndexScan *) node);
			break;
443

444 445 446
		case T_Sort:
			ExecSortMarkPos((Sort *) node);
			break;
447

448
		default:
V
Vadim B. Mikheev 已提交
449
			elog(DEBUG, "ExecMarkPos: node type %u not supported", nodeTag(node));
450
			break;
451 452
	}
	return;
453
}
454

455
/* ----------------------------------------------------------------
456
 *		ExecRestrPos
457
 *
458
 *		restores the scan position previously saved with ExecMarkPos()
459 460 461
 * ----------------------------------------------------------------
 */
void
462
ExecRestrPos(Plan *node)
463
{
464 465
	switch (nodeTag(node))
	{
466
			case T_SeqScan:
467 468
			ExecSeqRestrPos((SeqScan *) node);
			return;
469

470 471 472
		case T_IndexScan:
			ExecIndexRestrPos((IndexScan *) node);
			return;
473

474 475 476
		case T_Sort:
			ExecSortRestrPos((Sort *) node);
			return;
477

478
		default:
V
Vadim B. Mikheev 已提交
479
			elog(DEBUG, "ExecRestrPos: node type %u not supported", nodeTag(node));
480
			return;
481
	}
482
}
483

484
/* ----------------------------------------------------------------
485
 *		ExecCreatR
486 487
 *
 * old comments
488
 *		Creates a relation.
489
 *
490 491 492 493 494
 *		Parameters:
 *		  attrType	-- type information on the attributes.
 *		  accessMtd -- access methods used to access the created relation.
 *		  relation	-- optional. Either an index to the range table or
 *					   negative number indicating a temporary relation.
B
Bruce Momjian 已提交
495
 *					   A temporary relation is assume if this field is absent.
496 497 498 499 500
 * ----------------------------------------------------------------
 */

Relation
ExecCreatR(TupleDesc tupType,
501
		   Oid relationOid)
502
{
503
	Relation	relDesc;
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

	EU3_printf("ExecCreatR: %s type=%d oid=%d\n",
			   "entering: ", tupType, relationOid);
	CXT1_printf("ExecCreatR: context is %d\n", CurrentMemoryContext);

	relDesc = NULL;

	if (relationOid == _TEMP_RELATION_ID_)
	{
		/* ----------------
		 *	 create a temporary relation
		 *	 (currently the planner always puts a _TEMP_RELATION_ID
		 *	 in the relation argument so we expect this to be the case although
		 *	 it's possible that someday we'll get the name from
		 *	 from the range table.. -cim 10/12/89)
		 * ----------------
		 */

		/*
523
		 * heap_create creates a name if the argument to heap_create is
524 525
		 * '\0 '
		 */
526
		relDesc = heap_create("", tupType);
527
	}
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
	else
	{
		/* ----------------
		 *		use a relation from the range table
		 * ----------------
		 */
		elog(DEBUG, "ExecCreatR: %s",
			 "stuff using range table id's is not functional");
	}

	if (relDesc == NULL)
		elog(DEBUG, "ExecCreatR: failed to create relation.");

	EU1_printf("ExecCreatR: returning relDesc=%d\n", relDesc);

	return relDesc;
544
}