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