execAmi.c 13.0 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
10
 *	  $Header: /cvsroot/pgsql/src/backend/executor/execAmi.c,v 1.22 1998/07/15 22:16:17 momjian 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 45
#include "executor/nodeMaterial.h"
#include "executor/nodeNestloop.h"
#include "executor/nodeHashjoin.h"
#include "executor/nodeHash.h"
#include "executor/nodeAgg.h"
#include "executor/nodeResult.h"
46
#include "executor/nodeUnique.h"
V
Vadim B. Mikheev 已提交
47
#include "executor/nodeMergejoin.h"
V
Vadim B. Mikheev 已提交
48
#include "executor/nodeSubplan.h"
M
Marc G. Fournier 已提交
49
#include "executor/execdebug.h"
50
#include "optimizer/internal.h" /* for _TEMP_RELATION_ID_ */
M
Marc G. Fournier 已提交
51 52 53
#include "access/genam.h"
#include "access/heapam.h"
#include "catalog/heap.h"
54

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

60
/* ----------------------------------------------------------------
61
 *		ExecOpenScanR
62 63
 *
 * old comments:
64 65 66 67 68 69 70 71 72 73
 *		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.
74 75 76 77
 * ----------------------------------------------------------------
 */
void
ExecOpenScanR(Oid relOid,
78 79 80 81
			  int nkeys,
			  ScanKey skeys,
			  bool isindex,
			  ScanDirection dir,
82 83
			  Relation *returnRelation, /* return */
			  Pointer *returnScanDesc)	/* return */
84
{
85 86
	Relation	relation;
	Pointer		scanDesc;
87 88 89 90 91 92 93 94 95 96 97 98 99

	/* ----------------
	 *	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,
100
							 dir);
101 102 103 104 105

	if (returnRelation != NULL)
		*returnRelation = relation;
	if (scanDesc != NULL)
		*returnScanDesc = scanDesc;
106
}
107

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

	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;
135
}
136

137
/* ----------------------------------------------------------------
138
 *		ExecBeginScan
139
 *
140
 *		beginscans a relation in current direction.
141
 *
142 143 144 145 146
 *		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
147 148
 * ----------------------------------------------------------------
 */
149
static Pointer
150
ExecBeginScan(Relation relation,
151 152 153
			  int nkeys,
			  ScanKey skeys,
			  bool isindex,
154
			  ScanDirection dir)
155
{
156
	Pointer		scanDesc;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

	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),
179
											false,
180 181 182 183 184 185 186 187 188
											nkeys,
											skeys);
	}

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


	return scanDesc;
189
}
190

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

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

215 216 217
		case T_SeqScan:
			state = ((SeqScan *) node)->scanstate;
			break;
218

219 220 221
		case T_IndexScan:
			state = ((IndexScan *) node)->scan.scanstate;
			break;
222

223 224 225
		case T_Material:
			state = &(((Material *) node)->matstate->csstate);
			break;
226

227 228 229
		case T_Sort:
			state = &(((Sort *) node)->sortstate->csstate);
			break;
230

231 232 233
		case T_Agg:
			state = &(((Agg *) node)->aggstate->csstate);
			break;
234

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

	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)
	{
256
		IndexScan  *iscan = (IndexScan *) node;
257
		IndexScanState *indexstate;
258 259
		int			numIndices;
		RelationPtr indexRelationDescs;
260
		IndexScanDescPtr indexScanDescs;
261
		int			i;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

		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]);
		}
281 282
	}
}
283

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

298
	if (node->chgParam != NULL) /* Wow! */
V
Vadim B. Mikheev 已提交
299
	{
300 301 302
		List	   *lst;

		foreach(lst, node->initPlan)
V
Vadim B. Mikheev 已提交
303
		{
304 305 306 307 308 309 310
			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 已提交
311
		}
312
		foreach(lst, node->subPlan)
V
Vadim B. Mikheev 已提交
313
		{
314 315 316 317
			Plan	   *splan = ((SubPlan *) lfirst(lst))->plan;

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

326 327
	switch (nodeTag(node))
	{
V
Vadim B. Mikheev 已提交
328
		case T_SeqScan:
329
			ExecSeqReScan((SeqScan *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
330
			break;
331 332 333

		case T_IndexScan:
			ExecIndexReScan((IndexScan *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
334
			break;
335 336

		case T_Material:
337
			ExecMaterialReScan((Material *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
338
			break;
339

V
Vadim B. Mikheev 已提交
340
		case T_NestLoop:
341
			ExecReScanNestLoop((NestLoop *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
342 343 344
			break;

		case T_HashJoin:
345
			ExecReScanHashJoin((HashJoin *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
346 347 348
			break;

		case T_Hash:
349
			ExecReScanHash((Hash *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
350 351 352
			break;

		case T_Agg:
353
			ExecReScanAgg((Agg *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
354
			break;
355

V
Vadim B. Mikheev 已提交
356
		case T_Result:
357
			ExecReScanResult((Result *) node, exprCtxt, parent);
V
Vadim B. Mikheev 已提交
358 359
			break;

360
		case T_Unique:
361
			ExecReScanUnique((Unique *) node, exprCtxt, parent);
362 363 364
			break;

		case T_Sort:
365
			ExecReScanSort((Sort *) node, exprCtxt, parent);
366 367
			break;

V
Vadim B. Mikheev 已提交
368 369 370 371
		case T_MergeJoin:
			ExecReScanMergeJoin((MergeJoin *) node, exprCtxt, parent);
			break;

372 373 374 375
		case T_Append:
			ExecReScanAppend((Append *) node, exprCtxt, parent);
			break;

376
/*
V
Vadim B. Mikheev 已提交
377
 * Tee is never used
378 379 380
		case T_Tee:
			ExecTeeReScan((Tee *) node, exprCtxt, parent);
			break;
V
Vadim B. Mikheev 已提交
381
 */
382
		default:
V
Vadim B. Mikheev 已提交
383
			elog(ERROR, "ExecReScan: node type %u not supported", nodeTag(node));
384
			return;
385
	}
386 387

	if (node->chgParam != NULL)
V
Vadim B. Mikheev 已提交
388
	{
389
		freeList(node->chgParam);
V
Vadim B. Mikheev 已提交
390 391
		node->chgParam = NULL;
	}
392
}
393

394
/* ----------------------------------------------------------------
395
 *		ExecReScanR
396
 *
397
 *		XXX this does not do the right thing with indices yet.
398 399 400
 * ----------------------------------------------------------------
 */
HeapScanDesc
401
ExecReScanR(Relation relDesc,	/* LLL relDesc unused  */
402 403 404 405
			HeapScanDesc scanDesc,
			ScanDirection direction,
			int nkeys,			/* LLL nkeys unused  */
			ScanKey skeys)
406
{
407 408 409 410 411 412
	if (scanDesc != NULL)
		heap_rescan(scanDesc,	/* scan desc */
					ScanDirectionIsBackward(direction), /* backward flag */
					skeys);		/* scan keys */

	return scanDesc;
413
}
414

415
/* ----------------------------------------------------------------
416
 *		ExecMarkPos
417
 *
418
 *		Marks the current scan position.
419
 *
420
 *		XXX Needs to be extended to include all the node types.
421 422 423
 * ----------------------------------------------------------------
 */
void
424
ExecMarkPos(Plan *node)
425
{
426 427
	switch (nodeTag(node))
	{
428
			case T_SeqScan:
429 430
			ExecSeqMarkPos((SeqScan *) node);
			break;
431

432 433 434
		case T_IndexScan:
			ExecIndexMarkPos((IndexScan *) node);
			break;
435

436 437 438
		case T_Sort:
			ExecSortMarkPos((Sort *) node);
			break;
439

440
		default:
V
Vadim B. Mikheev 已提交
441
			elog(DEBUG, "ExecMarkPos: node type %u not supported", nodeTag(node));
442
			break;
443 444
	}
	return;
445
}
446

447
/* ----------------------------------------------------------------
448
 *		ExecRestrPos
449
 *
450
 *		restores the scan position previously saved with ExecMarkPos()
451 452 453
 * ----------------------------------------------------------------
 */
void
454
ExecRestrPos(Plan *node)
455
{
456 457
	switch (nodeTag(node))
	{
458
			case T_SeqScan:
459 460
			ExecSeqRestrPos((SeqScan *) node);
			return;
461

462 463 464
		case T_IndexScan:
			ExecIndexRestrPos((IndexScan *) node);
			return;
465

466 467 468
		case T_Sort:
			ExecSortRestrPos((Sort *) node);
			return;
469

470
		default:
V
Vadim B. Mikheev 已提交
471
			elog(DEBUG, "ExecRestrPos: node type %u not supported", nodeTag(node));
472
			return;
473
	}
474
}
475

476
/* ----------------------------------------------------------------
477
 *		ExecCreatR
478 479
 *
 * old comments
480
 *		Creates a relation.
481
 *
482 483 484 485 486
 *		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 已提交
487
 *					   A temporary relation is assume if this field is absent.
488 489 490 491 492
 * ----------------------------------------------------------------
 */

Relation
ExecCreatR(TupleDesc tupType,
493
		   Oid relationOid)
494
{
495
	Relation	relDesc;
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

	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)
		 * ----------------
		 */
513
/*
514 515
		sprintf(tempname, "temp_%d.%d", getpid(), tmpcnt++);
		EU1_printf("ExecCreatR: attempting to create %s\n", tempname);
516
*/
517 518

		/*
519
		 * heap_create creates a name if the argument to heap_create is
520 521
		 * '\0 '
		 */
522
		relDesc = heap_create("", tupType);
523
	}
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	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;
540
}