1 %{
2 
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5  *
6  * gram.y
7  *	  POSTGRESQL BISON rules/actions
8  *
9  * Portions Copyright (c) 2003-2021, PgPool Global Development Group
10  * Portions Copyright (c) 1996-2021, 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 /*
67  * Definition taken from
68  * postgreSQL source code file: src/include/commands/trigger.h
69  */
70 #define TRIGGER_FIRES_ON_ORIGIN                         'O'
71 #define TRIGGER_FIRES_ALWAYS                            'A'
72 #define TRIGGER_FIRES_ON_REPLICA                        'R'
73 #define TRIGGER_DISABLED                                'D'
74 
75 /*
76  * Definition taken from
77  * postgreSQL source code file: src/include/catalog/pg_class.h
78  */
79 
80 #define           REPLICA_IDENTITY_DEFAULT      'd'
81 #define           REPLICA_IDENTITY_NOTHING      'n'
82 #define           REPLICA_IDENTITY_FULL         'f'
83 #define           REPLICA_IDENTITY_INDEX        'i'
84 
85 /*
86  * Definition taken from
87  * postgreSQL source code file: src/include/catalog/pg_attribute.h
88  */
89 #define		  ATTRIBUTE_IDENTITY_ALWAYS	'a'
90 #define		  ATTRIBUTE_IDENTITY_BY_DEFAULT 'd'
91 
92 /*
93  * Definition taken from
94  * postgreSQL source code file: src/include/utils/xml.h
95  */
96 typedef enum
97 {
98 	XML_STANDALONE_YES,
99 	XML_STANDALONE_NO,
100 	XML_STANDALONE_NO_VALUE,
101 	XML_STANDALONE_OMITTED
102 } XmlStandaloneType;
103 
104 /*
105  * Location tracking support --- simpler than bison's default, since we only
106  * want to track the start position not the end position of each nonterminal.
107  */
108 #define YYLLOC_DEFAULT(Current, Rhs, N) \
109 	do { \
110 		if ((N) > 0) \
111 			(Current) = (Rhs)[1]; \
112 		else \
113 			(Current) = (-1); \
114 	} while (0)
115 
116 /*
117  * The above macro assigns -1 (unknown) as the parse location of any
118  * nonterminal that was reduced from an empty rule, or whose leftmost
119  * component was reduced from an empty rule.  This is problematic
120  * for nonterminals defined like
121  *		OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ;
122  * because we'll set -1 as the location during the first reduction and then
123  * copy it during each subsequent reduction, leaving us with -1 for the
124  * location even when the list is not empty.  To fix that, do this in the
125  * action for the nonempty rule(s):
126  *		if (@$ < 0) @$ = @2;
127  * (Although we have many nonterminals that follow this pattern, we only
128  * bother with fixing @$ like this when the nonterminal's parse location
129  * is actually referenced in some rule.)
130  *
131  * A cleaner answer would be to make YYLLOC_DEFAULT scan all the Rhs
132  * locations until it's found one that's not -1.  Then we'd get a correct
133  * location for any nonterminal that isn't entirely empty.  But this way
134  * would add overhead to every rule reduction, and so far there's not been
135  * a compelling reason to pay that overhead.
136  */
137 
138 /*
139  * Bison doesn't allocate anything that needs to live across parser calls,
140  * so we can easily have it use palloc instead of malloc.  This prevents
141  * memory leaks if we error out during parsing.  Note this only works with
142  * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
143  * if possible, so there's not really much problem anyhow, at least if
144  * you're building with gcc.
145  */
146 #define YYMALLOC palloc
147 #define YYFREE   pfree
148 
149 /* Private struct for the result of privilege_target production */
150 typedef struct PrivTarget
151 {
152 	GrantTargetType targtype;
153 	ObjectType	objtype;
154 	List	   *objs;
155 } PrivTarget;
156 
157 /* Private struct for the result of import_qualification production */
158 typedef struct ImportQual
159 {
160 	ImportForeignSchemaType type;
161 	List	   *table_names;
162 } ImportQual;
163 
164 /* Private struct for the result of opt_select_limit production */
165 typedef struct SelectLimit
166 {
167 	Node *limitOffset;
168 	Node *limitCount;
169 	LimitOption limitOption;
170 } SelectLimit;
171 
172 /* Private struct for the result of group_clause production */
173 typedef struct GroupClause
174 {
175 	bool	distinct;
176 	List   *list;
177 } GroupClause;
178 
179 /* ConstraintAttributeSpec yields an integer bitmask of these flags: */
180 #define CAS_NOT_DEFERRABLE			0x01
181 #define CAS_DEFERRABLE				0x02
182 #define CAS_INITIALLY_IMMEDIATE		0x04
183 #define CAS_INITIALLY_DEFERRED		0x08
184 #define CAS_NOT_VALID				0x10
185 #define CAS_NO_INHERIT				0x20
186 
187 
188 #define parser_yyerror(msg)  scanner_yyerror(msg, yyscanner)
189 #define parser_errposition(pos)  scanner_errposition(pos, yyscanner)
190 
191 static void base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner,
192 						 const char *msg);
193 static RawStmt *makeRawStmt(Node *stmt, int stmt_location);
194 static void updateRawStmtEnd(RawStmt *rs, int end_location);
195 static Node *makeColumnRef(char *colname, List *indirection,
196 						   int location, core_yyscan_t yyscanner);
197 static Node *makeStringConst(char *str, int location);
198 static Node *makeFloatConst(char *str, int location);
199 static Node *makeBitStringConst(char *str, int location);
200 static Node *makeNullAConst(int location);
201 static Node *makeAConst(Value *v, int location);
202 static Node *makeBoolAConst(bool state, int location);
203 static RoleSpec *makeRoleSpec(RoleSpecType type, int location);
204 static void check_qualified_name(List *names, core_yyscan_t yyscanner);
205 static List *check_func_name(List *names, core_yyscan_t yyscanner);
206 static List *check_indirection(List *indirection, core_yyscan_t yyscanner);
207 static List *extractArgTypes(List *parameters);
208 static List *extractAggrArgTypes(List *aggrargs);
209 static List *makeOrderedSetArgs(List *directargs, List *orderedargs,
210 								core_yyscan_t yyscanner);
211 static void insertSelectOptions(SelectStmt *stmt,
212 								List *sortClause, List *lockingClause,
213 								SelectLimit *limitClause,
214 								WithClause *withClause,
215 								core_yyscan_t yyscanner);
216 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
217 static Node *doNegate(Node *n, int location);
218 static void doNegateFloat(Value *v);
219 static Node *makeAndExpr(Node *lexpr, Node *rexpr, int location);
220 static Node *makeOrExpr(Node *lexpr, Node *rexpr, int location);
221 static Node *makeNotExpr(Node *expr, int location);
222 static Node *makeAArrayExpr(List *elements, int location);
223 static Node *makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod,
224 								  int location);
225 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
226 						 List *args, int location);
227 static List *mergeTableFuncParameters(List *func_args, List *columns);
228 static TypeName *TableFuncTypeName(List *columns);
229 static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner);
230 static void SplitColQualList(List *qualList,
231 							 List **constraintList, CollateClause **collClause,
232 							 core_yyscan_t yyscanner);
233 static void processCASbits(int cas_bits, int location, const char *constrType,
234 			   bool *deferrable, bool *initdeferred, bool *not_valid,
235 			   bool *no_inherit, core_yyscan_t yyscanner);
236 static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
237 
238 %}
239 
240 %pure-parser
241 %expect 0
242 %name-prefix="base_yy"
243 %locations
244 
245 %parse-param {core_yyscan_t yyscanner}
246 %lex-param   {core_yyscan_t yyscanner}
247 
248 %union
249 {
250 	core_YYSTYPE		core_yystype;
251 	/* these fields must match core_YYSTYPE: */
252 	int					ival;
253 	char				*str;
254 	const char			*keyword;
255 
256 	char				chr;
257 	bool				boolean;
258 	JoinType			jtype;
259 	DropBehavior		dbehavior;
260 	OnCommitAction		oncommit;
261 	List				*list;
262 	Node				*node;
263 	Value				*value;
264 	ObjectType			objtype;
265 	TypeName			*typnam;
266 	FunctionParameter   *fun_param;
267 	FunctionParameterMode fun_param_mode;
268 	ObjectWithArgs		*objwithargs;
269 	DefElem				*defelt;
270 	SortBy				*sortby;
271 	WindowDef			*windef;
272 	JoinExpr			*jexpr;
273 	IndexElem			*ielem;
274 	StatsElem			*selem;
275 	Alias				*alias;
276 	RangeVar			*range;
277 	IntoClause			*into;
278 	WithClause			*with;
279 	InferClause			*infer;
280 	OnConflictClause	*onconflict;
281 	A_Indices			*aind;
282 	ResTarget			*target;
283 	struct PrivTarget	*privtarget;
284 	AccessPriv			*accesspriv;
285 	struct ImportQual	*importqual;
286 	InsertStmt			*istmt;
287 	VariableSetStmt		*vsetstmt;
288 	PartitionElem		*partelem;
289 	PartitionSpec		*partspec;
290 	PartitionBoundSpec	*partboundspec;
291 	RoleSpec			*rolespec;
292 	struct SelectLimit	*selectlimit;
293 	SetQuantifier	 setquantifier;
294 	struct GroupClause  *groupclause;
295 }
296 
297 %type <node>	stmt toplevel_stmt schema_stmt routine_body_stmt
298 		AlterEventTrigStmt AlterCollationStmt
299 		AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterEnumStmt
300 		AlterFdwStmt AlterForeignServerStmt AlterGroupStmt
301 		AlterObjectDependsStmt AlterObjectSchemaStmt AlterOwnerStmt
302 		AlterOperatorStmt AlterTypeStmt AlterSeqStmt AlterSystemStmt AlterTableStmt
303 		AlterTblSpcStmt AlterExtensionStmt AlterExtensionContentsStmt
304 		AlterCompositeTypeStmt AlterUserMappingStmt
305 		AlterRoleStmt AlterRoleSetStmt AlterPolicyStmt AlterStatsStmt
306 		AlterDefaultPrivilegesStmt DefACLAction
307 		AnalyzeStmt CallStmt ClosePortalStmt ClusterStmt CommentStmt
308 		ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
309 		CreateDomainStmt CreateExtensionStmt CreateGroupStmt CreateOpClassStmt
310 		CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
311 		CreateSchemaStmt CreateSeqStmt CreateStmt CreateStatsStmt CreateTableSpaceStmt
312 		CreateFdwStmt CreateForeignServerStmt CreateForeignTableStmt
313 		CreateAssertionStmt CreateTransformStmt CreateTrigStmt CreateEventTrigStmt
314 		CreateUserStmt CreateUserMappingStmt CreateRoleStmt CreatePolicyStmt
315 		CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
316 		DropOpClassStmt DropOpFamilyStmt DropStmt
317 		DropCastStmt DropRoleStmt
318 		DropdbStmt DropTableSpaceStmt
319 		DropTransformStmt
320 		DropUserMappingStmt ExplainStmt FetchStmt
321 		GrantStmt GrantRoleStmt ImportForeignSchemaStmt IndexStmt InsertStmt
322 		ListenStmt LoadStmt LockStmt NotifyStmt ExplainableStmt PreparableStmt
323 		CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
324 		RemoveFuncStmt RemoveOperStmt RenameStmt ReturnStmt RevokeStmt RevokeRoleStmt
325 		RuleActionStmt RuleActionStmtOrEmpty RuleStmt
326 		SecLabelStmt SelectStmt TransactionStmt TransactionStmtLegacy TruncateStmt
327 		UnlistenStmt UpdateStmt
328 		VacuumStmt
329 		VariableResetStmt VariableSetStmt VariableShowStmt
330 		ViewStmt CheckPointStmt CreateConversionStmt
331 		DeallocateStmt PrepareStmt ExecuteStmt
332 		DropOwnedStmt ReassignOwnedStmt
333 		AlterTSConfigurationStmt AlterTSDictionaryStmt
334 		CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
335 		CreatePublicationStmt AlterPublicationStmt
336 		CreateSubscriptionStmt AlterSubscriptionStmt DropSubscriptionStmt
337 
338 %type <node>	select_no_parens select_with_parens select_clause
339 				simple_select values_clause
340 				PLpgSQL_Expr PLAssignStmt
341 
342 %type <node>	alter_column_default opclass_item opclass_drop alter_using
343 %type <ival>	add_drop opt_asc_desc opt_nulls_order
344 
345 %type <node>	alter_table_cmd alter_type_cmd opt_collate_clause
346 	   replica_identity partition_cmd index_partition_cmd
347 %type <list>	alter_table_cmds alter_type_cmds
348 %type <list>    alter_identity_column_option_list
349 %type <defelt>  alter_identity_column_option
350 
351 %type <dbehavior>	opt_drop_behavior
352 
353 %type <list>	createdb_opt_list createdb_opt_items copy_opt_list
354 				transaction_mode_list
355 				create_extension_opt_list alter_extension_opt_list
356 %type <defelt>	createdb_opt_item copy_opt_item
357 				transaction_mode_item
358 				create_extension_opt_item alter_extension_opt_item
359 
360 %type <ival>	opt_lock lock_type cast_context
361 %type <str>		utility_option_name
362 %type <defelt>	utility_option_elem
363 %type <list>	utility_option_list
364 %type <node>	utility_option_arg
365 %type <defelt>	drop_option
366 %type <boolean>	opt_or_replace opt_no
367 				opt_grant_grant_option opt_grant_admin_option
368 				opt_nowait opt_if_exists opt_with_data
369 				opt_transaction_chain
370 %type <ival>	opt_nowait_or_skip
371 
372 %type <list>	OptRoleList AlterOptRoleList
373 %type <defelt>	CreateOptRoleElem AlterOptRoleElem
374 
375 %type <str>		opt_type
376 %type <str>		foreign_server_version opt_foreign_server_version
377 %type <str>		opt_in_database
378 
379 %type <str>		OptSchemaName
380 %type <list>	OptSchemaEltList
381 
382 %type <chr>		am_type
383 
384 %type <boolean> TriggerForSpec TriggerForType
385 %type <ival>	TriggerActionTime
386 %type <list>	TriggerEvents TriggerOneEvent
387 %type <value>	TriggerFuncArg
388 %type <node>	TriggerWhen
389 %type <str>		TransitionRelName
390 %type <boolean>	TransitionRowOrTable TransitionOldOrNew
391 %type <node>	TriggerTransition
392 
393 %type <list>	event_trigger_when_list event_trigger_value_list
394 %type <defelt>	event_trigger_when_item
395 %type <chr>		enable_trigger
396 
397 %type <str>		copy_file_name
398 				access_method_clause attr_name
399 				table_access_method_clause name cursor_name file_name
400 				opt_index_name cluster_index_specification
401 
402 %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
403 				opt_class opt_inline_handler opt_validator validator_clause
404 				opt_collate
405 
406 %type <range>	qualified_name insert_target OptConstrFromTable
407 
408 %type <str>		all_Op MathOp
409 
410 %type <str>		row_security_cmd RowSecurityDefaultForCmd
411 %type <boolean> RowSecurityDefaultPermissive
412 %type <node>	RowSecurityOptionalWithCheck RowSecurityOptionalExpr
413 %type <list>	RowSecurityDefaultToRole RowSecurityOptionalToRole
414 
415 %type <str>		iso_level opt_encoding
416 %type <rolespec> grantee
417 %type <list>	grantee_list
418 %type <accesspriv> privilege
419 %type <list>	privileges privilege_list
420 %type <privtarget> privilege_target
421 %type <objwithargs> function_with_argtypes aggregate_with_argtypes operator_with_argtypes
422 %type <list>	function_with_argtypes_list aggregate_with_argtypes_list operator_with_argtypes_list
423 %type <ival>	defacl_privilege_target
424 %type <defelt>	DefACLOption
425 %type <list>	DefACLOptionList
426 %type <ival>	import_qualification_type
427 %type <importqual> import_qualification
428 %type <node>	vacuum_relation
429 %type <selectlimit> opt_select_limit select_limit limit_clause
430 
431 %type <list>	parse_toplevel stmtmulti routine_body_stmt_list
432 				OptTableElementList TableElementList OptInherit definition
433 				OptTypedTableElementList TypedTableElementList
434 				reloptions opt_reloptions
435 				OptWith opt_definition func_args func_args_list
436 				func_args_with_defaults func_args_with_defaults_list
437 				aggr_args aggr_args_list
438 				func_as createfunc_opt_list opt_createfunc_opt_list alterfunc_opt_list
439 				old_aggr_definition old_aggr_list
440 				oper_argtypes RuleActionList RuleActionMulti
441 				opt_column_list columnList opt_name_list
442 				sort_clause opt_sort_clause sortby_list index_params stats_params
443 				opt_include opt_c_include index_including_params
444 				name_list role_list from_clause from_list opt_array_bounds
445 				qualified_name_list any_name any_name_list type_name_list
446 				any_operator expr_list attrs
447 				distinct_clause opt_distinct_clause
448 				target_list opt_target_list insert_column_list set_target_list
449 				set_clause_list set_clause
450 				def_list operator_def_list indirection opt_indirection
451 				reloption_list TriggerFuncArgs opclass_item_list opclass_drop_list
452 				opclass_purpose opt_opfamily transaction_mode_list_or_empty
453 				OptTableFuncElementList TableFuncElementList opt_type_modifiers
454 				prep_type_clause
455 				execute_param_clause using_clause returning_clause
456 				opt_enum_val_list enum_val_list table_func_column_list
457 				create_generic_options alter_generic_options
458 				relation_expr_list dostmt_opt_list
459 				transform_element_list transform_type_list
460 				TriggerTransitions TriggerReferencing
461 				vacuum_relation_list opt_vacuum_relation_list
462 				drop_option_list
463 
464 %type <node>	opt_routine_body
465 %type <groupclause> group_clause
466 %type <list>	group_by_list
467 %type <node>	group_by_item empty_grouping_set rollup_clause cube_clause
468 %type <node>	grouping_sets_clause
469 %type <node>	opt_publication_for_tables publication_for_tables
470 
471 %type <list>	opt_fdw_options fdw_options
472 %type <defelt>	fdw_option
473 
474 %type <range>	OptTempTableName
475 %type <into>	into_clause create_as_target create_mv_target
476 
477 %type <defelt>	createfunc_opt_item common_func_opt_item dostmt_opt_item
478 %type <fun_param> func_arg func_arg_with_default table_func_column aggr_arg
479 %type <fun_param_mode> arg_class
480 %type <typnam>	func_return func_type
481 
482 %type <boolean>  opt_trusted opt_restart_seqs
483 %type <ival>	 OptTemp
484 %type <ival>	 OptNoLog
485 %type <oncommit> OnCommitOption
486 
487 %type <ival>	for_locking_strength
488 %type <node>	for_locking_item
489 %type <list>	for_locking_clause opt_for_locking_clause for_locking_items
490 %type <list>	locked_rels_list
491 %type <setquantifier> set_quantifier
492 
493 %type <node>	join_qual
494 %type <jtype>	join_type
495 
496 %type <list>	extract_list overlay_list position_list
497 %type <list>	substr_list trim_list
498 %type <list>	opt_interval interval_second
499 %type <str>		unicode_normal_form
500 
501 %type <boolean> opt_instead
502 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
503 %type <boolean> opt_freeze opt_analyze opt_default opt_recheck
504 %type <defelt>	opt_binary copy_delimiter
505 
506 %type <boolean> copy_from opt_program
507 
508 %type <ival>	event cursor_options opt_hold opt_set_data
509 %type <objtype>	object_type_any_name object_type_name object_type_name_on_any_name
510 				drop_type_name
511 
512 %type <node>	fetch_args select_limit_value
513 				offset_clause select_offset_value
514 				select_fetch_first_value I_or_F_const
515 %type <ival>	row_or_rows first_or_next
516 
517 %type <list>	OptSeqOptList SeqOptList OptParenthesizedSeqOptList
518 %type <defelt>	SeqOptElem
519 
520 %type <istmt>	insert_rest
521 %type <infer>	opt_conf_expr
522 %type <onconflict> opt_on_conflict
523 
524 %type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
525 				 SetResetClause FunctionSetResetClause
526 
527 %type <node>	TableElement TypedTableElement ConstraintElem TableFuncElement
528 %type <node>	columnDef columnOptions
529 %type <defelt>	def_elem reloption_elem old_aggr_elem operator_def_elem
530 %type <node>	def_arg columnElem where_clause where_or_current_clause
531 				a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
532 				columnref in_expr having_clause func_table xmltable array_expr
533 				OptWhereClause operator_def_arg
534 %type <list>	rowsfrom_item rowsfrom_list opt_col_def_list
535 %type <boolean> opt_ordinality
536 %type <list>	ExclusionConstraintList ExclusionConstraintElem
537 %type <list>	func_arg_list func_arg_list_opt
538 %type <node>	func_arg_expr
539 %type <list>	row explicit_row implicit_row type_list array_expr_list
540 %type <node>	case_expr case_arg when_clause case_default
541 %type <list>	when_clause_list
542 %type <node>	opt_search_clause opt_cycle_clause
543 %type <ival>	sub_type opt_materialized
544 %type <value>	NumericOnly
545 %type <list>	NumericOnly_list
546 %type <alias>	alias_clause opt_alias_clause opt_alias_clause_for_join_using
547 %type <list>	func_alias_clause
548 %type <sortby>	sortby
549 %type <ielem>	index_elem index_elem_options
550 %type <selem>	stats_param
551 %type <node>	table_ref
552 %type <jexpr>	joined_table
553 %type <range>	relation_expr
554 %type <range>	relation_expr_opt_alias
555 %type <node>	tablesample_clause opt_repeatable_clause
556 %type <target>	target_el set_target insert_column_item
557 
558 %type <str>		generic_option_name
559 %type <node>	generic_option_arg
560 %type <defelt>	generic_option_elem alter_generic_option_elem
561 %type <list>	generic_option_list alter_generic_option_list
562 
563 %type <ival>	reindex_target_type reindex_target_multitable
564 
565 %type <node>	copy_generic_opt_arg copy_generic_opt_arg_list_item
566 %type <defelt>	copy_generic_opt_elem
567 %type <list>	copy_generic_opt_list copy_generic_opt_arg_list
568 %type <list>	copy_options
569 
570 %type <typnam>	Typename SimpleTypename ConstTypename
571 				GenericType Numeric opt_float
572 				Character ConstCharacter
573 				CharacterWithLength CharacterWithoutLength
574 				ConstDatetime ConstInterval
575 				Bit ConstBit BitWithLength BitWithoutLength
576 %type <str>		character
577 %type <str>		extract_arg
578 %type <boolean> opt_varying opt_timezone opt_no_inherit
579 
580 %type <ival>	Iconst SignedIconst
581 %type <str>		Sconst comment_text notify_payload
582 %type <str>		RoleId opt_boolean_or_string
583 %type <list>	var_list
584 %type <str>		ColId ColLabel BareColLabel
585 %type <str>		NonReservedWord NonReservedWord_or_Sconst
586 %type <str>		var_name type_function_name param_name
587 %type <str>		createdb_opt_name plassign_target
588 %type <node>	var_value zone_value
589 %type <rolespec> auth_ident RoleSpec opt_granted_by
590 
591 %type <keyword> unreserved_keyword type_func_name_keyword
592 %type <keyword> col_name_keyword reserved_keyword
593 %type <keyword> bare_label_keyword
594 
595 %type <node>	TableConstraint TableLikeClause
596 %type <ival>	TableLikeOptionList TableLikeOption
597 %type <str>		column_compression opt_column_compression
598 %type <list>	ColQualList
599 %type <node>	ColConstraint ColConstraintElem ConstraintAttr
600 %type <ival>	key_actions key_delete key_match key_update key_action
601 %type <ival>	ConstraintAttributeSpec ConstraintAttributeElem
602 %type <str>		ExistingIndex
603 
604 %type <list>	constraints_set_list
605 %type <boolean> constraints_set_mode
606 %type <str>		OptTableSpace OptConsTableSpace
607 %type <rolespec> OptTableSpaceOwner
608 %type <ival>	opt_check_option
609 
610 %type <str>		opt_provider security_label
611 
612 %type <target>	xml_attribute_el
613 %type <list>	xml_attribute_list xml_attributes
614 %type <node>	xml_root_version opt_xml_root_standalone
615 %type <node>	xmlexists_argument
616 %type <ival>	document_or_content
617 %type <boolean> xml_whitespace_option
618 %type <list>	xmltable_column_list xmltable_column_option_list
619 %type <node>	xmltable_column_el
620 %type <defelt>	xmltable_column_option_el
621 %type <list>	xml_namespace_list
622 %type <target>	xml_namespace_el
623 
624 %type <node>	func_application func_expr_common_subexpr
625 %type <node>	func_expr func_expr_windowless
626 %type <node>	common_table_expr
627 %type <with>	with_clause opt_with_clause
628 %type <list>	cte_list
629 
630 %type <list>	within_group_clause
631 %type <node>	filter_clause
632 %type <list>	window_clause window_definition_list opt_partition_clause
633 %type <windef>	window_definition over_clause window_specification
634 				opt_frame_clause frame_extent frame_bound
635 %type <ival>	opt_window_exclusion_clause
636 %type <str>		opt_existing_window_name
637 %type <boolean> opt_if_not_exists
638 %type <ival>	generated_when override_kind
639 %type <partspec>	PartitionSpec OptPartitionSpec
640 %type <partelem>	part_elem
641 %type <list>		part_params
642 %type <partboundspec> PartitionBoundSpec
643 %type <list>		hash_partbound
644 %type <defelt>		hash_partbound_elem
645 
646 
647 /*
648  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
649  * They must be listed first so that their numeric codes do not depend on
650  * the set of keywords.  PL/pgSQL depends on this so that it can share the
651  * same lexer.  If you add/change tokens here, fix PL/pgSQL to match!
652  *
653  * UIDENT and USCONST are reduced to IDENT and SCONST in parser.c, so that
654  * they need no productions here; but we must assign token codes to them.
655  *
656  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
657  * parse errors.  It is needed by PL/pgSQL.
658  */
659 %token <str>	IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op
660 %token <ival>	ICONST PARAM
661 %token			TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
662 %token			LESS_EQUALS GREATER_EQUALS NOT_EQUALS
663 
664 /*
665  * If you want to make any keyword changes, update the keyword table in
666  * src/include/parser/kwlist.h and add new keywords to the appropriate one
667  * of the reserved-or-not-so-reserved keyword lists, below; search
668  * this file for "Keyword category lists".
669  */
670 
671 /* ordinary key words in alphabetical order */
672 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
673 	AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
674 	ASENSITIVE ASSERTION ASSIGNMENT ASYMMETRIC ATOMIC AT ATTACH ATTRIBUTE AUTHORIZATION
675 
676 	BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
677 	BOOLEAN_P BOTH BREADTH BY
678 
679 	CACHE CALL CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
680 	CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
681 	CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMENT COMMENTS COMMIT
682 	COMMITTED COMPRESSION CONCURRENTLY CONFIGURATION CONFLICT
683 	CONNECTION CONSTRAINT CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY
684 	COST CREATE CROSS CSV CUBE CURRENT_P
685 	CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
686 	CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
687 
688 	DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
689 	DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DEPTH DESC
690 	DETACH DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P
691 	DOUBLE_P DROP
692 
693 	EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EVENT EXCEPT
694 	EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN EXPRESSION
695 	EXTENSION EXTERNAL EXTRACT
696 
697 	FALSE_P FAMILY FETCH FILTER FINALIZE FIRST_P FLOAT_P FOLLOWING FOR
698 	FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
699 
700 	GENERATED GLOBAL GRANT GRANTED GREATEST GROUP_P GROUPING GROUPS
701 
702 	HANDLER HAVING HEADER_P HOLD HOUR_P
703 
704 	IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P INCLUDE
705 	INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
706 	INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
707 	INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
708 
709 	JOIN
710 
711 	KEY
712 
713 	LABEL LANGUAGE LARGE_P LAST_P LATERAL_P
714 	LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL
715 	LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED
716 
717 	MAPPING MATCH MATERIALIZED MAXVALUE METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE
718 
719 	NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NFC NFD NFKC NFKD NO NONE
720 	NORMALIZE NORMALIZED
721 	NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
722 	NULLS_P NUMERIC
723 
724 	OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
725 	ORDER ORDINALITY OTHERS OUT_P OUTER_P
726 	OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
727 
728 	PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PGPOOL PLACING PLANS POLICY
729 	POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
730 	PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROCEDURES PROGRAM PUBLICATION
731 
732 	QUOTE
733 
734 	RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REFERENCING
735 	REFRESH REINDEX RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
736 	RESET RESTART RESTRICT RETURN RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP
737 	ROUTINE ROUTINES ROW ROWS RULE
738 
739 	SAVEPOINT SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
740 	SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
741 	SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P
742 	START STATEMENT STATISTICS STDIN STDOUT STORAGE STORED STRICT_P STRIP_P
743 	SUBSCRIPTION SUBSTRING SUPPORT SYMMETRIC SYSID SYSTEM_P
744 
745 	TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
746 	TIES TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM
747 	TREAT TRIGGER TRIM TRUE_P
748 	TRUNCATE TRUSTED TYPE_P TYPES_P
749 
750 	UESCAPE UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN
751 	UNLISTEN UNLOGGED UNTIL UPDATE USER USING
752 
753 	VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
754 	VERBOSE VERSION_P VIEW VIEWS VOLATILE
755 
756 	WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE
757 
758 	XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLNAMESPACES
759 	XMLPARSE XMLPI XMLROOT XMLSERIALIZE XMLTABLE
760 
761 	YEAR_P YES_P
762 
763 	ZONE
764 
765 /*
766  * The grammar thinks these are keywords, but they are not in the kwlist.h
767  * list and so can never be entered directly.  The filter in parser.c
768  * creates these tokens when required (based on looking one token ahead).
769  *
770  * NOT_LA exists so that productions such as NOT LIKE can be given the same
771  * precedence as LIKE; otherwise they'd effectively have the same precedence
772  * as NOT, at least with respect to their left-hand subexpression.
773  * NULLS_LA and WITH_LA are needed to make the grammar LALR(1).
774  */
775 %token		NOT_LA NULLS_LA WITH_LA
776 
777 /*
778  * The grammar likewise thinks these tokens are keywords, but they are never
779  * generated by the scanner.  Rather, they can be injected by parser.c as
780  * the initial token of the string (using the lookahead-token mechanism
781  * implemented there).  This provides a way to tell the grammar to parse
782  * something other than the usual list of SQL commands.
783  */
784 %token		MODE_TYPE_NAME
785 %token		MODE_PLPGSQL_EXPR
786 %token		MODE_PLPGSQL_ASSIGN1
787 %token		MODE_PLPGSQL_ASSIGN2
788 %token		MODE_PLPGSQL_ASSIGN3
789 
790 
791 /* Precedence: lowest to highest */
792 %nonassoc	SET				/* see relation_expr_opt_alias */
793 %left		UNION EXCEPT
794 %left		INTERSECT
795 %left		OR
796 %left		AND
797 %right		NOT
798 %nonassoc	IS ISNULL NOTNULL	/* IS sets precedence for IS NULL, etc */
799 %nonassoc	'<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
800 %nonassoc	BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA
801 %nonassoc	ESCAPE			/* ESCAPE must be just above LIKE/ILIKE/SIMILAR */
802 /*
803  * To support target_el without AS, it used to be necessary to assign IDENT an
804  * explicit precedence just less than Op.  While that's not really necessary
805  * since we removed postfix operators, it's still helpful to do so because
806  * there are some other unreserved keywords that need precedence assignments.
807  * If those keywords have the same precedence as IDENT then they clearly act
808  * the same as non-keywords, reducing the risk of unwanted precedence effects.
809  *
810  * We need to do this for PARTITION, RANGE, ROWS, and GROUPS to support
811  * opt_existing_window_name (see comment there).
812  *
813  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
814  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
815  * there is no principled way to distinguish these from the productions
816  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
817  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
818  * appear to cause UNBOUNDED to be treated differently from other unreserved
819  * keywords anywhere else in the grammar, but it's definitely risky.  We can
820  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
821  *
822  * To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
823  * an explicit priority lower than '(', so that a rule with CUBE '(' will shift
824  * rather than reducing a conflicting rule that takes CUBE as a function name.
825  * Using the same precedence as IDENT seems right for the reasons given above.
826  */
827 %nonassoc	UNBOUNDED		/* ideally would have same precedence as IDENT */
828 %nonassoc	IDENT PARTITION RANGE ROWS GROUPS PRECEDING FOLLOWING CUBE ROLLUP
829 %left		Op OPERATOR		/* multi-character ops and user-defined operators */
830 %left		'+' '-'
831 %left		'*' '/' '%'
832 %left		'^'
833 /* Unary Operators */
834 %left		AT				/* sets precedence for AT TIME ZONE */
835 %left		COLLATE
836 %right		UMINUS
837 %left		'[' ']'
838 %left		'(' ')'
839 %left		TYPECAST
840 %left		'.'
841 /*
842  * These might seem to be low-precedence, but actually they are not part
843  * of the arithmetic hierarchy at all in their use as JOIN operators.
844  * We make them high-precedence to support their use as function names.
845  * They wouldn't be given a precedence at all, were it not that we need
846  * left-associativity among the JOIN rules themselves.
847  */
848 %left		JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
849 
850 %%
851 
852 /*
853  *	The target production for the whole parse.
854  *
855  * Ordinarily we parse a list of statements, but if we see one of the
856  * special MODE_XXX symbols as first token, we parse something else.
857  * The options here correspond to enum RawParseMode, which see for details.
858  */
859 parse_toplevel:
860 			stmtmulti
861 			{
862 				pg_yyget_extra(yyscanner)->parsetree = $1;
863 			}
864 			| MODE_TYPE_NAME Typename
865 			{
866 				pg_yyget_extra(yyscanner)->parsetree = list_make1($2);
867 			}
868 			| MODE_PLPGSQL_EXPR PLpgSQL_Expr
869 			{
870 				pg_yyget_extra(yyscanner)->parsetree =
871 					list_make1(makeRawStmt($2, 0));
872 			}
873 			| MODE_PLPGSQL_ASSIGN1 PLAssignStmt
874 			{
875 				PLAssignStmt *n = (PLAssignStmt *) $2;
876 				n->nnames = 1;
877 				pg_yyget_extra(yyscanner)->parsetree =
878 					list_make1(makeRawStmt((Node *) n, 0));
879 			}
880 			| MODE_PLPGSQL_ASSIGN2 PLAssignStmt
881 			{
882 				PLAssignStmt *n = (PLAssignStmt *) $2;
883 				n->nnames = 2;
884 				pg_yyget_extra(yyscanner)->parsetree =
885 					list_make1(makeRawStmt((Node *) n, 0));
886 			}
887 			| MODE_PLPGSQL_ASSIGN3 PLAssignStmt
888 			{
889 				PLAssignStmt *n = (PLAssignStmt *) $2;
890 				n->nnames = 3;
891 				pg_yyget_extra(yyscanner)->parsetree =
892 					list_make1(makeRawStmt((Node *) n, 0));
893 			}
894 		;
895 
896 /*
897  * At top level, we wrap each stmt with a RawStmt node carrying start location
898  * and length of the stmt's text.  Notice that the start loc/len are driven
899  * entirely from semicolon locations (@2).  It would seem natural to use
900  * @1 or @3 to get the true start location of a stmt, but that doesn't work
901  * for statements that can start with empty nonterminals (opt_with_clause is
902  * the main offender here); as noted in the comments for YYLLOC_DEFAULT,
903  * we'd get -1 for the location in such cases.
904  * We also take care to discard empty statements entirely.
905  */
906 stmtmulti:	stmtmulti ';' toplevel_stmt
907 				{
908 					if ($1 != NIL)
909 					{
910 						/* update length of previous stmt */
911 						updateRawStmtEnd(llast_node(RawStmt, $1), @2);
912 					}
913 					if ($3 != NULL)
914 						$$ = lappend($1, makeRawStmt($3, @2 + 1));
915 					else
916 						$$ = $1;
917 				}
918 			| toplevel_stmt
919 				{
920 					if ($1 != NULL)
921 						$$ = list_make1(makeRawStmt($1, 0));
922 					else
923 						$$ = NIL;
924 				}
925 		;
926 
927 /*
928  * toplevel_stmt includes BEGIN and END.  stmt does not include them, because
929  * those words have different meanings in function bodys.
930  */
931 toplevel_stmt:
932 			stmt
933 			| TransactionStmtLegacy
934 		;
935 
936 stmt:
937 			AlterEventTrigStmt
938 			| AlterCollationStmt
939 			| AlterDatabaseStmt
940 			| AlterDatabaseSetStmt
941 			| AlterDefaultPrivilegesStmt
942 			| AlterDomainStmt
943 			| AlterEnumStmt
944 			| AlterExtensionStmt
945 			| AlterExtensionContentsStmt
946 			| AlterFdwStmt
947 			| AlterForeignServerStmt
948 			| AlterFunctionStmt
949 			| AlterGroupStmt
950 			| AlterObjectDependsStmt
951 			| AlterObjectSchemaStmt
952 			| AlterOwnerStmt
953 			| AlterOperatorStmt
954 			| AlterTypeStmt
955 			| AlterPolicyStmt
956 			| AlterSeqStmt
957 			| AlterSystemStmt
958 			| AlterTableStmt
959 			| AlterTblSpcStmt
960 			| AlterCompositeTypeStmt
961 			| AlterPublicationStmt
962 			| AlterRoleSetStmt
963 			| AlterRoleStmt
964 			| AlterSubscriptionStmt
965 			| AlterStatsStmt
966 			| AlterTSConfigurationStmt
967 			| AlterTSDictionaryStmt
968 			| AlterUserMappingStmt
969 			| AnalyzeStmt
970 			| CallStmt
971 			| CheckPointStmt
972 			| ClosePortalStmt
973 			| ClusterStmt
974 			| CommentStmt
975 			| ConstraintsSetStmt
976 			| CopyStmt
977 			| CreateAmStmt
978 			| CreateAsStmt
979 			| CreateAssertionStmt
980 			| CreateCastStmt
981 			| CreateConversionStmt
982 			| CreateDomainStmt
983 			| CreateExtensionStmt
984 			| CreateFdwStmt
985 			| CreateForeignServerStmt
986 			| CreateForeignTableStmt
987 			| CreateFunctionStmt
988 			| CreateGroupStmt
989 			| CreateMatViewStmt
990 			| CreateOpClassStmt
991 			| CreateOpFamilyStmt
992 			| CreatePublicationStmt
993 			| AlterOpFamilyStmt
994 			| CreatePolicyStmt
995 			| CreatePLangStmt
996 			| CreateSchemaStmt
997 			| CreateSeqStmt
998 			| CreateStmt
999 			| CreateSubscriptionStmt
1000 			| CreateStatsStmt
1001 			| CreateTableSpaceStmt
1002 			| CreateTransformStmt
1003 			| CreateTrigStmt
1004 			| CreateEventTrigStmt
1005 			| CreateRoleStmt
1006 			| CreateUserStmt
1007 			| CreateUserMappingStmt
1008 			| CreatedbStmt
1009 			| DeallocateStmt
1010 			| DeclareCursorStmt
1011 			| DefineStmt
1012 			| DeleteStmt
1013 			| DiscardStmt
1014 			| DoStmt
1015 			| DropCastStmt
1016 			| DropOpClassStmt
1017 			| DropOpFamilyStmt
1018 			| DropOwnedStmt
1019 			| DropStmt
1020 			| DropSubscriptionStmt
1021 			| DropTableSpaceStmt
1022 			| DropTransformStmt
1023 			| DropRoleStmt
1024 			| DropUserMappingStmt
1025 			| DropdbStmt
1026 			| ExecuteStmt
1027 			| ExplainStmt
1028 			| FetchStmt
1029 			| GrantStmt
1030 			| GrantRoleStmt
1031 			| ImportForeignSchemaStmt
1032 			| IndexStmt
1033 			| InsertStmt
1034 			| ListenStmt
1035 			| RefreshMatViewStmt
1036 			| LoadStmt
1037 			| LockStmt
1038 			| NotifyStmt
1039 			| PrepareStmt
1040 			| ReassignOwnedStmt
1041 			| ReindexStmt
1042 			| RemoveAggrStmt
1043 			| RemoveFuncStmt
1044 			| RemoveOperStmt
1045 			| RenameStmt
1046 			| RevokeStmt
1047 			| RevokeRoleStmt
1048 			| RuleStmt
1049 			| SecLabelStmt
1050 			| SelectStmt
1051 			| TransactionStmt
1052 			| TruncateStmt
1053 			| UnlistenStmt
1054 			| UpdateStmt
1055 			| VacuumStmt
1056 			| VariableResetStmt
1057 			| VariableSetStmt
1058 			| VariableShowStmt
1059 			| ViewStmt
1060 			| /*EMPTY*/
1061 				{ $$ = NULL; }
1062 		;
1063 
1064 /*****************************************************************************
1065  *
1066  * CALL statement
1067  *
1068  *****************************************************************************/
1069 
1070 CallStmt:	CALL func_application
1071 				{
1072 					CallStmt *n = makeNode(CallStmt);
1073 					n->funccall = castNode(FuncCall, $2);
1074 					$$ = (Node *)n;
1075 				}
1076 		;
1077 
1078 /*****************************************************************************
1079  *
1080  * Create a new Postgres DBMS role
1081  *
1082  *****************************************************************************/
1083 
1084 CreateRoleStmt:
1085 			CREATE ROLE RoleId opt_with OptRoleList
1086 				{
1087 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1088 					n->stmt_type = ROLESTMT_ROLE;
1089 					n->role = $3;
1090 					n->options = $5;
1091 					$$ = (Node *)n;
1092 				}
1093 		;
1094 
1095 
1096 opt_with:	WITH
1097 			| WITH_LA
1098 			| /*EMPTY*/
1099 		;
1100 
1101 /*
1102  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
1103  * for backwards compatibility).  Note: the only option required by SQL99
1104  * is "WITH ADMIN name".
1105  */
1106 OptRoleList:
1107 			OptRoleList CreateOptRoleElem			{ $$ = lappend($1, $2); }
1108 			| /* EMPTY */							{ $$ = NIL; }
1109 		;
1110 
1111 AlterOptRoleList:
1112 			AlterOptRoleList AlterOptRoleElem		{ $$ = lappend($1, $2); }
1113 			| /* EMPTY */							{ $$ = NIL; }
1114 		;
1115 
1116 AlterOptRoleElem:
1117 			PASSWORD Sconst
1118 				{
1119 					$$ = makeDefElem("password",
1120 									 (Node *)makeString($2), @1);
1121 				}
1122 			| PASSWORD NULL_P
1123 				{
1124 					$$ = makeDefElem("password", NULL, @1);
1125 				}
1126 			| ENCRYPTED PASSWORD Sconst
1127 				{
1128 					/*
1129 					 * These days, passwords are always stored in encrypted
1130 					 * form, so there is no difference between PASSWORD and
1131 					 * ENCRYPTED PASSWORD.
1132 					 */
1133 					$$ = makeDefElem("password",
1134 									 (Node *)makeString($3), @1);
1135 				}
1136 			| UNENCRYPTED PASSWORD Sconst
1137 				{
1138 					ereport(ERROR,
1139 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1140 							 errmsg("UNENCRYPTED PASSWORD is no longer supported"),
1141 							 errhint("Remove UNENCRYPTED to store the password in encrypted form instead."),
1142 							 parser_errposition(@1)));
1143 				}
1144 			| INHERIT
1145 				{
1146 					$$ = makeDefElem("inherit", (Node *)makeInteger(true), @1);
1147 				}
1148 			| CONNECTION LIMIT SignedIconst
1149 				{
1150 					$$ = makeDefElem("connectionlimit", (Node *)makeInteger($3), @1);
1151 				}
1152 			| VALID UNTIL Sconst
1153 				{
1154 					$$ = makeDefElem("validUntil", (Node *)makeString($3), @1);
1155 				}
1156 		/*	Supported but not documented for roles, for use by ALTER GROUP. */
1157 			| USER role_list
1158 				{
1159 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1160 				}
1161 			| IDENT
1162 				{
1163 					/*
1164 					 * We handle identifiers that aren't parser keywords with
1165 					 * the following special-case codes, to avoid bloating the
1166 					 * size of the main parser.
1167 					 */
1168 					if (strcmp($1, "superuser") == 0)
1169 						$$ = makeDefElem("superuser", (Node *)makeInteger(true), @1);
1170 					else if (strcmp($1, "nosuperuser") == 0)
1171 						$$ = makeDefElem("superuser", (Node *)makeInteger(false), @1);
1172 					else if (strcmp($1, "createrole") == 0)
1173 						$$ = makeDefElem("createrole", (Node *)makeInteger(true), @1);
1174 					else if (strcmp($1, "nocreaterole") == 0)
1175 						$$ = makeDefElem("createrole", (Node *)makeInteger(false), @1);
1176 					else if (strcmp($1, "replication") == 0)
1177 						$$ = makeDefElem("isreplication", (Node *)makeInteger(true), @1);
1178 					else if (strcmp($1, "noreplication") == 0)
1179 						$$ = makeDefElem("isreplication", (Node *)makeInteger(false), @1);
1180 					else if (strcmp($1, "createdb") == 0)
1181 						$$ = makeDefElem("createdb", (Node *)makeInteger(true), @1);
1182 					else if (strcmp($1, "nocreatedb") == 0)
1183 						$$ = makeDefElem("createdb", (Node *)makeInteger(false), @1);
1184 					else if (strcmp($1, "login") == 0)
1185 						$$ = makeDefElem("canlogin", (Node *)makeInteger(true), @1);
1186 					else if (strcmp($1, "nologin") == 0)
1187 						$$ = makeDefElem("canlogin", (Node *)makeInteger(false), @1);
1188 					else if (strcmp($1, "bypassrls") == 0)
1189 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(true), @1);
1190 					else if (strcmp($1, "nobypassrls") == 0)
1191 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(false), @1);
1192 					else if (strcmp($1, "noinherit") == 0)
1193 					{
1194 						/*
1195 						 * Note that INHERIT is a keyword, so it's handled by main parser, but
1196 						 * NOINHERIT is handled here.
1197 						 */
1198 						$$ = makeDefElem("inherit", (Node *)makeInteger(false), @1);
1199 					}
1200 					else
1201 						ereport(ERROR,
1202 								(errcode(ERRCODE_SYNTAX_ERROR),
1203 								 errmsg("unrecognized role option \"%s\"", $1),
1204 									 parser_errposition(@1)));
1205 				}
1206 		;
1207 
1208 CreateOptRoleElem:
1209 			AlterOptRoleElem			{ $$ = $1; }
1210 			/* The following are not supported by ALTER ROLE/USER/GROUP */
1211 			| SYSID Iconst
1212 				{
1213 					$$ = makeDefElem("sysid", (Node *)makeInteger($2), @1);
1214 				}
1215 			| ADMIN role_list
1216 				{
1217 					$$ = makeDefElem("adminmembers", (Node *)$2, @1);
1218 				}
1219 			| ROLE role_list
1220 				{
1221 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1222 				}
1223 			| IN_P ROLE role_list
1224 				{
1225 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1226 				}
1227 			| IN_P GROUP_P role_list
1228 				{
1229 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1230 				}
1231 		;
1232 
1233 
1234 /*****************************************************************************
1235  *
1236  * Create a new Postgres DBMS user (role with implied login ability)
1237  *
1238  *****************************************************************************/
1239 
1240 CreateUserStmt:
1241 			CREATE USER RoleId opt_with OptRoleList
1242 				{
1243 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1244 					n->stmt_type = ROLESTMT_USER;
1245 					n->role = $3;
1246 					n->options = $5;
1247 					$$ = (Node *)n;
1248 				}
1249 		;
1250 
1251 
1252 /*****************************************************************************
1253  *
1254  * Alter a postgresql DBMS role
1255  *
1256  *****************************************************************************/
1257 
1258 AlterRoleStmt:
1259 			ALTER ROLE RoleSpec opt_with AlterOptRoleList
1260 				 {
1261 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1262 					n->role = $3;
1263 					n->action = +1;	/* add, if there are members */
1264 					n->options = $5;
1265 					$$ = (Node *)n;
1266 				 }
1267 			| ALTER USER RoleSpec opt_with AlterOptRoleList
1268 				 {
1269 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1270 					n->role = $3;
1271 					n->action = +1;	/* add, if there are members */
1272 					n->options = $5;
1273 					$$ = (Node *)n;
1274 				 }
1275 		;
1276 
1277 opt_in_database:
1278 			   /* EMPTY */					{ $$ = NULL; }
1279 			| IN_P DATABASE name	{ $$ = $3; }
1280 		;
1281 
1282 AlterRoleSetStmt:
1283 			ALTER ROLE RoleSpec opt_in_database SetResetClause
1284 				{
1285 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1286 					n->role = $3;
1287 					n->database = $4;
1288 					n->setstmt = $5;
1289 					$$ = (Node *)n;
1290 				}
1291 			| ALTER ROLE ALL opt_in_database SetResetClause
1292 				{
1293 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1294 					n->role = NULL;
1295 					n->database = $4;
1296 					n->setstmt = $5;
1297 					$$ = (Node *)n;
1298 				}
1299 			| ALTER USER RoleSpec opt_in_database SetResetClause
1300 				{
1301 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1302 					n->role = $3;
1303 					n->database = $4;
1304 					n->setstmt = $5;
1305 					$$ = (Node *)n;
1306 				}
1307 			| ALTER USER ALL opt_in_database SetResetClause
1308 				{
1309 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1310 					n->role = NULL;
1311 					n->database = $4;
1312 					n->setstmt = $5;
1313 					$$ = (Node *)n;
1314 				}
1315 		;
1316 
1317 
1318 /*****************************************************************************
1319  *
1320  * Drop a postgresql DBMS role
1321  *
1322  * XXX Ideally this would have CASCADE/RESTRICT options, but a role
1323  * might own objects in multiple databases, and there is presently no way to
1324  * implement cascading to other databases.  So we always behave as RESTRICT.
1325  *****************************************************************************/
1326 
1327 DropRoleStmt:
1328 			DROP ROLE role_list
1329 				{
1330 					DropRoleStmt *n = makeNode(DropRoleStmt);
1331 					n->missing_ok = false;
1332 					n->roles = $3;
1333 					$$ = (Node *)n;
1334 				}
1335 			| DROP ROLE IF_P EXISTS role_list
1336 				{
1337 					DropRoleStmt *n = makeNode(DropRoleStmt);
1338 					n->missing_ok = true;
1339 					n->roles = $5;
1340 					$$ = (Node *)n;
1341 				}
1342 			| DROP USER role_list
1343 				{
1344 					DropRoleStmt *n = makeNode(DropRoleStmt);
1345 					n->missing_ok = false;
1346 					n->roles = $3;
1347 					$$ = (Node *)n;
1348 				}
1349 			| DROP USER IF_P EXISTS role_list
1350 				{
1351 					DropRoleStmt *n = makeNode(DropRoleStmt);
1352 					n->roles = $5;
1353 					n->missing_ok = true;
1354 					$$ = (Node *)n;
1355 				}
1356 			| DROP GROUP_P role_list
1357 				{
1358 					DropRoleStmt *n = makeNode(DropRoleStmt);
1359 					n->missing_ok = false;
1360 					n->roles = $3;
1361 					$$ = (Node *)n;
1362 				}
1363 			| DROP GROUP_P IF_P EXISTS role_list
1364 				{
1365 					DropRoleStmt *n = makeNode(DropRoleStmt);
1366 					n->missing_ok = true;
1367 					n->roles = $5;
1368 					$$ = (Node *)n;
1369 				}
1370 			;
1371 
1372 
1373 /*****************************************************************************
1374  *
1375  * Create a postgresql group (role without login ability)
1376  *
1377  *****************************************************************************/
1378 
1379 CreateGroupStmt:
1380 			CREATE GROUP_P RoleId opt_with OptRoleList
1381 				{
1382 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1383 					n->stmt_type = ROLESTMT_GROUP;
1384 					n->role = $3;
1385 					n->options = $5;
1386 					$$ = (Node *)n;
1387 				}
1388 		;
1389 
1390 
1391 /*****************************************************************************
1392  *
1393  * Alter a postgresql group
1394  *
1395  *****************************************************************************/
1396 
1397 AlterGroupStmt:
1398 			ALTER GROUP_P RoleSpec add_drop USER role_list
1399 				{
1400 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1401 					n->role = $3;
1402 					n->action = $4;
1403 					n->options = list_make1(makeDefElem("rolemembers",
1404 														(Node *)$6, @6));
1405 					$$ = (Node *)n;
1406 				}
1407 		;
1408 
1409 add_drop:	ADD_P									{ $$ = +1; }
1410 			| DROP									{ $$ = -1; }
1411 		;
1412 
1413 
1414 /*****************************************************************************
1415  *
1416  * Manipulate a schema
1417  *
1418  *****************************************************************************/
1419 
1420 CreateSchemaStmt:
1421 			CREATE SCHEMA OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1422 				{
1423 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1424 					/* One can omit the schema name or the authorization id. */
1425 					n->schemaname = $3;
1426 					n->authrole = $5;
1427 					n->schemaElts = $6;
1428 					n->if_not_exists = false;
1429 					$$ = (Node *)n;
1430 				}
1431 			| CREATE SCHEMA ColId OptSchemaEltList
1432 				{
1433 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1434 					/* ...but not both */
1435 					n->schemaname = $3;
1436 					n->authrole = NULL;
1437 					n->schemaElts = $4;
1438 					n->if_not_exists = false;
1439 					$$ = (Node *)n;
1440 				}
1441 			| CREATE SCHEMA IF_P NOT EXISTS OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1442 				{
1443 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1444 					/* schema name can be omitted here, too */
1445 					n->schemaname = $6;
1446 					n->authrole = $8;
1447 					if ($9 != NIL)
1448 						ereport(ERROR,
1449 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1450 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1451 								 parser_errposition(@9)));
1452 					n->schemaElts = $9;
1453 					n->if_not_exists = true;
1454 					$$ = (Node *)n;
1455 				}
1456 			| CREATE SCHEMA IF_P NOT EXISTS ColId OptSchemaEltList
1457 				{
1458 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1459 					/* ...but not here */
1460 					n->schemaname = $6;
1461 					n->authrole = NULL;
1462 					if ($7 != NIL)
1463 						ereport(ERROR,
1464 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1465 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1466 								 parser_errposition(@7)));
1467 					n->schemaElts = $7;
1468 					n->if_not_exists = true;
1469 					$$ = (Node *)n;
1470 				}
1471 		;
1472 
1473 OptSchemaName:
1474 			ColId									{ $$ = $1; }
1475 			| /* EMPTY */							{ $$ = NULL; }
1476 		;
1477 
1478 OptSchemaEltList:
1479 			OptSchemaEltList schema_stmt
1480 				{
1481 					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
1482 						@$ = @2;
1483 					$$ = lappend($1, $2);
1484 				}
1485 			| /* EMPTY */
1486 				{ $$ = NIL; }
1487 		;
1488 
1489 /*
1490  *	schema_stmt are the ones that can show up inside a CREATE SCHEMA
1491  *	statement (in addition to by themselves).
1492  */
1493 schema_stmt:
1494 			CreateStmt
1495 			| IndexStmt
1496 			| CreateSeqStmt
1497 			| CreateTrigStmt
1498 			| GrantStmt
1499 			| ViewStmt
1500 		;
1501 
1502 
1503 /*****************************************************************************
1504  *
1505  * Set PG internal variable
1506  *	  SET name TO 'var_value'
1507  * Include SQL syntax (thomas 1997-10-22):
1508  *	  SET TIME ZONE 'var_value'
1509  *
1510  *****************************************************************************/
1511 
1512 VariableSetStmt:
1513 			PGPOOL SET generic_set
1514 				{
1515 					VariableSetStmt *n = $3;
1516 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep changes minimum */
1517 					n->is_local = false;
1518 					$$ = (Node *) n;
1519 				}
1520 			| SET set_rest
1521 				{
1522 					VariableSetStmt *n = $2;
1523 					n->is_local = false;
1524 					$$ = (Node *) n;
1525 				}
1526 			| SET LOCAL set_rest
1527 				{
1528 					VariableSetStmt *n = $3;
1529 					n->is_local = true;
1530 					$$ = (Node *) n;
1531 				}
1532 			| SET SESSION set_rest
1533 				{
1534 					VariableSetStmt *n = $3;
1535 					n->is_local = false;
1536 					$$ = (Node *) n;
1537 				}
1538 		;
1539 
1540 set_rest:
1541 			TRANSACTION transaction_mode_list
1542 				{
1543 					VariableSetStmt *n = makeNode(VariableSetStmt);
1544 					n->kind = VAR_SET_MULTI;
1545 					n->name = "TRANSACTION";
1546 					n->args = $2;
1547 					$$ = n;
1548 				}
1549 			| SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1550 				{
1551 					VariableSetStmt *n = makeNode(VariableSetStmt);
1552 					n->kind = VAR_SET_MULTI;
1553 					n->name = "SESSION CHARACTERISTICS";
1554 					n->args = $5;
1555 					$$ = n;
1556 				}
1557 			| set_rest_more
1558 			;
1559 
1560 generic_set:
1561 			var_name TO var_list
1562 				{
1563 					VariableSetStmt *n = makeNode(VariableSetStmt);
1564 					n->kind = VAR_SET_VALUE;
1565 					n->name = $1;
1566 					n->args = $3;
1567 					$$ = n;
1568 				}
1569 			| var_name '=' var_list
1570 				{
1571 					VariableSetStmt *n = makeNode(VariableSetStmt);
1572 					n->kind = VAR_SET_VALUE;
1573 					n->name = $1;
1574 					n->args = $3;
1575 					$$ = n;
1576 				}
1577 			| var_name TO DEFAULT
1578 				{
1579 					VariableSetStmt *n = makeNode(VariableSetStmt);
1580 					n->kind = VAR_SET_DEFAULT;
1581 					n->name = $1;
1582 					$$ = n;
1583 				}
1584 			| var_name '=' DEFAULT
1585 				{
1586 					VariableSetStmt *n = makeNode(VariableSetStmt);
1587 					n->kind = VAR_SET_DEFAULT;
1588 					n->name = $1;
1589 					$$ = n;
1590 				}
1591 		;
1592 
1593 set_rest_more:	/* Generic SET syntaxes: */
1594 			generic_set							{$$ = $1;}
1595 			| var_name FROM CURRENT_P
1596 				{
1597 					VariableSetStmt *n = makeNode(VariableSetStmt);
1598 					n->kind = VAR_SET_CURRENT;
1599 					n->name = $1;
1600 					$$ = n;
1601 				}
1602 			/* Special syntaxes mandated by SQL standard: */
1603 			| TIME ZONE zone_value
1604 				{
1605 					VariableSetStmt *n = makeNode(VariableSetStmt);
1606 					n->kind = VAR_SET_VALUE;
1607 					n->name = "timezone";
1608 					if ($3 != NULL)
1609 						n->args = list_make1($3);
1610 					else
1611 						n->kind = VAR_SET_DEFAULT;
1612 					$$ = n;
1613 				}
1614 			| CATALOG_P Sconst
1615 				{
1616 					ereport(ERROR,
1617 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1618 							 errmsg("current database cannot be changed"),
1619 							 parser_errposition(@2)));
1620 					$$ = NULL; /*not reached*/
1621 				}
1622 			| SCHEMA Sconst
1623 				{
1624 					VariableSetStmt *n = makeNode(VariableSetStmt);
1625 					n->kind = VAR_SET_VALUE;
1626 					n->name = "search_path";
1627 					n->args = list_make1(makeStringConst($2, @2));
1628 					$$ = n;
1629 				}
1630 			| NAMES opt_encoding
1631 				{
1632 					VariableSetStmt *n = makeNode(VariableSetStmt);
1633 					n->kind = VAR_SET_VALUE;
1634 					n->name = "client_encoding";
1635 					if ($2 != NULL)
1636 						n->args = list_make1(makeStringConst($2, @2));
1637 					else
1638 						n->kind = VAR_SET_DEFAULT;
1639 					$$ = n;
1640 				}
1641 			| ROLE NonReservedWord_or_Sconst
1642 				{
1643 					VariableSetStmt *n = makeNode(VariableSetStmt);
1644 					n->kind = VAR_SET_VALUE;
1645 					n->name = "role";
1646 					n->args = list_make1(makeStringConst($2, @2));
1647 					$$ = n;
1648 				}
1649 			| SESSION AUTHORIZATION NonReservedWord_or_Sconst
1650 				{
1651 					VariableSetStmt *n = makeNode(VariableSetStmt);
1652 					n->kind = VAR_SET_VALUE;
1653 					n->name = "session_authorization";
1654 					n->args = list_make1(makeStringConst($3, @3));
1655 					$$ = n;
1656 				}
1657 			| SESSION AUTHORIZATION DEFAULT
1658 				{
1659 					VariableSetStmt *n = makeNode(VariableSetStmt);
1660 					n->kind = VAR_SET_DEFAULT;
1661 					n->name = "session_authorization";
1662 					$$ = n;
1663 				}
1664 			| XML_P OPTION document_or_content
1665 				{
1666 					VariableSetStmt *n = makeNode(VariableSetStmt);
1667 					n->kind = VAR_SET_VALUE;
1668 					n->name = "xmloption";
1669 					n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1670 					$$ = n;
1671 				}
1672 			/* Special syntaxes invented by PostgreSQL: */
1673 			| TRANSACTION SNAPSHOT Sconst
1674 				{
1675 					VariableSetStmt *n = makeNode(VariableSetStmt);
1676 					n->kind = VAR_SET_MULTI;
1677 					n->name = "TRANSACTION SNAPSHOT";
1678 					n->args = list_make1(makeStringConst($3, @3));
1679 					$$ = n;
1680 				}
1681 		;
1682 
1683 var_name:	ColId								{ $$ = $1; }
1684 			| var_name '.' ColId
1685 				{ $$ = psprintf("%s.%s", $1, $3); }
1686 		;
1687 
1688 var_list:	var_value								{ $$ = list_make1($1); }
1689 			| var_list ',' var_value				{ $$ = lappend($1, $3); }
1690 		;
1691 
1692 var_value:	opt_boolean_or_string
1693 				{ $$ = makeStringConst($1, @1); }
1694 			| NumericOnly
1695 				{ $$ = makeAConst($1, @1); }
1696 		;
1697 
1698 iso_level:	READ UNCOMMITTED						{ $$ = "read uncommitted"; }
1699 			| READ COMMITTED						{ $$ = "read committed"; }
1700 			| REPEATABLE READ						{ $$ = "repeatable read"; }
1701 			| SERIALIZABLE							{ $$ = "serializable"; }
1702 		;
1703 
1704 opt_boolean_or_string:
1705 			TRUE_P									{ $$ = "true"; }
1706 			| FALSE_P								{ $$ = "false"; }
1707 			| ON									{ $$ = "on"; }
1708 			/*
1709 			 * OFF is also accepted as a boolean value, but is handled by
1710 			 * the NonReservedWord rule.  The action for booleans and strings
1711 			 * is the same, so we don't need to distinguish them here.
1712 			 */
1713 			| NonReservedWord_or_Sconst				{ $$ = $1; }
1714 		;
1715 
1716 /* Timezone values can be:
1717  * - a string such as 'pst8pdt'
1718  * - an identifier such as "pst8pdt"
1719  * - an integer or floating point number
1720  * - a time interval per SQL99
1721  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1722  * so use IDENT (meaning we reject anything that is a key word).
1723  */
1724 zone_value:
1725 			Sconst
1726 				{
1727 					$$ = makeStringConst($1, @1);
1728 				}
1729 			| IDENT
1730 				{
1731 					$$ = makeStringConst($1, @1);
1732 				}
1733 			| ConstInterval Sconst opt_interval
1734 				{
1735 					TypeName *t = $1;
1736 					if ($3 != NIL)
1737 					{
1738 						A_Const *n = (A_Const *) linitial($3);
1739 						if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1740 							ereport(ERROR,
1741 									(errcode(ERRCODE_SYNTAX_ERROR),
1742 									 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1743 									 parser_errposition(@3)));
1744 					}
1745 					t->typmods = $3;
1746 					$$ = makeStringConstCast($2, @2, t);
1747 				}
1748 			| ConstInterval '(' Iconst ')' Sconst
1749 				{
1750 					TypeName *t = $1;
1751 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1752 											makeIntConst($3, @3));
1753 					$$ = makeStringConstCast($5, @5, t);
1754 				}
1755 			| NumericOnly							{ $$ = makeAConst($1, @1); }
1756 			| DEFAULT								{ $$ = NULL; }
1757 			| LOCAL									{ $$ = NULL; }
1758 		;
1759 
1760 opt_encoding:
1761 			Sconst									{ $$ = $1; }
1762 			| DEFAULT								{ $$ = NULL; }
1763 			| /*EMPTY*/								{ $$ = NULL; }
1764 		;
1765 
1766 NonReservedWord_or_Sconst:
1767 			NonReservedWord							{ $$ = $1; }
1768 			| Sconst								{ $$ = $1; }
1769 		;
1770 
1771 VariableResetStmt:
1772 			RESET reset_rest						{ $$ = (Node *) $2; }
1773 			| PGPOOL RESET generic_reset
1774 				{
1775 					VariableSetStmt *n = $3;
1776 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep the changes minimum */
1777 					$$ = (Node *) n;
1778 				}
1779 		;
1780 
1781 reset_rest:
1782 			generic_reset							{ $$ = $1; }
1783 			| TIME ZONE
1784 				{
1785 					VariableSetStmt *n = makeNode(VariableSetStmt);
1786 					n->kind = VAR_RESET;
1787 					n->name = "timezone";
1788 					$$ = n;
1789 				}
1790 			| TRANSACTION ISOLATION LEVEL
1791 				{
1792 					VariableSetStmt *n = makeNode(VariableSetStmt);
1793 					n->kind = VAR_RESET;
1794 					n->name = "transaction_isolation";
1795 					$$ = n;
1796 				}
1797 			| SESSION AUTHORIZATION
1798 				{
1799 					VariableSetStmt *n = makeNode(VariableSetStmt);
1800 					n->kind = VAR_RESET;
1801 					n->name = "session_authorization";
1802 					$$ = n;
1803 				}
1804 		;
1805 
1806 generic_reset:
1807 			var_name
1808 				{
1809 					VariableSetStmt *n = makeNode(VariableSetStmt);
1810 					n->kind = VAR_RESET;
1811 					n->name = $1;
1812 					$$ = n;
1813 				}
1814 			| ALL
1815 				{
1816 					VariableSetStmt *n = makeNode(VariableSetStmt);
1817 					n->kind = VAR_RESET_ALL;
1818 					$$ = n;
1819 				}
1820 		;
1821 
1822 /* SetResetClause allows SET or RESET without LOCAL */
1823 SetResetClause:
1824 			SET set_rest					{ $$ = $2; }
1825 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1826 		;
1827 
1828 /* SetResetClause allows SET or RESET without LOCAL */
1829 FunctionSetResetClause:
1830 			SET set_rest_more				{ $$ = $2; }
1831 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1832 		;
1833 
1834 
1835 VariableShowStmt:
1836 			/* pgpool extension */
1837 			PGPOOL SHOW var_name
1838 			{
1839 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1840 				n->name = $3;
1841 				$$ = (Node *) n;
1842 			}
1843 			| PGPOOL SHOW ALL
1844 			{
1845 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1846 				n->name = "all";
1847 				$$ = (Node *) n;
1848 			}
1849 			| SHOW var_name
1850 				{
1851 					VariableShowStmt *n = makeNode(VariableShowStmt);
1852 					n->name = $2;
1853 					$$ = (Node *) n;
1854 				}
1855 			| SHOW TIME ZONE
1856 				{
1857 					VariableShowStmt *n = makeNode(VariableShowStmt);
1858 					n->name = "timezone";
1859 					$$ = (Node *) n;
1860 				}
1861 			| SHOW TRANSACTION ISOLATION LEVEL
1862 				{
1863 					VariableShowStmt *n = makeNode(VariableShowStmt);
1864 					n->name = "transaction_isolation";
1865 					$$ = (Node *) n;
1866 				}
1867 			| SHOW SESSION AUTHORIZATION
1868 				{
1869 					VariableShowStmt *n = makeNode(VariableShowStmt);
1870 					n->name = "session_authorization";
1871 					$$ = (Node *) n;
1872 				}
1873 			| SHOW ALL
1874 				{
1875 					VariableShowStmt *n = makeNode(VariableShowStmt);
1876 					n->name = "all";
1877 					$$ = (Node *) n;
1878 				}
1879 		;
1880 
1881 
1882 ConstraintsSetStmt:
1883 			SET CONSTRAINTS constraints_set_list constraints_set_mode
1884 				{
1885 					ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1886 					n->constraints = $3;
1887 					n->deferred = $4;
1888 					$$ = (Node *) n;
1889 				}
1890 		;
1891 
1892 constraints_set_list:
1893 			ALL										{ $$ = NIL; }
1894 			| qualified_name_list					{ $$ = $1; }
1895 		;
1896 
1897 constraints_set_mode:
1898 			DEFERRED								{ $$ = true; }
1899 			| IMMEDIATE								{ $$ = false; }
1900 		;
1901 
1902 
1903 /*
1904  * Checkpoint statement
1905  */
1906 CheckPointStmt:
1907 			CHECKPOINT
1908 				{
1909 					CheckPointStmt *n = makeNode(CheckPointStmt);
1910 					$$ = (Node *)n;
1911 				}
1912 		;
1913 
1914 
1915 /*****************************************************************************
1916  *
1917  * DISCARD { ALL | TEMP | PLANS | SEQUENCES }
1918  *
1919  *****************************************************************************/
1920 
1921 DiscardStmt:
1922 			DISCARD ALL
1923 				{
1924 					DiscardStmt *n = makeNode(DiscardStmt);
1925 					n->target = DISCARD_ALL;
1926 					$$ = (Node *) n;
1927 				}
1928 			| DISCARD TEMP
1929 				{
1930 					DiscardStmt *n = makeNode(DiscardStmt);
1931 					n->target = DISCARD_TEMP;
1932 					$$ = (Node *) n;
1933 				}
1934 			| DISCARD TEMPORARY
1935 				{
1936 					DiscardStmt *n = makeNode(DiscardStmt);
1937 					n->target = DISCARD_TEMP;
1938 					$$ = (Node *) n;
1939 				}
1940 			| DISCARD PLANS
1941 				{
1942 					DiscardStmt *n = makeNode(DiscardStmt);
1943 					n->target = DISCARD_PLANS;
1944 					$$ = (Node *) n;
1945 				}
1946 			| DISCARD SEQUENCES
1947 				{
1948 					DiscardStmt *n = makeNode(DiscardStmt);
1949 					n->target = DISCARD_SEQUENCES;
1950 					$$ = (Node *) n;
1951 				}
1952 
1953 		;
1954 
1955 
1956 /*****************************************************************************
1957  *
1958  *	ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW | FOREIGN TABLE ] variations
1959  *
1960  * Note: we accept all subcommands for each of the variants, and sort
1961  * out what's really legal at execution time.
1962  *****************************************************************************/
1963 
1964 AlterTableStmt:
1965 			ALTER TABLE relation_expr alter_table_cmds
1966 				{
1967 					AlterTableStmt *n = makeNode(AlterTableStmt);
1968 					n->relation = $3;
1969 					n->cmds = $4;
1970 					n->objtype = OBJECT_TABLE;
1971 					n->missing_ok = false;
1972 					$$ = (Node *)n;
1973 				}
1974 		|	ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
1975 				{
1976 					AlterTableStmt *n = makeNode(AlterTableStmt);
1977 					n->relation = $5;
1978 					n->cmds = $6;
1979 					n->objtype = OBJECT_TABLE;
1980 					n->missing_ok = true;
1981 					$$ = (Node *)n;
1982 				}
1983 		|	ALTER TABLE relation_expr partition_cmd
1984 				{
1985 					AlterTableStmt *n = makeNode(AlterTableStmt);
1986 					n->relation = $3;
1987 					n->cmds = list_make1($4);
1988 					n->objtype = OBJECT_TABLE;
1989 					n->missing_ok = false;
1990 					$$ = (Node *)n;
1991 				}
1992 		|	ALTER TABLE IF_P EXISTS relation_expr partition_cmd
1993 				{
1994 					AlterTableStmt *n = makeNode(AlterTableStmt);
1995 					n->relation = $5;
1996 					n->cmds = list_make1($6);
1997 					n->objtype = OBJECT_TABLE;
1998 					n->missing_ok = true;
1999 					$$ = (Node *)n;
2000 				}
2001 		|	ALTER TABLE ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
2002 				{
2003 					AlterTableMoveAllStmt *n =
2004 						makeNode(AlterTableMoveAllStmt);
2005 					n->orig_tablespacename = $6;
2006 					n->objtype = OBJECT_TABLE;
2007 					n->roles = NIL;
2008 					n->new_tablespacename = $9;
2009 					n->nowait = $10;
2010 					$$ = (Node *)n;
2011 				}
2012 		|	ALTER TABLE ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
2013 				{
2014 					AlterTableMoveAllStmt *n =
2015 						makeNode(AlterTableMoveAllStmt);
2016 					n->orig_tablespacename = $6;
2017 					n->objtype = OBJECT_TABLE;
2018 					n->roles = $9;
2019 					n->new_tablespacename = $12;
2020 					n->nowait = $13;
2021 					$$ = (Node *)n;
2022 				}
2023 		|	ALTER INDEX qualified_name alter_table_cmds
2024 				{
2025 					AlterTableStmt *n = makeNode(AlterTableStmt);
2026 					n->relation = $3;
2027 					n->cmds = $4;
2028 					n->objtype = OBJECT_INDEX;
2029 					n->missing_ok = false;
2030 					$$ = (Node *)n;
2031 				}
2032 		|	ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
2033 				{
2034 					AlterTableStmt *n = makeNode(AlterTableStmt);
2035 					n->relation = $5;
2036 					n->cmds = $6;
2037 					n->objtype = OBJECT_INDEX;
2038 					n->missing_ok = true;
2039 					$$ = (Node *)n;
2040 				}
2041 		|	ALTER INDEX qualified_name index_partition_cmd
2042 				{
2043 					AlterTableStmt *n = makeNode(AlterTableStmt);
2044 					n->relation = $3;
2045 					n->cmds = list_make1($4);
2046 					n->objtype = OBJECT_INDEX;
2047 					n->missing_ok = false;
2048 					$$ = (Node *)n;
2049 				}
2050 		|	ALTER INDEX ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
2051 				{
2052 					AlterTableMoveAllStmt *n =
2053 						makeNode(AlterTableMoveAllStmt);
2054 					n->orig_tablespacename = $6;
2055 					n->objtype = OBJECT_INDEX;
2056 					n->roles = NIL;
2057 					n->new_tablespacename = $9;
2058 					n->nowait = $10;
2059 					$$ = (Node *)n;
2060 				}
2061 		|	ALTER INDEX ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
2062 				{
2063 					AlterTableMoveAllStmt *n =
2064 						makeNode(AlterTableMoveAllStmt);
2065 					n->orig_tablespacename = $6;
2066 					n->objtype = OBJECT_INDEX;
2067 					n->roles = $9;
2068 					n->new_tablespacename = $12;
2069 					n->nowait = $13;
2070 					$$ = (Node *)n;
2071 				}
2072 		|	ALTER SEQUENCE qualified_name alter_table_cmds
2073 				{
2074 					AlterTableStmt *n = makeNode(AlterTableStmt);
2075 					n->relation = $3;
2076 					n->cmds = $4;
2077 					n->objtype = OBJECT_SEQUENCE;
2078 					n->missing_ok = false;
2079 					$$ = (Node *)n;
2080 				}
2081 		|	ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
2082 				{
2083 					AlterTableStmt *n = makeNode(AlterTableStmt);
2084 					n->relation = $5;
2085 					n->cmds = $6;
2086 					n->objtype = OBJECT_SEQUENCE;
2087 					n->missing_ok = true;
2088 					$$ = (Node *)n;
2089 				}
2090 		|	ALTER VIEW qualified_name alter_table_cmds
2091 				{
2092 					AlterTableStmt *n = makeNode(AlterTableStmt);
2093 					n->relation = $3;
2094 					n->cmds = $4;
2095 					n->objtype = OBJECT_VIEW;
2096 					n->missing_ok = false;
2097 					$$ = (Node *)n;
2098 				}
2099 		|	ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
2100 				{
2101 					AlterTableStmt *n = makeNode(AlterTableStmt);
2102 					n->relation = $5;
2103 					n->cmds = $6;
2104 					n->objtype = OBJECT_VIEW;
2105 					n->missing_ok = true;
2106 					$$ = (Node *)n;
2107 				}
2108 		|	ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
2109 				{
2110 					AlterTableStmt *n = makeNode(AlterTableStmt);
2111 					n->relation = $4;
2112 					n->cmds = $5;
2113 					n->objtype = OBJECT_MATVIEW;
2114 					n->missing_ok = false;
2115 					$$ = (Node *)n;
2116 				}
2117 		|	ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
2118 				{
2119 					AlterTableStmt *n = makeNode(AlterTableStmt);
2120 					n->relation = $6;
2121 					n->cmds = $7;
2122 					n->objtype = OBJECT_MATVIEW;
2123 					n->missing_ok = true;
2124 					$$ = (Node *)n;
2125 				}
2126 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
2127 				{
2128 					AlterTableMoveAllStmt *n =
2129 						makeNode(AlterTableMoveAllStmt);
2130 					n->orig_tablespacename = $7;
2131 					n->objtype = OBJECT_MATVIEW;
2132 					n->roles = NIL;
2133 					n->new_tablespacename = $10;
2134 					n->nowait = $11;
2135 					$$ = (Node *)n;
2136 				}
2137 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
2138 				{
2139 					AlterTableMoveAllStmt *n =
2140 						makeNode(AlterTableMoveAllStmt);
2141 					n->orig_tablespacename = $7;
2142 					n->objtype = OBJECT_MATVIEW;
2143 					n->roles = $10;
2144 					n->new_tablespacename = $13;
2145 					n->nowait = $14;
2146 					$$ = (Node *)n;
2147 				}
2148 		|	ALTER FOREIGN TABLE relation_expr alter_table_cmds
2149 				{
2150 					AlterTableStmt *n = makeNode(AlterTableStmt);
2151 					n->relation = $4;
2152 					n->cmds = $5;
2153 					n->objtype = OBJECT_FOREIGN_TABLE;
2154 					n->missing_ok = false;
2155 					$$ = (Node *)n;
2156 				}
2157 		|	ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
2158 				{
2159 					AlterTableStmt *n = makeNode(AlterTableStmt);
2160 					n->relation = $6;
2161 					n->cmds = $7;
2162 					n->objtype = OBJECT_FOREIGN_TABLE;
2163 					n->missing_ok = true;
2164 					$$ = (Node *)n;
2165 				}
2166 		;
2167 
2168 alter_table_cmds:
2169 			alter_table_cmd							{ $$ = list_make1($1); }
2170 			| alter_table_cmds ',' alter_table_cmd	{ $$ = lappend($1, $3); }
2171 		;
2172 
2173 partition_cmd:
2174 			/* ALTER TABLE <name> ATTACH PARTITION <table_name> FOR VALUES */
2175 			ATTACH PARTITION qualified_name PartitionBoundSpec
2176 				{
2177 					AlterTableCmd *n = makeNode(AlterTableCmd);
2178 					PartitionCmd *cmd = makeNode(PartitionCmd);
2179 
2180 					n->subtype = AT_AttachPartition;
2181 					cmd->name = $3;
2182 					cmd->bound = $4;
2183 					cmd->concurrent = false;
2184 					n->def = (Node *) cmd;
2185 
2186 					$$ = (Node *) n;
2187 				}
2188 			/* ALTER TABLE <name> DETACH PARTITION <partition_name> [CONCURRENTLY] */
2189 			| DETACH PARTITION qualified_name opt_concurrently
2190 				{
2191 					AlterTableCmd *n = makeNode(AlterTableCmd);
2192 					PartitionCmd *cmd = makeNode(PartitionCmd);
2193 
2194 					n->subtype = AT_DetachPartition;
2195 					cmd->name = $3;
2196 					cmd->bound = NULL;
2197 					cmd->concurrent = $4;
2198 					n->def = (Node *) cmd;
2199 
2200 					$$ = (Node *) n;
2201 				}
2202 			| DETACH PARTITION qualified_name FINALIZE
2203 				{
2204 					AlterTableCmd *n = makeNode(AlterTableCmd);
2205 					PartitionCmd *cmd = makeNode(PartitionCmd);
2206 
2207 					n->subtype = AT_DetachPartitionFinalize;
2208 					cmd->name = $3;
2209 					cmd->bound = NULL;
2210 					cmd->concurrent = false;
2211 					n->def = (Node *) cmd;
2212 					$$ = (Node *) n;
2213 				}
2214 		;
2215 
2216 index_partition_cmd:
2217 			/* ALTER INDEX <name> ATTACH PARTITION <index_name> */
2218 			ATTACH PARTITION qualified_name
2219 				{
2220 					AlterTableCmd *n = makeNode(AlterTableCmd);
2221 					PartitionCmd *cmd = makeNode(PartitionCmd);
2222 
2223 					n->subtype = AT_AttachPartition;
2224 					cmd->name = $3;
2225 					cmd->bound = NULL;
2226 					cmd->concurrent = false;
2227 					n->def = (Node *) cmd;
2228 
2229 					$$ = (Node *) n;
2230 				}
2231 		;
2232 
2233 alter_table_cmd:
2234 			/* ALTER TABLE <name> ADD <coldef> */
2235 			ADD_P columnDef
2236 				{
2237 					AlterTableCmd *n = makeNode(AlterTableCmd);
2238 					n->subtype = AT_AddColumn;
2239 					n->def = $2;
2240 					n->missing_ok = false;
2241 					$$ = (Node *)n;
2242 				}
2243 			/* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
2244 			| ADD_P IF_P NOT EXISTS columnDef
2245 				{
2246 					AlterTableCmd *n = makeNode(AlterTableCmd);
2247 					n->subtype = AT_AddColumn;
2248 					n->def = $5;
2249 					n->missing_ok = true;
2250 					$$ = (Node *)n;
2251 				}
2252 			/* ALTER TABLE <name> ADD COLUMN <coldef> */
2253 			| ADD_P COLUMN columnDef
2254 				{
2255 					AlterTableCmd *n = makeNode(AlterTableCmd);
2256 					n->subtype = AT_AddColumn;
2257 					n->def = $3;
2258 					n->missing_ok = false;
2259 					$$ = (Node *)n;
2260 				}
2261 			/* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
2262 			| ADD_P COLUMN IF_P NOT EXISTS columnDef
2263 				{
2264 					AlterTableCmd *n = makeNode(AlterTableCmd);
2265 					n->subtype = AT_AddColumn;
2266 					n->def = $6;
2267 					n->missing_ok = true;
2268 					$$ = (Node *)n;
2269 				}
2270 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
2271 			| ALTER opt_column ColId alter_column_default
2272 				{
2273 					AlterTableCmd *n = makeNode(AlterTableCmd);
2274 					n->subtype = AT_ColumnDefault;
2275 					n->name = $3;
2276 					n->def = $4;
2277 					$$ = (Node *)n;
2278 				}
2279 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
2280 			| ALTER opt_column ColId DROP NOT NULL_P
2281 				{
2282 					AlterTableCmd *n = makeNode(AlterTableCmd);
2283 					n->subtype = AT_DropNotNull;
2284 					n->name = $3;
2285 					$$ = (Node *)n;
2286 				}
2287 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
2288 			| ALTER opt_column ColId SET NOT NULL_P
2289 				{
2290 					AlterTableCmd *n = makeNode(AlterTableCmd);
2291 					n->subtype = AT_SetNotNull;
2292 					n->name = $3;
2293 					$$ = (Node *)n;
2294 				}
2295 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP EXPRESSION */
2296 			| ALTER opt_column ColId DROP EXPRESSION
2297 				{
2298 					AlterTableCmd *n = makeNode(AlterTableCmd);
2299 					n->subtype = AT_DropExpression;
2300 					n->name = $3;
2301 					$$ = (Node *)n;
2302 				}
2303 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP EXPRESSION IF EXISTS */
2304 			| ALTER opt_column ColId DROP EXPRESSION IF_P EXISTS
2305 				{
2306 					AlterTableCmd *n = makeNode(AlterTableCmd);
2307 					n->subtype = AT_DropExpression;
2308 					n->name = $3;
2309 					n->missing_ok = true;
2310 					$$ = (Node *)n;
2311 				}
2312 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
2313 			| ALTER opt_column ColId SET STATISTICS SignedIconst
2314 				{
2315 					AlterTableCmd *n = makeNode(AlterTableCmd);
2316 					n->subtype = AT_SetStatistics;
2317 					n->name = $3;
2318 					n->def = (Node *) makeInteger($6);
2319 					$$ = (Node *)n;
2320 				}
2321 			/* ALTER TABLE <name> ALTER [COLUMN] <colnum> SET STATISTICS <SignedIconst> */
2322 			| ALTER opt_column Iconst SET STATISTICS SignedIconst
2323 				{
2324 					AlterTableCmd *n = makeNode(AlterTableCmd);
2325 
2326 					if ($3 <= 0 || $3 > PG_INT16_MAX)
2327 						ereport(ERROR,
2328 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2329 								 errmsg("column number must be in range from 1 to %d", PG_INT16_MAX),
2330 								 parser_errposition(@3)));
2331 
2332 					n->subtype = AT_SetStatistics;
2333 					n->num = (int16) $3;
2334 					n->def = (Node *) makeInteger($6);
2335 					$$ = (Node *)n;
2336 				}
2337 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
2338 			| ALTER opt_column ColId SET reloptions
2339 				{
2340 					AlterTableCmd *n = makeNode(AlterTableCmd);
2341 					n->subtype = AT_SetOptions;
2342 					n->name = $3;
2343 					n->def = (Node *) $5;
2344 					$$ = (Node *)n;
2345 				}
2346 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
2347 			| ALTER opt_column ColId RESET reloptions
2348 				{
2349 					AlterTableCmd *n = makeNode(AlterTableCmd);
2350 					n->subtype = AT_ResetOptions;
2351 					n->name = $3;
2352 					n->def = (Node *) $5;
2353 					$$ = (Node *)n;
2354 				}
2355 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
2356 			| ALTER opt_column ColId SET STORAGE ColId
2357 				{
2358 					AlterTableCmd *n = makeNode(AlterTableCmd);
2359 					n->subtype = AT_SetStorage;
2360 					n->name = $3;
2361 					n->def = (Node *) makeString($6);
2362 					$$ = (Node *)n;
2363 				}
2364 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET COMPRESSION <cm> */
2365 			| ALTER opt_column ColId SET column_compression
2366 				{
2367 					AlterTableCmd *n = makeNode(AlterTableCmd);
2368 					n->subtype = AT_SetCompression;
2369 					n->name = $3;
2370 					n->def = (Node *) makeString($5);
2371 					$$ = (Node *)n;
2372 				}
2373 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> ADD GENERATED ... AS IDENTITY ... */
2374 			| ALTER opt_column ColId ADD_P GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
2375 				{
2376 					AlterTableCmd *n = makeNode(AlterTableCmd);
2377 					Constraint *c = makeNode(Constraint);
2378 
2379 					c->contype = CONSTR_IDENTITY;
2380 					c->generated_when = $6;
2381 					c->options = $9;
2382 					c->location = @5;
2383 
2384 					n->subtype = AT_AddIdentity;
2385 					n->name = $3;
2386 					n->def = (Node *) c;
2387 
2388 					$$ = (Node *)n;
2389 				}
2390 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET <sequence options>/RESET */
2391 			| ALTER opt_column ColId alter_identity_column_option_list
2392 				{
2393 					AlterTableCmd *n = makeNode(AlterTableCmd);
2394 					n->subtype = AT_SetIdentity;
2395 					n->name = $3;
2396 					n->def = (Node *) $4;
2397 					$$ = (Node *)n;
2398 				}
2399 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY */
2400 			| ALTER opt_column ColId DROP IDENTITY_P
2401 				{
2402 					AlterTableCmd *n = makeNode(AlterTableCmd);
2403 					n->subtype = AT_DropIdentity;
2404 					n->name = $3;
2405 					n->missing_ok = false;
2406 					$$ = (Node *)n;
2407 				}
2408 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY IF EXISTS */
2409 			| ALTER opt_column ColId DROP IDENTITY_P IF_P EXISTS
2410 				{
2411 					AlterTableCmd *n = makeNode(AlterTableCmd);
2412 					n->subtype = AT_DropIdentity;
2413 					n->name = $3;
2414 					n->missing_ok = true;
2415 					$$ = (Node *)n;
2416 				}
2417 			/* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
2418 			| DROP opt_column IF_P EXISTS ColId opt_drop_behavior
2419 				{
2420 					AlterTableCmd *n = makeNode(AlterTableCmd);
2421 					n->subtype = AT_DropColumn;
2422 					n->name = $5;
2423 					n->behavior = $6;
2424 					n->missing_ok = true;
2425 					$$ = (Node *)n;
2426 				}
2427 			/* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
2428 			| DROP opt_column ColId opt_drop_behavior
2429 				{
2430 					AlterTableCmd *n = makeNode(AlterTableCmd);
2431 					n->subtype = AT_DropColumn;
2432 					n->name = $3;
2433 					n->behavior = $4;
2434 					n->missing_ok = false;
2435 					$$ = (Node *)n;
2436 				}
2437 			/*
2438 			 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
2439 			 *		[ USING <expression> ]
2440 			 */
2441 			| ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
2442 				{
2443 					AlterTableCmd *n = makeNode(AlterTableCmd);
2444 					ColumnDef *def = makeNode(ColumnDef);
2445 					n->subtype = AT_AlterColumnType;
2446 					n->name = $3;
2447 					n->def = (Node *) def;
2448 					/* We only use these fields of the ColumnDef node */
2449 					def->typeName = $6;
2450 					def->collClause = (CollateClause *) $7;
2451 					def->raw_default = $8;
2452 					def->location = @3;
2453 					$$ = (Node *)n;
2454 				}
2455 			/* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
2456 			| ALTER opt_column ColId alter_generic_options
2457 				{
2458 					AlterTableCmd *n = makeNode(AlterTableCmd);
2459 					n->subtype = AT_AlterColumnGenericOptions;
2460 					n->name = $3;
2461 					n->def = (Node *) $4;
2462 					$$ = (Node *)n;
2463 				}
2464 			/* ALTER TABLE <name> ADD CONSTRAINT ... */
2465 			| ADD_P TableConstraint
2466 				{
2467 					AlterTableCmd *n = makeNode(AlterTableCmd);
2468 					n->subtype = AT_AddConstraint;
2469 					n->def = $2;
2470 					n->missing_ok = false;
2471 					$$ = (Node *)n;
2472 				}
2473 			/* ALTER TABLE <name> ALTER CONSTRAINT ... */
2474 			| ALTER CONSTRAINT name ConstraintAttributeSpec
2475 				{
2476 					AlterTableCmd *n = makeNode(AlterTableCmd);
2477 					Constraint *c = makeNode(Constraint);
2478 					n->subtype = AT_AlterConstraint;
2479 					n->def = (Node *) c;
2480 					c->contype = CONSTR_FOREIGN; /* others not supported, yet */
2481 					c->conname = $3;
2482 					processCASbits($4, @4, "ALTER CONSTRAINT statement",
2483 									&c->deferrable,
2484 									&c->initdeferred,
2485 									NULL, NULL, yyscanner);
2486 					$$ = (Node *)n;
2487 				}
2488 			/* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
2489 			| VALIDATE CONSTRAINT name
2490 				{
2491 					AlterTableCmd *n = makeNode(AlterTableCmd);
2492 					n->subtype = AT_ValidateConstraint;
2493 					n->name = $3;
2494 					$$ = (Node *)n;
2495 				}
2496 			/* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
2497 			| DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
2498 				{
2499 					AlterTableCmd *n = makeNode(AlterTableCmd);
2500 					n->subtype = AT_DropConstraint;
2501 					n->name = $5;
2502 					n->behavior = $6;
2503 					n->missing_ok = true;
2504 					$$ = (Node *)n;
2505 				}
2506 			/* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
2507 			| DROP CONSTRAINT name opt_drop_behavior
2508 				{
2509 					AlterTableCmd *n = makeNode(AlterTableCmd);
2510 					n->subtype = AT_DropConstraint;
2511 					n->name = $3;
2512 					n->behavior = $4;
2513 					n->missing_ok = false;
2514 					$$ = (Node *)n;
2515 				}
2516 			/* ALTER TABLE <name> SET WITHOUT OIDS, for backward compat */
2517 			| SET WITHOUT OIDS
2518 				{
2519 					AlterTableCmd *n = makeNode(AlterTableCmd);
2520 					n->subtype = AT_DropOids;
2521 					$$ = (Node *)n;
2522 				}
2523 			/* ALTER TABLE <name> CLUSTER ON <indexname> */
2524 			| CLUSTER ON name
2525 				{
2526 					AlterTableCmd *n = makeNode(AlterTableCmd);
2527 					n->subtype = AT_ClusterOn;
2528 					n->name = $3;
2529 					$$ = (Node *)n;
2530 				}
2531 			/* ALTER TABLE <name> SET WITHOUT CLUSTER */
2532 			| SET WITHOUT CLUSTER
2533 				{
2534 					AlterTableCmd *n = makeNode(AlterTableCmd);
2535 					n->subtype = AT_DropCluster;
2536 					n->name = NULL;
2537 					$$ = (Node *)n;
2538 				}
2539 			/* ALTER TABLE <name> SET LOGGED */
2540 			| SET LOGGED
2541 				{
2542 					AlterTableCmd *n = makeNode(AlterTableCmd);
2543 					n->subtype = AT_SetLogged;
2544 					$$ = (Node *)n;
2545 				}
2546 			/* ALTER TABLE <name> SET UNLOGGED */
2547 			| SET UNLOGGED
2548 				{
2549 					AlterTableCmd *n = makeNode(AlterTableCmd);
2550 					n->subtype = AT_SetUnLogged;
2551 					$$ = (Node *)n;
2552 				}
2553 			/* ALTER TABLE <name> ENABLE TRIGGER <trig> */
2554 			| ENABLE_P TRIGGER name
2555 				{
2556 					AlterTableCmd *n = makeNode(AlterTableCmd);
2557 					n->subtype = AT_EnableTrig;
2558 					n->name = $3;
2559 					$$ = (Node *)n;
2560 				}
2561 			/* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
2562 			| ENABLE_P ALWAYS TRIGGER name
2563 				{
2564 					AlterTableCmd *n = makeNode(AlterTableCmd);
2565 					n->subtype = AT_EnableAlwaysTrig;
2566 					n->name = $4;
2567 					$$ = (Node *)n;
2568 				}
2569 			/* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
2570 			| ENABLE_P REPLICA TRIGGER name
2571 				{
2572 					AlterTableCmd *n = makeNode(AlterTableCmd);
2573 					n->subtype = AT_EnableReplicaTrig;
2574 					n->name = $4;
2575 					$$ = (Node *)n;
2576 				}
2577 			/* ALTER TABLE <name> ENABLE TRIGGER ALL */
2578 			| ENABLE_P TRIGGER ALL
2579 				{
2580 					AlterTableCmd *n = makeNode(AlterTableCmd);
2581 					n->subtype = AT_EnableTrigAll;
2582 					$$ = (Node *)n;
2583 				}
2584 			/* ALTER TABLE <name> ENABLE TRIGGER USER */
2585 			| ENABLE_P TRIGGER USER
2586 				{
2587 					AlterTableCmd *n = makeNode(AlterTableCmd);
2588 					n->subtype = AT_EnableTrigUser;
2589 					$$ = (Node *)n;
2590 				}
2591 			/* ALTER TABLE <name> DISABLE TRIGGER <trig> */
2592 			| DISABLE_P TRIGGER name
2593 				{
2594 					AlterTableCmd *n = makeNode(AlterTableCmd);
2595 					n->subtype = AT_DisableTrig;
2596 					n->name = $3;
2597 					$$ = (Node *)n;
2598 				}
2599 			/* ALTER TABLE <name> DISABLE TRIGGER ALL */
2600 			| DISABLE_P TRIGGER ALL
2601 				{
2602 					AlterTableCmd *n = makeNode(AlterTableCmd);
2603 					n->subtype = AT_DisableTrigAll;
2604 					$$ = (Node *)n;
2605 				}
2606 			/* ALTER TABLE <name> DISABLE TRIGGER USER */
2607 			| DISABLE_P TRIGGER USER
2608 				{
2609 					AlterTableCmd *n = makeNode(AlterTableCmd);
2610 					n->subtype = AT_DisableTrigUser;
2611 					$$ = (Node *)n;
2612 				}
2613 			/* ALTER TABLE <name> ENABLE RULE <rule> */
2614 			| ENABLE_P RULE name
2615 				{
2616 					AlterTableCmd *n = makeNode(AlterTableCmd);
2617 					n->subtype = AT_EnableRule;
2618 					n->name = $3;
2619 					$$ = (Node *)n;
2620 				}
2621 			/* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
2622 			| ENABLE_P ALWAYS RULE name
2623 				{
2624 					AlterTableCmd *n = makeNode(AlterTableCmd);
2625 					n->subtype = AT_EnableAlwaysRule;
2626 					n->name = $4;
2627 					$$ = (Node *)n;
2628 				}
2629 			/* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
2630 			| ENABLE_P REPLICA RULE name
2631 				{
2632 					AlterTableCmd *n = makeNode(AlterTableCmd);
2633 					n->subtype = AT_EnableReplicaRule;
2634 					n->name = $4;
2635 					$$ = (Node *)n;
2636 				}
2637 			/* ALTER TABLE <name> DISABLE RULE <rule> */
2638 			| DISABLE_P RULE name
2639 				{
2640 					AlterTableCmd *n = makeNode(AlterTableCmd);
2641 					n->subtype = AT_DisableRule;
2642 					n->name = $3;
2643 					$$ = (Node *)n;
2644 				}
2645 			/* ALTER TABLE <name> INHERIT <parent> */
2646 			| INHERIT qualified_name
2647 				{
2648 					AlterTableCmd *n = makeNode(AlterTableCmd);
2649 					n->subtype = AT_AddInherit;
2650 					n->def = (Node *) $2;
2651 					$$ = (Node *)n;
2652 				}
2653 			/* ALTER TABLE <name> NO INHERIT <parent> */
2654 			| NO INHERIT qualified_name
2655 				{
2656 					AlterTableCmd *n = makeNode(AlterTableCmd);
2657 					n->subtype = AT_DropInherit;
2658 					n->def = (Node *) $3;
2659 					$$ = (Node *)n;
2660 				}
2661 			/* ALTER TABLE <name> OF <type_name> */
2662 			| OF any_name
2663 				{
2664 					AlterTableCmd *n = makeNode(AlterTableCmd);
2665 					TypeName *def = makeTypeNameFromNameList($2);
2666 					def->location = @2;
2667 					n->subtype = AT_AddOf;
2668 					n->def = (Node *) def;
2669 					$$ = (Node *)n;
2670 				}
2671 			/* ALTER TABLE <name> NOT OF */
2672 			| NOT OF
2673 				{
2674 					AlterTableCmd *n = makeNode(AlterTableCmd);
2675 					n->subtype = AT_DropOf;
2676 					$$ = (Node *)n;
2677 				}
2678 			/* ALTER TABLE <name> OWNER TO RoleSpec */
2679 			| OWNER TO RoleSpec
2680 				{
2681 					AlterTableCmd *n = makeNode(AlterTableCmd);
2682 					n->subtype = AT_ChangeOwner;
2683 					n->newowner = $3;
2684 					$$ = (Node *)n;
2685 				}
2686 			/* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
2687 			| SET TABLESPACE name
2688 				{
2689 					AlterTableCmd *n = makeNode(AlterTableCmd);
2690 					n->subtype = AT_SetTableSpace;
2691 					n->name = $3;
2692 					$$ = (Node *)n;
2693 				}
2694 			/* ALTER TABLE <name> SET (...) */
2695 			| SET reloptions
2696 				{
2697 					AlterTableCmd *n = makeNode(AlterTableCmd);
2698 					n->subtype = AT_SetRelOptions;
2699 					n->def = (Node *)$2;
2700 					$$ = (Node *)n;
2701 				}
2702 			/* ALTER TABLE <name> RESET (...) */
2703 			| RESET reloptions
2704 				{
2705 					AlterTableCmd *n = makeNode(AlterTableCmd);
2706 					n->subtype = AT_ResetRelOptions;
2707 					n->def = (Node *)$2;
2708 					$$ = (Node *)n;
2709 				}
2710 			/* ALTER TABLE <name> REPLICA IDENTITY */
2711 			| REPLICA IDENTITY_P replica_identity
2712 				{
2713 					AlterTableCmd *n = makeNode(AlterTableCmd);
2714 					n->subtype = AT_ReplicaIdentity;
2715 					n->def = $3;
2716 					$$ = (Node *)n;
2717 				}
2718 			/* ALTER TABLE <name> ENABLE ROW LEVEL SECURITY */
2719 			| ENABLE_P ROW LEVEL SECURITY
2720 				{
2721 					AlterTableCmd *n = makeNode(AlterTableCmd);
2722 					n->subtype = AT_EnableRowSecurity;
2723 					$$ = (Node *)n;
2724 				}
2725 			/* ALTER TABLE <name> DISABLE ROW LEVEL SECURITY */
2726 			| DISABLE_P ROW LEVEL SECURITY
2727 				{
2728 					AlterTableCmd *n = makeNode(AlterTableCmd);
2729 					n->subtype = AT_DisableRowSecurity;
2730 					$$ = (Node *)n;
2731 				}
2732 			/* ALTER TABLE <name> FORCE ROW LEVEL SECURITY */
2733 			| FORCE ROW LEVEL SECURITY
2734 				{
2735 					AlterTableCmd *n = makeNode(AlterTableCmd);
2736 					n->subtype = AT_ForceRowSecurity;
2737 					$$ = (Node *)n;
2738 				}
2739 			/* ALTER TABLE <name> NO FORCE ROW LEVEL SECURITY */
2740 			| NO FORCE ROW LEVEL SECURITY
2741 				{
2742 					AlterTableCmd *n = makeNode(AlterTableCmd);
2743 					n->subtype = AT_NoForceRowSecurity;
2744 					$$ = (Node *)n;
2745 				}
2746 			| alter_generic_options
2747 				{
2748 					AlterTableCmd *n = makeNode(AlterTableCmd);
2749 					n->subtype = AT_GenericOptions;
2750 					n->def = (Node *)$1;
2751 					$$ = (Node *) n;
2752 				}
2753 		;
2754 
2755 alter_column_default:
2756 			SET DEFAULT a_expr			{ $$ = $3; }
2757 			| DROP DEFAULT				{ $$ = NULL; }
2758 		;
2759 
2760 opt_drop_behavior:
2761 			CASCADE						{ $$ = DROP_CASCADE; }
2762 			| RESTRICT					{ $$ = DROP_RESTRICT; }
2763 			| /* EMPTY */				{ $$ = DROP_RESTRICT; /* default */ }
2764 		;
2765 
2766 opt_collate_clause:
2767 			COLLATE any_name
2768 				{
2769 					CollateClause *n = makeNode(CollateClause);
2770 					n->arg = NULL;
2771 					n->collname = $2;
2772 					n->location = @1;
2773 					$$ = (Node *) n;
2774 				}
2775 			| /* EMPTY */				{ $$ = NULL; }
2776 		;
2777 
2778 alter_using:
2779 			USING a_expr				{ $$ = $2; }
2780 			| /* EMPTY */				{ $$ = NULL; }
2781 		;
2782 
2783 replica_identity:
2784 			NOTHING
2785 				{
2786 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2787 					n->identity_type = REPLICA_IDENTITY_NOTHING;
2788 					n->name = NULL;
2789 					$$ = (Node *) n;
2790 				}
2791 			| FULL
2792 				{
2793 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2794 					n->identity_type = REPLICA_IDENTITY_FULL;
2795 					n->name = NULL;
2796 					$$ = (Node *) n;
2797 				}
2798 			| DEFAULT
2799 				{
2800 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2801 					n->identity_type = REPLICA_IDENTITY_DEFAULT;
2802 					n->name = NULL;
2803 					$$ = (Node *) n;
2804 				}
2805 			| USING INDEX name
2806 				{
2807 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2808 					n->identity_type = REPLICA_IDENTITY_INDEX;
2809 					n->name = $3;
2810 					$$ = (Node *) n;
2811 				}
2812 ;
2813 
2814 reloptions:
2815 			'(' reloption_list ')'					{ $$ = $2; }
2816 		;
2817 
2818 opt_reloptions:		WITH reloptions					{ $$ = $2; }
2819 			 |		/* EMPTY */						{ $$ = NIL; }
2820 		;
2821 
2822 reloption_list:
2823 			reloption_elem							{ $$ = list_make1($1); }
2824 			| reloption_list ',' reloption_elem		{ $$ = lappend($1, $3); }
2825 		;
2826 
2827 /* This should match def_elem and also allow qualified names */
2828 reloption_elem:
2829 			ColLabel '=' def_arg
2830 				{
2831 					$$ = makeDefElem($1, (Node *) $3, @1);
2832 				}
2833 			| ColLabel
2834 				{
2835 					$$ = makeDefElem($1, NULL, @1);
2836 				}
2837 			| ColLabel '.' ColLabel '=' def_arg
2838 				{
2839 					$$ = makeDefElemExtended($1, $3, (Node *) $5,
2840 											 DEFELEM_UNSPEC, @1);
2841 				}
2842 			| ColLabel '.' ColLabel
2843 				{
2844 					$$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC, @1);
2845 				}
2846 		;
2847 
2848 alter_identity_column_option_list:
2849 			alter_identity_column_option
2850 				{ $$ = list_make1($1); }
2851 			| alter_identity_column_option_list alter_identity_column_option
2852 				{ $$ = lappend($1, $2); }
2853 		;
2854 
2855 alter_identity_column_option:
2856 			RESTART
2857 				{
2858 					$$ = makeDefElem("restart", NULL, @1);
2859 				}
2860 			| RESTART opt_with NumericOnly
2861 				{
2862 					$$ = makeDefElem("restart", (Node *)$3, @1);
2863 				}
2864 			| SET SeqOptElem
2865 				{
2866 					if (strcmp($2->defname, "as") == 0 ||
2867 						strcmp($2->defname, "restart") == 0 ||
2868 						strcmp($2->defname, "owned_by") == 0)
2869 						ereport(ERROR,
2870 								(errcode(ERRCODE_SYNTAX_ERROR),
2871 								 errmsg("sequence option \"%s\" not supported here", $2->defname),
2872 								 parser_errposition(@2)));
2873 					$$ = $2;
2874 				}
2875 			| SET GENERATED generated_when
2876 				{
2877 					$$ = makeDefElem("generated", (Node *) makeInteger($3), @1);
2878 				}
2879 		;
2880 
2881 PartitionBoundSpec:
2882 			/* a HASH partition */
2883 			FOR VALUES WITH '(' hash_partbound ')'
2884 				{
2885 					ListCell   *lc;
2886 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2887 
2888 					n->strategy = PARTITION_STRATEGY_HASH;
2889 					n->modulus = n->remainder = -1;
2890 
foreach(lc,$5)2891 					foreach (lc, $5)
2892 					{
2893 						DefElem    *opt = lfirst_node(DefElem, lc);
2894 
2895 						if (strcmp(opt->defname, "modulus") == 0)
2896 						{
2897 							if (n->modulus != -1)
2898 								ereport(ERROR,
2899 										(errcode(ERRCODE_DUPLICATE_OBJECT),
2900 										 errmsg("modulus for hash partition provided more than once"),
2901 										 parser_errposition(opt->location)));
2902 							n->modulus = defGetInt32(opt);
2903 						}
2904 						else if (strcmp(opt->defname, "remainder") == 0)
2905 						{
2906 							if (n->remainder != -1)
2907 								ereport(ERROR,
2908 										(errcode(ERRCODE_DUPLICATE_OBJECT),
2909 										 errmsg("remainder for hash partition provided more than once"),
2910 										 parser_errposition(opt->location)));
2911 							n->remainder = defGetInt32(opt);
2912 						}
2913 						else
2914 							ereport(ERROR,
2915 									(errcode(ERRCODE_SYNTAX_ERROR),
2916 									 errmsg("unrecognized hash partition bound specification \"%s\"",
2917 											opt->defname),
2918 									 parser_errposition(opt->location)));
2919 					}
2920 
2921 					if (n->modulus == -1)
2922 						ereport(ERROR,
2923 								(errcode(ERRCODE_SYNTAX_ERROR),
2924 								 errmsg("modulus for hash partition must be specified")));
2925 					if (n->remainder == -1)
2926 						ereport(ERROR,
2927 								(errcode(ERRCODE_SYNTAX_ERROR),
2928 								 errmsg("remainder for hash partition must be specified")));
2929 
2930 					n->location = @3;
2931 
2932 					$$ = n;
2933 				}
2934 
2935 			/* a LIST partition */
2936 			| FOR VALUES IN_P '(' expr_list ')'
2937 				{
2938 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2939 
2940 					n->strategy = PARTITION_STRATEGY_LIST;
2941 					n->is_default = false;
2942 					n->listdatums = $5;
2943 					n->location = @3;
2944 
2945 					$$ = n;
2946 				}
2947 
2948 			/* a RANGE partition */
2949 			| FOR VALUES FROM '(' expr_list ')' TO '(' expr_list ')'
2950 				{
2951 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2952 
2953 					n->strategy = PARTITION_STRATEGY_RANGE;
2954 					n->is_default = false;
2955 					n->lowerdatums = $5;
2956 					n->upperdatums = $9;
2957 					n->location = @3;
2958 
2959 					$$ = n;
2960 				}
2961 
2962 			/* a DEFAULT partition */
2963 			| DEFAULT
2964 				{
2965 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2966 
2967 					n->is_default = true;
2968 					n->location = @1;
2969 
2970 					$$ = n;
2971 				}
2972 		;
2973 
2974 hash_partbound_elem:
2975 		NonReservedWord Iconst
2976 			{
2977 				$$ = makeDefElem($1, (Node *)makeInteger($2), @1);
2978 			}
2979 		;
2980 
2981 hash_partbound:
2982 		hash_partbound_elem
2983 			{
2984 				$$ = list_make1($1);
2985 			}
2986 		| hash_partbound ',' hash_partbound_elem
2987 			{
2988 				$$ = lappend($1, $3);
2989 			}
2990 		;
2991 
2992 /*****************************************************************************
2993  *
2994  *	ALTER TYPE
2995  *
2996  * really variants of the ALTER TABLE subcommands with different spellings
2997  *****************************************************************************/
2998 
2999 AlterCompositeTypeStmt:
3000 			ALTER TYPE_P any_name alter_type_cmds
3001 				{
3002 					AlterTableStmt *n = makeNode(AlterTableStmt);
3003 
3004 					/* can't use qualified_name, sigh */
3005 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
3006 					n->cmds = $4;
3007 					n->objtype = OBJECT_TYPE;
3008 					$$ = (Node *)n;
3009 				}
3010 			;
3011 
3012 alter_type_cmds:
3013 			alter_type_cmd							{ $$ = list_make1($1); }
3014 			| alter_type_cmds ',' alter_type_cmd	{ $$ = lappend($1, $3); }
3015 		;
3016 
3017 alter_type_cmd:
3018 			/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
3019 			ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
3020 				{
3021 					AlterTableCmd *n = makeNode(AlterTableCmd);
3022 					n->subtype = AT_AddColumn;
3023 					n->def = $3;
3024 					n->behavior = $4;
3025 					$$ = (Node *)n;
3026 				}
3027 			/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
3028 			| DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
3029 				{
3030 					AlterTableCmd *n = makeNode(AlterTableCmd);
3031 					n->subtype = AT_DropColumn;
3032 					n->name = $5;
3033 					n->behavior = $6;
3034 					n->missing_ok = true;
3035 					$$ = (Node *)n;
3036 				}
3037 			/* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
3038 			| DROP ATTRIBUTE ColId opt_drop_behavior
3039 				{
3040 					AlterTableCmd *n = makeNode(AlterTableCmd);
3041 					n->subtype = AT_DropColumn;
3042 					n->name = $3;
3043 					n->behavior = $4;
3044 					n->missing_ok = false;
3045 					$$ = (Node *)n;
3046 				}
3047 			/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
3048 			| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
3049 				{
3050 					AlterTableCmd *n = makeNode(AlterTableCmd);
3051 					ColumnDef *def = makeNode(ColumnDef);
3052 					n->subtype = AT_AlterColumnType;
3053 					n->name = $3;
3054 					n->def = (Node *) def;
3055 					n->behavior = $8;
3056 					/* We only use these fields of the ColumnDef node */
3057 					def->typeName = $6;
3058 					def->collClause = (CollateClause *) $7;
3059 					def->raw_default = NULL;
3060 					def->location = @3;
3061 					$$ = (Node *)n;
3062 				}
3063 		;
3064 
3065 
3066 /*****************************************************************************
3067  *
3068  *		QUERY :
3069  *				close <portalname>
3070  *
3071  *****************************************************************************/
3072 
3073 ClosePortalStmt:
3074 			CLOSE cursor_name
3075 				{
3076 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
3077 					n->portalname = $2;
3078 					$$ = (Node *)n;
3079 				}
3080 			| CLOSE ALL
3081 				{
3082 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
3083 					n->portalname = NULL;
3084 					$$ = (Node *)n;
3085 				}
3086 		;
3087 
3088 
3089 /*****************************************************************************
3090  *
3091  *		QUERY :
3092  *				COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
3093  *				COPY ( query ) TO file	[WITH] [(options)]
3094  *
3095  *				where 'query' can be one of:
3096  *				{ SELECT | UPDATE | INSERT | DELETE }
3097  *
3098  *				and 'file' can be one of:
3099  *				{ PROGRAM 'command' | STDIN | STDOUT | 'filename' }
3100  *
3101  *				In the preferred syntax the options are comma-separated
3102  *				and use generic identifiers instead of keywords.  The pre-9.0
3103  *				syntax had a hard-wired, space-separated set of options.
3104  *
3105  *				Really old syntax, from versions 7.2 and prior:
3106  *				COPY [ BINARY ] table FROM/TO file
3107  *					[ [ USING ] DELIMITERS 'delimiter' ] ]
3108  *					[ WITH NULL AS 'null string' ]
3109  *				This option placement is not supported with COPY (query...).
3110  *
3111  *****************************************************************************/
3112 
3113 CopyStmt:	COPY opt_binary qualified_name opt_column_list
3114 			copy_from opt_program copy_file_name copy_delimiter opt_with
3115 			copy_options where_clause
3116 				{
3117 					CopyStmt *n = makeNode(CopyStmt);
3118 					n->relation = $3;
3119 					n->query = NULL;
3120 					n->attlist = $4;
3121 					n->is_from = $5;
3122 					n->is_program = $6;
3123 					n->filename = $7;
3124 					n->whereClause = $11;
3125 
3126 					if (n->is_program && n->filename == NULL)
3127 						ereport(ERROR,
3128 								(errcode(ERRCODE_SYNTAX_ERROR),
3129 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
3130 								 parser_errposition(@8)));
3131 
3132 					if (!n->is_from && n->whereClause != NULL)
3133 						ereport(ERROR,
3134 								(errcode(ERRCODE_SYNTAX_ERROR),
3135 								 errmsg("WHERE clause not allowed with COPY TO"),
3136 								 parser_errposition(@11)));
3137 
3138 					n->options = NIL;
3139 					/* Concatenate user-supplied flags */
3140 					if ($2)
3141 						n->options = lappend(n->options, $2);
3142 					if ($8)
3143 						n->options = lappend(n->options, $8);
3144 					if ($10)
3145 						n->options = list_concat(n->options, $10);
3146 					$$ = (Node *)n;
3147 				}
3148 			| COPY '(' PreparableStmt ')' TO opt_program copy_file_name opt_with copy_options
3149 				{
3150 					CopyStmt *n = makeNode(CopyStmt);
3151 					n->relation = NULL;
3152 					n->query = $3;
3153 					n->attlist = NIL;
3154 					n->is_from = false;
3155 					n->is_program = $6;
3156 					n->filename = $7;
3157 					n->options = $9;
3158 
3159 					if (n->is_program && n->filename == NULL)
3160 						ereport(ERROR,
3161 								(errcode(ERRCODE_SYNTAX_ERROR),
3162 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
3163 								 parser_errposition(@5)));
3164 
3165 					$$ = (Node *)n;
3166 				}
3167 		;
3168 
3169 copy_from:
3170 			FROM									{ $$ = true; }
3171 			| TO									{ $$ = false; }
3172 		;
3173 
3174 opt_program:
3175 			PROGRAM									{ $$ = true; }
3176 			| /* EMPTY */							{ $$ = false; }
3177 		;
3178 
3179 /*
3180  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
3181  * used depends on the direction. (It really doesn't make sense to copy from
3182  * stdout. We silently correct the "typo".)		 - AY 9/94
3183  */
3184 copy_file_name:
3185 			Sconst									{ $$ = $1; }
3186 			| STDIN									{ $$ = NULL; }
3187 			| STDOUT								{ $$ = NULL; }
3188 		;
3189 
3190 copy_options: copy_opt_list							{ $$ = $1; }
3191 			| '(' copy_generic_opt_list ')'			{ $$ = $2; }
3192 		;
3193 
3194 /* old COPY option syntax */
3195 copy_opt_list:
3196 			copy_opt_list copy_opt_item				{ $$ = lappend($1, $2); }
3197 			| /* EMPTY */							{ $$ = NIL; }
3198 		;
3199 
3200 copy_opt_item:
3201 			BINARY
3202 				{
3203 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3204 				}
3205 			| FREEZE
3206 				{
3207 					$$ = makeDefElem("freeze", (Node *)makeInteger(true), @1);
3208 				}
3209 			| DELIMITER opt_as Sconst
3210 				{
3211 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @1);
3212 				}
3213 			| NULL_P opt_as Sconst
3214 				{
3215 					$$ = makeDefElem("null", (Node *)makeString($3), @1);
3216 				}
3217 			| CSV
3218 				{
3219 					$$ = makeDefElem("format", (Node *)makeString("csv"), @1);
3220 				}
3221 			| HEADER_P
3222 				{
3223 					$$ = makeDefElem("header", (Node *)makeInteger(true), @1);
3224 				}
3225 			| QUOTE opt_as Sconst
3226 				{
3227 					$$ = makeDefElem("quote", (Node *)makeString($3), @1);
3228 				}
3229 			| ESCAPE opt_as Sconst
3230 				{
3231 					$$ = makeDefElem("escape", (Node *)makeString($3), @1);
3232 				}
3233 			| FORCE QUOTE columnList
3234 				{
3235 					$$ = makeDefElem("force_quote", (Node *)$3, @1);
3236 				}
3237 			| FORCE QUOTE '*'
3238 				{
3239 					$$ = makeDefElem("force_quote", (Node *)makeNode(A_Star), @1);
3240 				}
3241 			| FORCE NOT NULL_P columnList
3242 				{
3243 					$$ = makeDefElem("force_not_null", (Node *)$4, @1);
3244 				}
3245 			| FORCE NULL_P columnList
3246 				{
3247 					$$ = makeDefElem("force_null", (Node *)$3, @1);
3248 				}
3249 			| ENCODING Sconst
3250 				{
3251 					$$ = makeDefElem("encoding", (Node *)makeString($2), @1);
3252 				}
3253 		;
3254 
3255 /* The following exist for backward compatibility with very old versions */
3256 
3257 opt_binary:
3258 			BINARY
3259 				{
3260 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3261 				}
3262 			| /*EMPTY*/								{ $$ = NULL; }
3263 		;
3264 
3265 copy_delimiter:
3266 			opt_using DELIMITERS Sconst
3267 				{
3268 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @2);
3269 				}
3270 			| /*EMPTY*/								{ $$ = NULL; }
3271 		;
3272 
3273 opt_using:
3274 			USING
3275 			| /*EMPTY*/
3276 		;
3277 
3278 /* new COPY option syntax */
3279 copy_generic_opt_list:
3280 			copy_generic_opt_elem
3281 				{
3282 					$$ = list_make1($1);
3283 				}
3284 			| copy_generic_opt_list ',' copy_generic_opt_elem
3285 				{
3286 					$$ = lappend($1, $3);
3287 				}
3288 		;
3289 
3290 copy_generic_opt_elem:
3291 			ColLabel copy_generic_opt_arg
3292 				{
3293 					$$ = makeDefElem($1, $2, @1);
3294 				}
3295 		;
3296 
3297 copy_generic_opt_arg:
3298 			opt_boolean_or_string			{ $$ = (Node *) makeString($1); }
3299 			| NumericOnly					{ $$ = (Node *) $1; }
3300 			| '*'							{ $$ = (Node *) makeNode(A_Star); }
3301 			| '(' copy_generic_opt_arg_list ')'		{ $$ = (Node *) $2; }
3302 			| /* EMPTY */					{ $$ = NULL; }
3303 		;
3304 
3305 copy_generic_opt_arg_list:
3306 			  copy_generic_opt_arg_list_item
3307 				{
3308 					$$ = list_make1($1);
3309 				}
3310 			| copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
3311 				{
3312 					$$ = lappend($1, $3);
3313 				}
3314 		;
3315 
3316 /* beware of emitting non-string list elements here; see commands/define.c */
3317 copy_generic_opt_arg_list_item:
3318 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
3319 		;
3320 
3321 
3322 /*****************************************************************************
3323  *
3324  *		QUERY :
3325  *				CREATE TABLE relname
3326  *
3327  *****************************************************************************/
3328 
3329 CreateStmt:	CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
3330 			OptInherit OptPartitionSpec table_access_method_clause OptWith
3331 			OnCommitOption OptTableSpace
3332 				{
3333 					CreateStmt *n = makeNode(CreateStmt);
3334 					$4->relpersistence = $2;
3335 					n->relation = $4;
3336 					n->tableElts = $6;
3337 					n->inhRelations = $8;
3338 					n->partspec = $9;
3339 					n->ofTypename = NULL;
3340 					n->constraints = NIL;
3341 					n->accessMethod = $10;
3342 					n->options = $11;
3343 					n->oncommit = $12;
3344 					n->tablespacename = $13;
3345 					n->if_not_exists = false;
3346 					$$ = (Node *)n;
3347 				}
3348 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
3349 			OptTableElementList ')' OptInherit OptPartitionSpec table_access_method_clause
3350 			OptWith OnCommitOption OptTableSpace
3351 				{
3352 					CreateStmt *n = makeNode(CreateStmt);
3353 					$7->relpersistence = $2;
3354 					n->relation = $7;
3355 					n->tableElts = $9;
3356 					n->inhRelations = $11;
3357 					n->partspec = $12;
3358 					n->ofTypename = NULL;
3359 					n->constraints = NIL;
3360 					n->accessMethod = $13;
3361 					n->options = $14;
3362 					n->oncommit = $15;
3363 					n->tablespacename = $16;
3364 					n->if_not_exists = true;
3365 					$$ = (Node *)n;
3366 				}
3367 		| CREATE OptTemp TABLE qualified_name OF any_name
3368 			OptTypedTableElementList OptPartitionSpec table_access_method_clause
3369 			OptWith OnCommitOption OptTableSpace
3370 				{
3371 					CreateStmt *n = makeNode(CreateStmt);
3372 					$4->relpersistence = $2;
3373 					n->relation = $4;
3374 					n->tableElts = $7;
3375 					n->inhRelations = NIL;
3376 					n->partspec = $8;
3377 					n->ofTypename = makeTypeNameFromNameList($6);
3378 					n->ofTypename->location = @6;
3379 					n->constraints = NIL;
3380 					n->accessMethod = $9;
3381 					n->options = $10;
3382 					n->oncommit = $11;
3383 					n->tablespacename = $12;
3384 					n->if_not_exists = false;
3385 					$$ = (Node *)n;
3386 				}
3387 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
3388 			OptTypedTableElementList OptPartitionSpec table_access_method_clause
3389 			OptWith OnCommitOption OptTableSpace
3390 				{
3391 					CreateStmt *n = makeNode(CreateStmt);
3392 					$7->relpersistence = $2;
3393 					n->relation = $7;
3394 					n->tableElts = $10;
3395 					n->inhRelations = NIL;
3396 					n->partspec = $11;
3397 					n->ofTypename = makeTypeNameFromNameList($9);
3398 					n->ofTypename->location = @9;
3399 					n->constraints = NIL;
3400 					n->accessMethod = $12;
3401 					n->options = $13;
3402 					n->oncommit = $14;
3403 					n->tablespacename = $15;
3404 					n->if_not_exists = true;
3405 					$$ = (Node *)n;
3406 				}
3407 		| CREATE OptTemp TABLE qualified_name PARTITION OF qualified_name
3408 			OptTypedTableElementList PartitionBoundSpec OptPartitionSpec
3409 			table_access_method_clause OptWith OnCommitOption OptTableSpace
3410 				{
3411 					CreateStmt *n = makeNode(CreateStmt);
3412 					$4->relpersistence = $2;
3413 					n->relation = $4;
3414 					n->tableElts = $8;
3415 					n->inhRelations = list_make1($7);
3416 					n->partbound = $9;
3417 					n->partspec = $10;
3418 					n->ofTypename = NULL;
3419 					n->constraints = NIL;
3420 					n->accessMethod = $11;
3421 					n->options = $12;
3422 					n->oncommit = $13;
3423 					n->tablespacename = $14;
3424 					n->if_not_exists = false;
3425 					$$ = (Node *)n;
3426 				}
3427 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name PARTITION OF
3428 			qualified_name OptTypedTableElementList PartitionBoundSpec OptPartitionSpec
3429 			table_access_method_clause OptWith OnCommitOption OptTableSpace
3430 				{
3431 					CreateStmt *n = makeNode(CreateStmt);
3432 					$7->relpersistence = $2;
3433 					n->relation = $7;
3434 					n->tableElts = $11;
3435 					n->inhRelations = list_make1($10);
3436 					n->partbound = $12;
3437 					n->partspec = $13;
3438 					n->ofTypename = NULL;
3439 					n->constraints = NIL;
3440 					n->accessMethod = $14;
3441 					n->options = $15;
3442 					n->oncommit = $16;
3443 					n->tablespacename = $17;
3444 					n->if_not_exists = true;
3445 					$$ = (Node *)n;
3446 				}
3447 		;
3448 
3449 /*
3450  * Redundancy here is needed to avoid shift/reduce conflicts,
3451  * since TEMP is not a reserved word.  See also OptTempTableName.
3452  *
3453  * NOTE: we accept both GLOBAL and LOCAL options.  They currently do nothing,
3454  * but future versions might consider GLOBAL to request SQL-spec-compliant
3455  * temp table behavior, so warn about that.  Since we have no modules the
3456  * LOCAL keyword is really meaningless; furthermore, some other products
3457  * implement LOCAL as meaning the same as our default temp table behavior,
3458  * so we'll probably continue to treat LOCAL as a noise word.
3459  */
3460 OptTemp:	TEMPORARY					{ $$ = RELPERSISTENCE_TEMP; }
3461 			| TEMP						{ $$ = RELPERSISTENCE_TEMP; }
3462 			| LOCAL TEMPORARY			{ $$ = RELPERSISTENCE_TEMP; }
3463 			| LOCAL TEMP				{ $$ = RELPERSISTENCE_TEMP; }
3464 			| GLOBAL TEMPORARY
3465 				{
3466 					ereport(WARNING,
3467 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3468 							 parser_errposition(@1)));
3469 					$$ = RELPERSISTENCE_TEMP;
3470 				}
3471 			| GLOBAL TEMP
3472 				{
3473 					ereport(WARNING,
3474 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3475 							 parser_errposition(@1)));
3476 					$$ = RELPERSISTENCE_TEMP;
3477 				}
3478 			| UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
3479 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
3480 		;
3481 
3482 OptTableElementList:
3483 			TableElementList					{ $$ = $1; }
3484 			| /*EMPTY*/							{ $$ = NIL; }
3485 		;
3486 
3487 OptTypedTableElementList:
3488 			'(' TypedTableElementList ')'		{ $$ = $2; }
3489 			| /*EMPTY*/							{ $$ = NIL; }
3490 		;
3491 
3492 TableElementList:
3493 			TableElement
3494 				{
3495 					$$ = list_make1($1);
3496 				}
3497 			| TableElementList ',' TableElement
3498 				{
3499 					$$ = lappend($1, $3);
3500 				}
3501 		;
3502 
3503 TypedTableElementList:
3504 			TypedTableElement
3505 				{
3506 					$$ = list_make1($1);
3507 				}
3508 			| TypedTableElementList ',' TypedTableElement
3509 				{
3510 					$$ = lappend($1, $3);
3511 				}
3512 		;
3513 
3514 TableElement:
3515 			columnDef							{ $$ = $1; }
3516 			| TableLikeClause					{ $$ = $1; }
3517 			| TableConstraint					{ $$ = $1; }
3518 		;
3519 
3520 TypedTableElement:
3521 			columnOptions						{ $$ = $1; }
3522 			| TableConstraint					{ $$ = $1; }
3523 		;
3524 
3525 columnDef:	ColId Typename opt_column_compression create_generic_options ColQualList
3526 				{
3527 					ColumnDef *n = makeNode(ColumnDef);
3528 					n->colname = $1;
3529 					n->typeName = $2;
3530 					n->compression = $3;
3531 					n->inhcount = 0;
3532 					n->is_local = true;
3533 					n->is_not_null = false;
3534 					n->is_from_type = false;
3535 					n->storage = 0;
3536 					n->raw_default = NULL;
3537 					n->cooked_default = NULL;
3538 					n->collOid = InvalidOid;
3539 					n->fdwoptions = $4;
3540 					SplitColQualList($5, &n->constraints, &n->collClause,
3541 									 yyscanner);
3542 					n->location = @1;
3543 					$$ = (Node *)n;
3544 				}
3545 		;
3546 
3547 columnOptions:	ColId ColQualList
3548 				{
3549 					ColumnDef *n = makeNode(ColumnDef);
3550 					n->colname = $1;
3551 					n->typeName = NULL;
3552 					n->inhcount = 0;
3553 					n->is_local = true;
3554 					n->is_not_null = false;
3555 					n->is_from_type = false;
3556 					n->storage = 0;
3557 					n->raw_default = NULL;
3558 					n->cooked_default = NULL;
3559 					n->collOid = InvalidOid;
3560 					SplitColQualList($2, &n->constraints, &n->collClause,
3561 									 yyscanner);
3562 					n->location = @1;
3563 					$$ = (Node *)n;
3564 				}
3565 				| ColId WITH OPTIONS ColQualList
3566 				{
3567 					ColumnDef *n = makeNode(ColumnDef);
3568 					n->colname = $1;
3569 					n->typeName = NULL;
3570 					n->inhcount = 0;
3571 					n->is_local = true;
3572 					n->is_not_null = false;
3573 					n->is_from_type = false;
3574 					n->storage = 0;
3575 					n->raw_default = NULL;
3576 					n->cooked_default = NULL;
3577 					n->collOid = InvalidOid;
3578 					SplitColQualList($4, &n->constraints, &n->collClause,
3579 									 yyscanner);
3580 					n->location = @1;
3581 					$$ = (Node *)n;
3582 				}
3583 		;
3584 
3585 column_compression:
3586 			COMPRESSION ColId						{ $$ = $2; }
3587 			| COMPRESSION DEFAULT					{ $$ = pstrdup("default"); }
3588 		;
3589 
3590 opt_column_compression:
3591 			column_compression						{ $$ = $1; }
3592 			| /*EMPTY*/								{ $$ = NULL; }
3593 		;
3594 
3595 ColQualList:
3596 			ColQualList ColConstraint				{ $$ = lappend($1, $2); }
3597 			| /*EMPTY*/								{ $$ = NIL; }
3598 		;
3599 
3600 ColConstraint:
3601 			CONSTRAINT name ColConstraintElem
3602 				{
3603 					Constraint *n = castNode(Constraint, $3);
3604 					n->conname = $2;
3605 					n->location = @1;
3606 					$$ = (Node *) n;
3607 				}
3608 			| ColConstraintElem						{ $$ = $1; }
3609 			| ConstraintAttr						{ $$ = $1; }
3610 			| COLLATE any_name
3611 				{
3612 					/*
3613 					 * Note: the CollateClause is momentarily included in
3614 					 * the list built by ColQualList, but we split it out
3615 					 * again in SplitColQualList.
3616 					 */
3617 					CollateClause *n = makeNode(CollateClause);
3618 					n->arg = NULL;
3619 					n->collname = $2;
3620 					n->location = @1;
3621 					$$ = (Node *) n;
3622 				}
3623 		;
3624 
3625 /* DEFAULT NULL is already the default for Postgres.
3626  * But define it here and carry it forward into the system
3627  * to make it explicit.
3628  * - thomas 1998-09-13
3629  *
3630  * WITH NULL and NULL are not SQL-standard syntax elements,
3631  * so leave them out. Use DEFAULT NULL to explicitly indicate
3632  * that a column may have that value. WITH NULL leads to
3633  * shift/reduce conflicts with WITH TIME ZONE anyway.
3634  * - thomas 1999-01-08
3635  *
3636  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
3637  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
3638  * or be part of a_expr NOT LIKE or similar constructs).
3639  */
3640 ColConstraintElem:
3641 			NOT NULL_P
3642 				{
3643 					Constraint *n = makeNode(Constraint);
3644 					n->contype = CONSTR_NOTNULL;
3645 					n->location = @1;
3646 					$$ = (Node *)n;
3647 				}
3648 			| NULL_P
3649 				{
3650 					Constraint *n = makeNode(Constraint);
3651 					n->contype = CONSTR_NULL;
3652 					n->location = @1;
3653 					$$ = (Node *)n;
3654 				}
3655 			| UNIQUE opt_definition OptConsTableSpace
3656 				{
3657 					Constraint *n = makeNode(Constraint);
3658 					n->contype = CONSTR_UNIQUE;
3659 					n->location = @1;
3660 					n->keys = NULL;
3661 					n->options = $2;
3662 					n->indexname = NULL;
3663 					n->indexspace = $3;
3664 					$$ = (Node *)n;
3665 				}
3666 			| PRIMARY KEY opt_definition OptConsTableSpace
3667 				{
3668 					Constraint *n = makeNode(Constraint);
3669 					n->contype = CONSTR_PRIMARY;
3670 					n->location = @1;
3671 					n->keys = NULL;
3672 					n->options = $3;
3673 					n->indexname = NULL;
3674 					n->indexspace = $4;
3675 					$$ = (Node *)n;
3676 				}
3677 			| CHECK '(' a_expr ')' opt_no_inherit
3678 				{
3679 					Constraint *n = makeNode(Constraint);
3680 					n->contype = CONSTR_CHECK;
3681 					n->location = @1;
3682 					n->is_no_inherit = $5;
3683 					n->raw_expr = $3;
3684 					n->cooked_expr = NULL;
3685 					n->skip_validation = false;
3686 					n->initially_valid = true;
3687 					$$ = (Node *)n;
3688 				}
3689 			| DEFAULT b_expr
3690 				{
3691 					Constraint *n = makeNode(Constraint);
3692 					n->contype = CONSTR_DEFAULT;
3693 					n->location = @1;
3694 					n->raw_expr = $2;
3695 					n->cooked_expr = NULL;
3696 					n->skip_validation = false;
3697 					n->initially_valid = true;
3698 					$$ = (Node *)n;
3699 				}
3700 			| GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
3701 				{
3702 					Constraint *n = makeNode(Constraint);
3703 					n->contype = CONSTR_IDENTITY;
3704 					n->generated_when = $2;
3705 					n->options = $5;
3706 					n->location = @1;
3707 					$$ = (Node *)n;
3708 				}
3709 			| GENERATED generated_when AS '(' a_expr ')' STORED
3710 				{
3711 					Constraint *n = makeNode(Constraint);
3712 					n->contype = CONSTR_GENERATED;
3713 					n->generated_when = $2;
3714 					n->raw_expr = $5;
3715 					n->cooked_expr = NULL;
3716 					n->location = @1;
3717 
3718 					/*
3719 					 * Can't do this in the grammar because of shift/reduce
3720 					 * conflicts.  (IDENTITY allows both ALWAYS and BY
3721 					 * DEFAULT, but generated columns only allow ALWAYS.)  We
3722 					 * can also give a more useful error message and location.
3723 					 */
3724 					if ($2 != ATTRIBUTE_IDENTITY_ALWAYS)
3725 						ereport(ERROR,
3726 								(errcode(ERRCODE_SYNTAX_ERROR),
3727 								 errmsg("for a generated column, GENERATED ALWAYS must be specified"),
3728 								 parser_errposition(@2)));
3729 
3730 					$$ = (Node *)n;
3731 				}
3732 			| REFERENCES qualified_name opt_column_list key_match key_actions
3733 				{
3734 					Constraint *n = makeNode(Constraint);
3735 					n->contype = CONSTR_FOREIGN;
3736 					n->location = @1;
3737 					n->pktable			= $2;
3738 					n->fk_attrs			= NIL;
3739 					n->pk_attrs			= $3;
3740 					n->fk_matchtype		= $4;
3741 					n->fk_upd_action	= (char) ($5 >> 8);
3742 					n->fk_del_action	= (char) ($5 & 0xFF);
3743 					n->skip_validation  = false;
3744 					n->initially_valid  = true;
3745 					$$ = (Node *)n;
3746 				}
3747 		;
3748 
3749 generated_when:
3750 			ALWAYS			{ $$ = ATTRIBUTE_IDENTITY_ALWAYS; }
3751 			| BY DEFAULT	{ $$ = ATTRIBUTE_IDENTITY_BY_DEFAULT; }
3752 		;
3753 
3754 /*
3755  * ConstraintAttr represents constraint attributes, which we parse as if
3756  * they were independent constraint clauses, in order to avoid shift/reduce
3757  * conflicts (since NOT might start either an independent NOT NULL clause
3758  * or an attribute).  parse_utilcmd.c is responsible for attaching the
3759  * attribute information to the preceding "real" constraint node, and for
3760  * complaining if attribute clauses appear in the wrong place or wrong
3761  * combinations.
3762  *
3763  * See also ConstraintAttributeSpec, which can be used in places where
3764  * there is no parsing conflict.  (Note: currently, NOT VALID and NO INHERIT
3765  * are allowed clauses in ConstraintAttributeSpec, but not here.  Someday we
3766  * might need to allow them here too, but for the moment it doesn't seem
3767  * useful in the statements that use ConstraintAttr.)
3768  */
3769 ConstraintAttr:
3770 			DEFERRABLE
3771 				{
3772 					Constraint *n = makeNode(Constraint);
3773 					n->contype = CONSTR_ATTR_DEFERRABLE;
3774 					n->location = @1;
3775 					$$ = (Node *)n;
3776 				}
3777 			| NOT DEFERRABLE
3778 				{
3779 					Constraint *n = makeNode(Constraint);
3780 					n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
3781 					n->location = @1;
3782 					$$ = (Node *)n;
3783 				}
3784 			| INITIALLY DEFERRED
3785 				{
3786 					Constraint *n = makeNode(Constraint);
3787 					n->contype = CONSTR_ATTR_DEFERRED;
3788 					n->location = @1;
3789 					$$ = (Node *)n;
3790 				}
3791 			| INITIALLY IMMEDIATE
3792 				{
3793 					Constraint *n = makeNode(Constraint);
3794 					n->contype = CONSTR_ATTR_IMMEDIATE;
3795 					n->location = @1;
3796 					$$ = (Node *)n;
3797 				}
3798 		;
3799 
3800 
3801 TableLikeClause:
3802 			LIKE qualified_name TableLikeOptionList
3803 				{
3804 					TableLikeClause *n = makeNode(TableLikeClause);
3805 					n->relation = $2;
3806 					n->options = $3;
3807 					n->relationOid = InvalidOid;
3808 					$$ = (Node *)n;
3809 				}
3810 		;
3811 
3812 TableLikeOptionList:
3813 				TableLikeOptionList INCLUDING TableLikeOption	{ $$ = $1 | $3; }
3814 				| TableLikeOptionList EXCLUDING TableLikeOption	{ $$ = $1 & ~$3; }
3815 				| /* EMPTY */						{ $$ = 0; }
3816 		;
3817 
3818 TableLikeOption:
3819 				COMMENTS			{ $$ = CREATE_TABLE_LIKE_COMMENTS; }
3820 				| COMPRESSION		{ $$ = CREATE_TABLE_LIKE_COMPRESSION; }
3821 				| CONSTRAINTS		{ $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
3822 				| DEFAULTS			{ $$ = CREATE_TABLE_LIKE_DEFAULTS; }
3823 				| IDENTITY_P		{ $$ = CREATE_TABLE_LIKE_IDENTITY; }
3824 				| GENERATED			{ $$ = CREATE_TABLE_LIKE_GENERATED; }
3825 				| INDEXES			{ $$ = CREATE_TABLE_LIKE_INDEXES; }
3826 				| STATISTICS		{ $$ = CREATE_TABLE_LIKE_STATISTICS; }
3827 				| STORAGE			{ $$ = CREATE_TABLE_LIKE_STORAGE; }
3828 				| ALL				{ $$ = CREATE_TABLE_LIKE_ALL; }
3829 		;
3830 
3831 
3832 /* ConstraintElem specifies constraint syntax which is not embedded into
3833  *	a column definition. ColConstraintElem specifies the embedded form.
3834  * - thomas 1997-12-03
3835  */
3836 TableConstraint:
3837 			CONSTRAINT name ConstraintElem
3838 				{
3839 					Constraint *n = castNode(Constraint, $3);
3840 					n->conname = $2;
3841 					n->location = @1;
3842 					$$ = (Node *) n;
3843 				}
3844 			| ConstraintElem						{ $$ = $1; }
3845 		;
3846 
3847 ConstraintElem:
3848 			CHECK '(' a_expr ')' ConstraintAttributeSpec
3849 				{
3850 					Constraint *n = makeNode(Constraint);
3851 					n->contype = CONSTR_CHECK;
3852 					n->location = @1;
3853 					n->raw_expr = $3;
3854 					n->cooked_expr = NULL;
3855 					processCASbits($5, @5, "CHECK",
3856 								   NULL, NULL, &n->skip_validation,
3857 								   &n->is_no_inherit, yyscanner);
3858 					n->initially_valid = !n->skip_validation;
3859 					$$ = (Node *)n;
3860 				}
3861 			| UNIQUE '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3862 				ConstraintAttributeSpec
3863 				{
3864 					Constraint *n = makeNode(Constraint);
3865 					n->contype = CONSTR_UNIQUE;
3866 					n->location = @1;
3867 					n->keys = $3;
3868 					n->including = $5;
3869 					n->options = $6;
3870 					n->indexname = NULL;
3871 					n->indexspace = $7;
3872 					processCASbits($8, @8, "UNIQUE",
3873 								   &n->deferrable, &n->initdeferred, NULL,
3874 								   NULL, yyscanner);
3875 					$$ = (Node *)n;
3876 				}
3877 			| UNIQUE ExistingIndex ConstraintAttributeSpec
3878 				{
3879 					Constraint *n = makeNode(Constraint);
3880 					n->contype = CONSTR_UNIQUE;
3881 					n->location = @1;
3882 					n->keys = NIL;
3883 					n->including = NIL;
3884 					n->options = NIL;
3885 					n->indexname = $2;
3886 					n->indexspace = NULL;
3887 					processCASbits($3, @3, "UNIQUE",
3888 								   &n->deferrable, &n->initdeferred, NULL,
3889 								   NULL, yyscanner);
3890 					$$ = (Node *)n;
3891 				}
3892 			| PRIMARY KEY '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3893 				ConstraintAttributeSpec
3894 				{
3895 					Constraint *n = makeNode(Constraint);
3896 					n->contype = CONSTR_PRIMARY;
3897 					n->location = @1;
3898 					n->keys = $4;
3899 					n->including = $6;
3900 					n->options = $7;
3901 					n->indexname = NULL;
3902 					n->indexspace = $8;
3903 					processCASbits($9, @9, "PRIMARY KEY",
3904 								   &n->deferrable, &n->initdeferred, NULL,
3905 								   NULL, yyscanner);
3906 					$$ = (Node *)n;
3907 				}
3908 			| PRIMARY KEY ExistingIndex ConstraintAttributeSpec
3909 				{
3910 					Constraint *n = makeNode(Constraint);
3911 					n->contype = CONSTR_PRIMARY;
3912 					n->location = @1;
3913 					n->keys = NIL;
3914 					n->including = NIL;
3915 					n->options = NIL;
3916 					n->indexname = $3;
3917 					n->indexspace = NULL;
3918 					processCASbits($4, @4, "PRIMARY KEY",
3919 								   &n->deferrable, &n->initdeferred, NULL,
3920 								   NULL, yyscanner);
3921 					$$ = (Node *)n;
3922 				}
3923 			| EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
3924 				opt_c_include opt_definition OptConsTableSpace OptWhereClause
3925 				ConstraintAttributeSpec
3926 				{
3927 					Constraint *n = makeNode(Constraint);
3928 					n->contype = CONSTR_EXCLUSION;
3929 					n->location = @1;
3930 					n->access_method	= $2;
3931 					n->exclusions		= $4;
3932 					n->including		= $6;
3933 					n->options			= $7;
3934 					n->indexname		= NULL;
3935 					n->indexspace		= $8;
3936 					n->where_clause		= $9;
3937 					processCASbits($10, @10, "EXCLUDE",
3938 								   &n->deferrable, &n->initdeferred, NULL,
3939 								   NULL, yyscanner);
3940 					$$ = (Node *)n;
3941 				}
3942 			| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
3943 				opt_column_list key_match key_actions ConstraintAttributeSpec
3944 				{
3945 					Constraint *n = makeNode(Constraint);
3946 					n->contype = CONSTR_FOREIGN;
3947 					n->location = @1;
3948 					n->pktable			= $7;
3949 					n->fk_attrs			= $4;
3950 					n->pk_attrs			= $8;
3951 					n->fk_matchtype		= $9;
3952 					n->fk_upd_action	= (char) ($10 >> 8);
3953 					n->fk_del_action	= (char) ($10 & 0xFF);
3954 					processCASbits($11, @11, "FOREIGN KEY",
3955 								   &n->deferrable, &n->initdeferred,
3956 								   &n->skip_validation, NULL,
3957 								   yyscanner);
3958 					n->initially_valid = !n->skip_validation;
3959 					$$ = (Node *)n;
3960 				}
3961 		;
3962 
3963 opt_no_inherit:	NO INHERIT							{  $$ = true; }
3964 			| /* EMPTY */							{  $$ = false; }
3965 		;
3966 
3967 opt_column_list:
3968 			'(' columnList ')'						{ $$ = $2; }
3969 			| /*EMPTY*/								{ $$ = NIL; }
3970 		;
3971 
3972 columnList:
3973 			columnElem								{ $$ = list_make1($1); }
3974 			| columnList ',' columnElem				{ $$ = lappend($1, $3); }
3975 		;
3976 
3977 columnElem: ColId
3978 				{
3979 					$$ = (Node *) makeString($1);
3980 				}
3981 		;
3982 
3983 opt_c_include:	INCLUDE '(' columnList ')'			{ $$ = $3; }
3984 			 |		/* EMPTY */						{ $$ = NIL; }
3985 		;
3986 
3987 key_match:  MATCH FULL
3988 			{
3989 				$$ = FKCONSTR_MATCH_FULL;
3990 			}
3991 		| MATCH PARTIAL
3992 			{
3993 				ereport(ERROR,
3994 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3995 						 errmsg("MATCH PARTIAL not yet implemented"),
3996 						 parser_errposition(@1)));
3997 				$$ = FKCONSTR_MATCH_PARTIAL;
3998 			}
3999 		| MATCH SIMPLE
4000 			{
4001 				$$ = FKCONSTR_MATCH_SIMPLE;
4002 			}
4003 		| /*EMPTY*/
4004 			{
4005 				$$ = FKCONSTR_MATCH_SIMPLE;
4006 			}
4007 		;
4008 
4009 ExclusionConstraintList:
4010 			ExclusionConstraintElem					{ $$ = list_make1($1); }
4011 			| ExclusionConstraintList ',' ExclusionConstraintElem
4012 													{ $$ = lappend($1, $3); }
4013 		;
4014 
4015 ExclusionConstraintElem: index_elem WITH any_operator
4016 			{
4017 				$$ = list_make2($1, $3);
4018 			}
4019 			/* allow OPERATOR() decoration for the benefit of ruleutils.c */
4020 			| index_elem WITH OPERATOR '(' any_operator ')'
4021 			{
4022 				$$ = list_make2($1, $5);
4023 			}
4024 		;
4025 
4026 OptWhereClause:
4027 			WHERE '(' a_expr ')'					{ $$ = $3; }
4028 			| /*EMPTY*/								{ $$ = NULL; }
4029 		;
4030 
4031 /*
4032  * We combine the update and delete actions into one value temporarily
4033  * for simplicity of parsing, and then break them down again in the
4034  * calling production.  update is in the left 8 bits, delete in the right.
4035  * Note that NOACTION is the default.
4036  */
4037 key_actions:
4038 			key_update
4039 				{ $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
4040 			| key_delete
4041 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
4042 			| key_update key_delete
4043 				{ $$ = ($1 << 8) | ($2 & 0xFF); }
4044 			| key_delete key_update
4045 				{ $$ = ($2 << 8) | ($1 & 0xFF); }
4046 			| /*EMPTY*/
4047 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
4048 		;
4049 
4050 key_update: ON UPDATE key_action		{ $$ = $3; }
4051 		;
4052 
4053 key_delete: ON DELETE_P key_action		{ $$ = $3; }
4054 		;
4055 
4056 key_action:
4057 			NO ACTION					{ $$ = FKCONSTR_ACTION_NOACTION; }
4058 			| RESTRICT					{ $$ = FKCONSTR_ACTION_RESTRICT; }
4059 			| CASCADE					{ $$ = FKCONSTR_ACTION_CASCADE; }
4060 			| SET NULL_P				{ $$ = FKCONSTR_ACTION_SETNULL; }
4061 			| SET DEFAULT				{ $$ = FKCONSTR_ACTION_SETDEFAULT; }
4062 		;
4063 
4064 OptInherit: INHERITS '(' qualified_name_list ')'	{ $$ = $3; }
4065 			| /*EMPTY*/								{ $$ = NIL; }
4066 		;
4067 
4068 /* Optional partition key specification */
4069 OptPartitionSpec: PartitionSpec	{ $$ = $1; }
4070 			| /*EMPTY*/			{ $$ = NULL; }
4071 		;
4072 
4073 PartitionSpec: PARTITION BY ColId '(' part_params ')'
4074 				{
4075 					PartitionSpec *n = makeNode(PartitionSpec);
4076 
4077 					n->strategy = $3;
4078 					n->partParams = $5;
4079 					n->location = @1;
4080 
4081 					$$ = n;
4082 				}
4083 		;
4084 
4085 part_params:	part_elem						{ $$ = list_make1($1); }
4086 			| part_params ',' part_elem			{ $$ = lappend($1, $3); }
4087 		;
4088 
4089 part_elem: ColId opt_collate opt_class
4090 				{
4091 					PartitionElem *n = makeNode(PartitionElem);
4092 
4093 					n->name = $1;
4094 					n->expr = NULL;
4095 					n->collation = $2;
4096 					n->opclass = $3;
4097 					n->location = @1;
4098 					$$ = n;
4099 				}
4100 			| func_expr_windowless opt_collate opt_class
4101 				{
4102 					PartitionElem *n = makeNode(PartitionElem);
4103 
4104 					n->name = NULL;
4105 					n->expr = $1;
4106 					n->collation = $2;
4107 					n->opclass = $3;
4108 					n->location = @1;
4109 					$$ = n;
4110 				}
4111 			| '(' a_expr ')' opt_collate opt_class
4112 				{
4113 					PartitionElem *n = makeNode(PartitionElem);
4114 
4115 					n->name = NULL;
4116 					n->expr = $2;
4117 					n->collation = $4;
4118 					n->opclass = $5;
4119 					n->location = @1;
4120 					$$ = n;
4121 				}
4122 		;
4123 
4124 table_access_method_clause:
4125 			USING name							{ $$ = $2; }
4126 			| /*EMPTY*/							{ $$ = NULL; }
4127 		;
4128 
4129 /* WITHOUT OIDS is legacy only */
4130 OptWith:
4131 			WITH reloptions				{ $$ = $2; }
4132 			| WITHOUT OIDS				{ $$ = NIL; }
4133 			| /*EMPTY*/					{ $$ = NIL; }
4134 		;
4135 
4136 OnCommitOption:  ON COMMIT DROP				{ $$ = ONCOMMIT_DROP; }
4137 			| ON COMMIT DELETE_P ROWS		{ $$ = ONCOMMIT_DELETE_ROWS; }
4138 			| ON COMMIT PRESERVE ROWS		{ $$ = ONCOMMIT_PRESERVE_ROWS; }
4139 			| /*EMPTY*/						{ $$ = ONCOMMIT_NOOP; }
4140 		;
4141 
4142 OptTableSpace:   TABLESPACE name					{ $$ = $2; }
4143 			| /*EMPTY*/								{ $$ = NULL; }
4144 		;
4145 
4146 OptConsTableSpace:   USING INDEX TABLESPACE name	{ $$ = $4; }
4147 			| /*EMPTY*/								{ $$ = NULL; }
4148 		;
4149 
4150 ExistingIndex:   USING INDEX name					{ $$ = $3; }
4151 		;
4152 
4153 /*****************************************************************************
4154  *
4155  *		QUERY :
4156  *				CREATE STATISTICS [IF NOT EXISTS] stats_name [(stat types)]
4157  *					ON expression-list FROM from_list
4158  *
4159  * Note: the expectation here is that the clauses after ON are a subset of
4160  * SELECT syntax, allowing for expressions and joined tables, and probably
4161  * someday a WHERE clause.  Much less than that is currently implemented,
4162  * but the grammar accepts it and then we'll throw FEATURE_NOT_SUPPORTED
4163  * errors as necessary at execution.
4164  *
4165  *****************************************************************************/
4166 
4167 CreateStatsStmt:
4168 			CREATE STATISTICS any_name
4169 			opt_name_list ON stats_params FROM from_list
4170 				{
4171 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
4172 					n->defnames = $3;
4173 					n->stat_types = $4;
4174 					n->exprs = $6;
4175 					n->relations = $8;
4176 					n->stxcomment = NULL;
4177 					n->if_not_exists = false;
4178 					$$ = (Node *)n;
4179 				}
4180 			| CREATE STATISTICS IF_P NOT EXISTS any_name
4181 			opt_name_list ON stats_params FROM from_list
4182 				{
4183 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
4184 					n->defnames = $6;
4185 					n->stat_types = $7;
4186 					n->exprs = $9;
4187 					n->relations = $11;
4188 					n->stxcomment = NULL;
4189 					n->if_not_exists = true;
4190 					$$ = (Node *)n;
4191 				}
4192 			;
4193 
4194 /*
4195  * Statistics attributes can be either simple column references, or arbitrary
4196  * expressions in parens.  For compatibility with index attributes permitted
4197  * in CREATE INDEX, we allow an expression that's just a function call to be
4198  * written without parens.
4199  */
4200 
4201 stats_params:	stats_param							{ $$ = list_make1($1); }
4202 			| stats_params ',' stats_param			{ $$ = lappend($1, $3); }
4203 		;
4204 
4205 stats_param:	ColId
4206 				{
4207 					$$ = makeNode(StatsElem);
4208 					$$->name = $1;
4209 					$$->expr = NULL;
4210 				}
4211 			| func_expr_windowless
4212 				{
4213 					$$ = makeNode(StatsElem);
4214 					$$->name = NULL;
4215 					$$->expr = $1;
4216 				}
4217 			| '(' a_expr ')'
4218 				{
4219 					$$ = makeNode(StatsElem);
4220 					$$->name = NULL;
4221 					$$->expr = $2;
4222 				}
4223 		;
4224 
4225 /*****************************************************************************
4226  *
4227  *		QUERY :
4228  *				ALTER STATISTICS [IF EXISTS] stats_name
4229  *					SET STATISTICS  <SignedIconst>
4230  *
4231  *****************************************************************************/
4232 
4233 AlterStatsStmt:
4234 			ALTER STATISTICS any_name SET STATISTICS SignedIconst
4235 				{
4236 					AlterStatsStmt *n = makeNode(AlterStatsStmt);
4237 					n->defnames = $3;
4238 					n->missing_ok = false;
4239 					n->stxstattarget = $6;
4240 					$$ = (Node *)n;
4241 				}
4242 			| ALTER STATISTICS IF_P EXISTS any_name SET STATISTICS SignedIconst
4243 				{
4244 					AlterStatsStmt *n = makeNode(AlterStatsStmt);
4245 					n->defnames = $5;
4246 					n->missing_ok = true;
4247 					n->stxstattarget = $8;
4248 					$$ = (Node *)n;
4249 				}
4250 			;
4251 
4252 /*****************************************************************************
4253  *
4254  *		QUERY :
4255  *				CREATE TABLE relname AS SelectStmt [ WITH [NO] DATA ]
4256  *
4257  *
4258  * Note: SELECT ... INTO is a now-deprecated alternative for this.
4259  *
4260  *****************************************************************************/
4261 
4262 CreateAsStmt:
4263 		CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
4264 				{
4265 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4266 					ctas->query = $6;
4267 					ctas->into = $4;
4268 					ctas->objtype = OBJECT_TABLE;
4269 					ctas->is_select_into = false;
4270 					ctas->if_not_exists = false;
4271 					/* cram additional flags into the IntoClause */
4272 					$4->rel->relpersistence = $2;
4273 					$4->skipData = !($7);
4274 					$$ = (Node *) ctas;
4275 				}
4276 		| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS SelectStmt opt_with_data
4277 				{
4278 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4279 					ctas->query = $9;
4280 					ctas->into = $7;
4281 					ctas->objtype = OBJECT_TABLE;
4282 					ctas->is_select_into = false;
4283 					ctas->if_not_exists = true;
4284 					/* cram additional flags into the IntoClause */
4285 					$7->rel->relpersistence = $2;
4286 					$7->skipData = !($10);
4287 					$$ = (Node *) ctas;
4288 				}
4289 		;
4290 
4291 create_as_target:
4292 			qualified_name opt_column_list table_access_method_clause
4293 			OptWith OnCommitOption OptTableSpace
4294 				{
4295 					$$ = makeNode(IntoClause);
4296 					$$->rel = $1;
4297 					$$->colNames = $2;
4298 					$$->accessMethod = $3;
4299 					$$->options = $4;
4300 					$$->onCommit = $5;
4301 					$$->tableSpaceName = $6;
4302 					$$->viewQuery = NULL;
4303 					$$->skipData = false;		/* might get changed later */
4304 				}
4305 		;
4306 
4307 opt_with_data:
4308 			WITH DATA_P								{ $$ = true; }
4309 			| WITH NO DATA_P						{ $$ = false; }
4310 			| /*EMPTY*/								{ $$ = true; }
4311 		;
4312 
4313 
4314 /*****************************************************************************
4315  *
4316  *		QUERY :
4317  *				CREATE MATERIALIZED VIEW relname AS SelectStmt
4318  *
4319  *****************************************************************************/
4320 
4321 CreateMatViewStmt:
4322 		CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data
4323 				{
4324 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4325 					ctas->query = $7;
4326 					ctas->into = $5;
4327 					ctas->objtype = OBJECT_MATVIEW;
4328 					ctas->is_select_into = false;
4329 					ctas->if_not_exists = false;
4330 					/* cram additional flags into the IntoClause */
4331 					$5->rel->relpersistence = $2;
4332 					$5->skipData = !($8);
4333 					$$ = (Node *) ctas;
4334 				}
4335 		| CREATE OptNoLog MATERIALIZED VIEW IF_P NOT EXISTS create_mv_target AS SelectStmt opt_with_data
4336 				{
4337 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4338 					ctas->query = $10;
4339 					ctas->into = $8;
4340 					ctas->objtype = OBJECT_MATVIEW;
4341 					ctas->is_select_into = false;
4342 					ctas->if_not_exists = true;
4343 					/* cram additional flags into the IntoClause */
4344 					$8->rel->relpersistence = $2;
4345 					$8->skipData = !($11);
4346 					$$ = (Node *) ctas;
4347 				}
4348 		;
4349 
4350 create_mv_target:
4351 			qualified_name opt_column_list table_access_method_clause opt_reloptions OptTableSpace
4352 				{
4353 					$$ = makeNode(IntoClause);
4354 					$$->rel = $1;
4355 					$$->colNames = $2;
4356 					$$->accessMethod = $3;
4357 					$$->options = $4;
4358 					$$->onCommit = ONCOMMIT_NOOP;
4359 					$$->tableSpaceName = $5;
4360 					$$->viewQuery = NULL;		/* filled at analysis time */
4361 					$$->skipData = false;		/* might get changed later */
4362 				}
4363 		;
4364 
4365 OptNoLog:	UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
4366 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
4367 		;
4368 
4369 
4370 /*****************************************************************************
4371  *
4372  *		QUERY :
4373  *				REFRESH MATERIALIZED VIEW qualified_name
4374  *
4375  *****************************************************************************/
4376 
4377 RefreshMatViewStmt:
4378 			REFRESH MATERIALIZED VIEW opt_concurrently qualified_name opt_with_data
4379 				{
4380 					RefreshMatViewStmt *n = makeNode(RefreshMatViewStmt);
4381 					n->concurrent = $4;
4382 					n->relation = $5;
4383 					n->skipData = !($6);
4384 					$$ = (Node *) n;
4385 				}
4386 		;
4387 
4388 
4389 /*****************************************************************************
4390  *
4391  *		QUERY :
4392  *				CREATE SEQUENCE seqname
4393  *				ALTER SEQUENCE seqname
4394  *
4395  *****************************************************************************/
4396 
4397 CreateSeqStmt:
4398 			CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
4399 				{
4400 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4401 					$4->relpersistence = $2;
4402 					n->sequence = $4;
4403 					n->options = $5;
4404 					n->ownerId = InvalidOid;
4405 					n->if_not_exists = false;
4406 					$$ = (Node *)n;
4407 				}
4408 			| CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
4409 				{
4410 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4411 					$7->relpersistence = $2;
4412 					n->sequence = $7;
4413 					n->options = $8;
4414 					n->ownerId = InvalidOid;
4415 					n->if_not_exists = true;
4416 					$$ = (Node *)n;
4417 				}
4418 		;
4419 
4420 AlterSeqStmt:
4421 			ALTER SEQUENCE qualified_name SeqOptList
4422 				{
4423 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4424 					n->sequence = $3;
4425 					n->options = $4;
4426 					n->missing_ok = false;
4427 					$$ = (Node *)n;
4428 				}
4429 			| ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
4430 				{
4431 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4432 					n->sequence = $5;
4433 					n->options = $6;
4434 					n->missing_ok = true;
4435 					$$ = (Node *)n;
4436 				}
4437 
4438 		;
4439 
4440 OptSeqOptList: SeqOptList							{ $$ = $1; }
4441 			| /*EMPTY*/								{ $$ = NIL; }
4442 		;
4443 
4444 OptParenthesizedSeqOptList: '(' SeqOptList ')'		{ $$ = $2; }
4445 			| /*EMPTY*/								{ $$ = NIL; }
4446 		;
4447 
4448 SeqOptList: SeqOptElem								{ $$ = list_make1($1); }
4449 			| SeqOptList SeqOptElem					{ $$ = lappend($1, $2); }
4450 		;
4451 
4452 SeqOptElem: AS SimpleTypename
4453 				{
4454 					$$ = makeDefElem("as", (Node *)$2, @1);
4455 				}
4456 			| CACHE NumericOnly
4457 				{
4458 					$$ = makeDefElem("cache", (Node *)$2, @1);
4459 				}
4460 			| CYCLE
4461 				{
4462 					$$ = makeDefElem("cycle", (Node *)makeInteger(true), @1);
4463 				}
4464 			| NO CYCLE
4465 				{
4466 					$$ = makeDefElem("cycle", (Node *)makeInteger(false), @1);
4467 				}
4468 			| INCREMENT opt_by NumericOnly
4469 				{
4470 					$$ = makeDefElem("increment", (Node *)$3, @1);
4471 				}
4472 			| MAXVALUE NumericOnly
4473 				{
4474 					$$ = makeDefElem("maxvalue", (Node *)$2, @1);
4475 				}
4476 			| MINVALUE NumericOnly
4477 				{
4478 					$$ = makeDefElem("minvalue", (Node *)$2, @1);
4479 				}
4480 			| NO MAXVALUE
4481 				{
4482 					$$ = makeDefElem("maxvalue", NULL, @1);
4483 				}
4484 			| NO MINVALUE
4485 				{
4486 					$$ = makeDefElem("minvalue", NULL, @1);
4487 				}
4488 			| OWNED BY any_name
4489 				{
4490 					$$ = makeDefElem("owned_by", (Node *)$3, @1);
4491 				}
4492 			| SEQUENCE NAME_P any_name
4493 				{
4494 					/* not documented, only used by pg_dump */
4495 					$$ = makeDefElem("sequence_name", (Node *)$3, @1);
4496 				}
4497 			| START opt_with NumericOnly
4498 				{
4499 					$$ = makeDefElem("start", (Node *)$3, @1);
4500 				}
4501 			| RESTART
4502 				{
4503 					$$ = makeDefElem("restart", NULL, @1);
4504 				}
4505 			| RESTART opt_with NumericOnly
4506 				{
4507 					$$ = makeDefElem("restart", (Node *)$3, @1);
4508 				}
4509 		;
4510 
4511 opt_by:		BY
4512 			| /* EMPTY */
4513 	  ;
4514 
4515 NumericOnly:
4516 			FCONST								{ $$ = makeFloat($1); }
4517 			| '+' FCONST						{ $$ = makeFloat($2); }
4518 			| '-' FCONST
4519 				{
4520 					$$ = makeFloat($2);
4521 					doNegateFloat($$);
4522 				}
4523 			| SignedIconst						{ $$ = makeInteger($1); }
4524 		;
4525 
4526 NumericOnly_list:	NumericOnly						{ $$ = list_make1($1); }
4527 				| NumericOnly_list ',' NumericOnly	{ $$ = lappend($1, $3); }
4528 		;
4529 
4530 /*****************************************************************************
4531  *
4532  *		QUERIES :
4533  *				CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
4534  *				DROP [PROCEDURAL] LANGUAGE ...
4535  *
4536  *****************************************************************************/
4537 
4538 CreatePLangStmt:
4539 			CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE name
4540 			{
4541 				/*
4542 				 * We now interpret parameterless CREATE LANGUAGE as
4543 				 * CREATE EXTENSION.  "OR REPLACE" is silently translated
4544 				 * to "IF NOT EXISTS", which isn't quite the same, but
4545 				 * seems more useful than throwing an error.  We just
4546 				 * ignore TRUSTED, as the previous code would have too.
4547 				 */
4548 				CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4549 				n->if_not_exists = $2;
4550 				n->extname = $6;
4551 				n->options = NIL;
4552 				$$ = (Node *)n;
4553 			}
4554 			| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE name
4555 			  HANDLER handler_name opt_inline_handler opt_validator
4556 			{
4557 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4558 				n->replace = $2;
4559 				n->plname = $6;
4560 				n->plhandler = $8;
4561 				n->plinline = $9;
4562 				n->plvalidator = $10;
4563 				n->pltrusted = $3;
4564 				$$ = (Node *)n;
4565 			}
4566 		;
4567 
4568 opt_trusted:
4569 			TRUSTED									{ $$ = true; }
4570 			| /*EMPTY*/								{ $$ = false; }
4571 		;
4572 
4573 /* This ought to be just func_name, but that causes reduce/reduce conflicts
4574  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
4575  * Work around by using simple names, instead.
4576  */
4577 handler_name:
4578 			name						{ $$ = list_make1(makeString($1)); }
4579 			| name attrs				{ $$ = lcons(makeString($1), $2); }
4580 		;
4581 
4582 opt_inline_handler:
4583 			INLINE_P handler_name					{ $$ = $2; }
4584 			| /*EMPTY*/								{ $$ = NIL; }
4585 		;
4586 
4587 validator_clause:
4588 			VALIDATOR handler_name					{ $$ = $2; }
4589 			| NO VALIDATOR							{ $$ = NIL; }
4590 		;
4591 
4592 opt_validator:
4593 			validator_clause						{ $$ = $1; }
4594 			| /*EMPTY*/								{ $$ = NIL; }
4595 		;
4596 
4597 opt_procedural:
4598 			PROCEDURAL
4599 			| /*EMPTY*/
4600 		;
4601 
4602 /*****************************************************************************
4603  *
4604  *		QUERY:
4605  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
4606  *
4607  *****************************************************************************/
4608 
4609 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst opt_reloptions
4610 				{
4611 					CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
4612 					n->tablespacename = $3;
4613 					n->owner = $4;
4614 					n->location = $6;
4615 					n->options = $7;
4616 					$$ = (Node *) n;
4617 				}
4618 		;
4619 
4620 OptTableSpaceOwner: OWNER RoleSpec		{ $$ = $2; }
4621 			| /*EMPTY */				{ $$ = NULL; }
4622 		;
4623 
4624 /*****************************************************************************
4625  *
4626  *		QUERY :
4627  *				DROP TABLESPACE <tablespace>
4628  *
4629  *		No need for drop behaviour as we cannot implement dependencies for
4630  *		objects in other databases; we can only support RESTRICT.
4631  *
4632  ****************************************************************************/
4633 
4634 DropTableSpaceStmt: DROP TABLESPACE name
4635 				{
4636 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4637 					n->tablespacename = $3;
4638 					n->missing_ok = false;
4639 					$$ = (Node *) n;
4640 				}
4641 				|  DROP TABLESPACE IF_P EXISTS name
4642 				{
4643 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4644 					n->tablespacename = $5;
4645 					n->missing_ok = true;
4646 					$$ = (Node *) n;
4647 				}
4648 		;
4649 
4650 /*****************************************************************************
4651  *
4652  *		QUERY:
4653  *             CREATE EXTENSION extension
4654  *             [ WITH ] [ SCHEMA schema ] [ VERSION version ]
4655  *
4656  *****************************************************************************/
4657 
4658 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
4659 				{
4660 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4661 					n->extname = $3;
4662 					n->if_not_exists = false;
4663 					n->options = $5;
4664 					$$ = (Node *) n;
4665 				}
4666 				| CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
4667 				{
4668 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4669 					n->extname = $6;
4670 					n->if_not_exists = true;
4671 					n->options = $8;
4672 					$$ = (Node *) n;
4673 				}
4674 		;
4675 
4676 create_extension_opt_list:
4677 			create_extension_opt_list create_extension_opt_item
4678 				{ $$ = lappend($1, $2); }
4679 			| /* EMPTY */
4680 				{ $$ = NIL; }
4681 		;
4682 
4683 create_extension_opt_item:
4684 			SCHEMA name
4685 				{
4686 					$$ = makeDefElem("schema", (Node *)makeString($2), @1);
4687 				}
4688 			| VERSION_P NonReservedWord_or_Sconst
4689 				{
4690 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4691 				}
4692 			| FROM NonReservedWord_or_Sconst
4693 				{
4694 					ereport(ERROR,
4695 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4696 							 errmsg("CREATE EXTENSION ... FROM is no longer supported"),
4697 							 parser_errposition(@1)));
4698 				}
4699 			| CASCADE
4700 				{
4701 					$$ = makeDefElem("cascade", (Node *)makeInteger(true), @1);
4702 				}
4703 		;
4704 
4705 /*****************************************************************************
4706  *
4707  * ALTER EXTENSION name UPDATE [ TO version ]
4708  *
4709  *****************************************************************************/
4710 
4711 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
4712 				{
4713 					AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
4714 					n->extname = $3;
4715 					n->options = $5;
4716 					$$ = (Node *) n;
4717 				}
4718 		;
4719 
4720 alter_extension_opt_list:
4721 			alter_extension_opt_list alter_extension_opt_item
4722 				{ $$ = lappend($1, $2); }
4723 			| /* EMPTY */
4724 				{ $$ = NIL; }
4725 		;
4726 
4727 alter_extension_opt_item:
4728 			TO NonReservedWord_or_Sconst
4729 				{
4730 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4731 				}
4732 		;
4733 
4734 /*****************************************************************************
4735  *
4736  * ALTER EXTENSION name ADD/DROP object-identifier
4737  *
4738  *****************************************************************************/
4739 
4740 AlterExtensionContentsStmt:
4741 			ALTER EXTENSION name add_drop object_type_name name
4742 				{
4743 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4744 					n->extname = $3;
4745 					n->action = $4;
4746 					n->objtype = $5;
4747 					n->object = (Node *) makeString($6);
4748 					$$ = (Node *)n;
4749 				}
4750 			| ALTER EXTENSION name add_drop object_type_any_name any_name
4751 				{
4752 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4753 					n->extname = $3;
4754 					n->action = $4;
4755 					n->objtype = $5;
4756 					n->object = (Node *) $6;
4757 					$$ = (Node *)n;
4758 				}
4759 			| ALTER EXTENSION name add_drop AGGREGATE aggregate_with_argtypes
4760 				{
4761 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4762 					n->extname = $3;
4763 					n->action = $4;
4764 					n->objtype = OBJECT_AGGREGATE;
4765 					n->object = (Node *) $6;
4766 					$$ = (Node *)n;
4767 				}
4768 			| ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
4769 				{
4770 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4771 					n->extname = $3;
4772 					n->action = $4;
4773 					n->objtype = OBJECT_CAST;
4774 					n->object = (Node *) list_make2($7, $9);
4775 					$$ = (Node *) n;
4776 				}
4777 			| ALTER EXTENSION name add_drop DOMAIN_P Typename
4778 				{
4779 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4780 					n->extname = $3;
4781 					n->action = $4;
4782 					n->objtype = OBJECT_DOMAIN;
4783 					n->object = (Node *) $6;
4784 					$$ = (Node *)n;
4785 				}
4786 			| ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
4787 				{
4788 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4789 					n->extname = $3;
4790 					n->action = $4;
4791 					n->objtype = OBJECT_FUNCTION;
4792 					n->object = (Node *) $6;
4793 					$$ = (Node *)n;
4794 				}
4795 			| ALTER EXTENSION name add_drop OPERATOR operator_with_argtypes
4796 				{
4797 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4798 					n->extname = $3;
4799 					n->action = $4;
4800 					n->objtype = OBJECT_OPERATOR;
4801 					n->object = (Node *) $6;
4802 					$$ = (Node *)n;
4803 				}
4804 			| ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING name
4805 				{
4806 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4807 					n->extname = $3;
4808 					n->action = $4;
4809 					n->objtype = OBJECT_OPCLASS;
4810 					n->object = (Node *) lcons(makeString($9), $7);
4811 					$$ = (Node *)n;
4812 				}
4813 			| ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING name
4814 				{
4815 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4816 					n->extname = $3;
4817 					n->action = $4;
4818 					n->objtype = OBJECT_OPFAMILY;
4819 					n->object = (Node *) lcons(makeString($9), $7);
4820 					$$ = (Node *)n;
4821 				}
4822 			| ALTER EXTENSION name add_drop PROCEDURE function_with_argtypes
4823 				{
4824 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4825 					n->extname = $3;
4826 					n->action = $4;
4827 					n->objtype = OBJECT_PROCEDURE;
4828 					n->object = (Node *) $6;
4829 					$$ = (Node *)n;
4830 				}
4831 			| ALTER EXTENSION name add_drop ROUTINE function_with_argtypes
4832 				{
4833 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4834 					n->extname = $3;
4835 					n->action = $4;
4836 					n->objtype = OBJECT_ROUTINE;
4837 					n->object = (Node *) $6;
4838 					$$ = (Node *)n;
4839 				}
4840 			| ALTER EXTENSION name add_drop TRANSFORM FOR Typename LANGUAGE name
4841 				{
4842 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4843 					n->extname = $3;
4844 					n->action = $4;
4845 					n->objtype = OBJECT_TRANSFORM;
4846 					n->object = (Node *) list_make2($7, makeString($9));
4847 					$$ = (Node *)n;
4848 				}
4849 			| ALTER EXTENSION name add_drop TYPE_P Typename
4850 				{
4851 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4852 					n->extname = $3;
4853 					n->action = $4;
4854 					n->objtype = OBJECT_TYPE;
4855 					n->object = (Node *) $6;
4856 					$$ = (Node *)n;
4857 				}
4858 		;
4859 
4860 /*****************************************************************************
4861  *
4862  *		QUERY:
4863  *             CREATE FOREIGN DATA WRAPPER name options
4864  *
4865  *****************************************************************************/
4866 
4867 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
4868 				{
4869 					CreateFdwStmt *n = makeNode(CreateFdwStmt);
4870 					n->fdwname = $5;
4871 					n->func_options = $6;
4872 					n->options = $7;
4873 					$$ = (Node *) n;
4874 				}
4875 		;
4876 
4877 fdw_option:
4878 			HANDLER handler_name				{ $$ = makeDefElem("handler", (Node *)$2, @1); }
4879 			| NO HANDLER						{ $$ = makeDefElem("handler", NULL, @1); }
4880 			| VALIDATOR handler_name			{ $$ = makeDefElem("validator", (Node *)$2, @1); }
4881 			| NO VALIDATOR						{ $$ = makeDefElem("validator", NULL, @1); }
4882 		;
4883 
4884 fdw_options:
4885 			fdw_option							{ $$ = list_make1($1); }
4886 			| fdw_options fdw_option			{ $$ = lappend($1, $2); }
4887 		;
4888 
4889 opt_fdw_options:
4890 			fdw_options							{ $$ = $1; }
4891 			| /*EMPTY*/							{ $$ = NIL; }
4892 		;
4893 
4894 /*****************************************************************************
4895  *
4896  *		QUERY :
4897  *				ALTER FOREIGN DATA WRAPPER name options
4898  *
4899  ****************************************************************************/
4900 
4901 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
4902 				{
4903 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4904 					n->fdwname = $5;
4905 					n->func_options = $6;
4906 					n->options = $7;
4907 					$$ = (Node *) n;
4908 				}
4909 			| ALTER FOREIGN DATA_P WRAPPER name fdw_options
4910 				{
4911 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4912 					n->fdwname = $5;
4913 					n->func_options = $6;
4914 					n->options = NIL;
4915 					$$ = (Node *) n;
4916 				}
4917 		;
4918 
4919 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
4920 create_generic_options:
4921 			OPTIONS '(' generic_option_list ')'			{ $$ = $3; }
4922 			| /*EMPTY*/									{ $$ = NIL; }
4923 		;
4924 
4925 generic_option_list:
4926 			generic_option_elem
4927 				{
4928 					$$ = list_make1($1);
4929 				}
4930 			| generic_option_list ',' generic_option_elem
4931 				{
4932 					$$ = lappend($1, $3);
4933 				}
4934 		;
4935 
4936 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
4937 alter_generic_options:
4938 			OPTIONS	'(' alter_generic_option_list ')'		{ $$ = $3; }
4939 		;
4940 
4941 alter_generic_option_list:
4942 			alter_generic_option_elem
4943 				{
4944 					$$ = list_make1($1);
4945 				}
4946 			| alter_generic_option_list ',' alter_generic_option_elem
4947 				{
4948 					$$ = lappend($1, $3);
4949 				}
4950 		;
4951 
4952 alter_generic_option_elem:
4953 			generic_option_elem
4954 				{
4955 					$$ = $1;
4956 				}
4957 			| SET generic_option_elem
4958 				{
4959 					$$ = $2;
4960 					$$->defaction = DEFELEM_SET;
4961 				}
4962 			| ADD_P generic_option_elem
4963 				{
4964 					$$ = $2;
4965 					$$->defaction = DEFELEM_ADD;
4966 				}
4967 			| DROP generic_option_name
4968 				{
4969 					$$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP, @2);
4970 				}
4971 		;
4972 
4973 generic_option_elem:
4974 			generic_option_name generic_option_arg
4975 				{
4976 					$$ = makeDefElem($1, $2, @1);
4977 				}
4978 		;
4979 
4980 generic_option_name:
4981 				ColLabel			{ $$ = $1; }
4982 		;
4983 
4984 /* We could use def_arg here, but the spec only requires string literals */
4985 generic_option_arg:
4986 				Sconst				{ $$ = (Node *) makeString($1); }
4987 		;
4988 
4989 /*****************************************************************************
4990  *
4991  *		QUERY:
4992  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
4993  *
4994  *****************************************************************************/
4995 
4996 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
4997 						 FOREIGN DATA_P WRAPPER name create_generic_options
4998 				{
4999 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
5000 					n->servername = $3;
5001 					n->servertype = $4;
5002 					n->version = $5;
5003 					n->fdwname = $9;
5004 					n->options = $10;
5005 					n->if_not_exists = false;
5006 					$$ = (Node *) n;
5007 				}
5008 				| CREATE SERVER IF_P NOT EXISTS name opt_type opt_foreign_server_version
5009 						 FOREIGN DATA_P WRAPPER name create_generic_options
5010 				{
5011 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
5012 					n->servername = $6;
5013 					n->servertype = $7;
5014 					n->version = $8;
5015 					n->fdwname = $12;
5016 					n->options = $13;
5017 					n->if_not_exists = true;
5018 					$$ = (Node *) n;
5019 				}
5020 		;
5021 
5022 opt_type:
5023 			TYPE_P Sconst			{ $$ = $2; }
5024 			| /*EMPTY*/				{ $$ = NULL; }
5025 		;
5026 
5027 
5028 foreign_server_version:
5029 			VERSION_P Sconst		{ $$ = $2; }
5030 		|	VERSION_P NULL_P		{ $$ = NULL; }
5031 		;
5032 
5033 opt_foreign_server_version:
5034 			foreign_server_version	{ $$ = $1; }
5035 			| /*EMPTY*/				{ $$ = NULL; }
5036 		;
5037 
5038 /*****************************************************************************
5039  *
5040  *		QUERY :
5041  *				ALTER SERVER name [VERSION] [OPTIONS]
5042  *
5043  ****************************************************************************/
5044 
5045 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
5046 				{
5047 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5048 					n->servername = $3;
5049 					n->version = $4;
5050 					n->options = $5;
5051 					n->has_version = true;
5052 					$$ = (Node *) n;
5053 				}
5054 			| ALTER SERVER name foreign_server_version
5055 				{
5056 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5057 					n->servername = $3;
5058 					n->version = $4;
5059 					n->has_version = true;
5060 					$$ = (Node *) n;
5061 				}
5062 			| ALTER SERVER name alter_generic_options
5063 				{
5064 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5065 					n->servername = $3;
5066 					n->options = $4;
5067 					$$ = (Node *) n;
5068 				}
5069 		;
5070 
5071 /*****************************************************************************
5072  *
5073  *		QUERY:
5074  *             CREATE FOREIGN TABLE relname (...) SERVER name (...)
5075  *
5076  *****************************************************************************/
5077 
5078 CreateForeignTableStmt:
5079 		CREATE FOREIGN TABLE qualified_name
5080 			'(' OptTableElementList ')'
5081 			OptInherit SERVER name create_generic_options
5082 				{
5083 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5084 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
5085 					n->base.relation = $4;
5086 					n->base.tableElts = $6;
5087 					n->base.inhRelations = $8;
5088 					n->base.ofTypename = NULL;
5089 					n->base.constraints = NIL;
5090 					n->base.options = NIL;
5091 					n->base.oncommit = ONCOMMIT_NOOP;
5092 					n->base.tablespacename = NULL;
5093 					n->base.if_not_exists = false;
5094 					/* FDW-specific data */
5095 					n->servername = $10;
5096 					n->options = $11;
5097 					$$ = (Node *) n;
5098 				}
5099 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5100 			'(' OptTableElementList ')'
5101 			OptInherit SERVER name create_generic_options
5102 				{
5103 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5104 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
5105 					n->base.relation = $7;
5106 					n->base.tableElts = $9;
5107 					n->base.inhRelations = $11;
5108 					n->base.ofTypename = NULL;
5109 					n->base.constraints = NIL;
5110 					n->base.options = NIL;
5111 					n->base.oncommit = ONCOMMIT_NOOP;
5112 					n->base.tablespacename = NULL;
5113 					n->base.if_not_exists = true;
5114 					/* FDW-specific data */
5115 					n->servername = $13;
5116 					n->options = $14;
5117 					$$ = (Node *) n;
5118 				}
5119 		| CREATE FOREIGN TABLE qualified_name
5120 			PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5121 			SERVER name create_generic_options
5122 				{
5123 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5124 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
5125 					n->base.relation = $4;
5126 					n->base.inhRelations = list_make1($7);
5127 					n->base.tableElts = $8;
5128 					n->base.partbound = $9;
5129 					n->base.ofTypename = NULL;
5130 					n->base.constraints = NIL;
5131 					n->base.options = NIL;
5132 					n->base.oncommit = ONCOMMIT_NOOP;
5133 					n->base.tablespacename = NULL;
5134 					n->base.if_not_exists = false;
5135 					/* FDW-specific data */
5136 					n->servername = $11;
5137 					n->options = $12;
5138 					$$ = (Node *) n;
5139 				}
5140 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5141 			PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5142 			SERVER name create_generic_options
5143 				{
5144 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5145 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
5146 					n->base.relation = $7;
5147 					n->base.inhRelations = list_make1($10);
5148 					n->base.tableElts = $11;
5149 					n->base.partbound = $12;
5150 					n->base.ofTypename = NULL;
5151 					n->base.constraints = NIL;
5152 					n->base.options = NIL;
5153 					n->base.oncommit = ONCOMMIT_NOOP;
5154 					n->base.tablespacename = NULL;
5155 					n->base.if_not_exists = true;
5156 					/* FDW-specific data */
5157 					n->servername = $14;
5158 					n->options = $15;
5159 					$$ = (Node *) n;
5160 				}
5161 		;
5162 
5163 /*****************************************************************************
5164  *
5165  *		QUERY:
5166  *				IMPORT FOREIGN SCHEMA remote_schema
5167  *				[ { LIMIT TO | EXCEPT } ( table_list ) ]
5168  *				FROM SERVER server_name INTO local_schema [ OPTIONS (...) ]
5169  *
5170  ****************************************************************************/
5171 
5172 ImportForeignSchemaStmt:
5173 		IMPORT_P FOREIGN SCHEMA name import_qualification
5174 		  FROM SERVER name INTO name create_generic_options
5175 			{
5176 				ImportForeignSchemaStmt *n = makeNode(ImportForeignSchemaStmt);
5177 				n->server_name = $8;
5178 				n->remote_schema = $4;
5179 				n->local_schema = $10;
5180 				n->list_type = $5->type;
5181 				n->table_list = $5->table_names;
5182 				n->options = $11;
5183 				$$ = (Node *) n;
5184 			}
5185 		;
5186 
5187 import_qualification_type:
5188 		LIMIT TO				{ $$ = FDW_IMPORT_SCHEMA_LIMIT_TO; }
5189 		| EXCEPT				{ $$ = FDW_IMPORT_SCHEMA_EXCEPT; }
5190 		;
5191 
5192 import_qualification:
5193 		import_qualification_type '(' relation_expr_list ')'
5194 			{
5195 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5196 				n->type = $1;
5197 				n->table_names = $3;
5198 				$$ = n;
5199 			}
5200 		| /*EMPTY*/
5201 			{
5202 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5203 				n->type = FDW_IMPORT_SCHEMA_ALL;
5204 				n->table_names = NIL;
5205 				$$ = n;
5206 			}
5207 		;
5208 
5209 /*****************************************************************************
5210  *
5211  *		QUERY:
5212  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
5213  *
5214  *****************************************************************************/
5215 
5216 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
5217 				{
5218 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5219 					n->user = $5;
5220 					n->servername = $7;
5221 					n->options = $8;
5222 					n->if_not_exists = false;
5223 					$$ = (Node *) n;
5224 				}
5225 				| CREATE USER MAPPING IF_P NOT EXISTS FOR auth_ident SERVER name create_generic_options
5226 				{
5227 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5228 					n->user = $8;
5229 					n->servername = $10;
5230 					n->options = $11;
5231 					n->if_not_exists = true;
5232 					$$ = (Node *) n;
5233 				}
5234 		;
5235 
5236 /* User mapping authorization identifier */
5237 auth_ident: RoleSpec			{ $$ = $1; }
5238 			| USER				{ $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1); }
5239 		;
5240 
5241 /*****************************************************************************
5242  *
5243  *		QUERY :
5244  *				DROP USER MAPPING FOR auth_ident SERVER name
5245  *
5246  * XXX you'd think this should have a CASCADE/RESTRICT option, even if it's
5247  * only pro forma; but the SQL standard doesn't show one.
5248  ****************************************************************************/
5249 
5250 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
5251 				{
5252 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5253 					n->user = $5;
5254 					n->servername = $7;
5255 					n->missing_ok = false;
5256 					$$ = (Node *) n;
5257 				}
5258 				|  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
5259 				{
5260 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5261 					n->user = $7;
5262 					n->servername = $9;
5263 					n->missing_ok = true;
5264 					$$ = (Node *) n;
5265 				}
5266 		;
5267 
5268 /*****************************************************************************
5269  *
5270  *		QUERY :
5271  *				ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
5272  *
5273  ****************************************************************************/
5274 
5275 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
5276 				{
5277 					AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
5278 					n->user = $5;
5279 					n->servername = $7;
5280 					n->options = $8;
5281 					$$ = (Node *) n;
5282 				}
5283 		;
5284 
5285 /*****************************************************************************
5286  *
5287  *		QUERIES:
5288  *				CREATE POLICY name ON table
5289  *					[AS { PERMISSIVE | RESTRICTIVE } ]
5290  *					[FOR { SELECT | INSERT | UPDATE | DELETE } ]
5291  *					[TO role, ...]
5292  *					[USING (qual)] [WITH CHECK (with check qual)]
5293  *				ALTER POLICY name ON table [TO role, ...]
5294  *					[USING (qual)] [WITH CHECK (with check qual)]
5295  *
5296  *****************************************************************************/
5297 
5298 CreatePolicyStmt:
5299 			CREATE POLICY name ON qualified_name RowSecurityDefaultPermissive
5300 				RowSecurityDefaultForCmd RowSecurityDefaultToRole
5301 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5302 				{
5303 					CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
5304 					n->policy_name = $3;
5305 					n->table = $5;
5306 					n->permissive = $6;
5307 					n->cmd_name = $7;
5308 					n->roles = $8;
5309 					n->qual = $9;
5310 					n->with_check = $10;
5311 					$$ = (Node *) n;
5312 				}
5313 		;
5314 
5315 AlterPolicyStmt:
5316 			ALTER POLICY name ON qualified_name RowSecurityOptionalToRole
5317 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5318 				{
5319 					AlterPolicyStmt *n = makeNode(AlterPolicyStmt);
5320 					n->policy_name = $3;
5321 					n->table = $5;
5322 					n->roles = $6;
5323 					n->qual = $7;
5324 					n->with_check = $8;
5325 					$$ = (Node *) n;
5326 				}
5327 		;
5328 
5329 RowSecurityOptionalExpr:
5330 			USING '(' a_expr ')'	{ $$ = $3; }
5331 			| /* EMPTY */			{ $$ = NULL; }
5332 		;
5333 
5334 RowSecurityOptionalWithCheck:
5335 			WITH CHECK '(' a_expr ')'		{ $$ = $4; }
5336 			| /* EMPTY */					{ $$ = NULL; }
5337 		;
5338 
5339 RowSecurityDefaultToRole:
5340 			TO role_list			{ $$ = $2; }
5341 			| /* EMPTY */			{ $$ = list_make1(makeRoleSpec(ROLESPEC_PUBLIC, -1)); }
5342 		;
5343 
5344 RowSecurityOptionalToRole:
5345 			TO role_list			{ $$ = $2; }
5346 			| /* EMPTY */			{ $$ = NULL; }
5347 		;
5348 
5349 RowSecurityDefaultPermissive:
5350 			AS IDENT
5351 				{
5352 					if (strcmp($2, "permissive") == 0)
5353 						$$ = true;
5354 					else if (strcmp($2, "restrictive") == 0)
5355 						$$ = false;
5356 					else
5357 						ereport(ERROR,
5358 								(errcode(ERRCODE_SYNTAX_ERROR),
5359 							 errmsg("unrecognized row security option \"%s\"", $2),
5360 								 errhint("Only PERMISSIVE or RESTRICTIVE policies are supported currently."),
5361 									 parser_errposition(@2)));
5362 
5363 				}
5364 			| /* EMPTY */			{ $$ = true; }
5365 		;
5366 
5367 RowSecurityDefaultForCmd:
5368 			FOR row_security_cmd	{ $$ = $2; }
5369 			| /* EMPTY */			{ $$ = "all"; }
5370 		;
5371 
5372 row_security_cmd:
5373 			ALL				{ $$ = "all"; }
5374 		|	SELECT			{ $$ = "select"; }
5375 		|	INSERT			{ $$ = "insert"; }
5376 		|	UPDATE			{ $$ = "update"; }
5377 		|	DELETE_P		{ $$ = "delete"; }
5378 		;
5379 
5380 /*****************************************************************************
5381  *
5382  *		QUERY:
5383  *             CREATE ACCESS METHOD name HANDLER handler_name
5384  *
5385  *****************************************************************************/
5386 
5387 CreateAmStmt: CREATE ACCESS METHOD name TYPE_P am_type HANDLER handler_name
5388 				{
5389 					CreateAmStmt *n = makeNode(CreateAmStmt);
5390 					n->amname = $4;
5391 					n->handler_name = $8;
5392 					n->amtype = $6;
5393 					$$ = (Node *) n;
5394 				}
5395 		;
5396 
5397 am_type:
5398 			INDEX			{ $$ = AMTYPE_INDEX; }
5399 		|	TABLE			{ $$ = AMTYPE_TABLE; }
5400 		;
5401 
5402 /*****************************************************************************
5403  *
5404  *		QUERIES :
5405  *				CREATE TRIGGER ...
5406  *
5407  *****************************************************************************/
5408 
5409 CreateTrigStmt:
5410 			CREATE opt_or_replace TRIGGER name TriggerActionTime TriggerEvents ON
5411 			qualified_name TriggerReferencing TriggerForSpec TriggerWhen
5412 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' TriggerFuncArgs ')'
5413 				{
5414 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5415 					n->replace = $2;
5416 					n->isconstraint = false;
5417 					n->trigname = $4;
5418 					n->relation = $8;
5419 					n->funcname = $14;
5420 					n->args = $16;
5421 					n->row = $10;
5422 					n->timing = $5;
5423 					n->events = intVal(linitial($6));
5424 					n->columns = (List *) lsecond($6);
5425 					n->whenClause = $11;
5426 					n->transitionRels = $9;
5427 					n->deferrable = false;
5428 					n->initdeferred = false;
5429 					n->constrrel = NULL;
5430 					$$ = (Node *)n;
5431 				}
5432 		  | CREATE opt_or_replace CONSTRAINT TRIGGER name AFTER TriggerEvents ON
5433 			qualified_name OptConstrFromTable ConstraintAttributeSpec
5434 			FOR EACH ROW TriggerWhen
5435 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' TriggerFuncArgs ')'
5436 				{
5437 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5438 					n->replace = $2;
5439 					if (n->replace) /* not supported, see CreateTrigger */
5440 						ereport(ERROR,
5441 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5442 								 errmsg("CREATE OR REPLACE CONSTRAINT TRIGGER is not supported")));
5443 					n->isconstraint = true;
5444 					n->trigname = $5;
5445 					n->relation = $9;
5446 					n->funcname = $18;
5447 					n->args = $20;
5448 					n->row = true;
5449 					n->timing = TRIGGER_TYPE_AFTER;
5450 					n->events = intVal(linitial($7));
5451 					n->columns = (List *) lsecond($7);
5452 					n->whenClause = $15;
5453 					n->transitionRels = NIL;
5454 					processCASbits($11, @11, "TRIGGER",
5455 								   &n->deferrable, &n->initdeferred, NULL,
5456 								   NULL, yyscanner);
5457 					n->constrrel = $10;
5458 					$$ = (Node *)n;
5459 				}
5460 		;
5461 
5462 TriggerActionTime:
5463 			BEFORE								{ $$ = TRIGGER_TYPE_BEFORE; }
5464 			| AFTER								{ $$ = TRIGGER_TYPE_AFTER; }
5465 			| INSTEAD OF						{ $$ = TRIGGER_TYPE_INSTEAD; }
5466 		;
5467 
5468 TriggerEvents:
5469 			TriggerOneEvent
5470 				{ $$ = $1; }
5471 			| TriggerEvents OR TriggerOneEvent
5472 				{
5473 					int		events1 = intVal(linitial($1));
5474 					int		events2 = intVal(linitial($3));
5475 					List   *columns1 = (List *) lsecond($1);
5476 					List   *columns2 = (List *) lsecond($3);
5477 
5478 					if (events1 & events2)
5479 						parser_yyerror("duplicate trigger events specified");
5480 					/*
5481 					 * concat'ing the columns lists loses information about
5482 					 * which columns went with which event, but so long as
5483 					 * only UPDATE carries columns and we disallow multiple
5484 					 * UPDATE items, it doesn't matter.  Command execution
5485 					 * should just ignore the columns for non-UPDATE events.
5486 					 */
5487 					$$ = list_make2(makeInteger(events1 | events2),
5488 									list_concat(columns1, columns2));
5489 				}
5490 		;
5491 
5492 TriggerOneEvent:
5493 			INSERT
5494 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
5495 			| DELETE_P
5496 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
5497 			| UPDATE
5498 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
5499 			| UPDATE OF columnList
5500 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
5501 			| TRUNCATE
5502 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
5503 		;
5504 
5505 TriggerReferencing:
5506 			REFERENCING TriggerTransitions			{ $$ = $2; }
5507 			| /*EMPTY*/								{ $$ = NIL; }
5508 		;
5509 
5510 TriggerTransitions:
5511 			TriggerTransition						{ $$ = list_make1($1); }
5512 			| TriggerTransitions TriggerTransition	{ $$ = lappend($1, $2); }
5513 		;
5514 
5515 TriggerTransition:
5516 			TransitionOldOrNew TransitionRowOrTable opt_as TransitionRelName
5517 				{
5518 					TriggerTransition *n = makeNode(TriggerTransition);
5519 					n->name = $4;
5520 					n->isNew = $1;
5521 					n->isTable = $2;
5522 					$$ = (Node *)n;
5523 				}
5524 		;
5525 
5526 TransitionOldOrNew:
5527 			NEW										{ $$ = true; }
5528 			| OLD									{ $$ = false; }
5529 		;
5530 
5531 TransitionRowOrTable:
5532 			TABLE									{ $$ = true; }
5533 			/*
5534 			 * According to the standard, lack of a keyword here implies ROW.
5535 			 * Support for that would require prohibiting ROW entirely here,
5536 			 * reserving the keyword ROW, and/or requiring AS (instead of
5537 			 * allowing it to be optional, as the standard specifies) as the
5538 			 * next token.  Requiring ROW seems cleanest and easiest to
5539 			 * explain.
5540 			 */
5541 			| ROW									{ $$ = false; }
5542 		;
5543 
5544 TransitionRelName:
5545 			ColId									{ $$ = $1; }
5546 		;
5547 
5548 TriggerForSpec:
5549 			FOR TriggerForOptEach TriggerForType
5550 				{
5551 					$$ = $3;
5552 				}
5553 			| /* EMPTY */
5554 				{
5555 					/*
5556 					 * If ROW/STATEMENT not specified, default to
5557 					 * STATEMENT, per SQL
5558 					 */
5559 					$$ = false;
5560 				}
5561 		;
5562 
5563 TriggerForOptEach:
5564 			EACH
5565 			| /*EMPTY*/
5566 		;
5567 
5568 TriggerForType:
5569 			ROW										{ $$ = true; }
5570 			| STATEMENT								{ $$ = false; }
5571 		;
5572 
5573 TriggerWhen:
5574 			WHEN '(' a_expr ')'						{ $$ = $3; }
5575 			| /*EMPTY*/								{ $$ = NULL; }
5576 		;
5577 
5578 FUNCTION_or_PROCEDURE:
5579 			FUNCTION
5580 		|	PROCEDURE
5581 		;
5582 
5583 TriggerFuncArgs:
5584 			TriggerFuncArg							{ $$ = list_make1($1); }
5585 			| TriggerFuncArgs ',' TriggerFuncArg	{ $$ = lappend($1, $3); }
5586 			| /*EMPTY*/								{ $$ = NIL; }
5587 		;
5588 
5589 TriggerFuncArg:
5590 			Iconst
5591 				{
5592 					$$ = makeString(psprintf("%d", $1));
5593 				}
5594 			| FCONST								{ $$ = makeString($1); }
5595 			| Sconst								{ $$ = makeString($1); }
5596 			| ColLabel								{ $$ = makeString($1); }
5597 		;
5598 
5599 OptConstrFromTable:
5600 			FROM qualified_name						{ $$ = $2; }
5601 			| /*EMPTY*/								{ $$ = NULL; }
5602 		;
5603 
5604 ConstraintAttributeSpec:
5605 			/*EMPTY*/
5606 				{ $$ = 0; }
5607 			| ConstraintAttributeSpec ConstraintAttributeElem
5608 				{
5609 					/*
5610 					 * We must complain about conflicting options.
5611 					 * We could, but choose not to, complain about redundant
5612 					 * options (ie, where $2's bit is already set in $1).
5613 					 */
5614 					int		newspec = $1 | $2;
5615 
5616 					/* special message for this case */
5617 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED)) == (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED))
5618 						ereport(ERROR,
5619 								(errcode(ERRCODE_SYNTAX_ERROR),
5620 								 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
5621 								 parser_errposition(@2)));
5622 					/* generic message for other conflicts */
5623 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE)) == (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE) ||
5624 						(newspec & (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED)) == (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED))
5625 						ereport(ERROR,
5626 								(errcode(ERRCODE_SYNTAX_ERROR),
5627 								 errmsg("conflicting constraint properties"),
5628 								 parser_errposition(@2)));
5629 					$$ = newspec;
5630 				}
5631 		;
5632 
5633 ConstraintAttributeElem:
5634 			NOT DEFERRABLE					{ $$ = CAS_NOT_DEFERRABLE; }
5635 			| DEFERRABLE					{ $$ = CAS_DEFERRABLE; }
5636 			| INITIALLY IMMEDIATE			{ $$ = CAS_INITIALLY_IMMEDIATE; }
5637 			| INITIALLY DEFERRED			{ $$ = CAS_INITIALLY_DEFERRED; }
5638 			| NOT VALID						{ $$ = CAS_NOT_VALID; }
5639 			| NO INHERIT					{ $$ = CAS_NO_INHERIT; }
5640 		;
5641 
5642 
5643 /*****************************************************************************
5644  *
5645  *		QUERIES :
5646  *				CREATE EVENT TRIGGER ...
5647  *				ALTER EVENT TRIGGER ...
5648  *
5649  *****************************************************************************/
5650 
5651 CreateEventTrigStmt:
5652 			CREATE EVENT TRIGGER name ON ColLabel
5653 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' ')'
5654 				{
5655 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5656 					n->trigname = $4;
5657 					n->eventname = $6;
5658 					n->whenclause = NULL;
5659 					n->funcname = $9;
5660 					$$ = (Node *)n;
5661 				}
5662 		  | CREATE EVENT TRIGGER name ON ColLabel
5663 			WHEN event_trigger_when_list
5664 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' ')'
5665 				{
5666 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5667 					n->trigname = $4;
5668 					n->eventname = $6;
5669 					n->whenclause = $8;
5670 					n->funcname = $11;
5671 					$$ = (Node *)n;
5672 				}
5673 		;
5674 
5675 event_trigger_when_list:
5676 		  event_trigger_when_item
5677 			{ $$ = list_make1($1); }
5678 		| event_trigger_when_list AND event_trigger_when_item
5679 			{ $$ = lappend($1, $3); }
5680 		;
5681 
5682 event_trigger_when_item:
5683 		ColId IN_P '(' event_trigger_value_list ')'
5684 			{ $$ = makeDefElem($1, (Node *) $4, @1); }
5685 		;
5686 
5687 event_trigger_value_list:
5688 		  SCONST
5689 			{ $$ = list_make1(makeString($1)); }
5690 		| event_trigger_value_list ',' SCONST
5691 			{ $$ = lappend($1, makeString($3)); }
5692 		;
5693 
5694 AlterEventTrigStmt:
5695 			ALTER EVENT TRIGGER name enable_trigger
5696 				{
5697 					AlterEventTrigStmt *n = makeNode(AlterEventTrigStmt);
5698 					n->trigname = $4;
5699 					n->tgenabled = $5;
5700 					$$ = (Node *) n;
5701 				}
5702 		;
5703 
5704 enable_trigger:
5705 			ENABLE_P					{ $$ = TRIGGER_FIRES_ON_ORIGIN; }
5706 			| ENABLE_P REPLICA			{ $$ = TRIGGER_FIRES_ON_REPLICA; }
5707 			| ENABLE_P ALWAYS			{ $$ = TRIGGER_FIRES_ALWAYS; }
5708 			| DISABLE_P					{ $$ = TRIGGER_DISABLED; }
5709 		;
5710 
5711 /*****************************************************************************
5712  *
5713  *		QUERY :
5714  *				CREATE ASSERTION ...
5715  *
5716  *****************************************************************************/
5717 
5718 CreateAssertionStmt:
5719 			CREATE ASSERTION any_name CHECK '(' a_expr ')' ConstraintAttributeSpec
5720 				{
5721 					ereport(ERROR,
5722 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5723 							 errmsg("CREATE ASSERTION is not yet implemented")));
5724 
5725 					$$ = NULL;
5726 				}
5727 		;
5728 
5729 
5730 /*****************************************************************************
5731  *
5732  *		QUERY :
5733  *				define (aggregate,operator,type)
5734  *
5735  *****************************************************************************/
5736 
5737 DefineStmt:
5738 			CREATE opt_or_replace AGGREGATE func_name aggr_args definition
5739 				{
5740 					DefineStmt *n = makeNode(DefineStmt);
5741 					n->kind = OBJECT_AGGREGATE;
5742 					n->oldstyle = false;
5743 					n->replace = $2;
5744 					n->defnames = $4;
5745 					n->args = $5;
5746 					n->definition = $6;
5747 					$$ = (Node *)n;
5748 				}
5749 			| CREATE opt_or_replace AGGREGATE func_name old_aggr_definition
5750 				{
5751 					/* old-style (pre-8.2) syntax for CREATE AGGREGATE */
5752 					DefineStmt *n = makeNode(DefineStmt);
5753 					n->kind = OBJECT_AGGREGATE;
5754 					n->oldstyle = true;
5755 					n->replace = $2;
5756 					n->defnames = $4;
5757 					n->args = NIL;
5758 					n->definition = $5;
5759 					$$ = (Node *)n;
5760 				}
5761 			| CREATE OPERATOR any_operator definition
5762 				{
5763 					DefineStmt *n = makeNode(DefineStmt);
5764 					n->kind = OBJECT_OPERATOR;
5765 					n->oldstyle = false;
5766 					n->defnames = $3;
5767 					n->args = NIL;
5768 					n->definition = $4;
5769 					$$ = (Node *)n;
5770 				}
5771 			| CREATE TYPE_P any_name definition
5772 				{
5773 					DefineStmt *n = makeNode(DefineStmt);
5774 					n->kind = OBJECT_TYPE;
5775 					n->oldstyle = false;
5776 					n->defnames = $3;
5777 					n->args = NIL;
5778 					n->definition = $4;
5779 					$$ = (Node *)n;
5780 				}
5781 			| CREATE TYPE_P any_name
5782 				{
5783 					/* Shell type (identified by lack of definition) */
5784 					DefineStmt *n = makeNode(DefineStmt);
5785 					n->kind = OBJECT_TYPE;
5786 					n->oldstyle = false;
5787 					n->defnames = $3;
5788 					n->args = NIL;
5789 					n->definition = NIL;
5790 					$$ = (Node *)n;
5791 				}
5792 			| CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
5793 				{
5794 					CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
5795 
5796 					/* can't use qualified_name, sigh */
5797 					n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
5798 					n->coldeflist = $6;
5799 					$$ = (Node *)n;
5800 				}
5801 			| CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
5802 				{
5803 					CreateEnumStmt *n = makeNode(CreateEnumStmt);
5804 					n->typeName = $3;
5805 					n->vals = $7;
5806 					$$ = (Node *)n;
5807 				}
5808 			| CREATE TYPE_P any_name AS RANGE definition
5809 				{
5810 					CreateRangeStmt *n = makeNode(CreateRangeStmt);
5811 					n->typeName = $3;
5812 					n->params	= $6;
5813 					$$ = (Node *)n;
5814 				}
5815 			| CREATE TEXT_P SEARCH PARSER any_name definition
5816 				{
5817 					DefineStmt *n = makeNode(DefineStmt);
5818 					n->kind = OBJECT_TSPARSER;
5819 					n->args = NIL;
5820 					n->defnames = $5;
5821 					n->definition = $6;
5822 					$$ = (Node *)n;
5823 				}
5824 			| CREATE TEXT_P SEARCH DICTIONARY any_name definition
5825 				{
5826 					DefineStmt *n = makeNode(DefineStmt);
5827 					n->kind = OBJECT_TSDICTIONARY;
5828 					n->args = NIL;
5829 					n->defnames = $5;
5830 					n->definition = $6;
5831 					$$ = (Node *)n;
5832 				}
5833 			| CREATE TEXT_P SEARCH TEMPLATE any_name definition
5834 				{
5835 					DefineStmt *n = makeNode(DefineStmt);
5836 					n->kind = OBJECT_TSTEMPLATE;
5837 					n->args = NIL;
5838 					n->defnames = $5;
5839 					n->definition = $6;
5840 					$$ = (Node *)n;
5841 				}
5842 			| CREATE TEXT_P SEARCH CONFIGURATION any_name definition
5843 				{
5844 					DefineStmt *n = makeNode(DefineStmt);
5845 					n->kind = OBJECT_TSCONFIGURATION;
5846 					n->args = NIL;
5847 					n->defnames = $5;
5848 					n->definition = $6;
5849 					$$ = (Node *)n;
5850 				}
5851 			| CREATE COLLATION any_name definition
5852 				{
5853 					DefineStmt *n = makeNode(DefineStmt);
5854 					n->kind = OBJECT_COLLATION;
5855 					n->args = NIL;
5856 					n->defnames = $3;
5857 					n->definition = $4;
5858 					$$ = (Node *)n;
5859 				}
5860 			| CREATE COLLATION IF_P NOT EXISTS any_name definition
5861 				{
5862 					DefineStmt *n = makeNode(DefineStmt);
5863 					n->kind = OBJECT_COLLATION;
5864 					n->args = NIL;
5865 					n->defnames = $6;
5866 					n->definition = $7;
5867 					n->if_not_exists = true;
5868 					$$ = (Node *)n;
5869 				}
5870 			| CREATE COLLATION any_name FROM any_name
5871 				{
5872 					DefineStmt *n = makeNode(DefineStmt);
5873 					n->kind = OBJECT_COLLATION;
5874 					n->args = NIL;
5875 					n->defnames = $3;
5876 					n->definition = list_make1(makeDefElem("from", (Node *) $5, @5));
5877 					$$ = (Node *)n;
5878 				}
5879 			| CREATE COLLATION IF_P NOT EXISTS any_name FROM any_name
5880 				{
5881 					DefineStmt *n = makeNode(DefineStmt);
5882 					n->kind = OBJECT_COLLATION;
5883 					n->args = NIL;
5884 					n->defnames = $6;
5885 					n->definition = list_make1(makeDefElem("from", (Node *) $8, @8));
5886 					n->if_not_exists = true;
5887 					$$ = (Node *)n;
5888 				}
5889 		;
5890 
5891 definition: '(' def_list ')'						{ $$ = $2; }
5892 		;
5893 
5894 def_list:	def_elem								{ $$ = list_make1($1); }
5895 			| def_list ',' def_elem					{ $$ = lappend($1, $3); }
5896 		;
5897 
5898 def_elem:	ColLabel '=' def_arg
5899 				{
5900 					$$ = makeDefElem($1, (Node *) $3, @1);
5901 				}
5902 			| ColLabel
5903 				{
5904 					$$ = makeDefElem($1, NULL, @1);
5905 				}
5906 		;
5907 
5908 /* Note: any simple identifier will be returned as a type name! */
5909 def_arg:	func_type						{ $$ = (Node *)$1; }
5910 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
5911 			| qual_all_Op					{ $$ = (Node *)$1; }
5912 			| NumericOnly					{ $$ = (Node *)$1; }
5913 			| Sconst						{ $$ = (Node *)makeString($1); }
5914 			| NONE							{ $$ = (Node *)makeString(pstrdup($1)); }
5915 		;
5916 
5917 old_aggr_definition: '(' old_aggr_list ')'			{ $$ = $2; }
5918 		;
5919 
5920 old_aggr_list: old_aggr_elem						{ $$ = list_make1($1); }
5921 			| old_aggr_list ',' old_aggr_elem		{ $$ = lappend($1, $3); }
5922 		;
5923 
5924 /*
5925  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
5926  * the item names needed in old aggregate definitions are likely to become
5927  * SQL keywords.
5928  */
5929 old_aggr_elem:  IDENT '=' def_arg
5930 				{
5931 					$$ = makeDefElem($1, (Node *)$3, @1);
5932 				}
5933 		;
5934 
5935 opt_enum_val_list:
5936 		enum_val_list							{ $$ = $1; }
5937 		| /*EMPTY*/								{ $$ = NIL; }
5938 		;
5939 
5940 enum_val_list:	Sconst
5941 				{ $$ = list_make1(makeString($1)); }
5942 			| enum_val_list ',' Sconst
5943 				{ $$ = lappend($1, makeString($3)); }
5944 		;
5945 
5946 /*****************************************************************************
5947  *
5948  *	ALTER TYPE enumtype ADD ...
5949  *
5950  *****************************************************************************/
5951 
5952 AlterEnumStmt:
5953 		ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst
5954 			{
5955 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5956 				n->typeName = $3;
5957 				n->oldVal = NULL;
5958 				n->newVal = $7;
5959 				n->newValNeighbor = NULL;
5960 				n->newValIsAfter = true;
5961 				n->skipIfNewValExists = $6;
5962 				$$ = (Node *) n;
5963 			}
5964 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst BEFORE Sconst
5965 			{
5966 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5967 				n->typeName = $3;
5968 				n->oldVal = NULL;
5969 				n->newVal = $7;
5970 				n->newValNeighbor = $9;
5971 				n->newValIsAfter = false;
5972 				n->skipIfNewValExists = $6;
5973 				$$ = (Node *) n;
5974 			}
5975 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst AFTER Sconst
5976 			{
5977 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5978 				n->typeName = $3;
5979 				n->oldVal = NULL;
5980 				n->newVal = $7;
5981 				n->newValNeighbor = $9;
5982 				n->newValIsAfter = true;
5983 				n->skipIfNewValExists = $6;
5984 				$$ = (Node *) n;
5985 			}
5986 		 | ALTER TYPE_P any_name RENAME VALUE_P Sconst TO Sconst
5987 			{
5988 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5989 				n->typeName = $3;
5990 				n->oldVal = $6;
5991 				n->newVal = $8;
5992 				n->newValNeighbor = NULL;
5993 				n->newValIsAfter = false;
5994 				n->skipIfNewValExists = false;
5995 				$$ = (Node *) n;
5996 			}
5997 		 ;
5998 
5999 opt_if_not_exists: IF_P NOT EXISTS              { $$ = true; }
6000 		| /* EMPTY */                          { $$ = false; }
6001 		;
6002 
6003 
6004 /*****************************************************************************
6005  *
6006  *		QUERIES :
6007  *				CREATE OPERATOR CLASS ...
6008  *				CREATE OPERATOR FAMILY ...
6009  *				ALTER OPERATOR FAMILY ...
6010  *				DROP OPERATOR CLASS ...
6011  *				DROP OPERATOR FAMILY ...
6012  *
6013  *****************************************************************************/
6014 
6015 CreateOpClassStmt:
6016 			CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
6017 			USING name opt_opfamily AS opclass_item_list
6018 				{
6019 					CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
6020 					n->opclassname = $4;
6021 					n->isDefault = $5;
6022 					n->datatype = $8;
6023 					n->amname = $10;
6024 					n->opfamilyname = $11;
6025 					n->items = $13;
6026 					$$ = (Node *) n;
6027 				}
6028 		;
6029 
6030 opclass_item_list:
6031 			opclass_item							{ $$ = list_make1($1); }
6032 			| opclass_item_list ',' opclass_item	{ $$ = lappend($1, $3); }
6033 		;
6034 
6035 opclass_item:
6036 			OPERATOR Iconst any_operator opclass_purpose opt_recheck
6037 				{
6038 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6039 					ObjectWithArgs *owa = makeNode(ObjectWithArgs);
6040 					owa->objname = $3;
6041 					owa->objargs = NIL;
6042 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6043 					n->name = owa;
6044 					n->number = $2;
6045 					n->order_family = $4;
6046 					$$ = (Node *) n;
6047 				}
6048 			| OPERATOR Iconst operator_with_argtypes opclass_purpose
6049 			  opt_recheck
6050 				{
6051 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6052 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6053 					n->name = $3;
6054 					n->number = $2;
6055 					n->order_family = $4;
6056 					$$ = (Node *) n;
6057 				}
6058 			| FUNCTION Iconst function_with_argtypes
6059 				{
6060 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6061 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6062 					n->name = $3;
6063 					n->number = $2;
6064 					$$ = (Node *) n;
6065 				}
6066 			| FUNCTION Iconst '(' type_list ')' function_with_argtypes
6067 				{
6068 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6069 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6070 					n->name = $6;
6071 					n->number = $2;
6072 					n->class_args = $4;
6073 					$$ = (Node *) n;
6074 				}
6075 			| STORAGE Typename
6076 				{
6077 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6078 					n->itemtype = OPCLASS_ITEM_STORAGETYPE;
6079 					n->storedtype = $2;
6080 					$$ = (Node *) n;
6081 				}
6082 		;
6083 
6084 opt_default:	DEFAULT						{ $$ = true; }
6085 			| /*EMPTY*/						{ $$ = false; }
6086 		;
6087 
6088 opt_opfamily:	FAMILY any_name				{ $$ = $2; }
6089 			| /*EMPTY*/						{ $$ = NIL; }
6090 		;
6091 
6092 opclass_purpose: FOR SEARCH					{ $$ = NIL; }
6093 			| FOR ORDER BY any_name			{ $$ = $4; }
6094 			| /*EMPTY*/						{ $$ = NIL; }
6095 		;
6096 
6097 opt_recheck:	RECHECK
6098 				{
6099 					/*
6100 					 * RECHECK no longer does anything in opclass definitions,
6101 					 * but we still accept it to ease porting of old database
6102 					 * dumps.
6103 					 */
6104 					ereport(NOTICE,
6105 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6106 							 errmsg("RECHECK is no longer required"),
6107 							 errhint("Update your data type."),
6108 							 parser_errposition(@1)));
6109 					$$ = true;
6110 				}
6111 			| /*EMPTY*/						{ $$ = false; }
6112 		;
6113 
6114 
6115 CreateOpFamilyStmt:
6116 			CREATE OPERATOR FAMILY any_name USING name
6117 				{
6118 					CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
6119 					n->opfamilyname = $4;
6120 					n->amname = $6;
6121 					$$ = (Node *) n;
6122 				}
6123 		;
6124 
6125 AlterOpFamilyStmt:
6126 			ALTER OPERATOR FAMILY any_name USING name ADD_P opclass_item_list
6127 				{
6128 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6129 					n->opfamilyname = $4;
6130 					n->amname = $6;
6131 					n->isDrop = false;
6132 					n->items = $8;
6133 					$$ = (Node *) n;
6134 				}
6135 			| ALTER OPERATOR FAMILY any_name USING name DROP opclass_drop_list
6136 				{
6137 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6138 					n->opfamilyname = $4;
6139 					n->amname = $6;
6140 					n->isDrop = true;
6141 					n->items = $8;
6142 					$$ = (Node *) n;
6143 				}
6144 		;
6145 
6146 opclass_drop_list:
6147 			opclass_drop							{ $$ = list_make1($1); }
6148 			| opclass_drop_list ',' opclass_drop	{ $$ = lappend($1, $3); }
6149 		;
6150 
6151 opclass_drop:
6152 			OPERATOR Iconst '(' type_list ')'
6153 				{
6154 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6155 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6156 					n->number = $2;
6157 					n->class_args = $4;
6158 					$$ = (Node *) n;
6159 				}
6160 			| FUNCTION Iconst '(' type_list ')'
6161 				{
6162 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6163 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6164 					n->number = $2;
6165 					n->class_args = $4;
6166 					$$ = (Node *) n;
6167 				}
6168 		;
6169 
6170 
6171 DropOpClassStmt:
6172 			DROP OPERATOR CLASS any_name USING name opt_drop_behavior
6173 				{
6174 					DropStmt *n = makeNode(DropStmt);
6175 					n->objects = list_make1(lcons(makeString($6), $4));
6176 					n->removeType = OBJECT_OPCLASS;
6177 					n->behavior = $7;
6178 					n->missing_ok = false;
6179 					n->concurrent = false;
6180 					$$ = (Node *) n;
6181 				}
6182 			| DROP OPERATOR CLASS IF_P EXISTS any_name USING name opt_drop_behavior
6183 				{
6184 					DropStmt *n = makeNode(DropStmt);
6185 					n->objects = list_make1(lcons(makeString($8), $6));
6186 					n->removeType = OBJECT_OPCLASS;
6187 					n->behavior = $9;
6188 					n->missing_ok = true;
6189 					n->concurrent = false;
6190 					$$ = (Node *) n;
6191 				}
6192 		;
6193 
6194 DropOpFamilyStmt:
6195 			DROP OPERATOR FAMILY any_name USING name opt_drop_behavior
6196 				{
6197 					DropStmt *n = makeNode(DropStmt);
6198 					n->objects = list_make1(lcons(makeString($6), $4));
6199 					n->removeType = OBJECT_OPFAMILY;
6200 					n->behavior = $7;
6201 					n->missing_ok = false;
6202 					n->concurrent = false;
6203 					$$ = (Node *) n;
6204 				}
6205 			| DROP OPERATOR FAMILY IF_P EXISTS any_name USING name opt_drop_behavior
6206 				{
6207 					DropStmt *n = makeNode(DropStmt);
6208 					n->objects = list_make1(lcons(makeString($8), $6));
6209 					n->removeType = OBJECT_OPFAMILY;
6210 					n->behavior = $9;
6211 					n->missing_ok = true;
6212 					n->concurrent = false;
6213 					$$ = (Node *) n;
6214 				}
6215 		;
6216 
6217 
6218 /*****************************************************************************
6219  *
6220  *		QUERY:
6221  *
6222  *		DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
6223  *		REASSIGN OWNED BY username [, username ...] TO username
6224  *
6225  *****************************************************************************/
6226 DropOwnedStmt:
6227 			DROP OWNED BY role_list opt_drop_behavior
6228 				{
6229 					DropOwnedStmt *n = makeNode(DropOwnedStmt);
6230 					n->roles = $4;
6231 					n->behavior = $5;
6232 					$$ = (Node *)n;
6233 				}
6234 		;
6235 
6236 ReassignOwnedStmt:
6237 			REASSIGN OWNED BY role_list TO RoleSpec
6238 				{
6239 					ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
6240 					n->roles = $4;
6241 					n->newrole = $6;
6242 					$$ = (Node *)n;
6243 				}
6244 		;
6245 
6246 /*****************************************************************************
6247  *
6248  *		QUERY:
6249  *
6250  *		DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
6251  *           [ RESTRICT | CASCADE ]
6252  *
6253  *****************************************************************************/
6254 
6255 DropStmt:	DROP object_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
6256 				{
6257 					DropStmt *n = makeNode(DropStmt);
6258 					n->removeType = $2;
6259 					n->missing_ok = true;
6260 					n->objects = $5;
6261 					n->behavior = $6;
6262 					n->concurrent = false;
6263 					$$ = (Node *)n;
6264 				}
6265 			| DROP object_type_any_name any_name_list opt_drop_behavior
6266 				{
6267 					DropStmt *n = makeNode(DropStmt);
6268 					n->removeType = $2;
6269 					n->missing_ok = false;
6270 					n->objects = $3;
6271 					n->behavior = $4;
6272 					n->concurrent = false;
6273 					$$ = (Node *)n;
6274 				}
6275 			| DROP drop_type_name IF_P EXISTS name_list opt_drop_behavior
6276 				{
6277 					DropStmt *n = makeNode(DropStmt);
6278 					n->removeType = $2;
6279 					n->missing_ok = true;
6280 					n->objects = $5;
6281 					n->behavior = $6;
6282 					n->concurrent = false;
6283 					$$ = (Node *)n;
6284 				}
6285 			| DROP drop_type_name name_list opt_drop_behavior
6286 				{
6287 					DropStmt *n = makeNode(DropStmt);
6288 					n->removeType = $2;
6289 					n->missing_ok = false;
6290 					n->objects = $3;
6291 					n->behavior = $4;
6292 					n->concurrent = false;
6293 					$$ = (Node *)n;
6294 				}
6295 			| DROP object_type_name_on_any_name name ON any_name opt_drop_behavior
6296 				{
6297 					DropStmt *n = makeNode(DropStmt);
6298 					n->removeType = $2;
6299 					n->objects = list_make1(lappend($5, makeString($3)));
6300 					n->behavior = $6;
6301 					n->missing_ok = false;
6302 					n->concurrent = false;
6303 					$$ = (Node *) n;
6304 				}
6305 			| DROP object_type_name_on_any_name IF_P EXISTS name ON any_name opt_drop_behavior
6306 				{
6307 					DropStmt *n = makeNode(DropStmt);
6308 					n->removeType = $2;
6309 					n->objects = list_make1(lappend($7, makeString($5)));
6310 					n->behavior = $8;
6311 					n->missing_ok = true;
6312 					n->concurrent = false;
6313 					$$ = (Node *) n;
6314 				}
6315 			| DROP TYPE_P type_name_list opt_drop_behavior
6316 				{
6317 					DropStmt *n = makeNode(DropStmt);
6318 					n->removeType = OBJECT_TYPE;
6319 					n->missing_ok = false;
6320 					n->objects = $3;
6321 					n->behavior = $4;
6322 					n->concurrent = false;
6323 					$$ = (Node *) n;
6324 				}
6325 			| DROP TYPE_P IF_P EXISTS type_name_list opt_drop_behavior
6326 				{
6327 					DropStmt *n = makeNode(DropStmt);
6328 					n->removeType = OBJECT_TYPE;
6329 					n->missing_ok = true;
6330 					n->objects = $5;
6331 					n->behavior = $6;
6332 					n->concurrent = false;
6333 					$$ = (Node *) n;
6334 				}
6335 			| DROP DOMAIN_P type_name_list opt_drop_behavior
6336 				{
6337 					DropStmt *n = makeNode(DropStmt);
6338 					n->removeType = OBJECT_DOMAIN;
6339 					n->missing_ok = false;
6340 					n->objects = $3;
6341 					n->behavior = $4;
6342 					n->concurrent = false;
6343 					$$ = (Node *) n;
6344 				}
6345 			| DROP DOMAIN_P IF_P EXISTS type_name_list opt_drop_behavior
6346 				{
6347 					DropStmt *n = makeNode(DropStmt);
6348 					n->removeType = OBJECT_DOMAIN;
6349 					n->missing_ok = true;
6350 					n->objects = $5;
6351 					n->behavior = $6;
6352 					n->concurrent = false;
6353 					$$ = (Node *) n;
6354 				}
6355 			| DROP INDEX CONCURRENTLY any_name_list opt_drop_behavior
6356 				{
6357 					DropStmt *n = makeNode(DropStmt);
6358 					n->removeType = OBJECT_INDEX;
6359 					n->missing_ok = false;
6360 					n->objects = $4;
6361 					n->behavior = $5;
6362 					n->concurrent = true;
6363 					$$ = (Node *)n;
6364 				}
6365 			| DROP INDEX CONCURRENTLY IF_P EXISTS any_name_list opt_drop_behavior
6366 				{
6367 					DropStmt *n = makeNode(DropStmt);
6368 					n->removeType = OBJECT_INDEX;
6369 					n->missing_ok = true;
6370 					n->objects = $6;
6371 					n->behavior = $7;
6372 					n->concurrent = true;
6373 					$$ = (Node *)n;
6374 				}
6375 		;
6376 
6377 /* object types taking any_name/any_name_list */
6378 object_type_any_name:
6379 			TABLE									{ $$ = OBJECT_TABLE; }
6380 			| SEQUENCE								{ $$ = OBJECT_SEQUENCE; }
6381 			| VIEW									{ $$ = OBJECT_VIEW; }
6382 			| MATERIALIZED VIEW						{ $$ = OBJECT_MATVIEW; }
6383 			| INDEX									{ $$ = OBJECT_INDEX; }
6384 			| FOREIGN TABLE							{ $$ = OBJECT_FOREIGN_TABLE; }
6385 			| COLLATION								{ $$ = OBJECT_COLLATION; }
6386 			| CONVERSION_P							{ $$ = OBJECT_CONVERSION; }
6387 			| STATISTICS							{ $$ = OBJECT_STATISTIC_EXT; }
6388 			| TEXT_P SEARCH PARSER					{ $$ = OBJECT_TSPARSER; }
6389 			| TEXT_P SEARCH DICTIONARY				{ $$ = OBJECT_TSDICTIONARY; }
6390 			| TEXT_P SEARCH TEMPLATE				{ $$ = OBJECT_TSTEMPLATE; }
6391 			| TEXT_P SEARCH CONFIGURATION			{ $$ = OBJECT_TSCONFIGURATION; }
6392 		;
6393 
6394 /*
6395  * object types taking name/name_list
6396  *
6397  * DROP handles some of them separately
6398  */
6399 
6400 object_type_name:
6401 			drop_type_name							{ $$ = $1; }
6402 			| DATABASE								{ $$ = OBJECT_DATABASE; }
6403 			| ROLE									{ $$ = OBJECT_ROLE; }
6404 			| SUBSCRIPTION							{ $$ = OBJECT_SUBSCRIPTION; }
6405 			| TABLESPACE							{ $$ = OBJECT_TABLESPACE; }
6406 		;
6407 
6408 drop_type_name:
6409 			ACCESS METHOD							{ $$ = OBJECT_ACCESS_METHOD; }
6410 			| EVENT TRIGGER							{ $$ = OBJECT_EVENT_TRIGGER; }
6411 			| EXTENSION								{ $$ = OBJECT_EXTENSION; }
6412 			| FOREIGN DATA_P WRAPPER				{ $$ = OBJECT_FDW; }
6413 			| opt_procedural LANGUAGE				{ $$ = OBJECT_LANGUAGE; }
6414 			| PUBLICATION							{ $$ = OBJECT_PUBLICATION; }
6415 			| SCHEMA								{ $$ = OBJECT_SCHEMA; }
6416 			| SERVER								{ $$ = OBJECT_FOREIGN_SERVER; }
6417 		;
6418 
6419 /* object types attached to a table */
6420 object_type_name_on_any_name:
6421 			POLICY									{ $$ = OBJECT_POLICY; }
6422 			| RULE									{ $$ = OBJECT_RULE; }
6423 			| TRIGGER								{ $$ = OBJECT_TRIGGER; }
6424 		;
6425 
6426 any_name_list:
6427 			any_name								{ $$ = list_make1($1); }
6428 			| any_name_list ',' any_name			{ $$ = lappend($1, $3); }
6429 		;
6430 
6431 any_name:	ColId						{ $$ = list_make1(makeString($1)); }
6432 			| ColId attrs				{ $$ = lcons(makeString($1), $2); }
6433 		;
6434 
6435 attrs:		'.' attr_name
6436 					{ $$ = list_make1(makeString($2)); }
6437 			| attrs '.' attr_name
6438 					{ $$ = lappend($1, makeString($3)); }
6439 		;
6440 
6441 type_name_list:
6442 			Typename								{ $$ = list_make1($1); }
6443 			| type_name_list ',' Typename			{ $$ = lappend($1, $3); }
6444 		;
6445 
6446 /*****************************************************************************
6447  *
6448  *		QUERY:
6449  *				truncate table relname1, relname2, ...
6450  *
6451  *****************************************************************************/
6452 
6453 TruncateStmt:
6454 			TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
6455 				{
6456 					TruncateStmt *n = makeNode(TruncateStmt);
6457 					n->relations = $3;
6458 					n->restart_seqs = $4;
6459 					n->behavior = $5;
6460 					$$ = (Node *)n;
6461 				}
6462 		;
6463 
6464 opt_restart_seqs:
6465 			CONTINUE_P IDENTITY_P		{ $$ = false; }
6466 			| RESTART IDENTITY_P		{ $$ = true; }
6467 			| /* EMPTY */				{ $$ = false; }
6468 		;
6469 
6470 /*****************************************************************************
6471  *
6472  * COMMENT ON <object> IS <text>
6473  *
6474  *****************************************************************************/
6475 
6476 CommentStmt:
6477 			COMMENT ON object_type_any_name any_name IS comment_text
6478 				{
6479 					CommentStmt *n = makeNode(CommentStmt);
6480 					n->objtype = $3;
6481 					n->object = (Node *) $4;
6482 					n->comment = $6;
6483 					$$ = (Node *) n;
6484 				}
6485 			| COMMENT ON COLUMN any_name IS comment_text
6486 				{
6487 					CommentStmt *n = makeNode(CommentStmt);
6488 					n->objtype = OBJECT_COLUMN;
6489 					n->object = (Node *) $4;
6490 					n->comment = $6;
6491 					$$ = (Node *) n;
6492 				}
6493 			| COMMENT ON object_type_name name IS comment_text
6494 				{
6495 					CommentStmt *n = makeNode(CommentStmt);
6496 					n->objtype = $3;
6497 					n->object = (Node *) makeString($4);
6498 					n->comment = $6;
6499 					$$ = (Node *) n;
6500 				}
6501 			| COMMENT ON TYPE_P Typename IS comment_text
6502 				{
6503 					CommentStmt *n = makeNode(CommentStmt);
6504 					n->objtype = OBJECT_TYPE;
6505 					n->object = (Node *) $4;
6506 					n->comment = $6;
6507 					$$ = (Node *) n;
6508 				}
6509 			| COMMENT ON DOMAIN_P Typename IS comment_text
6510 				{
6511 					CommentStmt *n = makeNode(CommentStmt);
6512 					n->objtype = OBJECT_DOMAIN;
6513 					n->object = (Node *) $4;
6514 					n->comment = $6;
6515 					$$ = (Node *) n;
6516 				}
6517 			| COMMENT ON AGGREGATE aggregate_with_argtypes IS comment_text
6518 				{
6519 					CommentStmt *n = makeNode(CommentStmt);
6520 					n->objtype = OBJECT_AGGREGATE;
6521 					n->object = (Node *) $4;
6522 					n->comment = $6;
6523 					$$ = (Node *) n;
6524 				}
6525 			| COMMENT ON FUNCTION function_with_argtypes IS comment_text
6526 				{
6527 					CommentStmt *n = makeNode(CommentStmt);
6528 					n->objtype = OBJECT_FUNCTION;
6529 					n->object = (Node *) $4;
6530 					n->comment = $6;
6531 					$$ = (Node *) n;
6532 				}
6533 			| COMMENT ON OPERATOR operator_with_argtypes IS comment_text
6534 				{
6535 					CommentStmt *n = makeNode(CommentStmt);
6536 					n->objtype = OBJECT_OPERATOR;
6537 					n->object = (Node *) $4;
6538 					n->comment = $6;
6539 					$$ = (Node *) n;
6540 				}
6541 			| COMMENT ON CONSTRAINT name ON any_name IS comment_text
6542 				{
6543 					CommentStmt *n = makeNode(CommentStmt);
6544 					n->objtype = OBJECT_TABCONSTRAINT;
6545 					n->object = (Node *) lappend($6, makeString($4));
6546 					n->comment = $8;
6547 					$$ = (Node *) n;
6548 				}
6549 			| COMMENT ON CONSTRAINT name ON DOMAIN_P any_name IS comment_text
6550 				{
6551 					CommentStmt *n = makeNode(CommentStmt);
6552 					n->objtype = OBJECT_DOMCONSTRAINT;
6553 					/*
6554 					 * should use Typename not any_name in the production, but
6555 					 * there's a shift/reduce conflict if we do that, so fix it
6556 					 * up here.
6557 					 */
6558 					n->object = (Node *) list_make2(makeTypeNameFromNameList($7), makeString($4));
6559 					n->comment = $9;
6560 					$$ = (Node *) n;
6561 				}
6562 			| COMMENT ON object_type_name_on_any_name name ON any_name IS comment_text
6563 				{
6564 					CommentStmt *n = makeNode(CommentStmt);
6565 					n->objtype = $3;
6566 					n->object = (Node *) lappend($6, makeString($4));
6567 					n->comment = $8;
6568 					$$ = (Node *) n;
6569 				}
6570 			| COMMENT ON PROCEDURE function_with_argtypes IS comment_text
6571 				{
6572 					CommentStmt *n = makeNode(CommentStmt);
6573 					n->objtype = OBJECT_PROCEDURE;
6574 					n->object = (Node *) $4;
6575 					n->comment = $6;
6576 					$$ = (Node *) n;
6577 				}
6578 			| COMMENT ON ROUTINE function_with_argtypes IS comment_text
6579 				{
6580 					CommentStmt *n = makeNode(CommentStmt);
6581 					n->objtype = OBJECT_ROUTINE;
6582 					n->object = (Node *) $4;
6583 					n->comment = $6;
6584 					$$ = (Node *) n;
6585 				}
6586 			| COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
6587 				{
6588 					CommentStmt *n = makeNode(CommentStmt);
6589 					n->objtype = OBJECT_TRANSFORM;
6590 					n->object = (Node *) list_make2($5, makeString($7));
6591 					n->comment = $9;
6592 					$$ = (Node *) n;
6593 				}
6594 			| COMMENT ON OPERATOR CLASS any_name USING name IS comment_text
6595 				{
6596 					CommentStmt *n = makeNode(CommentStmt);
6597 					n->objtype = OBJECT_OPCLASS;
6598 					n->object = (Node *) lcons(makeString($7), $5);
6599 					n->comment = $9;
6600 					$$ = (Node *) n;
6601 				}
6602 			| COMMENT ON OPERATOR FAMILY any_name USING name IS comment_text
6603 				{
6604 					CommentStmt *n = makeNode(CommentStmt);
6605 					n->objtype = OBJECT_OPFAMILY;
6606 					n->object = (Node *) lcons(makeString($7), $5);
6607 					n->comment = $9;
6608 					$$ = (Node *) n;
6609 				}
6610 			| COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
6611 				{
6612 					CommentStmt *n = makeNode(CommentStmt);
6613 					n->objtype = OBJECT_LARGEOBJECT;
6614 					n->object = (Node *) $5;
6615 					n->comment = $7;
6616 					$$ = (Node *) n;
6617 				}
6618 			| COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
6619 				{
6620 					CommentStmt *n = makeNode(CommentStmt);
6621 					n->objtype = OBJECT_CAST;
6622 					n->object = (Node *) list_make2($5, $7);
6623 					n->comment = $10;
6624 					$$ = (Node *) n;
6625 				}
6626 		;
6627 
6628 comment_text:
6629 			Sconst								{ $$ = $1; }
6630 			| NULL_P							{ $$ = NULL; }
6631 		;
6632 
6633 
6634 /*****************************************************************************
6635  *
6636  *  SECURITY LABEL [FOR <provider>] ON <object> IS <label>
6637  *
6638  *  As with COMMENT ON, <object> can refer to various types of database
6639  *  objects (e.g. TABLE, COLUMN, etc.).
6640  *
6641  *****************************************************************************/
6642 
6643 SecLabelStmt:
6644 			SECURITY LABEL opt_provider ON object_type_any_name any_name
6645 			IS security_label
6646 				{
6647 					SecLabelStmt *n = makeNode(SecLabelStmt);
6648 					n->provider = $3;
6649 					n->objtype = $5;
6650 					n->object = (Node *) $6;
6651 					n->label = $8;
6652 					$$ = (Node *) n;
6653 				}
6654 			| SECURITY LABEL opt_provider ON COLUMN any_name
6655 			  IS security_label
6656 				{
6657 					SecLabelStmt *n = makeNode(SecLabelStmt);
6658 					n->provider = $3;
6659 					n->objtype = OBJECT_COLUMN;
6660 					n->object = (Node *) $6;
6661 					n->label = $8;
6662 					$$ = (Node *) n;
6663 				}
6664 			| SECURITY LABEL opt_provider ON object_type_name name
6665 			  IS security_label
6666 				{
6667 					SecLabelStmt *n = makeNode(SecLabelStmt);
6668 					n->provider = $3;
6669 					n->objtype = $5;
6670 					n->object = (Node *) makeString($6);
6671 					n->label = $8;
6672 					$$ = (Node *) n;
6673 				}
6674 			| SECURITY LABEL opt_provider ON TYPE_P Typename
6675 			  IS security_label
6676 				{
6677 					SecLabelStmt *n = makeNode(SecLabelStmt);
6678 					n->provider = $3;
6679 					n->objtype = OBJECT_TYPE;
6680 					n->object = (Node *) $6;
6681 					n->label = $8;
6682 					$$ = (Node *) n;
6683 				}
6684 			| SECURITY LABEL opt_provider ON DOMAIN_P Typename
6685 			  IS security_label
6686 				{
6687 					SecLabelStmt *n = makeNode(SecLabelStmt);
6688 					n->provider = $3;
6689 					n->objtype = OBJECT_DOMAIN;
6690 					n->object = (Node *) $6;
6691 					n->label = $8;
6692 					$$ = (Node *) n;
6693 				}
6694 			| SECURITY LABEL opt_provider ON AGGREGATE aggregate_with_argtypes
6695 			  IS security_label
6696 				{
6697 					SecLabelStmt *n = makeNode(SecLabelStmt);
6698 					n->provider = $3;
6699 					n->objtype = OBJECT_AGGREGATE;
6700 					n->object = (Node *) $6;
6701 					n->label = $8;
6702 					$$ = (Node *) n;
6703 				}
6704 			| SECURITY LABEL opt_provider ON FUNCTION function_with_argtypes
6705 			  IS security_label
6706 				{
6707 					SecLabelStmt *n = makeNode(SecLabelStmt);
6708 					n->provider = $3;
6709 					n->objtype = OBJECT_FUNCTION;
6710 					n->object = (Node *) $6;
6711 					n->label = $8;
6712 					$$ = (Node *) n;
6713 				}
6714 			| SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
6715 			  IS security_label
6716 				{
6717 					SecLabelStmt *n = makeNode(SecLabelStmt);
6718 					n->provider = $3;
6719 					n->objtype = OBJECT_LARGEOBJECT;
6720 					n->object = (Node *) $7;
6721 					n->label = $9;
6722 					$$ = (Node *) n;
6723 				}
6724 			| SECURITY LABEL opt_provider ON PROCEDURE function_with_argtypes
6725 			  IS security_label
6726 				{
6727 					SecLabelStmt *n = makeNode(SecLabelStmt);
6728 					n->provider = $3;
6729 					n->objtype = OBJECT_PROCEDURE;
6730 					n->object = (Node *) $6;
6731 					n->label = $8;
6732 					$$ = (Node *) n;
6733 				}
6734 			| SECURITY LABEL opt_provider ON ROUTINE function_with_argtypes
6735 			  IS security_label
6736 				{
6737 					SecLabelStmt *n = makeNode(SecLabelStmt);
6738 					n->provider = $3;
6739 					n->objtype = OBJECT_ROUTINE;
6740 					n->object = (Node *) $6;
6741 					n->label = $8;
6742 					$$ = (Node *) n;
6743 				}
6744 		;
6745 
6746 opt_provider:	FOR NonReservedWord_or_Sconst	{ $$ = $2; }
6747 				| /* EMPTY */					{ $$ = NULL; }
6748 		;
6749 
6750 security_label:	Sconst				{ $$ = $1; }
6751 				| NULL_P			{ $$ = NULL; }
6752 		;
6753 
6754 /*****************************************************************************
6755  *
6756  *		QUERY:
6757  *			fetch/move
6758  *
6759  *****************************************************************************/
6760 
6761 FetchStmt:	FETCH fetch_args
6762 				{
6763 					FetchStmt *n = (FetchStmt *) $2;
6764 					n->ismove = false;
6765 					$$ = (Node *)n;
6766 				}
6767 			| MOVE fetch_args
6768 				{
6769 					FetchStmt *n = (FetchStmt *) $2;
6770 					n->ismove = true;
6771 					$$ = (Node *)n;
6772 				}
6773 		;
6774 
6775 fetch_args:	cursor_name
6776 				{
6777 					FetchStmt *n = makeNode(FetchStmt);
6778 					n->portalname = $1;
6779 					n->direction = FETCH_FORWARD;
6780 					n->howMany = 1;
6781 					$$ = (Node *)n;
6782 				}
6783 			| from_in cursor_name
6784 				{
6785 					FetchStmt *n = makeNode(FetchStmt);
6786 					n->portalname = $2;
6787 					n->direction = FETCH_FORWARD;
6788 					n->howMany = 1;
6789 					$$ = (Node *)n;
6790 				}
6791 			| NEXT opt_from_in cursor_name
6792 				{
6793 					FetchStmt *n = makeNode(FetchStmt);
6794 					n->portalname = $3;
6795 					n->direction = FETCH_FORWARD;
6796 					n->howMany = 1;
6797 					$$ = (Node *)n;
6798 				}
6799 			| PRIOR opt_from_in cursor_name
6800 				{
6801 					FetchStmt *n = makeNode(FetchStmt);
6802 					n->portalname = $3;
6803 					n->direction = FETCH_BACKWARD;
6804 					n->howMany = 1;
6805 					$$ = (Node *)n;
6806 				}
6807 			| FIRST_P opt_from_in cursor_name
6808 				{
6809 					FetchStmt *n = makeNode(FetchStmt);
6810 					n->portalname = $3;
6811 					n->direction = FETCH_ABSOLUTE;
6812 					n->howMany = 1;
6813 					$$ = (Node *)n;
6814 				}
6815 			| LAST_P opt_from_in cursor_name
6816 				{
6817 					FetchStmt *n = makeNode(FetchStmt);
6818 					n->portalname = $3;
6819 					n->direction = FETCH_ABSOLUTE;
6820 					n->howMany = -1;
6821 					$$ = (Node *)n;
6822 				}
6823 			| ABSOLUTE_P SignedIconst opt_from_in cursor_name
6824 				{
6825 					FetchStmt *n = makeNode(FetchStmt);
6826 					n->portalname = $4;
6827 					n->direction = FETCH_ABSOLUTE;
6828 					n->howMany = $2;
6829 					$$ = (Node *)n;
6830 				}
6831 			| RELATIVE_P SignedIconst opt_from_in cursor_name
6832 				{
6833 					FetchStmt *n = makeNode(FetchStmt);
6834 					n->portalname = $4;
6835 					n->direction = FETCH_RELATIVE;
6836 					n->howMany = $2;
6837 					$$ = (Node *)n;
6838 				}
6839 			| SignedIconst opt_from_in cursor_name
6840 				{
6841 					FetchStmt *n = makeNode(FetchStmt);
6842 					n->portalname = $3;
6843 					n->direction = FETCH_FORWARD;
6844 					n->howMany = $1;
6845 					$$ = (Node *)n;
6846 				}
6847 			| ALL opt_from_in cursor_name
6848 				{
6849 					FetchStmt *n = makeNode(FetchStmt);
6850 					n->portalname = $3;
6851 					n->direction = FETCH_FORWARD;
6852 					n->howMany = FETCH_ALL;
6853 					$$ = (Node *)n;
6854 				}
6855 			| FORWARD opt_from_in cursor_name
6856 				{
6857 					FetchStmt *n = makeNode(FetchStmt);
6858 					n->portalname = $3;
6859 					n->direction = FETCH_FORWARD;
6860 					n->howMany = 1;
6861 					$$ = (Node *)n;
6862 				}
6863 			| FORWARD SignedIconst opt_from_in cursor_name
6864 				{
6865 					FetchStmt *n = makeNode(FetchStmt);
6866 					n->portalname = $4;
6867 					n->direction = FETCH_FORWARD;
6868 					n->howMany = $2;
6869 					$$ = (Node *)n;
6870 				}
6871 			| FORWARD ALL opt_from_in cursor_name
6872 				{
6873 					FetchStmt *n = makeNode(FetchStmt);
6874 					n->portalname = $4;
6875 					n->direction = FETCH_FORWARD;
6876 					n->howMany = FETCH_ALL;
6877 					$$ = (Node *)n;
6878 				}
6879 			| BACKWARD opt_from_in cursor_name
6880 				{
6881 					FetchStmt *n = makeNode(FetchStmt);
6882 					n->portalname = $3;
6883 					n->direction = FETCH_BACKWARD;
6884 					n->howMany = 1;
6885 					$$ = (Node *)n;
6886 				}
6887 			| BACKWARD SignedIconst opt_from_in cursor_name
6888 				{
6889 					FetchStmt *n = makeNode(FetchStmt);
6890 					n->portalname = $4;
6891 					n->direction = FETCH_BACKWARD;
6892 					n->howMany = $2;
6893 					$$ = (Node *)n;
6894 				}
6895 			| BACKWARD ALL opt_from_in cursor_name
6896 				{
6897 					FetchStmt *n = makeNode(FetchStmt);
6898 					n->portalname = $4;
6899 					n->direction = FETCH_BACKWARD;
6900 					n->howMany = FETCH_ALL;
6901 					$$ = (Node *)n;
6902 				}
6903 		;
6904 
6905 from_in:	FROM
6906 			| IN_P
6907 		;
6908 
6909 opt_from_in:	from_in
6910 			| /* EMPTY */
6911 		;
6912 
6913 
6914 /*****************************************************************************
6915  *
6916  * GRANT and REVOKE statements
6917  *
6918  *****************************************************************************/
6919 
6920 GrantStmt:	GRANT privileges ON privilege_target TO grantee_list
6921 			opt_grant_grant_option opt_granted_by
6922 				{
6923 					GrantStmt *n = makeNode(GrantStmt);
6924 					n->is_grant = true;
6925 					n->privileges = $2;
6926 					n->targtype = ($4)->targtype;
6927 					n->objtype = ($4)->objtype;
6928 					n->objects = ($4)->objs;
6929 					n->grantees = $6;
6930 					n->grant_option = $7;
6931 					n->grantor = $8;
6932 					$$ = (Node*)n;
6933 				}
6934 		;
6935 
6936 RevokeStmt:
6937 			REVOKE privileges ON privilege_target
6938 			FROM grantee_list opt_granted_by opt_drop_behavior
6939 				{
6940 					GrantStmt *n = makeNode(GrantStmt);
6941 					n->is_grant = false;
6942 					n->grant_option = false;
6943 					n->privileges = $2;
6944 					n->targtype = ($4)->targtype;
6945 					n->objtype = ($4)->objtype;
6946 					n->objects = ($4)->objs;
6947 					n->grantees = $6;
6948 					n->grantor = $7;
6949 					n->behavior = $8;
6950 					$$ = (Node *)n;
6951 				}
6952 			| REVOKE GRANT OPTION FOR privileges ON privilege_target
6953 			FROM grantee_list opt_granted_by opt_drop_behavior
6954 				{
6955 					GrantStmt *n = makeNode(GrantStmt);
6956 					n->is_grant = false;
6957 					n->grant_option = true;
6958 					n->privileges = $5;
6959 					n->targtype = ($7)->targtype;
6960 					n->objtype = ($7)->objtype;
6961 					n->objects = ($7)->objs;
6962 					n->grantees = $9;
6963 					n->grantor = $10;
6964 					n->behavior = $11;
6965 					$$ = (Node *)n;
6966 				}
6967 		;
6968 
6969 
6970 /*
6971  * Privilege names are represented as strings; the validity of the privilege
6972  * names gets checked at execution.  This is a bit annoying but we have little
6973  * choice because of the syntactic conflict with lists of role names in
6974  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
6975  * production any reserved keywords that need to be usable as privilege names.
6976  */
6977 
6978 /* either ALL [PRIVILEGES] or a list of individual privileges */
6979 privileges: privilege_list
6980 				{ $$ = $1; }
6981 			| ALL
6982 				{ $$ = NIL; }
6983 			| ALL PRIVILEGES
6984 				{ $$ = NIL; }
6985 			| ALL '(' columnList ')'
6986 				{
6987 					AccessPriv *n = makeNode(AccessPriv);
6988 					n->priv_name = NULL;
6989 					n->cols = $3;
6990 					$$ = list_make1(n);
6991 				}
6992 			| ALL PRIVILEGES '(' columnList ')'
6993 				{
6994 					AccessPriv *n = makeNode(AccessPriv);
6995 					n->priv_name = NULL;
6996 					n->cols = $4;
6997 					$$ = list_make1(n);
6998 				}
6999 		;
7000 
7001 privilege_list:	privilege							{ $$ = list_make1($1); }
7002 			| privilege_list ',' privilege			{ $$ = lappend($1, $3); }
7003 		;
7004 
7005 privilege:	SELECT opt_column_list
7006 			{
7007 				AccessPriv *n = makeNode(AccessPriv);
7008 				n->priv_name = pstrdup($1);
7009 				n->cols = $2;
7010 				$$ = n;
7011 			}
7012 		| REFERENCES opt_column_list
7013 			{
7014 				AccessPriv *n = makeNode(AccessPriv);
7015 				n->priv_name = pstrdup($1);
7016 				n->cols = $2;
7017 				$$ = n;
7018 			}
7019 		| CREATE opt_column_list
7020 			{
7021 				AccessPriv *n = makeNode(AccessPriv);
7022 				n->priv_name = pstrdup($1);
7023 				n->cols = $2;
7024 				$$ = n;
7025 			}
7026 		| ColId opt_column_list
7027 			{
7028 				AccessPriv *n = makeNode(AccessPriv);
7029 				n->priv_name = $1;
7030 				n->cols = $2;
7031 				$$ = n;
7032 			}
7033 		;
7034 
7035 
7036 /* Don't bother trying to fold the first two rules into one using
7037  * opt_table.  You're going to get conflicts.
7038  */
7039 privilege_target:
7040 			qualified_name_list
7041 				{
7042 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7043 					n->targtype = ACL_TARGET_OBJECT;
7044 					n->objtype = OBJECT_TABLE;
7045 					n->objs = $1;
7046 					$$ = n;
7047 				}
7048 			| TABLE qualified_name_list
7049 				{
7050 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7051 					n->targtype = ACL_TARGET_OBJECT;
7052 					n->objtype = OBJECT_TABLE;
7053 					n->objs = $2;
7054 					$$ = n;
7055 				}
7056 			| SEQUENCE qualified_name_list
7057 				{
7058 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7059 					n->targtype = ACL_TARGET_OBJECT;
7060 					n->objtype = OBJECT_SEQUENCE;
7061 					n->objs = $2;
7062 					$$ = n;
7063 				}
7064 			| FOREIGN DATA_P WRAPPER name_list
7065 				{
7066 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7067 					n->targtype = ACL_TARGET_OBJECT;
7068 					n->objtype = OBJECT_FDW;
7069 					n->objs = $4;
7070 					$$ = n;
7071 				}
7072 			| FOREIGN SERVER name_list
7073 				{
7074 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7075 					n->targtype = ACL_TARGET_OBJECT;
7076 					n->objtype = OBJECT_FOREIGN_SERVER;
7077 					n->objs = $3;
7078 					$$ = n;
7079 				}
7080 			| FUNCTION function_with_argtypes_list
7081 				{
7082 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7083 					n->targtype = ACL_TARGET_OBJECT;
7084 					n->objtype = OBJECT_FUNCTION;
7085 					n->objs = $2;
7086 					$$ = n;
7087 				}
7088 			| PROCEDURE function_with_argtypes_list
7089 				{
7090 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7091 					n->targtype = ACL_TARGET_OBJECT;
7092 					n->objtype = OBJECT_PROCEDURE;
7093 					n->objs = $2;
7094 					$$ = n;
7095 				}
7096 			| ROUTINE function_with_argtypes_list
7097 				{
7098 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7099 					n->targtype = ACL_TARGET_OBJECT;
7100 					n->objtype = OBJECT_ROUTINE;
7101 					n->objs = $2;
7102 					$$ = n;
7103 				}
7104 			| DATABASE name_list
7105 				{
7106 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7107 					n->targtype = ACL_TARGET_OBJECT;
7108 					n->objtype = OBJECT_DATABASE;
7109 					n->objs = $2;
7110 					$$ = n;
7111 				}
7112 			| DOMAIN_P any_name_list
7113 				{
7114 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7115 					n->targtype = ACL_TARGET_OBJECT;
7116 					n->objtype = OBJECT_DOMAIN;
7117 					n->objs = $2;
7118 					$$ = n;
7119 				}
7120 			| LANGUAGE name_list
7121 				{
7122 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7123 					n->targtype = ACL_TARGET_OBJECT;
7124 					n->objtype = OBJECT_LANGUAGE;
7125 					n->objs = $2;
7126 					$$ = n;
7127 				}
7128 			| LARGE_P OBJECT_P NumericOnly_list
7129 				{
7130 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7131 					n->targtype = ACL_TARGET_OBJECT;
7132 					n->objtype = OBJECT_LARGEOBJECT;
7133 					n->objs = $3;
7134 					$$ = n;
7135 				}
7136 			| SCHEMA name_list
7137 				{
7138 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7139 					n->targtype = ACL_TARGET_OBJECT;
7140 					n->objtype = OBJECT_SCHEMA;
7141 					n->objs = $2;
7142 					$$ = n;
7143 				}
7144 			| TABLESPACE name_list
7145 				{
7146 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7147 					n->targtype = ACL_TARGET_OBJECT;
7148 					n->objtype = OBJECT_TABLESPACE;
7149 					n->objs = $2;
7150 					$$ = n;
7151 				}
7152 			| TYPE_P any_name_list
7153 				{
7154 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7155 					n->targtype = ACL_TARGET_OBJECT;
7156 					n->objtype = OBJECT_TYPE;
7157 					n->objs = $2;
7158 					$$ = n;
7159 				}
7160 			| ALL TABLES IN_P SCHEMA name_list
7161 				{
7162 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7163 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7164 					n->objtype = OBJECT_TABLE;
7165 					n->objs = $5;
7166 					$$ = n;
7167 				}
7168 			| ALL SEQUENCES IN_P SCHEMA name_list
7169 				{
7170 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7171 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7172 					n->objtype = OBJECT_SEQUENCE;
7173 					n->objs = $5;
7174 					$$ = n;
7175 				}
7176 			| ALL FUNCTIONS IN_P SCHEMA name_list
7177 				{
7178 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7179 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7180 					n->objtype = OBJECT_FUNCTION;
7181 					n->objs = $5;
7182 					$$ = n;
7183 				}
7184 			| ALL PROCEDURES IN_P SCHEMA name_list
7185 				{
7186 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7187 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7188 					n->objtype = OBJECT_PROCEDURE;
7189 					n->objs = $5;
7190 					$$ = n;
7191 				}
7192 			| ALL ROUTINES IN_P SCHEMA name_list
7193 				{
7194 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7195 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7196 					n->objtype = OBJECT_ROUTINE;
7197 					n->objs = $5;
7198 					$$ = n;
7199 				}
7200 		;
7201 
7202 
7203 grantee_list:
7204 			grantee									{ $$ = list_make1($1); }
7205 			| grantee_list ',' grantee				{ $$ = lappend($1, $3); }
7206 		;
7207 
7208 grantee:
7209 			RoleSpec								{ $$ = $1; }
7210 			| GROUP_P RoleSpec						{ $$ = $2; }
7211 		;
7212 
7213 
7214 opt_grant_grant_option:
7215 			WITH GRANT OPTION { $$ = true; }
7216 			| /*EMPTY*/ { $$ = false; }
7217 		;
7218 
7219 /*****************************************************************************
7220  *
7221  * GRANT and REVOKE ROLE statements
7222  *
7223  *****************************************************************************/
7224 
7225 GrantRoleStmt:
7226 			GRANT privilege_list TO role_list opt_grant_admin_option opt_granted_by
7227 				{
7228 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7229 					n->is_grant = true;
7230 					n->granted_roles = $2;
7231 					n->grantee_roles = $4;
7232 					n->admin_opt = $5;
7233 					n->grantor = $6;
7234 					$$ = (Node*)n;
7235 				}
7236 		;
7237 
7238 RevokeRoleStmt:
7239 			REVOKE privilege_list FROM role_list opt_granted_by opt_drop_behavior
7240 				{
7241 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7242 					n->is_grant = false;
7243 					n->admin_opt = false;
7244 					n->granted_roles = $2;
7245 					n->grantee_roles = $4;
7246 					n->behavior = $6;
7247 					$$ = (Node*)n;
7248 				}
7249 			| REVOKE ADMIN OPTION FOR privilege_list FROM role_list opt_granted_by opt_drop_behavior
7250 				{
7251 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7252 					n->is_grant = false;
7253 					n->admin_opt = true;
7254 					n->granted_roles = $5;
7255 					n->grantee_roles = $7;
7256 					n->behavior = $9;
7257 					$$ = (Node*)n;
7258 				}
7259 		;
7260 
7261 opt_grant_admin_option: WITH ADMIN OPTION				{ $$ = true; }
7262 			| /*EMPTY*/									{ $$ = false; }
7263 		;
7264 
7265 opt_granted_by: GRANTED BY RoleSpec						{ $$ = $3; }
7266 			| /*EMPTY*/									{ $$ = NULL; }
7267 		;
7268 
7269 /*****************************************************************************
7270  *
7271  * ALTER DEFAULT PRIVILEGES statement
7272  *
7273  *****************************************************************************/
7274 
7275 AlterDefaultPrivilegesStmt:
7276 			ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
7277 				{
7278 					AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
7279 					n->options = $4;
7280 					n->action = (GrantStmt *) $5;
7281 					$$ = (Node*)n;
7282 				}
7283 		;
7284 
7285 DefACLOptionList:
7286 			DefACLOptionList DefACLOption			{ $$ = lappend($1, $2); }
7287 			| /* EMPTY */							{ $$ = NIL; }
7288 		;
7289 
7290 DefACLOption:
7291 			IN_P SCHEMA name_list
7292 				{
7293 					$$ = makeDefElem("schemas", (Node *)$3, @1);
7294 				}
7295 			| FOR ROLE role_list
7296 				{
7297 					$$ = makeDefElem("roles", (Node *)$3, @1);
7298 				}
7299 			| FOR USER role_list
7300 				{
7301 					$$ = makeDefElem("roles", (Node *)$3, @1);
7302 				}
7303 		;
7304 
7305 /*
7306  * This should match GRANT/REVOKE, except that individual target objects
7307  * are not mentioned and we only allow a subset of object types.
7308  */
7309 DefACLAction:
7310 			GRANT privileges ON defacl_privilege_target TO grantee_list
7311 			opt_grant_grant_option
7312 				{
7313 					GrantStmt *n = makeNode(GrantStmt);
7314 					n->is_grant = true;
7315 					n->privileges = $2;
7316 					n->targtype = ACL_TARGET_DEFAULTS;
7317 					n->objtype = $4;
7318 					n->objects = NIL;
7319 					n->grantees = $6;
7320 					n->grant_option = $7;
7321 					$$ = (Node*)n;
7322 				}
7323 			| REVOKE privileges ON defacl_privilege_target
7324 			FROM grantee_list opt_drop_behavior
7325 				{
7326 					GrantStmt *n = makeNode(GrantStmt);
7327 					n->is_grant = false;
7328 					n->grant_option = false;
7329 					n->privileges = $2;
7330 					n->targtype = ACL_TARGET_DEFAULTS;
7331 					n->objtype = $4;
7332 					n->objects = NIL;
7333 					n->grantees = $6;
7334 					n->behavior = $7;
7335 					$$ = (Node *)n;
7336 				}
7337 			| REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
7338 			FROM grantee_list opt_drop_behavior
7339 				{
7340 					GrantStmt *n = makeNode(GrantStmt);
7341 					n->is_grant = false;
7342 					n->grant_option = true;
7343 					n->privileges = $5;
7344 					n->targtype = ACL_TARGET_DEFAULTS;
7345 					n->objtype = $7;
7346 					n->objects = NIL;
7347 					n->grantees = $9;
7348 					n->behavior = $10;
7349 					$$ = (Node *)n;
7350 				}
7351 		;
7352 
7353 defacl_privilege_target:
7354 			TABLES			{ $$ = OBJECT_TABLE; }
7355 			| FUNCTIONS		{ $$ = OBJECT_FUNCTION; }
7356 			| ROUTINES		{ $$ = OBJECT_FUNCTION; }
7357 			| SEQUENCES		{ $$ = OBJECT_SEQUENCE; }
7358 			| TYPES_P		{ $$ = OBJECT_TYPE; }
7359 			| SCHEMAS		{ $$ = OBJECT_SCHEMA; }
7360 		;
7361 
7362 
7363 /*****************************************************************************
7364  *
7365  *		QUERY: CREATE INDEX
7366  *
7367  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
7368  * willing to make TABLESPACE a fully reserved word.
7369  *****************************************************************************/
7370 
7371 IndexStmt:	CREATE opt_unique INDEX opt_concurrently opt_index_name
7372 			ON relation_expr access_method_clause '(' index_params ')'
7373 			opt_include opt_reloptions OptTableSpace where_clause
7374 				{
7375 					IndexStmt *n = makeNode(IndexStmt);
7376 					n->unique = $2;
7377 					n->concurrent = $4;
7378 					n->idxname = $5;
7379 					n->relation = $7;
7380 					n->accessMethod = $8;
7381 					n->indexParams = $10;
7382 					n->indexIncludingParams = $12;
7383 					n->options = $13;
7384 					n->tableSpace = $14;
7385 					n->whereClause = $15;
7386 					n->excludeOpNames = NIL;
7387 					n->idxcomment = NULL;
7388 					n->indexOid = InvalidOid;
7389 					n->oldNode = InvalidOid;
7390 					n->oldCreateSubid = InvalidSubTransactionId;
7391 					n->oldFirstRelfilenodeSubid = InvalidSubTransactionId;
7392 					n->primary = false;
7393 					n->isconstraint = false;
7394 					n->deferrable = false;
7395 					n->initdeferred = false;
7396 					n->transformed = false;
7397 					n->if_not_exists = false;
7398 					n->reset_default_tblspc = false;
7399 					$$ = (Node *)n;
7400 				}
7401 			| CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS name
7402 			ON relation_expr access_method_clause '(' index_params ')'
7403 			opt_include opt_reloptions OptTableSpace where_clause
7404 				{
7405 					IndexStmt *n = makeNode(IndexStmt);
7406 					n->unique = $2;
7407 					n->concurrent = $4;
7408 					n->idxname = $8;
7409 					n->relation = $10;
7410 					n->accessMethod = $11;
7411 					n->indexParams = $13;
7412 					n->indexIncludingParams = $15;
7413 					n->options = $16;
7414 					n->tableSpace = $17;
7415 					n->whereClause = $18;
7416 					n->excludeOpNames = NIL;
7417 					n->idxcomment = NULL;
7418 					n->indexOid = InvalidOid;
7419 					n->oldNode = InvalidOid;
7420 					n->oldCreateSubid = InvalidSubTransactionId;
7421 					n->oldFirstRelfilenodeSubid = InvalidSubTransactionId;
7422 					n->primary = false;
7423 					n->isconstraint = false;
7424 					n->deferrable = false;
7425 					n->initdeferred = false;
7426 					n->transformed = false;
7427 					n->if_not_exists = true;
7428 					n->reset_default_tblspc = false;
7429 					$$ = (Node *)n;
7430 				}
7431 		;
7432 
7433 opt_unique:
7434 			UNIQUE									{ $$ = true; }
7435 			| /*EMPTY*/								{ $$ = false; }
7436 		;
7437 
7438 opt_concurrently:
7439 			CONCURRENTLY							{ $$ = true; }
7440 			| /*EMPTY*/								{ $$ = false; }
7441 		;
7442 
7443 opt_index_name:
7444 			name									{ $$ = $1; }
7445 			| /*EMPTY*/								{ $$ = NULL; }
7446 		;
7447 
7448 access_method_clause:
7449 			USING name								{ $$ = $2; }
7450 			| /*EMPTY*/								{ $$ = DEFAULT_INDEX_TYPE; }
7451 		;
7452 
7453 index_params:	index_elem							{ $$ = list_make1($1); }
7454 			| index_params ',' index_elem			{ $$ = lappend($1, $3); }
7455 		;
7456 
7457 
7458 index_elem_options:
7459 	opt_collate opt_class opt_asc_desc opt_nulls_order
7460 		{
7461 			$$ = makeNode(IndexElem);
7462 			$$->name = NULL;
7463 			$$->expr = NULL;
7464 			$$->indexcolname = NULL;
7465 			$$->collation = $1;
7466 			$$->opclass = $2;
7467 			$$->opclassopts = NIL;
7468 			$$->ordering = $3;
7469 			$$->nulls_ordering = $4;
7470 		}
7471 	| opt_collate any_name reloptions opt_asc_desc opt_nulls_order
7472 		{
7473 			$$ = makeNode(IndexElem);
7474 			$$->name = NULL;
7475 			$$->expr = NULL;
7476 			$$->indexcolname = NULL;
7477 			$$->collation = $1;
7478 			$$->opclass = $2;
7479 			$$->opclassopts = $3;
7480 			$$->ordering = $4;
7481 			$$->nulls_ordering = $5;
7482 		}
7483 	;
7484 
7485 /*
7486  * Index attributes can be either simple column references, or arbitrary
7487  * expressions in parens.  For backwards-compatibility reasons, we allow
7488  * an expression that's just a function call to be written without parens.
7489  */
7490 index_elem: ColId index_elem_options
7491 				{
7492 					$$ = $2;
7493 					$$->name = $1;
7494 				}
7495 			| func_expr_windowless index_elem_options
7496 				{
7497 					$$ = $2;
7498 					$$->expr = $1;
7499 				}
7500 			| '(' a_expr ')' index_elem_options
7501 				{
7502 					$$ = $4;
7503 					$$->expr = $2;
7504 				}
7505 		;
7506 
7507 opt_include:		INCLUDE '(' index_including_params ')'			{ $$ = $3; }
7508 			 |		/* EMPTY */						{ $$ = NIL; }
7509 		;
7510 
7511 index_including_params:	index_elem						{ $$ = list_make1($1); }
7512 			| index_including_params ',' index_elem		{ $$ = lappend($1, $3); }
7513 		;
7514 
7515 opt_collate: COLLATE any_name						{ $$ = $2; }
7516 			| /*EMPTY*/								{ $$ = NIL; }
7517 		;
7518 
7519 opt_class:	any_name								{ $$ = $1; }
7520 			| /*EMPTY*/								{ $$ = NIL; }
7521 		;
7522 
7523 opt_asc_desc: ASC							{ $$ = SORTBY_ASC; }
7524 			| DESC							{ $$ = SORTBY_DESC; }
7525 			| /*EMPTY*/						{ $$ = SORTBY_DEFAULT; }
7526 		;
7527 
7528 opt_nulls_order: NULLS_LA FIRST_P			{ $$ = SORTBY_NULLS_FIRST; }
7529 			| NULLS_LA LAST_P				{ $$ = SORTBY_NULLS_LAST; }
7530 			| /*EMPTY*/						{ $$ = SORTBY_NULLS_DEFAULT; }
7531 		;
7532 
7533 
7534 /*****************************************************************************
7535  *
7536  *		QUERY:
7537  *				create [or replace] function <fname>
7538  *						[(<type-1> { , <type-n>})]
7539  *						returns <type-r>
7540  *						as <filename or code in language as appropriate>
7541  *						language <lang> [with parameters]
7542  *
7543  *****************************************************************************/
7544 
7545 CreateFunctionStmt:
7546 			CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7547 			RETURNS func_return opt_createfunc_opt_list opt_routine_body
7548 				{
7549 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7550 					n->is_procedure = false;
7551 					n->replace = $2;
7552 					n->funcname = $4;
7553 					n->parameters = $5;
7554 					n->returnType = $7;
7555 					n->options = $8;
7556 					n->sql_body = $9;
7557 					$$ = (Node *)n;
7558 				}
7559 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7560 			  RETURNS TABLE '(' table_func_column_list ')' opt_createfunc_opt_list opt_routine_body
7561 				{
7562 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7563 					n->is_procedure = false;
7564 					n->replace = $2;
7565 					n->funcname = $4;
7566 					n->parameters = mergeTableFuncParameters($5, $9);
7567 					n->returnType = TableFuncTypeName($9);
7568 					n->returnType->location = @7;
7569 					n->options = $11;
7570 					n->sql_body = $12;
7571 					$$ = (Node *)n;
7572 				}
7573 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7574 			  opt_createfunc_opt_list opt_routine_body
7575 				{
7576 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7577 					n->is_procedure = false;
7578 					n->replace = $2;
7579 					n->funcname = $4;
7580 					n->parameters = $5;
7581 					n->returnType = NULL;
7582 					n->options = $6;
7583 					n->sql_body = $7;
7584 					$$ = (Node *)n;
7585 				}
7586 			| CREATE opt_or_replace PROCEDURE func_name func_args_with_defaults
7587 			  opt_createfunc_opt_list opt_routine_body
7588 				{
7589 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7590 					n->is_procedure = true;
7591 					n->replace = $2;
7592 					n->funcname = $4;
7593 					n->parameters = $5;
7594 					n->returnType = NULL;
7595 					n->options = $6;
7596 					n->sql_body = $7;
7597 					$$ = (Node *)n;
7598 				}
7599 		;
7600 
7601 opt_or_replace:
7602 			OR REPLACE								{ $$ = true; }
7603 			| /*EMPTY*/								{ $$ = false; }
7604 		;
7605 
7606 func_args:	'(' func_args_list ')'					{ $$ = $2; }
7607 			| '(' ')'								{ $$ = NIL; }
7608 		;
7609 
7610 func_args_list:
7611 			func_arg								{ $$ = list_make1($1); }
7612 			| func_args_list ',' func_arg			{ $$ = lappend($1, $3); }
7613 		;
7614 
7615 function_with_argtypes_list:
7616 			function_with_argtypes					{ $$ = list_make1($1); }
7617 			| function_with_argtypes_list ',' function_with_argtypes
7618 													{ $$ = lappend($1, $3); }
7619 		;
7620 
7621 function_with_argtypes:
7622 			func_name func_args
7623 				{
7624 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7625 					n->objname = $1;
7626 					n->objargs = extractArgTypes($2);
7627 					n->objfuncargs = $2;
7628 					$$ = n;
7629 				}
7630 			/*
7631 			 * Because of reduce/reduce conflicts, we can't use func_name
7632 			 * below, but we can write it out the long way, which actually
7633 			 * allows more cases.
7634 			 */
7635 			| type_func_name_keyword
7636 				{
7637 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7638 					n->objname = list_make1(makeString(pstrdup($1)));
7639 					n->args_unspecified = true;
7640 					$$ = n;
7641 				}
7642 			| ColId
7643 				{
7644 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7645 					n->objname = list_make1(makeString($1));
7646 					n->args_unspecified = true;
7647 					$$ = n;
7648 				}
7649 			| ColId indirection
7650 				{
7651 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7652 					n->objname = check_func_name(lcons(makeString($1), $2),
7653 												  yyscanner);
7654 					n->args_unspecified = true;
7655 					$$ = n;
7656 				}
7657 		;
7658 
7659 /*
7660  * func_args_with_defaults is separate because we only want to accept
7661  * defaults in CREATE FUNCTION, not in ALTER etc.
7662  */
7663 func_args_with_defaults:
7664 		'(' func_args_with_defaults_list ')'		{ $$ = $2; }
7665 		| '(' ')'									{ $$ = NIL; }
7666 		;
7667 
7668 func_args_with_defaults_list:
7669 		func_arg_with_default						{ $$ = list_make1($1); }
7670 		| func_args_with_defaults_list ',' func_arg_with_default
7671 													{ $$ = lappend($1, $3); }
7672 		;
7673 
7674 /*
7675  * The style with arg_class first is SQL99 standard, but Oracle puts
7676  * param_name first; accept both since it's likely people will try both
7677  * anyway.  Don't bother trying to save productions by letting arg_class
7678  * have an empty alternative ... you'll get shift/reduce conflicts.
7679  *
7680  * We can catch over-specified arguments here if we want to,
7681  * but for now better to silently swallow typmod, etc.
7682  * - thomas 2000-03-22
7683  */
7684 func_arg:
7685 			arg_class param_name func_type
7686 				{
7687 					FunctionParameter *n = makeNode(FunctionParameter);
7688 					n->name = $2;
7689 					n->argType = $3;
7690 					n->mode = $1;
7691 					n->defexpr = NULL;
7692 					$$ = n;
7693 				}
7694 			| param_name arg_class func_type
7695 				{
7696 					FunctionParameter *n = makeNode(FunctionParameter);
7697 					n->name = $1;
7698 					n->argType = $3;
7699 					n->mode = $2;
7700 					n->defexpr = NULL;
7701 					$$ = n;
7702 				}
7703 			| param_name func_type
7704 				{
7705 					FunctionParameter *n = makeNode(FunctionParameter);
7706 					n->name = $1;
7707 					n->argType = $2;
7708 					n->mode = FUNC_PARAM_DEFAULT;
7709 					n->defexpr = NULL;
7710 					$$ = n;
7711 				}
7712 			| arg_class func_type
7713 				{
7714 					FunctionParameter *n = makeNode(FunctionParameter);
7715 					n->name = NULL;
7716 					n->argType = $2;
7717 					n->mode = $1;
7718 					n->defexpr = NULL;
7719 					$$ = n;
7720 				}
7721 			| func_type
7722 				{
7723 					FunctionParameter *n = makeNode(FunctionParameter);
7724 					n->name = NULL;
7725 					n->argType = $1;
7726 					n->mode = FUNC_PARAM_DEFAULT;
7727 					n->defexpr = NULL;
7728 					$$ = n;
7729 				}
7730 		;
7731 
7732 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
7733 arg_class:	IN_P								{ $$ = FUNC_PARAM_IN; }
7734 			| OUT_P								{ $$ = FUNC_PARAM_OUT; }
7735 			| INOUT								{ $$ = FUNC_PARAM_INOUT; }
7736 			| IN_P OUT_P						{ $$ = FUNC_PARAM_INOUT; }
7737 			| VARIADIC							{ $$ = FUNC_PARAM_VARIADIC; }
7738 		;
7739 
7740 /*
7741  * Ideally param_name should be ColId, but that causes too many conflicts.
7742  */
7743 param_name:	type_function_name
7744 		;
7745 
7746 func_return:
7747 			func_type
7748 				{
7749 					/* We can catch over-specified results here if we want to,
7750 					 * but for now better to silently swallow typmod, etc.
7751 					 * - thomas 2000-03-22
7752 					 */
7753 					$$ = $1;
7754 				}
7755 		;
7756 
7757 /*
7758  * We would like to make the %TYPE productions here be ColId attrs etc,
7759  * but that causes reduce/reduce conflicts.  type_function_name
7760  * is next best choice.
7761  */
7762 func_type:	Typename								{ $$ = $1; }
7763 			| type_function_name attrs '%' TYPE_P
7764 				{
7765 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7766 					$$->pct_type = true;
7767 					$$->location = @1;
7768 				}
7769 			| SETOF type_function_name attrs '%' TYPE_P
7770 				{
7771 					$$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
7772 					$$->pct_type = true;
7773 					$$->setof = true;
7774 					$$->location = @2;
7775 				}
7776 		;
7777 
7778 func_arg_with_default:
7779 		func_arg
7780 				{
7781 					$$ = $1;
7782 				}
7783 		| func_arg DEFAULT a_expr
7784 				{
7785 					$$ = $1;
7786 					$$->defexpr = $3;
7787 				}
7788 		| func_arg '=' a_expr
7789 				{
7790 					$$ = $1;
7791 					$$->defexpr = $3;
7792 				}
7793 		;
7794 
7795 /* Aggregate args can be most things that function args can be */
7796 aggr_arg:	func_arg
7797 				{
7798 					if (!($1->mode == FUNC_PARAM_DEFAULT ||
7799 						  $1->mode == FUNC_PARAM_IN ||
7800 						  $1->mode == FUNC_PARAM_VARIADIC))
7801 						ereport(ERROR,
7802 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7803 								 errmsg("aggregates cannot have output arguments"),
7804 								 parser_errposition(@1)));
7805 					$$ = $1;
7806 				}
7807 		;
7808 
7809 /*
7810  * The SQL standard offers no guidance on how to declare aggregate argument
7811  * lists, since it doesn't have CREATE AGGREGATE etc.  We accept these cases:
7812  *
7813  * (*)									- normal agg with no args
7814  * (aggr_arg,...)						- normal agg with args
7815  * (ORDER BY aggr_arg,...)				- ordered-set agg with no direct args
7816  * (aggr_arg,... ORDER BY aggr_arg,...)	- ordered-set agg with direct args
7817  *
7818  * The zero-argument case is spelled with '*' for consistency with COUNT(*).
7819  *
7820  * An additional restriction is that if the direct-args list ends in a
7821  * VARIADIC item, the ordered-args list must contain exactly one item that
7822  * is also VARIADIC with the same type.  This allows us to collapse the two
7823  * VARIADIC items into one, which is necessary to represent the aggregate in
7824  * pg_proc.  We check this at the grammar stage so that we can return a list
7825  * in which the second VARIADIC item is already discarded, avoiding extra work
7826  * in cases such as DROP AGGREGATE.
7827  *
7828  * The return value of this production is a two-element list, in which the
7829  * first item is a sublist of FunctionParameter nodes (with any duplicate
7830  * VARIADIC item already dropped, as per above) and the second is an integer
7831  * Value node, containing -1 if there was no ORDER BY and otherwise the number
7832  * of argument declarations before the ORDER BY.  (If this number is equal
7833  * to the first sublist's length, then we dropped a duplicate VARIADIC item.)
7834  * This representation is passed as-is to CREATE AGGREGATE; for operations
7835  * on existing aggregates, we can just apply extractArgTypes to the first
7836  * sublist.
7837  */
7838 aggr_args:	'(' '*' ')'
7839 				{
7840 					$$ = list_make2(NIL, makeInteger(-1));
7841 				}
7842 			| '(' aggr_args_list ')'
7843 				{
7844 					$$ = list_make2($2, makeInteger(-1));
7845 				}
7846 			| '(' ORDER BY aggr_args_list ')'
7847 				{
7848 					$$ = list_make2($4, makeInteger(0));
7849 				}
7850 			| '(' aggr_args_list ORDER BY aggr_args_list ')'
7851 				{
7852 					/* this is the only case requiring consistency checking */
7853 					$$ = makeOrderedSetArgs($2, $5, yyscanner);
7854 				}
7855 		;
7856 
7857 aggr_args_list:
7858 			aggr_arg								{ $$ = list_make1($1); }
7859 			| aggr_args_list ',' aggr_arg			{ $$ = lappend($1, $3); }
7860 		;
7861 
7862 aggregate_with_argtypes:
7863 			func_name aggr_args
7864 				{
7865 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7866 					n->objname = $1;
7867 					n->objargs = extractAggrArgTypes($2);
7868 					n->objfuncargs = (List *) linitial($2);
7869 					$$ = n;
7870 				}
7871 		;
7872 
7873 aggregate_with_argtypes_list:
7874 			aggregate_with_argtypes					{ $$ = list_make1($1); }
7875 			| aggregate_with_argtypes_list ',' aggregate_with_argtypes
7876 													{ $$ = lappend($1, $3); }
7877 		;
7878 
7879 opt_createfunc_opt_list:
7880 			createfunc_opt_list
7881 			| /*EMPTY*/ { $$ = NIL; }
7882 	;
7883 
7884 createfunc_opt_list:
7885 			/* Must be at least one to prevent conflict */
7886 			createfunc_opt_item						{ $$ = list_make1($1); }
7887 			| createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
7888 	;
7889 
7890 /*
7891  * Options common to both CREATE FUNCTION and ALTER FUNCTION
7892  */
7893 common_func_opt_item:
7894 			CALLED ON NULL_P INPUT_P
7895 				{
7896 					$$ = makeDefElem("strict", (Node *)makeInteger(false), @1);
7897 				}
7898 			| RETURNS NULL_P ON NULL_P INPUT_P
7899 				{
7900 					$$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7901 				}
7902 			| STRICT_P
7903 				{
7904 					$$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7905 				}
7906 			| IMMUTABLE
7907 				{
7908 					$$ = makeDefElem("volatility", (Node *)makeString("immutable"), @1);
7909 				}
7910 			| STABLE
7911 				{
7912 					$$ = makeDefElem("volatility", (Node *)makeString("stable"), @1);
7913 				}
7914 			| VOLATILE
7915 				{
7916 					$$ = makeDefElem("volatility", (Node *)makeString("volatile"), @1);
7917 				}
7918 			| EXTERNAL SECURITY DEFINER
7919 				{
7920 					$$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7921 				}
7922 			| EXTERNAL SECURITY INVOKER
7923 				{
7924 					$$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7925 				}
7926 			| SECURITY DEFINER
7927 				{
7928 					$$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7929 				}
7930 			| SECURITY INVOKER
7931 				{
7932 					$$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7933 				}
7934 			| LEAKPROOF
7935 				{
7936 					$$ = makeDefElem("leakproof", (Node *)makeInteger(true), @1);
7937 				}
7938 			| NOT LEAKPROOF
7939 				{
7940 					$$ = makeDefElem("leakproof", (Node *)makeInteger(false), @1);
7941 				}
7942 			| COST NumericOnly
7943 				{
7944 					$$ = makeDefElem("cost", (Node *)$2, @1);
7945 				}
7946 			| ROWS NumericOnly
7947 				{
7948 					$$ = makeDefElem("rows", (Node *)$2, @1);
7949 				}
7950 			| SUPPORT any_name
7951 				{
7952 					$$ = makeDefElem("support", (Node *)$2, @1);
7953 				}
7954 			| FunctionSetResetClause
7955 				{
7956 					/* we abuse the normal content of a DefElem here */
7957 					$$ = makeDefElem("set", (Node *)$1, @1);
7958 				}
7959 			| PARALLEL ColId
7960 				{
7961 					$$ = makeDefElem("parallel", (Node *)makeString($2), @1);
7962 				}
7963 		;
7964 
7965 createfunc_opt_item:
7966 			AS func_as
7967 				{
7968 					$$ = makeDefElem("as", (Node *)$2, @1);
7969 				}
7970 			| LANGUAGE NonReservedWord_or_Sconst
7971 				{
7972 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
7973 				}
7974 			| TRANSFORM transform_type_list
7975 				{
7976 					$$ = makeDefElem("transform", (Node *)$2, @1);
7977 				}
7978 			| WINDOW
7979 				{
7980 					$$ = makeDefElem("window", (Node *)makeInteger(true), @1);
7981 				}
7982 			| common_func_opt_item
7983 				{
7984 					$$ = $1;
7985 				}
7986 		;
7987 
7988 func_as:	Sconst						{ $$ = list_make1(makeString($1)); }
7989 			| Sconst ',' Sconst
7990 				{
7991 					$$ = list_make2(makeString($1), makeString($3));
7992 				}
7993 		;
7994 
7995 ReturnStmt:	RETURN a_expr
7996 				{
7997 					ReturnStmt *r = makeNode(ReturnStmt);
7998 					r->returnval = (Node *) $2;
7999 					$$ = (Node *) r;
8000 				}
8001 		;
8002 
8003 opt_routine_body:
8004 			ReturnStmt
8005 				{
8006 					$$ = $1;
8007 				}
8008 			| BEGIN_P ATOMIC routine_body_stmt_list END_P
8009 				{
8010 					/*
8011 					 * A compound statement is stored as a single-item list
8012 					 * containing the list of statements as its member.  That
8013 					 * way, the parse analysis code can tell apart an empty
8014 					 * body from no body at all.
8015 					 */
8016 					$$ = (Node *) list_make1($3);
8017 				}
8018 			| /*EMPTY*/
8019 				{
8020 					$$ = NULL;
8021 				}
8022 		;
8023 
8024 routine_body_stmt_list:
8025 			routine_body_stmt_list routine_body_stmt ';'
8026 				{
8027 					/* As in stmtmulti, discard empty statements */
8028 					if ($2 != NULL)
8029 						$$ = lappend($1, $2);
8030 					else
8031 						$$ = $1;
8032 				}
8033 			| /*EMPTY*/
8034 				{
8035 					$$ = NIL;
8036 				}
8037 		;
8038 
8039 routine_body_stmt:
8040 			stmt
8041 			| ReturnStmt
8042 		;
8043 
8044 transform_type_list:
8045 			FOR TYPE_P Typename { $$ = list_make1($3); }
8046 			| transform_type_list ',' FOR TYPE_P Typename { $$ = lappend($1, $5); }
8047 		;
8048 
8049 opt_definition:
8050 			WITH definition							{ $$ = $2; }
8051 			| /*EMPTY*/								{ $$ = NIL; }
8052 		;
8053 
8054 table_func_column:	param_name func_type
8055 				{
8056 					FunctionParameter *n = makeNode(FunctionParameter);
8057 					n->name = $1;
8058 					n->argType = $2;
8059 					n->mode = FUNC_PARAM_TABLE;
8060 					n->defexpr = NULL;
8061 					$$ = n;
8062 				}
8063 		;
8064 
8065 table_func_column_list:
8066 			table_func_column
8067 				{
8068 					$$ = list_make1($1);
8069 				}
8070 			| table_func_column_list ',' table_func_column
8071 				{
8072 					$$ = lappend($1, $3);
8073 				}
8074 		;
8075 
8076 /*****************************************************************************
8077  * ALTER FUNCTION / ALTER PROCEDURE / ALTER ROUTINE
8078  *
8079  * RENAME and OWNER subcommands are already provided by the generic
8080  * ALTER infrastructure, here we just specify alterations that can
8081  * only be applied to functions.
8082  *
8083  *****************************************************************************/
8084 AlterFunctionStmt:
8085 			ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
8086 				{
8087 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8088 					n->objtype = OBJECT_FUNCTION;
8089 					n->func = $3;
8090 					n->actions = $4;
8091 					$$ = (Node *) n;
8092 				}
8093 			| ALTER PROCEDURE function_with_argtypes alterfunc_opt_list opt_restrict
8094 				{
8095 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8096 					n->objtype = OBJECT_PROCEDURE;
8097 					n->func = $3;
8098 					n->actions = $4;
8099 					$$ = (Node *) n;
8100 				}
8101 			| ALTER ROUTINE function_with_argtypes alterfunc_opt_list opt_restrict
8102 				{
8103 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8104 					n->objtype = OBJECT_ROUTINE;
8105 					n->func = $3;
8106 					n->actions = $4;
8107 					$$ = (Node *) n;
8108 				}
8109 		;
8110 
8111 alterfunc_opt_list:
8112 			/* At least one option must be specified */
8113 			common_func_opt_item					{ $$ = list_make1($1); }
8114 			| alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
8115 		;
8116 
8117 /* Ignored, merely for SQL compliance */
8118 opt_restrict:
8119 			RESTRICT
8120 			| /* EMPTY */
8121 		;
8122 
8123 
8124 /*****************************************************************************
8125  *
8126  *		QUERY:
8127  *
8128  *		DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8129  *		DROP PROCEDURE procname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8130  *		DROP ROUTINE routname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8131  *		DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
8132  *		DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
8133  *
8134  *****************************************************************************/
8135 
8136 RemoveFuncStmt:
8137 			DROP FUNCTION function_with_argtypes_list opt_drop_behavior
8138 				{
8139 					DropStmt *n = makeNode(DropStmt);
8140 					n->removeType = OBJECT_FUNCTION;
8141 					n->objects = $3;
8142 					n->behavior = $4;
8143 					n->missing_ok = false;
8144 					n->concurrent = false;
8145 					$$ = (Node *)n;
8146 				}
8147 			| DROP FUNCTION IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8148 				{
8149 					DropStmt *n = makeNode(DropStmt);
8150 					n->removeType = OBJECT_FUNCTION;
8151 					n->objects = $5;
8152 					n->behavior = $6;
8153 					n->missing_ok = true;
8154 					n->concurrent = false;
8155 					$$ = (Node *)n;
8156 				}
8157 			| DROP PROCEDURE function_with_argtypes_list opt_drop_behavior
8158 				{
8159 					DropStmt *n = makeNode(DropStmt);
8160 					n->removeType = OBJECT_PROCEDURE;
8161 					n->objects = $3;
8162 					n->behavior = $4;
8163 					n->missing_ok = false;
8164 					n->concurrent = false;
8165 					$$ = (Node *)n;
8166 				}
8167 			| DROP PROCEDURE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8168 				{
8169 					DropStmt *n = makeNode(DropStmt);
8170 					n->removeType = OBJECT_PROCEDURE;
8171 					n->objects = $5;
8172 					n->behavior = $6;
8173 					n->missing_ok = true;
8174 					n->concurrent = false;
8175 					$$ = (Node *)n;
8176 				}
8177 			| DROP ROUTINE function_with_argtypes_list opt_drop_behavior
8178 				{
8179 					DropStmt *n = makeNode(DropStmt);
8180 					n->removeType = OBJECT_ROUTINE;
8181 					n->objects = $3;
8182 					n->behavior = $4;
8183 					n->missing_ok = false;
8184 					n->concurrent = false;
8185 					$$ = (Node *)n;
8186 				}
8187 			| DROP ROUTINE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8188 				{
8189 					DropStmt *n = makeNode(DropStmt);
8190 					n->removeType = OBJECT_ROUTINE;
8191 					n->objects = $5;
8192 					n->behavior = $6;
8193 					n->missing_ok = true;
8194 					n->concurrent = false;
8195 					$$ = (Node *)n;
8196 				}
8197 		;
8198 
8199 RemoveAggrStmt:
8200 			DROP AGGREGATE aggregate_with_argtypes_list opt_drop_behavior
8201 				{
8202 					DropStmt *n = makeNode(DropStmt);
8203 					n->removeType = OBJECT_AGGREGATE;
8204 					n->objects = $3;
8205 					n->behavior = $4;
8206 					n->missing_ok = false;
8207 					n->concurrent = false;
8208 					$$ = (Node *)n;
8209 				}
8210 			| DROP AGGREGATE IF_P EXISTS aggregate_with_argtypes_list opt_drop_behavior
8211 				{
8212 					DropStmt *n = makeNode(DropStmt);
8213 					n->removeType = OBJECT_AGGREGATE;
8214 					n->objects = $5;
8215 					n->behavior = $6;
8216 					n->missing_ok = true;
8217 					n->concurrent = false;
8218 					$$ = (Node *)n;
8219 				}
8220 		;
8221 
8222 RemoveOperStmt:
8223 			DROP OPERATOR operator_with_argtypes_list opt_drop_behavior
8224 				{
8225 					DropStmt *n = makeNode(DropStmt);
8226 					n->removeType = OBJECT_OPERATOR;
8227 					n->objects = $3;
8228 					n->behavior = $4;
8229 					n->missing_ok = false;
8230 					n->concurrent = false;
8231 					$$ = (Node *)n;
8232 				}
8233 			| DROP OPERATOR IF_P EXISTS operator_with_argtypes_list opt_drop_behavior
8234 				{
8235 					DropStmt *n = makeNode(DropStmt);
8236 					n->removeType = OBJECT_OPERATOR;
8237 					n->objects = $5;
8238 					n->behavior = $6;
8239 					n->missing_ok = true;
8240 					n->concurrent = false;
8241 					$$ = (Node *)n;
8242 				}
8243 		;
8244 
8245 oper_argtypes:
8246 			'(' Typename ')'
8247 				{
8248 				   ereport(ERROR,
8249 						   (errcode(ERRCODE_SYNTAX_ERROR),
8250 							errmsg("missing argument"),
8251 							errhint("Use NONE to denote the missing argument of a unary operator."),
8252 							parser_errposition(@3)));
8253 				}
8254 			| '(' Typename ',' Typename ')'
8255 					{ $$ = list_make2($2, $4); }
8256 			| '(' NONE ',' Typename ')'					/* left unary */
8257 					{ $$ = list_make2(NULL, $4); }
8258 			| '(' Typename ',' NONE ')'					/* right unary */
8259 					{ $$ = list_make2($2, NULL); }
8260 		;
8261 
8262 any_operator:
8263 			all_Op
8264 					{ $$ = list_make1(makeString($1)); }
8265 			| ColId '.' any_operator
8266 					{ $$ = lcons(makeString($1), $3); }
8267 		;
8268 
8269 operator_with_argtypes_list:
8270 			operator_with_argtypes					{ $$ = list_make1($1); }
8271 			| operator_with_argtypes_list ',' operator_with_argtypes
8272 													{ $$ = lappend($1, $3); }
8273 		;
8274 
8275 operator_with_argtypes:
8276 			any_operator oper_argtypes
8277 				{
8278 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
8279 					n->objname = $1;
8280 					n->objargs = $2;
8281 					$$ = n;
8282 				}
8283 		;
8284 
8285 /*****************************************************************************
8286  *
8287  *		DO <anonymous code block> [ LANGUAGE language ]
8288  *
8289  * We use a DefElem list for future extensibility, and to allow flexibility
8290  * in the clause order.
8291  *
8292  *****************************************************************************/
8293 
8294 DoStmt: DO dostmt_opt_list
8295 				{
8296 					DoStmt *n = makeNode(DoStmt);
8297 					n->args = $2;
8298 					$$ = (Node *)n;
8299 				}
8300 		;
8301 
8302 dostmt_opt_list:
8303 			dostmt_opt_item						{ $$ = list_make1($1); }
8304 			| dostmt_opt_list dostmt_opt_item	{ $$ = lappend($1, $2); }
8305 		;
8306 
8307 dostmt_opt_item:
8308 			Sconst
8309 				{
8310 					$$ = makeDefElem("as", (Node *)makeString($1), @1);
8311 				}
8312 			| LANGUAGE NonReservedWord_or_Sconst
8313 				{
8314 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
8315 				}
8316 		;
8317 
8318 /*****************************************************************************
8319  *
8320  *		CREATE CAST / DROP CAST
8321  *
8322  *****************************************************************************/
8323 
8324 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
8325 					WITH FUNCTION function_with_argtypes cast_context
8326 				{
8327 					CreateCastStmt *n = makeNode(CreateCastStmt);
8328 					n->sourcetype = $4;
8329 					n->targettype = $6;
8330 					n->func = $10;
8331 					n->context = (CoercionContext) $11;
8332 					n->inout = false;
8333 					$$ = (Node *)n;
8334 				}
8335 			| CREATE CAST '(' Typename AS Typename ')'
8336 					WITHOUT FUNCTION cast_context
8337 				{
8338 					CreateCastStmt *n = makeNode(CreateCastStmt);
8339 					n->sourcetype = $4;
8340 					n->targettype = $6;
8341 					n->func = NULL;
8342 					n->context = (CoercionContext) $10;
8343 					n->inout = false;
8344 					$$ = (Node *)n;
8345 				}
8346 			| CREATE CAST '(' Typename AS Typename ')'
8347 					WITH INOUT cast_context
8348 				{
8349 					CreateCastStmt *n = makeNode(CreateCastStmt);
8350 					n->sourcetype = $4;
8351 					n->targettype = $6;
8352 					n->func = NULL;
8353 					n->context = (CoercionContext) $10;
8354 					n->inout = true;
8355 					$$ = (Node *)n;
8356 				}
8357 		;
8358 
8359 cast_context:  AS IMPLICIT_P					{ $$ = COERCION_IMPLICIT; }
8360 		| AS ASSIGNMENT							{ $$ = COERCION_ASSIGNMENT; }
8361 		| /*EMPTY*/								{ $$ = COERCION_EXPLICIT; }
8362 		;
8363 
8364 
8365 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
8366 				{
8367 					DropStmt *n = makeNode(DropStmt);
8368 					n->removeType = OBJECT_CAST;
8369 					n->objects = list_make1(list_make2($5, $7));
8370 					n->behavior = $9;
8371 					n->missing_ok = $3;
8372 					n->concurrent = false;
8373 					$$ = (Node *)n;
8374 				}
8375 		;
8376 
8377 opt_if_exists: IF_P EXISTS						{ $$ = true; }
8378 		| /*EMPTY*/								{ $$ = false; }
8379 		;
8380 
8381 
8382 /*****************************************************************************
8383  *
8384  *		CREATE TRANSFORM / DROP TRANSFORM
8385  *
8386  *****************************************************************************/
8387 
8388 CreateTransformStmt: CREATE opt_or_replace TRANSFORM FOR Typename LANGUAGE name '(' transform_element_list ')'
8389 				{
8390 					CreateTransformStmt *n = makeNode(CreateTransformStmt);
8391 					n->replace = $2;
8392 					n->type_name = $5;
8393 					n->lang = $7;
8394 					n->fromsql = linitial($9);
8395 					n->tosql = lsecond($9);
8396 					$$ = (Node *)n;
8397 				}
8398 		;
8399 
8400 transform_element_list: FROM SQL_P WITH FUNCTION function_with_argtypes ',' TO SQL_P WITH FUNCTION function_with_argtypes
8401 				{
8402 					$$ = list_make2($5, $11);
8403 				}
8404 				| TO SQL_P WITH FUNCTION function_with_argtypes ',' FROM SQL_P WITH FUNCTION function_with_argtypes
8405 				{
8406 					$$ = list_make2($11, $5);
8407 				}
8408 				| FROM SQL_P WITH FUNCTION function_with_argtypes
8409 				{
8410 					$$ = list_make2($5, NULL);
8411 				}
8412 				| TO SQL_P WITH FUNCTION function_with_argtypes
8413 				{
8414 					$$ = list_make2(NULL, $5);
8415 				}
8416 		;
8417 
8418 
8419 DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_drop_behavior
8420 				{
8421 					DropStmt *n = makeNode(DropStmt);
8422 					n->removeType = OBJECT_TRANSFORM;
8423 					n->objects = list_make1(list_make2($5, makeString($7)));
8424 					n->behavior = $8;
8425 					n->missing_ok = $3;
8426 					$$ = (Node *)n;
8427 				}
8428 		;
8429 
8430 
8431 /*****************************************************************************
8432  *
8433  *		QUERY:
8434  *
8435  *		REINDEX [ (options) ] type [CONCURRENTLY] <name>
8436  *****************************************************************************/
8437 
8438 ReindexStmt:
8439 			REINDEX reindex_target_type opt_concurrently qualified_name
8440 				{
8441 					ReindexStmt *n = makeNode(ReindexStmt);
8442 					n->kind = $2;
8443 					n->relation = $4;
8444 					n->name = NULL;
8445 					n->params = NIL;
8446 					if ($3)
8447 						n->params = lappend(n->params,
8448 								makeDefElem("concurrently", NULL, @3));
8449 					$$ = (Node *)n;
8450 				}
8451 			| REINDEX reindex_target_multitable opt_concurrently name
8452 				{
8453 					ReindexStmt *n = makeNode(ReindexStmt);
8454 					n->kind = $2;
8455 					n->name = $4;
8456 					n->relation = NULL;
8457 					n->params = NIL;
8458 					if ($3)
8459 						n->params = lappend(n->params,
8460 								makeDefElem("concurrently", NULL, @3));
8461 					$$ = (Node *)n;
8462 				}
8463 			| REINDEX '(' utility_option_list ')' reindex_target_type opt_concurrently qualified_name
8464 				{
8465 					ReindexStmt *n = makeNode(ReindexStmt);
8466 					n->kind = $5;
8467 					n->relation = $7;
8468 					n->name = NULL;
8469 					n->params = $3;
8470 					if ($6)
8471 						n->params = lappend(n->params,
8472 								makeDefElem("concurrently", NULL, @6));
8473 					$$ = (Node *)n;
8474 				}
8475 			| REINDEX '(' utility_option_list ')' reindex_target_multitable opt_concurrently name
8476 				{
8477 					ReindexStmt *n = makeNode(ReindexStmt);
8478 					n->kind = $5;
8479 					n->name = $7;
8480 					n->relation = NULL;
8481 					n->params = $3;
8482 					if ($6)
8483 						n->params = lappend(n->params,
8484 								makeDefElem("concurrently", NULL, @6));
8485 					$$ = (Node *)n;
8486 				}
8487 		;
8488 reindex_target_type:
8489 			INDEX					{ $$ = REINDEX_OBJECT_INDEX; }
8490 			| TABLE					{ $$ = REINDEX_OBJECT_TABLE; }
8491 		;
8492 reindex_target_multitable:
8493 			SCHEMA					{ $$ = REINDEX_OBJECT_SCHEMA; }
8494 			| SYSTEM_P				{ $$ = REINDEX_OBJECT_SYSTEM; }
8495 			| DATABASE				{ $$ = REINDEX_OBJECT_DATABASE; }
8496 		;
8497 
8498 /*****************************************************************************
8499  *
8500  * ALTER TABLESPACE
8501  *
8502  *****************************************************************************/
8503 
8504 AlterTblSpcStmt:
8505 			ALTER TABLESPACE name SET reloptions
8506 				{
8507 					AlterTableSpaceOptionsStmt *n =
8508 						makeNode(AlterTableSpaceOptionsStmt);
8509 					n->tablespacename = $3;
8510 					n->options = $5;
8511 					n->isReset = false;
8512 					$$ = (Node *)n;
8513 				}
8514 			| ALTER TABLESPACE name RESET reloptions
8515 				{
8516 					AlterTableSpaceOptionsStmt *n =
8517 						makeNode(AlterTableSpaceOptionsStmt);
8518 					n->tablespacename = $3;
8519 					n->options = $5;
8520 					n->isReset = true;
8521 					$$ = (Node *)n;
8522 				}
8523 		;
8524 
8525 /*****************************************************************************
8526  *
8527  * ALTER THING name RENAME TO newname
8528  *
8529  *****************************************************************************/
8530 
8531 RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
8532 				{
8533 					RenameStmt *n = makeNode(RenameStmt);
8534 					n->renameType = OBJECT_AGGREGATE;
8535 					n->object = (Node *) $3;
8536 					n->newname = $6;
8537 					n->missing_ok = false;
8538 					$$ = (Node *)n;
8539 				}
8540 			| ALTER COLLATION any_name RENAME TO name
8541 				{
8542 					RenameStmt *n = makeNode(RenameStmt);
8543 					n->renameType = OBJECT_COLLATION;
8544 					n->object = (Node *) $3;
8545 					n->newname = $6;
8546 					n->missing_ok = false;
8547 					$$ = (Node *)n;
8548 				}
8549 			| ALTER CONVERSION_P any_name RENAME TO name
8550 				{
8551 					RenameStmt *n = makeNode(RenameStmt);
8552 					n->renameType = OBJECT_CONVERSION;
8553 					n->object = (Node *) $3;
8554 					n->newname = $6;
8555 					n->missing_ok = false;
8556 					$$ = (Node *)n;
8557 				}
8558 			| ALTER DATABASE name RENAME TO name
8559 				{
8560 					RenameStmt *n = makeNode(RenameStmt);
8561 					n->renameType = OBJECT_DATABASE;
8562 					n->subname = $3;
8563 					n->newname = $6;
8564 					n->missing_ok = false;
8565 					$$ = (Node *)n;
8566 				}
8567 			| ALTER DOMAIN_P any_name RENAME TO name
8568 				{
8569 					RenameStmt *n = makeNode(RenameStmt);
8570 					n->renameType = OBJECT_DOMAIN;
8571 					n->object = (Node *) $3;
8572 					n->newname = $6;
8573 					n->missing_ok = false;
8574 					$$ = (Node *)n;
8575 				}
8576 			| ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
8577 				{
8578 					RenameStmt *n = makeNode(RenameStmt);
8579 					n->renameType = OBJECT_DOMCONSTRAINT;
8580 					n->object = (Node *) $3;
8581 					n->subname = $6;
8582 					n->newname = $8;
8583 					$$ = (Node *)n;
8584 				}
8585 			| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
8586 				{
8587 					RenameStmt *n = makeNode(RenameStmt);
8588 					n->renameType = OBJECT_FDW;
8589 					n->object = (Node *) makeString($5);
8590 					n->newname = $8;
8591 					n->missing_ok = false;
8592 					$$ = (Node *)n;
8593 				}
8594 			| ALTER FUNCTION function_with_argtypes RENAME TO name
8595 				{
8596 					RenameStmt *n = makeNode(RenameStmt);
8597 					n->renameType = OBJECT_FUNCTION;
8598 					n->object = (Node *) $3;
8599 					n->newname = $6;
8600 					n->missing_ok = false;
8601 					$$ = (Node *)n;
8602 				}
8603 			| ALTER GROUP_P RoleId RENAME TO RoleId
8604 				{
8605 					RenameStmt *n = makeNode(RenameStmt);
8606 					n->renameType = OBJECT_ROLE;
8607 					n->subname = $3;
8608 					n->newname = $6;
8609 					n->missing_ok = false;
8610 					$$ = (Node *)n;
8611 				}
8612 			| ALTER opt_procedural LANGUAGE name RENAME TO name
8613 				{
8614 					RenameStmt *n = makeNode(RenameStmt);
8615 					n->renameType = OBJECT_LANGUAGE;
8616 					n->object = (Node *) makeString($4);
8617 					n->newname = $7;
8618 					n->missing_ok = false;
8619 					$$ = (Node *)n;
8620 				}
8621 			| ALTER OPERATOR CLASS any_name USING name RENAME TO name
8622 				{
8623 					RenameStmt *n = makeNode(RenameStmt);
8624 					n->renameType = OBJECT_OPCLASS;
8625 					n->object = (Node *) lcons(makeString($6), $4);
8626 					n->newname = $9;
8627 					n->missing_ok = false;
8628 					$$ = (Node *)n;
8629 				}
8630 			| ALTER OPERATOR FAMILY any_name USING name RENAME TO name
8631 				{
8632 					RenameStmt *n = makeNode(RenameStmt);
8633 					n->renameType = OBJECT_OPFAMILY;
8634 					n->object = (Node *) lcons(makeString($6), $4);
8635 					n->newname = $9;
8636 					n->missing_ok = false;
8637 					$$ = (Node *)n;
8638 				}
8639 			| ALTER POLICY name ON qualified_name RENAME TO name
8640 				{
8641 					RenameStmt *n = makeNode(RenameStmt);
8642 					n->renameType = OBJECT_POLICY;
8643 					n->relation = $5;
8644 					n->subname = $3;
8645 					n->newname = $8;
8646 					n->missing_ok = false;
8647 					$$ = (Node *)n;
8648 				}
8649 			| ALTER POLICY IF_P EXISTS name ON qualified_name RENAME TO name
8650 				{
8651 					RenameStmt *n = makeNode(RenameStmt);
8652 					n->renameType = OBJECT_POLICY;
8653 					n->relation = $7;
8654 					n->subname = $5;
8655 					n->newname = $10;
8656 					n->missing_ok = true;
8657 					$$ = (Node *)n;
8658 				}
8659 			| ALTER PROCEDURE function_with_argtypes RENAME TO name
8660 				{
8661 					RenameStmt *n = makeNode(RenameStmt);
8662 					n->renameType = OBJECT_PROCEDURE;
8663 					n->object = (Node *) $3;
8664 					n->newname = $6;
8665 					n->missing_ok = false;
8666 					$$ = (Node *)n;
8667 				}
8668 			| ALTER PUBLICATION name RENAME TO name
8669 				{
8670 					RenameStmt *n = makeNode(RenameStmt);
8671 					n->renameType = OBJECT_PUBLICATION;
8672 					n->object = (Node *) makeString($3);
8673 					n->newname = $6;
8674 					n->missing_ok = false;
8675 					$$ = (Node *)n;
8676 				}
8677 			| ALTER ROUTINE function_with_argtypes RENAME TO name
8678 				{
8679 					RenameStmt *n = makeNode(RenameStmt);
8680 					n->renameType = OBJECT_ROUTINE;
8681 					n->object = (Node *) $3;
8682 					n->newname = $6;
8683 					n->missing_ok = false;
8684 					$$ = (Node *)n;
8685 				}
8686 			| ALTER SCHEMA name RENAME TO name
8687 				{
8688 					RenameStmt *n = makeNode(RenameStmt);
8689 					n->renameType = OBJECT_SCHEMA;
8690 					n->subname = $3;
8691 					n->newname = $6;
8692 					n->missing_ok = false;
8693 					$$ = (Node *)n;
8694 				}
8695 			| ALTER SERVER name RENAME TO name
8696 				{
8697 					RenameStmt *n = makeNode(RenameStmt);
8698 					n->renameType = OBJECT_FOREIGN_SERVER;
8699 					n->object = (Node *) makeString($3);
8700 					n->newname = $6;
8701 					n->missing_ok = false;
8702 					$$ = (Node *)n;
8703 				}
8704 			| ALTER SUBSCRIPTION name RENAME TO name
8705 				{
8706 					RenameStmt *n = makeNode(RenameStmt);
8707 					n->renameType = OBJECT_SUBSCRIPTION;
8708 					n->object = (Node *) makeString($3);
8709 					n->newname = $6;
8710 					n->missing_ok = false;
8711 					$$ = (Node *)n;
8712 				}
8713 			| ALTER TABLE relation_expr RENAME TO name
8714 				{
8715 					RenameStmt *n = makeNode(RenameStmt);
8716 					n->renameType = OBJECT_TABLE;
8717 					n->relation = $3;
8718 					n->subname = NULL;
8719 					n->newname = $6;
8720 					n->missing_ok = false;
8721 					$$ = (Node *)n;
8722 				}
8723 			| ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
8724 				{
8725 					RenameStmt *n = makeNode(RenameStmt);
8726 					n->renameType = OBJECT_TABLE;
8727 					n->relation = $5;
8728 					n->subname = NULL;
8729 					n->newname = $8;
8730 					n->missing_ok = true;
8731 					$$ = (Node *)n;
8732 				}
8733 			| ALTER SEQUENCE qualified_name RENAME TO name
8734 				{
8735 					RenameStmt *n = makeNode(RenameStmt);
8736 					n->renameType = OBJECT_SEQUENCE;
8737 					n->relation = $3;
8738 					n->subname = NULL;
8739 					n->newname = $6;
8740 					n->missing_ok = false;
8741 					$$ = (Node *)n;
8742 				}
8743 			| ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
8744 				{
8745 					RenameStmt *n = makeNode(RenameStmt);
8746 					n->renameType = OBJECT_SEQUENCE;
8747 					n->relation = $5;
8748 					n->subname = NULL;
8749 					n->newname = $8;
8750 					n->missing_ok = true;
8751 					$$ = (Node *)n;
8752 				}
8753 			| ALTER VIEW qualified_name RENAME TO name
8754 				{
8755 					RenameStmt *n = makeNode(RenameStmt);
8756 					n->renameType = OBJECT_VIEW;
8757 					n->relation = $3;
8758 					n->subname = NULL;
8759 					n->newname = $6;
8760 					n->missing_ok = false;
8761 					$$ = (Node *)n;
8762 				}
8763 			| ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
8764 				{
8765 					RenameStmt *n = makeNode(RenameStmt);
8766 					n->renameType = OBJECT_VIEW;
8767 					n->relation = $5;
8768 					n->subname = NULL;
8769 					n->newname = $8;
8770 					n->missing_ok = true;
8771 					$$ = (Node *)n;
8772 				}
8773 			| ALTER MATERIALIZED VIEW qualified_name RENAME TO name
8774 				{
8775 					RenameStmt *n = makeNode(RenameStmt);
8776 					n->renameType = OBJECT_MATVIEW;
8777 					n->relation = $4;
8778 					n->subname = NULL;
8779 					n->newname = $7;
8780 					n->missing_ok = false;
8781 					$$ = (Node *)n;
8782 				}
8783 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
8784 				{
8785 					RenameStmt *n = makeNode(RenameStmt);
8786 					n->renameType = OBJECT_MATVIEW;
8787 					n->relation = $6;
8788 					n->subname = NULL;
8789 					n->newname = $9;
8790 					n->missing_ok = true;
8791 					$$ = (Node *)n;
8792 				}
8793 			| ALTER INDEX qualified_name RENAME TO name
8794 				{
8795 					RenameStmt *n = makeNode(RenameStmt);
8796 					n->renameType = OBJECT_INDEX;
8797 					n->relation = $3;
8798 					n->subname = NULL;
8799 					n->newname = $6;
8800 					n->missing_ok = false;
8801 					$$ = (Node *)n;
8802 				}
8803 			| ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
8804 				{
8805 					RenameStmt *n = makeNode(RenameStmt);
8806 					n->renameType = OBJECT_INDEX;
8807 					n->relation = $5;
8808 					n->subname = NULL;
8809 					n->newname = $8;
8810 					n->missing_ok = true;
8811 					$$ = (Node *)n;
8812 				}
8813 			| ALTER FOREIGN TABLE relation_expr RENAME TO name
8814 				{
8815 					RenameStmt *n = makeNode(RenameStmt);
8816 					n->renameType = OBJECT_FOREIGN_TABLE;
8817 					n->relation = $4;
8818 					n->subname = NULL;
8819 					n->newname = $7;
8820 					n->missing_ok = false;
8821 					$$ = (Node *)n;
8822 				}
8823 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
8824 				{
8825 					RenameStmt *n = makeNode(RenameStmt);
8826 					n->renameType = OBJECT_FOREIGN_TABLE;
8827 					n->relation = $6;
8828 					n->subname = NULL;
8829 					n->newname = $9;
8830 					n->missing_ok = true;
8831 					$$ = (Node *)n;
8832 				}
8833 			| ALTER TABLE relation_expr RENAME opt_column name TO name
8834 				{
8835 					RenameStmt *n = makeNode(RenameStmt);
8836 					n->renameType = OBJECT_COLUMN;
8837 					n->relationType = OBJECT_TABLE;
8838 					n->relation = $3;
8839 					n->subname = $6;
8840 					n->newname = $8;
8841 					n->missing_ok = false;
8842 					$$ = (Node *)n;
8843 				}
8844 			| ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8845 				{
8846 					RenameStmt *n = makeNode(RenameStmt);
8847 					n->renameType = OBJECT_COLUMN;
8848 					n->relationType = OBJECT_TABLE;
8849 					n->relation = $5;
8850 					n->subname = $8;
8851 					n->newname = $10;
8852 					n->missing_ok = true;
8853 					$$ = (Node *)n;
8854 				}
8855 			| ALTER VIEW qualified_name RENAME opt_column name TO name
8856 				{
8857 					RenameStmt *n = makeNode(RenameStmt);
8858 					n->renameType = OBJECT_COLUMN;
8859 					n->relationType = OBJECT_VIEW;
8860 					n->relation = $3;
8861 					n->subname = $6;
8862 					n->newname = $8;
8863 					n->missing_ok = false;
8864 					$$ = (Node *)n;
8865 				}
8866 			| ALTER VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
8867 				{
8868 					RenameStmt *n = makeNode(RenameStmt);
8869 					n->renameType = OBJECT_COLUMN;
8870 					n->relationType = OBJECT_VIEW;
8871 					n->relation = $5;
8872 					n->subname = $8;
8873 					n->newname = $10;
8874 					n->missing_ok = true;
8875 					$$ = (Node *)n;
8876 				}
8877 			| ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
8878 				{
8879 					RenameStmt *n = makeNode(RenameStmt);
8880 					n->renameType = OBJECT_COLUMN;
8881 					n->relationType = OBJECT_MATVIEW;
8882 					n->relation = $4;
8883 					n->subname = $7;
8884 					n->newname = $9;
8885 					n->missing_ok = false;
8886 					$$ = (Node *)n;
8887 				}
8888 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
8889 				{
8890 					RenameStmt *n = makeNode(RenameStmt);
8891 					n->renameType = OBJECT_COLUMN;
8892 					n->relationType = OBJECT_MATVIEW;
8893 					n->relation = $6;
8894 					n->subname = $9;
8895 					n->newname = $11;
8896 					n->missing_ok = true;
8897 					$$ = (Node *)n;
8898 				}
8899 			| ALTER TABLE relation_expr RENAME CONSTRAINT name TO name
8900 				{
8901 					RenameStmt *n = makeNode(RenameStmt);
8902 					n->renameType = OBJECT_TABCONSTRAINT;
8903 					n->relation = $3;
8904 					n->subname = $6;
8905 					n->newname = $8;
8906 					n->missing_ok = false;
8907 					$$ = (Node *)n;
8908 				}
8909 			| ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
8910 				{
8911 					RenameStmt *n = makeNode(RenameStmt);
8912 					n->renameType = OBJECT_TABCONSTRAINT;
8913 					n->relation = $5;
8914 					n->subname = $8;
8915 					n->newname = $10;
8916 					n->missing_ok = true;
8917 					$$ = (Node *)n;
8918 				}
8919 			| ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
8920 				{
8921 					RenameStmt *n = makeNode(RenameStmt);
8922 					n->renameType = OBJECT_COLUMN;
8923 					n->relationType = OBJECT_FOREIGN_TABLE;
8924 					n->relation = $4;
8925 					n->subname = $7;
8926 					n->newname = $9;
8927 					n->missing_ok = false;
8928 					$$ = (Node *)n;
8929 				}
8930 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8931 				{
8932 					RenameStmt *n = makeNode(RenameStmt);
8933 					n->renameType = OBJECT_COLUMN;
8934 					n->relationType = OBJECT_FOREIGN_TABLE;
8935 					n->relation = $6;
8936 					n->subname = $9;
8937 					n->newname = $11;
8938 					n->missing_ok = true;
8939 					$$ = (Node *)n;
8940 				}
8941 			| ALTER RULE name ON qualified_name RENAME TO name
8942 				{
8943 					RenameStmt *n = makeNode(RenameStmt);
8944 					n->renameType = OBJECT_RULE;
8945 					n->relation = $5;
8946 					n->subname = $3;
8947 					n->newname = $8;
8948 					n->missing_ok = false;
8949 					$$ = (Node *)n;
8950 				}
8951 			| ALTER TRIGGER name ON qualified_name RENAME TO name
8952 				{
8953 					RenameStmt *n = makeNode(RenameStmt);
8954 					n->renameType = OBJECT_TRIGGER;
8955 					n->relation = $5;
8956 					n->subname = $3;
8957 					n->newname = $8;
8958 					n->missing_ok = false;
8959 					$$ = (Node *)n;
8960 				}
8961 			| ALTER EVENT TRIGGER name RENAME TO name
8962 				{
8963 					RenameStmt *n = makeNode(RenameStmt);
8964 					n->renameType = OBJECT_EVENT_TRIGGER;
8965 					n->object = (Node *) makeString($4);
8966 					n->newname = $7;
8967 					$$ = (Node *)n;
8968 				}
8969 			| ALTER ROLE RoleId RENAME TO RoleId
8970 				{
8971 					RenameStmt *n = makeNode(RenameStmt);
8972 					n->renameType = OBJECT_ROLE;
8973 					n->subname = $3;
8974 					n->newname = $6;
8975 					n->missing_ok = false;
8976 					$$ = (Node *)n;
8977 				}
8978 			| ALTER USER RoleId RENAME TO RoleId
8979 				{
8980 					RenameStmt *n = makeNode(RenameStmt);
8981 					n->renameType = OBJECT_ROLE;
8982 					n->subname = $3;
8983 					n->newname = $6;
8984 					n->missing_ok = false;
8985 					$$ = (Node *)n;
8986 				}
8987 			| ALTER TABLESPACE name RENAME TO name
8988 				{
8989 					RenameStmt *n = makeNode(RenameStmt);
8990 					n->renameType = OBJECT_TABLESPACE;
8991 					n->subname = $3;
8992 					n->newname = $6;
8993 					n->missing_ok = false;
8994 					$$ = (Node *)n;
8995 				}
8996 			| ALTER STATISTICS any_name RENAME TO name
8997 				{
8998 					RenameStmt *n = makeNode(RenameStmt);
8999 					n->renameType = OBJECT_STATISTIC_EXT;
9000 					n->object = (Node *) $3;
9001 					n->newname = $6;
9002 					n->missing_ok = false;
9003 					$$ = (Node *)n;
9004 				}
9005 			| ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
9006 				{
9007 					RenameStmt *n = makeNode(RenameStmt);
9008 					n->renameType = OBJECT_TSPARSER;
9009 					n->object = (Node *) $5;
9010 					n->newname = $8;
9011 					n->missing_ok = false;
9012 					$$ = (Node *)n;
9013 				}
9014 			| ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
9015 				{
9016 					RenameStmt *n = makeNode(RenameStmt);
9017 					n->renameType = OBJECT_TSDICTIONARY;
9018 					n->object = (Node *) $5;
9019 					n->newname = $8;
9020 					n->missing_ok = false;
9021 					$$ = (Node *)n;
9022 				}
9023 			| ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
9024 				{
9025 					RenameStmt *n = makeNode(RenameStmt);
9026 					n->renameType = OBJECT_TSTEMPLATE;
9027 					n->object = (Node *) $5;
9028 					n->newname = $8;
9029 					n->missing_ok = false;
9030 					$$ = (Node *)n;
9031 				}
9032 			| ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
9033 				{
9034 					RenameStmt *n = makeNode(RenameStmt);
9035 					n->renameType = OBJECT_TSCONFIGURATION;
9036 					n->object = (Node *) $5;
9037 					n->newname = $8;
9038 					n->missing_ok = false;
9039 					$$ = (Node *)n;
9040 				}
9041 			| ALTER TYPE_P any_name RENAME TO name
9042 				{
9043 					RenameStmt *n = makeNode(RenameStmt);
9044 					n->renameType = OBJECT_TYPE;
9045 					n->object = (Node *) $3;
9046 					n->newname = $6;
9047 					n->missing_ok = false;
9048 					$$ = (Node *)n;
9049 				}
9050 			| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
9051 				{
9052 					RenameStmt *n = makeNode(RenameStmt);
9053 					n->renameType = OBJECT_ATTRIBUTE;
9054 					n->relationType = OBJECT_TYPE;
9055 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
9056 					n->subname = $6;
9057 					n->newname = $8;
9058 					n->behavior = $9;
9059 					n->missing_ok = false;
9060 					$$ = (Node *)n;
9061 				}
9062 		;
9063 
9064 opt_column: COLUMN
9065 			| /*EMPTY*/
9066 		;
9067 
9068 opt_set_data: SET DATA_P							{ $$ = 1; }
9069 			| /*EMPTY*/								{ $$ = 0; }
9070 		;
9071 
9072 /*****************************************************************************
9073  *
9074  * ALTER THING name DEPENDS ON EXTENSION name
9075  *
9076  *****************************************************************************/
9077 
9078 AlterObjectDependsStmt:
9079 			ALTER FUNCTION function_with_argtypes opt_no DEPENDS ON EXTENSION name
9080 				{
9081 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9082 					n->objectType = OBJECT_FUNCTION;
9083 					n->object = (Node *) $3;
9084 					n->extname = makeString($8);
9085 					n->remove = $4;
9086 					$$ = (Node *)n;
9087 				}
9088 			| ALTER PROCEDURE function_with_argtypes opt_no DEPENDS ON EXTENSION name
9089 				{
9090 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9091 					n->objectType = OBJECT_PROCEDURE;
9092 					n->object = (Node *) $3;
9093 					n->extname = makeString($8);
9094 					n->remove = $4;
9095 					$$ = (Node *)n;
9096 				}
9097 			| ALTER ROUTINE function_with_argtypes opt_no DEPENDS ON EXTENSION name
9098 				{
9099 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9100 					n->objectType = OBJECT_ROUTINE;
9101 					n->object = (Node *) $3;
9102 					n->extname = makeString($8);
9103 					n->remove = $4;
9104 					$$ = (Node *)n;
9105 				}
9106 			| ALTER TRIGGER name ON qualified_name opt_no DEPENDS ON EXTENSION name
9107 				{
9108 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9109 					n->objectType = OBJECT_TRIGGER;
9110 					n->relation = $5;
9111 					n->object = (Node *) list_make1(makeString($3));
9112 					n->extname = makeString($10);
9113 					n->remove = $6;
9114 					$$ = (Node *)n;
9115 				}
9116 			| ALTER MATERIALIZED VIEW qualified_name opt_no DEPENDS ON EXTENSION name
9117 				{
9118 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9119 					n->objectType = OBJECT_MATVIEW;
9120 					n->relation = $4;
9121 					n->extname = makeString($9);
9122 					n->remove = $5;
9123 					$$ = (Node *)n;
9124 				}
9125 			| ALTER INDEX qualified_name opt_no DEPENDS ON EXTENSION name
9126 				{
9127 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9128 					n->objectType = OBJECT_INDEX;
9129 					n->relation = $3;
9130 					n->extname = makeString($8);
9131 					n->remove = $4;
9132 					$$ = (Node *)n;
9133 				}
9134 		;
9135 
9136 opt_no:		NO				{ $$ = true; }
9137 			| /* EMPTY */	{ $$ = false;	}
9138 		;
9139 
9140 /*****************************************************************************
9141  *
9142  * ALTER THING name SET SCHEMA name
9143  *
9144  *****************************************************************************/
9145 
9146 AlterObjectSchemaStmt:
9147 			ALTER AGGREGATE aggregate_with_argtypes SET SCHEMA name
9148 				{
9149 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9150 					n->objectType = OBJECT_AGGREGATE;
9151 					n->object = (Node *) $3;
9152 					n->newschema = $6;
9153 					n->missing_ok = false;
9154 					$$ = (Node *)n;
9155 				}
9156 			| ALTER COLLATION any_name SET SCHEMA name
9157 				{
9158 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9159 					n->objectType = OBJECT_COLLATION;
9160 					n->object = (Node *) $3;
9161 					n->newschema = $6;
9162 					n->missing_ok = false;
9163 					$$ = (Node *)n;
9164 				}
9165 			| ALTER CONVERSION_P any_name SET SCHEMA name
9166 				{
9167 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9168 					n->objectType = OBJECT_CONVERSION;
9169 					n->object = (Node *) $3;
9170 					n->newschema = $6;
9171 					n->missing_ok = false;
9172 					$$ = (Node *)n;
9173 				}
9174 			| ALTER DOMAIN_P any_name SET SCHEMA name
9175 				{
9176 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9177 					n->objectType = OBJECT_DOMAIN;
9178 					n->object = (Node *) $3;
9179 					n->newschema = $6;
9180 					n->missing_ok = false;
9181 					$$ = (Node *)n;
9182 				}
9183 			| ALTER EXTENSION name SET SCHEMA name
9184 				{
9185 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9186 					n->objectType = OBJECT_EXTENSION;
9187 					n->object = (Node *) makeString($3);
9188 					n->newschema = $6;
9189 					n->missing_ok = false;
9190 					$$ = (Node *)n;
9191 				}
9192 			| ALTER FUNCTION function_with_argtypes SET SCHEMA name
9193 				{
9194 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9195 					n->objectType = OBJECT_FUNCTION;
9196 					n->object = (Node *) $3;
9197 					n->newschema = $6;
9198 					n->missing_ok = false;
9199 					$$ = (Node *)n;
9200 				}
9201 			| ALTER OPERATOR operator_with_argtypes SET SCHEMA name
9202 				{
9203 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9204 					n->objectType = OBJECT_OPERATOR;
9205 					n->object = (Node *) $3;
9206 					n->newschema = $6;
9207 					n->missing_ok = false;
9208 					$$ = (Node *)n;
9209 				}
9210 			| ALTER OPERATOR CLASS any_name USING name SET SCHEMA name
9211 				{
9212 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9213 					n->objectType = OBJECT_OPCLASS;
9214 					n->object = (Node *) lcons(makeString($6), $4);
9215 					n->newschema = $9;
9216 					n->missing_ok = false;
9217 					$$ = (Node *)n;
9218 				}
9219 			| ALTER OPERATOR FAMILY any_name USING name SET SCHEMA name
9220 				{
9221 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9222 					n->objectType = OBJECT_OPFAMILY;
9223 					n->object = (Node *) lcons(makeString($6), $4);
9224 					n->newschema = $9;
9225 					n->missing_ok = false;
9226 					$$ = (Node *)n;
9227 				}
9228 			| ALTER PROCEDURE function_with_argtypes SET SCHEMA name
9229 				{
9230 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9231 					n->objectType = OBJECT_PROCEDURE;
9232 					n->object = (Node *) $3;
9233 					n->newschema = $6;
9234 					n->missing_ok = false;
9235 					$$ = (Node *)n;
9236 				}
9237 			| ALTER ROUTINE function_with_argtypes SET SCHEMA name
9238 				{
9239 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9240 					n->objectType = OBJECT_ROUTINE;
9241 					n->object = (Node *) $3;
9242 					n->newschema = $6;
9243 					n->missing_ok = false;
9244 					$$ = (Node *)n;
9245 				}
9246 			| ALTER TABLE relation_expr SET SCHEMA name
9247 				{
9248 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9249 					n->objectType = OBJECT_TABLE;
9250 					n->relation = $3;
9251 					n->newschema = $6;
9252 					n->missing_ok = false;
9253 					$$ = (Node *)n;
9254 				}
9255 			| ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
9256 				{
9257 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9258 					n->objectType = OBJECT_TABLE;
9259 					n->relation = $5;
9260 					n->newschema = $8;
9261 					n->missing_ok = true;
9262 					$$ = (Node *)n;
9263 				}
9264 			| ALTER STATISTICS any_name SET SCHEMA name
9265 				{
9266 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9267 					n->objectType = OBJECT_STATISTIC_EXT;
9268 					n->object = (Node *) $3;
9269 					n->newschema = $6;
9270 					n->missing_ok = false;
9271 					$$ = (Node *)n;
9272 				}
9273 			| ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
9274 				{
9275 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9276 					n->objectType = OBJECT_TSPARSER;
9277 					n->object = (Node *) $5;
9278 					n->newschema = $8;
9279 					n->missing_ok = false;
9280 					$$ = (Node *)n;
9281 				}
9282 			| ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
9283 				{
9284 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9285 					n->objectType = OBJECT_TSDICTIONARY;
9286 					n->object = (Node *) $5;
9287 					n->newschema = $8;
9288 					n->missing_ok = false;
9289 					$$ = (Node *)n;
9290 				}
9291 			| ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
9292 				{
9293 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9294 					n->objectType = OBJECT_TSTEMPLATE;
9295 					n->object = (Node *) $5;
9296 					n->newschema = $8;
9297 					n->missing_ok = false;
9298 					$$ = (Node *)n;
9299 				}
9300 			| ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
9301 				{
9302 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9303 					n->objectType = OBJECT_TSCONFIGURATION;
9304 					n->object = (Node *) $5;
9305 					n->newschema = $8;
9306 					n->missing_ok = false;
9307 					$$ = (Node *)n;
9308 				}
9309 			| ALTER SEQUENCE qualified_name SET SCHEMA name
9310 				{
9311 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9312 					n->objectType = OBJECT_SEQUENCE;
9313 					n->relation = $3;
9314 					n->newschema = $6;
9315 					n->missing_ok = false;
9316 					$$ = (Node *)n;
9317 				}
9318 			| ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
9319 				{
9320 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9321 					n->objectType = OBJECT_SEQUENCE;
9322 					n->relation = $5;
9323 					n->newschema = $8;
9324 					n->missing_ok = true;
9325 					$$ = (Node *)n;
9326 				}
9327 			| ALTER VIEW qualified_name SET SCHEMA name
9328 				{
9329 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9330 					n->objectType = OBJECT_VIEW;
9331 					n->relation = $3;
9332 					n->newschema = $6;
9333 					n->missing_ok = false;
9334 					$$ = (Node *)n;
9335 				}
9336 			| ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
9337 				{
9338 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9339 					n->objectType = OBJECT_VIEW;
9340 					n->relation = $5;
9341 					n->newschema = $8;
9342 					n->missing_ok = true;
9343 					$$ = (Node *)n;
9344 				}
9345 			| ALTER MATERIALIZED VIEW qualified_name SET SCHEMA name
9346 				{
9347 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9348 					n->objectType = OBJECT_MATVIEW;
9349 					n->relation = $4;
9350 					n->newschema = $7;
9351 					n->missing_ok = false;
9352 					$$ = (Node *)n;
9353 				}
9354 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name SET SCHEMA name
9355 				{
9356 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9357 					n->objectType = OBJECT_MATVIEW;
9358 					n->relation = $6;
9359 					n->newschema = $9;
9360 					n->missing_ok = true;
9361 					$$ = (Node *)n;
9362 				}
9363 			| ALTER FOREIGN TABLE relation_expr SET SCHEMA name
9364 				{
9365 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9366 					n->objectType = OBJECT_FOREIGN_TABLE;
9367 					n->relation = $4;
9368 					n->newschema = $7;
9369 					n->missing_ok = false;
9370 					$$ = (Node *)n;
9371 				}
9372 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
9373 				{
9374 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9375 					n->objectType = OBJECT_FOREIGN_TABLE;
9376 					n->relation = $6;
9377 					n->newschema = $9;
9378 					n->missing_ok = true;
9379 					$$ = (Node *)n;
9380 				}
9381 			| ALTER TYPE_P any_name SET SCHEMA name
9382 				{
9383 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9384 					n->objectType = OBJECT_TYPE;
9385 					n->object = (Node *) $3;
9386 					n->newschema = $6;
9387 					n->missing_ok = false;
9388 					$$ = (Node *)n;
9389 				}
9390 		;
9391 
9392 /*****************************************************************************
9393  *
9394  * ALTER OPERATOR name SET define
9395  *
9396  *****************************************************************************/
9397 
9398 AlterOperatorStmt:
9399 			ALTER OPERATOR operator_with_argtypes SET '(' operator_def_list ')'
9400 				{
9401 					AlterOperatorStmt *n = makeNode(AlterOperatorStmt);
9402 					n->opername = $3;
9403 					n->options = $6;
9404 					$$ = (Node *)n;
9405 				}
9406 		;
9407 
9408 operator_def_list:	operator_def_elem								{ $$ = list_make1($1); }
9409 			| operator_def_list ',' operator_def_elem				{ $$ = lappend($1, $3); }
9410 		;
9411 
9412 operator_def_elem: ColLabel '=' NONE
9413 						{ $$ = makeDefElem($1, NULL, @1); }
9414 				   | ColLabel '=' operator_def_arg
9415 						{ $$ = makeDefElem($1, (Node *) $3, @1); }
9416 		;
9417 
9418 /* must be similar enough to def_arg to avoid reduce/reduce conflicts */
9419 operator_def_arg:
9420 			func_type						{ $$ = (Node *)$1; }
9421 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
9422 			| qual_all_Op					{ $$ = (Node *)$1; }
9423 			| NumericOnly					{ $$ = (Node *)$1; }
9424 			| Sconst						{ $$ = (Node *)makeString($1); }
9425 		;
9426 
9427 /*****************************************************************************
9428  *
9429  * ALTER TYPE name SET define
9430  *
9431  * We repurpose ALTER OPERATOR's version of "definition" here
9432  *
9433  *****************************************************************************/
9434 
9435 AlterTypeStmt:
9436 			ALTER TYPE_P any_name SET '(' operator_def_list ')'
9437 				{
9438 					AlterTypeStmt *n = makeNode(AlterTypeStmt);
9439 					n->typeName = $3;
9440 					n->options = $6;
9441 					$$ = (Node *)n;
9442 				}
9443 		;
9444 
9445 /*****************************************************************************
9446  *
9447  * ALTER THING name OWNER TO newname
9448  *
9449  *****************************************************************************/
9450 
9451 AlterOwnerStmt: ALTER AGGREGATE aggregate_with_argtypes OWNER TO RoleSpec
9452 				{
9453 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9454 					n->objectType = OBJECT_AGGREGATE;
9455 					n->object = (Node *) $3;
9456 					n->newowner = $6;
9457 					$$ = (Node *)n;
9458 				}
9459 			| ALTER COLLATION any_name OWNER TO RoleSpec
9460 				{
9461 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9462 					n->objectType = OBJECT_COLLATION;
9463 					n->object = (Node *) $3;
9464 					n->newowner = $6;
9465 					$$ = (Node *)n;
9466 				}
9467 			| ALTER CONVERSION_P any_name OWNER TO RoleSpec
9468 				{
9469 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9470 					n->objectType = OBJECT_CONVERSION;
9471 					n->object = (Node *) $3;
9472 					n->newowner = $6;
9473 					$$ = (Node *)n;
9474 				}
9475 			| ALTER DATABASE name OWNER TO RoleSpec
9476 				{
9477 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9478 					n->objectType = OBJECT_DATABASE;
9479 					n->object = (Node *) makeString($3);
9480 					n->newowner = $6;
9481 					$$ = (Node *)n;
9482 				}
9483 			| ALTER DOMAIN_P any_name OWNER TO RoleSpec
9484 				{
9485 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9486 					n->objectType = OBJECT_DOMAIN;
9487 					n->object = (Node *) $3;
9488 					n->newowner = $6;
9489 					$$ = (Node *)n;
9490 				}
9491 			| ALTER FUNCTION function_with_argtypes OWNER TO RoleSpec
9492 				{
9493 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9494 					n->objectType = OBJECT_FUNCTION;
9495 					n->object = (Node *) $3;
9496 					n->newowner = $6;
9497 					$$ = (Node *)n;
9498 				}
9499 			| ALTER opt_procedural LANGUAGE name OWNER TO RoleSpec
9500 				{
9501 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9502 					n->objectType = OBJECT_LANGUAGE;
9503 					n->object = (Node *) makeString($4);
9504 					n->newowner = $7;
9505 					$$ = (Node *)n;
9506 				}
9507 			| ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleSpec
9508 				{
9509 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9510 					n->objectType = OBJECT_LARGEOBJECT;
9511 					n->object = (Node *) $4;
9512 					n->newowner = $7;
9513 					$$ = (Node *)n;
9514 				}
9515 			| ALTER OPERATOR operator_with_argtypes OWNER TO RoleSpec
9516 				{
9517 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9518 					n->objectType = OBJECT_OPERATOR;
9519 					n->object = (Node *) $3;
9520 					n->newowner = $6;
9521 					$$ = (Node *)n;
9522 				}
9523 			| ALTER OPERATOR CLASS any_name USING name OWNER TO RoleSpec
9524 				{
9525 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9526 					n->objectType = OBJECT_OPCLASS;
9527 					n->object = (Node *) lcons(makeString($6), $4);
9528 					n->newowner = $9;
9529 					$$ = (Node *)n;
9530 				}
9531 			| ALTER OPERATOR FAMILY any_name USING name OWNER TO RoleSpec
9532 				{
9533 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9534 					n->objectType = OBJECT_OPFAMILY;
9535 					n->object = (Node *) lcons(makeString($6), $4);
9536 					n->newowner = $9;
9537 					$$ = (Node *)n;
9538 				}
9539 			| ALTER PROCEDURE function_with_argtypes OWNER TO RoleSpec
9540 				{
9541 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9542 					n->objectType = OBJECT_PROCEDURE;
9543 					n->object = (Node *) $3;
9544 					n->newowner = $6;
9545 					$$ = (Node *)n;
9546 				}
9547 			| ALTER ROUTINE function_with_argtypes OWNER TO RoleSpec
9548 				{
9549 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9550 					n->objectType = OBJECT_ROUTINE;
9551 					n->object = (Node *) $3;
9552 					n->newowner = $6;
9553 					$$ = (Node *)n;
9554 				}
9555 			| ALTER SCHEMA name OWNER TO RoleSpec
9556 				{
9557 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9558 					n->objectType = OBJECT_SCHEMA;
9559 					n->object = (Node *) makeString($3);
9560 					n->newowner = $6;
9561 					$$ = (Node *)n;
9562 				}
9563 			| ALTER TYPE_P any_name OWNER TO RoleSpec
9564 				{
9565 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9566 					n->objectType = OBJECT_TYPE;
9567 					n->object = (Node *) $3;
9568 					n->newowner = $6;
9569 					$$ = (Node *)n;
9570 				}
9571 			| ALTER TABLESPACE name OWNER TO RoleSpec
9572 				{
9573 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9574 					n->objectType = OBJECT_TABLESPACE;
9575 					n->object = (Node *) makeString($3);
9576 					n->newowner = $6;
9577 					$$ = (Node *)n;
9578 				}
9579 			| ALTER STATISTICS any_name OWNER TO RoleSpec
9580 				{
9581 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9582 					n->objectType = OBJECT_STATISTIC_EXT;
9583 					n->object = (Node *) $3;
9584 					n->newowner = $6;
9585 					$$ = (Node *)n;
9586 				}
9587 			| ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleSpec
9588 				{
9589 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9590 					n->objectType = OBJECT_TSDICTIONARY;
9591 					n->object = (Node *) $5;
9592 					n->newowner = $8;
9593 					$$ = (Node *)n;
9594 				}
9595 			| ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleSpec
9596 				{
9597 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9598 					n->objectType = OBJECT_TSCONFIGURATION;
9599 					n->object = (Node *) $5;
9600 					n->newowner = $8;
9601 					$$ = (Node *)n;
9602 				}
9603 			| ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleSpec
9604 				{
9605 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9606 					n->objectType = OBJECT_FDW;
9607 					n->object = (Node *) makeString($5);
9608 					n->newowner = $8;
9609 					$$ = (Node *)n;
9610 				}
9611 			| ALTER SERVER name OWNER TO RoleSpec
9612 				{
9613 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9614 					n->objectType = OBJECT_FOREIGN_SERVER;
9615 					n->object = (Node *) makeString($3);
9616 					n->newowner = $6;
9617 					$$ = (Node *)n;
9618 				}
9619 			| ALTER EVENT TRIGGER name OWNER TO RoleSpec
9620 				{
9621 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9622 					n->objectType = OBJECT_EVENT_TRIGGER;
9623 					n->object = (Node *) makeString($4);
9624 					n->newowner = $7;
9625 					$$ = (Node *)n;
9626 				}
9627 			| ALTER PUBLICATION name OWNER TO RoleSpec
9628 				{
9629 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9630 					n->objectType = OBJECT_PUBLICATION;
9631 					n->object = (Node *) makeString($3);
9632 					n->newowner = $6;
9633 					$$ = (Node *)n;
9634 				}
9635 			| ALTER SUBSCRIPTION name OWNER TO RoleSpec
9636 				{
9637 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9638 					n->objectType = OBJECT_SUBSCRIPTION;
9639 					n->object = (Node *) makeString($3);
9640 					n->newowner = $6;
9641 					$$ = (Node *)n;
9642 				}
9643 		;
9644 
9645 
9646 /*****************************************************************************
9647  *
9648  * CREATE PUBLICATION name [ FOR TABLE ] [ WITH options ]
9649  *
9650  *****************************************************************************/
9651 
9652 CreatePublicationStmt:
9653 			CREATE PUBLICATION name opt_publication_for_tables opt_definition
9654 				{
9655 					CreatePublicationStmt *n = makeNode(CreatePublicationStmt);
9656 					n->pubname = $3;
9657 					n->options = $5;
9658 					if ($4 != NULL)
9659 					{
9660 						/* FOR TABLE */
9661 						if (IsA($4, List))
9662 							n->tables = (List *)$4;
9663 						/* FOR ALL TABLES */
9664 						else
9665 							n->for_all_tables = true;
9666 					}
9667 					$$ = (Node *)n;
9668 				}
9669 		;
9670 
9671 opt_publication_for_tables:
9672 			publication_for_tables					{ $$ = $1; }
9673 			| /* EMPTY */							{ $$ = NULL; }
9674 		;
9675 
9676 publication_for_tables:
9677 			FOR TABLE relation_expr_list
9678 				{
9679 					$$ = (Node *) $3;
9680 				}
9681 			| FOR ALL TABLES
9682 				{
9683 					$$ = (Node *) makeInteger(true);
9684 				}
9685 		;
9686 
9687 
9688 /*****************************************************************************
9689  *
9690  * ALTER PUBLICATION name SET ( options )
9691  *
9692  * ALTER PUBLICATION name ADD TABLE table [, table2]
9693  *
9694  * ALTER PUBLICATION name DROP TABLE table [, table2]
9695  *
9696  * ALTER PUBLICATION name SET TABLE table [, table2]
9697  *
9698  *****************************************************************************/
9699 
9700 AlterPublicationStmt:
9701 			ALTER PUBLICATION name SET definition
9702 				{
9703 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9704 					n->pubname = $3;
9705 					n->options = $5;
9706 					$$ = (Node *)n;
9707 				}
9708 			| ALTER PUBLICATION name ADD_P TABLE relation_expr_list
9709 				{
9710 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9711 					n->pubname = $3;
9712 					n->tables = $6;
9713 					n->tableAction = DEFELEM_ADD;
9714 					$$ = (Node *)n;
9715 				}
9716 			| ALTER PUBLICATION name SET TABLE relation_expr_list
9717 				{
9718 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9719 					n->pubname = $3;
9720 					n->tables = $6;
9721 					n->tableAction = DEFELEM_SET;
9722 					$$ = (Node *)n;
9723 				}
9724 			| ALTER PUBLICATION name DROP TABLE relation_expr_list
9725 				{
9726 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9727 					n->pubname = $3;
9728 					n->tables = $6;
9729 					n->tableAction = DEFELEM_DROP;
9730 					$$ = (Node *)n;
9731 				}
9732 		;
9733 
9734 /*****************************************************************************
9735  *
9736  * CREATE SUBSCRIPTION name ...
9737  *
9738  *****************************************************************************/
9739 
9740 CreateSubscriptionStmt:
9741 			CREATE SUBSCRIPTION name CONNECTION Sconst PUBLICATION name_list opt_definition
9742 				{
9743 					CreateSubscriptionStmt *n =
9744 						makeNode(CreateSubscriptionStmt);
9745 					n->subname = $3;
9746 					n->conninfo = $5;
9747 					n->publication = $7;
9748 					n->options = $8;
9749 					$$ = (Node *)n;
9750 				}
9751 		;
9752 
9753 /*****************************************************************************
9754  *
9755  * ALTER SUBSCRIPTION name ...
9756  *
9757  *****************************************************************************/
9758 
9759 AlterSubscriptionStmt:
9760 			ALTER SUBSCRIPTION name SET definition
9761 				{
9762 					AlterSubscriptionStmt *n =
9763 						makeNode(AlterSubscriptionStmt);
9764 					n->kind = ALTER_SUBSCRIPTION_OPTIONS;
9765 					n->subname = $3;
9766 					n->options = $5;
9767 					$$ = (Node *)n;
9768 				}
9769 			| ALTER SUBSCRIPTION name CONNECTION Sconst
9770 				{
9771 					AlterSubscriptionStmt *n =
9772 						makeNode(AlterSubscriptionStmt);
9773 					n->kind = ALTER_SUBSCRIPTION_CONNECTION;
9774 					n->subname = $3;
9775 					n->conninfo = $5;
9776 					$$ = (Node *)n;
9777 				}
9778 			| ALTER SUBSCRIPTION name REFRESH PUBLICATION opt_definition
9779 				{
9780 					AlterSubscriptionStmt *n =
9781 						makeNode(AlterSubscriptionStmt);
9782 					n->kind = ALTER_SUBSCRIPTION_REFRESH;
9783 					n->subname = $3;
9784 					n->options = $6;
9785 					$$ = (Node *)n;
9786 				}
9787 			| ALTER SUBSCRIPTION name ADD_P PUBLICATION name_list opt_definition
9788 				{
9789 					AlterSubscriptionStmt *n =
9790 						makeNode(AlterSubscriptionStmt);
9791 					n->kind = ALTER_SUBSCRIPTION_ADD_PUBLICATION;
9792 					n->subname = $3;
9793 					n->publication = $6;
9794 					n->options = $7;
9795 					$$ = (Node *)n;
9796 				}
9797 			| ALTER SUBSCRIPTION name DROP PUBLICATION name_list opt_definition
9798 				{
9799 					AlterSubscriptionStmt *n =
9800 						makeNode(AlterSubscriptionStmt);
9801 					n->kind = ALTER_SUBSCRIPTION_DROP_PUBLICATION;
9802 					n->subname = $3;
9803 					n->publication = $6;
9804 					n->options = $7;
9805 					$$ = (Node *)n;
9806 				}
9807 			| ALTER SUBSCRIPTION name SET PUBLICATION name_list opt_definition
9808 				{
9809 					AlterSubscriptionStmt *n =
9810 						makeNode(AlterSubscriptionStmt);
9811 					n->kind = ALTER_SUBSCRIPTION_SET_PUBLICATION;
9812 					n->subname = $3;
9813 					n->publication = $6;
9814 					n->options = $7;
9815 					$$ = (Node *)n;
9816 				}
9817 			| ALTER SUBSCRIPTION name ENABLE_P
9818 				{
9819 					AlterSubscriptionStmt *n =
9820 						makeNode(AlterSubscriptionStmt);
9821 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9822 					n->subname = $3;
9823 					n->options = list_make1(makeDefElem("enabled",
9824 											(Node *)makeInteger(true), @1));
9825 					$$ = (Node *)n;
9826 				}
9827 			| ALTER SUBSCRIPTION name DISABLE_P
9828 				{
9829 					AlterSubscriptionStmt *n =
9830 						makeNode(AlterSubscriptionStmt);
9831 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9832 					n->subname = $3;
9833 					n->options = list_make1(makeDefElem("enabled",
9834 											(Node *)makeInteger(false), @1));
9835 					$$ = (Node *)n;
9836 				}
9837 		;
9838 
9839 /*****************************************************************************
9840  *
9841  * DROP SUBSCRIPTION [ IF EXISTS ] name
9842  *
9843  *****************************************************************************/
9844 
9845 DropSubscriptionStmt: DROP SUBSCRIPTION name opt_drop_behavior
9846 				{
9847 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9848 					n->subname = $3;
9849 					n->missing_ok = false;
9850 					n->behavior = $4;
9851 					$$ = (Node *) n;
9852 				}
9853 				|  DROP SUBSCRIPTION IF_P EXISTS name opt_drop_behavior
9854 				{
9855 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9856 					n->subname = $5;
9857 					n->missing_ok = true;
9858 					n->behavior = $6;
9859 					$$ = (Node *) n;
9860 				}
9861 		;
9862 
9863 /*****************************************************************************
9864  *
9865  *		QUERY:	Define Rewrite Rule
9866  *
9867  *****************************************************************************/
9868 
9869 RuleStmt:	CREATE opt_or_replace RULE name AS
9870 			ON event TO qualified_name where_clause
9871 			DO opt_instead RuleActionList
9872 				{
9873 					RuleStmt *n = makeNode(RuleStmt);
9874 					n->replace = $2;
9875 					n->relation = $9;
9876 					n->rulename = $4;
9877 					n->whereClause = $10;
9878 					n->event = $7;
9879 					n->instead = $12;
9880 					n->actions = $13;
9881 					$$ = (Node *)n;
9882 				}
9883 		;
9884 
9885 RuleActionList:
9886 			NOTHING									{ $$ = NIL; }
9887 			| RuleActionStmt						{ $$ = list_make1($1); }
9888 			| '(' RuleActionMulti ')'				{ $$ = $2; }
9889 		;
9890 
9891 /* the thrashing around here is to discard "empty" statements... */
9892 RuleActionMulti:
9893 			RuleActionMulti ';' RuleActionStmtOrEmpty
9894 				{ if ($3 != NULL)
9895 					$$ = lappend($1, $3);
9896 				  else
9897 					$$ = $1;
9898 				}
9899 			| RuleActionStmtOrEmpty
9900 				{ if ($1 != NULL)
9901 					$$ = list_make1($1);
9902 				  else
9903 					$$ = NIL;
9904 				}
9905 		;
9906 
9907 RuleActionStmt:
9908 			SelectStmt
9909 			| InsertStmt
9910 			| UpdateStmt
9911 			| DeleteStmt
9912 			| NotifyStmt
9913 		;
9914 
9915 RuleActionStmtOrEmpty:
9916 			RuleActionStmt							{ $$ = $1; }
9917 			|	/*EMPTY*/							{ $$ = NULL; }
9918 		;
9919 
9920 event:		SELECT									{ $$ = CMD_SELECT; }
9921 			| UPDATE								{ $$ = CMD_UPDATE; }
9922 			| DELETE_P								{ $$ = CMD_DELETE; }
9923 			| INSERT								{ $$ = CMD_INSERT; }
9924 		 ;
9925 
9926 opt_instead:
9927 			INSTEAD									{ $$ = true; }
9928 			| ALSO									{ $$ = false; }
9929 			| /*EMPTY*/								{ $$ = false; }
9930 		;
9931 
9932 
9933 /*****************************************************************************
9934  *
9935  *		QUERY:
9936  *				NOTIFY <identifier> can appear both in rule bodies and
9937  *				as a query-level command
9938  *
9939  *****************************************************************************/
9940 
9941 NotifyStmt: NOTIFY ColId notify_payload
9942 				{
9943 					NotifyStmt *n = makeNode(NotifyStmt);
9944 					n->conditionname = $2;
9945 					n->payload = $3;
9946 					$$ = (Node *)n;
9947 				}
9948 		;
9949 
9950 notify_payload:
9951 			',' Sconst							{ $$ = $2; }
9952 			| /*EMPTY*/							{ $$ = NULL; }
9953 		;
9954 
9955 ListenStmt: LISTEN ColId
9956 				{
9957 					ListenStmt *n = makeNode(ListenStmt);
9958 					n->conditionname = $2;
9959 					$$ = (Node *)n;
9960 				}
9961 		;
9962 
9963 UnlistenStmt:
9964 			UNLISTEN ColId
9965 				{
9966 					UnlistenStmt *n = makeNode(UnlistenStmt);
9967 					n->conditionname = $2;
9968 					$$ = (Node *)n;
9969 				}
9970 			| UNLISTEN '*'
9971 				{
9972 					UnlistenStmt *n = makeNode(UnlistenStmt);
9973 					n->conditionname = NULL;
9974 					$$ = (Node *)n;
9975 				}
9976 		;
9977 
9978 
9979 /*****************************************************************************
9980  *
9981  *		Transactions:
9982  *
9983  *		BEGIN / COMMIT / ROLLBACK
9984  *		(also older versions END / ABORT)
9985  *
9986  *****************************************************************************/
9987 
9988 TransactionStmt:
9989 			ABORT_P opt_transaction opt_transaction_chain
9990 				{
9991 					TransactionStmt *n = makeNode(TransactionStmt);
9992 					n->kind = TRANS_STMT_ROLLBACK;
9993 					n->options = NIL;
9994 					n->chain = $3;
9995 					$$ = (Node *)n;
9996 				}
9997 			| START TRANSACTION transaction_mode_list_or_empty
9998 				{
9999 					TransactionStmt *n = makeNode(TransactionStmt);
10000 					n->kind = TRANS_STMT_START;
10001 					n->options = $3;
10002 					$$ = (Node *)n;
10003 				}
10004 			| COMMIT opt_transaction opt_transaction_chain
10005 				{
10006 					TransactionStmt *n = makeNode(TransactionStmt);
10007 					n->kind = TRANS_STMT_COMMIT;
10008 					n->options = NIL;
10009 					n->chain = $3;
10010 					$$ = (Node *)n;
10011 				}
10012 			| ROLLBACK opt_transaction opt_transaction_chain
10013 				{
10014 					TransactionStmt *n = makeNode(TransactionStmt);
10015 					n->kind = TRANS_STMT_ROLLBACK;
10016 					n->options = NIL;
10017 					n->chain = $3;
10018 					$$ = (Node *)n;
10019 				}
10020 			| SAVEPOINT ColId
10021 				{
10022 					TransactionStmt *n = makeNode(TransactionStmt);
10023 					n->kind = TRANS_STMT_SAVEPOINT;
10024 					n->savepoint_name = $2;
10025 					$$ = (Node *)n;
10026 				}
10027 			| RELEASE SAVEPOINT ColId
10028 				{
10029 					TransactionStmt *n = makeNode(TransactionStmt);
10030 					n->kind = TRANS_STMT_RELEASE;
10031 					n->savepoint_name = $3;
10032 					$$ = (Node *)n;
10033 				}
10034 			| RELEASE ColId
10035 				{
10036 					TransactionStmt *n = makeNode(TransactionStmt);
10037 					n->kind = TRANS_STMT_RELEASE;
10038 					n->savepoint_name = $2;
10039 					$$ = (Node *)n;
10040 				}
10041 			| ROLLBACK opt_transaction TO SAVEPOINT ColId
10042 				{
10043 					TransactionStmt *n = makeNode(TransactionStmt);
10044 					n->kind = TRANS_STMT_ROLLBACK_TO;
10045 					n->savepoint_name = $5;
10046 					$$ = (Node *)n;
10047 				}
10048 			| ROLLBACK opt_transaction TO ColId
10049 				{
10050 					TransactionStmt *n = makeNode(TransactionStmt);
10051 					n->kind = TRANS_STMT_ROLLBACK_TO;
10052 					n->savepoint_name = $4;
10053 					$$ = (Node *)n;
10054 				}
10055 			| PREPARE TRANSACTION Sconst
10056 				{
10057 					TransactionStmt *n = makeNode(TransactionStmt);
10058 					n->kind = TRANS_STMT_PREPARE;
10059 					n->gid = $3;
10060 					$$ = (Node *)n;
10061 				}
10062 			| COMMIT PREPARED Sconst
10063 				{
10064 					TransactionStmt *n = makeNode(TransactionStmt);
10065 					n->kind = TRANS_STMT_COMMIT_PREPARED;
10066 					n->gid = $3;
10067 					$$ = (Node *)n;
10068 				}
10069 			| ROLLBACK PREPARED Sconst
10070 				{
10071 					TransactionStmt *n = makeNode(TransactionStmt);
10072 					n->kind = TRANS_STMT_ROLLBACK_PREPARED;
10073 					n->gid = $3;
10074 					$$ = (Node *)n;
10075 				}
10076 		;
10077 
10078 TransactionStmtLegacy:
10079 			BEGIN_P opt_transaction transaction_mode_list_or_empty
10080 				{
10081 					TransactionStmt *n = makeNode(TransactionStmt);
10082 					n->kind = TRANS_STMT_BEGIN;
10083 					n->options = $3;
10084 					$$ = (Node *)n;
10085 				}
10086 			| END_P opt_transaction opt_transaction_chain
10087 				{
10088 					TransactionStmt *n = makeNode(TransactionStmt);
10089 					n->kind = TRANS_STMT_COMMIT;
10090 					n->options = NIL;
10091 					n->chain = $3;
10092 					$$ = (Node *)n;
10093 				}
10094 		;
10095 
10096 opt_transaction:	WORK
10097 			| TRANSACTION
10098 			| /*EMPTY*/
10099 		;
10100 
10101 transaction_mode_item:
10102 			ISOLATION LEVEL iso_level
10103 					{ $$ = makeDefElem("transaction_isolation",
10104 									   makeStringConst($3, @3), @1); }
10105 			| READ ONLY
10106 					{ $$ = makeDefElem("transaction_read_only",
10107 									   makeIntConst(true, @1), @1); }
10108 			| READ WRITE
10109 					{ $$ = makeDefElem("transaction_read_only",
10110 									   makeIntConst(false, @1), @1); }
10111 			| DEFERRABLE
10112 					{ $$ = makeDefElem("transaction_deferrable",
10113 									   makeIntConst(true, @1), @1); }
10114 			| NOT DEFERRABLE
10115 					{ $$ = makeDefElem("transaction_deferrable",
10116 									   makeIntConst(false, @1), @1); }
10117 		;
10118 
10119 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
10120 transaction_mode_list:
10121 			transaction_mode_item
10122 					{ $$ = list_make1($1); }
10123 			| transaction_mode_list ',' transaction_mode_item
10124 					{ $$ = lappend($1, $3); }
10125 			| transaction_mode_list transaction_mode_item
10126 					{ $$ = lappend($1, $2); }
10127 		;
10128 
10129 transaction_mode_list_or_empty:
10130 			transaction_mode_list
10131 			| /* EMPTY */
10132 					{ $$ = NIL; }
10133 		;
10134 
10135 opt_transaction_chain:
10136 			AND CHAIN		{ $$ = true; }
10137 			| AND NO CHAIN	{ $$ = false; }
10138 			| /* EMPTY */	{ $$ = false; }
10139 		;
10140 
10141 
10142 /*****************************************************************************
10143  *
10144  *	QUERY:
10145  *		CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
10146  *			AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
10147  *
10148  *****************************************************************************/
10149 
10150 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10151 				AS SelectStmt opt_check_option
10152 				{
10153 					ViewStmt *n = makeNode(ViewStmt);
10154 					n->view = $4;
10155 					n->view->relpersistence = $2;
10156 					n->aliases = $5;
10157 					n->query = $8;
10158 					n->replace = false;
10159 					n->options = $6;
10160 					n->withCheckOption = $9;
10161 					$$ = (Node *) n;
10162 				}
10163 		| CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10164 				AS SelectStmt opt_check_option
10165 				{
10166 					ViewStmt *n = makeNode(ViewStmt);
10167 					n->view = $6;
10168 					n->view->relpersistence = $4;
10169 					n->aliases = $7;
10170 					n->query = $10;
10171 					n->replace = true;
10172 					n->options = $8;
10173 					n->withCheckOption = $11;
10174 					$$ = (Node *) n;
10175 				}
10176 		| CREATE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10177 				AS SelectStmt opt_check_option
10178 				{
10179 					ViewStmt *n = makeNode(ViewStmt);
10180 					n->view = $5;
10181 					n->view->relpersistence = $2;
10182 					n->aliases = $7;
10183 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $11);
10184 					n->replace = false;
10185 					n->options = $9;
10186 					n->withCheckOption = $12;
10187 					if (n->withCheckOption != NO_CHECK_OPTION)
10188 						ereport(ERROR,
10189 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10190 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
10191 								 parser_errposition(@12)));
10192 					$$ = (Node *) n;
10193 				}
10194 		| CREATE OR REPLACE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10195 				AS SelectStmt opt_check_option
10196 				{
10197 					ViewStmt *n = makeNode(ViewStmt);
10198 					n->view = $7;
10199 					n->view->relpersistence = $4;
10200 					n->aliases = $9;
10201 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $13);
10202 					n->replace = true;
10203 					n->options = $11;
10204 					n->withCheckOption = $14;
10205 					if (n->withCheckOption != NO_CHECK_OPTION)
10206 						ereport(ERROR,
10207 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10208 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
10209 								 parser_errposition(@14)));
10210 					$$ = (Node *) n;
10211 				}
10212 		;
10213 
10214 opt_check_option:
10215 		WITH CHECK OPTION				{ $$ = CASCADED_CHECK_OPTION; }
10216 		| WITH CASCADED CHECK OPTION	{ $$ = CASCADED_CHECK_OPTION; }
10217 		| WITH LOCAL CHECK OPTION		{ $$ = LOCAL_CHECK_OPTION; }
10218 		| /* EMPTY */					{ $$ = NO_CHECK_OPTION; }
10219 		;
10220 
10221 /*****************************************************************************
10222  *
10223  *		QUERY:
10224  *				LOAD "filename"
10225  *
10226  *****************************************************************************/
10227 
10228 LoadStmt:	LOAD file_name
10229 				{
10230 					LoadStmt *n = makeNode(LoadStmt);
10231 					n->filename = $2;
10232 					$$ = (Node *)n;
10233 				}
10234 		;
10235 
10236 
10237 /*****************************************************************************
10238  *
10239  *		CREATE DATABASE
10240  *
10241  *****************************************************************************/
10242 
10243 CreatedbStmt:
10244 			CREATE DATABASE name opt_with createdb_opt_list
10245 				{
10246 					CreatedbStmt *n = makeNode(CreatedbStmt);
10247 					n->dbname = $3;
10248 					n->options = $5;
10249 					$$ = (Node *)n;
10250 				}
10251 		;
10252 
10253 createdb_opt_list:
10254 			createdb_opt_items						{ $$ = $1; }
10255 			| /* EMPTY */							{ $$ = NIL; }
10256 		;
10257 
10258 createdb_opt_items:
10259 			createdb_opt_item						{ $$ = list_make1($1); }
10260 			| createdb_opt_items createdb_opt_item	{ $$ = lappend($1, $2); }
10261 		;
10262 
10263 createdb_opt_item:
10264 			createdb_opt_name opt_equal SignedIconst
10265 				{
10266 					$$ = makeDefElem($1, (Node *)makeInteger($3), @1);
10267 				}
10268 			| createdb_opt_name opt_equal opt_boolean_or_string
10269 				{
10270 					$$ = makeDefElem($1, (Node *)makeString($3), @1);
10271 				}
10272 			| createdb_opt_name opt_equal DEFAULT
10273 				{
10274 					$$ = makeDefElem($1, NULL, @1);
10275 				}
10276 		;
10277 
10278 /*
10279  * Ideally we'd use ColId here, but that causes shift/reduce conflicts against
10280  * the ALTER DATABASE SET/RESET syntaxes.  Instead call out specific keywords
10281  * we need, and allow IDENT so that database option names don't have to be
10282  * parser keywords unless they are already keywords for other reasons.
10283  *
10284  * XXX this coding technique is fragile since if someone makes a formerly
10285  * non-keyword option name into a keyword and forgets to add it here, the
10286  * option will silently break.  Best defense is to provide a regression test
10287  * exercising every such option, at least at the syntax level.
10288  */
10289 createdb_opt_name:
10290 			IDENT							{ $$ = $1; }
10291 			| CONNECTION LIMIT				{ $$ = pstrdup("connection_limit"); }
10292 			| ENCODING						{ $$ = pstrdup($1); }
10293 			| LOCATION						{ $$ = pstrdup($1); }
10294 			| OWNER							{ $$ = pstrdup($1); }
10295 			| TABLESPACE					{ $$ = pstrdup($1); }
10296 			| TEMPLATE						{ $$ = pstrdup($1); }
10297 		;
10298 
10299 /*
10300  *	Though the equals sign doesn't match other WITH options, pg_dump uses
10301  *	equals for backward compatibility, and it doesn't seem worth removing it.
10302  */
10303 opt_equal:	'='
10304 			| /*EMPTY*/
10305 		;
10306 
10307 
10308 /*****************************************************************************
10309  *
10310  *		ALTER DATABASE
10311  *
10312  *****************************************************************************/
10313 
10314 AlterDatabaseStmt:
10315 			ALTER DATABASE name WITH createdb_opt_list
10316 				 {
10317 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10318 					n->dbname = $3;
10319 					n->options = $5;
10320 					$$ = (Node *)n;
10321 				 }
10322 			| ALTER DATABASE name createdb_opt_list
10323 				 {
10324 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10325 					n->dbname = $3;
10326 					n->options = $4;
10327 					$$ = (Node *)n;
10328 				 }
10329 			| ALTER DATABASE name SET TABLESPACE name
10330 				 {
10331 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10332 					n->dbname = $3;
10333 					n->options = list_make1(makeDefElem("tablespace",
10334 														(Node *)makeString($6), @6));
10335 					$$ = (Node *)n;
10336 				 }
10337 		;
10338 
10339 AlterDatabaseSetStmt:
10340 			ALTER DATABASE name SetResetClause
10341 				{
10342 					AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
10343 					n->dbname = $3;
10344 					n->setstmt = $4;
10345 					$$ = (Node *)n;
10346 				}
10347 		;
10348 
10349 
10350 /*****************************************************************************
10351  *
10352  *		DROP DATABASE [ IF EXISTS ] dbname [ [ WITH ] ( options ) ]
10353  *
10354  * This is implicitly CASCADE, no need for drop behavior
10355  *****************************************************************************/
10356 
10357 DropdbStmt: DROP DATABASE name
10358 				{
10359 					DropdbStmt *n = makeNode(DropdbStmt);
10360 					n->dbname = $3;
10361 					n->missing_ok = false;
10362 					n->options = NULL;
10363 					$$ = (Node *)n;
10364 				}
10365 			| DROP DATABASE IF_P EXISTS name
10366 				{
10367 					DropdbStmt *n = makeNode(DropdbStmt);
10368 					n->dbname = $5;
10369 					n->missing_ok = true;
10370 					n->options = NULL;
10371 					$$ = (Node *)n;
10372 				}
10373 			| DROP DATABASE name opt_with '(' drop_option_list ')'
10374 				{
10375 					DropdbStmt *n = makeNode(DropdbStmt);
10376 					n->dbname = $3;
10377 					n->missing_ok = false;
10378 					n->options = $6;
10379 					$$ = (Node *)n;
10380 				}
10381 			| DROP DATABASE IF_P EXISTS name opt_with '(' drop_option_list ')'
10382 				{
10383 					DropdbStmt *n = makeNode(DropdbStmt);
10384 					n->dbname = $5;
10385 					n->missing_ok = true;
10386 					n->options = $8;
10387 					$$ = (Node *)n;
10388 				}
10389 		;
10390 
10391 drop_option_list:
10392 			drop_option
10393 				{
10394 					$$ = list_make1((Node *) $1);
10395 				}
10396 			| drop_option_list ',' drop_option
10397 				{
10398 					$$ = lappend($1, (Node *) $3);
10399 				}
10400 		;
10401 
10402 /*
10403  * Currently only the FORCE option is supported, but the syntax is designed
10404  * to be extensible so that we can add more options in the future if required.
10405  */
10406 drop_option:
10407 			FORCE
10408 				{
10409 					$$ = makeDefElem("force", NULL, @1);
10410 				}
10411 		;
10412 
10413 /*****************************************************************************
10414  *
10415  *		ALTER COLLATION
10416  *
10417  *****************************************************************************/
10418 
10419 AlterCollationStmt: ALTER COLLATION any_name REFRESH VERSION_P
10420 				{
10421 					AlterCollationStmt *n = makeNode(AlterCollationStmt);
10422 					n->collname = $3;
10423 					$$ = (Node *)n;
10424 				}
10425 		;
10426 
10427 
10428 /*****************************************************************************
10429  *
10430  *		ALTER SYSTEM
10431  *
10432  * This is used to change configuration parameters persistently.
10433  *****************************************************************************/
10434 
10435 AlterSystemStmt:
10436 			ALTER SYSTEM_P SET generic_set
10437 				{
10438 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
10439 					n->setstmt = $4;
10440 					$$ = (Node *)n;
10441 				}
10442 			| ALTER SYSTEM_P RESET generic_reset
10443 				{
10444 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
10445 					n->setstmt = $4;
10446 					$$ = (Node *)n;
10447 				}
10448 		;
10449 
10450 
10451 /*****************************************************************************
10452  *
10453  * Manipulate a domain
10454  *
10455  *****************************************************************************/
10456 
10457 CreateDomainStmt:
10458 			CREATE DOMAIN_P any_name opt_as Typename ColQualList
10459 				{
10460 					CreateDomainStmt *n = makeNode(CreateDomainStmt);
10461 					n->domainname = $3;
10462 					n->typeName = $5;
10463 					SplitColQualList($6, &n->constraints, &n->collClause,
10464 									 yyscanner);
10465 					$$ = (Node *)n;
10466 				}
10467 		;
10468 
10469 AlterDomainStmt:
10470 			/* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
10471 			ALTER DOMAIN_P any_name alter_column_default
10472 				{
10473 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10474 					n->subtype = 'T';
10475 					n->typeName = $3;
10476 					n->def = $4;
10477 					$$ = (Node *)n;
10478 				}
10479 			/* ALTER DOMAIN <domain> DROP NOT NULL */
10480 			| ALTER DOMAIN_P any_name DROP NOT NULL_P
10481 				{
10482 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10483 					n->subtype = 'N';
10484 					n->typeName = $3;
10485 					$$ = (Node *)n;
10486 				}
10487 			/* ALTER DOMAIN <domain> SET NOT NULL */
10488 			| ALTER DOMAIN_P any_name SET NOT NULL_P
10489 				{
10490 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10491 					n->subtype = 'O';
10492 					n->typeName = $3;
10493 					$$ = (Node *)n;
10494 				}
10495 			/* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
10496 			| ALTER DOMAIN_P any_name ADD_P TableConstraint
10497 				{
10498 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10499 					n->subtype = 'C';
10500 					n->typeName = $3;
10501 					n->def = $5;
10502 					$$ = (Node *)n;
10503 				}
10504 			/* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
10505 			| ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
10506 				{
10507 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10508 					n->subtype = 'X';
10509 					n->typeName = $3;
10510 					n->name = $6;
10511 					n->behavior = $7;
10512 					n->missing_ok = false;
10513 					$$ = (Node *)n;
10514 				}
10515 			/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
10516 			| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
10517 				{
10518 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10519 					n->subtype = 'X';
10520 					n->typeName = $3;
10521 					n->name = $8;
10522 					n->behavior = $9;
10523 					n->missing_ok = true;
10524 					$$ = (Node *)n;
10525 				}
10526 			/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
10527 			| ALTER DOMAIN_P any_name VALIDATE CONSTRAINT name
10528 				{
10529 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10530 					n->subtype = 'V';
10531 					n->typeName = $3;
10532 					n->name = $6;
10533 					$$ = (Node *)n;
10534 				}
10535 			;
10536 
10537 opt_as:		AS
10538 			| /* EMPTY */
10539 		;
10540 
10541 
10542 /*****************************************************************************
10543  *
10544  * Manipulate a text search dictionary or configuration
10545  *
10546  *****************************************************************************/
10547 
10548 AlterTSDictionaryStmt:
10549 			ALTER TEXT_P SEARCH DICTIONARY any_name definition
10550 				{
10551 					AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
10552 					n->dictname = $5;
10553 					n->options = $6;
10554 					$$ = (Node *)n;
10555 				}
10556 		;
10557 
10558 AlterTSConfigurationStmt:
10559 			ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list any_with any_name_list
10560 				{
10561 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10562 					n->kind = ALTER_TSCONFIG_ADD_MAPPING;
10563 					n->cfgname = $5;
10564 					n->tokentype = $9;
10565 					n->dicts = $11;
10566 					n->override = false;
10567 					n->replace = false;
10568 					$$ = (Node*)n;
10569 				}
10570 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list any_with any_name_list
10571 				{
10572 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10573 					n->kind = ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN;
10574 					n->cfgname = $5;
10575 					n->tokentype = $9;
10576 					n->dicts = $11;
10577 					n->override = true;
10578 					n->replace = false;
10579 					$$ = (Node*)n;
10580 				}
10581 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name any_with any_name
10582 				{
10583 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10584 					n->kind = ALTER_TSCONFIG_REPLACE_DICT;
10585 					n->cfgname = $5;
10586 					n->tokentype = NIL;
10587 					n->dicts = list_make2($9,$11);
10588 					n->override = false;
10589 					n->replace = true;
10590 					$$ = (Node*)n;
10591 				}
10592 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name any_with any_name
10593 				{
10594 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10595 					n->kind = ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN;
10596 					n->cfgname = $5;
10597 					n->tokentype = $9;
10598 					n->dicts = list_make2($11,$13);
10599 					n->override = false;
10600 					n->replace = true;
10601 					$$ = (Node*)n;
10602 				}
10603 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
10604 				{
10605 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10606 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10607 					n->cfgname = $5;
10608 					n->tokentype = $9;
10609 					n->missing_ok = false;
10610 					$$ = (Node*)n;
10611 				}
10612 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
10613 				{
10614 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10615 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10616 					n->cfgname = $5;
10617 					n->tokentype = $11;
10618 					n->missing_ok = true;
10619 					$$ = (Node*)n;
10620 				}
10621 		;
10622 
10623 /* Use this if TIME or ORDINALITY after WITH should be taken as an identifier */
10624 any_with:	WITH
10625 			| WITH_LA
10626 		;
10627 
10628 
10629 /*****************************************************************************
10630  *
10631  * Manipulate a conversion
10632  *
10633  *		CREATE [DEFAULT] CONVERSION <conversion_name>
10634  *		FOR <encoding_name> TO <encoding_name> FROM <func_name>
10635  *
10636  *****************************************************************************/
10637 
10638 CreateConversionStmt:
10639 			CREATE opt_default CONVERSION_P any_name FOR Sconst
10640 			TO Sconst FROM any_name
10641 			{
10642 				CreateConversionStmt *n = makeNode(CreateConversionStmt);
10643 				n->conversion_name = $4;
10644 				n->for_encoding_name = $6;
10645 				n->to_encoding_name = $8;
10646 				n->func_name = $10;
10647 				n->def = $2;
10648 				$$ = (Node *)n;
10649 			}
10650 		;
10651 
10652 /*****************************************************************************
10653  *
10654  *		QUERY:
10655  *				CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
10656  *				CLUSTER [ (options) ] <qualified_name> [ USING <index_name> ]
10657  *				CLUSTER [VERBOSE]
10658  *				CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
10659  *
10660  *****************************************************************************/
10661 
10662 ClusterStmt:
10663 			CLUSTER opt_verbose qualified_name cluster_index_specification
10664 				{
10665 					ClusterStmt *n = makeNode(ClusterStmt);
10666 					n->relation = $3;
10667 					n->indexname = $4;
10668 					n->params = NIL;
10669 					if ($2)
10670 						n->params = lappend(n->params, makeDefElem("verbose", NULL, @2));
10671 					$$ = (Node*)n;
10672 				}
10673 
10674 			| CLUSTER '(' utility_option_list ')' qualified_name cluster_index_specification
10675 				{
10676 					ClusterStmt *n = makeNode(ClusterStmt);
10677 					n->relation = $5;
10678 					n->indexname = $6;
10679 					n->params = $3;
10680 					$$ = (Node*)n;
10681 				}
10682 			| CLUSTER opt_verbose
10683 				{
10684 					ClusterStmt *n = makeNode(ClusterStmt);
10685 					n->relation = NULL;
10686 					n->indexname = NULL;
10687 					n->params = NIL;
10688 					if ($2)
10689 						n->params = lappend(n->params, makeDefElem("verbose", NULL, @2));
10690 					$$ = (Node*)n;
10691 				}
10692 			/* kept for pre-8.3 compatibility */
10693 			| CLUSTER opt_verbose name ON qualified_name
10694 				{
10695 					ClusterStmt *n = makeNode(ClusterStmt);
10696 					n->relation = $5;
10697 					n->indexname = $3;
10698 					n->params = NIL;
10699 					if ($2)
10700 						n->params = lappend(n->params, makeDefElem("verbose", NULL, @2));
10701 					$$ = (Node*)n;
10702 				}
10703 		;
10704 
10705 cluster_index_specification:
10706 			USING name				{ $$ = $2; }
10707 			| /*EMPTY*/				{ $$ = NULL; }
10708 		;
10709 
10710 
10711 /*****************************************************************************
10712  *
10713  *		QUERY:
10714  *				VACUUM
10715  *				ANALYZE
10716  *
10717  *****************************************************************************/
10718 
10719 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose opt_analyze opt_vacuum_relation_list
10720 				{
10721 					VacuumStmt *n = makeNode(VacuumStmt);
10722 					n->options = NIL;
10723 					if ($2)
10724 						n->options = lappend(n->options,
10725 											 makeDefElem("full", NULL, @2));
10726 					if ($3)
10727 						n->options = lappend(n->options,
10728 											 makeDefElem("freeze", NULL, @3));
10729 					if ($4)
10730 						n->options = lappend(n->options,
10731 											 makeDefElem("verbose", NULL, @4));
10732 					if ($5)
10733 						n->options = lappend(n->options,
10734 											 makeDefElem("analyze", NULL, @5));
10735 					n->rels = $6;
10736 					n->is_vacuumcmd = true;
10737 					$$ = (Node *)n;
10738 				}
10739 			| VACUUM '(' utility_option_list ')' opt_vacuum_relation_list
10740 				{
10741 					VacuumStmt *n = makeNode(VacuumStmt);
10742 					n->options = $3;
10743 					n->rels = $5;
10744 					n->is_vacuumcmd = true;
10745 					$$ = (Node *) n;
10746 				}
10747 		;
10748 
10749 AnalyzeStmt: analyze_keyword opt_verbose opt_vacuum_relation_list
10750 				{
10751 					VacuumStmt *n = makeNode(VacuumStmt);
10752 					n->options = NIL;
10753 					if ($2)
10754 						n->options = lappend(n->options,
10755 											 makeDefElem("verbose", NULL, @2));
10756 					n->rels = $3;
10757 					n->is_vacuumcmd = false;
10758 					$$ = (Node *)n;
10759 				}
10760 			| analyze_keyword '(' utility_option_list ')' opt_vacuum_relation_list
10761 				{
10762 					VacuumStmt *n = makeNode(VacuumStmt);
10763 					n->options = $3;
10764 					n->rels = $5;
10765 					n->is_vacuumcmd = false;
10766 					$$ = (Node *) n;
10767 				}
10768 		;
10769 
10770 utility_option_list:
10771 			utility_option_elem
10772 				{
10773 					$$ = list_make1($1);
10774 				}
10775 			| utility_option_list ',' utility_option_elem
10776 				{
10777 					$$ = lappend($1, $3);
10778 				}
10779 		;
10780 
10781 analyze_keyword:
10782 			ANALYZE
10783 			| ANALYSE /* British */
10784 		;
10785 
10786 utility_option_elem:
10787 			utility_option_name utility_option_arg
10788 				{
10789 					$$ = makeDefElem($1, $2, @1);
10790 				}
10791 		;
10792 
10793 utility_option_name:
10794 			NonReservedWord							{ $$ = $1; }
10795 			| analyze_keyword						{ $$ = "analyze"; }
10796 		;
10797 
10798 utility_option_arg:
10799 			opt_boolean_or_string					{ $$ = (Node *) makeString($1); }
10800 			| NumericOnly							{ $$ = (Node *) $1; }
10801 			| /* EMPTY */							{ $$ = NULL; }
10802 		;
10803 
10804 opt_analyze:
10805 			analyze_keyword							{ $$ = true; }
10806 			| /*EMPTY*/								{ $$ = false; }
10807 		;
10808 
10809 opt_verbose:
10810 			VERBOSE									{ $$ = true; }
10811 			| /*EMPTY*/								{ $$ = false; }
10812 		;
10813 
10814 opt_full:	FULL									{ $$ = true; }
10815 			| /*EMPTY*/								{ $$ = false; }
10816 		;
10817 
10818 opt_freeze: FREEZE									{ $$ = true; }
10819 			| /*EMPTY*/								{ $$ = false; }
10820 		;
10821 
10822 opt_name_list:
10823 			'(' name_list ')'						{ $$ = $2; }
10824 			| /*EMPTY*/								{ $$ = NIL; }
10825 		;
10826 
10827 vacuum_relation:
10828 			qualified_name opt_name_list
10829 				{
10830 					$$ = (Node *) makeVacuumRelation($1, InvalidOid, $2);
10831 				}
10832 		;
10833 
10834 vacuum_relation_list:
10835 			vacuum_relation
10836 					{ $$ = list_make1($1); }
10837 			| vacuum_relation_list ',' vacuum_relation
10838 					{ $$ = lappend($1, $3); }
10839 		;
10840 
10841 opt_vacuum_relation_list:
10842 			vacuum_relation_list					{ $$ = $1; }
10843 			| /*EMPTY*/								{ $$ = NIL; }
10844 		;
10845 
10846 
10847 /*****************************************************************************
10848  *
10849  *		QUERY:
10850  *				EXPLAIN [ANALYZE] [VERBOSE] query
10851  *				EXPLAIN ( options ) query
10852  *
10853  *****************************************************************************/
10854 
10855 ExplainStmt:
10856 		EXPLAIN ExplainableStmt
10857 				{
10858 					ExplainStmt *n = makeNode(ExplainStmt);
10859 					n->query = $2;
10860 					n->options = NIL;
10861 					$$ = (Node *) n;
10862 				}
10863 		| EXPLAIN analyze_keyword opt_verbose ExplainableStmt
10864 				{
10865 					ExplainStmt *n = makeNode(ExplainStmt);
10866 					n->query = $4;
10867 					n->options = list_make1(makeDefElem("analyze", NULL, @2));
10868 					if ($3)
10869 						n->options = lappend(n->options,
10870 											 makeDefElem("verbose", NULL, @3));
10871 					$$ = (Node *) n;
10872 				}
10873 		| EXPLAIN VERBOSE ExplainableStmt
10874 				{
10875 					ExplainStmt *n = makeNode(ExplainStmt);
10876 					n->query = $3;
10877 					n->options = list_make1(makeDefElem("verbose", NULL, @2));
10878 					$$ = (Node *) n;
10879 				}
10880 		| EXPLAIN '(' utility_option_list ')' ExplainableStmt
10881 				{
10882 					ExplainStmt *n = makeNode(ExplainStmt);
10883 					n->query = $5;
10884 					n->options = $3;
10885 					$$ = (Node *) n;
10886 				}
10887 		;
10888 
10889 ExplainableStmt:
10890 			SelectStmt
10891 			| InsertStmt
10892 			| UpdateStmt
10893 			| DeleteStmt
10894 			| DeclareCursorStmt
10895 			| CreateAsStmt
10896 			| CreateMatViewStmt
10897 			| RefreshMatViewStmt
10898 			| ExecuteStmt					/* by default all are $$=$1 */
10899 		;
10900 
10901 /*****************************************************************************
10902  *
10903  *		QUERY:
10904  *				PREPARE <plan_name> [(args, ...)] AS <query>
10905  *
10906  *****************************************************************************/
10907 
10908 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
10909 				{
10910 					PrepareStmt *n = makeNode(PrepareStmt);
10911 					n->name = $2;
10912 					n->argtypes = $3;
10913 					n->query = $5;
10914 					$$ = (Node *) n;
10915 				}
10916 		;
10917 
10918 prep_type_clause: '(' type_list ')'			{ $$ = $2; }
10919 				| /* EMPTY */				{ $$ = NIL; }
10920 		;
10921 
10922 PreparableStmt:
10923 			SelectStmt
10924 			| InsertStmt
10925 			| UpdateStmt
10926 			| DeleteStmt					/* by default all are $$=$1 */
10927 		;
10928 
10929 /*****************************************************************************
10930  *
10931  * EXECUTE <plan_name> [(params, ...)]
10932  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
10933  *
10934  *****************************************************************************/
10935 
10936 ExecuteStmt: EXECUTE name execute_param_clause
10937 				{
10938 					ExecuteStmt *n = makeNode(ExecuteStmt);
10939 					n->name = $2;
10940 					n->params = $3;
10941 					$$ = (Node *) n;
10942 				}
10943 			| CREATE OptTemp TABLE create_as_target AS
10944 				EXECUTE name execute_param_clause opt_with_data
10945 				{
10946 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10947 					ExecuteStmt *n = makeNode(ExecuteStmt);
10948 					n->name = $7;
10949 					n->params = $8;
10950 					ctas->query = (Node *) n;
10951 					ctas->into = $4;
10952 					ctas->objtype = OBJECT_TABLE;
10953 					ctas->is_select_into = false;
10954 					ctas->if_not_exists = false;
10955 					/* cram additional flags into the IntoClause */
10956 					$4->rel->relpersistence = $2;
10957 					$4->skipData = !($9);
10958 					$$ = (Node *) ctas;
10959 				}
10960 			| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS
10961 				EXECUTE name execute_param_clause opt_with_data
10962 				{
10963 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10964 					ExecuteStmt *n = makeNode(ExecuteStmt);
10965 					n->name = $10;
10966 					n->params = $11;
10967 					ctas->query = (Node *) n;
10968 					ctas->into = $7;
10969 					ctas->objtype = OBJECT_TABLE;
10970 					ctas->is_select_into = false;
10971 					ctas->if_not_exists = true;
10972 					/* cram additional flags into the IntoClause */
10973 					$7->rel->relpersistence = $2;
10974 					$7->skipData = !($12);
10975 					$$ = (Node *) ctas;
10976 				}
10977 		;
10978 
10979 execute_param_clause: '(' expr_list ')'				{ $$ = $2; }
10980 					| /* EMPTY */					{ $$ = NIL; }
10981 					;
10982 
10983 /*****************************************************************************
10984  *
10985  *		QUERY:
10986  *				DEALLOCATE [PREPARE] <plan_name>
10987  *
10988  *****************************************************************************/
10989 
10990 DeallocateStmt: DEALLOCATE name
10991 					{
10992 						DeallocateStmt *n = makeNode(DeallocateStmt);
10993 						n->name = $2;
10994 						$$ = (Node *) n;
10995 					}
10996 				| DEALLOCATE PREPARE name
10997 					{
10998 						DeallocateStmt *n = makeNode(DeallocateStmt);
10999 						n->name = $3;
11000 						$$ = (Node *) n;
11001 					}
11002 				| DEALLOCATE ALL
11003 					{
11004 						DeallocateStmt *n = makeNode(DeallocateStmt);
11005 						n->name = NULL;
11006 						$$ = (Node *) n;
11007 					}
11008 				| DEALLOCATE PREPARE ALL
11009 					{
11010 						DeallocateStmt *n = makeNode(DeallocateStmt);
11011 						n->name = NULL;
11012 						$$ = (Node *) n;
11013 					}
11014 		;
11015 
11016 /*****************************************************************************
11017  *
11018  *		QUERY:
11019  *				INSERT STATEMENTS
11020  *
11021  *****************************************************************************/
11022 
11023 InsertStmt:
11024 			opt_with_clause INSERT INTO insert_target insert_rest
11025 			opt_on_conflict returning_clause
11026 				{
11027 					$5->relation = $4;
11028 					$5->onConflictClause = $6;
11029 					$5->returningList = $7;
11030 					$5->withClause = $1;
11031 					$$ = (Node *) $5;
11032 				}
11033 		;
11034 
11035 /*
11036  * Can't easily make AS optional here, because VALUES in insert_rest would
11037  * have a shift/reduce conflict with VALUES as an optional alias.  We could
11038  * easily allow unreserved_keywords as optional aliases, but that'd be an odd
11039  * divergence from other places.  So just require AS for now.
11040  */
11041 insert_target:
11042 			qualified_name
11043 				{
11044 					$$ = $1;
11045 				}
11046 			| qualified_name AS ColId
11047 				{
11048 					$1->alias = makeAlias($3, NIL);
11049 					$$ = $1;
11050 				}
11051 		;
11052 
11053 insert_rest:
11054 			SelectStmt
11055 				{
11056 					$$ = makeNode(InsertStmt);
11057 					$$->cols = NIL;
11058 					$$->selectStmt = $1;
11059 				}
11060 			| OVERRIDING override_kind VALUE_P SelectStmt
11061 				{
11062 					$$ = makeNode(InsertStmt);
11063 					$$->cols = NIL;
11064 					$$->override = $2;
11065 					$$->selectStmt = $4;
11066 				}
11067 			| '(' insert_column_list ')' SelectStmt
11068 				{
11069 					$$ = makeNode(InsertStmt);
11070 					$$->cols = $2;
11071 					$$->selectStmt = $4;
11072 				}
11073 			| '(' insert_column_list ')' OVERRIDING override_kind VALUE_P SelectStmt
11074 				{
11075 					$$ = makeNode(InsertStmt);
11076 					$$->cols = $2;
11077 					$$->override = $5;
11078 					$$->selectStmt = $7;
11079 				}
11080 			| DEFAULT VALUES
11081 				{
11082 					$$ = makeNode(InsertStmt);
11083 					$$->cols = NIL;
11084 					$$->selectStmt = NULL;
11085 				}
11086 		;
11087 
11088 override_kind:
11089 			USER		{ $$ = OVERRIDING_USER_VALUE; }
11090 			| SYSTEM_P	{ $$ = OVERRIDING_SYSTEM_VALUE; }
11091 		;
11092 
11093 insert_column_list:
11094 			insert_column_item
11095 					{ $$ = list_make1($1); }
11096 			| insert_column_list ',' insert_column_item
11097 					{ $$ = lappend($1, $3); }
11098 		;
11099 
11100 insert_column_item:
11101 			ColId opt_indirection
11102 				{
11103 					$$ = makeNode(ResTarget);
11104 					$$->name = $1;
11105 					$$->indirection = check_indirection($2, yyscanner);
11106 					$$->val = NULL;
11107 					$$->location = @1;
11108 				}
11109 		;
11110 
11111 opt_on_conflict:
11112 			ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list	where_clause
11113 				{
11114 					$$ = makeNode(OnConflictClause);
11115 					$$->action = ONCONFLICT_UPDATE;
11116 					$$->infer = $3;
11117 					$$->targetList = $7;
11118 					$$->whereClause = $8;
11119 					$$->location = @1;
11120 				}
11121 			|
11122 			ON CONFLICT opt_conf_expr DO NOTHING
11123 				{
11124 					$$ = makeNode(OnConflictClause);
11125 					$$->action = ONCONFLICT_NOTHING;
11126 					$$->infer = $3;
11127 					$$->targetList = NIL;
11128 					$$->whereClause = NULL;
11129 					$$->location = @1;
11130 				}
11131 			| /*EMPTY*/
11132 				{
11133 					$$ = NULL;
11134 				}
11135 		;
11136 
11137 opt_conf_expr:
11138 			'(' index_params ')' where_clause
11139 				{
11140 					$$ = makeNode(InferClause);
11141 					$$->indexElems = $2;
11142 					$$->whereClause = $4;
11143 					$$->conname = NULL;
11144 					$$->location = @1;
11145 				}
11146 			|
11147 			ON CONSTRAINT name
11148 				{
11149 					$$ = makeNode(InferClause);
11150 					$$->indexElems = NIL;
11151 					$$->whereClause = NULL;
11152 					$$->conname = $3;
11153 					$$->location = @1;
11154 				}
11155 			| /*EMPTY*/
11156 				{
11157 					$$ = NULL;
11158 				}
11159 		;
11160 
11161 returning_clause:
11162 			RETURNING target_list		{ $$ = $2; }
11163 			| /* EMPTY */				{ $$ = NIL; }
11164 		;
11165 
11166 
11167 /*****************************************************************************
11168  *
11169  *		QUERY:
11170  *				DELETE STATEMENTS
11171  *
11172  *****************************************************************************/
11173 
11174 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
11175 			using_clause where_or_current_clause returning_clause
11176 				{
11177 					DeleteStmt *n = makeNode(DeleteStmt);
11178 					n->relation = $4;
11179 					n->usingClause = $5;
11180 					n->whereClause = $6;
11181 					n->returningList = $7;
11182 					n->withClause = $1;
11183 					$$ = (Node *)n;
11184 				}
11185 		;
11186 
11187 using_clause:
11188 				USING from_list						{ $$ = $2; }
11189 			| /*EMPTY*/								{ $$ = NIL; }
11190 		;
11191 
11192 
11193 /*****************************************************************************
11194  *
11195  *		QUERY:
11196  *				LOCK TABLE
11197  *
11198  *****************************************************************************/
11199 
11200 LockStmt:	LOCK_P opt_table relation_expr_list opt_lock opt_nowait
11201 				{
11202 					LockStmt *n = makeNode(LockStmt);
11203 
11204 					n->relations = $3;
11205 					n->mode = $4;
11206 					n->nowait = $5;
11207 					$$ = (Node *)n;
11208 				}
11209 		;
11210 
11211 opt_lock:	IN_P lock_type MODE				{ $$ = $2; }
11212 			| /*EMPTY*/						{ $$ = AccessExclusiveLock; }
11213 		;
11214 
11215 lock_type:	ACCESS SHARE					{ $$ = AccessShareLock; }
11216 			| ROW SHARE						{ $$ = RowShareLock; }
11217 			| ROW EXCLUSIVE					{ $$ = RowExclusiveLock; }
11218 			| SHARE UPDATE EXCLUSIVE		{ $$ = ShareUpdateExclusiveLock; }
11219 			| SHARE							{ $$ = ShareLock; }
11220 			| SHARE ROW EXCLUSIVE			{ $$ = ShareRowExclusiveLock; }
11221 			| EXCLUSIVE						{ $$ = ExclusiveLock; }
11222 			| ACCESS EXCLUSIVE				{ $$ = AccessExclusiveLock; }
11223 		;
11224 
11225 opt_nowait:	NOWAIT							{ $$ = true; }
11226 			| /*EMPTY*/						{ $$ = false; }
11227 		;
11228 
11229 opt_nowait_or_skip:
11230 			NOWAIT							{ $$ = LockWaitError; }
11231 			| SKIP LOCKED					{ $$ = LockWaitSkip; }
11232 			| /*EMPTY*/						{ $$ = LockWaitBlock; }
11233 		;
11234 
11235 
11236 /*****************************************************************************
11237  *
11238  *		QUERY:
11239  *				UpdateStmt (UPDATE)
11240  *
11241  *****************************************************************************/
11242 
11243 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
11244 			SET set_clause_list
11245 			from_clause
11246 			where_or_current_clause
11247 			returning_clause
11248 				{
11249 					UpdateStmt *n = makeNode(UpdateStmt);
11250 					n->relation = $3;
11251 					n->targetList = $5;
11252 					n->fromClause = $6;
11253 					n->whereClause = $7;
11254 					n->returningList = $8;
11255 					n->withClause = $1;
11256 					$$ = (Node *)n;
11257 				}
11258 		;
11259 set_clause_list:
11260 			set_clause							{ $$ = $1; }
11261 			| set_clause_list ',' set_clause	{ $$ = list_concat($1,$3); }
11262 		;
11263 
11264 set_clause:
11265 			set_target '=' a_expr
11266 				{
11267 					$1->val = (Node *) $3;
11268 					$$ = list_make1($1);
11269 				}
11270 			| '(' set_target_list ')' '=' a_expr
11271 				{
11272 					int ncolumns = list_length($2);
11273 					int i = 1;
11274 					ListCell *col_cell;
11275 
11276 					/* Create a MultiAssignRef source for each target */
foreach(col_cell,$2)11277 					foreach(col_cell, $2)
11278 					{
11279 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
11280 						MultiAssignRef *r = makeNode(MultiAssignRef);
11281 
11282 						r->source = (Node *) $5;
11283 						r->colno = i;
11284 						r->ncolumns = ncolumns;
11285 						res_col->val = (Node *) r;
11286 						i++;
11287 					}
11288 
11289 					$$ = $2;
11290 				}
11291 		;
11292 
11293 set_target:
11294 			ColId opt_indirection
11295 				{
11296 					$$ = makeNode(ResTarget);
11297 					$$->name = $1;
11298 					$$->indirection = check_indirection($2, yyscanner);
11299 					$$->val = NULL;	/* upper production sets this */
11300 					$$->location = @1;
11301 				}
11302 		;
11303 
11304 set_target_list:
11305 			set_target								{ $$ = list_make1($1); }
11306 			| set_target_list ',' set_target		{ $$ = lappend($1,$3); }
11307 		;
11308 
11309 
11310 /*****************************************************************************
11311  *
11312  *		QUERY:
11313  *				CURSOR STATEMENTS
11314  *
11315  *****************************************************************************/
11316 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
11317 				{
11318 					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
11319 					n->portalname = $2;
11320 					/* currently we always set FAST_PLAN option */
11321 					n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
11322 					n->query = $7;
11323 					$$ = (Node *)n;
11324 				}
11325 		;
11326 
11327 cursor_name:	name						{ $$ = $1; }
11328 		;
11329 
11330 cursor_options: /*EMPTY*/					{ $$ = 0; }
11331 			| cursor_options NO SCROLL		{ $$ = $1 | CURSOR_OPT_NO_SCROLL; }
11332 			| cursor_options SCROLL			{ $$ = $1 | CURSOR_OPT_SCROLL; }
11333 			| cursor_options BINARY			{ $$ = $1 | CURSOR_OPT_BINARY; }
11334 			| cursor_options ASENSITIVE		{ $$ = $1 | CURSOR_OPT_ASENSITIVE; }
11335 			| cursor_options INSENSITIVE	{ $$ = $1 | CURSOR_OPT_INSENSITIVE; }
11336 		;
11337 
11338 opt_hold: /* EMPTY */						{ $$ = 0; }
11339 			| WITH HOLD						{ $$ = CURSOR_OPT_HOLD; }
11340 			| WITHOUT HOLD					{ $$ = 0; }
11341 		;
11342 
11343 /*****************************************************************************
11344  *
11345  *		QUERY:
11346  *				SELECT STATEMENTS
11347  *
11348  *****************************************************************************/
11349 
11350 /* A complete SELECT statement looks like this.
11351  *
11352  * The rule returns either a single SelectStmt node or a tree of them,
11353  * representing a set-operation tree.
11354  *
11355  * There is an ambiguity when a sub-SELECT is within an a_expr and there
11356  * are excess parentheses: do the parentheses belong to the sub-SELECT or
11357  * to the surrounding a_expr?  We don't really care, but bison wants to know.
11358  * To resolve the ambiguity, we are careful to define the grammar so that
11359  * the decision is staved off as long as possible: as long as we can keep
11360  * absorbing parentheses into the sub-SELECT, we will do so, and only when
11361  * it's no longer possible to do that will we decide that parens belong to
11362  * the expression.	For example, in "SELECT (((SELECT 2)) + 3)" the extra
11363  * parentheses are treated as part of the sub-select.  The necessity of doing
11364  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".	Had we
11365  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
11366  * SELECT viewpoint when we see the UNION.
11367  *
11368  * This approach is implemented by defining a nonterminal select_with_parens,
11369  * which represents a SELECT with at least one outer layer of parentheses,
11370  * and being careful to use select_with_parens, never '(' SelectStmt ')',
11371  * in the expression grammar.  We will then have shift-reduce conflicts
11372  * which we can resolve in favor of always treating '(' <select> ')' as
11373  * a select_with_parens.  To resolve the conflicts, the productions that
11374  * conflict with the select_with_parens productions are manually given
11375  * precedences lower than the precedence of ')', thereby ensuring that we
11376  * shift ')' (and then reduce to select_with_parens) rather than trying to
11377  * reduce the inner <select> nonterminal to something else.  We use UMINUS
11378  * precedence for this, which is a fairly arbitrary choice.
11379  *
11380  * To be able to define select_with_parens itself without ambiguity, we need
11381  * a nonterminal select_no_parens that represents a SELECT structure with no
11382  * outermost parentheses.  This is a little bit tedious, but it works.
11383  *
11384  * In non-expression contexts, we use SelectStmt which can represent a SELECT
11385  * with or without outer parentheses.
11386  */
11387 
11388 SelectStmt: select_no_parens			%prec UMINUS
11389 			| select_with_parens		%prec UMINUS
11390 		;
11391 
11392 select_with_parens:
11393 			'(' select_no_parens ')'				{ $$ = $2; }
11394 			| '(' select_with_parens ')'			{ $$ = $2; }
11395 		;
11396 
11397 /*
11398  * This rule parses the equivalent of the standard's <query expression>.
11399  * The duplicative productions are annoying, but hard to get rid of without
11400  * creating shift/reduce conflicts.
11401  *
11402  *	The locking clause (FOR UPDATE etc) may be before or after LIMIT/OFFSET.
11403  *	In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
11404  *	We now support both orderings, but prefer LIMIT/OFFSET before the locking
11405  * clause.
11406  *	2002-08-28 bjm
11407  */
11408 select_no_parens:
11409 			simple_select						{ $$ = $1; }
11410 			| select_clause sort_clause
11411 				{
11412 					insertSelectOptions((SelectStmt *) $1, $2, NIL,
11413 										NULL, NULL,
11414 										yyscanner);
11415 					$$ = $1;
11416 				}
11417 			| select_clause opt_sort_clause for_locking_clause opt_select_limit
11418 				{
11419 					insertSelectOptions((SelectStmt *) $1, $2, $3,
11420 										$4,
11421 										NULL,
11422 										yyscanner);
11423 					$$ = $1;
11424 				}
11425 			| select_clause opt_sort_clause select_limit opt_for_locking_clause
11426 				{
11427 					insertSelectOptions((SelectStmt *) $1, $2, $4,
11428 										$3,
11429 										NULL,
11430 										yyscanner);
11431 					$$ = $1;
11432 				}
11433 			| with_clause select_clause
11434 				{
11435 					insertSelectOptions((SelectStmt *) $2, NULL, NIL,
11436 										NULL,
11437 										$1,
11438 										yyscanner);
11439 					$$ = $2;
11440 				}
11441 			| with_clause select_clause sort_clause
11442 				{
11443 					insertSelectOptions((SelectStmt *) $2, $3, NIL,
11444 										NULL,
11445 										$1,
11446 										yyscanner);
11447 					$$ = $2;
11448 				}
11449 			| with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
11450 				{
11451 					insertSelectOptions((SelectStmt *) $2, $3, $4,
11452 										$5,
11453 										$1,
11454 										yyscanner);
11455 					$$ = $2;
11456 				}
11457 			| with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
11458 				{
11459 					insertSelectOptions((SelectStmt *) $2, $3, $5,
11460 										$4,
11461 										$1,
11462 										yyscanner);
11463 					$$ = $2;
11464 				}
11465 		;
11466 
11467 select_clause:
11468 			simple_select							{ $$ = $1; }
11469 			| select_with_parens					{ $$ = $1; }
11470 		;
11471 
11472 /*
11473  * This rule parses SELECT statements that can appear within set operations,
11474  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
11475  * the ordering of the set operations.	Without '(' and ')' we want the
11476  * operations to be ordered per the precedence specs at the head of this file.
11477  *
11478  * As with select_no_parens, simple_select cannot have outer parentheses,
11479  * but can have parenthesized subclauses.
11480  *
11481  * It might appear that we could fold the first two alternatives into one
11482  * by using opt_distinct_clause.  However, that causes a shift/reduce conflict
11483  * against INSERT ... SELECT ... ON CONFLICT.  We avoid the ambiguity by
11484  * requiring SELECT DISTINCT [ON] to be followed by a non-empty target_list.
11485  *
11486  * Note that sort clauses cannot be included at this level --- SQL requires
11487  *		SELECT foo UNION SELECT bar ORDER BY baz
11488  * to be parsed as
11489  *		(SELECT foo UNION SELECT bar) ORDER BY baz
11490  * not
11491  *		SELECT foo UNION (SELECT bar ORDER BY baz)
11492  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
11493  * described as part of the select_no_parens production, not simple_select.
11494  * This does not limit functionality, because you can reintroduce these
11495  * clauses inside parentheses.
11496  *
11497  * NOTE: only the leftmost component SelectStmt should have INTO.
11498  * However, this is not checked by the grammar; parse analysis must check it.
11499  */
11500 simple_select:
11501 			SELECT opt_all_clause opt_target_list
11502 			into_clause from_clause where_clause
11503 			group_clause having_clause window_clause
11504 				{
11505 					SelectStmt *n = makeNode(SelectStmt);
11506 					n->targetList = $3;
11507 					n->intoClause = $4;
11508 					n->fromClause = $5;
11509 					n->whereClause = $6;
11510 					n->groupClause = ($7)->list;
11511 					n->groupDistinct = ($7)->distinct;
11512 					n->havingClause = $8;
11513 					n->windowClause = $9;
11514 					$$ = (Node *)n;
11515 				}
11516 			| SELECT distinct_clause target_list
11517 			into_clause from_clause where_clause
11518 			group_clause having_clause window_clause
11519 				{
11520 					SelectStmt *n = makeNode(SelectStmt);
11521 					n->distinctClause = $2;
11522 					n->targetList = $3;
11523 					n->intoClause = $4;
11524 					n->fromClause = $5;
11525 					n->whereClause = $6;
11526 					n->groupClause = ($7)->list;
11527 					n->groupDistinct = ($7)->distinct;
11528 					n->havingClause = $8;
11529 					n->windowClause = $9;
11530 					$$ = (Node *)n;
11531 				}
11532 			| values_clause							{ $$ = $1; }
11533 			| TABLE relation_expr
11534 				{
11535 					/* same as SELECT * FROM relation_expr */
11536 					ColumnRef *cr = makeNode(ColumnRef);
11537 					ResTarget *rt = makeNode(ResTarget);
11538 					SelectStmt *n = makeNode(SelectStmt);
11539 
11540 					cr->fields = list_make1(makeNode(A_Star));
11541 					cr->location = -1;
11542 
11543 					rt->name = NULL;
11544 					rt->indirection = NIL;
11545 					rt->val = (Node *)cr;
11546 					rt->location = -1;
11547 
11548 					n->targetList = list_make1(rt);
11549 					n->fromClause = list_make1($2);
11550 					$$ = (Node *)n;
11551 				}
11552 			| select_clause UNION set_quantifier select_clause
11553 				{
11554 					$$ = makeSetOp(SETOP_UNION, $3 == SET_QUANTIFIER_ALL, $1, $4);
11555 				}
11556 			| select_clause INTERSECT set_quantifier select_clause
11557 				{
11558 					$$ = makeSetOp(SETOP_INTERSECT, $3 == SET_QUANTIFIER_ALL, $1, $4);
11559 				}
11560 			| select_clause EXCEPT set_quantifier select_clause
11561 				{
11562 					$$ = makeSetOp(SETOP_EXCEPT, $3 == SET_QUANTIFIER_ALL, $1, $4);
11563 				}
11564 		;
11565 
11566 /*
11567  * SQL standard WITH clause looks like:
11568  *
11569  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
11570  *		AS (query) [ SEARCH or CYCLE clause ]
11571  *
11572  * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY.
11573  */
11574 with_clause:
11575 		WITH cte_list
11576 			{
11577 				$$ = makeNode(WithClause);
11578 				$$->ctes = $2;
11579 				$$->recursive = false;
11580 				$$->location = @1;
11581 			}
11582 		| WITH_LA cte_list
11583 			{
11584 				$$ = makeNode(WithClause);
11585 				$$->ctes = $2;
11586 				$$->recursive = false;
11587 				$$->location = @1;
11588 			}
11589 		| WITH RECURSIVE cte_list
11590 			{
11591 				$$ = makeNode(WithClause);
11592 				$$->ctes = $3;
11593 				$$->recursive = true;
11594 				$$->location = @1;
11595 			}
11596 		;
11597 
11598 cte_list:
11599 		common_table_expr						{ $$ = list_make1($1); }
11600 		| cte_list ',' common_table_expr		{ $$ = lappend($1, $3); }
11601 		;
11602 
11603 common_table_expr:  name opt_name_list AS opt_materialized '(' PreparableStmt ')' opt_search_clause opt_cycle_clause
11604 			{
11605 				CommonTableExpr *n = makeNode(CommonTableExpr);
11606 				n->ctename = $1;
11607 				n->aliascolnames = $2;
11608 				n->ctematerialized = $4;
11609 				n->ctequery = $6;
11610 				n->search_clause = castNode(CTESearchClause, $8);
11611 				n->cycle_clause = castNode(CTECycleClause, $9);
11612 				n->location = @1;
11613 				$$ = (Node *) n;
11614 			}
11615 		;
11616 
11617 opt_materialized:
11618 		MATERIALIZED							{ $$ = CTEMaterializeAlways; }
11619 		| NOT MATERIALIZED						{ $$ = CTEMaterializeNever; }
11620 		| /*EMPTY*/								{ $$ = CTEMaterializeDefault; }
11621 		;
11622 
11623 opt_search_clause:
11624 		SEARCH DEPTH FIRST_P BY columnList SET ColId
11625 			{
11626 				CTESearchClause *n = makeNode(CTESearchClause);
11627 				n->search_col_list = $5;
11628 				n->search_breadth_first = false;
11629 				n->search_seq_column = $7;
11630 				n->location = @1;
11631 				$$ = (Node *) n;
11632 			}
11633 		| SEARCH BREADTH FIRST_P BY columnList SET ColId
11634 			{
11635 				CTESearchClause *n = makeNode(CTESearchClause);
11636 				n->search_col_list = $5;
11637 				n->search_breadth_first = true;
11638 				n->search_seq_column = $7;
11639 				n->location = @1;
11640 				$$ = (Node *) n;
11641 			}
11642 		| /*EMPTY*/
11643 			{
11644 				$$ = NULL;
11645 			}
11646 		;
11647 
11648 opt_cycle_clause:
11649 		CYCLE columnList SET ColId TO AexprConst DEFAULT AexprConst USING ColId
11650 			{
11651 				CTECycleClause *n = makeNode(CTECycleClause);
11652 				n->cycle_col_list = $2;
11653 				n->cycle_mark_column = $4;
11654 				n->cycle_mark_value = $6;
11655 				n->cycle_mark_default = $8;
11656 				n->cycle_path_column = $10;
11657 				n->location = @1;
11658 				$$ = (Node *) n;
11659 			}
11660 		| CYCLE columnList SET ColId USING ColId
11661 			{
11662 				CTECycleClause *n = makeNode(CTECycleClause);
11663 				n->cycle_col_list = $2;
11664 				n->cycle_mark_column = $4;
11665 				n->cycle_mark_value = makeBoolAConst(true, -1);
11666 				n->cycle_mark_default = makeBoolAConst(false, -1);
11667 				n->cycle_path_column = $6;
11668 				n->location = @1;
11669 				$$ = (Node *) n;
11670 			}
11671 		| /*EMPTY*/
11672 			{
11673 				$$ = NULL;
11674 			}
11675 		;
11676 
11677 opt_with_clause:
11678 		with_clause								{ $$ = $1; }
11679 		| /*EMPTY*/								{ $$ = NULL; }
11680 		;
11681 
11682 into_clause:
11683 			INTO OptTempTableName
11684 				{
11685 					$$ = makeNode(IntoClause);
11686 					$$->rel = $2;
11687 					$$->colNames = NIL;
11688 					$$->options = NIL;
11689 					$$->onCommit = ONCOMMIT_NOOP;
11690 					$$->tableSpaceName = NULL;
11691 					$$->viewQuery = NULL;
11692 					$$->skipData = false;
11693 				}
11694 			| /*EMPTY*/
11695 				{ $$ = NULL; }
11696 		;
11697 
11698 /*
11699  * Redundancy here is needed to avoid shift/reduce conflicts,
11700  * since TEMP is not a reserved word.  See also OptTemp.
11701  */
11702 OptTempTableName:
11703 			TEMPORARY opt_table qualified_name
11704 				{
11705 					$$ = $3;
11706 					$$->relpersistence = RELPERSISTENCE_TEMP;
11707 				}
11708 			| TEMP opt_table qualified_name
11709 				{
11710 					$$ = $3;
11711 					$$->relpersistence = RELPERSISTENCE_TEMP;
11712 				}
11713 			| LOCAL TEMPORARY opt_table qualified_name
11714 				{
11715 					$$ = $4;
11716 					$$->relpersistence = RELPERSISTENCE_TEMP;
11717 				}
11718 			| LOCAL TEMP opt_table qualified_name
11719 				{
11720 					$$ = $4;
11721 					$$->relpersistence = RELPERSISTENCE_TEMP;
11722 				}
11723 			| GLOBAL TEMPORARY opt_table qualified_name
11724 				{
11725 					ereport(WARNING,
11726 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11727 							 parser_errposition(@1)));
11728 					$$ = $4;
11729 					$$->relpersistence = RELPERSISTENCE_TEMP;
11730 				}
11731 			| GLOBAL TEMP opt_table qualified_name
11732 				{
11733 					ereport(WARNING,
11734 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11735 							 parser_errposition(@1)));
11736 					$$ = $4;
11737 					$$->relpersistence = RELPERSISTENCE_TEMP;
11738 				}
11739 			| UNLOGGED opt_table qualified_name
11740 				{
11741 					$$ = $3;
11742 					$$->relpersistence = RELPERSISTENCE_UNLOGGED;
11743 				}
11744 			| TABLE qualified_name
11745 				{
11746 					$$ = $2;
11747 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11748 				}
11749 			| qualified_name
11750 				{
11751 					$$ = $1;
11752 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11753 				}
11754 		;
11755 
11756 opt_table:	TABLE
11757 			| /*EMPTY*/
11758 		;
11759 
11760 set_quantifier:
11761 			ALL										{ $$ = SET_QUANTIFIER_ALL; }
11762 			| DISTINCT								{ $$ = SET_QUANTIFIER_DISTINCT; }
11763 			| /*EMPTY*/								{ $$ = SET_QUANTIFIER_DEFAULT; }
11764 		;
11765 
11766 /* We use (NIL) as a placeholder to indicate that all target expressions
11767  * should be placed in the DISTINCT list during parsetree analysis.
11768  */
11769 distinct_clause:
11770 			DISTINCT								{ $$ = list_make1(NIL); }
11771 			| DISTINCT ON '(' expr_list ')'			{ $$ = $4; }
11772 		;
11773 
11774 opt_all_clause:
11775 			ALL
11776 			| /*EMPTY*/
11777 		;
11778 
11779 opt_distinct_clause:
11780 			distinct_clause							{ $$ = $1; }
11781 			| opt_all_clause						{ $$ = NIL; }
11782 		;
11783 
11784 opt_sort_clause:
11785 			sort_clause								{ $$ = $1; }
11786 			| /*EMPTY*/								{ $$ = NIL; }
11787 		;
11788 
11789 sort_clause:
11790 			ORDER BY sortby_list					{ $$ = $3; }
11791 		;
11792 
11793 sortby_list:
11794 			sortby									{ $$ = list_make1($1); }
11795 			| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
11796 		;
11797 
11798 sortby:		a_expr USING qual_all_Op opt_nulls_order
11799 				{
11800 					$$ = makeNode(SortBy);
11801 					$$->node = $1;
11802 					$$->sortby_dir = SORTBY_USING;
11803 					$$->sortby_nulls = $4;
11804 					$$->useOp = $3;
11805 					$$->location = @3;
11806 				}
11807 			| a_expr opt_asc_desc opt_nulls_order
11808 				{
11809 					$$ = makeNode(SortBy);
11810 					$$->node = $1;
11811 					$$->sortby_dir = $2;
11812 					$$->sortby_nulls = $3;
11813 					$$->useOp = NIL;
11814 					$$->location = -1;		/* no operator */
11815 				}
11816 		;
11817 
11818 
11819 select_limit:
11820 			limit_clause offset_clause
11821 				{
11822 					$$ = $1;
11823 					($$)->limitOffset = $2;
11824 				}
11825 			| offset_clause limit_clause
11826 				{
11827 					$$ = $2;
11828 					($$)->limitOffset = $1;
11829 				}
11830 			| limit_clause
11831 				{
11832 					$$ = $1;
11833 				}
11834 			| offset_clause
11835 				{
11836 					SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit));
11837 					n->limitOffset = $1;
11838 					n->limitCount = NULL;
11839 					n->limitOption = LIMIT_OPTION_COUNT;
11840 					$$ = n;
11841 				}
11842 		;
11843 
11844 opt_select_limit:
11845 			select_limit						{ $$ = $1; }
11846 			| /* EMPTY */						{ $$ = NULL; }
11847 		;
11848 
11849 limit_clause:
11850 			LIMIT select_limit_value
11851 				{
11852 					SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit));
11853 					n->limitOffset = NULL;
11854 					n->limitCount = $2;
11855 					n->limitOption = LIMIT_OPTION_COUNT;
11856 					$$ = n;
11857 				}
11858 			| LIMIT select_limit_value ',' select_offset_value
11859 				{
11860 					/* Disabled because it was too confusing, bjm 2002-02-18 */
11861 					ereport(ERROR,
11862 							(errcode(ERRCODE_SYNTAX_ERROR),
11863 							 errmsg("LIMIT #,# syntax is not supported"),
11864 							 errhint("Use separate LIMIT and OFFSET clauses."),
11865 							 parser_errposition(@1)));
11866 				}
11867 			/* SQL:2008 syntax */
11868 			/* to avoid shift/reduce conflicts, handle the optional value with
11869 			 * a separate production rather than an opt_ expression.  The fact
11870 			 * that ONLY is fully reserved means that this way, we defer any
11871 			 * decision about what rule reduces ROW or ROWS to the point where
11872 			 * we can see the ONLY token in the lookahead slot.
11873 			 */
11874 			| FETCH first_or_next select_fetch_first_value row_or_rows ONLY
11875 				{
11876 					SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit));
11877 					n->limitOffset = NULL;
11878 					n->limitCount = $3;
11879 					n->limitOption = LIMIT_OPTION_COUNT;
11880 					$$ = n;
11881 				}
11882 			| FETCH first_or_next select_fetch_first_value row_or_rows WITH TIES
11883 				{
11884 					SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit));
11885 					n->limitOffset = NULL;
11886 					n->limitCount = $3;
11887 					n->limitOption = LIMIT_OPTION_WITH_TIES;
11888 					$$ = n;
11889 				}
11890 			| FETCH first_or_next row_or_rows ONLY
11891 				{
11892 					SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit));
11893 					n->limitOffset = NULL;
11894 					n->limitCount = makeIntConst(1, -1);
11895 					n->limitOption = LIMIT_OPTION_COUNT;
11896 					$$ = n;
11897 				}
11898 			| FETCH first_or_next row_or_rows WITH TIES
11899 				{
11900 					SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit));
11901 					n->limitOffset = NULL;
11902 					n->limitCount = makeIntConst(1, -1);
11903 					n->limitOption = LIMIT_OPTION_WITH_TIES;
11904 					$$ = n;
11905 				}
11906 		;
11907 
11908 offset_clause:
11909 			OFFSET select_offset_value
11910 				{ $$ = $2; }
11911 			/* SQL:2008 syntax */
11912 			| OFFSET select_fetch_first_value row_or_rows
11913 				{ $$ = $2; }
11914 		;
11915 
11916 select_limit_value:
11917 			a_expr									{ $$ = $1; }
11918 			| ALL
11919 				{
11920 					/* LIMIT ALL is represented as a NULL constant */
11921 					$$ = makeNullAConst(@1);
11922 				}
11923 		;
11924 
11925 select_offset_value:
11926 			a_expr									{ $$ = $1; }
11927 		;
11928 
11929 /*
11930  * Allowing full expressions without parentheses causes various parsing
11931  * problems with the trailing ROW/ROWS key words.  SQL spec only calls for
11932  * <simple value specification>, which is either a literal or a parameter (but
11933  * an <SQL parameter reference> could be an identifier, bringing up conflicts
11934  * with ROW/ROWS). We solve this by leveraging the presence of ONLY (see above)
11935  * to determine whether the expression is missing rather than trying to make it
11936  * optional in this rule.
11937  *
11938  * c_expr covers almost all the spec-required cases (and more), but it doesn't
11939  * cover signed numeric literals, which are allowed by the spec. So we include
11940  * those here explicitly. We need FCONST as well as ICONST because values that
11941  * don't fit in the platform's "long", but do fit in bigint, should still be
11942  * accepted here. (This is possible in 64-bit Windows as well as all 32-bit
11943  * builds.)
11944  */
11945 select_fetch_first_value:
11946 			c_expr									{ $$ = $1; }
11947 			| '+' I_or_F_const
11948 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
11949 			| '-' I_or_F_const
11950 				{ $$ = doNegate($2, @1); }
11951 		;
11952 
11953 I_or_F_const:
11954 			Iconst									{ $$ = makeIntConst($1,@1); }
11955 			| FCONST								{ $$ = makeFloatConst($1,@1); }
11956 		;
11957 
11958 /* noise words */
11959 row_or_rows: ROW									{ $$ = 0; }
11960 			| ROWS									{ $$ = 0; }
11961 		;
11962 
11963 first_or_next: FIRST_P								{ $$ = 0; }
11964 			| NEXT									{ $$ = 0; }
11965 		;
11966 
11967 
11968 /*
11969  * This syntax for group_clause tries to follow the spec quite closely.
11970  * However, the spec allows only column references, not expressions,
11971  * which introduces an ambiguity between implicit row constructors
11972  * (a,b) and lists of column references.
11973  *
11974  * We handle this by using the a_expr production for what the spec calls
11975  * <ordinary grouping set>, which in the spec represents either one column
11976  * reference or a parenthesized list of column references. Then, we check the
11977  * top node of the a_expr to see if it's an implicit RowExpr, and if so, just
11978  * grab and use the list, discarding the node. (this is done in parse analysis,
11979  * not here)
11980  *
11981  * (we abuse the row_format field of RowExpr to distinguish implicit and
11982  * explicit row constructors; it's debatable if anyone sanely wants to use them
11983  * in a group clause, but if they have a reason to, we make it possible.)
11984  *
11985  * Each item in the group_clause list is either an expression tree or a
11986  * GroupingSet node of some type.
11987  */
11988 group_clause:
11989 			GROUP_P BY set_quantifier group_by_list
11990 				{
11991 					GroupClause *n = (GroupClause *) palloc(sizeof(GroupClause));
11992 					n->distinct = $3 == SET_QUANTIFIER_DISTINCT;
11993 					n->list = $4;
11994 					$$ = n;
11995 				}
11996 			| /*EMPTY*/
11997 				{
11998 					GroupClause *n = (GroupClause *) palloc(sizeof(GroupClause));
11999 					n->distinct = false;
12000 					n->list = NIL;
12001 					$$ = n;
12002 				}
12003 		;
12004 
12005 group_by_list:
12006 			group_by_item							{ $$ = list_make1($1); }
12007 			| group_by_list ',' group_by_item		{ $$ = lappend($1,$3); }
12008 		;
12009 
12010 group_by_item:
12011 			a_expr									{ $$ = $1; }
12012 			| empty_grouping_set					{ $$ = $1; }
12013 			| cube_clause							{ $$ = $1; }
12014 			| rollup_clause							{ $$ = $1; }
12015 			| grouping_sets_clause					{ $$ = $1; }
12016 		;
12017 
12018 empty_grouping_set:
12019 			'(' ')'
12020 				{
12021 					$$ = (Node *) makeGroupingSet(GROUPING_SET_EMPTY, NIL, @1);
12022 				}
12023 		;
12024 
12025 /*
12026  * These hacks rely on setting precedence of CUBE and ROLLUP below that of '(',
12027  * so that they shift in these rules rather than reducing the conflicting
12028  * unreserved_keyword rule.
12029  */
12030 
12031 rollup_clause:
12032 			ROLLUP '(' expr_list ')'
12033 				{
12034 					$$ = (Node *) makeGroupingSet(GROUPING_SET_ROLLUP, $3, @1);
12035 				}
12036 		;
12037 
12038 cube_clause:
12039 			CUBE '(' expr_list ')'
12040 				{
12041 					$$ = (Node *) makeGroupingSet(GROUPING_SET_CUBE, $3, @1);
12042 				}
12043 		;
12044 
12045 grouping_sets_clause:
12046 			GROUPING SETS '(' group_by_list ')'
12047 				{
12048 					$$ = (Node *) makeGroupingSet(GROUPING_SET_SETS, $4, @1);
12049 				}
12050 		;
12051 
12052 having_clause:
12053 			HAVING a_expr							{ $$ = $2; }
12054 			| /*EMPTY*/								{ $$ = NULL; }
12055 		;
12056 
12057 for_locking_clause:
12058 			for_locking_items						{ $$ = $1; }
12059 			| FOR READ ONLY							{ $$ = NIL; }
12060 		;
12061 
12062 opt_for_locking_clause:
12063 			for_locking_clause						{ $$ = $1; }
12064 			| /* EMPTY */							{ $$ = NIL; }
12065 		;
12066 
12067 for_locking_items:
12068 			for_locking_item						{ $$ = list_make1($1); }
12069 			| for_locking_items for_locking_item	{ $$ = lappend($1, $2); }
12070 		;
12071 
12072 for_locking_item:
12073 			for_locking_strength locked_rels_list opt_nowait_or_skip
12074 				{
12075 					LockingClause *n = makeNode(LockingClause);
12076 					n->lockedRels = $2;
12077 					n->strength = $1;
12078 					n->waitPolicy = $3;
12079 					$$ = (Node *) n;
12080 				}
12081 		;
12082 
12083 for_locking_strength:
12084 			FOR UPDATE							{ $$ = LCS_FORUPDATE; }
12085 			| FOR NO KEY UPDATE					{ $$ = LCS_FORNOKEYUPDATE; }
12086 			| FOR SHARE							{ $$ = LCS_FORSHARE; }
12087 			| FOR KEY SHARE						{ $$ = LCS_FORKEYSHARE; }
12088 		;
12089 
12090 locked_rels_list:
12091 			OF qualified_name_list					{ $$ = $2; }
12092 			| /* EMPTY */							{ $$ = NIL; }
12093 		;
12094 
12095 
12096 /*
12097  * We should allow ROW '(' expr_list ')' too, but that seems to require
12098  * making VALUES a fully reserved word, which will probably break more apps
12099  * than allowing the noise-word is worth.
12100  */
12101 values_clause:
12102 			VALUES '(' expr_list ')'
12103 				{
12104 					SelectStmt *n = makeNode(SelectStmt);
12105 					n->valuesLists = list_make1($3);
12106 					$$ = (Node *) n;
12107 				}
12108 			| values_clause ',' '(' expr_list ')'
12109 				{
12110 					SelectStmt *n = (SelectStmt *) $1;
12111 					n->valuesLists = lappend(n->valuesLists, $4);
12112 					$$ = (Node *) n;
12113 				}
12114 		;
12115 
12116 
12117 /*****************************************************************************
12118  *
12119  *	clauses common to all Optimizable Stmts:
12120  *		from_clause		- allow list of both JOIN expressions and table names
12121  *		where_clause	- qualifications for joins or restrictions
12122  *
12123  *****************************************************************************/
12124 
12125 from_clause:
12126 			FROM from_list							{ $$ = $2; }
12127 			| /*EMPTY*/								{ $$ = NIL; }
12128 		;
12129 
12130 from_list:
12131 			table_ref								{ $$ = list_make1($1); }
12132 			| from_list ',' table_ref				{ $$ = lappend($1, $3); }
12133 		;
12134 
12135 /*
12136  * table_ref is where an alias clause can be attached.
12137  */
12138 table_ref:	relation_expr opt_alias_clause
12139 				{
12140 					$1->alias = $2;
12141 					$$ = (Node *) $1;
12142 				}
12143 			| relation_expr opt_alias_clause tablesample_clause
12144 				{
12145 					RangeTableSample *n = (RangeTableSample *) $3;
12146 					$1->alias = $2;
12147 					/* relation_expr goes inside the RangeTableSample node */
12148 					n->relation = (Node *) $1;
12149 					$$ = (Node *) n;
12150 				}
12151 			| func_table func_alias_clause
12152 				{
12153 					RangeFunction *n = (RangeFunction *) $1;
12154 					n->alias = linitial($2);
12155 					n->coldeflist = lsecond($2);
12156 					$$ = (Node *) n;
12157 				}
12158 			| LATERAL_P func_table func_alias_clause
12159 				{
12160 					RangeFunction *n = (RangeFunction *) $2;
12161 					n->lateral = true;
12162 					n->alias = linitial($3);
12163 					n->coldeflist = lsecond($3);
12164 					$$ = (Node *) n;
12165 				}
12166 			| xmltable opt_alias_clause
12167 				{
12168 					RangeTableFunc *n = (RangeTableFunc *) $1;
12169 					n->alias = $2;
12170 					$$ = (Node *) n;
12171 				}
12172 			| LATERAL_P xmltable opt_alias_clause
12173 				{
12174 					RangeTableFunc *n = (RangeTableFunc *) $2;
12175 					n->lateral = true;
12176 					n->alias = $3;
12177 					$$ = (Node *) n;
12178 				}
12179 			| select_with_parens opt_alias_clause
12180 				{
12181 					RangeSubselect *n = makeNode(RangeSubselect);
12182 					n->lateral = false;
12183 					n->subquery = $1;
12184 					n->alias = $2;
12185 					/*
12186 					 * The SQL spec does not permit a subselect
12187 					 * (<derived_table>) without an alias clause,
12188 					 * so we don't either.  This avoids the problem
12189 					 * of needing to invent a unique refname for it.
12190 					 * That could be surmounted if there's sufficient
12191 					 * popular demand, but for now let's just implement
12192 					 * the spec and see if anyone complains.
12193 					 * However, it does seem like a good idea to emit
12194 					 * an error message that's better than "syntax error".
12195 					 */
12196 					if ($2 == NULL)
12197 					{
12198 						if (IsA($1, SelectStmt) &&
12199 							((SelectStmt *) $1)->valuesLists)
12200 							ereport(ERROR,
12201 									(errcode(ERRCODE_SYNTAX_ERROR),
12202 									 errmsg("VALUES in FROM must have an alias"),
12203 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
12204 									 parser_errposition(@1)));
12205 						else
12206 							ereport(ERROR,
12207 									(errcode(ERRCODE_SYNTAX_ERROR),
12208 									 errmsg("subquery in FROM must have an alias"),
12209 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
12210 									 parser_errposition(@1)));
12211 					}
12212 					$$ = (Node *) n;
12213 				}
12214 			| LATERAL_P select_with_parens opt_alias_clause
12215 				{
12216 					RangeSubselect *n = makeNode(RangeSubselect);
12217 					n->lateral = true;
12218 					n->subquery = $2;
12219 					n->alias = $3;
12220 					/* same comment as above */
12221 					if ($3 == NULL)
12222 					{
12223 						if (IsA($2, SelectStmt) &&
12224 							((SelectStmt *) $2)->valuesLists)
12225 							ereport(ERROR,
12226 									(errcode(ERRCODE_SYNTAX_ERROR),
12227 									 errmsg("VALUES in FROM must have an alias"),
12228 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
12229 									 parser_errposition(@2)));
12230 						else
12231 							ereport(ERROR,
12232 									(errcode(ERRCODE_SYNTAX_ERROR),
12233 									 errmsg("subquery in FROM must have an alias"),
12234 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
12235 									 parser_errposition(@2)));
12236 					}
12237 					$$ = (Node *) n;
12238 				}
12239 			| joined_table
12240 				{
12241 					$$ = (Node *) $1;
12242 				}
12243 			| '(' joined_table ')' alias_clause
12244 				{
12245 					$2->alias = $4;
12246 					$$ = (Node *) $2;
12247 				}
12248 		;
12249 
12250 
12251 /*
12252  * It may seem silly to separate joined_table from table_ref, but there is
12253  * method in SQL's madness: if you don't do it this way you get reduce-
12254  * reduce conflicts, because it's not clear to the parser generator whether
12255  * to expect alias_clause after ')' or not.  For the same reason we must
12256  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
12257  * join_type to expand to empty; if we try it, the parser generator can't
12258  * figure out when to reduce an empty join_type right after table_ref.
12259  *
12260  * Note that a CROSS JOIN is the same as an unqualified
12261  * INNER JOIN, and an INNER JOIN/ON has the same shape
12262  * but a qualification expression to limit membership.
12263  * A NATURAL JOIN implicitly matches column names between
12264  * tables and the shape is determined by which columns are
12265  * in common. We'll collect columns during the later transformations.
12266  */
12267 
12268 joined_table:
12269 			'(' joined_table ')'
12270 				{
12271 					$$ = $2;
12272 				}
12273 			| table_ref CROSS JOIN table_ref
12274 				{
12275 					/* CROSS JOIN is same as unqualified inner join */
12276 					JoinExpr *n = makeNode(JoinExpr);
12277 					n->jointype = JOIN_INNER;
12278 					n->isNatural = false;
12279 					n->larg = $1;
12280 					n->rarg = $4;
12281 					n->usingClause = NIL;
12282 					n->join_using_alias = NULL;
12283 					n->quals = NULL;
12284 					$$ = n;
12285 				}
12286 			| table_ref join_type JOIN table_ref join_qual
12287 				{
12288 					JoinExpr *n = makeNode(JoinExpr);
12289 					n->jointype = $2;
12290 					n->isNatural = false;
12291 					n->larg = $1;
12292 					n->rarg = $4;
12293 					if ($5 != NULL && IsA($5, List))
12294 					{
12295 						 /* USING clause */
12296 						n->usingClause = linitial_node(List, castNode(List, $5));
12297 						n->join_using_alias = lsecond_node(Alias, castNode(List, $5));
12298 					}
12299 					else
12300 					{
12301 						/* ON clause */
12302 						n->quals = $5;
12303 					}
12304 					$$ = n;
12305 				}
12306 			| table_ref JOIN table_ref join_qual
12307 				{
12308 					/* letting join_type reduce to empty doesn't work */
12309 					JoinExpr *n = makeNode(JoinExpr);
12310 					n->jointype = JOIN_INNER;
12311 					n->isNatural = false;
12312 					n->larg = $1;
12313 					n->rarg = $3;
12314 					if ($4 != NULL && IsA($4, List))
12315 					{
12316 						/* USING clause */
12317 						n->usingClause = linitial_node(List, castNode(List, $4));
12318 						n->join_using_alias = lsecond_node(Alias, castNode(List, $4));
12319 					}
12320 					else
12321 					{
12322 						/* ON clause */
12323 						n->quals = $4;
12324 					}
12325 					$$ = n;
12326 				}
12327 			| table_ref NATURAL join_type JOIN table_ref
12328 				{
12329 					JoinExpr *n = makeNode(JoinExpr);
12330 					n->jointype = $3;
12331 					n->isNatural = true;
12332 					n->larg = $1;
12333 					n->rarg = $5;
12334 					n->usingClause = NIL; /* figure out which columns later... */
12335 					n->join_using_alias = NULL;
12336 					n->quals = NULL; /* fill later */
12337 					$$ = n;
12338 				}
12339 			| table_ref NATURAL JOIN table_ref
12340 				{
12341 					/* letting join_type reduce to empty doesn't work */
12342 					JoinExpr *n = makeNode(JoinExpr);
12343 					n->jointype = JOIN_INNER;
12344 					n->isNatural = true;
12345 					n->larg = $1;
12346 					n->rarg = $4;
12347 					n->usingClause = NIL; /* figure out which columns later... */
12348 					n->join_using_alias = NULL;
12349 					n->quals = NULL; /* fill later */
12350 					$$ = n;
12351 				}
12352 		;
12353 
12354 alias_clause:
12355 			AS ColId '(' name_list ')'
12356 				{
12357 					$$ = makeNode(Alias);
12358 					$$->aliasname = $2;
12359 					$$->colnames = $4;
12360 				}
12361 			| AS ColId
12362 				{
12363 					$$ = makeNode(Alias);
12364 					$$->aliasname = $2;
12365 				}
12366 			| ColId '(' name_list ')'
12367 				{
12368 					$$ = makeNode(Alias);
12369 					$$->aliasname = $1;
12370 					$$->colnames = $3;
12371 				}
12372 			| ColId
12373 				{
12374 					$$ = makeNode(Alias);
12375 					$$->aliasname = $1;
12376 				}
12377 		;
12378 
12379 opt_alias_clause: alias_clause						{ $$ = $1; }
12380 			| /*EMPTY*/								{ $$ = NULL; }
12381 		;
12382 
12383 /*
12384  * The alias clause after JOIN ... USING only accepts the AS ColId spelling,
12385  * per SQL standard.  (The grammar could parse the other variants, but they
12386  * don't seem to be useful, and it might lead to parser problems in the
12387  * future.)
12388  */
12389 opt_alias_clause_for_join_using:
12390 			AS ColId
12391 				{
12392 					$$ = makeNode(Alias);
12393 					$$->aliasname = $2;
12394 					/* the column name list will be inserted later */
12395 				}
12396 			| /*EMPTY*/								{ $$ = NULL; }
12397 		;
12398 
12399 /*
12400  * func_alias_clause can include both an Alias and a coldeflist, so we make it
12401  * return a 2-element list that gets disassembled by calling production.
12402  */
12403 func_alias_clause:
12404 			alias_clause
12405 				{
12406 					$$ = list_make2($1, NIL);
12407 				}
12408 			| AS '(' TableFuncElementList ')'
12409 				{
12410 					$$ = list_make2(NULL, $3);
12411 				}
12412 			| AS ColId '(' TableFuncElementList ')'
12413 				{
12414 					Alias *a = makeNode(Alias);
12415 					a->aliasname = $2;
12416 					$$ = list_make2(a, $4);
12417 				}
12418 			| ColId '(' TableFuncElementList ')'
12419 				{
12420 					Alias *a = makeNode(Alias);
12421 					a->aliasname = $1;
12422 					$$ = list_make2(a, $3);
12423 				}
12424 			| /*EMPTY*/
12425 				{
12426 					$$ = list_make2(NULL, NIL);
12427 				}
12428 		;
12429 
12430 join_type:	FULL opt_outer							{ $$ = JOIN_FULL; }
12431 			| LEFT opt_outer						{ $$ = JOIN_LEFT; }
12432 			| RIGHT opt_outer						{ $$ = JOIN_RIGHT; }
12433 			| INNER_P								{ $$ = JOIN_INNER; }
12434 		;
12435 
12436 /* OUTER is just noise... */
12437 opt_outer: OUTER_P
12438 			| /*EMPTY*/
12439 		;
12440 
12441 /* JOIN qualification clauses
12442  * Possibilities are:
12443  *	USING ( column list ) [ AS alias ]
12444  *						  allows only unqualified column names,
12445  *						  which must match between tables.
12446  *	ON expr allows more general qualifications.
12447  *
12448  * We return USING as a two-element List (the first item being a sub-List
12449  * of the common column names, and the second either an Alias item or NULL).
12450  * An ON-expr will not be a List, so it can be told apart that way.
12451  */
12452 
12453 join_qual: USING '(' name_list ')' opt_alias_clause_for_join_using
12454 				{
12455 					$$ = (Node *) list_make2($3, $5);
12456 				}
12457 			| ON a_expr
12458 				{
12459 					$$ = $2;
12460 				}
12461 		;
12462 
12463 
12464 relation_expr:
12465 			qualified_name
12466 				{
12467 					/* inheritance query, implicitly */
12468 					$$ = $1;
12469 					$$->inh = true;
12470 					$$->alias = NULL;
12471 				}
12472 			| qualified_name '*'
12473 				{
12474 					/* inheritance query, explicitly */
12475 					$$ = $1;
12476 					$$->inh = true;
12477 					$$->alias = NULL;
12478 				}
12479 			| ONLY qualified_name
12480 				{
12481 					/* no inheritance */
12482 					$$ = $2;
12483 					$$->inh = false;
12484 					$$->alias = NULL;
12485 				}
12486 			| ONLY '(' qualified_name ')'
12487 				{
12488 					/* no inheritance, SQL99-style syntax */
12489 					$$ = $3;
12490 					$$->inh = false;
12491 					$$->alias = NULL;
12492 				}
12493 		;
12494 
12495 
12496 relation_expr_list:
12497 			relation_expr							{ $$ = list_make1($1); }
12498 			| relation_expr_list ',' relation_expr	{ $$ = lappend($1, $3); }
12499 		;
12500 
12501 
12502 /*
12503  * Given "UPDATE foo set set ...", we have to decide without looking any
12504  * further ahead whether the first "set" is an alias or the UPDATE's SET
12505  * keyword.  Since "set" is allowed as a column name both interpretations
12506  * are feasible.  We resolve the shift/reduce conflict by giving the first
12507  * relation_expr_opt_alias production a higher precedence than the SET token
12508  * has, causing the parser to prefer to reduce, in effect assuming that the
12509  * SET is not an alias.
12510  */
12511 relation_expr_opt_alias: relation_expr					%prec UMINUS
12512 				{
12513 					$$ = $1;
12514 				}
12515 			| relation_expr ColId
12516 				{
12517 					Alias *alias = makeNode(Alias);
12518 					alias->aliasname = $2;
12519 					$1->alias = alias;
12520 					$$ = $1;
12521 				}
12522 			| relation_expr AS ColId
12523 				{
12524 					Alias *alias = makeNode(Alias);
12525 					alias->aliasname = $3;
12526 					$1->alias = alias;
12527 					$$ = $1;
12528 				}
12529 		;
12530 
12531 /*
12532  * TABLESAMPLE decoration in a FROM item
12533  */
12534 tablesample_clause:
12535 			TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause
12536 				{
12537 					RangeTableSample *n = makeNode(RangeTableSample);
12538 					/* n->relation will be filled in later */
12539 					n->method = $2;
12540 					n->args = $4;
12541 					n->repeatable = $6;
12542 					n->location = @2;
12543 					$$ = (Node *) n;
12544 				}
12545 		;
12546 
12547 opt_repeatable_clause:
12548 			REPEATABLE '(' a_expr ')'	{ $$ = (Node *) $3; }
12549 			| /*EMPTY*/					{ $$ = NULL; }
12550 		;
12551 
12552 /*
12553  * func_table represents a function invocation in a FROM list. It can be
12554  * a plain function call, like "foo(...)", or a ROWS FROM expression with
12555  * one or more function calls, "ROWS FROM (foo(...), bar(...))",
12556  * optionally with WITH ORDINALITY attached.
12557  * In the ROWS FROM syntax, a column definition list can be given for each
12558  * function, for example:
12559  *     ROWS FROM (foo() AS (foo_res_a text, foo_res_b text),
12560  *                bar() AS (bar_res_a text, bar_res_b text))
12561  * It's also possible to attach a column definition list to the RangeFunction
12562  * as a whole, but that's handled by the table_ref production.
12563  */
12564 func_table: func_expr_windowless opt_ordinality
12565 				{
12566 					RangeFunction *n = makeNode(RangeFunction);
12567 					n->lateral = false;
12568 					n->ordinality = $2;
12569 					n->is_rowsfrom = false;
12570 					n->functions = list_make1(list_make2($1, NIL));
12571 					/* alias and coldeflist are set by table_ref production */
12572 					$$ = (Node *) n;
12573 				}
12574 			| ROWS FROM '(' rowsfrom_list ')' opt_ordinality
12575 				{
12576 					RangeFunction *n = makeNode(RangeFunction);
12577 					n->lateral = false;
12578 					n->ordinality = $6;
12579 					n->is_rowsfrom = true;
12580 					n->functions = $4;
12581 					/* alias and coldeflist are set by table_ref production */
12582 					$$ = (Node *) n;
12583 				}
12584 		;
12585 
12586 rowsfrom_item: func_expr_windowless opt_col_def_list
12587 				{ $$ = list_make2($1, $2); }
12588 		;
12589 
12590 rowsfrom_list:
12591 			rowsfrom_item						{ $$ = list_make1($1); }
12592 			| rowsfrom_list ',' rowsfrom_item	{ $$ = lappend($1, $3); }
12593 		;
12594 
12595 opt_col_def_list: AS '(' TableFuncElementList ')'	{ $$ = $3; }
12596 			| /*EMPTY*/								{ $$ = NIL; }
12597 		;
12598 
12599 opt_ordinality: WITH_LA ORDINALITY					{ $$ = true; }
12600 			| /*EMPTY*/								{ $$ = false; }
12601 		;
12602 
12603 
12604 where_clause:
12605 			WHERE a_expr							{ $$ = $2; }
12606 			| /*EMPTY*/								{ $$ = NULL; }
12607 		;
12608 
12609 /* variant for UPDATE and DELETE */
12610 where_or_current_clause:
12611 			WHERE a_expr							{ $$ = $2; }
12612 			| WHERE CURRENT_P OF cursor_name
12613 				{
12614 					CurrentOfExpr *n = makeNode(CurrentOfExpr);
12615 					/* cvarno is filled in by parse analysis */
12616 					n->cursor_name = $4;
12617 					n->cursor_param = 0;
12618 					$$ = (Node *) n;
12619 				}
12620 			| /*EMPTY*/								{ $$ = NULL; }
12621 		;
12622 
12623 
12624 OptTableFuncElementList:
12625 			TableFuncElementList				{ $$ = $1; }
12626 			| /*EMPTY*/							{ $$ = NIL; }
12627 		;
12628 
12629 TableFuncElementList:
12630 			TableFuncElement
12631 				{
12632 					$$ = list_make1($1);
12633 				}
12634 			| TableFuncElementList ',' TableFuncElement
12635 				{
12636 					$$ = lappend($1, $3);
12637 				}
12638 		;
12639 
12640 TableFuncElement:	ColId Typename opt_collate_clause
12641 				{
12642 					ColumnDef *n = makeNode(ColumnDef);
12643 					n->colname = $1;
12644 					n->typeName = $2;
12645 					n->inhcount = 0;
12646 					n->is_local = true;
12647 					n->is_not_null = false;
12648 					n->is_from_type = false;
12649 					n->storage = 0;
12650 					n->raw_default = NULL;
12651 					n->cooked_default = NULL;
12652 					n->collClause = (CollateClause *) $3;
12653 					n->collOid = InvalidOid;
12654 					n->constraints = NIL;
12655 					n->location = @1;
12656 					$$ = (Node *)n;
12657 				}
12658 		;
12659 
12660 /*
12661  * XMLTABLE
12662  */
12663 xmltable:
12664 			XMLTABLE '(' c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12665 				{
12666 					RangeTableFunc *n = makeNode(RangeTableFunc);
12667 					n->rowexpr = $3;
12668 					n->docexpr = $4;
12669 					n->columns = $6;
12670 					n->namespaces = NIL;
12671 					n->location = @1;
12672 					$$ = (Node *)n;
12673 				}
12674 			| XMLTABLE '(' XMLNAMESPACES '(' xml_namespace_list ')' ','
12675 				c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12676 				{
12677 					RangeTableFunc *n = makeNode(RangeTableFunc);
12678 					n->rowexpr = $8;
12679 					n->docexpr = $9;
12680 					n->columns = $11;
12681 					n->namespaces = $5;
12682 					n->location = @1;
12683 					$$ = (Node *)n;
12684 				}
12685 		;
12686 
12687 xmltable_column_list: xmltable_column_el					{ $$ = list_make1($1); }
12688 			| xmltable_column_list ',' xmltable_column_el	{ $$ = lappend($1, $3); }
12689 		;
12690 
12691 xmltable_column_el:
12692 			ColId Typename
12693 				{
12694 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12695 
12696 					fc->colname = $1;
12697 					fc->for_ordinality = false;
12698 					fc->typeName = $2;
12699 					fc->is_not_null = false;
12700 					fc->colexpr = NULL;
12701 					fc->coldefexpr = NULL;
12702 					fc->location = @1;
12703 
12704 					$$ = (Node *) fc;
12705 				}
12706 			| ColId Typename xmltable_column_option_list
12707 				{
12708 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12709 					ListCell		   *option;
12710 					bool				nullability_seen = false;
12711 
12712 					fc->colname = $1;
12713 					fc->typeName = $2;
12714 					fc->for_ordinality = false;
12715 					fc->is_not_null = false;
12716 					fc->colexpr = NULL;
12717 					fc->coldefexpr = NULL;
12718 					fc->location = @1;
12719 
foreach(option,$3)12720 					foreach(option, $3)
12721 					{
12722 						DefElem   *defel = (DefElem *) lfirst(option);
12723 
12724 						if (strcmp(defel->defname, "default") == 0)
12725 						{
12726 							if (fc->coldefexpr != NULL)
12727 								ereport(ERROR,
12728 										(errcode(ERRCODE_SYNTAX_ERROR),
12729 										 errmsg("only one DEFAULT value is allowed"),
12730 										 parser_errposition(defel->location)));
12731 							fc->coldefexpr = defel->arg;
12732 						}
12733 						else if (strcmp(defel->defname, "path") == 0)
12734 						{
12735 							if (fc->colexpr != NULL)
12736 								ereport(ERROR,
12737 										(errcode(ERRCODE_SYNTAX_ERROR),
12738 										 errmsg("only one PATH value per column is allowed"),
12739 										 parser_errposition(defel->location)));
12740 							fc->colexpr = defel->arg;
12741 						}
12742 						else if (strcmp(defel->defname, "is_not_null") == 0)
12743 						{
12744 							if (nullability_seen)
12745 								ereport(ERROR,
12746 										(errcode(ERRCODE_SYNTAX_ERROR),
12747 										 errmsg("conflicting or redundant NULL / NOT NULL declarations for column \"%s\"", fc->colname),
12748 										 parser_errposition(defel->location)));
12749 							fc->is_not_null = intVal(defel->arg);
12750 							nullability_seen = true;
12751 						}
12752 						else
12753 						{
12754 							ereport(ERROR,
12755 									(errcode(ERRCODE_SYNTAX_ERROR),
12756 									 errmsg("unrecognized column option \"%s\"",
12757 											defel->defname),
12758 									 parser_errposition(defel->location)));
12759 						}
12760 					}
12761 					$$ = (Node *) fc;
12762 				}
12763 			| ColId FOR ORDINALITY
12764 				{
12765 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12766 
12767 					fc->colname = $1;
12768 					fc->for_ordinality = true;
12769 					/* other fields are ignored, initialized by makeNode */
12770 					fc->location = @1;
12771 
12772 					$$ = (Node *) fc;
12773 				}
12774 		;
12775 
12776 xmltable_column_option_list:
12777 			xmltable_column_option_el
12778 				{ $$ = list_make1($1); }
12779 			| xmltable_column_option_list xmltable_column_option_el
12780 				{ $$ = lappend($1, $2); }
12781 		;
12782 
12783 xmltable_column_option_el:
12784 			IDENT b_expr
12785 				{ $$ = makeDefElem($1, $2, @1); }
12786 			| DEFAULT b_expr
12787 				{ $$ = makeDefElem("default", $2, @1); }
12788 			| NOT NULL_P
12789 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(true), @1); }
12790 			| NULL_P
12791 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(false), @1); }
12792 		;
12793 
12794 xml_namespace_list:
12795 			xml_namespace_el
12796 				{ $$ = list_make1($1); }
12797 			| xml_namespace_list ',' xml_namespace_el
12798 				{ $$ = lappend($1, $3); }
12799 		;
12800 
12801 xml_namespace_el:
12802 			b_expr AS ColLabel
12803 				{
12804 					$$ = makeNode(ResTarget);
12805 					$$->name = $3;
12806 					$$->indirection = NIL;
12807 					$$->val = $1;
12808 					$$->location = @1;
12809 				}
12810 			| DEFAULT b_expr
12811 				{
12812 					$$ = makeNode(ResTarget);
12813 					$$->name = NULL;
12814 					$$->indirection = NIL;
12815 					$$->val = $2;
12816 					$$->location = @1;
12817 				}
12818 		;
12819 
12820 /*****************************************************************************
12821  *
12822  *	Type syntax
12823  *		SQL introduces a large amount of type-specific syntax.
12824  *		Define individual clauses to handle these cases, and use
12825  *		 the generic case to handle regular type-extensible Postgres syntax.
12826  *		- thomas 1997-10-10
12827  *
12828  *****************************************************************************/
12829 
12830 Typename:	SimpleTypename opt_array_bounds
12831 				{
12832 					$$ = $1;
12833 					$$->arrayBounds = $2;
12834 				}
12835 			| SETOF SimpleTypename opt_array_bounds
12836 				{
12837 					$$ = $2;
12838 					$$->arrayBounds = $3;
12839 					$$->setof = true;
12840 				}
12841 			/* SQL standard syntax, currently only one-dimensional */
12842 			| SimpleTypename ARRAY '[' Iconst ']'
12843 				{
12844 					$$ = $1;
12845 					$$->arrayBounds = list_make1(makeInteger($4));
12846 				}
12847 			| SETOF SimpleTypename ARRAY '[' Iconst ']'
12848 				{
12849 					$$ = $2;
12850 					$$->arrayBounds = list_make1(makeInteger($5));
12851 					$$->setof = true;
12852 				}
12853 			| SimpleTypename ARRAY
12854 				{
12855 					$$ = $1;
12856 					$$->arrayBounds = list_make1(makeInteger(-1));
12857 				}
12858 			| SETOF SimpleTypename ARRAY
12859 				{
12860 					$$ = $2;
12861 					$$->arrayBounds = list_make1(makeInteger(-1));
12862 					$$->setof = true;
12863 				}
12864 		;
12865 
12866 opt_array_bounds:
12867 			opt_array_bounds '[' ']'
12868 					{  $$ = lappend($1, makeInteger(-1)); }
12869 			| opt_array_bounds '[' Iconst ']'
12870 					{  $$ = lappend($1, makeInteger($3)); }
12871 			| /*EMPTY*/
12872 					{  $$ = NIL; }
12873 		;
12874 
12875 SimpleTypename:
12876 			GenericType								{ $$ = $1; }
12877 			| Numeric								{ $$ = $1; }
12878 			| Bit									{ $$ = $1; }
12879 			| Character								{ $$ = $1; }
12880 			| ConstDatetime							{ $$ = $1; }
12881 			| ConstInterval opt_interval
12882 				{
12883 					$$ = $1;
12884 					$$->typmods = $2;
12885 				}
12886 			| ConstInterval '(' Iconst ')'
12887 				{
12888 					$$ = $1;
12889 					$$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
12890 											 makeIntConst($3, @3));
12891 				}
12892 		;
12893 
12894 /* We have a separate ConstTypename to allow defaulting fixed-length
12895  * types such as CHAR() and BIT() to an unspecified length.
12896  * SQL9x requires that these default to a length of one, but this
12897  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
12898  * where there is an obvious better choice to make.
12899  * Note that ConstInterval is not included here since it must
12900  * be pushed up higher in the rules to accommodate the postfix
12901  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
12902  * the generic-type-name case in AexprConst to avoid premature
12903  * reduce/reduce conflicts against function names.
12904  */
12905 ConstTypename:
12906 			Numeric									{ $$ = $1; }
12907 			| ConstBit								{ $$ = $1; }
12908 			| ConstCharacter						{ $$ = $1; }
12909 			| ConstDatetime							{ $$ = $1; }
12910 		;
12911 
12912 /*
12913  * GenericType covers all type names that don't have special syntax mandated
12914  * by the standard, including qualified names.  We also allow type modifiers.
12915  * To avoid parsing conflicts against function invocations, the modifiers
12916  * have to be shown as expr_list here, but parse analysis will only accept
12917  * constants for them.
12918  */
12919 GenericType:
12920 			type_function_name opt_type_modifiers
12921 				{
12922 					$$ = makeTypeName($1);
12923 					$$->typmods = $2;
12924 					$$->location = @1;
12925 				}
12926 			| type_function_name attrs opt_type_modifiers
12927 				{
12928 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
12929 					$$->typmods = $3;
12930 					$$->location = @1;
12931 				}
12932 		;
12933 
12934 opt_type_modifiers: '(' expr_list ')'				{ $$ = $2; }
12935 					| /* EMPTY */					{ $$ = NIL; }
12936 		;
12937 
12938 /*
12939  * SQL numeric data types
12940  */
12941 Numeric:	INT_P
12942 				{
12943 					$$ = SystemTypeName("int4");
12944 					$$->location = @1;
12945 				}
12946 			| INTEGER
12947 				{
12948 					$$ = SystemTypeName("int4");
12949 					$$->location = @1;
12950 				}
12951 			| SMALLINT
12952 				{
12953 					$$ = SystemTypeName("int2");
12954 					$$->location = @1;
12955 				}
12956 			| BIGINT
12957 				{
12958 					$$ = SystemTypeName("int8");
12959 					$$->location = @1;
12960 				}
12961 			| REAL
12962 				{
12963 					$$ = SystemTypeName("float4");
12964 					$$->location = @1;
12965 				}
12966 			| FLOAT_P opt_float
12967 				{
12968 					$$ = $2;
12969 					$$->location = @1;
12970 				}
12971 			| DOUBLE_P PRECISION
12972 				{
12973 					$$ = SystemTypeName("float8");
12974 					$$->location = @1;
12975 				}
12976 			| DECIMAL_P opt_type_modifiers
12977 				{
12978 					$$ = SystemTypeName("numeric");
12979 					$$->typmods = $2;
12980 					$$->location = @1;
12981 				}
12982 			| DEC opt_type_modifiers
12983 				{
12984 					$$ = SystemTypeName("numeric");
12985 					$$->typmods = $2;
12986 					$$->location = @1;
12987 				}
12988 			| NUMERIC opt_type_modifiers
12989 				{
12990 					$$ = SystemTypeName("numeric");
12991 					$$->typmods = $2;
12992 					$$->location = @1;
12993 				}
12994 			| BOOLEAN_P
12995 				{
12996 					$$ = SystemTypeName("bool");
12997 					$$->location = @1;
12998 				}
12999 		;
13000 
13001 opt_float:	'(' Iconst ')'
13002 				{
13003 					/*
13004 					 * Check FLOAT() precision limits assuming IEEE floating
13005 					 * types - thomas 1997-09-18
13006 					 */
13007 					if ($2 < 1)
13008 						ereport(ERROR,
13009 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
13010 								 errmsg("precision for type float must be at least 1 bit"),
13011 								 parser_errposition(@2)));
13012 					else if ($2 <= 24)
13013 						$$ = SystemTypeName("float4");
13014 					else if ($2 <= 53)
13015 						$$ = SystemTypeName("float8");
13016 					else
13017 						ereport(ERROR,
13018 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
13019 								 errmsg("precision for type float must be less than 54 bits"),
13020 								 parser_errposition(@2)));
13021 				}
13022 			| /*EMPTY*/
13023 				{
13024 					$$ = SystemTypeName("float8");
13025 				}
13026 		;
13027 
13028 /*
13029  * SQL bit-field data types
13030  * The following implements BIT() and BIT VARYING().
13031  */
13032 Bit:		BitWithLength
13033 				{
13034 					$$ = $1;
13035 				}
13036 			| BitWithoutLength
13037 				{
13038 					$$ = $1;
13039 				}
13040 		;
13041 
13042 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
13043 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
13044 ConstBit:	BitWithLength
13045 				{
13046 					$$ = $1;
13047 				}
13048 			| BitWithoutLength
13049 				{
13050 					$$ = $1;
13051 					$$->typmods = NIL;
13052 				}
13053 		;
13054 
13055 BitWithLength:
13056 			BIT opt_varying '(' expr_list ')'
13057 				{
13058 					char *typname;
13059 
13060 					typname = $2 ? "varbit" : "bit";
13061 					$$ = SystemTypeName(typname);
13062 					$$->typmods = $4;
13063 					$$->location = @1;
13064 				}
13065 		;
13066 
13067 BitWithoutLength:
13068 			BIT opt_varying
13069 				{
13070 					/* bit defaults to bit(1), varbit to no limit */
13071 					if ($2)
13072 					{
13073 						$$ = SystemTypeName("varbit");
13074 					}
13075 					else
13076 					{
13077 						$$ = SystemTypeName("bit");
13078 						$$->typmods = list_make1(makeIntConst(1, -1));
13079 					}
13080 					$$->location = @1;
13081 				}
13082 		;
13083 
13084 
13085 /*
13086  * SQL character data types
13087  * The following implements CHAR() and VARCHAR().
13088  */
13089 Character:  CharacterWithLength
13090 				{
13091 					$$ = $1;
13092 				}
13093 			| CharacterWithoutLength
13094 				{
13095 					$$ = $1;
13096 				}
13097 		;
13098 
13099 ConstCharacter:  CharacterWithLength
13100 				{
13101 					$$ = $1;
13102 				}
13103 			| CharacterWithoutLength
13104 				{
13105 					/* Length was not specified so allow to be unrestricted.
13106 					 * This handles problems with fixed-length (bpchar) strings
13107 					 * which in column definitions must default to a length
13108 					 * of one, but should not be constrained if the length
13109 					 * was not specified.
13110 					 */
13111 					$$ = $1;
13112 					$$->typmods = NIL;
13113 				}
13114 		;
13115 
13116 CharacterWithLength:  character '(' Iconst ')'
13117 				{
13118 					$$ = SystemTypeName($1);
13119 					$$->typmods = list_make1(makeIntConst($3, @3));
13120 					$$->location = @1;
13121 				}
13122 		;
13123 
13124 CharacterWithoutLength:	 character
13125 				{
13126 					$$ = SystemTypeName($1);
13127 					/* char defaults to char(1), varchar to no limit */
13128 					if (strcmp($1, "bpchar") == 0)
13129 						$$->typmods = list_make1(makeIntConst(1, -1));
13130 					$$->location = @1;
13131 				}
13132 		;
13133 
13134 character:	CHARACTER opt_varying
13135 										{ $$ = $2 ? "varchar": "bpchar"; }
13136 			| CHAR_P opt_varying
13137 										{ $$ = $2 ? "varchar": "bpchar"; }
13138 			| VARCHAR
13139 										{ $$ = "varchar"; }
13140 			| NATIONAL CHARACTER opt_varying
13141 										{ $$ = $3 ? "varchar": "bpchar"; }
13142 			| NATIONAL CHAR_P opt_varying
13143 										{ $$ = $3 ? "varchar": "bpchar"; }
13144 			| NCHAR opt_varying
13145 										{ $$ = $2 ? "varchar": "bpchar"; }
13146 		;
13147 
13148 opt_varying:
13149 			VARYING									{ $$ = true; }
13150 			| /*EMPTY*/								{ $$ = false; }
13151 		;
13152 
13153 /*
13154  * SQL date/time types
13155  */
13156 ConstDatetime:
13157 			TIMESTAMP '(' Iconst ')' opt_timezone
13158 				{
13159 					if ($5)
13160 						$$ = SystemTypeName("timestamptz");
13161 					else
13162 						$$ = SystemTypeName("timestamp");
13163 					$$->typmods = list_make1(makeIntConst($3, @3));
13164 					$$->location = @1;
13165 				}
13166 			| TIMESTAMP opt_timezone
13167 				{
13168 					if ($2)
13169 						$$ = SystemTypeName("timestamptz");
13170 					else
13171 						$$ = SystemTypeName("timestamp");
13172 					$$->location = @1;
13173 				}
13174 			| TIME '(' Iconst ')' opt_timezone
13175 				{
13176 					if ($5)
13177 						$$ = SystemTypeName("timetz");
13178 					else
13179 						$$ = SystemTypeName("time");
13180 					$$->typmods = list_make1(makeIntConst($3, @3));
13181 					$$->location = @1;
13182 				}
13183 			| TIME opt_timezone
13184 				{
13185 					if ($2)
13186 						$$ = SystemTypeName("timetz");
13187 					else
13188 						$$ = SystemTypeName("time");
13189 					$$->location = @1;
13190 				}
13191 		;
13192 
13193 ConstInterval:
13194 			INTERVAL
13195 				{
13196 					$$ = SystemTypeName("interval");
13197 					$$->location = @1;
13198 				}
13199 		;
13200 
13201 opt_timezone:
13202 			WITH_LA TIME ZONE						{ $$ = true; }
13203 			| WITHOUT TIME ZONE						{ $$ = false; }
13204 			| /*EMPTY*/								{ $$ = false; }
13205 		;
13206 
13207 opt_interval:
13208 			YEAR_P
13209 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
13210 			| MONTH_P
13211 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
13212 			| DAY_P
13213 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
13214 			| HOUR_P
13215 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
13216 			| MINUTE_P
13217 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
13218 			| interval_second
13219 				{ $$ = $1; }
13220 			| YEAR_P TO MONTH_P
13221 				{
13222 					$$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
13223 												 INTERVAL_MASK(MONTH), @1));
13224 				}
13225 			| DAY_P TO HOUR_P
13226 				{
13227 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
13228 												 INTERVAL_MASK(HOUR), @1));
13229 				}
13230 			| DAY_P TO MINUTE_P
13231 				{
13232 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
13233 												 INTERVAL_MASK(HOUR) |
13234 												 INTERVAL_MASK(MINUTE), @1));
13235 				}
13236 			| DAY_P TO interval_second
13237 				{
13238 					$$ = $3;
13239 					linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
13240 												INTERVAL_MASK(HOUR) |
13241 												INTERVAL_MASK(MINUTE) |
13242 												INTERVAL_MASK(SECOND), @1);
13243 				}
13244 			| HOUR_P TO MINUTE_P
13245 				{
13246 					$$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
13247 												 INTERVAL_MASK(MINUTE), @1));
13248 				}
13249 			| HOUR_P TO interval_second
13250 				{
13251 					$$ = $3;
13252 					linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
13253 												INTERVAL_MASK(MINUTE) |
13254 												INTERVAL_MASK(SECOND), @1);
13255 				}
13256 			| MINUTE_P TO interval_second
13257 				{
13258 					$$ = $3;
13259 					linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
13260 												INTERVAL_MASK(SECOND), @1);
13261 				}
13262 			| /*EMPTY*/
13263 				{ $$ = NIL; }
13264 		;
13265 
13266 interval_second:
13267 			SECOND_P
13268 				{
13269 					$$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
13270 				}
13271 			| SECOND_P '(' Iconst ')'
13272 				{
13273 					$$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
13274 									makeIntConst($3, @3));
13275 				}
13276 		;
13277 
13278 
13279 /*****************************************************************************
13280  *
13281  *	expression grammar
13282  *
13283  *****************************************************************************/
13284 
13285 /*
13286  * General expressions
13287  * This is the heart of the expression syntax.
13288  *
13289  * We have two expression types: a_expr is the unrestricted kind, and
13290  * b_expr is a subset that must be used in some places to avoid shift/reduce
13291  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
13292  * because that use of AND conflicts with AND as a boolean operator.  So,
13293  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
13294  *
13295  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
13296  * always be used by surrounding it with parens.
13297  *
13298  * c_expr is all the productions that are common to a_expr and b_expr;
13299  * it's factored out just to eliminate redundant coding.
13300  *
13301  * Be careful of productions involving more than one terminal token.
13302  * By default, bison will assign such productions the precedence of their
13303  * last terminal, but in nearly all cases you want it to be the precedence
13304  * of the first terminal instead; otherwise you will not get the behavior
13305  * you expect!  So we use %prec annotations freely to set precedences.
13306  */
13307 a_expr:		c_expr									{ $$ = $1; }
13308 			| a_expr TYPECAST Typename
13309 					{ $$ = makeTypeCast($1, $3, @2); }
13310 			| a_expr COLLATE any_name
13311 				{
13312 					CollateClause *n = makeNode(CollateClause);
13313 					n->arg = $1;
13314 					n->collname = $3;
13315 					n->location = @2;
13316 					$$ = (Node *) n;
13317 				}
13318 			| a_expr AT TIME ZONE a_expr			%prec AT
13319 				{
13320 					$$ = (Node *) makeFuncCall(SystemFuncName("timezone"),
13321 											   list_make2($5, $1),
13322 											   COERCE_SQL_SYNTAX,
13323 											   @2);
13324 				}
13325 		/*
13326 		 * These operators must be called out explicitly in order to make use
13327 		 * of bison's automatic operator-precedence handling.  All other
13328 		 * operator names are handled by the generic productions using "Op",
13329 		 * below; and all those operators will have the same precedence.
13330 		 *
13331 		 * If you add more explicitly-known operators, be sure to add them
13332 		 * also to b_expr and to the MathOp list below.
13333 		 */
13334 			| '+' a_expr					%prec UMINUS
13335 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13336 			| '-' a_expr					%prec UMINUS
13337 				{ $$ = doNegate($2, @1); }
13338 			| a_expr '+' a_expr
13339 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13340 			| a_expr '-' a_expr
13341 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13342 			| a_expr '*' a_expr
13343 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13344 			| a_expr '/' a_expr
13345 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13346 			| a_expr '%' a_expr
13347 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13348 			| a_expr '^' a_expr
13349 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13350 			| a_expr '<' a_expr
13351 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13352 			| a_expr '>' a_expr
13353 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13354 			| a_expr '=' a_expr
13355 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13356 			| a_expr LESS_EQUALS a_expr
13357 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13358 			| a_expr GREATER_EQUALS a_expr
13359 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13360 			| a_expr NOT_EQUALS a_expr
13361 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13362 
13363 			| a_expr qual_Op a_expr				%prec Op
13364 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13365 			| qual_Op a_expr					%prec Op
13366 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13367 
13368 			| a_expr AND a_expr
13369 				{ $$ = makeAndExpr($1, $3, @2); }
13370 			| a_expr OR a_expr
13371 				{ $$ = makeOrExpr($1, $3, @2); }
13372 			| NOT a_expr
13373 				{ $$ = makeNotExpr($2, @1); }
13374 			| NOT_LA a_expr						%prec NOT
13375 				{ $$ = makeNotExpr($2, @1); }
13376 
13377 			| a_expr LIKE a_expr
13378 				{
13379 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13380 												   $1, $3, @2);
13381 				}
13382 			| a_expr LIKE a_expr ESCAPE a_expr					%prec LIKE
13383 				{
13384 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13385 											   list_make2($3, $5),
13386 											   COERCE_EXPLICIT_CALL,
13387 											   @2);
13388 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13389 												   $1, (Node *) n, @2);
13390 				}
13391 			| a_expr NOT_LA LIKE a_expr							%prec NOT_LA
13392 				{
13393 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13394 												   $1, $4, @2);
13395 				}
13396 			| a_expr NOT_LA LIKE a_expr ESCAPE a_expr			%prec NOT_LA
13397 				{
13398 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13399 											   list_make2($4, $6),
13400 											   COERCE_EXPLICIT_CALL,
13401 											   @2);
13402 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13403 												   $1, (Node *) n, @2);
13404 				}
13405 			| a_expr ILIKE a_expr
13406 				{
13407 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13408 												   $1, $3, @2);
13409 				}
13410 			| a_expr ILIKE a_expr ESCAPE a_expr					%prec ILIKE
13411 				{
13412 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13413 											   list_make2($3, $5),
13414 											   COERCE_EXPLICIT_CALL,
13415 											   @2);
13416 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13417 												   $1, (Node *) n, @2);
13418 				}
13419 			| a_expr NOT_LA ILIKE a_expr						%prec NOT_LA
13420 				{
13421 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13422 												   $1, $4, @2);
13423 				}
13424 			| a_expr NOT_LA ILIKE a_expr ESCAPE a_expr			%prec NOT_LA
13425 				{
13426 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13427 											   list_make2($4, $6),
13428 											   COERCE_EXPLICIT_CALL,
13429 											   @2);
13430 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13431 												   $1, (Node *) n, @2);
13432 				}
13433 
13434 			| a_expr SIMILAR TO a_expr							%prec SIMILAR
13435 				{
13436 					FuncCall *n = makeFuncCall(SystemFuncName("similar_to_escape"),
13437 											   list_make1($4),
13438 											   COERCE_EXPLICIT_CALL,
13439 											   @2);
13440 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13441 												   $1, (Node *) n, @2);
13442 				}
13443 			| a_expr SIMILAR TO a_expr ESCAPE a_expr			%prec SIMILAR
13444 				{
13445 					FuncCall *n = makeFuncCall(SystemFuncName("similar_to_escape"),
13446 											   list_make2($4, $6),
13447 											   COERCE_EXPLICIT_CALL,
13448 											   @2);
13449 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13450 												   $1, (Node *) n, @2);
13451 				}
13452 			| a_expr NOT_LA SIMILAR TO a_expr					%prec NOT_LA
13453 				{
13454 					FuncCall *n = makeFuncCall(SystemFuncName("similar_to_escape"),
13455 											   list_make1($5),
13456 											   COERCE_EXPLICIT_CALL,
13457 											   @2);
13458 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13459 												   $1, (Node *) n, @2);
13460 				}
13461 			| a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr		%prec NOT_LA
13462 				{
13463 					FuncCall *n = makeFuncCall(SystemFuncName("similar_to_escape"),
13464 											   list_make2($5, $7),
13465 											   COERCE_EXPLICIT_CALL,
13466 											   @2);
13467 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13468 												   $1, (Node *) n, @2);
13469 				}
13470 
13471 			/* NullTest clause
13472 			 * Define SQL-style Null test clause.
13473 			 * Allow two forms described in the standard:
13474 			 *	a IS NULL
13475 			 *	a IS NOT NULL
13476 			 * Allow two SQL extensions
13477 			 *	a ISNULL
13478 			 *	a NOTNULL
13479 			 */
13480 			| a_expr IS NULL_P							%prec IS
13481 				{
13482 					NullTest *n = makeNode(NullTest);
13483 					n->arg = (Expr *) $1;
13484 					n->nulltesttype = IS_NULL;
13485 					n->location = @2;
13486 					$$ = (Node *)n;
13487 				}
13488 			| a_expr ISNULL
13489 				{
13490 					NullTest *n = makeNode(NullTest);
13491 					n->arg = (Expr *) $1;
13492 					n->nulltesttype = IS_NULL;
13493 					n->location = @2;
13494 					$$ = (Node *)n;
13495 				}
13496 			| a_expr IS NOT NULL_P						%prec IS
13497 				{
13498 					NullTest *n = makeNode(NullTest);
13499 					n->arg = (Expr *) $1;
13500 					n->nulltesttype = IS_NOT_NULL;
13501 					n->location = @2;
13502 					$$ = (Node *)n;
13503 				}
13504 			| a_expr NOTNULL
13505 				{
13506 					NullTest *n = makeNode(NullTest);
13507 					n->arg = (Expr *) $1;
13508 					n->nulltesttype = IS_NOT_NULL;
13509 					n->location = @2;
13510 					$$ = (Node *)n;
13511 				}
13512 			| row OVERLAPS row
13513 				{
13514 					if (list_length($1) != 2)
13515 						ereport(ERROR,
13516 								(errcode(ERRCODE_SYNTAX_ERROR),
13517 								 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
13518 								 parser_errposition(@1)));
13519 					if (list_length($3) != 2)
13520 						ereport(ERROR,
13521 								(errcode(ERRCODE_SYNTAX_ERROR),
13522 								 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
13523 								 parser_errposition(@3)));
13524 					$$ = (Node *) makeFuncCall(SystemFuncName("overlaps"),
13525 											   list_concat($1, $3),
13526 											   COERCE_SQL_SYNTAX,
13527 											   @2);
13528 				}
13529 			| a_expr IS TRUE_P							%prec IS
13530 				{
13531 					BooleanTest *b = makeNode(BooleanTest);
13532 					b->arg = (Expr *) $1;
13533 					b->booltesttype = IS_TRUE;
13534 					b->location = @2;
13535 					$$ = (Node *)b;
13536 				}
13537 			| a_expr IS NOT TRUE_P						%prec IS
13538 				{
13539 					BooleanTest *b = makeNode(BooleanTest);
13540 					b->arg = (Expr *) $1;
13541 					b->booltesttype = IS_NOT_TRUE;
13542 					b->location = @2;
13543 					$$ = (Node *)b;
13544 				}
13545 			| a_expr IS FALSE_P							%prec IS
13546 				{
13547 					BooleanTest *b = makeNode(BooleanTest);
13548 					b->arg = (Expr *) $1;
13549 					b->booltesttype = IS_FALSE;
13550 					b->location = @2;
13551 					$$ = (Node *)b;
13552 				}
13553 			| a_expr IS NOT FALSE_P						%prec IS
13554 				{
13555 					BooleanTest *b = makeNode(BooleanTest);
13556 					b->arg = (Expr *) $1;
13557 					b->booltesttype = IS_NOT_FALSE;
13558 					b->location = @2;
13559 					$$ = (Node *)b;
13560 				}
13561 			| a_expr IS UNKNOWN							%prec IS
13562 				{
13563 					BooleanTest *b = makeNode(BooleanTest);
13564 					b->arg = (Expr *) $1;
13565 					b->booltesttype = IS_UNKNOWN;
13566 					b->location = @2;
13567 					$$ = (Node *)b;
13568 				}
13569 			| a_expr IS NOT UNKNOWN						%prec IS
13570 				{
13571 					BooleanTest *b = makeNode(BooleanTest);
13572 					b->arg = (Expr *) $1;
13573 					b->booltesttype = IS_NOT_UNKNOWN;
13574 					b->location = @2;
13575 					$$ = (Node *)b;
13576 				}
13577 			| a_expr IS DISTINCT FROM a_expr			%prec IS
13578 				{
13579 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13580 				}
13581 			| a_expr IS NOT DISTINCT FROM a_expr		%prec IS
13582 				{
13583                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13584 				}
13585 			| a_expr BETWEEN opt_asymmetric b_expr AND a_expr		%prec BETWEEN
13586 				{
13587 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN,
13588 												   "BETWEEN",
13589 												   $1,
13590 												   (Node *) list_make2($4, $6),
13591 												   @2);
13592 				}
13593 			| a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr %prec NOT_LA
13594 				{
13595 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN,
13596 												   "NOT BETWEEN",
13597 												   $1,
13598 												   (Node *) list_make2($5, $7),
13599 												   @2);
13600 				}
13601 			| a_expr BETWEEN SYMMETRIC b_expr AND a_expr			%prec BETWEEN
13602 				{
13603 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN_SYM,
13604 												   "BETWEEN SYMMETRIC",
13605 												   $1,
13606 												   (Node *) list_make2($4, $6),
13607 												   @2);
13608 				}
13609 			| a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr		%prec NOT_LA
13610 				{
13611 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN_SYM,
13612 												   "NOT BETWEEN SYMMETRIC",
13613 												   $1,
13614 												   (Node *) list_make2($5, $7),
13615 												   @2);
13616 				}
13617 			| a_expr IN_P in_expr
13618 				{
13619 					/* in_expr returns a SubLink or a list of a_exprs */
13620 					if (IsA($3, SubLink))
13621 					{
13622 						/* generate foo = ANY (subquery) */
13623 						SubLink *n = (SubLink *) $3;
13624 						n->subLinkType = ANY_SUBLINK;
13625 						n->subLinkId = 0;
13626 						n->testexpr = $1;
13627 						n->operName = NIL;		/* show it's IN not = ANY */
13628 						n->location = @2;
13629 						$$ = (Node *)n;
13630 					}
13631 					else
13632 					{
13633 						/* generate scalar IN expression */
13634 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
13635 					}
13636 				}
13637 			| a_expr NOT_LA IN_P in_expr						%prec NOT_LA
13638 				{
13639 					/* in_expr returns a SubLink or a list of a_exprs */
13640 					if (IsA($4, SubLink))
13641 					{
13642 						/* generate NOT (foo = ANY (subquery)) */
13643 						/* Make an = ANY node */
13644 						SubLink *n = (SubLink *) $4;
13645 						n->subLinkType = ANY_SUBLINK;
13646 						n->subLinkId = 0;
13647 						n->testexpr = $1;
13648 						n->operName = NIL;		/* show it's IN not = ANY */
13649 						n->location = @2;
13650 						/* Stick a NOT on top; must have same parse location */
13651 						$$ = makeNotExpr((Node *) n, @2);
13652 					}
13653 					else
13654 					{
13655 						/* generate scalar NOT IN expression */
13656 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
13657 					}
13658 				}
13659 			| a_expr subquery_Op sub_type select_with_parens	%prec Op
13660 				{
13661 					SubLink *n = makeNode(SubLink);
13662 					n->subLinkType = $3;
13663 					n->subLinkId = 0;
13664 					n->testexpr = $1;
13665 					n->operName = $2;
13666 					n->subselect = $4;
13667 					n->location = @2;
13668 					$$ = (Node *)n;
13669 				}
13670 			| a_expr subquery_Op sub_type '(' a_expr ')'		%prec Op
13671 				{
13672 					if ($3 == ANY_SUBLINK)
13673 						$$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
13674 					else
13675 						$$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
13676 				}
13677 			| UNIQUE select_with_parens
13678 				{
13679 					/* Not sure how to get rid of the parentheses
13680 					 * but there are lots of shift/reduce errors without them.
13681 					 *
13682 					 * Should be able to implement this by plopping the entire
13683 					 * select into a node, then transforming the target expressions
13684 					 * from whatever they are into count(*), and testing the
13685 					 * entire result equal to one.
13686 					 * But, will probably implement a separate node in the executor.
13687 					 */
13688 					ereport(ERROR,
13689 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13690 							 errmsg("UNIQUE predicate is not yet implemented"),
13691 							 parser_errposition(@1)));
13692 				}
13693 			| a_expr IS DOCUMENT_P					%prec IS
13694 				{
13695 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13696 									 list_make1($1), @2);
13697 				}
13698 			| a_expr IS NOT DOCUMENT_P				%prec IS
13699 				{
13700 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13701 												 list_make1($1), @2),
13702 									 @2);
13703 				}
13704 			| a_expr IS NORMALIZED								%prec IS
13705 				{
13706 					$$ = (Node *) makeFuncCall(SystemFuncName("is_normalized"),
13707 											   list_make1($1),
13708 											   COERCE_SQL_SYNTAX,
13709 											   @2);
13710 				}
13711 			| a_expr IS unicode_normal_form NORMALIZED			%prec IS
13712 				{
13713 					$$ = (Node *) makeFuncCall(SystemFuncName("is_normalized"),
13714 											   list_make2($1, makeStringConst($3, @3)),
13715 											   COERCE_SQL_SYNTAX,
13716 											   @2);
13717 				}
13718 			| a_expr IS NOT NORMALIZED							%prec IS
13719 				{
13720 					$$ = makeNotExpr((Node *) makeFuncCall(SystemFuncName("is_normalized"),
13721 														   list_make1($1),
13722 														   COERCE_SQL_SYNTAX,
13723 														   @2),
13724 									 @2);
13725 				}
13726 			| a_expr IS NOT unicode_normal_form NORMALIZED		%prec IS
13727 				{
13728 					$$ = makeNotExpr((Node *) makeFuncCall(SystemFuncName("is_normalized"),
13729 														   list_make2($1, makeStringConst($4, @4)),
13730 														   COERCE_SQL_SYNTAX,
13731 														   @2),
13732 									 @2);
13733 				}
13734 			| DEFAULT
13735 				{
13736 					/*
13737 					 * The SQL spec only allows DEFAULT in "contextually typed
13738 					 * expressions", but for us, it's easier to allow it in
13739 					 * any a_expr and then throw error during parse analysis
13740 					 * if it's in an inappropriate context.  This way also
13741 					 * lets us say something smarter than "syntax error".
13742 					 */
13743 					SetToDefault *n = makeNode(SetToDefault);
13744 					/* parse analysis will fill in the rest */
13745 					n->location = @1;
13746 					$$ = (Node *)n;
13747 				}
13748 		;
13749 
13750 /*
13751  * Restricted expressions
13752  *
13753  * b_expr is a subset of the complete expression syntax defined by a_expr.
13754  *
13755  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
13756  * cause trouble in the places where b_expr is used.  For simplicity, we
13757  * just eliminate all the boolean-keyword-operator productions from b_expr.
13758  */
13759 b_expr:		c_expr
13760 				{ $$ = $1; }
13761 			| b_expr TYPECAST Typename
13762 				{ $$ = makeTypeCast($1, $3, @2); }
13763 			| '+' b_expr					%prec UMINUS
13764 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13765 			| '-' b_expr					%prec UMINUS
13766 				{ $$ = doNegate($2, @1); }
13767 			| b_expr '+' b_expr
13768 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13769 			| b_expr '-' b_expr
13770 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13771 			| b_expr '*' b_expr
13772 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13773 			| b_expr '/' b_expr
13774 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13775 			| b_expr '%' b_expr
13776 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13777 			| b_expr '^' b_expr
13778 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13779 			| b_expr '<' b_expr
13780 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13781 			| b_expr '>' b_expr
13782 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13783 			| b_expr '=' b_expr
13784 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13785 			| b_expr LESS_EQUALS b_expr
13786 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13787 			| b_expr GREATER_EQUALS b_expr
13788 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13789 			| b_expr NOT_EQUALS b_expr
13790 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13791 			| b_expr qual_Op b_expr				%prec Op
13792 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13793 			| qual_Op b_expr					%prec Op
13794 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13795 			| b_expr IS DISTINCT FROM b_expr		%prec IS
13796 				{
13797 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13798 				}
13799 			| b_expr IS NOT DISTINCT FROM b_expr	%prec IS
13800 				{
13801                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13802 				}
13803 			| b_expr IS DOCUMENT_P					%prec IS
13804 				{
13805 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13806 									 list_make1($1), @2);
13807 				}
13808 			| b_expr IS NOT DOCUMENT_P				%prec IS
13809 				{
13810 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13811 												 list_make1($1), @2),
13812 									 @2);
13813 				}
13814 		;
13815 
13816 /*
13817  * Productions that can be used in both a_expr and b_expr.
13818  *
13819  * Note: productions that refer recursively to a_expr or b_expr mostly
13820  * cannot appear here.	However, it's OK to refer to a_exprs that occur
13821  * inside parentheses, such as function arguments; that cannot introduce
13822  * ambiguity to the b_expr syntax.
13823  */
13824 c_expr:		columnref								{ $$ = $1; }
13825 			| AexprConst							{ $$ = $1; }
13826 			| PARAM opt_indirection
13827 				{
13828 					ParamRef *p = makeNode(ParamRef);
13829 					p->number = $1;
13830 					p->location = @1;
13831 					if ($2)
13832 					{
13833 						A_Indirection *n = makeNode(A_Indirection);
13834 						n->arg = (Node *) p;
13835 						n->indirection = check_indirection($2, yyscanner);
13836 						$$ = (Node *) n;
13837 					}
13838 					else
13839 						$$ = (Node *) p;
13840 				}
13841 			| '(' a_expr ')' opt_indirection
13842 				{
13843 					if ($4)
13844 					{
13845 						A_Indirection *n = makeNode(A_Indirection);
13846 						n->arg = $2;
13847 						n->indirection = check_indirection($4, yyscanner);
13848 						$$ = (Node *)n;
13849 					}
13850 					else
13851 						$$ = $2;
13852 				}
13853 			| case_expr
13854 				{ $$ = $1; }
13855 			| func_expr
13856 				{ $$ = $1; }
13857 			| select_with_parens			%prec UMINUS
13858 				{
13859 					SubLink *n = makeNode(SubLink);
13860 					n->subLinkType = EXPR_SUBLINK;
13861 					n->subLinkId = 0;
13862 					n->testexpr = NULL;
13863 					n->operName = NIL;
13864 					n->subselect = $1;
13865 					n->location = @1;
13866 					$$ = (Node *)n;
13867 				}
13868 			| select_with_parens indirection
13869 				{
13870 					/*
13871 					 * Because the select_with_parens nonterminal is designed
13872 					 * to "eat" as many levels of parens as possible, the
13873 					 * '(' a_expr ')' opt_indirection production above will
13874 					 * fail to match a sub-SELECT with indirection decoration;
13875 					 * the sub-SELECT won't be regarded as an a_expr as long
13876 					 * as there are parens around it.  To support applying
13877 					 * subscripting or field selection to a sub-SELECT result,
13878 					 * we need this redundant-looking production.
13879 					 */
13880 					SubLink *n = makeNode(SubLink);
13881 					A_Indirection *a = makeNode(A_Indirection);
13882 					n->subLinkType = EXPR_SUBLINK;
13883 					n->subLinkId = 0;
13884 					n->testexpr = NULL;
13885 					n->operName = NIL;
13886 					n->subselect = $1;
13887 					n->location = @1;
13888 					a->arg = (Node *)n;
13889 					a->indirection = check_indirection($2, yyscanner);
13890 					$$ = (Node *)a;
13891 				}
13892 			| EXISTS select_with_parens
13893 				{
13894 					SubLink *n = makeNode(SubLink);
13895 					n->subLinkType = EXISTS_SUBLINK;
13896 					n->subLinkId = 0;
13897 					n->testexpr = NULL;
13898 					n->operName = NIL;
13899 					n->subselect = $2;
13900 					n->location = @1;
13901 					$$ = (Node *)n;
13902 				}
13903 			| ARRAY select_with_parens
13904 				{
13905 					SubLink *n = makeNode(SubLink);
13906 					n->subLinkType = ARRAY_SUBLINK;
13907 					n->subLinkId = 0;
13908 					n->testexpr = NULL;
13909 					n->operName = NIL;
13910 					n->subselect = $2;
13911 					n->location = @1;
13912 					$$ = (Node *)n;
13913 				}
13914 			| ARRAY array_expr
13915 				{
13916 					A_ArrayExpr *n = castNode(A_ArrayExpr, $2);
13917 					/* point outermost A_ArrayExpr to the ARRAY keyword */
13918 					n->location = @1;
13919 					$$ = (Node *)n;
13920 				}
13921 			| explicit_row
13922 				{
13923 					RowExpr *r = makeNode(RowExpr);
13924 					r->args = $1;
13925 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13926 					r->colnames = NIL;	/* to be filled in during analysis */
13927 					r->row_format = COERCE_EXPLICIT_CALL; /* abuse */
13928 					r->location = @1;
13929 					$$ = (Node *)r;
13930 				}
13931 			| implicit_row
13932 				{
13933 					RowExpr *r = makeNode(RowExpr);
13934 					r->args = $1;
13935 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13936 					r->colnames = NIL;	/* to be filled in during analysis */
13937 					r->row_format = COERCE_IMPLICIT_CAST; /* abuse */
13938 					r->location = @1;
13939 					$$ = (Node *)r;
13940 				}
13941 			| GROUPING '(' expr_list ')'
13942 			  {
13943 				  GroupingFunc *g = makeNode(GroupingFunc);
13944 				  g->args = $3;
13945 				  g->location = @1;
13946 				  $$ = (Node *)g;
13947 			  }
13948 		;
13949 
13950 func_application: func_name '(' ')'
13951 				{
13952 					$$ = (Node *) makeFuncCall($1, NIL,
13953 											   COERCE_EXPLICIT_CALL,
13954 											   @1);
13955 				}
13956 			| func_name '(' func_arg_list opt_sort_clause ')'
13957 				{
13958 					FuncCall *n = makeFuncCall($1, $3,
13959 											   COERCE_EXPLICIT_CALL,
13960 											   @1);
13961 					n->agg_order = $4;
13962 					$$ = (Node *)n;
13963 				}
13964 			| func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'
13965 				{
13966 					FuncCall *n = makeFuncCall($1, list_make1($4),
13967 											   COERCE_EXPLICIT_CALL,
13968 											   @1);
13969 					n->func_variadic = true;
13970 					n->agg_order = $5;
13971 					$$ = (Node *)n;
13972 				}
13973 			| func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'
13974 				{
13975 					FuncCall *n = makeFuncCall($1, lappend($3, $6),
13976 											   COERCE_EXPLICIT_CALL,
13977 											   @1);
13978 					n->func_variadic = true;
13979 					n->agg_order = $7;
13980 					$$ = (Node *)n;
13981 				}
13982 			| func_name '(' ALL func_arg_list opt_sort_clause ')'
13983 				{
13984 					FuncCall *n = makeFuncCall($1, $4,
13985 											   COERCE_EXPLICIT_CALL,
13986 											   @1);
13987 					n->agg_order = $5;
13988 					/* Ideally we'd mark the FuncCall node to indicate
13989 					 * "must be an aggregate", but there's no provision
13990 					 * for that in FuncCall at the moment.
13991 					 */
13992 					$$ = (Node *)n;
13993 				}
13994 			| func_name '(' DISTINCT func_arg_list opt_sort_clause ')'
13995 				{
13996 					FuncCall *n = makeFuncCall($1, $4,
13997 											   COERCE_EXPLICIT_CALL,
13998 											   @1);
13999 					n->agg_order = $5;
14000 					n->agg_distinct = true;
14001 					$$ = (Node *)n;
14002 				}
14003 			| func_name '(' '*' ')'
14004 				{
14005 					/*
14006 					 * We consider AGGREGATE(*) to invoke a parameterless
14007 					 * aggregate.  This does the right thing for COUNT(*),
14008 					 * and there are no other aggregates in SQL that accept
14009 					 * '*' as parameter.
14010 					 *
14011 					 * The FuncCall node is also marked agg_star = true,
14012 					 * so that later processing can detect what the argument
14013 					 * really was.
14014 					 */
14015 					FuncCall *n = makeFuncCall($1, NIL,
14016 											   COERCE_EXPLICIT_CALL,
14017 											   @1);
14018 					n->agg_star = true;
14019 					$$ = (Node *)n;
14020 				}
14021 		;
14022 
14023 
14024 /*
14025  * func_expr and its cousin func_expr_windowless are split out from c_expr just
14026  * so that we have classifications for "everything that is a function call or
14027  * looks like one".  This isn't very important, but it saves us having to
14028  * document which variants are legal in places like "FROM function()" or the
14029  * backwards-compatible functional-index syntax for CREATE INDEX.
14030  * (Note that many of the special SQL functions wouldn't actually make any
14031  * sense as functional index entries, but we ignore that consideration here.)
14032  */
14033 func_expr: func_application within_group_clause filter_clause over_clause
14034 				{
14035 					FuncCall *n = (FuncCall *) $1;
14036 					/*
14037 					 * The order clause for WITHIN GROUP and the one for
14038 					 * plain-aggregate ORDER BY share a field, so we have to
14039 					 * check here that at most one is present.  We also check
14040 					 * for DISTINCT and VARIADIC here to give a better error
14041 					 * location.  Other consistency checks are deferred to
14042 					 * parse analysis.
14043 					 */
14044 					if ($2 != NIL)
14045 					{
14046 						if (n->agg_order != NIL)
14047 							ereport(ERROR,
14048 									(errcode(ERRCODE_SYNTAX_ERROR),
14049 									 errmsg("cannot use multiple ORDER BY clauses with WITHIN GROUP"),
14050 									 parser_errposition(@2)));
14051 						if (n->agg_distinct)
14052 							ereport(ERROR,
14053 									(errcode(ERRCODE_SYNTAX_ERROR),
14054 									 errmsg("cannot use DISTINCT with WITHIN GROUP"),
14055 									 parser_errposition(@2)));
14056 						if (n->func_variadic)
14057 							ereport(ERROR,
14058 									(errcode(ERRCODE_SYNTAX_ERROR),
14059 									 errmsg("cannot use VARIADIC with WITHIN GROUP"),
14060 									 parser_errposition(@2)));
14061 						n->agg_order = $2;
14062 						n->agg_within_group = true;
14063 					}
14064 					n->agg_filter = $3;
14065 					n->over = $4;
14066 					$$ = (Node *) n;
14067 				}
14068 			| func_expr_common_subexpr
14069 				{ $$ = $1; }
14070 		;
14071 
14072 /*
14073  * As func_expr but does not accept WINDOW functions directly
14074  * (but they can still be contained in arguments for functions etc).
14075  * Use this when window expressions are not allowed, where needed to
14076  * disambiguate the grammar (e.g. in CREATE INDEX).
14077  */
14078 func_expr_windowless:
14079 			func_application						{ $$ = $1; }
14080 			| func_expr_common_subexpr				{ $$ = $1; }
14081 		;
14082 
14083 /*
14084  * Special expressions that are considered to be functions.
14085  */
14086 func_expr_common_subexpr:
14087 			COLLATION FOR '(' a_expr ')'
14088 				{
14089 					$$ = (Node *) makeFuncCall(SystemFuncName("pg_collation_for"),
14090 											   list_make1($4),
14091 											   COERCE_SQL_SYNTAX,
14092 											   @1);
14093 				}
14094 			| CURRENT_DATE
14095 				{
14096 					$$ = makeSQLValueFunction(SVFOP_CURRENT_DATE, -1, @1);
14097 				}
14098 			| CURRENT_TIME
14099 				{
14100 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME, -1, @1);
14101 				}
14102 			| CURRENT_TIME '(' Iconst ')'
14103 				{
14104 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME_N, $3, @1);
14105 				}
14106 			| CURRENT_TIMESTAMP
14107 				{
14108 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP, -1, @1);
14109 				}
14110 			| CURRENT_TIMESTAMP '(' Iconst ')'
14111 				{
14112 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP_N, $3, @1);
14113 				}
14114 			| LOCALTIME
14115 				{
14116 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME, -1, @1);
14117 				}
14118 			| LOCALTIME '(' Iconst ')'
14119 				{
14120 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME_N, $3, @1);
14121 				}
14122 			| LOCALTIMESTAMP
14123 				{
14124 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP, -1, @1);
14125 				}
14126 			| LOCALTIMESTAMP '(' Iconst ')'
14127 				{
14128 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP_N, $3, @1);
14129 				}
14130 			| CURRENT_ROLE
14131 				{
14132 					$$ = makeSQLValueFunction(SVFOP_CURRENT_ROLE, -1, @1);
14133 				}
14134 			| CURRENT_USER
14135 				{
14136 					$$ = makeSQLValueFunction(SVFOP_CURRENT_USER, -1, @1);
14137 				}
14138 			| SESSION_USER
14139 				{
14140 					$$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1);
14141 				}
14142 			| USER
14143 				{
14144 					$$ = makeSQLValueFunction(SVFOP_USER, -1, @1);
14145 				}
14146 			| CURRENT_CATALOG
14147 				{
14148 					$$ = makeSQLValueFunction(SVFOP_CURRENT_CATALOG, -1, @1);
14149 				}
14150 			| CURRENT_SCHEMA
14151 				{
14152 					$$ = makeSQLValueFunction(SVFOP_CURRENT_SCHEMA, -1, @1);
14153 				}
14154 			| CAST '(' a_expr AS Typename ')'
14155 				{ $$ = makeTypeCast($3, $5, @1); }
14156 			| EXTRACT '(' extract_list ')'
14157 				{
14158 					$$ = (Node *) makeFuncCall(SystemFuncName("extract"),
14159 											   $3,
14160 											   COERCE_SQL_SYNTAX,
14161 											   @1);
14162 				}
14163 			| NORMALIZE '(' a_expr ')'
14164 				{
14165 					$$ = (Node *) makeFuncCall(SystemFuncName("normalize"),
14166 											   list_make1($3),
14167 											   COERCE_SQL_SYNTAX,
14168 											   @1);
14169 				}
14170 			| NORMALIZE '(' a_expr ',' unicode_normal_form ')'
14171 				{
14172 					$$ = (Node *) makeFuncCall(SystemFuncName("normalize"),
14173 											   list_make2($3, makeStringConst($5, @5)),
14174 											   COERCE_SQL_SYNTAX,
14175 											   @1);
14176 				}
14177 			| OVERLAY '(' overlay_list ')'
14178 				{
14179 					$$ = (Node *) makeFuncCall(SystemFuncName("overlay"),
14180 											   $3,
14181 											   COERCE_SQL_SYNTAX,
14182 											   @1);
14183 				}
14184 			| OVERLAY '(' func_arg_list_opt ')'
14185 				{
14186 					/*
14187 					 * allow functions named overlay() to be called without
14188 					 * special syntax
14189 					 */
14190 					$$ = (Node *) makeFuncCall(list_make1(makeString("overlay")),
14191 											   $3,
14192 											   COERCE_EXPLICIT_CALL,
14193 											   @1);
14194 				}
14195 			| POSITION '(' position_list ')'
14196 				{
14197 					/*
14198 					 * position(A in B) is converted to position(B, A)
14199 					 *
14200 					 * We deliberately don't offer a "plain syntax" option
14201 					 * for position(), because the reversal of the arguments
14202 					 * creates too much risk of confusion.
14203 					 */
14204 					$$ = (Node *) makeFuncCall(SystemFuncName("position"),
14205 											   $3,
14206 											   COERCE_SQL_SYNTAX,
14207 											   @1);
14208 				}
14209 			| SUBSTRING '(' substr_list ')'
14210 				{
14211 					/* substring(A from B for C) is converted to
14212 					 * substring(A, B, C) - thomas 2000-11-28
14213 					 */
14214 					$$ = (Node *) makeFuncCall(SystemFuncName("substring"),
14215 											   $3,
14216 											   COERCE_SQL_SYNTAX,
14217 											   @1);
14218 				}
14219 			| SUBSTRING '(' func_arg_list_opt ')'
14220 				{
14221 					/*
14222 					 * allow functions named substring() to be called without
14223 					 * special syntax
14224 					 */
14225 					$$ = (Node *) makeFuncCall(list_make1(makeString("substring")),
14226 											   $3,
14227 											   COERCE_EXPLICIT_CALL,
14228 											   @1);
14229 				}
14230 			| TREAT '(' a_expr AS Typename ')'
14231 				{
14232 					/* TREAT(expr AS target) converts expr of a particular type to target,
14233 					 * which is defined to be a subtype of the original expression.
14234 					 * In SQL99, this is intended for use with structured UDTs,
14235 					 * but let's make this a generally useful form allowing stronger
14236 					 * coercions than are handled by implicit casting.
14237 					 *
14238 					 * Convert SystemTypeName() to SystemFuncName() even though
14239 					 * at the moment they result in the same thing.
14240 					 */
14241 					$$ = (Node *) makeFuncCall(SystemFuncName(strVal(llast($5->names))),
14242 											   list_make1($3),
14243 											   COERCE_EXPLICIT_CALL,
14244 											   @1);
14245 				}
14246 			| TRIM '(' BOTH trim_list ')'
14247 				{
14248 					/* various trim expressions are defined in SQL
14249 					 * - thomas 1997-07-19
14250 					 */
14251 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"),
14252 											   $4,
14253 											   COERCE_SQL_SYNTAX,
14254 											   @1);
14255 				}
14256 			| TRIM '(' LEADING trim_list ')'
14257 				{
14258 					$$ = (Node *) makeFuncCall(SystemFuncName("ltrim"),
14259 											   $4,
14260 											   COERCE_SQL_SYNTAX,
14261 											   @1);
14262 				}
14263 			| TRIM '(' TRAILING trim_list ')'
14264 				{
14265 					$$ = (Node *) makeFuncCall(SystemFuncName("rtrim"),
14266 											   $4,
14267 											   COERCE_SQL_SYNTAX,
14268 											   @1);
14269 				}
14270 			| TRIM '(' trim_list ')'
14271 				{
14272 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"),
14273 											   $3,
14274 											   COERCE_SQL_SYNTAX,
14275 											   @1);
14276 				}
14277 			| NULLIF '(' a_expr ',' a_expr ')'
14278 				{
14279 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
14280 				}
14281 			| COALESCE '(' expr_list ')'
14282 				{
14283 					CoalesceExpr *c = makeNode(CoalesceExpr);
14284 					c->args = $3;
14285 					c->location = @1;
14286 					$$ = (Node *)c;
14287 				}
14288 			| GREATEST '(' expr_list ')'
14289 				{
14290 					MinMaxExpr *v = makeNode(MinMaxExpr);
14291 					v->args = $3;
14292 					v->op = IS_GREATEST;
14293 					v->location = @1;
14294 					$$ = (Node *)v;
14295 				}
14296 			| LEAST '(' expr_list ')'
14297 				{
14298 					MinMaxExpr *v = makeNode(MinMaxExpr);
14299 					v->args = $3;
14300 					v->op = IS_LEAST;
14301 					v->location = @1;
14302 					$$ = (Node *)v;
14303 				}
14304 			| XMLCONCAT '(' expr_list ')'
14305 				{
14306 					$$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
14307 				}
14308 			| XMLELEMENT '(' NAME_P ColLabel ')'
14309 				{
14310 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
14311 				}
14312 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
14313 				{
14314 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
14315 				}
14316 			| XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
14317 				{
14318 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
14319 				}
14320 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
14321 				{
14322 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
14323 				}
14324 			| XMLEXISTS '(' c_expr xmlexists_argument ')'
14325 				{
14326 					/* xmlexists(A PASSING [BY REF] B [BY REF]) is
14327 					 * converted to xmlexists(A, B)*/
14328 					$$ = (Node *) makeFuncCall(SystemFuncName("xmlexists"),
14329 											   list_make2($3, $4),
14330 											   COERCE_SQL_SYNTAX,
14331 											   @1);
14332 				}
14333 			| XMLFOREST '(' xml_attribute_list ')'
14334 				{
14335 					$$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
14336 				}
14337 			| XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
14338 				{
14339 					XmlExpr *x = (XmlExpr *)
14340 						makeXmlExpr(IS_XMLPARSE, NULL, NIL,
14341 									list_make2($4, makeBoolAConst($5, -1)),
14342 									@1);
14343 					x->xmloption = $3;
14344 					$$ = (Node *)x;
14345 				}
14346 			| XMLPI '(' NAME_P ColLabel ')'
14347 				{
14348 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
14349 				}
14350 			| XMLPI '(' NAME_P ColLabel ',' a_expr ')'
14351 				{
14352 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
14353 				}
14354 			| XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
14355 				{
14356 					$$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
14357 									 list_make3($3, $5, $6), @1);
14358 				}
14359 			| XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
14360 				{
14361 					XmlSerialize *n = makeNode(XmlSerialize);
14362 					n->xmloption = $3;
14363 					n->expr = $4;
14364 					n->typeName = $6;
14365 					n->location = @1;
14366 					$$ = (Node *)n;
14367 				}
14368 		;
14369 
14370 /*
14371  * SQL/XML support
14372  */
14373 xml_root_version: VERSION_P a_expr
14374 				{ $$ = $2; }
14375 			| VERSION_P NO VALUE_P
14376 				{ $$ = makeNullAConst(-1); }
14377 		;
14378 
14379 opt_xml_root_standalone: ',' STANDALONE_P YES_P
14380 				{ $$ = makeIntConst(XML_STANDALONE_YES, -1); }
14381 			| ',' STANDALONE_P NO
14382 				{ $$ = makeIntConst(XML_STANDALONE_NO, -1); }
14383 			| ',' STANDALONE_P NO VALUE_P
14384 				{ $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
14385 			| /*EMPTY*/
14386 				{ $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
14387 		;
14388 
14389 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'	{ $$ = $3; }
14390 		;
14391 
14392 xml_attribute_list:	xml_attribute_el					{ $$ = list_make1($1); }
14393 			| xml_attribute_list ',' xml_attribute_el	{ $$ = lappend($1, $3); }
14394 		;
14395 
14396 xml_attribute_el: a_expr AS ColLabel
14397 				{
14398 					$$ = makeNode(ResTarget);
14399 					$$->name = $3;
14400 					$$->indirection = NIL;
14401 					$$->val = (Node *) $1;
14402 					$$->location = @1;
14403 				}
14404 			| a_expr
14405 				{
14406 					$$ = makeNode(ResTarget);
14407 					$$->name = NULL;
14408 					$$->indirection = NIL;
14409 					$$->val = (Node *) $1;
14410 					$$->location = @1;
14411 				}
14412 		;
14413 
14414 document_or_content: DOCUMENT_P						{ $$ = XMLOPTION_DOCUMENT; }
14415 			| CONTENT_P								{ $$ = XMLOPTION_CONTENT; }
14416 		;
14417 
14418 xml_whitespace_option: PRESERVE WHITESPACE_P		{ $$ = true; }
14419 			| STRIP_P WHITESPACE_P					{ $$ = false; }
14420 			| /*EMPTY*/								{ $$ = false; }
14421 		;
14422 
14423 /* We allow several variants for SQL and other compatibility. */
14424 xmlexists_argument:
14425 			PASSING c_expr
14426 				{
14427 					$$ = $2;
14428 				}
14429 			| PASSING c_expr xml_passing_mech
14430 				{
14431 					$$ = $2;
14432 				}
14433 			| PASSING xml_passing_mech c_expr
14434 				{
14435 					$$ = $3;
14436 				}
14437 			| PASSING xml_passing_mech c_expr xml_passing_mech
14438 				{
14439 					$$ = $3;
14440 				}
14441 		;
14442 
14443 xml_passing_mech:
14444 			BY REF
14445 			| BY VALUE_P
14446 		;
14447 
14448 
14449 /*
14450  * Aggregate decoration clauses
14451  */
14452 within_group_clause:
14453 			WITHIN GROUP_P '(' sort_clause ')'		{ $$ = $4; }
14454 			| /*EMPTY*/								{ $$ = NIL; }
14455 		;
14456 
14457 filter_clause:
14458 			FILTER '(' WHERE a_expr ')'				{ $$ = $4; }
14459 			| /*EMPTY*/								{ $$ = NULL; }
14460 		;
14461 
14462 
14463 /*
14464  * Window Definitions
14465  */
14466 window_clause:
14467 			WINDOW window_definition_list			{ $$ = $2; }
14468 			| /*EMPTY*/								{ $$ = NIL; }
14469 		;
14470 
14471 window_definition_list:
14472 			window_definition						{ $$ = list_make1($1); }
14473 			| window_definition_list ',' window_definition
14474 													{ $$ = lappend($1, $3); }
14475 		;
14476 
14477 window_definition:
14478 			ColId AS window_specification
14479 				{
14480 					WindowDef *n = $3;
14481 					n->name = $1;
14482 					$$ = n;
14483 				}
14484 		;
14485 
14486 over_clause: OVER window_specification
14487 				{ $$ = $2; }
14488 			| OVER ColId
14489 				{
14490 					WindowDef *n = makeNode(WindowDef);
14491 					n->name = $2;
14492 					n->refname = NULL;
14493 					n->partitionClause = NIL;
14494 					n->orderClause = NIL;
14495 					n->frameOptions = FRAMEOPTION_DEFAULTS;
14496 					n->startOffset = NULL;
14497 					n->endOffset = NULL;
14498 					n->location = @2;
14499 					$$ = n;
14500 				}
14501 			| /*EMPTY*/
14502 				{ $$ = NULL; }
14503 		;
14504 
14505 window_specification: '(' opt_existing_window_name opt_partition_clause
14506 						opt_sort_clause opt_frame_clause ')'
14507 				{
14508 					WindowDef *n = makeNode(WindowDef);
14509 					n->name = NULL;
14510 					n->refname = $2;
14511 					n->partitionClause = $3;
14512 					n->orderClause = $4;
14513 					/* copy relevant fields of opt_frame_clause */
14514 					n->frameOptions = $5->frameOptions;
14515 					n->startOffset = $5->startOffset;
14516 					n->endOffset = $5->endOffset;
14517 					n->location = @1;
14518 					$$ = n;
14519 				}
14520 		;
14521 
14522 /*
14523  * If we see PARTITION, RANGE, ROWS or GROUPS as the first token after the '('
14524  * of a window_specification, we want the assumption to be that there is
14525  * no existing_window_name; but those keywords are unreserved and so could
14526  * be ColIds.  We fix this by making them have the same precedence as IDENT
14527  * and giving the empty production here a slightly higher precedence, so
14528  * that the shift/reduce conflict is resolved in favor of reducing the rule.
14529  * These keywords are thus precluded from being an existing_window_name but
14530  * are not reserved for any other purpose.
14531  */
14532 opt_existing_window_name: ColId						{ $$ = $1; }
14533 			| /*EMPTY*/				%prec Op		{ $$ = NULL; }
14534 		;
14535 
14536 opt_partition_clause: PARTITION BY expr_list		{ $$ = $3; }
14537 			| /*EMPTY*/								{ $$ = NIL; }
14538 		;
14539 
14540 /*
14541  * For frame clauses, we return a WindowDef, but only some fields are used:
14542  * frameOptions, startOffset, and endOffset.
14543  */
14544 opt_frame_clause:
14545 			RANGE frame_extent opt_window_exclusion_clause
14546 				{
14547 					WindowDef *n = $2;
14548 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
14549 					n->frameOptions |= $3;
14550 					$$ = n;
14551 				}
14552 			| ROWS frame_extent opt_window_exclusion_clause
14553 				{
14554 					WindowDef *n = $2;
14555 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
14556 					n->frameOptions |= $3;
14557 					$$ = n;
14558 				}
14559 			| GROUPS frame_extent opt_window_exclusion_clause
14560 				{
14561 					WindowDef *n = $2;
14562 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_GROUPS;
14563 					n->frameOptions |= $3;
14564 					$$ = n;
14565 				}
14566 			| /*EMPTY*/
14567 				{
14568 					WindowDef *n = makeNode(WindowDef);
14569 					n->frameOptions = FRAMEOPTION_DEFAULTS;
14570 					n->startOffset = NULL;
14571 					n->endOffset = NULL;
14572 					$$ = n;
14573 				}
14574 		;
14575 
14576 frame_extent: frame_bound
14577 				{
14578 					WindowDef *n = $1;
14579 					/* reject invalid cases */
14580 					if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14581 						ereport(ERROR,
14582 								(errcode(ERRCODE_WINDOWING_ERROR),
14583 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14584 								 parser_errposition(@1)));
14585 					if (n->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
14586 						ereport(ERROR,
14587 								(errcode(ERRCODE_WINDOWING_ERROR),
14588 								 errmsg("frame starting from following row cannot end with current row"),
14589 								 parser_errposition(@1)));
14590 					n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
14591 					$$ = n;
14592 				}
14593 			| BETWEEN frame_bound AND frame_bound
14594 				{
14595 					WindowDef *n1 = $2;
14596 					WindowDef *n2 = $4;
14597 					/* form merged options */
14598 					int		frameOptions = n1->frameOptions;
14599 					/* shift converts START_ options to END_ options */
14600 					frameOptions |= n2->frameOptions << 1;
14601 					frameOptions |= FRAMEOPTION_BETWEEN;
14602 					/* reject invalid cases */
14603 					if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14604 						ereport(ERROR,
14605 								(errcode(ERRCODE_WINDOWING_ERROR),
14606 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14607 								 parser_errposition(@2)));
14608 					if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
14609 						ereport(ERROR,
14610 								(errcode(ERRCODE_WINDOWING_ERROR),
14611 								 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
14612 								 parser_errposition(@4)));
14613 					if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
14614 						(frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING))
14615 						ereport(ERROR,
14616 								(errcode(ERRCODE_WINDOWING_ERROR),
14617 								 errmsg("frame starting from current row cannot have preceding rows"),
14618 								 parser_errposition(@4)));
14619 					if ((frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING) &&
14620 						(frameOptions & (FRAMEOPTION_END_OFFSET_PRECEDING |
14621 										 FRAMEOPTION_END_CURRENT_ROW)))
14622 						ereport(ERROR,
14623 								(errcode(ERRCODE_WINDOWING_ERROR),
14624 								 errmsg("frame starting from following row cannot have preceding rows"),
14625 								 parser_errposition(@4)));
14626 					n1->frameOptions = frameOptions;
14627 					n1->endOffset = n2->startOffset;
14628 					$$ = n1;
14629 				}
14630 		;
14631 
14632 /*
14633  * This is used for both frame start and frame end, with output set up on
14634  * the assumption it's frame start; the frame_extent productions must reject
14635  * invalid cases.
14636  */
14637 frame_bound:
14638 			UNBOUNDED PRECEDING
14639 				{
14640 					WindowDef *n = makeNode(WindowDef);
14641 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
14642 					n->startOffset = NULL;
14643 					n->endOffset = NULL;
14644 					$$ = n;
14645 				}
14646 			| UNBOUNDED FOLLOWING
14647 				{
14648 					WindowDef *n = makeNode(WindowDef);
14649 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
14650 					n->startOffset = NULL;
14651 					n->endOffset = NULL;
14652 					$$ = n;
14653 				}
14654 			| CURRENT_P ROW
14655 				{
14656 					WindowDef *n = makeNode(WindowDef);
14657 					n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
14658 					n->startOffset = NULL;
14659 					n->endOffset = NULL;
14660 					$$ = n;
14661 				}
14662 			| a_expr PRECEDING
14663 				{
14664 					WindowDef *n = makeNode(WindowDef);
14665 					n->frameOptions = FRAMEOPTION_START_OFFSET_PRECEDING;
14666 					n->startOffset = $1;
14667 					n->endOffset = NULL;
14668 					$$ = n;
14669 				}
14670 			| a_expr FOLLOWING
14671 				{
14672 					WindowDef *n = makeNode(WindowDef);
14673 					n->frameOptions = FRAMEOPTION_START_OFFSET_FOLLOWING;
14674 					n->startOffset = $1;
14675 					n->endOffset = NULL;
14676 					$$ = n;
14677 				}
14678 		;
14679 
14680 opt_window_exclusion_clause:
14681 			EXCLUDE CURRENT_P ROW	{ $$ = FRAMEOPTION_EXCLUDE_CURRENT_ROW; }
14682 			| EXCLUDE GROUP_P		{ $$ = FRAMEOPTION_EXCLUDE_GROUP; }
14683 			| EXCLUDE TIES			{ $$ = FRAMEOPTION_EXCLUDE_TIES; }
14684 			| EXCLUDE NO OTHERS		{ $$ = 0; }
14685 			| /*EMPTY*/				{ $$ = 0; }
14686 		;
14687 
14688 
14689 /*
14690  * Supporting nonterminals for expressions.
14691  */
14692 
14693 /* Explicit row production.
14694  *
14695  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
14696  * without conflicting with the parenthesized a_expr production.  Without the
14697  * ROW keyword, there must be more than one a_expr inside the parens.
14698  */
14699 row:		ROW '(' expr_list ')'					{ $$ = $3; }
14700 			| ROW '(' ')'							{ $$ = NIL; }
14701 			| '(' expr_list ',' a_expr ')'			{ $$ = lappend($2, $4); }
14702 		;
14703 
14704 explicit_row:	ROW '(' expr_list ')'				{ $$ = $3; }
14705 			| ROW '(' ')'							{ $$ = NIL; }
14706 		;
14707 
14708 implicit_row:	'(' expr_list ',' a_expr ')'		{ $$ = lappend($2, $4); }
14709 		;
14710 
14711 sub_type:	ANY										{ $$ = ANY_SUBLINK; }
14712 			| SOME									{ $$ = ANY_SUBLINK; }
14713 			| ALL									{ $$ = ALL_SUBLINK; }
14714 		;
14715 
14716 all_Op:		Op										{ $$ = $1; }
14717 			| MathOp								{ $$ = $1; }
14718 		;
14719 
14720 MathOp:		 '+'									{ $$ = "+"; }
14721 			| '-'									{ $$ = "-"; }
14722 			| '*'									{ $$ = "*"; }
14723 			| '/'									{ $$ = "/"; }
14724 			| '%'									{ $$ = "%"; }
14725 			| '^'									{ $$ = "^"; }
14726 			| '<'									{ $$ = "<"; }
14727 			| '>'									{ $$ = ">"; }
14728 			| '='									{ $$ = "="; }
14729 			| LESS_EQUALS							{ $$ = "<="; }
14730 			| GREATER_EQUALS						{ $$ = ">="; }
14731 			| NOT_EQUALS							{ $$ = "<>"; }
14732 		;
14733 
14734 qual_Op:	Op
14735 					{ $$ = list_make1(makeString($1)); }
14736 			| OPERATOR '(' any_operator ')'
14737 					{ $$ = $3; }
14738 		;
14739 
14740 qual_all_Op:
14741 			all_Op
14742 					{ $$ = list_make1(makeString($1)); }
14743 			| OPERATOR '(' any_operator ')'
14744 					{ $$ = $3; }
14745 		;
14746 
14747 subquery_Op:
14748 			all_Op
14749 					{ $$ = list_make1(makeString($1)); }
14750 			| OPERATOR '(' any_operator ')'
14751 					{ $$ = $3; }
14752 			| LIKE
14753 					{ $$ = list_make1(makeString("~~")); }
14754 			| NOT_LA LIKE
14755 					{ $$ = list_make1(makeString("!~~")); }
14756 			| ILIKE
14757 					{ $$ = list_make1(makeString("~~*")); }
14758 			| NOT_LA ILIKE
14759 					{ $$ = list_make1(makeString("!~~*")); }
14760 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
14761  * the regular expression is preprocessed by a function (similar_to_escape),
14762  * and the ~ operator for posix regular expressions is used.
14763  *        x SIMILAR TO y     ->    x ~ similar_to_escape(y)
14764  * this transformation is made on the fly by the parser upwards.
14765  * however the SubLink structure which handles any/some/all stuff
14766  * is not ready for such a thing.
14767  */
14768 			;
14769 
14770 expr_list:	a_expr
14771 				{
14772 					$$ = list_make1($1);
14773 				}
14774 			| expr_list ',' a_expr
14775 				{
14776 					$$ = lappend($1, $3);
14777 				}
14778 		;
14779 
14780 /* function arguments can have names */
14781 func_arg_list:  func_arg_expr
14782 				{
14783 					$$ = list_make1($1);
14784 				}
14785 			| func_arg_list ',' func_arg_expr
14786 				{
14787 					$$ = lappend($1, $3);
14788 				}
14789 		;
14790 
14791 func_arg_expr:  a_expr
14792 				{
14793 					$$ = $1;
14794 				}
14795 			| param_name COLON_EQUALS a_expr
14796 				{
14797 					NamedArgExpr *na = makeNode(NamedArgExpr);
14798 					na->name = $1;
14799 					na->arg = (Expr *) $3;
14800 					na->argnumber = -1;		/* until determined */
14801 					na->location = @1;
14802 					$$ = (Node *) na;
14803 				}
14804 			| param_name EQUALS_GREATER a_expr
14805 				{
14806 					NamedArgExpr *na = makeNode(NamedArgExpr);
14807 					na->name = $1;
14808 					na->arg = (Expr *) $3;
14809 					na->argnumber = -1;		/* until determined */
14810 					na->location = @1;
14811 					$$ = (Node *) na;
14812 				}
14813 		;
14814 
14815 func_arg_list_opt:	func_arg_list					{ $$ = $1; }
14816 			| /*EMPTY*/								{ $$ = NIL; }
14817 		;
14818 
14819 type_list:	Typename								{ $$ = list_make1($1); }
14820 			| type_list ',' Typename				{ $$ = lappend($1, $3); }
14821 		;
14822 
14823 array_expr: '[' expr_list ']'
14824 				{
14825 					$$ = makeAArrayExpr($2, @1);
14826 				}
14827 			| '[' array_expr_list ']'
14828 				{
14829 					$$ = makeAArrayExpr($2, @1);
14830 				}
14831 			| '[' ']'
14832 				{
14833 					$$ = makeAArrayExpr(NIL, @1);
14834 				}
14835 		;
14836 
14837 array_expr_list: array_expr							{ $$ = list_make1($1); }
14838 			| array_expr_list ',' array_expr		{ $$ = lappend($1, $3); }
14839 		;
14840 
14841 
14842 extract_list:
14843 			extract_arg FROM a_expr
14844 				{
14845 					$$ = list_make2(makeStringConst($1, @1), $3);
14846 				}
14847 		;
14848 
14849 /* Allow delimited string Sconst in extract_arg as an SQL extension.
14850  * - thomas 2001-04-12
14851  */
14852 extract_arg:
14853 			IDENT									{ $$ = $1; }
14854 			| YEAR_P								{ $$ = "year"; }
14855 			| MONTH_P								{ $$ = "month"; }
14856 			| DAY_P									{ $$ = "day"; }
14857 			| HOUR_P								{ $$ = "hour"; }
14858 			| MINUTE_P								{ $$ = "minute"; }
14859 			| SECOND_P								{ $$ = "second"; }
14860 			| Sconst								{ $$ = $1; }
14861 		;
14862 
14863 unicode_normal_form:
14864 			NFC										{ $$ = "NFC"; }
14865 			| NFD									{ $$ = "NFD"; }
14866 			| NFKC									{ $$ = "NFKC"; }
14867 			| NFKD									{ $$ = "NFKD"; }
14868 		;
14869 
14870 /* OVERLAY() arguments */
14871 overlay_list:
14872 			a_expr PLACING a_expr FROM a_expr FOR a_expr
14873 				{
14874 					/* overlay(A PLACING B FROM C FOR D) is converted to overlay(A, B, C, D) */
14875 					$$ = list_make4($1, $3, $5, $7);
14876 				}
14877 			| a_expr PLACING a_expr FROM a_expr
14878 				{
14879 					/* overlay(A PLACING B FROM C) is converted to overlay(A, B, C) */
14880 					$$ = list_make3($1, $3, $5);
14881 				}
14882 		;
14883 
14884 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
14885 position_list:
14886 			b_expr IN_P b_expr						{ $$ = list_make2($3, $1); }
14887 		;
14888 
14889 /*
14890  * SUBSTRING() arguments
14891  *
14892  * Note that SQL:1999 has both
14893  *     text FROM int FOR int
14894  * and
14895  *     text FROM pattern FOR escape
14896  *
14897  * In the parser we map them both to a call to the substring() function and
14898  * rely on type resolution to pick the right one.
14899  *
14900  * In SQL:2003, the second variant was changed to
14901  *     text SIMILAR pattern ESCAPE escape
14902  * We could in theory map that to a different function internally, but
14903  * since we still support the SQL:1999 version, we don't.  However,
14904  * ruleutils.c will reverse-list the call in the newer style.
14905  */
14906 substr_list:
14907 			a_expr FROM a_expr FOR a_expr
14908 				{
14909 					$$ = list_make3($1, $3, $5);
14910 				}
14911 			| a_expr FOR a_expr FROM a_expr
14912 				{
14913 					/* not legal per SQL, but might as well allow it */
14914 					$$ = list_make3($1, $5, $3);
14915 				}
14916 			| a_expr FROM a_expr
14917 				{
14918 					/*
14919 					 * Because we aren't restricting data types here, this
14920 					 * syntax can end up resolving to textregexsubstr().
14921 					 * We've historically allowed that to happen, so continue
14922 					 * to accept it.  However, ruleutils.c will reverse-list
14923 					 * such a call in regular function call syntax.
14924 					 */
14925 					$$ = list_make2($1, $3);
14926 				}
14927 			| a_expr FOR a_expr
14928 				{
14929 					/* not legal per SQL */
14930 
14931 					/*
14932 					 * Since there are no cases where this syntax allows
14933 					 * a textual FOR value, we forcibly cast the argument
14934 					 * to int4.  The possible matches in pg_proc are
14935 					 * substring(text,int4) and substring(text,text),
14936 					 * and we don't want the parser to choose the latter,
14937 					 * which it is likely to do if the second argument
14938 					 * is unknown or doesn't have an implicit cast to int4.
14939 					 */
14940 					$$ = list_make3($1, makeIntConst(1, -1),
14941 									makeTypeCast($3,
14942 												 SystemTypeName("int4"), -1));
14943 				}
14944 			| a_expr SIMILAR a_expr ESCAPE a_expr
14945 				{
14946 					$$ = list_make3($1, $3, $5);
14947 				}
14948 		;
14949 
14950 trim_list:	a_expr FROM expr_list					{ $$ = lappend($3, $1); }
14951 			| FROM expr_list						{ $$ = $2; }
14952 			| expr_list								{ $$ = $1; }
14953 		;
14954 
14955 in_expr:	select_with_parens
14956 				{
14957 					SubLink *n = makeNode(SubLink);
14958 					n->subselect = $1;
14959 					/* other fields will be filled later */
14960 					$$ = (Node *)n;
14961 				}
14962 			| '(' expr_list ')'						{ $$ = (Node *)$2; }
14963 		;
14964 
14965 /*
14966  * Define SQL-style CASE clause.
14967  * - Full specification
14968  *	CASE WHEN a = b THEN c ... ELSE d END
14969  * - Implicit argument
14970  *	CASE a WHEN b THEN c ... ELSE d END
14971  */
14972 case_expr:	CASE case_arg when_clause_list case_default END_P
14973 				{
14974 					CaseExpr *c = makeNode(CaseExpr);
14975 					c->casetype = InvalidOid; /* not analyzed yet */
14976 					c->arg = (Expr *) $2;
14977 					c->args = $3;
14978 					c->defresult = (Expr *) $4;
14979 					c->location = @1;
14980 					$$ = (Node *)c;
14981 				}
14982 		;
14983 
14984 when_clause_list:
14985 			/* There must be at least one */
14986 			when_clause								{ $$ = list_make1($1); }
14987 			| when_clause_list when_clause			{ $$ = lappend($1, $2); }
14988 		;
14989 
14990 when_clause:
14991 			WHEN a_expr THEN a_expr
14992 				{
14993 					CaseWhen *w = makeNode(CaseWhen);
14994 					w->expr = (Expr *) $2;
14995 					w->result = (Expr *) $4;
14996 					w->location = @1;
14997 					$$ = (Node *)w;
14998 				}
14999 		;
15000 
15001 case_default:
15002 			ELSE a_expr								{ $$ = $2; }
15003 			| /*EMPTY*/								{ $$ = NULL; }
15004 		;
15005 
15006 case_arg:	a_expr									{ $$ = $1; }
15007 			| /*EMPTY*/								{ $$ = NULL; }
15008 		;
15009 
15010 columnref:	ColId
15011 				{
15012 					$$ = makeColumnRef($1, NIL, @1, yyscanner);
15013 				}
15014 			| ColId indirection
15015 				{
15016 					$$ = makeColumnRef($1, $2, @1, yyscanner);
15017 				}
15018 		;
15019 
15020 indirection_el:
15021 			'.' attr_name
15022 				{
15023 					$$ = (Node *) makeString($2);
15024 				}
15025 			| '.' '*'
15026 				{
15027 					$$ = (Node *) makeNode(A_Star);
15028 				}
15029 			| '[' a_expr ']'
15030 				{
15031 					A_Indices *ai = makeNode(A_Indices);
15032 					ai->is_slice = false;
15033 					ai->lidx = NULL;
15034 					ai->uidx = $2;
15035 					$$ = (Node *) ai;
15036 				}
15037 			| '[' opt_slice_bound ':' opt_slice_bound ']'
15038 				{
15039 					A_Indices *ai = makeNode(A_Indices);
15040 					ai->is_slice = true;
15041 					ai->lidx = $2;
15042 					ai->uidx = $4;
15043 					$$ = (Node *) ai;
15044 				}
15045 		;
15046 
15047 opt_slice_bound:
15048 			a_expr									{ $$ = $1; }
15049 			| /*EMPTY*/								{ $$ = NULL; }
15050 		;
15051 
15052 indirection:
15053 			indirection_el							{ $$ = list_make1($1); }
15054 			| indirection indirection_el			{ $$ = lappend($1, $2); }
15055 		;
15056 
15057 opt_indirection:
15058 			/*EMPTY*/								{ $$ = NIL; }
15059 			| opt_indirection indirection_el		{ $$ = lappend($1, $2); }
15060 		;
15061 
15062 opt_asymmetric: ASYMMETRIC
15063 			| /*EMPTY*/
15064 		;
15065 
15066 
15067 /*****************************************************************************
15068  *
15069  *	target list for SELECT
15070  *
15071  *****************************************************************************/
15072 
15073 opt_target_list: target_list						{ $$ = $1; }
15074 			| /* EMPTY */							{ $$ = NIL; }
15075 		;
15076 
15077 target_list:
15078 			target_el								{ $$ = list_make1($1); }
15079 			| target_list ',' target_el				{ $$ = lappend($1, $3); }
15080 		;
15081 
15082 target_el:	a_expr AS ColLabel
15083 				{
15084 					$$ = makeNode(ResTarget);
15085 					$$->name = $3;
15086 					$$->indirection = NIL;
15087 					$$->val = (Node *)$1;
15088 					$$->location = @1;
15089 				}
15090 			| a_expr BareColLabel
15091 				{
15092 					$$ = makeNode(ResTarget);
15093 					$$->name = $2;
15094 					$$->indirection = NIL;
15095 					$$->val = (Node *)$1;
15096 					$$->location = @1;
15097 				}
15098 			| a_expr
15099 				{
15100 					$$ = makeNode(ResTarget);
15101 					$$->name = NULL;
15102 					$$->indirection = NIL;
15103 					$$->val = (Node *)$1;
15104 					$$->location = @1;
15105 				}
15106 			| '*'
15107 				{
15108 					ColumnRef *n = makeNode(ColumnRef);
15109 					n->fields = list_make1(makeNode(A_Star));
15110 					n->location = @1;
15111 
15112 					$$ = makeNode(ResTarget);
15113 					$$->name = NULL;
15114 					$$->indirection = NIL;
15115 					$$->val = (Node *)n;
15116 					$$->location = @1;
15117 				}
15118 		;
15119 
15120 
15121 /*****************************************************************************
15122  *
15123  *	Names and constants
15124  *
15125  *****************************************************************************/
15126 
15127 qualified_name_list:
15128 			qualified_name							{ $$ = list_make1($1); }
15129 			| qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
15130 		;
15131 
15132 /*
15133  * The production for a qualified relation name has to exactly match the
15134  * production for a qualified func_name, because in a FROM clause we cannot
15135  * tell which we are parsing until we see what comes after it ('(' for a
15136  * func_name, something else for a relation). Therefore we allow 'indirection'
15137  * which may contain subscripts, and reject that case in the C code.
15138  */
15139 qualified_name:
15140 			ColId
15141 				{
15142 					$$ = makeRangeVar(NULL, $1, @1);
15143 				}
15144 			| ColId indirection
15145 				{
15146 					check_qualified_name($2, yyscanner);
15147 					$$ = makeRangeVar(NULL, NULL, @1);
15148 					switch (list_length($2))
15149 					{
15150 						case 1:
15151 							$$->catalogname = NULL;
15152 							$$->schemaname = $1;
15153 							$$->relname = strVal(linitial($2));
15154 							break;
15155 						case 2:
15156 							$$->catalogname = $1;
15157 							$$->schemaname = strVal(linitial($2));
15158 							$$->relname = strVal(lsecond($2));
15159 							break;
15160 						default:
15161 							ereport(ERROR,
15162 									(errcode(ERRCODE_SYNTAX_ERROR),
15163 									 errmsg("improper qualified name (too many dotted names): %s",
15164 											NameListToString(lcons(makeString($1), $2))),
15165 									 parser_errposition(@1)));
15166 							break;
15167 					}
15168 				}
15169 		;
15170 
15171 name_list:	name
15172 					{ $$ = list_make1(makeString($1)); }
15173 			| name_list ',' name
15174 					{ $$ = lappend($1, makeString($3)); }
15175 		;
15176 
15177 
15178 name:		ColId									{ $$ = $1; };
15179 
15180 attr_name:	ColLabel								{ $$ = $1; };
15181 
15182 file_name:	Sconst									{ $$ = $1; };
15183 
15184 /*
15185  * The production for a qualified func_name has to exactly match the
15186  * production for a qualified columnref, because we cannot tell which we
15187  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
15188  * anything else for a columnref).  Therefore we allow 'indirection' which
15189  * may contain subscripts, and reject that case in the C code.  (If we
15190  * ever implement SQL99-like methods, such syntax may actually become legal!)
15191  */
15192 func_name:	type_function_name
15193 					{ $$ = list_make1(makeString($1)); }
15194 			| ColId indirection
15195 					{
15196 						$$ = check_func_name(lcons(makeString($1), $2),
15197 											 yyscanner);
15198 					}
15199 		;
15200 
15201 
15202 /*
15203  * Constants
15204  */
15205 AexprConst: Iconst
15206 				{
15207 					$$ = makeIntConst($1, @1);
15208 				}
15209 			| FCONST
15210 				{
15211 					$$ = makeFloatConst($1, @1);
15212 				}
15213 			| Sconst
15214 				{
15215 					$$ = makeStringConst($1, @1);
15216 				}
15217 			| BCONST
15218 				{
15219 					$$ = makeBitStringConst($1, @1);
15220 				}
15221 			| XCONST
15222 				{
15223 					/* This is a bit constant per SQL99:
15224 					 * Without Feature F511, "BIT data type",
15225 					 * a <general literal> shall not be a
15226 					 * <bit string literal> or a <hex string literal>.
15227 					 */
15228 					$$ = makeBitStringConst($1, @1);
15229 				}
15230 			| func_name Sconst
15231 				{
15232 					/* generic type 'literal' syntax */
15233 					TypeName *t = makeTypeNameFromNameList($1);
15234 					t->location = @1;
15235 					$$ = makeStringConstCast($2, @2, t);
15236 				}
15237 			| func_name '(' func_arg_list opt_sort_clause ')' Sconst
15238 				{
15239 					/* generic syntax with a type modifier */
15240 					TypeName *t = makeTypeNameFromNameList($1);
15241 					ListCell *lc;
15242 
15243 					/*
15244 					 * We must use func_arg_list and opt_sort_clause in the
15245 					 * production to avoid reduce/reduce conflicts, but we
15246 					 * don't actually wish to allow NamedArgExpr in this
15247 					 * context, nor ORDER BY.
15248 					 */
foreach(lc,$3)15249 					foreach(lc, $3)
15250 					{
15251 						NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
15252 
15253 						if (IsA(arg, NamedArgExpr))
15254 							ereport(ERROR,
15255 									(errcode(ERRCODE_SYNTAX_ERROR),
15256 									 errmsg("type modifier cannot have parameter name"),
15257 									 parser_errposition(arg->location)));
15258 					}
15259 					if ($4 != NIL)
15260 							ereport(ERROR,
15261 									(errcode(ERRCODE_SYNTAX_ERROR),
15262 									 errmsg("type modifier cannot have ORDER BY"),
15263 									 parser_errposition(@4)));
15264 
15265 					t->typmods = $3;
15266 					t->location = @1;
15267 					$$ = makeStringConstCast($6, @6, t);
15268 				}
15269 			| ConstTypename Sconst
15270 				{
15271 					$$ = makeStringConstCast($2, @2, $1);
15272 				}
15273 			| ConstInterval Sconst opt_interval
15274 				{
15275 					TypeName *t = $1;
15276 					t->typmods = $3;
15277 					$$ = makeStringConstCast($2, @2, t);
15278 				}
15279 			| ConstInterval '(' Iconst ')' Sconst
15280 				{
15281 					TypeName *t = $1;
15282 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
15283 											makeIntConst($3, @3));
15284 					$$ = makeStringConstCast($5, @5, t);
15285 				}
15286 			| TRUE_P
15287 				{
15288 					$$ = makeBoolAConst(true, @1);
15289 				}
15290 			| FALSE_P
15291 				{
15292 					$$ = makeBoolAConst(false, @1);
15293 				}
15294 			| NULL_P
15295 				{
15296 					$$ = makeNullAConst(@1);
15297 				}
15298 		;
15299 
15300 Iconst:		ICONST									{ $$ = $1; };
15301 Sconst:		SCONST									{ $$ = $1; };
15302 
15303 SignedIconst: Iconst								{ $$ = $1; }
15304 			| '+' Iconst							{ $$ = + $2; }
15305 			| '-' Iconst							{ $$ = - $2; }
15306 		;
15307 
15308 /* Role specifications */
15309 RoleId:		RoleSpec
15310 				{
15311 					RoleSpec *spc = (RoleSpec *) $1;
15312 					switch (spc->roletype)
15313 					{
15314 						case ROLESPEC_CSTRING:
15315 							$$ = spc->rolename;
15316 							break;
15317 						case ROLESPEC_PUBLIC:
15318 							ereport(ERROR,
15319 									(errcode(ERRCODE_RESERVED_NAME),
15320 									 errmsg("role name \"%s\" is reserved",
15321 											"public"),
15322 									 parser_errposition(@1)));
15323 							break;
15324 						case ROLESPEC_SESSION_USER:
15325 							ereport(ERROR,
15326 									(errcode(ERRCODE_RESERVED_NAME),
15327 									 errmsg("%s cannot be used as a role name here",
15328 											"SESSION_USER"),
15329 									 parser_errposition(@1)));
15330 							break;
15331 						case ROLESPEC_CURRENT_USER:
15332 							ereport(ERROR,
15333 									(errcode(ERRCODE_RESERVED_NAME),
15334 									 errmsg("%s cannot be used as a role name here",
15335 											"CURRENT_USER"),
15336 									 parser_errposition(@1)));
15337 							break;
15338 						case ROLESPEC_CURRENT_ROLE:
15339 							ereport(ERROR,
15340 									(errcode(ERRCODE_RESERVED_NAME),
15341 									 errmsg("%s cannot be used as a role name here",
15342 											"CURRENT_ROLE"),
15343 									 parser_errposition(@1)));
15344 							break;
15345 					}
15346 				}
15347 			;
15348 
15349 RoleSpec:	NonReservedWord
15350 					{
15351 						/*
15352 						 * "public" and "none" are not keywords, but they must
15353 						 * be treated specially here.
15354 						 */
15355 						RoleSpec *n;
15356 						if (strcmp($1, "public") == 0)
15357 						{
15358 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_PUBLIC, @1);
15359 							n->roletype = ROLESPEC_PUBLIC;
15360 						}
15361 						else if (strcmp($1, "none") == 0)
15362 						{
15363 							ereport(ERROR,
15364 									(errcode(ERRCODE_RESERVED_NAME),
15365 									 errmsg("role name \"%s\" is reserved",
15366 											"none"),
15367 									 parser_errposition(@1)));
15368 						}
15369 						else
15370 						{
15371 							n = makeRoleSpec(ROLESPEC_CSTRING, @1);
15372 							n->rolename = pstrdup($1);
15373 						}
15374 						$$ = n;
15375 					}
15376 			| CURRENT_ROLE
15377 					{
15378 						$$ = makeRoleSpec(ROLESPEC_CURRENT_ROLE, @1);
15379 					}
15380 			| CURRENT_USER
15381 					{
15382 						$$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1);
15383 					}
15384 			| SESSION_USER
15385 					{
15386 						$$ = makeRoleSpec(ROLESPEC_SESSION_USER, @1);
15387 					}
15388 		;
15389 
15390 role_list:	RoleSpec
15391 					{ $$ = list_make1($1); }
15392 			| role_list ',' RoleSpec
15393 					{ $$ = lappend($1, $3); }
15394 		;
15395 
15396 
15397 /*****************************************************************************
15398  *
15399  * PL/pgSQL extensions
15400  *
15401  * You'd think a PL/pgSQL "expression" should be just an a_expr, but
15402  * historically it can include just about anything that can follow SELECT.
15403  * Therefore the returned struct is a SelectStmt.
15404  *****************************************************************************/
15405 
15406 PLpgSQL_Expr: opt_distinct_clause opt_target_list
15407 			from_clause where_clause
15408 			group_clause having_clause window_clause
15409 			opt_sort_clause opt_select_limit opt_for_locking_clause
15410 				{
15411 					SelectStmt *n = makeNode(SelectStmt);
15412 
15413 					n->distinctClause = $1;
15414 					n->targetList = $2;
15415 					n->fromClause = $3;
15416 					n->whereClause = $4;
15417 					n->groupClause = ($5)->list;
15418 					n->groupDistinct = ($5)->distinct;
15419 					n->havingClause = $6;
15420 					n->windowClause = $7;
15421 					n->sortClause = $8;
15422 					if ($9)
15423 					{
15424 						n->limitOffset = $9->limitOffset;
15425 						n->limitCount = $9->limitCount;
15426 						if (!n->sortClause &&
15427 							$9->limitOption == LIMIT_OPTION_WITH_TIES)
15428 							ereport(ERROR,
15429 									(errcode(ERRCODE_SYNTAX_ERROR),
15430 									 errmsg("WITH TIES cannot be specified without ORDER BY clause")));
15431 						n->limitOption = $9->limitOption;
15432 					}
15433 					n->lockingClause = $10;
15434 					$$ = (Node *) n;
15435 				}
15436 		;
15437 
15438 /*
15439  * PL/pgSQL Assignment statement: name opt_indirection := PLpgSQL_Expr
15440  */
15441 
15442 PLAssignStmt: plassign_target opt_indirection plassign_equals PLpgSQL_Expr
15443 				{
15444 					PLAssignStmt *n = makeNode(PLAssignStmt);
15445 
15446 					n->name = $1;
15447 					n->indirection = check_indirection($2, yyscanner);
15448 					/* nnames will be filled by calling production */
15449 					n->val = (SelectStmt *) $4;
15450 					n->location = @1;
15451 					$$ = (Node *) n;
15452 				}
15453 		;
15454 
15455 plassign_target: ColId							{ $$ = $1; }
15456 			| PARAM								{ $$ = psprintf("$%d", $1); }
15457 		;
15458 
15459 plassign_equals: COLON_EQUALS
15460 			| '='
15461 		;
15462 
15463 
15464 /*
15465  * Name classification hierarchy.
15466  *
15467  * IDENT is the lexeme returned by the lexer for identifiers that match
15468  * no known keyword.  In most cases, we can accept certain keywords as
15469  * names, not only IDENTs.	We prefer to accept as many such keywords
15470  * as possible to minimize the impact of "reserved words" on programmers.
15471  * So, we divide names into several possible classes.  The classification
15472  * is chosen in part to make keywords acceptable as names wherever possible.
15473  */
15474 
15475 /* Column identifier --- names that can be column, table, etc names.
15476  */
15477 ColId:		IDENT									{ $$ = $1; }
15478 			| unreserved_keyword					{ $$ = pstrdup($1); }
15479 			| col_name_keyword						{ $$ = pstrdup($1); }
15480 		;
15481 
15482 /* Type/function identifier --- names that can be type or function names.
15483  */
15484 type_function_name:	IDENT							{ $$ = $1; }
15485 			| unreserved_keyword					{ $$ = pstrdup($1); }
15486 			| type_func_name_keyword				{ $$ = pstrdup($1); }
15487 		;
15488 
15489 /* Any not-fully-reserved word --- these names can be, eg, role names.
15490  */
15491 NonReservedWord:	IDENT							{ $$ = $1; }
15492 			| unreserved_keyword					{ $$ = pstrdup($1); }
15493 			| col_name_keyword						{ $$ = pstrdup($1); }
15494 			| type_func_name_keyword				{ $$ = pstrdup($1); }
15495 		;
15496 
15497 /* Column label --- allowed labels in "AS" clauses.
15498  * This presently includes *all* Postgres keywords.
15499  */
15500 ColLabel:	IDENT									{ $$ = $1; }
15501 			| unreserved_keyword					{ $$ = pstrdup($1); }
15502 			| col_name_keyword						{ $$ = pstrdup($1); }
15503 			| type_func_name_keyword				{ $$ = pstrdup($1); }
15504 			| reserved_keyword						{ $$ = pstrdup($1); }
15505 		;
15506 
15507 /* Bare column label --- names that can be column labels without writing "AS".
15508  * This classification is orthogonal to the other keyword categories.
15509  */
15510 BareColLabel:	IDENT								{ $$ = $1; }
15511 			| bare_label_keyword					{ $$ = pstrdup($1); }
15512 		;
15513 
15514 
15515 /*
15516  * Keyword category lists.  Generally, every keyword present in
15517  * the Postgres grammar should appear in exactly one of these lists.
15518  *
15519  * Put a new keyword into the first list that it can go into without causing
15520  * shift or reduce conflicts.  The earlier lists define "less reserved"
15521  * categories of keywords.
15522  *
15523  * Make sure that each keyword's category in kwlist.h matches where
15524  * it is listed here.  (Someday we may be able to generate these lists and
15525  * kwlist.h's table from one source of truth.)
15526  */
15527 
15528 /* "Unreserved" keywords --- available for use as any kind of name.
15529  */
15530 unreserved_keyword:
15531 			  ABORT_P
15532 			| ABSOLUTE_P
15533 			| ACCESS
15534 			| ACTION
15535 			| ADD_P
15536 			| ADMIN
15537 			| AFTER
15538 			| AGGREGATE
15539 			| ALSO
15540 			| ALTER
15541 			| ALWAYS
15542 			| ASENSITIVE
15543 			| ASSERTION
15544 			| ASSIGNMENT
15545 			| AT
15546 			| ATOMIC
15547 			| ATTACH
15548 			| ATTRIBUTE
15549 			| BACKWARD
15550 			| BEFORE
15551 			| BEGIN_P
15552 			| BREADTH
15553 			| BY
15554 			| CACHE
15555 			| CALL
15556 			| CALLED
15557 			| CASCADE
15558 			| CASCADED
15559 			| CATALOG_P
15560 			| CHAIN
15561 			| CHARACTERISTICS
15562 			| CHECKPOINT
15563 			| CLASS
15564 			| CLOSE
15565 			| CLUSTER
15566 			| COLUMNS
15567 			| COMMENT
15568 			| COMMENTS
15569 			| COMMIT
15570 			| COMMITTED
15571 			| COMPRESSION
15572 			| CONFIGURATION
15573 			| CONFLICT
15574 			| CONNECTION
15575 			| CONSTRAINTS
15576 			| CONTENT_P
15577 			| CONTINUE_P
15578 			| CONVERSION_P
15579 			| COPY
15580 			| COST
15581 			| CSV
15582 			| CUBE
15583 			| CURRENT_P
15584 			| CURSOR
15585 			| CYCLE
15586 			| DATA_P
15587 			| DATABASE
15588 			| DAY_P
15589 			| DEALLOCATE
15590 			| DECLARE
15591 			| DEFAULTS
15592 			| DEFERRED
15593 			| DEFINER
15594 			| DELETE_P
15595 			| DELIMITER
15596 			| DELIMITERS
15597 			| DEPENDS
15598 			| DEPTH
15599 			| DETACH
15600 			| DICTIONARY
15601 			| DISABLE_P
15602 			| DISCARD
15603 			| DOCUMENT_P
15604 			| DOMAIN_P
15605 			| DOUBLE_P
15606 			| DROP
15607 			| EACH
15608 			| ENABLE_P
15609 			| ENCODING
15610 			| ENCRYPTED
15611 			| ENUM_P
15612 			| ESCAPE
15613 			| EVENT
15614 			| EXCLUDE
15615 			| EXCLUDING
15616 			| EXCLUSIVE
15617 			| EXECUTE
15618 			| EXPLAIN
15619 			| EXPRESSION
15620 			| EXTENSION
15621 			| EXTERNAL
15622 			| FAMILY
15623 			| FILTER
15624 			| FINALIZE
15625 			| FIRST_P
15626 			| FOLLOWING
15627 			| FORCE
15628 			| FORWARD
15629 			| FUNCTION
15630 			| FUNCTIONS
15631 			| GENERATED
15632 			| GLOBAL
15633 			| GRANTED
15634 			| GROUPS
15635 			| HANDLER
15636 			| HEADER_P
15637 			| HOLD
15638 			| HOUR_P
15639 			| IDENTITY_P
15640 			| IF_P
15641 			| IMMEDIATE
15642 			| IMMUTABLE
15643 			| IMPLICIT_P
15644 			| IMPORT_P
15645 			| INCLUDE
15646 			| INCLUDING
15647 			| INCREMENT
15648 			| INDEX
15649 			| INDEXES
15650 			| INHERIT
15651 			| INHERITS
15652 			| INLINE_P
15653 			| INPUT_P
15654 			| INSENSITIVE
15655 			| INSERT
15656 			| INSTEAD
15657 			| INVOKER
15658 			| ISOLATION
15659 			| KEY
15660 			| LABEL
15661 			| LANGUAGE
15662 			| LARGE_P
15663 			| LAST_P
15664 			| LEAKPROOF
15665 			| LEVEL
15666 			| LISTEN
15667 			| LOAD
15668 			| LOCAL
15669 			| LOCATION
15670 			| LOCK_P
15671 			| LOCKED
15672 			| LOGGED
15673 			| MAPPING
15674 			| MATCH
15675 			| MATERIALIZED
15676 			| MAXVALUE
15677 			| METHOD
15678 			| MINUTE_P
15679 			| MINVALUE
15680 			| MODE
15681 			| MONTH_P
15682 			| MOVE
15683 			| NAME_P
15684 			| NAMES
15685 			| NEW
15686 			| NEXT
15687 			| NFC
15688 			| NFD
15689 			| NFKC
15690 			| NFKD
15691 			| NO
15692 			| NORMALIZED
15693 			| NOTHING
15694 			| NOTIFY
15695 			| NOWAIT
15696 			| NULLS_P
15697 			| OBJECT_P
15698 			| OF
15699 			| OFF
15700 			| OIDS
15701 			| OLD
15702 			| OPERATOR
15703 			| OPTION
15704 			| OPTIONS
15705 			| ORDINALITY
15706 			| OTHERS
15707 			| OVER
15708 			| OVERRIDING
15709 			| OWNED
15710 			| OWNER
15711 			| PARALLEL
15712 			| PARSER
15713 			| PARTIAL
15714 			| PARTITION
15715 			| PASSING
15716 			| PASSWORD
15717 			| PLANS
15718 			| POLICY
15719 			| PRECEDING
15720 			| PREPARE
15721 			| PREPARED
15722 			| PRESERVE
15723 			| PRIOR
15724 			| PRIVILEGES
15725 			| PROCEDURAL
15726 			| PROCEDURE
15727 			| PROCEDURES
15728 			| PROGRAM
15729 			| PUBLICATION
15730 			| QUOTE
15731 			| RANGE
15732 			| READ
15733 			| REASSIGN
15734 			| RECHECK
15735 			| RECURSIVE
15736 			| REF
15737 			| REFERENCING
15738 			| REFRESH
15739 			| REINDEX
15740 			| RELATIVE_P
15741 			| RELEASE
15742 			| RENAME
15743 			| REPEATABLE
15744 			| REPLACE
15745 			| REPLICA
15746 			| RESET
15747 			| RESTART
15748 			| RESTRICT
15749 			| RETURN
15750 			| RETURNS
15751 			| REVOKE
15752 			| ROLE
15753 			| ROLLBACK
15754 			| ROLLUP
15755 			| ROUTINE
15756 			| ROUTINES
15757 			| ROWS
15758 			| RULE
15759 			| SAVEPOINT
15760 			| SCHEMA
15761 			| SCHEMAS
15762 			| SCROLL
15763 			| SEARCH
15764 			| SECOND_P
15765 			| SECURITY
15766 			| SEQUENCE
15767 			| SEQUENCES
15768 			| SERIALIZABLE
15769 			| SERVER
15770 			| SESSION
15771 			| SET
15772 			| SETS
15773 			| SHARE
15774 			| SHOW
15775 			| SIMPLE
15776 			| SKIP
15777 			| SNAPSHOT
15778 			| SQL_P
15779 			| STABLE
15780 			| STANDALONE_P
15781 			| START
15782 			| STATEMENT
15783 			| STATISTICS
15784 			| STDIN
15785 			| STDOUT
15786 			| STORAGE
15787 			| STORED
15788 			| STRICT_P
15789 			| STRIP_P
15790 			| SUBSCRIPTION
15791 			| SUPPORT
15792 			| SYSID
15793 			| SYSTEM_P
15794 			| TABLES
15795 			| TABLESPACE
15796 			| TEMP
15797 			| TEMPLATE
15798 			| TEMPORARY
15799 			| TEXT_P
15800 			| TIES
15801 			| TRANSACTION
15802 			| TRANSFORM
15803 			| TRIGGER
15804 			| TRUNCATE
15805 			| TRUSTED
15806 			| TYPE_P
15807 			| TYPES_P
15808 			| UESCAPE
15809 			| UNBOUNDED
15810 			| UNCOMMITTED
15811 			| UNENCRYPTED
15812 			| UNKNOWN
15813 			| UNLISTEN
15814 			| UNLOGGED
15815 			| UNTIL
15816 			| UPDATE
15817 			| VACUUM
15818 			| VALID
15819 			| VALIDATE
15820 			| VALIDATOR
15821 			| VALUE_P
15822 			| VARYING
15823 			| VERSION_P
15824 			| VIEW
15825 			| VIEWS
15826 			| VOLATILE
15827 			| WHITESPACE_P
15828 			| WITHIN
15829 			| WITHOUT
15830 			| WORK
15831 			| WRAPPER
15832 			| WRITE
15833 			| XML_P
15834 			| YEAR_P
15835 			| YES_P
15836 			| ZONE
15837 		;
15838 
15839 /* Column identifier --- keywords that can be column, table, etc names.
15840  *
15841  * Many of these keywords will in fact be recognized as type or function
15842  * names too; but they have special productions for the purpose, and so
15843  * can't be treated as "generic" type or function names.
15844  *
15845  * The type names appearing here are not usable as function names
15846  * because they can be followed by '(' in typename productions, which
15847  * looks too much like a function call for an LR(1) parser.
15848  */
15849 col_name_keyword:
15850 			  BETWEEN
15851 			| BIGINT
15852 			| BIT
15853 			| BOOLEAN_P
15854 			| CHAR_P
15855 			| CHARACTER
15856 			| COALESCE
15857 			| DEC
15858 			| DECIMAL_P
15859 			| EXISTS
15860 			| EXTRACT
15861 			| FLOAT_P
15862 			| GREATEST
15863 			| GROUPING
15864 			| INOUT
15865 			| INT_P
15866 			| INTEGER
15867 			| INTERVAL
15868 			| LEAST
15869 			| NATIONAL
15870 			| NCHAR
15871 			| NONE
15872 			| NORMALIZE
15873 			| NULLIF
15874 			| NUMERIC
15875 			| OUT_P
15876 			| OVERLAY
15877 			| POSITION
15878 			| PRECISION
15879 			| REAL
15880 			| ROW
15881 			| SETOF
15882 			| SMALLINT
15883 			| SUBSTRING
15884 			| TIME
15885 			| TIMESTAMP
15886 			| TREAT
15887 			| TRIM
15888 			| VALUES
15889 			| VARCHAR
15890 			| XMLATTRIBUTES
15891 			| XMLCONCAT
15892 			| XMLELEMENT
15893 			| XMLEXISTS
15894 			| XMLFOREST
15895 			| XMLNAMESPACES
15896 			| XMLPARSE
15897 			| XMLPI
15898 			| XMLROOT
15899 			| XMLSERIALIZE
15900 			| XMLTABLE
15901 		;
15902 
15903 /* Type/function identifier --- keywords that can be type or function names.
15904  *
15905  * Most of these are keywords that are used as operators in expressions;
15906  * in general such keywords can't be column names because they would be
15907  * ambiguous with variables, but they are unambiguous as function identifiers.
15908  *
15909  * Do not include POSITION, SUBSTRING, etc here since they have explicit
15910  * productions in a_expr to support the goofy SQL9x argument syntax.
15911  * - thomas 2000-11-28
15912  */
15913 type_func_name_keyword:
15914 			  AUTHORIZATION
15915 			| BINARY
15916 			| COLLATION
15917 			| CONCURRENTLY
15918 			| CROSS
15919 			| CURRENT_SCHEMA
15920 			| FREEZE
15921 			| FULL
15922 			| ILIKE
15923 			| INNER_P
15924 			| IS
15925 			| ISNULL
15926 			| JOIN
15927 			| LEFT
15928 			| LIKE
15929 			| NATURAL
15930 			| NOTNULL
15931 			| OUTER_P
15932 			| OVERLAPS
15933 			| RIGHT
15934 			| SIMILAR
15935 			| TABLESAMPLE
15936 			| VERBOSE
15937 		;
15938 
15939 /* Reserved keyword --- these keywords are usable only as a ColLabel.
15940  *
15941  * Keywords appear here if they could not be distinguished from variable,
15942  * type, or function names in some contexts.  Don't put things here unless
15943  * forced to.
15944  */
15945 reserved_keyword:
15946 			  ALL
15947 			| ANALYSE
15948 			| ANALYZE
15949 			| AND
15950 			| ANY
15951 			| ARRAY
15952 			| AS
15953 			| ASC
15954 			| ASYMMETRIC
15955 			| BOTH
15956 			| CASE
15957 			| CAST
15958 			| CHECK
15959 			| COLLATE
15960 			| COLUMN
15961 			| CONSTRAINT
15962 			| CREATE
15963 			| CURRENT_CATALOG
15964 			| CURRENT_DATE
15965 			| CURRENT_ROLE
15966 			| CURRENT_TIME
15967 			| CURRENT_TIMESTAMP
15968 			| CURRENT_USER
15969 			| DEFAULT
15970 			| DEFERRABLE
15971 			| DESC
15972 			| DISTINCT
15973 			| DO
15974 			| ELSE
15975 			| END_P
15976 			| EXCEPT
15977 			| FALSE_P
15978 			| FETCH
15979 			| FOR
15980 			| FOREIGN
15981 			| FROM
15982 			| GRANT
15983 			| GROUP_P
15984 			| HAVING
15985 			| IN_P
15986 			| INITIALLY
15987 			| INTERSECT
15988 			| INTO
15989 			| LATERAL_P
15990 			| LEADING
15991 			| LIMIT
15992 			| LOCALTIME
15993 			| LOCALTIMESTAMP
15994 			| NOT
15995 			| NULL_P
15996 			| OFFSET
15997 			| ON
15998 			| ONLY
15999 			| OR
16000 			| ORDER
16001 			| PLACING
16002 			| PRIMARY
16003 			| REFERENCES
16004 			| RETURNING
16005 			| SELECT
16006 			| SESSION_USER
16007 			| SOME
16008 			| SYMMETRIC
16009 			| TABLE
16010 			| THEN
16011 			| TO
16012 			| TRAILING
16013 			| TRUE_P
16014 			| UNION
16015 			| UNIQUE
16016 			| USER
16017 			| USING
16018 			| VARIADIC
16019 			| WHEN
16020 			| WHERE
16021 			| WINDOW
16022 			| WITH
16023 		;
16024 
16025 /*
16026  * While all keywords can be used as column labels when preceded by AS,
16027  * not all of them can be used as a "bare" column label without AS.
16028  * Those that can be used as a bare label must be listed here,
16029  * in addition to appearing in one of the category lists above.
16030  *
16031  * Always add a new keyword to this list if possible.  Mark it BARE_LABEL
16032  * in kwlist.h if it is included here, or AS_LABEL if it is not.
16033  */
16034 bare_label_keyword:
16035 			  ABORT_P
16036 			| ABSOLUTE_P
16037 			| ACCESS
16038 			| ACTION
16039 			| ADD_P
16040 			| ADMIN
16041 			| AFTER
16042 			| AGGREGATE
16043 			| ALL
16044 			| ALSO
16045 			| ALTER
16046 			| ALWAYS
16047 			| ANALYSE
16048 			| ANALYZE
16049 			| AND
16050 			| ANY
16051 			| ASC
16052 			| ASENSITIVE
16053 			| ASSERTION
16054 			| ASSIGNMENT
16055 			| ASYMMETRIC
16056 			| AT
16057 			| ATOMIC
16058 			| ATTACH
16059 			| ATTRIBUTE
16060 			| AUTHORIZATION
16061 			| BACKWARD
16062 			| BEFORE
16063 			| BEGIN_P
16064 			| BETWEEN
16065 			| BIGINT
16066 			| BINARY
16067 			| BIT
16068 			| BOOLEAN_P
16069 			| BOTH
16070 			| BREADTH
16071 			| BY
16072 			| CACHE
16073 			| CALL
16074 			| CALLED
16075 			| CASCADE
16076 			| CASCADED
16077 			| CASE
16078 			| CAST
16079 			| CATALOG_P
16080 			| CHAIN
16081 			| CHARACTERISTICS
16082 			| CHECK
16083 			| CHECKPOINT
16084 			| CLASS
16085 			| CLOSE
16086 			| CLUSTER
16087 			| COALESCE
16088 			| COLLATE
16089 			| COLLATION
16090 			| COLUMN
16091 			| COLUMNS
16092 			| COMMENT
16093 			| COMMENTS
16094 			| COMMIT
16095 			| COMMITTED
16096 			| COMPRESSION
16097 			| CONCURRENTLY
16098 			| CONFIGURATION
16099 			| CONFLICT
16100 			| CONNECTION
16101 			| CONSTRAINT
16102 			| CONSTRAINTS
16103 			| CONTENT_P
16104 			| CONTINUE_P
16105 			| CONVERSION_P
16106 			| COPY
16107 			| COST
16108 			| CROSS
16109 			| CSV
16110 			| CUBE
16111 			| CURRENT_P
16112 			| CURRENT_CATALOG
16113 			| CURRENT_DATE
16114 			| CURRENT_ROLE
16115 			| CURRENT_SCHEMA
16116 			| CURRENT_TIME
16117 			| CURRENT_TIMESTAMP
16118 			| CURRENT_USER
16119 			| CURSOR
16120 			| CYCLE
16121 			| DATA_P
16122 			| DATABASE
16123 			| DEALLOCATE
16124 			| DEC
16125 			| DECIMAL_P
16126 			| DECLARE
16127 			| DEFAULT
16128 			| DEFAULTS
16129 			| DEFERRABLE
16130 			| DEFERRED
16131 			| DEFINER
16132 			| DELETE_P
16133 			| DELIMITER
16134 			| DELIMITERS
16135 			| DEPENDS
16136 			| DEPTH
16137 			| DESC
16138 			| DETACH
16139 			| DICTIONARY
16140 			| DISABLE_P
16141 			| DISCARD
16142 			| DISTINCT
16143 			| DO
16144 			| DOCUMENT_P
16145 			| DOMAIN_P
16146 			| DOUBLE_P
16147 			| DROP
16148 			| EACH
16149 			| ELSE
16150 			| ENABLE_P
16151 			| ENCODING
16152 			| ENCRYPTED
16153 			| END_P
16154 			| ENUM_P
16155 			| ESCAPE
16156 			| EVENT
16157 			| EXCLUDE
16158 			| EXCLUDING
16159 			| EXCLUSIVE
16160 			| EXECUTE
16161 			| EXISTS
16162 			| EXPLAIN
16163 			| EXPRESSION
16164 			| EXTENSION
16165 			| EXTERNAL
16166 			| EXTRACT
16167 			| FALSE_P
16168 			| FAMILY
16169 			| FINALIZE
16170 			| FIRST_P
16171 			| FLOAT_P
16172 			| FOLLOWING
16173 			| FORCE
16174 			| FOREIGN
16175 			| FORWARD
16176 			| FREEZE
16177 			| FULL
16178 			| FUNCTION
16179 			| FUNCTIONS
16180 			| GENERATED
16181 			| GLOBAL
16182 			| GRANTED
16183 			| GREATEST
16184 			| GROUPING
16185 			| GROUPS
16186 			| HANDLER
16187 			| HEADER_P
16188 			| HOLD
16189 			| IDENTITY_P
16190 			| IF_P
16191 			| ILIKE
16192 			| IMMEDIATE
16193 			| IMMUTABLE
16194 			| IMPLICIT_P
16195 			| IMPORT_P
16196 			| IN_P
16197 			| INCLUDE
16198 			| INCLUDING
16199 			| INCREMENT
16200 			| INDEX
16201 			| INDEXES
16202 			| INHERIT
16203 			| INHERITS
16204 			| INITIALLY
16205 			| INLINE_P
16206 			| INNER_P
16207 			| INOUT
16208 			| INPUT_P
16209 			| INSENSITIVE
16210 			| INSERT
16211 			| INSTEAD
16212 			| INT_P
16213 			| INTEGER
16214 			| INTERVAL
16215 			| INVOKER
16216 			| IS
16217 			| ISOLATION
16218 			| JOIN
16219 			| KEY
16220 			| LABEL
16221 			| LANGUAGE
16222 			| LARGE_P
16223 			| LAST_P
16224 			| LATERAL_P
16225 			| LEADING
16226 			| LEAKPROOF
16227 			| LEAST
16228 			| LEFT
16229 			| LEVEL
16230 			| LIKE
16231 			| LISTEN
16232 			| LOAD
16233 			| LOCAL
16234 			| LOCALTIME
16235 			| LOCALTIMESTAMP
16236 			| LOCATION
16237 			| LOCK_P
16238 			| LOCKED
16239 			| LOGGED
16240 			| MAPPING
16241 			| MATCH
16242 			| MATERIALIZED
16243 			| MAXVALUE
16244 			| METHOD
16245 			| MINVALUE
16246 			| MODE
16247 			| MOVE
16248 			| NAME_P
16249 			| NAMES
16250 			| NATIONAL
16251 			| NATURAL
16252 			| NCHAR
16253 			| NEW
16254 			| NEXT
16255 			| NFC
16256 			| NFD
16257 			| NFKC
16258 			| NFKD
16259 			| NO
16260 			| NONE
16261 			| NORMALIZE
16262 			| NORMALIZED
16263 			| NOT
16264 			| NOTHING
16265 			| NOTIFY
16266 			| NOWAIT
16267 			| NULL_P
16268 			| NULLIF
16269 			| NULLS_P
16270 			| NUMERIC
16271 			| OBJECT_P
16272 			| OF
16273 			| OFF
16274 			| OIDS
16275 			| OLD
16276 			| ONLY
16277 			| OPERATOR
16278 			| OPTION
16279 			| OPTIONS
16280 			| OR
16281 			| ORDINALITY
16282 			| OTHERS
16283 			| OUT_P
16284 			| OUTER_P
16285 			| OVERLAY
16286 			| OVERRIDING
16287 			| OWNED
16288 			| OWNER
16289 			| PARALLEL
16290 			| PARSER
16291 			| PARTIAL
16292 			| PARTITION
16293 			| PASSING
16294 			| PASSWORD
16295 			| PLACING
16296 			| PLANS
16297 			| POLICY
16298 			| POSITION
16299 			| PRECEDING
16300 			| PREPARE
16301 			| PREPARED
16302 			| PRESERVE
16303 			| PRIMARY
16304 			| PRIOR
16305 			| PRIVILEGES
16306 			| PROCEDURAL
16307 			| PROCEDURE
16308 			| PROCEDURES
16309 			| PROGRAM
16310 			| PUBLICATION
16311 			| QUOTE
16312 			| RANGE
16313 			| READ
16314 			| REAL
16315 			| REASSIGN
16316 			| RECHECK
16317 			| RECURSIVE
16318 			| REF
16319 			| REFERENCES
16320 			| REFERENCING
16321 			| REFRESH
16322 			| REINDEX
16323 			| RELATIVE_P
16324 			| RELEASE
16325 			| RENAME
16326 			| REPEATABLE
16327 			| REPLACE
16328 			| REPLICA
16329 			| RESET
16330 			| RESTART
16331 			| RESTRICT
16332 			| RETURN
16333 			| RETURNS
16334 			| REVOKE
16335 			| RIGHT
16336 			| ROLE
16337 			| ROLLBACK
16338 			| ROLLUP
16339 			| ROUTINE
16340 			| ROUTINES
16341 			| ROW
16342 			| ROWS
16343 			| RULE
16344 			| SAVEPOINT
16345 			| SCHEMA
16346 			| SCHEMAS
16347 			| SCROLL
16348 			| SEARCH
16349 			| SECURITY
16350 			| SELECT
16351 			| SEQUENCE
16352 			| SEQUENCES
16353 			| SERIALIZABLE
16354 			| SERVER
16355 			| SESSION
16356 			| SESSION_USER
16357 			| SET
16358 			| SETOF
16359 			| SETS
16360 			| SHARE
16361 			| SHOW
16362 			| SIMILAR
16363 			| SIMPLE
16364 			| SKIP
16365 			| SMALLINT
16366 			| SNAPSHOT
16367 			| SOME
16368 			| SQL_P
16369 			| STABLE
16370 			| STANDALONE_P
16371 			| START
16372 			| STATEMENT
16373 			| STATISTICS
16374 			| STDIN
16375 			| STDOUT
16376 			| STORAGE
16377 			| STORED
16378 			| STRICT_P
16379 			| STRIP_P
16380 			| SUBSCRIPTION
16381 			| SUBSTRING
16382 			| SUPPORT
16383 			| SYMMETRIC
16384 			| SYSID
16385 			| SYSTEM_P
16386 			| TABLE
16387 			| TABLES
16388 			| TABLESAMPLE
16389 			| TABLESPACE
16390 			| TEMP
16391 			| TEMPLATE
16392 			| TEMPORARY
16393 			| TEXT_P
16394 			| THEN
16395 			| TIES
16396 			| TIME
16397 			| TIMESTAMP
16398 			| TRAILING
16399 			| TRANSACTION
16400 			| TRANSFORM
16401 			| TREAT
16402 			| TRIGGER
16403 			| TRIM
16404 			| TRUE_P
16405 			| TRUNCATE
16406 			| TRUSTED
16407 			| TYPE_P
16408 			| TYPES_P
16409 			| UESCAPE
16410 			| UNBOUNDED
16411 			| UNCOMMITTED
16412 			| UNENCRYPTED
16413 			| UNIQUE
16414 			| UNKNOWN
16415 			| UNLISTEN
16416 			| UNLOGGED
16417 			| UNTIL
16418 			| UPDATE
16419 			| USER
16420 			| USING
16421 			| VACUUM
16422 			| VALID
16423 			| VALIDATE
16424 			| VALIDATOR
16425 			| VALUE_P
16426 			| VALUES
16427 			| VARCHAR
16428 			| VARIADIC
16429 			| VERBOSE
16430 			| VERSION_P
16431 			| VIEW
16432 			| VIEWS
16433 			| VOLATILE
16434 			| WHEN
16435 			| WHITESPACE_P
16436 			| WORK
16437 			| WRAPPER
16438 			| WRITE
16439 			| XML_P
16440 			| XMLATTRIBUTES
16441 			| XMLCONCAT
16442 			| XMLELEMENT
16443 			| XMLEXISTS
16444 			| XMLFOREST
16445 			| XMLNAMESPACES
16446 			| XMLPARSE
16447 			| XMLPI
16448 			| XMLROOT
16449 			| XMLSERIALIZE
16450 			| XMLTABLE
16451 			| YES_P
16452 			| ZONE
16453 		;
16454 
16455 %%
16456 
16457 /*
16458  * The signature of this function is required by bison.  However, we
16459  * ignore the passed yylloc and instead use the last token position
16460  * available from the scanner.
16461  */
16462 static void
16463 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
16464 {
16465 	parser_yyerror(msg);
16466 }
16467 
16468 static RawStmt *
makeRawStmt(Node * stmt,int stmt_location)16469 makeRawStmt(Node *stmt, int stmt_location)
16470 {
16471 	RawStmt    *rs = makeNode(RawStmt);
16472 
16473 	rs->stmt = stmt;
16474 	rs->stmt_location = stmt_location;
16475 	rs->stmt_len = 0;			/* might get changed later */
16476 	return rs;
16477 }
16478 
16479 /* Adjust a RawStmt to reflect that it doesn't run to the end of the string */
16480 static void
updateRawStmtEnd(RawStmt * rs,int end_location)16481 updateRawStmtEnd(RawStmt *rs, int end_location)
16482 {
16483 	/*
16484 	 * If we already set the length, don't change it.  This is for situations
16485 	 * like "select foo ;; select bar" where the same statement will be last
16486 	 * in the string for more than one semicolon.
16487 	 */
16488 	if (rs->stmt_len > 0)
16489 		return;
16490 
16491 	/* OK, update length of RawStmt */
16492 	rs->stmt_len = end_location - rs->stmt_location;
16493 }
16494 
16495 static Node *
makeColumnRef(char * colname,List * indirection,int location,core_yyscan_t yyscanner)16496 makeColumnRef(char *colname, List *indirection,
16497 			  int location, core_yyscan_t yyscanner)
16498 {
16499 	/*
16500 	 * Generate a ColumnRef node, with an A_Indirection node added if there
16501 	 * is any subscripting in the specified indirection list.  However,
16502 	 * any field selection at the start of the indirection list must be
16503 	 * transposed into the "fields" part of the ColumnRef node.
16504 	 */
16505 	ColumnRef  *c = makeNode(ColumnRef);
16506 	int		nfields = 0;
16507 	ListCell *l;
16508 
16509 	c->location = location;
16510 	foreach(l, indirection)
16511 	{
16512 		if (IsA(lfirst(l), A_Indices))
16513 		{
16514 			A_Indirection *i = makeNode(A_Indirection);
16515 
16516 			if (nfields == 0)
16517 			{
16518 				/* easy case - all indirection goes to A_Indirection */
16519 				c->fields = list_make1(makeString(colname));
16520 				i->indirection = check_indirection(indirection, yyscanner);
16521 			}
16522 			else
16523 			{
16524 				/* got to split the list in two */
16525 				i->indirection = check_indirection(list_copy_tail(indirection,
16526 																  nfields),
16527 												   yyscanner);
16528 				indirection = list_truncate(indirection, nfields);
16529 				c->fields = lcons(makeString(colname), indirection);
16530 			}
16531 			i->arg = (Node *) c;
16532 			return (Node *) i;
16533 		}
16534 		else if (IsA(lfirst(l), A_Star))
16535 		{
16536 			/* We only allow '*' at the end of a ColumnRef */
16537 			if (lnext(indirection, l) != NULL)
16538 				parser_yyerror("improper use of \"*\"");
16539 		}
16540 		nfields++;
16541 	}
16542 	/* No subscripting, so all indirection gets added to field list */
16543 	c->fields = lcons(makeString(colname), indirection);
16544 	return (Node *) c;
16545 }
16546 
16547 Node *
makeTypeCast(Node * arg,TypeName * typename,int location)16548 makeTypeCast(Node *arg, TypeName *typename, int location)
16549 {
16550 	TypeCast *n = makeNode(TypeCast);
16551 	n->arg = arg;
16552 	n->typeName = typename;
16553 	n->location = location;
16554 	return (Node *) n;
16555 }
16556 
16557 static Node *
makeStringConst(char * str,int location)16558 makeStringConst(char *str, int location)
16559 {
16560 	A_Const *n = makeNode(A_Const);
16561 
16562 	n->val.type = T_String;
16563 	n->val.val.str = str;
16564 	n->location = location;
16565 
16566 	return (Node *)n;
16567 }
16568 
16569 Node *
makeStringConstCast(char * str,int location,TypeName * typename)16570 makeStringConstCast(char *str, int location, TypeName *typename)
16571 {
16572 	Node *s = makeStringConst(str, location);
16573 
16574 	return makeTypeCast(s, typename, -1);
16575 }
16576 
16577 Node *
makeIntConst(int val,int location)16578 makeIntConst(int val, int location)
16579 {
16580 	A_Const *n = makeNode(A_Const);
16581 
16582 	n->val.type = T_Integer;
16583 	n->val.val.ival = val;
16584 	n->location = location;
16585 
16586 	return (Node *)n;
16587 }
16588 
16589 static Node *
makeFloatConst(char * str,int location)16590 makeFloatConst(char *str, int location)
16591 {
16592 	A_Const *n = makeNode(A_Const);
16593 
16594 	n->val.type = T_Float;
16595 	n->val.val.str = str;
16596 	n->location = location;
16597 
16598 	return (Node *)n;
16599 }
16600 
16601 static Node *
makeBitStringConst(char * str,int location)16602 makeBitStringConst(char *str, int location)
16603 {
16604 	A_Const *n = makeNode(A_Const);
16605 
16606 	n->val.type = T_BitString;
16607 	n->val.val.str = str;
16608 	n->location = location;
16609 
16610 	return (Node *)n;
16611 }
16612 
16613 static Node *
makeNullAConst(int location)16614 makeNullAConst(int location)
16615 {
16616 	A_Const *n = makeNode(A_Const);
16617 
16618 	n->val.type = T_Null;
16619 	n->location = location;
16620 
16621 	return (Node *)n;
16622 }
16623 
16624 static Node *
makeAConst(Value * v,int location)16625 makeAConst(Value *v, int location)
16626 {
16627 	Node *n;
16628 
16629 	switch (v->type)
16630 	{
16631 		case T_Float:
16632 			n = makeFloatConst(v->val.str, location);
16633 			break;
16634 
16635 		case T_Integer:
16636 			n = makeIntConst(v->val.ival, location);
16637 			break;
16638 
16639 		case T_String:
16640 		default:
16641 			n = makeStringConst(v->val.str, location);
16642 			break;
16643 	}
16644 
16645 	return n;
16646 }
16647 
16648 /* makeBoolAConst()
16649  * Create an A_Const string node and put it inside a boolean cast.
16650  */
16651 static Node *
makeBoolAConst(bool state,int location)16652 makeBoolAConst(bool state, int location)
16653 {
16654 	A_Const *n = makeNode(A_Const);
16655 
16656 	n->val.type = T_String;
16657 	n->val.val.str = (state ? "t" : "f");
16658 	n->location = location;
16659 
16660 	return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
16661 }
16662 
16663 /* makeRoleSpec
16664  * Create a RoleSpec with the given type
16665  */
16666 static RoleSpec *
makeRoleSpec(RoleSpecType type,int location)16667 makeRoleSpec(RoleSpecType type, int location)
16668 {
16669 	RoleSpec *spec = makeNode(RoleSpec);
16670 
16671 	spec->roletype = type;
16672 	spec->location = location;
16673 
16674 	return spec;
16675 }
16676 
16677 /* check_qualified_name --- check the result of qualified_name production
16678  *
16679  * It's easiest to let the grammar production for qualified_name allow
16680  * subscripts and '*', which we then must reject here.
16681  */
16682 static void
check_qualified_name(List * names,core_yyscan_t yyscanner)16683 check_qualified_name(List *names, core_yyscan_t yyscanner)
16684 {
16685 	ListCell   *i;
16686 
16687 	foreach(i, names)
16688 	{
16689 		if (!IsA(lfirst(i), String))
16690 			parser_yyerror("syntax error");
16691 	}
16692 }
16693 
16694 /* check_func_name --- check the result of func_name production
16695  *
16696  * It's easiest to let the grammar production for func_name allow subscripts
16697  * and '*', which we then must reject here.
16698  */
16699 static List *
check_func_name(List * names,core_yyscan_t yyscanner)16700 check_func_name(List *names, core_yyscan_t yyscanner)
16701 {
16702 	ListCell   *i;
16703 
16704 	foreach(i, names)
16705 	{
16706 		if (!IsA(lfirst(i), String))
16707 			parser_yyerror("syntax error");
16708 	}
16709 	return names;
16710 }
16711 
16712 /* check_indirection --- check the result of indirection production
16713  *
16714  * We only allow '*' at the end of the list, but it's hard to enforce that
16715  * in the grammar, so do it here.
16716  */
16717 static List *
check_indirection(List * indirection,core_yyscan_t yyscanner)16718 check_indirection(List *indirection, core_yyscan_t yyscanner)
16719 {
16720 	ListCell *l;
16721 
16722 	foreach(l, indirection)
16723 	{
16724 		if (IsA(lfirst(l), A_Star))
16725 		{
16726 			if (lnext(indirection, l) != NULL)
16727 				parser_yyerror("improper use of \"*\"");
16728 		}
16729 	}
16730 	return indirection;
16731 }
16732 
16733 /* extractArgTypes()
16734  * Given a list of FunctionParameter nodes, extract a list of just the
16735  * argument types (TypeNames) for input parameters only.  This is what
16736  * is needed to look up an existing function, which is what is wanted by
16737  * the productions that use this call.
16738  */
16739 static List *
extractArgTypes(List * parameters)16740 extractArgTypes(List *parameters)
16741 {
16742 	List	   *result = NIL;
16743 	ListCell   *i;
16744 
16745 	foreach(i, parameters)
16746 	{
16747 		FunctionParameter *p = (FunctionParameter *) lfirst(i);
16748 
16749 		if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
16750 			result = lappend(result, p->argType);
16751 	}
16752 	return result;
16753 }
16754 
16755 /* extractAggrArgTypes()
16756  * As above, but work from the output of the aggr_args production.
16757  */
16758 static List *
extractAggrArgTypes(List * aggrargs)16759 extractAggrArgTypes(List *aggrargs)
16760 {
16761 	Assert(list_length(aggrargs) == 2);
16762 	return extractArgTypes((List *) linitial(aggrargs));
16763 }
16764 
16765 /* makeOrderedSetArgs()
16766  * Build the result of the aggr_args production (which see the comments for).
16767  * This handles only the case where both given lists are nonempty, so that
16768  * we have to deal with multiple VARIADIC arguments.
16769  */
16770 static List *
makeOrderedSetArgs(List * directargs,List * orderedargs,core_yyscan_t yyscanner)16771 makeOrderedSetArgs(List *directargs, List *orderedargs,
16772 				   core_yyscan_t yyscanner)
16773 {
16774 	FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
16775 	Value	   *ndirectargs;
16776 
16777 	/* No restriction unless last direct arg is VARIADIC */
16778 	if (lastd->mode == FUNC_PARAM_VARIADIC)
16779 	{
16780 		FunctionParameter *firsto = (FunctionParameter *) linitial(orderedargs);
16781 
16782 		/*
16783 		 * We ignore the names, though the aggr_arg production allows them;
16784 		 * it doesn't allow default values, so those need not be checked.
16785 		 */
16786 		if (list_length(orderedargs) != 1 ||
16787 			firsto->mode != FUNC_PARAM_VARIADIC)
16788 			ereport(ERROR,
16789 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16790 					 errmsg("an ordered-set aggregate with a VARIADIC direct argument must have one VARIADIC aggregated argument of the same data type"),
16791 					 parser_errposition(exprLocation((Node *) firsto))));
16792 
16793 		/* OK, drop the duplicate VARIADIC argument from the internal form */
16794 		orderedargs = NIL;
16795 	}
16796 
16797 	/* don't merge into the next line, as list_concat changes directargs */
16798 	ndirectargs = makeInteger(list_length(directargs));
16799 
16800 	return list_make2(list_concat(directargs, orderedargs),
16801 					  ndirectargs);
16802 }
16803 
16804 /* insertSelectOptions()
16805  * Insert ORDER BY, etc into an already-constructed SelectStmt.
16806  *
16807  * This routine is just to avoid duplicating code in SelectStmt productions.
16808  */
16809 static void
insertSelectOptions(SelectStmt * stmt,List * sortClause,List * lockingClause,SelectLimit * limitClause,WithClause * withClause,core_yyscan_t yyscanner)16810 insertSelectOptions(SelectStmt *stmt,
16811 					List *sortClause, List *lockingClause,
16812 					SelectLimit *limitClause,
16813 					WithClause *withClause,
16814 					core_yyscan_t yyscanner)
16815 {
16816 	Assert(IsA(stmt, SelectStmt));
16817 
16818 	/*
16819 	 * Tests here are to reject constructs like
16820 	 *	(SELECT foo ORDER BY bar) ORDER BY baz
16821 	 */
16822 	if (sortClause)
16823 	{
16824 		if (stmt->sortClause)
16825 			ereport(ERROR,
16826 					(errcode(ERRCODE_SYNTAX_ERROR),
16827 					 errmsg("multiple ORDER BY clauses not allowed"),
16828 					 parser_errposition(exprLocation((Node *) sortClause))));
16829 		stmt->sortClause = sortClause;
16830 	}
16831 	/* We can handle multiple locking clauses, though */
16832 	stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
16833 	if (limitClause && limitClause->limitOffset)
16834 	{
16835 		if (stmt->limitOffset)
16836 			ereport(ERROR,
16837 					(errcode(ERRCODE_SYNTAX_ERROR),
16838 					 errmsg("multiple OFFSET clauses not allowed"),
16839 					 parser_errposition(exprLocation(limitClause->limitOffset))));
16840 		stmt->limitOffset = limitClause->limitOffset;
16841 	}
16842 	if (limitClause && limitClause->limitCount)
16843 	{
16844 		if (stmt->limitCount)
16845 			ereport(ERROR,
16846 					(errcode(ERRCODE_SYNTAX_ERROR),
16847 					 errmsg("multiple LIMIT clauses not allowed"),
16848 					 parser_errposition(exprLocation(limitClause->limitCount))));
16849 		stmt->limitCount = limitClause->limitCount;
16850 	}
16851 	if (limitClause && limitClause->limitOption != LIMIT_OPTION_DEFAULT)
16852 	{
16853 		if (stmt->limitOption)
16854 			ereport(ERROR,
16855 					(errcode(ERRCODE_SYNTAX_ERROR),
16856 					 errmsg("multiple limit options not allowed")));
16857 		if (!stmt->sortClause && limitClause->limitOption == LIMIT_OPTION_WITH_TIES)
16858 			ereport(ERROR,
16859 					(errcode(ERRCODE_SYNTAX_ERROR),
16860 					 errmsg("WITH TIES cannot be specified without ORDER BY clause")));
16861 		stmt->limitOption = limitClause->limitOption;
16862 	}
16863 	if (withClause)
16864 	{
16865 		if (stmt->withClause)
16866 			ereport(ERROR,
16867 					(errcode(ERRCODE_SYNTAX_ERROR),
16868 					 errmsg("multiple WITH clauses not allowed"),
16869 					 parser_errposition(exprLocation((Node *) withClause))));
16870 		stmt->withClause = withClause;
16871 	}
16872 }
16873 
16874 static Node *
makeSetOp(SetOperation op,bool all,Node * larg,Node * rarg)16875 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
16876 {
16877 	SelectStmt *n = makeNode(SelectStmt);
16878 
16879 	n->op = op;
16880 	n->all = all;
16881 	n->larg = (SelectStmt *) larg;
16882 	n->rarg = (SelectStmt *) rarg;
16883 	return (Node *) n;
16884 }
16885 
16886 /* SystemFuncName()
16887  * Build a properly-qualified reference to a built-in function.
16888  */
16889 List *
SystemFuncName(char * name)16890 SystemFuncName(char *name)
16891 {
16892 	return list_make2(makeString("pg_catalog"), makeString(name));
16893 }
16894 
16895 /* SystemTypeName()
16896  * Build a properly-qualified reference to a built-in type.
16897  *
16898  * typmod is defaulted, but may be changed afterwards by caller.
16899  * Likewise for the location.
16900  */
16901 TypeName *
SystemTypeName(char * name)16902 SystemTypeName(char *name)
16903 {
16904 	return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
16905 											   makeString(name)));
16906 }
16907 /* doNegate()
16908  * Handle negation of a numeric constant.
16909  *
16910  * Formerly, we did this here because the optimizer couldn't cope with
16911  * indexquals that looked like "var = -4" --- it wants "var = const"
16912  * and a unary minus operator applied to a constant didn't qualify.
16913  * As of Postgres 7.0, that problem doesn't exist anymore because there
16914  * is a constant-subexpression simplifier in the optimizer.  However,
16915  * there's still a good reason for doing this here, which is that we can
16916  * postpone committing to a particular internal representation for simple
16917  * negative constants.	It's better to leave "-123.456" in string form
16918  * until we know what the desired type is.
16919  */
16920 static Node *
doNegate(Node * n,int location)16921 doNegate(Node *n, int location)
16922 {
16923 	if (IsA(n, A_Const))
16924 	{
16925 		A_Const *con = (A_Const *)n;
16926 
16927 		/* report the constant's location as that of the '-' sign */
16928 		con->location = location;
16929 
16930 		if (con->val.type == T_Integer)
16931 		{
16932 			con->val.val.ival = -con->val.val.ival;
16933 			return n;
16934 		}
16935 		if (con->val.type == T_Float)
16936 		{
16937 			doNegateFloat(&con->val);
16938 			return n;
16939 		}
16940 	}
16941 
16942 	return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
16943 }
16944 
16945 static void
doNegateFloat(Value * v)16946 doNegateFloat(Value *v)
16947 {
16948 	char   *oldval = v->val.str;
16949 
16950 	Assert(IsA(v, Float));
16951 	if (*oldval == '+')
16952 		oldval++;
16953 	if (*oldval == '-')
16954 		v->val.str = oldval+1;	/* just strip the '-' */
16955 	else
16956 		v->val.str = psprintf("-%s", oldval);
16957 }
16958 
16959 static Node *
makeAndExpr(Node * lexpr,Node * rexpr,int location)16960 makeAndExpr(Node *lexpr, Node *rexpr, int location)
16961 {
16962 	/* Flatten "a AND b AND c ..." to a single BoolExpr on sight */
16963 	if (IsA(lexpr, BoolExpr))
16964 	{
16965 		BoolExpr *blexpr = (BoolExpr *) lexpr;
16966 
16967 		if (blexpr->boolop == AND_EXPR)
16968 		{
16969 			blexpr->args = lappend(blexpr->args, rexpr);
16970 			return (Node *) blexpr;
16971 		}
16972 	}
16973 	return (Node *) makeBoolExpr(AND_EXPR, list_make2(lexpr, rexpr), location);
16974 }
16975 
16976 static Node *
makeOrExpr(Node * lexpr,Node * rexpr,int location)16977 makeOrExpr(Node *lexpr, Node *rexpr, int location)
16978 {
16979 	/* Flatten "a OR b OR c ..." to a single BoolExpr on sight */
16980 	if (IsA(lexpr, BoolExpr))
16981 	{
16982 		BoolExpr *blexpr = (BoolExpr *) lexpr;
16983 
16984 		if (blexpr->boolop == OR_EXPR)
16985 		{
16986 			blexpr->args = lappend(blexpr->args, rexpr);
16987 			return (Node *) blexpr;
16988 		}
16989 	}
16990 	return (Node *) makeBoolExpr(OR_EXPR, list_make2(lexpr, rexpr), location);
16991 }
16992 
16993 static Node *
makeNotExpr(Node * expr,int location)16994 makeNotExpr(Node *expr, int location)
16995 {
16996 	return (Node *) makeBoolExpr(NOT_EXPR, list_make1(expr), location);
16997 }
16998 
16999 static Node *
makeAArrayExpr(List * elements,int location)17000 makeAArrayExpr(List *elements, int location)
17001 {
17002 	A_ArrayExpr *n = makeNode(A_ArrayExpr);
17003 
17004 	n->elements = elements;
17005 	n->location = location;
17006 	return (Node *) n;
17007 }
17008 
17009 static Node *
makeSQLValueFunction(SQLValueFunctionOp op,int32 typmod,int location)17010 makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location)
17011 {
17012 	SQLValueFunction *svf = makeNode(SQLValueFunction);
17013 
17014 	svf->op = op;
17015 	/* svf->type will be filled during parse analysis */
17016 	svf->typmod = typmod;
17017 	svf->location = location;
17018 	return (Node *) svf;
17019 }
17020 
17021 static Node *
makeXmlExpr(XmlExprOp op,char * name,List * named_args,List * args,int location)17022 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
17023 			int location)
17024 {
17025 	XmlExpr		*x = makeNode(XmlExpr);
17026 
17027 	x->op = op;
17028 	x->name = name;
17029 	/*
17030 	 * named_args is a list of ResTarget; it'll be split apart into separate
17031 	 * expression and name lists in transformXmlExpr().
17032 	 */
17033 	x->named_args = named_args;
17034 	x->arg_names = NIL;
17035 	x->args = args;
17036 	/* xmloption, if relevant, must be filled in by caller */
17037 	/* type and typmod will be filled in during parse analysis */
17038 	x->type = InvalidOid;			/* marks the node as not analyzed */
17039 	x->location = location;
17040 	return (Node *) x;
17041 }
17042 
17043 /*
17044  * Merge the input and output parameters of a table function.
17045  */
17046 static List *
mergeTableFuncParameters(List * func_args,List * columns)17047 mergeTableFuncParameters(List *func_args, List *columns)
17048 {
17049 	ListCell   *lc;
17050 
17051 	/* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
17052 	foreach(lc, func_args)
17053 	{
17054 		FunctionParameter *p = (FunctionParameter *) lfirst(lc);
17055 
17056 		if (p->mode != FUNC_PARAM_DEFAULT &&
17057 			p->mode != FUNC_PARAM_IN &&
17058 			p->mode != FUNC_PARAM_VARIADIC)
17059 			ereport(ERROR,
17060 					(errcode(ERRCODE_SYNTAX_ERROR),
17061 					 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
17062 	}
17063 
17064 	return list_concat(func_args, columns);
17065 }
17066 
17067 /*
17068  * Determine return type of a TABLE function.  A single result column
17069  * returns setof that column's type; otherwise return setof record.
17070  */
17071 static TypeName *
TableFuncTypeName(List * columns)17072 TableFuncTypeName(List *columns)
17073 {
17074 	TypeName *result;
17075 
17076 	if (list_length(columns) == 1)
17077 	{
17078 		FunctionParameter *p = (FunctionParameter *) linitial(columns);
17079 
17080 		result = copyObject(p->argType);
17081 	}
17082 	else
17083 		result = SystemTypeName("record");
17084 
17085 	result->setof = true;
17086 
17087 	return result;
17088 }
17089 
17090 /*
17091  * Convert a list of (dotted) names to a RangeVar (like
17092  * makeRangeVarFromNameList, but with position support).  The
17093  * "AnyName" refers to the any_name production in the grammar.
17094  */
17095 static RangeVar *
makeRangeVarFromAnyName(List * names,int position,core_yyscan_t yyscanner)17096 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
17097 {
17098 	RangeVar *r = makeNode(RangeVar);
17099 
17100 	switch (list_length(names))
17101 	{
17102 		case 1:
17103 			r->catalogname = NULL;
17104 			r->schemaname = NULL;
17105 			r->relname = strVal(linitial(names));
17106 			break;
17107 		case 2:
17108 			r->catalogname = NULL;
17109 			r->schemaname = strVal(linitial(names));
17110 			r->relname = strVal(lsecond(names));
17111 			break;
17112 		case 3:
17113 			r->catalogname = strVal(linitial(names));
17114 			r->schemaname = strVal(lsecond(names));
17115 			r->relname = strVal(lthird(names));
17116 			break;
17117 		default:
17118 			ereport(ERROR,
17119 					(errcode(ERRCODE_SYNTAX_ERROR),
17120 					 errmsg("improper qualified name (too many dotted names): %s",
17121 							NameListToString(names)),
17122 					 parser_errposition(position)));
17123 			break;
17124 	}
17125 
17126 	r->relpersistence = RELPERSISTENCE_PERMANENT;
17127 	r->location = position;
17128 
17129 	return r;
17130 }
17131 
17132 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
17133 static void
SplitColQualList(List * qualList,List ** constraintList,CollateClause ** collClause,core_yyscan_t yyscanner)17134 SplitColQualList(List *qualList,
17135 				 List **constraintList, CollateClause **collClause,
17136 				 core_yyscan_t yyscanner)
17137 {
17138 	ListCell   *cell;
17139 
17140 	*collClause = NULL;
17141 	foreach(cell, qualList)
17142 	{
17143 		Node   *n = (Node *) lfirst(cell);
17144 
17145 		if (IsA(n, Constraint))
17146 		{
17147 			/* keep it in list */
17148 			continue;
17149 		}
17150 		if (IsA(n, CollateClause))
17151 		{
17152 			CollateClause *c = (CollateClause *) n;
17153 
17154 			if (*collClause)
17155 				ereport(ERROR,
17156 						(errcode(ERRCODE_SYNTAX_ERROR),
17157 						 errmsg("multiple COLLATE clauses not allowed"),
17158 						 parser_errposition(c->location)));
17159 			*collClause = c;
17160 		}
17161 		else
17162 			elog(ERROR, "unexpected node type %d", (int) n->type);
17163 		/* remove non-Constraint nodes from qualList */
17164 		qualList = foreach_delete_current(qualList, cell);
17165 	}
17166 	*constraintList = qualList;
17167 }
17168 
17169 /*
17170  * Process result of ConstraintAttributeSpec, and set appropriate bool flags
17171  * in the output command node.  Pass NULL for any flags the particular
17172  * command doesn't support.
17173  */
17174 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)17175 processCASbits(int cas_bits, int location, const char *constrType,
17176 			   bool *deferrable, bool *initdeferred, bool *not_valid,
17177 			   bool *no_inherit, core_yyscan_t yyscanner)
17178 {
17179 	/* defaults */
17180 	if (deferrable)
17181 		*deferrable = false;
17182 	if (initdeferred)
17183 		*initdeferred = false;
17184 	if (not_valid)
17185 		*not_valid = false;
17186 
17187 	if (cas_bits & (CAS_DEFERRABLE | CAS_INITIALLY_DEFERRED))
17188 	{
17189 		if (deferrable)
17190 			*deferrable = true;
17191 		else
17192 			ereport(ERROR,
17193 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
17194 					 /* translator: %s is CHECK, UNIQUE, or similar */
17195 					 errmsg("%s constraints cannot be marked DEFERRABLE",
17196 							constrType),
17197 					 parser_errposition(location)));
17198 	}
17199 
17200 	if (cas_bits & CAS_INITIALLY_DEFERRED)
17201 	{
17202 		if (initdeferred)
17203 			*initdeferred = true;
17204 		else
17205 			ereport(ERROR,
17206 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
17207 					 /* translator: %s is CHECK, UNIQUE, or similar */
17208 					 errmsg("%s constraints cannot be marked DEFERRABLE",
17209 							constrType),
17210 					 parser_errposition(location)));
17211 	}
17212 
17213 	if (cas_bits & CAS_NOT_VALID)
17214 	{
17215 		if (not_valid)
17216 			*not_valid = true;
17217 		else
17218 			ereport(ERROR,
17219 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
17220 					 /* translator: %s is CHECK, UNIQUE, or similar */
17221 					 errmsg("%s constraints cannot be marked NOT VALID",
17222 							constrType),
17223 					 parser_errposition(location)));
17224 	}
17225 
17226 	if (cas_bits & CAS_NO_INHERIT)
17227 	{
17228 		if (no_inherit)
17229 			*no_inherit = true;
17230 		else
17231 			ereport(ERROR,
17232 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
17233 					 /* translator: %s is CHECK, UNIQUE, or similar */
17234 					 errmsg("%s constraints cannot be marked NO INHERIT",
17235 							constrType),
17236 					 parser_errposition(location)));
17237 	}
17238 }
17239 
17240 /*----------
17241  * Recursive view transformation
17242  *
17243  * Convert
17244  *
17245  *     CREATE RECURSIVE VIEW relname (aliases) AS query
17246  *
17247  * to
17248  *
17249  *     CREATE VIEW relname (aliases) AS
17250  *         WITH RECURSIVE relname (aliases) AS (query)
17251  *         SELECT aliases FROM relname
17252  *
17253  * Actually, just the WITH ... part, which is then inserted into the original
17254  * view definition as the query.
17255  * ----------
17256  */
17257 static Node *
makeRecursiveViewSelect(char * relname,List * aliases,Node * query)17258 makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
17259 {
17260 	SelectStmt *s = makeNode(SelectStmt);
17261 	WithClause *w = makeNode(WithClause);
17262 	CommonTableExpr *cte = makeNode(CommonTableExpr);
17263 	List	   *tl = NIL;
17264 	ListCell   *lc;
17265 
17266 	/* create common table expression */
17267 	cte->ctename = relname;
17268 	cte->aliascolnames = aliases;
17269 	cte->ctematerialized = CTEMaterializeDefault;
17270 	cte->ctequery = query;
17271 	cte->location = -1;
17272 
17273 	/* create WITH clause and attach CTE */
17274 	w->recursive = true;
17275 	w->ctes = list_make1(cte);
17276 	w->location = -1;
17277 
17278 	/* create target list for the new SELECT from the alias list of the
17279 	 * recursive view specification */
17280 	foreach (lc, aliases)
17281 	{
17282 		ResTarget *rt = makeNode(ResTarget);
17283 
17284 		rt->name = NULL;
17285 		rt->indirection = NIL;
17286 		rt->val = makeColumnRef(strVal(lfirst(lc)), NIL, -1, 0);
17287 		rt->location = -1;
17288 
17289 		tl = lappend(tl, rt);
17290 	}
17291 
17292 	/* create new SELECT combining WITH clause, target list, and fake FROM
17293 	 * clause */
17294 	s->withClause = w;
17295 	s->targetList = tl;
17296 	s->fromClause = list_make1(makeRangeVar(NULL, relname, -1));
17297 
17298 	return (Node *) s;
17299 }
17300 
17301 /* parser_init()
17302  * Initialize to parse one query string
17303  */
17304 void
parser_init(base_yy_extra_type * yyext)17305 parser_init(base_yy_extra_type *yyext)
17306 {
17307 	yyext->parsetree = NIL;		/* in case grammar forgets to set it */
17308 }
17309 
17310