execnodes.h 41.3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * execnodes.h
4
 *	  definitions for executor state nodes
5 6
 *
 *
7
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.131 2005/05/05 03:37:23 tgl Exp $
11 12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
#ifndef EXECNODES_H
#define EXECNODES_H

17 18
#include "access/relscan.h"
#include "executor/tuptable.h"
19
#include "fmgr.h"
20
#include "nodes/bitmapset.h"
Bruce Momjian's avatar
Bruce Momjian committed
21
#include "nodes/params.h"
22
#include "nodes/plannodes.h"
23
#include "nodes/tidbitmap.h"
24
#include "utils/hsearch.h"
25 26
#include "utils/tuplestore.h"

27 28

/* ----------------
29
 *	  IndexInfo information
30
 *
31
 *		this struct holds the information needed to construct new index
32 33
 *		entries for a particular index.  Used for both index_build and
 *		retail creation of index entries.
34
 *
35 36
 *		NumIndexAttrs		number of columns in this index
 *		KeyAttrNumbers		underlying-rel attribute numbers used as keys
37 38 39
 *							(zeroes indicate expressions)
 *		Expressions			expr trees for expression entries, or NIL if none
 *		ExpressionsState	exec state for expressions, or NIL if none
40
 *		Predicate			partial-index predicate, or NIL if none
41
 *		PredicateState		exec state for predicate, or NIL if none
42
 *		Unique				is it a unique index?
43 44
 * ----------------
 */
45 46
typedef struct IndexInfo
{
47
	NodeTag		type;
48 49
	int			ii_NumIndexAttrs;
	AttrNumber	ii_KeyAttrNumbers[INDEX_MAX_KEYS];
Bruce Momjian's avatar
Bruce Momjian committed
50 51
	List	   *ii_Expressions; /* list of Expr */
	List	   *ii_ExpressionsState;	/* list of ExprState */
52
	List	   *ii_Predicate;	/* list of Expr */
Bruce Momjian's avatar
Bruce Momjian committed
53
	List	   *ii_PredicateState;		/* list of ExprState */
54
	bool		ii_Unique;
55
} IndexInfo;
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/* ----------------
 *	  ExprContext_CB
 *
 *		List of callbacks to be called at ExprContext shutdown.
 * ----------------
 */
typedef void (*ExprContextCallbackFunction) (Datum arg);

typedef struct ExprContext_CB
{
	struct ExprContext_CB *next;
	ExprContextCallbackFunction function;
	Datum		arg;
} ExprContext_CB;

72
/* ----------------
73 74 75 76 77 78 79 80
 *	  ExprContext
 *
 *		This class holds the "current context" information
 *		needed to evaluate expressions for doing tuple qualifications
 *		and tuple projections.	For example, if an expression refers
 *		to an attribute in the current inner tuple then we need to know
 *		what the current inner tuple is and so we look at the expression
 *		context.
81 82
 *
 *	There are two memory contexts associated with an ExprContext:
83
 *	* ecxt_per_query_memory is a query-lifespan context, typically the same
Bruce Momjian's avatar
Bruce Momjian committed
84
 *	  context the ExprContext node itself is allocated in.	This context
85
 *	  can be used for purposes such as storing function call cache info.
86 87 88 89
 *	* ecxt_per_tuple_memory is a short-term context for expression results.
 *	  As the name suggests, it will typically be reset once per tuple,
 *	  before we begin to evaluate expressions for that tuple.  Each
 *	  ExprContext normally has its very own per-tuple memory context.
90
 *
91 92
 *	CurrentMemoryContext should be set to ecxt_per_tuple_memory before
 *	calling ExecEvalExpr() --- see ExecEvalExprSwitchContext().
93 94
 * ----------------
 */
95 96
typedef struct ExprContext
{
Bruce Momjian's avatar
Bruce Momjian committed
97
	NodeTag		type;
98

99
	/* Tuples that Var nodes in expression may refer to */
100 101 102
	TupleTableSlot *ecxt_scantuple;
	TupleTableSlot *ecxt_innertuple;
	TupleTableSlot *ecxt_outertuple;
103

104
	/* Memory contexts for expression evaluation --- see notes above */
Bruce Momjian's avatar
Bruce Momjian committed
105 106
	MemoryContext ecxt_per_query_memory;
	MemoryContext ecxt_per_tuple_memory;
107

108
	/* Values to substitute for Param nodes in expression */
Bruce Momjian's avatar
Bruce Momjian committed
109 110
	ParamExecData *ecxt_param_exec_vals;		/* for PARAM_EXEC params */
	ParamListInfo ecxt_param_list_info; /* for other param types */
111

112
	/* Values to substitute for Aggref nodes in expression */
Bruce Momjian's avatar
Bruce Momjian committed
113 114
	Datum	   *ecxt_aggvalues; /* precomputed values for Aggref nodes */
	bool	   *ecxt_aggnulls;	/* null flags for Aggref nodes */
115

116 117 118 119
	/* Value to substitute for CaseTestExpr nodes in expression */
	Datum		caseValue_datum;
	bool		caseValue_isNull;

120
	/* Value to substitute for CoerceToDomainValue nodes in expression */
121 122 123
	Datum		domainValue_datum;
	bool		domainValue_isNull;

124 125 126
	/* Link to containing EState */
	struct EState *ecxt_estate;

127 128
	/* Functions to call back when ExprContext is shut down */
	ExprContext_CB *ecxt_callbacks;
129
} ExprContext;
130

131 132 133 134 135
/*
 * Set-result status returned by ExecEvalExpr()
 */
typedef enum
{
136 137 138
	ExprSingleResult,			/* expression does not return a set */
	ExprMultipleResult,			/* this result is an element of a set */
	ExprEndResult				/* there are no more elements in the set */
139 140
} ExprDoneCond;

141 142 143 144 145 146 147 148 149 150 151
/*
 * Return modes for functions returning sets.  Note values must be chosen
 * as separate bits so that a bitmask can be formed to indicate supported
 * modes.
 */
typedef enum
{
	SFRM_ValuePerCall = 0x01,	/* one value returned per call */
	SFRM_Materialize = 0x02		/* result set instantiated in Tuplestore */
} SetFunctionReturnMode;

152 153 154 155
/*
 * When calling a function that might return a set (multiple rows),
 * a node of this type is passed as fcinfo->resultinfo to allow
 * return status to be passed back.  A function returning set should
156
 * raise an error if no such resultinfo is provided.
157 158 159 160
 */
typedef struct ReturnSetInfo
{
	NodeTag		type;
161
	/* values set by caller: */
162
	ExprContext *econtext;		/* context function is being called in */
163
	TupleDesc	expectedDesc;	/* tuple descriptor expected by caller */
164
	int			allowedModes;	/* bitmask: return modes caller can handle */
165 166
	/* result status from function (but pre-initialized by caller): */
	SetFunctionReturnMode returnMode;	/* actual return mode */
167
	ExprDoneCond isDone;		/* status for ValuePerCall mode */
168
	/* fields filled by function in Materialize return mode: */
Bruce Momjian's avatar
Bruce Momjian committed
169
	Tuplestorestate *setResult; /* holds the complete returned tuple set */
170
	TupleDesc	setDesc;		/* actual descriptor for returned tuples */
171 172
} ReturnSetInfo;

173
/* ----------------
174 175
 *		ProjectionInfo node information
 *
176 177 178
 *		This is all the information needed to perform projections ---
 *		that is, form new tuples by evaluation of targetlist expressions.
 *		Nodes which need to do projections create one of these.
179
 *
180
 *		ExecProject() evaluates the tlist, forms a tuple, and stores it
181 182 183 184 185 186 187 188 189
 *		in the given slot.	Note that the result will be a "virtual" tuple
 *		unless ExecMaterializeSlot() is then called to force it to be
 *		converted to a physical tuple.  The slot must have a tupledesc
 *		that matches the output of the tlist!
 *
 *		The planner very often produces tlists that consist entirely of
 *		simple Var references (lower levels of a plan tree almost always
 *		look like that).  So we have an optimization to handle that case
 *		with minimum overhead.
190
 *
191
 *		targetlist		target list for projection
192
 *		exprContext		expression context in which to evaluate targetlist
193
 *		slot			slot to place projection result in
194
 *		itemIsDone		workspace for ExecProject
195 196 197 198 199 200
 *		isVarList		TRUE if simple-Var-list optimization applies
 *		varSlotOffsets	array indicating which slot each simple Var is from
 *		varNumbers		array indicating attr numbers of simple Vars
 *		lastInnerVar	highest attnum from inner tuple slot (0 if none)
 *		lastOuterVar	highest attnum from outer tuple slot (0 if none)
 *		lastScanVar		highest attnum from scan tuple slot (0 if none)
201 202
 * ----------------
 */
203 204
typedef struct ProjectionInfo
{
205 206 207
	NodeTag		type;
	List	   *pi_targetlist;
	ExprContext *pi_exprContext;
208
	TupleTableSlot *pi_slot;
209
	ExprDoneCond *pi_itemIsDone;
210 211 212 213 214 215
	bool		pi_isVarList;
	int		   *pi_varSlotOffsets;
	int		   *pi_varNumbers;
	int			pi_lastInnerVar;
	int			pi_lastOuterVar;
	int			pi_lastScanVar;
216
} ProjectionInfo;
217 218

/* ----------------
219 220
 *	  JunkFilter
 *
221
 *	  This class is used to store information regarding junk attributes.
222 223
 *	  A junk attribute is an attribute in a tuple that is needed only for
 *	  storing intermediate information in the executor, and does not belong
224
 *	  in emitted tuples.  For example, when we do an UPDATE query,
225 226
 *	  the planner adds a "junk" entry to the targetlist so that the tuples
 *	  returned to ExecutePlan() contain an extra attribute: the ctid of
227
 *	  the tuple to be updated.	This is needed to do the update, but we
228 229 230
 *	  don't want the ctid to be part of the stored new tuple!  So, we
 *	  apply a "junk filter" to remove the junk attributes and form the
 *	  real output tuple.
231 232
 *
 *	  targetList:		the original target list (including junk attributes).
233
 *	  cleanTupType:		the tuple descriptor for the "clean" tuple (with
234
 *						junk attributes removed).
235 236 237
 *	  cleanMap:			A map with the correspondence between the non-junk
 *						attribute numbers of the "original" tuple and the
 *						attribute numbers of the "clean" tuple.
238
 *	  resultSlot:		tuple slot used to hold cleaned tuple.
239 240
 * ----------------
 */
241 242
typedef struct JunkFilter
{
243 244 245 246
	NodeTag		type;
	List	   *jf_targetList;
	TupleDesc	jf_cleanTupType;
	AttrNumber *jf_cleanMap;
247
	TupleTableSlot *jf_resultSlot;
248
} JunkFilter;
249

250 251 252
/* ----------------
 *	  ResultRelInfo information
 *
253 254 255 256
 *		Whenever we update an existing relation, we have to
 *		update indices on the relation, and perhaps also fire triggers.
 *		The ResultRelInfo class is used to hold all the information needed
 *		about a result relation, including indices.. -cim 10/15/89
257 258 259 260 261 262
 *
 *		RangeTableIndex			result relation's range table index
 *		RelationDesc			relation descriptor for result relation
 *		NumIndices				# of indices existing on result relation
 *		IndexRelationDescs		array of relation descriptors for indices
 *		IndexRelationInfo		array of key/attr info for indices
263 264
 *		TrigDesc				triggers to be fired, if any
 *		TrigFunctions			cached lookup info for trigger functions
265
 *		TrigInstrument			optional runtime measurements for triggers
266
 *		ConstraintExprs			array of constraint-checking expr states
267 268 269 270 271 272 273 274 275 276 277
 *		junkFilter				for removing junk attributes from tuples
 * ----------------
 */
typedef struct ResultRelInfo
{
	NodeTag		type;
	Index		ri_RangeTableIndex;
	Relation	ri_RelationDesc;
	int			ri_NumIndices;
	RelationPtr ri_IndexRelationDescs;
	IndexInfo **ri_IndexRelationInfo;
278 279
	TriggerDesc *ri_TrigDesc;
	FmgrInfo   *ri_TrigFunctions;
280
	struct Instrumentation *ri_TrigInstrument;
281 282 283 284
	List	  **ri_ConstraintExprs;
	JunkFilter *ri_junkFilter;
} ResultRelInfo;

285
/* ----------------
286
 *	  EState information
287
 *
288
 * Master working state for an Executor invocation
289
 * ----------------
290
 */
291 292
typedef struct EState
{
Bruce Momjian's avatar
Bruce Momjian committed
293
	NodeTag		type;
294 295

	/* Basic state for all query types: */
Bruce Momjian's avatar
Bruce Momjian committed
296
	ScanDirection es_direction; /* current scan direction */
297
	Snapshot	es_snapshot;	/* time qual to use */
Bruce Momjian's avatar
Bruce Momjian committed
298
	Snapshot	es_crosscheck_snapshot; /* crosscheck time qual for RI */
Bruce Momjian's avatar
Bruce Momjian committed
299
	List	   *es_range_table; /* List of RangeTableEntrys */
300 301

	/* Info about target table for insert/update/delete queries: */
302 303 304 305 306
	ResultRelInfo *es_result_relations; /* array of ResultRelInfos */
	int			es_num_result_relations;		/* length of array */
	ResultRelInfo *es_result_relation_info;		/* currently active array
												 * elt */
	JunkFilter *es_junkFilter;	/* currently active junk filter */
Bruce Momjian's avatar
Bruce Momjian committed
307
	Relation	es_into_relation_descriptor;	/* for SELECT INTO */
308 309

	/* Parameter info: */
310 311
	ParamListInfo es_param_list_info;	/* values of external params */
	ParamExecData *es_param_exec_vals;	/* values of internal params */
312 313 314 315 316 317

	/* Other working state: */
	MemoryContext es_query_cxt; /* per-query context in which EState lives */

	TupleTable	es_tupleTable;	/* Array of TupleTableSlots */

Bruce Momjian's avatar
Bruce Momjian committed
318 319 320
	uint32		es_processed;	/* # of tuples processed */
	Oid			es_lastoid;		/* last oid processed (by INSERT) */
	List	   *es_rowMark;		/* not good place, but there is no other */
321
	bool		es_forUpdate;	/* was it FOR UPDATE or FOR SHARE */
322

323
	bool		es_instrument;	/* true requests runtime instrumentation */
Bruce Momjian's avatar
Bruce Momjian committed
324
	bool		es_select_into; /* true if doing SELECT INTO */
325
	bool		es_into_oids;	/* true to generate OIDs in SELECT INTO */
326

Bruce Momjian's avatar
Bruce Momjian committed
327
	List	   *es_exprcontexts;	/* List of ExprContexts within EState */
328

329 330
	/*
	 * this ExprContext is for per-output-tuple operations, such as
331 332 333
	 * constraint checks and index-value computations.	It will be reset
	 * for each output tuple.  Note that it will be created only if
	 * needed.
334 335
	 */
	ExprContext *es_per_tuple_exprcontext;
336

337
	/* Below is to re-evaluate plan qual in READ COMMITTED mode */
338
	Plan	   *es_topPlan;		/* link to top of plan tree */
Bruce Momjian's avatar
Bruce Momjian committed
339 340 341
	struct evalPlanQual *es_evalPlanQual;		/* chain of PlanQual
												 * states */
	bool	   *es_evTupleNull; /* local array of EPQ status */
342
	HeapTuple  *es_evTuple;		/* shared array of EPQ substitute tuples */
Bruce Momjian's avatar
Bruce Momjian committed
343
	bool		es_useEvalPlan; /* evaluating EPQ tuples? */
344
} EState;
345 346


347 348 349 350 351 352 353 354 355 356 357
/* ----------------------------------------------------------------
 *				 Tuple Hash Tables
 *
 * All-in-memory tuple hash tables are used for a number of purposes.
 * ----------------------------------------------------------------
 */
typedef struct TupleHashEntryData *TupleHashEntry;
typedef struct TupleHashTableData *TupleHashTable;

typedef struct TupleHashEntryData
{
358
	/* firstTuple must be the first field in this struct! */
359 360
	HeapTuple	firstTuple;		/* copy of first tuple in this group */
	/* there may be additional data beyond the end of this struct */
361
} TupleHashEntryData;			/* VARIABLE LENGTH STRUCT */
362 363 364

typedef struct TupleHashTableData
{
365
	HTAB	   *hashtab;		/* underlying dynahash table */
366 367 368
	int			numCols;		/* number of columns in lookup key */
	AttrNumber *keyColIdx;		/* attr numbers of key columns */
	FmgrInfo   *eqfunctions;	/* lookup data for comparison functions */
369
	FmgrInfo   *hashfunctions;	/* lookup data for hash functions */
370 371 372
	MemoryContext tablecxt;		/* memory context containing table */
	MemoryContext tempcxt;		/* context for function evaluations */
	Size		entrysize;		/* actual size to make each hash entry */
373 374
	TupleTableSlot *tableslot;	/* slot for referencing table entries */
	TupleTableSlot *inputslot;	/* current input tuple's slot */
375
} TupleHashTableData;
376

377
typedef HASH_SEQ_STATUS TupleHashIterator;
378

379 380 381 382
#define ResetTupleHashIterator(htable, iter) \
	hash_seq_init(iter, (htable)->hashtab)
#define ScanTupleHashTable(iter) \
	((TupleHashEntry) hash_seq_search(iter))
383 384


385
/* ----------------------------------------------------------------
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
 *				 Expression State Trees
 *
 * Each executable expression tree has a parallel ExprState tree.
 *
 * Unlike PlanState, there is not an exact one-for-one correspondence between
 * ExprState node types and Expr node types.  Many Expr node types have no
 * need for node-type-specific run-time state, and so they can use plain
 * ExprState or GenericExprState as their associated ExprState node type.
 * ----------------------------------------------------------------
 */

/* ----------------
 *		ExprState node
 *
 * ExprState is the common superclass for all ExprState-type nodes.
 *
 * It can also be instantiated directly for leaf Expr nodes that need no
 * local run-time state (such as Var, Const, or Param).
404 405 406
 *
 * To save on dispatch overhead, each ExprState node contains a function
 * pointer to the routine to execute to evaluate the node.
407 408
 * ----------------
 */
409 410 411 412

typedef struct ExprState ExprState;

typedef Datum (*ExprStateEvalFunc) (ExprState *expression,
Bruce Momjian's avatar
Bruce Momjian committed
413 414 415
												ExprContext *econtext,
												bool *isNull,
												ExprDoneCond *isDone);
416 417

struct ExprState
418 419 420
{
	NodeTag		type;
	Expr	   *expr;			/* associated Expr node */
Bruce Momjian's avatar
Bruce Momjian committed
421
	ExprStateEvalFunc evalfunc; /* routine to run to execute node */
422
};
423 424 425 426 427 428 429 430 431 432 433 434

/* ----------------
 *		GenericExprState node
 *
 * This is used for Expr node types that need no local run-time state,
 * but have one child Expr node.
 * ----------------
 */
typedef struct GenericExprState
{
	ExprState	xprstate;
	ExprState  *arg;			/* state of my child node */
435
} GenericExprState;
436 437 438 439 440 441 442 443 444 445

/* ----------------
 *		AggrefExprState node
 * ----------------
 */
typedef struct AggrefExprState
{
	ExprState	xprstate;
	ExprState  *target;			/* state of my child node */
	int			aggno;			/* ID number for agg within its plan node */
446
} AggrefExprState;
447 448 449

/* ----------------
 *		ArrayRefExprState node
450 451 452 453
 *
 * Note: array types can be fixed-length (typlen > 0), but only when the
 * element type is itself fixed-length.  Otherwise they are varlena structures
 * and have typlen = -1.  In any case, an array type is never pass-by-value.
454 455 456 457 458
 * ----------------
 */
typedef struct ArrayRefExprState
{
	ExprState	xprstate;
Bruce Momjian's avatar
Bruce Momjian committed
459
	List	   *refupperindexpr;	/* states for child nodes */
460 461 462
	List	   *reflowerindexpr;
	ExprState  *refexpr;
	ExprState  *refassgnexpr;
463 464 465 466
	int16		refattrlength;	/* typlen of array type */
	int16		refelemlength;	/* typlen of the array element type */
	bool		refelembyval;	/* is the element type pass-by-value? */
	char		refelemalign;	/* typalign of the element type */
467
} ArrayRefExprState;
468 469 470 471

/* ----------------
 *		FuncExprState node
 *
472 473 474
 * Although named for FuncExpr, this is also used for OpExpr, DistinctExpr,
 * and NullIf nodes; be careful to check what xprstate.expr is actually
 * pointing at!
475 476 477 478 479 480 481 482
 * ----------------
 */
typedef struct FuncExprState
{
	ExprState	xprstate;
	List	   *args;			/* states of argument expressions */

	/*
Bruce Momjian's avatar
Bruce Momjian committed
483 484
	 * Function manager's lookup info for the target function.  If
	 * func.fn_oid is InvalidOid, we haven't initialized it yet.
485 486 487 488
	 */
	FmgrInfo	func;

	/*
Bruce Momjian's avatar
Bruce Momjian committed
489 490
	 * We also need to store argument values across calls when evaluating
	 * a function-returning-set.
491
	 *
Bruce Momjian's avatar
Bruce Momjian committed
492 493
	 * setArgsValid is true when we are evaluating a set-valued function and
	 * we are in the middle of a call series; we want to pass the same
494 495 496 497 498 499 500 501 502 503 504 505
	 * argument values to the function again (and again, until it returns
	 * ExprEndResult).
	 */
	bool		setArgsValid;

	/*
	 * Flag to remember whether we found a set-valued argument to the
	 * function. This causes the function result to be a set as well.
	 * Valid only when setArgsValid is true.
	 */
	bool		setHasSetArg;	/* some argument returns a set */

506 507
	/*
	 * Flag to remember whether we have registered a shutdown callback for
Bruce Momjian's avatar
Bruce Momjian committed
508 509 510
	 * this FuncExprState.	We do so only if setArgsValid has been true at
	 * least once (since all the callback is for is to clear
	 * setArgsValid).
511 512 513
	 */
	bool		shutdown_reg;	/* a shutdown callback is registered */

514 515 516 517 518
	/*
	 * Current argument data for a set-valued function; contains valid
	 * data only if setArgsValid is true.
	 */
	FunctionCallInfoData setArgs;
519
} FuncExprState;
520

521 522 523 524 525 526 527 528
/* ----------------
 *		ScalarArrayOpExprState node
 *
 * This is a FuncExprState plus some additional data.
 * ----------------
 */
typedef struct ScalarArrayOpExprState
{
Bruce Momjian's avatar
Bruce Momjian committed
529
	FuncExprState fxprstate;
530
	/* Cached info about array element type */
Bruce Momjian's avatar
Bruce Momjian committed
531 532 533 534
	Oid			element_type;
	int16		typlen;
	bool		typbyval;
	char		typalign;
535
} ScalarArrayOpExprState;
536

537 538 539 540 541 542 543 544
/* ----------------
 *		BoolExprState node
 * ----------------
 */
typedef struct BoolExprState
{
	ExprState	xprstate;
	List	   *args;			/* states of argument expression(s) */
545
} BoolExprState;
546 547

/* ----------------
548
 *		SubPlanState node
549 550
 * ----------------
 */
551
typedef struct SubPlanState
552 553
{
	ExprState	xprstate;
554
	EState	   *sub_estate;		/* subselect plan has its own EState */
Bruce Momjian's avatar
Bruce Momjian committed
555
	struct PlanState *planstate;	/* subselect plan's state tree */
556 557
	List	   *exprs;			/* states of combining expression(s) */
	List	   *args;			/* states of argument expression(s) */
558 559
	bool		needShutdown;	/* TRUE = need to shutdown subplan */
	HeapTuple	curTuple;		/* copy of most recent tuple from subplan */
560
	/* these are used when hashing the subselect's output: */
561 562
	ProjectionInfo *projLeft;	/* for projecting lefthand exprs */
	ProjectionInfo *projRight;	/* for projecting subselect output */
563 564
	TupleHashTable hashtable;	/* hash table for no-nulls subselect rows */
	TupleHashTable hashnulls;	/* hash table for rows with null(s) */
565 566 567
	bool		havehashrows;	/* TRUE if hashtable is not empty */
	bool		havenullrows;	/* TRUE if hashnulls is not empty */
	MemoryContext tablecxt;		/* memory context containing tables */
Bruce Momjian's avatar
Bruce Momjian committed
568
	ExprContext *innerecontext; /* working context for comparisons */
569 570
	AttrNumber *keyColIdx;		/* control data for hash tables */
	FmgrInfo   *eqfunctions;	/* comparison functions for hash tables */
571
	FmgrInfo   *hashfunctions;	/* lookup data for hash functions */
572
} SubPlanState;
573

574 575 576 577 578 579 580 581 582 583 584
/* ----------------
 *		FieldSelectState node
 * ----------------
 */
typedef struct FieldSelectState
{
	ExprState	xprstate;
	ExprState  *arg;			/* input expression */
	TupleDesc	argdesc;		/* tupdesc for most recent input */
} FieldSelectState;

585 586 587 588 589 590 591 592 593 594 595 596
/* ----------------
 *		FieldStoreState node
 * ----------------
 */
typedef struct FieldStoreState
{
	ExprState	xprstate;
	ExprState  *arg;			/* input tuple value */
	List	   *newvals;		/* new value(s) for field(s) */
	TupleDesc	argdesc;		/* tupdesc for most recent input */
} FieldStoreState;

597 598 599 600 601 602 603 604 605 606 607 608
/* ----------------
 *		ConvertRowtypeExprState node
 * ----------------
 */
typedef struct ConvertRowtypeExprState
{
	ExprState	xprstate;
	ExprState  *arg;			/* input tuple value */
	TupleDesc	indesc;			/* tupdesc for source rowtype */
	TupleDesc	outdesc;		/* tupdesc for result rowtype */
	AttrNumber *attrMap;		/* indexes of input fields, or 0 for null */
	Datum	   *invalues;		/* workspace for deconstructing source */
609
	bool	   *inisnull;
610
	Datum	   *outvalues;		/* workspace for constructing result */
611
	bool	   *outisnull;
612 613
} ConvertRowtypeExprState;

614 615 616 617 618 619 620
/* ----------------
 *		CaseExprState node
 * ----------------
 */
typedef struct CaseExprState
{
	ExprState	xprstate;
621
	ExprState  *arg;			/* implicit equality comparison argument */
622 623
	List	   *args;			/* the arguments (list of WHEN clauses) */
	ExprState  *defresult;		/* the default result (ELSE clause) */
624
} CaseExprState;
625 626 627 628 629 630 631 632 633 634

/* ----------------
 *		CaseWhenState node
 * ----------------
 */
typedef struct CaseWhenState
{
	ExprState	xprstate;
	ExprState  *expr;			/* condition expression */
	ExprState  *result;			/* substitution result */
635
} CaseWhenState;
636

637 638 639 640 641 642 643 644 645 646 647 648 649 650
/* ----------------
 *		ArrayExprState node
 *
 * Note: ARRAY[] expressions always produce varlena arrays, never fixed-length
 * arrays.
 * ----------------
 */
typedef struct ArrayExprState
{
	ExprState	xprstate;
	List	   *elements;		/* states for child nodes */
	int16		elemlength;		/* typlen of the array element type */
	bool		elembyval;		/* is the element type pass-by-value? */
	char		elemalign;		/* typalign of the element type */
651
} ArrayExprState;
652

653 654 655 656 657 658 659 660 661 662 663
/* ----------------
 *		RowExprState node
 * ----------------
 */
typedef struct RowExprState
{
	ExprState	xprstate;
	List	   *args;			/* the arguments */
	TupleDesc	tupdesc;		/* descriptor for result tuples */
} RowExprState;

664 665 666 667 668 669 670
/* ----------------
 *		CoalesceExprState node
 * ----------------
 */
typedef struct CoalesceExprState
{
	ExprState	xprstate;
Bruce Momjian's avatar
Bruce Momjian committed
671
	List	   *args;			/* the arguments */
672
} CoalesceExprState;
673

674
/* ----------------
675
 *		CoerceToDomainState node
676 677
 * ----------------
 */
678
typedef struct CoerceToDomainState
679 680 681
{
	ExprState	xprstate;
	ExprState  *arg;			/* input expression */
682 683
	/* Cached list of constraints that need to be checked */
	List	   *constraints;	/* list of DomainConstraintState nodes */
684
} CoerceToDomainState;
685 686 687 688 689 690 691 692 693 694 695 696

/*
 * DomainConstraintState - one item to check during CoerceToDomain
 *
 * Note: this is just a Node, and not an ExprState, because it has no
 * corresponding Expr to link to.  Nonetheless it is part of an ExprState
 * tree, so we give it a name following the xxxState convention.
 */
typedef enum DomainConstraintType
{
	DOM_CONSTRAINT_NOTNULL,
	DOM_CONSTRAINT_CHECK
697
} DomainConstraintType;
698 699 700 701

typedef struct DomainConstraintState
{
	NodeTag		type;
Bruce Momjian's avatar
Bruce Momjian committed
702
	DomainConstraintType constrainttype;		/* constraint type */
703 704
	char	   *name;			/* name of constraint (for error msgs) */
	ExprState  *check_expr;		/* for CHECK, a boolean expression */
705
} DomainConstraintState;
706 707 708 709 710 711 712


/* ----------------------------------------------------------------
 *				 Executor State Trees
 *
 * An executing query has a PlanState tree paralleling the Plan tree
 * that describes the plan.
713 714 715 716
 * ----------------------------------------------------------------
 */

/* ----------------
717
 *		PlanState node
718
 *
719 720
 * We never actually instantiate any PlanState nodes; this is just the common
 * abstract superclass for all PlanState-type nodes.
721 722
 * ----------------
 */
723
typedef struct PlanState
724
{
725
	NodeTag		type;
726

727
	Plan	   *plan;			/* associated Plan node */
728

729 730 731 732 733 734 735 736
	EState	   *state;			/* at execution time, state's of
								 * individual nodes point to one EState
								 * for the whole top-level plan */

	struct Instrumentation *instrument; /* Optional runtime stats for this
										 * plan node */

	/*
Bruce Momjian's avatar
Bruce Momjian committed
737 738 739 740
	 * Common structural data for all Plan types.  These links to
	 * subsidiary state trees parallel links in the associated plan tree
	 * (except for the subPlan list, which does not exist in the plan
	 * tree).
741 742 743
	 */
	List	   *targetlist;		/* target list to be computed at this node */
	List	   *qual;			/* implicitly-ANDed qual conditions */
Bruce Momjian's avatar
Bruce Momjian committed
744
	struct PlanState *lefttree; /* input plan tree(s) */
745
	struct PlanState *righttree;
746
	List	   *initPlan;		/* Init SubPlanState nodes (un-correlated
747
								 * expr subselects) */
748
	List	   *subPlan;		/* SubPlanState nodes in my expressions */
749 750 751 752

	/*
	 * State for management of parameter-change-driven rescanning
	 */
753
	Bitmapset  *chgParam;		/* set of IDs of changed Params */
754 755 756 757

	/*
	 * Other run-time state needed by most if not all node types.
	 */
Bruce Momjian's avatar
Bruce Momjian committed
758 759 760 761 762 763
	TupleTableSlot *ps_OuterTupleSlot;	/* slot for current "outer" tuple */
	TupleTableSlot *ps_ResultTupleSlot; /* slot for my result tuples */
	ExprContext *ps_ExprContext;	/* node's expression-evaluation context */
	ProjectionInfo *ps_ProjInfo;	/* info for doing tuple projection */
	bool		ps_TupFromTlist;/* state flag for processing set-valued
								 * functions in targetlist */
764
} PlanState;
765 766 767 768 769 770 771

/* ----------------
 *	these are are defined to avoid confusion problems with "left"
 *	and "right" and "inner" and "outer".  The convention is that
 *	the "left" plan is the "outer" plan and the "right" plan is
 *	the inner plan, but these make the code more readable.
 * ----------------
772
 */
773 774 775
#define innerPlanState(node)		(((PlanState *)(node))->righttree)
#define outerPlanState(node)		(((PlanState *)(node))->lefttree)

776 777

/* ----------------
778
 *	 ResultState information
779 780
 * ----------------
 */
781 782
typedef struct ResultState
{
783
	PlanState	ps;				/* its first field is NodeTag */
784
	ExprState  *resconstantqual;
785 786
	bool		rs_done;		/* are we done? */
	bool		rs_checkqual;	/* do we need to check the qual? */
787
} ResultState;
788 789

/* ----------------
790 791
 *	 AppendState information
 *
792
 *		nplans			how many plans are in the list
793
 *		whichplan		which plan is being executed (0 .. n-1)
794 795
 *		firstplan		first plan to execute (usually 0)
 *		lastplan		last plan to execute (usually n-1)
796 797
 * ----------------
 */
798 799
typedef struct AppendState
{
800 801 802
	PlanState	ps;				/* its first field is NodeTag */
	PlanState **appendplans;	/* array of PlanStates for my inputs */
	int			as_nplans;
803
	int			as_whichplan;
804 805
	int			as_firstplan;
	int			as_lastplan;
Bruce Momjian's avatar
Bruce Momjian committed
806
} AppendState;
807

808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
/* ----------------
 *	 BitmapAndState information
 * ----------------
 */
typedef struct BitmapAndState
{
	PlanState	ps;				/* its first field is NodeTag */
	PlanState **bitmapplans;	/* array of PlanStates for my inputs */
	int			nplans;			/* number of input plans */
} BitmapAndState;

/* ----------------
 *	 BitmapOrState information
 * ----------------
 */
typedef struct BitmapOrState
{
	PlanState	ps;				/* its first field is NodeTag */
	PlanState **bitmapplans;	/* array of PlanStates for my inputs */
	int			nplans;			/* number of input plans */
} BitmapOrState;

830
/* ----------------------------------------------------------------
831
 *				 Scan State Information
832 833 834 835
 * ----------------------------------------------------------------
 */

/* ----------------
836
 *	 ScanState information
837
 *
838
 *		ScanState extends PlanState for node types that represent
839 840 841 842
 *		scans of an underlying relation.  It can also be used for nodes
 *		that scan the output of an underlying plan node --- in that case,
 *		only ScanTupleSlot is actually useful, and it refers to the tuple
 *		retrieved from the subplan.
843
 *
844 845
 *		currentRelation    relation being scanned (NULL if none)
 *		currentScanDesc    current scan descriptor for scan (NULL if none)
846
 *		ScanTupleSlot	   pointer to slot in tuple table holding scan tuple
847 848
 * ----------------
 */
849
typedef struct ScanState
850
{
851 852 853 854
	PlanState	ps;				/* its first field is NodeTag */
	Relation	ss_currentRelation;
	HeapScanDesc ss_currentScanDesc;
	TupleTableSlot *ss_ScanTupleSlot;
855
} ScanState;
856

857
/*
858
 * SeqScan uses a bare ScanState as its state node, since it needs
859 860
 * no additional fields.
 */
861
typedef ScanState SeqScanState;
862

863
/* ----------------
864 865
 *	 IndexScanState information
 *
866 867 868 869
 *		indexqualorig	   execution state for indexqualorig expressions
 *		ScanKeys		   Skey structures to scan index rel
 *		NumScanKeys		   number of Skey structs
 *		RuntimeKeyInfo	   array of exprstates for Skeys
870
 *						   that will be evaluated at runtime
871
 *		RuntimeContext	   expr context for evaling runtime Skeys
872
 *		RuntimeKeysReady   true if runtime Skeys have been computed
873 874
 *		RelationDesc	   index relation descriptor
 *		ScanDesc		   index scan descriptor
875 876
 * ----------------
 */
877 878
typedef struct IndexScanState
{
879
	ScanState	ss;				/* its first field is NodeTag */
880 881 882 883
	List	   *indexqualorig;
	ScanKey		iss_ScanKeys;
	int			iss_NumScanKeys;
	ExprState **iss_RuntimeKeyInfo;
884
	ExprContext *iss_RuntimeContext;
885
	bool		iss_RuntimeKeysReady;
886 887
	Relation	iss_RelationDesc;
	IndexScanDesc iss_ScanDesc;
888
} IndexScanState;
889

890 891 892
/* ----------------
 *	 BitmapIndexScanState information
 *
893
 *		result			   bitmap to return output into, or NULL
894 895 896 897 898 899
 *		ScanKeys		   Skey structures to scan index rel
 *		NumScanKeys		   number of Skey structs
 *		RuntimeKeyInfo	   array of exprstates for Skeys
 *						   that will be evaluated at runtime
 *		RuntimeContext	   expr context for evaling runtime Skeys
 *		RuntimeKeysReady   true if runtime Skeys have been computed
900 901
 *		RelationDesc	   index relation descriptor
 *		ScanDesc		   index scan descriptor
902 903 904 905 906
 * ----------------
 */
typedef struct BitmapIndexScanState
{
	ScanState	ss;				/* its first field is NodeTag */
907
	TIDBitmap  *biss_result;
908 909 910 911 912
	ScanKey		biss_ScanKeys;
	int			biss_NumScanKeys;
	ExprState **biss_RuntimeKeyInfo;
	ExprContext *biss_RuntimeContext;
	bool		biss_RuntimeKeysReady;
913 914
	Relation	biss_RelationDesc;
	IndexScanDesc biss_ScanDesc;
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
} BitmapIndexScanState;

/* ----------------
 *	 BitmapHeapScanState information
 *
 *		bitmapqualorig	   execution state for bitmapqualorig expressions
 *		tbm				   bitmap obtained from child index scan(s)
 *		tbmres			   current-page data
 *		curslot			   current tbmres index or tuple offset on page
 *		minslot			   lowest tbmres index or tuple offset to try
 *		maxslot			   highest tbmres index or tuple offset to try
 * ----------------
 */
typedef struct BitmapHeapScanState
{
	ScanState	ss;				/* its first field is NodeTag */
	List	   *bitmapqualorig;
	TIDBitmap  *tbm;
	TBMIterateResult *tbmres;
	int			curslot;
	int			minslot;
	int			maxslot;
} BitmapHeapScanState;

939 940 941 942
/* ----------------
 *	 TidScanState information
 *
 *		NumTids		   number of tids in this scan
943 944
 *		TidPtr		   current tid in use
 *		TidList		   evaluated item pointers
945 946 947 948
 * ----------------
 */
typedef struct TidScanState
{
949
	ScanState	ss;				/* its first field is NodeTag */
950
	List	   *tss_tideval;	/* list of ExprState nodes */
951 952 953
	int			tss_NumTids;
	int			tss_TidPtr;
	int			tss_MarkTidPtr;
954
	ItemPointerData *tss_TidList;
955
	HeapTupleData tss_htup;
956
} TidScanState;
957

958 959 960 961 962 963 964 965 966 967 968 969
/* ----------------
 *	 SubqueryScanState information
 *
 *		SubqueryScanState is used for scanning a sub-query in the range table.
 *		The sub-query will have its own EState, which we save here.
 *		ScanTupleSlot references the current output tuple of the sub-query.
 *
 *		SubEState		   exec state for sub-query
 * ----------------
 */
typedef struct SubqueryScanState
{
970 971
	ScanState	ss;				/* its first field is NodeTag */
	PlanState  *subplan;
972 973 974
	EState	   *sss_SubEState;
} SubqueryScanState;

975 976 977 978 979 980
/* ----------------
 *	 FunctionScanState information
 *
 *		Function nodes are used to scan the results of a
 *		function appearing in FROM (typically a function returning set).
 *
981
 *		tupdesc				expected return tuple description
982
 *		tuplestorestate		private state of tuplestore.c
983
 *		funcexpr			state for function expression being evaluated
984 985 986 987
 * ----------------
 */
typedef struct FunctionScanState
{
988
	ScanState	ss;				/* its first field is NodeTag */
Bruce Momjian's avatar
Bruce Momjian committed
989
	TupleDesc	tupdesc;
990
	Tuplestorestate *tuplestorestate;
991
	ExprState  *funcexpr;
992 993
} FunctionScanState;

994
/* ----------------------------------------------------------------
995
 *				 Join State Information
996 997 998 999
 * ----------------------------------------------------------------
 */

/* ----------------
1000
 *	 JoinState information
1001
 *
1002
 *		Superclass for state nodes of join plans.
1003 1004
 * ----------------
 */
1005 1006 1007 1008 1009 1010
typedef struct JoinState
{
	PlanState	ps;
	JoinType	jointype;
	List	   *joinqual;		/* JOIN quals (in addition to ps.qual) */
} JoinState;
1011 1012

/* ----------------
1013
 *	 NestLoopState information
1014
 *
1015 1016
 *		NeedNewOuter	   true if need new outer tuple on next call
 *		MatchedOuter	   true if found a join match for current outer tuple
1017
 *		NullInnerTupleSlot prepared null tuple for left outer joins
1018 1019
 * ----------------
 */
1020 1021
typedef struct NestLoopState
{
1022
	JoinState	js;				/* its first field is NodeTag */
1023 1024 1025
	bool		nl_NeedNewOuter;
	bool		nl_MatchedOuter;
	TupleTableSlot *nl_NullInnerTupleSlot;
1026
} NestLoopState;
1027 1028

/* ----------------
1029
 *	 MergeJoinState information
1030
 *
1031 1032
 *		OuterSkipQual	   outerKey1 < innerKey1 ...
 *		InnerSkipQual	   outerKey1 > innerKey1 ...
1033
 *		JoinState		   current "state" of join. see executor.h
1034 1035 1036 1037
 *		MatchedOuter	   true if found a join match for current outer tuple
 *		MatchedInner	   true if found a join match for current inner tuple
 *		OuterTupleSlot	   pointer to slot in tuple table for cur outer tuple
 *		InnerTupleSlot	   pointer to slot in tuple table for cur inner tuple
1038
 *		MarkedTupleSlot    pointer to slot in tuple table for marked tuple
1039 1040
 *		NullOuterTupleSlot prepared null tuple for right outer joins
 *		NullInnerTupleSlot prepared null tuple for left outer joins
1041 1042
 * ----------------
 */
1043 1044
typedef struct MergeJoinState
{
1045
	JoinState	js;				/* its first field is NodeTag */
Bruce Momjian's avatar
Bruce Momjian committed
1046 1047 1048
	List	   *mergeclauses;	/* list of ExprState nodes */
	List	   *mj_OuterSkipQual;		/* list of ExprState nodes */
	List	   *mj_InnerSkipQual;		/* list of ExprState nodes */
1049
	int			mj_JoinState;
1050 1051 1052 1053
	bool		mj_MatchedOuter;
	bool		mj_MatchedInner;
	TupleTableSlot *mj_OuterTupleSlot;
	TupleTableSlot *mj_InnerTupleSlot;
1054
	TupleTableSlot *mj_MarkedTupleSlot;
1055 1056
	TupleTableSlot *mj_NullOuterTupleSlot;
	TupleTableSlot *mj_NullInnerTupleSlot;
1057
} MergeJoinState;
1058 1059

/* ----------------
1060 1061
 *	 HashJoinState information
 *
1062
 *		hj_HashTable			hash table for the hashjoin
1063 1064
 *								(NULL if table not built yet)
 *		hj_CurHashValue			hash value for current outer tuple
1065 1066 1067
 *		hj_CurBucketNo			bucket# for current outer tuple
 *		hj_CurTuple				last inner tuple matched to current outer
 *								tuple, or NULL if starting search
1068 1069
 *								(CurHashValue, CurBucketNo and CurTuple are
 *								 undefined if OuterTupleSlot is empty!)
1070 1071
 *		hj_OuterHashKeys		the outer hash keys in the hashjoin condition
 *		hj_InnerHashKeys		the inner hash keys in the hashjoin condition
1072
 *		hj_HashOperators		the join operators in the hashjoin condition
1073 1074
 *		hj_OuterTupleSlot		tuple slot for outer tuples
 *		hj_HashTupleSlot		tuple slot for hashed tuples
1075 1076 1077
 *		hj_NullInnerTupleSlot	prepared null tuple for left outer joins
 *		hj_NeedNewOuter			true if need new outer tuple on next call
 *		hj_MatchedOuter			true if found a join match for current outer
1078 1079
 * ----------------
 */
1080 1081 1082 1083 1084

/* these structs are defined in executor/hashjoin.h: */
typedef struct HashJoinTupleData *HashJoinTuple;
typedef struct HashJoinTableData *HashJoinTable;

1085 1086
typedef struct HashJoinState
{
1087
	JoinState	js;				/* its first field is NodeTag */
1088
	List	   *hashclauses;	/* list of ExprState nodes */
Bruce Momjian's avatar
Bruce Momjian committed
1089
	HashJoinTable hj_HashTable;
1090
	uint32		hj_CurHashValue;
Bruce Momjian's avatar
Bruce Momjian committed
1091 1092
	int			hj_CurBucketNo;
	HashJoinTuple hj_CurTuple;
Bruce Momjian's avatar
Bruce Momjian committed
1093 1094 1095
	List	   *hj_OuterHashKeys;		/* list of ExprState nodes */
	List	   *hj_InnerHashKeys;		/* list of ExprState nodes */
	List	   *hj_HashOperators;		/* list of operator OIDs */
1096 1097
	TupleTableSlot *hj_OuterTupleSlot;
	TupleTableSlot *hj_HashTupleSlot;
1098 1099 1100
	TupleTableSlot *hj_NullInnerTupleSlot;
	bool		hj_NeedNewOuter;
	bool		hj_MatchedOuter;
1101
} HashJoinState;
1102 1103 1104


/* ----------------------------------------------------------------
1105
 *				 Materialization State Information
1106 1107 1108 1109
 * ----------------------------------------------------------------
 */

/* ----------------
1110
 *	 MaterialState information
1111
 *
1112
 *		materialize nodes are used to materialize the results
1113
 *		of a subplan into a temporary file.
1114
 *
1115
 *		ss.ss_ScanTupleSlot refers to output of underlying plan.
1116 1117
 * ----------------
 */
1118 1119
typedef struct MaterialState
{
1120
	ScanState	ss;				/* its first field is NodeTag */
Bruce Momjian's avatar
Bruce Momjian committed
1121 1122
	void	   *tuplestorestate;	/* private state of tuplestore.c */
	bool		eof_underlying; /* reached end of underlying plan? */
1123
} MaterialState;
1124

1125 1126 1127 1128 1129 1130 1131 1132
/* ----------------
 *	 SortState information
 * ----------------
 */
typedef struct SortState
{
	ScanState	ss;				/* its first field is NodeTag */
	bool		sort_Done;		/* sort completed yet? */
Bruce Momjian's avatar
Bruce Momjian committed
1133
	void	   *tuplesortstate; /* private state of tuplesort.c */
1134 1135
} SortState;

1136
/* ---------------------
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
 *	GroupState information
 * -------------------------
 */
typedef struct GroupState
{
	ScanState	ss;				/* its first field is NodeTag */
	FmgrInfo   *eqfunctions;	/* per-field lookup data for equality fns */
	bool		grp_done;		/* indicates completion of Group scan */
} GroupState;

/* ---------------------
 *	AggState information
1149
 *
1150
 *	ss.ss_ScanTupleSlot refers to output of underlying plan.
1151
 *
1152
 *	Note: ss.ps.ps_ExprContext contains ecxt_aggvalues and
1153 1154 1155 1156
 *	ecxt_aggnulls arrays, which hold the computed agg values for the current
 *	input group during evaluation of an Agg node's output tuple(s).  We
 *	create a second ExprContext, tmpcontext, in which to evaluate input
 *	expressions and run the aggregate transition functions.
1157 1158
 * -------------------------
 */
1159 1160 1161
/* these structs are private in nodeAgg.c: */
typedef struct AggStatePerAggData *AggStatePerAgg;
typedef struct AggStatePerGroupData *AggStatePerGroup;
1162

1163 1164
typedef struct AggState
{
1165
	ScanState	ss;				/* its first field is NodeTag */
1166
	List	   *aggs;			/* all Aggref nodes in targetlist & quals */
1167
	int			numaggs;		/* length of list (could be zero!) */
1168
	FmgrInfo   *eqfunctions;	/* per-grouping-field equality fns */
1169
	FmgrInfo   *hashfunctions;	/* per-grouping-field hash fns */
1170 1171 1172
	AggStatePerAgg peragg;		/* per-Aggref information */
	MemoryContext aggcontext;	/* memory context for long-lived data */
	ExprContext *tmpcontext;	/* econtext for input expressions */
1173
	bool		agg_done;		/* indicates completion of Agg scan */
1174 1175
	/* these fields are used in AGG_PLAIN and AGG_SORTED modes: */
	AggStatePerGroup pergroup;	/* per-Aggref-per-group working state */
Bruce Momjian's avatar
Bruce Momjian committed
1176
	HeapTuple	grp_firstTuple; /* copy of first tuple of current group */
1177
	/* these fields are used in AGG_HASHED mode: */
1178
	TupleHashTable hashtable;	/* hash table with one entry per group */
1179
	bool		table_filled;	/* hash table filled yet? */
Bruce Momjian's avatar
Bruce Momjian committed
1180
	TupleHashIterator hashiter; /* for iterating through hash table */
Bruce Momjian's avatar
Bruce Momjian committed
1181
} AggState;
1182 1183

/* ----------------
1184 1185 1186 1187 1188
 *	 UniqueState information
 *
 *		Unique nodes are used "on top of" sort nodes to discard
 *		duplicate tuples returned from the sort phase.	Basically
 *		all it does is compare the current tuple from the subplan
1189
 *		with the previously fetched tuple (stored in its result slot).
1190 1191
 *		If the two are identical in all interesting fields, then
 *		we just fetch another tuple from the sort and try again.
1192 1193
 * ----------------
 */
1194 1195
typedef struct UniqueState
{
1196
	PlanState	ps;				/* its first field is NodeTag */
1197
	FmgrInfo   *eqfunctions;	/* per-field lookup data for equality fns */
1198
	MemoryContext tempContext;	/* short-term context for comparisons */
1199
} UniqueState;
1200

1201 1202 1203 1204 1205 1206 1207 1208
/* ----------------
 *	 HashState information
 * ----------------
 */
typedef struct HashState
{
	PlanState	ps;				/* its first field is NodeTag */
	HashJoinTable hashtable;	/* hash table for the hashjoin */
1209 1210
	List	   *hashkeys;		/* list of ExprState nodes */
	/* hashkeys is same as parent's hj_InnerHashKeys */
1211 1212
} HashState;

1213 1214 1215 1216
/* ----------------
 *	 SetOpState information
 *
 *		SetOp nodes are used "on top of" sort nodes to discard
1217
 *		duplicate tuples returned from the sort phase.	These are
1218 1219 1220 1221 1222 1223
 *		more complex than a simple Unique since we have to count
 *		how many duplicates to return.
 * ----------------
 */
typedef struct SetOpState
{
1224
	PlanState	ps;				/* its first field is NodeTag */
1225 1226 1227 1228 1229 1230 1231 1232
	FmgrInfo   *eqfunctions;	/* per-field lookup data for equality fns */
	bool		subplan_done;	/* has subplan returned EOF? */
	long		numLeft;		/* number of left-input dups of cur group */
	long		numRight;		/* number of right-input dups of cur group */
	long		numOutput;		/* number of dups left to output */
	MemoryContext tempContext;	/* short-term context for comparisons */
} SetOpState;

1233 1234 1235 1236 1237 1238 1239 1240 1241
/* ----------------
 *	 LimitState information
 *
 *		Limit nodes are used to enforce LIMIT/OFFSET clauses.
 *		They just select the desired subrange of their subplan's output.
 *
 * offset is the number of initial tuples to skip (0 does nothing).
 * count is the number of tuples to return after skipping the offset tuples.
 * If no limit count was specified, count is undefined and noCount is true.
1242
 * When lstate == LIMIT_INITIAL, offset/count/noCount haven't been set yet.
1243 1244
 * ----------------
 */
1245 1246 1247 1248 1249 1250 1251 1252
typedef enum
{
	LIMIT_INITIAL,				/* initial state for LIMIT node */
	LIMIT_EMPTY,				/* there are no returnable rows */
	LIMIT_INWINDOW,				/* have returned a row in the window */
	LIMIT_SUBPLANEOF,			/* at EOF of subplan (within window) */
	LIMIT_WINDOWEND,			/* stepped off end of window */
	LIMIT_WINDOWSTART			/* stepped off beginning of window */
1253
} LimitStateCond;
1254

1255 1256
typedef struct LimitState
{
1257
	PlanState	ps;				/* its first field is NodeTag */
1258 1259
	ExprState  *limitOffset;	/* OFFSET parameter, or NULL if none */
	ExprState  *limitCount;		/* COUNT parameter, or NULL if none */
1260 1261 1262
	long		offset;			/* current OFFSET value */
	long		count;			/* current COUNT, if any */
	bool		noCount;		/* if true, ignore count */
1263 1264 1265
	LimitStateCond lstate;		/* state machine status, as above */
	long		position;		/* 1-based index of last tuple returned */
	TupleTableSlot *subSlot;	/* tuple last obtained from subplan */
1266 1267
} LimitState;

1268
#endif   /* EXECNODES_H */