PostgreSQL Source Code  git master
nodes.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodes.h
4  * Definitions for tagged nodes.
5  *
6  *
7  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/nodes/nodes.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef NODES_H
15 #define NODES_H
16 
17 /*
18  * The first field of every node is NodeTag. Each node created (with makeNode)
19  * will have one of the following tags as the value of its first field.
20  *
21  * Note that inserting or deleting node types changes the numbers of other
22  * node types later in the list. This is no problem during development, since
23  * the node numbers are never stored on disk. But don't do it in a released
24  * branch, because that would represent an ABI break for extensions.
25  */
26 typedef enum NodeTag
27 {
28  T_Invalid = 0,
29 
30 #include "nodes/nodetags.h"
32 
33 /*
34  * pg_node_attr() - Used in node definitions to set extra information for
35  * gen_node_support.pl
36  *
37  * Attributes can be attached to a node as a whole (place the attribute
38  * specification on the first line after the struct's opening brace)
39  * or to a specific field (place it at the end of that field's line). The
40  * argument is a comma-separated list of attributes. Unrecognized attributes
41  * cause an error.
42  *
43  * Valid node attributes:
44  *
45  * - abstract: Abstract types are types that cannot be instantiated but that
46  * can be supertypes of other types. We track their fields, so that
47  * subtypes can use them, but we don't emit a node tag, so you can't
48  * instantiate them.
49  *
50  * - custom_copy_equal: Has custom implementations in copyfuncs.c and
51  * equalfuncs.c.
52  *
53  * - custom_read_write: Has custom implementations in outfuncs.c and
54  * readfuncs.c.
55  *
56  * - no_copy: Does not support copyObject() at all.
57  *
58  * - no_equal: Does not support equal() at all.
59  *
60  * - no_copy_equal: Shorthand for both no_copy and no_equal.
61  *
62  * - no_read: Does not support nodeRead() at all.
63  *
64  * - nodetag_only: Does not support copyObject(), equal(), outNode(),
65  * or nodeRead().
66  *
67  * - special_read_write: Has special treatment in outNode() and nodeRead().
68  *
69  * - nodetag_number(VALUE): assign the specified nodetag number instead of
70  * an auto-generated number. Typically this would only be used in stable
71  * branches, to give a newly-added node type a number without breaking ABI
72  * by changing the numbers of existing node types.
73  *
74  * Node types can be supertypes of other types whether or not they are marked
75  * abstract: if a node struct appears as the first field of another struct
76  * type, then it is the supertype of that type. The no_copy, no_equal, and
77  * no_read node attributes are automatically inherited from the supertype.
78  * (Notice that nodetag_only does not inherit, so it's not quite equivalent
79  * to a combination of other attributes.)
80  *
81  * Valid node field attributes:
82  *
83  * - array_size(OTHERFIELD): This field is a dynamically allocated array with
84  * size indicated by the mentioned other field. The other field is either a
85  * scalar or a list, in which case the length of the list is used.
86  *
87  * - copy_as(VALUE): In copyObject(), replace the field's value with VALUE.
88  *
89  * - copy_as_scalar: In copyObject(), copy the field as a scalar value
90  * (e.g. a pointer) even if it is a node-type pointer.
91  *
92  * - equal_as_scalar: In equal(), compare the field as a scalar value
93  * even if it is a node-type pointer.
94  *
95  * - equal_ignore: Ignore the field for equality.
96  *
97  * - equal_ignore_if_zero: Ignore the field for equality if it is zero.
98  * (Otherwise, compare normally.)
99  *
100  * - read_as(VALUE): In nodeRead(), replace the field's value with VALUE.
101  *
102  * - read_write_ignore: Ignore the field for read/write. This is only allowed
103  * if the node type is marked no_read or read_as() is also specified.
104  *
105  * - write_only_relids, write_only_nondefault_pathtarget, write_only_req_outer:
106  * Special handling for Path struct; see there.
107  *
108  */
109 #define pg_node_attr(...)
110 
111 /*
112  * The first field of a node of any type is guaranteed to be the NodeTag.
113  * Hence the type of any node can be gotten by casting it to Node. Declaring
114  * a variable to be of Node * (instead of void *) can also facilitate
115  * debugging.
116  */
117 typedef struct Node
118 {
121 
122 #define nodeTag(nodeptr) (((const Node*)(nodeptr))->type)
123 
124 /*
125  * newNode -
126  * create a new node of the specified size and tag the node with the
127  * specified tag.
128  *
129  * !WARNING!: Avoid using newNode directly. You should be using the
130  * macro makeNode. eg. to create a Query node, use makeNode(Query)
131  *
132  * Note: the size argument should always be a compile-time constant, so the
133  * apparent risk of multiple evaluation doesn't matter in practice.
134  */
135 #ifdef __GNUC__
136 
137 /* With GCC, we can use a compound statement within an expression */
138 #define newNode(size, tag) \
139 ({ Node *_result; \
140  AssertMacro((size) >= sizeof(Node)); /* need the tag, at least */ \
141  _result = (Node *) palloc0fast(size); \
142  _result->type = (tag); \
143  _result; \
144 })
145 #else
146 
147 /*
148  * There is no way to dereference the palloc'ed pointer to assign the
149  * tag, and also return the pointer itself, so we need a holder variable.
150  * Fortunately, this macro isn't recursive so we just define
151  * a global variable for this purpose.
152  */
154 
155 #define newNode(size, tag) \
156 ( \
157  AssertMacro((size) >= sizeof(Node)), /* need the tag, at least */ \
158  newNodeMacroHolder = (Node *) palloc0fast(size), \
159  newNodeMacroHolder->type = (tag), \
160  newNodeMacroHolder \
161 )
162 #endif /* __GNUC__ */
163 
164 
165 #define makeNode(_type_) ((_type_ *) newNode(sizeof(_type_),T_##_type_))
166 #define NodeSetTag(nodeptr,t) (((Node*)(nodeptr))->type = (t))
167 
168 #define IsA(nodeptr,_type_) (nodeTag(nodeptr) == T_##_type_)
169 
170 /*
171  * castNode(type, ptr) casts ptr to "type *", and if assertions are enabled,
172  * verifies that the node has the appropriate type (using its nodeTag()).
173  *
174  * Use an inline function when assertions are enabled, to avoid multiple
175  * evaluations of the ptr argument (which could e.g. be a function call).
176  */
177 #ifdef USE_ASSERT_CHECKING
178 static inline Node *
179 castNodeImpl(NodeTag type, void *ptr)
180 {
181  Assert(ptr == NULL || nodeTag(ptr) == type);
182  return (Node *) ptr;
183 }
184 #define castNode(_type_, nodeptr) ((_type_ *) castNodeImpl(T_##_type_, nodeptr))
185 #else
186 #define castNode(_type_, nodeptr) ((_type_ *) (nodeptr))
187 #endif /* USE_ASSERT_CHECKING */
188 
189 
190 /* ----------------------------------------------------------------
191  * extern declarations follow
192  * ----------------------------------------------------------------
193  */
194 
195 /*
196  * nodes/{outfuncs.c,print.c}
197  */
198 struct Bitmapset; /* not to include bitmapset.h here */
199 struct StringInfoData; /* not to include stringinfo.h here */
200 
201 extern void outNode(struct StringInfoData *str, const void *obj);
202 extern void outToken(struct StringInfoData *str, const char *s);
203 extern void outBitmapset(struct StringInfoData *str,
204  const struct Bitmapset *bms);
205 extern void outDatum(struct StringInfoData *str, uintptr_t value,
206  int typlen, bool typbyval);
207 extern char *nodeToString(const void *obj);
208 extern char *bmsToString(const struct Bitmapset *bms);
209 
210 /*
211  * nodes/{readfuncs.c,read.c}
212  */
213 extern void *stringToNode(const char *str);
214 #ifdef WRITE_READ_PARSE_PLAN_TREES
215 extern void *stringToNodeWithLocations(const char *str);
216 #endif
217 extern struct Bitmapset *readBitmapset(void);
218 extern uintptr_t readDatum(bool typbyval);
219 extern bool *readBoolCols(int numCols);
220 extern int *readIntCols(int numCols);
221 extern Oid *readOidCols(int numCols);
222 extern int16 *readAttrNumberCols(int numCols);
223 
224 /*
225  * nodes/copyfuncs.c
226  */
227 extern void *copyObjectImpl(const void *from);
228 
229 /* cast result back to argument type, if supported by compiler */
230 #ifdef HAVE_TYPEOF
231 #define copyObject(obj) ((typeof(obj)) copyObjectImpl(obj))
232 #else
233 #define copyObject(obj) copyObjectImpl(obj)
234 #endif
235 
236 /*
237  * nodes/equalfuncs.c
238  */
239 extern bool equal(const void *a, const void *b);
240 
241 
242 /*
243  * Typedefs for identifying qualifier selectivities and plan costs as such.
244  * These are just plain "double"s, but declaring a variable as Selectivity
245  * or Cost makes the intent more obvious.
246  *
247  * These could have gone into plannodes.h or some such, but many files
248  * depend on them...
249  */
250 typedef double Selectivity; /* fraction of tuples a qualifier will pass */
251 typedef double Cost; /* execution cost (in page-access units) */
252 typedef double Cardinality; /* (estimated) number of rows or other integer
253  * count */
254 
255 
256 /*
257  * CmdType -
258  * enums for type of operation represented by a Query or PlannedStmt
259  *
260  * This is needed in both parsenodes.h and plannodes.h, so put it here...
261  */
262 typedef enum CmdType
263 {
265  CMD_SELECT, /* select stmt */
266  CMD_UPDATE, /* update stmt */
267  CMD_INSERT, /* insert stmt */
268  CMD_DELETE, /* delete stmt */
269  CMD_MERGE, /* merge stmt */
270  CMD_UTILITY, /* cmds like create, destroy, copy, vacuum,
271  * etc. */
272  CMD_NOTHING /* dummy command for instead nothing rules
273  * with qual */
275 
276 
277 /*
278  * JoinType -
279  * enums for types of relation joins
280  *
281  * JoinType determines the exact semantics of joining two relations using
282  * a matching qualification. For example, it tells what to do with a tuple
283  * that has no match in the other relation.
284  *
285  * This is needed in both parsenodes.h and plannodes.h, so put it here...
286  */
287 typedef enum JoinType
288 {
289  /*
290  * The canonical kinds of joins according to the SQL JOIN syntax. Only
291  * these codes can appear in parser output (e.g., JoinExpr nodes).
292  */
293  JOIN_INNER, /* matching tuple pairs only */
294  JOIN_LEFT, /* pairs + unmatched LHS tuples */
295  JOIN_FULL, /* pairs + unmatched LHS + unmatched RHS */
296  JOIN_RIGHT, /* pairs + unmatched RHS tuples */
297 
298  /*
299  * Semijoins and anti-semijoins (as defined in relational theory) do not
300  * appear in the SQL JOIN syntax, but there are standard idioms for
301  * representing them (e.g., using EXISTS). The planner recognizes these
302  * cases and converts them to joins. So the planner and executor must
303  * support these codes. NOTE: in JOIN_SEMI output, it is unspecified
304  * which matching RHS row is joined to. In JOIN_ANTI output, the row is
305  * guaranteed to be null-extended.
306  */
307  JOIN_SEMI, /* 1 copy of each LHS row that has match(es) */
308  JOIN_ANTI, /* 1 copy of each LHS row that has no match */
309 
310  /*
311  * These codes are used internally in the planner, but are not supported
312  * by the executor (nor, indeed, by most of the planner).
313  */
314  JOIN_UNIQUE_OUTER, /* LHS path must be made unique */
315  JOIN_UNIQUE_INNER /* RHS path must be made unique */
316 
317  /*
318  * We might need additional join types someday.
319  */
321 
322 /*
323  * OUTER joins are those for which pushed-down quals must behave differently
324  * from the join's own quals. This is in fact everything except INNER and
325  * SEMI joins. However, this macro must also exclude the JOIN_UNIQUE symbols
326  * since those are temporary proxies for what will eventually be an INNER
327  * join.
328  *
329  * Note: semijoins are a hybrid case, but we choose to treat them as not
330  * being outer joins. This is okay principally because the SQL syntax makes
331  * it impossible to have a pushed-down qual that refers to the inner relation
332  * of a semijoin; so there is no strong need to distinguish join quals from
333  * pushed-down quals. This is convenient because for almost all purposes,
334  * quals attached to a semijoin can be treated the same as innerjoin quals.
335  */
336 #define IS_OUTER_JOIN(jointype) \
337  (((1 << (jointype)) & \
338  ((1 << JOIN_LEFT) | \
339  (1 << JOIN_FULL) | \
340  (1 << JOIN_RIGHT) | \
341  (1 << JOIN_ANTI))) != 0)
342 
343 /*
344  * AggStrategy -
345  * overall execution strategies for Agg plan nodes
346  *
347  * This is needed in both pathnodes.h and plannodes.h, so put it here...
348  */
349 typedef enum AggStrategy
350 {
351  AGG_PLAIN, /* simple agg across all input rows */
352  AGG_SORTED, /* grouped agg, input must be sorted */
353  AGG_HASHED, /* grouped agg, use internal hashtable */
354  AGG_MIXED /* grouped agg, hash and sort both used */
356 
357 /*
358  * AggSplit -
359  * splitting (partial aggregation) modes for Agg plan nodes
360  *
361  * This is needed in both pathnodes.h and plannodes.h, so put it here...
362  */
363 
364 /* Primitive options supported by nodeAgg.c: */
365 #define AGGSPLITOP_COMBINE 0x01 /* substitute combinefn for transfn */
366 #define AGGSPLITOP_SKIPFINAL 0x02 /* skip finalfn, return state as-is */
367 #define AGGSPLITOP_SERIALIZE 0x04 /* apply serialfn to output */
368 #define AGGSPLITOP_DESERIALIZE 0x08 /* apply deserialfn to input */
369 
370 /* Supported operating modes (i.e., useful combinations of these options): */
371 typedef enum AggSplit
372 {
373  /* Basic, non-split aggregation: */
375  /* Initial phase of partial aggregation, with serialization: */
377  /* Final phase of partial aggregation, with deserialization: */
380 
381 /* Test whether an AggSplit value selects each primitive option: */
382 #define DO_AGGSPLIT_COMBINE(as) (((as) & AGGSPLITOP_COMBINE) != 0)
383 #define DO_AGGSPLIT_SKIPFINAL(as) (((as) & AGGSPLITOP_SKIPFINAL) != 0)
384 #define DO_AGGSPLIT_SERIALIZE(as) (((as) & AGGSPLITOP_SERIALIZE) != 0)
385 #define DO_AGGSPLIT_DESERIALIZE(as) (((as) & AGGSPLITOP_DESERIALIZE) != 0)
386 
387 /*
388  * SetOpCmd and SetOpStrategy -
389  * overall semantics and execution strategies for SetOp plan nodes
390  *
391  * This is needed in both pathnodes.h and plannodes.h, so put it here...
392  */
393 typedef enum SetOpCmd
394 {
400 
401 typedef enum SetOpStrategy
402 {
403  SETOP_SORTED, /* input must be sorted */
404  SETOP_HASHED /* use internal hashtable */
406 
407 /*
408  * OnConflictAction -
409  * "ON CONFLICT" clause type of query
410  *
411  * This is needed in both parsenodes.h and plannodes.h, so put it here...
412  */
413 typedef enum OnConflictAction
414 {
415  ONCONFLICT_NONE, /* No "ON CONFLICT" clause */
416  ONCONFLICT_NOTHING, /* ON CONFLICT ... DO NOTHING */
417  ONCONFLICT_UPDATE /* ON CONFLICT ... DO UPDATE */
419 
420 /*
421  * LimitOption -
422  * LIMIT option of query
423  *
424  * This is needed in both parsenodes.h and plannodes.h, so put it here...
425  */
426 typedef enum LimitOption
427 {
428  LIMIT_OPTION_COUNT, /* FETCH FIRST... ONLY */
429  LIMIT_OPTION_WITH_TIES, /* FETCH FIRST... WITH TIES */
430  LIMIT_OPTION_DEFAULT, /* No limit present */
432 
433 #endif /* NODES_H */
#define PGDLLIMPORT
Definition: c.h:1252
signed short int16
Definition: c.h:429
static struct @143 value
int b
Definition: isn.c:70
int a
Definition: isn.c:69
Assert(fmt[strlen(fmt) - 1] !='\n')
SetOpCmd
Definition: nodes.h:394
@ SETOPCMD_EXCEPT
Definition: nodes.h:397
@ SETOPCMD_EXCEPT_ALL
Definition: nodes.h:398
@ SETOPCMD_INTERSECT_ALL
Definition: nodes.h:396
@ SETOPCMD_INTERSECT
Definition: nodes.h:395
SetOpStrategy
Definition: nodes.h:402
@ SETOP_HASHED
Definition: nodes.h:404
@ SETOP_SORTED
Definition: nodes.h:403
double Cost
Definition: nodes.h:251
void outToken(struct StringInfoData *str, const char *s)
#define nodeTag(nodeptr)
Definition: nodes.h:122
struct Node Node
void outDatum(struct StringInfoData *str, uintptr_t value, int typlen, bool typbyval)
int * readIntCols(int numCols)
#define AGGSPLITOP_DESERIALIZE
Definition: nodes.h:368
#define AGGSPLITOP_SKIPFINAL
Definition: nodes.h:366
OnConflictAction
Definition: nodes.h:414
@ ONCONFLICT_NONE
Definition: nodes.h:415
@ ONCONFLICT_UPDATE
Definition: nodes.h:417
@ ONCONFLICT_NOTHING
Definition: nodes.h:416
#define AGGSPLITOP_SERIALIZE
Definition: nodes.h:367
PGDLLIMPORT Node * newNodeMacroHolder
Definition: nodes.c:31
struct Bitmapset * readBitmapset(void)
Definition: readfuncs.c:245
void outBitmapset(struct StringInfoData *str, const struct Bitmapset *bms)
double Cardinality
Definition: nodes.h:252
CmdType
Definition: nodes.h:263
@ CMD_MERGE
Definition: nodes.h:269
@ CMD_UTILITY
Definition: nodes.h:270
@ CMD_INSERT
Definition: nodes.h:267
@ CMD_DELETE
Definition: nodes.h:268
@ CMD_UNKNOWN
Definition: nodes.h:264
@ CMD_UPDATE
Definition: nodes.h:266
@ CMD_SELECT
Definition: nodes.h:265
@ CMD_NOTHING
Definition: nodes.h:272
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:225
AggStrategy
Definition: nodes.h:350
@ AGG_SORTED
Definition: nodes.h:352
@ AGG_HASHED
Definition: nodes.h:353
@ AGG_MIXED
Definition: nodes.h:354
@ AGG_PLAIN
Definition: nodes.h:351
char * bmsToString(const struct Bitmapset *bms)
NodeTag
Definition: nodes.h:27
@ T_Invalid
Definition: nodes.h:28
double Selectivity
Definition: nodes.h:250
AggSplit
Definition: nodes.h:372
@ AGGSPLIT_FINAL_DESERIAL
Definition: nodes.h:378
@ AGGSPLIT_SIMPLE
Definition: nodes.h:374
@ AGGSPLIT_INITIAL_SERIAL
Definition: nodes.h:376
LimitOption
Definition: nodes.h:427
@ LIMIT_OPTION_COUNT
Definition: nodes.h:428
@ LIMIT_OPTION_WITH_TIES
Definition: nodes.h:429
@ LIMIT_OPTION_DEFAULT
Definition: nodes.h:430
void * stringToNode(const char *str)
Definition: read.c:90
bool * readBoolCols(int numCols)
#define AGGSPLITOP_COMBINE
Definition: nodes.h:365
Oid * readOidCols(int numCols)
void outNode(struct StringInfoData *str, const void *obj)
void * copyObjectImpl(const void *from)
Definition: copyfuncs.c:177
uintptr_t readDatum(bool typbyval)
Definition: readfuncs.c:705
int16 * readAttrNumberCols(int numCols)
JoinType
Definition: nodes.h:288
@ JOIN_SEMI
Definition: nodes.h:307
@ JOIN_FULL
Definition: nodes.h:295
@ JOIN_INNER
Definition: nodes.h:293
@ JOIN_RIGHT
Definition: nodes.h:296
@ JOIN_LEFT
Definition: nodes.h:294
@ JOIN_UNIQUE_OUTER
Definition: nodes.h:314
@ JOIN_UNIQUE_INNER
Definition: nodes.h:315
@ JOIN_ANTI
Definition: nodes.h:308
char * nodeToString(const void *obj)
Definition: outfuncs.c:874
unsigned int Oid
Definition: postgres_ext.h:31
Definition: nodes.h:118
NodeTag type
Definition: nodes.h:119