1 %{
2 
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5  *
6  * gram.y
7  *	  POSTGRESQL BISON rules/actions
8  *
9  * Portions Copyright (c) 2003-2018, PgPool Global Development Group
10  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  *	  src/backend/parser/gram.y
16  *
17  * HISTORY
18  *	  AUTHOR			DATE			MAJOR EVENT
19  *	  Andrew Yu			Sept, 1994		POSTQUEL to SQL conversion
20  *	  Andrew Yu			Oct, 1994		lispy code conversion
21  *
22  * NOTES
23  *	  CAPITALS are used to represent terminal symbols.
24  *	  non-capitals are used to represent non-terminals.
25  *
26  *	  In general, nothing in this file should initiate database accesses
27  *	  nor depend on changeable state (such as SET variables).  If you do
28  *	  database accesses, your code will fail when we have aborted the
29  *	  current transaction and are just parsing commands to find the next
30  *	  ROLLBACK or COMMIT.  If you make use of SET variables, then you
31  *	  will do the wrong thing in multi-query strings like this:
32  *			SET constraint_exclusion TO off; SELECT * FROM foo;
33  *	  because the entire string is parsed by gram.y before the SET gets
34  *	  executed.  Anything that depends on the database or changeable state
35  *	  should be handled during parse analysis so that it happens at the
36  *	  right time not the wrong time.
37  *
38  * WARNINGS
39  *	  If you use a list, make sure the datum is a node so that the printing
40  *	  routines work.
41  *
42  *	  Sometimes we assign constants to makeStrings. Make sure we don't free
43  *	  those.
44  *
45  *-------------------------------------------------------------------------
46  */
47 
48 #include "pool_parser.h"
49 #include "utils/elog.h"
50 #include "utils/palloc.h"
51 #include <ctype.h>
52 #include <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include "nodes.h"
58 #include "keywords.h"
59 #include "gramparse.h"
60 #include "makefuncs.h"
61 #include "pool_string.h"
62 #include "parser.h"
63 #include "pg_class.h"
64 #include "pg_trigger.h"
65 
66 /* This is a configuration parameter since PostgreSQL 9.5.
67  * We set this false in pgpool-II. This is default in PostgreSQL.
68  */
69 bool operator_precedence_warning = false;
70 
71 /*
72  * Definition taken from
73  * postgreSQL source code file: src/include/commands/trigger.h
74  */
75 #define TRIGGER_FIRES_ON_ORIGIN                         'O'
76 #define TRIGGER_FIRES_ALWAYS                            'A'
77 #define TRIGGER_FIRES_ON_REPLICA                        'R'
78 #define TRIGGER_DISABLED                                'D'
79 
80 /*
81  * Definition taken from
82  * postgreSQL source code file: src/include/catalog/pg_class.h
83  */
84 
85 #define           REPLICA_IDENTITY_DEFAULT      'd'
86 #define           REPLICA_IDENTITY_NOTHING      'n'
87 #define           REPLICA_IDENTITY_FULL         'f'
88 #define           REPLICA_IDENTITY_INDEX        'i'
89 
90 /*
91  * Definition taken from
92  * postgreSQL source code file: src/include/catalog/pg_attribute.h
93  */
94 #define		  ATTRIBUTE_IDENTITY_ALWAYS	'a'
95 #define		  ATTRIBUTE_IDENTITY_BY_DEFAULT 'd'
96 
97 /*
98  * Definition taken from
99  * postgreSQL source code file: src/include/utils/xml.h
100  */
101 typedef enum
102 {
103 	XML_STANDALONE_YES,
104 	XML_STANDALONE_NO,
105 	XML_STANDALONE_NO_VALUE,
106 	XML_STANDALONE_OMITTED
107 } XmlStandaloneType;
108 
109 /*
110  * Location tracking support --- simpler than bison's default, since we only
111  * want to track the start position not the end position of each nonterminal.
112  */
113 #define YYLLOC_DEFAULT(Current, Rhs, N) \
114 	do { \
115 		if ((N) > 0) \
116 			(Current) = (Rhs)[1]; \
117 		else \
118 			(Current) = (-1); \
119 	} while (0)
120 
121 /*
122  * The above macro assigns -1 (unknown) as the parse location of any
123  * nonterminal that was reduced from an empty rule, or whose leftmost
124  * component was reduced from an empty rule.  This is problematic
125  * for nonterminals defined like
126  *		OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ;
127  * because we'll set -1 as the location during the first reduction and then
128  * copy it during each subsequent reduction, leaving us with -1 for the
129  * location even when the list is not empty.  To fix that, do this in the
130  * action for the nonempty rule(s):
131  *		if (@$ < 0) @$ = @2;
132  * (Although we have many nonterminals that follow this pattern, we only
133  * bother with fixing @$ like this when the nonterminal's parse location
134  * is actually referenced in some rule.)
135  *
136  * A cleaner answer would be to make YYLLOC_DEFAULT scan all the Rhs
137  * locations until it's found one that's not -1.  Then we'd get a correct
138  * location for any nonterminal that isn't entirely empty.  But this way
139  * would add overhead to every rule reduction, and so far there's not been
140  * a compelling reason to pay that overhead.
141  */
142 
143 /*
144  * Bison doesn't allocate anything that needs to live across parser calls,
145  * so we can easily have it use palloc instead of malloc.  This prevents
146  * memory leaks if we error out during parsing.  Note this only works with
147  * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
148  * if possible, so there's not really much problem anyhow, at least if
149  * you're building with gcc.
150  */
151 #define YYMALLOC palloc
152 #define YYFREE   pfree
153 
154 /* Private struct for the result of privilege_target production */
155 typedef struct PrivTarget
156 {
157 	GrantTargetType targtype;
158 	ObjectType	objtype;
159 	List	   *objs;
160 } PrivTarget;
161 
162 /* Private struct for the result of import_qualification production */
163 typedef struct ImportQual
164 {
165 	ImportForeignSchemaType type;
166 	List	   *table_names;
167 } ImportQual;
168 
169 /* ConstraintAttributeSpec yields an integer bitmask of these flags: */
170 #define CAS_NOT_DEFERRABLE			0x01
171 #define CAS_DEFERRABLE				0x02
172 #define CAS_INITIALLY_IMMEDIATE		0x04
173 #define CAS_INITIALLY_DEFERRED		0x08
174 #define CAS_NOT_VALID				0x10
175 #define CAS_NO_INHERIT				0x20
176 
177 
178 #define parser_yyerror(msg)  scanner_yyerror(msg, yyscanner)
179 #define parser_errposition(pos)  scanner_errposition(pos, yyscanner)
180 
181 static void base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner,
182 						 const char *msg);
183 static RawStmt *makeRawStmt(Node *stmt, int stmt_location);
184 static void updateRawStmtEnd(RawStmt *rs, int end_location);
185 static Node *makeColumnRef(char *colname, List *indirection,
186 						   int location, core_yyscan_t yyscanner);
187 static Node *makeStringConst(char *str, int location);
188 static Node *makeFloatConst(char *str, int location);
189 static Node *makeBitStringConst(char *str, int location);
190 static Node *makeNullAConst(int location);
191 static Node *makeAConst(Value *v, int location);
192 static Node *makeBoolAConst(bool state, int location);
193 static RoleSpec *makeRoleSpec(RoleSpecType type, int location);
194 static void check_qualified_name(List *names, core_yyscan_t yyscanner);
195 static List *check_func_name(List *names, core_yyscan_t yyscanner);
196 static List *check_indirection(List *indirection, core_yyscan_t yyscanner);
197 static List *extractArgTypes(List *parameters);
198 static List *extractAggrArgTypes(List *aggrargs);
199 static List *makeOrderedSetArgs(List *directargs, List *orderedargs,
200 								core_yyscan_t yyscanner);
201 static void insertSelectOptions(SelectStmt *stmt,
202 								List *sortClause, List *lockingClause,
203 								Node *limitOffset, Node *limitCount,
204 								WithClause *withClause,
205 								core_yyscan_t yyscanner);
206 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
207 static Node *doNegate(Node *n, int location);
208 static void doNegateFloat(Value *v);
209 static Node *makeAndExpr(Node *lexpr, Node *rexpr, int location);
210 static Node *makeOrExpr(Node *lexpr, Node *rexpr, int location);
211 static Node *makeNotExpr(Node *expr, int location);
212 static Node *makeAArrayExpr(List *elements, int location);
213 static Node *makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod,
214 								  int location);
215 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
216 						 List *args, int location);
217 static List *mergeTableFuncParameters(List *func_args, List *columns);
218 static TypeName *TableFuncTypeName(List *columns);
219 static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner);
220 static void SplitColQualList(List *qualList,
221 							 List **constraintList, CollateClause **collClause,
222 							 core_yyscan_t yyscanner);
223 static void processCASbits(int cas_bits, int location, const char *constrType,
224 			   bool *deferrable, bool *initdeferred, bool *not_valid,
225 			   bool *no_inherit, core_yyscan_t yyscanner);
226 static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
227 
228 %}
229 
230 %pure-parser
231 %expect 0
232 %name-prefix="base_yy"
233 %locations
234 
235 %parse-param {core_yyscan_t yyscanner}
236 %lex-param   {core_yyscan_t yyscanner}
237 
238 %union
239 {
240 	core_YYSTYPE		core_yystype;
241 	/* these fields must match core_YYSTYPE: */
242 	int					ival;
243 	char				*str;
244 	const char			*keyword;
245 
246 	char				chr;
247 	bool				boolean;
248 	JoinType			jtype;
249 	DropBehavior		dbehavior;
250 	OnCommitAction		oncommit;
251 	List				*list;
252 	Node				*node;
253 	Value				*value;
254 	ObjectType			objtype;
255 	TypeName			*typnam;
256 	FunctionParameter   *fun_param;
257 	FunctionParameterMode fun_param_mode;
258 	ObjectWithArgs		*objwithargs;
259 	DefElem				*defelt;
260 	SortBy				*sortby;
261 	WindowDef			*windef;
262 	JoinExpr			*jexpr;
263 	IndexElem			*ielem;
264 	Alias				*alias;
265 	RangeVar			*range;
266 	IntoClause			*into;
267 	WithClause			*with;
268 	InferClause			*infer;
269 	OnConflictClause	*onconflict;
270 	A_Indices			*aind;
271 	ResTarget			*target;
272 	struct PrivTarget	*privtarget;
273 	AccessPriv			*accesspriv;
274 	struct ImportQual	*importqual;
275 	InsertStmt			*istmt;
276 	VariableSetStmt		*vsetstmt;
277 	PartitionElem		*partelem;
278 	PartitionSpec		*partspec;
279 	PartitionBoundSpec	*partboundspec;
280 	RoleSpec			*rolespec;
281 }
282 
283 %type <node>	stmt schema_stmt
284 		AlterEventTrigStmt AlterCollationStmt
285 		AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterEnumStmt
286 		AlterFdwStmt AlterForeignServerStmt AlterGroupStmt
287 		AlterObjectDependsStmt AlterObjectSchemaStmt AlterOwnerStmt
288 		AlterOperatorStmt AlterSeqStmt AlterSystemStmt AlterTableStmt
289 		AlterTblSpcStmt AlterExtensionStmt AlterExtensionContentsStmt AlterForeignTableStmt
290 		AlterCompositeTypeStmt AlterUserMappingStmt
291 		AlterRoleStmt AlterRoleSetStmt AlterPolicyStmt
292 		AlterDefaultPrivilegesStmt DefACLAction
293 		AnalyzeStmt CallStmt ClosePortalStmt ClusterStmt CommentStmt
294 		ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
295 		CreateDomainStmt CreateExtensionStmt CreateGroupStmt CreateOpClassStmt
296 		CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
297 		CreateSchemaStmt CreateSeqStmt CreateStmt CreateStatsStmt CreateTableSpaceStmt
298 		CreateFdwStmt CreateForeignServerStmt CreateForeignTableStmt
299 		CreateAssertStmt CreateTransformStmt CreateTrigStmt CreateEventTrigStmt
300 		CreateUserStmt CreateUserMappingStmt CreateRoleStmt CreatePolicyStmt
301 		CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
302 		DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
303 		DropAssertStmt DropCastStmt DropRoleStmt
304 		DropdbStmt DropTableSpaceStmt
305 		DropTransformStmt
306 		DropUserMappingStmt ExplainStmt FetchStmt
307 		GrantStmt GrantRoleStmt ImportForeignSchemaStmt IndexStmt InsertStmt
308 		ListenStmt LoadStmt LockStmt NotifyStmt ExplainableStmt PreparableStmt
309 		CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
310 		RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
311 		RuleActionStmt RuleActionStmtOrEmpty RuleStmt
312 		SecLabelStmt SelectStmt TransactionStmt TruncateStmt
313 		UnlistenStmt UpdateStmt VacuumStmt
314 		VariableResetStmt VariableSetStmt VariableShowStmt
315 		ViewStmt CheckPointStmt CreateConversionStmt
316 		DeallocateStmt PrepareStmt ExecuteStmt
317 		DropOwnedStmt ReassignOwnedStmt
318 		AlterTSConfigurationStmt AlterTSDictionaryStmt
319 		CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
320 		CreatePublicationStmt AlterPublicationStmt
321 		CreateSubscriptionStmt AlterSubscriptionStmt DropSubscriptionStmt
322 
323 %type <node>	select_no_parens select_with_parens select_clause
324 				simple_select values_clause
325 
326 %type <node>	alter_column_default opclass_item opclass_drop alter_using
327 %type <ival>	add_drop opt_asc_desc opt_nulls_order
328 
329 %type <node>	alter_table_cmd alter_type_cmd opt_collate_clause
330 	   replica_identity partition_cmd index_partition_cmd
331 %type <list>	alter_table_cmds alter_type_cmds
332 %type <list>    alter_identity_column_option_list
333 %type <defelt>  alter_identity_column_option
334 
335 %type <dbehavior>	opt_drop_behavior
336 
337 %type <list>	createdb_opt_list createdb_opt_items copy_opt_list
338 				transaction_mode_list
339 				create_extension_opt_list alter_extension_opt_list
340 %type <defelt>	createdb_opt_item copy_opt_item
341 				transaction_mode_item
342 				create_extension_opt_item alter_extension_opt_item
343 
344 %type <ival>	opt_lock lock_type cast_context
345 %type <ival>	vacuum_option_list vacuum_option_elem
346 				analyze_option_list analyze_option_elem
347 %type <boolean>	opt_or_replace
348 				opt_grant_grant_option opt_grant_admin_option
349 				opt_nowait opt_if_exists opt_with_data
350 %type <ival>	opt_nowait_or_skip
351 
352 %type <list>	OptRoleList AlterOptRoleList
353 %type <defelt>	CreateOptRoleElem AlterOptRoleElem
354 
355 %type <str>		opt_type
356 %type <str>		foreign_server_version opt_foreign_server_version
357 %type <str>		opt_in_database
358 
359 %type <str>		OptSchemaName
360 %type <list>	OptSchemaEltList
361 
362 %type <boolean> TriggerForSpec TriggerForType
363 %type <ival>	TriggerActionTime
364 %type <list>	TriggerEvents TriggerOneEvent
365 %type <value>	TriggerFuncArg
366 %type <node>	TriggerWhen
367 %type <str>		TransitionRelName
368 %type <boolean>	TransitionRowOrTable TransitionOldOrNew
369 %type <node>	TriggerTransition
370 
371 %type <list>	event_trigger_when_list event_trigger_value_list
372 %type <defelt>	event_trigger_when_item
373 %type <chr>		enable_trigger
374 
375 %type <str>		copy_file_name
376 				database_name access_method_clause access_method attr_name
377 				name cursor_name file_name
378 				index_name opt_index_name cluster_index_specification
379 
380 %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
381 				opt_class opt_inline_handler opt_validator validator_clause
382 				opt_collate
383 
384 %type <range>	qualified_name insert_target OptConstrFromTable
385 
386 %type <str>		all_Op MathOp
387 
388 %type <str>		row_security_cmd RowSecurityDefaultForCmd
389 %type <boolean> RowSecurityDefaultPermissive
390 %type <node>	RowSecurityOptionalWithCheck RowSecurityOptionalExpr
391 %type <list>	RowSecurityDefaultToRole RowSecurityOptionalToRole
392 
393 %type <str>		iso_level opt_encoding
394 %type <rolespec> grantee
395 %type <list>	grantee_list
396 %type <accesspriv> privilege
397 %type <list>	privileges privilege_list
398 %type <privtarget> privilege_target
399 %type <objwithargs> function_with_argtypes aggregate_with_argtypes operator_with_argtypes
400 %type <list>	function_with_argtypes_list aggregate_with_argtypes_list operator_with_argtypes_list
401 %type <ival>	defacl_privilege_target
402 %type <defelt>	DefACLOption
403 %type <list>	DefACLOptionList
404 %type <ival>	import_qualification_type
405 %type <importqual> import_qualification
406 %type <node>	vacuum_relation
407 
408 %type <list>	stmtblock stmtmulti
409 				OptTableElementList TableElementList OptInherit definition
410 				OptTypedTableElementList TypedTableElementList
411 				reloptions opt_reloptions
412 				OptWith distinct_clause opt_all_clause opt_definition func_args func_args_list
413 				func_args_with_defaults func_args_with_defaults_list
414 				aggr_args aggr_args_list
415 				func_as createfunc_opt_list alterfunc_opt_list
416 				old_aggr_definition old_aggr_list
417 				oper_argtypes RuleActionList RuleActionMulti
418 				opt_column_list columnList opt_name_list
419 				sort_clause opt_sort_clause sortby_list index_params
420 				opt_include opt_c_include index_including_params
421 				name_list role_list from_clause from_list opt_array_bounds
422 				qualified_name_list any_name any_name_list type_name_list
423 				any_operator expr_list attrs
424 				target_list opt_target_list insert_column_list set_target_list
425 				set_clause_list set_clause
426 				def_list operator_def_list indirection opt_indirection
427 				reloption_list group_clause TriggerFuncArgs select_limit
428 				opt_select_limit opclass_item_list opclass_drop_list
429 				opclass_purpose opt_opfamily transaction_mode_list_or_empty
430 				OptTableFuncElementList TableFuncElementList opt_type_modifiers
431 				prep_type_clause
432 				execute_param_clause using_clause returning_clause
433 				opt_enum_val_list enum_val_list table_func_column_list
434 				create_generic_options alter_generic_options
435 				relation_expr_list dostmt_opt_list
436 				transform_element_list transform_type_list
437 				TriggerTransitions TriggerReferencing
438 				publication_name_list
439 				vacuum_relation_list opt_vacuum_relation_list
440 
441 %type <list>	group_by_list
442 %type <node>	group_by_item empty_grouping_set rollup_clause cube_clause
443 %type <node>	grouping_sets_clause
444 %type <node>	opt_publication_for_tables publication_for_tables
445 %type <value>	publication_name_item
446 
447 %type <list>	opt_fdw_options fdw_options
448 %type <defelt>	fdw_option
449 
450 %type <range>	OptTempTableName
451 %type <into>	into_clause create_as_target create_mv_target
452 
453 %type <defelt>	createfunc_opt_item common_func_opt_item dostmt_opt_item
454 %type <fun_param> func_arg func_arg_with_default table_func_column aggr_arg
455 %type <fun_param_mode> arg_class
456 %type <typnam>	func_return func_type
457 
458 %type <boolean>  opt_trusted opt_restart_seqs
459 %type <ival>	 OptTemp
460 %type <ival>	 OptNoLog
461 %type <oncommit> OnCommitOption
462 
463 %type <ival>	for_locking_strength
464 %type <node>	for_locking_item
465 %type <list>	for_locking_clause opt_for_locking_clause for_locking_items
466 %type <list>	locked_rels_list
467 %type <boolean>	all_or_distinct
468 
469 %type <node>	join_outer join_qual
470 %type <jtype>	join_type
471 
472 %type <list>	extract_list overlay_list position_list
473 %type <list>	substr_list trim_list
474 %type <list>	opt_interval interval_second
475 %type <node>	overlay_placing substr_from substr_for
476 
477 %type <boolean> opt_instead
478 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
479 %type <boolean> opt_freeze opt_analyze opt_default opt_recheck
480 %type <defelt>	opt_binary opt_oids copy_delimiter
481 
482 %type <boolean> copy_from opt_program
483 
484 %type <ival>	opt_column event cursor_options opt_hold opt_set_data
485 %type <objtype>	drop_type_any_name drop_type_name drop_type_name_on_any_name
486 				comment_type_any_name comment_type_name
487 				security_label_type_any_name security_label_type_name
488 
489 %type <node>	fetch_args limit_clause select_limit_value
490 				offset_clause select_offset_value
491 				select_fetch_first_value I_or_F_const
492 %type <ival>	row_or_rows first_or_next
493 
494 %type <list>	OptSeqOptList SeqOptList OptParenthesizedSeqOptList
495 %type <defelt>	SeqOptElem
496 
497 %type <istmt>	insert_rest
498 %type <infer>	opt_conf_expr
499 %type <onconflict> opt_on_conflict
500 
501 %type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
502 				 SetResetClause FunctionSetResetClause
503 
504 %type <node>	TableElement TypedTableElement ConstraintElem TableFuncElement
505 %type <node>	columnDef columnOptions
506 %type <defelt>	def_elem reloption_elem old_aggr_elem operator_def_elem
507 %type <node>	def_arg columnElem where_clause where_or_current_clause
508 				a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
509 				columnref in_expr having_clause func_table xmltable array_expr
510 				ExclusionWhereClause operator_def_arg
511 %type <list>	rowsfrom_item rowsfrom_list opt_col_def_list
512 %type <boolean> opt_ordinality
513 %type <list>	ExclusionConstraintList ExclusionConstraintElem
514 %type <list>	func_arg_list
515 %type <node>	func_arg_expr
516 %type <list>	row explicit_row implicit_row type_list array_expr_list
517 %type <node>	case_expr case_arg when_clause case_default
518 %type <list>	when_clause_list
519 %type <ival>	sub_type
520 %type <value>	NumericOnly
521 %type <list>	NumericOnly_list
522 %type <alias>	alias_clause opt_alias_clause
523 %type <list>	func_alias_clause
524 %type <sortby>	sortby
525 %type <ielem>	index_elem
526 %type <node>	table_ref
527 %type <jexpr>	joined_table
528 %type <range>	relation_expr
529 %type <range>	relation_expr_opt_alias
530 %type <node>	tablesample_clause opt_repeatable_clause
531 %type <target>	target_el set_target insert_column_item
532 
533 %type <str>		generic_option_name
534 %type <node>	generic_option_arg
535 %type <defelt>	generic_option_elem alter_generic_option_elem
536 %type <list>	generic_option_list alter_generic_option_list
537 %type <str>		explain_option_name
538 %type <node>	explain_option_arg
539 %type <defelt>	explain_option_elem
540 %type <list>	explain_option_list
541 
542 %type <ival>	reindex_target_type reindex_target_multitable
543 %type <ival>	reindex_option_list reindex_option_elem
544 
545 %type <node>	copy_generic_opt_arg copy_generic_opt_arg_list_item
546 %type <defelt>	copy_generic_opt_elem
547 %type <list>	copy_generic_opt_list copy_generic_opt_arg_list
548 %type <list>	copy_options
549 
550 %type <typnam>	Typename SimpleTypename ConstTypename
551 				GenericType Numeric opt_float
552 				Character ConstCharacter
553 				CharacterWithLength CharacterWithoutLength
554 				ConstDatetime ConstInterval
555 				Bit ConstBit BitWithLength BitWithoutLength
556 %type <str>		character
557 %type <str>		extract_arg
558 %type <boolean> opt_varying opt_timezone opt_no_inherit
559 
560 %type <ival>	Iconst SignedIconst
561 %type <str>		Sconst comment_text notify_payload
562 %type <str>		RoleId opt_boolean_or_string
563 %type <list>	var_list
564 %type <str>		ColId ColLabel var_name type_function_name param_name
565 %type <str>		NonReservedWord NonReservedWord_or_Sconst
566 %type <str>		createdb_opt_name
567 %type <node>	var_value zone_value
568 %type <rolespec> auth_ident RoleSpec opt_granted_by
569 
570 %type <keyword> unreserved_keyword type_func_name_keyword
571 %type <keyword> col_name_keyword reserved_keyword
572 
573 %type <node>	TableConstraint TableLikeClause
574 %type <ival>	TableLikeOptionList TableLikeOption
575 %type <list>	ColQualList
576 %type <node>	ColConstraint ColConstraintElem ConstraintAttr
577 %type <ival>	key_actions key_delete key_match key_update key_action
578 %type <ival>	ConstraintAttributeSpec ConstraintAttributeElem
579 %type <str>		ExistingIndex
580 
581 %type <list>	constraints_set_list
582 %type <boolean> constraints_set_mode
583 %type <str>		OptTableSpace OptConsTableSpace
584 %type <rolespec> OptTableSpaceOwner
585 %type <ival>	opt_check_option
586 
587 %type <str>		opt_provider security_label
588 
589 %type <target>	xml_attribute_el
590 %type <list>	xml_attribute_list xml_attributes
591 %type <node>	xml_root_version opt_xml_root_standalone
592 %type <node>	xmlexists_argument
593 %type <ival>	document_or_content
594 %type <boolean> xml_whitespace_option
595 %type <list>	xmltable_column_list xmltable_column_option_list
596 %type <node>	xmltable_column_el
597 %type <defelt>	xmltable_column_option_el
598 %type <list>	xml_namespace_list
599 %type <target>	xml_namespace_el
600 
601 %type <node>	func_application func_expr_common_subexpr
602 %type <node>	func_expr func_expr_windowless
603 %type <node>	common_table_expr
604 %type <with>	with_clause opt_with_clause
605 %type <list>	cte_list
606 
607 %type <list>	within_group_clause
608 %type <node>	filter_clause
609 %type <list>	window_clause window_definition_list opt_partition_clause
610 %type <windef>	window_definition over_clause window_specification
611 				opt_frame_clause frame_extent frame_bound
612 %type <ival>	opt_window_exclusion_clause
613 %type <str>		opt_existing_window_name
614 %type <boolean> opt_if_not_exists
615 %type <ival>	generated_when override_kind
616 %type <partspec>	PartitionSpec OptPartitionSpec
617 %type <str>			part_strategy
618 %type <partelem>	part_elem
619 %type <list>		part_params
620 %type <partboundspec> PartitionBoundSpec
621 %type <node>		partbound_datum PartitionRangeDatum
622 %type <list>		hash_partbound partbound_datum_list range_datum_list
623 %type <defelt>		hash_partbound_elem
624 
625 /*
626  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
627  * They must be listed first so that their numeric codes do not depend on
628  * the set of keywords.  PL/pgSQL depends on this so that it can share the
629  * same lexer.  If you add/change tokens here, fix PL/pgSQL to match!
630  *
631  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
632  * parse errors.  It is needed by PL/pgSQL.
633  */
634 %token <str>	IDENT FCONST SCONST BCONST XCONST Op
635 %token <ival>	ICONST PARAM
636 %token			TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
637 %token			LESS_EQUALS GREATER_EQUALS NOT_EQUALS
638 
639 /*
640  * If you want to make any keyword changes, update the keyword table in
641  * src/include/parser/kwlist.h and add new keywords to the appropriate one
642  * of the reserved-or-not-so-reserved keyword lists, below; search
643  * this file for "Keyword category lists".
644  */
645 
646 /* ordinary key words in alphabetical order */
647 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
648 	AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
649 	ASSERTION ASSIGNMENT ASYMMETRIC AT ATTACH ATTRIBUTE AUTHORIZATION
650 
651 	BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
652 	BOOLEAN_P BOTH BY
653 
654 	CACHE CALL CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
655 	CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
656 	CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMENT COMMENTS COMMIT
657 	COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION CONSTRAINT
658 	CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE
659 	CROSS CSV CUBE CURRENT_P
660 	CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
661 	CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
662 
663 	DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
664 	DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DESC
665 	DETACH DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P
666 	DOUBLE_P DROP
667 
668 	EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EVENT EXCEPT
669 	EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
670 	EXTENSION EXTERNAL EXTRACT
671 
672 	FALSE_P FAMILY FETCH FILTER FIRST_P FLOAT_P FOLLOWING FOR
673 	FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
674 
675 	GENERATED GLOBAL GRANT GRANTED GREATEST GROUP_P GROUPING GROUPS
676 
677 	HANDLER HAVING HEADER_P HOLD HOUR_P
678 
679 	IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P INCLUDE
680 	INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
681 	INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
682 	INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
683 
684 	JOIN
685 
686 	KEY
687 
688 	LABEL LANGUAGE LARGE_P LAST_P LATERAL_P
689 	LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL
690 	LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED
691 
692 	MAPPING MATCH MATERIALIZED MAXVALUE METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE
693 
694 	NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NONE
695 	NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
696 	NULLS_P NUMERIC
697 
698 	OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
699 	ORDER ORDINALITY OTHERS OUT_P OUTER_P
700 	OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
701 
702 	PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PGPOOL PLACING PLANS POLICY
703 	POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
704 	PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROCEDURES PROGRAM PUBLICATION
705 
706 	QUOTE
707 
708 	RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REFERENCING
709 	REFRESH REINDEX RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
710 	RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP
711 	ROUTINE ROUTINES ROW ROWS RULE
712 
713 	SAVEPOINT SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
714 	SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
715 	SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P
716 	START STATEMENT STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P
717 	SUBSCRIPTION SUBSTRING SYMMETRIC SYSID SYSTEM_P
718 
719 	TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
720 	TIES TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM
721 	TREAT TRIGGER TRIM TRUE_P
722 	TRUNCATE TRUSTED TYPE_P TYPES_P
723 
724 	UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNLOGGED
725 	UNTIL UPDATE USER USING
726 
727 	VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
728 	VERBOSE VERSION_P VIEW VIEWS VOLATILE
729 
730 	WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE
731 
732 	XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLNAMESPACES
733 	XMLPARSE XMLPI XMLROOT XMLSERIALIZE XMLTABLE
734 
735 	YEAR_P YES_P
736 
737 	ZONE
738 
739 /*
740  * The grammar thinks these are keywords, but they are not in the kwlist.h
741  * list and so can never be entered directly.  The filter in parser.c
742  * creates these tokens when required (based on looking one token ahead).
743  *
744  * NOT_LA exists so that productions such as NOT LIKE can be given the same
745  * precedence as LIKE; otherwise they'd effectively have the same precedence
746  * as NOT, at least with respect to their left-hand subexpression.
747  * NULLS_LA and WITH_LA are needed to make the grammar LALR(1).
748  */
749 %token		NOT_LA NULLS_LA WITH_LA
750 
751 
752 /* Precedence: lowest to highest */
753 %nonassoc	SET				/* see relation_expr_opt_alias */
754 %left		UNION EXCEPT
755 %left		INTERSECT
756 %left		OR
757 %left		AND
758 %right		NOT
759 %nonassoc	IS ISNULL NOTNULL	/* IS sets precedence for IS NULL, etc */
760 %nonassoc	'<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
761 %nonassoc	BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA
762 %nonassoc	ESCAPE			/* ESCAPE must be just above LIKE/ILIKE/SIMILAR */
763 %left		POSTFIXOP		/* dummy for postfix Op rules */
764 /*
765  * To support target_el without AS, we must give IDENT an explicit priority
766  * between POSTFIXOP and Op.  We can safely assign the same priority to
767  * various unreserved keywords as needed to resolve ambiguities (this can't
768  * have any bad effects since obviously the keywords will still behave the
769  * same as if they weren't keywords).  We need to do this:
770  * for PARTITION, RANGE, ROWS, GROUPS to support opt_existing_window_name;
771  * for RANGE, ROWS, GROUPS so that they can follow a_expr without creating
772  * postfix-operator problems;
773  * for GENERATED so that it can follow b_expr;
774  * and for NULL so that it can follow b_expr in ColQualList without creating
775  * postfix-operator problems.
776  *
777  * To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
778  * an explicit priority lower than '(', so that a rule with CUBE '(' will shift
779  * rather than reducing a conflicting rule that takes CUBE as a function name.
780  * Using the same precedence as IDENT seems right for the reasons given above.
781  *
782  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
783  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
784  * there is no principled way to distinguish these from the productions
785  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
786  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
787  * appear to cause UNBOUNDED to be treated differently from other unreserved
788  * keywords anywhere else in the grammar, but it's definitely risky.  We can
789  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
790  */
791 %nonassoc	UNBOUNDED		/* ideally should have same precedence as IDENT */
792 %nonassoc	IDENT GENERATED NULL_P PARTITION RANGE ROWS GROUPS PRECEDING FOLLOWING CUBE ROLLUP
793 %left		Op OPERATOR		/* multi-character ops and user-defined operators */
794 %left		'+' '-'
795 %left		'*' '/' '%'
796 %left		'^'
797 /* Unary Operators */
798 %left		AT				/* sets precedence for AT TIME ZONE */
799 %left		COLLATE
800 %right		UMINUS
801 %left		'[' ']'
802 %left		'(' ')'
803 %left		TYPECAST
804 %left		'.'
805 /*
806  * These might seem to be low-precedence, but actually they are not part
807  * of the arithmetic hierarchy at all in their use as JOIN operators.
808  * We make them high-precedence to support their use as function names.
809  * They wouldn't be given a precedence at all, were it not that we need
810  * left-associativity among the JOIN rules themselves.
811  */
812 %left		JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
813 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
814 %right		PRESERVE STRIP_P
815 
816 %%
817 
818 /*
819  *	The target production for the whole parse.
820  */
821 stmtblock:	stmtmulti
822 			{
823 				pg_yyget_extra(yyscanner)->parsetree = $1;
824 			}
825 		;
826 
827 /*
828  * At top level, we wrap each stmt with a RawStmt node carrying start location
829  * and length of the stmt's text.  Notice that the start loc/len are driven
830  * entirely from semicolon locations (@2).  It would seem natural to use
831  * @1 or @3 to get the true start location of a stmt, but that doesn't work
832  * for statements that can start with empty nonterminals (opt_with_clause is
833  * the main offender here); as noted in the comments for YYLLOC_DEFAULT,
834  * we'd get -1 for the location in such cases.
835  * We also take care to discard empty statements entirely.
836  */
837 stmtmulti:	stmtmulti ';' stmt
838 				{
839 					if ($1 != NIL)
840 					{
841 						/* update length of previous stmt */
842 						updateRawStmtEnd(llast_node(RawStmt, $1), @2);
843 					}
844 					if ($3 != NULL)
845 						$$ = lappend($1, makeRawStmt($3, @2 + 1));
846 					else
847 						$$ = $1;
848 				}
849 			| stmt
850 				{
851 					if ($1 != NULL)
852 						$$ = list_make1(makeRawStmt($1, 0));
853 					else
854 						$$ = NIL;
855 				}
856 		;
857 
858 stmt :
859 			AlterEventTrigStmt
860 			| AlterCollationStmt
861 			| AlterDatabaseStmt
862 			| AlterDatabaseSetStmt
863 			| AlterDefaultPrivilegesStmt
864 			| AlterDomainStmt
865 			| AlterEnumStmt
866 			| AlterExtensionStmt
867 			| AlterExtensionContentsStmt
868 			| AlterFdwStmt
869 			| AlterForeignServerStmt
870 			| AlterForeignTableStmt
871 			| AlterFunctionStmt
872 			| AlterGroupStmt
873 			| AlterObjectDependsStmt
874 			| AlterObjectSchemaStmt
875 			| AlterOwnerStmt
876 			| AlterOperatorStmt
877 			| AlterPolicyStmt
878 			| AlterSeqStmt
879 			| AlterSystemStmt
880 			| AlterTableStmt
881 			| AlterTblSpcStmt
882 			| AlterCompositeTypeStmt
883 			| AlterPublicationStmt
884 			| AlterRoleSetStmt
885 			| AlterRoleStmt
886 			| AlterSubscriptionStmt
887 			| AlterTSConfigurationStmt
888 			| AlterTSDictionaryStmt
889 			| AlterUserMappingStmt
890 			| AnalyzeStmt
891 			| CallStmt
892 			| CheckPointStmt
893 			| ClosePortalStmt
894 			| ClusterStmt
895 			| CommentStmt
896 			| ConstraintsSetStmt
897 			| CopyStmt
898 			| CreateAmStmt
899 			| CreateAsStmt
900 			| CreateAssertStmt
901 			| CreateCastStmt
902 			| CreateConversionStmt
903 			| CreateDomainStmt
904 			| CreateExtensionStmt
905 			| CreateFdwStmt
906 			| CreateForeignServerStmt
907 			| CreateForeignTableStmt
908 			| CreateFunctionStmt
909 			| CreateGroupStmt
910 			| CreateMatViewStmt
911 			| CreateOpClassStmt
912 			| CreateOpFamilyStmt
913 			| CreatePublicationStmt
914 			| AlterOpFamilyStmt
915 			| CreatePolicyStmt
916 			| CreatePLangStmt
917 			| CreateSchemaStmt
918 			| CreateSeqStmt
919 			| CreateStmt
920 			| CreateSubscriptionStmt
921 			| CreateStatsStmt
922 			| CreateTableSpaceStmt
923 			| CreateTransformStmt
924 			| CreateTrigStmt
925 			| CreateEventTrigStmt
926 			| CreateRoleStmt
927 			| CreateUserStmt
928 			| CreateUserMappingStmt
929 			| CreatedbStmt
930 			| DeallocateStmt
931 			| DeclareCursorStmt
932 			| DefineStmt
933 			| DeleteStmt
934 			| DiscardStmt
935 			| DoStmt
936 			| DropAssertStmt
937 			| DropCastStmt
938 			| DropOpClassStmt
939 			| DropOpFamilyStmt
940 			| DropOwnedStmt
941 			| DropPLangStmt
942 			| DropStmt
943 			| DropSubscriptionStmt
944 			| DropTableSpaceStmt
945 			| DropTransformStmt
946 			| DropRoleStmt
947 			| DropUserMappingStmt
948 			| DropdbStmt
949 			| ExecuteStmt
950 			| ExplainStmt
951 			| FetchStmt
952 			| GrantStmt
953 			| GrantRoleStmt
954 			| ImportForeignSchemaStmt
955 			| IndexStmt
956 			| InsertStmt
957 			| ListenStmt
958 			| RefreshMatViewStmt
959 			| LoadStmt
960 			| LockStmt
961 			| NotifyStmt
962 			| PrepareStmt
963 			| ReassignOwnedStmt
964 			| ReindexStmt
965 			| RemoveAggrStmt
966 			| RemoveFuncStmt
967 			| RemoveOperStmt
968 			| RenameStmt
969 			| RevokeStmt
970 			| RevokeRoleStmt
971 			| RuleStmt
972 			| SecLabelStmt
973 			| SelectStmt
974 			| TransactionStmt
975 			| TruncateStmt
976 			| UnlistenStmt
977 			| UpdateStmt
978 			| VacuumStmt
979 			| VariableResetStmt
980 			| VariableSetStmt
981 			| VariableShowStmt
982 			| ViewStmt
983 			| /*EMPTY*/
984 				{ $$ = NULL; }
985 		;
986 
987 /*****************************************************************************
988  *
989  * CALL statement
990  *
991  *****************************************************************************/
992 
993 CallStmt:	CALL func_application
994 				{
995 					CallStmt *n = makeNode(CallStmt);
996 					n->funccall = castNode(FuncCall, $2);
997 					$$ = (Node *)n;
998 				}
999 		;
1000 
1001 /*****************************************************************************
1002  *
1003  * Create a new Postgres DBMS role
1004  *
1005  *****************************************************************************/
1006 
1007 CreateRoleStmt:
1008 			CREATE ROLE RoleId opt_with OptRoleList
1009 				{
1010 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1011 					n->stmt_type = ROLESTMT_ROLE;
1012 					n->role = $3;
1013 					n->options = $5;
1014 					$$ = (Node *)n;
1015 				}
1016 		;
1017 
1018 
1019 opt_with:	WITH									{}
1020 			| WITH_LA								{}
1021 			| /*EMPTY*/								{}
1022 		;
1023 
1024 /*
1025  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
1026  * for backwards compatibility).  Note: the only option required by SQL99
1027  * is "WITH ADMIN name".
1028  */
1029 OptRoleList:
1030 			OptRoleList CreateOptRoleElem			{ $$ = lappend($1, $2); }
1031 			| /* EMPTY */							{ $$ = NIL; }
1032 		;
1033 
1034 AlterOptRoleList:
1035 			AlterOptRoleList AlterOptRoleElem		{ $$ = lappend($1, $2); }
1036 			| /* EMPTY */							{ $$ = NIL; }
1037 		;
1038 
1039 AlterOptRoleElem:
1040 			PASSWORD Sconst
1041 				{
1042 					$$ = makeDefElem("password",
1043 									 (Node *)makeString($2), @1);
1044 				}
1045 			| PASSWORD NULL_P
1046 				{
1047 					$$ = makeDefElem("password", NULL, @1);
1048 				}
1049 			| ENCRYPTED PASSWORD Sconst
1050 				{
1051 					/*
1052 					 * These days, passwords are always stored in encrypted
1053 					 * form, so there is no difference between PASSWORD and
1054 					 * ENCRYPTED PASSWORD.
1055 					 */
1056 					$$ = makeDefElem("password",
1057 									 (Node *)makeString($3), @1);
1058 				}
1059 			| UNENCRYPTED PASSWORD Sconst
1060 				{
1061 					ereport(ERROR,
1062 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1063 							 errmsg("UNENCRYPTED PASSWORD is no longer supported"),
1064 							 errhint("Remove UNENCRYPTED to store the password in encrypted form instead."),
1065 							 parser_errposition(@1)));
1066 				}
1067 			| INHERIT
1068 				{
1069 					$$ = makeDefElem("inherit", (Node *)makeInteger(true), @1);
1070 				}
1071 			| CONNECTION LIMIT SignedIconst
1072 				{
1073 					$$ = makeDefElem("connectionlimit", (Node *)makeInteger($3), @1);
1074 				}
1075 			| VALID UNTIL Sconst
1076 				{
1077 					$$ = makeDefElem("validUntil", (Node *)makeString($3), @1);
1078 				}
1079 		/*	Supported but not documented for roles, for use by ALTER GROUP. */
1080 			| USER role_list
1081 				{
1082 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1083 				}
1084 			| IDENT
1085 				{
1086 					/*
1087 					 * We handle identifiers that aren't parser keywords with
1088 					 * the following special-case codes, to avoid bloating the
1089 					 * size of the main parser.
1090 					 */
1091 					if (strcmp($1, "superuser") == 0)
1092 						$$ = makeDefElem("superuser", (Node *)makeInteger(true), @1);
1093 					else if (strcmp($1, "nosuperuser") == 0)
1094 						$$ = makeDefElem("superuser", (Node *)makeInteger(false), @1);
1095 					else if (strcmp($1, "createrole") == 0)
1096 						$$ = makeDefElem("createrole", (Node *)makeInteger(true), @1);
1097 					else if (strcmp($1, "nocreaterole") == 0)
1098 						$$ = makeDefElem("createrole", (Node *)makeInteger(false), @1);
1099 					else if (strcmp($1, "replication") == 0)
1100 						$$ = makeDefElem("isreplication", (Node *)makeInteger(true), @1);
1101 					else if (strcmp($1, "noreplication") == 0)
1102 						$$ = makeDefElem("isreplication", (Node *)makeInteger(false), @1);
1103 					else if (strcmp($1, "createdb") == 0)
1104 						$$ = makeDefElem("createdb", (Node *)makeInteger(true), @1);
1105 					else if (strcmp($1, "nocreatedb") == 0)
1106 						$$ = makeDefElem("createdb", (Node *)makeInteger(false), @1);
1107 					else if (strcmp($1, "login") == 0)
1108 						$$ = makeDefElem("canlogin", (Node *)makeInteger(true), @1);
1109 					else if (strcmp($1, "nologin") == 0)
1110 						$$ = makeDefElem("canlogin", (Node *)makeInteger(false), @1);
1111 					else if (strcmp($1, "bypassrls") == 0)
1112 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(true), @1);
1113 					else if (strcmp($1, "nobypassrls") == 0)
1114 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(false), @1);
1115 					else if (strcmp($1, "noinherit") == 0)
1116 					{
1117 						/*
1118 						 * Note that INHERIT is a keyword, so it's handled by main parser, but
1119 						 * NOINHERIT is handled here.
1120 						 */
1121 						$$ = makeDefElem("inherit", (Node *)makeInteger(false), @1);
1122 					}
1123 					else
1124 						ereport(ERROR,
1125 								(errcode(ERRCODE_SYNTAX_ERROR),
1126 								 errmsg("unrecognized role option \"%s\"", $1),
1127 									 parser_errposition(@1)));
1128 				}
1129 		;
1130 
1131 CreateOptRoleElem:
1132 			AlterOptRoleElem			{ $$ = $1; }
1133 			/* The following are not supported by ALTER ROLE/USER/GROUP */
1134 			| SYSID Iconst
1135 				{
1136 					$$ = makeDefElem("sysid", (Node *)makeInteger($2), @1);
1137 				}
1138 			| ADMIN role_list
1139 				{
1140 					$$ = makeDefElem("adminmembers", (Node *)$2, @1);
1141 				}
1142 			| ROLE role_list
1143 				{
1144 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1145 				}
1146 			| IN_P ROLE role_list
1147 				{
1148 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1149 				}
1150 			| IN_P GROUP_P role_list
1151 				{
1152 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1153 				}
1154 		;
1155 
1156 
1157 /*****************************************************************************
1158  *
1159  * Create a new Postgres DBMS user (role with implied login ability)
1160  *
1161  *****************************************************************************/
1162 
1163 CreateUserStmt:
1164 			CREATE USER RoleId opt_with OptRoleList
1165 				{
1166 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1167 					n->stmt_type = ROLESTMT_USER;
1168 					n->role = $3;
1169 					n->options = $5;
1170 					$$ = (Node *)n;
1171 				}
1172 		;
1173 
1174 
1175 /*****************************************************************************
1176  *
1177  * Alter a postgresql DBMS role
1178  *
1179  *****************************************************************************/
1180 
1181 AlterRoleStmt:
1182 			ALTER ROLE RoleSpec opt_with AlterOptRoleList
1183 				 {
1184 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1185 					n->role = $3;
1186 					n->action = +1;	/* add, if there are members */
1187 					n->options = $5;
1188 					$$ = (Node *)n;
1189 				 }
1190 			| ALTER USER RoleSpec opt_with AlterOptRoleList
1191 				 {
1192 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1193 					n->role = $3;
1194 					n->action = +1;	/* add, if there are members */
1195 					n->options = $5;
1196 					$$ = (Node *)n;
1197 				 }
1198 		;
1199 
1200 opt_in_database:
1201 			   /* EMPTY */					{ $$ = NULL; }
1202 			| IN_P DATABASE database_name	{ $$ = $3; }
1203 		;
1204 
1205 AlterRoleSetStmt:
1206 			ALTER ROLE RoleSpec opt_in_database SetResetClause
1207 				{
1208 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1209 					n->role = $3;
1210 					n->database = $4;
1211 					n->setstmt = $5;
1212 					$$ = (Node *)n;
1213 				}
1214 			| ALTER ROLE ALL opt_in_database SetResetClause
1215 				{
1216 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1217 					n->role = NULL;
1218 					n->database = $4;
1219 					n->setstmt = $5;
1220 					$$ = (Node *)n;
1221 				}
1222 			| ALTER USER RoleSpec opt_in_database SetResetClause
1223 				{
1224 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1225 					n->role = $3;
1226 					n->database = $4;
1227 					n->setstmt = $5;
1228 					$$ = (Node *)n;
1229 				}
1230 			| ALTER USER ALL opt_in_database SetResetClause
1231 				{
1232 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1233 					n->role = NULL;
1234 					n->database = $4;
1235 					n->setstmt = $5;
1236 					$$ = (Node *)n;
1237 				}
1238 		;
1239 
1240 
1241 /*****************************************************************************
1242  *
1243  * Drop a postgresql DBMS role
1244  *
1245  * XXX Ideally this would have CASCADE/RESTRICT options, but a role
1246  * might own objects in multiple databases, and there is presently no way to
1247  * implement cascading to other databases.  So we always behave as RESTRICT.
1248  *****************************************************************************/
1249 
1250 DropRoleStmt:
1251 			DROP ROLE role_list
1252 				{
1253 					DropRoleStmt *n = makeNode(DropRoleStmt);
1254 					n->missing_ok = false;
1255 					n->roles = $3;
1256 					$$ = (Node *)n;
1257 				}
1258 			| DROP ROLE IF_P EXISTS role_list
1259 				{
1260 					DropRoleStmt *n = makeNode(DropRoleStmt);
1261 					n->missing_ok = true;
1262 					n->roles = $5;
1263 					$$ = (Node *)n;
1264 				}
1265 			| DROP USER role_list
1266 				{
1267 					DropRoleStmt *n = makeNode(DropRoleStmt);
1268 					n->missing_ok = false;
1269 					n->roles = $3;
1270 					$$ = (Node *)n;
1271 				}
1272 			| DROP USER IF_P EXISTS role_list
1273 				{
1274 					DropRoleStmt *n = makeNode(DropRoleStmt);
1275 					n->roles = $5;
1276 					n->missing_ok = true;
1277 					$$ = (Node *)n;
1278 				}
1279 			| DROP GROUP_P role_list
1280 				{
1281 					DropRoleStmt *n = makeNode(DropRoleStmt);
1282 					n->missing_ok = false;
1283 					n->roles = $3;
1284 					$$ = (Node *)n;
1285 				}
1286 			| DROP GROUP_P IF_P EXISTS role_list
1287 				{
1288 					DropRoleStmt *n = makeNode(DropRoleStmt);
1289 					n->missing_ok = true;
1290 					n->roles = $5;
1291 					$$ = (Node *)n;
1292 				}
1293 			;
1294 
1295 
1296 /*****************************************************************************
1297  *
1298  * Create a postgresql group (role without login ability)
1299  *
1300  *****************************************************************************/
1301 
1302 CreateGroupStmt:
1303 			CREATE GROUP_P RoleId opt_with OptRoleList
1304 				{
1305 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1306 					n->stmt_type = ROLESTMT_GROUP;
1307 					n->role = $3;
1308 					n->options = $5;
1309 					$$ = (Node *)n;
1310 				}
1311 		;
1312 
1313 
1314 /*****************************************************************************
1315  *
1316  * Alter a postgresql group
1317  *
1318  *****************************************************************************/
1319 
1320 AlterGroupStmt:
1321 			ALTER GROUP_P RoleSpec add_drop USER role_list
1322 				{
1323 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1324 					n->role = $3;
1325 					n->action = $4;
1326 					n->options = list_make1(makeDefElem("rolemembers",
1327 														(Node *)$6, @6));
1328 					$$ = (Node *)n;
1329 				}
1330 		;
1331 
1332 add_drop:	ADD_P									{ $$ = +1; }
1333 			| DROP									{ $$ = -1; }
1334 		;
1335 
1336 
1337 /*****************************************************************************
1338  *
1339  * Manipulate a schema
1340  *
1341  *****************************************************************************/
1342 
1343 CreateSchemaStmt:
1344 			CREATE SCHEMA OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1345 				{
1346 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1347 					/* One can omit the schema name or the authorization id. */
1348 					n->schemaname = $3;
1349 					n->authrole = $5;
1350 					n->schemaElts = $6;
1351 					n->if_not_exists = false;
1352 					$$ = (Node *)n;
1353 				}
1354 			| CREATE SCHEMA ColId OptSchemaEltList
1355 				{
1356 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1357 					/* ...but not both */
1358 					n->schemaname = $3;
1359 					n->authrole = NULL;
1360 					n->schemaElts = $4;
1361 					n->if_not_exists = false;
1362 					$$ = (Node *)n;
1363 				}
1364 			| CREATE SCHEMA IF_P NOT EXISTS OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1365 				{
1366 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1367 					/* schema name can be omitted here, too */
1368 					n->schemaname = $6;
1369 					n->authrole = $8;
1370 					if ($9 != NIL)
1371 						ereport(ERROR,
1372 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1373 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1374 								 parser_errposition(@9)));
1375 					n->schemaElts = $9;
1376 					n->if_not_exists = true;
1377 					$$ = (Node *)n;
1378 				}
1379 			| CREATE SCHEMA IF_P NOT EXISTS ColId OptSchemaEltList
1380 				{
1381 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1382 					/* ...but not here */
1383 					n->schemaname = $6;
1384 					n->authrole = NULL;
1385 					if ($7 != NIL)
1386 						ereport(ERROR,
1387 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1388 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1389 								 parser_errposition(@7)));
1390 					n->schemaElts = $7;
1391 					n->if_not_exists = true;
1392 					$$ = (Node *)n;
1393 				}
1394 		;
1395 
1396 OptSchemaName:
1397 			ColId									{ $$ = $1; }
1398 			| /* EMPTY */							{ $$ = NULL; }
1399 		;
1400 
1401 OptSchemaEltList:
1402 			OptSchemaEltList schema_stmt
1403 				{
1404 					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
1405 						@$ = @2;
1406 					$$ = lappend($1, $2);
1407 				}
1408 			| /* EMPTY */
1409 				{ $$ = NIL; }
1410 		;
1411 
1412 /*
1413  *	schema_stmt are the ones that can show up inside a CREATE SCHEMA
1414  *	statement (in addition to by themselves).
1415  */
1416 schema_stmt:
1417 			CreateStmt
1418 			| IndexStmt
1419 			| CreateSeqStmt
1420 			| CreateTrigStmt
1421 			| GrantStmt
1422 			| ViewStmt
1423 		;
1424 
1425 
1426 /*****************************************************************************
1427  *
1428  * Set PG internal variable
1429  *	  SET name TO 'var_value'
1430  * Include SQL syntax (thomas 1997-10-22):
1431  *	  SET TIME ZONE 'var_value'
1432  *
1433  *****************************************************************************/
1434 
1435 VariableSetStmt:
1436 			PGPOOL SET generic_set
1437 				{
1438 					VariableSetStmt *n = $3;
1439 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep changes minumum */
1440 					n->is_local = false;
1441 					$$ = (Node *) n;
1442 				}
1443 			| SET set_rest
1444 				{
1445 					VariableSetStmt *n = $2;
1446 					n->is_local = false;
1447 					$$ = (Node *) n;
1448 				}
1449 			| SET LOCAL set_rest
1450 				{
1451 					VariableSetStmt *n = $3;
1452 					n->is_local = true;
1453 					$$ = (Node *) n;
1454 				}
1455 			| SET SESSION set_rest
1456 				{
1457 					VariableSetStmt *n = $3;
1458 					n->is_local = false;
1459 					$$ = (Node *) n;
1460 				}
1461 		;
1462 
1463 set_rest:
1464 			TRANSACTION transaction_mode_list
1465 				{
1466 					VariableSetStmt *n = makeNode(VariableSetStmt);
1467 					n->kind = VAR_SET_MULTI;
1468 					n->name = "TRANSACTION";
1469 					n->args = $2;
1470 					$$ = n;
1471 				}
1472 			| SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1473 				{
1474 					VariableSetStmt *n = makeNode(VariableSetStmt);
1475 					n->kind = VAR_SET_MULTI;
1476 					n->name = "SESSION CHARACTERISTICS";
1477 					n->args = $5;
1478 					$$ = n;
1479 				}
1480 			| set_rest_more
1481 			;
1482 
1483 generic_set:
1484 			var_name TO var_list
1485 				{
1486 					VariableSetStmt *n = makeNode(VariableSetStmt);
1487 					n->kind = VAR_SET_VALUE;
1488 					n->name = $1;
1489 					n->args = $3;
1490 					$$ = n;
1491 				}
1492 			| var_name '=' var_list
1493 				{
1494 					VariableSetStmt *n = makeNode(VariableSetStmt);
1495 					n->kind = VAR_SET_VALUE;
1496 					n->name = $1;
1497 					n->args = $3;
1498 					$$ = n;
1499 				}
1500 			| var_name TO DEFAULT
1501 				{
1502 					VariableSetStmt *n = makeNode(VariableSetStmt);
1503 					n->kind = VAR_SET_DEFAULT;
1504 					n->name = $1;
1505 					$$ = n;
1506 				}
1507 			| var_name '=' DEFAULT
1508 				{
1509 					VariableSetStmt *n = makeNode(VariableSetStmt);
1510 					n->kind = VAR_SET_DEFAULT;
1511 					n->name = $1;
1512 					$$ = n;
1513 				}
1514 
1515 set_rest_more:	/* Generic SET syntaxes: */
1516 			generic_set 						{$$ = $1;}
1517 			| var_name FROM CURRENT_P
1518 				{
1519 					VariableSetStmt *n = makeNode(VariableSetStmt);
1520 					n->kind = VAR_SET_CURRENT;
1521 					n->name = $1;
1522 					$$ = n;
1523 				}
1524 			/* Special syntaxes mandated by SQL standard: */
1525 			| TIME ZONE zone_value
1526 				{
1527 					VariableSetStmt *n = makeNode(VariableSetStmt);
1528 					n->kind = VAR_SET_VALUE;
1529 					n->name = "timezone";
1530 					if ($3 != NULL)
1531 						n->args = list_make1($3);
1532 					else
1533 						n->kind = VAR_SET_DEFAULT;
1534 					$$ = n;
1535 				}
1536 			| CATALOG_P Sconst
1537 				{
1538 					ereport(ERROR,
1539 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1540 							 errmsg("current database cannot be changed"),
1541 							 parser_errposition(@2)));
1542 					$$ = NULL; /*not reached*/
1543 				}
1544 			| SCHEMA Sconst
1545 				{
1546 					VariableSetStmt *n = makeNode(VariableSetStmt);
1547 					n->kind = VAR_SET_VALUE;
1548 					n->name = "search_path";
1549 					n->args = list_make1(makeStringConst($2, @2));
1550 					$$ = n;
1551 				}
1552 			| NAMES opt_encoding
1553 				{
1554 					VariableSetStmt *n = makeNode(VariableSetStmt);
1555 					n->kind = VAR_SET_VALUE;
1556 					n->name = "client_encoding";
1557 					if ($2 != NULL)
1558 						n->args = list_make1(makeStringConst($2, @2));
1559 					else
1560 						n->kind = VAR_SET_DEFAULT;
1561 					$$ = n;
1562 				}
1563 			| ROLE NonReservedWord_or_Sconst
1564 				{
1565 					VariableSetStmt *n = makeNode(VariableSetStmt);
1566 					n->kind = VAR_SET_VALUE;
1567 					n->name = "role";
1568 					n->args = list_make1(makeStringConst($2, @2));
1569 					$$ = n;
1570 				}
1571 			| SESSION AUTHORIZATION NonReservedWord_or_Sconst
1572 				{
1573 					VariableSetStmt *n = makeNode(VariableSetStmt);
1574 					n->kind = VAR_SET_VALUE;
1575 					n->name = "session_authorization";
1576 					n->args = list_make1(makeStringConst($3, @3));
1577 					$$ = n;
1578 				}
1579 			| SESSION AUTHORIZATION DEFAULT
1580 				{
1581 					VariableSetStmt *n = makeNode(VariableSetStmt);
1582 					n->kind = VAR_SET_DEFAULT;
1583 					n->name = "session_authorization";
1584 					$$ = n;
1585 				}
1586 			| XML_P OPTION document_or_content
1587 				{
1588 					VariableSetStmt *n = makeNode(VariableSetStmt);
1589 					n->kind = VAR_SET_VALUE;
1590 					n->name = "xmloption";
1591 					n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1592 					$$ = n;
1593 				}
1594 			/* Special syntaxes invented by PostgreSQL: */
1595 			| TRANSACTION SNAPSHOT Sconst
1596 				{
1597 					VariableSetStmt *n = makeNode(VariableSetStmt);
1598 					n->kind = VAR_SET_MULTI;
1599 					n->name = "TRANSACTION SNAPSHOT";
1600 					n->args = list_make1(makeStringConst($3, @3));
1601 					$$ = n;
1602 				}
1603 		;
1604 
1605 var_name:	ColId								{ $$ = $1; }
1606 			| var_name '.' ColId
1607 				{ $$ = psprintf("%s.%s", $1, $3); }
1608 		;
1609 
1610 var_list:	var_value								{ $$ = list_make1($1); }
1611 			| var_list ',' var_value				{ $$ = lappend($1, $3); }
1612 		;
1613 
1614 var_value:	opt_boolean_or_string
1615 				{ $$ = makeStringConst($1, @1); }
1616 			| NumericOnly
1617 				{ $$ = makeAConst($1, @1); }
1618 		;
1619 
1620 iso_level:	READ UNCOMMITTED						{ $$ = "read uncommitted"; }
1621 			| READ COMMITTED						{ $$ = "read committed"; }
1622 			| REPEATABLE READ						{ $$ = "repeatable read"; }
1623 			| SERIALIZABLE							{ $$ = "serializable"; }
1624 		;
1625 
1626 opt_boolean_or_string:
1627 			TRUE_P									{ $$ = "true"; }
1628 			| FALSE_P								{ $$ = "false"; }
1629 			| ON									{ $$ = "on"; }
1630 			/*
1631 			 * OFF is also accepted as a boolean value, but is handled by
1632 			 * the NonReservedWord rule.  The action for booleans and strings
1633 			 * is the same, so we don't need to distinguish them here.
1634 			 */
1635 			| NonReservedWord_or_Sconst				{ $$ = $1; }
1636 		;
1637 
1638 /* Timezone values can be:
1639  * - a string such as 'pst8pdt'
1640  * - an identifier such as "pst8pdt"
1641  * - an integer or floating point number
1642  * - a time interval per SQL99
1643  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1644  * so use IDENT (meaning we reject anything that is a key word).
1645  */
1646 zone_value:
1647 			Sconst
1648 				{
1649 					$$ = makeStringConst($1, @1);
1650 				}
1651 			| IDENT
1652 				{
1653 					$$ = makeStringConst($1, @1);
1654 				}
1655 			| ConstInterval Sconst opt_interval
1656 				{
1657 					TypeName *t = $1;
1658 					if ($3 != NIL)
1659 					{
1660 						A_Const *n = (A_Const *) linitial($3);
1661 						if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1662 							ereport(ERROR,
1663 									(errcode(ERRCODE_SYNTAX_ERROR),
1664 									 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1665 									 parser_errposition(@3)));
1666 					}
1667 					t->typmods = $3;
1668 					$$ = makeStringConstCast($2, @2, t);
1669 				}
1670 			| ConstInterval '(' Iconst ')' Sconst
1671 				{
1672 					TypeName *t = $1;
1673 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1674 											makeIntConst($3, @3));
1675 					$$ = makeStringConstCast($5, @5, t);
1676 				}
1677 			| NumericOnly							{ $$ = makeAConst($1, @1); }
1678 			| DEFAULT								{ $$ = NULL; }
1679 			| LOCAL									{ $$ = NULL; }
1680 		;
1681 
1682 opt_encoding:
1683 			Sconst									{ $$ = $1; }
1684 			| DEFAULT								{ $$ = NULL; }
1685 			| /*EMPTY*/								{ $$ = NULL; }
1686 		;
1687 
1688 NonReservedWord_or_Sconst:
1689 			NonReservedWord							{ $$ = $1; }
1690 			| Sconst								{ $$ = $1; }
1691 		;
1692 
1693 VariableResetStmt:
1694 			RESET reset_rest						{ $$ = (Node *) $2; }
1695 			| PGPOOL RESET generic_reset
1696 				{
1697 					VariableSetStmt *n = $3;
1698 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep the changes minumum */
1699 					$$ = (Node *) n;
1700 				}
1701 		;
1702 
1703 reset_rest:
1704 			generic_reset							{ $$ = $1; }
1705 			| TIME ZONE
1706 				{
1707 					VariableSetStmt *n = makeNode(VariableSetStmt);
1708 					n->kind = VAR_RESET;
1709 					n->name = "timezone";
1710 					$$ = n;
1711 				}
1712 			| TRANSACTION ISOLATION LEVEL
1713 				{
1714 					VariableSetStmt *n = makeNode(VariableSetStmt);
1715 					n->kind = VAR_RESET;
1716 					n->name = "transaction_isolation";
1717 					$$ = n;
1718 				}
1719 			| SESSION AUTHORIZATION
1720 				{
1721 					VariableSetStmt *n = makeNode(VariableSetStmt);
1722 					n->kind = VAR_RESET;
1723 					n->name = "session_authorization";
1724 					$$ = n;
1725 				}
1726 		;
1727 
1728 generic_reset:
1729 			var_name
1730 				{
1731 					VariableSetStmt *n = makeNode(VariableSetStmt);
1732 					n->kind = VAR_RESET;
1733 					n->name = $1;
1734 					$$ = n;
1735 				}
1736 			| ALL
1737 				{
1738 					VariableSetStmt *n = makeNode(VariableSetStmt);
1739 					n->kind = VAR_RESET_ALL;
1740 					$$ = n;
1741 				}
1742 		;
1743 
1744 /* SetResetClause allows SET or RESET without LOCAL */
1745 SetResetClause:
1746 			SET set_rest					{ $$ = $2; }
1747 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1748 		;
1749 
1750 /* SetResetClause allows SET or RESET without LOCAL */
1751 FunctionSetResetClause:
1752 			SET set_rest_more				{ $$ = $2; }
1753 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1754 		;
1755 
1756 
1757 VariableShowStmt:
1758 			/* pgpool extension */
1759 			PGPOOL SHOW var_name
1760 			{
1761 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1762 				n->name = $3;
1763 				$$ = (Node *) n;
1764 			}
1765 			| PGPOOL SHOW ALL
1766 			{
1767 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1768 				n->name = "all";
1769 				$$ = (Node *) n;
1770 			}
1771 			| SHOW var_name
1772 				{
1773 					VariableShowStmt *n = makeNode(VariableShowStmt);
1774 					n->name = $2;
1775 					$$ = (Node *) n;
1776 				}
1777 			| SHOW TIME ZONE
1778 				{
1779 					VariableShowStmt *n = makeNode(VariableShowStmt);
1780 					n->name = "timezone";
1781 					$$ = (Node *) n;
1782 				}
1783 			| SHOW TRANSACTION ISOLATION LEVEL
1784 				{
1785 					VariableShowStmt *n = makeNode(VariableShowStmt);
1786 					n->name = "transaction_isolation";
1787 					$$ = (Node *) n;
1788 				}
1789 			| SHOW SESSION AUTHORIZATION
1790 				{
1791 					VariableShowStmt *n = makeNode(VariableShowStmt);
1792 					n->name = "session_authorization";
1793 					$$ = (Node *) n;
1794 				}
1795 			| SHOW ALL
1796 				{
1797 					VariableShowStmt *n = makeNode(VariableShowStmt);
1798 					n->name = "all";
1799 					$$ = (Node *) n;
1800 				}
1801 		;
1802 
1803 
1804 ConstraintsSetStmt:
1805 			SET CONSTRAINTS constraints_set_list constraints_set_mode
1806 				{
1807 					ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1808 					n->constraints = $3;
1809 					n->deferred = $4;
1810 					$$ = (Node *) n;
1811 				}
1812 		;
1813 
1814 constraints_set_list:
1815 			ALL										{ $$ = NIL; }
1816 			| qualified_name_list					{ $$ = $1; }
1817 		;
1818 
1819 constraints_set_mode:
1820 			DEFERRED								{ $$ = true; }
1821 			| IMMEDIATE								{ $$ = false; }
1822 		;
1823 
1824 
1825 /*
1826  * Checkpoint statement
1827  */
1828 CheckPointStmt:
1829 			CHECKPOINT
1830 				{
1831 					CheckPointStmt *n = makeNode(CheckPointStmt);
1832 					$$ = (Node *)n;
1833 				}
1834 		;
1835 
1836 
1837 /*****************************************************************************
1838  *
1839  * DISCARD { ALL | TEMP | PLANS | SEQUENCES }
1840  *
1841  *****************************************************************************/
1842 
1843 DiscardStmt:
1844 			DISCARD ALL
1845 				{
1846 					DiscardStmt *n = makeNode(DiscardStmt);
1847 					n->target = DISCARD_ALL;
1848 					$$ = (Node *) n;
1849 				}
1850 			| DISCARD TEMP
1851 				{
1852 					DiscardStmt *n = makeNode(DiscardStmt);
1853 					n->target = DISCARD_TEMP;
1854 					$$ = (Node *) n;
1855 				}
1856 			| DISCARD TEMPORARY
1857 				{
1858 					DiscardStmt *n = makeNode(DiscardStmt);
1859 					n->target = DISCARD_TEMP;
1860 					$$ = (Node *) n;
1861 				}
1862 			| DISCARD PLANS
1863 				{
1864 					DiscardStmt *n = makeNode(DiscardStmt);
1865 					n->target = DISCARD_PLANS;
1866 					$$ = (Node *) n;
1867 				}
1868 			| DISCARD SEQUENCES
1869 				{
1870 					DiscardStmt *n = makeNode(DiscardStmt);
1871 					n->target = DISCARD_SEQUENCES;
1872 					$$ = (Node *) n;
1873 				}
1874 
1875 		;
1876 
1877 
1878 /*****************************************************************************
1879  *
1880  *	ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW ] variations
1881  *
1882  * Note: we accept all subcommands for each of the five variants, and sort
1883  * out what's really legal at execution time.
1884  *****************************************************************************/
1885 
1886 AlterTableStmt:
1887 			ALTER TABLE relation_expr alter_table_cmds
1888 				{
1889 					AlterTableStmt *n = makeNode(AlterTableStmt);
1890 					n->relation = $3;
1891 					n->cmds = $4;
1892 					n->relkind = OBJECT_TABLE;
1893 					n->missing_ok = false;
1894 					$$ = (Node *)n;
1895 				}
1896 		|	ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
1897 				{
1898 					AlterTableStmt *n = makeNode(AlterTableStmt);
1899 					n->relation = $5;
1900 					n->cmds = $6;
1901 					n->relkind = OBJECT_TABLE;
1902 					n->missing_ok = true;
1903 					$$ = (Node *)n;
1904 				}
1905 		|	ALTER TABLE relation_expr partition_cmd
1906 				{
1907 					AlterTableStmt *n = makeNode(AlterTableStmt);
1908 					n->relation = $3;
1909 					n->cmds = list_make1($4);
1910 					n->relkind = OBJECT_TABLE;
1911 					n->missing_ok = false;
1912 					$$ = (Node *)n;
1913 				}
1914 		|	ALTER TABLE IF_P EXISTS relation_expr partition_cmd
1915 				{
1916 					AlterTableStmt *n = makeNode(AlterTableStmt);
1917 					n->relation = $5;
1918 					n->cmds = list_make1($6);
1919 					n->relkind = OBJECT_TABLE;
1920 					n->missing_ok = true;
1921 					$$ = (Node *)n;
1922 				}
1923 		|	ALTER TABLE ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1924 				{
1925 					AlterTableMoveAllStmt *n =
1926 						makeNode(AlterTableMoveAllStmt);
1927 					n->orig_tablespacename = $6;
1928 					n->objtype = OBJECT_TABLE;
1929 					n->roles = NIL;
1930 					n->new_tablespacename = $9;
1931 					n->nowait = $10;
1932 					$$ = (Node *)n;
1933 				}
1934 		|	ALTER TABLE ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1935 				{
1936 					AlterTableMoveAllStmt *n =
1937 						makeNode(AlterTableMoveAllStmt);
1938 					n->orig_tablespacename = $6;
1939 					n->objtype = OBJECT_TABLE;
1940 					n->roles = $9;
1941 					n->new_tablespacename = $12;
1942 					n->nowait = $13;
1943 					$$ = (Node *)n;
1944 				}
1945 		|	ALTER INDEX qualified_name alter_table_cmds
1946 				{
1947 					AlterTableStmt *n = makeNode(AlterTableStmt);
1948 					n->relation = $3;
1949 					n->cmds = $4;
1950 					n->relkind = OBJECT_INDEX;
1951 					n->missing_ok = false;
1952 					$$ = (Node *)n;
1953 				}
1954 		|	ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
1955 				{
1956 					AlterTableStmt *n = makeNode(AlterTableStmt);
1957 					n->relation = $5;
1958 					n->cmds = $6;
1959 					n->relkind = OBJECT_INDEX;
1960 					n->missing_ok = true;
1961 					$$ = (Node *)n;
1962 				}
1963 		|	ALTER INDEX qualified_name index_partition_cmd
1964 				{
1965 					AlterTableStmt *n = makeNode(AlterTableStmt);
1966 					n->relation = $3;
1967 					n->cmds = list_make1($4);
1968 					n->relkind = OBJECT_INDEX;
1969 					n->missing_ok = false;
1970 					$$ = (Node *)n;
1971 				}
1972 		|	ALTER INDEX ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1973 				{
1974 					AlterTableMoveAllStmt *n =
1975 						makeNode(AlterTableMoveAllStmt);
1976 					n->orig_tablespacename = $6;
1977 					n->objtype = OBJECT_INDEX;
1978 					n->roles = NIL;
1979 					n->new_tablespacename = $9;
1980 					n->nowait = $10;
1981 					$$ = (Node *)n;
1982 				}
1983 		|	ALTER INDEX ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1984 				{
1985 					AlterTableMoveAllStmt *n =
1986 						makeNode(AlterTableMoveAllStmt);
1987 					n->orig_tablespacename = $6;
1988 					n->objtype = OBJECT_INDEX;
1989 					n->roles = $9;
1990 					n->new_tablespacename = $12;
1991 					n->nowait = $13;
1992 					$$ = (Node *)n;
1993 				}
1994 		|	ALTER SEQUENCE qualified_name alter_table_cmds
1995 				{
1996 					AlterTableStmt *n = makeNode(AlterTableStmt);
1997 					n->relation = $3;
1998 					n->cmds = $4;
1999 					n->relkind = OBJECT_SEQUENCE;
2000 					n->missing_ok = false;
2001 					$$ = (Node *)n;
2002 				}
2003 		|	ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
2004 				{
2005 					AlterTableStmt *n = makeNode(AlterTableStmt);
2006 					n->relation = $5;
2007 					n->cmds = $6;
2008 					n->relkind = OBJECT_SEQUENCE;
2009 					n->missing_ok = true;
2010 					$$ = (Node *)n;
2011 				}
2012 		|	ALTER VIEW qualified_name alter_table_cmds
2013 				{
2014 					AlterTableStmt *n = makeNode(AlterTableStmt);
2015 					n->relation = $3;
2016 					n->cmds = $4;
2017 					n->relkind = OBJECT_VIEW;
2018 					n->missing_ok = false;
2019 					$$ = (Node *)n;
2020 				}
2021 		|	ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
2022 				{
2023 					AlterTableStmt *n = makeNode(AlterTableStmt);
2024 					n->relation = $5;
2025 					n->cmds = $6;
2026 					n->relkind = OBJECT_VIEW;
2027 					n->missing_ok = true;
2028 					$$ = (Node *)n;
2029 				}
2030 		|	ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
2031 				{
2032 					AlterTableStmt *n = makeNode(AlterTableStmt);
2033 					n->relation = $4;
2034 					n->cmds = $5;
2035 					n->relkind = OBJECT_MATVIEW;
2036 					n->missing_ok = false;
2037 					$$ = (Node *)n;
2038 				}
2039 		|	ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
2040 				{
2041 					AlterTableStmt *n = makeNode(AlterTableStmt);
2042 					n->relation = $6;
2043 					n->cmds = $7;
2044 					n->relkind = OBJECT_MATVIEW;
2045 					n->missing_ok = true;
2046 					$$ = (Node *)n;
2047 				}
2048 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
2049 				{
2050 					AlterTableMoveAllStmt *n =
2051 						makeNode(AlterTableMoveAllStmt);
2052 					n->orig_tablespacename = $7;
2053 					n->objtype = OBJECT_MATVIEW;
2054 					n->roles = NIL;
2055 					n->new_tablespacename = $10;
2056 					n->nowait = $11;
2057 					$$ = (Node *)n;
2058 				}
2059 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
2060 				{
2061 					AlterTableMoveAllStmt *n =
2062 						makeNode(AlterTableMoveAllStmt);
2063 					n->orig_tablespacename = $7;
2064 					n->objtype = OBJECT_MATVIEW;
2065 					n->roles = $10;
2066 					n->new_tablespacename = $13;
2067 					n->nowait = $14;
2068 					$$ = (Node *)n;
2069 				}
2070 		;
2071 
2072 alter_table_cmds:
2073 			alter_table_cmd							{ $$ = list_make1($1); }
2074 			| alter_table_cmds ',' alter_table_cmd	{ $$ = lappend($1, $3); }
2075 		;
2076 
2077 partition_cmd:
2078 			/* ALTER TABLE <name> ATTACH PARTITION <table_name> FOR VALUES */
2079 			ATTACH PARTITION qualified_name PartitionBoundSpec
2080 				{
2081 					AlterTableCmd *n = makeNode(AlterTableCmd);
2082 					PartitionCmd *cmd = makeNode(PartitionCmd);
2083 
2084 					n->subtype = AT_AttachPartition;
2085 					cmd->name = $3;
2086 					cmd->bound = $4;
2087 					n->def = (Node *) cmd;
2088 
2089 					$$ = (Node *) n;
2090 				}
2091 			/* ALTER TABLE <name> DETACH PARTITION <partition_name> */
2092 			| DETACH PARTITION qualified_name
2093 				{
2094 					AlterTableCmd *n = makeNode(AlterTableCmd);
2095 					PartitionCmd *cmd = makeNode(PartitionCmd);
2096 
2097 					n->subtype = AT_DetachPartition;
2098 					cmd->name = $3;
2099 					cmd->bound = NULL;
2100 					n->def = (Node *) cmd;
2101 
2102 					$$ = (Node *) n;
2103 				}
2104 		;
2105 
2106 index_partition_cmd:
2107 			/* ALTER INDEX <name> ATTACH PARTITION <index_name> */
2108 			ATTACH PARTITION qualified_name
2109 				{
2110 					AlterTableCmd *n = makeNode(AlterTableCmd);
2111 					PartitionCmd *cmd = makeNode(PartitionCmd);
2112 
2113 					n->subtype = AT_AttachPartition;
2114 					cmd->name = $3;
2115 					cmd->bound = NULL;
2116 					n->def = (Node *) cmd;
2117 
2118 					$$ = (Node *) n;
2119 				}
2120 		;
2121 
2122 alter_table_cmd:
2123 			/* ALTER TABLE <name> ADD <coldef> */
2124 			ADD_P columnDef
2125 				{
2126 					AlterTableCmd *n = makeNode(AlterTableCmd);
2127 					n->subtype = AT_AddColumn;
2128 					n->def = $2;
2129 					n->missing_ok = false;
2130 					$$ = (Node *)n;
2131 				}
2132 			/* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
2133 			| ADD_P IF_P NOT EXISTS columnDef
2134 				{
2135 					AlterTableCmd *n = makeNode(AlterTableCmd);
2136 					n->subtype = AT_AddColumn;
2137 					n->def = $5;
2138 					n->missing_ok = true;
2139 					$$ = (Node *)n;
2140 				}
2141 			/* ALTER TABLE <name> ADD COLUMN <coldef> */
2142 			| ADD_P COLUMN columnDef
2143 				{
2144 					AlterTableCmd *n = makeNode(AlterTableCmd);
2145 					n->subtype = AT_AddColumn;
2146 					n->def = $3;
2147 					n->missing_ok = false;
2148 					$$ = (Node *)n;
2149 				}
2150 			/* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
2151 			| ADD_P COLUMN IF_P NOT EXISTS columnDef
2152 				{
2153 					AlterTableCmd *n = makeNode(AlterTableCmd);
2154 					n->subtype = AT_AddColumn;
2155 					n->def = $6;
2156 					n->missing_ok = true;
2157 					$$ = (Node *)n;
2158 				}
2159 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
2160 			| ALTER opt_column ColId alter_column_default
2161 				{
2162 					AlterTableCmd *n = makeNode(AlterTableCmd);
2163 					n->subtype = AT_ColumnDefault;
2164 					n->name = $3;
2165 					n->def = $4;
2166 					$$ = (Node *)n;
2167 				}
2168 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
2169 			| ALTER opt_column ColId DROP NOT NULL_P
2170 				{
2171 					AlterTableCmd *n = makeNode(AlterTableCmd);
2172 					n->subtype = AT_DropNotNull;
2173 					n->name = $3;
2174 					$$ = (Node *)n;
2175 				}
2176 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
2177 			| ALTER opt_column ColId SET NOT NULL_P
2178 				{
2179 					AlterTableCmd *n = makeNode(AlterTableCmd);
2180 					n->subtype = AT_SetNotNull;
2181 					n->name = $3;
2182 					$$ = (Node *)n;
2183 				}
2184 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
2185 			| ALTER opt_column ColId SET STATISTICS SignedIconst
2186 				{
2187 					AlterTableCmd *n = makeNode(AlterTableCmd);
2188 					n->subtype = AT_SetStatistics;
2189 					n->name = $3;
2190 					n->def = (Node *) makeInteger($6);
2191 					$$ = (Node *)n;
2192 				}
2193 			/* ALTER TABLE <name> ALTER [COLUMN] <colnum> SET STATISTICS <SignedIconst> */
2194 			| ALTER opt_column Iconst SET STATISTICS SignedIconst
2195 				{
2196 					AlterTableCmd *n = makeNode(AlterTableCmd);
2197 
2198 					if ($3 <= 0 || $3 > PG_INT16_MAX)
2199 						ereport(ERROR,
2200 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2201 								 errmsg("column number must be in range from 1 to %d", PG_INT16_MAX),
2202 								 parser_errposition(@3)));
2203 
2204 					n->subtype = AT_SetStatistics;
2205 					n->num = (int16) $3;
2206 					n->def = (Node *) makeInteger($6);
2207 					$$ = (Node *)n;
2208 				}
2209 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
2210 			| ALTER opt_column ColId SET reloptions
2211 				{
2212 					AlterTableCmd *n = makeNode(AlterTableCmd);
2213 					n->subtype = AT_SetOptions;
2214 					n->name = $3;
2215 					n->def = (Node *) $5;
2216 					$$ = (Node *)n;
2217 				}
2218 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
2219 			| ALTER opt_column ColId RESET reloptions
2220 				{
2221 					AlterTableCmd *n = makeNode(AlterTableCmd);
2222 					n->subtype = AT_ResetOptions;
2223 					n->name = $3;
2224 					n->def = (Node *) $5;
2225 					$$ = (Node *)n;
2226 				}
2227 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
2228 			| ALTER opt_column ColId SET STORAGE ColId
2229 				{
2230 					AlterTableCmd *n = makeNode(AlterTableCmd);
2231 					n->subtype = AT_SetStorage;
2232 					n->name = $3;
2233 					n->def = (Node *) makeString($6);
2234 					$$ = (Node *)n;
2235 				}
2236 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> ADD GENERATED ... AS IDENTITY ... */
2237 			| ALTER opt_column ColId ADD_P GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
2238 				{
2239 					AlterTableCmd *n = makeNode(AlterTableCmd);
2240 					Constraint *c = makeNode(Constraint);
2241 
2242 					c->contype = CONSTR_IDENTITY;
2243 					c->generated_when = $6;
2244 					c->options = $9;
2245 					c->location = @5;
2246 
2247 					n->subtype = AT_AddIdentity;
2248 					n->name = $3;
2249 					n->def = (Node *) c;
2250 
2251 					$$ = (Node *)n;
2252 				}
2253 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET <sequence options>/RESET */
2254 			| ALTER opt_column ColId alter_identity_column_option_list
2255 				{
2256 					AlterTableCmd *n = makeNode(AlterTableCmd);
2257 					n->subtype = AT_SetIdentity;
2258 					n->name = $3;
2259 					n->def = (Node *) $4;
2260 					$$ = (Node *)n;
2261 				}
2262 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY */
2263 			| ALTER opt_column ColId DROP IDENTITY_P
2264 				{
2265 					AlterTableCmd *n = makeNode(AlterTableCmd);
2266 					n->subtype = AT_DropIdentity;
2267 					n->name = $3;
2268 					n->missing_ok = false;
2269 					$$ = (Node *)n;
2270 				}
2271 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY IF EXISTS */
2272 			| ALTER opt_column ColId DROP IDENTITY_P IF_P EXISTS
2273 				{
2274 					AlterTableCmd *n = makeNode(AlterTableCmd);
2275 					n->subtype = AT_DropIdentity;
2276 					n->name = $3;
2277 					n->missing_ok = true;
2278 					$$ = (Node *)n;
2279 				}
2280 			/* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
2281 			| DROP opt_column IF_P EXISTS ColId opt_drop_behavior
2282 				{
2283 					AlterTableCmd *n = makeNode(AlterTableCmd);
2284 					n->subtype = AT_DropColumn;
2285 					n->name = $5;
2286 					n->behavior = $6;
2287 					n->missing_ok = true;
2288 					$$ = (Node *)n;
2289 				}
2290 			/* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
2291 			| DROP opt_column ColId opt_drop_behavior
2292 				{
2293 					AlterTableCmd *n = makeNode(AlterTableCmd);
2294 					n->subtype = AT_DropColumn;
2295 					n->name = $3;
2296 					n->behavior = $4;
2297 					n->missing_ok = false;
2298 					$$ = (Node *)n;
2299 				}
2300 			/*
2301 			 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
2302 			 *		[ USING <expression> ]
2303 			 */
2304 			| ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
2305 				{
2306 					AlterTableCmd *n = makeNode(AlterTableCmd);
2307 					ColumnDef *def = makeNode(ColumnDef);
2308 					n->subtype = AT_AlterColumnType;
2309 					n->name = $3;
2310 					n->def = (Node *) def;
2311 					/* We only use these fields of the ColumnDef node */
2312 					def->typeName = $6;
2313 					def->collClause = (CollateClause *) $7;
2314 					def->raw_default = $8;
2315 					def->location = @3;
2316 					$$ = (Node *)n;
2317 				}
2318 			/* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
2319 			| ALTER opt_column ColId alter_generic_options
2320 				{
2321 					AlterTableCmd *n = makeNode(AlterTableCmd);
2322 					n->subtype = AT_AlterColumnGenericOptions;
2323 					n->name = $3;
2324 					n->def = (Node *) $4;
2325 					$$ = (Node *)n;
2326 				}
2327 			/* ALTER TABLE <name> ADD CONSTRAINT ... */
2328 			| ADD_P TableConstraint
2329 				{
2330 					AlterTableCmd *n = makeNode(AlterTableCmd);
2331 					n->subtype = AT_AddConstraint;
2332 					n->def = $2;
2333 					n->missing_ok = false;
2334 					$$ = (Node *)n;
2335 				}
2336 			/* ALTER TABLE <name> ALTER CONSTRAINT ... */
2337 			| ALTER CONSTRAINT name ConstraintAttributeSpec
2338 				{
2339 					AlterTableCmd *n = makeNode(AlterTableCmd);
2340 					Constraint *c = makeNode(Constraint);
2341 					n->subtype = AT_AlterConstraint;
2342 					n->def = (Node *) c;
2343 					c->contype = CONSTR_FOREIGN; /* others not supported, yet */
2344 					c->conname = $3;
2345 					processCASbits($4, @4, "ALTER CONSTRAINT statement",
2346 									&c->deferrable,
2347 									&c->initdeferred,
2348 									NULL, NULL, yyscanner);
2349 					$$ = (Node *)n;
2350 				}
2351 			/* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
2352 			| VALIDATE CONSTRAINT name
2353 				{
2354 					AlterTableCmd *n = makeNode(AlterTableCmd);
2355 					n->subtype = AT_ValidateConstraint;
2356 					n->name = $3;
2357 					$$ = (Node *)n;
2358 				}
2359 			/* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
2360 			| DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
2361 				{
2362 					AlterTableCmd *n = makeNode(AlterTableCmd);
2363 					n->subtype = AT_DropConstraint;
2364 					n->name = $5;
2365 					n->behavior = $6;
2366 					n->missing_ok = true;
2367 					$$ = (Node *)n;
2368 				}
2369 			/* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
2370 			| DROP CONSTRAINT name opt_drop_behavior
2371 				{
2372 					AlterTableCmd *n = makeNode(AlterTableCmd);
2373 					n->subtype = AT_DropConstraint;
2374 					n->name = $3;
2375 					n->behavior = $4;
2376 					n->missing_ok = false;
2377 					$$ = (Node *)n;
2378 				}
2379 			/* ALTER TABLE <name> SET WITH OIDS  */
2380 			| SET WITH OIDS
2381 				{
2382 					AlterTableCmd *n = makeNode(AlterTableCmd);
2383 					n->subtype = AT_AddOids;
2384 					$$ = (Node *)n;
2385 				}
2386 			/* ALTER TABLE <name> SET WITHOUT OIDS  */
2387 			| SET WITHOUT OIDS
2388 				{
2389 					AlterTableCmd *n = makeNode(AlterTableCmd);
2390 					n->subtype = AT_DropOids;
2391 					$$ = (Node *)n;
2392 				}
2393 			/* ALTER TABLE <name> CLUSTER ON <indexname> */
2394 			| CLUSTER ON name
2395 				{
2396 					AlterTableCmd *n = makeNode(AlterTableCmd);
2397 					n->subtype = AT_ClusterOn;
2398 					n->name = $3;
2399 					$$ = (Node *)n;
2400 				}
2401 			/* ALTER TABLE <name> SET WITHOUT CLUSTER */
2402 			| SET WITHOUT CLUSTER
2403 				{
2404 					AlterTableCmd *n = makeNode(AlterTableCmd);
2405 					n->subtype = AT_DropCluster;
2406 					n->name = NULL;
2407 					$$ = (Node *)n;
2408 				}
2409 			/* ALTER TABLE <name> SET LOGGED  */
2410 			| SET LOGGED
2411 				{
2412 					AlterTableCmd *n = makeNode(AlterTableCmd);
2413 					n->subtype = AT_SetLogged;
2414 					$$ = (Node *)n;
2415 				}
2416 			/* ALTER TABLE <name> SET UNLOGGED  */
2417 			| SET UNLOGGED
2418 				{
2419 					AlterTableCmd *n = makeNode(AlterTableCmd);
2420 					n->subtype = AT_SetUnLogged;
2421 					$$ = (Node *)n;
2422 				}
2423 			/* ALTER TABLE <name> ENABLE TRIGGER <trig> */
2424 			| ENABLE_P TRIGGER name
2425 				{
2426 					AlterTableCmd *n = makeNode(AlterTableCmd);
2427 					n->subtype = AT_EnableTrig;
2428 					n->name = $3;
2429 					$$ = (Node *)n;
2430 				}
2431 			/* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
2432 			| ENABLE_P ALWAYS TRIGGER name
2433 				{
2434 					AlterTableCmd *n = makeNode(AlterTableCmd);
2435 					n->subtype = AT_EnableAlwaysTrig;
2436 					n->name = $4;
2437 					$$ = (Node *)n;
2438 				}
2439 			/* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
2440 			| ENABLE_P REPLICA TRIGGER name
2441 				{
2442 					AlterTableCmd *n = makeNode(AlterTableCmd);
2443 					n->subtype = AT_EnableReplicaTrig;
2444 					n->name = $4;
2445 					$$ = (Node *)n;
2446 				}
2447 			/* ALTER TABLE <name> ENABLE TRIGGER ALL */
2448 			| ENABLE_P TRIGGER ALL
2449 				{
2450 					AlterTableCmd *n = makeNode(AlterTableCmd);
2451 					n->subtype = AT_EnableTrigAll;
2452 					$$ = (Node *)n;
2453 				}
2454 			/* ALTER TABLE <name> ENABLE TRIGGER USER */
2455 			| ENABLE_P TRIGGER USER
2456 				{
2457 					AlterTableCmd *n = makeNode(AlterTableCmd);
2458 					n->subtype = AT_EnableTrigUser;
2459 					$$ = (Node *)n;
2460 				}
2461 			/* ALTER TABLE <name> DISABLE TRIGGER <trig> */
2462 			| DISABLE_P TRIGGER name
2463 				{
2464 					AlterTableCmd *n = makeNode(AlterTableCmd);
2465 					n->subtype = AT_DisableTrig;
2466 					n->name = $3;
2467 					$$ = (Node *)n;
2468 				}
2469 			/* ALTER TABLE <name> DISABLE TRIGGER ALL */
2470 			| DISABLE_P TRIGGER ALL
2471 				{
2472 					AlterTableCmd *n = makeNode(AlterTableCmd);
2473 					n->subtype = AT_DisableTrigAll;
2474 					$$ = (Node *)n;
2475 				}
2476 			/* ALTER TABLE <name> DISABLE TRIGGER USER */
2477 			| DISABLE_P TRIGGER USER
2478 				{
2479 					AlterTableCmd *n = makeNode(AlterTableCmd);
2480 					n->subtype = AT_DisableTrigUser;
2481 					$$ = (Node *)n;
2482 				}
2483 			/* ALTER TABLE <name> ENABLE RULE <rule> */
2484 			| ENABLE_P RULE name
2485 				{
2486 					AlterTableCmd *n = makeNode(AlterTableCmd);
2487 					n->subtype = AT_EnableRule;
2488 					n->name = $3;
2489 					$$ = (Node *)n;
2490 				}
2491 			/* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
2492 			| ENABLE_P ALWAYS RULE name
2493 				{
2494 					AlterTableCmd *n = makeNode(AlterTableCmd);
2495 					n->subtype = AT_EnableAlwaysRule;
2496 					n->name = $4;
2497 					$$ = (Node *)n;
2498 				}
2499 			/* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
2500 			| ENABLE_P REPLICA RULE name
2501 				{
2502 					AlterTableCmd *n = makeNode(AlterTableCmd);
2503 					n->subtype = AT_EnableReplicaRule;
2504 					n->name = $4;
2505 					$$ = (Node *)n;
2506 				}
2507 			/* ALTER TABLE <name> DISABLE RULE <rule> */
2508 			| DISABLE_P RULE name
2509 				{
2510 					AlterTableCmd *n = makeNode(AlterTableCmd);
2511 					n->subtype = AT_DisableRule;
2512 					n->name = $3;
2513 					$$ = (Node *)n;
2514 				}
2515 			/* ALTER TABLE <name> INHERIT <parent> */
2516 			| INHERIT qualified_name
2517 				{
2518 					AlterTableCmd *n = makeNode(AlterTableCmd);
2519 					n->subtype = AT_AddInherit;
2520 					n->def = (Node *) $2;
2521 					$$ = (Node *)n;
2522 				}
2523 			/* ALTER TABLE <name> NO INHERIT <parent> */
2524 			| NO INHERIT qualified_name
2525 				{
2526 					AlterTableCmd *n = makeNode(AlterTableCmd);
2527 					n->subtype = AT_DropInherit;
2528 					n->def = (Node *) $3;
2529 					$$ = (Node *)n;
2530 				}
2531 			/* ALTER TABLE <name> OF <type_name> */
2532 			| OF any_name
2533 				{
2534 					AlterTableCmd *n = makeNode(AlterTableCmd);
2535 					TypeName *def = makeTypeNameFromNameList($2);
2536 					def->location = @2;
2537 					n->subtype = AT_AddOf;
2538 					n->def = (Node *) def;
2539 					$$ = (Node *)n;
2540 				}
2541 			/* ALTER TABLE <name> NOT OF */
2542 			| NOT OF
2543 				{
2544 					AlterTableCmd *n = makeNode(AlterTableCmd);
2545 					n->subtype = AT_DropOf;
2546 					$$ = (Node *)n;
2547 				}
2548 			/* ALTER TABLE <name> OWNER TO RoleSpec */
2549 			| OWNER TO RoleSpec
2550 				{
2551 					AlterTableCmd *n = makeNode(AlterTableCmd);
2552 					n->subtype = AT_ChangeOwner;
2553 					n->newowner = $3;
2554 					$$ = (Node *)n;
2555 				}
2556 			/* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
2557 			| SET TABLESPACE name
2558 				{
2559 					AlterTableCmd *n = makeNode(AlterTableCmd);
2560 					n->subtype = AT_SetTableSpace;
2561 					n->name = $3;
2562 					$$ = (Node *)n;
2563 				}
2564 			/* ALTER TABLE <name> SET (...) */
2565 			| SET reloptions
2566 				{
2567 					AlterTableCmd *n = makeNode(AlterTableCmd);
2568 					n->subtype = AT_SetRelOptions;
2569 					n->def = (Node *)$2;
2570 					$$ = (Node *)n;
2571 				}
2572 			/* ALTER TABLE <name> RESET (...) */
2573 			| RESET reloptions
2574 				{
2575 					AlterTableCmd *n = makeNode(AlterTableCmd);
2576 					n->subtype = AT_ResetRelOptions;
2577 					n->def = (Node *)$2;
2578 					$$ = (Node *)n;
2579 				}
2580 			/* ALTER TABLE <name> REPLICA IDENTITY  */
2581 			| REPLICA IDENTITY_P replica_identity
2582 				{
2583 					AlterTableCmd *n = makeNode(AlterTableCmd);
2584 					n->subtype = AT_ReplicaIdentity;
2585 					n->def = $3;
2586 					$$ = (Node *)n;
2587 				}
2588 			/* ALTER TABLE <name> ENABLE ROW LEVEL SECURITY */
2589 			| ENABLE_P ROW LEVEL SECURITY
2590 				{
2591 					AlterTableCmd *n = makeNode(AlterTableCmd);
2592 					n->subtype = AT_EnableRowSecurity;
2593 					$$ = (Node *)n;
2594 				}
2595 			/* ALTER TABLE <name> DISABLE ROW LEVEL SECURITY */
2596 			| DISABLE_P ROW LEVEL SECURITY
2597 				{
2598 					AlterTableCmd *n = makeNode(AlterTableCmd);
2599 					n->subtype = AT_DisableRowSecurity;
2600 					$$ = (Node *)n;
2601 				}
2602 			/* ALTER TABLE <name> FORCE ROW LEVEL SECURITY */
2603 			| FORCE ROW LEVEL SECURITY
2604 				{
2605 					AlterTableCmd *n = makeNode(AlterTableCmd);
2606 					n->subtype = AT_ForceRowSecurity;
2607 					$$ = (Node *)n;
2608 				}
2609 			/* ALTER TABLE <name> NO FORCE ROW LEVEL SECURITY */
2610 			| NO FORCE ROW LEVEL SECURITY
2611 				{
2612 					AlterTableCmd *n = makeNode(AlterTableCmd);
2613 					n->subtype = AT_NoForceRowSecurity;
2614 					$$ = (Node *)n;
2615 				}
2616 			| alter_generic_options
2617 				{
2618 					AlterTableCmd *n = makeNode(AlterTableCmd);
2619 					n->subtype = AT_GenericOptions;
2620 					n->def = (Node *)$1;
2621 					$$ = (Node *) n;
2622 				}
2623 		;
2624 
2625 alter_column_default:
2626 			SET DEFAULT a_expr			{ $$ = $3; }
2627 			| DROP DEFAULT				{ $$ = NULL; }
2628 		;
2629 
2630 opt_drop_behavior:
2631 			CASCADE						{ $$ = DROP_CASCADE; }
2632 			| RESTRICT					{ $$ = DROP_RESTRICT; }
2633 			| /* EMPTY */				{ $$ = DROP_RESTRICT; /* default */ }
2634 		;
2635 
2636 opt_collate_clause:
2637 			COLLATE any_name
2638 				{
2639 					CollateClause *n = makeNode(CollateClause);
2640 					n->arg = NULL;
2641 					n->collname = $2;
2642 					n->location = @1;
2643 					$$ = (Node *) n;
2644 				}
2645 			| /* EMPTY */				{ $$ = NULL; }
2646 		;
2647 
2648 alter_using:
2649 			USING a_expr				{ $$ = $2; }
2650 			| /* EMPTY */				{ $$ = NULL; }
2651 		;
2652 
2653 replica_identity:
2654 			NOTHING
2655 				{
2656 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2657 					n->identity_type = REPLICA_IDENTITY_NOTHING;
2658 					n->name = NULL;
2659 					$$ = (Node *) n;
2660 				}
2661 			| FULL
2662 				{
2663 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2664 					n->identity_type = REPLICA_IDENTITY_FULL;
2665 					n->name = NULL;
2666 					$$ = (Node *) n;
2667 				}
2668 			| DEFAULT
2669 				{
2670 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2671 					n->identity_type = REPLICA_IDENTITY_DEFAULT;
2672 					n->name = NULL;
2673 					$$ = (Node *) n;
2674 				}
2675 			| USING INDEX name
2676 				{
2677 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2678 					n->identity_type = REPLICA_IDENTITY_INDEX;
2679 					n->name = $3;
2680 					$$ = (Node *) n;
2681 				}
2682 ;
2683 
2684 reloptions:
2685 			'(' reloption_list ')'					{ $$ = $2; }
2686 		;
2687 
2688 opt_reloptions:		WITH reloptions					{ $$ = $2; }
2689 			 |		/* EMPTY */						{ $$ = NIL; }
2690 		;
2691 
2692 reloption_list:
2693 			reloption_elem							{ $$ = list_make1($1); }
2694 			| reloption_list ',' reloption_elem		{ $$ = lappend($1, $3); }
2695 		;
2696 
2697 /* This should match def_elem and also allow qualified names */
2698 reloption_elem:
2699 			ColLabel '=' def_arg
2700 				{
2701 					$$ = makeDefElem($1, (Node *) $3, @1);
2702 				}
2703 			| ColLabel
2704 				{
2705 					$$ = makeDefElem($1, NULL, @1);
2706 				}
2707 			| ColLabel '.' ColLabel '=' def_arg
2708 				{
2709 					$$ = makeDefElemExtended($1, $3, (Node *) $5,
2710 											 DEFELEM_UNSPEC, @1);
2711 				}
2712 			| ColLabel '.' ColLabel
2713 				{
2714 					$$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC, @1);
2715 				}
2716 		;
2717 
2718 alter_identity_column_option_list:
2719 			alter_identity_column_option
2720 				{ $$ = list_make1($1); }
2721 			| alter_identity_column_option_list alter_identity_column_option
2722 				{ $$ = lappend($1, $2); }
2723 		;
2724 
2725 alter_identity_column_option:
2726 			RESTART
2727 				{
2728 					$$ = makeDefElem("restart", NULL, @1);
2729 				}
2730 			| RESTART opt_with NumericOnly
2731 				{
2732 					$$ = makeDefElem("restart", (Node *)$3, @1);
2733 				}
2734 			| SET SeqOptElem
2735 				{
2736 					if (strcmp($2->defname, "as") == 0 ||
2737 						strcmp($2->defname, "restart") == 0 ||
2738 						strcmp($2->defname, "owned_by") == 0)
2739 						ereport(ERROR,
2740 								(errcode(ERRCODE_SYNTAX_ERROR),
2741 								 errmsg("sequence option \"%s\" not supported here", $2->defname),
2742 								 parser_errposition(@2)));
2743 					$$ = $2;
2744 				}
2745 			| SET GENERATED generated_when
2746 				{
2747 					$$ = makeDefElem("generated", (Node *) makeInteger($3), @1);
2748 				}
2749 		;
2750 
2751 PartitionBoundSpec:
2752 			/* a HASH partition*/
2753 			FOR VALUES WITH '(' hash_partbound ')'
2754 				{
2755 					ListCell   *lc;
2756 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2757 
2758 					n->strategy = PARTITION_STRATEGY_HASH;
2759 					n->modulus = n->remainder = -1;
2760 
foreach(lc,$5)2761 					foreach (lc, $5)
2762 					{
2763 						DefElem    *opt = lfirst_node(DefElem, lc);
2764 
2765 						if (strcmp(opt->defname, "modulus") == 0)
2766 						{
2767 							if (n->modulus != -1)
2768 								ereport(ERROR,
2769 										(errcode(ERRCODE_DUPLICATE_OBJECT),
2770 										 errmsg("modulus for hash partition provided more than once"),
2771 										 parser_errposition(opt->location)));
2772 							n->modulus = defGetInt32(opt);
2773 						}
2774 						else if (strcmp(opt->defname, "remainder") == 0)
2775 						{
2776 							if (n->remainder != -1)
2777 								ereport(ERROR,
2778 										(errcode(ERRCODE_DUPLICATE_OBJECT),
2779 										 errmsg("remainder for hash partition provided more than once"),
2780 										 parser_errposition(opt->location)));
2781 							n->remainder = defGetInt32(opt);
2782 						}
2783 						else
2784 							ereport(ERROR,
2785 									(errcode(ERRCODE_SYNTAX_ERROR),
2786 									 errmsg("unrecognized hash partition bound specification \"%s\"",
2787 											opt->defname),
2788 									 parser_errposition(opt->location)));
2789 					}
2790 
2791 					if (n->modulus == -1)
2792 						ereport(ERROR,
2793 								(errcode(ERRCODE_SYNTAX_ERROR),
2794 								 errmsg("modulus for hash partition must be specified")));
2795 					if (n->remainder == -1)
2796 						ereport(ERROR,
2797 								(errcode(ERRCODE_SYNTAX_ERROR),
2798 								 errmsg("remainder for hash partition must be specified")));
2799 
2800 					n->location = @3;
2801 
2802 					$$ = n;
2803 				}
2804 
2805 			/* a LIST partition */
2806 			| FOR VALUES IN_P '(' partbound_datum_list ')'
2807 				{
2808 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2809 
2810 					n->strategy = PARTITION_STRATEGY_LIST;
2811 					n->is_default = false;
2812 					n->listdatums = $5;
2813 					n->location = @3;
2814 
2815 					$$ = n;
2816 				}
2817 
2818 			/* a RANGE partition */
2819 			| FOR VALUES FROM '(' range_datum_list ')' TO '(' range_datum_list ')'
2820 				{
2821 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2822 
2823 					n->strategy = PARTITION_STRATEGY_RANGE;
2824 					n->is_default = false;
2825 					n->lowerdatums = $5;
2826 					n->upperdatums = $9;
2827 					n->location = @3;
2828 
2829 					$$ = n;
2830 				}
2831 
2832 			/* a DEFAULT partition */
2833 			| DEFAULT
2834 				{
2835 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2836 
2837 					n->is_default = true;
2838 					n->location = @1;
2839 
2840 					$$ = n;
2841 				}
2842 		;
2843 
2844 hash_partbound_elem:
2845 		NonReservedWord Iconst
2846 			{
2847 				$$ = makeDefElem($1, (Node *)makeInteger($2), @1);
2848 			}
2849 		;
2850 
2851 hash_partbound:
2852 		hash_partbound_elem
2853 			{
2854 				$$ = list_make1($1);
2855 			}
2856 		| hash_partbound ',' hash_partbound_elem
2857 			{
2858 				$$ = lappend($1, $3);
2859 			}
2860 		;
2861 
2862 partbound_datum:
2863 			Sconst			{ $$ = makeStringConst($1, @1); }
2864 			| NumericOnly	{ $$ = makeAConst($1, @1); }
2865 			| TRUE_P		{ $$ = makeStringConst(pstrdup("true"), @1); }
2866 			| FALSE_P		{ $$ = makeStringConst(pstrdup("false"), @1); }
2867 			| NULL_P		{ $$ = makeNullAConst(@1); }
2868 		;
2869 
2870 partbound_datum_list:
2871 			partbound_datum						{ $$ = list_make1($1); }
2872 			| partbound_datum_list ',' partbound_datum
2873 												{ $$ = lappend($1, $3); }
2874 		;
2875 
2876 range_datum_list:
2877 			PartitionRangeDatum					{ $$ = list_make1($1); }
2878 			| range_datum_list ',' PartitionRangeDatum
2879 												{ $$ = lappend($1, $3); }
2880 		;
2881 
2882 PartitionRangeDatum:
2883 			MINVALUE
2884 				{
2885 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2886 
2887 					n->kind = PARTITION_RANGE_DATUM_MINVALUE;
2888 					n->value = NULL;
2889 					n->location = @1;
2890 
2891 					$$ = (Node *) n;
2892 				}
2893 			| MAXVALUE
2894 				{
2895 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2896 
2897 					n->kind = PARTITION_RANGE_DATUM_MAXVALUE;
2898 					n->value = NULL;
2899 					n->location = @1;
2900 
2901 					$$ = (Node *) n;
2902 				}
2903 			| partbound_datum
2904 				{
2905 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2906 
2907 					n->kind = PARTITION_RANGE_DATUM_VALUE;
2908 					n->value = $1;
2909 					n->location = @1;
2910 
2911 					$$ = (Node *) n;
2912 				}
2913 		;
2914 
2915 /*****************************************************************************
2916  *
2917  *	ALTER TYPE
2918  *
2919  * really variants of the ALTER TABLE subcommands with different spellings
2920  *****************************************************************************/
2921 
2922 AlterCompositeTypeStmt:
2923 			ALTER TYPE_P any_name alter_type_cmds
2924 				{
2925 					AlterTableStmt *n = makeNode(AlterTableStmt);
2926 
2927 					/* can't use qualified_name, sigh */
2928 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
2929 					n->cmds = $4;
2930 					n->relkind = OBJECT_TYPE;
2931 					$$ = (Node *)n;
2932 				}
2933 			;
2934 
2935 alter_type_cmds:
2936 			alter_type_cmd							{ $$ = list_make1($1); }
2937 			| alter_type_cmds ',' alter_type_cmd	{ $$ = lappend($1, $3); }
2938 		;
2939 
2940 alter_type_cmd:
2941 			/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
2942 			ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
2943 				{
2944 					AlterTableCmd *n = makeNode(AlterTableCmd);
2945 					n->subtype = AT_AddColumn;
2946 					n->def = $3;
2947 					n->behavior = $4;
2948 					$$ = (Node *)n;
2949 				}
2950 			/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
2951 			| DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
2952 				{
2953 					AlterTableCmd *n = makeNode(AlterTableCmd);
2954 					n->subtype = AT_DropColumn;
2955 					n->name = $5;
2956 					n->behavior = $6;
2957 					n->missing_ok = true;
2958 					$$ = (Node *)n;
2959 				}
2960 			/* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
2961 			| DROP ATTRIBUTE ColId opt_drop_behavior
2962 				{
2963 					AlterTableCmd *n = makeNode(AlterTableCmd);
2964 					n->subtype = AT_DropColumn;
2965 					n->name = $3;
2966 					n->behavior = $4;
2967 					n->missing_ok = false;
2968 					$$ = (Node *)n;
2969 				}
2970 			/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
2971 			| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
2972 				{
2973 					AlterTableCmd *n = makeNode(AlterTableCmd);
2974 					ColumnDef *def = makeNode(ColumnDef);
2975 					n->subtype = AT_AlterColumnType;
2976 					n->name = $3;
2977 					n->def = (Node *) def;
2978 					n->behavior = $8;
2979 					/* We only use these fields of the ColumnDef node */
2980 					def->typeName = $6;
2981 					def->collClause = (CollateClause *) $7;
2982 					def->raw_default = NULL;
2983 					def->location = @3;
2984 					$$ = (Node *)n;
2985 				}
2986 		;
2987 
2988 
2989 /*****************************************************************************
2990  *
2991  *		QUERY :
2992  *				close <portalname>
2993  *
2994  *****************************************************************************/
2995 
2996 ClosePortalStmt:
2997 			CLOSE cursor_name
2998 				{
2999 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
3000 					n->portalname = $2;
3001 					$$ = (Node *)n;
3002 				}
3003 			| CLOSE ALL
3004 				{
3005 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
3006 					n->portalname = NULL;
3007 					$$ = (Node *)n;
3008 				}
3009 		;
3010 
3011 
3012 /*****************************************************************************
3013  *
3014  *		QUERY :
3015  *				COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
3016  *				COPY ( query ) TO file	[WITH] [(options)]
3017  *
3018  *				where 'query' can be one of:
3019  *				{ SELECT | UPDATE | INSERT | DELETE }
3020  *
3021  *				and 'file' can be one of:
3022  *				{ PROGRAM 'command' | STDIN | STDOUT | 'filename' }
3023  *
3024  *				In the preferred syntax the options are comma-separated
3025  *				and use generic identifiers instead of keywords.  The pre-9.0
3026  *				syntax had a hard-wired, space-separated set of options.
3027  *
3028  *				Really old syntax, from versions 7.2 and prior:
3029  *				COPY [ BINARY ] table [ WITH OIDS ] FROM/TO file
3030  *					[ [ USING ] DELIMITERS 'delimiter' ] ]
3031  *					[ WITH NULL AS 'null string' ]
3032  *				This option placement is not supported with COPY (query...).
3033  *
3034  *****************************************************************************/
3035 
3036 CopyStmt:	COPY opt_binary qualified_name opt_column_list opt_oids
3037 			copy_from opt_program copy_file_name copy_delimiter opt_with copy_options
3038 				{
3039 					CopyStmt *n = makeNode(CopyStmt);
3040 					n->relation = $3;
3041 					n->query = NULL;
3042 					n->attlist = $4;
3043 					n->is_from = $6;
3044 					n->is_program = $7;
3045 					n->filename = $8;
3046 
3047 					if (n->is_program && n->filename == NULL)
3048 						ereport(ERROR,
3049 								(errcode(ERRCODE_SYNTAX_ERROR),
3050 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
3051 								 parser_errposition(@8)));
3052 
3053 					n->options = NIL;
3054 					/* Concatenate user-supplied flags */
3055 					if ($2)
3056 						n->options = lappend(n->options, $2);
3057 					if ($5)
3058 						n->options = lappend(n->options, $5);
3059 					if ($9)
3060 						n->options = lappend(n->options, $9);
3061 					if ($11)
3062 						n->options = list_concat(n->options, $11);
3063 					$$ = (Node *)n;
3064 				}
3065 			| COPY '(' PreparableStmt ')' TO opt_program copy_file_name opt_with copy_options
3066 				{
3067 					CopyStmt *n = makeNode(CopyStmt);
3068 					n->relation = NULL;
3069 					n->query = $3;
3070 					n->attlist = NIL;
3071 					n->is_from = false;
3072 					n->is_program = $6;
3073 					n->filename = $7;
3074 					n->options = $9;
3075 
3076 					if (n->is_program && n->filename == NULL)
3077 						ereport(ERROR,
3078 								(errcode(ERRCODE_SYNTAX_ERROR),
3079 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
3080 								 parser_errposition(@5)));
3081 
3082 					$$ = (Node *)n;
3083 				}
3084 		;
3085 
3086 copy_from:
3087 			FROM									{ $$ = true; }
3088 			| TO									{ $$ = false; }
3089 		;
3090 
3091 opt_program:
3092 			PROGRAM									{ $$ = true; }
3093 			| /* EMPTY */							{ $$ = false; }
3094 		;
3095 
3096 /*
3097  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
3098  * used depends on the direction. (It really doesn't make sense to copy from
3099  * stdout. We silently correct the "typo".)		 - AY 9/94
3100  */
3101 copy_file_name:
3102 			Sconst									{ $$ = $1; }
3103 			| STDIN									{ $$ = NULL; }
3104 			| STDOUT								{ $$ = NULL; }
3105 		;
3106 
3107 copy_options: copy_opt_list							{ $$ = $1; }
3108 			| '(' copy_generic_opt_list ')'			{ $$ = $2; }
3109 		;
3110 
3111 /* old COPY option syntax */
3112 copy_opt_list:
3113 			copy_opt_list copy_opt_item				{ $$ = lappend($1, $2); }
3114 			| /* EMPTY */							{ $$ = NIL; }
3115 		;
3116 
3117 copy_opt_item:
3118 			BINARY
3119 				{
3120 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3121 				}
3122 			| OIDS
3123 				{
3124 					$$ = makeDefElem("oids", (Node *)makeInteger(true), @1);
3125 				}
3126 			| FREEZE
3127 				{
3128 					$$ = makeDefElem("freeze", (Node *)makeInteger(true), @1);
3129 				}
3130 			| DELIMITER opt_as Sconst
3131 				{
3132 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @1);
3133 				}
3134 			| NULL_P opt_as Sconst
3135 				{
3136 					$$ = makeDefElem("null", (Node *)makeString($3), @1);
3137 				}
3138 			| CSV
3139 				{
3140 					$$ = makeDefElem("format", (Node *)makeString("csv"), @1);
3141 				}
3142 			| HEADER_P
3143 				{
3144 					$$ = makeDefElem("header", (Node *)makeInteger(true), @1);
3145 				}
3146 			| QUOTE opt_as Sconst
3147 				{
3148 					$$ = makeDefElem("quote", (Node *)makeString($3), @1);
3149 				}
3150 			| ESCAPE opt_as Sconst
3151 				{
3152 					$$ = makeDefElem("escape", (Node *)makeString($3), @1);
3153 				}
3154 			| FORCE QUOTE columnList
3155 				{
3156 					$$ = makeDefElem("force_quote", (Node *)$3, @1);
3157 				}
3158 			| FORCE QUOTE '*'
3159 				{
3160 					$$ = makeDefElem("force_quote", (Node *)makeNode(A_Star), @1);
3161 				}
3162 			| FORCE NOT NULL_P columnList
3163 				{
3164 					$$ = makeDefElem("force_not_null", (Node *)$4, @1);
3165 				}
3166 			| FORCE NULL_P columnList
3167 				{
3168 					$$ = makeDefElem("force_null", (Node *)$3, @1);
3169 				}
3170 			| ENCODING Sconst
3171 				{
3172 					$$ = makeDefElem("encoding", (Node *)makeString($2), @1);
3173 				}
3174 		;
3175 
3176 /* The following exist for backward compatibility with very old versions */
3177 
3178 opt_binary:
3179 			BINARY
3180 				{
3181 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3182 				}
3183 			| /*EMPTY*/								{ $$ = NULL; }
3184 		;
3185 
3186 opt_oids:
3187 			WITH OIDS
3188 				{
3189 					$$ = makeDefElem("oids", (Node *)makeInteger(true), @1);
3190 				}
3191 			| /*EMPTY*/								{ $$ = NULL; }
3192 		;
3193 
3194 copy_delimiter:
3195 			opt_using DELIMITERS Sconst
3196 				{
3197 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @2);
3198 				}
3199 			| /*EMPTY*/								{ $$ = NULL; }
3200 		;
3201 
3202 opt_using:
3203 			USING									{}
3204 			| /*EMPTY*/								{}
3205 		;
3206 
3207 /* new COPY option syntax */
3208 copy_generic_opt_list:
3209 			copy_generic_opt_elem
3210 				{
3211 					$$ = list_make1($1);
3212 				}
3213 			| copy_generic_opt_list ',' copy_generic_opt_elem
3214 				{
3215 					$$ = lappend($1, $3);
3216 				}
3217 		;
3218 
3219 copy_generic_opt_elem:
3220 			ColLabel copy_generic_opt_arg
3221 				{
3222 					$$ = makeDefElem($1, $2, @1);
3223 				}
3224 		;
3225 
3226 copy_generic_opt_arg:
3227 			opt_boolean_or_string			{ $$ = (Node *) makeString($1); }
3228 			| NumericOnly					{ $$ = (Node *) $1; }
3229 			| '*'							{ $$ = (Node *) makeNode(A_Star); }
3230 			| '(' copy_generic_opt_arg_list ')'		{ $$ = (Node *) $2; }
3231 			| /* EMPTY */					{ $$ = NULL; }
3232 		;
3233 
3234 copy_generic_opt_arg_list:
3235 			  copy_generic_opt_arg_list_item
3236 				{
3237 					$$ = list_make1($1);
3238 				}
3239 			| copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
3240 				{
3241 					$$ = lappend($1, $3);
3242 				}
3243 		;
3244 
3245 /* beware of emitting non-string list elements here; see commands/define.c */
3246 copy_generic_opt_arg_list_item:
3247 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
3248 		;
3249 
3250 
3251 /*****************************************************************************
3252  *
3253  *		QUERY :
3254  *				CREATE TABLE relname
3255  *
3256  *****************************************************************************/
3257 
3258 CreateStmt:	CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
3259 			OptInherit OptPartitionSpec OptWith OnCommitOption OptTableSpace
3260 				{
3261 					CreateStmt *n = makeNode(CreateStmt);
3262 					$4->relpersistence = $2;
3263 					n->relation = $4;
3264 					n->tableElts = $6;
3265 					n->inhRelations = $8;
3266 					n->partspec = $9;
3267 					n->ofTypename = NULL;
3268 					n->constraints = NIL;
3269 					n->options = $10;
3270 					n->oncommit = $11;
3271 					n->tablespacename = $12;
3272 					n->if_not_exists = false;
3273 					$$ = (Node *)n;
3274 				}
3275 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
3276 			OptTableElementList ')' OptInherit OptPartitionSpec OptWith
3277 			OnCommitOption OptTableSpace
3278 				{
3279 					CreateStmt *n = makeNode(CreateStmt);
3280 					$7->relpersistence = $2;
3281 					n->relation = $7;
3282 					n->tableElts = $9;
3283 					n->inhRelations = $11;
3284 					n->partspec = $12;
3285 					n->ofTypename = NULL;
3286 					n->constraints = NIL;
3287 					n->options = $13;
3288 					n->oncommit = $14;
3289 					n->tablespacename = $15;
3290 					n->if_not_exists = true;
3291 					$$ = (Node *)n;
3292 				}
3293 		| CREATE OptTemp TABLE qualified_name OF any_name
3294 			OptTypedTableElementList OptPartitionSpec OptWith OnCommitOption
3295 			OptTableSpace
3296 				{
3297 					CreateStmt *n = makeNode(CreateStmt);
3298 					$4->relpersistence = $2;
3299 					n->relation = $4;
3300 					n->tableElts = $7;
3301 					n->inhRelations = NIL;
3302 					n->partspec = $8;
3303 					n->ofTypename = makeTypeNameFromNameList($6);
3304 					n->ofTypename->location = @6;
3305 					n->constraints = NIL;
3306 					n->options = $9;
3307 					n->oncommit = $10;
3308 					n->tablespacename = $11;
3309 					n->if_not_exists = false;
3310 					$$ = (Node *)n;
3311 				}
3312 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
3313 			OptTypedTableElementList OptPartitionSpec OptWith OnCommitOption
3314 			OptTableSpace
3315 				{
3316 					CreateStmt *n = makeNode(CreateStmt);
3317 					$7->relpersistence = $2;
3318 					n->relation = $7;
3319 					n->tableElts = $10;
3320 					n->inhRelations = NIL;
3321 					n->partspec = $11;
3322 					n->ofTypename = makeTypeNameFromNameList($9);
3323 					n->ofTypename->location = @9;
3324 					n->constraints = NIL;
3325 					n->options = $12;
3326 					n->oncommit = $13;
3327 					n->tablespacename = $14;
3328 					n->if_not_exists = true;
3329 					$$ = (Node *)n;
3330 				}
3331 		| CREATE OptTemp TABLE qualified_name PARTITION OF qualified_name
3332 			OptTypedTableElementList PartitionBoundSpec OptPartitionSpec OptWith
3333 			OnCommitOption OptTableSpace
3334 				{
3335 					CreateStmt *n = makeNode(CreateStmt);
3336 					$4->relpersistence = $2;
3337 					n->relation = $4;
3338 					n->tableElts = $8;
3339 					n->inhRelations = list_make1($7);
3340 					n->partbound = $9;
3341 					n->partspec = $10;
3342 					n->ofTypename = NULL;
3343 					n->constraints = NIL;
3344 					n->options = $11;
3345 					n->oncommit = $12;
3346 					n->tablespacename = $13;
3347 					n->if_not_exists = false;
3348 					$$ = (Node *)n;
3349 				}
3350 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name PARTITION OF
3351 			qualified_name OptTypedTableElementList PartitionBoundSpec OptPartitionSpec
3352 			OptWith OnCommitOption OptTableSpace
3353 				{
3354 					CreateStmt *n = makeNode(CreateStmt);
3355 					$7->relpersistence = $2;
3356 					n->relation = $7;
3357 					n->tableElts = $11;
3358 					n->inhRelations = list_make1($10);
3359 					n->partbound = $12;
3360 					n->partspec = $13;
3361 					n->ofTypename = NULL;
3362 					n->constraints = NIL;
3363 					n->options = $14;
3364 					n->oncommit = $15;
3365 					n->tablespacename = $16;
3366 					n->if_not_exists = true;
3367 					$$ = (Node *)n;
3368 				}
3369 		;
3370 
3371 /*
3372  * Redundancy here is needed to avoid shift/reduce conflicts,
3373  * since TEMP is not a reserved word.  See also OptTempTableName.
3374  *
3375  * NOTE: we accept both GLOBAL and LOCAL options.  They currently do nothing,
3376  * but future versions might consider GLOBAL to request SQL-spec-compliant
3377  * temp table behavior, so warn about that.  Since we have no modules the
3378  * LOCAL keyword is really meaningless; furthermore, some other products
3379  * implement LOCAL as meaning the same as our default temp table behavior,
3380  * so we'll probably continue to treat LOCAL as a noise word.
3381  */
3382 OptTemp:	TEMPORARY					{ $$ = RELPERSISTENCE_TEMP; }
3383 			| TEMP						{ $$ = RELPERSISTENCE_TEMP; }
3384 			| LOCAL TEMPORARY			{ $$ = RELPERSISTENCE_TEMP; }
3385 			| LOCAL TEMP				{ $$ = RELPERSISTENCE_TEMP; }
3386 			| GLOBAL TEMPORARY
3387 				{
3388 					ereport(WARNING,
3389 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3390 							 parser_errposition(@1)));
3391 					$$ = RELPERSISTENCE_TEMP;
3392 				}
3393 			| GLOBAL TEMP
3394 				{
3395 					ereport(WARNING,
3396 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3397 							 parser_errposition(@1)));
3398 					$$ = RELPERSISTENCE_TEMP;
3399 				}
3400 			| UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
3401 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
3402 		;
3403 
3404 OptTableElementList:
3405 			TableElementList					{ $$ = $1; }
3406 			| /*EMPTY*/							{ $$ = NIL; }
3407 		;
3408 
3409 OptTypedTableElementList:
3410 			'(' TypedTableElementList ')'		{ $$ = $2; }
3411 			| /*EMPTY*/							{ $$ = NIL; }
3412 		;
3413 
3414 TableElementList:
3415 			TableElement
3416 				{
3417 					$$ = list_make1($1);
3418 				}
3419 			| TableElementList ',' TableElement
3420 				{
3421 					$$ = lappend($1, $3);
3422 				}
3423 		;
3424 
3425 TypedTableElementList:
3426 			TypedTableElement
3427 				{
3428 					$$ = list_make1($1);
3429 				}
3430 			| TypedTableElementList ',' TypedTableElement
3431 				{
3432 					$$ = lappend($1, $3);
3433 				}
3434 		;
3435 
3436 TableElement:
3437 			columnDef							{ $$ = $1; }
3438 			| TableLikeClause					{ $$ = $1; }
3439 			| TableConstraint					{ $$ = $1; }
3440 		;
3441 
3442 TypedTableElement:
3443 			columnOptions						{ $$ = $1; }
3444 			| TableConstraint					{ $$ = $1; }
3445 		;
3446 
3447 columnDef:	ColId Typename create_generic_options ColQualList
3448 				{
3449 					ColumnDef *n = makeNode(ColumnDef);
3450 					n->colname = $1;
3451 					n->typeName = $2;
3452 					n->inhcount = 0;
3453 					n->is_local = true;
3454 					n->is_not_null = false;
3455 					n->is_from_type = false;
3456 					n->is_from_parent = false;
3457 					n->storage = 0;
3458 					n->raw_default = NULL;
3459 					n->cooked_default = NULL;
3460 					n->collOid = InvalidOid;
3461 					n->fdwoptions = $3;
3462 					SplitColQualList($4, &n->constraints, &n->collClause,
3463 									 yyscanner);
3464 					n->location = @1;
3465 					$$ = (Node *)n;
3466 				}
3467 		;
3468 
3469 columnOptions:	ColId ColQualList
3470 				{
3471 					ColumnDef *n = makeNode(ColumnDef);
3472 					n->colname = $1;
3473 					n->typeName = NULL;
3474 					n->inhcount = 0;
3475 					n->is_local = true;
3476 					n->is_not_null = false;
3477 					n->is_from_type = false;
3478 					n->is_from_parent = false;
3479 					n->storage = 0;
3480 					n->raw_default = NULL;
3481 					n->cooked_default = NULL;
3482 					n->collOid = InvalidOid;
3483 					SplitColQualList($2, &n->constraints, &n->collClause,
3484 									 yyscanner);
3485 					n->location = @1;
3486 					$$ = (Node *)n;
3487 				}
3488 				| ColId WITH OPTIONS ColQualList
3489 				{
3490 					ColumnDef *n = makeNode(ColumnDef);
3491 					n->colname = $1;
3492 					n->typeName = NULL;
3493 					n->inhcount = 0;
3494 					n->is_local = true;
3495 					n->is_not_null = false;
3496 					n->is_from_type = false;
3497 					n->is_from_parent = false;
3498 					n->storage = 0;
3499 					n->raw_default = NULL;
3500 					n->cooked_default = NULL;
3501 					n->collOid = InvalidOid;
3502 					SplitColQualList($4, &n->constraints, &n->collClause,
3503 									 yyscanner);
3504 					n->location = @1;
3505 					$$ = (Node *)n;
3506 				}
3507 		;
3508 
3509 ColQualList:
3510 			ColQualList ColConstraint				{ $$ = lappend($1, $2); }
3511 			| /*EMPTY*/								{ $$ = NIL; }
3512 		;
3513 
3514 ColConstraint:
3515 			CONSTRAINT name ColConstraintElem
3516 				{
3517 					Constraint *n = castNode(Constraint, $3);
3518 					n->conname = $2;
3519 					n->location = @1;
3520 					$$ = (Node *) n;
3521 				}
3522 			| ColConstraintElem						{ $$ = $1; }
3523 			| ConstraintAttr						{ $$ = $1; }
3524 			| COLLATE any_name
3525 				{
3526 					/*
3527 					 * Note: the CollateClause is momentarily included in
3528 					 * the list built by ColQualList, but we split it out
3529 					 * again in SplitColQualList.
3530 					 */
3531 					CollateClause *n = makeNode(CollateClause);
3532 					n->arg = NULL;
3533 					n->collname = $2;
3534 					n->location = @1;
3535 					$$ = (Node *) n;
3536 				}
3537 		;
3538 
3539 /* DEFAULT NULL is already the default for Postgres.
3540  * But define it here and carry it forward into the system
3541  * to make it explicit.
3542  * - thomas 1998-09-13
3543  *
3544  * WITH NULL and NULL are not SQL-standard syntax elements,
3545  * so leave them out. Use DEFAULT NULL to explicitly indicate
3546  * that a column may have that value. WITH NULL leads to
3547  * shift/reduce conflicts with WITH TIME ZONE anyway.
3548  * - thomas 1999-01-08
3549  *
3550  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
3551  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
3552  * or be part of a_expr NOT LIKE or similar constructs).
3553  */
3554 ColConstraintElem:
3555 			NOT NULL_P
3556 				{
3557 					Constraint *n = makeNode(Constraint);
3558 					n->contype = CONSTR_NOTNULL;
3559 					n->location = @1;
3560 					$$ = (Node *)n;
3561 				}
3562 			| NULL_P
3563 				{
3564 					Constraint *n = makeNode(Constraint);
3565 					n->contype = CONSTR_NULL;
3566 					n->location = @1;
3567 					$$ = (Node *)n;
3568 				}
3569 			| UNIQUE opt_definition OptConsTableSpace
3570 				{
3571 					Constraint *n = makeNode(Constraint);
3572 					n->contype = CONSTR_UNIQUE;
3573 					n->location = @1;
3574 					n->keys = NULL;
3575 					n->options = $2;
3576 					n->indexname = NULL;
3577 					n->indexspace = $3;
3578 					$$ = (Node *)n;
3579 				}
3580 			| PRIMARY KEY opt_definition OptConsTableSpace
3581 				{
3582 					Constraint *n = makeNode(Constraint);
3583 					n->contype = CONSTR_PRIMARY;
3584 					n->location = @1;
3585 					n->keys = NULL;
3586 					n->options = $3;
3587 					n->indexname = NULL;
3588 					n->indexspace = $4;
3589 					$$ = (Node *)n;
3590 				}
3591 			| CHECK '(' a_expr ')' opt_no_inherit
3592 				{
3593 					Constraint *n = makeNode(Constraint);
3594 					n->contype = CONSTR_CHECK;
3595 					n->location = @1;
3596 					n->is_no_inherit = $5;
3597 					n->raw_expr = $3;
3598 					n->cooked_expr = NULL;
3599 					n->skip_validation = false;
3600 					n->initially_valid = true;
3601 					$$ = (Node *)n;
3602 				}
3603 			| DEFAULT b_expr
3604 				{
3605 					Constraint *n = makeNode(Constraint);
3606 					n->contype = CONSTR_DEFAULT;
3607 					n->location = @1;
3608 					n->raw_expr = $2;
3609 					n->cooked_expr = NULL;
3610 					n->skip_validation = false;
3611 					n->initially_valid = true;
3612 					$$ = (Node *)n;
3613 				}
3614 			| GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
3615 				{
3616 					Constraint *n = makeNode(Constraint);
3617 					n->contype = CONSTR_IDENTITY;
3618 					n->generated_when = $2;
3619 					n->options = $5;
3620 					n->location = @1;
3621 					$$ = (Node *)n;
3622 				}
3623 			| REFERENCES qualified_name opt_column_list key_match key_actions
3624 				{
3625 					Constraint *n = makeNode(Constraint);
3626 					n->contype = CONSTR_FOREIGN;
3627 					n->location = @1;
3628 					n->pktable			= $2;
3629 					n->fk_attrs			= NIL;
3630 					n->pk_attrs			= $3;
3631 					n->fk_matchtype		= $4;
3632 					n->fk_upd_action	= (char) ($5 >> 8);
3633 					n->fk_del_action	= (char) ($5 & 0xFF);
3634 					n->skip_validation  = false;
3635 					n->initially_valid  = true;
3636 					$$ = (Node *)n;
3637 				}
3638 		;
3639 
3640 generated_when:
3641 			ALWAYS			{ $$ = ATTRIBUTE_IDENTITY_ALWAYS; }
3642 			| BY DEFAULT	{ $$ = ATTRIBUTE_IDENTITY_BY_DEFAULT; }
3643 		;
3644 
3645 /*
3646  * ConstraintAttr represents constraint attributes, which we parse as if
3647  * they were independent constraint clauses, in order to avoid shift/reduce
3648  * conflicts (since NOT might start either an independent NOT NULL clause
3649  * or an attribute).  parse_utilcmd.c is responsible for attaching the
3650  * attribute information to the preceding "real" constraint node, and for
3651  * complaining if attribute clauses appear in the wrong place or wrong
3652  * combinations.
3653  *
3654  * See also ConstraintAttributeSpec, which can be used in places where
3655  * there is no parsing conflict.  (Note: currently, NOT VALID and NO INHERIT
3656  * are allowed clauses in ConstraintAttributeSpec, but not here.  Someday we
3657  * might need to allow them here too, but for the moment it doesn't seem
3658  * useful in the statements that use ConstraintAttr.)
3659  */
3660 ConstraintAttr:
3661 			DEFERRABLE
3662 				{
3663 					Constraint *n = makeNode(Constraint);
3664 					n->contype = CONSTR_ATTR_DEFERRABLE;
3665 					n->location = @1;
3666 					$$ = (Node *)n;
3667 				}
3668 			| NOT DEFERRABLE
3669 				{
3670 					Constraint *n = makeNode(Constraint);
3671 					n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
3672 					n->location = @1;
3673 					$$ = (Node *)n;
3674 				}
3675 			| INITIALLY DEFERRED
3676 				{
3677 					Constraint *n = makeNode(Constraint);
3678 					n->contype = CONSTR_ATTR_DEFERRED;
3679 					n->location = @1;
3680 					$$ = (Node *)n;
3681 				}
3682 			| INITIALLY IMMEDIATE
3683 				{
3684 					Constraint *n = makeNode(Constraint);
3685 					n->contype = CONSTR_ATTR_IMMEDIATE;
3686 					n->location = @1;
3687 					$$ = (Node *)n;
3688 				}
3689 		;
3690 
3691 
3692 TableLikeClause:
3693 			LIKE qualified_name TableLikeOptionList
3694 				{
3695 					TableLikeClause *n = makeNode(TableLikeClause);
3696 					n->relation = $2;
3697 					n->options = $3;
3698 					$$ = (Node *)n;
3699 				}
3700 		;
3701 
3702 TableLikeOptionList:
3703 				TableLikeOptionList INCLUDING TableLikeOption	{ $$ = $1 | $3; }
3704 				| TableLikeOptionList EXCLUDING TableLikeOption	{ $$ = $1 & ~$3; }
3705 				| /* EMPTY */						{ $$ = 0; }
3706 		;
3707 
3708 TableLikeOption:
3709 				COMMENTS			{ $$ = CREATE_TABLE_LIKE_COMMENTS; }
3710 				| CONSTRAINTS		{ $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
3711 				| DEFAULTS			{ $$ = CREATE_TABLE_LIKE_DEFAULTS; }
3712 				| IDENTITY_P		{ $$ = CREATE_TABLE_LIKE_IDENTITY; }
3713 				| INDEXES			{ $$ = CREATE_TABLE_LIKE_INDEXES; }
3714 				| STATISTICS		{ $$ = CREATE_TABLE_LIKE_STATISTICS; }
3715 				| STORAGE			{ $$ = CREATE_TABLE_LIKE_STORAGE; }
3716 				| ALL				{ $$ = CREATE_TABLE_LIKE_ALL; }
3717 		;
3718 
3719 
3720 /* ConstraintElem specifies constraint syntax which is not embedded into
3721  *	a column definition. ColConstraintElem specifies the embedded form.
3722  * - thomas 1997-12-03
3723  */
3724 TableConstraint:
3725 			CONSTRAINT name ConstraintElem
3726 				{
3727 					Constraint *n = castNode(Constraint, $3);
3728 					n->conname = $2;
3729 					n->location = @1;
3730 					$$ = (Node *) n;
3731 				}
3732 			| ConstraintElem						{ $$ = $1; }
3733 		;
3734 
3735 ConstraintElem:
3736 			CHECK '(' a_expr ')' ConstraintAttributeSpec
3737 				{
3738 					Constraint *n = makeNode(Constraint);
3739 					n->contype = CONSTR_CHECK;
3740 					n->location = @1;
3741 					n->raw_expr = $3;
3742 					n->cooked_expr = NULL;
3743 					processCASbits($5, @5, "CHECK",
3744 								   NULL, NULL, &n->skip_validation,
3745 								   &n->is_no_inherit, yyscanner);
3746 					n->initially_valid = !n->skip_validation;
3747 					$$ = (Node *)n;
3748 				}
3749 			| UNIQUE '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3750 				ConstraintAttributeSpec
3751 				{
3752 					Constraint *n = makeNode(Constraint);
3753 					n->contype = CONSTR_UNIQUE;
3754 					n->location = @1;
3755 					n->keys = $3;
3756 					n->including = $5;
3757 					n->options = $6;
3758 					n->indexname = NULL;
3759 					n->indexspace = $7;
3760 					processCASbits($8, @8, "UNIQUE",
3761 								   &n->deferrable, &n->initdeferred, NULL,
3762 								   NULL, yyscanner);
3763 					$$ = (Node *)n;
3764 				}
3765 			| UNIQUE ExistingIndex ConstraintAttributeSpec
3766 				{
3767 					Constraint *n = makeNode(Constraint);
3768 					n->contype = CONSTR_UNIQUE;
3769 					n->location = @1;
3770 					n->keys = NIL;
3771 					n->including = NIL;
3772 					n->options = NIL;
3773 					n->indexname = $2;
3774 					n->indexspace = NULL;
3775 					processCASbits($3, @3, "UNIQUE",
3776 								   &n->deferrable, &n->initdeferred, NULL,
3777 								   NULL, yyscanner);
3778 					$$ = (Node *)n;
3779 				}
3780 			| PRIMARY KEY '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3781 				ConstraintAttributeSpec
3782 				{
3783 					Constraint *n = makeNode(Constraint);
3784 					n->contype = CONSTR_PRIMARY;
3785 					n->location = @1;
3786 					n->keys = $4;
3787 					n->including = $6;
3788 					n->options = $7;
3789 					n->indexname = NULL;
3790 					n->indexspace = $8;
3791 					processCASbits($9, @9, "PRIMARY KEY",
3792 								   &n->deferrable, &n->initdeferred, NULL,
3793 								   NULL, yyscanner);
3794 					$$ = (Node *)n;
3795 				}
3796 			| PRIMARY KEY ExistingIndex ConstraintAttributeSpec
3797 				{
3798 					Constraint *n = makeNode(Constraint);
3799 					n->contype = CONSTR_PRIMARY;
3800 					n->location = @1;
3801 					n->keys = NIL;
3802 					n->including = NIL;
3803 					n->options = NIL;
3804 					n->indexname = $3;
3805 					n->indexspace = NULL;
3806 					processCASbits($4, @4, "PRIMARY KEY",
3807 								   &n->deferrable, &n->initdeferred, NULL,
3808 								   NULL, yyscanner);
3809 					$$ = (Node *)n;
3810 				}
3811 			| EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
3812 				opt_c_include opt_definition OptConsTableSpace  ExclusionWhereClause
3813 				ConstraintAttributeSpec
3814 				{
3815 					Constraint *n = makeNode(Constraint);
3816 					n->contype = CONSTR_EXCLUSION;
3817 					n->location = @1;
3818 					n->access_method	= $2;
3819 					n->exclusions		= $4;
3820 					n->including		= $6;
3821 					n->options			= $7;
3822 					n->indexname		= NULL;
3823 					n->indexspace		= $8;
3824 					n->where_clause		= $9;
3825 					processCASbits($10, @10, "EXCLUDE",
3826 								   &n->deferrable, &n->initdeferred, NULL,
3827 								   NULL, yyscanner);
3828 					$$ = (Node *)n;
3829 				}
3830 			| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
3831 				opt_column_list key_match key_actions ConstraintAttributeSpec
3832 				{
3833 					Constraint *n = makeNode(Constraint);
3834 					n->contype = CONSTR_FOREIGN;
3835 					n->location = @1;
3836 					n->pktable			= $7;
3837 					n->fk_attrs			= $4;
3838 					n->pk_attrs			= $8;
3839 					n->fk_matchtype		= $9;
3840 					n->fk_upd_action	= (char) ($10 >> 8);
3841 					n->fk_del_action	= (char) ($10 & 0xFF);
3842 					processCASbits($11, @11, "FOREIGN KEY",
3843 								   &n->deferrable, &n->initdeferred,
3844 								   &n->skip_validation, NULL,
3845 								   yyscanner);
3846 					n->initially_valid = !n->skip_validation;
3847 					$$ = (Node *)n;
3848 				}
3849 		;
3850 
3851 opt_no_inherit:	NO INHERIT							{  $$ = true; }
3852 			| /* EMPTY */							{  $$ = false; }
3853 		;
3854 
3855 opt_column_list:
3856 			'(' columnList ')'						{ $$ = $2; }
3857 			| /*EMPTY*/								{ $$ = NIL; }
3858 		;
3859 
3860 columnList:
3861 			columnElem								{ $$ = list_make1($1); }
3862 			| columnList ',' columnElem				{ $$ = lappend($1, $3); }
3863 		;
3864 
3865 columnElem: ColId
3866 				{
3867 					$$ = (Node *) makeString($1);
3868 				}
3869 		;
3870 
3871 opt_c_include:	INCLUDE '(' columnList ')'			{ $$ = $3; }
3872 			 |		/* EMPTY */						{ $$ = NIL; }
3873 		;
3874 
3875 key_match:  MATCH FULL
3876 			{
3877 				$$ = FKCONSTR_MATCH_FULL;
3878 			}
3879 		| MATCH PARTIAL
3880 			{
3881 				ereport(ERROR,
3882 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3883 						 errmsg("MATCH PARTIAL not yet implemented"),
3884 						 parser_errposition(@1)));
3885 				$$ = FKCONSTR_MATCH_PARTIAL;
3886 			}
3887 		| MATCH SIMPLE
3888 			{
3889 				$$ = FKCONSTR_MATCH_SIMPLE;
3890 			}
3891 		| /*EMPTY*/
3892 			{
3893 				$$ = FKCONSTR_MATCH_SIMPLE;
3894 			}
3895 		;
3896 
3897 ExclusionConstraintList:
3898 			ExclusionConstraintElem					{ $$ = list_make1($1); }
3899 			| ExclusionConstraintList ',' ExclusionConstraintElem
3900 													{ $$ = lappend($1, $3); }
3901 		;
3902 
3903 ExclusionConstraintElem: index_elem WITH any_operator
3904 			{
3905 				$$ = list_make2($1, $3);
3906 			}
3907 			/* allow OPERATOR() decoration for the benefit of ruleutils.c */
3908 			| index_elem WITH OPERATOR '(' any_operator ')'
3909 			{
3910 				$$ = list_make2($1, $5);
3911 			}
3912 		;
3913 
3914 ExclusionWhereClause:
3915 			WHERE '(' a_expr ')'					{ $$ = $3; }
3916 			| /*EMPTY*/								{ $$ = NULL; }
3917 		;
3918 
3919 /*
3920  * We combine the update and delete actions into one value temporarily
3921  * for simplicity of parsing, and then break them down again in the
3922  * calling production.  update is in the left 8 bits, delete in the right.
3923  * Note that NOACTION is the default.
3924  */
3925 key_actions:
3926 			key_update
3927 				{ $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3928 			| key_delete
3929 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
3930 			| key_update key_delete
3931 				{ $$ = ($1 << 8) | ($2 & 0xFF); }
3932 			| key_delete key_update
3933 				{ $$ = ($2 << 8) | ($1 & 0xFF); }
3934 			| /*EMPTY*/
3935 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3936 		;
3937 
3938 key_update: ON UPDATE key_action		{ $$ = $3; }
3939 		;
3940 
3941 key_delete: ON DELETE_P key_action		{ $$ = $3; }
3942 		;
3943 
3944 key_action:
3945 			NO ACTION					{ $$ = FKCONSTR_ACTION_NOACTION; }
3946 			| RESTRICT					{ $$ = FKCONSTR_ACTION_RESTRICT; }
3947 			| CASCADE					{ $$ = FKCONSTR_ACTION_CASCADE; }
3948 			| SET NULL_P				{ $$ = FKCONSTR_ACTION_SETNULL; }
3949 			| SET DEFAULT				{ $$ = FKCONSTR_ACTION_SETDEFAULT; }
3950 		;
3951 
3952 OptInherit: INHERITS '(' qualified_name_list ')'	{ $$ = $3; }
3953 			| /*EMPTY*/								{ $$ = NIL; }
3954 		;
3955 
3956 /* Optional partition key specification */
3957 OptPartitionSpec: PartitionSpec	{ $$ = $1; }
3958 			| /*EMPTY*/			{ $$ = NULL; }
3959 		;
3960 
3961 PartitionSpec: PARTITION BY part_strategy '(' part_params ')'
3962 				{
3963 					PartitionSpec *n = makeNode(PartitionSpec);
3964 
3965 					n->strategy = $3;
3966 					n->partParams = $5;
3967 					n->location = @1;
3968 
3969 					$$ = n;
3970 				}
3971 		;
3972 
3973 part_strategy:	IDENT					{ $$ = $1; }
3974 				| unreserved_keyword	{ $$ = pstrdup($1); }
3975 		;
3976 
3977 part_params:	part_elem						{ $$ = list_make1($1); }
3978 			| part_params ',' part_elem			{ $$ = lappend($1, $3); }
3979 		;
3980 
3981 part_elem: ColId opt_collate opt_class
3982 				{
3983 					PartitionElem *n = makeNode(PartitionElem);
3984 
3985 					n->name = $1;
3986 					n->expr = NULL;
3987 					n->collation = $2;
3988 					n->opclass = $3;
3989 					n->location = @1;
3990 					$$ = n;
3991 				}
3992 			| func_expr_windowless opt_collate opt_class
3993 				{
3994 					PartitionElem *n = makeNode(PartitionElem);
3995 
3996 					n->name = NULL;
3997 					n->expr = $1;
3998 					n->collation = $2;
3999 					n->opclass = $3;
4000 					n->location = @1;
4001 					$$ = n;
4002 				}
4003 			| '(' a_expr ')' opt_collate opt_class
4004 				{
4005 					PartitionElem *n = makeNode(PartitionElem);
4006 
4007 					n->name = NULL;
4008 					n->expr = $2;
4009 					n->collation = $4;
4010 					n->opclass = $5;
4011 					n->location = @1;
4012 					$$ = n;
4013 				}
4014 		;
4015 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
4016 OptWith:
4017 			WITH reloptions				{ $$ = $2; }
4018 			| WITH OIDS					{ $$ = list_make1(makeDefElem("oids", (Node *) makeInteger(true), @1)); }
4019 			| WITHOUT OIDS				{ $$ = list_make1(makeDefElem("oids", (Node *) makeInteger(false), @1)); }
4020 			| /*EMPTY*/					{ $$ = NIL; }
4021 		;
4022 
4023 OnCommitOption:  ON COMMIT DROP				{ $$ = ONCOMMIT_DROP; }
4024 			| ON COMMIT DELETE_P ROWS		{ $$ = ONCOMMIT_DELETE_ROWS; }
4025 			| ON COMMIT PRESERVE ROWS		{ $$ = ONCOMMIT_PRESERVE_ROWS; }
4026 			| /*EMPTY*/						{ $$ = ONCOMMIT_NOOP; }
4027 		;
4028 
4029 OptTableSpace:   TABLESPACE name					{ $$ = $2; }
4030 			| /*EMPTY*/								{ $$ = NULL; }
4031 		;
4032 
4033 OptConsTableSpace:   USING INDEX TABLESPACE name	{ $$ = $4; }
4034 			| /*EMPTY*/								{ $$ = NULL; }
4035 		;
4036 
4037 ExistingIndex:   USING INDEX index_name				{ $$ = $3; }
4038 		;
4039 
4040 /*****************************************************************************
4041  *
4042  *		QUERY :
4043  *				CREATE STATISTICS [IF NOT EXISTS] stats_name [(stat types)]
4044  *					ON expression-list FROM from_list
4045  *
4046  * Note: the expectation here is that the clauses after ON are a subset of
4047  * SELECT syntax, allowing for expressions and joined tables, and probably
4048  * someday a WHERE clause.  Much less than that is currently implemented,
4049  * but the grammar accepts it and then we'll throw FEATURE_NOT_SUPPORTED
4050  * errors as necessary at execution.
4051  *
4052  *****************************************************************************/
4053 
4054 CreateStatsStmt:
4055 			CREATE STATISTICS any_name
4056 			opt_name_list ON expr_list FROM from_list
4057 				{
4058 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
4059 					n->defnames = $3;
4060 					n->stat_types = $4;
4061 					n->exprs = $6;
4062 					n->relations = $8;
4063 					n->stxcomment = NULL;
4064 					n->if_not_exists = false;
4065 					$$ = (Node *)n;
4066 				}
4067 			| CREATE STATISTICS IF_P NOT EXISTS any_name
4068 			opt_name_list ON expr_list FROM from_list
4069 				{
4070 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
4071 					n->defnames = $6;
4072 					n->stat_types = $7;
4073 					n->exprs = $9;
4074 					n->relations = $11;
4075 					n->stxcomment = NULL;
4076 					n->if_not_exists = true;
4077 					$$ = (Node *)n;
4078 				}
4079 			;
4080 
4081 /*****************************************************************************
4082  *
4083  *		QUERY :
4084  *				CREATE TABLE relname AS SelectStmt [ WITH [NO] DATA ]
4085  *
4086  *
4087  * Note: SELECT ... INTO is a now-deprecated alternative for this.
4088  *
4089  *****************************************************************************/
4090 
4091 CreateAsStmt:
4092 		CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
4093 				{
4094 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4095 					ctas->query = $6;
4096 					ctas->into = $4;
4097 					ctas->relkind = OBJECT_TABLE;
4098 					ctas->is_select_into = false;
4099 					ctas->if_not_exists = false;
4100 					/* cram additional flags into the IntoClause */
4101 					$4->rel->relpersistence = $2;
4102 					$4->skipData = !($7);
4103 					$$ = (Node *) ctas;
4104 				}
4105 		| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS SelectStmt opt_with_data
4106 				{
4107 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4108 					ctas->query = $9;
4109 					ctas->into = $7;
4110 					ctas->relkind = OBJECT_TABLE;
4111 					ctas->is_select_into = false;
4112 					ctas->if_not_exists = true;
4113 					/* cram additional flags into the IntoClause */
4114 					$7->rel->relpersistence = $2;
4115 					$7->skipData = !($10);
4116 					$$ = (Node *) ctas;
4117 				}
4118 		;
4119 
4120 create_as_target:
4121 			qualified_name opt_column_list OptWith OnCommitOption OptTableSpace
4122 				{
4123 					$$ = makeNode(IntoClause);
4124 					$$->rel = $1;
4125 					$$->colNames = $2;
4126 					$$->options = $3;
4127 					$$->onCommit = $4;
4128 					$$->tableSpaceName = $5;
4129 					$$->viewQuery = NULL;
4130 					$$->skipData = false;		/* might get changed later */
4131 				}
4132 		;
4133 
4134 opt_with_data:
4135 			WITH DATA_P								{ $$ = true; }
4136 			| WITH NO DATA_P						{ $$ = false; }
4137 			| /*EMPTY*/								{ $$ = true; }
4138 		;
4139 
4140 
4141 /*****************************************************************************
4142  *
4143  *		QUERY :
4144  *				CREATE MATERIALIZED VIEW relname AS SelectStmt
4145  *
4146  *****************************************************************************/
4147 
4148 CreateMatViewStmt:
4149 		CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data
4150 				{
4151 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4152 					ctas->query = $7;
4153 					ctas->into = $5;
4154 					ctas->relkind = OBJECT_MATVIEW;
4155 					ctas->is_select_into = false;
4156 					ctas->if_not_exists = false;
4157 					/* cram additional flags into the IntoClause */
4158 					$5->rel->relpersistence = $2;
4159 					$5->skipData = !($8);
4160 					$$ = (Node *) ctas;
4161 				}
4162 		| CREATE OptNoLog MATERIALIZED VIEW IF_P NOT EXISTS create_mv_target AS SelectStmt opt_with_data
4163 				{
4164 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4165 					ctas->query = $10;
4166 					ctas->into = $8;
4167 					ctas->relkind = OBJECT_MATVIEW;
4168 					ctas->is_select_into = false;
4169 					ctas->if_not_exists = true;
4170 					/* cram additional flags into the IntoClause */
4171 					$8->rel->relpersistence = $2;
4172 					$8->skipData = !($11);
4173 					$$ = (Node *) ctas;
4174 				}
4175 		;
4176 
4177 create_mv_target:
4178 			qualified_name opt_column_list opt_reloptions OptTableSpace
4179 				{
4180 					$$ = makeNode(IntoClause);
4181 					$$->rel = $1;
4182 					$$->colNames = $2;
4183 					$$->options = $3;
4184 					$$->onCommit = ONCOMMIT_NOOP;
4185 					$$->tableSpaceName = $4;
4186 					$$->viewQuery = NULL;		/* filled at analysis time */
4187 					$$->skipData = false;		/* might get changed later */
4188 				}
4189 		;
4190 
4191 OptNoLog:	UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
4192 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
4193 		;
4194 
4195 
4196 /*****************************************************************************
4197  *
4198  *		QUERY :
4199  *				REFRESH MATERIALIZED VIEW qualified_name
4200  *
4201  *****************************************************************************/
4202 
4203 RefreshMatViewStmt:
4204 			REFRESH MATERIALIZED VIEW opt_concurrently qualified_name opt_with_data
4205 				{
4206 					RefreshMatViewStmt *n = makeNode(RefreshMatViewStmt);
4207 					n->concurrent = $4;
4208 					n->relation = $5;
4209 					n->skipData = !($6);
4210 					$$ = (Node *) n;
4211 				}
4212 		;
4213 
4214 
4215 /*****************************************************************************
4216  *
4217  *		QUERY :
4218  *				CREATE SEQUENCE seqname
4219  *				ALTER SEQUENCE seqname
4220  *
4221  *****************************************************************************/
4222 
4223 CreateSeqStmt:
4224 			CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
4225 				{
4226 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4227 					$4->relpersistence = $2;
4228 					n->sequence = $4;
4229 					n->options = $5;
4230 					n->ownerId = InvalidOid;
4231 					n->if_not_exists = false;
4232 					$$ = (Node *)n;
4233 				}
4234 			| CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
4235 				{
4236 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4237 					$7->relpersistence = $2;
4238 					n->sequence = $7;
4239 					n->options = $8;
4240 					n->ownerId = InvalidOid;
4241 					n->if_not_exists = true;
4242 					$$ = (Node *)n;
4243 				}
4244 		;
4245 
4246 AlterSeqStmt:
4247 			ALTER SEQUENCE qualified_name SeqOptList
4248 				{
4249 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4250 					n->sequence = $3;
4251 					n->options = $4;
4252 					n->missing_ok = false;
4253 					$$ = (Node *)n;
4254 				}
4255 			| ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
4256 				{
4257 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4258 					n->sequence = $5;
4259 					n->options = $6;
4260 					n->missing_ok = true;
4261 					$$ = (Node *)n;
4262 				}
4263 
4264 		;
4265 
4266 OptSeqOptList: SeqOptList							{ $$ = $1; }
4267 			| /*EMPTY*/								{ $$ = NIL; }
4268 		;
4269 
4270 OptParenthesizedSeqOptList: '(' SeqOptList ')'		{ $$ = $2; }
4271 			| /*EMPTY*/								{ $$ = NIL; }
4272 		;
4273 
4274 SeqOptList: SeqOptElem								{ $$ = list_make1($1); }
4275 			| SeqOptList SeqOptElem					{ $$ = lappend($1, $2); }
4276 		;
4277 
4278 SeqOptElem: AS SimpleTypename
4279 				{
4280 					$$ = makeDefElem("as", (Node *)$2, @1);
4281 				}
4282 			| CACHE NumericOnly
4283 				{
4284 					$$ = makeDefElem("cache", (Node *)$2, @1);
4285 				}
4286 			| CYCLE
4287 				{
4288 					$$ = makeDefElem("cycle", (Node *)makeInteger(true), @1);
4289 				}
4290 			| NO CYCLE
4291 				{
4292 					$$ = makeDefElem("cycle", (Node *)makeInteger(false), @1);
4293 				}
4294 			| INCREMENT opt_by NumericOnly
4295 				{
4296 					$$ = makeDefElem("increment", (Node *)$3, @1);
4297 				}
4298 			| MAXVALUE NumericOnly
4299 				{
4300 					$$ = makeDefElem("maxvalue", (Node *)$2, @1);
4301 				}
4302 			| MINVALUE NumericOnly
4303 				{
4304 					$$ = makeDefElem("minvalue", (Node *)$2, @1);
4305 				}
4306 			| NO MAXVALUE
4307 				{
4308 					$$ = makeDefElem("maxvalue", NULL, @1);
4309 				}
4310 			| NO MINVALUE
4311 				{
4312 					$$ = makeDefElem("minvalue", NULL, @1);
4313 				}
4314 			| OWNED BY any_name
4315 				{
4316 					$$ = makeDefElem("owned_by", (Node *)$3, @1);
4317 				}
4318 			| SEQUENCE NAME_P any_name
4319 				{
4320 					/* not documented, only used by pg_dump */
4321 					$$ = makeDefElem("sequence_name", (Node *)$3, @1);
4322 				}
4323 			| START opt_with NumericOnly
4324 				{
4325 					$$ = makeDefElem("start", (Node *)$3, @1);
4326 				}
4327 			| RESTART
4328 				{
4329 					$$ = makeDefElem("restart", NULL, @1);
4330 				}
4331 			| RESTART opt_with NumericOnly
4332 				{
4333 					$$ = makeDefElem("restart", (Node *)$3, @1);
4334 				}
4335 		;
4336 
4337 opt_by:		BY				{}
4338 			| /* empty */	{}
4339 	  ;
4340 
4341 NumericOnly:
4342 			FCONST								{ $$ = makeFloat($1); }
4343 			| '+' FCONST						{ $$ = makeFloat($2); }
4344 			| '-' FCONST
4345 				{
4346 					$$ = makeFloat($2);
4347 					doNegateFloat($$);
4348 				}
4349 			| SignedIconst						{ $$ = makeInteger($1); }
4350 		;
4351 
4352 NumericOnly_list:	NumericOnly						{ $$ = list_make1($1); }
4353 				| NumericOnly_list ',' NumericOnly	{ $$ = lappend($1, $3); }
4354 		;
4355 
4356 /*****************************************************************************
4357  *
4358  *		QUERIES :
4359  *				CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
4360  *				DROP [PROCEDURAL] LANGUAGE ...
4361  *
4362  *****************************************************************************/
4363 
4364 CreatePLangStmt:
4365 			CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4366 			{
4367 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4368 				n->replace = $2;
4369 				n->plname = $6;
4370 				/* parameters are all to be supplied by system */
4371 				n->plhandler = NIL;
4372 				n->plinline = NIL;
4373 				n->plvalidator = NIL;
4374 				n->pltrusted = false;
4375 				$$ = (Node *)n;
4376 			}
4377 			| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4378 			  HANDLER handler_name opt_inline_handler opt_validator
4379 			{
4380 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4381 				n->replace = $2;
4382 				n->plname = $6;
4383 				n->plhandler = $8;
4384 				n->plinline = $9;
4385 				n->plvalidator = $10;
4386 				n->pltrusted = $3;
4387 				$$ = (Node *)n;
4388 			}
4389 		;
4390 
4391 opt_trusted:
4392 			TRUSTED									{ $$ = true; }
4393 			| /*EMPTY*/								{ $$ = false; }
4394 		;
4395 
4396 /* This ought to be just func_name, but that causes reduce/reduce conflicts
4397  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
4398  * Work around by using simple names, instead.
4399  */
4400 handler_name:
4401 			name						{ $$ = list_make1(makeString($1)); }
4402 			| name attrs				{ $$ = lcons(makeString($1), $2); }
4403 		;
4404 
4405 opt_inline_handler:
4406 			INLINE_P handler_name					{ $$ = $2; }
4407 			| /*EMPTY*/								{ $$ = NIL; }
4408 		;
4409 
4410 validator_clause:
4411 			VALIDATOR handler_name					{ $$ = $2; }
4412 			| NO VALIDATOR							{ $$ = NIL; }
4413 		;
4414 
4415 opt_validator:
4416 			validator_clause						{ $$ = $1; }
4417 			| /*EMPTY*/								{ $$ = NIL; }
4418 		;
4419 
4420 DropPLangStmt:
4421 			DROP opt_procedural LANGUAGE NonReservedWord_or_Sconst opt_drop_behavior
4422 				{
4423 					DropStmt *n = makeNode(DropStmt);
4424 					n->removeType = OBJECT_LANGUAGE;
4425 					n->objects = list_make1(makeString($4));
4426 					n->behavior = $5;
4427 					n->missing_ok = false;
4428 					n->concurrent = false;
4429 					$$ = (Node *)n;
4430 				}
4431 			| DROP opt_procedural LANGUAGE IF_P EXISTS NonReservedWord_or_Sconst opt_drop_behavior
4432 				{
4433 					DropStmt *n = makeNode(DropStmt);
4434 					n->removeType = OBJECT_LANGUAGE;
4435 					n->objects = list_make1(makeString($6));
4436 					n->behavior = $7;
4437 					n->missing_ok = true;
4438 					n->concurrent = false;
4439 					$$ = (Node *)n;
4440 				}
4441 		;
4442 
4443 opt_procedural:
4444 			PROCEDURAL								{}
4445 			| /*EMPTY*/								{}
4446 		;
4447 
4448 /*****************************************************************************
4449  *
4450  *		QUERY:
4451  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
4452  *
4453  *****************************************************************************/
4454 
4455 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst opt_reloptions
4456 				{
4457 					CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
4458 					n->tablespacename = $3;
4459 					n->owner = $4;
4460 					n->location = $6;
4461 					n->options = $7;
4462 					$$ = (Node *) n;
4463 				}
4464 		;
4465 
4466 OptTableSpaceOwner: OWNER RoleSpec		{ $$ = $2; }
4467 			| /*EMPTY */				{ $$ = NULL; }
4468 		;
4469 
4470 /*****************************************************************************
4471  *
4472  *		QUERY :
4473  *				DROP TABLESPACE <tablespace>
4474  *
4475  *		No need for drop behaviour as we cannot implement dependencies for
4476  *		objects in other databases; we can only support RESTRICT.
4477  *
4478  ****************************************************************************/
4479 
4480 DropTableSpaceStmt: DROP TABLESPACE name
4481 				{
4482 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4483 					n->tablespacename = $3;
4484 					n->missing_ok = false;
4485 					$$ = (Node *) n;
4486 				}
4487 				|  DROP TABLESPACE IF_P EXISTS name
4488 				{
4489 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4490 					n->tablespacename = $5;
4491 					n->missing_ok = true;
4492 					$$ = (Node *) n;
4493 				}
4494 		;
4495 
4496 /*****************************************************************************
4497  *
4498  *		QUERY:
4499  *             CREATE EXTENSION extension
4500  *             [ WITH ] [ SCHEMA schema ] [ VERSION version ] [ FROM oldversion ]
4501  *
4502  *****************************************************************************/
4503 
4504 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
4505 				{
4506 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4507 					n->extname = $3;
4508 					n->if_not_exists = false;
4509 					n->options = $5;
4510 					$$ = (Node *) n;
4511 				}
4512 				| CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
4513 				{
4514 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4515 					n->extname = $6;
4516 					n->if_not_exists = true;
4517 					n->options = $8;
4518 					$$ = (Node *) n;
4519 				}
4520 		;
4521 
4522 create_extension_opt_list:
4523 			create_extension_opt_list create_extension_opt_item
4524 				{ $$ = lappend($1, $2); }
4525 			| /* EMPTY */
4526 				{ $$ = NIL; }
4527 		;
4528 
4529 create_extension_opt_item:
4530 			SCHEMA name
4531 				{
4532 					$$ = makeDefElem("schema", (Node *)makeString($2), @1);
4533 				}
4534 			| VERSION_P NonReservedWord_or_Sconst
4535 				{
4536 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4537 				}
4538 			| FROM NonReservedWord_or_Sconst
4539 				{
4540 					$$ = makeDefElem("old_version", (Node *)makeString($2), @1);
4541 				}
4542 			| CASCADE
4543 				{
4544 					$$ = makeDefElem("cascade", (Node *)makeInteger(true), @1);
4545 				}
4546 		;
4547 
4548 /*****************************************************************************
4549  *
4550  * ALTER EXTENSION name UPDATE [ TO version ]
4551  *
4552  *****************************************************************************/
4553 
4554 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
4555 				{
4556 					AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
4557 					n->extname = $3;
4558 					n->options = $5;
4559 					$$ = (Node *) n;
4560 				}
4561 		;
4562 
4563 alter_extension_opt_list:
4564 			alter_extension_opt_list alter_extension_opt_item
4565 				{ $$ = lappend($1, $2); }
4566 			| /* EMPTY */
4567 				{ $$ = NIL; }
4568 		;
4569 
4570 alter_extension_opt_item:
4571 			TO NonReservedWord_or_Sconst
4572 				{
4573 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4574 				}
4575 		;
4576 
4577 /*****************************************************************************
4578  *
4579  * ALTER EXTENSION name ADD/DROP object-identifier
4580  *
4581  *****************************************************************************/
4582 
4583 AlterExtensionContentsStmt:
4584 			ALTER EXTENSION name add_drop ACCESS METHOD name
4585 				{
4586 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4587 					n->extname = $3;
4588 					n->action = $4;
4589 					n->objtype = OBJECT_ACCESS_METHOD;
4590 					n->object = (Node *) makeString($7);
4591 					$$ = (Node *)n;
4592 				}
4593 			| ALTER EXTENSION name add_drop AGGREGATE aggregate_with_argtypes
4594 				{
4595 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4596 					n->extname = $3;
4597 					n->action = $4;
4598 					n->objtype = OBJECT_AGGREGATE;
4599 					n->object = (Node *) $6;
4600 					$$ = (Node *)n;
4601 				}
4602 			| ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
4603 				{
4604 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4605 					n->extname = $3;
4606 					n->action = $4;
4607 					n->objtype = OBJECT_CAST;
4608 					n->object = (Node *) list_make2($7, $9);
4609 					$$ = (Node *) n;
4610 				}
4611 			| ALTER EXTENSION name add_drop COLLATION any_name
4612 				{
4613 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4614 					n->extname = $3;
4615 					n->action = $4;
4616 					n->objtype = OBJECT_COLLATION;
4617 					n->object = (Node *) $6;
4618 					$$ = (Node *)n;
4619 				}
4620 			| ALTER EXTENSION name add_drop CONVERSION_P any_name
4621 				{
4622 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4623 					n->extname = $3;
4624 					n->action = $4;
4625 					n->objtype = OBJECT_CONVERSION;
4626 					n->object = (Node *) $6;
4627 					$$ = (Node *)n;
4628 				}
4629 			| ALTER EXTENSION name add_drop DOMAIN_P Typename
4630 				{
4631 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4632 					n->extname = $3;
4633 					n->action = $4;
4634 					n->objtype = OBJECT_DOMAIN;
4635 					n->object = (Node *) $6;
4636 					$$ = (Node *)n;
4637 				}
4638 			| ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
4639 				{
4640 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4641 					n->extname = $3;
4642 					n->action = $4;
4643 					n->objtype = OBJECT_FUNCTION;
4644 					n->object = (Node *) $6;
4645 					$$ = (Node *)n;
4646 				}
4647 			| ALTER EXTENSION name add_drop opt_procedural LANGUAGE name
4648 				{
4649 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4650 					n->extname = $3;
4651 					n->action = $4;
4652 					n->objtype = OBJECT_LANGUAGE;
4653 					n->object = (Node *) makeString($7);
4654 					$$ = (Node *)n;
4655 				}
4656 			| ALTER EXTENSION name add_drop OPERATOR operator_with_argtypes
4657 				{
4658 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4659 					n->extname = $3;
4660 					n->action = $4;
4661 					n->objtype = OBJECT_OPERATOR;
4662 					n->object = (Node *) $6;
4663 					$$ = (Node *)n;
4664 				}
4665 			| ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING access_method
4666 				{
4667 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4668 					n->extname = $3;
4669 					n->action = $4;
4670 					n->objtype = OBJECT_OPCLASS;
4671 					n->object = (Node *) lcons(makeString($9), $7);
4672 					$$ = (Node *)n;
4673 				}
4674 			| ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING access_method
4675 				{
4676 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4677 					n->extname = $3;
4678 					n->action = $4;
4679 					n->objtype = OBJECT_OPFAMILY;
4680 					n->object = (Node *) lcons(makeString($9), $7);
4681 					$$ = (Node *)n;
4682 				}
4683 			| ALTER EXTENSION name add_drop PROCEDURE function_with_argtypes
4684 				{
4685 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4686 					n->extname = $3;
4687 					n->action = $4;
4688 					n->objtype = OBJECT_PROCEDURE;
4689 					n->object = (Node *) $6;
4690 					$$ = (Node *)n;
4691 				}
4692 			| ALTER EXTENSION name add_drop ROUTINE function_with_argtypes
4693 				{
4694 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4695 					n->extname = $3;
4696 					n->action = $4;
4697 					n->objtype = OBJECT_ROUTINE;
4698 					n->object = (Node *) $6;
4699 					$$ = (Node *)n;
4700 				}
4701 			| ALTER EXTENSION name add_drop SCHEMA name
4702 				{
4703 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4704 					n->extname = $3;
4705 					n->action = $4;
4706 					n->objtype = OBJECT_SCHEMA;
4707 					n->object = (Node *) makeString($6);
4708 					$$ = (Node *)n;
4709 				}
4710 			| ALTER EXTENSION name add_drop EVENT TRIGGER name
4711 				{
4712 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4713 					n->extname = $3;
4714 					n->action = $4;
4715 					n->objtype = OBJECT_EVENT_TRIGGER;
4716 					n->object = (Node *) makeString($7);
4717 					$$ = (Node *)n;
4718 				}
4719 			| ALTER EXTENSION name add_drop TABLE any_name
4720 				{
4721 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4722 					n->extname = $3;
4723 					n->action = $4;
4724 					n->objtype = OBJECT_TABLE;
4725 					n->object = (Node *) $6;
4726 					$$ = (Node *)n;
4727 				}
4728 			| ALTER EXTENSION name add_drop TEXT_P SEARCH PARSER any_name
4729 				{
4730 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4731 					n->extname = $3;
4732 					n->action = $4;
4733 					n->objtype = OBJECT_TSPARSER;
4734 					n->object = (Node *) $8;
4735 					$$ = (Node *)n;
4736 				}
4737 			| ALTER EXTENSION name add_drop TEXT_P SEARCH DICTIONARY any_name
4738 				{
4739 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4740 					n->extname = $3;
4741 					n->action = $4;
4742 					n->objtype = OBJECT_TSDICTIONARY;
4743 					n->object = (Node *) $8;
4744 					$$ = (Node *)n;
4745 				}
4746 			| ALTER EXTENSION name add_drop TEXT_P SEARCH TEMPLATE any_name
4747 				{
4748 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4749 					n->extname = $3;
4750 					n->action = $4;
4751 					n->objtype = OBJECT_TSTEMPLATE;
4752 					n->object = (Node *) $8;
4753 					$$ = (Node *)n;
4754 				}
4755 			| ALTER EXTENSION name add_drop TEXT_P SEARCH CONFIGURATION any_name
4756 				{
4757 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4758 					n->extname = $3;
4759 					n->action = $4;
4760 					n->objtype = OBJECT_TSCONFIGURATION;
4761 					n->object = (Node *) $8;
4762 					$$ = (Node *)n;
4763 				}
4764 			| ALTER EXTENSION name add_drop SEQUENCE any_name
4765 				{
4766 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4767 					n->extname = $3;
4768 					n->action = $4;
4769 					n->objtype = OBJECT_SEQUENCE;
4770 					n->object = (Node *) $6;
4771 					$$ = (Node *)n;
4772 				}
4773 			| ALTER EXTENSION name add_drop VIEW any_name
4774 				{
4775 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4776 					n->extname = $3;
4777 					n->action = $4;
4778 					n->objtype = OBJECT_VIEW;
4779 					n->object = (Node *) $6;
4780 					$$ = (Node *)n;
4781 				}
4782 			| ALTER EXTENSION name add_drop MATERIALIZED VIEW any_name
4783 				{
4784 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4785 					n->extname = $3;
4786 					n->action = $4;
4787 					n->objtype = OBJECT_MATVIEW;
4788 					n->object = (Node *) $7;
4789 					$$ = (Node *)n;
4790 				}
4791 			| ALTER EXTENSION name add_drop FOREIGN TABLE any_name
4792 				{
4793 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4794 					n->extname = $3;
4795 					n->action = $4;
4796 					n->objtype = OBJECT_FOREIGN_TABLE;
4797 					n->object = (Node *) $7;
4798 					$$ = (Node *)n;
4799 				}
4800 			| ALTER EXTENSION name add_drop FOREIGN DATA_P WRAPPER name
4801 				{
4802 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4803 					n->extname = $3;
4804 					n->action = $4;
4805 					n->objtype = OBJECT_FDW;
4806 					n->object = (Node *) makeString($8);
4807 					$$ = (Node *)n;
4808 				}
4809 			| ALTER EXTENSION name add_drop SERVER name
4810 				{
4811 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4812 					n->extname = $3;
4813 					n->action = $4;
4814 					n->objtype = OBJECT_FOREIGN_SERVER;
4815 					n->object = (Node *) makeString($6);
4816 					$$ = (Node *)n;
4817 				}
4818 			| ALTER EXTENSION name add_drop TRANSFORM FOR Typename LANGUAGE name
4819 				{
4820 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4821 					n->extname = $3;
4822 					n->action = $4;
4823 					n->objtype = OBJECT_TRANSFORM;
4824 					n->object = (Node *) list_make2($7, makeString($9));
4825 					$$ = (Node *)n;
4826 				}
4827 			| ALTER EXTENSION name add_drop TYPE_P Typename
4828 				{
4829 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4830 					n->extname = $3;
4831 					n->action = $4;
4832 					n->objtype = OBJECT_TYPE;
4833 					n->object = (Node *) $6;
4834 					$$ = (Node *)n;
4835 				}
4836 		;
4837 
4838 /*****************************************************************************
4839  *
4840  *		QUERY:
4841  *             CREATE FOREIGN DATA WRAPPER name options
4842  *
4843  *****************************************************************************/
4844 
4845 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
4846 				{
4847 					CreateFdwStmt *n = makeNode(CreateFdwStmt);
4848 					n->fdwname = $5;
4849 					n->func_options = $6;
4850 					n->options = $7;
4851 					$$ = (Node *) n;
4852 				}
4853 		;
4854 
4855 fdw_option:
4856 			HANDLER handler_name				{ $$ = makeDefElem("handler", (Node *)$2, @1); }
4857 			| NO HANDLER						{ $$ = makeDefElem("handler", NULL, @1); }
4858 			| VALIDATOR handler_name			{ $$ = makeDefElem("validator", (Node *)$2, @1); }
4859 			| NO VALIDATOR						{ $$ = makeDefElem("validator", NULL, @1); }
4860 		;
4861 
4862 fdw_options:
4863 			fdw_option							{ $$ = list_make1($1); }
4864 			| fdw_options fdw_option			{ $$ = lappend($1, $2); }
4865 		;
4866 
4867 opt_fdw_options:
4868 			fdw_options							{ $$ = $1; }
4869 			| /*EMPTY*/							{ $$ = NIL; }
4870 		;
4871 
4872 /*****************************************************************************
4873  *
4874  *		QUERY :
4875  *				ALTER FOREIGN DATA WRAPPER name options
4876  *
4877  ****************************************************************************/
4878 
4879 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
4880 				{
4881 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4882 					n->fdwname = $5;
4883 					n->func_options = $6;
4884 					n->options = $7;
4885 					$$ = (Node *) n;
4886 				}
4887 			| ALTER FOREIGN DATA_P WRAPPER name fdw_options
4888 				{
4889 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4890 					n->fdwname = $5;
4891 					n->func_options = $6;
4892 					n->options = NIL;
4893 					$$ = (Node *) n;
4894 				}
4895 		;
4896 
4897 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
4898 create_generic_options:
4899 			OPTIONS '(' generic_option_list ')'			{ $$ = $3; }
4900 			| /*EMPTY*/									{ $$ = NIL; }
4901 		;
4902 
4903 generic_option_list:
4904 			generic_option_elem
4905 				{
4906 					$$ = list_make1($1);
4907 				}
4908 			| generic_option_list ',' generic_option_elem
4909 				{
4910 					$$ = lappend($1, $3);
4911 				}
4912 		;
4913 
4914 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
4915 alter_generic_options:
4916 			OPTIONS	'(' alter_generic_option_list ')'		{ $$ = $3; }
4917 		;
4918 
4919 alter_generic_option_list:
4920 			alter_generic_option_elem
4921 				{
4922 					$$ = list_make1($1);
4923 				}
4924 			| alter_generic_option_list ',' alter_generic_option_elem
4925 				{
4926 					$$ = lappend($1, $3);
4927 				}
4928 		;
4929 
4930 alter_generic_option_elem:
4931 			generic_option_elem
4932 				{
4933 					$$ = $1;
4934 				}
4935 			| SET generic_option_elem
4936 				{
4937 					$$ = $2;
4938 					$$->defaction = DEFELEM_SET;
4939 				}
4940 			| ADD_P generic_option_elem
4941 				{
4942 					$$ = $2;
4943 					$$->defaction = DEFELEM_ADD;
4944 				}
4945 			| DROP generic_option_name
4946 				{
4947 					$$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP, @2);
4948 				}
4949 		;
4950 
4951 generic_option_elem:
4952 			generic_option_name generic_option_arg
4953 				{
4954 					$$ = makeDefElem($1, $2, @1);
4955 				}
4956 		;
4957 
4958 generic_option_name:
4959 				ColLabel			{ $$ = $1; }
4960 		;
4961 
4962 /* We could use def_arg here, but the spec only requires string literals */
4963 generic_option_arg:
4964 				Sconst				{ $$ = (Node *) makeString($1); }
4965 		;
4966 
4967 /*****************************************************************************
4968  *
4969  *		QUERY:
4970  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
4971  *
4972  *****************************************************************************/
4973 
4974 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
4975 						 FOREIGN DATA_P WRAPPER name create_generic_options
4976 				{
4977 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4978 					n->servername = $3;
4979 					n->servertype = $4;
4980 					n->version = $5;
4981 					n->fdwname = $9;
4982 					n->options = $10;
4983 					n->if_not_exists = false;
4984 					$$ = (Node *) n;
4985 				}
4986 				| CREATE SERVER IF_P NOT EXISTS name opt_type opt_foreign_server_version
4987 						 FOREIGN DATA_P WRAPPER name create_generic_options
4988 				{
4989 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4990 					n->servername = $6;
4991 					n->servertype = $7;
4992 					n->version = $8;
4993 					n->fdwname = $12;
4994 					n->options = $13;
4995 					n->if_not_exists = true;
4996 					$$ = (Node *) n;
4997 				}
4998 		;
4999 
5000 opt_type:
5001 			TYPE_P Sconst			{ $$ = $2; }
5002 			| /*EMPTY*/				{ $$ = NULL; }
5003 		;
5004 
5005 
5006 foreign_server_version:
5007 			VERSION_P Sconst		{ $$ = $2; }
5008 		|	VERSION_P NULL_P		{ $$ = NULL; }
5009 		;
5010 
5011 opt_foreign_server_version:
5012 			foreign_server_version	{ $$ = $1; }
5013 			| /*EMPTY*/				{ $$ = NULL; }
5014 		;
5015 
5016 /*****************************************************************************
5017  *
5018  *		QUERY :
5019  *				ALTER SERVER name [VERSION] [OPTIONS]
5020  *
5021  ****************************************************************************/
5022 
5023 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
5024 				{
5025 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5026 					n->servername = $3;
5027 					n->version = $4;
5028 					n->options = $5;
5029 					n->has_version = true;
5030 					$$ = (Node *) n;
5031 				}
5032 			| ALTER SERVER name foreign_server_version
5033 				{
5034 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5035 					n->servername = $3;
5036 					n->version = $4;
5037 					n->has_version = true;
5038 					$$ = (Node *) n;
5039 				}
5040 			| ALTER SERVER name alter_generic_options
5041 				{
5042 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5043 					n->servername = $3;
5044 					n->options = $4;
5045 					$$ = (Node *) n;
5046 				}
5047 		;
5048 
5049 /*****************************************************************************
5050  *
5051  *		QUERY:
5052  *             CREATE FOREIGN TABLE relname (...) SERVER name (...)
5053  *
5054  *****************************************************************************/
5055 
5056 CreateForeignTableStmt:
5057 		CREATE FOREIGN TABLE qualified_name
5058 			'(' OptTableElementList ')'
5059 			OptInherit SERVER name create_generic_options
5060 				{
5061 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5062 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
5063 					n->base.relation = $4;
5064 					n->base.tableElts = $6;
5065 					n->base.inhRelations = $8;
5066 					n->base.ofTypename = NULL;
5067 					n->base.constraints = NIL;
5068 					n->base.options = NIL;
5069 					n->base.oncommit = ONCOMMIT_NOOP;
5070 					n->base.tablespacename = NULL;
5071 					n->base.if_not_exists = false;
5072 					/* FDW-specific data */
5073 					n->servername = $10;
5074 					n->options = $11;
5075 					$$ = (Node *) n;
5076 				}
5077 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5078 			'(' OptTableElementList ')'
5079 			OptInherit SERVER name create_generic_options
5080 				{
5081 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5082 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
5083 					n->base.relation = $7;
5084 					n->base.tableElts = $9;
5085 					n->base.inhRelations = $11;
5086 					n->base.ofTypename = NULL;
5087 					n->base.constraints = NIL;
5088 					n->base.options = NIL;
5089 					n->base.oncommit = ONCOMMIT_NOOP;
5090 					n->base.tablespacename = NULL;
5091 					n->base.if_not_exists = true;
5092 					/* FDW-specific data */
5093 					n->servername = $13;
5094 					n->options = $14;
5095 					$$ = (Node *) n;
5096 				}
5097 		| CREATE FOREIGN TABLE qualified_name
5098 			PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5099 			SERVER name create_generic_options
5100 				{
5101 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5102 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
5103 					n->base.relation = $4;
5104 					n->base.inhRelations = list_make1($7);
5105 					n->base.tableElts = $8;
5106 					n->base.partbound = $9;
5107 					n->base.ofTypename = NULL;
5108 					n->base.constraints = NIL;
5109 					n->base.options = NIL;
5110 					n->base.oncommit = ONCOMMIT_NOOP;
5111 					n->base.tablespacename = NULL;
5112 					n->base.if_not_exists = false;
5113 					/* FDW-specific data */
5114 					n->servername = $11;
5115 					n->options = $12;
5116 					$$ = (Node *) n;
5117 				}
5118 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5119 			PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5120 			SERVER name create_generic_options
5121 				{
5122 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5123 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
5124 					n->base.relation = $7;
5125 					n->base.inhRelations = list_make1($10);
5126 					n->base.tableElts = $11;
5127 					n->base.partbound = $12;
5128 					n->base.ofTypename = NULL;
5129 					n->base.constraints = NIL;
5130 					n->base.options = NIL;
5131 					n->base.oncommit = ONCOMMIT_NOOP;
5132 					n->base.tablespacename = NULL;
5133 					n->base.if_not_exists = true;
5134 					/* FDW-specific data */
5135 					n->servername = $14;
5136 					n->options = $15;
5137 					$$ = (Node *) n;
5138 				}
5139 		;
5140 
5141 /*****************************************************************************
5142  *
5143  *		QUERY:
5144  *             ALTER FOREIGN TABLE relname [...]
5145  *
5146  *****************************************************************************/
5147 
5148 AlterForeignTableStmt:
5149 			ALTER FOREIGN TABLE relation_expr alter_table_cmds
5150 				{
5151 					AlterTableStmt *n = makeNode(AlterTableStmt);
5152 					n->relation = $4;
5153 					n->cmds = $5;
5154 					n->relkind = OBJECT_FOREIGN_TABLE;
5155 					n->missing_ok = false;
5156 					$$ = (Node *)n;
5157 				}
5158 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
5159 				{
5160 					AlterTableStmt *n = makeNode(AlterTableStmt);
5161 					n->relation = $6;
5162 					n->cmds = $7;
5163 					n->relkind = OBJECT_FOREIGN_TABLE;
5164 					n->missing_ok = true;
5165 					$$ = (Node *)n;
5166 				}
5167 		;
5168 
5169 /*****************************************************************************
5170  *
5171  *		QUERY:
5172  *				IMPORT FOREIGN SCHEMA remote_schema
5173  *				[ { LIMIT TO | EXCEPT } ( table_list ) ]
5174  *				FROM SERVER server_name INTO local_schema [ OPTIONS (...) ]
5175  *
5176  ****************************************************************************/
5177 
5178 ImportForeignSchemaStmt:
5179 		IMPORT_P FOREIGN SCHEMA name import_qualification
5180 		  FROM SERVER name INTO name create_generic_options
5181 			{
5182 				ImportForeignSchemaStmt *n = makeNode(ImportForeignSchemaStmt);
5183 				n->server_name = $8;
5184 				n->remote_schema = $4;
5185 				n->local_schema = $10;
5186 				n->list_type = $5->type;
5187 				n->table_list = $5->table_names;
5188 				n->options = $11;
5189 				$$ = (Node *) n;
5190 			}
5191 		;
5192 
5193 import_qualification_type:
5194 		LIMIT TO 				{ $$ = FDW_IMPORT_SCHEMA_LIMIT_TO; }
5195 		| EXCEPT 				{ $$ = FDW_IMPORT_SCHEMA_EXCEPT; }
5196 		;
5197 
5198 import_qualification:
5199 		import_qualification_type '(' relation_expr_list ')'
5200 			{
5201 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5202 				n->type = $1;
5203 				n->table_names = $3;
5204 				$$ = n;
5205 			}
5206 		| /*EMPTY*/
5207 			{
5208 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5209 				n->type = FDW_IMPORT_SCHEMA_ALL;
5210 				n->table_names = NIL;
5211 				$$ = n;
5212 			}
5213 		;
5214 
5215 /*****************************************************************************
5216  *
5217  *		QUERY:
5218  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
5219  *
5220  *****************************************************************************/
5221 
5222 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
5223 				{
5224 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5225 					n->user = $5;
5226 					n->servername = $7;
5227 					n->options = $8;
5228 					n->if_not_exists = false;
5229 					$$ = (Node *) n;
5230 				}
5231 				| CREATE USER MAPPING IF_P NOT EXISTS FOR auth_ident SERVER name create_generic_options
5232 				{
5233 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5234 					n->user = $8;
5235 					n->servername = $10;
5236 					n->options = $11;
5237 					n->if_not_exists = true;
5238 					$$ = (Node *) n;
5239 				}
5240 		;
5241 
5242 /* User mapping authorization identifier */
5243 auth_ident: RoleSpec			{ $$ = $1; }
5244 			| USER				{ $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1); }
5245 		;
5246 
5247 /*****************************************************************************
5248  *
5249  *		QUERY :
5250  *				DROP USER MAPPING FOR auth_ident SERVER name
5251  *
5252  * XXX you'd think this should have a CASCADE/RESTRICT option, even if it's
5253  * only pro forma; but the SQL standard doesn't show one.
5254  ****************************************************************************/
5255 
5256 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
5257 				{
5258 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5259 					n->user = $5;
5260 					n->servername = $7;
5261 					n->missing_ok = false;
5262 					$$ = (Node *) n;
5263 				}
5264 				|  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
5265 				{
5266 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5267 					n->user = $7;
5268 					n->servername = $9;
5269 					n->missing_ok = true;
5270 					$$ = (Node *) n;
5271 				}
5272 		;
5273 
5274 /*****************************************************************************
5275  *
5276  *		QUERY :
5277  *				ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
5278  *
5279  ****************************************************************************/
5280 
5281 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
5282 				{
5283 					AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
5284 					n->user = $5;
5285 					n->servername = $7;
5286 					n->options = $8;
5287 					$$ = (Node *) n;
5288 				}
5289 		;
5290 
5291 /*****************************************************************************
5292  *
5293  *		QUERIES:
5294  *				CREATE POLICY name ON table
5295  *					[AS { PERMISSIVE | RESTRICTIVE } ]
5296  *					[FOR { SELECT | INSERT | UPDATE | DELETE } ]
5297  *					[TO role, ...]
5298  *					[USING (qual)] [WITH CHECK (with check qual)]
5299  *				ALTER POLICY name ON table [TO role, ...]
5300  *					[USING (qual)] [WITH CHECK (with check qual)]
5301  *
5302  *****************************************************************************/
5303 
5304 CreatePolicyStmt:
5305 			CREATE POLICY name ON qualified_name RowSecurityDefaultPermissive
5306 				RowSecurityDefaultForCmd RowSecurityDefaultToRole
5307 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5308 				{
5309 					CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
5310 					n->policy_name = $3;
5311 					n->table = $5;
5312 					n->permissive = $6;
5313 					n->cmd_name = $7;
5314 					n->roles = $8;
5315 					n->qual = $9;
5316 					n->with_check = $10;
5317 					$$ = (Node *) n;
5318 				}
5319 		;
5320 
5321 AlterPolicyStmt:
5322 			ALTER POLICY name ON qualified_name RowSecurityOptionalToRole
5323 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5324 				{
5325 					AlterPolicyStmt *n = makeNode(AlterPolicyStmt);
5326 					n->policy_name = $3;
5327 					n->table = $5;
5328 					n->roles = $6;
5329 					n->qual = $7;
5330 					n->with_check = $8;
5331 					$$ = (Node *) n;
5332 				}
5333 		;
5334 
5335 RowSecurityOptionalExpr:
5336 			USING '(' a_expr ')'	{ $$ = $3; }
5337 			| /* EMPTY */			{ $$ = NULL; }
5338 		;
5339 
5340 RowSecurityOptionalWithCheck:
5341 			WITH CHECK '(' a_expr ')'		{ $$ = $4; }
5342 			| /* EMPTY */					{ $$ = NULL; }
5343 		;
5344 
5345 RowSecurityDefaultToRole:
5346 			TO role_list			{ $$ = $2; }
5347 			| /* EMPTY */			{ $$ = list_make1(makeRoleSpec(ROLESPEC_PUBLIC, -1)); }
5348 		;
5349 
5350 RowSecurityOptionalToRole:
5351 			TO role_list			{ $$ = $2; }
5352 			| /* EMPTY */			{ $$ = NULL; }
5353 		;
5354 
5355 RowSecurityDefaultPermissive:
5356 			AS IDENT
5357 				{
5358 					if (strcmp($2, "permissive") == 0)
5359 						$$ = true;
5360 					else if (strcmp($2, "restrictive") == 0)
5361 						$$ = false;
5362 					else
5363 						ereport(ERROR,
5364 								(errcode(ERRCODE_SYNTAX_ERROR),
5365 							 errmsg("unrecognized row security option \"%s\"", $2),
5366 								 errhint("Only PERMISSIVE or RESTRICTIVE policies are supported currently."),
5367 									 parser_errposition(@2)));
5368 
5369 				}
5370 			| /* EMPTY */			{ $$ = true; }
5371 		;
5372 
5373 RowSecurityDefaultForCmd:
5374 			FOR row_security_cmd	{ $$ = $2; }
5375 			| /* EMPTY */			{ $$ = "all"; }
5376 		;
5377 
5378 row_security_cmd:
5379 			ALL				{ $$ = "all"; }
5380 		|	SELECT			{ $$ = "select"; }
5381 		|	INSERT			{ $$ = "insert"; }
5382 		|	UPDATE			{ $$ = "update"; }
5383 		|	DELETE_P		{ $$ = "delete"; }
5384 		;
5385 
5386 /*****************************************************************************
5387  *
5388  *		QUERY:
5389  *             CREATE ACCESS METHOD name HANDLER handler_name
5390  *
5391  *****************************************************************************/
5392 
5393 CreateAmStmt: CREATE ACCESS METHOD name TYPE_P INDEX HANDLER handler_name
5394 				{
5395 					CreateAmStmt *n = makeNode(CreateAmStmt);
5396 					n->amname = $4;
5397 					n->handler_name = $8;
5398 					n->amtype = AMTYPE_INDEX;
5399 					$$ = (Node *) n;
5400 				}
5401 		;
5402 
5403 /*****************************************************************************
5404  *
5405  *		QUERIES :
5406  *				CREATE TRIGGER ...
5407  *
5408  *****************************************************************************/
5409 
5410 CreateTrigStmt:
5411 			CREATE TRIGGER name TriggerActionTime TriggerEvents ON
5412 			qualified_name TriggerReferencing TriggerForSpec TriggerWhen
5413 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
5414 				{
5415 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5416 					n->trigname = $3;
5417 					n->relation = $7;
5418 					n->funcname = $13;
5419 					n->args = $15;
5420 					n->row = $9;
5421 					n->timing = $4;
5422 					n->events = intVal(linitial($5));
5423 					n->columns = (List *) lsecond($5);
5424 					n->whenClause = $10;
5425 					n->transitionRels = $8;
5426 					n->isconstraint  = false;
5427 					n->deferrable	 = false;
5428 					n->initdeferred  = false;
5429 					n->constrrel = NULL;
5430 					$$ = (Node *)n;
5431 				}
5432 			| CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
5433 			qualified_name OptConstrFromTable ConstraintAttributeSpec
5434 			FOR EACH ROW TriggerWhen
5435 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
5436 				{
5437 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5438 					n->trigname = $4;
5439 					n->relation = $8;
5440 					n->funcname = $17;
5441 					n->args = $19;
5442 					n->row = true;
5443 					n->timing = TRIGGER_TYPE_AFTER;
5444 					n->events = intVal(linitial($6));
5445 					n->columns = (List *) lsecond($6);
5446 					n->whenClause = $14;
5447 					n->transitionRels = NIL;
5448 					n->isconstraint  = true;
5449 					processCASbits($10, @10, "TRIGGER",
5450 								   &n->deferrable, &n->initdeferred, NULL,
5451 								   NULL, yyscanner);
5452 					n->constrrel = $9;
5453 					$$ = (Node *)n;
5454 				}
5455 		;
5456 
5457 TriggerActionTime:
5458 			BEFORE								{ $$ = TRIGGER_TYPE_BEFORE; }
5459 			| AFTER								{ $$ = TRIGGER_TYPE_AFTER; }
5460 			| INSTEAD OF						{ $$ = TRIGGER_TYPE_INSTEAD; }
5461 		;
5462 
5463 TriggerEvents:
5464 			TriggerOneEvent
5465 				{ $$ = $1; }
5466 			| TriggerEvents OR TriggerOneEvent
5467 				{
5468 					int		events1 = intVal(linitial($1));
5469 					int		events2 = intVal(linitial($3));
5470 					List   *columns1 = (List *) lsecond($1);
5471 					List   *columns2 = (List *) lsecond($3);
5472 
5473 					if (events1 & events2)
5474 						parser_yyerror("duplicate trigger events specified");
5475 					/*
5476 					 * concat'ing the columns lists loses information about
5477 					 * which columns went with which event, but so long as
5478 					 * only UPDATE carries columns and we disallow multiple
5479 					 * UPDATE items, it doesn't matter.  Command execution
5480 					 * should just ignore the columns for non-UPDATE events.
5481 					 */
5482 					$$ = list_make2(makeInteger(events1 | events2),
5483 									list_concat(columns1, columns2));
5484 				}
5485 		;
5486 
5487 TriggerOneEvent:
5488 			INSERT
5489 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
5490 			| DELETE_P
5491 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
5492 			| UPDATE
5493 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
5494 			| UPDATE OF columnList
5495 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
5496 			| TRUNCATE
5497 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
5498 		;
5499 
5500 TriggerReferencing:
5501 			REFERENCING TriggerTransitions			{ $$ = $2; }
5502 			| /*EMPTY*/								{ $$ = NIL; }
5503 		;
5504 
5505 TriggerTransitions:
5506 			TriggerTransition						{ $$ = list_make1($1); }
5507 			| TriggerTransitions TriggerTransition	{ $$ = lappend($1, $2); }
5508 		;
5509 
5510 TriggerTransition:
5511 			TransitionOldOrNew TransitionRowOrTable opt_as TransitionRelName
5512 				{
5513 					TriggerTransition *n = makeNode(TriggerTransition);
5514 					n->name = $4;
5515 					n->isNew = $1;
5516 					n->isTable = $2;
5517 					$$ = (Node *)n;
5518 				}
5519 		;
5520 
5521 TransitionOldOrNew:
5522 			NEW										{ $$ = true; }
5523 			| OLD									{ $$ = false; }
5524 		;
5525 
5526 TransitionRowOrTable:
5527 			TABLE									{ $$ = true; }
5528 			/*
5529 			 * According to the standard, lack of a keyword here implies ROW.
5530 			 * Support for that would require prohibiting ROW entirely here,
5531 			 * reserving the keyword ROW, and/or requiring AS (instead of
5532 			 * allowing it to be optional, as the standard specifies) as the
5533 			 * next token.  Requiring ROW seems cleanest and easiest to
5534 			 * explain.
5535 			 */
5536 			| ROW									{ $$ = false; }
5537 		;
5538 
5539 TransitionRelName:
5540 			ColId									{ $$ = $1; }
5541 		;
5542 
5543 TriggerForSpec:
5544 			FOR TriggerForOptEach TriggerForType
5545 				{
5546 					$$ = $3;
5547 				}
5548 			| /* EMPTY */
5549 				{
5550 					/*
5551 					 * If ROW/STATEMENT not specified, default to
5552 					 * STATEMENT, per SQL
5553 					 */
5554 					$$ = false;
5555 				}
5556 		;
5557 
5558 TriggerForOptEach:
5559 			EACH									{}
5560 			| /*EMPTY*/								{}
5561 		;
5562 
5563 TriggerForType:
5564 			ROW										{ $$ = true; }
5565 			| STATEMENT								{ $$ = false; }
5566 		;
5567 
5568 TriggerWhen:
5569 			WHEN '(' a_expr ')'						{ $$ = $3; }
5570 			| /*EMPTY*/								{ $$ = NULL; }
5571 		;
5572 
5573 TriggerFuncArgs:
5574 			TriggerFuncArg							{ $$ = list_make1($1); }
5575 			| TriggerFuncArgs ',' TriggerFuncArg	{ $$ = lappend($1, $3); }
5576 			| /*EMPTY*/								{ $$ = NIL; }
5577 		;
5578 
5579 TriggerFuncArg:
5580 			Iconst
5581 				{
5582 					$$ = makeString(psprintf("%d", $1));
5583 				}
5584 			| FCONST								{ $$ = makeString($1); }
5585 			| Sconst								{ $$ = makeString($1); }
5586 			| ColLabel								{ $$ = makeString($1); }
5587 		;
5588 
5589 OptConstrFromTable:
5590 			FROM qualified_name						{ $$ = $2; }
5591 			| /*EMPTY*/								{ $$ = NULL; }
5592 		;
5593 
5594 ConstraintAttributeSpec:
5595 			/*EMPTY*/
5596 				{ $$ = 0; }
5597 			| ConstraintAttributeSpec ConstraintAttributeElem
5598 				{
5599 					/*
5600 					 * We must complain about conflicting options.
5601 					 * We could, but choose not to, complain about redundant
5602 					 * options (ie, where $2's bit is already set in $1).
5603 					 */
5604 					int		newspec = $1 | $2;
5605 
5606 					/* special message for this case */
5607 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED)) == (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED))
5608 						ereport(ERROR,
5609 								(errcode(ERRCODE_SYNTAX_ERROR),
5610 								 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
5611 								 parser_errposition(@2)));
5612 					/* generic message for other conflicts */
5613 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE)) == (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE) ||
5614 						(newspec & (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED)) == (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED))
5615 						ereport(ERROR,
5616 								(errcode(ERRCODE_SYNTAX_ERROR),
5617 								 errmsg("conflicting constraint properties"),
5618 								 parser_errposition(@2)));
5619 					$$ = newspec;
5620 				}
5621 		;
5622 
5623 ConstraintAttributeElem:
5624 			NOT DEFERRABLE					{ $$ = CAS_NOT_DEFERRABLE; }
5625 			| DEFERRABLE					{ $$ = CAS_DEFERRABLE; }
5626 			| INITIALLY IMMEDIATE			{ $$ = CAS_INITIALLY_IMMEDIATE; }
5627 			| INITIALLY DEFERRED			{ $$ = CAS_INITIALLY_DEFERRED; }
5628 			| NOT VALID						{ $$ = CAS_NOT_VALID; }
5629 			| NO INHERIT					{ $$ = CAS_NO_INHERIT; }
5630 		;
5631 
5632 
5633 /*****************************************************************************
5634  *
5635  *		QUERIES :
5636  *				CREATE EVENT TRIGGER ...
5637  *				ALTER EVENT TRIGGER ...
5638  *
5639  *****************************************************************************/
5640 
5641 CreateEventTrigStmt:
5642 			CREATE EVENT TRIGGER name ON ColLabel
5643 			EXECUTE PROCEDURE func_name '(' ')'
5644 				{
5645 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5646 					n->trigname = $4;
5647 					n->eventname = $6;
5648 					n->whenclause = NULL;
5649 					n->funcname = $9;
5650 					$$ = (Node *)n;
5651 				}
5652 		  | CREATE EVENT TRIGGER name ON ColLabel
5653 			WHEN event_trigger_when_list
5654 			EXECUTE PROCEDURE func_name '(' ')'
5655 				{
5656 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5657 					n->trigname = $4;
5658 					n->eventname = $6;
5659 					n->whenclause = $8;
5660 					n->funcname = $11;
5661 					$$ = (Node *)n;
5662 				}
5663 		;
5664 
5665 event_trigger_when_list:
5666 		  event_trigger_when_item
5667 			{ $$ = list_make1($1); }
5668 		| event_trigger_when_list AND event_trigger_when_item
5669 			{ $$ = lappend($1, $3); }
5670 		;
5671 
5672 event_trigger_when_item:
5673 		ColId IN_P '(' event_trigger_value_list ')'
5674 			{ $$ = makeDefElem($1, (Node *) $4, @1); }
5675 		;
5676 
5677 event_trigger_value_list:
5678 		  SCONST
5679 			{ $$ = list_make1(makeString($1)); }
5680 		| event_trigger_value_list ',' SCONST
5681 			{ $$ = lappend($1, makeString($3)); }
5682 		;
5683 
5684 AlterEventTrigStmt:
5685 			ALTER EVENT TRIGGER name enable_trigger
5686 				{
5687 					AlterEventTrigStmt *n = makeNode(AlterEventTrigStmt);
5688 					n->trigname = $4;
5689 					n->tgenabled = $5;
5690 					$$ = (Node *) n;
5691 				}
5692 		;
5693 
5694 enable_trigger:
5695 			ENABLE_P					{ $$ = TRIGGER_FIRES_ON_ORIGIN; }
5696 			| ENABLE_P REPLICA			{ $$ = TRIGGER_FIRES_ON_REPLICA; }
5697 			| ENABLE_P ALWAYS			{ $$ = TRIGGER_FIRES_ALWAYS; }
5698 			| DISABLE_P					{ $$ = TRIGGER_DISABLED; }
5699 		;
5700 
5701 /*****************************************************************************
5702  *
5703  *		QUERIES :
5704  *				CREATE ASSERTION ...
5705  *				DROP ASSERTION ...
5706  *
5707  *****************************************************************************/
5708 
5709 CreateAssertStmt:
5710 			CREATE ASSERTION name CHECK '(' a_expr ')'
5711 			ConstraintAttributeSpec
5712 				{
5713 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5714 					n->trigname = $3;
5715 					n->args = list_make1($6);
5716 					n->isconstraint  = true;
5717 					processCASbits($8, @8, "ASSERTION",
5718 								   &n->deferrable, &n->initdeferred, NULL,
5719 								   NULL, yyscanner);
5720 
5721 					ereport(ERROR,
5722 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5723 							 errmsg("CREATE ASSERTION is not yet implemented")));
5724 
5725 					$$ = (Node *)n;
5726 				}
5727 		;
5728 
5729 DropAssertStmt:
5730 			DROP ASSERTION name opt_drop_behavior
5731 				{
5732 					DropStmt *n = makeNode(DropStmt);
5733 					n->objects = NIL;
5734 					n->behavior = $4;
5735 					n->removeType = OBJECT_TRIGGER; /* XXX */
5736 					ereport(ERROR,
5737 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5738 							 errmsg("DROP ASSERTION is not yet implemented")));
5739 					$$ = (Node *) n;
5740 				}
5741 		;
5742 
5743 
5744 /*****************************************************************************
5745  *
5746  *		QUERY :
5747  *				define (aggregate,operator,type)
5748  *
5749  *****************************************************************************/
5750 
5751 DefineStmt:
5752 			CREATE AGGREGATE func_name aggr_args definition
5753 				{
5754 					DefineStmt *n = makeNode(DefineStmt);
5755 					n->kind = OBJECT_AGGREGATE;
5756 					n->oldstyle = false;
5757 					n->defnames = $3;
5758 					n->args = $4;
5759 					n->definition = $5;
5760 					$$ = (Node *)n;
5761 				}
5762 			| CREATE AGGREGATE func_name old_aggr_definition
5763 				{
5764 					/* old-style (pre-8.2) syntax for CREATE AGGREGATE */
5765 					DefineStmt *n = makeNode(DefineStmt);
5766 					n->kind = OBJECT_AGGREGATE;
5767 					n->oldstyle = true;
5768 					n->defnames = $3;
5769 					n->args = NIL;
5770 					n->definition = $4;
5771 					$$ = (Node *)n;
5772 				}
5773 			| CREATE OPERATOR any_operator definition
5774 				{
5775 					DefineStmt *n = makeNode(DefineStmt);
5776 					n->kind = OBJECT_OPERATOR;
5777 					n->oldstyle = false;
5778 					n->defnames = $3;
5779 					n->args = NIL;
5780 					n->definition = $4;
5781 					$$ = (Node *)n;
5782 				}
5783 			| CREATE TYPE_P any_name definition
5784 				{
5785 					DefineStmt *n = makeNode(DefineStmt);
5786 					n->kind = OBJECT_TYPE;
5787 					n->oldstyle = false;
5788 					n->defnames = $3;
5789 					n->args = NIL;
5790 					n->definition = $4;
5791 					$$ = (Node *)n;
5792 				}
5793 			| CREATE TYPE_P any_name
5794 				{
5795 					/* Shell type (identified by lack of definition) */
5796 					DefineStmt *n = makeNode(DefineStmt);
5797 					n->kind = OBJECT_TYPE;
5798 					n->oldstyle = false;
5799 					n->defnames = $3;
5800 					n->args = NIL;
5801 					n->definition = NIL;
5802 					$$ = (Node *)n;
5803 				}
5804 			| CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
5805 				{
5806 					CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
5807 
5808 					/* can't use qualified_name, sigh */
5809 					n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
5810 					n->coldeflist = $6;
5811 					$$ = (Node *)n;
5812 				}
5813 			| CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
5814 				{
5815 					CreateEnumStmt *n = makeNode(CreateEnumStmt);
5816 					n->typeName = $3;
5817 					n->vals = $7;
5818 					$$ = (Node *)n;
5819 				}
5820 			| CREATE TYPE_P any_name AS RANGE definition
5821 				{
5822 					CreateRangeStmt *n = makeNode(CreateRangeStmt);
5823 					n->typeName = $3;
5824 					n->params	= $6;
5825 					$$ = (Node *)n;
5826 				}
5827 			| CREATE TEXT_P SEARCH PARSER any_name definition
5828 				{
5829 					DefineStmt *n = makeNode(DefineStmt);
5830 					n->kind = OBJECT_TSPARSER;
5831 					n->args = NIL;
5832 					n->defnames = $5;
5833 					n->definition = $6;
5834 					$$ = (Node *)n;
5835 				}
5836 			| CREATE TEXT_P SEARCH DICTIONARY any_name definition
5837 				{
5838 					DefineStmt *n = makeNode(DefineStmt);
5839 					n->kind = OBJECT_TSDICTIONARY;
5840 					n->args = NIL;
5841 					n->defnames = $5;
5842 					n->definition = $6;
5843 					$$ = (Node *)n;
5844 				}
5845 			| CREATE TEXT_P SEARCH TEMPLATE any_name definition
5846 				{
5847 					DefineStmt *n = makeNode(DefineStmt);
5848 					n->kind = OBJECT_TSTEMPLATE;
5849 					n->args = NIL;
5850 					n->defnames = $5;
5851 					n->definition = $6;
5852 					$$ = (Node *)n;
5853 				}
5854 			| CREATE TEXT_P SEARCH CONFIGURATION any_name definition
5855 				{
5856 					DefineStmt *n = makeNode(DefineStmt);
5857 					n->kind = OBJECT_TSCONFIGURATION;
5858 					n->args = NIL;
5859 					n->defnames = $5;
5860 					n->definition = $6;
5861 					$$ = (Node *)n;
5862 				}
5863 			| CREATE COLLATION any_name definition
5864 				{
5865 					DefineStmt *n = makeNode(DefineStmt);
5866 					n->kind = OBJECT_COLLATION;
5867 					n->args = NIL;
5868 					n->defnames = $3;
5869 					n->definition = $4;
5870 					$$ = (Node *)n;
5871 				}
5872 			| CREATE COLLATION IF_P NOT EXISTS any_name definition
5873 				{
5874 					DefineStmt *n = makeNode(DefineStmt);
5875 					n->kind = OBJECT_COLLATION;
5876 					n->args = NIL;
5877 					n->defnames = $6;
5878 					n->definition = $7;
5879 					n->if_not_exists = true;
5880 					$$ = (Node *)n;
5881 				}
5882 			| CREATE COLLATION any_name FROM any_name
5883 				{
5884 					DefineStmt *n = makeNode(DefineStmt);
5885 					n->kind = OBJECT_COLLATION;
5886 					n->args = NIL;
5887 					n->defnames = $3;
5888 					n->definition = list_make1(makeDefElem("from", (Node *) $5, @5));
5889 					$$ = (Node *)n;
5890 				}
5891 			| CREATE COLLATION IF_P NOT EXISTS any_name FROM any_name
5892 				{
5893 					DefineStmt *n = makeNode(DefineStmt);
5894 					n->kind = OBJECT_COLLATION;
5895 					n->args = NIL;
5896 					n->defnames = $6;
5897 					n->definition = list_make1(makeDefElem("from", (Node *) $8, @8));
5898 					n->if_not_exists = true;
5899 					$$ = (Node *)n;
5900 				}
5901 		;
5902 
5903 definition: '(' def_list ')'						{ $$ = $2; }
5904 		;
5905 
5906 def_list:	def_elem								{ $$ = list_make1($1); }
5907 			| def_list ',' def_elem					{ $$ = lappend($1, $3); }
5908 		;
5909 
5910 def_elem:	ColLabel '=' def_arg
5911 				{
5912 					$$ = makeDefElem($1, (Node *) $3, @1);
5913 				}
5914 			| ColLabel
5915 				{
5916 					$$ = makeDefElem($1, NULL, @1);
5917 				}
5918 		;
5919 
5920 /* Note: any simple identifier will be returned as a type name! */
5921 def_arg:	func_type						{ $$ = (Node *)$1; }
5922 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
5923 			| qual_all_Op					{ $$ = (Node *)$1; }
5924 			| NumericOnly					{ $$ = (Node *)$1; }
5925 			| Sconst						{ $$ = (Node *)makeString($1); }
5926 			| NONE							{ $$ = (Node *)makeString(pstrdup($1)); }
5927 		;
5928 
5929 old_aggr_definition: '(' old_aggr_list ')'			{ $$ = $2; }
5930 		;
5931 
5932 old_aggr_list: old_aggr_elem						{ $$ = list_make1($1); }
5933 			| old_aggr_list ',' old_aggr_elem		{ $$ = lappend($1, $3); }
5934 		;
5935 
5936 /*
5937  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
5938  * the item names needed in old aggregate definitions are likely to become
5939  * SQL keywords.
5940  */
5941 old_aggr_elem:  IDENT '=' def_arg
5942 				{
5943 					$$ = makeDefElem($1, (Node *)$3, @1);
5944 				}
5945 		;
5946 
5947 opt_enum_val_list:
5948 		enum_val_list							{ $$ = $1; }
5949 		| /*EMPTY*/								{ $$ = NIL; }
5950 		;
5951 
5952 enum_val_list:	Sconst
5953 				{ $$ = list_make1(makeString($1)); }
5954 			| enum_val_list ',' Sconst
5955 				{ $$ = lappend($1, makeString($3)); }
5956 		;
5957 
5958 /*****************************************************************************
5959  *
5960  *	ALTER TYPE enumtype ADD ...
5961  *
5962  *****************************************************************************/
5963 
5964 AlterEnumStmt:
5965 		ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst
5966 			{
5967 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5968 				n->typeName = $3;
5969 				n->oldVal = NULL;
5970 				n->newVal = $7;
5971 				n->newValNeighbor = NULL;
5972 				n->newValIsAfter = true;
5973 				n->skipIfNewValExists = $6;
5974 				$$ = (Node *) n;
5975 			}
5976 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst BEFORE Sconst
5977 			{
5978 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5979 				n->typeName = $3;
5980 				n->oldVal = NULL;
5981 				n->newVal = $7;
5982 				n->newValNeighbor = $9;
5983 				n->newValIsAfter = false;
5984 				n->skipIfNewValExists = $6;
5985 				$$ = (Node *) n;
5986 			}
5987 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst AFTER Sconst
5988 			{
5989 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5990 				n->typeName = $3;
5991 				n->oldVal = NULL;
5992 				n->newVal = $7;
5993 				n->newValNeighbor = $9;
5994 				n->newValIsAfter = true;
5995 				n->skipIfNewValExists = $6;
5996 				$$ = (Node *) n;
5997 			}
5998 		 | ALTER TYPE_P any_name RENAME VALUE_P Sconst TO Sconst
5999 			{
6000 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
6001 				n->typeName = $3;
6002 				n->oldVal = $6;
6003 				n->newVal = $8;
6004 				n->newValNeighbor = NULL;
6005 				n->newValIsAfter = false;
6006 				n->skipIfNewValExists = false;
6007 				$$ = (Node *) n;
6008 			}
6009 		 ;
6010 
6011 opt_if_not_exists: IF_P NOT EXISTS              { $$ = true; }
6012 		| /* empty */                          { $$ = false; }
6013 		;
6014 
6015 
6016 /*****************************************************************************
6017  *
6018  *		QUERIES :
6019  *				CREATE OPERATOR CLASS ...
6020  *				CREATE OPERATOR FAMILY ...
6021  *				ALTER OPERATOR FAMILY ...
6022  *				DROP OPERATOR CLASS ...
6023  *				DROP OPERATOR FAMILY ...
6024  *
6025  *****************************************************************************/
6026 
6027 CreateOpClassStmt:
6028 			CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
6029 			USING access_method opt_opfamily AS opclass_item_list
6030 				{
6031 					CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
6032 					n->opclassname = $4;
6033 					n->isDefault = $5;
6034 					n->datatype = $8;
6035 					n->amname = $10;
6036 					n->opfamilyname = $11;
6037 					n->items = $13;
6038 					$$ = (Node *) n;
6039 				}
6040 		;
6041 
6042 opclass_item_list:
6043 			opclass_item							{ $$ = list_make1($1); }
6044 			| opclass_item_list ',' opclass_item	{ $$ = lappend($1, $3); }
6045 		;
6046 
6047 opclass_item:
6048 			OPERATOR Iconst any_operator opclass_purpose opt_recheck
6049 				{
6050 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6051 					ObjectWithArgs *owa = makeNode(ObjectWithArgs);
6052 					owa->objname = $3;
6053 					owa->objargs = NIL;
6054 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6055 					n->name = owa;
6056 					n->number = $2;
6057 					n->order_family = $4;
6058 					$$ = (Node *) n;
6059 				}
6060 			| OPERATOR Iconst operator_with_argtypes opclass_purpose
6061 			  opt_recheck
6062 				{
6063 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6064 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6065 					n->name = $3;
6066 					n->number = $2;
6067 					n->order_family = $4;
6068 					$$ = (Node *) n;
6069 				}
6070 			| FUNCTION Iconst function_with_argtypes
6071 				{
6072 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6073 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6074 					n->name = $3;
6075 					n->number = $2;
6076 					$$ = (Node *) n;
6077 				}
6078 			| FUNCTION Iconst '(' type_list ')' function_with_argtypes
6079 				{
6080 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6081 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6082 					n->name = $6;
6083 					n->number = $2;
6084 					n->class_args = $4;
6085 					$$ = (Node *) n;
6086 				}
6087 			| STORAGE Typename
6088 				{
6089 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6090 					n->itemtype = OPCLASS_ITEM_STORAGETYPE;
6091 					n->storedtype = $2;
6092 					$$ = (Node *) n;
6093 				}
6094 		;
6095 
6096 opt_default:	DEFAULT						{ $$ = true; }
6097 			| /*EMPTY*/						{ $$ = false; }
6098 		;
6099 
6100 opt_opfamily:	FAMILY any_name				{ $$ = $2; }
6101 			| /*EMPTY*/						{ $$ = NIL; }
6102 		;
6103 
6104 opclass_purpose: FOR SEARCH					{ $$ = NIL; }
6105 			| FOR ORDER BY any_name			{ $$ = $4; }
6106 			| /*EMPTY*/						{ $$ = NIL; }
6107 		;
6108 
6109 opt_recheck:	RECHECK
6110 				{
6111 					/*
6112 					 * RECHECK no longer does anything in opclass definitions,
6113 					 * but we still accept it to ease porting of old database
6114 					 * dumps.
6115 					 */
6116 					ereport(NOTICE,
6117 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6118 							 errmsg("RECHECK is no longer required"),
6119 							 errhint("Update your data type."),
6120 							 parser_errposition(@1)));
6121 					$$ = true;
6122 				}
6123 			| /*EMPTY*/						{ $$ = false; }
6124 		;
6125 
6126 
6127 CreateOpFamilyStmt:
6128 			CREATE OPERATOR FAMILY any_name USING access_method
6129 				{
6130 					CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
6131 					n->opfamilyname = $4;
6132 					n->amname = $6;
6133 					$$ = (Node *) n;
6134 				}
6135 		;
6136 
6137 AlterOpFamilyStmt:
6138 			ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
6139 				{
6140 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6141 					n->opfamilyname = $4;
6142 					n->amname = $6;
6143 					n->isDrop = false;
6144 					n->items = $8;
6145 					$$ = (Node *) n;
6146 				}
6147 			| ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
6148 				{
6149 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6150 					n->opfamilyname = $4;
6151 					n->amname = $6;
6152 					n->isDrop = true;
6153 					n->items = $8;
6154 					$$ = (Node *) n;
6155 				}
6156 		;
6157 
6158 opclass_drop_list:
6159 			opclass_drop							{ $$ = list_make1($1); }
6160 			| opclass_drop_list ',' opclass_drop	{ $$ = lappend($1, $3); }
6161 		;
6162 
6163 opclass_drop:
6164 			OPERATOR Iconst '(' type_list ')'
6165 				{
6166 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6167 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6168 					n->number = $2;
6169 					n->class_args = $4;
6170 					$$ = (Node *) n;
6171 				}
6172 			| FUNCTION Iconst '(' type_list ')'
6173 				{
6174 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6175 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6176 					n->number = $2;
6177 					n->class_args = $4;
6178 					$$ = (Node *) n;
6179 				}
6180 		;
6181 
6182 
6183 DropOpClassStmt:
6184 			DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
6185 				{
6186 					DropStmt *n = makeNode(DropStmt);
6187 					n->objects = list_make1(lcons(makeString($6), $4));
6188 					n->removeType = OBJECT_OPCLASS;
6189 					n->behavior = $7;
6190 					n->missing_ok = false;
6191 					n->concurrent = false;
6192 					$$ = (Node *) n;
6193 				}
6194 			| DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
6195 				{
6196 					DropStmt *n = makeNode(DropStmt);
6197 					n->objects = list_make1(lcons(makeString($8), $6));
6198 					n->removeType = OBJECT_OPCLASS;
6199 					n->behavior = $9;
6200 					n->missing_ok = true;
6201 					n->concurrent = false;
6202 					$$ = (Node *) n;
6203 				}
6204 		;
6205 
6206 DropOpFamilyStmt:
6207 			DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
6208 				{
6209 					DropStmt *n = makeNode(DropStmt);
6210 					n->objects = list_make1(lcons(makeString($6), $4));
6211 					n->removeType = OBJECT_OPFAMILY;
6212 					n->behavior = $7;
6213 					n->missing_ok = false;
6214 					n->concurrent = false;
6215 					$$ = (Node *) n;
6216 				}
6217 			| DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
6218 				{
6219 					DropStmt *n = makeNode(DropStmt);
6220 					n->objects = list_make1(lcons(makeString($8), $6));
6221 					n->removeType = OBJECT_OPFAMILY;
6222 					n->behavior = $9;
6223 					n->missing_ok = true;
6224 					n->concurrent = false;
6225 					$$ = (Node *) n;
6226 				}
6227 		;
6228 
6229 
6230 /*****************************************************************************
6231  *
6232  *		QUERY:
6233  *
6234  *		DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
6235  *		REASSIGN OWNED BY username [, username ...] TO username
6236  *
6237  *****************************************************************************/
6238 DropOwnedStmt:
6239 			DROP OWNED BY role_list opt_drop_behavior
6240 				{
6241 					DropOwnedStmt *n = makeNode(DropOwnedStmt);
6242 					n->roles = $4;
6243 					n->behavior = $5;
6244 					$$ = (Node *)n;
6245 				}
6246 		;
6247 
6248 ReassignOwnedStmt:
6249 			REASSIGN OWNED BY role_list TO RoleSpec
6250 				{
6251 					ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
6252 					n->roles = $4;
6253 					n->newrole = $6;
6254 					$$ = (Node *)n;
6255 				}
6256 		;
6257 
6258 /*****************************************************************************
6259  *
6260  *		QUERY:
6261  *
6262  *		DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
6263  *           [ RESTRICT | CASCADE ]
6264  *
6265  *****************************************************************************/
6266 
6267 DropStmt:	DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
6268 				{
6269 					DropStmt *n = makeNode(DropStmt);
6270 					n->removeType = $2;
6271 					n->missing_ok = true;
6272 					n->objects = $5;
6273 					n->behavior = $6;
6274 					n->concurrent = false;
6275 					$$ = (Node *)n;
6276 				}
6277 			| DROP drop_type_any_name any_name_list opt_drop_behavior
6278 				{
6279 					DropStmt *n = makeNode(DropStmt);
6280 					n->removeType = $2;
6281 					n->missing_ok = false;
6282 					n->objects = $3;
6283 					n->behavior = $4;
6284 					n->concurrent = false;
6285 					$$ = (Node *)n;
6286 				}
6287 			| DROP drop_type_name IF_P EXISTS name_list opt_drop_behavior
6288 				{
6289 					DropStmt *n = makeNode(DropStmt);
6290 					n->removeType = $2;
6291 					n->missing_ok = true;
6292 					n->objects = $5;
6293 					n->behavior = $6;
6294 					n->concurrent = false;
6295 					$$ = (Node *)n;
6296 				}
6297 			| DROP drop_type_name name_list opt_drop_behavior
6298 				{
6299 					DropStmt *n = makeNode(DropStmt);
6300 					n->removeType = $2;
6301 					n->missing_ok = false;
6302 					n->objects = $3;
6303 					n->behavior = $4;
6304 					n->concurrent = false;
6305 					$$ = (Node *)n;
6306 				}
6307 			| DROP drop_type_name_on_any_name name ON any_name opt_drop_behavior
6308 				{
6309 					DropStmt *n = makeNode(DropStmt);
6310 					n->removeType = $2;
6311 					n->objects = list_make1(lappend($5, makeString($3)));
6312 					n->behavior = $6;
6313 					n->missing_ok = false;
6314 					n->concurrent = false;
6315 					$$ = (Node *) n;
6316 				}
6317 			| DROP drop_type_name_on_any_name IF_P EXISTS name ON any_name opt_drop_behavior
6318 				{
6319 					DropStmt *n = makeNode(DropStmt);
6320 					n->removeType = $2;
6321 					n->objects = list_make1(lappend($7, makeString($5)));
6322 					n->behavior = $8;
6323 					n->missing_ok = true;
6324 					n->concurrent = false;
6325 					$$ = (Node *) n;
6326 				}
6327 			| DROP TYPE_P type_name_list opt_drop_behavior
6328 				{
6329 					DropStmt *n = makeNode(DropStmt);
6330 					n->removeType = OBJECT_TYPE;
6331 					n->missing_ok = false;
6332 					n->objects = $3;
6333 					n->behavior = $4;
6334 					n->concurrent = false;
6335 					$$ = (Node *) n;
6336 				}
6337 			| DROP TYPE_P IF_P EXISTS type_name_list opt_drop_behavior
6338 				{
6339 					DropStmt *n = makeNode(DropStmt);
6340 					n->removeType = OBJECT_TYPE;
6341 					n->missing_ok = true;
6342 					n->objects = $5;
6343 					n->behavior = $6;
6344 					n->concurrent = false;
6345 					$$ = (Node *) n;
6346 				}
6347 			| DROP DOMAIN_P type_name_list opt_drop_behavior
6348 				{
6349 					DropStmt *n = makeNode(DropStmt);
6350 					n->removeType = OBJECT_DOMAIN;
6351 					n->missing_ok = false;
6352 					n->objects = $3;
6353 					n->behavior = $4;
6354 					n->concurrent = false;
6355 					$$ = (Node *) n;
6356 				}
6357 			| DROP DOMAIN_P IF_P EXISTS type_name_list opt_drop_behavior
6358 				{
6359 					DropStmt *n = makeNode(DropStmt);
6360 					n->removeType = OBJECT_DOMAIN;
6361 					n->missing_ok = true;
6362 					n->objects = $5;
6363 					n->behavior = $6;
6364 					n->concurrent = false;
6365 					$$ = (Node *) n;
6366 				}
6367 			| DROP INDEX CONCURRENTLY any_name_list opt_drop_behavior
6368 				{
6369 					DropStmt *n = makeNode(DropStmt);
6370 					n->removeType = OBJECT_INDEX;
6371 					n->missing_ok = false;
6372 					n->objects = $4;
6373 					n->behavior = $5;
6374 					n->concurrent = true;
6375 					$$ = (Node *)n;
6376 				}
6377 			| DROP INDEX CONCURRENTLY IF_P EXISTS any_name_list opt_drop_behavior
6378 				{
6379 					DropStmt *n = makeNode(DropStmt);
6380 					n->removeType = OBJECT_INDEX;
6381 					n->missing_ok = true;
6382 					n->objects = $6;
6383 					n->behavior = $7;
6384 					n->concurrent = true;
6385 					$$ = (Node *)n;
6386 				}
6387 		;
6388 
6389 /* object types taking any_name_list */
6390 drop_type_any_name:
6391 			TABLE									{ $$ = OBJECT_TABLE; }
6392 			| SEQUENCE								{ $$ = OBJECT_SEQUENCE; }
6393 			| VIEW									{ $$ = OBJECT_VIEW; }
6394 			| MATERIALIZED VIEW						{ $$ = OBJECT_MATVIEW; }
6395 			| INDEX									{ $$ = OBJECT_INDEX; }
6396 			| FOREIGN TABLE							{ $$ = OBJECT_FOREIGN_TABLE; }
6397 			| COLLATION								{ $$ = OBJECT_COLLATION; }
6398 			| CONVERSION_P							{ $$ = OBJECT_CONVERSION; }
6399 			| STATISTICS							{ $$ = OBJECT_STATISTIC_EXT; }
6400 			| TEXT_P SEARCH PARSER					{ $$ = OBJECT_TSPARSER; }
6401 			| TEXT_P SEARCH DICTIONARY				{ $$ = OBJECT_TSDICTIONARY; }
6402 			| TEXT_P SEARCH TEMPLATE				{ $$ = OBJECT_TSTEMPLATE; }
6403 			| TEXT_P SEARCH CONFIGURATION			{ $$ = OBJECT_TSCONFIGURATION; }
6404 		;
6405 
6406 /* object types taking name_list */
6407 drop_type_name:
6408 			ACCESS METHOD							{ $$ = OBJECT_ACCESS_METHOD; }
6409 			| EVENT TRIGGER							{ $$ = OBJECT_EVENT_TRIGGER; }
6410 			| EXTENSION								{ $$ = OBJECT_EXTENSION; }
6411 			| FOREIGN DATA_P WRAPPER				{ $$ = OBJECT_FDW; }
6412 			| PUBLICATION							{ $$ = OBJECT_PUBLICATION; }
6413 			| SCHEMA								{ $$ = OBJECT_SCHEMA; }
6414 			| SERVER								{ $$ = OBJECT_FOREIGN_SERVER; }
6415 		;
6416 
6417 /* object types attached to a table */
6418 drop_type_name_on_any_name:
6419 			POLICY									{ $$ = OBJECT_POLICY; }
6420 			| RULE									{ $$ = OBJECT_RULE; }
6421 			| TRIGGER								{ $$ = OBJECT_TRIGGER; }
6422 		;
6423 
6424 any_name_list:
6425 			any_name								{ $$ = list_make1($1); }
6426 			| any_name_list ',' any_name			{ $$ = lappend($1, $3); }
6427 		;
6428 
6429 any_name:	ColId						{ $$ = list_make1(makeString($1)); }
6430 			| ColId attrs				{ $$ = lcons(makeString($1), $2); }
6431 		;
6432 
6433 attrs:		'.' attr_name
6434 					{ $$ = list_make1(makeString($2)); }
6435 			| attrs '.' attr_name
6436 					{ $$ = lappend($1, makeString($3)); }
6437 		;
6438 
6439 type_name_list:
6440 			Typename								{ $$ = list_make1($1); }
6441 			| type_name_list ',' Typename			{ $$ = lappend($1, $3); }
6442 
6443 /*****************************************************************************
6444  *
6445  *		QUERY:
6446  *				truncate table relname1, relname2, ...
6447  *
6448  *****************************************************************************/
6449 
6450 TruncateStmt:
6451 			TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
6452 				{
6453 					TruncateStmt *n = makeNode(TruncateStmt);
6454 					n->relations = $3;
6455 					n->restart_seqs = $4;
6456 					n->behavior = $5;
6457 					$$ = (Node *)n;
6458 				}
6459 		;
6460 
6461 opt_restart_seqs:
6462 			CONTINUE_P IDENTITY_P		{ $$ = false; }
6463 			| RESTART IDENTITY_P		{ $$ = true; }
6464 			| /* EMPTY */				{ $$ = false; }
6465 		;
6466 
6467 /*****************************************************************************
6468  *
6469  *	The COMMENT ON statement can take different forms based upon the type of
6470  *	the object associated with the comment. The form of the statement is:
6471  *
6472  *	COMMENT ON [ [ ACCESS METHOD | CONVERSION | COLLATION |
6473  *                 DATABASE | DOMAIN |
6474  *                 EXTENSION | EVENT TRIGGER | FOREIGN DATA WRAPPER |
6475  *                 FOREIGN TABLE | INDEX | [PROCEDURAL] LANGUAGE |
6476  *                 MATERIALIZED VIEW | POLICY | ROLE | SCHEMA | SEQUENCE |
6477  *                 SERVER | TABLE | TABLESPACE |
6478  *                 TEXT SEARCH CONFIGURATION | TEXT SEARCH DICTIONARY |
6479  *                 TEXT SEARCH PARSER | TEXT SEARCH TEMPLATE | TYPE |
6480  *                 VIEW] <objname> |
6481  *				 AGGREGATE <aggname> (arg1, ...) |
6482  *				 CAST (<src type> AS <dst type>) |
6483  *				 COLUMN <relname>.<colname> |
6484  *				 CONSTRAINT <constraintname> ON <relname> |
6485  *				 CONSTRAINT <constraintname> ON DOMAIN <domainname> |
6486  *				 FUNCTION <funcname> (arg1, arg2, ...) |
6487  *				 LARGE OBJECT <oid> |
6488  *				 OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
6489  *				 OPERATOR CLASS <name> USING <access-method> |
6490  *				 OPERATOR FAMILY <name> USING <access-method> |
6491  *				 RULE <rulename> ON <relname> |
6492  *				 TRIGGER <triggername> ON <relname> ]
6493  *			   IS { 'text' | NULL }
6494  *
6495  *****************************************************************************/
6496 
6497 CommentStmt:
6498 			COMMENT ON comment_type_any_name any_name IS comment_text
6499 				{
6500 					CommentStmt *n = makeNode(CommentStmt);
6501 					n->objtype = $3;
6502 					n->object = (Node *) $4;
6503 					n->comment = $6;
6504 					$$ = (Node *) n;
6505 				}
6506 			| COMMENT ON comment_type_name name IS comment_text
6507 				{
6508 					CommentStmt *n = makeNode(CommentStmt);
6509 					n->objtype = $3;
6510 					n->object = (Node *) makeString($4);
6511 					n->comment = $6;
6512 					$$ = (Node *) n;
6513 				}
6514 			| COMMENT ON TYPE_P Typename IS comment_text
6515 				{
6516 					CommentStmt *n = makeNode(CommentStmt);
6517 					n->objtype = OBJECT_TYPE;
6518 					n->object = (Node *) $4;
6519 					n->comment = $6;
6520 					$$ = (Node *) n;
6521 				}
6522 			| COMMENT ON DOMAIN_P Typename IS comment_text
6523 				{
6524 					CommentStmt *n = makeNode(CommentStmt);
6525 					n->objtype = OBJECT_DOMAIN;
6526 					n->object = (Node *) $4;
6527 					n->comment = $6;
6528 					$$ = (Node *) n;
6529 				}
6530 			| COMMENT ON AGGREGATE aggregate_with_argtypes IS comment_text
6531 				{
6532 					CommentStmt *n = makeNode(CommentStmt);
6533 					n->objtype = OBJECT_AGGREGATE;
6534 					n->object = (Node *) $4;
6535 					n->comment = $6;
6536 					$$ = (Node *) n;
6537 				}
6538 			| COMMENT ON FUNCTION function_with_argtypes IS comment_text
6539 				{
6540 					CommentStmt *n = makeNode(CommentStmt);
6541 					n->objtype = OBJECT_FUNCTION;
6542 					n->object = (Node *) $4;
6543 					n->comment = $6;
6544 					$$ = (Node *) n;
6545 				}
6546 			| COMMENT ON OPERATOR operator_with_argtypes IS comment_text
6547 				{
6548 					CommentStmt *n = makeNode(CommentStmt);
6549 					n->objtype = OBJECT_OPERATOR;
6550 					n->object = (Node *) $4;
6551 					n->comment = $6;
6552 					$$ = (Node *) n;
6553 				}
6554 			| COMMENT ON CONSTRAINT name ON any_name IS comment_text
6555 				{
6556 					CommentStmt *n = makeNode(CommentStmt);
6557 					n->objtype = OBJECT_TABCONSTRAINT;
6558 					n->object = (Node *) lappend($6, makeString($4));
6559 					n->comment = $8;
6560 					$$ = (Node *) n;
6561 				}
6562 			| COMMENT ON CONSTRAINT name ON DOMAIN_P any_name IS comment_text
6563 				{
6564 					CommentStmt *n = makeNode(CommentStmt);
6565 					n->objtype = OBJECT_DOMCONSTRAINT;
6566 					/*
6567 					 * should use Typename not any_name in the production, but
6568 					 * there's a shift/reduce conflict if we do that, so fix it
6569 					 * up here.
6570 					 */
6571 					n->object = (Node *) list_make2(makeTypeNameFromNameList($7), makeString($4));
6572 					n->comment = $9;
6573 					$$ = (Node *) n;
6574 				}
6575 			| COMMENT ON POLICY name ON any_name IS comment_text
6576 				{
6577 					CommentStmt *n = makeNode(CommentStmt);
6578 					n->objtype = OBJECT_POLICY;
6579 					n->object = (Node *) lappend($6, makeString($4));
6580 					n->comment = $8;
6581 					$$ = (Node *) n;
6582 				}
6583 			| COMMENT ON PROCEDURE function_with_argtypes IS comment_text
6584 				{
6585 					CommentStmt *n = makeNode(CommentStmt);
6586 					n->objtype = OBJECT_PROCEDURE;
6587 					n->object = (Node *) $4;
6588 					n->comment = $6;
6589 					$$ = (Node *) n;
6590 				}
6591 			| COMMENT ON ROUTINE function_with_argtypes IS comment_text
6592 				{
6593 					CommentStmt *n = makeNode(CommentStmt);
6594 					n->objtype = OBJECT_ROUTINE;
6595 					n->object = (Node *) $4;
6596 					n->comment = $6;
6597 					$$ = (Node *) n;
6598 				}
6599 			| COMMENT ON RULE name ON any_name IS comment_text
6600 				{
6601 					CommentStmt *n = makeNode(CommentStmt);
6602 					n->objtype = OBJECT_RULE;
6603 					n->object = (Node *) lappend($6, makeString($4));
6604 					n->comment = $8;
6605 					$$ = (Node *) n;
6606 				}
6607 			| COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
6608 				{
6609 					CommentStmt *n = makeNode(CommentStmt);
6610 					n->objtype = OBJECT_TRANSFORM;
6611 					n->object = (Node *) list_make2($5, makeString($7));
6612 					n->comment = $9;
6613 					$$ = (Node *) n;
6614 				}
6615 			| COMMENT ON TRIGGER name ON any_name IS comment_text
6616 				{
6617 					CommentStmt *n = makeNode(CommentStmt);
6618 					n->objtype = OBJECT_TRIGGER;
6619 					n->object = (Node *) lappend($6, makeString($4));
6620 					n->comment = $8;
6621 					$$ = (Node *) n;
6622 				}
6623 			| COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
6624 				{
6625 					CommentStmt *n = makeNode(CommentStmt);
6626 					n->objtype = OBJECT_OPCLASS;
6627 					n->object = (Node *) lcons(makeString($7), $5);
6628 					n->comment = $9;
6629 					$$ = (Node *) n;
6630 				}
6631 			| COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
6632 				{
6633 					CommentStmt *n = makeNode(CommentStmt);
6634 					n->objtype = OBJECT_OPFAMILY;
6635 					n->object = (Node *) lcons(makeString($7), $5);
6636 					n->comment = $9;
6637 					$$ = (Node *) n;
6638 				}
6639 			| COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
6640 				{
6641 					CommentStmt *n = makeNode(CommentStmt);
6642 					n->objtype = OBJECT_LARGEOBJECT;
6643 					n->object = (Node *) $5;
6644 					n->comment = $7;
6645 					$$ = (Node *) n;
6646 				}
6647 			| COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
6648 				{
6649 					CommentStmt *n = makeNode(CommentStmt);
6650 					n->objtype = OBJECT_CAST;
6651 					n->object = (Node *) list_make2($5, $7);
6652 					n->comment = $10;
6653 					$$ = (Node *) n;
6654 				}
6655 		;
6656 
6657 /* object types taking any_name */
6658 comment_type_any_name:
6659 			COLUMN								{ $$ = OBJECT_COLUMN; }
6660 			| INDEX								{ $$ = OBJECT_INDEX; }
6661 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6662 			| STATISTICS						{ $$ = OBJECT_STATISTIC_EXT; }
6663 			| TABLE								{ $$ = OBJECT_TABLE; }
6664 			| VIEW								{ $$ = OBJECT_VIEW; }
6665 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6666 			| COLLATION							{ $$ = OBJECT_COLLATION; }
6667 			| CONVERSION_P						{ $$ = OBJECT_CONVERSION; }
6668 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6669 			| TEXT_P SEARCH CONFIGURATION		{ $$ = OBJECT_TSCONFIGURATION; }
6670 			| TEXT_P SEARCH DICTIONARY			{ $$ = OBJECT_TSDICTIONARY; }
6671 			| TEXT_P SEARCH PARSER				{ $$ = OBJECT_TSPARSER; }
6672 			| TEXT_P SEARCH TEMPLATE			{ $$ = OBJECT_TSTEMPLATE; }
6673 		;
6674 
6675 /* object types taking name */
6676 comment_type_name:
6677 			ACCESS METHOD						{ $$ = OBJECT_ACCESS_METHOD; }
6678 			| DATABASE							{ $$ = OBJECT_DATABASE; }
6679 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6680 			| EXTENSION							{ $$ = OBJECT_EXTENSION; }
6681 			| FOREIGN DATA_P WRAPPER			{ $$ = OBJECT_FDW; }
6682 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6683 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6684 			| ROLE								{ $$ = OBJECT_ROLE; }
6685 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6686 			| SERVER							{ $$ = OBJECT_FOREIGN_SERVER; }
6687 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6688 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6689 		;
6690 
6691 comment_text:
6692 			Sconst								{ $$ = $1; }
6693 			| NULL_P							{ $$ = NULL; }
6694 		;
6695 
6696 
6697 /*****************************************************************************
6698  *
6699  *  SECURITY LABEL [FOR <provider>] ON <object> IS <label>
6700  *
6701  *  As with COMMENT ON, <object> can refer to various types of database
6702  *  objects (e.g. TABLE, COLUMN, etc.).
6703  *
6704  *****************************************************************************/
6705 
6706 SecLabelStmt:
6707 			SECURITY LABEL opt_provider ON security_label_type_any_name any_name
6708 			IS security_label
6709 				{
6710 					SecLabelStmt *n = makeNode(SecLabelStmt);
6711 					n->provider = $3;
6712 					n->objtype = $5;
6713 					n->object = (Node *) $6;
6714 					n->label = $8;
6715 					$$ = (Node *) n;
6716 				}
6717 			| SECURITY LABEL opt_provider ON security_label_type_name name
6718 			  IS security_label
6719 				{
6720 					SecLabelStmt *n = makeNode(SecLabelStmt);
6721 					n->provider = $3;
6722 					n->objtype = $5;
6723 					n->object = (Node *) makeString($6);
6724 					n->label = $8;
6725 					$$ = (Node *) n;
6726 				}
6727 			| SECURITY LABEL opt_provider ON TYPE_P Typename
6728 			  IS security_label
6729 				{
6730 					SecLabelStmt *n = makeNode(SecLabelStmt);
6731 					n->provider = $3;
6732 					n->objtype = OBJECT_TYPE;
6733 					n->object = (Node *) $6;
6734 					n->label = $8;
6735 					$$ = (Node *) n;
6736 				}
6737 			| SECURITY LABEL opt_provider ON DOMAIN_P Typename
6738 			  IS security_label
6739 				{
6740 					SecLabelStmt *n = makeNode(SecLabelStmt);
6741 					n->provider = $3;
6742 					n->objtype = OBJECT_DOMAIN;
6743 					n->object = (Node *) $6;
6744 					n->label = $8;
6745 					$$ = (Node *) n;
6746 				}
6747 			| SECURITY LABEL opt_provider ON AGGREGATE aggregate_with_argtypes
6748 			  IS security_label
6749 				{
6750 					SecLabelStmt *n = makeNode(SecLabelStmt);
6751 					n->provider = $3;
6752 					n->objtype = OBJECT_AGGREGATE;
6753 					n->object = (Node *) $6;
6754 					n->label = $8;
6755 					$$ = (Node *) n;
6756 				}
6757 			| SECURITY LABEL opt_provider ON FUNCTION function_with_argtypes
6758 			  IS security_label
6759 				{
6760 					SecLabelStmt *n = makeNode(SecLabelStmt);
6761 					n->provider = $3;
6762 					n->objtype = OBJECT_FUNCTION;
6763 					n->object = (Node *) $6;
6764 					n->label = $8;
6765 					$$ = (Node *) n;
6766 				}
6767 			| SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
6768 			  IS security_label
6769 				{
6770 					SecLabelStmt *n = makeNode(SecLabelStmt);
6771 					n->provider = $3;
6772 					n->objtype = OBJECT_LARGEOBJECT;
6773 					n->object = (Node *) $7;
6774 					n->label = $9;
6775 					$$ = (Node *) n;
6776 				}
6777 			| SECURITY LABEL opt_provider ON PROCEDURE function_with_argtypes
6778 			  IS security_label
6779 				{
6780 					SecLabelStmt *n = makeNode(SecLabelStmt);
6781 					n->provider = $3;
6782 					n->objtype = OBJECT_PROCEDURE;
6783 					n->object = (Node *) $6;
6784 					n->label = $8;
6785 					$$ = (Node *) n;
6786 				}
6787 			| SECURITY LABEL opt_provider ON ROUTINE function_with_argtypes
6788 			  IS security_label
6789 				{
6790 					SecLabelStmt *n = makeNode(SecLabelStmt);
6791 					n->provider = $3;
6792 					n->objtype = OBJECT_ROUTINE;
6793 					n->object = (Node *) $6;
6794 					n->label = $8;
6795 					$$ = (Node *) n;
6796 				}
6797 		;
6798 
6799 opt_provider:	FOR NonReservedWord_or_Sconst	{ $$ = $2; }
6800 				| /* empty */					{ $$ = NULL; }
6801 		;
6802 
6803 /* object types taking any_name */
6804 security_label_type_any_name:
6805 			COLUMN								{ $$ = OBJECT_COLUMN; }
6806 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6807 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6808 			| TABLE								{ $$ = OBJECT_TABLE; }
6809 			| VIEW								{ $$ = OBJECT_VIEW; }
6810 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6811 		;
6812 
6813 /* object types taking name */
6814 security_label_type_name:
6815 			DATABASE							{ $$ = OBJECT_DATABASE; }
6816 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6817 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6818 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6819 			| ROLE								{ $$ = OBJECT_ROLE; }
6820 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6821 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6822 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6823 		;
6824 
6825 security_label:	Sconst				{ $$ = $1; }
6826 				| NULL_P			{ $$ = NULL; }
6827 		;
6828 
6829 /*****************************************************************************
6830  *
6831  *		QUERY:
6832  *			fetch/move
6833  *
6834  *****************************************************************************/
6835 
6836 FetchStmt:	FETCH fetch_args
6837 				{
6838 					FetchStmt *n = (FetchStmt *) $2;
6839 					n->ismove = false;
6840 					$$ = (Node *)n;
6841 				}
6842 			| MOVE fetch_args
6843 				{
6844 					FetchStmt *n = (FetchStmt *) $2;
6845 					n->ismove = true;
6846 					$$ = (Node *)n;
6847 				}
6848 		;
6849 
6850 fetch_args:	cursor_name
6851 				{
6852 					FetchStmt *n = makeNode(FetchStmt);
6853 					n->portalname = $1;
6854 					n->direction = FETCH_FORWARD;
6855 					n->howMany = 1;
6856 					$$ = (Node *)n;
6857 				}
6858 			| from_in cursor_name
6859 				{
6860 					FetchStmt *n = makeNode(FetchStmt);
6861 					n->portalname = $2;
6862 					n->direction = FETCH_FORWARD;
6863 					n->howMany = 1;
6864 					$$ = (Node *)n;
6865 				}
6866 			| NEXT opt_from_in cursor_name
6867 				{
6868 					FetchStmt *n = makeNode(FetchStmt);
6869 					n->portalname = $3;
6870 					n->direction = FETCH_FORWARD;
6871 					n->howMany = 1;
6872 					$$ = (Node *)n;
6873 				}
6874 			| PRIOR opt_from_in cursor_name
6875 				{
6876 					FetchStmt *n = makeNode(FetchStmt);
6877 					n->portalname = $3;
6878 					n->direction = FETCH_BACKWARD;
6879 					n->howMany = 1;
6880 					$$ = (Node *)n;
6881 				}
6882 			| FIRST_P opt_from_in cursor_name
6883 				{
6884 					FetchStmt *n = makeNode(FetchStmt);
6885 					n->portalname = $3;
6886 					n->direction = FETCH_ABSOLUTE;
6887 					n->howMany = 1;
6888 					$$ = (Node *)n;
6889 				}
6890 			| LAST_P opt_from_in cursor_name
6891 				{
6892 					FetchStmt *n = makeNode(FetchStmt);
6893 					n->portalname = $3;
6894 					n->direction = FETCH_ABSOLUTE;
6895 					n->howMany = -1;
6896 					$$ = (Node *)n;
6897 				}
6898 			| ABSOLUTE_P SignedIconst opt_from_in cursor_name
6899 				{
6900 					FetchStmt *n = makeNode(FetchStmt);
6901 					n->portalname = $4;
6902 					n->direction = FETCH_ABSOLUTE;
6903 					n->howMany = $2;
6904 					$$ = (Node *)n;
6905 				}
6906 			| RELATIVE_P SignedIconst opt_from_in cursor_name
6907 				{
6908 					FetchStmt *n = makeNode(FetchStmt);
6909 					n->portalname = $4;
6910 					n->direction = FETCH_RELATIVE;
6911 					n->howMany = $2;
6912 					$$ = (Node *)n;
6913 				}
6914 			| SignedIconst opt_from_in cursor_name
6915 				{
6916 					FetchStmt *n = makeNode(FetchStmt);
6917 					n->portalname = $3;
6918 					n->direction = FETCH_FORWARD;
6919 					n->howMany = $1;
6920 					$$ = (Node *)n;
6921 				}
6922 			| ALL opt_from_in cursor_name
6923 				{
6924 					FetchStmt *n = makeNode(FetchStmt);
6925 					n->portalname = $3;
6926 					n->direction = FETCH_FORWARD;
6927 					n->howMany = FETCH_ALL;
6928 					$$ = (Node *)n;
6929 				}
6930 			| FORWARD opt_from_in cursor_name
6931 				{
6932 					FetchStmt *n = makeNode(FetchStmt);
6933 					n->portalname = $3;
6934 					n->direction = FETCH_FORWARD;
6935 					n->howMany = 1;
6936 					$$ = (Node *)n;
6937 				}
6938 			| FORWARD SignedIconst opt_from_in cursor_name
6939 				{
6940 					FetchStmt *n = makeNode(FetchStmt);
6941 					n->portalname = $4;
6942 					n->direction = FETCH_FORWARD;
6943 					n->howMany = $2;
6944 					$$ = (Node *)n;
6945 				}
6946 			| FORWARD ALL opt_from_in cursor_name
6947 				{
6948 					FetchStmt *n = makeNode(FetchStmt);
6949 					n->portalname = $4;
6950 					n->direction = FETCH_FORWARD;
6951 					n->howMany = FETCH_ALL;
6952 					$$ = (Node *)n;
6953 				}
6954 			| BACKWARD opt_from_in cursor_name
6955 				{
6956 					FetchStmt *n = makeNode(FetchStmt);
6957 					n->portalname = $3;
6958 					n->direction = FETCH_BACKWARD;
6959 					n->howMany = 1;
6960 					$$ = (Node *)n;
6961 				}
6962 			| BACKWARD SignedIconst opt_from_in cursor_name
6963 				{
6964 					FetchStmt *n = makeNode(FetchStmt);
6965 					n->portalname = $4;
6966 					n->direction = FETCH_BACKWARD;
6967 					n->howMany = $2;
6968 					$$ = (Node *)n;
6969 				}
6970 			| BACKWARD ALL opt_from_in cursor_name
6971 				{
6972 					FetchStmt *n = makeNode(FetchStmt);
6973 					n->portalname = $4;
6974 					n->direction = FETCH_BACKWARD;
6975 					n->howMany = FETCH_ALL;
6976 					$$ = (Node *)n;
6977 				}
6978 		;
6979 
6980 from_in:	FROM									{}
6981 			| IN_P									{}
6982 		;
6983 
6984 opt_from_in:	from_in								{}
6985 			| /* EMPTY */							{}
6986 		;
6987 
6988 
6989 /*****************************************************************************
6990  *
6991  * GRANT and REVOKE statements
6992  *
6993  *****************************************************************************/
6994 
6995 GrantStmt:	GRANT privileges ON privilege_target TO grantee_list
6996 			opt_grant_grant_option
6997 				{
6998 					GrantStmt *n = makeNode(GrantStmt);
6999 					n->is_grant = true;
7000 					n->privileges = $2;
7001 					n->targtype = ($4)->targtype;
7002 					n->objtype = ($4)->objtype;
7003 					n->objects = ($4)->objs;
7004 					n->grantees = $6;
7005 					n->grant_option = $7;
7006 					$$ = (Node*)n;
7007 				}
7008 		;
7009 
7010 RevokeStmt:
7011 			REVOKE privileges ON privilege_target
7012 			FROM grantee_list opt_drop_behavior
7013 				{
7014 					GrantStmt *n = makeNode(GrantStmt);
7015 					n->is_grant = false;
7016 					n->grant_option = false;
7017 					n->privileges = $2;
7018 					n->targtype = ($4)->targtype;
7019 					n->objtype = ($4)->objtype;
7020 					n->objects = ($4)->objs;
7021 					n->grantees = $6;
7022 					n->behavior = $7;
7023 					$$ = (Node *)n;
7024 				}
7025 			| REVOKE GRANT OPTION FOR privileges ON privilege_target
7026 			FROM grantee_list opt_drop_behavior
7027 				{
7028 					GrantStmt *n = makeNode(GrantStmt);
7029 					n->is_grant = false;
7030 					n->grant_option = true;
7031 					n->privileges = $5;
7032 					n->targtype = ($7)->targtype;
7033 					n->objtype = ($7)->objtype;
7034 					n->objects = ($7)->objs;
7035 					n->grantees = $9;
7036 					n->behavior = $10;
7037 					$$ = (Node *)n;
7038 				}
7039 		;
7040 
7041 
7042 /*
7043  * Privilege names are represented as strings; the validity of the privilege
7044  * names gets checked at execution.  This is a bit annoying but we have little
7045  * choice because of the syntactic conflict with lists of role names in
7046  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
7047  * production any reserved keywords that need to be usable as privilege names.
7048  */
7049 
7050 /* either ALL [PRIVILEGES] or a list of individual privileges */
7051 privileges: privilege_list
7052 				{ $$ = $1; }
7053 			| ALL
7054 				{ $$ = NIL; }
7055 			| ALL PRIVILEGES
7056 				{ $$ = NIL; }
7057 			| ALL '(' columnList ')'
7058 				{
7059 					AccessPriv *n = makeNode(AccessPriv);
7060 					n->priv_name = NULL;
7061 					n->cols = $3;
7062 					$$ = list_make1(n);
7063 				}
7064 			| ALL PRIVILEGES '(' columnList ')'
7065 				{
7066 					AccessPriv *n = makeNode(AccessPriv);
7067 					n->priv_name = NULL;
7068 					n->cols = $4;
7069 					$$ = list_make1(n);
7070 				}
7071 		;
7072 
7073 privilege_list:	privilege							{ $$ = list_make1($1); }
7074 			| privilege_list ',' privilege			{ $$ = lappend($1, $3); }
7075 		;
7076 
7077 privilege:	SELECT opt_column_list
7078 			{
7079 				AccessPriv *n = makeNode(AccessPriv);
7080 				n->priv_name = pstrdup($1);
7081 				n->cols = $2;
7082 				$$ = n;
7083 			}
7084 		| REFERENCES opt_column_list
7085 			{
7086 				AccessPriv *n = makeNode(AccessPriv);
7087 				n->priv_name = pstrdup($1);
7088 				n->cols = $2;
7089 				$$ = n;
7090 			}
7091 		| CREATE opt_column_list
7092 			{
7093 				AccessPriv *n = makeNode(AccessPriv);
7094 				n->priv_name = pstrdup($1);
7095 				n->cols = $2;
7096 				$$ = n;
7097 			}
7098 		| ColId opt_column_list
7099 			{
7100 				AccessPriv *n = makeNode(AccessPriv);
7101 				n->priv_name = $1;
7102 				n->cols = $2;
7103 				$$ = n;
7104 			}
7105 		;
7106 
7107 
7108 /* Don't bother trying to fold the first two rules into one using
7109  * opt_table.  You're going to get conflicts.
7110  */
7111 privilege_target:
7112 			qualified_name_list
7113 				{
7114 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7115 					n->targtype = ACL_TARGET_OBJECT;
7116 					n->objtype = OBJECT_TABLE;
7117 					n->objs = $1;
7118 					$$ = n;
7119 				}
7120 			| TABLE qualified_name_list
7121 				{
7122 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7123 					n->targtype = ACL_TARGET_OBJECT;
7124 					n->objtype = OBJECT_TABLE;
7125 					n->objs = $2;
7126 					$$ = n;
7127 				}
7128 			| SEQUENCE qualified_name_list
7129 				{
7130 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7131 					n->targtype = ACL_TARGET_OBJECT;
7132 					n->objtype = OBJECT_SEQUENCE;
7133 					n->objs = $2;
7134 					$$ = n;
7135 				}
7136 			| FOREIGN DATA_P WRAPPER name_list
7137 				{
7138 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7139 					n->targtype = ACL_TARGET_OBJECT;
7140 					n->objtype = OBJECT_FDW;
7141 					n->objs = $4;
7142 					$$ = n;
7143 				}
7144 			| FOREIGN SERVER name_list
7145 				{
7146 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7147 					n->targtype = ACL_TARGET_OBJECT;
7148 					n->objtype = OBJECT_FOREIGN_SERVER;
7149 					n->objs = $3;
7150 					$$ = n;
7151 				}
7152 			| FUNCTION function_with_argtypes_list
7153 				{
7154 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7155 					n->targtype = ACL_TARGET_OBJECT;
7156 					n->objtype = OBJECT_FUNCTION;
7157 					n->objs = $2;
7158 					$$ = n;
7159 				}
7160 			| PROCEDURE function_with_argtypes_list
7161 				{
7162 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7163 					n->targtype = ACL_TARGET_OBJECT;
7164 					n->objtype = OBJECT_PROCEDURE;
7165 					n->objs = $2;
7166 					$$ = n;
7167 				}
7168 			| ROUTINE function_with_argtypes_list
7169 				{
7170 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7171 					n->targtype = ACL_TARGET_OBJECT;
7172 					n->objtype = OBJECT_ROUTINE;
7173 					n->objs = $2;
7174 					$$ = n;
7175 				}
7176 			| DATABASE name_list
7177 				{
7178 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7179 					n->targtype = ACL_TARGET_OBJECT;
7180 					n->objtype = OBJECT_DATABASE;
7181 					n->objs = $2;
7182 					$$ = n;
7183 				}
7184 			| DOMAIN_P any_name_list
7185 				{
7186 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7187 					n->targtype = ACL_TARGET_OBJECT;
7188 					n->objtype = OBJECT_DOMAIN;
7189 					n->objs = $2;
7190 					$$ = n;
7191 				}
7192 			| LANGUAGE name_list
7193 				{
7194 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7195 					n->targtype = ACL_TARGET_OBJECT;
7196 					n->objtype = OBJECT_LANGUAGE;
7197 					n->objs = $2;
7198 					$$ = n;
7199 				}
7200 			| LARGE_P OBJECT_P NumericOnly_list
7201 				{
7202 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7203 					n->targtype = ACL_TARGET_OBJECT;
7204 					n->objtype = OBJECT_LARGEOBJECT;
7205 					n->objs = $3;
7206 					$$ = n;
7207 				}
7208 			| SCHEMA name_list
7209 				{
7210 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7211 					n->targtype = ACL_TARGET_OBJECT;
7212 					n->objtype = OBJECT_SCHEMA;
7213 					n->objs = $2;
7214 					$$ = n;
7215 				}
7216 			| TABLESPACE name_list
7217 				{
7218 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7219 					n->targtype = ACL_TARGET_OBJECT;
7220 					n->objtype = OBJECT_TABLESPACE;
7221 					n->objs = $2;
7222 					$$ = n;
7223 				}
7224 			| TYPE_P any_name_list
7225 				{
7226 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7227 					n->targtype = ACL_TARGET_OBJECT;
7228 					n->objtype = OBJECT_TYPE;
7229 					n->objs = $2;
7230 					$$ = n;
7231 				}
7232 			| ALL TABLES IN_P SCHEMA name_list
7233 				{
7234 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7235 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7236 					n->objtype = OBJECT_TABLE;
7237 					n->objs = $5;
7238 					$$ = n;
7239 				}
7240 			| ALL SEQUENCES IN_P SCHEMA name_list
7241 				{
7242 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7243 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7244 					n->objtype = OBJECT_SEQUENCE;
7245 					n->objs = $5;
7246 					$$ = n;
7247 				}
7248 			| ALL FUNCTIONS IN_P SCHEMA name_list
7249 				{
7250 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7251 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7252 					n->objtype = OBJECT_FUNCTION;
7253 					n->objs = $5;
7254 					$$ = n;
7255 				}
7256 			| ALL PROCEDURES IN_P SCHEMA name_list
7257 				{
7258 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7259 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7260 					n->objtype = OBJECT_PROCEDURE;
7261 					n->objs = $5;
7262 					$$ = n;
7263 				}
7264 			| ALL ROUTINES IN_P SCHEMA name_list
7265 				{
7266 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7267 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7268 					n->objtype = OBJECT_ROUTINE;
7269 					n->objs = $5;
7270 					$$ = n;
7271 				}
7272 		;
7273 
7274 
7275 grantee_list:
7276 			grantee									{ $$ = list_make1($1); }
7277 			| grantee_list ',' grantee				{ $$ = lappend($1, $3); }
7278 		;
7279 
7280 grantee:
7281 			RoleSpec								{ $$ = $1; }
7282 			| GROUP_P RoleSpec						{ $$ = $2; }
7283 		;
7284 
7285 
7286 opt_grant_grant_option:
7287 			WITH GRANT OPTION { $$ = true; }
7288 			| /*EMPTY*/ { $$ = false; }
7289 		;
7290 
7291 /*****************************************************************************
7292  *
7293  * GRANT and REVOKE ROLE statements
7294  *
7295  *****************************************************************************/
7296 
7297 GrantRoleStmt:
7298 			GRANT privilege_list TO role_list opt_grant_admin_option opt_granted_by
7299 				{
7300 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7301 					n->is_grant = true;
7302 					n->granted_roles = $2;
7303 					n->grantee_roles = $4;
7304 					n->admin_opt = $5;
7305 					n->grantor = $6;
7306 					$$ = (Node*)n;
7307 				}
7308 		;
7309 
7310 RevokeRoleStmt:
7311 			REVOKE privilege_list FROM role_list opt_granted_by opt_drop_behavior
7312 				{
7313 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7314 					n->is_grant = false;
7315 					n->admin_opt = false;
7316 					n->granted_roles = $2;
7317 					n->grantee_roles = $4;
7318 					n->behavior = $6;
7319 					$$ = (Node*)n;
7320 				}
7321 			| REVOKE ADMIN OPTION FOR privilege_list FROM role_list opt_granted_by opt_drop_behavior
7322 				{
7323 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7324 					n->is_grant = false;
7325 					n->admin_opt = true;
7326 					n->granted_roles = $5;
7327 					n->grantee_roles = $7;
7328 					n->behavior = $9;
7329 					$$ = (Node*)n;
7330 				}
7331 		;
7332 
7333 opt_grant_admin_option: WITH ADMIN OPTION				{ $$ = true; }
7334 			| /*EMPTY*/									{ $$ = false; }
7335 		;
7336 
7337 opt_granted_by: GRANTED BY RoleSpec						{ $$ = $3; }
7338 			| /*EMPTY*/									{ $$ = NULL; }
7339 		;
7340 
7341 /*****************************************************************************
7342  *
7343  * ALTER DEFAULT PRIVILEGES statement
7344  *
7345  *****************************************************************************/
7346 
7347 AlterDefaultPrivilegesStmt:
7348 			ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
7349 				{
7350 					AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
7351 					n->options = $4;
7352 					n->action = (GrantStmt *) $5;
7353 					$$ = (Node*)n;
7354 				}
7355 		;
7356 
7357 DefACLOptionList:
7358 			DefACLOptionList DefACLOption			{ $$ = lappend($1, $2); }
7359 			| /* EMPTY */							{ $$ = NIL; }
7360 		;
7361 
7362 DefACLOption:
7363 			IN_P SCHEMA name_list
7364 				{
7365 					$$ = makeDefElem("schemas", (Node *)$3, @1);
7366 				}
7367 			| FOR ROLE role_list
7368 				{
7369 					$$ = makeDefElem("roles", (Node *)$3, @1);
7370 				}
7371 			| FOR USER role_list
7372 				{
7373 					$$ = makeDefElem("roles", (Node *)$3, @1);
7374 				}
7375 		;
7376 
7377 /*
7378  * This should match GRANT/REVOKE, except that individual target objects
7379  * are not mentioned and we only allow a subset of object types.
7380  */
7381 DefACLAction:
7382 			GRANT privileges ON defacl_privilege_target TO grantee_list
7383 			opt_grant_grant_option
7384 				{
7385 					GrantStmt *n = makeNode(GrantStmt);
7386 					n->is_grant = true;
7387 					n->privileges = $2;
7388 					n->targtype = ACL_TARGET_DEFAULTS;
7389 					n->objtype = $4;
7390 					n->objects = NIL;
7391 					n->grantees = $6;
7392 					n->grant_option = $7;
7393 					$$ = (Node*)n;
7394 				}
7395 			| REVOKE privileges ON defacl_privilege_target
7396 			FROM grantee_list opt_drop_behavior
7397 				{
7398 					GrantStmt *n = makeNode(GrantStmt);
7399 					n->is_grant = false;
7400 					n->grant_option = false;
7401 					n->privileges = $2;
7402 					n->targtype = ACL_TARGET_DEFAULTS;
7403 					n->objtype = $4;
7404 					n->objects = NIL;
7405 					n->grantees = $6;
7406 					n->behavior = $7;
7407 					$$ = (Node *)n;
7408 				}
7409 			| REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
7410 			FROM grantee_list opt_drop_behavior
7411 				{
7412 					GrantStmt *n = makeNode(GrantStmt);
7413 					n->is_grant = false;
7414 					n->grant_option = true;
7415 					n->privileges = $5;
7416 					n->targtype = ACL_TARGET_DEFAULTS;
7417 					n->objtype = $7;
7418 					n->objects = NIL;
7419 					n->grantees = $9;
7420 					n->behavior = $10;
7421 					$$ = (Node *)n;
7422 				}
7423 		;
7424 
7425 defacl_privilege_target:
7426 			TABLES			{ $$ = OBJECT_TABLE; }
7427 			| FUNCTIONS		{ $$ = OBJECT_FUNCTION; }
7428 			| ROUTINES		{ $$ = OBJECT_FUNCTION; }
7429 			| SEQUENCES		{ $$ = OBJECT_SEQUENCE; }
7430 			| TYPES_P		{ $$ = OBJECT_TYPE; }
7431 			| SCHEMAS		{ $$ = OBJECT_SCHEMA; }
7432 		;
7433 
7434 
7435 /*****************************************************************************
7436  *
7437  *		QUERY: CREATE INDEX
7438  *
7439  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
7440  * willing to make TABLESPACE a fully reserved word.
7441  *****************************************************************************/
7442 
7443 IndexStmt:	CREATE opt_unique INDEX opt_concurrently opt_index_name
7444 			ON relation_expr access_method_clause '(' index_params ')'
7445 			opt_include opt_reloptions OptTableSpace where_clause
7446 				{
7447 					IndexStmt *n = makeNode(IndexStmt);
7448 					n->unique = $2;
7449 					n->concurrent = $4;
7450 					n->idxname = $5;
7451 					n->relation = $7;
7452 					n->relationId = InvalidOid;
7453 					n->accessMethod = $8;
7454 					n->indexParams = $10;
7455 					n->indexIncludingParams = $12;
7456 					n->options = $13;
7457 					n->tableSpace = $14;
7458 					n->whereClause = $15;
7459 					n->excludeOpNames = NIL;
7460 					n->idxcomment = NULL;
7461 					n->indexOid = InvalidOid;
7462 					n->oldNode = InvalidOid;
7463 					n->primary = false;
7464 					n->isconstraint = false;
7465 					n->deferrable = false;
7466 					n->initdeferred = false;
7467 					n->transformed = false;
7468 					n->if_not_exists = false;
7469 					$$ = (Node *)n;
7470 				}
7471 			| CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS index_name
7472 			ON relation_expr access_method_clause '(' index_params ')'
7473 			opt_include opt_reloptions OptTableSpace where_clause
7474 				{
7475 					IndexStmt *n = makeNode(IndexStmt);
7476 					n->unique = $2;
7477 					n->concurrent = $4;
7478 					n->idxname = $8;
7479 					n->relation = $10;
7480 					n->relationId = InvalidOid;
7481 					n->accessMethod = $11;
7482 					n->indexParams = $13;
7483 					n->indexIncludingParams = $15;
7484 					n->options = $16;
7485 					n->tableSpace = $17;
7486 					n->whereClause = $18;
7487 					n->excludeOpNames = NIL;
7488 					n->idxcomment = NULL;
7489 					n->indexOid = InvalidOid;
7490 					n->oldNode = InvalidOid;
7491 					n->primary = false;
7492 					n->isconstraint = false;
7493 					n->deferrable = false;
7494 					n->initdeferred = false;
7495 					n->transformed = false;
7496 					n->if_not_exists = true;
7497 					$$ = (Node *)n;
7498 				}
7499 		;
7500 
7501 opt_unique:
7502 			UNIQUE									{ $$ = true; }
7503 			| /*EMPTY*/								{ $$ = false; }
7504 		;
7505 
7506 opt_concurrently:
7507 			CONCURRENTLY							{ $$ = true; }
7508 			| /*EMPTY*/								{ $$ = false; }
7509 		;
7510 
7511 opt_index_name:
7512 			index_name								{ $$ = $1; }
7513 			| /*EMPTY*/								{ $$ = NULL; }
7514 		;
7515 
7516 access_method_clause:
7517 			USING access_method						{ $$ = $2; }
7518 			| /*EMPTY*/								{ $$ = DEFAULT_INDEX_TYPE; }
7519 		;
7520 
7521 index_params:	index_elem							{ $$ = list_make1($1); }
7522 			| index_params ',' index_elem			{ $$ = lappend($1, $3); }
7523 		;
7524 
7525 /*
7526  * Index attributes can be either simple column references, or arbitrary
7527  * expressions in parens.  For backwards-compatibility reasons, we allow
7528  * an expression that's just a function call to be written without parens.
7529  */
7530 index_elem:	ColId opt_collate opt_class opt_asc_desc opt_nulls_order
7531 				{
7532 					$$ = makeNode(IndexElem);
7533 					$$->name = $1;
7534 					$$->expr = NULL;
7535 					$$->indexcolname = NULL;
7536 					$$->collation = $2;
7537 					$$->opclass = $3;
7538 					$$->ordering = $4;
7539 					$$->nulls_ordering = $5;
7540 				}
7541 			| func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order
7542 				{
7543 					$$ = makeNode(IndexElem);
7544 					$$->name = NULL;
7545 					$$->expr = $1;
7546 					$$->indexcolname = NULL;
7547 					$$->collation = $2;
7548 					$$->opclass = $3;
7549 					$$->ordering = $4;
7550 					$$->nulls_ordering = $5;
7551 				}
7552 			| '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order
7553 				{
7554 					$$ = makeNode(IndexElem);
7555 					$$->name = NULL;
7556 					$$->expr = $2;
7557 					$$->indexcolname = NULL;
7558 					$$->collation = $4;
7559 					$$->opclass = $5;
7560 					$$->ordering = $6;
7561 					$$->nulls_ordering = $7;
7562 				}
7563 		;
7564 
7565 opt_include:		INCLUDE '(' index_including_params ')'			{ $$ = $3; }
7566 			 |		/* EMPTY */						{ $$ = NIL; }
7567 		;
7568 
7569 index_including_params:	index_elem						{ $$ = list_make1($1); }
7570 			| index_including_params ',' index_elem		{ $$ = lappend($1, $3); }
7571 		;
7572 
7573 opt_collate: COLLATE any_name						{ $$ = $2; }
7574 			| /*EMPTY*/								{ $$ = NIL; }
7575 		;
7576 
7577 opt_class:	any_name								{ $$ = $1; }
7578 			| /*EMPTY*/								{ $$ = NIL; }
7579 		;
7580 
7581 opt_asc_desc: ASC							{ $$ = SORTBY_ASC; }
7582 			| DESC							{ $$ = SORTBY_DESC; }
7583 			| /*EMPTY*/						{ $$ = SORTBY_DEFAULT; }
7584 		;
7585 
7586 opt_nulls_order: NULLS_LA FIRST_P			{ $$ = SORTBY_NULLS_FIRST; }
7587 			| NULLS_LA LAST_P				{ $$ = SORTBY_NULLS_LAST; }
7588 			| /*EMPTY*/						{ $$ = SORTBY_NULLS_DEFAULT; }
7589 		;
7590 
7591 
7592 /*****************************************************************************
7593  *
7594  *		QUERY:
7595  *				create [or replace] function <fname>
7596  *						[(<type-1> { , <type-n>})]
7597  *						returns <type-r>
7598  *						as <filename or code in language as appropriate>
7599  *						language <lang> [with parameters]
7600  *
7601  *****************************************************************************/
7602 
7603 CreateFunctionStmt:
7604 			CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7605 			RETURNS func_return createfunc_opt_list
7606 				{
7607 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7608 					n->is_procedure = false;
7609 					n->replace = $2;
7610 					n->funcname = $4;
7611 					n->parameters = $5;
7612 					n->returnType = $7;
7613 					n->options = $8;
7614 					$$ = (Node *)n;
7615 				}
7616 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7617 			  RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list
7618 				{
7619 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7620 					n->is_procedure = false;
7621 					n->replace = $2;
7622 					n->funcname = $4;
7623 					n->parameters = mergeTableFuncParameters($5, $9);
7624 					n->returnType = TableFuncTypeName($9);
7625 					n->returnType->location = @7;
7626 					n->options = $11;
7627 					$$ = (Node *)n;
7628 				}
7629 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7630 			  createfunc_opt_list
7631 				{
7632 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7633 					n->is_procedure = false;
7634 					n->replace = $2;
7635 					n->funcname = $4;
7636 					n->parameters = $5;
7637 					n->returnType = NULL;
7638 					n->options = $6;
7639 					$$ = (Node *)n;
7640 				}
7641 			| CREATE opt_or_replace PROCEDURE func_name func_args_with_defaults
7642 			  createfunc_opt_list
7643 				{
7644 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7645 					n->is_procedure = true;
7646 					n->replace = $2;
7647 					n->funcname = $4;
7648 					n->parameters = $5;
7649 					n->returnType = NULL;
7650 					n->options = $6;
7651 					$$ = (Node *)n;
7652 				}
7653 		;
7654 
7655 opt_or_replace:
7656 			OR REPLACE								{ $$ = true; }
7657 			| /*EMPTY*/								{ $$ = false; }
7658 		;
7659 
7660 func_args:	'(' func_args_list ')'					{ $$ = $2; }
7661 			| '(' ')'								{ $$ = NIL; }
7662 		;
7663 
7664 func_args_list:
7665 			func_arg								{ $$ = list_make1($1); }
7666 			| func_args_list ',' func_arg			{ $$ = lappend($1, $3); }
7667 		;
7668 
7669 function_with_argtypes_list:
7670 			function_with_argtypes					{ $$ = list_make1($1); }
7671 			| function_with_argtypes_list ',' function_with_argtypes
7672 													{ $$ = lappend($1, $3); }
7673 		;
7674 
7675 function_with_argtypes:
7676 			func_name func_args
7677 				{
7678 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7679 					n->objname = $1;
7680 					n->objargs = extractArgTypes($2);
7681 					$$ = n;
7682 				}
7683 			/*
7684 			 * Because of reduce/reduce conflicts, we can't use func_name
7685 			 * below, but we can write it out the long way, which actually
7686 			 * allows more cases.
7687 			 */
7688 			| type_func_name_keyword
7689 				{
7690 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7691 					n->objname = list_make1(makeString(pstrdup($1)));
7692 					n->args_unspecified = true;
7693 					$$ = n;
7694 				}
7695 			| ColId
7696 				{
7697 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7698 					n->objname = list_make1(makeString($1));
7699 					n->args_unspecified = true;
7700 					$$ = n;
7701 				}
7702 			| ColId indirection
7703 				{
7704 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7705 					n->objname = check_func_name(lcons(makeString($1), $2),
7706 												  yyscanner);
7707 					n->args_unspecified = true;
7708 					$$ = n;
7709 				}
7710 		;
7711 
7712 /*
7713  * func_args_with_defaults is separate because we only want to accept
7714  * defaults in CREATE FUNCTION, not in ALTER etc.
7715  */
7716 func_args_with_defaults:
7717 		'(' func_args_with_defaults_list ')'		{ $$ = $2; }
7718 		| '(' ')'									{ $$ = NIL; }
7719 		;
7720 
7721 func_args_with_defaults_list:
7722 		func_arg_with_default						{ $$ = list_make1($1); }
7723 		| func_args_with_defaults_list ',' func_arg_with_default
7724 													{ $$ = lappend($1, $3); }
7725 		;
7726 
7727 /*
7728  * The style with arg_class first is SQL99 standard, but Oracle puts
7729  * param_name first; accept both since it's likely people will try both
7730  * anyway.  Don't bother trying to save productions by letting arg_class
7731  * have an empty alternative ... you'll get shift/reduce conflicts.
7732  *
7733  * We can catch over-specified arguments here if we want to,
7734  * but for now better to silently swallow typmod, etc.
7735  * - thomas 2000-03-22
7736  */
7737 func_arg:
7738 			arg_class param_name func_type
7739 				{
7740 					FunctionParameter *n = makeNode(FunctionParameter);
7741 					n->name = $2;
7742 					n->argType = $3;
7743 					n->mode = $1;
7744 					n->defexpr = NULL;
7745 					$$ = n;
7746 				}
7747 			| param_name arg_class func_type
7748 				{
7749 					FunctionParameter *n = makeNode(FunctionParameter);
7750 					n->name = $1;
7751 					n->argType = $3;
7752 					n->mode = $2;
7753 					n->defexpr = NULL;
7754 					$$ = n;
7755 				}
7756 			| param_name func_type
7757 				{
7758 					FunctionParameter *n = makeNode(FunctionParameter);
7759 					n->name = $1;
7760 					n->argType = $2;
7761 					n->mode = FUNC_PARAM_IN;
7762 					n->defexpr = NULL;
7763 					$$ = n;
7764 				}
7765 			| arg_class func_type
7766 				{
7767 					FunctionParameter *n = makeNode(FunctionParameter);
7768 					n->name = NULL;
7769 					n->argType = $2;
7770 					n->mode = $1;
7771 					n->defexpr = NULL;
7772 					$$ = n;
7773 				}
7774 			| func_type
7775 				{
7776 					FunctionParameter *n = makeNode(FunctionParameter);
7777 					n->name = NULL;
7778 					n->argType = $1;
7779 					n->mode = FUNC_PARAM_IN;
7780 					n->defexpr = NULL;
7781 					$$ = n;
7782 				}
7783 		;
7784 
7785 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
7786 arg_class:	IN_P								{ $$ = FUNC_PARAM_IN; }
7787 			| OUT_P								{ $$ = FUNC_PARAM_OUT; }
7788 			| INOUT								{ $$ = FUNC_PARAM_INOUT; }
7789 			| IN_P OUT_P						{ $$ = FUNC_PARAM_INOUT; }
7790 			| VARIADIC							{ $$ = FUNC_PARAM_VARIADIC; }
7791 		;
7792 
7793 /*
7794  * Ideally param_name should be ColId, but that causes too many conflicts.
7795  */
7796 param_name:	type_function_name
7797 		;
7798 
7799 func_return:
7800 			func_type
7801 				{
7802 					/* We can catch over-specified results here if we want to,
7803 					 * but for now better to silently swallow typmod, etc.
7804 					 * - thomas 2000-03-22
7805 					 */
7806 					$$ = $1;
7807 				}
7808 		;
7809 
7810 /*
7811  * We would like to make the %TYPE productions here be ColId attrs etc,
7812  * but that causes reduce/reduce conflicts.  type_function_name
7813  * is next best choice.
7814  */
7815 func_type:	Typename								{ $$ = $1; }
7816 			| type_function_name attrs '%' TYPE_P
7817 				{
7818 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7819 					$$->pct_type = true;
7820 					$$->location = @1;
7821 				}
7822 			| SETOF type_function_name attrs '%' TYPE_P
7823 				{
7824 					$$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
7825 					$$->pct_type = true;
7826 					$$->setof = true;
7827 					$$->location = @2;
7828 				}
7829 		;
7830 
7831 func_arg_with_default:
7832 		func_arg
7833 				{
7834 					$$ = $1;
7835 				}
7836 		| func_arg DEFAULT a_expr
7837 				{
7838 					$$ = $1;
7839 					$$->defexpr = $3;
7840 				}
7841 		| func_arg '=' a_expr
7842 				{
7843 					$$ = $1;
7844 					$$->defexpr = $3;
7845 				}
7846 		;
7847 
7848 /* Aggregate args can be most things that function args can be */
7849 aggr_arg:	func_arg
7850 				{
7851 					if (!($1->mode == FUNC_PARAM_IN ||
7852 						  $1->mode == FUNC_PARAM_VARIADIC))
7853 						ereport(ERROR,
7854 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7855 								 errmsg("aggregates cannot have output arguments"),
7856 								 parser_errposition(@1)));
7857 					$$ = $1;
7858 				}
7859 		;
7860 
7861 /*
7862  * The SQL standard offers no guidance on how to declare aggregate argument
7863  * lists, since it doesn't have CREATE AGGREGATE etc.  We accept these cases:
7864  *
7865  * (*)									- normal agg with no args
7866  * (aggr_arg,...)						- normal agg with args
7867  * (ORDER BY aggr_arg,...)				- ordered-set agg with no direct args
7868  * (aggr_arg,... ORDER BY aggr_arg,...)	- ordered-set agg with direct args
7869  *
7870  * The zero-argument case is spelled with '*' for consistency with COUNT(*).
7871  *
7872  * An additional restriction is that if the direct-args list ends in a
7873  * VARIADIC item, the ordered-args list must contain exactly one item that
7874  * is also VARIADIC with the same type.  This allows us to collapse the two
7875  * VARIADIC items into one, which is necessary to represent the aggregate in
7876  * pg_proc.  We check this at the grammar stage so that we can return a list
7877  * in which the second VARIADIC item is already discarded, avoiding extra work
7878  * in cases such as DROP AGGREGATE.
7879  *
7880  * The return value of this production is a two-element list, in which the
7881  * first item is a sublist of FunctionParameter nodes (with any duplicate
7882  * VARIADIC item already dropped, as per above) and the second is an integer
7883  * Value node, containing -1 if there was no ORDER BY and otherwise the number
7884  * of argument declarations before the ORDER BY.  (If this number is equal
7885  * to the first sublist's length, then we dropped a duplicate VARIADIC item.)
7886  * This representation is passed as-is to CREATE AGGREGATE; for operations
7887  * on existing aggregates, we can just apply extractArgTypes to the first
7888  * sublist.
7889  */
7890 aggr_args:	'(' '*' ')'
7891 				{
7892 					$$ = list_make2(NIL, makeInteger(-1));
7893 				}
7894 			| '(' aggr_args_list ')'
7895 				{
7896 					$$ = list_make2($2, makeInteger(-1));
7897 				}
7898 			| '(' ORDER BY aggr_args_list ')'
7899 				{
7900 					$$ = list_make2($4, makeInteger(0));
7901 				}
7902 			| '(' aggr_args_list ORDER BY aggr_args_list ')'
7903 				{
7904 					/* this is the only case requiring consistency checking */
7905 					$$ = makeOrderedSetArgs($2, $5, yyscanner);
7906 				}
7907 		;
7908 
7909 aggr_args_list:
7910 			aggr_arg								{ $$ = list_make1($1); }
7911 			| aggr_args_list ',' aggr_arg			{ $$ = lappend($1, $3); }
7912 		;
7913 
7914 aggregate_with_argtypes:
7915 			func_name aggr_args
7916 				{
7917 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7918 					n->objname = $1;
7919 					n->objargs = extractAggrArgTypes($2);
7920 					$$ = n;
7921 				}
7922 		;
7923 
7924 aggregate_with_argtypes_list:
7925 			aggregate_with_argtypes					{ $$ = list_make1($1); }
7926 			| aggregate_with_argtypes_list ',' aggregate_with_argtypes
7927 													{ $$ = lappend($1, $3); }
7928 		;
7929 
7930 createfunc_opt_list:
7931 			/* Must be at least one to prevent conflict */
7932 			createfunc_opt_item						{ $$ = list_make1($1); }
7933 			| createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
7934 	;
7935 
7936 /*
7937  * Options common to both CREATE FUNCTION and ALTER FUNCTION
7938  */
7939 common_func_opt_item:
7940 			CALLED ON NULL_P INPUT_P
7941 				{
7942 					$$ = makeDefElem("strict", (Node *)makeInteger(false), @1);
7943 				}
7944 			| RETURNS NULL_P ON NULL_P INPUT_P
7945 				{
7946 					$$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7947 				}
7948 			| STRICT_P
7949 				{
7950 					$$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7951 				}
7952 			| IMMUTABLE
7953 				{
7954 					$$ = makeDefElem("volatility", (Node *)makeString("immutable"), @1);
7955 				}
7956 			| STABLE
7957 				{
7958 					$$ = makeDefElem("volatility", (Node *)makeString("stable"), @1);
7959 				}
7960 			| VOLATILE
7961 				{
7962 					$$ = makeDefElem("volatility", (Node *)makeString("volatile"), @1);
7963 				}
7964 			| EXTERNAL SECURITY DEFINER
7965 				{
7966 					$$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7967 				}
7968 			| EXTERNAL SECURITY INVOKER
7969 				{
7970 					$$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7971 				}
7972 			| SECURITY DEFINER
7973 				{
7974 					$$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7975 				}
7976 			| SECURITY INVOKER
7977 				{
7978 					$$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7979 				}
7980 			| LEAKPROOF
7981 				{
7982 					$$ = makeDefElem("leakproof", (Node *)makeInteger(true), @1);
7983 				}
7984 			| NOT LEAKPROOF
7985 				{
7986 					$$ = makeDefElem("leakproof", (Node *)makeInteger(false), @1);
7987 				}
7988 			| COST NumericOnly
7989 				{
7990 					$$ = makeDefElem("cost", (Node *)$2, @1);
7991 				}
7992 			| ROWS NumericOnly
7993 				{
7994 					$$ = makeDefElem("rows", (Node *)$2, @1);
7995 				}
7996 			| FunctionSetResetClause
7997 				{
7998 					/* we abuse the normal content of a DefElem here */
7999 					$$ = makeDefElem("set", (Node *)$1, @1);
8000 				}
8001 			| PARALLEL ColId
8002 				{
8003 					$$ = makeDefElem("parallel", (Node *)makeString($2), @1);
8004 				}
8005 		;
8006 
8007 createfunc_opt_item:
8008 			AS func_as
8009 				{
8010 					$$ = makeDefElem("as", (Node *)$2, @1);
8011 				}
8012 			| LANGUAGE NonReservedWord_or_Sconst
8013 				{
8014 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
8015 				}
8016 			| TRANSFORM transform_type_list
8017 				{
8018 					$$ = makeDefElem("transform", (Node *)$2, @1);
8019 				}
8020 			| WINDOW
8021 				{
8022 					$$ = makeDefElem("window", (Node *)makeInteger(true), @1);
8023 				}
8024 			| common_func_opt_item
8025 				{
8026 					$$ = $1;
8027 				}
8028 		;
8029 
8030 func_as:	Sconst						{ $$ = list_make1(makeString($1)); }
8031 			| Sconst ',' Sconst
8032 				{
8033 					$$ = list_make2(makeString($1), makeString($3));
8034 				}
8035 		;
8036 
8037 transform_type_list:
8038 			FOR TYPE_P Typename { $$ = list_make1($3); }
8039 			| transform_type_list ',' FOR TYPE_P Typename { $$ = lappend($1, $5); }
8040 		;
8041 
8042 opt_definition:
8043 			WITH definition							{ $$ = $2; }
8044 			| /*EMPTY*/								{ $$ = NIL; }
8045 		;
8046 
8047 table_func_column:	param_name func_type
8048 				{
8049 					FunctionParameter *n = makeNode(FunctionParameter);
8050 					n->name = $1;
8051 					n->argType = $2;
8052 					n->mode = FUNC_PARAM_TABLE;
8053 					n->defexpr = NULL;
8054 					$$ = n;
8055 				}
8056 		;
8057 
8058 table_func_column_list:
8059 			table_func_column
8060 				{
8061 					$$ = list_make1($1);
8062 				}
8063 			| table_func_column_list ',' table_func_column
8064 				{
8065 					$$ = lappend($1, $3);
8066 				}
8067 		;
8068 
8069 /*****************************************************************************
8070  * ALTER FUNCTION / ALTER PROCEDURE / ALTER ROUTINE
8071  *
8072  * RENAME and OWNER subcommands are already provided by the generic
8073  * ALTER infrastructure, here we just specify alterations that can
8074  * only be applied to functions.
8075  *
8076  *****************************************************************************/
8077 AlterFunctionStmt:
8078 			ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
8079 				{
8080 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8081 					n->objtype = OBJECT_FUNCTION;
8082 					n->func = $3;
8083 					n->actions = $4;
8084 					$$ = (Node *) n;
8085 				}
8086 			| ALTER PROCEDURE function_with_argtypes alterfunc_opt_list opt_restrict
8087 				{
8088 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8089 					n->objtype = OBJECT_PROCEDURE;
8090 					n->func = $3;
8091 					n->actions = $4;
8092 					$$ = (Node *) n;
8093 				}
8094 			| ALTER ROUTINE function_with_argtypes alterfunc_opt_list opt_restrict
8095 				{
8096 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8097 					n->objtype = OBJECT_ROUTINE;
8098 					n->func = $3;
8099 					n->actions = $4;
8100 					$$ = (Node *) n;
8101 				}
8102 		;
8103 
8104 alterfunc_opt_list:
8105 			/* At least one option must be specified */
8106 			common_func_opt_item					{ $$ = list_make1($1); }
8107 			| alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
8108 		;
8109 
8110 /* Ignored, merely for SQL compliance */
8111 opt_restrict:
8112 			RESTRICT
8113 			| /* EMPTY */
8114 		;
8115 
8116 
8117 /*****************************************************************************
8118  *
8119  *		QUERY:
8120  *
8121  *		DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8122  *		DROP PROCEDURE procname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8123  *		DROP ROUTINE routname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8124  *		DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
8125  *		DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
8126  *
8127  *****************************************************************************/
8128 
8129 RemoveFuncStmt:
8130 			DROP FUNCTION function_with_argtypes_list opt_drop_behavior
8131 				{
8132 					DropStmt *n = makeNode(DropStmt);
8133 					n->removeType = OBJECT_FUNCTION;
8134 					n->objects = $3;
8135 					n->behavior = $4;
8136 					n->missing_ok = false;
8137 					n->concurrent = false;
8138 					$$ = (Node *)n;
8139 				}
8140 			| DROP FUNCTION IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8141 				{
8142 					DropStmt *n = makeNode(DropStmt);
8143 					n->removeType = OBJECT_FUNCTION;
8144 					n->objects = $5;
8145 					n->behavior = $6;
8146 					n->missing_ok = true;
8147 					n->concurrent = false;
8148 					$$ = (Node *)n;
8149 				}
8150 			| DROP PROCEDURE function_with_argtypes_list opt_drop_behavior
8151 				{
8152 					DropStmt *n = makeNode(DropStmt);
8153 					n->removeType = OBJECT_PROCEDURE;
8154 					n->objects = $3;
8155 					n->behavior = $4;
8156 					n->missing_ok = false;
8157 					n->concurrent = false;
8158 					$$ = (Node *)n;
8159 				}
8160 			| DROP PROCEDURE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8161 				{
8162 					DropStmt *n = makeNode(DropStmt);
8163 					n->removeType = OBJECT_PROCEDURE;
8164 					n->objects = $5;
8165 					n->behavior = $6;
8166 					n->missing_ok = true;
8167 					n->concurrent = false;
8168 					$$ = (Node *)n;
8169 				}
8170 			| DROP ROUTINE function_with_argtypes_list opt_drop_behavior
8171 				{
8172 					DropStmt *n = makeNode(DropStmt);
8173 					n->removeType = OBJECT_ROUTINE;
8174 					n->objects = $3;
8175 					n->behavior = $4;
8176 					n->missing_ok = false;
8177 					n->concurrent = false;
8178 					$$ = (Node *)n;
8179 				}
8180 			| DROP ROUTINE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8181 				{
8182 					DropStmt *n = makeNode(DropStmt);
8183 					n->removeType = OBJECT_ROUTINE;
8184 					n->objects = $5;
8185 					n->behavior = $6;
8186 					n->missing_ok = true;
8187 					n->concurrent = false;
8188 					$$ = (Node *)n;
8189 				}
8190 		;
8191 
8192 RemoveAggrStmt:
8193 			DROP AGGREGATE aggregate_with_argtypes_list opt_drop_behavior
8194 				{
8195 					DropStmt *n = makeNode(DropStmt);
8196 					n->removeType = OBJECT_AGGREGATE;
8197 					n->objects = $3;
8198 					n->behavior = $4;
8199 					n->missing_ok = false;
8200 					n->concurrent = false;
8201 					$$ = (Node *)n;
8202 				}
8203 			| DROP AGGREGATE IF_P EXISTS aggregate_with_argtypes_list opt_drop_behavior
8204 				{
8205 					DropStmt *n = makeNode(DropStmt);
8206 					n->removeType = OBJECT_AGGREGATE;
8207 					n->objects = $5;
8208 					n->behavior = $6;
8209 					n->missing_ok = true;
8210 					n->concurrent = false;
8211 					$$ = (Node *)n;
8212 				}
8213 		;
8214 
8215 RemoveOperStmt:
8216 			DROP OPERATOR operator_with_argtypes_list opt_drop_behavior
8217 				{
8218 					DropStmt *n = makeNode(DropStmt);
8219 					n->removeType = OBJECT_OPERATOR;
8220 					n->objects = $3;
8221 					n->behavior = $4;
8222 					n->missing_ok = false;
8223 					n->concurrent = false;
8224 					$$ = (Node *)n;
8225 				}
8226 			| DROP OPERATOR IF_P EXISTS operator_with_argtypes_list opt_drop_behavior
8227 				{
8228 					DropStmt *n = makeNode(DropStmt);
8229 					n->removeType = OBJECT_OPERATOR;
8230 					n->objects = $5;
8231 					n->behavior = $6;
8232 					n->missing_ok = true;
8233 					n->concurrent = false;
8234 					$$ = (Node *)n;
8235 				}
8236 		;
8237 
8238 oper_argtypes:
8239 			'(' Typename ')'
8240 				{
8241 				   ereport(ERROR,
8242 						   (errcode(ERRCODE_SYNTAX_ERROR),
8243 							errmsg("missing argument"),
8244 							errhint("Use NONE to denote the missing argument of a unary operator."),
8245 							parser_errposition(@3)));
8246 				}
8247 			| '(' Typename ',' Typename ')'
8248 					{ $$ = list_make2($2, $4); }
8249 			| '(' NONE ',' Typename ')'					/* left unary */
8250 					{ $$ = list_make2(NULL, $4); }
8251 			| '(' Typename ',' NONE ')'					/* right unary */
8252 					{ $$ = list_make2($2, NULL); }
8253 		;
8254 
8255 any_operator:
8256 			all_Op
8257 					{ $$ = list_make1(makeString($1)); }
8258 			| ColId '.' any_operator
8259 					{ $$ = lcons(makeString($1), $3); }
8260 		;
8261 
8262 operator_with_argtypes_list:
8263 			operator_with_argtypes					{ $$ = list_make1($1); }
8264 			| operator_with_argtypes_list ',' operator_with_argtypes
8265 													{ $$ = lappend($1, $3); }
8266 		;
8267 
8268 operator_with_argtypes:
8269 			any_operator oper_argtypes
8270 				{
8271 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
8272 					n->objname = $1;
8273 					n->objargs = $2;
8274 					$$ = n;
8275 				}
8276 		;
8277 
8278 /*****************************************************************************
8279  *
8280  *		DO <anonymous code block> [ LANGUAGE language ]
8281  *
8282  * We use a DefElem list for future extensibility, and to allow flexibility
8283  * in the clause order.
8284  *
8285  *****************************************************************************/
8286 
8287 DoStmt: DO dostmt_opt_list
8288 				{
8289 					DoStmt *n = makeNode(DoStmt);
8290 					n->args = $2;
8291 					$$ = (Node *)n;
8292 				}
8293 		;
8294 
8295 dostmt_opt_list:
8296 			dostmt_opt_item						{ $$ = list_make1($1); }
8297 			| dostmt_opt_list dostmt_opt_item	{ $$ = lappend($1, $2); }
8298 		;
8299 
8300 dostmt_opt_item:
8301 			Sconst
8302 				{
8303 					$$ = makeDefElem("as", (Node *)makeString($1), @1);
8304 				}
8305 			| LANGUAGE NonReservedWord_or_Sconst
8306 				{
8307 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
8308 				}
8309 		;
8310 
8311 /*****************************************************************************
8312  *
8313  *		CREATE CAST / DROP CAST
8314  *
8315  *****************************************************************************/
8316 
8317 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
8318 					WITH FUNCTION function_with_argtypes cast_context
8319 				{
8320 					CreateCastStmt *n = makeNode(CreateCastStmt);
8321 					n->sourcetype = $4;
8322 					n->targettype = $6;
8323 					n->func = $10;
8324 					n->context = (CoercionContext) $11;
8325 					n->inout = false;
8326 					$$ = (Node *)n;
8327 				}
8328 			| CREATE CAST '(' Typename AS Typename ')'
8329 					WITHOUT FUNCTION cast_context
8330 				{
8331 					CreateCastStmt *n = makeNode(CreateCastStmt);
8332 					n->sourcetype = $4;
8333 					n->targettype = $6;
8334 					n->func = NULL;
8335 					n->context = (CoercionContext) $10;
8336 					n->inout = false;
8337 					$$ = (Node *)n;
8338 				}
8339 			| CREATE CAST '(' Typename AS Typename ')'
8340 					WITH INOUT cast_context
8341 				{
8342 					CreateCastStmt *n = makeNode(CreateCastStmt);
8343 					n->sourcetype = $4;
8344 					n->targettype = $6;
8345 					n->func = NULL;
8346 					n->context = (CoercionContext) $10;
8347 					n->inout = true;
8348 					$$ = (Node *)n;
8349 				}
8350 		;
8351 
8352 cast_context:  AS IMPLICIT_P					{ $$ = COERCION_IMPLICIT; }
8353 		| AS ASSIGNMENT							{ $$ = COERCION_ASSIGNMENT; }
8354 		| /*EMPTY*/								{ $$ = COERCION_EXPLICIT; }
8355 		;
8356 
8357 
8358 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
8359 				{
8360 					DropStmt *n = makeNode(DropStmt);
8361 					n->removeType = OBJECT_CAST;
8362 					n->objects = list_make1(list_make2($5, $7));
8363 					n->behavior = $9;
8364 					n->missing_ok = $3;
8365 					n->concurrent = false;
8366 					$$ = (Node *)n;
8367 				}
8368 		;
8369 
8370 opt_if_exists: IF_P EXISTS						{ $$ = true; }
8371 		| /*EMPTY*/								{ $$ = false; }
8372 		;
8373 
8374 
8375 /*****************************************************************************
8376  *
8377  *		CREATE TRANSFORM / DROP TRANSFORM
8378  *
8379  *****************************************************************************/
8380 
8381 CreateTransformStmt: CREATE opt_or_replace TRANSFORM FOR Typename LANGUAGE name '(' transform_element_list ')'
8382 				{
8383 					CreateTransformStmt *n = makeNode(CreateTransformStmt);
8384 					n->replace = $2;
8385 					n->type_name = $5;
8386 					n->lang = $7;
8387 					n->fromsql = linitial($9);
8388 					n->tosql = lsecond($9);
8389 					$$ = (Node *)n;
8390 				}
8391 		;
8392 
8393 transform_element_list: FROM SQL_P WITH FUNCTION function_with_argtypes ',' TO SQL_P WITH FUNCTION function_with_argtypes
8394 				{
8395 					$$ = list_make2($5, $11);
8396 				}
8397 				| TO SQL_P WITH FUNCTION function_with_argtypes ',' FROM SQL_P WITH FUNCTION function_with_argtypes
8398 				{
8399 					$$ = list_make2($11, $5);
8400 				}
8401 				| FROM SQL_P WITH FUNCTION function_with_argtypes
8402 				{
8403 					$$ = list_make2($5, NULL);
8404 				}
8405 				| TO SQL_P WITH FUNCTION function_with_argtypes
8406 				{
8407 					$$ = list_make2(NULL, $5);
8408 				}
8409 		;
8410 
8411 
8412 DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_drop_behavior
8413 				{
8414 					DropStmt *n = makeNode(DropStmt);
8415 					n->removeType = OBJECT_TRANSFORM;
8416 					n->objects = list_make1(list_make2($5, makeString($7)));
8417 					n->behavior = $8;
8418 					n->missing_ok = $3;
8419 					$$ = (Node *)n;
8420 				}
8421 		;
8422 
8423 
8424 /*****************************************************************************
8425  *
8426  *		QUERY:
8427  *
8428  *		REINDEX [ (options) ] type <name>
8429  *****************************************************************************/
8430 
8431 ReindexStmt:
8432 			REINDEX reindex_target_type qualified_name
8433 				{
8434 					ReindexStmt *n = makeNode(ReindexStmt);
8435 					n->kind = $2;
8436 					n->relation = $3;
8437 					n->name = NULL;
8438 					n->options = 0;
8439 					$$ = (Node *)n;
8440 				}
8441 			| REINDEX reindex_target_multitable name
8442 				{
8443 					ReindexStmt *n = makeNode(ReindexStmt);
8444 					n->kind = $2;
8445 					n->name = $3;
8446 					n->relation = NULL;
8447 					n->options = 0;
8448 					$$ = (Node *)n;
8449 				}
8450 			| REINDEX '(' reindex_option_list ')' reindex_target_type qualified_name
8451 				{
8452 					ReindexStmt *n = makeNode(ReindexStmt);
8453 					n->kind = $5;
8454 					n->relation = $6;
8455 					n->name = NULL;
8456 					n->options = $3;
8457 					$$ = (Node *)n;
8458 				}
8459 			| REINDEX '(' reindex_option_list ')' reindex_target_multitable name
8460 				{
8461 					ReindexStmt *n = makeNode(ReindexStmt);
8462 					n->kind = $5;
8463 					n->name = $6;
8464 					n->relation = NULL;
8465 					n->options = $3;
8466 					$$ = (Node *)n;
8467 				}
8468 		;
8469 reindex_target_type:
8470 			INDEX					{ $$ = REINDEX_OBJECT_INDEX; }
8471 			| TABLE					{ $$ = REINDEX_OBJECT_TABLE; }
8472 		;
8473 reindex_target_multitable:
8474 			SCHEMA					{ $$ = REINDEX_OBJECT_SCHEMA; }
8475 			| SYSTEM_P				{ $$ = REINDEX_OBJECT_SYSTEM; }
8476 			| DATABASE				{ $$ = REINDEX_OBJECT_DATABASE; }
8477 		;
8478 reindex_option_list:
8479 			reindex_option_elem								{ $$ = $1; }
8480 			| reindex_option_list ',' reindex_option_elem	{ $$ = $1 | $3; }
8481 		;
8482 reindex_option_elem:
8483 			VERBOSE	{ $$ = REINDEXOPT_VERBOSE; }
8484 		;
8485 
8486 /*****************************************************************************
8487  *
8488  * ALTER TABLESPACE
8489  *
8490  *****************************************************************************/
8491 
8492 AlterTblSpcStmt:
8493 			ALTER TABLESPACE name SET reloptions
8494 				{
8495 					AlterTableSpaceOptionsStmt *n =
8496 						makeNode(AlterTableSpaceOptionsStmt);
8497 					n->tablespacename = $3;
8498 					n->options = $5;
8499 					n->isReset = false;
8500 					$$ = (Node *)n;
8501 				}
8502 			| ALTER TABLESPACE name RESET reloptions
8503 				{
8504 					AlterTableSpaceOptionsStmt *n =
8505 						makeNode(AlterTableSpaceOptionsStmt);
8506 					n->tablespacename = $3;
8507 					n->options = $5;
8508 					n->isReset = true;
8509 					$$ = (Node *)n;
8510 				}
8511 		;
8512 
8513 /*****************************************************************************
8514  *
8515  * ALTER THING name RENAME TO newname
8516  *
8517  *****************************************************************************/
8518 
8519 RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
8520 				{
8521 					RenameStmt *n = makeNode(RenameStmt);
8522 					n->renameType = OBJECT_AGGREGATE;
8523 					n->object = (Node *) $3;
8524 					n->newname = $6;
8525 					n->missing_ok = false;
8526 					$$ = (Node *)n;
8527 				}
8528 			| ALTER COLLATION any_name RENAME TO name
8529 				{
8530 					RenameStmt *n = makeNode(RenameStmt);
8531 					n->renameType = OBJECT_COLLATION;
8532 					n->object = (Node *) $3;
8533 					n->newname = $6;
8534 					n->missing_ok = false;
8535 					$$ = (Node *)n;
8536 				}
8537 			| ALTER CONVERSION_P any_name RENAME TO name
8538 				{
8539 					RenameStmt *n = makeNode(RenameStmt);
8540 					n->renameType = OBJECT_CONVERSION;
8541 					n->object = (Node *) $3;
8542 					n->newname = $6;
8543 					n->missing_ok = false;
8544 					$$ = (Node *)n;
8545 				}
8546 			| ALTER DATABASE database_name RENAME TO database_name
8547 				{
8548 					RenameStmt *n = makeNode(RenameStmt);
8549 					n->renameType = OBJECT_DATABASE;
8550 					n->subname = $3;
8551 					n->newname = $6;
8552 					n->missing_ok = false;
8553 					$$ = (Node *)n;
8554 				}
8555 			| ALTER DOMAIN_P any_name RENAME TO name
8556 				{
8557 					RenameStmt *n = makeNode(RenameStmt);
8558 					n->renameType = OBJECT_DOMAIN;
8559 					n->object = (Node *) $3;
8560 					n->newname = $6;
8561 					n->missing_ok = false;
8562 					$$ = (Node *)n;
8563 				}
8564 			| ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
8565 				{
8566 					RenameStmt *n = makeNode(RenameStmt);
8567 					n->renameType = OBJECT_DOMCONSTRAINT;
8568 					n->object = (Node *) $3;
8569 					n->subname = $6;
8570 					n->newname = $8;
8571 					$$ = (Node *)n;
8572 				}
8573 			| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
8574 				{
8575 					RenameStmt *n = makeNode(RenameStmt);
8576 					n->renameType = OBJECT_FDW;
8577 					n->object = (Node *) makeString($5);
8578 					n->newname = $8;
8579 					n->missing_ok = false;
8580 					$$ = (Node *)n;
8581 				}
8582 			| ALTER FUNCTION function_with_argtypes RENAME TO name
8583 				{
8584 					RenameStmt *n = makeNode(RenameStmt);
8585 					n->renameType = OBJECT_FUNCTION;
8586 					n->object = (Node *) $3;
8587 					n->newname = $6;
8588 					n->missing_ok = false;
8589 					$$ = (Node *)n;
8590 				}
8591 			| ALTER GROUP_P RoleId RENAME TO RoleId
8592 				{
8593 					RenameStmt *n = makeNode(RenameStmt);
8594 					n->renameType = OBJECT_ROLE;
8595 					n->subname = $3;
8596 					n->newname = $6;
8597 					n->missing_ok = false;
8598 					$$ = (Node *)n;
8599 				}
8600 			| ALTER opt_procedural LANGUAGE name RENAME TO name
8601 				{
8602 					RenameStmt *n = makeNode(RenameStmt);
8603 					n->renameType = OBJECT_LANGUAGE;
8604 					n->object = (Node *) makeString($4);
8605 					n->newname = $7;
8606 					n->missing_ok = false;
8607 					$$ = (Node *)n;
8608 				}
8609 			| ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
8610 				{
8611 					RenameStmt *n = makeNode(RenameStmt);
8612 					n->renameType = OBJECT_OPCLASS;
8613 					n->object = (Node *) lcons(makeString($6), $4);
8614 					n->newname = $9;
8615 					n->missing_ok = false;
8616 					$$ = (Node *)n;
8617 				}
8618 			| ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
8619 				{
8620 					RenameStmt *n = makeNode(RenameStmt);
8621 					n->renameType = OBJECT_OPFAMILY;
8622 					n->object = (Node *) lcons(makeString($6), $4);
8623 					n->newname = $9;
8624 					n->missing_ok = false;
8625 					$$ = (Node *)n;
8626 				}
8627 			| ALTER POLICY name ON qualified_name RENAME TO name
8628 				{
8629 					RenameStmt *n = makeNode(RenameStmt);
8630 					n->renameType = OBJECT_POLICY;
8631 					n->relation = $5;
8632 					n->subname = $3;
8633 					n->newname = $8;
8634 					n->missing_ok = false;
8635 					$$ = (Node *)n;
8636 				}
8637 			| ALTER POLICY IF_P EXISTS name ON qualified_name RENAME TO name
8638 				{
8639 					RenameStmt *n = makeNode(RenameStmt);
8640 					n->renameType = OBJECT_POLICY;
8641 					n->relation = $7;
8642 					n->subname = $5;
8643 					n->newname = $10;
8644 					n->missing_ok = true;
8645 					$$ = (Node *)n;
8646 				}
8647 			| ALTER PROCEDURE function_with_argtypes RENAME TO name
8648 				{
8649 					RenameStmt *n = makeNode(RenameStmt);
8650 					n->renameType = OBJECT_PROCEDURE;
8651 					n->object = (Node *) $3;
8652 					n->newname = $6;
8653 					n->missing_ok = false;
8654 					$$ = (Node *)n;
8655 				}
8656 			| ALTER PUBLICATION name RENAME TO name
8657 				{
8658 					RenameStmt *n = makeNode(RenameStmt);
8659 					n->renameType = OBJECT_PUBLICATION;
8660 					n->object = (Node *) makeString($3);
8661 					n->newname = $6;
8662 					n->missing_ok = false;
8663 					$$ = (Node *)n;
8664 				}
8665 			| ALTER ROUTINE function_with_argtypes RENAME TO name
8666 				{
8667 					RenameStmt *n = makeNode(RenameStmt);
8668 					n->renameType = OBJECT_ROUTINE;
8669 					n->object = (Node *) $3;
8670 					n->newname = $6;
8671 					n->missing_ok = false;
8672 					$$ = (Node *)n;
8673 				}
8674 			| ALTER SCHEMA name RENAME TO name
8675 				{
8676 					RenameStmt *n = makeNode(RenameStmt);
8677 					n->renameType = OBJECT_SCHEMA;
8678 					n->subname = $3;
8679 					n->newname = $6;
8680 					n->missing_ok = false;
8681 					$$ = (Node *)n;
8682 				}
8683 			| ALTER SERVER name RENAME TO name
8684 				{
8685 					RenameStmt *n = makeNode(RenameStmt);
8686 					n->renameType = OBJECT_FOREIGN_SERVER;
8687 					n->object = (Node *) makeString($3);
8688 					n->newname = $6;
8689 					n->missing_ok = false;
8690 					$$ = (Node *)n;
8691 				}
8692 			| ALTER SUBSCRIPTION name RENAME TO name
8693 				{
8694 					RenameStmt *n = makeNode(RenameStmt);
8695 					n->renameType = OBJECT_SUBSCRIPTION;
8696 					n->object = (Node *) makeString($3);
8697 					n->newname = $6;
8698 					n->missing_ok = false;
8699 					$$ = (Node *)n;
8700 				}
8701 			| ALTER TABLE relation_expr RENAME TO name
8702 				{
8703 					RenameStmt *n = makeNode(RenameStmt);
8704 					n->renameType = OBJECT_TABLE;
8705 					n->relation = $3;
8706 					n->subname = NULL;
8707 					n->newname = $6;
8708 					n->missing_ok = false;
8709 					$$ = (Node *)n;
8710 				}
8711 			| ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
8712 				{
8713 					RenameStmt *n = makeNode(RenameStmt);
8714 					n->renameType = OBJECT_TABLE;
8715 					n->relation = $5;
8716 					n->subname = NULL;
8717 					n->newname = $8;
8718 					n->missing_ok = true;
8719 					$$ = (Node *)n;
8720 				}
8721 			| ALTER SEQUENCE qualified_name RENAME TO name
8722 				{
8723 					RenameStmt *n = makeNode(RenameStmt);
8724 					n->renameType = OBJECT_SEQUENCE;
8725 					n->relation = $3;
8726 					n->subname = NULL;
8727 					n->newname = $6;
8728 					n->missing_ok = false;
8729 					$$ = (Node *)n;
8730 				}
8731 			| ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
8732 				{
8733 					RenameStmt *n = makeNode(RenameStmt);
8734 					n->renameType = OBJECT_SEQUENCE;
8735 					n->relation = $5;
8736 					n->subname = NULL;
8737 					n->newname = $8;
8738 					n->missing_ok = true;
8739 					$$ = (Node *)n;
8740 				}
8741 			| ALTER VIEW qualified_name RENAME TO name
8742 				{
8743 					RenameStmt *n = makeNode(RenameStmt);
8744 					n->renameType = OBJECT_VIEW;
8745 					n->relation = $3;
8746 					n->subname = NULL;
8747 					n->newname = $6;
8748 					n->missing_ok = false;
8749 					$$ = (Node *)n;
8750 				}
8751 			| ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
8752 				{
8753 					RenameStmt *n = makeNode(RenameStmt);
8754 					n->renameType = OBJECT_VIEW;
8755 					n->relation = $5;
8756 					n->subname = NULL;
8757 					n->newname = $8;
8758 					n->missing_ok = true;
8759 					$$ = (Node *)n;
8760 				}
8761 			| ALTER MATERIALIZED VIEW qualified_name RENAME TO name
8762 				{
8763 					RenameStmt *n = makeNode(RenameStmt);
8764 					n->renameType = OBJECT_MATVIEW;
8765 					n->relation = $4;
8766 					n->subname = NULL;
8767 					n->newname = $7;
8768 					n->missing_ok = false;
8769 					$$ = (Node *)n;
8770 				}
8771 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
8772 				{
8773 					RenameStmt *n = makeNode(RenameStmt);
8774 					n->renameType = OBJECT_MATVIEW;
8775 					n->relation = $6;
8776 					n->subname = NULL;
8777 					n->newname = $9;
8778 					n->missing_ok = true;
8779 					$$ = (Node *)n;
8780 				}
8781 			| ALTER INDEX qualified_name RENAME TO name
8782 				{
8783 					RenameStmt *n = makeNode(RenameStmt);
8784 					n->renameType = OBJECT_INDEX;
8785 					n->relation = $3;
8786 					n->subname = NULL;
8787 					n->newname = $6;
8788 					n->missing_ok = false;
8789 					$$ = (Node *)n;
8790 				}
8791 			| ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
8792 				{
8793 					RenameStmt *n = makeNode(RenameStmt);
8794 					n->renameType = OBJECT_INDEX;
8795 					n->relation = $5;
8796 					n->subname = NULL;
8797 					n->newname = $8;
8798 					n->missing_ok = true;
8799 					$$ = (Node *)n;
8800 				}
8801 			| ALTER FOREIGN TABLE relation_expr RENAME TO name
8802 				{
8803 					RenameStmt *n = makeNode(RenameStmt);
8804 					n->renameType = OBJECT_FOREIGN_TABLE;
8805 					n->relation = $4;
8806 					n->subname = NULL;
8807 					n->newname = $7;
8808 					n->missing_ok = false;
8809 					$$ = (Node *)n;
8810 				}
8811 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
8812 				{
8813 					RenameStmt *n = makeNode(RenameStmt);
8814 					n->renameType = OBJECT_FOREIGN_TABLE;
8815 					n->relation = $6;
8816 					n->subname = NULL;
8817 					n->newname = $9;
8818 					n->missing_ok = true;
8819 					$$ = (Node *)n;
8820 				}
8821 			| ALTER TABLE relation_expr RENAME opt_column name TO name
8822 				{
8823 					RenameStmt *n = makeNode(RenameStmt);
8824 					n->renameType = OBJECT_COLUMN;
8825 					n->relationType = OBJECT_TABLE;
8826 					n->relation = $3;
8827 					n->subname = $6;
8828 					n->newname = $8;
8829 					n->missing_ok = false;
8830 					$$ = (Node *)n;
8831 				}
8832 			| ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8833 				{
8834 					RenameStmt *n = makeNode(RenameStmt);
8835 					n->renameType = OBJECT_COLUMN;
8836 					n->relationType = OBJECT_TABLE;
8837 					n->relation = $5;
8838 					n->subname = $8;
8839 					n->newname = $10;
8840 					n->missing_ok = true;
8841 					$$ = (Node *)n;
8842 				}
8843 			| ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
8844 				{
8845 					RenameStmt *n = makeNode(RenameStmt);
8846 					n->renameType = OBJECT_COLUMN;
8847 					n->relationType = OBJECT_MATVIEW;
8848 					n->relation = $4;
8849 					n->subname = $7;
8850 					n->newname = $9;
8851 					n->missing_ok = false;
8852 					$$ = (Node *)n;
8853 				}
8854 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
8855 				{
8856 					RenameStmt *n = makeNode(RenameStmt);
8857 					n->renameType = OBJECT_COLUMN;
8858 					n->relationType = OBJECT_MATVIEW;
8859 					n->relation = $6;
8860 					n->subname = $9;
8861 					n->newname = $11;
8862 					n->missing_ok = true;
8863 					$$ = (Node *)n;
8864 				}
8865 			| ALTER TABLE relation_expr RENAME CONSTRAINT name TO name
8866 				{
8867 					RenameStmt *n = makeNode(RenameStmt);
8868 					n->renameType = OBJECT_TABCONSTRAINT;
8869 					n->relation = $3;
8870 					n->subname = $6;
8871 					n->newname = $8;
8872 					n->missing_ok = false;
8873 					$$ = (Node *)n;
8874 				}
8875 			| ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
8876 				{
8877 					RenameStmt *n = makeNode(RenameStmt);
8878 					n->renameType = OBJECT_TABCONSTRAINT;
8879 					n->relation = $5;
8880 					n->subname = $8;
8881 					n->newname = $10;
8882 					n->missing_ok = true;
8883 					$$ = (Node *)n;
8884 				}
8885 			| ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
8886 				{
8887 					RenameStmt *n = makeNode(RenameStmt);
8888 					n->renameType = OBJECT_COLUMN;
8889 					n->relationType = OBJECT_FOREIGN_TABLE;
8890 					n->relation = $4;
8891 					n->subname = $7;
8892 					n->newname = $9;
8893 					n->missing_ok = false;
8894 					$$ = (Node *)n;
8895 				}
8896 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8897 				{
8898 					RenameStmt *n = makeNode(RenameStmt);
8899 					n->renameType = OBJECT_COLUMN;
8900 					n->relationType = OBJECT_FOREIGN_TABLE;
8901 					n->relation = $6;
8902 					n->subname = $9;
8903 					n->newname = $11;
8904 					n->missing_ok = true;
8905 					$$ = (Node *)n;
8906 				}
8907 			| ALTER RULE name ON qualified_name RENAME TO name
8908 				{
8909 					RenameStmt *n = makeNode(RenameStmt);
8910 					n->renameType = OBJECT_RULE;
8911 					n->relation = $5;
8912 					n->subname = $3;
8913 					n->newname = $8;
8914 					n->missing_ok = false;
8915 					$$ = (Node *)n;
8916 				}
8917 			| ALTER TRIGGER name ON qualified_name RENAME TO name
8918 				{
8919 					RenameStmt *n = makeNode(RenameStmt);
8920 					n->renameType = OBJECT_TRIGGER;
8921 					n->relation = $5;
8922 					n->subname = $3;
8923 					n->newname = $8;
8924 					n->missing_ok = false;
8925 					$$ = (Node *)n;
8926 				}
8927 			| ALTER EVENT TRIGGER name RENAME TO name
8928 				{
8929 					RenameStmt *n = makeNode(RenameStmt);
8930 					n->renameType = OBJECT_EVENT_TRIGGER;
8931 					n->object = (Node *) makeString($4);
8932 					n->newname = $7;
8933 					$$ = (Node *)n;
8934 				}
8935 			| ALTER ROLE RoleId RENAME TO RoleId
8936 				{
8937 					RenameStmt *n = makeNode(RenameStmt);
8938 					n->renameType = OBJECT_ROLE;
8939 					n->subname = $3;
8940 					n->newname = $6;
8941 					n->missing_ok = false;
8942 					$$ = (Node *)n;
8943 				}
8944 			| ALTER USER RoleId RENAME TO RoleId
8945 				{
8946 					RenameStmt *n = makeNode(RenameStmt);
8947 					n->renameType = OBJECT_ROLE;
8948 					n->subname = $3;
8949 					n->newname = $6;
8950 					n->missing_ok = false;
8951 					$$ = (Node *)n;
8952 				}
8953 			| ALTER TABLESPACE name RENAME TO name
8954 				{
8955 					RenameStmt *n = makeNode(RenameStmt);
8956 					n->renameType = OBJECT_TABLESPACE;
8957 					n->subname = $3;
8958 					n->newname = $6;
8959 					n->missing_ok = false;
8960 					$$ = (Node *)n;
8961 				}
8962 			| ALTER STATISTICS any_name RENAME TO name
8963 				{
8964 					RenameStmt *n = makeNode(RenameStmt);
8965 					n->renameType = OBJECT_STATISTIC_EXT;
8966 					n->object = (Node *) $3;
8967 					n->newname = $6;
8968 					n->missing_ok = false;
8969 					$$ = (Node *)n;
8970 				}
8971 			| ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
8972 				{
8973 					RenameStmt *n = makeNode(RenameStmt);
8974 					n->renameType = OBJECT_TSPARSER;
8975 					n->object = (Node *) $5;
8976 					n->newname = $8;
8977 					n->missing_ok = false;
8978 					$$ = (Node *)n;
8979 				}
8980 			| ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
8981 				{
8982 					RenameStmt *n = makeNode(RenameStmt);
8983 					n->renameType = OBJECT_TSDICTIONARY;
8984 					n->object = (Node *) $5;
8985 					n->newname = $8;
8986 					n->missing_ok = false;
8987 					$$ = (Node *)n;
8988 				}
8989 			| ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
8990 				{
8991 					RenameStmt *n = makeNode(RenameStmt);
8992 					n->renameType = OBJECT_TSTEMPLATE;
8993 					n->object = (Node *) $5;
8994 					n->newname = $8;
8995 					n->missing_ok = false;
8996 					$$ = (Node *)n;
8997 				}
8998 			| ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
8999 				{
9000 					RenameStmt *n = makeNode(RenameStmt);
9001 					n->renameType = OBJECT_TSCONFIGURATION;
9002 					n->object = (Node *) $5;
9003 					n->newname = $8;
9004 					n->missing_ok = false;
9005 					$$ = (Node *)n;
9006 				}
9007 			| ALTER TYPE_P any_name RENAME TO name
9008 				{
9009 					RenameStmt *n = makeNode(RenameStmt);
9010 					n->renameType = OBJECT_TYPE;
9011 					n->object = (Node *) $3;
9012 					n->newname = $6;
9013 					n->missing_ok = false;
9014 					$$ = (Node *)n;
9015 				}
9016 			| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
9017 				{
9018 					RenameStmt *n = makeNode(RenameStmt);
9019 					n->renameType = OBJECT_ATTRIBUTE;
9020 					n->relationType = OBJECT_TYPE;
9021 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
9022 					n->subname = $6;
9023 					n->newname = $8;
9024 					n->behavior = $9;
9025 					n->missing_ok = false;
9026 					$$ = (Node *)n;
9027 				}
9028 		;
9029 
9030 opt_column: COLUMN									{ $$ = COLUMN; }
9031 			| /*EMPTY*/								{ $$ = 0; }
9032 		;
9033 
9034 opt_set_data: SET DATA_P							{ $$ = 1; }
9035 			| /*EMPTY*/								{ $$ = 0; }
9036 		;
9037 
9038 /*****************************************************************************
9039  *
9040  * ALTER THING name DEPENDS ON EXTENSION name
9041  *
9042  *****************************************************************************/
9043 
9044 AlterObjectDependsStmt:
9045 			ALTER FUNCTION function_with_argtypes DEPENDS ON EXTENSION name
9046 				{
9047 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9048 					n->objectType = OBJECT_FUNCTION;
9049 					n->object = (Node *) $3;
9050 					n->extname = makeString($7);
9051 					$$ = (Node *)n;
9052 				}
9053 			| ALTER PROCEDURE function_with_argtypes DEPENDS ON EXTENSION name
9054 				{
9055 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9056 					n->objectType = OBJECT_PROCEDURE;
9057 					n->object = (Node *) $3;
9058 					n->extname = makeString($7);
9059 					$$ = (Node *)n;
9060 				}
9061 			| ALTER ROUTINE function_with_argtypes DEPENDS ON EXTENSION name
9062 				{
9063 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9064 					n->objectType = OBJECT_ROUTINE;
9065 					n->object = (Node *) $3;
9066 					n->extname = makeString($7);
9067 					$$ = (Node *)n;
9068 				}
9069 			| ALTER TRIGGER name ON qualified_name DEPENDS ON EXTENSION name
9070 				{
9071 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9072 					n->objectType = OBJECT_TRIGGER;
9073 					n->relation = $5;
9074 					n->object = (Node *) list_make1(makeString($3));
9075 					n->extname = makeString($9);
9076 					$$ = (Node *)n;
9077 				}
9078 			| ALTER MATERIALIZED VIEW qualified_name DEPENDS ON EXTENSION name
9079 				{
9080 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9081 					n->objectType = OBJECT_MATVIEW;
9082 					n->relation = $4;
9083 					n->extname = makeString($8);
9084 					$$ = (Node *)n;
9085 				}
9086 			| ALTER INDEX qualified_name DEPENDS ON EXTENSION name
9087 				{
9088 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9089 					n->objectType = OBJECT_INDEX;
9090 					n->relation = $3;
9091 					n->extname = makeString($7);
9092 					$$ = (Node *)n;
9093 				}
9094 		;
9095 
9096 /*****************************************************************************
9097  *
9098  * ALTER THING name SET SCHEMA name
9099  *
9100  *****************************************************************************/
9101 
9102 AlterObjectSchemaStmt:
9103 			ALTER AGGREGATE aggregate_with_argtypes SET SCHEMA name
9104 				{
9105 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9106 					n->objectType = OBJECT_AGGREGATE;
9107 					n->object = (Node *) $3;
9108 					n->newschema = $6;
9109 					n->missing_ok = false;
9110 					$$ = (Node *)n;
9111 				}
9112 			| ALTER COLLATION any_name SET SCHEMA name
9113 				{
9114 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9115 					n->objectType = OBJECT_COLLATION;
9116 					n->object = (Node *) $3;
9117 					n->newschema = $6;
9118 					n->missing_ok = false;
9119 					$$ = (Node *)n;
9120 				}
9121 			| ALTER CONVERSION_P any_name SET SCHEMA name
9122 				{
9123 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9124 					n->objectType = OBJECT_CONVERSION;
9125 					n->object = (Node *) $3;
9126 					n->newschema = $6;
9127 					n->missing_ok = false;
9128 					$$ = (Node *)n;
9129 				}
9130 			| ALTER DOMAIN_P any_name SET SCHEMA name
9131 				{
9132 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9133 					n->objectType = OBJECT_DOMAIN;
9134 					n->object = (Node *) $3;
9135 					n->newschema = $6;
9136 					n->missing_ok = false;
9137 					$$ = (Node *)n;
9138 				}
9139 			| ALTER EXTENSION name SET SCHEMA name
9140 				{
9141 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9142 					n->objectType = OBJECT_EXTENSION;
9143 					n->object = (Node *) makeString($3);
9144 					n->newschema = $6;
9145 					n->missing_ok = false;
9146 					$$ = (Node *)n;
9147 				}
9148 			| ALTER FUNCTION function_with_argtypes SET SCHEMA name
9149 				{
9150 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9151 					n->objectType = OBJECT_FUNCTION;
9152 					n->object = (Node *) $3;
9153 					n->newschema = $6;
9154 					n->missing_ok = false;
9155 					$$ = (Node *)n;
9156 				}
9157 			| ALTER OPERATOR operator_with_argtypes SET SCHEMA name
9158 				{
9159 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9160 					n->objectType = OBJECT_OPERATOR;
9161 					n->object = (Node *) $3;
9162 					n->newschema = $6;
9163 					n->missing_ok = false;
9164 					$$ = (Node *)n;
9165 				}
9166 			| ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
9167 				{
9168 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9169 					n->objectType = OBJECT_OPCLASS;
9170 					n->object = (Node *) lcons(makeString($6), $4);
9171 					n->newschema = $9;
9172 					n->missing_ok = false;
9173 					$$ = (Node *)n;
9174 				}
9175 			| ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
9176 				{
9177 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9178 					n->objectType = OBJECT_OPFAMILY;
9179 					n->object = (Node *) lcons(makeString($6), $4);
9180 					n->newschema = $9;
9181 					n->missing_ok = false;
9182 					$$ = (Node *)n;
9183 				}
9184 			| ALTER PROCEDURE function_with_argtypes SET SCHEMA name
9185 				{
9186 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9187 					n->objectType = OBJECT_PROCEDURE;
9188 					n->object = (Node *) $3;
9189 					n->newschema = $6;
9190 					n->missing_ok = false;
9191 					$$ = (Node *)n;
9192 				}
9193 			| ALTER ROUTINE function_with_argtypes SET SCHEMA name
9194 				{
9195 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9196 					n->objectType = OBJECT_ROUTINE;
9197 					n->object = (Node *) $3;
9198 					n->newschema = $6;
9199 					n->missing_ok = false;
9200 					$$ = (Node *)n;
9201 				}
9202 			| ALTER TABLE relation_expr SET SCHEMA name
9203 				{
9204 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9205 					n->objectType = OBJECT_TABLE;
9206 					n->relation = $3;
9207 					n->newschema = $6;
9208 					n->missing_ok = false;
9209 					$$ = (Node *)n;
9210 				}
9211 			| ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
9212 				{
9213 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9214 					n->objectType = OBJECT_TABLE;
9215 					n->relation = $5;
9216 					n->newschema = $8;
9217 					n->missing_ok = true;
9218 					$$ = (Node *)n;
9219 				}
9220 			| ALTER STATISTICS any_name SET SCHEMA name
9221 				{
9222 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9223 					n->objectType = OBJECT_STATISTIC_EXT;
9224 					n->object = (Node *) $3;
9225 					n->newschema = $6;
9226 					n->missing_ok = false;
9227 					$$ = (Node *)n;
9228 				}
9229 			| ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
9230 				{
9231 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9232 					n->objectType = OBJECT_TSPARSER;
9233 					n->object = (Node *) $5;
9234 					n->newschema = $8;
9235 					n->missing_ok = false;
9236 					$$ = (Node *)n;
9237 				}
9238 			| ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
9239 				{
9240 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9241 					n->objectType = OBJECT_TSDICTIONARY;
9242 					n->object = (Node *) $5;
9243 					n->newschema = $8;
9244 					n->missing_ok = false;
9245 					$$ = (Node *)n;
9246 				}
9247 			| ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
9248 				{
9249 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9250 					n->objectType = OBJECT_TSTEMPLATE;
9251 					n->object = (Node *) $5;
9252 					n->newschema = $8;
9253 					n->missing_ok = false;
9254 					$$ = (Node *)n;
9255 				}
9256 			| ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
9257 				{
9258 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9259 					n->objectType = OBJECT_TSCONFIGURATION;
9260 					n->object = (Node *) $5;
9261 					n->newschema = $8;
9262 					n->missing_ok = false;
9263 					$$ = (Node *)n;
9264 				}
9265 			| ALTER SEQUENCE qualified_name SET SCHEMA name
9266 				{
9267 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9268 					n->objectType = OBJECT_SEQUENCE;
9269 					n->relation = $3;
9270 					n->newschema = $6;
9271 					n->missing_ok = false;
9272 					$$ = (Node *)n;
9273 				}
9274 			| ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
9275 				{
9276 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9277 					n->objectType = OBJECT_SEQUENCE;
9278 					n->relation = $5;
9279 					n->newschema = $8;
9280 					n->missing_ok = true;
9281 					$$ = (Node *)n;
9282 				}
9283 			| ALTER VIEW qualified_name SET SCHEMA name
9284 				{
9285 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9286 					n->objectType = OBJECT_VIEW;
9287 					n->relation = $3;
9288 					n->newschema = $6;
9289 					n->missing_ok = false;
9290 					$$ = (Node *)n;
9291 				}
9292 			| ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
9293 				{
9294 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9295 					n->objectType = OBJECT_VIEW;
9296 					n->relation = $5;
9297 					n->newschema = $8;
9298 					n->missing_ok = true;
9299 					$$ = (Node *)n;
9300 				}
9301 			| ALTER MATERIALIZED VIEW qualified_name SET SCHEMA name
9302 				{
9303 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9304 					n->objectType = OBJECT_MATVIEW;
9305 					n->relation = $4;
9306 					n->newschema = $7;
9307 					n->missing_ok = false;
9308 					$$ = (Node *)n;
9309 				}
9310 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name SET SCHEMA name
9311 				{
9312 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9313 					n->objectType = OBJECT_MATVIEW;
9314 					n->relation = $6;
9315 					n->newschema = $9;
9316 					n->missing_ok = true;
9317 					$$ = (Node *)n;
9318 				}
9319 			| ALTER FOREIGN TABLE relation_expr SET SCHEMA name
9320 				{
9321 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9322 					n->objectType = OBJECT_FOREIGN_TABLE;
9323 					n->relation = $4;
9324 					n->newschema = $7;
9325 					n->missing_ok = false;
9326 					$$ = (Node *)n;
9327 				}
9328 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
9329 				{
9330 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9331 					n->objectType = OBJECT_FOREIGN_TABLE;
9332 					n->relation = $6;
9333 					n->newschema = $9;
9334 					n->missing_ok = true;
9335 					$$ = (Node *)n;
9336 				}
9337 			| ALTER TYPE_P any_name SET SCHEMA name
9338 				{
9339 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9340 					n->objectType = OBJECT_TYPE;
9341 					n->object = (Node *) $3;
9342 					n->newschema = $6;
9343 					n->missing_ok = false;
9344 					$$ = (Node *)n;
9345 				}
9346 		;
9347 
9348 /*****************************************************************************
9349  *
9350  * ALTER OPERATOR name SET define
9351  *
9352  *****************************************************************************/
9353 
9354 AlterOperatorStmt:
9355 			ALTER OPERATOR operator_with_argtypes SET '(' operator_def_list ')'
9356 				{
9357 					AlterOperatorStmt *n = makeNode(AlterOperatorStmt);
9358 					n->opername = $3;
9359 					n->options = $6;
9360 					$$ = (Node *)n;
9361 				}
9362 		;
9363 
9364 operator_def_list:	operator_def_elem								{ $$ = list_make1($1); }
9365 			| operator_def_list ',' operator_def_elem				{ $$ = lappend($1, $3); }
9366 		;
9367 
9368 operator_def_elem: ColLabel '=' NONE
9369 						{ $$ = makeDefElem($1, NULL, @1); }
9370 				   | ColLabel '=' operator_def_arg
9371 						{ $$ = makeDefElem($1, (Node *) $3, @1); }
9372 		;
9373 
9374 /* must be similar enough to def_arg to avoid reduce/reduce conflicts */
9375 operator_def_arg:
9376 			func_type						{ $$ = (Node *)$1; }
9377 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
9378 			| qual_all_Op					{ $$ = (Node *)$1; }
9379 			| NumericOnly					{ $$ = (Node *)$1; }
9380 			| Sconst						{ $$ = (Node *)makeString($1); }
9381 		;
9382 
9383 /*****************************************************************************
9384  *
9385  * ALTER THING name OWNER TO newname
9386  *
9387  *****************************************************************************/
9388 
9389 AlterOwnerStmt: ALTER AGGREGATE aggregate_with_argtypes OWNER TO RoleSpec
9390 				{
9391 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9392 					n->objectType = OBJECT_AGGREGATE;
9393 					n->object = (Node *) $3;
9394 					n->newowner = $6;
9395 					$$ = (Node *)n;
9396 				}
9397 			| ALTER COLLATION any_name OWNER TO RoleSpec
9398 				{
9399 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9400 					n->objectType = OBJECT_COLLATION;
9401 					n->object = (Node *) $3;
9402 					n->newowner = $6;
9403 					$$ = (Node *)n;
9404 				}
9405 			| ALTER CONVERSION_P any_name OWNER TO RoleSpec
9406 				{
9407 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9408 					n->objectType = OBJECT_CONVERSION;
9409 					n->object = (Node *) $3;
9410 					n->newowner = $6;
9411 					$$ = (Node *)n;
9412 				}
9413 			| ALTER DATABASE database_name OWNER TO RoleSpec
9414 				{
9415 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9416 					n->objectType = OBJECT_DATABASE;
9417 					n->object = (Node *) makeString($3);
9418 					n->newowner = $6;
9419 					$$ = (Node *)n;
9420 				}
9421 			| ALTER DOMAIN_P any_name OWNER TO RoleSpec
9422 				{
9423 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9424 					n->objectType = OBJECT_DOMAIN;
9425 					n->object = (Node *) $3;
9426 					n->newowner = $6;
9427 					$$ = (Node *)n;
9428 				}
9429 			| ALTER FUNCTION function_with_argtypes OWNER TO RoleSpec
9430 				{
9431 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9432 					n->objectType = OBJECT_FUNCTION;
9433 					n->object = (Node *) $3;
9434 					n->newowner = $6;
9435 					$$ = (Node *)n;
9436 				}
9437 			| ALTER opt_procedural LANGUAGE name OWNER TO RoleSpec
9438 				{
9439 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9440 					n->objectType = OBJECT_LANGUAGE;
9441 					n->object = (Node *) makeString($4);
9442 					n->newowner = $7;
9443 					$$ = (Node *)n;
9444 				}
9445 			| ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleSpec
9446 				{
9447 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9448 					n->objectType = OBJECT_LARGEOBJECT;
9449 					n->object = (Node *) $4;
9450 					n->newowner = $7;
9451 					$$ = (Node *)n;
9452 				}
9453 			| ALTER OPERATOR operator_with_argtypes OWNER TO RoleSpec
9454 				{
9455 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9456 					n->objectType = OBJECT_OPERATOR;
9457 					n->object = (Node *) $3;
9458 					n->newowner = $6;
9459 					$$ = (Node *)n;
9460 				}
9461 			| ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleSpec
9462 				{
9463 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9464 					n->objectType = OBJECT_OPCLASS;
9465 					n->object = (Node *) lcons(makeString($6), $4);
9466 					n->newowner = $9;
9467 					$$ = (Node *)n;
9468 				}
9469 			| ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleSpec
9470 				{
9471 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9472 					n->objectType = OBJECT_OPFAMILY;
9473 					n->object = (Node *) lcons(makeString($6), $4);
9474 					n->newowner = $9;
9475 					$$ = (Node *)n;
9476 				}
9477 			| ALTER PROCEDURE function_with_argtypes OWNER TO RoleSpec
9478 				{
9479 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9480 					n->objectType = OBJECT_PROCEDURE;
9481 					n->object = (Node *) $3;
9482 					n->newowner = $6;
9483 					$$ = (Node *)n;
9484 				}
9485 			| ALTER ROUTINE function_with_argtypes OWNER TO RoleSpec
9486 				{
9487 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9488 					n->objectType = OBJECT_ROUTINE;
9489 					n->object = (Node *) $3;
9490 					n->newowner = $6;
9491 					$$ = (Node *)n;
9492 				}
9493 			| ALTER SCHEMA name OWNER TO RoleSpec
9494 				{
9495 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9496 					n->objectType = OBJECT_SCHEMA;
9497 					n->object = (Node *) makeString($3);
9498 					n->newowner = $6;
9499 					$$ = (Node *)n;
9500 				}
9501 			| ALTER TYPE_P any_name OWNER TO RoleSpec
9502 				{
9503 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9504 					n->objectType = OBJECT_TYPE;
9505 					n->object = (Node *) $3;
9506 					n->newowner = $6;
9507 					$$ = (Node *)n;
9508 				}
9509 			| ALTER TABLESPACE name OWNER TO RoleSpec
9510 				{
9511 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9512 					n->objectType = OBJECT_TABLESPACE;
9513 					n->object = (Node *) makeString($3);
9514 					n->newowner = $6;
9515 					$$ = (Node *)n;
9516 				}
9517 			| ALTER STATISTICS any_name OWNER TO RoleSpec
9518 				{
9519 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9520 					n->objectType = OBJECT_STATISTIC_EXT;
9521 					n->object = (Node *) $3;
9522 					n->newowner = $6;
9523 					$$ = (Node *)n;
9524 				}
9525 			| ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleSpec
9526 				{
9527 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9528 					n->objectType = OBJECT_TSDICTIONARY;
9529 					n->object = (Node *) $5;
9530 					n->newowner = $8;
9531 					$$ = (Node *)n;
9532 				}
9533 			| ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleSpec
9534 				{
9535 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9536 					n->objectType = OBJECT_TSCONFIGURATION;
9537 					n->object = (Node *) $5;
9538 					n->newowner = $8;
9539 					$$ = (Node *)n;
9540 				}
9541 			| ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleSpec
9542 				{
9543 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9544 					n->objectType = OBJECT_FDW;
9545 					n->object = (Node *) makeString($5);
9546 					n->newowner = $8;
9547 					$$ = (Node *)n;
9548 				}
9549 			| ALTER SERVER name OWNER TO RoleSpec
9550 				{
9551 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9552 					n->objectType = OBJECT_FOREIGN_SERVER;
9553 					n->object = (Node *) makeString($3);
9554 					n->newowner = $6;
9555 					$$ = (Node *)n;
9556 				}
9557 			| ALTER EVENT TRIGGER name OWNER TO RoleSpec
9558 				{
9559 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9560 					n->objectType = OBJECT_EVENT_TRIGGER;
9561 					n->object = (Node *) makeString($4);
9562 					n->newowner = $7;
9563 					$$ = (Node *)n;
9564 				}
9565 			| ALTER PUBLICATION name OWNER TO RoleSpec
9566 				{
9567 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9568 					n->objectType = OBJECT_PUBLICATION;
9569 					n->object = (Node *) makeString($3);
9570 					n->newowner = $6;
9571 					$$ = (Node *)n;
9572 				}
9573 			| ALTER SUBSCRIPTION name OWNER TO RoleSpec
9574 				{
9575 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9576 					n->objectType = OBJECT_SUBSCRIPTION;
9577 					n->object = (Node *) makeString($3);
9578 					n->newowner = $6;
9579 					$$ = (Node *)n;
9580 				}
9581 		;
9582 
9583 
9584 /*****************************************************************************
9585  *
9586  * CREATE PUBLICATION name [ FOR TABLE ] [ WITH options ]
9587  *
9588  *****************************************************************************/
9589 
9590 CreatePublicationStmt:
9591 			CREATE PUBLICATION name opt_publication_for_tables opt_definition
9592 				{
9593 					CreatePublicationStmt *n = makeNode(CreatePublicationStmt);
9594 					n->pubname = $3;
9595 					n->options = $5;
9596 					if ($4 != NULL)
9597 					{
9598 						/* FOR TABLE */
9599 						if (IsA($4, List))
9600 							n->tables = (List *)$4;
9601 						/* FOR ALL TABLES */
9602 						else
9603 							n->for_all_tables = true;
9604 					}
9605 					$$ = (Node *)n;
9606 				}
9607 		;
9608 
9609 opt_publication_for_tables:
9610 			publication_for_tables					{ $$ = $1; }
9611 			| /* EMPTY */							{ $$ = NULL; }
9612 		;
9613 
9614 publication_for_tables:
9615 			FOR TABLE relation_expr_list
9616 				{
9617 					$$ = (Node *) $3;
9618 				}
9619 			| FOR ALL TABLES
9620 				{
9621 					$$ = (Node *) makeInteger(true);
9622 				}
9623 		;
9624 
9625 
9626 /*****************************************************************************
9627  *
9628  * ALTER PUBLICATION name SET ( options )
9629  *
9630  * ALTER PUBLICATION name ADD TABLE table [, table2]
9631  *
9632  * ALTER PUBLICATION name DROP TABLE table [, table2]
9633  *
9634  * ALTER PUBLICATION name SET TABLE table [, table2]
9635  *
9636  *****************************************************************************/
9637 
9638 AlterPublicationStmt:
9639 			ALTER PUBLICATION name SET definition
9640 				{
9641 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9642 					n->pubname = $3;
9643 					n->options = $5;
9644 					$$ = (Node *)n;
9645 				}
9646 			| ALTER PUBLICATION name ADD_P TABLE relation_expr_list
9647 				{
9648 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9649 					n->pubname = $3;
9650 					n->tables = $6;
9651 					n->tableAction = DEFELEM_ADD;
9652 					$$ = (Node *)n;
9653 				}
9654 			| ALTER PUBLICATION name SET TABLE relation_expr_list
9655 				{
9656 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9657 					n->pubname = $3;
9658 					n->tables = $6;
9659 					n->tableAction = DEFELEM_SET;
9660 					$$ = (Node *)n;
9661 				}
9662 			| ALTER PUBLICATION name DROP TABLE relation_expr_list
9663 				{
9664 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9665 					n->pubname = $3;
9666 					n->tables = $6;
9667 					n->tableAction = DEFELEM_DROP;
9668 					$$ = (Node *)n;
9669 				}
9670 		;
9671 
9672 /*****************************************************************************
9673  *
9674  * CREATE SUBSCRIPTION name ...
9675  *
9676  *****************************************************************************/
9677 
9678 CreateSubscriptionStmt:
9679 			CREATE SUBSCRIPTION name CONNECTION Sconst PUBLICATION publication_name_list opt_definition
9680 				{
9681 					CreateSubscriptionStmt *n =
9682 						makeNode(CreateSubscriptionStmt);
9683 					n->subname = $3;
9684 					n->conninfo = $5;
9685 					n->publication = $7;
9686 					n->options = $8;
9687 					$$ = (Node *)n;
9688 				}
9689 		;
9690 
9691 publication_name_list:
9692 			publication_name_item
9693 				{
9694 					$$ = list_make1($1);
9695 				}
9696 			| publication_name_list ',' publication_name_item
9697 				{
9698 					$$ = lappend($1, $3);
9699 				}
9700 		;
9701 
9702 publication_name_item:
9703 			ColLabel			{ $$ = makeString($1); };
9704 
9705 /*****************************************************************************
9706  *
9707  * ALTER SUBSCRIPTION name ...
9708  *
9709  *****************************************************************************/
9710 
9711 AlterSubscriptionStmt:
9712 			ALTER SUBSCRIPTION name SET definition
9713 				{
9714 					AlterSubscriptionStmt *n =
9715 						makeNode(AlterSubscriptionStmt);
9716 					n->kind = ALTER_SUBSCRIPTION_OPTIONS;
9717 					n->subname = $3;
9718 					n->options = $5;
9719 					$$ = (Node *)n;
9720 				}
9721 			| ALTER SUBSCRIPTION name CONNECTION Sconst
9722 				{
9723 					AlterSubscriptionStmt *n =
9724 						makeNode(AlterSubscriptionStmt);
9725 					n->kind = ALTER_SUBSCRIPTION_CONNECTION;
9726 					n->subname = $3;
9727 					n->conninfo = $5;
9728 					$$ = (Node *)n;
9729 				}
9730 			| ALTER SUBSCRIPTION name REFRESH PUBLICATION opt_definition
9731 				{
9732 					AlterSubscriptionStmt *n =
9733 						makeNode(AlterSubscriptionStmt);
9734 					n->kind = ALTER_SUBSCRIPTION_REFRESH;
9735 					n->subname = $3;
9736 					n->options = $6;
9737 					$$ = (Node *)n;
9738 				}
9739 			| ALTER SUBSCRIPTION name SET PUBLICATION publication_name_list opt_definition
9740 				{
9741 					AlterSubscriptionStmt *n =
9742 						makeNode(AlterSubscriptionStmt);
9743 					n->kind = ALTER_SUBSCRIPTION_PUBLICATION;
9744 					n->subname = $3;
9745 					n->publication = $6;
9746 					n->options = $7;
9747 					$$ = (Node *)n;
9748 				}
9749 			| ALTER SUBSCRIPTION name ENABLE_P
9750 				{
9751 					AlterSubscriptionStmt *n =
9752 						makeNode(AlterSubscriptionStmt);
9753 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9754 					n->subname = $3;
9755 					n->options = list_make1(makeDefElem("enabled",
9756 											(Node *)makeInteger(true), @1));
9757 					$$ = (Node *)n;
9758 				}
9759 			| ALTER SUBSCRIPTION name DISABLE_P
9760 				{
9761 					AlterSubscriptionStmt *n =
9762 						makeNode(AlterSubscriptionStmt);
9763 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9764 					n->subname = $3;
9765 					n->options = list_make1(makeDefElem("enabled",
9766 											(Node *)makeInteger(false), @1));
9767 					$$ = (Node *)n;
9768 				}
9769 		;
9770 
9771 /*****************************************************************************
9772  *
9773  * DROP SUBSCRIPTION [ IF EXISTS ] name
9774  *
9775  *****************************************************************************/
9776 
9777 DropSubscriptionStmt: DROP SUBSCRIPTION name opt_drop_behavior
9778 				{
9779 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9780 					n->subname = $3;
9781 					n->missing_ok = false;
9782 					n->behavior = $4;
9783 					$$ = (Node *) n;
9784 				}
9785 				|  DROP SUBSCRIPTION IF_P EXISTS name opt_drop_behavior
9786 				{
9787 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9788 					n->subname = $5;
9789 					n->missing_ok = true;
9790 					n->behavior = $6;
9791 					$$ = (Node *) n;
9792 				}
9793 		;
9794 
9795 /*****************************************************************************
9796  *
9797  *		QUERY:	Define Rewrite Rule
9798  *
9799  *****************************************************************************/
9800 
9801 RuleStmt:	CREATE opt_or_replace RULE name AS
9802 			ON event TO qualified_name where_clause
9803 			DO opt_instead RuleActionList
9804 				{
9805 					RuleStmt *n = makeNode(RuleStmt);
9806 					n->replace = $2;
9807 					n->relation = $9;
9808 					n->rulename = $4;
9809 					n->whereClause = $10;
9810 					n->event = $7;
9811 					n->instead = $12;
9812 					n->actions = $13;
9813 					$$ = (Node *)n;
9814 				}
9815 		;
9816 
9817 RuleActionList:
9818 			NOTHING									{ $$ = NIL; }
9819 			| RuleActionStmt						{ $$ = list_make1($1); }
9820 			| '(' RuleActionMulti ')'				{ $$ = $2; }
9821 		;
9822 
9823 /* the thrashing around here is to discard "empty" statements... */
9824 RuleActionMulti:
9825 			RuleActionMulti ';' RuleActionStmtOrEmpty
9826 				{ if ($3 != NULL)
9827 					$$ = lappend($1, $3);
9828 				  else
9829 					$$ = $1;
9830 				}
9831 			| RuleActionStmtOrEmpty
9832 				{ if ($1 != NULL)
9833 					$$ = list_make1($1);
9834 				  else
9835 					$$ = NIL;
9836 				}
9837 		;
9838 
9839 RuleActionStmt:
9840 			SelectStmt
9841 			| InsertStmt
9842 			| UpdateStmt
9843 			| DeleteStmt
9844 			| NotifyStmt
9845 		;
9846 
9847 RuleActionStmtOrEmpty:
9848 			RuleActionStmt							{ $$ = $1; }
9849 			|	/*EMPTY*/							{ $$ = NULL; }
9850 		;
9851 
9852 event:		SELECT									{ $$ = CMD_SELECT; }
9853 			| UPDATE								{ $$ = CMD_UPDATE; }
9854 			| DELETE_P								{ $$ = CMD_DELETE; }
9855 			| INSERT								{ $$ = CMD_INSERT; }
9856 		 ;
9857 
9858 opt_instead:
9859 			INSTEAD									{ $$ = true; }
9860 			| ALSO									{ $$ = false; }
9861 			| /*EMPTY*/								{ $$ = false; }
9862 		;
9863 
9864 
9865 /*****************************************************************************
9866  *
9867  *		QUERY:
9868  *				NOTIFY <identifier> can appear both in rule bodies and
9869  *				as a query-level command
9870  *
9871  *****************************************************************************/
9872 
9873 NotifyStmt: NOTIFY ColId notify_payload
9874 				{
9875 					NotifyStmt *n = makeNode(NotifyStmt);
9876 					n->conditionname = $2;
9877 					n->payload = $3;
9878 					$$ = (Node *)n;
9879 				}
9880 		;
9881 
9882 notify_payload:
9883 			',' Sconst							{ $$ = $2; }
9884 			| /*EMPTY*/							{ $$ = NULL; }
9885 		;
9886 
9887 ListenStmt: LISTEN ColId
9888 				{
9889 					ListenStmt *n = makeNode(ListenStmt);
9890 					n->conditionname = $2;
9891 					$$ = (Node *)n;
9892 				}
9893 		;
9894 
9895 UnlistenStmt:
9896 			UNLISTEN ColId
9897 				{
9898 					UnlistenStmt *n = makeNode(UnlistenStmt);
9899 					n->conditionname = $2;
9900 					$$ = (Node *)n;
9901 				}
9902 			| UNLISTEN '*'
9903 				{
9904 					UnlistenStmt *n = makeNode(UnlistenStmt);
9905 					n->conditionname = NULL;
9906 					$$ = (Node *)n;
9907 				}
9908 		;
9909 
9910 
9911 /*****************************************************************************
9912  *
9913  *		Transactions:
9914  *
9915  *		BEGIN / COMMIT / ROLLBACK
9916  *		(also older versions END / ABORT)
9917  *
9918  *****************************************************************************/
9919 
9920 TransactionStmt:
9921 			ABORT_P opt_transaction
9922 				{
9923 					TransactionStmt *n = makeNode(TransactionStmt);
9924 					n->kind = TRANS_STMT_ROLLBACK;
9925 					n->options = NIL;
9926 					$$ = (Node *)n;
9927 				}
9928 			| BEGIN_P opt_transaction transaction_mode_list_or_empty
9929 				{
9930 					TransactionStmt *n = makeNode(TransactionStmt);
9931 					n->kind = TRANS_STMT_BEGIN;
9932 					n->options = $3;
9933 					$$ = (Node *)n;
9934 				}
9935 			| START TRANSACTION transaction_mode_list_or_empty
9936 				{
9937 					TransactionStmt *n = makeNode(TransactionStmt);
9938 					n->kind = TRANS_STMT_START;
9939 					n->options = $3;
9940 					$$ = (Node *)n;
9941 				}
9942 			| COMMIT opt_transaction
9943 				{
9944 					TransactionStmt *n = makeNode(TransactionStmt);
9945 					n->kind = TRANS_STMT_COMMIT;
9946 					n->options = NIL;
9947 					$$ = (Node *)n;
9948 				}
9949 			| END_P opt_transaction
9950 				{
9951 					TransactionStmt *n = makeNode(TransactionStmt);
9952 					n->kind = TRANS_STMT_COMMIT;
9953 					n->options = NIL;
9954 					$$ = (Node *)n;
9955 				}
9956 			| ROLLBACK opt_transaction
9957 				{
9958 					TransactionStmt *n = makeNode(TransactionStmt);
9959 					n->kind = TRANS_STMT_ROLLBACK;
9960 					n->options = NIL;
9961 					$$ = (Node *)n;
9962 				}
9963 			| SAVEPOINT ColId
9964 				{
9965 					TransactionStmt *n = makeNode(TransactionStmt);
9966 					n->kind = TRANS_STMT_SAVEPOINT;
9967 					n->savepoint_name = $2;
9968 					$$ = (Node *)n;
9969 				}
9970 			| RELEASE SAVEPOINT ColId
9971 				{
9972 					TransactionStmt *n = makeNode(TransactionStmt);
9973 					n->kind = TRANS_STMT_RELEASE;
9974 					n->savepoint_name = $3;
9975 					$$ = (Node *)n;
9976 				}
9977 			| RELEASE ColId
9978 				{
9979 					TransactionStmt *n = makeNode(TransactionStmt);
9980 					n->kind = TRANS_STMT_RELEASE;
9981 					n->savepoint_name = $2;
9982 					$$ = (Node *)n;
9983 				}
9984 			| ROLLBACK opt_transaction TO SAVEPOINT ColId
9985 				{
9986 					TransactionStmt *n = makeNode(TransactionStmt);
9987 					n->kind = TRANS_STMT_ROLLBACK_TO;
9988 					n->savepoint_name = $5;
9989 					$$ = (Node *)n;
9990 				}
9991 			| ROLLBACK opt_transaction TO ColId
9992 				{
9993 					TransactionStmt *n = makeNode(TransactionStmt);
9994 					n->kind = TRANS_STMT_ROLLBACK_TO;
9995 					n->savepoint_name = $4;
9996 					$$ = (Node *)n;
9997 				}
9998 			| PREPARE TRANSACTION Sconst
9999 				{
10000 					TransactionStmt *n = makeNode(TransactionStmt);
10001 					n->kind = TRANS_STMT_PREPARE;
10002 					n->gid = $3;
10003 					$$ = (Node *)n;
10004 				}
10005 			| COMMIT PREPARED Sconst
10006 				{
10007 					TransactionStmt *n = makeNode(TransactionStmt);
10008 					n->kind = TRANS_STMT_COMMIT_PREPARED;
10009 					n->gid = $3;
10010 					$$ = (Node *)n;
10011 				}
10012 			| ROLLBACK PREPARED Sconst
10013 				{
10014 					TransactionStmt *n = makeNode(TransactionStmt);
10015 					n->kind = TRANS_STMT_ROLLBACK_PREPARED;
10016 					n->gid = $3;
10017 					$$ = (Node *)n;
10018 				}
10019 		;
10020 
10021 opt_transaction:	WORK							{}
10022 			| TRANSACTION							{}
10023 			| /*EMPTY*/								{}
10024 		;
10025 
10026 transaction_mode_item:
10027 			ISOLATION LEVEL iso_level
10028 					{ $$ = makeDefElem("transaction_isolation",
10029 									   makeStringConst($3, @3), @1); }
10030 			| READ ONLY
10031 					{ $$ = makeDefElem("transaction_read_only",
10032 									   makeIntConst(true, @1), @1); }
10033 			| READ WRITE
10034 					{ $$ = makeDefElem("transaction_read_only",
10035 									   makeIntConst(false, @1), @1); }
10036 			| DEFERRABLE
10037 					{ $$ = makeDefElem("transaction_deferrable",
10038 									   makeIntConst(true, @1), @1); }
10039 			| NOT DEFERRABLE
10040 					{ $$ = makeDefElem("transaction_deferrable",
10041 									   makeIntConst(false, @1), @1); }
10042 		;
10043 
10044 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
10045 transaction_mode_list:
10046 			transaction_mode_item
10047 					{ $$ = list_make1($1); }
10048 			| transaction_mode_list ',' transaction_mode_item
10049 					{ $$ = lappend($1, $3); }
10050 			| transaction_mode_list transaction_mode_item
10051 					{ $$ = lappend($1, $2); }
10052 		;
10053 
10054 transaction_mode_list_or_empty:
10055 			transaction_mode_list
10056 			| /* EMPTY */
10057 					{ $$ = NIL; }
10058 		;
10059 
10060 
10061 /*****************************************************************************
10062  *
10063  *	QUERY:
10064  *		CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
10065  *			AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
10066  *
10067  *****************************************************************************/
10068 
10069 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10070 				AS SelectStmt opt_check_option
10071 				{
10072 					ViewStmt *n = makeNode(ViewStmt);
10073 					n->view = $4;
10074 					n->view->relpersistence = $2;
10075 					n->aliases = $5;
10076 					n->query = $8;
10077 					n->replace = false;
10078 					n->options = $6;
10079 					n->withCheckOption = $9;
10080 					$$ = (Node *) n;
10081 				}
10082 		| CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10083 				AS SelectStmt opt_check_option
10084 				{
10085 					ViewStmt *n = makeNode(ViewStmt);
10086 					n->view = $6;
10087 					n->view->relpersistence = $4;
10088 					n->aliases = $7;
10089 					n->query = $10;
10090 					n->replace = true;
10091 					n->options = $8;
10092 					n->withCheckOption = $11;
10093 					$$ = (Node *) n;
10094 				}
10095 		| CREATE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10096 				AS SelectStmt opt_check_option
10097 				{
10098 					ViewStmt *n = makeNode(ViewStmt);
10099 					n->view = $5;
10100 					n->view->relpersistence = $2;
10101 					n->aliases = $7;
10102 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $11);
10103 					n->replace = false;
10104 					n->options = $9;
10105 					n->withCheckOption = $12;
10106 					if (n->withCheckOption != NO_CHECK_OPTION)
10107 						ereport(ERROR,
10108 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10109 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
10110 								 parser_errposition(@12)));
10111 					$$ = (Node *) n;
10112 				}
10113 		| CREATE OR REPLACE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10114 				AS SelectStmt opt_check_option
10115 				{
10116 					ViewStmt *n = makeNode(ViewStmt);
10117 					n->view = $7;
10118 					n->view->relpersistence = $4;
10119 					n->aliases = $9;
10120 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $13);
10121 					n->replace = true;
10122 					n->options = $11;
10123 					n->withCheckOption = $14;
10124 					if (n->withCheckOption != NO_CHECK_OPTION)
10125 						ereport(ERROR,
10126 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10127 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
10128 								 parser_errposition(@14)));
10129 					$$ = (Node *) n;
10130 				}
10131 		;
10132 
10133 opt_check_option:
10134 		WITH CHECK OPTION				{ $$ = CASCADED_CHECK_OPTION; }
10135 		| WITH CASCADED CHECK OPTION	{ $$ = CASCADED_CHECK_OPTION; }
10136 		| WITH LOCAL CHECK OPTION		{ $$ = LOCAL_CHECK_OPTION; }
10137 		| /* EMPTY */					{ $$ = NO_CHECK_OPTION; }
10138 		;
10139 
10140 /*****************************************************************************
10141  *
10142  *		QUERY:
10143  *				LOAD "filename"
10144  *
10145  *****************************************************************************/
10146 
10147 LoadStmt:	LOAD file_name
10148 				{
10149 					LoadStmt *n = makeNode(LoadStmt);
10150 					n->filename = $2;
10151 					$$ = (Node *)n;
10152 				}
10153 		;
10154 
10155 
10156 /*****************************************************************************
10157  *
10158  *		CREATE DATABASE
10159  *
10160  *****************************************************************************/
10161 
10162 CreatedbStmt:
10163 			CREATE DATABASE database_name opt_with createdb_opt_list
10164 				{
10165 					CreatedbStmt *n = makeNode(CreatedbStmt);
10166 					n->dbname = $3;
10167 					n->options = $5;
10168 					$$ = (Node *)n;
10169 				}
10170 		;
10171 
10172 createdb_opt_list:
10173 			createdb_opt_items						{ $$ = $1; }
10174 			| /* EMPTY */							{ $$ = NIL; }
10175 		;
10176 
10177 createdb_opt_items:
10178 			createdb_opt_item						{ $$ = list_make1($1); }
10179 			| createdb_opt_items createdb_opt_item	{ $$ = lappend($1, $2); }
10180 		;
10181 
10182 createdb_opt_item:
10183 			createdb_opt_name opt_equal SignedIconst
10184 				{
10185 					$$ = makeDefElem($1, (Node *)makeInteger($3), @1);
10186 				}
10187 			| createdb_opt_name opt_equal opt_boolean_or_string
10188 				{
10189 					$$ = makeDefElem($1, (Node *)makeString($3), @1);
10190 				}
10191 			| createdb_opt_name opt_equal DEFAULT
10192 				{
10193 					$$ = makeDefElem($1, NULL, @1);
10194 				}
10195 		;
10196 
10197 /*
10198  * Ideally we'd use ColId here, but that causes shift/reduce conflicts against
10199  * the ALTER DATABASE SET/RESET syntaxes.  Instead call out specific keywords
10200  * we need, and allow IDENT so that database option names don't have to be
10201  * parser keywords unless they are already keywords for other reasons.
10202  *
10203  * XXX this coding technique is fragile since if someone makes a formerly
10204  * non-keyword option name into a keyword and forgets to add it here, the
10205  * option will silently break.  Best defense is to provide a regression test
10206  * exercising every such option, at least at the syntax level.
10207  */
10208 createdb_opt_name:
10209 			IDENT							{ $$ = $1; }
10210 			| CONNECTION LIMIT				{ $$ = pstrdup("connection_limit"); }
10211 			| ENCODING						{ $$ = pstrdup($1); }
10212 			| LOCATION						{ $$ = pstrdup($1); }
10213 			| OWNER							{ $$ = pstrdup($1); }
10214 			| TABLESPACE					{ $$ = pstrdup($1); }
10215 			| TEMPLATE						{ $$ = pstrdup($1); }
10216 		;
10217 
10218 /*
10219  *	Though the equals sign doesn't match other WITH options, pg_dump uses
10220  *	equals for backward compatibility, and it doesn't seem worth removing it.
10221  */
10222 opt_equal:	'='										{}
10223 			| /*EMPTY*/								{}
10224 		;
10225 
10226 
10227 /*****************************************************************************
10228  *
10229  *		ALTER DATABASE
10230  *
10231  *****************************************************************************/
10232 
10233 AlterDatabaseStmt:
10234 			ALTER DATABASE database_name WITH createdb_opt_list
10235 				 {
10236 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10237 					n->dbname = $3;
10238 					n->options = $5;
10239 					$$ = (Node *)n;
10240 				 }
10241 			| ALTER DATABASE database_name createdb_opt_list
10242 				 {
10243 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10244 					n->dbname = $3;
10245 					n->options = $4;
10246 					$$ = (Node *)n;
10247 				 }
10248 			| ALTER DATABASE database_name SET TABLESPACE name
10249 				 {
10250 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10251 					n->dbname = $3;
10252 					n->options = list_make1(makeDefElem("tablespace",
10253 														(Node *)makeString($6), @6));
10254 					$$ = (Node *)n;
10255 				 }
10256 		;
10257 
10258 AlterDatabaseSetStmt:
10259 			ALTER DATABASE database_name SetResetClause
10260 				{
10261 					AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
10262 					n->dbname = $3;
10263 					n->setstmt = $4;
10264 					$$ = (Node *)n;
10265 				}
10266 		;
10267 
10268 
10269 /*****************************************************************************
10270  *
10271  *		DROP DATABASE [ IF EXISTS ]
10272  *
10273  * This is implicitly CASCADE, no need for drop behavior
10274  *****************************************************************************/
10275 
10276 DropdbStmt: DROP DATABASE database_name
10277 				{
10278 					DropdbStmt *n = makeNode(DropdbStmt);
10279 					n->dbname = $3;
10280 					n->missing_ok = false;
10281 					$$ = (Node *)n;
10282 				}
10283 			| DROP DATABASE IF_P EXISTS database_name
10284 				{
10285 					DropdbStmt *n = makeNode(DropdbStmt);
10286 					n->dbname = $5;
10287 					n->missing_ok = true;
10288 					$$ = (Node *)n;
10289 				}
10290 		;
10291 
10292 
10293 /*****************************************************************************
10294  *
10295  *		ALTER COLLATION
10296  *
10297  *****************************************************************************/
10298 
10299 AlterCollationStmt: ALTER COLLATION any_name REFRESH VERSION_P
10300 				{
10301 					AlterCollationStmt *n = makeNode(AlterCollationStmt);
10302 					n->collname = $3;
10303 					$$ = (Node *)n;
10304 				}
10305 		;
10306 
10307 
10308 /*****************************************************************************
10309  *
10310  *		ALTER SYSTEM
10311  *
10312  * This is used to change configuration parameters persistently.
10313  *****************************************************************************/
10314 
10315 AlterSystemStmt:
10316 			ALTER SYSTEM_P SET generic_set
10317 				{
10318 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
10319 					n->setstmt = $4;
10320 					$$ = (Node *)n;
10321 				}
10322 			| ALTER SYSTEM_P RESET generic_reset
10323 				{
10324 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
10325 					n->setstmt = $4;
10326 					$$ = (Node *)n;
10327 				}
10328 		;
10329 
10330 
10331 /*****************************************************************************
10332  *
10333  * Manipulate a domain
10334  *
10335  *****************************************************************************/
10336 
10337 CreateDomainStmt:
10338 			CREATE DOMAIN_P any_name opt_as Typename ColQualList
10339 				{
10340 					CreateDomainStmt *n = makeNode(CreateDomainStmt);
10341 					n->domainname = $3;
10342 					n->typeName = $5;
10343 					SplitColQualList($6, &n->constraints, &n->collClause,
10344 									 yyscanner);
10345 					$$ = (Node *)n;
10346 				}
10347 		;
10348 
10349 AlterDomainStmt:
10350 			/* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
10351 			ALTER DOMAIN_P any_name alter_column_default
10352 				{
10353 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10354 					n->subtype = 'T';
10355 					n->typeName = $3;
10356 					n->def = $4;
10357 					$$ = (Node *)n;
10358 				}
10359 			/* ALTER DOMAIN <domain> DROP NOT NULL */
10360 			| ALTER DOMAIN_P any_name DROP NOT NULL_P
10361 				{
10362 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10363 					n->subtype = 'N';
10364 					n->typeName = $3;
10365 					$$ = (Node *)n;
10366 				}
10367 			/* ALTER DOMAIN <domain> SET NOT NULL */
10368 			| ALTER DOMAIN_P any_name SET NOT NULL_P
10369 				{
10370 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10371 					n->subtype = 'O';
10372 					n->typeName = $3;
10373 					$$ = (Node *)n;
10374 				}
10375 			/* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
10376 			| ALTER DOMAIN_P any_name ADD_P TableConstraint
10377 				{
10378 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10379 					n->subtype = 'C';
10380 					n->typeName = $3;
10381 					n->def = $5;
10382 					$$ = (Node *)n;
10383 				}
10384 			/* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
10385 			| ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
10386 				{
10387 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10388 					n->subtype = 'X';
10389 					n->typeName = $3;
10390 					n->name = $6;
10391 					n->behavior = $7;
10392 					n->missing_ok = false;
10393 					$$ = (Node *)n;
10394 				}
10395 			/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
10396 			| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
10397 				{
10398 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10399 					n->subtype = 'X';
10400 					n->typeName = $3;
10401 					n->name = $8;
10402 					n->behavior = $9;
10403 					n->missing_ok = true;
10404 					$$ = (Node *)n;
10405 				}
10406 			/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
10407 			| ALTER DOMAIN_P any_name VALIDATE CONSTRAINT name
10408 				{
10409 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10410 					n->subtype = 'V';
10411 					n->typeName = $3;
10412 					n->name = $6;
10413 					$$ = (Node *)n;
10414 				}
10415 			;
10416 
10417 opt_as:		AS										{}
10418 			| /* EMPTY */							{}
10419 		;
10420 
10421 
10422 /*****************************************************************************
10423  *
10424  * Manipulate a text search dictionary or configuration
10425  *
10426  *****************************************************************************/
10427 
10428 AlterTSDictionaryStmt:
10429 			ALTER TEXT_P SEARCH DICTIONARY any_name definition
10430 				{
10431 					AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
10432 					n->dictname = $5;
10433 					n->options = $6;
10434 					$$ = (Node *)n;
10435 				}
10436 		;
10437 
10438 AlterTSConfigurationStmt:
10439 			ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list any_with any_name_list
10440 				{
10441 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10442 					n->kind = ALTER_TSCONFIG_ADD_MAPPING;
10443 					n->cfgname = $5;
10444 					n->tokentype = $9;
10445 					n->dicts = $11;
10446 					n->override = false;
10447 					n->replace = false;
10448 					$$ = (Node*)n;
10449 				}
10450 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list any_with any_name_list
10451 				{
10452 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10453 					n->kind = ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN;
10454 					n->cfgname = $5;
10455 					n->tokentype = $9;
10456 					n->dicts = $11;
10457 					n->override = true;
10458 					n->replace = false;
10459 					$$ = (Node*)n;
10460 				}
10461 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name any_with any_name
10462 				{
10463 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10464 					n->kind = ALTER_TSCONFIG_REPLACE_DICT;
10465 					n->cfgname = $5;
10466 					n->tokentype = NIL;
10467 					n->dicts = list_make2($9,$11);
10468 					n->override = false;
10469 					n->replace = true;
10470 					$$ = (Node*)n;
10471 				}
10472 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name any_with any_name
10473 				{
10474 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10475 					n->kind = ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN;
10476 					n->cfgname = $5;
10477 					n->tokentype = $9;
10478 					n->dicts = list_make2($11,$13);
10479 					n->override = false;
10480 					n->replace = true;
10481 					$$ = (Node*)n;
10482 				}
10483 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
10484 				{
10485 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10486 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10487 					n->cfgname = $5;
10488 					n->tokentype = $9;
10489 					n->missing_ok = false;
10490 					$$ = (Node*)n;
10491 				}
10492 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
10493 				{
10494 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10495 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10496 					n->cfgname = $5;
10497 					n->tokentype = $11;
10498 					n->missing_ok = true;
10499 					$$ = (Node*)n;
10500 				}
10501 		;
10502 
10503 /* Use this if TIME or ORDINALITY after WITH should be taken as an identifier */
10504 any_with:	WITH									{}
10505 			| WITH_LA								{}
10506 		;
10507 
10508 
10509 /*****************************************************************************
10510  *
10511  * Manipulate a conversion
10512  *
10513  *		CREATE [DEFAULT] CONVERSION <conversion_name>
10514  *		FOR <encoding_name> TO <encoding_name> FROM <func_name>
10515  *
10516  *****************************************************************************/
10517 
10518 CreateConversionStmt:
10519 			CREATE opt_default CONVERSION_P any_name FOR Sconst
10520 			TO Sconst FROM any_name
10521 			{
10522 				CreateConversionStmt *n = makeNode(CreateConversionStmt);
10523 				n->conversion_name = $4;
10524 				n->for_encoding_name = $6;
10525 				n->to_encoding_name = $8;
10526 				n->func_name = $10;
10527 				n->def = $2;
10528 				$$ = (Node *)n;
10529 			}
10530 		;
10531 
10532 /*****************************************************************************
10533  *
10534  *		QUERY:
10535  *				CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
10536  *				CLUSTER [VERBOSE]
10537  *				CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
10538  *
10539  *****************************************************************************/
10540 
10541 ClusterStmt:
10542 			CLUSTER opt_verbose qualified_name cluster_index_specification
10543 				{
10544 					ClusterStmt *n = makeNode(ClusterStmt);
10545 					n->relation = $3;
10546 					n->indexname = $4;
10547 					n->verbose = $2;
10548 					$$ = (Node*)n;
10549 				}
10550 			| CLUSTER opt_verbose
10551 				{
10552 					ClusterStmt *n = makeNode(ClusterStmt);
10553 					n->relation = NULL;
10554 					n->indexname = NULL;
10555 					n->verbose = $2;
10556 					$$ = (Node*)n;
10557 				}
10558 			/* kept for pre-8.3 compatibility */
10559 			| CLUSTER opt_verbose index_name ON qualified_name
10560 				{
10561 					ClusterStmt *n = makeNode(ClusterStmt);
10562 					n->relation = $5;
10563 					n->indexname = $3;
10564 					n->verbose = $2;
10565 					$$ = (Node*)n;
10566 				}
10567 		;
10568 
10569 cluster_index_specification:
10570 			USING index_name		{ $$ = $2; }
10571 			| /*EMPTY*/				{ $$ = NULL; }
10572 		;
10573 
10574 
10575 /*****************************************************************************
10576  *
10577  *		QUERY:
10578  *				VACUUM
10579  *				ANALYZE
10580  *
10581  *****************************************************************************/
10582 
10583 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose opt_analyze opt_vacuum_relation_list
10584 				{
10585 					VacuumStmt *n = makeNode(VacuumStmt);
10586 					n->options = VACOPT_VACUUM;
10587 					if ($2)
10588 						n->options |= VACOPT_FULL;
10589 					if ($3)
10590 						n->options |= VACOPT_FREEZE;
10591 					if ($4)
10592 						n->options |= VACOPT_VERBOSE;
10593 					if ($5)
10594 						n->options |= VACOPT_ANALYZE;
10595 					n->rels = $6;
10596 					$$ = (Node *)n;
10597 				}
10598 			| VACUUM '(' vacuum_option_list ')' opt_vacuum_relation_list
10599 				{
10600 					VacuumStmt *n = makeNode(VacuumStmt);
10601 					n->options = VACOPT_VACUUM | $3;
10602 					n->rels = $5;
10603 					$$ = (Node *) n;
10604 				}
10605 		;
10606 
10607 vacuum_option_list:
10608 			vacuum_option_elem								{ $$ = $1; }
10609 			| vacuum_option_list ',' vacuum_option_elem		{ $$ = $1 | $3; }
10610 		;
10611 
10612 vacuum_option_elem:
10613 			analyze_keyword		{ $$ = VACOPT_ANALYZE; }
10614 			| VERBOSE			{ $$ = VACOPT_VERBOSE; }
10615 			| FREEZE			{ $$ = VACOPT_FREEZE; }
10616 			| FULL				{ $$ = VACOPT_FULL; }
10617 			| IDENT
10618 				{
10619 					if (strcmp($1, "disable_page_skipping") == 0)
10620 						$$ = VACOPT_DISABLE_PAGE_SKIPPING;
10621 					else
10622 						ereport(ERROR,
10623 								(errcode(ERRCODE_SYNTAX_ERROR),
10624 							 errmsg("unrecognized VACUUM option \"%s\"", $1),
10625 									 parser_errposition(@1)));
10626 				}
10627 		;
10628 
10629 AnalyzeStmt: analyze_keyword opt_verbose opt_vacuum_relation_list
10630 				{
10631 					VacuumStmt *n = makeNode(VacuumStmt);
10632 					n->options = VACOPT_ANALYZE;
10633 					if ($2)
10634 						n->options |= VACOPT_VERBOSE;
10635 					n->rels = $3;
10636 					$$ = (Node *)n;
10637 				}
10638 			| analyze_keyword '(' analyze_option_list ')' opt_vacuum_relation_list
10639 				{
10640 					VacuumStmt *n = makeNode(VacuumStmt);
10641 					n->options = VACOPT_ANALYZE | $3;
10642 					n->rels = $5;
10643 					$$ = (Node *) n;
10644 				}
10645 		;
10646 
10647 analyze_option_list:
10648 			analyze_option_elem								{ $$ = $1; }
10649 			| analyze_option_list ',' analyze_option_elem	{ $$ = $1 | $3; }
10650 		;
10651 
10652 analyze_option_elem:
10653 			VERBOSE				{ $$ = VACOPT_VERBOSE; }
10654 		;
10655 
10656 analyze_keyword:
10657 			ANALYZE									{}
10658 			| ANALYSE /* British */					{}
10659 		;
10660 
10661 opt_analyze:
10662 			analyze_keyword							{ $$ = true; }
10663 			| /*EMPTY*/								{ $$ = false; }
10664 		;
10665 
10666 opt_verbose:
10667 			VERBOSE									{ $$ = true; }
10668 			| /*EMPTY*/								{ $$ = false; }
10669 		;
10670 
10671 opt_full:	FULL									{ $$ = true; }
10672 			| /*EMPTY*/								{ $$ = false; }
10673 		;
10674 
10675 opt_freeze: FREEZE									{ $$ = true; }
10676 			| /*EMPTY*/								{ $$ = false; }
10677 		;
10678 
10679 opt_name_list:
10680 			'(' name_list ')'						{ $$ = $2; }
10681 			| /*EMPTY*/								{ $$ = NIL; }
10682 		;
10683 
10684 vacuum_relation:
10685 			qualified_name opt_name_list
10686 				{
10687 					$$ = (Node *) makeVacuumRelation($1, InvalidOid, $2);
10688 				}
10689 		;
10690 
10691 vacuum_relation_list:
10692 			vacuum_relation
10693 					{ $$ = list_make1($1); }
10694 			| vacuum_relation_list ',' vacuum_relation
10695 					{ $$ = lappend($1, $3); }
10696 		;
10697 
10698 opt_vacuum_relation_list:
10699 			vacuum_relation_list					{ $$ = $1; }
10700 			| /*EMPTY*/								{ $$ = NIL; }
10701 		;
10702 
10703 
10704 /*****************************************************************************
10705  *
10706  *		QUERY:
10707  *				EXPLAIN [ANALYZE] [VERBOSE] query
10708  *				EXPLAIN ( options ) query
10709  *
10710  *****************************************************************************/
10711 
10712 ExplainStmt:
10713 		EXPLAIN ExplainableStmt
10714 				{
10715 					ExplainStmt *n = makeNode(ExplainStmt);
10716 					n->query = $2;
10717 					n->options = NIL;
10718 					$$ = (Node *) n;
10719 				}
10720 		| EXPLAIN analyze_keyword opt_verbose ExplainableStmt
10721 				{
10722 					ExplainStmt *n = makeNode(ExplainStmt);
10723 					n->query = $4;
10724 					n->options = list_make1(makeDefElem("analyze", NULL, @2));
10725 					if ($3)
10726 						n->options = lappend(n->options,
10727 											 makeDefElem("verbose", NULL, @3));
10728 					$$ = (Node *) n;
10729 				}
10730 		| EXPLAIN VERBOSE ExplainableStmt
10731 				{
10732 					ExplainStmt *n = makeNode(ExplainStmt);
10733 					n->query = $3;
10734 					n->options = list_make1(makeDefElem("verbose", NULL, @2));
10735 					$$ = (Node *) n;
10736 				}
10737 		| EXPLAIN '(' explain_option_list ')' ExplainableStmt
10738 				{
10739 					ExplainStmt *n = makeNode(ExplainStmt);
10740 					n->query = $5;
10741 					n->options = $3;
10742 					$$ = (Node *) n;
10743 				}
10744 		;
10745 
10746 ExplainableStmt:
10747 			SelectStmt
10748 			| InsertStmt
10749 			| UpdateStmt
10750 			| DeleteStmt
10751 			| DeclareCursorStmt
10752 			| CreateAsStmt
10753 			| CreateMatViewStmt
10754 			| RefreshMatViewStmt
10755 			| ExecuteStmt					/* by default all are $$=$1 */
10756 		;
10757 
10758 explain_option_list:
10759 			explain_option_elem
10760 				{
10761 					$$ = list_make1($1);
10762 				}
10763 			| explain_option_list ',' explain_option_elem
10764 				{
10765 					$$ = lappend($1, $3);
10766 				}
10767 		;
10768 
10769 explain_option_elem:
10770 			explain_option_name explain_option_arg
10771 				{
10772 					$$ = makeDefElem($1, $2, @1);
10773 				}
10774 		;
10775 
10776 explain_option_name:
10777 			NonReservedWord			{ $$ = $1; }
10778 			| analyze_keyword		{ $$ = "analyze"; }
10779 		;
10780 
10781 explain_option_arg:
10782 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
10783 			| NumericOnly			{ $$ = (Node *) $1; }
10784 			| /* EMPTY */			{ $$ = NULL; }
10785 		;
10786 
10787 /*****************************************************************************
10788  *
10789  *		QUERY:
10790  *				PREPARE <plan_name> [(args, ...)] AS <query>
10791  *
10792  *****************************************************************************/
10793 
10794 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
10795 				{
10796 					PrepareStmt *n = makeNode(PrepareStmt);
10797 					n->name = $2;
10798 					n->argtypes = $3;
10799 					n->query = $5;
10800 					$$ = (Node *) n;
10801 				}
10802 		;
10803 
10804 prep_type_clause: '(' type_list ')'			{ $$ = $2; }
10805 				| /* EMPTY */				{ $$ = NIL; }
10806 		;
10807 
10808 PreparableStmt:
10809 			SelectStmt
10810 			| InsertStmt
10811 			| UpdateStmt
10812 			| DeleteStmt					/* by default all are $$=$1 */
10813 		;
10814 
10815 /*****************************************************************************
10816  *
10817  * EXECUTE <plan_name> [(params, ...)]
10818  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
10819  *
10820  *****************************************************************************/
10821 
10822 ExecuteStmt: EXECUTE name execute_param_clause
10823 				{
10824 					ExecuteStmt *n = makeNode(ExecuteStmt);
10825 					n->name = $2;
10826 					n->params = $3;
10827 					$$ = (Node *) n;
10828 				}
10829 			| CREATE OptTemp TABLE create_as_target AS
10830 				EXECUTE name execute_param_clause opt_with_data
10831 				{
10832 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10833 					ExecuteStmt *n = makeNode(ExecuteStmt);
10834 					n->name = $7;
10835 					n->params = $8;
10836 					ctas->query = (Node *) n;
10837 					ctas->into = $4;
10838 					ctas->relkind = OBJECT_TABLE;
10839 					ctas->is_select_into = false;
10840 					/* cram additional flags into the IntoClause */
10841 					$4->rel->relpersistence = $2;
10842 					$4->skipData = !($9);
10843 					$$ = (Node *) ctas;
10844 				}
10845 		;
10846 
10847 execute_param_clause: '(' expr_list ')'				{ $$ = $2; }
10848 					| /* EMPTY */					{ $$ = NIL; }
10849 					;
10850 
10851 /*****************************************************************************
10852  *
10853  *		QUERY:
10854  *				DEALLOCATE [PREPARE] <plan_name>
10855  *
10856  *****************************************************************************/
10857 
10858 DeallocateStmt: DEALLOCATE name
10859 					{
10860 						DeallocateStmt *n = makeNode(DeallocateStmt);
10861 						n->name = $2;
10862 						$$ = (Node *) n;
10863 					}
10864 				| DEALLOCATE PREPARE name
10865 					{
10866 						DeallocateStmt *n = makeNode(DeallocateStmt);
10867 						n->name = $3;
10868 						$$ = (Node *) n;
10869 					}
10870 				| DEALLOCATE ALL
10871 					{
10872 						DeallocateStmt *n = makeNode(DeallocateStmt);
10873 						n->name = NULL;
10874 						$$ = (Node *) n;
10875 					}
10876 				| DEALLOCATE PREPARE ALL
10877 					{
10878 						DeallocateStmt *n = makeNode(DeallocateStmt);
10879 						n->name = NULL;
10880 						$$ = (Node *) n;
10881 					}
10882 		;
10883 
10884 /*****************************************************************************
10885  *
10886  *		QUERY:
10887  *				INSERT STATEMENTS
10888  *
10889  *****************************************************************************/
10890 
10891 InsertStmt:
10892 			opt_with_clause INSERT INTO insert_target insert_rest
10893 			opt_on_conflict returning_clause
10894 				{
10895 					$5->relation = $4;
10896 					$5->onConflictClause = $6;
10897 					$5->returningList = $7;
10898 					$5->withClause = $1;
10899 					$$ = (Node *) $5;
10900 				}
10901 		;
10902 
10903 /*
10904  * Can't easily make AS optional here, because VALUES in insert_rest would
10905  * have a shift/reduce conflict with VALUES as an optional alias.  We could
10906  * easily allow unreserved_keywords as optional aliases, but that'd be an odd
10907  * divergence from other places.  So just require AS for now.
10908  */
10909 insert_target:
10910 			qualified_name
10911 				{
10912 					$$ = $1;
10913 				}
10914 			| qualified_name AS ColId
10915 				{
10916 					$1->alias = makeAlias($3, NIL);
10917 					$$ = $1;
10918 				}
10919 		;
10920 
10921 insert_rest:
10922 			SelectStmt
10923 				{
10924 					$$ = makeNode(InsertStmt);
10925 					$$->cols = NIL;
10926 					$$->selectStmt = $1;
10927 				}
10928 			| OVERRIDING override_kind VALUE_P SelectStmt
10929 				{
10930 					$$ = makeNode(InsertStmt);
10931 					$$->cols = NIL;
10932 					$$->override = $2;
10933 					$$->selectStmt = $4;
10934 				}
10935 			| '(' insert_column_list ')' SelectStmt
10936 				{
10937 					$$ = makeNode(InsertStmt);
10938 					$$->cols = $2;
10939 					$$->selectStmt = $4;
10940 				}
10941 			| '(' insert_column_list ')' OVERRIDING override_kind VALUE_P SelectStmt
10942 				{
10943 					$$ = makeNode(InsertStmt);
10944 					$$->cols = $2;
10945 					$$->override = $5;
10946 					$$->selectStmt = $7;
10947 				}
10948 			| DEFAULT VALUES
10949 				{
10950 					$$ = makeNode(InsertStmt);
10951 					$$->cols = NIL;
10952 					$$->selectStmt = NULL;
10953 				}
10954 		;
10955 
10956 override_kind:
10957 			USER		{ $$ = OVERRIDING_USER_VALUE; }
10958 			| SYSTEM_P	{ $$ = OVERRIDING_SYSTEM_VALUE; }
10959 		;
10960 
10961 insert_column_list:
10962 			insert_column_item
10963 					{ $$ = list_make1($1); }
10964 			| insert_column_list ',' insert_column_item
10965 					{ $$ = lappend($1, $3); }
10966 		;
10967 
10968 insert_column_item:
10969 			ColId opt_indirection
10970 				{
10971 					$$ = makeNode(ResTarget);
10972 					$$->name = $1;
10973 					$$->indirection = check_indirection($2, yyscanner);
10974 					$$->val = NULL;
10975 					$$->location = @1;
10976 				}
10977 		;
10978 
10979 opt_on_conflict:
10980 			ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list	where_clause
10981 				{
10982 					$$ = makeNode(OnConflictClause);
10983 					$$->action = ONCONFLICT_UPDATE;
10984 					$$->infer = $3;
10985 					$$->targetList = $7;
10986 					$$->whereClause = $8;
10987 					$$->location = @1;
10988 				}
10989 			|
10990 			ON CONFLICT opt_conf_expr DO NOTHING
10991 				{
10992 					$$ = makeNode(OnConflictClause);
10993 					$$->action = ONCONFLICT_NOTHING;
10994 					$$->infer = $3;
10995 					$$->targetList = NIL;
10996 					$$->whereClause = NULL;
10997 					$$->location = @1;
10998 				}
10999 			| /*EMPTY*/
11000 				{
11001 					$$ = NULL;
11002 				}
11003 		;
11004 
11005 opt_conf_expr:
11006 			'(' index_params ')' where_clause
11007 				{
11008 					$$ = makeNode(InferClause);
11009 					$$->indexElems = $2;
11010 					$$->whereClause = $4;
11011 					$$->conname = NULL;
11012 					$$->location = @1;
11013 				}
11014 			|
11015 			ON CONSTRAINT name
11016 				{
11017 					$$ = makeNode(InferClause);
11018 					$$->indexElems = NIL;
11019 					$$->whereClause = NULL;
11020 					$$->conname = $3;
11021 					$$->location = @1;
11022 				}
11023 			| /*EMPTY*/
11024 				{
11025 					$$ = NULL;
11026 				}
11027 		;
11028 
11029 returning_clause:
11030 			RETURNING target_list		{ $$ = $2; }
11031 			| /* EMPTY */				{ $$ = NIL; }
11032 		;
11033 
11034 
11035 /*****************************************************************************
11036  *
11037  *		QUERY:
11038  *				DELETE STATEMENTS
11039  *
11040  *****************************************************************************/
11041 
11042 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
11043 			using_clause where_or_current_clause returning_clause
11044 				{
11045 					DeleteStmt *n = makeNode(DeleteStmt);
11046 					n->relation = $4;
11047 					n->usingClause = $5;
11048 					n->whereClause = $6;
11049 					n->returningList = $7;
11050 					n->withClause = $1;
11051 					$$ = (Node *)n;
11052 				}
11053 		;
11054 
11055 using_clause:
11056 				USING from_list						{ $$ = $2; }
11057 			| /*EMPTY*/								{ $$ = NIL; }
11058 		;
11059 
11060 
11061 /*****************************************************************************
11062  *
11063  *		QUERY:
11064  *				LOCK TABLE
11065  *
11066  *****************************************************************************/
11067 
11068 LockStmt:	LOCK_P opt_table relation_expr_list opt_lock opt_nowait
11069 				{
11070 					LockStmt *n = makeNode(LockStmt);
11071 
11072 					n->relations = $3;
11073 					n->mode = $4;
11074 					n->nowait = $5;
11075 					$$ = (Node *)n;
11076 				}
11077 		;
11078 
11079 opt_lock:	IN_P lock_type MODE				{ $$ = $2; }
11080 			| /*EMPTY*/						{ $$ = AccessExclusiveLock; }
11081 		;
11082 
11083 lock_type:	ACCESS SHARE					{ $$ = AccessShareLock; }
11084 			| ROW SHARE						{ $$ = RowShareLock; }
11085 			| ROW EXCLUSIVE					{ $$ = RowExclusiveLock; }
11086 			| SHARE UPDATE EXCLUSIVE		{ $$ = ShareUpdateExclusiveLock; }
11087 			| SHARE							{ $$ = ShareLock; }
11088 			| SHARE ROW EXCLUSIVE			{ $$ = ShareRowExclusiveLock; }
11089 			| EXCLUSIVE						{ $$ = ExclusiveLock; }
11090 			| ACCESS EXCLUSIVE				{ $$ = AccessExclusiveLock; }
11091 		;
11092 
11093 opt_nowait:	NOWAIT							{ $$ = true; }
11094 			| /*EMPTY*/						{ $$ = false; }
11095 		;
11096 
11097 opt_nowait_or_skip:
11098 			NOWAIT							{ $$ = LockWaitError; }
11099 			| SKIP LOCKED					{ $$ = LockWaitSkip; }
11100 			| /*EMPTY*/						{ $$ = LockWaitBlock; }
11101 		;
11102 
11103 
11104 /*****************************************************************************
11105  *
11106  *		QUERY:
11107  *				UpdateStmt (UPDATE)
11108  *
11109  *****************************************************************************/
11110 
11111 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
11112 			SET set_clause_list
11113 			from_clause
11114 			where_or_current_clause
11115 			returning_clause
11116 				{
11117 					UpdateStmt *n = makeNode(UpdateStmt);
11118 					n->relation = $3;
11119 					n->targetList = $5;
11120 					n->fromClause = $6;
11121 					n->whereClause = $7;
11122 					n->returningList = $8;
11123 					n->withClause = $1;
11124 					$$ = (Node *)n;
11125 				}
11126 		;
11127 
11128 set_clause_list:
11129 			set_clause							{ $$ = $1; }
11130 			| set_clause_list ',' set_clause	{ $$ = list_concat($1,$3); }
11131 		;
11132 
11133 set_clause:
11134 			set_target '=' a_expr
11135 				{
11136 					$1->val = (Node *) $3;
11137 					$$ = list_make1($1);
11138 				}
11139 			| '(' set_target_list ')' '=' a_expr
11140 				{
11141 					int ncolumns = list_length($2);
11142 					int i = 1;
11143 					ListCell *col_cell;
11144 
11145 					/* Create a MultiAssignRef source for each target */
foreach(col_cell,$2)11146 					foreach(col_cell, $2)
11147 					{
11148 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
11149 						MultiAssignRef *r = makeNode(MultiAssignRef);
11150 
11151 						r->source = (Node *) $5;
11152 						r->colno = i;
11153 						r->ncolumns = ncolumns;
11154 						res_col->val = (Node *) r;
11155 						i++;
11156 					}
11157 
11158 					$$ = $2;
11159 				}
11160 		;
11161 
11162 set_target:
11163 			ColId opt_indirection
11164 				{
11165 					$$ = makeNode(ResTarget);
11166 					$$->name = $1;
11167 					$$->indirection = check_indirection($2, yyscanner);
11168 					$$->val = NULL;	/* upper production sets this */
11169 					$$->location = @1;
11170 				}
11171 		;
11172 
11173 set_target_list:
11174 			set_target								{ $$ = list_make1($1); }
11175 			| set_target_list ',' set_target		{ $$ = lappend($1,$3); }
11176 		;
11177 
11178 
11179 /*****************************************************************************
11180  *
11181  *		QUERY:
11182  *				CURSOR STATEMENTS
11183  *
11184  *****************************************************************************/
11185 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
11186 				{
11187 					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
11188 					n->portalname = $2;
11189 					/* currently we always set FAST_PLAN option */
11190 					n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
11191 					n->query = $7;
11192 					$$ = (Node *)n;
11193 				}
11194 		;
11195 
11196 cursor_name:	name						{ $$ = $1; }
11197 		;
11198 
11199 cursor_options: /*EMPTY*/					{ $$ = 0; }
11200 			| cursor_options NO SCROLL		{ $$ = $1 | CURSOR_OPT_NO_SCROLL; }
11201 			| cursor_options SCROLL			{ $$ = $1 | CURSOR_OPT_SCROLL; }
11202 			| cursor_options BINARY			{ $$ = $1 | CURSOR_OPT_BINARY; }
11203 			| cursor_options INSENSITIVE	{ $$ = $1 | CURSOR_OPT_INSENSITIVE; }
11204 		;
11205 
11206 opt_hold: /* EMPTY */						{ $$ = 0; }
11207 			| WITH HOLD						{ $$ = CURSOR_OPT_HOLD; }
11208 			| WITHOUT HOLD					{ $$ = 0; }
11209 		;
11210 
11211 /*****************************************************************************
11212  *
11213  *		QUERY:
11214  *				SELECT STATEMENTS
11215  *
11216  *****************************************************************************/
11217 
11218 /* A complete SELECT statement looks like this.
11219  *
11220  * The rule returns either a single SelectStmt node or a tree of them,
11221  * representing a set-operation tree.
11222  *
11223  * There is an ambiguity when a sub-SELECT is within an a_expr and there
11224  * are excess parentheses: do the parentheses belong to the sub-SELECT or
11225  * to the surrounding a_expr?  We don't really care, but bison wants to know.
11226  * To resolve the ambiguity, we are careful to define the grammar so that
11227  * the decision is staved off as long as possible: as long as we can keep
11228  * absorbing parentheses into the sub-SELECT, we will do so, and only when
11229  * it's no longer possible to do that will we decide that parens belong to
11230  * the expression.	For example, in "SELECT (((SELECT 2)) + 3)" the extra
11231  * parentheses are treated as part of the sub-select.  The necessity of doing
11232  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".	Had we
11233  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
11234  * SELECT viewpoint when we see the UNION.
11235  *
11236  * This approach is implemented by defining a nonterminal select_with_parens,
11237  * which represents a SELECT with at least one outer layer of parentheses,
11238  * and being careful to use select_with_parens, never '(' SelectStmt ')',
11239  * in the expression grammar.  We will then have shift-reduce conflicts
11240  * which we can resolve in favor of always treating '(' <select> ')' as
11241  * a select_with_parens.  To resolve the conflicts, the productions that
11242  * conflict with the select_with_parens productions are manually given
11243  * precedences lower than the precedence of ')', thereby ensuring that we
11244  * shift ')' (and then reduce to select_with_parens) rather than trying to
11245  * reduce the inner <select> nonterminal to something else.  We use UMINUS
11246  * precedence for this, which is a fairly arbitrary choice.
11247  *
11248  * To be able to define select_with_parens itself without ambiguity, we need
11249  * a nonterminal select_no_parens that represents a SELECT structure with no
11250  * outermost parentheses.  This is a little bit tedious, but it works.
11251  *
11252  * In non-expression contexts, we use SelectStmt which can represent a SELECT
11253  * with or without outer parentheses.
11254  */
11255 
11256 SelectStmt: select_no_parens			%prec UMINUS
11257 			| select_with_parens		%prec UMINUS
11258 		;
11259 
11260 select_with_parens:
11261 			'(' select_no_parens ')'				{ $$ = $2; }
11262 			| '(' select_with_parens ')'			{ $$ = $2; }
11263 		;
11264 
11265 /*
11266  * This rule parses the equivalent of the standard's <query expression>.
11267  * The duplicative productions are annoying, but hard to get rid of without
11268  * creating shift/reduce conflicts.
11269  *
11270  *	The locking clause (FOR UPDATE etc) may be before or after LIMIT/OFFSET.
11271  *	In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
11272  *	We now support both orderings, but prefer LIMIT/OFFSET before the locking
11273  * clause.
11274  *	2002-08-28 bjm
11275  */
11276 select_no_parens:
11277 			simple_select						{ $$ = $1; }
11278 			| select_clause sort_clause
11279 				{
11280 					insertSelectOptions((SelectStmt *) $1, $2, NIL,
11281 										NULL, NULL, NULL,
11282 										yyscanner);
11283 					$$ = $1;
11284 				}
11285 			| select_clause opt_sort_clause for_locking_clause opt_select_limit
11286 				{
11287 					insertSelectOptions((SelectStmt *) $1, $2, $3,
11288 										list_nth($4, 0), list_nth($4, 1),
11289 										NULL,
11290 										yyscanner);
11291 					$$ = $1;
11292 				}
11293 			| select_clause opt_sort_clause select_limit opt_for_locking_clause
11294 				{
11295 					insertSelectOptions((SelectStmt *) $1, $2, $4,
11296 										list_nth($3, 0), list_nth($3, 1),
11297 										NULL,
11298 										yyscanner);
11299 					$$ = $1;
11300 				}
11301 			| with_clause select_clause
11302 				{
11303 					insertSelectOptions((SelectStmt *) $2, NULL, NIL,
11304 										NULL, NULL,
11305 										$1,
11306 										yyscanner);
11307 					$$ = $2;
11308 				}
11309 			| with_clause select_clause sort_clause
11310 				{
11311 					insertSelectOptions((SelectStmt *) $2, $3, NIL,
11312 										NULL, NULL,
11313 										$1,
11314 										yyscanner);
11315 					$$ = $2;
11316 				}
11317 			| with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
11318 				{
11319 					insertSelectOptions((SelectStmt *) $2, $3, $4,
11320 										list_nth($5, 0), list_nth($5, 1),
11321 										$1,
11322 										yyscanner);
11323 					$$ = $2;
11324 				}
11325 			| with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
11326 				{
11327 					insertSelectOptions((SelectStmt *) $2, $3, $5,
11328 										list_nth($4, 0), list_nth($4, 1),
11329 										$1,
11330 										yyscanner);
11331 					$$ = $2;
11332 				}
11333 		;
11334 
11335 select_clause:
11336 			simple_select							{ $$ = $1; }
11337 			| select_with_parens					{ $$ = $1; }
11338 		;
11339 
11340 /*
11341  * This rule parses SELECT statements that can appear within set operations,
11342  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
11343  * the ordering of the set operations.	Without '(' and ')' we want the
11344  * operations to be ordered per the precedence specs at the head of this file.
11345  *
11346  * As with select_no_parens, simple_select cannot have outer parentheses,
11347  * but can have parenthesized subclauses.
11348  *
11349  * Note that sort clauses cannot be included at this level --- SQL requires
11350  *		SELECT foo UNION SELECT bar ORDER BY baz
11351  * to be parsed as
11352  *		(SELECT foo UNION SELECT bar) ORDER BY baz
11353  * not
11354  *		SELECT foo UNION (SELECT bar ORDER BY baz)
11355  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
11356  * described as part of the select_no_parens production, not simple_select.
11357  * This does not limit functionality, because you can reintroduce these
11358  * clauses inside parentheses.
11359  *
11360  * NOTE: only the leftmost component SelectStmt should have INTO.
11361  * However, this is not checked by the grammar; parse analysis must check it.
11362  */
11363 simple_select:
11364 			SELECT opt_all_clause opt_target_list
11365 			into_clause from_clause where_clause
11366 			group_clause having_clause window_clause
11367 				{
11368 					SelectStmt *n = makeNode(SelectStmt);
11369 					n->targetList = $3;
11370 					n->intoClause = $4;
11371 					n->fromClause = $5;
11372 					n->whereClause = $6;
11373 					n->groupClause = $7;
11374 					n->havingClause = $8;
11375 					n->windowClause = $9;
11376 					$$ = (Node *)n;
11377 				}
11378 			| SELECT distinct_clause target_list
11379 			into_clause from_clause where_clause
11380 			group_clause having_clause window_clause
11381 				{
11382 					SelectStmt *n = makeNode(SelectStmt);
11383 					n->distinctClause = $2;
11384 					n->targetList = $3;
11385 					n->intoClause = $4;
11386 					n->fromClause = $5;
11387 					n->whereClause = $6;
11388 					n->groupClause = $7;
11389 					n->havingClause = $8;
11390 					n->windowClause = $9;
11391 					$$ = (Node *)n;
11392 				}
11393 			| values_clause							{ $$ = $1; }
11394 			| TABLE relation_expr
11395 				{
11396 					/* same as SELECT * FROM relation_expr */
11397 					ColumnRef *cr = makeNode(ColumnRef);
11398 					ResTarget *rt = makeNode(ResTarget);
11399 					SelectStmt *n = makeNode(SelectStmt);
11400 
11401 					cr->fields = list_make1(makeNode(A_Star));
11402 					cr->location = -1;
11403 
11404 					rt->name = NULL;
11405 					rt->indirection = NIL;
11406 					rt->val = (Node *)cr;
11407 					rt->location = -1;
11408 
11409 					n->targetList = list_make1(rt);
11410 					n->fromClause = list_make1($2);
11411 					$$ = (Node *)n;
11412 				}
11413 			| select_clause UNION all_or_distinct select_clause
11414 				{
11415 					$$ = makeSetOp(SETOP_UNION, $3, $1, $4);
11416 				}
11417 			| select_clause INTERSECT all_or_distinct select_clause
11418 				{
11419 					$$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
11420 				}
11421 			| select_clause EXCEPT all_or_distinct select_clause
11422 				{
11423 					$$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
11424 				}
11425 		;
11426 
11427 /*
11428  * SQL standard WITH clause looks like:
11429  *
11430  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
11431  *		AS (query) [ SEARCH or CYCLE clause ]
11432  *
11433  * We don't currently support the SEARCH or CYCLE clause.
11434  *
11435  * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY.
11436  */
11437 with_clause:
11438 		WITH cte_list
11439 			{
11440 				$$ = makeNode(WithClause);
11441 				$$->ctes = $2;
11442 				$$->recursive = false;
11443 				$$->location = @1;
11444 			}
11445 		| WITH_LA cte_list
11446 			{
11447 				$$ = makeNode(WithClause);
11448 				$$->ctes = $2;
11449 				$$->recursive = false;
11450 				$$->location = @1;
11451 			}
11452 		| WITH RECURSIVE cte_list
11453 			{
11454 				$$ = makeNode(WithClause);
11455 				$$->ctes = $3;
11456 				$$->recursive = true;
11457 				$$->location = @1;
11458 			}
11459 		;
11460 
11461 cte_list:
11462 		common_table_expr						{ $$ = list_make1($1); }
11463 		| cte_list ',' common_table_expr		{ $$ = lappend($1, $3); }
11464 		;
11465 
11466 common_table_expr:  name opt_name_list AS '(' PreparableStmt ')'
11467 			{
11468 				CommonTableExpr *n = makeNode(CommonTableExpr);
11469 				n->ctename = $1;
11470 				n->aliascolnames = $2;
11471 				n->ctequery = $5;
11472 				n->location = @1;
11473 				$$ = (Node *) n;
11474 			}
11475 		;
11476 
11477 opt_with_clause:
11478 		with_clause								{ $$ = $1; }
11479 		| /*EMPTY*/								{ $$ = NULL; }
11480 		;
11481 
11482 into_clause:
11483 			INTO OptTempTableName
11484 				{
11485 					$$ = makeNode(IntoClause);
11486 					$$->rel = $2;
11487 					$$->colNames = NIL;
11488 					$$->options = NIL;
11489 					$$->onCommit = ONCOMMIT_NOOP;
11490 					$$->tableSpaceName = NULL;
11491 					$$->viewQuery = NULL;
11492 					$$->skipData = false;
11493 				}
11494 			| /*EMPTY*/
11495 				{ $$ = NULL; }
11496 		;
11497 
11498 /*
11499  * Redundancy here is needed to avoid shift/reduce conflicts,
11500  * since TEMP is not a reserved word.  See also OptTemp.
11501  */
11502 OptTempTableName:
11503 			TEMPORARY opt_table qualified_name
11504 				{
11505 					$$ = $3;
11506 					$$->relpersistence = RELPERSISTENCE_TEMP;
11507 				}
11508 			| TEMP opt_table qualified_name
11509 				{
11510 					$$ = $3;
11511 					$$->relpersistence = RELPERSISTENCE_TEMP;
11512 				}
11513 			| LOCAL TEMPORARY opt_table qualified_name
11514 				{
11515 					$$ = $4;
11516 					$$->relpersistence = RELPERSISTENCE_TEMP;
11517 				}
11518 			| LOCAL TEMP opt_table qualified_name
11519 				{
11520 					$$ = $4;
11521 					$$->relpersistence = RELPERSISTENCE_TEMP;
11522 				}
11523 			| GLOBAL TEMPORARY opt_table qualified_name
11524 				{
11525 					ereport(WARNING,
11526 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11527 							 parser_errposition(@1)));
11528 					$$ = $4;
11529 					$$->relpersistence = RELPERSISTENCE_TEMP;
11530 				}
11531 			| GLOBAL TEMP opt_table qualified_name
11532 				{
11533 					ereport(WARNING,
11534 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11535 							 parser_errposition(@1)));
11536 					$$ = $4;
11537 					$$->relpersistence = RELPERSISTENCE_TEMP;
11538 				}
11539 			| UNLOGGED opt_table qualified_name
11540 				{
11541 					$$ = $3;
11542 					$$->relpersistence = RELPERSISTENCE_UNLOGGED;
11543 				}
11544 			| TABLE qualified_name
11545 				{
11546 					$$ = $2;
11547 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11548 				}
11549 			| qualified_name
11550 				{
11551 					$$ = $1;
11552 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11553 				}
11554 		;
11555 
11556 opt_table:	TABLE									{}
11557 			| /*EMPTY*/								{}
11558 		;
11559 
11560 all_or_distinct:
11561 			ALL										{ $$ = true; }
11562 			| DISTINCT								{ $$ = false; }
11563 			| /*EMPTY*/								{ $$ = false; }
11564 		;
11565 
11566 /* We use (NIL) as a placeholder to indicate that all target expressions
11567  * should be placed in the DISTINCT list during parsetree analysis.
11568  */
11569 distinct_clause:
11570 			DISTINCT								{ $$ = list_make1(NIL); }
11571 			| DISTINCT ON '(' expr_list ')'			{ $$ = $4; }
11572 		;
11573 
11574 opt_all_clause:
11575 			ALL										{ $$ = NIL;}
11576 			| /*EMPTY*/								{ $$ = NIL; }
11577 		;
11578 
11579 opt_sort_clause:
11580 			sort_clause								{ $$ = $1;}
11581 			| /*EMPTY*/								{ $$ = NIL; }
11582 		;
11583 
11584 sort_clause:
11585 			ORDER BY sortby_list					{ $$ = $3; }
11586 		;
11587 
11588 sortby_list:
11589 			sortby									{ $$ = list_make1($1); }
11590 			| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
11591 		;
11592 
11593 sortby:		a_expr USING qual_all_Op opt_nulls_order
11594 				{
11595 					$$ = makeNode(SortBy);
11596 					$$->node = $1;
11597 					$$->sortby_dir = SORTBY_USING;
11598 					$$->sortby_nulls = $4;
11599 					$$->useOp = $3;
11600 					$$->location = @3;
11601 				}
11602 			| a_expr opt_asc_desc opt_nulls_order
11603 				{
11604 					$$ = makeNode(SortBy);
11605 					$$->node = $1;
11606 					$$->sortby_dir = $2;
11607 					$$->sortby_nulls = $3;
11608 					$$->useOp = NIL;
11609 					$$->location = -1;		/* no operator */
11610 				}
11611 		;
11612 
11613 
11614 select_limit:
11615 			limit_clause offset_clause			{ $$ = list_make2($2, $1); }
11616 			| offset_clause limit_clause		{ $$ = list_make2($1, $2); }
11617 			| limit_clause						{ $$ = list_make2(NULL, $1); }
11618 			| offset_clause						{ $$ = list_make2($1, NULL); }
11619 		;
11620 
11621 opt_select_limit:
11622 			select_limit						{ $$ = $1; }
11623 			| /* EMPTY */						{ $$ = list_make2(NULL,NULL); }
11624 		;
11625 
11626 limit_clause:
11627 			LIMIT select_limit_value
11628 				{ $$ = $2; }
11629 			| LIMIT select_limit_value ',' select_offset_value
11630 				{
11631 					/* Disabled because it was too confusing, bjm 2002-02-18 */
11632 					ereport(ERROR,
11633 							(errcode(ERRCODE_SYNTAX_ERROR),
11634 							 errmsg("LIMIT #,# syntax is not supported"),
11635 							 errhint("Use separate LIMIT and OFFSET clauses."),
11636 							 parser_errposition(@1)));
11637 				}
11638 			/* SQL:2008 syntax */
11639 			/* to avoid shift/reduce conflicts, handle the optional value with
11640 			 * a separate production rather than an opt_ expression.  The fact
11641 			 * that ONLY is fully reserved means that this way, we defer any
11642 			 * decision about what rule reduces ROW or ROWS to the point where
11643 			 * we can see the ONLY token in the lookahead slot.
11644 			 */
11645 			| FETCH first_or_next select_fetch_first_value row_or_rows ONLY
11646 				{ $$ = $3; }
11647 			| FETCH first_or_next row_or_rows ONLY
11648 				{ $$ = makeIntConst(1, -1); }
11649 		;
11650 
11651 offset_clause:
11652 			OFFSET select_offset_value
11653 				{ $$ = $2; }
11654 			/* SQL:2008 syntax */
11655 			| OFFSET select_fetch_first_value row_or_rows
11656 				{ $$ = $2; }
11657 		;
11658 
11659 select_limit_value:
11660 			a_expr									{ $$ = $1; }
11661 			| ALL
11662 				{
11663 					/* LIMIT ALL is represented as a NULL constant */
11664 					$$ = makeNullAConst(@1);
11665 				}
11666 		;
11667 
11668 select_offset_value:
11669 			a_expr									{ $$ = $1; }
11670 		;
11671 
11672 /*
11673  * Allowing full expressions without parentheses causes various parsing
11674  * problems with the trailing ROW/ROWS key words.  SQL spec only calls for
11675  * <simple value specification>, which is either a literal or a parameter (but
11676  * an <SQL parameter reference> could be an identifier, bringing up conflicts
11677  * with ROW/ROWS). We solve this by leveraging the presence of ONLY (see above)
11678  * to determine whether the expression is missing rather than trying to make it
11679  * optional in this rule.
11680  *
11681  * c_expr covers almost all the spec-required cases (and more), but it doesn't
11682  * cover signed numeric literals, which are allowed by the spec. So we include
11683  * those here explicitly. We need FCONST as well as ICONST because values that
11684  * don't fit in the platform's "long", but do fit in bigint, should still be
11685  * accepted here. (This is possible in 64-bit Windows as well as all 32-bit
11686  * builds.)
11687  */
11688 select_fetch_first_value:
11689 			c_expr									{ $$ = $1; }
11690 			| '+' I_or_F_const
11691 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
11692 			| '-' I_or_F_const
11693 				{ $$ = doNegate($2, @1); }
11694 		;
11695 
11696 I_or_F_const:
11697 			Iconst									{ $$ = makeIntConst($1,@1); }
11698 			| FCONST								{ $$ = makeFloatConst($1,@1); }
11699 		;
11700 
11701 /* noise words */
11702 row_or_rows: ROW									{ $$ = 0; }
11703 			| ROWS									{ $$ = 0; }
11704 		;
11705 
11706 first_or_next: FIRST_P								{ $$ = 0; }
11707 			| NEXT									{ $$ = 0; }
11708 		;
11709 
11710 
11711 /*
11712  * This syntax for group_clause tries to follow the spec quite closely.
11713  * However, the spec allows only column references, not expressions,
11714  * which introduces an ambiguity between implicit row constructors
11715  * (a,b) and lists of column references.
11716  *
11717  * We handle this by using the a_expr production for what the spec calls
11718  * <ordinary grouping set>, which in the spec represents either one column
11719  * reference or a parenthesized list of column references. Then, we check the
11720  * top node of the a_expr to see if it's an implicit RowExpr, and if so, just
11721  * grab and use the list, discarding the node. (this is done in parse analysis,
11722  * not here)
11723  *
11724  * (we abuse the row_format field of RowExpr to distinguish implicit and
11725  * explicit row constructors; it's debatable if anyone sanely wants to use them
11726  * in a group clause, but if they have a reason to, we make it possible.)
11727  *
11728  * Each item in the group_clause list is either an expression tree or a
11729  * GroupingSet node of some type.
11730  */
11731 group_clause:
11732 			GROUP_P BY group_by_list				{ $$ = $3; }
11733 			| /*EMPTY*/								{ $$ = NIL; }
11734 		;
11735 
11736 group_by_list:
11737 			group_by_item							{ $$ = list_make1($1); }
11738 			| group_by_list ',' group_by_item		{ $$ = lappend($1,$3); }
11739 		;
11740 
11741 group_by_item:
11742 			a_expr									{ $$ = $1; }
11743 			| empty_grouping_set					{ $$ = $1; }
11744 			| cube_clause							{ $$ = $1; }
11745 			| rollup_clause							{ $$ = $1; }
11746 			| grouping_sets_clause					{ $$ = $1; }
11747 		;
11748 
11749 empty_grouping_set:
11750 			'(' ')'
11751 				{
11752 					$$ = (Node *) makeGroupingSet(GROUPING_SET_EMPTY, NIL, @1);
11753 				}
11754 		;
11755 
11756 /*
11757  * These hacks rely on setting precedence of CUBE and ROLLUP below that of '(',
11758  * so that they shift in these rules rather than reducing the conflicting
11759  * unreserved_keyword rule.
11760  */
11761 
11762 rollup_clause:
11763 			ROLLUP '(' expr_list ')'
11764 				{
11765 					$$ = (Node *) makeGroupingSet(GROUPING_SET_ROLLUP, $3, @1);
11766 				}
11767 		;
11768 
11769 cube_clause:
11770 			CUBE '(' expr_list ')'
11771 				{
11772 					$$ = (Node *) makeGroupingSet(GROUPING_SET_CUBE, $3, @1);
11773 				}
11774 		;
11775 
11776 grouping_sets_clause:
11777 			GROUPING SETS '(' group_by_list ')'
11778 				{
11779 					$$ = (Node *) makeGroupingSet(GROUPING_SET_SETS, $4, @1);
11780 				}
11781 		;
11782 
11783 having_clause:
11784 			HAVING a_expr							{ $$ = $2; }
11785 			| /*EMPTY*/								{ $$ = NULL; }
11786 		;
11787 
11788 for_locking_clause:
11789 			for_locking_items						{ $$ = $1; }
11790 			| FOR READ ONLY							{ $$ = NIL; }
11791 		;
11792 
11793 opt_for_locking_clause:
11794 			for_locking_clause						{ $$ = $1; }
11795 			| /* EMPTY */							{ $$ = NIL; }
11796 		;
11797 
11798 for_locking_items:
11799 			for_locking_item						{ $$ = list_make1($1); }
11800 			| for_locking_items for_locking_item	{ $$ = lappend($1, $2); }
11801 		;
11802 
11803 for_locking_item:
11804 			for_locking_strength locked_rels_list opt_nowait_or_skip
11805 				{
11806 					LockingClause *n = makeNode(LockingClause);
11807 					n->lockedRels = $2;
11808 					n->strength = $1;
11809 					n->waitPolicy = $3;
11810 					$$ = (Node *) n;
11811 				}
11812 		;
11813 
11814 for_locking_strength:
11815 			FOR UPDATE 							{ $$ = LCS_FORUPDATE; }
11816 			| FOR NO KEY UPDATE 				{ $$ = LCS_FORNOKEYUPDATE; }
11817 			| FOR SHARE 						{ $$ = LCS_FORSHARE; }
11818 			| FOR KEY SHARE 					{ $$ = LCS_FORKEYSHARE; }
11819 		;
11820 
11821 locked_rels_list:
11822 			OF qualified_name_list					{ $$ = $2; }
11823 			| /* EMPTY */							{ $$ = NIL; }
11824 		;
11825 
11826 
11827 /*
11828  * We should allow ROW '(' expr_list ')' too, but that seems to require
11829  * making VALUES a fully reserved word, which will probably break more apps
11830  * than allowing the noise-word is worth.
11831  */
11832 values_clause:
11833 			VALUES '(' expr_list ')'
11834 				{
11835 					SelectStmt *n = makeNode(SelectStmt);
11836 					n->valuesLists = list_make1($3);
11837 					$$ = (Node *) n;
11838 				}
11839 			| values_clause ',' '(' expr_list ')'
11840 				{
11841 					SelectStmt *n = (SelectStmt *) $1;
11842 					n->valuesLists = lappend(n->valuesLists, $4);
11843 					$$ = (Node *) n;
11844 				}
11845 		;
11846 
11847 
11848 /*****************************************************************************
11849  *
11850  *	clauses common to all Optimizable Stmts:
11851  *		from_clause		- allow list of both JOIN expressions and table names
11852  *		where_clause	- qualifications for joins or restrictions
11853  *
11854  *****************************************************************************/
11855 
11856 from_clause:
11857 			FROM from_list							{ $$ = $2; }
11858 			| /*EMPTY*/								{ $$ = NIL; }
11859 		;
11860 
11861 from_list:
11862 			table_ref								{ $$ = list_make1($1); }
11863 			| from_list ',' table_ref				{ $$ = lappend($1, $3); }
11864 		;
11865 
11866 /*
11867  * table_ref is where an alias clause can be attached.
11868  */
11869 table_ref:	relation_expr opt_alias_clause
11870 				{
11871 					$1->alias = $2;
11872 					$$ = (Node *) $1;
11873 				}
11874 			| relation_expr opt_alias_clause tablesample_clause
11875 				{
11876 					RangeTableSample *n = (RangeTableSample *) $3;
11877 					$1->alias = $2;
11878 					/* relation_expr goes inside the RangeTableSample node */
11879 					n->relation = (Node *) $1;
11880 					$$ = (Node *) n;
11881 				}
11882 			| func_table func_alias_clause
11883 				{
11884 					RangeFunction *n = (RangeFunction *) $1;
11885 					n->alias = linitial($2);
11886 					n->coldeflist = lsecond($2);
11887 					$$ = (Node *) n;
11888 				}
11889 			| LATERAL_P func_table func_alias_clause
11890 				{
11891 					RangeFunction *n = (RangeFunction *) $2;
11892 					n->lateral = true;
11893 					n->alias = linitial($3);
11894 					n->coldeflist = lsecond($3);
11895 					$$ = (Node *) n;
11896 				}
11897 			| xmltable opt_alias_clause
11898 				{
11899 					RangeTableFunc *n = (RangeTableFunc *) $1;
11900 					n->alias = $2;
11901 					$$ = (Node *) n;
11902 				}
11903 			| LATERAL_P xmltable opt_alias_clause
11904 				{
11905 					RangeTableFunc *n = (RangeTableFunc *) $2;
11906 					n->lateral = true;
11907 					n->alias = $3;
11908 					$$ = (Node *) n;
11909 				}
11910 			| select_with_parens opt_alias_clause
11911 				{
11912 					RangeSubselect *n = makeNode(RangeSubselect);
11913 					n->lateral = false;
11914 					n->subquery = $1;
11915 					n->alias = $2;
11916 					/*
11917 					 * The SQL spec does not permit a subselect
11918 					 * (<derived_table>) without an alias clause,
11919 					 * so we don't either.  This avoids the problem
11920 					 * of needing to invent a unique refname for it.
11921 					 * That could be surmounted if there's sufficient
11922 					 * popular demand, but for now let's just implement
11923 					 * the spec and see if anyone complains.
11924 					 * However, it does seem like a good idea to emit
11925 					 * an error message that's better than "syntax error".
11926 					 */
11927 					if ($2 == NULL)
11928 					{
11929 						if (IsA($1, SelectStmt) &&
11930 							((SelectStmt *) $1)->valuesLists)
11931 							ereport(ERROR,
11932 									(errcode(ERRCODE_SYNTAX_ERROR),
11933 									 errmsg("VALUES in FROM must have an alias"),
11934 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
11935 									 parser_errposition(@1)));
11936 						else
11937 							ereport(ERROR,
11938 									(errcode(ERRCODE_SYNTAX_ERROR),
11939 									 errmsg("subquery in FROM must have an alias"),
11940 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
11941 									 parser_errposition(@1)));
11942 					}
11943 					$$ = (Node *) n;
11944 				}
11945 			| LATERAL_P select_with_parens opt_alias_clause
11946 				{
11947 					RangeSubselect *n = makeNode(RangeSubselect);
11948 					n->lateral = true;
11949 					n->subquery = $2;
11950 					n->alias = $3;
11951 					/* same comment as above */
11952 					if ($3 == NULL)
11953 					{
11954 						if (IsA($2, SelectStmt) &&
11955 							((SelectStmt *) $2)->valuesLists)
11956 							ereport(ERROR,
11957 									(errcode(ERRCODE_SYNTAX_ERROR),
11958 									 errmsg("VALUES in FROM must have an alias"),
11959 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
11960 									 parser_errposition(@2)));
11961 						else
11962 							ereport(ERROR,
11963 									(errcode(ERRCODE_SYNTAX_ERROR),
11964 									 errmsg("subquery in FROM must have an alias"),
11965 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
11966 									 parser_errposition(@2)));
11967 					}
11968 					$$ = (Node *) n;
11969 				}
11970 			| joined_table
11971 				{
11972 					$$ = (Node *) $1;
11973 				}
11974 			| '(' joined_table ')' alias_clause
11975 				{
11976 					$2->alias = $4;
11977 					$$ = (Node *) $2;
11978 				}
11979 		;
11980 
11981 
11982 /*
11983  * It may seem silly to separate joined_table from table_ref, but there is
11984  * method in SQL's madness: if you don't do it this way you get reduce-
11985  * reduce conflicts, because it's not clear to the parser generator whether
11986  * to expect alias_clause after ')' or not.  For the same reason we must
11987  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
11988  * join_type to expand to empty; if we try it, the parser generator can't
11989  * figure out when to reduce an empty join_type right after table_ref.
11990  *
11991  * Note that a CROSS JOIN is the same as an unqualified
11992  * INNER JOIN, and an INNER JOIN/ON has the same shape
11993  * but a qualification expression to limit membership.
11994  * A NATURAL JOIN implicitly matches column names between
11995  * tables and the shape is determined by which columns are
11996  * in common. We'll collect columns during the later transformations.
11997  */
11998 
11999 joined_table:
12000 			'(' joined_table ')'
12001 				{
12002 					$$ = $2;
12003 				}
12004 			| table_ref CROSS JOIN table_ref
12005 				{
12006 					/* CROSS JOIN is same as unqualified inner join */
12007 					JoinExpr *n = makeNode(JoinExpr);
12008 					n->jointype = JOIN_INNER;
12009 					n->isNatural = false;
12010 					n->larg = $1;
12011 					n->rarg = $4;
12012 					n->usingClause = NIL;
12013 					n->quals = NULL;
12014 					$$ = n;
12015 				}
12016 			| table_ref join_type JOIN table_ref join_qual
12017 				{
12018 					JoinExpr *n = makeNode(JoinExpr);
12019 					n->jointype = $2;
12020 					n->isNatural = false;
12021 					n->larg = $1;
12022 					n->rarg = $4;
12023 					if ($5 != NULL && IsA($5, List))
12024 						n->usingClause = (List *) $5; /* USING clause */
12025 					else
12026 						n->quals = $5; /* ON clause */
12027 					$$ = n;
12028 				}
12029 			| table_ref JOIN table_ref join_qual
12030 				{
12031 					/* letting join_type reduce to empty doesn't work */
12032 					JoinExpr *n = makeNode(JoinExpr);
12033 					n->jointype = JOIN_INNER;
12034 					n->isNatural = false;
12035 					n->larg = $1;
12036 					n->rarg = $3;
12037 					if ($4 != NULL && IsA($4, List))
12038 						n->usingClause = (List *) $4; /* USING clause */
12039 					else
12040 						n->quals = $4; /* ON clause */
12041 					$$ = n;
12042 				}
12043 			| table_ref NATURAL join_type JOIN table_ref
12044 				{
12045 					JoinExpr *n = makeNode(JoinExpr);
12046 					n->jointype = $3;
12047 					n->isNatural = true;
12048 					n->larg = $1;
12049 					n->rarg = $5;
12050 					n->usingClause = NIL; /* figure out which columns later... */
12051 					n->quals = NULL; /* fill later */
12052 					$$ = n;
12053 				}
12054 			| table_ref NATURAL JOIN table_ref
12055 				{
12056 					/* letting join_type reduce to empty doesn't work */
12057 					JoinExpr *n = makeNode(JoinExpr);
12058 					n->jointype = JOIN_INNER;
12059 					n->isNatural = true;
12060 					n->larg = $1;
12061 					n->rarg = $4;
12062 					n->usingClause = NIL; /* figure out which columns later... */
12063 					n->quals = NULL; /* fill later */
12064 					$$ = n;
12065 				}
12066 		;
12067 
12068 alias_clause:
12069 			AS ColId '(' name_list ')'
12070 				{
12071 					$$ = makeNode(Alias);
12072 					$$->aliasname = $2;
12073 					$$->colnames = $4;
12074 				}
12075 			| AS ColId
12076 				{
12077 					$$ = makeNode(Alias);
12078 					$$->aliasname = $2;
12079 				}
12080 			| ColId '(' name_list ')'
12081 				{
12082 					$$ = makeNode(Alias);
12083 					$$->aliasname = $1;
12084 					$$->colnames = $3;
12085 				}
12086 			| ColId
12087 				{
12088 					$$ = makeNode(Alias);
12089 					$$->aliasname = $1;
12090 				}
12091 		;
12092 
12093 opt_alias_clause: alias_clause						{ $$ = $1; }
12094 			| /*EMPTY*/								{ $$ = NULL; }
12095 		;
12096 
12097 /*
12098  * func_alias_clause can include both an Alias and a coldeflist, so we make it
12099  * return a 2-element list that gets disassembled by calling production.
12100  */
12101 func_alias_clause:
12102 			alias_clause
12103 				{
12104 					$$ = list_make2($1, NIL);
12105 				}
12106 			| AS '(' TableFuncElementList ')'
12107 				{
12108 					$$ = list_make2(NULL, $3);
12109 				}
12110 			| AS ColId '(' TableFuncElementList ')'
12111 				{
12112 					Alias *a = makeNode(Alias);
12113 					a->aliasname = $2;
12114 					$$ = list_make2(a, $4);
12115 				}
12116 			| ColId '(' TableFuncElementList ')'
12117 				{
12118 					Alias *a = makeNode(Alias);
12119 					a->aliasname = $1;
12120 					$$ = list_make2(a, $3);
12121 				}
12122 			| /*EMPTY*/
12123 				{
12124 					$$ = list_make2(NULL, NIL);
12125 				}
12126 		;
12127 
12128 join_type:	FULL join_outer							{ $$ = JOIN_FULL; }
12129 			| LEFT join_outer						{ $$ = JOIN_LEFT; }
12130 			| RIGHT join_outer						{ $$ = JOIN_RIGHT; }
12131 			| INNER_P								{ $$ = JOIN_INNER; }
12132 		;
12133 
12134 /* OUTER is just noise... */
12135 join_outer: OUTER_P									{ $$ = NULL; }
12136 			| /*EMPTY*/								{ $$ = NULL; }
12137 		;
12138 
12139 /* JOIN qualification clauses
12140  * Possibilities are:
12141  *	USING ( column list ) allows only unqualified column names,
12142  *						  which must match between tables.
12143  *	ON expr allows more general qualifications.
12144  *
12145  * We return USING as a List node, while an ON-expr will not be a List.
12146  */
12147 
12148 join_qual:	USING '(' name_list ')'					{ $$ = (Node *) $3; }
12149 			| ON a_expr								{ $$ = $2; }
12150 		;
12151 
12152 
12153 relation_expr:
12154 			qualified_name
12155 				{
12156 					/* inheritance query, implicitly */
12157 					$$ = $1;
12158 					$$->inh = true;
12159 					$$->alias = NULL;
12160 				}
12161 			| qualified_name '*'
12162 				{
12163 					/* inheritance query, explicitly */
12164 					$$ = $1;
12165 					$$->inh = true;
12166 					$$->alias = NULL;
12167 				}
12168 			| ONLY qualified_name
12169 				{
12170 					/* no inheritance */
12171 					$$ = $2;
12172 					$$->inh = false;
12173 					$$->alias = NULL;
12174 				}
12175 			| ONLY '(' qualified_name ')'
12176 				{
12177 					/* no inheritance, SQL99-style syntax */
12178 					$$ = $3;
12179 					$$->inh = false;
12180 					$$->alias = NULL;
12181 				}
12182 		;
12183 
12184 
12185 relation_expr_list:
12186 			relation_expr							{ $$ = list_make1($1); }
12187 			| relation_expr_list ',' relation_expr	{ $$ = lappend($1, $3); }
12188 		;
12189 
12190 
12191 /*
12192  * Given "UPDATE foo set set ...", we have to decide without looking any
12193  * further ahead whether the first "set" is an alias or the UPDATE's SET
12194  * keyword.  Since "set" is allowed as a column name both interpretations
12195  * are feasible.  We resolve the shift/reduce conflict by giving the first
12196  * relation_expr_opt_alias production a higher precedence than the SET token
12197  * has, causing the parser to prefer to reduce, in effect assuming that the
12198  * SET is not an alias.
12199  */
12200 relation_expr_opt_alias: relation_expr					%prec UMINUS
12201 				{
12202 					$$ = $1;
12203 				}
12204 			| relation_expr ColId
12205 				{
12206 					Alias *alias = makeNode(Alias);
12207 					alias->aliasname = $2;
12208 					$1->alias = alias;
12209 					$$ = $1;
12210 				}
12211 			| relation_expr AS ColId
12212 				{
12213 					Alias *alias = makeNode(Alias);
12214 					alias->aliasname = $3;
12215 					$1->alias = alias;
12216 					$$ = $1;
12217 				}
12218 		;
12219 
12220 /*
12221  * TABLESAMPLE decoration in a FROM item
12222  */
12223 tablesample_clause:
12224 			TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause
12225 				{
12226 					RangeTableSample *n = makeNode(RangeTableSample);
12227 					/* n->relation will be filled in later */
12228 					n->method = $2;
12229 					n->args = $4;
12230 					n->repeatable = $6;
12231 					n->location = @2;
12232 					$$ = (Node *) n;
12233 				}
12234 		;
12235 
12236 opt_repeatable_clause:
12237 			REPEATABLE '(' a_expr ')'	{ $$ = (Node *) $3; }
12238 			| /*EMPTY*/					{ $$ = NULL; }
12239 		;
12240 
12241 /*
12242  * func_table represents a function invocation in a FROM list. It can be
12243  * a plain function call, like "foo(...)", or a ROWS FROM expression with
12244  * one or more function calls, "ROWS FROM (foo(...), bar(...))",
12245  * optionally with WITH ORDINALITY attached.
12246  * In the ROWS FROM syntax, a column definition list can be given for each
12247  * function, for example:
12248  *     ROWS FROM (foo() AS (foo_res_a text, foo_res_b text),
12249  *                bar() AS (bar_res_a text, bar_res_b text))
12250  * It's also possible to attach a column definition list to the RangeFunction
12251  * as a whole, but that's handled by the table_ref production.
12252  */
12253 func_table: func_expr_windowless opt_ordinality
12254 				{
12255 					RangeFunction *n = makeNode(RangeFunction);
12256 					n->lateral = false;
12257 					n->ordinality = $2;
12258 					n->is_rowsfrom = false;
12259 					n->functions = list_make1(list_make2($1, NIL));
12260 					/* alias and coldeflist are set by table_ref production */
12261 					$$ = (Node *) n;
12262 				}
12263 			| ROWS FROM '(' rowsfrom_list ')' opt_ordinality
12264 				{
12265 					RangeFunction *n = makeNode(RangeFunction);
12266 					n->lateral = false;
12267 					n->ordinality = $6;
12268 					n->is_rowsfrom = true;
12269 					n->functions = $4;
12270 					/* alias and coldeflist are set by table_ref production */
12271 					$$ = (Node *) n;
12272 				}
12273 		;
12274 
12275 rowsfrom_item: func_expr_windowless opt_col_def_list
12276 				{ $$ = list_make2($1, $2); }
12277 		;
12278 
12279 rowsfrom_list:
12280 			rowsfrom_item						{ $$ = list_make1($1); }
12281 			| rowsfrom_list ',' rowsfrom_item	{ $$ = lappend($1, $3); }
12282 		;
12283 
12284 opt_col_def_list: AS '(' TableFuncElementList ')'	{ $$ = $3; }
12285 			| /*EMPTY*/								{ $$ = NIL; }
12286 		;
12287 
12288 opt_ordinality: WITH_LA ORDINALITY					{ $$ = true; }
12289 			| /*EMPTY*/								{ $$ = false; }
12290 		;
12291 
12292 
12293 where_clause:
12294 			WHERE a_expr							{ $$ = $2; }
12295 			| /*EMPTY*/								{ $$ = NULL; }
12296 		;
12297 
12298 /* variant for UPDATE and DELETE */
12299 where_or_current_clause:
12300 			WHERE a_expr							{ $$ = $2; }
12301 			| WHERE CURRENT_P OF cursor_name
12302 				{
12303 					CurrentOfExpr *n = makeNode(CurrentOfExpr);
12304 					/* cvarno is filled in by parse analysis */
12305 					n->cursor_name = $4;
12306 					n->cursor_param = 0;
12307 					$$ = (Node *) n;
12308 				}
12309 			| /*EMPTY*/								{ $$ = NULL; }
12310 		;
12311 
12312 
12313 OptTableFuncElementList:
12314 			TableFuncElementList				{ $$ = $1; }
12315 			| /*EMPTY*/							{ $$ = NIL; }
12316 		;
12317 
12318 TableFuncElementList:
12319 			TableFuncElement
12320 				{
12321 					$$ = list_make1($1);
12322 				}
12323 			| TableFuncElementList ',' TableFuncElement
12324 				{
12325 					$$ = lappend($1, $3);
12326 				}
12327 		;
12328 
12329 TableFuncElement:	ColId Typename opt_collate_clause
12330 				{
12331 					ColumnDef *n = makeNode(ColumnDef);
12332 					n->colname = $1;
12333 					n->typeName = $2;
12334 					n->inhcount = 0;
12335 					n->is_local = true;
12336 					n->is_not_null = false;
12337 					n->is_from_type = false;
12338 					n->is_from_parent = false;
12339 					n->storage = 0;
12340 					n->raw_default = NULL;
12341 					n->cooked_default = NULL;
12342 					n->collClause = (CollateClause *) $3;
12343 					n->collOid = InvalidOid;
12344 					n->constraints = NIL;
12345 					n->location = @1;
12346 					$$ = (Node *)n;
12347 				}
12348 		;
12349 
12350 /*
12351  * XMLTABLE
12352  */
12353 xmltable:
12354 			XMLTABLE '(' c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12355 				{
12356 					RangeTableFunc *n = makeNode(RangeTableFunc);
12357 					n->rowexpr = $3;
12358 					n->docexpr = $4;
12359 					n->columns = $6;
12360 					n->namespaces = NIL;
12361 					n->location = @1;
12362 					$$ = (Node *)n;
12363 				}
12364 			| XMLTABLE '(' XMLNAMESPACES '(' xml_namespace_list ')' ','
12365 				c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12366 				{
12367 					RangeTableFunc *n = makeNode(RangeTableFunc);
12368 					n->rowexpr = $8;
12369 					n->docexpr = $9;
12370 					n->columns = $11;
12371 					n->namespaces = $5;
12372 					n->location = @1;
12373 					$$ = (Node *)n;
12374 				}
12375 		;
12376 
12377 xmltable_column_list: xmltable_column_el					{ $$ = list_make1($1); }
12378 			| xmltable_column_list ',' xmltable_column_el	{ $$ = lappend($1, $3); }
12379 		;
12380 
12381 xmltable_column_el:
12382 			ColId Typename
12383 				{
12384 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12385 
12386 					fc->colname = $1;
12387 					fc->for_ordinality = false;
12388 					fc->typeName = $2;
12389 					fc->is_not_null = false;
12390 					fc->colexpr = NULL;
12391 					fc->coldefexpr = NULL;
12392 					fc->location = @1;
12393 
12394 					$$ = (Node *) fc;
12395 				}
12396 			| ColId Typename xmltable_column_option_list
12397 				{
12398 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12399 					ListCell		   *option;
12400 					bool				nullability_seen = false;
12401 
12402 					fc->colname = $1;
12403 					fc->typeName = $2;
12404 					fc->for_ordinality = false;
12405 					fc->is_not_null = false;
12406 					fc->colexpr = NULL;
12407 					fc->coldefexpr = NULL;
12408 					fc->location = @1;
12409 
foreach(option,$3)12410 					foreach(option, $3)
12411 					{
12412 						DefElem   *defel = (DefElem *) lfirst(option);
12413 
12414 						if (strcmp(defel->defname, "default") == 0)
12415 						{
12416 							if (fc->coldefexpr != NULL)
12417 								ereport(ERROR,
12418 										(errcode(ERRCODE_SYNTAX_ERROR),
12419 										 errmsg("only one DEFAULT value is allowed"),
12420 										 parser_errposition(defel->location)));
12421 							fc->coldefexpr = defel->arg;
12422 						}
12423 						else if (strcmp(defel->defname, "path") == 0)
12424 						{
12425 							if (fc->colexpr != NULL)
12426 								ereport(ERROR,
12427 										(errcode(ERRCODE_SYNTAX_ERROR),
12428 										 errmsg("only one PATH value per column is allowed"),
12429 										 parser_errposition(defel->location)));
12430 							fc->colexpr = defel->arg;
12431 						}
12432 						else if (strcmp(defel->defname, "is_not_null") == 0)
12433 						{
12434 							if (nullability_seen)
12435 								ereport(ERROR,
12436 										(errcode(ERRCODE_SYNTAX_ERROR),
12437 										 errmsg("conflicting or redundant NULL / NOT NULL declarations for column \"%s\"", fc->colname),
12438 										 parser_errposition(defel->location)));
12439 							fc->is_not_null = intVal(defel->arg);
12440 							nullability_seen = true;
12441 						}
12442 						else
12443 						{
12444 							ereport(ERROR,
12445 									(errcode(ERRCODE_SYNTAX_ERROR),
12446 									 errmsg("unrecognized column option \"%s\"",
12447 											defel->defname),
12448 									 parser_errposition(defel->location)));
12449 						}
12450 					}
12451 					$$ = (Node *) fc;
12452 				}
12453 			| ColId FOR ORDINALITY
12454 				{
12455 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12456 
12457 					fc->colname = $1;
12458 					fc->for_ordinality = true;
12459 					/* other fields are ignored, initialized by makeNode */
12460 					fc->location = @1;
12461 
12462 					$$ = (Node *) fc;
12463 				}
12464 		;
12465 
12466 xmltable_column_option_list:
12467 			xmltable_column_option_el
12468 				{ $$ = list_make1($1); }
12469 			| xmltable_column_option_list xmltable_column_option_el
12470 				{ $$ = lappend($1, $2); }
12471 		;
12472 
12473 xmltable_column_option_el:
12474 			IDENT b_expr
12475 				{ $$ = makeDefElem($1, $2, @1); }
12476 			| DEFAULT b_expr
12477 				{ $$ = makeDefElem("default", $2, @1); }
12478 			| NOT NULL_P
12479 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(true), @1); }
12480 			| NULL_P
12481 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(false), @1); }
12482 		;
12483 
12484 xml_namespace_list:
12485 			xml_namespace_el
12486 				{ $$ = list_make1($1); }
12487 			| xml_namespace_list ',' xml_namespace_el
12488 				{ $$ = lappend($1, $3); }
12489 		;
12490 
12491 xml_namespace_el:
12492 			b_expr AS ColLabel
12493 				{
12494 					$$ = makeNode(ResTarget);
12495 					$$->name = $3;
12496 					$$->indirection = NIL;
12497 					$$->val = $1;
12498 					$$->location = @1;
12499 				}
12500 			| DEFAULT b_expr
12501 				{
12502 					$$ = makeNode(ResTarget);
12503 					$$->name = NULL;
12504 					$$->indirection = NIL;
12505 					$$->val = $2;
12506 					$$->location = @1;
12507 				}
12508 		;
12509 
12510 /*****************************************************************************
12511  *
12512  *	Type syntax
12513  *		SQL introduces a large amount of type-specific syntax.
12514  *		Define individual clauses to handle these cases, and use
12515  *		 the generic case to handle regular type-extensible Postgres syntax.
12516  *		- thomas 1997-10-10
12517  *
12518  *****************************************************************************/
12519 
12520 Typename:	SimpleTypename opt_array_bounds
12521 				{
12522 					$$ = $1;
12523 					$$->arrayBounds = $2;
12524 				}
12525 			| SETOF SimpleTypename opt_array_bounds
12526 				{
12527 					$$ = $2;
12528 					$$->arrayBounds = $3;
12529 					$$->setof = true;
12530 				}
12531 			/* SQL standard syntax, currently only one-dimensional */
12532 			| SimpleTypename ARRAY '[' Iconst ']'
12533 				{
12534 					$$ = $1;
12535 					$$->arrayBounds = list_make1(makeInteger($4));
12536 				}
12537 			| SETOF SimpleTypename ARRAY '[' Iconst ']'
12538 				{
12539 					$$ = $2;
12540 					$$->arrayBounds = list_make1(makeInteger($5));
12541 					$$->setof = true;
12542 				}
12543 			| SimpleTypename ARRAY
12544 				{
12545 					$$ = $1;
12546 					$$->arrayBounds = list_make1(makeInteger(-1));
12547 				}
12548 			| SETOF SimpleTypename ARRAY
12549 				{
12550 					$$ = $2;
12551 					$$->arrayBounds = list_make1(makeInteger(-1));
12552 					$$->setof = true;
12553 				}
12554 		;
12555 
12556 opt_array_bounds:
12557 			opt_array_bounds '[' ']'
12558 					{  $$ = lappend($1, makeInteger(-1)); }
12559 			| opt_array_bounds '[' Iconst ']'
12560 					{  $$ = lappend($1, makeInteger($3)); }
12561 			| /*EMPTY*/
12562 					{  $$ = NIL; }
12563 		;
12564 
12565 SimpleTypename:
12566 			GenericType								{ $$ = $1; }
12567 			| Numeric								{ $$ = $1; }
12568 			| Bit									{ $$ = $1; }
12569 			| Character								{ $$ = $1; }
12570 			| ConstDatetime							{ $$ = $1; }
12571 			| ConstInterval opt_interval
12572 				{
12573 					$$ = $1;
12574 					$$->typmods = $2;
12575 				}
12576 			| ConstInterval '(' Iconst ')'
12577 				{
12578 					$$ = $1;
12579 					$$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
12580 											 makeIntConst($3, @3));
12581 				}
12582 		;
12583 
12584 /* We have a separate ConstTypename to allow defaulting fixed-length
12585  * types such as CHAR() and BIT() to an unspecified length.
12586  * SQL9x requires that these default to a length of one, but this
12587  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
12588  * where there is an obvious better choice to make.
12589  * Note that ConstInterval is not included here since it must
12590  * be pushed up higher in the rules to accommodate the postfix
12591  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
12592  * the generic-type-name case in AExprConst to avoid premature
12593  * reduce/reduce conflicts against function names.
12594  */
12595 ConstTypename:
12596 			Numeric									{ $$ = $1; }
12597 			| ConstBit								{ $$ = $1; }
12598 			| ConstCharacter						{ $$ = $1; }
12599 			| ConstDatetime							{ $$ = $1; }
12600 		;
12601 
12602 /*
12603  * GenericType covers all type names that don't have special syntax mandated
12604  * by the standard, including qualified names.  We also allow type modifiers.
12605  * To avoid parsing conflicts against function invocations, the modifiers
12606  * have to be shown as expr_list here, but parse analysis will only accept
12607  * constants for them.
12608  */
12609 GenericType:
12610 			type_function_name opt_type_modifiers
12611 				{
12612 					$$ = makeTypeName($1);
12613 					$$->typmods = $2;
12614 					$$->location = @1;
12615 				}
12616 			| type_function_name attrs opt_type_modifiers
12617 				{
12618 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
12619 					$$->typmods = $3;
12620 					$$->location = @1;
12621 				}
12622 		;
12623 
12624 opt_type_modifiers: '(' expr_list ')'				{ $$ = $2; }
12625 					| /* EMPTY */					{ $$ = NIL; }
12626 		;
12627 
12628 /*
12629  * SQL numeric data types
12630  */
12631 Numeric:	INT_P
12632 				{
12633 					$$ = SystemTypeName("int4");
12634 					$$->location = @1;
12635 				}
12636 			| INTEGER
12637 				{
12638 					$$ = SystemTypeName("int4");
12639 					$$->location = @1;
12640 				}
12641 			| SMALLINT
12642 				{
12643 					$$ = SystemTypeName("int2");
12644 					$$->location = @1;
12645 				}
12646 			| BIGINT
12647 				{
12648 					$$ = SystemTypeName("int8");
12649 					$$->location = @1;
12650 				}
12651 			| REAL
12652 				{
12653 					$$ = SystemTypeName("float4");
12654 					$$->location = @1;
12655 				}
12656 			| FLOAT_P opt_float
12657 				{
12658 					$$ = $2;
12659 					$$->location = @1;
12660 				}
12661 			| DOUBLE_P PRECISION
12662 				{
12663 					$$ = SystemTypeName("float8");
12664 					$$->location = @1;
12665 				}
12666 			| DECIMAL_P opt_type_modifiers
12667 				{
12668 					$$ = SystemTypeName("numeric");
12669 					$$->typmods = $2;
12670 					$$->location = @1;
12671 				}
12672 			| DEC opt_type_modifiers
12673 				{
12674 					$$ = SystemTypeName("numeric");
12675 					$$->typmods = $2;
12676 					$$->location = @1;
12677 				}
12678 			| NUMERIC opt_type_modifiers
12679 				{
12680 					$$ = SystemTypeName("numeric");
12681 					$$->typmods = $2;
12682 					$$->location = @1;
12683 				}
12684 			| BOOLEAN_P
12685 				{
12686 					$$ = SystemTypeName("bool");
12687 					$$->location = @1;
12688 				}
12689 		;
12690 
12691 opt_float:	'(' Iconst ')'
12692 				{
12693 					/*
12694 					 * Check FLOAT() precision limits assuming IEEE floating
12695 					 * types - thomas 1997-09-18
12696 					 */
12697 					if ($2 < 1)
12698 						ereport(ERROR,
12699 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12700 								 errmsg("precision for type float must be at least 1 bit"),
12701 								 parser_errposition(@2)));
12702 					else if ($2 <= 24)
12703 						$$ = SystemTypeName("float4");
12704 					else if ($2 <= 53)
12705 						$$ = SystemTypeName("float8");
12706 					else
12707 						ereport(ERROR,
12708 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12709 								 errmsg("precision for type float must be less than 54 bits"),
12710 								 parser_errposition(@2)));
12711 				}
12712 			| /*EMPTY*/
12713 				{
12714 					$$ = SystemTypeName("float8");
12715 				}
12716 		;
12717 
12718 /*
12719  * SQL bit-field data types
12720  * The following implements BIT() and BIT VARYING().
12721  */
12722 Bit:		BitWithLength
12723 				{
12724 					$$ = $1;
12725 				}
12726 			| BitWithoutLength
12727 				{
12728 					$$ = $1;
12729 				}
12730 		;
12731 
12732 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
12733 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
12734 ConstBit:	BitWithLength
12735 				{
12736 					$$ = $1;
12737 				}
12738 			| BitWithoutLength
12739 				{
12740 					$$ = $1;
12741 					$$->typmods = NIL;
12742 				}
12743 		;
12744 
12745 BitWithLength:
12746 			BIT opt_varying '(' expr_list ')'
12747 				{
12748 					char *typname;
12749 
12750 					typname = $2 ? "varbit" : "bit";
12751 					$$ = SystemTypeName(typname);
12752 					$$->typmods = $4;
12753 					$$->location = @1;
12754 				}
12755 		;
12756 
12757 BitWithoutLength:
12758 			BIT opt_varying
12759 				{
12760 					/* bit defaults to bit(1), varbit to no limit */
12761 					if ($2)
12762 					{
12763 						$$ = SystemTypeName("varbit");
12764 					}
12765 					else
12766 					{
12767 						$$ = SystemTypeName("bit");
12768 						$$->typmods = list_make1(makeIntConst(1, -1));
12769 					}
12770 					$$->location = @1;
12771 				}
12772 		;
12773 
12774 
12775 /*
12776  * SQL character data types
12777  * The following implements CHAR() and VARCHAR().
12778  */
12779 Character:  CharacterWithLength
12780 				{
12781 					$$ = $1;
12782 				}
12783 			| CharacterWithoutLength
12784 				{
12785 					$$ = $1;
12786 				}
12787 		;
12788 
12789 ConstCharacter:  CharacterWithLength
12790 				{
12791 					$$ = $1;
12792 				}
12793 			| CharacterWithoutLength
12794 				{
12795 					/* Length was not specified so allow to be unrestricted.
12796 					 * This handles problems with fixed-length (bpchar) strings
12797 					 * which in column definitions must default to a length
12798 					 * of one, but should not be constrained if the length
12799 					 * was not specified.
12800 					 */
12801 					$$ = $1;
12802 					$$->typmods = NIL;
12803 				}
12804 		;
12805 
12806 CharacterWithLength:  character '(' Iconst ')'
12807 				{
12808 					$$ = SystemTypeName($1);
12809 					$$->typmods = list_make1(makeIntConst($3, @3));
12810 					$$->location = @1;
12811 				}
12812 		;
12813 
12814 CharacterWithoutLength:	 character
12815 				{
12816 					$$ = SystemTypeName($1);
12817 					/* char defaults to char(1), varchar to no limit */
12818 					if (strcmp($1, "bpchar") == 0)
12819 						$$->typmods = list_make1(makeIntConst(1, -1));
12820 					$$->location = @1;
12821 				}
12822 		;
12823 
12824 character:	CHARACTER opt_varying
12825 										{ $$ = $2 ? "varchar": "bpchar"; }
12826 			| CHAR_P opt_varying
12827 										{ $$ = $2 ? "varchar": "bpchar"; }
12828 			| VARCHAR
12829 										{ $$ = "varchar"; }
12830 			| NATIONAL CHARACTER opt_varying
12831 										{ $$ = $3 ? "varchar": "bpchar"; }
12832 			| NATIONAL CHAR_P opt_varying
12833 										{ $$ = $3 ? "varchar": "bpchar"; }
12834 			| NCHAR opt_varying
12835 										{ $$ = $2 ? "varchar": "bpchar"; }
12836 		;
12837 
12838 opt_varying:
12839 			VARYING									{ $$ = true; }
12840 			| /*EMPTY*/								{ $$ = false; }
12841 		;
12842 
12843 /*
12844  * SQL date/time types
12845  */
12846 ConstDatetime:
12847 			TIMESTAMP '(' Iconst ')' opt_timezone
12848 				{
12849 					if ($5)
12850 						$$ = SystemTypeName("timestamptz");
12851 					else
12852 						$$ = SystemTypeName("timestamp");
12853 					$$->typmods = list_make1(makeIntConst($3, @3));
12854 					$$->location = @1;
12855 				}
12856 			| TIMESTAMP opt_timezone
12857 				{
12858 					if ($2)
12859 						$$ = SystemTypeName("timestamptz");
12860 					else
12861 						$$ = SystemTypeName("timestamp");
12862 					$$->location = @1;
12863 				}
12864 			| TIME '(' Iconst ')' opt_timezone
12865 				{
12866 					if ($5)
12867 						$$ = SystemTypeName("timetz");
12868 					else
12869 						$$ = SystemTypeName("time");
12870 					$$->typmods = list_make1(makeIntConst($3, @3));
12871 					$$->location = @1;
12872 				}
12873 			| TIME opt_timezone
12874 				{
12875 					if ($2)
12876 						$$ = SystemTypeName("timetz");
12877 					else
12878 						$$ = SystemTypeName("time");
12879 					$$->location = @1;
12880 				}
12881 		;
12882 
12883 ConstInterval:
12884 			INTERVAL
12885 				{
12886 					$$ = SystemTypeName("interval");
12887 					$$->location = @1;
12888 				}
12889 		;
12890 
12891 opt_timezone:
12892 			WITH_LA TIME ZONE						{ $$ = true; }
12893 			| WITHOUT TIME ZONE						{ $$ = false; }
12894 			| /*EMPTY*/								{ $$ = false; }
12895 		;
12896 
12897 opt_interval:
12898 			YEAR_P
12899 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
12900 			| MONTH_P
12901 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
12902 			| DAY_P
12903 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
12904 			| HOUR_P
12905 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
12906 			| MINUTE_P
12907 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
12908 			| interval_second
12909 				{ $$ = $1; }
12910 			| YEAR_P TO MONTH_P
12911 				{
12912 					$$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
12913 												 INTERVAL_MASK(MONTH), @1));
12914 				}
12915 			| DAY_P TO HOUR_P
12916 				{
12917 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12918 												 INTERVAL_MASK(HOUR), @1));
12919 				}
12920 			| DAY_P TO MINUTE_P
12921 				{
12922 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12923 												 INTERVAL_MASK(HOUR) |
12924 												 INTERVAL_MASK(MINUTE), @1));
12925 				}
12926 			| DAY_P TO interval_second
12927 				{
12928 					$$ = $3;
12929 					linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
12930 												INTERVAL_MASK(HOUR) |
12931 												INTERVAL_MASK(MINUTE) |
12932 												INTERVAL_MASK(SECOND), @1);
12933 				}
12934 			| HOUR_P TO MINUTE_P
12935 				{
12936 					$$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
12937 												 INTERVAL_MASK(MINUTE), @1));
12938 				}
12939 			| HOUR_P TO interval_second
12940 				{
12941 					$$ = $3;
12942 					linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
12943 												INTERVAL_MASK(MINUTE) |
12944 												INTERVAL_MASK(SECOND), @1);
12945 				}
12946 			| MINUTE_P TO interval_second
12947 				{
12948 					$$ = $3;
12949 					linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
12950 												INTERVAL_MASK(SECOND), @1);
12951 				}
12952 			| /*EMPTY*/
12953 				{ $$ = NIL; }
12954 		;
12955 
12956 interval_second:
12957 			SECOND_P
12958 				{
12959 					$$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
12960 				}
12961 			| SECOND_P '(' Iconst ')'
12962 				{
12963 					$$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
12964 									makeIntConst($3, @3));
12965 				}
12966 		;
12967 
12968 
12969 /*****************************************************************************
12970  *
12971  *	expression grammar
12972  *
12973  *****************************************************************************/
12974 
12975 /*
12976  * General expressions
12977  * This is the heart of the expression syntax.
12978  *
12979  * We have two expression types: a_expr is the unrestricted kind, and
12980  * b_expr is a subset that must be used in some places to avoid shift/reduce
12981  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
12982  * because that use of AND conflicts with AND as a boolean operator.  So,
12983  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
12984  *
12985  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
12986  * always be used by surrounding it with parens.
12987  *
12988  * c_expr is all the productions that are common to a_expr and b_expr;
12989  * it's factored out just to eliminate redundant coding.
12990  *
12991  * Be careful of productions involving more than one terminal token.
12992  * By default, bison will assign such productions the precedence of their
12993  * last terminal, but in nearly all cases you want it to be the precedence
12994  * of the first terminal instead; otherwise you will not get the behavior
12995  * you expect!  So we use %prec annotations freely to set precedences.
12996  */
12997 a_expr:		c_expr									{ $$ = $1; }
12998 			| a_expr TYPECAST Typename
12999 					{ $$ = makeTypeCast($1, $3, @2); }
13000 			| a_expr COLLATE any_name
13001 				{
13002 					CollateClause *n = makeNode(CollateClause);
13003 					n->arg = $1;
13004 					n->collname = $3;
13005 					n->location = @2;
13006 					$$ = (Node *) n;
13007 				}
13008 			| a_expr AT TIME ZONE a_expr			%prec AT
13009 				{
13010 					$$ = (Node *) makeFuncCall(SystemFuncName("timezone"),
13011 											   list_make2($5, $1),
13012 											   @2);
13013 				}
13014 		/*
13015 		 * These operators must be called out explicitly in order to make use
13016 		 * of bison's automatic operator-precedence handling.  All other
13017 		 * operator names are handled by the generic productions using "Op",
13018 		 * below; and all those operators will have the same precedence.
13019 		 *
13020 		 * If you add more explicitly-known operators, be sure to add them
13021 		 * also to b_expr and to the MathOp list below.
13022 		 */
13023 			| '+' a_expr					%prec UMINUS
13024 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13025 			| '-' a_expr					%prec UMINUS
13026 				{ $$ = doNegate($2, @1); }
13027 			| a_expr '+' a_expr
13028 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13029 			| a_expr '-' a_expr
13030 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13031 			| a_expr '*' a_expr
13032 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13033 			| a_expr '/' a_expr
13034 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13035 			| a_expr '%' a_expr
13036 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13037 			| a_expr '^' a_expr
13038 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13039 			| a_expr '<' a_expr
13040 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13041 			| a_expr '>' a_expr
13042 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13043 			| a_expr '=' a_expr
13044 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13045 			| a_expr LESS_EQUALS a_expr
13046 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13047 			| a_expr GREATER_EQUALS a_expr
13048 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13049 			| a_expr NOT_EQUALS a_expr
13050 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13051 
13052 			| a_expr qual_Op a_expr				%prec Op
13053 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13054 			| qual_Op a_expr					%prec Op
13055 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13056 			| a_expr qual_Op					%prec POSTFIXOP
13057 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13058 
13059 			| a_expr AND a_expr
13060 				{ $$ = makeAndExpr($1, $3, @2); }
13061 			| a_expr OR a_expr
13062 				{ $$ = makeOrExpr($1, $3, @2); }
13063 			| NOT a_expr
13064 				{ $$ = makeNotExpr($2, @1); }
13065 			| NOT_LA a_expr						%prec NOT
13066 				{ $$ = makeNotExpr($2, @1); }
13067 
13068 			| a_expr LIKE a_expr
13069 				{
13070 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13071 												   $1, $3, @2);
13072 				}
13073 			| a_expr LIKE a_expr ESCAPE a_expr					%prec LIKE
13074 				{
13075 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13076 											   list_make2($3, $5),
13077 											   @2);
13078 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13079 												   $1, (Node *) n, @2);
13080 				}
13081 			| a_expr NOT_LA LIKE a_expr							%prec NOT_LA
13082 				{
13083 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13084 												   $1, $4, @2);
13085 				}
13086 			| a_expr NOT_LA LIKE a_expr ESCAPE a_expr			%prec NOT_LA
13087 				{
13088 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13089 											   list_make2($4, $6),
13090 											   @2);
13091 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13092 												   $1, (Node *) n, @2);
13093 				}
13094 			| a_expr ILIKE a_expr
13095 				{
13096 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13097 												   $1, $3, @2);
13098 				}
13099 			| a_expr ILIKE a_expr ESCAPE a_expr					%prec ILIKE
13100 				{
13101 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13102 											   list_make2($3, $5),
13103 											   @2);
13104 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13105 												   $1, (Node *) n, @2);
13106 				}
13107 			| a_expr NOT_LA ILIKE a_expr						%prec NOT_LA
13108 				{
13109 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13110 												   $1, $4, @2);
13111 				}
13112 			| a_expr NOT_LA ILIKE a_expr ESCAPE a_expr			%prec NOT_LA
13113 				{
13114 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13115 											   list_make2($4, $6),
13116 											   @2);
13117 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13118 												   $1, (Node *) n, @2);
13119 				}
13120 
13121 			| a_expr SIMILAR TO a_expr							%prec SIMILAR
13122 				{
13123 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13124 											   list_make2($4, makeNullAConst(-1)),
13125 											   @2);
13126 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13127 												   $1, (Node *) n, @2);
13128 				}
13129 			| a_expr SIMILAR TO a_expr ESCAPE a_expr			%prec SIMILAR
13130 				{
13131 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13132 											   list_make2($4, $6),
13133 											   @2);
13134 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13135 												   $1, (Node *) n, @2);
13136 				}
13137 			| a_expr NOT_LA SIMILAR TO a_expr					%prec NOT_LA
13138 				{
13139 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13140 											   list_make2($5, makeNullAConst(-1)),
13141 											   @2);
13142 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13143 												   $1, (Node *) n, @2);
13144 				}
13145 			| a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr		%prec NOT_LA
13146 				{
13147 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13148 											   list_make2($5, $7),
13149 											   @2);
13150 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13151 												   $1, (Node *) n, @2);
13152 				}
13153 
13154 			/* NullTest clause
13155 			 * Define SQL-style Null test clause.
13156 			 * Allow two forms described in the standard:
13157 			 *	a IS NULL
13158 			 *	a IS NOT NULL
13159 			 * Allow two SQL extensions
13160 			 *	a ISNULL
13161 			 *	a NOTNULL
13162 			 */
13163 			| a_expr IS NULL_P							%prec IS
13164 				{
13165 					NullTest *n = makeNode(NullTest);
13166 					n->arg = (Expr *) $1;
13167 					n->nulltesttype = IS_NULL;
13168 					n->location = @2;
13169 					$$ = (Node *)n;
13170 				}
13171 			| a_expr ISNULL
13172 				{
13173 					NullTest *n = makeNode(NullTest);
13174 					n->arg = (Expr *) $1;
13175 					n->nulltesttype = IS_NULL;
13176 					n->location = @2;
13177 					$$ = (Node *)n;
13178 				}
13179 			| a_expr IS NOT NULL_P						%prec IS
13180 				{
13181 					NullTest *n = makeNode(NullTest);
13182 					n->arg = (Expr *) $1;
13183 					n->nulltesttype = IS_NOT_NULL;
13184 					n->location = @2;
13185 					$$ = (Node *)n;
13186 				}
13187 			| a_expr NOTNULL
13188 				{
13189 					NullTest *n = makeNode(NullTest);
13190 					n->arg = (Expr *) $1;
13191 					n->nulltesttype = IS_NOT_NULL;
13192 					n->location = @2;
13193 					$$ = (Node *)n;
13194 				}
13195 			| row OVERLAPS row
13196 				{
13197 					if (list_length($1) != 2)
13198 						ereport(ERROR,
13199 								(errcode(ERRCODE_SYNTAX_ERROR),
13200 								 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
13201 								 parser_errposition(@1)));
13202 					if (list_length($3) != 2)
13203 						ereport(ERROR,
13204 								(errcode(ERRCODE_SYNTAX_ERROR),
13205 								 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
13206 								 parser_errposition(@3)));
13207 					$$ = (Node *) makeFuncCall(SystemFuncName("overlaps"),
13208 											   list_concat($1, $3),
13209 											   @2);
13210 				}
13211 			| a_expr IS TRUE_P							%prec IS
13212 				{
13213 					BooleanTest *b = makeNode(BooleanTest);
13214 					b->arg = (Expr *) $1;
13215 					b->booltesttype = IS_TRUE;
13216 					b->location = @2;
13217 					$$ = (Node *)b;
13218 				}
13219 			| a_expr IS NOT TRUE_P						%prec IS
13220 				{
13221 					BooleanTest *b = makeNode(BooleanTest);
13222 					b->arg = (Expr *) $1;
13223 					b->booltesttype = IS_NOT_TRUE;
13224 					b->location = @2;
13225 					$$ = (Node *)b;
13226 				}
13227 			| a_expr IS FALSE_P							%prec IS
13228 				{
13229 					BooleanTest *b = makeNode(BooleanTest);
13230 					b->arg = (Expr *) $1;
13231 					b->booltesttype = IS_FALSE;
13232 					b->location = @2;
13233 					$$ = (Node *)b;
13234 				}
13235 			| a_expr IS NOT FALSE_P						%prec IS
13236 				{
13237 					BooleanTest *b = makeNode(BooleanTest);
13238 					b->arg = (Expr *) $1;
13239 					b->booltesttype = IS_NOT_FALSE;
13240 					b->location = @2;
13241 					$$ = (Node *)b;
13242 				}
13243 			| a_expr IS UNKNOWN							%prec IS
13244 				{
13245 					BooleanTest *b = makeNode(BooleanTest);
13246 					b->arg = (Expr *) $1;
13247 					b->booltesttype = IS_UNKNOWN;
13248 					b->location = @2;
13249 					$$ = (Node *)b;
13250 				}
13251 			| a_expr IS NOT UNKNOWN						%prec IS
13252 				{
13253 					BooleanTest *b = makeNode(BooleanTest);
13254 					b->arg = (Expr *) $1;
13255 					b->booltesttype = IS_NOT_UNKNOWN;
13256 					b->location = @2;
13257 					$$ = (Node *)b;
13258 				}
13259 			| a_expr IS DISTINCT FROM a_expr			%prec IS
13260 				{
13261 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13262 				}
13263 			| a_expr IS NOT DISTINCT FROM a_expr		%prec IS
13264 				{
13265                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13266 				}
13267 			| a_expr IS OF '(' type_list ')'			%prec IS
13268 				{
13269 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13270 				}
13271 			| a_expr IS NOT OF '(' type_list ')'		%prec IS
13272 				{
13273 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13274 				}
13275 			| a_expr BETWEEN opt_asymmetric b_expr AND a_expr		%prec BETWEEN
13276 				{
13277 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN,
13278 												   "BETWEEN",
13279 												   $1,
13280 												   (Node *) list_make2($4, $6),
13281 												   @2);
13282 				}
13283 			| a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr %prec NOT_LA
13284 				{
13285 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN,
13286 												   "NOT BETWEEN",
13287 												   $1,
13288 												   (Node *) list_make2($5, $7),
13289 												   @2);
13290 				}
13291 			| a_expr BETWEEN SYMMETRIC b_expr AND a_expr			%prec BETWEEN
13292 				{
13293 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN_SYM,
13294 												   "BETWEEN SYMMETRIC",
13295 												   $1,
13296 												   (Node *) list_make2($4, $6),
13297 												   @2);
13298 				}
13299 			| a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr		%prec NOT_LA
13300 				{
13301 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN_SYM,
13302 												   "NOT BETWEEN SYMMETRIC",
13303 												   $1,
13304 												   (Node *) list_make2($5, $7),
13305 												   @2);
13306 				}
13307 			| a_expr IN_P in_expr
13308 				{
13309 					/* in_expr returns a SubLink or a list of a_exprs */
13310 					if (IsA($3, SubLink))
13311 					{
13312 						/* generate foo = ANY (subquery) */
13313 						SubLink *n = (SubLink *) $3;
13314 						n->subLinkType = ANY_SUBLINK;
13315 						n->subLinkId = 0;
13316 						n->testexpr = $1;
13317 						n->operName = NIL;		/* show it's IN not = ANY */
13318 						n->location = @2;
13319 						$$ = (Node *)n;
13320 					}
13321 					else
13322 					{
13323 						/* generate scalar IN expression */
13324 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
13325 					}
13326 				}
13327 			| a_expr NOT_LA IN_P in_expr						%prec NOT_LA
13328 				{
13329 					/* in_expr returns a SubLink or a list of a_exprs */
13330 					if (IsA($4, SubLink))
13331 					{
13332 						/* generate NOT (foo = ANY (subquery)) */
13333 						/* Make an = ANY node */
13334 						SubLink *n = (SubLink *) $4;
13335 						n->subLinkType = ANY_SUBLINK;
13336 						n->subLinkId = 0;
13337 						n->testexpr = $1;
13338 						n->operName = NIL;		/* show it's IN not = ANY */
13339 						n->location = @2;
13340 						/* Stick a NOT on top; must have same parse location */
13341 						$$ = makeNotExpr((Node *) n, @2);
13342 					}
13343 					else
13344 					{
13345 						/* generate scalar NOT IN expression */
13346 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
13347 					}
13348 				}
13349 			| a_expr subquery_Op sub_type select_with_parens	%prec Op
13350 				{
13351 					SubLink *n = makeNode(SubLink);
13352 					n->subLinkType = $3;
13353 					n->subLinkId = 0;
13354 					n->testexpr = $1;
13355 					n->operName = $2;
13356 					n->subselect = $4;
13357 					n->location = @2;
13358 					$$ = (Node *)n;
13359 				}
13360 			| a_expr subquery_Op sub_type '(' a_expr ')'		%prec Op
13361 				{
13362 					if ($3 == ANY_SUBLINK)
13363 						$$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
13364 					else
13365 						$$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
13366 				}
13367 			| UNIQUE select_with_parens
13368 				{
13369 					/* Not sure how to get rid of the parentheses
13370 					 * but there are lots of shift/reduce errors without them.
13371 					 *
13372 					 * Should be able to implement this by plopping the entire
13373 					 * select into a node, then transforming the target expressions
13374 					 * from whatever they are into count(*), and testing the
13375 					 * entire result equal to one.
13376 					 * But, will probably implement a separate node in the executor.
13377 					 */
13378 					ereport(ERROR,
13379 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13380 							 errmsg("UNIQUE predicate is not yet implemented"),
13381 							 parser_errposition(@1)));
13382 				}
13383 			| a_expr IS DOCUMENT_P					%prec IS
13384 				{
13385 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13386 									 list_make1($1), @2);
13387 				}
13388 			| a_expr IS NOT DOCUMENT_P				%prec IS
13389 				{
13390 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13391 												 list_make1($1), @2),
13392 									 @2);
13393 				}
13394 			| DEFAULT
13395 				{
13396 					/*
13397 					 * The SQL spec only allows DEFAULT in "contextually typed
13398 					 * expressions", but for us, it's easier to allow it in
13399 					 * any a_expr and then throw error during parse analysis
13400 					 * if it's in an inappropriate context.  This way also
13401 					 * lets us say something smarter than "syntax error".
13402 					 */
13403 					SetToDefault *n = makeNode(SetToDefault);
13404 					/* parse analysis will fill in the rest */
13405 					n->location = @1;
13406 					$$ = (Node *)n;
13407 				}
13408 		;
13409 
13410 /*
13411  * Restricted expressions
13412  *
13413  * b_expr is a subset of the complete expression syntax defined by a_expr.
13414  *
13415  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
13416  * cause trouble in the places where b_expr is used.  For simplicity, we
13417  * just eliminate all the boolean-keyword-operator productions from b_expr.
13418  */
13419 b_expr:		c_expr
13420 				{ $$ = $1; }
13421 			| b_expr TYPECAST Typename
13422 				{ $$ = makeTypeCast($1, $3, @2); }
13423 			| '+' b_expr					%prec UMINUS
13424 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13425 			| '-' b_expr					%prec UMINUS
13426 				{ $$ = doNegate($2, @1); }
13427 			| b_expr '+' b_expr
13428 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13429 			| b_expr '-' b_expr
13430 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13431 			| b_expr '*' b_expr
13432 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13433 			| b_expr '/' b_expr
13434 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13435 			| b_expr '%' b_expr
13436 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13437 			| b_expr '^' b_expr
13438 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13439 			| b_expr '<' b_expr
13440 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13441 			| b_expr '>' b_expr
13442 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13443 			| b_expr '=' b_expr
13444 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13445 			| b_expr LESS_EQUALS b_expr
13446 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13447 			| b_expr GREATER_EQUALS b_expr
13448 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13449 			| b_expr NOT_EQUALS b_expr
13450 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13451 			| b_expr qual_Op b_expr				%prec Op
13452 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13453 			| qual_Op b_expr					%prec Op
13454 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13455 			| b_expr qual_Op					%prec POSTFIXOP
13456 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13457 			| b_expr IS DISTINCT FROM b_expr		%prec IS
13458 				{
13459 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13460 				}
13461 			| b_expr IS NOT DISTINCT FROM b_expr	%prec IS
13462 				{
13463                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13464 				}
13465 			| b_expr IS OF '(' type_list ')'		%prec IS
13466 				{
13467 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13468 				}
13469 			| b_expr IS NOT OF '(' type_list ')'	%prec IS
13470 				{
13471 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13472 				}
13473 			| b_expr IS DOCUMENT_P					%prec IS
13474 				{
13475 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13476 									 list_make1($1), @2);
13477 				}
13478 			| b_expr IS NOT DOCUMENT_P				%prec IS
13479 				{
13480 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13481 												 list_make1($1), @2),
13482 									 @2);
13483 				}
13484 		;
13485 
13486 /*
13487  * Productions that can be used in both a_expr and b_expr.
13488  *
13489  * Note: productions that refer recursively to a_expr or b_expr mostly
13490  * cannot appear here.	However, it's OK to refer to a_exprs that occur
13491  * inside parentheses, such as function arguments; that cannot introduce
13492  * ambiguity to the b_expr syntax.
13493  */
13494 c_expr:		columnref								{ $$ = $1; }
13495 			| AexprConst							{ $$ = $1; }
13496 			| PARAM opt_indirection
13497 				{
13498 					ParamRef *p = makeNode(ParamRef);
13499 					p->number = $1;
13500 					p->location = @1;
13501 					if ($2)
13502 					{
13503 						A_Indirection *n = makeNode(A_Indirection);
13504 						n->arg = (Node *) p;
13505 						n->indirection = check_indirection($2, yyscanner);
13506 						$$ = (Node *) n;
13507 					}
13508 					else
13509 						$$ = (Node *) p;
13510 				}
13511 			| '(' a_expr ')' opt_indirection
13512 				{
13513 					if ($4)
13514 					{
13515 						A_Indirection *n = makeNode(A_Indirection);
13516 						n->arg = $2;
13517 						n->indirection = check_indirection($4, yyscanner);
13518 						$$ = (Node *)n;
13519 					}
13520 					else if (operator_precedence_warning)
13521 					{
13522 						/*
13523 						 * If precedence warnings are enabled, insert
13524 						 * AEXPR_PAREN nodes wrapping all explicitly
13525 						 * parenthesized subexpressions; this prevents bogus
13526 						 * warnings from being issued when the ordering has
13527 						 * been forced by parentheses.  Take care that an
13528 						 * AEXPR_PAREN node has the same exprLocation as its
13529 						 * child, so as not to cause surprising changes in
13530 						 * error cursor positioning.
13531 						 *
13532 						 * In principle we should not be relying on a GUC to
13533 						 * decide whether to insert AEXPR_PAREN nodes.
13534 						 * However, since they have no effect except to
13535 						 * suppress warnings, it's probably safe enough; and
13536 						 * we'd just as soon not waste cycles on dummy parse
13537 						 * nodes if we don't have to.
13538 						 */
13539 						$$ = (Node *) makeA_Expr(AEXPR_PAREN, NIL, $2, NULL,
13540 												 exprLocation($2));
13541 					}
13542 					else
13543 						$$ = $2;
13544 				}
13545 			| case_expr
13546 				{ $$ = $1; }
13547 			| func_expr
13548 				{ $$ = $1; }
13549 			| select_with_parens			%prec UMINUS
13550 				{
13551 					SubLink *n = makeNode(SubLink);
13552 					n->subLinkType = EXPR_SUBLINK;
13553 					n->subLinkId = 0;
13554 					n->testexpr = NULL;
13555 					n->operName = NIL;
13556 					n->subselect = $1;
13557 					n->location = @1;
13558 					$$ = (Node *)n;
13559 				}
13560 			| select_with_parens indirection
13561 				{
13562 					/*
13563 					 * Because the select_with_parens nonterminal is designed
13564 					 * to "eat" as many levels of parens as possible, the
13565 					 * '(' a_expr ')' opt_indirection production above will
13566 					 * fail to match a sub-SELECT with indirection decoration;
13567 					 * the sub-SELECT won't be regarded as an a_expr as long
13568 					 * as there are parens around it.  To support applying
13569 					 * subscripting or field selection to a sub-SELECT result,
13570 					 * we need this redundant-looking production.
13571 					 */
13572 					SubLink *n = makeNode(SubLink);
13573 					A_Indirection *a = makeNode(A_Indirection);
13574 					n->subLinkType = EXPR_SUBLINK;
13575 					n->subLinkId = 0;
13576 					n->testexpr = NULL;
13577 					n->operName = NIL;
13578 					n->subselect = $1;
13579 					n->location = @1;
13580 					a->arg = (Node *)n;
13581 					a->indirection = check_indirection($2, yyscanner);
13582 					$$ = (Node *)a;
13583 				}
13584 			| EXISTS select_with_parens
13585 				{
13586 					SubLink *n = makeNode(SubLink);
13587 					n->subLinkType = EXISTS_SUBLINK;
13588 					n->subLinkId = 0;
13589 					n->testexpr = NULL;
13590 					n->operName = NIL;
13591 					n->subselect = $2;
13592 					n->location = @1;
13593 					$$ = (Node *)n;
13594 				}
13595 			| ARRAY select_with_parens
13596 				{
13597 					SubLink *n = makeNode(SubLink);
13598 					n->subLinkType = ARRAY_SUBLINK;
13599 					n->subLinkId = 0;
13600 					n->testexpr = NULL;
13601 					n->operName = NIL;
13602 					n->subselect = $2;
13603 					n->location = @1;
13604 					$$ = (Node *)n;
13605 				}
13606 			| ARRAY array_expr
13607 				{
13608 					A_ArrayExpr *n = castNode(A_ArrayExpr, $2);
13609 					/* point outermost A_ArrayExpr to the ARRAY keyword */
13610 					n->location = @1;
13611 					$$ = (Node *)n;
13612 				}
13613 			| explicit_row
13614 				{
13615 					RowExpr *r = makeNode(RowExpr);
13616 					r->args = $1;
13617 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13618 					r->colnames = NIL;	/* to be filled in during analysis */
13619 					r->row_format = COERCE_EXPLICIT_CALL; /* abuse */
13620 					r->location = @1;
13621 					$$ = (Node *)r;
13622 				}
13623 			| implicit_row
13624 				{
13625 					RowExpr *r = makeNode(RowExpr);
13626 					r->args = $1;
13627 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13628 					r->colnames = NIL;	/* to be filled in during analysis */
13629 					r->row_format = COERCE_IMPLICIT_CAST; /* abuse */
13630 					r->location = @1;
13631 					$$ = (Node *)r;
13632 				}
13633 			| GROUPING '(' expr_list ')'
13634 			  {
13635 				  GroupingFunc *g = makeNode(GroupingFunc);
13636 				  g->args = $3;
13637 				  g->location = @1;
13638 				  $$ = (Node *)g;
13639 			  }
13640 		;
13641 
13642 func_application: func_name '(' ')'
13643 				{
13644 					$$ = (Node *) makeFuncCall($1, NIL, @1);
13645 				}
13646 			| func_name '(' func_arg_list opt_sort_clause ')'
13647 				{
13648 					FuncCall *n = makeFuncCall($1, $3, @1);
13649 					n->agg_order = $4;
13650 					$$ = (Node *)n;
13651 				}
13652 			| func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'
13653 				{
13654 					FuncCall *n = makeFuncCall($1, list_make1($4), @1);
13655 					n->func_variadic = true;
13656 					n->agg_order = $5;
13657 					$$ = (Node *)n;
13658 				}
13659 			| func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'
13660 				{
13661 					FuncCall *n = makeFuncCall($1, lappend($3, $6), @1);
13662 					n->func_variadic = true;
13663 					n->agg_order = $7;
13664 					$$ = (Node *)n;
13665 				}
13666 			| func_name '(' ALL func_arg_list opt_sort_clause ')'
13667 				{
13668 					FuncCall *n = makeFuncCall($1, $4, @1);
13669 					n->agg_order = $5;
13670 					/* Ideally we'd mark the FuncCall node to indicate
13671 					 * "must be an aggregate", but there's no provision
13672 					 * for that in FuncCall at the moment.
13673 					 */
13674 					$$ = (Node *)n;
13675 				}
13676 			| func_name '(' DISTINCT func_arg_list opt_sort_clause ')'
13677 				{
13678 					FuncCall *n = makeFuncCall($1, $4, @1);
13679 					n->agg_order = $5;
13680 					n->agg_distinct = true;
13681 					$$ = (Node *)n;
13682 				}
13683 			| func_name '(' '*' ')'
13684 				{
13685 					/*
13686 					 * We consider AGGREGATE(*) to invoke a parameterless
13687 					 * aggregate.  This does the right thing for COUNT(*),
13688 					 * and there are no other aggregates in SQL that accept
13689 					 * '*' as parameter.
13690 					 *
13691 					 * The FuncCall node is also marked agg_star = true,
13692 					 * so that later processing can detect what the argument
13693 					 * really was.
13694 					 */
13695 					FuncCall *n = makeFuncCall($1, NIL, @1);
13696 					n->agg_star = true;
13697 					$$ = (Node *)n;
13698 				}
13699 		;
13700 
13701 
13702 /*
13703  * func_expr and its cousin func_expr_windowless are split out from c_expr just
13704  * so that we have classifications for "everything that is a function call or
13705  * looks like one".  This isn't very important, but it saves us having to
13706  * document which variants are legal in places like "FROM function()" or the
13707  * backwards-compatible functional-index syntax for CREATE INDEX.
13708  * (Note that many of the special SQL functions wouldn't actually make any
13709  * sense as functional index entries, but we ignore that consideration here.)
13710  */
13711 func_expr: func_application within_group_clause filter_clause over_clause
13712 				{
13713 					FuncCall *n = (FuncCall *) $1;
13714 					/*
13715 					 * The order clause for WITHIN GROUP and the one for
13716 					 * plain-aggregate ORDER BY share a field, so we have to
13717 					 * check here that at most one is present.  We also check
13718 					 * for DISTINCT and VARIADIC here to give a better error
13719 					 * location.  Other consistency checks are deferred to
13720 					 * parse analysis.
13721 					 */
13722 					if ($2 != NIL)
13723 					{
13724 						if (n->agg_order != NIL)
13725 							ereport(ERROR,
13726 									(errcode(ERRCODE_SYNTAX_ERROR),
13727 									 errmsg("cannot use multiple ORDER BY clauses with WITHIN GROUP"),
13728 									 parser_errposition(@2)));
13729 						if (n->agg_distinct)
13730 							ereport(ERROR,
13731 									(errcode(ERRCODE_SYNTAX_ERROR),
13732 									 errmsg("cannot use DISTINCT with WITHIN GROUP"),
13733 									 parser_errposition(@2)));
13734 						if (n->func_variadic)
13735 							ereport(ERROR,
13736 									(errcode(ERRCODE_SYNTAX_ERROR),
13737 									 errmsg("cannot use VARIADIC with WITHIN GROUP"),
13738 									 parser_errposition(@2)));
13739 						n->agg_order = $2;
13740 						n->agg_within_group = true;
13741 					}
13742 					n->agg_filter = $3;
13743 					n->over = $4;
13744 					$$ = (Node *) n;
13745 				}
13746 			| func_expr_common_subexpr
13747 				{ $$ = $1; }
13748 		;
13749 
13750 /*
13751  * As func_expr but does not accept WINDOW functions directly
13752  * (but they can still be contained in arguments for functions etc).
13753  * Use this when window expressions are not allowed, where needed to
13754  * disambiguate the grammar (e.g. in CREATE INDEX).
13755  */
13756 func_expr_windowless:
13757 			func_application						{ $$ = $1; }
13758 			| func_expr_common_subexpr				{ $$ = $1; }
13759 		;
13760 
13761 /*
13762  * Special expressions that are considered to be functions.
13763  */
13764 func_expr_common_subexpr:
13765 			COLLATION FOR '(' a_expr ')'
13766 				{
13767 					$$ = (Node *) makeFuncCall(SystemFuncName("pg_collation_for"),
13768 											   list_make1($4),
13769 											   @1);
13770 				}
13771 			| CURRENT_DATE
13772 				{
13773 					$$ = makeSQLValueFunction(SVFOP_CURRENT_DATE, -1, @1);
13774 				}
13775 			| CURRENT_TIME
13776 				{
13777 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME, -1, @1);
13778 				}
13779 			| CURRENT_TIME '(' Iconst ')'
13780 				{
13781 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME_N, $3, @1);
13782 				}
13783 			| CURRENT_TIMESTAMP
13784 				{
13785 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP, -1, @1);
13786 				}
13787 			| CURRENT_TIMESTAMP '(' Iconst ')'
13788 				{
13789 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP_N, $3, @1);
13790 				}
13791 			| LOCALTIME
13792 				{
13793 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME, -1, @1);
13794 				}
13795 			| LOCALTIME '(' Iconst ')'
13796 				{
13797 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME_N, $3, @1);
13798 				}
13799 			| LOCALTIMESTAMP
13800 				{
13801 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP, -1, @1);
13802 				}
13803 			| LOCALTIMESTAMP '(' Iconst ')'
13804 				{
13805 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP_N, $3, @1);
13806 				}
13807 			| CURRENT_ROLE
13808 				{
13809 					$$ = makeSQLValueFunction(SVFOP_CURRENT_ROLE, -1, @1);
13810 				}
13811 			| CURRENT_USER
13812 				{
13813 					$$ = makeSQLValueFunction(SVFOP_CURRENT_USER, -1, @1);
13814 				}
13815 			| SESSION_USER
13816 				{
13817 					$$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1);
13818 				}
13819 			| USER
13820 				{
13821 					$$ = makeSQLValueFunction(SVFOP_USER, -1, @1);
13822 				}
13823 			| CURRENT_CATALOG
13824 				{
13825 					$$ = makeSQLValueFunction(SVFOP_CURRENT_CATALOG, -1, @1);
13826 				}
13827 			| CURRENT_SCHEMA
13828 				{
13829 					$$ = makeSQLValueFunction(SVFOP_CURRENT_SCHEMA, -1, @1);
13830 				}
13831 			| CAST '(' a_expr AS Typename ')'
13832 				{ $$ = makeTypeCast($3, $5, @1); }
13833 			| EXTRACT '(' extract_list ')'
13834 				{
13835 					$$ = (Node *) makeFuncCall(SystemFuncName("date_part"), $3, @1);
13836 				}
13837 			| OVERLAY '(' overlay_list ')'
13838 				{
13839 					/* overlay(A PLACING B FROM C FOR D) is converted to
13840 					 * overlay(A, B, C, D)
13841 					 * overlay(A PLACING B FROM C) is converted to
13842 					 * overlay(A, B, C)
13843 					 */
13844 					$$ = (Node *) makeFuncCall(SystemFuncName("overlay"), $3, @1);
13845 				}
13846 			| POSITION '(' position_list ')'
13847 				{
13848 					/* position(A in B) is converted to position(B, A) */
13849 					$$ = (Node *) makeFuncCall(SystemFuncName("position"), $3, @1);
13850 				}
13851 			| SUBSTRING '(' substr_list ')'
13852 				{
13853 					/* substring(A from B for C) is converted to
13854 					 * substring(A, B, C) - thomas 2000-11-28
13855 					 */
13856 					$$ = (Node *) makeFuncCall(SystemFuncName("substring"), $3, @1);
13857 				}
13858 			| TREAT '(' a_expr AS Typename ')'
13859 				{
13860 					/* TREAT(expr AS target) converts expr of a particular type to target,
13861 					 * which is defined to be a subtype of the original expression.
13862 					 * In SQL99, this is intended for use with structured UDTs,
13863 					 * but let's make this a generally useful form allowing stronger
13864 					 * coercions than are handled by implicit casting.
13865 					 *
13866 					 * Convert SystemTypeName() to SystemFuncName() even though
13867 					 * at the moment they result in the same thing.
13868 					 */
13869 					$$ = (Node *) makeFuncCall(SystemFuncName(((Value *)llast($5->names))->val.str),
13870 												list_make1($3),
13871 												@1);
13872 				}
13873 			| TRIM '(' BOTH trim_list ')'
13874 				{
13875 					/* various trim expressions are defined in SQL
13876 					 * - thomas 1997-07-19
13877 					 */
13878 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $4, @1);
13879 				}
13880 			| TRIM '(' LEADING trim_list ')'
13881 				{
13882 					$$ = (Node *) makeFuncCall(SystemFuncName("ltrim"), $4, @1);
13883 				}
13884 			| TRIM '(' TRAILING trim_list ')'
13885 				{
13886 					$$ = (Node *) makeFuncCall(SystemFuncName("rtrim"), $4, @1);
13887 				}
13888 			| TRIM '(' trim_list ')'
13889 				{
13890 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $3, @1);
13891 				}
13892 			| NULLIF '(' a_expr ',' a_expr ')'
13893 				{
13894 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
13895 				}
13896 			| COALESCE '(' expr_list ')'
13897 				{
13898 					CoalesceExpr *c = makeNode(CoalesceExpr);
13899 					c->args = $3;
13900 					c->location = @1;
13901 					$$ = (Node *)c;
13902 				}
13903 			| GREATEST '(' expr_list ')'
13904 				{
13905 					MinMaxExpr *v = makeNode(MinMaxExpr);
13906 					v->args = $3;
13907 					v->op = IS_GREATEST;
13908 					v->location = @1;
13909 					$$ = (Node *)v;
13910 				}
13911 			| LEAST '(' expr_list ')'
13912 				{
13913 					MinMaxExpr *v = makeNode(MinMaxExpr);
13914 					v->args = $3;
13915 					v->op = IS_LEAST;
13916 					v->location = @1;
13917 					$$ = (Node *)v;
13918 				}
13919 			| XMLCONCAT '(' expr_list ')'
13920 				{
13921 					$$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
13922 				}
13923 			| XMLELEMENT '(' NAME_P ColLabel ')'
13924 				{
13925 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
13926 				}
13927 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
13928 				{
13929 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
13930 				}
13931 			| XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
13932 				{
13933 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
13934 				}
13935 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
13936 				{
13937 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
13938 				}
13939 			| XMLEXISTS '(' c_expr xmlexists_argument ')'
13940 				{
13941 					/* xmlexists(A PASSING [BY REF] B [BY REF]) is
13942 					 * converted to xmlexists(A, B)*/
13943 					$$ = (Node *) makeFuncCall(SystemFuncName("xmlexists"), list_make2($3, $4), @1);
13944 				}
13945 			| XMLFOREST '(' xml_attribute_list ')'
13946 				{
13947 					$$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
13948 				}
13949 			| XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
13950 				{
13951 					XmlExpr *x = (XmlExpr *)
13952 						makeXmlExpr(IS_XMLPARSE, NULL, NIL,
13953 									list_make2($4, makeBoolAConst($5, -1)),
13954 									@1);
13955 					x->xmloption = $3;
13956 					$$ = (Node *)x;
13957 				}
13958 			| XMLPI '(' NAME_P ColLabel ')'
13959 				{
13960 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
13961 				}
13962 			| XMLPI '(' NAME_P ColLabel ',' a_expr ')'
13963 				{
13964 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
13965 				}
13966 			| XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
13967 				{
13968 					$$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
13969 									 list_make3($3, $5, $6), @1);
13970 				}
13971 			| XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
13972 				{
13973 					XmlSerialize *n = makeNode(XmlSerialize);
13974 					n->xmloption = $3;
13975 					n->expr = $4;
13976 					n->typeName = $6;
13977 					n->location = @1;
13978 					$$ = (Node *)n;
13979 				}
13980 		;
13981 
13982 /*
13983  * SQL/XML support
13984  */
13985 xml_root_version: VERSION_P a_expr
13986 				{ $$ = $2; }
13987 			| VERSION_P NO VALUE_P
13988 				{ $$ = makeNullAConst(-1); }
13989 		;
13990 
13991 opt_xml_root_standalone: ',' STANDALONE_P YES_P
13992 				{ $$ = makeIntConst(XML_STANDALONE_YES, -1); }
13993 			| ',' STANDALONE_P NO
13994 				{ $$ = makeIntConst(XML_STANDALONE_NO, -1); }
13995 			| ',' STANDALONE_P NO VALUE_P
13996 				{ $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
13997 			| /*EMPTY*/
13998 				{ $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
13999 		;
14000 
14001 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'	{ $$ = $3; }
14002 		;
14003 
14004 xml_attribute_list:	xml_attribute_el					{ $$ = list_make1($1); }
14005 			| xml_attribute_list ',' xml_attribute_el	{ $$ = lappend($1, $3); }
14006 		;
14007 
14008 xml_attribute_el: a_expr AS ColLabel
14009 				{
14010 					$$ = makeNode(ResTarget);
14011 					$$->name = $3;
14012 					$$->indirection = NIL;
14013 					$$->val = (Node *) $1;
14014 					$$->location = @1;
14015 				}
14016 			| a_expr
14017 				{
14018 					$$ = makeNode(ResTarget);
14019 					$$->name = NULL;
14020 					$$->indirection = NIL;
14021 					$$->val = (Node *) $1;
14022 					$$->location = @1;
14023 				}
14024 		;
14025 
14026 document_or_content: DOCUMENT_P						{ $$ = XMLOPTION_DOCUMENT; }
14027 			| CONTENT_P								{ $$ = XMLOPTION_CONTENT; }
14028 		;
14029 
14030 xml_whitespace_option: PRESERVE WHITESPACE_P		{ $$ = true; }
14031 			| STRIP_P WHITESPACE_P					{ $$ = false; }
14032 			| /*EMPTY*/								{ $$ = false; }
14033 		;
14034 
14035 /* We allow several variants for SQL and other compatibility. */
14036 xmlexists_argument:
14037 			PASSING c_expr
14038 				{
14039 					$$ = $2;
14040 				}
14041 			| PASSING c_expr BY REF
14042 				{
14043 					$$ = $2;
14044 				}
14045 			| PASSING BY REF c_expr
14046 				{
14047 					$$ = $4;
14048 				}
14049 			| PASSING BY REF c_expr BY REF
14050 				{
14051 					$$ = $4;
14052 				}
14053 		;
14054 
14055 
14056 /*
14057  * Aggregate decoration clauses
14058  */
14059 within_group_clause:
14060 			WITHIN GROUP_P '(' sort_clause ')'		{ $$ = $4; }
14061 			| /*EMPTY*/								{ $$ = NIL; }
14062 		;
14063 
14064 filter_clause:
14065 			FILTER '(' WHERE a_expr ')'				{ $$ = $4; }
14066 			| /*EMPTY*/								{ $$ = NULL; }
14067 		;
14068 
14069 
14070 /*
14071  * Window Definitions
14072  */
14073 window_clause:
14074 			WINDOW window_definition_list			{ $$ = $2; }
14075 			| /*EMPTY*/								{ $$ = NIL; }
14076 		;
14077 
14078 window_definition_list:
14079 			window_definition						{ $$ = list_make1($1); }
14080 			| window_definition_list ',' window_definition
14081 													{ $$ = lappend($1, $3); }
14082 		;
14083 
14084 window_definition:
14085 			ColId AS window_specification
14086 				{
14087 					WindowDef *n = $3;
14088 					n->name = $1;
14089 					$$ = n;
14090 				}
14091 		;
14092 
14093 over_clause: OVER window_specification
14094 				{ $$ = $2; }
14095 			| OVER ColId
14096 				{
14097 					WindowDef *n = makeNode(WindowDef);
14098 					n->name = $2;
14099 					n->refname = NULL;
14100 					n->partitionClause = NIL;
14101 					n->orderClause = NIL;
14102 					n->frameOptions = FRAMEOPTION_DEFAULTS;
14103 					n->startOffset = NULL;
14104 					n->endOffset = NULL;
14105 					n->location = @2;
14106 					$$ = n;
14107 				}
14108 			| /*EMPTY*/
14109 				{ $$ = NULL; }
14110 		;
14111 
14112 window_specification: '(' opt_existing_window_name opt_partition_clause
14113 						opt_sort_clause opt_frame_clause ')'
14114 				{
14115 					WindowDef *n = makeNode(WindowDef);
14116 					n->name = NULL;
14117 					n->refname = $2;
14118 					n->partitionClause = $3;
14119 					n->orderClause = $4;
14120 					/* copy relevant fields of opt_frame_clause */
14121 					n->frameOptions = $5->frameOptions;
14122 					n->startOffset = $5->startOffset;
14123 					n->endOffset = $5->endOffset;
14124 					n->location = @1;
14125 					$$ = n;
14126 				}
14127 		;
14128 
14129 /*
14130  * If we see PARTITION, RANGE, ROWS or GROUPS as the first token after the '('
14131  * of a window_specification, we want the assumption to be that there is
14132  * no existing_window_name; but those keywords are unreserved and so could
14133  * be ColIds.  We fix this by making them have the same precedence as IDENT
14134  * and giving the empty production here a slightly higher precedence, so
14135  * that the shift/reduce conflict is resolved in favor of reducing the rule.
14136  * These keywords are thus precluded from being an existing_window_name but
14137  * are not reserved for any other purpose.
14138  */
14139 opt_existing_window_name: ColId						{ $$ = $1; }
14140 			| /*EMPTY*/				%prec Op		{ $$ = NULL; }
14141 		;
14142 
14143 opt_partition_clause: PARTITION BY expr_list		{ $$ = $3; }
14144 			| /*EMPTY*/								{ $$ = NIL; }
14145 		;
14146 
14147 /*
14148  * For frame clauses, we return a WindowDef, but only some fields are used:
14149  * frameOptions, startOffset, and endOffset.
14150  */
14151 opt_frame_clause:
14152 			RANGE frame_extent opt_window_exclusion_clause
14153 				{
14154 					WindowDef *n = $2;
14155 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
14156 					n->frameOptions |= $3;
14157 					$$ = n;
14158 				}
14159 			| ROWS frame_extent opt_window_exclusion_clause
14160 				{
14161 					WindowDef *n = $2;
14162 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
14163 					n->frameOptions |= $3;
14164 					$$ = n;
14165 				}
14166 			| GROUPS frame_extent opt_window_exclusion_clause
14167 				{
14168 					WindowDef *n = $2;
14169 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_GROUPS;
14170 					n->frameOptions |= $3;
14171 					$$ = n;
14172 				}
14173 			| /*EMPTY*/
14174 				{
14175 					WindowDef *n = makeNode(WindowDef);
14176 					n->frameOptions = FRAMEOPTION_DEFAULTS;
14177 					n->startOffset = NULL;
14178 					n->endOffset = NULL;
14179 					$$ = n;
14180 				}
14181 		;
14182 
14183 frame_extent: frame_bound
14184 				{
14185 					WindowDef *n = $1;
14186 					/* reject invalid cases */
14187 					if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14188 						ereport(ERROR,
14189 								(errcode(ERRCODE_WINDOWING_ERROR),
14190 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14191 								 parser_errposition(@1)));
14192 					if (n->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
14193 						ereport(ERROR,
14194 								(errcode(ERRCODE_WINDOWING_ERROR),
14195 								 errmsg("frame starting from following row cannot end with current row"),
14196 								 parser_errposition(@1)));
14197 					n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
14198 					$$ = n;
14199 				}
14200 			| BETWEEN frame_bound AND frame_bound
14201 				{
14202 					WindowDef *n1 = $2;
14203 					WindowDef *n2 = $4;
14204 					/* form merged options */
14205 					int		frameOptions = n1->frameOptions;
14206 					/* shift converts START_ options to END_ options */
14207 					frameOptions |= n2->frameOptions << 1;
14208 					frameOptions |= FRAMEOPTION_BETWEEN;
14209 					/* reject invalid cases */
14210 					if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14211 						ereport(ERROR,
14212 								(errcode(ERRCODE_WINDOWING_ERROR),
14213 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14214 								 parser_errposition(@2)));
14215 					if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
14216 						ereport(ERROR,
14217 								(errcode(ERRCODE_WINDOWING_ERROR),
14218 								 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
14219 								 parser_errposition(@4)));
14220 					if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
14221 						(frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING))
14222 						ereport(ERROR,
14223 								(errcode(ERRCODE_WINDOWING_ERROR),
14224 								 errmsg("frame starting from current row cannot have preceding rows"),
14225 								 parser_errposition(@4)));
14226 					if ((frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING) &&
14227 						(frameOptions & (FRAMEOPTION_END_OFFSET_PRECEDING |
14228 										 FRAMEOPTION_END_CURRENT_ROW)))
14229 						ereport(ERROR,
14230 								(errcode(ERRCODE_WINDOWING_ERROR),
14231 								 errmsg("frame starting from following row cannot have preceding rows"),
14232 								 parser_errposition(@4)));
14233 					n1->frameOptions = frameOptions;
14234 					n1->endOffset = n2->startOffset;
14235 					$$ = n1;
14236 				}
14237 		;
14238 
14239 /*
14240  * This is used for both frame start and frame end, with output set up on
14241  * the assumption it's frame start; the frame_extent productions must reject
14242  * invalid cases.
14243  */
14244 frame_bound:
14245 			UNBOUNDED PRECEDING
14246 				{
14247 					WindowDef *n = makeNode(WindowDef);
14248 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
14249 					n->startOffset = NULL;
14250 					n->endOffset = NULL;
14251 					$$ = n;
14252 				}
14253 			| UNBOUNDED FOLLOWING
14254 				{
14255 					WindowDef *n = makeNode(WindowDef);
14256 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
14257 					n->startOffset = NULL;
14258 					n->endOffset = NULL;
14259 					$$ = n;
14260 				}
14261 			| CURRENT_P ROW
14262 				{
14263 					WindowDef *n = makeNode(WindowDef);
14264 					n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
14265 					n->startOffset = NULL;
14266 					n->endOffset = NULL;
14267 					$$ = n;
14268 				}
14269 			| a_expr PRECEDING
14270 				{
14271 					WindowDef *n = makeNode(WindowDef);
14272 					n->frameOptions = FRAMEOPTION_START_OFFSET_PRECEDING;
14273 					n->startOffset = $1;
14274 					n->endOffset = NULL;
14275 					$$ = n;
14276 				}
14277 			| a_expr FOLLOWING
14278 				{
14279 					WindowDef *n = makeNode(WindowDef);
14280 					n->frameOptions = FRAMEOPTION_START_OFFSET_FOLLOWING;
14281 					n->startOffset = $1;
14282 					n->endOffset = NULL;
14283 					$$ = n;
14284 				}
14285 		;
14286 
14287 opt_window_exclusion_clause:
14288 			EXCLUDE CURRENT_P ROW	{ $$ = FRAMEOPTION_EXCLUDE_CURRENT_ROW; }
14289 			| EXCLUDE GROUP_P		{ $$ = FRAMEOPTION_EXCLUDE_GROUP; }
14290 			| EXCLUDE TIES			{ $$ = FRAMEOPTION_EXCLUDE_TIES; }
14291 			| EXCLUDE NO OTHERS		{ $$ = 0; }
14292 			| /*EMPTY*/				{ $$ = 0; }
14293 		;
14294 
14295 
14296 /*
14297  * Supporting nonterminals for expressions.
14298  */
14299 
14300 /* Explicit row production.
14301  *
14302  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
14303  * without conflicting with the parenthesized a_expr production.  Without the
14304  * ROW keyword, there must be more than one a_expr inside the parens.
14305  */
14306 row:		ROW '(' expr_list ')'					{ $$ = $3; }
14307 			| ROW '(' ')'							{ $$ = NIL; }
14308 			| '(' expr_list ',' a_expr ')'			{ $$ = lappend($2, $4); }
14309 		;
14310 
14311 explicit_row:	ROW '(' expr_list ')'				{ $$ = $3; }
14312 			| ROW '(' ')'							{ $$ = NIL; }
14313 		;
14314 
14315 implicit_row:	'(' expr_list ',' a_expr ')'		{ $$ = lappend($2, $4); }
14316 		;
14317 
14318 sub_type:	ANY										{ $$ = ANY_SUBLINK; }
14319 			| SOME									{ $$ = ANY_SUBLINK; }
14320 			| ALL									{ $$ = ALL_SUBLINK; }
14321 		;
14322 
14323 all_Op:		Op										{ $$ = $1; }
14324 			| MathOp								{ $$ = $1; }
14325 		;
14326 
14327 MathOp:		 '+'									{ $$ = "+"; }
14328 			| '-'									{ $$ = "-"; }
14329 			| '*'									{ $$ = "*"; }
14330 			| '/'									{ $$ = "/"; }
14331 			| '%'									{ $$ = "%"; }
14332 			| '^'									{ $$ = "^"; }
14333 			| '<'									{ $$ = "<"; }
14334 			| '>'									{ $$ = ">"; }
14335 			| '='									{ $$ = "="; }
14336 			| LESS_EQUALS							{ $$ = "<="; }
14337 			| GREATER_EQUALS						{ $$ = ">="; }
14338 			| NOT_EQUALS							{ $$ = "<>"; }
14339 		;
14340 
14341 qual_Op:	Op
14342 					{ $$ = list_make1(makeString($1)); }
14343 			| OPERATOR '(' any_operator ')'
14344 					{ $$ = $3; }
14345 		;
14346 
14347 qual_all_Op:
14348 			all_Op
14349 					{ $$ = list_make1(makeString($1)); }
14350 			| OPERATOR '(' any_operator ')'
14351 					{ $$ = $3; }
14352 		;
14353 
14354 subquery_Op:
14355 			all_Op
14356 					{ $$ = list_make1(makeString($1)); }
14357 			| OPERATOR '(' any_operator ')'
14358 					{ $$ = $3; }
14359 			| LIKE
14360 					{ $$ = list_make1(makeString("~~")); }
14361 			| NOT_LA LIKE
14362 					{ $$ = list_make1(makeString("!~~")); }
14363 			| ILIKE
14364 					{ $$ = list_make1(makeString("~~*")); }
14365 			| NOT_LA ILIKE
14366 					{ $$ = list_make1(makeString("!~~*")); }
14367 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
14368  * the regular expression is preprocessed by a function (similar_escape),
14369  * and the ~ operator for posix regular expressions is used.
14370  *        x SIMILAR TO y     ->    x ~ similar_escape(y)
14371  * this transformation is made on the fly by the parser upwards.
14372  * however the SubLink structure which handles any/some/all stuff
14373  * is not ready for such a thing.
14374  */
14375 			;
14376 
14377 expr_list:	a_expr
14378 				{
14379 					$$ = list_make1($1);
14380 				}
14381 			| expr_list ',' a_expr
14382 				{
14383 					$$ = lappend($1, $3);
14384 				}
14385 		;
14386 
14387 /* function arguments can have names */
14388 func_arg_list:  func_arg_expr
14389 				{
14390 					$$ = list_make1($1);
14391 				}
14392 			| func_arg_list ',' func_arg_expr
14393 				{
14394 					$$ = lappend($1, $3);
14395 				}
14396 		;
14397 
14398 func_arg_expr:  a_expr
14399 				{
14400 					$$ = $1;
14401 				}
14402 			| param_name COLON_EQUALS a_expr
14403 				{
14404 					NamedArgExpr *na = makeNode(NamedArgExpr);
14405 					na->name = $1;
14406 					na->arg = (Expr *) $3;
14407 					na->argnumber = -1;		/* until determined */
14408 					na->location = @1;
14409 					$$ = (Node *) na;
14410 				}
14411 			| param_name EQUALS_GREATER a_expr
14412 				{
14413 					NamedArgExpr *na = makeNode(NamedArgExpr);
14414 					na->name = $1;
14415 					na->arg = (Expr *) $3;
14416 					na->argnumber = -1;		/* until determined */
14417 					na->location = @1;
14418 					$$ = (Node *) na;
14419 				}
14420 		;
14421 
14422 type_list:	Typename								{ $$ = list_make1($1); }
14423 			| type_list ',' Typename				{ $$ = lappend($1, $3); }
14424 		;
14425 
14426 array_expr: '[' expr_list ']'
14427 				{
14428 					$$ = makeAArrayExpr($2, @1);
14429 				}
14430 			| '[' array_expr_list ']'
14431 				{
14432 					$$ = makeAArrayExpr($2, @1);
14433 				}
14434 			| '[' ']'
14435 				{
14436 					$$ = makeAArrayExpr(NIL, @1);
14437 				}
14438 		;
14439 
14440 array_expr_list: array_expr							{ $$ = list_make1($1); }
14441 			| array_expr_list ',' array_expr		{ $$ = lappend($1, $3); }
14442 		;
14443 
14444 
14445 extract_list:
14446 			extract_arg FROM a_expr
14447 				{
14448 					$$ = list_make2(makeStringConst($1, @1), $3);
14449 				}
14450 			| /*EMPTY*/								{ $$ = NIL; }
14451 		;
14452 
14453 /* Allow delimited string Sconst in extract_arg as an SQL extension.
14454  * - thomas 2001-04-12
14455  */
14456 extract_arg:
14457 			IDENT									{ $$ = $1; }
14458 			| YEAR_P								{ $$ = "year"; }
14459 			| MONTH_P								{ $$ = "month"; }
14460 			| DAY_P									{ $$ = "day"; }
14461 			| HOUR_P								{ $$ = "hour"; }
14462 			| MINUTE_P								{ $$ = "minute"; }
14463 			| SECOND_P								{ $$ = "second"; }
14464 			| Sconst								{ $$ = $1; }
14465 		;
14466 
14467 /* OVERLAY() arguments
14468  * SQL99 defines the OVERLAY() function:
14469  * o overlay(text placing text from int for int)
14470  * o overlay(text placing text from int)
14471  * and similarly for binary strings
14472  */
14473 overlay_list:
14474 			a_expr overlay_placing substr_from substr_for
14475 				{
14476 					$$ = list_make4($1, $2, $3, $4);
14477 				}
14478 			| a_expr overlay_placing substr_from
14479 				{
14480 					$$ = list_make3($1, $2, $3);
14481 				}
14482 		;
14483 
14484 overlay_placing:
14485 			PLACING a_expr
14486 				{ $$ = $2; }
14487 		;
14488 
14489 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
14490 
14491 position_list:
14492 			b_expr IN_P b_expr						{ $$ = list_make2($3, $1); }
14493 			| /*EMPTY*/								{ $$ = NIL; }
14494 		;
14495 
14496 /* SUBSTRING() arguments
14497  * SQL9x defines a specific syntax for arguments to SUBSTRING():
14498  * o substring(text from int for int)
14499  * o substring(text from int) get entire string from starting point "int"
14500  * o substring(text for int) get first "int" characters of string
14501  * o substring(text from pattern) get entire string matching pattern
14502  * o substring(text from pattern for escape) same with specified escape char
14503  * We also want to support generic substring functions which accept
14504  * the usual generic list of arguments. So we will accept both styles
14505  * here, and convert the SQL9x style to the generic list for further
14506  * processing. - thomas 2000-11-28
14507  */
14508 substr_list:
14509 			a_expr substr_from substr_for
14510 				{
14511 					$$ = list_make3($1, $2, $3);
14512 				}
14513 			| a_expr substr_for substr_from
14514 				{
14515 					/* not legal per SQL99, but might as well allow it */
14516 					$$ = list_make3($1, $3, $2);
14517 				}
14518 			| a_expr substr_from
14519 				{
14520 					$$ = list_make2($1, $2);
14521 				}
14522 			| a_expr substr_for
14523 				{
14524 					/*
14525 					 * Since there are no cases where this syntax allows
14526 					 * a textual FOR value, we forcibly cast the argument
14527 					 * to int4.  The possible matches in pg_proc are
14528 					 * substring(text,int4) and substring(text,text),
14529 					 * and we don't want the parser to choose the latter,
14530 					 * which it is likely to do if the second argument
14531 					 * is unknown or doesn't have an implicit cast to int4.
14532 					 */
14533 					$$ = list_make3($1, makeIntConst(1, -1),
14534 									makeTypeCast($2,
14535 												 SystemTypeName("int4"), -1));
14536 				}
14537 			| expr_list
14538 				{
14539 					$$ = $1;
14540 				}
14541 			| /*EMPTY*/
14542 				{ $$ = NIL; }
14543 		;
14544 
14545 substr_from:
14546 			FROM a_expr								{ $$ = $2; }
14547 		;
14548 
14549 substr_for: FOR a_expr								{ $$ = $2; }
14550 		;
14551 
14552 trim_list:	a_expr FROM expr_list					{ $$ = lappend($3, $1); }
14553 			| FROM expr_list						{ $$ = $2; }
14554 			| expr_list								{ $$ = $1; }
14555 		;
14556 
14557 in_expr:	select_with_parens
14558 				{
14559 					SubLink *n = makeNode(SubLink);
14560 					n->subselect = $1;
14561 					/* other fields will be filled later */
14562 					$$ = (Node *)n;
14563 				}
14564 			| '(' expr_list ')'						{ $$ = (Node *)$2; }
14565 		;
14566 
14567 /*
14568  * Define SQL-style CASE clause.
14569  * - Full specification
14570  *	CASE WHEN a = b THEN c ... ELSE d END
14571  * - Implicit argument
14572  *	CASE a WHEN b THEN c ... ELSE d END
14573  */
14574 case_expr:	CASE case_arg when_clause_list case_default END_P
14575 				{
14576 					CaseExpr *c = makeNode(CaseExpr);
14577 					c->casetype = InvalidOid; /* not analyzed yet */
14578 					c->arg = (Expr *) $2;
14579 					c->args = $3;
14580 					c->defresult = (Expr *) $4;
14581 					c->location = @1;
14582 					$$ = (Node *)c;
14583 				}
14584 		;
14585 
14586 when_clause_list:
14587 			/* There must be at least one */
14588 			when_clause								{ $$ = list_make1($1); }
14589 			| when_clause_list when_clause			{ $$ = lappend($1, $2); }
14590 		;
14591 
14592 when_clause:
14593 			WHEN a_expr THEN a_expr
14594 				{
14595 					CaseWhen *w = makeNode(CaseWhen);
14596 					w->expr = (Expr *) $2;
14597 					w->result = (Expr *) $4;
14598 					w->location = @1;
14599 					$$ = (Node *)w;
14600 				}
14601 		;
14602 
14603 case_default:
14604 			ELSE a_expr								{ $$ = $2; }
14605 			| /*EMPTY*/								{ $$ = NULL; }
14606 		;
14607 
14608 case_arg:	a_expr									{ $$ = $1; }
14609 			| /*EMPTY*/								{ $$ = NULL; }
14610 		;
14611 
14612 columnref:	ColId
14613 				{
14614 					$$ = makeColumnRef($1, NIL, @1, yyscanner);
14615 				}
14616 			| ColId indirection
14617 				{
14618 					$$ = makeColumnRef($1, $2, @1, yyscanner);
14619 				}
14620 		;
14621 
14622 indirection_el:
14623 			'.' attr_name
14624 				{
14625 					$$ = (Node *) makeString($2);
14626 				}
14627 			| '.' '*'
14628 				{
14629 					$$ = (Node *) makeNode(A_Star);
14630 				}
14631 			| '[' a_expr ']'
14632 				{
14633 					A_Indices *ai = makeNode(A_Indices);
14634 					ai->is_slice = false;
14635 					ai->lidx = NULL;
14636 					ai->uidx = $2;
14637 					$$ = (Node *) ai;
14638 				}
14639 			| '[' opt_slice_bound ':' opt_slice_bound ']'
14640 				{
14641 					A_Indices *ai = makeNode(A_Indices);
14642 					ai->is_slice = true;
14643 					ai->lidx = $2;
14644 					ai->uidx = $4;
14645 					$$ = (Node *) ai;
14646 				}
14647 		;
14648 
14649 opt_slice_bound:
14650 			a_expr									{ $$ = $1; }
14651 			| /*EMPTY*/								{ $$ = NULL; }
14652 		;
14653 
14654 indirection:
14655 			indirection_el							{ $$ = list_make1($1); }
14656 			| indirection indirection_el			{ $$ = lappend($1, $2); }
14657 		;
14658 
14659 opt_indirection:
14660 			/*EMPTY*/								{ $$ = NIL; }
14661 			| opt_indirection indirection_el		{ $$ = lappend($1, $2); }
14662 		;
14663 
14664 opt_asymmetric: ASYMMETRIC
14665 			| /*EMPTY*/
14666 		;
14667 
14668 
14669 /*****************************************************************************
14670  *
14671  *	target list for SELECT
14672  *
14673  *****************************************************************************/
14674 
14675 opt_target_list: target_list						{ $$ = $1; }
14676 			| /* EMPTY */							{ $$ = NIL; }
14677 		;
14678 
14679 target_list:
14680 			target_el								{ $$ = list_make1($1); }
14681 			| target_list ',' target_el				{ $$ = lappend($1, $3); }
14682 		;
14683 
14684 target_el:	a_expr AS ColLabel
14685 				{
14686 					$$ = makeNode(ResTarget);
14687 					$$->name = $3;
14688 					$$->indirection = NIL;
14689 					$$->val = (Node *)$1;
14690 					$$->location = @1;
14691 				}
14692 			/*
14693 			 * We support omitting AS only for column labels that aren't
14694 			 * any known keyword.  There is an ambiguity against postfix
14695 			 * operators: is "a ! b" an infix expression, or a postfix
14696 			 * expression and a column label?  We prefer to resolve this
14697 			 * as an infix expression, which we accomplish by assigning
14698 			 * IDENT a precedence higher than POSTFIXOP.
14699 			 */
14700 			| a_expr IDENT
14701 				{
14702 					$$ = makeNode(ResTarget);
14703 					$$->name = $2;
14704 					$$->indirection = NIL;
14705 					$$->val = (Node *)$1;
14706 					$$->location = @1;
14707 				}
14708 			| a_expr
14709 				{
14710 					$$ = makeNode(ResTarget);
14711 					$$->name = NULL;
14712 					$$->indirection = NIL;
14713 					$$->val = (Node *)$1;
14714 					$$->location = @1;
14715 				}
14716 			| '*'
14717 				{
14718 					ColumnRef *n = makeNode(ColumnRef);
14719 					n->fields = list_make1(makeNode(A_Star));
14720 					n->location = @1;
14721 
14722 					$$ = makeNode(ResTarget);
14723 					$$->name = NULL;
14724 					$$->indirection = NIL;
14725 					$$->val = (Node *)n;
14726 					$$->location = @1;
14727 				}
14728 		;
14729 
14730 
14731 /*****************************************************************************
14732  *
14733  *	Names and constants
14734  *
14735  *****************************************************************************/
14736 
14737 qualified_name_list:
14738 			qualified_name							{ $$ = list_make1($1); }
14739 			| qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
14740 		;
14741 
14742 /*
14743  * The production for a qualified relation name has to exactly match the
14744  * production for a qualified func_name, because in a FROM clause we cannot
14745  * tell which we are parsing until we see what comes after it ('(' for a
14746  * func_name, something else for a relation). Therefore we allow 'indirection'
14747  * which may contain subscripts, and reject that case in the C code.
14748  */
14749 qualified_name:
14750 			ColId
14751 				{
14752 					$$ = makeRangeVar(NULL, $1, @1);
14753 				}
14754 			| ColId indirection
14755 				{
14756 					check_qualified_name($2, yyscanner);
14757 					$$ = makeRangeVar(NULL, NULL, @1);
14758 					switch (list_length($2))
14759 					{
14760 						case 1:
14761 							$$->catalogname = NULL;
14762 							$$->schemaname = $1;
14763 							$$->relname = strVal(linitial($2));
14764 							break;
14765 						case 2:
14766 							$$->catalogname = $1;
14767 							$$->schemaname = strVal(linitial($2));
14768 							$$->relname = strVal(lsecond($2));
14769 							break;
14770 						default:
14771 							ereport(ERROR,
14772 									(errcode(ERRCODE_SYNTAX_ERROR),
14773 									 errmsg("improper qualified name (too many dotted names): %s",
14774 											NameListToString(lcons(makeString($1), $2))),
14775 									 parser_errposition(@1)));
14776 							break;
14777 					}
14778 				}
14779 		;
14780 
14781 name_list:	name
14782 					{ $$ = list_make1(makeString($1)); }
14783 			| name_list ',' name
14784 					{ $$ = lappend($1, makeString($3)); }
14785 		;
14786 
14787 
14788 name:		ColId									{ $$ = $1; };
14789 
14790 database_name:
14791 			ColId									{ $$ = $1; };
14792 
14793 access_method:
14794 			ColId									{ $$ = $1; };
14795 
14796 attr_name:	ColLabel								{ $$ = $1; };
14797 
14798 index_name: ColId									{ $$ = $1; };
14799 
14800 file_name:	Sconst									{ $$ = $1; };
14801 
14802 /*
14803  * The production for a qualified func_name has to exactly match the
14804  * production for a qualified columnref, because we cannot tell which we
14805  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
14806  * anything else for a columnref).  Therefore we allow 'indirection' which
14807  * may contain subscripts, and reject that case in the C code.  (If we
14808  * ever implement SQL99-like methods, such syntax may actually become legal!)
14809  */
14810 func_name:	type_function_name
14811 					{ $$ = list_make1(makeString($1)); }
14812 			| ColId indirection
14813 					{
14814 						$$ = check_func_name(lcons(makeString($1), $2),
14815 											 yyscanner);
14816 					}
14817 		;
14818 
14819 
14820 /*
14821  * Constants
14822  */
14823 AexprConst: Iconst
14824 				{
14825 					$$ = makeIntConst($1, @1);
14826 				}
14827 			| FCONST
14828 				{
14829 					$$ = makeFloatConst($1, @1);
14830 				}
14831 			| Sconst
14832 				{
14833 					$$ = makeStringConst($1, @1);
14834 				}
14835 			| BCONST
14836 				{
14837 					$$ = makeBitStringConst($1, @1);
14838 				}
14839 			| XCONST
14840 				{
14841 					/* This is a bit constant per SQL99:
14842 					 * Without Feature F511, "BIT data type",
14843 					 * a <general literal> shall not be a
14844 					 * <bit string literal> or a <hex string literal>.
14845 					 */
14846 					$$ = makeBitStringConst($1, @1);
14847 				}
14848 			| func_name Sconst
14849 				{
14850 					/* generic type 'literal' syntax */
14851 					TypeName *t = makeTypeNameFromNameList($1);
14852 					t->location = @1;
14853 					$$ = makeStringConstCast($2, @2, t);
14854 				}
14855 			| func_name '(' func_arg_list opt_sort_clause ')' Sconst
14856 				{
14857 					/* generic syntax with a type modifier */
14858 					TypeName *t = makeTypeNameFromNameList($1);
14859 					ListCell *lc;
14860 
14861 					/*
14862 					 * We must use func_arg_list and opt_sort_clause in the
14863 					 * production to avoid reduce/reduce conflicts, but we
14864 					 * don't actually wish to allow NamedArgExpr in this
14865 					 * context, nor ORDER BY.
14866 					 */
foreach(lc,$3)14867 					foreach(lc, $3)
14868 					{
14869 						NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
14870 
14871 						if (IsA(arg, NamedArgExpr))
14872 							ereport(ERROR,
14873 									(errcode(ERRCODE_SYNTAX_ERROR),
14874 									 errmsg("type modifier cannot have parameter name"),
14875 									 parser_errposition(arg->location)));
14876 					}
14877 					if ($4 != NIL)
14878 							ereport(ERROR,
14879 									(errcode(ERRCODE_SYNTAX_ERROR),
14880 									 errmsg("type modifier cannot have ORDER BY"),
14881 									 parser_errposition(@4)));
14882 
14883 					t->typmods = $3;
14884 					t->location = @1;
14885 					$$ = makeStringConstCast($6, @6, t);
14886 				}
14887 			| ConstTypename Sconst
14888 				{
14889 					$$ = makeStringConstCast($2, @2, $1);
14890 				}
14891 			| ConstInterval Sconst opt_interval
14892 				{
14893 					TypeName *t = $1;
14894 					t->typmods = $3;
14895 					$$ = makeStringConstCast($2, @2, t);
14896 				}
14897 			| ConstInterval '(' Iconst ')' Sconst
14898 				{
14899 					TypeName *t = $1;
14900 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
14901 											makeIntConst($3, @3));
14902 					$$ = makeStringConstCast($5, @5, t);
14903 				}
14904 			| TRUE_P
14905 				{
14906 					$$ = makeBoolAConst(true, @1);
14907 				}
14908 			| FALSE_P
14909 				{
14910 					$$ = makeBoolAConst(false, @1);
14911 				}
14912 			| NULL_P
14913 				{
14914 					$$ = makeNullAConst(@1);
14915 				}
14916 		;
14917 
14918 Iconst:		ICONST									{ $$ = $1; };
14919 Sconst:		SCONST									{ $$ = $1; };
14920 
14921 SignedIconst: Iconst								{ $$ = $1; }
14922 			| '+' Iconst							{ $$ = + $2; }
14923 			| '-' Iconst							{ $$ = - $2; }
14924 		;
14925 
14926 /* Role specifications */
14927 RoleId:		RoleSpec
14928 				{
14929 					RoleSpec *spc = (RoleSpec *) $1;
14930 					switch (spc->roletype)
14931 					{
14932 						case ROLESPEC_CSTRING:
14933 							$$ = spc->rolename;
14934 							break;
14935 						case ROLESPEC_PUBLIC:
14936 							ereport(ERROR,
14937 									(errcode(ERRCODE_RESERVED_NAME),
14938 									 errmsg("role name \"%s\" is reserved",
14939 											"public"),
14940 									 parser_errposition(@1)));
14941 							break;
14942 						case ROLESPEC_SESSION_USER:
14943 							ereport(ERROR,
14944 									(errcode(ERRCODE_RESERVED_NAME),
14945 									 errmsg("%s cannot be used as a role name here",
14946 											"SESSION_USER"),
14947 									 parser_errposition(@1)));
14948 							break;
14949 						case ROLESPEC_CURRENT_USER:
14950 							ereport(ERROR,
14951 									(errcode(ERRCODE_RESERVED_NAME),
14952 									 errmsg("%s cannot be used as a role name here",
14953 											"CURRENT_USER"),
14954 									 parser_errposition(@1)));
14955 							break;
14956 					}
14957 				}
14958 			;
14959 
14960 RoleSpec:	NonReservedWord
14961 					{
14962 						/*
14963 						 * "public" and "none" are not keywords, but they must
14964 						 * be treated specially here.
14965 						 */
14966 						RoleSpec *n;
14967 						if (strcmp($1, "public") == 0)
14968 						{
14969 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_PUBLIC, @1);
14970 							n->roletype = ROLESPEC_PUBLIC;
14971 						}
14972 						else if (strcmp($1, "none") == 0)
14973 						{
14974 							ereport(ERROR,
14975 									(errcode(ERRCODE_RESERVED_NAME),
14976 									 errmsg("role name \"%s\" is reserved",
14977 											"none"),
14978 									 parser_errposition(@1)));
14979 						}
14980 						else
14981 						{
14982 							n = makeRoleSpec(ROLESPEC_CSTRING, @1);
14983 							n->rolename = pstrdup($1);
14984 						}
14985 						$$ = n;
14986 					}
14987 			| CURRENT_USER
14988 					{
14989 						$$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1);
14990 					}
14991 			| SESSION_USER
14992 					{
14993 						$$ = makeRoleSpec(ROLESPEC_SESSION_USER, @1);
14994 					}
14995 		;
14996 
14997 role_list:	RoleSpec
14998 					{ $$ = list_make1($1); }
14999 			| role_list ',' RoleSpec
15000 					{ $$ = lappend($1, $3); }
15001 		;
15002 
15003 /*
15004  * Name classification hierarchy.
15005  *
15006  * IDENT is the lexeme returned by the lexer for identifiers that match
15007  * no known keyword.  In most cases, we can accept certain keywords as
15008  * names, not only IDENTs.	We prefer to accept as many such keywords
15009  * as possible to minimize the impact of "reserved words" on programmers.
15010  * So, we divide names into several possible classes.  The classification
15011  * is chosen in part to make keywords acceptable as names wherever possible.
15012  */
15013 
15014 /* Column identifier --- names that can be column, table, etc names.
15015  */
15016 ColId:		IDENT									{ $$ = $1; }
15017 			| unreserved_keyword					{ $$ = pstrdup($1); }
15018 			| col_name_keyword						{ $$ = pstrdup($1); }
15019 		;
15020 
15021 /* Type/function identifier --- names that can be type or function names.
15022  */
15023 type_function_name:	IDENT							{ $$ = $1; }
15024 			| unreserved_keyword					{ $$ = pstrdup($1); }
15025 			| type_func_name_keyword				{ $$ = pstrdup($1); }
15026 		;
15027 
15028 /* Any not-fully-reserved word --- these names can be, eg, role names.
15029  */
15030 NonReservedWord:	IDENT							{ $$ = $1; }
15031 			| unreserved_keyword					{ $$ = pstrdup($1); }
15032 			| col_name_keyword						{ $$ = pstrdup($1); }
15033 			| type_func_name_keyword				{ $$ = pstrdup($1); }
15034 		;
15035 
15036 /* Column label --- allowed labels in "AS" clauses.
15037  * This presently includes *all* Postgres keywords.
15038  */
15039 ColLabel:	IDENT									{ $$ = $1; }
15040 			| unreserved_keyword					{ $$ = pstrdup($1); }
15041 			| col_name_keyword						{ $$ = pstrdup($1); }
15042 			| type_func_name_keyword				{ $$ = pstrdup($1); }
15043 			| reserved_keyword						{ $$ = pstrdup($1); }
15044 		;
15045 
15046 
15047 /*
15048  * Keyword category lists.  Generally, every keyword present in
15049  * the Postgres grammar should appear in exactly one of these lists.
15050  *
15051  * Put a new keyword into the first list that it can go into without causing
15052  * shift or reduce conflicts.  The earlier lists define "less reserved"
15053  * categories of keywords.
15054  *
15055  * Make sure that each keyword's category in kwlist.h matches where
15056  * it is listed here.  (Someday we may be able to generate these lists and
15057  * kwlist.h's table from a common master list.)
15058  */
15059 
15060 /* "Unreserved" keywords --- available for use as any kind of name.
15061  */
15062 unreserved_keyword:
15063 			  ABORT_P
15064 			| ABSOLUTE_P
15065 			| ACCESS
15066 			| ACTION
15067 			| ADD_P
15068 			| ADMIN
15069 			| AFTER
15070 			| AGGREGATE
15071 			| ALSO
15072 			| ALTER
15073 			| ALWAYS
15074 			| ASSERTION
15075 			| ASSIGNMENT
15076 			| AT
15077 			| ATTACH
15078 			| ATTRIBUTE
15079 			| BACKWARD
15080 			| BEFORE
15081 			| BEGIN_P
15082 			| BY
15083 			| CACHE
15084 			| CALL
15085 			| CALLED
15086 			| CASCADE
15087 			| CASCADED
15088 			| CATALOG_P
15089 			| CHAIN
15090 			| CHARACTERISTICS
15091 			| CHECKPOINT
15092 			| CLASS
15093 			| CLOSE
15094 			| CLUSTER
15095 			| COLUMNS
15096 			| COMMENT
15097 			| COMMENTS
15098 			| COMMIT
15099 			| COMMITTED
15100 			| CONFIGURATION
15101 			| CONFLICT
15102 			| CONNECTION
15103 			| CONSTRAINTS
15104 			| CONTENT_P
15105 			| CONTINUE_P
15106 			| CONVERSION_P
15107 			| COPY
15108 			| COST
15109 			| CSV
15110 			| CUBE
15111 			| CURRENT_P
15112 			| CURSOR
15113 			| CYCLE
15114 			| DATA_P
15115 			| DATABASE
15116 			| DAY_P
15117 			| DEALLOCATE
15118 			| DECLARE
15119 			| DEFAULTS
15120 			| DEFERRED
15121 			| DEFINER
15122 			| DELETE_P
15123 			| DELIMITER
15124 			| DELIMITERS
15125 			| DEPENDS
15126 			| DETACH
15127 			| DICTIONARY
15128 			| DISABLE_P
15129 			| DISCARD
15130 			| DOCUMENT_P
15131 			| DOMAIN_P
15132 			| DOUBLE_P
15133 			| DROP
15134 			| EACH
15135 			| ENABLE_P
15136 			| ENCODING
15137 			| ENCRYPTED
15138 			| ENUM_P
15139 			| ESCAPE
15140 			| EVENT
15141 			| EXCLUDE
15142 			| EXCLUDING
15143 			| EXCLUSIVE
15144 			| EXECUTE
15145 			| EXPLAIN
15146 			| EXTENSION
15147 			| EXTERNAL
15148 			| FAMILY
15149 			| FILTER
15150 			| FIRST_P
15151 			| FOLLOWING
15152 			| FORCE
15153 			| FORWARD
15154 			| FUNCTION
15155 			| FUNCTIONS
15156 			| GENERATED
15157 			| GLOBAL
15158 			| GRANTED
15159 			| GROUPS
15160 			| HANDLER
15161 			| HEADER_P
15162 			| HOLD
15163 			| HOUR_P
15164 			| IDENTITY_P
15165 			| IF_P
15166 			| IMMEDIATE
15167 			| IMMUTABLE
15168 			| IMPLICIT_P
15169 			| IMPORT_P
15170 			| INCLUDE
15171 			| INCLUDING
15172 			| INCREMENT
15173 			| INDEX
15174 			| INDEXES
15175 			| INHERIT
15176 			| INHERITS
15177 			| INLINE_P
15178 			| INPUT_P
15179 			| INSENSITIVE
15180 			| INSERT
15181 			| INSTEAD
15182 			| INVOKER
15183 			| ISOLATION
15184 			| KEY
15185 			| LABEL
15186 			| LANGUAGE
15187 			| LARGE_P
15188 			| LAST_P
15189 			| LEAKPROOF
15190 			| LEVEL
15191 			| LISTEN
15192 			| LOAD
15193 			| LOCAL
15194 			| LOCATION
15195 			| LOCK_P
15196 			| LOCKED
15197 			| LOGGED
15198 			| MAPPING
15199 			| MATCH
15200 			| MATERIALIZED
15201 			| MAXVALUE
15202 			| METHOD
15203 			| MINUTE_P
15204 			| MINVALUE
15205 			| MODE
15206 			| MONTH_P
15207 			| MOVE
15208 			| NAME_P
15209 			| NAMES
15210 			| NEW
15211 			| NEXT
15212 			| NO
15213 			| NOTHING
15214 			| NOTIFY
15215 			| NOWAIT
15216 			| NULLS_P
15217 			| OBJECT_P
15218 			| OF
15219 			| OFF
15220 			| OIDS
15221 			| OLD
15222 			| OPERATOR
15223 			| OPTION
15224 			| OPTIONS
15225 			| ORDINALITY
15226 			| OTHERS
15227 			| OVER
15228 			| OVERRIDING
15229 			| OWNED
15230 			| OWNER
15231 			| PARALLEL
15232 			| PARSER
15233 			| PARTIAL
15234 			| PARTITION
15235 			| PASSING
15236 			| PASSWORD
15237 			| PLANS
15238 			| POLICY
15239 			| PRECEDING
15240 			| PREPARE
15241 			| PREPARED
15242 			| PRESERVE
15243 			| PRIOR
15244 			| PRIVILEGES
15245 			| PROCEDURAL
15246 			| PROCEDURE
15247 			| PROCEDURES
15248 			| PROGRAM
15249 			| PUBLICATION
15250 			| QUOTE
15251 			| RANGE
15252 			| READ
15253 			| REASSIGN
15254 			| RECHECK
15255 			| RECURSIVE
15256 			| REF
15257 			| REFERENCING
15258 			| REFRESH
15259 			| REINDEX
15260 			| RELATIVE_P
15261 			| RELEASE
15262 			| RENAME
15263 			| REPEATABLE
15264 			| REPLACE
15265 			| REPLICA
15266 			| RESET
15267 			| RESTART
15268 			| RESTRICT
15269 			| RETURNS
15270 			| REVOKE
15271 			| ROLE
15272 			| ROLLBACK
15273 			| ROLLUP
15274 			| ROUTINE
15275 			| ROUTINES
15276 			| ROWS
15277 			| RULE
15278 			| SAVEPOINT
15279 			| SCHEMA
15280 			| SCHEMAS
15281 			| SCROLL
15282 			| SEARCH
15283 			| SECOND_P
15284 			| SECURITY
15285 			| SEQUENCE
15286 			| SEQUENCES
15287 			| SERIALIZABLE
15288 			| SERVER
15289 			| SESSION
15290 			| SET
15291 			| SETS
15292 			| SHARE
15293 			| SHOW
15294 			| SIMPLE
15295 			| SKIP
15296 			| SNAPSHOT
15297 			| SQL_P
15298 			| STABLE
15299 			| STANDALONE_P
15300 			| START
15301 			| STATEMENT
15302 			| STATISTICS
15303 			| STDIN
15304 			| STDOUT
15305 			| STORAGE
15306 			| STRICT_P
15307 			| STRIP_P
15308 			| SUBSCRIPTION
15309 			| SYSID
15310 			| SYSTEM_P
15311 			| TABLES
15312 			| TABLESPACE
15313 			| TEMP
15314 			| TEMPLATE
15315 			| TEMPORARY
15316 			| TEXT_P
15317 			| TIES
15318 			| TRANSACTION
15319 			| TRANSFORM
15320 			| TRIGGER
15321 			| TRUNCATE
15322 			| TRUSTED
15323 			| TYPE_P
15324 			| TYPES_P
15325 			| UNBOUNDED
15326 			| UNCOMMITTED
15327 			| UNENCRYPTED
15328 			| UNKNOWN
15329 			| UNLISTEN
15330 			| UNLOGGED
15331 			| UNTIL
15332 			| UPDATE
15333 			| VACUUM
15334 			| VALID
15335 			| VALIDATE
15336 			| VALIDATOR
15337 			| VALUE_P
15338 			| VARYING
15339 			| VERSION_P
15340 			| VIEW
15341 			| VIEWS
15342 			| VOLATILE
15343 			| WHITESPACE_P
15344 			| WITHIN
15345 			| WITHOUT
15346 			| WORK
15347 			| WRAPPER
15348 			| WRITE
15349 			| XML_P
15350 			| YEAR_P
15351 			| YES_P
15352 			| ZONE
15353 		;
15354 
15355 /* Column identifier --- keywords that can be column, table, etc names.
15356  *
15357  * Many of these keywords will in fact be recognized as type or function
15358  * names too; but they have special productions for the purpose, and so
15359  * can't be treated as "generic" type or function names.
15360  *
15361  * The type names appearing here are not usable as function names
15362  * because they can be followed by '(' in typename productions, which
15363  * looks too much like a function call for an LR(1) parser.
15364  */
15365 col_name_keyword:
15366 			  BETWEEN
15367 			| BIGINT
15368 			| BIT
15369 			| BOOLEAN_P
15370 			| CHAR_P
15371 			| CHARACTER
15372 			| COALESCE
15373 			| DEC
15374 			| DECIMAL_P
15375 			| EXISTS
15376 			| EXTRACT
15377 			| FLOAT_P
15378 			| GREATEST
15379 			| GROUPING
15380 			| INOUT
15381 			| INT_P
15382 			| INTEGER
15383 			| INTERVAL
15384 			| LEAST
15385 			| NATIONAL
15386 			| NCHAR
15387 			| NONE
15388 			| NULLIF
15389 			| NUMERIC
15390 			| OUT_P
15391 			| OVERLAY
15392 			| POSITION
15393 			| PRECISION
15394 			| REAL
15395 			| ROW
15396 			| SETOF
15397 			| SMALLINT
15398 			| SUBSTRING
15399 			| TIME
15400 			| TIMESTAMP
15401 			| TREAT
15402 			| TRIM
15403 			| VALUES
15404 			| VARCHAR
15405 			| XMLATTRIBUTES
15406 			| XMLCONCAT
15407 			| XMLELEMENT
15408 			| XMLEXISTS
15409 			| XMLFOREST
15410 			| XMLNAMESPACES
15411 			| XMLPARSE
15412 			| XMLPI
15413 			| XMLROOT
15414 			| XMLSERIALIZE
15415 			| XMLTABLE
15416 		;
15417 
15418 /* Type/function identifier --- keywords that can be type or function names.
15419  *
15420  * Most of these are keywords that are used as operators in expressions;
15421  * in general such keywords can't be column names because they would be
15422  * ambiguous with variables, but they are unambiguous as function identifiers.
15423  *
15424  * Do not include POSITION, SUBSTRING, etc here since they have explicit
15425  * productions in a_expr to support the goofy SQL9x argument syntax.
15426  * - thomas 2000-11-28
15427  */
15428 type_func_name_keyword:
15429 			  AUTHORIZATION
15430 			| BINARY
15431 			| COLLATION
15432 			| CONCURRENTLY
15433 			| CROSS
15434 			| CURRENT_SCHEMA
15435 			| FREEZE
15436 			| FULL
15437 			| ILIKE
15438 			| INNER_P
15439 			| IS
15440 			| ISNULL
15441 			| JOIN
15442 			| LEFT
15443 			| LIKE
15444 			| NATURAL
15445 			| NOTNULL
15446 			| OUTER_P
15447 			| OVERLAPS
15448 			| RIGHT
15449 			| SIMILAR
15450 			| TABLESAMPLE
15451 			| VERBOSE
15452 		;
15453 
15454 /* Reserved keyword --- these keywords are usable only as a ColLabel.
15455  *
15456  * Keywords appear here if they could not be distinguished from variable,
15457  * type, or function names in some contexts.  Don't put things here unless
15458  * forced to.
15459  */
15460 reserved_keyword:
15461 			  ALL
15462 			| ANALYSE
15463 			| ANALYZE
15464 			| AND
15465 			| ANY
15466 			| ARRAY
15467 			| AS
15468 			| ASC
15469 			| ASYMMETRIC
15470 			| BOTH
15471 			| CASE
15472 			| CAST
15473 			| CHECK
15474 			| COLLATE
15475 			| COLUMN
15476 			| CONSTRAINT
15477 			| CREATE
15478 			| CURRENT_CATALOG
15479 			| CURRENT_DATE
15480 			| CURRENT_ROLE
15481 			| CURRENT_TIME
15482 			| CURRENT_TIMESTAMP
15483 			| CURRENT_USER
15484 			| DEFAULT
15485 			| DEFERRABLE
15486 			| DESC
15487 			| DISTINCT
15488 			| DO
15489 			| ELSE
15490 			| END_P
15491 			| EXCEPT
15492 			| FALSE_P
15493 			| FETCH
15494 			| FOR
15495 			| FOREIGN
15496 			| FROM
15497 			| GRANT
15498 			| GROUP_P
15499 			| HAVING
15500 			| IN_P
15501 			| INITIALLY
15502 			| INTERSECT
15503 			| INTO
15504 			| LATERAL_P
15505 			| LEADING
15506 			| LIMIT
15507 			| LOCALTIME
15508 			| LOCALTIMESTAMP
15509 			| NOT
15510 			| NULL_P
15511 			| OFFSET
15512 			| ON
15513 			| ONLY
15514 			| OR
15515 			| ORDER
15516 			| PLACING
15517 			| PRIMARY
15518 			| REFERENCES
15519 			| RETURNING
15520 			| SELECT
15521 			| SESSION_USER
15522 			| SOME
15523 			| SYMMETRIC
15524 			| TABLE
15525 			| THEN
15526 			| TO
15527 			| TRAILING
15528 			| TRUE_P
15529 			| UNION
15530 			| UNIQUE
15531 			| USER
15532 			| USING
15533 			| VARIADIC
15534 			| WHEN
15535 			| WHERE
15536 			| WINDOW
15537 			| WITH
15538 		;
15539 
15540 %%
15541 
15542 /*
15543  * The signature of this function is required by bison.  However, we
15544  * ignore the passed yylloc and instead use the last token position
15545  * available from the scanner.
15546  */
15547 static void
15548 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
15549 {
15550 	parser_yyerror(msg);
15551 }
15552 
15553 static RawStmt *
makeRawStmt(Node * stmt,int stmt_location)15554 makeRawStmt(Node *stmt, int stmt_location)
15555 {
15556 	RawStmt    *rs = makeNode(RawStmt);
15557 
15558 	rs->stmt = stmt;
15559 	rs->stmt_location = stmt_location;
15560 	rs->stmt_len = 0;			/* might get changed later */
15561 	return rs;
15562 }
15563 
15564 /* Adjust a RawStmt to reflect that it doesn't run to the end of the string */
15565 static void
updateRawStmtEnd(RawStmt * rs,int end_location)15566 updateRawStmtEnd(RawStmt *rs, int end_location)
15567 {
15568 	/*
15569 	 * If we already set the length, don't change it.  This is for situations
15570 	 * like "select foo ;; select bar" where the same statement will be last
15571 	 * in the string for more than one semicolon.
15572 	 */
15573 	if (rs->stmt_len > 0)
15574 		return;
15575 
15576 	/* OK, update length of RawStmt */
15577 	rs->stmt_len = end_location - rs->stmt_location;
15578 }
15579 
15580 static Node *
makeColumnRef(char * colname,List * indirection,int location,core_yyscan_t yyscanner)15581 makeColumnRef(char *colname, List *indirection,
15582 			  int location, core_yyscan_t yyscanner)
15583 {
15584 	/*
15585 	 * Generate a ColumnRef node, with an A_Indirection node added if there
15586 	 * is any subscripting in the specified indirection list.  However,
15587 	 * any field selection at the start of the indirection list must be
15588 	 * transposed into the "fields" part of the ColumnRef node.
15589 	 */
15590 	ColumnRef  *c = makeNode(ColumnRef);
15591 	int		nfields = 0;
15592 	ListCell *l;
15593 
15594 	c->location = location;
15595 	foreach(l, indirection)
15596 	{
15597 		if (IsA(lfirst(l), A_Indices))
15598 		{
15599 			A_Indirection *i = makeNode(A_Indirection);
15600 
15601 			if (nfields == 0)
15602 			{
15603 				/* easy case - all indirection goes to A_Indirection */
15604 				c->fields = list_make1(makeString(colname));
15605 				i->indirection = check_indirection(indirection, yyscanner);
15606 			}
15607 			else
15608 			{
15609 				/* got to split the list in two */
15610 				i->indirection = check_indirection(list_copy_tail(indirection,
15611 																  nfields),
15612 												   yyscanner);
15613 				indirection = list_truncate(indirection, nfields);
15614 				c->fields = lcons(makeString(colname), indirection);
15615 			}
15616 			i->arg = (Node *) c;
15617 			return (Node *) i;
15618 		}
15619 		else if (IsA(lfirst(l), A_Star))
15620 		{
15621 			/* We only allow '*' at the end of a ColumnRef */
15622 			if (lnext(l) != NULL)
15623 				parser_yyerror("improper use of \"*\"");
15624 		}
15625 		nfields++;
15626 	}
15627 	/* No subscripting, so all indirection gets added to field list */
15628 	c->fields = lcons(makeString(colname), indirection);
15629 	return (Node *) c;
15630 }
15631 
15632 Node *
makeTypeCast(Node * arg,TypeName * typename,int location)15633 makeTypeCast(Node *arg, TypeName *typename, int location)
15634 {
15635 	TypeCast *n = makeNode(TypeCast);
15636 	n->arg = arg;
15637 	n->typeName = typename;
15638 	n->location = location;
15639 	return (Node *) n;
15640 }
15641 
15642 static Node *
makeStringConst(char * str,int location)15643 makeStringConst(char *str, int location)
15644 {
15645 	A_Const *n = makeNode(A_Const);
15646 
15647 	n->val.type = T_String;
15648 	n->val.val.str = str;
15649 	n->location = location;
15650 
15651 	return (Node *)n;
15652 }
15653 
15654 Node *
makeStringConstCast(char * str,int location,TypeName * typename)15655 makeStringConstCast(char *str, int location, TypeName *typename)
15656 {
15657 	Node *s = makeStringConst(str, location);
15658 
15659 	return makeTypeCast(s, typename, -1);
15660 }
15661 
15662 Node *
makeIntConst(int val,int location)15663 makeIntConst(int val, int location)
15664 {
15665 	A_Const *n = makeNode(A_Const);
15666 
15667 	n->val.type = T_Integer;
15668 	n->val.val.ival = val;
15669 	n->location = location;
15670 
15671 	return (Node *)n;
15672 }
15673 
15674 static Node *
makeFloatConst(char * str,int location)15675 makeFloatConst(char *str, int location)
15676 {
15677 	A_Const *n = makeNode(A_Const);
15678 
15679 	n->val.type = T_Float;
15680 	n->val.val.str = str;
15681 	n->location = location;
15682 
15683 	return (Node *)n;
15684 }
15685 
15686 static Node *
makeBitStringConst(char * str,int location)15687 makeBitStringConst(char *str, int location)
15688 {
15689 	A_Const *n = makeNode(A_Const);
15690 
15691 	n->val.type = T_BitString;
15692 	n->val.val.str = str;
15693 	n->location = location;
15694 
15695 	return (Node *)n;
15696 }
15697 
15698 static Node *
makeNullAConst(int location)15699 makeNullAConst(int location)
15700 {
15701 	A_Const *n = makeNode(A_Const);
15702 
15703 	n->val.type = T_Null;
15704 	n->location = location;
15705 
15706 	return (Node *)n;
15707 }
15708 
15709 static Node *
makeAConst(Value * v,int location)15710 makeAConst(Value *v, int location)
15711 {
15712 	Node *n;
15713 
15714 	switch (v->type)
15715 	{
15716 		case T_Float:
15717 			n = makeFloatConst(v->val.str, location);
15718 			break;
15719 
15720 		case T_Integer:
15721 			n = makeIntConst(v->val.ival, location);
15722 			break;
15723 
15724 		case T_String:
15725 		default:
15726 			n = makeStringConst(v->val.str, location);
15727 			break;
15728 	}
15729 
15730 	return n;
15731 }
15732 
15733 /* makeBoolAConst()
15734  * Create an A_Const string node and put it inside a boolean cast.
15735  */
15736 static Node *
makeBoolAConst(bool state,int location)15737 makeBoolAConst(bool state, int location)
15738 {
15739 	A_Const *n = makeNode(A_Const);
15740 
15741 	n->val.type = T_String;
15742 	n->val.val.str = (state ? "t" : "f");
15743 	n->location = location;
15744 
15745 	return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
15746 }
15747 
15748 /* makeRoleSpec
15749  * Create a RoleSpec with the given type
15750  */
15751 static RoleSpec *
makeRoleSpec(RoleSpecType type,int location)15752 makeRoleSpec(RoleSpecType type, int location)
15753 {
15754 	RoleSpec *spec = makeNode(RoleSpec);
15755 
15756 	spec->roletype = type;
15757 	spec->location = location;
15758 
15759 	return spec;
15760 }
15761 
15762 /* check_qualified_name --- check the result of qualified_name production
15763  *
15764  * It's easiest to let the grammar production for qualified_name allow
15765  * subscripts and '*', which we then must reject here.
15766  */
15767 static void
check_qualified_name(List * names,core_yyscan_t yyscanner)15768 check_qualified_name(List *names, core_yyscan_t yyscanner)
15769 {
15770 	ListCell   *i;
15771 
15772 	foreach(i, names)
15773 	{
15774 		if (!IsA(lfirst(i), String))
15775 			parser_yyerror("syntax error");
15776 	}
15777 }
15778 
15779 /* check_func_name --- check the result of func_name production
15780  *
15781  * It's easiest to let the grammar production for func_name allow subscripts
15782  * and '*', which we then must reject here.
15783  */
15784 static List *
check_func_name(List * names,core_yyscan_t yyscanner)15785 check_func_name(List *names, core_yyscan_t yyscanner)
15786 {
15787 	ListCell   *i;
15788 
15789 	foreach(i, names)
15790 	{
15791 		if (!IsA(lfirst(i), String))
15792 			parser_yyerror("syntax error");
15793 	}
15794 	return names;
15795 }
15796 
15797 /* check_indirection --- check the result of indirection production
15798  *
15799  * We only allow '*' at the end of the list, but it's hard to enforce that
15800  * in the grammar, so do it here.
15801  */
15802 static List *
check_indirection(List * indirection,core_yyscan_t yyscanner)15803 check_indirection(List *indirection, core_yyscan_t yyscanner)
15804 {
15805 	ListCell *l;
15806 
15807 	foreach(l, indirection)
15808 	{
15809 		if (IsA(lfirst(l), A_Star))
15810 		{
15811 			if (lnext(l) != NULL)
15812 				parser_yyerror("improper use of \"*\"");
15813 		}
15814 	}
15815 	return indirection;
15816 }
15817 
15818 /* extractArgTypes()
15819  * Given a list of FunctionParameter nodes, extract a list of just the
15820  * argument types (TypeNames) for input parameters only.  This is what
15821  * is needed to look up an existing function, which is what is wanted by
15822  * the productions that use this call.
15823  */
15824 static List *
extractArgTypes(List * parameters)15825 extractArgTypes(List *parameters)
15826 {
15827 	List	   *result = NIL;
15828 	ListCell   *i;
15829 
15830 	foreach(i, parameters)
15831 	{
15832 		FunctionParameter *p = (FunctionParameter *) lfirst(i);
15833 
15834 		if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
15835 			result = lappend(result, p->argType);
15836 	}
15837 	return result;
15838 }
15839 
15840 /* extractAggrArgTypes()
15841  * As above, but work from the output of the aggr_args production.
15842  */
15843 static List *
extractAggrArgTypes(List * aggrargs)15844 extractAggrArgTypes(List *aggrargs)
15845 {
15846 	Assert(list_length(aggrargs) == 2);
15847 	return extractArgTypes((List *) linitial(aggrargs));
15848 }
15849 
15850 /* makeOrderedSetArgs()
15851  * Build the result of the aggr_args production (which see the comments for).
15852  * This handles only the case where both given lists are nonempty, so that
15853  * we have to deal with multiple VARIADIC arguments.
15854  */
15855 static List *
makeOrderedSetArgs(List * directargs,List * orderedargs,core_yyscan_t yyscanner)15856 makeOrderedSetArgs(List *directargs, List *orderedargs,
15857 				   core_yyscan_t yyscanner)
15858 {
15859 	FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
15860 	int			ndirectargs;
15861 
15862 	/* No restriction unless last direct arg is VARIADIC */
15863 	if (lastd->mode == FUNC_PARAM_VARIADIC)
15864 	{
15865 		FunctionParameter *firsto = (FunctionParameter *) linitial(orderedargs);
15866 
15867 		/*
15868 		 * We ignore the names, though the aggr_arg production allows them;
15869 		 * it doesn't allow default values, so those need not be checked.
15870 		 */
15871 		if (list_length(orderedargs) != 1 ||
15872 			firsto->mode != FUNC_PARAM_VARIADIC)
15873 			ereport(ERROR,
15874 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15875 					 errmsg("an ordered-set aggregate with a VARIADIC direct argument must have one VARIADIC aggregated argument of the same data type"),
15876 					 parser_errposition(exprLocation((Node *) firsto))));
15877 
15878 		/* OK, drop the duplicate VARIADIC argument from the internal form */
15879 		orderedargs = NIL;
15880 	}
15881 
15882 	/* don't merge into the next line, as list_concat changes directargs */
15883 	ndirectargs = list_length(directargs);
15884 
15885 	return list_make2(list_concat(directargs, orderedargs),
15886 					  makeInteger(ndirectargs));
15887 }
15888 
15889 /* insertSelectOptions()
15890  * Insert ORDER BY, etc into an already-constructed SelectStmt.
15891  *
15892  * This routine is just to avoid duplicating code in SelectStmt productions.
15893  */
15894 static void
insertSelectOptions(SelectStmt * stmt,List * sortClause,List * lockingClause,Node * limitOffset,Node * limitCount,WithClause * withClause,core_yyscan_t yyscanner)15895 insertSelectOptions(SelectStmt *stmt,
15896 					List *sortClause, List *lockingClause,
15897 					Node *limitOffset, Node *limitCount,
15898 					WithClause *withClause,
15899 					core_yyscan_t yyscanner)
15900 {
15901 	Assert(IsA(stmt, SelectStmt));
15902 
15903 	/*
15904 	 * Tests here are to reject constructs like
15905 	 *	(SELECT foo ORDER BY bar) ORDER BY baz
15906 	 */
15907 	if (sortClause)
15908 	{
15909 		if (stmt->sortClause)
15910 			ereport(ERROR,
15911 					(errcode(ERRCODE_SYNTAX_ERROR),
15912 					 errmsg("multiple ORDER BY clauses not allowed"),
15913 					 parser_errposition(exprLocation((Node *) sortClause))));
15914 		stmt->sortClause = sortClause;
15915 	}
15916 	/* We can handle multiple locking clauses, though */
15917 	stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
15918 	if (limitOffset)
15919 	{
15920 		if (stmt->limitOffset)
15921 			ereport(ERROR,
15922 					(errcode(ERRCODE_SYNTAX_ERROR),
15923 					 errmsg("multiple OFFSET clauses not allowed"),
15924 					 parser_errposition(exprLocation(limitOffset))));
15925 		stmt->limitOffset = limitOffset;
15926 	}
15927 	if (limitCount)
15928 	{
15929 		if (stmt->limitCount)
15930 			ereport(ERROR,
15931 					(errcode(ERRCODE_SYNTAX_ERROR),
15932 					 errmsg("multiple LIMIT clauses not allowed"),
15933 					 parser_errposition(exprLocation(limitCount))));
15934 		stmt->limitCount = limitCount;
15935 	}
15936 	if (withClause)
15937 	{
15938 		if (stmt->withClause)
15939 			ereport(ERROR,
15940 					(errcode(ERRCODE_SYNTAX_ERROR),
15941 					 errmsg("multiple WITH clauses not allowed"),
15942 					 parser_errposition(exprLocation((Node *) withClause))));
15943 		stmt->withClause = withClause;
15944 	}
15945 }
15946 
15947 static Node *
makeSetOp(SetOperation op,bool all,Node * larg,Node * rarg)15948 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
15949 {
15950 	SelectStmt *n = makeNode(SelectStmt);
15951 
15952 	n->op = op;
15953 	n->all = all;
15954 	n->larg = (SelectStmt *) larg;
15955 	n->rarg = (SelectStmt *) rarg;
15956 	return (Node *) n;
15957 }
15958 
15959 /* SystemFuncName()
15960  * Build a properly-qualified reference to a built-in function.
15961  */
15962 List *
SystemFuncName(char * name)15963 SystemFuncName(char *name)
15964 {
15965 	return list_make2(makeString("pg_catalog"), makeString(name));
15966 }
15967 
15968 /* SystemTypeName()
15969  * Build a properly-qualified reference to a built-in type.
15970  *
15971  * typmod is defaulted, but may be changed afterwards by caller.
15972  * Likewise for the location.
15973  */
15974 TypeName *
SystemTypeName(char * name)15975 SystemTypeName(char *name)
15976 {
15977 	return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
15978 											   makeString(name)));
15979 }
15980 
15981 /* doNegate()
15982  * Handle negation of a numeric constant.
15983  *
15984  * Formerly, we did this here because the optimizer couldn't cope with
15985  * indexquals that looked like "var = -4" --- it wants "var = const"
15986  * and a unary minus operator applied to a constant didn't qualify.
15987  * As of Postgres 7.0, that problem doesn't exist anymore because there
15988  * is a constant-subexpression simplifier in the optimizer.  However,
15989  * there's still a good reason for doing this here, which is that we can
15990  * postpone committing to a particular internal representation for simple
15991  * negative constants.	It's better to leave "-123.456" in string form
15992  * until we know what the desired type is.
15993  */
15994 static Node *
doNegate(Node * n,int location)15995 doNegate(Node *n, int location)
15996 {
15997 	if (IsA(n, A_Const))
15998 	{
15999 		A_Const *con = (A_Const *)n;
16000 
16001 		/* report the constant's location as that of the '-' sign */
16002 		con->location = location;
16003 
16004 		if (con->val.type == T_Integer)
16005 		{
16006 			con->val.val.ival = -con->val.val.ival;
16007 			return n;
16008 		}
16009 		if (con->val.type == T_Float)
16010 		{
16011 			doNegateFloat(&con->val);
16012 			return n;
16013 		}
16014 	}
16015 
16016 	return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
16017 }
16018 
16019 static void
doNegateFloat(Value * v)16020 doNegateFloat(Value *v)
16021 {
16022 	char   *oldval = v->val.str;
16023 
16024 	Assert(IsA(v, Float));
16025 	if (*oldval == '+')
16026 		oldval++;
16027 	if (*oldval == '-')
16028 		v->val.str = oldval+1;	/* just strip the '-' */
16029 	else
16030 		v->val.str = psprintf("-%s", oldval);
16031 }
16032 
16033 static Node *
makeAndExpr(Node * lexpr,Node * rexpr,int location)16034 makeAndExpr(Node *lexpr, Node *rexpr, int location)
16035 {
16036 	Node	   *lexp = lexpr;
16037 
16038 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
16039 	while (IsA(lexp, A_Expr) &&
16040 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
16041 		lexp = ((A_Expr *) lexp)->lexpr;
16042 	/* Flatten "a AND b AND c ..." to a single BoolExpr on sight */
16043 	if (IsA(lexp, BoolExpr))
16044 	{
16045 		BoolExpr *blexpr = (BoolExpr *) lexp;
16046 
16047 		if (blexpr->boolop == AND_EXPR)
16048 		{
16049 			blexpr->args = lappend(blexpr->args, rexpr);
16050 			return (Node *) blexpr;
16051 		}
16052 	}
16053 	return (Node *) makeBoolExpr(AND_EXPR, list_make2(lexpr, rexpr), location);
16054 }
16055 
16056 static Node *
makeOrExpr(Node * lexpr,Node * rexpr,int location)16057 makeOrExpr(Node *lexpr, Node *rexpr, int location)
16058 {
16059 	Node	   *lexp = lexpr;
16060 
16061 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
16062 	while (IsA(lexp, A_Expr) &&
16063 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
16064 		lexp = ((A_Expr *) lexp)->lexpr;
16065 	/* Flatten "a OR b OR c ..." to a single BoolExpr on sight */
16066 	if (IsA(lexp, BoolExpr))
16067 	{
16068 		BoolExpr *blexpr = (BoolExpr *) lexp;
16069 
16070 		if (blexpr->boolop == OR_EXPR)
16071 		{
16072 			blexpr->args = lappend(blexpr->args, rexpr);
16073 			return (Node *) blexpr;
16074 		}
16075 	}
16076 	return (Node *) makeBoolExpr(OR_EXPR, list_make2(lexpr, rexpr), location);
16077 }
16078 
16079 static Node *
makeNotExpr(Node * expr,int location)16080 makeNotExpr(Node *expr, int location)
16081 {
16082 	return (Node *) makeBoolExpr(NOT_EXPR, list_make1(expr), location);
16083 }
16084 
16085 static Node *
makeAArrayExpr(List * elements,int location)16086 makeAArrayExpr(List *elements, int location)
16087 {
16088 	A_ArrayExpr *n = makeNode(A_ArrayExpr);
16089 
16090 	n->elements = elements;
16091 	n->location = location;
16092 	return (Node *) n;
16093 }
16094 
16095 static Node *
makeSQLValueFunction(SQLValueFunctionOp op,int32 typmod,int location)16096 makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location)
16097 {
16098 	SQLValueFunction *svf = makeNode(SQLValueFunction);
16099 
16100 	svf->op = op;
16101 	/* svf->type will be filled during parse analysis */
16102 	svf->typmod = typmod;
16103 	svf->location = location;
16104 	return (Node *) svf;
16105 }
16106 
16107 static Node *
makeXmlExpr(XmlExprOp op,char * name,List * named_args,List * args,int location)16108 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
16109 			int location)
16110 {
16111 	XmlExpr		*x = makeNode(XmlExpr);
16112 
16113 	x->op = op;
16114 	x->name = name;
16115 	/*
16116 	 * named_args is a list of ResTarget; it'll be split apart into separate
16117 	 * expression and name lists in transformXmlExpr().
16118 	 */
16119 	x->named_args = named_args;
16120 	x->arg_names = NIL;
16121 	x->args = args;
16122 	/* xmloption, if relevant, must be filled in by caller */
16123 	/* type and typmod will be filled in during parse analysis */
16124 	x->type = InvalidOid;			/* marks the node as not analyzed */
16125 	x->location = location;
16126 	return (Node *) x;
16127 }
16128 
16129 /*
16130  * Merge the input and output parameters of a table function.
16131  */
16132 static List *
mergeTableFuncParameters(List * func_args,List * columns)16133 mergeTableFuncParameters(List *func_args, List *columns)
16134 {
16135 	ListCell   *lc;
16136 
16137 	/* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
16138 	foreach(lc, func_args)
16139 	{
16140 		FunctionParameter *p = (FunctionParameter *) lfirst(lc);
16141 
16142 		if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
16143 			ereport(ERROR,
16144 					(errcode(ERRCODE_SYNTAX_ERROR),
16145 					 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
16146 	}
16147 
16148 	return list_concat(func_args, columns);
16149 }
16150 
16151 /*
16152  * Determine return type of a TABLE function.  A single result column
16153  * returns setof that column's type; otherwise return setof record.
16154  */
16155 static TypeName *
TableFuncTypeName(List * columns)16156 TableFuncTypeName(List *columns)
16157 {
16158 	TypeName *result;
16159 
16160 	if (list_length(columns) == 1)
16161 	{
16162 		FunctionParameter *p = (FunctionParameter *) linitial(columns);
16163 
16164 		result = copyObject(p->argType);
16165 	}
16166 	else
16167 		result = SystemTypeName("record");
16168 
16169 	result->setof = true;
16170 
16171 	return result;
16172 }
16173 
16174 /*
16175  * Convert a list of (dotted) names to a RangeVar (like
16176  * makeRangeVarFromNameList, but with position support).  The
16177  * "AnyName" refers to the any_name production in the grammar.
16178  */
16179 static RangeVar *
makeRangeVarFromAnyName(List * names,int position,core_yyscan_t yyscanner)16180 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
16181 {
16182 	RangeVar *r = makeNode(RangeVar);
16183 
16184 	switch (list_length(names))
16185 	{
16186 		case 1:
16187 			r->catalogname = NULL;
16188 			r->schemaname = NULL;
16189 			r->relname = strVal(linitial(names));
16190 			break;
16191 		case 2:
16192 			r->catalogname = NULL;
16193 			r->schemaname = strVal(linitial(names));
16194 			r->relname = strVal(lsecond(names));
16195 			break;
16196 		case 3:
16197 			r->catalogname = strVal(linitial(names));
16198 			r->schemaname = strVal(lsecond(names));
16199 			r->relname = strVal(lthird(names));
16200 			break;
16201 		default:
16202 			ereport(ERROR,
16203 					(errcode(ERRCODE_SYNTAX_ERROR),
16204 					 errmsg("improper qualified name (too many dotted names): %s",
16205 							NameListToString(names)),
16206 					 parser_errposition(position)));
16207 			break;
16208 	}
16209 
16210 	r->relpersistence = RELPERSISTENCE_PERMANENT;
16211 	r->location = position;
16212 
16213 	return r;
16214 }
16215 
16216 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
16217 static void
SplitColQualList(List * qualList,List ** constraintList,CollateClause ** collClause,core_yyscan_t yyscanner)16218 SplitColQualList(List *qualList,
16219 				 List **constraintList, CollateClause **collClause,
16220 				 core_yyscan_t yyscanner)
16221 {
16222 	ListCell   *cell;
16223 	ListCell   *prev;
16224 	ListCell   *next;
16225 
16226 	*collClause = NULL;
16227 	prev = NULL;
16228 	for (cell = list_head(qualList); cell; cell = next)
16229 	{
16230 		Node   *n = (Node *) lfirst(cell);
16231 
16232 		next = lnext(cell);
16233 		if (IsA(n, Constraint))
16234 		{
16235 			/* keep it in list */
16236 			prev = cell;
16237 			continue;
16238 		}
16239 		if (IsA(n, CollateClause))
16240 		{
16241 			CollateClause *c = (CollateClause *) n;
16242 
16243 			if (*collClause)
16244 				ereport(ERROR,
16245 						(errcode(ERRCODE_SYNTAX_ERROR),
16246 						 errmsg("multiple COLLATE clauses not allowed"),
16247 						 parser_errposition(c->location)));
16248 			*collClause = c;
16249 		}
16250 		else
16251 			elog(ERROR, "unexpected node type %d", (int) n->type);
16252 		/* remove non-Constraint nodes from qualList */
16253 		qualList = list_delete_cell(qualList, cell, prev);
16254 	}
16255 	*constraintList = qualList;
16256 }
16257 
16258 /*
16259  * Process result of ConstraintAttributeSpec, and set appropriate bool flags
16260  * in the output command node.  Pass NULL for any flags the particular
16261  * command doesn't support.
16262  */
16263 static void
processCASbits(int cas_bits,int location,const char * constrType,bool * deferrable,bool * initdeferred,bool * not_valid,bool * no_inherit,core_yyscan_t yyscanner)16264 processCASbits(int cas_bits, int location, const char *constrType,
16265 			   bool *deferrable, bool *initdeferred, bool *not_valid,
16266 			   bool *no_inherit, core_yyscan_t yyscanner)
16267 {
16268 	/* defaults */
16269 	if (deferrable)
16270 		*deferrable = false;
16271 	if (initdeferred)
16272 		*initdeferred = false;
16273 	if (not_valid)
16274 		*not_valid = false;
16275 
16276 	if (cas_bits & (CAS_DEFERRABLE | CAS_INITIALLY_DEFERRED))
16277 	{
16278 		if (deferrable)
16279 			*deferrable = true;
16280 		else
16281 			ereport(ERROR,
16282 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16283 					 /* translator: %s is CHECK, UNIQUE, or similar */
16284 					 errmsg("%s constraints cannot be marked DEFERRABLE",
16285 							constrType),
16286 					 parser_errposition(location)));
16287 	}
16288 
16289 	if (cas_bits & CAS_INITIALLY_DEFERRED)
16290 	{
16291 		if (initdeferred)
16292 			*initdeferred = true;
16293 		else
16294 			ereport(ERROR,
16295 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16296 					 /* translator: %s is CHECK, UNIQUE, or similar */
16297 					 errmsg("%s constraints cannot be marked DEFERRABLE",
16298 							constrType),
16299 					 parser_errposition(location)));
16300 	}
16301 
16302 	if (cas_bits & CAS_NOT_VALID)
16303 	{
16304 		if (not_valid)
16305 			*not_valid = true;
16306 		else
16307 			ereport(ERROR,
16308 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16309 					 /* translator: %s is CHECK, UNIQUE, or similar */
16310 					 errmsg("%s constraints cannot be marked NOT VALID",
16311 							constrType),
16312 					 parser_errposition(location)));
16313 	}
16314 
16315 	if (cas_bits & CAS_NO_INHERIT)
16316 	{
16317 		if (no_inherit)
16318 			*no_inherit = true;
16319 		else
16320 			ereport(ERROR,
16321 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16322 					 /* translator: %s is CHECK, UNIQUE, or similar */
16323 					 errmsg("%s constraints cannot be marked NO INHERIT",
16324 							constrType),
16325 					 parser_errposition(location)));
16326 	}
16327 }
16328 
16329 /*----------
16330  * Recursive view transformation
16331  *
16332  * Convert
16333  *
16334  *     CREATE RECURSIVE VIEW relname (aliases) AS query
16335  *
16336  * to
16337  *
16338  *     CREATE VIEW relname (aliases) AS
16339  *         WITH RECURSIVE relname (aliases) AS (query)
16340  *         SELECT aliases FROM relname
16341  *
16342  * Actually, just the WITH ... part, which is then inserted into the original
16343  * view definition as the query.
16344  * ----------
16345  */
16346 static Node *
makeRecursiveViewSelect(char * relname,List * aliases,Node * query)16347 makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
16348 {
16349 	SelectStmt *s = makeNode(SelectStmt);
16350 	WithClause *w = makeNode(WithClause);
16351 	CommonTableExpr *cte = makeNode(CommonTableExpr);
16352 	List	   *tl = NIL;
16353 	ListCell   *lc;
16354 
16355 	/* create common table expression */
16356 	cte->ctename = relname;
16357 	cte->aliascolnames = aliases;
16358 	cte->ctequery = query;
16359 	cte->location = -1;
16360 
16361 	/* create WITH clause and attach CTE */
16362 	w->recursive = true;
16363 	w->ctes = list_make1(cte);
16364 	w->location = -1;
16365 
16366 	/* create target list for the new SELECT from the alias list of the
16367 	 * recursive view specification */
16368 	foreach (lc, aliases)
16369 	{
16370 		ResTarget *rt = makeNode(ResTarget);
16371 
16372 		rt->name = NULL;
16373 		rt->indirection = NIL;
16374 		rt->val = makeColumnRef(strVal(lfirst(lc)), NIL, -1, 0);
16375 		rt->location = -1;
16376 
16377 		tl = lappend(tl, rt);
16378 	}
16379 
16380 	/* create new SELECT combining WITH clause, target list, and fake FROM
16381 	 * clause */
16382 	s->withClause = w;
16383 	s->targetList = tl;
16384 	s->fromClause = list_make1(makeRangeVar(NULL, relname, -1));
16385 
16386 	return (Node *) s;
16387 }
16388 
16389 /* parser_init()
16390  * Initialize to parse one query string
16391  */
16392 void
parser_init(base_yy_extra_type * yyext)16393 parser_init(base_yy_extra_type *yyext)
16394 {
16395 	yyext->parsetree = NIL;		/* in case grammar forgets to set it */
16396 }
16397 
16398