1 /*-------------------------------------------------------------------------
2  *
3  * execnodes.h
4  *	  definitions for executor state nodes
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/nodes/execnodes.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef EXECNODES_H
15 #define EXECNODES_H
16 
17 #include "access/genam.h"
18 #include "access/heapam.h"
19 #include "access/tupconvert.h"
20 #include "executor/instrument.h"
21 #include "lib/pairingheap.h"
22 #include "nodes/params.h"
23 #include "nodes/plannodes.h"
24 #include "utils/hsearch.h"
25 #include "utils/queryenvironment.h"
26 #include "utils/reltrigger.h"
27 #include "utils/sortsupport.h"
28 #include "utils/tuplestore.h"
29 #include "utils/tuplesort.h"
30 #include "nodes/tidbitmap.h"
31 #include "storage/condition_variable.h"
32 
33 
34 /* ----------------
35  *		ExprState node
36  *
37  * ExprState is the top-level node for expression evaluation.
38  * It contains instructions (in ->steps) to evaluate the expression.
39  * ----------------
40  */
41 struct ExprState;				/* forward references in this file */
42 struct ExprContext;
43 struct ExprEvalStep;			/* avoid including execExpr.h everywhere */
44 
45 typedef Datum (*ExprStateEvalFunc) (struct ExprState *expression,
46 									struct ExprContext *econtext,
47 									bool *isNull);
48 
49 /* Bits in ExprState->flags (see also execExpr.h for private flag bits): */
50 /* expression is for use with ExecQual() */
51 #define EEO_FLAG_IS_QUAL					(1 << 0)
52 
53 typedef struct ExprState
54 {
55 	Node		tag;
56 
57 	uint8		flags;			/* bitmask of EEO_FLAG_* bits, see above */
58 
59 	/*
60 	 * Storage for result value of a scalar expression, or for individual
61 	 * column results within expressions built by ExecBuildProjectionInfo().
62 	 */
63 	bool		resnull;
64 	Datum		resvalue;
65 
66 	/*
67 	 * If projecting a tuple result, this slot holds the result; else NULL.
68 	 */
69 	TupleTableSlot *resultslot;
70 
71 	/*
72 	 * Instructions to compute expression's return value.
73 	 */
74 	struct ExprEvalStep *steps;
75 
76 	/*
77 	 * Function that actually evaluates the expression.  This can be set to
78 	 * different values depending on the complexity of the expression.
79 	 */
80 	ExprStateEvalFunc evalfunc;
81 
82 	/* original expression tree, for debugging only */
83 	Expr	   *expr;
84 
85 	/*
86 	 * XXX: following only needed during "compilation", could be thrown away.
87 	 */
88 
89 	int			steps_len;		/* number of steps currently */
90 	int			steps_alloc;	/* allocated length of steps array */
91 
92 	Datum	   *innermost_caseval;
93 	bool	   *innermost_casenull;
94 
95 	Datum	   *innermost_domainval;
96 	bool	   *innermost_domainnull;
97 } ExprState;
98 
99 
100 /* ----------------
101  *	  IndexInfo information
102  *
103  *		this struct holds the information needed to construct new index
104  *		entries for a particular index.  Used for both index_build and
105  *		retail creation of index entries.
106  *
107  *		NumIndexAttrs		number of columns in this index
108  *		KeyAttrNumbers		underlying-rel attribute numbers used as keys
109  *							(zeroes indicate expressions)
110  *		Expressions			expr trees for expression entries, or NIL if none
111  *		ExpressionsState	exec state for expressions, or NIL if none
112  *		Predicate			partial-index predicate, or NIL if none
113  *		PredicateState		exec state for predicate, or NIL if none
114  *		ExclusionOps		Per-column exclusion operators, or NULL if none
115  *		ExclusionProcs		Underlying function OIDs for ExclusionOps
116  *		ExclusionStrats		Opclass strategy numbers for ExclusionOps
117  *		UniqueOps			Theses are like Exclusion*, but for unique indexes
118  *		UniqueProcs
119  *		UniqueStrats
120  *		Unique				is it a unique index?
121  *		ReadyForInserts		is it valid for inserts?
122  *		Concurrent			are we doing a concurrent index build?
123  *		BrokenHotChain		did we detect any broken HOT chains?
124  *		AmCache				private cache area for index AM
125  *		Context				memory context holding this IndexInfo
126  *
127  * ii_Concurrent and ii_BrokenHotChain are used only during index build;
128  * they're conventionally set to false otherwise.
129  * ----------------
130  */
131 typedef struct IndexInfo
132 {
133 	NodeTag		type;
134 	int			ii_NumIndexAttrs;
135 	AttrNumber	ii_KeyAttrNumbers[INDEX_MAX_KEYS];
136 	List	   *ii_Expressions; /* list of Expr */
137 	List	   *ii_ExpressionsState;	/* list of ExprState */
138 	List	   *ii_Predicate;	/* list of Expr */
139 	ExprState  *ii_PredicateState;
140 	Oid		   *ii_ExclusionOps;	/* array with one entry per column */
141 	Oid		   *ii_ExclusionProcs;	/* array with one entry per column */
142 	uint16	   *ii_ExclusionStrats; /* array with one entry per column */
143 	Oid		   *ii_UniqueOps;	/* array with one entry per column */
144 	Oid		   *ii_UniqueProcs; /* array with one entry per column */
145 	uint16	   *ii_UniqueStrats;	/* array with one entry per column */
146 	bool		ii_Unique;
147 	bool		ii_ReadyForInserts;
148 	bool		ii_Concurrent;
149 	bool		ii_BrokenHotChain;
150 	void	   *ii_AmCache;
151 	MemoryContext ii_Context;
152 } IndexInfo;
153 
154 /* ----------------
155  *	  ExprContext_CB
156  *
157  *		List of callbacks to be called at ExprContext shutdown.
158  * ----------------
159  */
160 typedef void (*ExprContextCallbackFunction) (Datum arg);
161 
162 typedef struct ExprContext_CB
163 {
164 	struct ExprContext_CB *next;
165 	ExprContextCallbackFunction function;
166 	Datum		arg;
167 } ExprContext_CB;
168 
169 /* ----------------
170  *	  ExprContext
171  *
172  *		This class holds the "current context" information
173  *		needed to evaluate expressions for doing tuple qualifications
174  *		and tuple projections.  For example, if an expression refers
175  *		to an attribute in the current inner tuple then we need to know
176  *		what the current inner tuple is and so we look at the expression
177  *		context.
178  *
179  *	There are two memory contexts associated with an ExprContext:
180  *	* ecxt_per_query_memory is a query-lifespan context, typically the same
181  *	  context the ExprContext node itself is allocated in.  This context
182  *	  can be used for purposes such as storing function call cache info.
183  *	* ecxt_per_tuple_memory is a short-term context for expression results.
184  *	  As the name suggests, it will typically be reset once per tuple,
185  *	  before we begin to evaluate expressions for that tuple.  Each
186  *	  ExprContext normally has its very own per-tuple memory context.
187  *
188  *	CurrentMemoryContext should be set to ecxt_per_tuple_memory before
189  *	calling ExecEvalExpr() --- see ExecEvalExprSwitchContext().
190  * ----------------
191  */
192 typedef struct ExprContext
193 {
194 	NodeTag		type;
195 
196 	/* Tuples that Var nodes in expression may refer to */
197 	TupleTableSlot *ecxt_scantuple;
198 	TupleTableSlot *ecxt_innertuple;
199 	TupleTableSlot *ecxt_outertuple;
200 
201 	/* Memory contexts for expression evaluation --- see notes above */
202 	MemoryContext ecxt_per_query_memory;
203 	MemoryContext ecxt_per_tuple_memory;
204 
205 	/* Values to substitute for Param nodes in expression */
206 	ParamExecData *ecxt_param_exec_vals;	/* for PARAM_EXEC params */
207 	ParamListInfo ecxt_param_list_info; /* for other param types */
208 
209 	/*
210 	 * Values to substitute for Aggref nodes in the expressions of an Agg
211 	 * node, or for WindowFunc nodes within a WindowAgg node.
212 	 */
213 	Datum	   *ecxt_aggvalues; /* precomputed values for aggs/windowfuncs */
214 	bool	   *ecxt_aggnulls;	/* null flags for aggs/windowfuncs */
215 
216 	/* Value to substitute for CaseTestExpr nodes in expression */
217 	Datum		caseValue_datum;
218 	bool		caseValue_isNull;
219 
220 	/* Value to substitute for CoerceToDomainValue nodes in expression */
221 	Datum		domainValue_datum;
222 	bool		domainValue_isNull;
223 
224 	/* Link to containing EState (NULL if a standalone ExprContext) */
225 	struct EState *ecxt_estate;
226 
227 	/* Functions to call back when ExprContext is shut down or rescanned */
228 	ExprContext_CB *ecxt_callbacks;
229 } ExprContext;
230 
231 /*
232  * Set-result status used when evaluating functions potentially returning a
233  * set.
234  */
235 typedef enum
236 {
237 	ExprSingleResult,			/* expression does not return a set */
238 	ExprMultipleResult,			/* this result is an element of a set */
239 	ExprEndResult				/* there are no more elements in the set */
240 } ExprDoneCond;
241 
242 /*
243  * Return modes for functions returning sets.  Note values must be chosen
244  * as separate bits so that a bitmask can be formed to indicate supported
245  * modes.  SFRM_Materialize_Random and SFRM_Materialize_Preferred are
246  * auxiliary flags about SFRM_Materialize mode, rather than separate modes.
247  */
248 typedef enum
249 {
250 	SFRM_ValuePerCall = 0x01,	/* one value returned per call */
251 	SFRM_Materialize = 0x02,	/* result set instantiated in Tuplestore */
252 	SFRM_Materialize_Random = 0x04, /* Tuplestore needs randomAccess */
253 	SFRM_Materialize_Preferred = 0x08	/* caller prefers Tuplestore */
254 } SetFunctionReturnMode;
255 
256 /*
257  * When calling a function that might return a set (multiple rows),
258  * a node of this type is passed as fcinfo->resultinfo to allow
259  * return status to be passed back.  A function returning set should
260  * raise an error if no such resultinfo is provided.
261  */
262 typedef struct ReturnSetInfo
263 {
264 	NodeTag		type;
265 	/* values set by caller: */
266 	ExprContext *econtext;		/* context function is being called in */
267 	TupleDesc	expectedDesc;	/* tuple descriptor expected by caller */
268 	int			allowedModes;	/* bitmask: return modes caller can handle */
269 	/* result status from function (but pre-initialized by caller): */
270 	SetFunctionReturnMode returnMode;	/* actual return mode */
271 	ExprDoneCond isDone;		/* status for ValuePerCall mode */
272 	/* fields filled by function in Materialize return mode: */
273 	Tuplestorestate *setResult; /* holds the complete returned tuple set */
274 	TupleDesc	setDesc;		/* actual descriptor for returned tuples */
275 } ReturnSetInfo;
276 
277 /* ----------------
278  *		ProjectionInfo node information
279  *
280  *		This is all the information needed to perform projections ---
281  *		that is, form new tuples by evaluation of targetlist expressions.
282  *		Nodes which need to do projections create one of these.
283  *
284  *		The target tuple slot is kept in ProjectionInfo->pi_state.resultslot.
285  *		ExecProject() evaluates the tlist, forms a tuple, and stores it
286  *		in the given slot.  Note that the result will be a "virtual" tuple
287  *		unless ExecMaterializeSlot() is then called to force it to be
288  *		converted to a physical tuple.  The slot must have a tupledesc
289  *		that matches the output of the tlist!
290  * ----------------
291  */
292 typedef struct ProjectionInfo
293 {
294 	NodeTag		type;
295 	/* instructions to evaluate projection */
296 	ExprState	pi_state;
297 	/* expression context in which to evaluate expression */
298 	ExprContext *pi_exprContext;
299 } ProjectionInfo;
300 
301 /* ----------------
302  *	  JunkFilter
303  *
304  *	  This class is used to store information regarding junk attributes.
305  *	  A junk attribute is an attribute in a tuple that is needed only for
306  *	  storing intermediate information in the executor, and does not belong
307  *	  in emitted tuples.  For example, when we do an UPDATE query,
308  *	  the planner adds a "junk" entry to the targetlist so that the tuples
309  *	  returned to ExecutePlan() contain an extra attribute: the ctid of
310  *	  the tuple to be updated.  This is needed to do the update, but we
311  *	  don't want the ctid to be part of the stored new tuple!  So, we
312  *	  apply a "junk filter" to remove the junk attributes and form the
313  *	  real output tuple.  The junkfilter code also provides routines to
314  *	  extract the values of the junk attribute(s) from the input tuple.
315  *
316  *	  targetList:		the original target list (including junk attributes).
317  *	  cleanTupType:		the tuple descriptor for the "clean" tuple (with
318  *						junk attributes removed).
319  *	  cleanMap:			A map with the correspondence between the non-junk
320  *						attribute numbers of the "original" tuple and the
321  *						attribute numbers of the "clean" tuple.
322  *	  resultSlot:		tuple slot used to hold cleaned tuple.
323  *	  junkAttNo:		not used by junkfilter code.  Can be used by caller
324  *						to remember the attno of a specific junk attribute
325  *						(nodeModifyTable.c keeps the "ctid" or "wholerow"
326  *						attno here).
327  * ----------------
328  */
329 typedef struct JunkFilter
330 {
331 	NodeTag		type;
332 	List	   *jf_targetList;
333 	TupleDesc	jf_cleanTupType;
334 	AttrNumber *jf_cleanMap;
335 	TupleTableSlot *jf_resultSlot;
336 	AttrNumber	jf_junkAttNo;
337 } JunkFilter;
338 
339 /*
340  * ResultRelInfo
341  *
342  * Whenever we update an existing relation, we have to update indexes on the
343  * relation, and perhaps also fire triggers.  ResultRelInfo holds all the
344  * information needed about a result relation, including indexes.
345  */
346 typedef struct ResultRelInfo
347 {
348 	NodeTag		type;
349 
350 	/* result relation's range table index */
351 	Index		ri_RangeTableIndex;
352 
353 	/* relation descriptor for result relation */
354 	Relation	ri_RelationDesc;
355 
356 	/* # of indices existing on result relation */
357 	int			ri_NumIndices;
358 
359 	/* array of relation descriptors for indices */
360 	RelationPtr ri_IndexRelationDescs;
361 
362 	/* array of key/attr info for indices */
363 	IndexInfo **ri_IndexRelationInfo;
364 
365 	/* triggers to be fired, if any */
366 	TriggerDesc *ri_TrigDesc;
367 
368 	/* cached lookup info for trigger functions */
369 	FmgrInfo   *ri_TrigFunctions;
370 
371 	/* array of trigger WHEN expr states */
372 	ExprState **ri_TrigWhenExprs;
373 
374 	/* optional runtime measurements for triggers */
375 	Instrumentation *ri_TrigInstrument;
376 
377 	/* FDW callback functions, if foreign table */
378 	struct FdwRoutine *ri_FdwRoutine;
379 
380 	/* available to save private state of FDW */
381 	void	   *ri_FdwState;
382 
383 	/* true when modifying foreign table directly */
384 	bool		ri_usesFdwDirectModify;
385 
386 	/* list of WithCheckOption's to be checked */
387 	List	   *ri_WithCheckOptions;
388 
389 	/* list of WithCheckOption expr states */
390 	List	   *ri_WithCheckOptionExprs;
391 
392 	/* array of constraint-checking expr states */
393 	ExprState **ri_ConstraintExprs;
394 
395 	/* for removing junk attributes from tuples */
396 	JunkFilter *ri_junkFilter;
397 
398 	/* for computing a RETURNING list */
399 	ProjectionInfo *ri_projectReturning;
400 
401 	/* for computing ON CONFLICT DO UPDATE SET */
402 	ProjectionInfo *ri_onConflictSetProj;
403 
404 	/* list of ON CONFLICT DO UPDATE exprs (qual) */
405 	ExprState  *ri_onConflictSetWhere;
406 
407 	/* partition check expression */
408 	List	   *ri_PartitionCheck;
409 
410 	/* partition check expression state */
411 	ExprState  *ri_PartitionCheckExpr;
412 
413 	/* relation descriptor for root partitioned table */
414 	Relation	ri_PartitionRoot;
415 } ResultRelInfo;
416 
417 /* ----------------
418  *	  EState information
419  *
420  * Master working state for an Executor invocation
421  * ----------------
422  */
423 typedef struct EState
424 {
425 	NodeTag		type;
426 
427 	/* Basic state for all query types: */
428 	ScanDirection es_direction; /* current scan direction */
429 	Snapshot	es_snapshot;	/* time qual to use */
430 	Snapshot	es_crosscheck_snapshot; /* crosscheck time qual for RI */
431 	List	   *es_range_table; /* List of RangeTblEntry */
432 	PlannedStmt *es_plannedstmt;	/* link to top of plan tree */
433 	const char *es_sourceText;	/* Source text from QueryDesc */
434 
435 	JunkFilter *es_junkFilter;	/* top-level junk filter, if any */
436 
437 	/* If query can insert/delete tuples, the command ID to mark them with */
438 	CommandId	es_output_cid;
439 
440 	/* Info about target table(s) for insert/update/delete queries: */
441 	ResultRelInfo *es_result_relations; /* array of ResultRelInfos */
442 	int			es_num_result_relations;	/* length of array */
443 	ResultRelInfo *es_result_relation_info; /* currently active array elt */
444 
445 	/*
446 	 * Info about the target partitioned target table root(s) for
447 	 * update/delete queries.  They required only to fire any per-statement
448 	 * triggers defined on the table.  It exists separately from
449 	 * es_result_relations, because partitioned tables don't appear in the
450 	 * plan tree for the update/delete cases.
451 	 */
452 	ResultRelInfo *es_root_result_relations;	/* array of ResultRelInfos */
453 	int			es_num_root_result_relations;	/* length of the array */
454 
455 	/* Info about leaf partitions of partitioned table(s) for insert queries: */
456 	List	   *es_leaf_result_relations;	/* List of ResultRelInfos */
457 
458 	/* Stuff used for firing triggers: */
459 	List	   *es_trig_target_relations;	/* trigger-only ResultRelInfos */
460 	TupleTableSlot *es_trig_tuple_slot; /* for trigger output tuples */
461 	TupleTableSlot *es_trig_oldtup_slot;	/* for TriggerEnabled */
462 	TupleTableSlot *es_trig_newtup_slot;	/* for TriggerEnabled */
463 
464 	/* Parameter info: */
465 	ParamListInfo es_param_list_info;	/* values of external params */
466 	ParamExecData *es_param_exec_vals;	/* values of internal params */
467 
468 	QueryEnvironment *es_queryEnv;	/* query environment */
469 
470 	/* Other working state: */
471 	MemoryContext es_query_cxt; /* per-query context in which EState lives */
472 
473 	List	   *es_tupleTable;	/* List of TupleTableSlots */
474 
475 	List	   *es_rowMarks;	/* List of ExecRowMarks */
476 
477 	uint64		es_processed;	/* # of tuples processed */
478 	Oid			es_lastoid;		/* last oid processed (by INSERT) */
479 
480 	int			es_top_eflags;	/* eflags passed to ExecutorStart */
481 	int			es_instrument;	/* OR of InstrumentOption flags */
482 	bool		es_finished;	/* true when ExecutorFinish is done */
483 
484 	List	   *es_exprcontexts;	/* List of ExprContexts within EState */
485 
486 	List	   *es_subplanstates;	/* List of PlanState for SubPlans */
487 
488 	List	   *es_auxmodifytables; /* List of secondary ModifyTableStates */
489 
490 	/*
491 	 * this ExprContext is for per-output-tuple operations, such as constraint
492 	 * checks and index-value computations.  It will be reset for each output
493 	 * tuple.  Note that it will be created only if needed.
494 	 */
495 	ExprContext *es_per_tuple_exprcontext;
496 
497 	/*
498 	 * These fields are for re-evaluating plan quals when an updated tuple is
499 	 * substituted in READ COMMITTED mode.  es_epqTuple[] contains tuples that
500 	 * scan plan nodes should return instead of whatever they'd normally
501 	 * return, or NULL if nothing to return; es_epqTupleSet[] is true if a
502 	 * particular array entry is valid; and es_epqScanDone[] is state to
503 	 * remember if the tuple has been returned already.  Arrays are of size
504 	 * list_length(es_range_table) and are indexed by scan node scanrelid - 1.
505 	 */
506 	HeapTuple  *es_epqTuple;	/* array of EPQ substitute tuples */
507 	bool	   *es_epqTupleSet; /* true if EPQ tuple is provided */
508 	bool	   *es_epqScanDone; /* true if EPQ tuple has been fetched */
509 
510 	/* The per-query shared memory area to use for parallel execution. */
511 	struct dsa_area *es_query_dsa;
512 
513 	bool		es_use_parallel_mode; /* can we use parallel workers? */
514 } EState;
515 
516 
517 /*
518  * ExecRowMark -
519  *	   runtime representation of FOR [KEY] UPDATE/SHARE clauses
520  *
521  * When doing UPDATE, DELETE, or SELECT FOR [KEY] UPDATE/SHARE, we will have an
522  * ExecRowMark for each non-target relation in the query (except inheritance
523  * parent RTEs, which can be ignored at runtime).  Virtual relations such as
524  * subqueries-in-FROM will have an ExecRowMark with relation == NULL.  See
525  * PlanRowMark for details about most of the fields.  In addition to fields
526  * directly derived from PlanRowMark, we store an activity flag (to denote
527  * inactive children of inheritance trees), curCtid, which is used by the
528  * WHERE CURRENT OF code, and ermExtra, which is available for use by the plan
529  * node that sources the relation (e.g., for a foreign table the FDW can use
530  * ermExtra to hold information).
531  *
532  * EState->es_rowMarks is a list of these structs.
533  */
534 typedef struct ExecRowMark
535 {
536 	Relation	relation;		/* opened and suitably locked relation */
537 	Oid			relid;			/* its OID (or InvalidOid, if subquery) */
538 	Index		rti;			/* its range table index */
539 	Index		prti;			/* parent range table index, if child */
540 	Index		rowmarkId;		/* unique identifier for resjunk columns */
541 	RowMarkType markType;		/* see enum in nodes/plannodes.h */
542 	LockClauseStrength strength;	/* LockingClause's strength, or LCS_NONE */
543 	LockWaitPolicy waitPolicy;	/* NOWAIT and SKIP LOCKED */
544 	bool		ermActive;		/* is this mark relevant for current tuple? */
545 	ItemPointerData curCtid;	/* ctid of currently locked tuple, if any */
546 	void	   *ermExtra;		/* available for use by relation source node */
547 } ExecRowMark;
548 
549 /*
550  * ExecAuxRowMark -
551  *	   additional runtime representation of FOR [KEY] UPDATE/SHARE clauses
552  *
553  * Each LockRows and ModifyTable node keeps a list of the rowmarks it needs to
554  * deal with.  In addition to a pointer to the related entry in es_rowMarks,
555  * this struct carries the column number(s) of the resjunk columns associated
556  * with the rowmark (see comments for PlanRowMark for more detail).  In the
557  * case of ModifyTable, there has to be a separate ExecAuxRowMark list for
558  * each child plan, because the resjunk columns could be at different physical
559  * column positions in different subplans.
560  */
561 typedef struct ExecAuxRowMark
562 {
563 	ExecRowMark *rowmark;		/* related entry in es_rowMarks */
564 	AttrNumber	ctidAttNo;		/* resno of ctid junk attribute, if any */
565 	AttrNumber	toidAttNo;		/* resno of tableoid junk attribute, if any */
566 	AttrNumber	wholeAttNo;		/* resno of whole-row junk attribute, if any */
567 } ExecAuxRowMark;
568 
569 
570 /* ----------------------------------------------------------------
571  *				 Tuple Hash Tables
572  *
573  * All-in-memory tuple hash tables are used for a number of purposes.
574  *
575  * Note: tab_hash_funcs are for the key datatype(s) stored in the table,
576  * and tab_eq_funcs are non-cross-type equality operators for those types.
577  * Normally these are the only functions used, but FindTupleHashEntry()
578  * supports searching a hashtable using cross-data-type hashing.  For that,
579  * the caller must supply hash functions for the LHS datatype as well as
580  * the cross-type equality operators to use.  in_hash_funcs and cur_eq_funcs
581  * are set to point to the caller's function arrays while doing such a search.
582  * During LookupTupleHashEntry(), they point to tab_hash_funcs and
583  * tab_eq_funcs respectively.
584  * ----------------------------------------------------------------
585  */
586 typedef struct TupleHashEntryData *TupleHashEntry;
587 typedef struct TupleHashTableData *TupleHashTable;
588 
589 typedef struct TupleHashEntryData
590 {
591 	MinimalTuple firstTuple;	/* copy of first tuple in this group */
592 	void	   *additional;		/* user data */
593 	uint32		status;			/* hash status */
594 	uint32		hash;			/* hash value (cached) */
595 } TupleHashEntryData;
596 
597 /* define parameters necessary to generate the tuple hash table interface */
598 #define SH_PREFIX tuplehash
599 #define SH_ELEMENT_TYPE TupleHashEntryData
600 #define SH_KEY_TYPE MinimalTuple
601 #define SH_SCOPE extern
602 #define SH_DECLARE
603 #include "lib/simplehash.h"
604 
605 typedef struct TupleHashTableData
606 {
607 	tuplehash_hash *hashtab;	/* underlying hash table */
608 	int			numCols;		/* number of columns in lookup key */
609 	AttrNumber *keyColIdx;		/* attr numbers of key columns */
610 	FmgrInfo   *tab_hash_funcs; /* hash functions for table datatype(s) */
611 	FmgrInfo   *tab_eq_funcs;	/* equality functions for table datatype(s) */
612 	MemoryContext tablecxt;		/* memory context containing table */
613 	MemoryContext tempcxt;		/* context for function evaluations */
614 	Size		entrysize;		/* actual size to make each hash entry */
615 	TupleTableSlot *tableslot;	/* slot for referencing table entries */
616 	/* The following fields are set transiently for each table search: */
617 	TupleTableSlot *inputslot;	/* current input tuple's slot */
618 	FmgrInfo   *in_hash_funcs;	/* hash functions for input datatype(s) */
619 	FmgrInfo   *cur_eq_funcs;	/* equality functions for input vs. table */
620 	uint32		hash_iv;		/* hash-function IV */
621 }			TupleHashTableData;
622 
623 typedef tuplehash_iterator TupleHashIterator;
624 
625 /*
626  * Use InitTupleHashIterator/TermTupleHashIterator for a read/write scan.
627  * Use ResetTupleHashIterator if the table can be frozen (in this case no
628  * explicit scan termination is needed).
629  */
630 #define InitTupleHashIterator(htable, iter) \
631 	tuplehash_start_iterate(htable->hashtab, iter)
632 #define TermTupleHashIterator(iter) \
633 	((void) 0)
634 #define ResetTupleHashIterator(htable, iter) \
635 	InitTupleHashIterator(htable, iter)
636 #define ScanTupleHashTable(htable, iter) \
637 	tuplehash_iterate(htable->hashtab, iter)
638 
639 
640 /* ----------------------------------------------------------------
641  *				 Expression State Nodes
642  *
643  * Formerly, there was a separate executor expression state node corresponding
644  * to each node in a planned expression tree.  That's no longer the case; for
645  * common expression node types, all the execution info is embedded into
646  * step(s) in a single ExprState node.  But we still have a few executor state
647  * node types for selected expression node types, mostly those in which info
648  * has to be shared with other parts of the execution state tree.
649  * ----------------------------------------------------------------
650  */
651 
652 /* ----------------
653  *		AggrefExprState node
654  * ----------------
655  */
656 typedef struct AggrefExprState
657 {
658 	NodeTag		type;
659 	Aggref	   *aggref;			/* expression plan node */
660 	int			aggno;			/* ID number for agg within its plan node */
661 } AggrefExprState;
662 
663 /* ----------------
664  *		WindowFuncExprState node
665  * ----------------
666  */
667 typedef struct WindowFuncExprState
668 {
669 	NodeTag		type;
670 	WindowFunc *wfunc;			/* expression plan node */
671 	List	   *args;			/* ExprStates for argument expressions */
672 	ExprState  *aggfilter;		/* FILTER expression */
673 	int			wfuncno;		/* ID number for wfunc within its plan node */
674 } WindowFuncExprState;
675 
676 
677 /* ----------------
678  *		SetExprState node
679  *
680  * State for evaluating a potentially set-returning expression (like FuncExpr
681  * or OpExpr).  In some cases, like some of the expressions in ROWS FROM(...)
682  * the expression might not be a SRF, but nonetheless it uses the same
683  * machinery as SRFs; it will be treated as a SRF returning a single row.
684  * ----------------
685  */
686 typedef struct SetExprState
687 {
688 	NodeTag		type;
689 	Expr	   *expr;			/* expression plan node */
690 	List	   *args;			/* ExprStates for argument expressions */
691 
692 	/*
693 	 * In ROWS FROM, functions can be inlined, removing the FuncExpr normally
694 	 * inside.  In such a case this is the compiled expression (which cannot
695 	 * return a set), which'll be evaluated using regular ExecEvalExpr().
696 	 */
697 	ExprState  *elidedFuncState;
698 
699 	/*
700 	 * Function manager's lookup info for the target function.  If func.fn_oid
701 	 * is InvalidOid, we haven't initialized it yet (nor any of the following
702 	 * fields, except funcReturnsSet).
703 	 */
704 	FmgrInfo	func;
705 
706 	/*
707 	 * For a set-returning function (SRF) that returns a tuplestore, we keep
708 	 * the tuplestore here and dole out the result rows one at a time. The
709 	 * slot holds the row currently being returned.
710 	 */
711 	Tuplestorestate *funcResultStore;
712 	TupleTableSlot *funcResultSlot;
713 
714 	/*
715 	 * In some cases we need to compute a tuple descriptor for the function's
716 	 * output.  If so, it's stored here.
717 	 */
718 	TupleDesc	funcResultDesc;
719 	bool		funcReturnsTuple;	/* valid when funcResultDesc isn't NULL */
720 
721 	/*
722 	 * Remember whether the function is declared to return a set.  This is set
723 	 * by ExecInitExpr, and is valid even before the FmgrInfo is set up.
724 	 */
725 	bool		funcReturnsSet;
726 
727 	/*
728 	 * setArgsValid is true when we are evaluating a set-returning function
729 	 * that uses value-per-call mode and we are in the middle of a call
730 	 * series; we want to pass the same argument values to the function again
731 	 * (and again, until it returns ExprEndResult).  This indicates that
732 	 * fcinfo_data already contains valid argument data.
733 	 */
734 	bool		setArgsValid;
735 
736 	/*
737 	 * Flag to remember whether we have registered a shutdown callback for
738 	 * this SetExprState.  We do so only if funcResultStore or setArgsValid
739 	 * has been set at least once (since all the callback is for is to release
740 	 * the tuplestore or clear setArgsValid).
741 	 */
742 	bool		shutdown_reg;	/* a shutdown callback is registered */
743 
744 	/*
745 	 * Call parameter structure for the function.  This has been initialized
746 	 * (by InitFunctionCallInfoData) if func.fn_oid is valid.  It also saves
747 	 * argument values between calls, when setArgsValid is true.
748 	 */
749 	FunctionCallInfoData fcinfo_data;
750 } SetExprState;
751 
752 /* ----------------
753  *		SubPlanState node
754  * ----------------
755  */
756 typedef struct SubPlanState
757 {
758 	NodeTag		type;
759 	SubPlan    *subplan;		/* expression plan node */
760 	struct PlanState *planstate;	/* subselect plan's state tree */
761 	struct PlanState *parent;	/* parent plan node's state tree */
762 	ExprState  *testexpr;		/* state of combining expression */
763 	List	   *args;			/* states of argument expression(s) */
764 	HeapTuple	curTuple;		/* copy of most recent tuple from subplan */
765 	Datum		curArray;		/* most recent array from ARRAY() subplan */
766 	/* these are used when hashing the subselect's output: */
767 	ProjectionInfo *projLeft;	/* for projecting lefthand exprs */
768 	ProjectionInfo *projRight;	/* for projecting subselect output */
769 	TupleHashTable hashtable;	/* hash table for no-nulls subselect rows */
770 	TupleHashTable hashnulls;	/* hash table for rows with null(s) */
771 	bool		havehashrows;	/* TRUE if hashtable is not empty */
772 	bool		havenullrows;	/* TRUE if hashnulls is not empty */
773 	MemoryContext hashtablecxt; /* memory context containing hash tables */
774 	MemoryContext hashtempcxt;	/* temp memory context for hash tables */
775 	ExprContext *innerecontext; /* econtext for computing inner tuples */
776 	/* each of the following fields is an array of length numCols: */
777 	AttrNumber *keyColIdx;		/* control data for hash tables */
778 	FmgrInfo   *tab_hash_funcs; /* hash functions for table datatype(s) */
779 	FmgrInfo   *tab_eq_funcs;	/* equality functions for table datatype(s) */
780 	FmgrInfo   *lhs_hash_funcs; /* hash functions for lefthand datatype(s) */
781 	FmgrInfo   *cur_eq_funcs;	/* equality functions for LHS vs. table */
782 	int			numCols;		/* number of columns being hashed */
783 } SubPlanState;
784 
785 /* ----------------
786  *		AlternativeSubPlanState node
787  * ----------------
788  */
789 typedef struct AlternativeSubPlanState
790 {
791 	NodeTag		type;
792 	AlternativeSubPlan *subplan;	/* expression plan node */
793 	List	   *subplans;		/* SubPlanStates of alternative subplans */
794 	int			active;			/* list index of the one we're using */
795 } AlternativeSubPlanState;
796 
797 /*
798  * DomainConstraintState - one item to check during CoerceToDomain
799  *
800  * Note: we consider this to be part of an ExprState tree, so we give it
801  * a name following the xxxState convention.  But there's no directly
802  * associated plan-tree node.
803  */
804 typedef enum DomainConstraintType
805 {
806 	DOM_CONSTRAINT_NOTNULL,
807 	DOM_CONSTRAINT_CHECK
808 } DomainConstraintType;
809 
810 typedef struct DomainConstraintState
811 {
812 	NodeTag		type;
813 	DomainConstraintType constrainttype;	/* constraint type */
814 	char	   *name;			/* name of constraint (for error msgs) */
815 	Expr	   *check_expr;		/* for CHECK, a boolean expression */
816 	ExprState  *check_exprstate;	/* check_expr's eval state, or NULL */
817 } DomainConstraintState;
818 
819 
820 /* ----------------------------------------------------------------
821  *				 Executor State Trees
822  *
823  * An executing query has a PlanState tree paralleling the Plan tree
824  * that describes the plan.
825  * ----------------------------------------------------------------
826  */
827 
828 struct PlanState;
829 
830 /* ----------------
831  *	 ExecProcNodeMtd
832  *
833  * This is the method called by ExecProcNode to return the next tuple
834  * from an executor node.  It returns NULL, or an empty TupleTableSlot,
835  * if no more tuples are available.
836  * ----------------
837  */
838 typedef TupleTableSlot *(*ExecProcNodeMtd) (struct PlanState *pstate);
839 
840 /* ----------------
841  *		PlanState node
842  *
843  * We never actually instantiate any PlanState nodes; this is just the common
844  * abstract superclass for all PlanState-type nodes.
845  * ----------------
846  */
847 typedef struct PlanState
848 {
849 	NodeTag		type;
850 
851 	Plan	   *plan;			/* associated Plan node */
852 
853 	EState	   *state;			/* at execution time, states of individual
854 								 * nodes point to one EState for the whole
855 								 * top-level plan */
856 
857 	ExecProcNodeMtd ExecProcNode;	/* function to return next tuple */
858 	ExecProcNodeMtd ExecProcNodeReal;	/* actual function, if above is a
859 										 * wrapper */
860 
861 	Instrumentation *instrument;	/* Optional runtime stats for this node */
862 	WorkerInstrumentation *worker_instrument;	/* per-worker instrumentation */
863 
864 	/*
865 	 * Common structural data for all Plan types.  These links to subsidiary
866 	 * state trees parallel links in the associated plan tree (except for the
867 	 * subPlan list, which does not exist in the plan tree).
868 	 */
869 	ExprState  *qual;			/* boolean qual condition */
870 	struct PlanState *lefttree; /* input plan tree(s) */
871 	struct PlanState *righttree;
872 	List	   *initPlan;		/* Init SubPlanState nodes (un-correlated expr
873 								 * subselects) */
874 	List	   *subPlan;		/* SubPlanState nodes in my expressions */
875 
876 	/*
877 	 * State for management of parameter-change-driven rescanning
878 	 */
879 	Bitmapset  *chgParam;		/* set of IDs of changed Params */
880 
881 	/*
882 	 * Other run-time state needed by most if not all node types.
883 	 */
884 	TupleTableSlot *ps_ResultTupleSlot; /* slot for my result tuples */
885 	ExprContext *ps_ExprContext;	/* node's expression-evaluation context */
886 	ProjectionInfo *ps_ProjInfo;	/* info for doing tuple projection */
887 } PlanState;
888 
889 /* ----------------
890  *	these are defined to avoid confusion problems with "left"
891  *	and "right" and "inner" and "outer".  The convention is that
892  *	the "left" plan is the "outer" plan and the "right" plan is
893  *	the inner plan, but these make the code more readable.
894  * ----------------
895  */
896 #define innerPlanState(node)		(((PlanState *)(node))->righttree)
897 #define outerPlanState(node)		(((PlanState *)(node))->lefttree)
898 
899 /* Macros for inline access to certain instrumentation counters */
900 #define InstrCountFiltered1(node, delta) \
901 	do { \
902 		if (((PlanState *)(node))->instrument) \
903 			((PlanState *)(node))->instrument->nfiltered1 += (delta); \
904 	} while(0)
905 #define InstrCountFiltered2(node, delta) \
906 	do { \
907 		if (((PlanState *)(node))->instrument) \
908 			((PlanState *)(node))->instrument->nfiltered2 += (delta); \
909 	} while(0)
910 
911 /*
912  * EPQState is state for executing an EvalPlanQual recheck on a candidate
913  * tuple in ModifyTable or LockRows.  The estate and planstate fields are
914  * NULL if inactive.
915  */
916 typedef struct EPQState
917 {
918 	EState	   *estate;			/* subsidiary EState */
919 	PlanState  *planstate;		/* plan state tree ready to be executed */
920 	TupleTableSlot *origslot;	/* original output tuple to be rechecked */
921 	Plan	   *plan;			/* plan tree to be executed */
922 	List	   *arowMarks;		/* ExecAuxRowMarks (non-locking only) */
923 	int			epqParam;		/* ID of Param to force scan node re-eval */
924 } EPQState;
925 
926 
927 /* ----------------
928  *	 ResultState information
929  * ----------------
930  */
931 typedef struct ResultState
932 {
933 	PlanState	ps;				/* its first field is NodeTag */
934 	ExprState  *resconstantqual;
935 	bool		rs_done;		/* are we done? */
936 	bool		rs_checkqual;	/* do we need to check the qual? */
937 } ResultState;
938 
939 /* ----------------
940  *	 ProjectSetState information
941  *
942  * Note: at least one of the "elems" will be a SetExprState; the rest are
943  * regular ExprStates.
944  * ----------------
945  */
946 typedef struct ProjectSetState
947 {
948 	PlanState	ps;				/* its first field is NodeTag */
949 	Node	  **elems;			/* array of expression states */
950 	ExprDoneCond *elemdone;		/* array of per-SRF is-done states */
951 	int			nelems;			/* length of elemdone[] array */
952 	bool		pending_srf_tuples; /* still evaluating srfs in tlist? */
953 } ProjectSetState;
954 
955 /* ----------------
956  *	 ModifyTableState information
957  * ----------------
958  */
959 typedef struct ModifyTableState
960 {
961 	PlanState	ps;				/* its first field is NodeTag */
962 	CmdType		operation;		/* INSERT, UPDATE, or DELETE */
963 	bool		canSetTag;		/* do we set the command tag/es_processed? */
964 	bool		mt_done;		/* are we done? */
965 	PlanState **mt_plans;		/* subplans (one per target rel) */
966 	int			mt_nplans;		/* number of plans in the array */
967 	int			mt_whichplan;	/* which one is being executed (0..n-1) */
968 	ResultRelInfo *resultRelInfo;	/* per-subplan target relations */
969 	ResultRelInfo *rootResultRelInfo;	/* root target relation (partitioned
970 										 * table root) */
971 	List	  **mt_arowmarks;	/* per-subplan ExecAuxRowMark lists */
972 	EPQState	mt_epqstate;	/* for evaluating EvalPlanQual rechecks */
973 	bool		fireBSTriggers; /* do we need to fire stmt triggers? */
974 	OnConflictAction mt_onconflict; /* ON CONFLICT type */
975 	List	   *mt_arbiterindexes;	/* unique index OIDs to arbitrate taking
976 									 * alt path */
977 	TupleTableSlot *mt_existing;	/* slot to store existing target tuple in */
978 	List	   *mt_excludedtlist;	/* the excluded pseudo relation's tlist  */
979 	TupleTableSlot *mt_conflproj;	/* CONFLICT ... SET ... projection target */
980 	struct PartitionDispatchData **mt_partition_dispatch_info;
981 	/* Tuple-routing support info */
982 	int			mt_num_dispatch;	/* Number of entries in the above array */
983 	int			mt_num_partitions;	/* Number of members in the following
984 									 * arrays */
985 	ResultRelInfo *mt_partitions;	/* Per partition result relation */
986 
987 	/* Per partition tuple conversion map */
988 	TupleConversionMap **mt_partition_tupconv_maps;
989 
990 	/*
991 	 * Used to manipulate any given leaf partition's rowtype after that
992 	 * partition is chosen for insertion by tuple-routing.
993 	 */
994 	TupleTableSlot *mt_partition_tuple_slot;
995 
996 	/* controls transition table population for specified operation */
997 	struct TransitionCaptureState *mt_transition_capture;
998 
999 	/* controls transition table population for INSERT...ON CONFLICT UPDATE */
1000 	struct TransitionCaptureState *mt_oc_transition_capture;
1001 
1002 	/* Per plan/partition tuple conversion */
1003 	TupleConversionMap **mt_transition_tupconv_maps;
1004 } ModifyTableState;
1005 
1006 /* ----------------
1007  *	 AppendState information
1008  *
1009  *		nplans			how many plans are in the array
1010  *		whichplan		which plan is being executed (0 .. n-1)
1011  * ----------------
1012  */
1013 typedef struct AppendState
1014 {
1015 	PlanState	ps;				/* its first field is NodeTag */
1016 	PlanState **appendplans;	/* array of PlanStates for my inputs */
1017 	int			as_nplans;
1018 	int			as_whichplan;
1019 } AppendState;
1020 
1021 /* ----------------
1022  *	 MergeAppendState information
1023  *
1024  *		nplans			how many plans are in the array
1025  *		nkeys			number of sort key columns
1026  *		sortkeys		sort keys in SortSupport representation
1027  *		slots			current output tuple of each subplan
1028  *		heap			heap of active tuples
1029  *		initialized		true if we have fetched first tuple from each subplan
1030  * ----------------
1031  */
1032 typedef struct MergeAppendState
1033 {
1034 	PlanState	ps;				/* its first field is NodeTag */
1035 	PlanState **mergeplans;		/* array of PlanStates for my inputs */
1036 	int			ms_nplans;
1037 	int			ms_nkeys;
1038 	SortSupport ms_sortkeys;	/* array of length ms_nkeys */
1039 	TupleTableSlot **ms_slots;	/* array of length ms_nplans */
1040 	struct binaryheap *ms_heap; /* binary heap of slot indices */
1041 	bool		ms_initialized; /* are subplans started? */
1042 } MergeAppendState;
1043 
1044 /* ----------------
1045  *	 RecursiveUnionState information
1046  *
1047  *		RecursiveUnionState is used for performing a recursive union.
1048  *
1049  *		recursing			T when we're done scanning the non-recursive term
1050  *		intermediate_empty	T if intermediate_table is currently empty
1051  *		working_table		working table (to be scanned by recursive term)
1052  *		intermediate_table	current recursive output (next generation of WT)
1053  * ----------------
1054  */
1055 typedef struct RecursiveUnionState
1056 {
1057 	PlanState	ps;				/* its first field is NodeTag */
1058 	bool		recursing;
1059 	bool		intermediate_empty;
1060 	Tuplestorestate *working_table;
1061 	Tuplestorestate *intermediate_table;
1062 	/* Remaining fields are unused in UNION ALL case */
1063 	FmgrInfo   *eqfunctions;	/* per-grouping-field equality fns */
1064 	FmgrInfo   *hashfunctions;	/* per-grouping-field hash fns */
1065 	MemoryContext tempContext;	/* short-term context for comparisons */
1066 	TupleHashTable hashtable;	/* hash table for tuples already seen */
1067 	MemoryContext tableContext; /* memory context containing hash table */
1068 } RecursiveUnionState;
1069 
1070 /* ----------------
1071  *	 BitmapAndState information
1072  * ----------------
1073  */
1074 typedef struct BitmapAndState
1075 {
1076 	PlanState	ps;				/* its first field is NodeTag */
1077 	PlanState **bitmapplans;	/* array of PlanStates for my inputs */
1078 	int			nplans;			/* number of input plans */
1079 } BitmapAndState;
1080 
1081 /* ----------------
1082  *	 BitmapOrState information
1083  * ----------------
1084  */
1085 typedef struct BitmapOrState
1086 {
1087 	PlanState	ps;				/* its first field is NodeTag */
1088 	PlanState **bitmapplans;	/* array of PlanStates for my inputs */
1089 	int			nplans;			/* number of input plans */
1090 } BitmapOrState;
1091 
1092 /* ----------------------------------------------------------------
1093  *				 Scan State Information
1094  * ----------------------------------------------------------------
1095  */
1096 
1097 /* ----------------
1098  *	 ScanState information
1099  *
1100  *		ScanState extends PlanState for node types that represent
1101  *		scans of an underlying relation.  It can also be used for nodes
1102  *		that scan the output of an underlying plan node --- in that case,
1103  *		only ScanTupleSlot is actually useful, and it refers to the tuple
1104  *		retrieved from the subplan.
1105  *
1106  *		currentRelation    relation being scanned (NULL if none)
1107  *		currentScanDesc    current scan descriptor for scan (NULL if none)
1108  *		ScanTupleSlot	   pointer to slot in tuple table holding scan tuple
1109  * ----------------
1110  */
1111 typedef struct ScanState
1112 {
1113 	PlanState	ps;				/* its first field is NodeTag */
1114 	Relation	ss_currentRelation;
1115 	HeapScanDesc ss_currentScanDesc;
1116 	TupleTableSlot *ss_ScanTupleSlot;
1117 } ScanState;
1118 
1119 /* ----------------
1120  *	 SeqScanState information
1121  * ----------------
1122  */
1123 typedef struct SeqScanState
1124 {
1125 	ScanState	ss;				/* its first field is NodeTag */
1126 	Size		pscan_len;		/* size of parallel heap scan descriptor */
1127 } SeqScanState;
1128 
1129 /* ----------------
1130  *	 SampleScanState information
1131  * ----------------
1132  */
1133 typedef struct SampleScanState
1134 {
1135 	ScanState	ss;
1136 	List	   *args;			/* expr states for TABLESAMPLE params */
1137 	ExprState  *repeatable;		/* expr state for REPEATABLE expr */
1138 	/* use struct pointer to avoid including tsmapi.h here */
1139 	struct TsmRoutine *tsmroutine;	/* descriptor for tablesample method */
1140 	void	   *tsm_state;		/* tablesample method can keep state here */
1141 	bool		use_bulkread;	/* use bulkread buffer access strategy? */
1142 	bool		use_pagemode;	/* use page-at-a-time visibility checking? */
1143 	bool		begun;			/* false means need to call BeginSampleScan */
1144 	uint32		seed;			/* random seed */
1145 } SampleScanState;
1146 
1147 /*
1148  * These structs store information about index quals that don't have simple
1149  * constant right-hand sides.  See comments for ExecIndexBuildScanKeys()
1150  * for discussion.
1151  */
1152 typedef struct
1153 {
1154 	ScanKey		scan_key;		/* scankey to put value into */
1155 	ExprState  *key_expr;		/* expr to evaluate to get value */
1156 	bool		key_toastable;	/* is expr's result a toastable datatype? */
1157 } IndexRuntimeKeyInfo;
1158 
1159 typedef struct
1160 {
1161 	ScanKey		scan_key;		/* scankey to put value into */
1162 	ExprState  *array_expr;		/* expr to evaluate to get array value */
1163 	int			next_elem;		/* next array element to use */
1164 	int			num_elems;		/* number of elems in current array value */
1165 	Datum	   *elem_values;	/* array of num_elems Datums */
1166 	bool	   *elem_nulls;		/* array of num_elems is-null flags */
1167 } IndexArrayKeyInfo;
1168 
1169 /* ----------------
1170  *	 IndexScanState information
1171  *
1172  *		indexqualorig	   execution state for indexqualorig expressions
1173  *		indexorderbyorig   execution state for indexorderbyorig expressions
1174  *		ScanKeys		   Skey structures for index quals
1175  *		NumScanKeys		   number of ScanKeys
1176  *		OrderByKeys		   Skey structures for index ordering operators
1177  *		NumOrderByKeys	   number of OrderByKeys
1178  *		RuntimeKeys		   info about Skeys that must be evaluated at runtime
1179  *		NumRuntimeKeys	   number of RuntimeKeys
1180  *		RuntimeKeysReady   true if runtime Skeys have been computed
1181  *		RuntimeContext	   expr context for evaling runtime Skeys
1182  *		RelationDesc	   index relation descriptor
1183  *		ScanDesc		   index scan descriptor
1184  *
1185  *		ReorderQueue	   tuples that need reordering due to re-check
1186  *		ReachedEnd		   have we fetched all tuples from index already?
1187  *		OrderByValues	   values of ORDER BY exprs of last fetched tuple
1188  *		OrderByNulls	   null flags for OrderByValues
1189  *		SortSupport		   for reordering ORDER BY exprs
1190  *		OrderByTypByVals   is the datatype of order by expression pass-by-value?
1191  *		OrderByTypLens	   typlens of the datatypes of order by expressions
1192  *		pscan_len		   size of parallel index scan descriptor
1193  * ----------------
1194  */
1195 typedef struct IndexScanState
1196 {
1197 	ScanState	ss;				/* its first field is NodeTag */
1198 	ExprState  *indexqualorig;
1199 	List	   *indexorderbyorig;
1200 	ScanKey		iss_ScanKeys;
1201 	int			iss_NumScanKeys;
1202 	ScanKey		iss_OrderByKeys;
1203 	int			iss_NumOrderByKeys;
1204 	IndexRuntimeKeyInfo *iss_RuntimeKeys;
1205 	int			iss_NumRuntimeKeys;
1206 	bool		iss_RuntimeKeysReady;
1207 	ExprContext *iss_RuntimeContext;
1208 	Relation	iss_RelationDesc;
1209 	IndexScanDesc iss_ScanDesc;
1210 
1211 	/* These are needed for re-checking ORDER BY expr ordering */
1212 	pairingheap *iss_ReorderQueue;
1213 	bool		iss_ReachedEnd;
1214 	Datum	   *iss_OrderByValues;
1215 	bool	   *iss_OrderByNulls;
1216 	SortSupport iss_SortSupport;
1217 	bool	   *iss_OrderByTypByVals;
1218 	int16	   *iss_OrderByTypLens;
1219 	Size		iss_PscanLen;
1220 } IndexScanState;
1221 
1222 /* ----------------
1223  *	 IndexOnlyScanState information
1224  *
1225  *		indexqual		   execution state for indexqual expressions
1226  *		ScanKeys		   Skey structures for index quals
1227  *		NumScanKeys		   number of ScanKeys
1228  *		OrderByKeys		   Skey structures for index ordering operators
1229  *		NumOrderByKeys	   number of OrderByKeys
1230  *		RuntimeKeys		   info about Skeys that must be evaluated at runtime
1231  *		NumRuntimeKeys	   number of RuntimeKeys
1232  *		RuntimeKeysReady   true if runtime Skeys have been computed
1233  *		RuntimeContext	   expr context for evaling runtime Skeys
1234  *		RelationDesc	   index relation descriptor
1235  *		ScanDesc		   index scan descriptor
1236  *		VMBuffer		   buffer in use for visibility map testing, if any
1237  *		HeapFetches		   number of tuples we were forced to fetch from heap
1238  *		ioss_PscanLen	   Size of parallel index-only scan descriptor
1239  * ----------------
1240  */
1241 typedef struct IndexOnlyScanState
1242 {
1243 	ScanState	ss;				/* its first field is NodeTag */
1244 	ExprState  *indexqual;
1245 	ScanKey		ioss_ScanKeys;
1246 	int			ioss_NumScanKeys;
1247 	ScanKey		ioss_OrderByKeys;
1248 	int			ioss_NumOrderByKeys;
1249 	IndexRuntimeKeyInfo *ioss_RuntimeKeys;
1250 	int			ioss_NumRuntimeKeys;
1251 	bool		ioss_RuntimeKeysReady;
1252 	ExprContext *ioss_RuntimeContext;
1253 	Relation	ioss_RelationDesc;
1254 	IndexScanDesc ioss_ScanDesc;
1255 	Buffer		ioss_VMBuffer;
1256 	long		ioss_HeapFetches;
1257 	Size		ioss_PscanLen;
1258 } IndexOnlyScanState;
1259 
1260 /* ----------------
1261  *	 BitmapIndexScanState information
1262  *
1263  *		result			   bitmap to return output into, or NULL
1264  *		ScanKeys		   Skey structures for index quals
1265  *		NumScanKeys		   number of ScanKeys
1266  *		RuntimeKeys		   info about Skeys that must be evaluated at runtime
1267  *		NumRuntimeKeys	   number of RuntimeKeys
1268  *		ArrayKeys		   info about Skeys that come from ScalarArrayOpExprs
1269  *		NumArrayKeys	   number of ArrayKeys
1270  *		RuntimeKeysReady   true if runtime Skeys have been computed
1271  *		RuntimeContext	   expr context for evaling runtime Skeys
1272  *		RelationDesc	   index relation descriptor
1273  *		ScanDesc		   index scan descriptor
1274  * ----------------
1275  */
1276 typedef struct BitmapIndexScanState
1277 {
1278 	ScanState	ss;				/* its first field is NodeTag */
1279 	TIDBitmap  *biss_result;
1280 	ScanKey		biss_ScanKeys;
1281 	int			biss_NumScanKeys;
1282 	IndexRuntimeKeyInfo *biss_RuntimeKeys;
1283 	int			biss_NumRuntimeKeys;
1284 	IndexArrayKeyInfo *biss_ArrayKeys;
1285 	int			biss_NumArrayKeys;
1286 	bool		biss_RuntimeKeysReady;
1287 	ExprContext *biss_RuntimeContext;
1288 	Relation	biss_RelationDesc;
1289 	IndexScanDesc biss_ScanDesc;
1290 } BitmapIndexScanState;
1291 
1292 /* ----------------
1293  *	 SharedBitmapState information
1294  *
1295  *		BM_INITIAL		TIDBitmap creation is not yet started, so first worker
1296  *						to see this state will set the state to BM_INPROGRESS
1297  *						and that process will be responsible for creating
1298  *						TIDBitmap.
1299  *		BM_INPROGRESS	TIDBitmap creation is in progress; workers need to
1300  *						sleep until it's finished.
1301  *		BM_FINISHED		TIDBitmap creation is done, so now all workers can
1302  *						proceed to iterate over TIDBitmap.
1303  * ----------------
1304  */
1305 typedef enum
1306 {
1307 	BM_INITIAL,
1308 	BM_INPROGRESS,
1309 	BM_FINISHED
1310 } SharedBitmapState;
1311 
1312 /* ----------------
1313  *	 ParallelBitmapHeapState information
1314  *		tbmiterator				iterator for scanning current pages
1315  *		prefetch_iterator		iterator for prefetching ahead of current page
1316  *		mutex					mutual exclusion for the prefetching variable
1317  *								and state
1318  *		prefetch_pages			# pages prefetch iterator is ahead of current
1319  *		prefetch_target			current target prefetch distance
1320  *		state					current state of the TIDBitmap
1321  *		cv						conditional wait variable
1322  *		phs_snapshot_data		snapshot data shared to workers
1323  * ----------------
1324  */
1325 typedef struct ParallelBitmapHeapState
1326 {
1327 	dsa_pointer tbmiterator;
1328 	dsa_pointer prefetch_iterator;
1329 	slock_t		mutex;
1330 	int			prefetch_pages;
1331 	int			prefetch_target;
1332 	SharedBitmapState state;
1333 	ConditionVariable cv;
1334 	char		phs_snapshot_data[FLEXIBLE_ARRAY_MEMBER];
1335 } ParallelBitmapHeapState;
1336 
1337 /* ----------------
1338  *	 BitmapHeapScanState information
1339  *
1340  *		bitmapqualorig	   execution state for bitmapqualorig expressions
1341  *		tbm				   bitmap obtained from child index scan(s)
1342  *		tbmiterator		   iterator for scanning current pages
1343  *		tbmres			   current-page data
1344  *		exact_pages		   total number of exact pages retrieved
1345  *		lossy_pages		   total number of lossy pages retrieved
1346  *		prefetch_iterator  iterator for prefetching ahead of current page
1347  *		prefetch_pages	   # pages prefetch iterator is ahead of current
1348  *		prefetch_target    current target prefetch distance
1349  *		prefetch_maximum   maximum value for prefetch_target
1350  *		pscan_len		   size of the shared memory for parallel bitmap
1351  *		initialized		   is node is ready to iterate
1352  *		shared_tbmiterator	   shared iterator
1353  *		shared_prefetch_iterator shared iterator for prefetching
1354  *		pstate			   shared state for parallel bitmap scan
1355  * ----------------
1356  */
1357 typedef struct BitmapHeapScanState
1358 {
1359 	ScanState	ss;				/* its first field is NodeTag */
1360 	ExprState  *bitmapqualorig;
1361 	TIDBitmap  *tbm;
1362 	TBMIterator *tbmiterator;
1363 	TBMIterateResult *tbmres;
1364 	long		exact_pages;
1365 	long		lossy_pages;
1366 	TBMIterator *prefetch_iterator;
1367 	int			prefetch_pages;
1368 	int			prefetch_target;
1369 	int			prefetch_maximum;
1370 	Size		pscan_len;
1371 	bool		initialized;
1372 	TBMSharedIterator *shared_tbmiterator;
1373 	TBMSharedIterator *shared_prefetch_iterator;
1374 	ParallelBitmapHeapState *pstate;
1375 } BitmapHeapScanState;
1376 
1377 /* ----------------
1378  *	 TidScanState information
1379  *
1380  *		tidexprs	   list of TidExpr structs (see nodeTidscan.c)
1381  *		isCurrentOf    scan has a CurrentOfExpr qual
1382  *		NumTids		   number of tids in this scan
1383  *		TidPtr		   index of currently fetched tid
1384  *		TidList		   evaluated item pointers (array of size NumTids)
1385  *		htup		   currently-fetched tuple, if any
1386  * ----------------
1387  */
1388 typedef struct TidScanState
1389 {
1390 	ScanState	ss;				/* its first field is NodeTag */
1391 	List	   *tss_tidexprs;
1392 	bool		tss_isCurrentOf;
1393 	int			tss_NumTids;
1394 	int			tss_TidPtr;
1395 	ItemPointerData *tss_TidList;
1396 	HeapTupleData tss_htup;
1397 } TidScanState;
1398 
1399 /* ----------------
1400  *	 SubqueryScanState information
1401  *
1402  *		SubqueryScanState is used for scanning a sub-query in the range table.
1403  *		ScanTupleSlot references the current output tuple of the sub-query.
1404  * ----------------
1405  */
1406 typedef struct SubqueryScanState
1407 {
1408 	ScanState	ss;				/* its first field is NodeTag */
1409 	PlanState  *subplan;
1410 } SubqueryScanState;
1411 
1412 /* ----------------
1413  *	 FunctionScanState information
1414  *
1415  *		Function nodes are used to scan the results of a
1416  *		function appearing in FROM (typically a function returning set).
1417  *
1418  *		eflags				node's capability flags
1419  *		ordinality			is this scan WITH ORDINALITY?
1420  *		simple				true if we have 1 function and no ordinality
1421  *		ordinal				current ordinal column value
1422  *		nfuncs				number of functions being executed
1423  *		funcstates			per-function execution states (private in
1424  *							nodeFunctionscan.c)
1425  *		argcontext			memory context to evaluate function arguments in
1426  * ----------------
1427  */
1428 struct FunctionScanPerFuncState;
1429 
1430 typedef struct FunctionScanState
1431 {
1432 	ScanState	ss;				/* its first field is NodeTag */
1433 	int			eflags;
1434 	bool		ordinality;
1435 	bool		simple;
1436 	int64		ordinal;
1437 	int			nfuncs;
1438 	struct FunctionScanPerFuncState *funcstates;	/* array of length nfuncs */
1439 	MemoryContext argcontext;
1440 } FunctionScanState;
1441 
1442 /* ----------------
1443  *	 ValuesScanState information
1444  *
1445  *		ValuesScan nodes are used to scan the results of a VALUES list
1446  *
1447  *		rowcontext			per-expression-list context
1448  *		exprlists			array of expression lists being evaluated
1449  *		exprstatelists		array of expression state lists, for SubPlans only
1450  *		array_len			size of above arrays
1451  *		curr_idx			current array index (0-based)
1452  *
1453  *	Note: ss.ps.ps_ExprContext is used to evaluate any qual or projection
1454  *	expressions attached to the node.  We create a second ExprContext,
1455  *	rowcontext, in which to build the executor expression state for each
1456  *	Values sublist.  Resetting this context lets us get rid of expression
1457  *	state for each row, avoiding major memory leakage over a long values list.
1458  *	However, that doesn't work for sublists containing SubPlans, because a
1459  *	SubPlan has to be connected up to the outer plan tree to work properly.
1460  *	Therefore, for only those sublists containing SubPlans, we do expression
1461  *	state construction at executor start, and store those pointers in
1462  *	exprstatelists[].  NULL entries in that array correspond to simple
1463  *	subexpressions that are handled as described above.
1464  * ----------------
1465  */
1466 typedef struct ValuesScanState
1467 {
1468 	ScanState	ss;				/* its first field is NodeTag */
1469 	ExprContext *rowcontext;
1470 	List	  **exprlists;
1471 	int			array_len;
1472 	int			curr_idx;
1473 	/* in back branches, put this at the end to avoid ABI break: */
1474 	List	  **exprstatelists;
1475 } ValuesScanState;
1476 
1477 /* ----------------
1478  *		TableFuncScanState node
1479  *
1480  * Used in table-expression functions like XMLTABLE.
1481  * ----------------
1482  */
1483 typedef struct TableFuncScanState
1484 {
1485 	ScanState	ss;				/* its first field is NodeTag */
1486 	ExprState  *docexpr;		/* state for document expression */
1487 	ExprState  *rowexpr;		/* state for row-generating expression */
1488 	List	   *colexprs;		/* state for column-generating expression */
1489 	List	   *coldefexprs;	/* state for column default expressions */
1490 	List	   *ns_names;		/* same as TableFunc.ns_names */
1491 	List	   *ns_uris;		/* list of states of namespace URI exprs */
1492 	Bitmapset  *notnulls;		/* nullability flag for each output column */
1493 	void	   *opaque;			/* table builder private space */
1494 	const struct TableFuncRoutine *routine; /* table builder methods */
1495 	FmgrInfo   *in_functions;	/* input function for each column */
1496 	Oid		   *typioparams;	/* typioparam for each column */
1497 	int64		ordinal;		/* row number to be output next */
1498 	MemoryContext perTableCxt;	/* per-table context */
1499 	Tuplestorestate *tupstore;	/* output tuple store */
1500 } TableFuncScanState;
1501 
1502 /* ----------------
1503  *	 CteScanState information
1504  *
1505  *		CteScan nodes are used to scan a CommonTableExpr query.
1506  *
1507  * Multiple CteScan nodes can read out from the same CTE query.  We use
1508  * a tuplestore to hold rows that have been read from the CTE query but
1509  * not yet consumed by all readers.
1510  * ----------------
1511  */
1512 typedef struct CteScanState
1513 {
1514 	ScanState	ss;				/* its first field is NodeTag */
1515 	int			eflags;			/* capability flags to pass to tuplestore */
1516 	int			readptr;		/* index of my tuplestore read pointer */
1517 	PlanState  *cteplanstate;	/* PlanState for the CTE query itself */
1518 	/* Link to the "leader" CteScanState (possibly this same node) */
1519 	struct CteScanState *leader;
1520 	/* The remaining fields are only valid in the "leader" CteScanState */
1521 	Tuplestorestate *cte_table; /* rows already read from the CTE query */
1522 	bool		eof_cte;		/* reached end of CTE query? */
1523 } CteScanState;
1524 
1525 /* ----------------
1526  *	 NamedTuplestoreScanState information
1527  *
1528  *		NamedTuplestoreScan nodes are used to scan a Tuplestore created and
1529  *		named prior to execution of the query.  An example is a transition
1530  *		table for an AFTER trigger.
1531  *
1532  * Multiple NamedTuplestoreScan nodes can read out from the same Tuplestore.
1533  * ----------------
1534  */
1535 typedef struct NamedTuplestoreScanState
1536 {
1537 	ScanState	ss;				/* its first field is NodeTag */
1538 	int			readptr;		/* index of my tuplestore read pointer */
1539 	TupleDesc	tupdesc;		/* format of the tuples in the tuplestore */
1540 	Tuplestorestate *relation;	/* the rows */
1541 } NamedTuplestoreScanState;
1542 
1543 /* ----------------
1544  *	 WorkTableScanState information
1545  *
1546  *		WorkTableScan nodes are used to scan the work table created by
1547  *		a RecursiveUnion node.  We locate the RecursiveUnion node
1548  *		during executor startup.
1549  * ----------------
1550  */
1551 typedef struct WorkTableScanState
1552 {
1553 	ScanState	ss;				/* its first field is NodeTag */
1554 	RecursiveUnionState *rustate;
1555 } WorkTableScanState;
1556 
1557 /* ----------------
1558  *	 ForeignScanState information
1559  *
1560  *		ForeignScan nodes are used to scan foreign-data tables.
1561  * ----------------
1562  */
1563 typedef struct ForeignScanState
1564 {
1565 	ScanState	ss;				/* its first field is NodeTag */
1566 	ExprState  *fdw_recheck_quals;	/* original quals not in ss.ps.qual */
1567 	Size		pscan_len;		/* size of parallel coordination information */
1568 	/* use struct pointer to avoid including fdwapi.h here */
1569 	struct FdwRoutine *fdwroutine;
1570 	void	   *fdw_state;		/* foreign-data wrapper can keep state here */
1571 } ForeignScanState;
1572 
1573 /* ----------------
1574  *	 CustomScanState information
1575  *
1576  *		CustomScan nodes are used to execute custom code within executor.
1577  *
1578  * Core code must avoid assuming that the CustomScanState is only as large as
1579  * the structure declared here; providers are allowed to make it the first
1580  * element in a larger structure, and typically would need to do so.  The
1581  * struct is actually allocated by the CreateCustomScanState method associated
1582  * with the plan node.  Any additional fields can be initialized there, or in
1583  * the BeginCustomScan method.
1584  * ----------------
1585  */
1586 struct CustomExecMethods;
1587 
1588 typedef struct CustomScanState
1589 {
1590 	ScanState	ss;
1591 	uint32		flags;			/* mask of CUSTOMPATH_* flags, see
1592 								 * nodes/extensible.h */
1593 	List	   *custom_ps;		/* list of child PlanState nodes, if any */
1594 	Size		pscan_len;		/* size of parallel coordination information */
1595 	const struct CustomExecMethods *methods;
1596 } CustomScanState;
1597 
1598 /* ----------------------------------------------------------------
1599  *				 Join State Information
1600  * ----------------------------------------------------------------
1601  */
1602 
1603 /* ----------------
1604  *	 JoinState information
1605  *
1606  *		Superclass for state nodes of join plans.
1607  * ----------------
1608  */
1609 typedef struct JoinState
1610 {
1611 	PlanState	ps;
1612 	JoinType	jointype;
1613 	bool		single_match;	/* True if we should skip to next outer tuple
1614 								 * after finding one inner match */
1615 	ExprState  *joinqual;		/* JOIN quals (in addition to ps.qual) */
1616 } JoinState;
1617 
1618 /* ----------------
1619  *	 NestLoopState information
1620  *
1621  *		NeedNewOuter	   true if need new outer tuple on next call
1622  *		MatchedOuter	   true if found a join match for current outer tuple
1623  *		NullInnerTupleSlot prepared null tuple for left outer joins
1624  * ----------------
1625  */
1626 typedef struct NestLoopState
1627 {
1628 	JoinState	js;				/* its first field is NodeTag */
1629 	bool		nl_NeedNewOuter;
1630 	bool		nl_MatchedOuter;
1631 	TupleTableSlot *nl_NullInnerTupleSlot;
1632 } NestLoopState;
1633 
1634 /* ----------------
1635  *	 MergeJoinState information
1636  *
1637  *		NumClauses		   number of mergejoinable join clauses
1638  *		Clauses			   info for each mergejoinable clause
1639  *		JoinState		   current state of ExecMergeJoin state machine
1640  *		SkipMarkRestore    true if we may skip Mark and Restore operations
1641  *		ExtraMarks		   true to issue extra Mark operations on inner scan
1642  *		ConstFalseJoin	   true if we have a constant-false joinqual
1643  *		FillOuter		   true if should emit unjoined outer tuples anyway
1644  *		FillInner		   true if should emit unjoined inner tuples anyway
1645  *		MatchedOuter	   true if found a join match for current outer tuple
1646  *		MatchedInner	   true if found a join match for current inner tuple
1647  *		OuterTupleSlot	   slot in tuple table for cur outer tuple
1648  *		InnerTupleSlot	   slot in tuple table for cur inner tuple
1649  *		MarkedTupleSlot    slot in tuple table for marked tuple
1650  *		NullOuterTupleSlot prepared null tuple for right outer joins
1651  *		NullInnerTupleSlot prepared null tuple for left outer joins
1652  *		OuterEContext	   workspace for computing outer tuple's join values
1653  *		InnerEContext	   workspace for computing inner tuple's join values
1654  * ----------------
1655  */
1656 /* private in nodeMergejoin.c: */
1657 typedef struct MergeJoinClauseData *MergeJoinClause;
1658 
1659 typedef struct MergeJoinState
1660 {
1661 	JoinState	js;				/* its first field is NodeTag */
1662 	int			mj_NumClauses;
1663 	MergeJoinClause mj_Clauses; /* array of length mj_NumClauses */
1664 	int			mj_JoinState;
1665 	bool		mj_SkipMarkRestore;
1666 	bool		mj_ExtraMarks;
1667 	bool		mj_ConstFalseJoin;
1668 	bool		mj_FillOuter;
1669 	bool		mj_FillInner;
1670 	bool		mj_MatchedOuter;
1671 	bool		mj_MatchedInner;
1672 	TupleTableSlot *mj_OuterTupleSlot;
1673 	TupleTableSlot *mj_InnerTupleSlot;
1674 	TupleTableSlot *mj_MarkedTupleSlot;
1675 	TupleTableSlot *mj_NullOuterTupleSlot;
1676 	TupleTableSlot *mj_NullInnerTupleSlot;
1677 	ExprContext *mj_OuterEContext;
1678 	ExprContext *mj_InnerEContext;
1679 } MergeJoinState;
1680 
1681 /* ----------------
1682  *	 HashJoinState information
1683  *
1684  *		hashclauses				original form of the hashjoin condition
1685  *		hj_OuterHashKeys		the outer hash keys in the hashjoin condition
1686  *		hj_InnerHashKeys		the inner hash keys in the hashjoin condition
1687  *		hj_HashOperators		the join operators in the hashjoin condition
1688  *		hj_HashTable			hash table for the hashjoin
1689  *								(NULL if table not built yet)
1690  *		hj_CurHashValue			hash value for current outer tuple
1691  *		hj_CurBucketNo			regular bucket# for current outer tuple
1692  *		hj_CurSkewBucketNo		skew bucket# for current outer tuple
1693  *		hj_CurTuple				last inner tuple matched to current outer
1694  *								tuple, or NULL if starting search
1695  *								(hj_CurXXX variables are undefined if
1696  *								OuterTupleSlot is empty!)
1697  *		hj_OuterTupleSlot		tuple slot for outer tuples
1698  *		hj_HashTupleSlot		tuple slot for inner (hashed) tuples
1699  *		hj_NullOuterTupleSlot	prepared null tuple for right/full outer joins
1700  *		hj_NullInnerTupleSlot	prepared null tuple for left/full outer joins
1701  *		hj_FirstOuterTupleSlot	first tuple retrieved from outer plan
1702  *		hj_JoinState			current state of ExecHashJoin state machine
1703  *		hj_MatchedOuter			true if found a join match for current outer
1704  *		hj_OuterNotEmpty		true if outer relation known not empty
1705  * ----------------
1706  */
1707 
1708 /* these structs are defined in executor/hashjoin.h: */
1709 typedef struct HashJoinTupleData *HashJoinTuple;
1710 typedef struct HashJoinTableData *HashJoinTable;
1711 
1712 typedef struct HashJoinState
1713 {
1714 	JoinState	js;				/* its first field is NodeTag */
1715 	ExprState  *hashclauses;
1716 	List	   *hj_OuterHashKeys;	/* list of ExprState nodes */
1717 	List	   *hj_InnerHashKeys;	/* list of ExprState nodes */
1718 	List	   *hj_HashOperators;	/* list of operator OIDs */
1719 	HashJoinTable hj_HashTable;
1720 	uint32		hj_CurHashValue;
1721 	int			hj_CurBucketNo;
1722 	int			hj_CurSkewBucketNo;
1723 	HashJoinTuple hj_CurTuple;
1724 	TupleTableSlot *hj_OuterTupleSlot;
1725 	TupleTableSlot *hj_HashTupleSlot;
1726 	TupleTableSlot *hj_NullOuterTupleSlot;
1727 	TupleTableSlot *hj_NullInnerTupleSlot;
1728 	TupleTableSlot *hj_FirstOuterTupleSlot;
1729 	int			hj_JoinState;
1730 	bool		hj_MatchedOuter;
1731 	bool		hj_OuterNotEmpty;
1732 } HashJoinState;
1733 
1734 
1735 /* ----------------------------------------------------------------
1736  *				 Materialization State Information
1737  * ----------------------------------------------------------------
1738  */
1739 
1740 /* ----------------
1741  *	 MaterialState information
1742  *
1743  *		materialize nodes are used to materialize the results
1744  *		of a subplan into a temporary file.
1745  *
1746  *		ss.ss_ScanTupleSlot refers to output of underlying plan.
1747  * ----------------
1748  */
1749 typedef struct MaterialState
1750 {
1751 	ScanState	ss;				/* its first field is NodeTag */
1752 	int			eflags;			/* capability flags to pass to tuplestore */
1753 	bool		eof_underlying; /* reached end of underlying plan? */
1754 	Tuplestorestate *tuplestorestate;
1755 } MaterialState;
1756 
1757 /* ----------------
1758  *	 SortState information
1759  * ----------------
1760  */
1761 typedef struct SortState
1762 {
1763 	ScanState	ss;				/* its first field is NodeTag */
1764 	bool		randomAccess;	/* need random access to sort output? */
1765 	bool		bounded;		/* is the result set bounded? */
1766 	int64		bound;			/* if bounded, how many tuples are needed */
1767 	bool		sort_Done;		/* sort completed yet? */
1768 	bool		bounded_Done;	/* value of bounded we did the sort with */
1769 	int64		bound_Done;		/* value of bound we did the sort with */
1770 	void	   *tuplesortstate; /* private state of tuplesort.c */
1771 } SortState;
1772 
1773 /* ---------------------
1774  *	GroupState information
1775  * ---------------------
1776  */
1777 typedef struct GroupState
1778 {
1779 	ScanState	ss;				/* its first field is NodeTag */
1780 	FmgrInfo   *eqfunctions;	/* per-field lookup data for equality fns */
1781 	bool		grp_done;		/* indicates completion of Group scan */
1782 } GroupState;
1783 
1784 /* ---------------------
1785  *	AggState information
1786  *
1787  *	ss.ss_ScanTupleSlot refers to output of underlying plan.
1788  *
1789  *	Note: ss.ps.ps_ExprContext contains ecxt_aggvalues and
1790  *	ecxt_aggnulls arrays, which hold the computed agg values for the current
1791  *	input group during evaluation of an Agg node's output tuple(s).  We
1792  *	create a second ExprContext, tmpcontext, in which to evaluate input
1793  *	expressions and run the aggregate transition functions.
1794  * ---------------------
1795  */
1796 /* these structs are private in nodeAgg.c: */
1797 typedef struct AggStatePerAggData *AggStatePerAgg;
1798 typedef struct AggStatePerTransData *AggStatePerTrans;
1799 typedef struct AggStatePerGroupData *AggStatePerGroup;
1800 typedef struct AggStatePerPhaseData *AggStatePerPhase;
1801 typedef struct AggStatePerHashData *AggStatePerHash;
1802 
1803 typedef struct AggState
1804 {
1805 	ScanState	ss;				/* its first field is NodeTag */
1806 	List	   *aggs;			/* all Aggref nodes in targetlist & quals */
1807 	int			numaggs;		/* length of list (could be zero!) */
1808 	int			numtrans;		/* number of pertrans items */
1809 	AggStrategy aggstrategy;	/* strategy mode */
1810 	AggSplit	aggsplit;		/* agg-splitting mode, see nodes.h */
1811 	AggStatePerPhase phase;		/* pointer to current phase data */
1812 	int			numphases;		/* number of phases (including phase 0) */
1813 	int			current_phase;	/* current phase number */
1814 	AggStatePerAgg peragg;		/* per-Aggref information */
1815 	AggStatePerTrans pertrans;	/* per-Trans state information */
1816 	ExprContext *hashcontext;	/* econtexts for long-lived data (hashtable) */
1817 	ExprContext **aggcontexts;	/* econtexts for long-lived data (per GS) */
1818 	ExprContext *tmpcontext;	/* econtext for input expressions */
1819 	ExprContext *curaggcontext; /* currently active aggcontext */
1820 	AggStatePerTrans curpertrans;	/* currently active trans state, if any */
1821 	bool		input_done;		/* indicates end of input */
1822 	bool		agg_done;		/* indicates completion of Agg scan */
1823 	int			projected_set;	/* The last projected grouping set */
1824 	int			current_set;	/* The current grouping set being evaluated */
1825 	Bitmapset  *grouped_cols;	/* grouped cols in current projection */
1826 	List	   *all_grouped_cols;	/* list of all grouped cols in DESC order */
1827 	/* These fields are for grouping set phase data */
1828 	int			maxsets;		/* The max number of sets in any phase */
1829 	AggStatePerPhase phases;	/* array of all phases */
1830 	Tuplesortstate *sort_in;	/* sorted input to phases > 1 */
1831 	Tuplesortstate *sort_out;	/* input is copied here for next phase */
1832 	TupleTableSlot *sort_slot;	/* slot for sort results */
1833 	/* these fields are used in AGG_PLAIN and AGG_SORTED modes: */
1834 	AggStatePerGroup pergroup;	/* per-Aggref-per-group working state */
1835 	HeapTuple	grp_firstTuple; /* copy of first tuple of current group */
1836 	/* these fields are used in AGG_HASHED and AGG_MIXED modes: */
1837 	bool		table_filled;	/* hash table filled yet? */
1838 	int			num_hashes;
1839 	AggStatePerHash perhash;
1840 	AggStatePerGroup *hash_pergroup;	/* array of per-group pointers */
1841 	/* support for evaluation of agg input expressions: */
1842 	ProjectionInfo *combinedproj;	/* projection machinery */
1843 	AggStatePerAgg curperagg;	/* currently active aggregate, if any */
1844 } AggState;
1845 
1846 /* ----------------
1847  *	WindowAggState information
1848  * ----------------
1849  */
1850 /* these structs are private in nodeWindowAgg.c: */
1851 typedef struct WindowStatePerFuncData *WindowStatePerFunc;
1852 typedef struct WindowStatePerAggData *WindowStatePerAgg;
1853 
1854 typedef struct WindowAggState
1855 {
1856 	ScanState	ss;				/* its first field is NodeTag */
1857 
1858 	/* these fields are filled in by ExecInitExpr: */
1859 	List	   *funcs;			/* all WindowFunc nodes in targetlist */
1860 	int			numfuncs;		/* total number of window functions */
1861 	int			numaggs;		/* number that are plain aggregates */
1862 
1863 	WindowStatePerFunc perfunc; /* per-window-function information */
1864 	WindowStatePerAgg peragg;	/* per-plain-aggregate information */
1865 	FmgrInfo   *partEqfunctions;	/* equality funcs for partition columns */
1866 	FmgrInfo   *ordEqfunctions; /* equality funcs for ordering columns */
1867 	Tuplestorestate *buffer;	/* stores rows of current partition */
1868 	int			current_ptr;	/* read pointer # for current */
1869 	int64		spooled_rows;	/* total # of rows in buffer */
1870 	int64		currentpos;		/* position of current row in partition */
1871 	int64		frameheadpos;	/* current frame head position */
1872 	int64		frametailpos;	/* current frame tail position */
1873 	/* use struct pointer to avoid including windowapi.h here */
1874 	struct WindowObjectData *agg_winobj;	/* winobj for aggregate fetches */
1875 	int64		aggregatedbase; /* start row for current aggregates */
1876 	int64		aggregatedupto; /* rows before this one are aggregated */
1877 
1878 	int			frameOptions;	/* frame_clause options, see WindowDef */
1879 	ExprState  *startOffset;	/* expression for starting bound offset */
1880 	ExprState  *endOffset;		/* expression for ending bound offset */
1881 	Datum		startOffsetValue;	/* result of startOffset evaluation */
1882 	Datum		endOffsetValue; /* result of endOffset evaluation */
1883 
1884 	MemoryContext partcontext;	/* context for partition-lifespan data */
1885 	MemoryContext aggcontext;	/* shared context for aggregate working data */
1886 	MemoryContext curaggcontext;	/* current aggregate's working data */
1887 	ExprContext *tmpcontext;	/* short-term evaluation context */
1888 
1889 	bool		all_first;		/* true if the scan is starting */
1890 	bool		all_done;		/* true if the scan is finished */
1891 	bool		partition_spooled;	/* true if all tuples in current partition
1892 									 * have been spooled into tuplestore */
1893 	bool		more_partitions;	/* true if there's more partitions after
1894 									 * this one */
1895 	bool		framehead_valid;	/* true if frameheadpos is known up to
1896 									 * date for current row */
1897 	bool		frametail_valid;	/* true if frametailpos is known up to
1898 									 * date for current row */
1899 
1900 	TupleTableSlot *first_part_slot;	/* first tuple of current or next
1901 										 * partition */
1902 
1903 	/* temporary slots for tuples fetched back from tuplestore */
1904 	TupleTableSlot *agg_row_slot;
1905 	TupleTableSlot *temp_slot_1;
1906 	TupleTableSlot *temp_slot_2;
1907 } WindowAggState;
1908 
1909 /* ----------------
1910  *	 UniqueState information
1911  *
1912  *		Unique nodes are used "on top of" sort nodes to discard
1913  *		duplicate tuples returned from the sort phase.  Basically
1914  *		all it does is compare the current tuple from the subplan
1915  *		with the previously fetched tuple (stored in its result slot).
1916  *		If the two are identical in all interesting fields, then
1917  *		we just fetch another tuple from the sort and try again.
1918  * ----------------
1919  */
1920 typedef struct UniqueState
1921 {
1922 	PlanState	ps;				/* its first field is NodeTag */
1923 	FmgrInfo   *eqfunctions;	/* per-field lookup data for equality fns */
1924 	MemoryContext tempContext;	/* short-term context for comparisons */
1925 } UniqueState;
1926 
1927 /* ----------------
1928  * GatherState information
1929  *
1930  *		Gather nodes launch 1 or more parallel workers, run a subplan
1931  *		in those workers, and collect the results.
1932  * ----------------
1933  */
1934 typedef struct GatherState
1935 {
1936 	PlanState	ps;				/* its first field is NodeTag */
1937 	bool		initialized;	/* workers launched? */
1938 	bool		need_to_scan_locally;	/* need to read from local plan? */
1939 	/* these fields are set up once: */
1940 	TupleTableSlot *funnel_slot;
1941 	struct ParallelExecutorInfo *pei;
1942 	/* all remaining fields are reinitialized during a rescan: */
1943 	int			nworkers_launched;	/* original number of workers */
1944 	int			nreaders;		/* number of still-active workers */
1945 	int			nextreader;		/* next one to try to read from */
1946 	struct TupleQueueReader **reader;	/* array with nreaders active entries */
1947 } GatherState;
1948 
1949 /* ----------------
1950  * GatherMergeState information
1951  *
1952  *		Gather merge nodes launch 1 or more parallel workers, run a
1953  *		subplan which produces sorted output in each worker, and then
1954  *		merge the results into a single sorted stream.
1955  * ----------------
1956  */
1957 struct GMReaderTupleBuffer;		/* private in nodeGatherMerge.c */
1958 
1959 typedef struct GatherMergeState
1960 {
1961 	PlanState	ps;				/* its first field is NodeTag */
1962 	bool		initialized;	/* workers launched? */
1963 	bool		gm_initialized; /* gather_merge_init() done? */
1964 	bool		need_to_scan_locally;	/* need to read from local plan? */
1965 	/* these fields are set up once: */
1966 	TupleDesc	tupDesc;		/* descriptor for subplan result tuples */
1967 	int			gm_nkeys;		/* number of sort columns */
1968 	SortSupport gm_sortkeys;	/* array of length gm_nkeys */
1969 	struct ParallelExecutorInfo *pei;
1970 	/* all remaining fields are reinitialized during a rescan */
1971 	/* (but the arrays are not reallocated, just cleared) */
1972 	int			nworkers_launched;	/* original number of workers */
1973 	int			nreaders;		/* number of active workers */
1974 	TupleTableSlot **gm_slots;	/* array with nreaders+1 entries */
1975 	struct TupleQueueReader **reader;	/* array with nreaders active entries */
1976 	struct GMReaderTupleBuffer *gm_tuple_buffers;	/* nreaders tuple buffers */
1977 	struct binaryheap *gm_heap; /* binary heap of slot indices */
1978 } GatherMergeState;
1979 
1980 /* ----------------
1981  *	 HashState information
1982  * ----------------
1983  */
1984 typedef struct HashState
1985 {
1986 	PlanState	ps;				/* its first field is NodeTag */
1987 	HashJoinTable hashtable;	/* hash table for the hashjoin */
1988 	List	   *hashkeys;		/* list of ExprState nodes */
1989 	/* hashkeys is same as parent's hj_InnerHashKeys */
1990 } HashState;
1991 
1992 /* ----------------
1993  *	 SetOpState information
1994  *
1995  *		Even in "sorted" mode, SetOp nodes are more complex than a simple
1996  *		Unique, since we have to count how many duplicates to return.  But
1997  *		we also support hashing, so this is really more like a cut-down
1998  *		form of Agg.
1999  * ----------------
2000  */
2001 /* this struct is private in nodeSetOp.c: */
2002 typedef struct SetOpStatePerGroupData *SetOpStatePerGroup;
2003 
2004 typedef struct SetOpState
2005 {
2006 	PlanState	ps;				/* its first field is NodeTag */
2007 	FmgrInfo   *eqfunctions;	/* per-grouping-field equality fns */
2008 	FmgrInfo   *hashfunctions;	/* per-grouping-field hash fns */
2009 	bool		setop_done;		/* indicates completion of output scan */
2010 	long		numOutput;		/* number of dups left to output */
2011 	MemoryContext tempContext;	/* short-term context for comparisons */
2012 	/* these fields are used in SETOP_SORTED mode: */
2013 	SetOpStatePerGroup pergroup;	/* per-group working state */
2014 	HeapTuple	grp_firstTuple; /* copy of first tuple of current group */
2015 	/* these fields are used in SETOP_HASHED mode: */
2016 	TupleHashTable hashtable;	/* hash table with one entry per group */
2017 	MemoryContext tableContext; /* memory context containing hash table */
2018 	bool		table_filled;	/* hash table filled yet? */
2019 	TupleHashIterator hashiter; /* for iterating through hash table */
2020 } SetOpState;
2021 
2022 /* ----------------
2023  *	 LockRowsState information
2024  *
2025  *		LockRows nodes are used to enforce FOR [KEY] UPDATE/SHARE locking.
2026  * ----------------
2027  */
2028 typedef struct LockRowsState
2029 {
2030 	PlanState	ps;				/* its first field is NodeTag */
2031 	List	   *lr_arowMarks;	/* List of ExecAuxRowMarks */
2032 	EPQState	lr_epqstate;	/* for evaluating EvalPlanQual rechecks */
2033 	HeapTuple  *lr_curtuples;	/* locked tuples (one entry per RT entry) */
2034 	int			lr_ntables;		/* length of lr_curtuples[] array */
2035 } LockRowsState;
2036 
2037 /* ----------------
2038  *	 LimitState information
2039  *
2040  *		Limit nodes are used to enforce LIMIT/OFFSET clauses.
2041  *		They just select the desired subrange of their subplan's output.
2042  *
2043  * offset is the number of initial tuples to skip (0 does nothing).
2044  * count is the number of tuples to return after skipping the offset tuples.
2045  * If no limit count was specified, count is undefined and noCount is true.
2046  * When lstate == LIMIT_INITIAL, offset/count/noCount haven't been set yet.
2047  * ----------------
2048  */
2049 typedef enum
2050 {
2051 	LIMIT_INITIAL,				/* initial state for LIMIT node */
2052 	LIMIT_RESCAN,				/* rescan after recomputing parameters */
2053 	LIMIT_EMPTY,				/* there are no returnable rows */
2054 	LIMIT_INWINDOW,				/* have returned a row in the window */
2055 	LIMIT_SUBPLANEOF,			/* at EOF of subplan (within window) */
2056 	LIMIT_WINDOWEND,			/* stepped off end of window */
2057 	LIMIT_WINDOWSTART			/* stepped off beginning of window */
2058 } LimitStateCond;
2059 
2060 typedef struct LimitState
2061 {
2062 	PlanState	ps;				/* its first field is NodeTag */
2063 	ExprState  *limitOffset;	/* OFFSET parameter, or NULL if none */
2064 	ExprState  *limitCount;		/* COUNT parameter, or NULL if none */
2065 	int64		offset;			/* current OFFSET value */
2066 	int64		count;			/* current COUNT, if any */
2067 	bool		noCount;		/* if true, ignore count */
2068 	LimitStateCond lstate;		/* state machine status, as above */
2069 	int64		position;		/* 1-based index of last tuple returned */
2070 	TupleTableSlot *subSlot;	/* tuple last obtained from subplan */
2071 } LimitState;
2072 
2073 #endif							/* EXECNODES_H */
2074