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