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