1 %{
2 
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5  *
6  * gram.y
7  *	  POSTGRESQL BISON rules/actions
8  *
9  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  *
13  * IDENTIFICATION
14  *	  src/backend/parser/gram.y
15  *
16  * HISTORY
17  *	  AUTHOR			DATE			MAJOR EVENT
18  *	  Andrew Yu			Sept, 1994		POSTQUEL to SQL conversion
19  *	  Andrew Yu			Oct, 1994		lispy code conversion
20  *
21  * NOTES
22  *	  CAPITALS are used to represent terminal symbols.
23  *	  non-capitals are used to represent non-terminals.
24  *
25  *	  In general, nothing in this file should initiate database accesses
26  *	  nor depend on changeable state (such as SET variables).  If you do
27  *	  database accesses, your code will fail when we have aborted the
28  *	  current transaction and are just parsing commands to find the next
29  *	  ROLLBACK or COMMIT.  If you make use of SET variables, then you
30  *	  will do the wrong thing in multi-query strings like this:
31  *			SET constraint_exclusion TO off; SELECT * FROM foo;
32  *	  because the entire string is parsed by gram.y before the SET gets
33  *	  executed.  Anything that depends on the database or changeable state
34  *	  should be handled during parse analysis so that it happens at the
35  *	  right time not the wrong time.
36  *
37  * WARNINGS
38  *	  If you use a list, make sure the datum is a node so that the printing
39  *	  routines work.
40  *
41  *	  Sometimes we assign constants to makeStrings. Make sure we don't free
42  *	  those.
43  *
44  *-------------------------------------------------------------------------
45  */
46 #include "postgres.h"
47 
48 #include <ctype.h>
49 #include <limits.h>
50 
51 #include "catalog/index.h"
52 #include "catalog/namespace.h"
53 #include "catalog/pg_am.h"
54 #include "catalog/pg_trigger.h"
55 #include "commands/defrem.h"
56 #include "commands/trigger.h"
57 #include "nodes/makefuncs.h"
58 #include "nodes/nodeFuncs.h"
59 #include "parser/gramparse.h"
60 #include "parser/parser.h"
61 #include "parser/parse_expr.h"
62 #include "storage/lmgr.h"
63 #include "utils/date.h"
64 #include "utils/datetime.h"
65 #include "utils/numeric.h"
66 #include "utils/xml.h"
67 
68 
69 /*
70  * Location tracking support --- simpler than bison's default, since we only
71  * want to track the start position not the end position of each nonterminal.
72  */
73 #define YYLLOC_DEFAULT(Current, Rhs, N) \
74 	do { \
75 		if ((N) > 0) \
76 			(Current) = (Rhs)[1]; \
77 		else \
78 			(Current) = (-1); \
79 	} while (0)
80 
81 /*
82  * The above macro assigns -1 (unknown) as the parse location of any
83  * nonterminal that was reduced from an empty rule, or whose leftmost
84  * component was reduced from an empty rule.  This is problematic
85  * for nonterminals defined like
86  *		OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ;
87  * because we'll set -1 as the location during the first reduction and then
88  * copy it during each subsequent reduction, leaving us with -1 for the
89  * location even when the list is not empty.  To fix that, do this in the
90  * action for the nonempty rule(s):
91  *		if (@$ < 0) @$ = @2;
92  * (Although we have many nonterminals that follow this pattern, we only
93  * bother with fixing @$ like this when the nonterminal's parse location
94  * is actually referenced in some rule.)
95  *
96  * A cleaner answer would be to make YYLLOC_DEFAULT scan all the Rhs
97  * locations until it's found one that's not -1.  Then we'd get a correct
98  * location for any nonterminal that isn't entirely empty.  But this way
99  * would add overhead to every rule reduction, and so far there's not been
100  * a compelling reason to pay that overhead.
101  */
102 
103 /*
104  * Bison doesn't allocate anything that needs to live across parser calls,
105  * so we can easily have it use palloc instead of malloc.  This prevents
106  * memory leaks if we error out during parsing.  Note this only works with
107  * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
108  * if possible, so there's not really much problem anyhow, at least if
109  * you're building with gcc.
110  */
111 #define YYMALLOC palloc
112 #define YYFREE   pfree
113 
114 /* Private struct for the result of privilege_target production */
115 typedef struct PrivTarget
116 {
117 	GrantTargetType targtype;
118 	ObjectType	objtype;
119 	List	   *objs;
120 } PrivTarget;
121 
122 /* Private struct for the result of import_qualification production */
123 typedef struct ImportQual
124 {
125 	ImportForeignSchemaType type;
126 	List	   *table_names;
127 } ImportQual;
128 
129 /* ConstraintAttributeSpec yields an integer bitmask of these flags: */
130 #define CAS_NOT_DEFERRABLE			0x01
131 #define CAS_DEFERRABLE				0x02
132 #define CAS_INITIALLY_IMMEDIATE		0x04
133 #define CAS_INITIALLY_DEFERRED		0x08
134 #define CAS_NOT_VALID				0x10
135 #define CAS_NO_INHERIT				0x20
136 
137 
138 #define parser_yyerror(msg)  scanner_yyerror(msg, yyscanner)
139 #define parser_errposition(pos)  scanner_errposition(pos, yyscanner)
140 
141 static void base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner,
142 						 const char *msg);
143 static RawStmt *makeRawStmt(Node *stmt, int stmt_location);
144 static void updateRawStmtEnd(RawStmt *rs, int end_location);
145 static Node *makeColumnRef(char *colname, List *indirection,
146 						   int location, core_yyscan_t yyscanner);
147 static Node *makeTypeCast(Node *arg, TypeName *typename, int location);
148 static Node *makeStringConst(char *str, int location);
149 static Node *makeStringConstCast(char *str, int location, TypeName *typename);
150 static Node *makeIntConst(int val, int location);
151 static Node *makeFloatConst(char *str, int location);
152 static Node *makeBitStringConst(char *str, int location);
153 static Node *makeNullAConst(int location);
154 static Node *makeAConst(Value *v, int location);
155 static Node *makeBoolAConst(bool state, int location);
156 static RoleSpec *makeRoleSpec(RoleSpecType type, int location);
157 static void check_qualified_name(List *names, core_yyscan_t yyscanner);
158 static List *check_func_name(List *names, core_yyscan_t yyscanner);
159 static List *check_indirection(List *indirection, core_yyscan_t yyscanner);
160 static List *extractArgTypes(List *parameters);
161 static List *extractAggrArgTypes(List *aggrargs);
162 static List *makeOrderedSetArgs(List *directargs, List *orderedargs,
163 								core_yyscan_t yyscanner);
164 static void insertSelectOptions(SelectStmt *stmt,
165 								List *sortClause, List *lockingClause,
166 								Node *limitOffset, Node *limitCount,
167 								WithClause *withClause,
168 								core_yyscan_t yyscanner);
169 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
170 static Node *doNegate(Node *n, int location);
171 static void doNegateFloat(Value *v);
172 static Node *makeAndExpr(Node *lexpr, Node *rexpr, int location);
173 static Node *makeOrExpr(Node *lexpr, Node *rexpr, int location);
174 static Node *makeNotExpr(Node *expr, int location);
175 static Node *makeAArrayExpr(List *elements, int location);
176 static Node *makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod,
177 								  int location);
178 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
179 						 List *args, int location);
180 static List *mergeTableFuncParameters(List *func_args, List *columns);
181 static TypeName *TableFuncTypeName(List *columns);
182 static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner);
183 static void SplitColQualList(List *qualList,
184 							 List **constraintList, CollateClause **collClause,
185 							 core_yyscan_t yyscanner);
186 static void processCASbits(int cas_bits, int location, const char *constrType,
187 			   bool *deferrable, bool *initdeferred, bool *not_valid,
188 			   bool *no_inherit, core_yyscan_t yyscanner);
189 static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
190 
191 %}
192 
193 %pure-parser
194 %expect 0
195 %name-prefix="base_yy"
196 %locations
197 
198 %parse-param {core_yyscan_t yyscanner}
199 %lex-param   {core_yyscan_t yyscanner}
200 
201 %union
202 {
203 	core_YYSTYPE		core_yystype;
204 	/* these fields must match core_YYSTYPE: */
205 	int					ival;
206 	char				*str;
207 	const char			*keyword;
208 
209 	char				chr;
210 	bool				boolean;
211 	JoinType			jtype;
212 	DropBehavior		dbehavior;
213 	OnCommitAction		oncommit;
214 	List				*list;
215 	Node				*node;
216 	Value				*value;
217 	ObjectType			objtype;
218 	TypeName			*typnam;
219 	FunctionParameter   *fun_param;
220 	FunctionParameterMode fun_param_mode;
221 	ObjectWithArgs		*objwithargs;
222 	DefElem				*defelt;
223 	SortBy				*sortby;
224 	WindowDef			*windef;
225 	JoinExpr			*jexpr;
226 	IndexElem			*ielem;
227 	Alias				*alias;
228 	RangeVar			*range;
229 	IntoClause			*into;
230 	WithClause			*with;
231 	InferClause			*infer;
232 	OnConflictClause	*onconflict;
233 	A_Indices			*aind;
234 	ResTarget			*target;
235 	struct PrivTarget	*privtarget;
236 	AccessPriv			*accesspriv;
237 	struct ImportQual	*importqual;
238 	InsertStmt			*istmt;
239 	VariableSetStmt		*vsetstmt;
240 	PartitionElem		*partelem;
241 	PartitionSpec		*partspec;
242 	PartitionBoundSpec	*partboundspec;
243 	RoleSpec			*rolespec;
244 }
245 
246 %type <node>	stmt schema_stmt
247 		AlterEventTrigStmt AlterCollationStmt
248 		AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterEnumStmt
249 		AlterFdwStmt AlterForeignServerStmt AlterGroupStmt
250 		AlterObjectDependsStmt AlterObjectSchemaStmt AlterOwnerStmt
251 		AlterOperatorStmt AlterSeqStmt AlterSystemStmt AlterTableStmt
252 		AlterTblSpcStmt AlterExtensionStmt AlterExtensionContentsStmt AlterForeignTableStmt
253 		AlterCompositeTypeStmt AlterUserMappingStmt
254 		AlterRoleStmt AlterRoleSetStmt AlterPolicyStmt
255 		AlterDefaultPrivilegesStmt DefACLAction
256 		AnalyzeStmt CallStmt ClosePortalStmt ClusterStmt CommentStmt
257 		ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
258 		CreateDomainStmt CreateExtensionStmt CreateGroupStmt CreateOpClassStmt
259 		CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
260 		CreateSchemaStmt CreateSeqStmt CreateStmt CreateStatsStmt CreateTableSpaceStmt
261 		CreateFdwStmt CreateForeignServerStmt CreateForeignTableStmt
262 		CreateAssertStmt CreateTransformStmt CreateTrigStmt CreateEventTrigStmt
263 		CreateUserStmt CreateUserMappingStmt CreateRoleStmt CreatePolicyStmt
264 		CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
265 		DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
266 		DropAssertStmt DropCastStmt DropRoleStmt
267 		DropdbStmt DropTableSpaceStmt
268 		DropTransformStmt
269 		DropUserMappingStmt ExplainStmt FetchStmt
270 		GrantStmt GrantRoleStmt ImportForeignSchemaStmt IndexStmt InsertStmt
271 		ListenStmt LoadStmt LockStmt NotifyStmt ExplainableStmt PreparableStmt
272 		CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
273 		RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
274 		RuleActionStmt RuleActionStmtOrEmpty RuleStmt
275 		SecLabelStmt SelectStmt TransactionStmt TruncateStmt
276 		UnlistenStmt UpdateStmt VacuumStmt
277 		VariableResetStmt VariableSetStmt VariableShowStmt
278 		ViewStmt CheckPointStmt CreateConversionStmt
279 		DeallocateStmt PrepareStmt ExecuteStmt
280 		DropOwnedStmt ReassignOwnedStmt
281 		AlterTSConfigurationStmt AlterTSDictionaryStmt
282 		CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
283 		CreatePublicationStmt AlterPublicationStmt
284 		CreateSubscriptionStmt AlterSubscriptionStmt DropSubscriptionStmt
285 
286 %type <node>	select_no_parens select_with_parens select_clause
287 				simple_select values_clause
288 
289 %type <node>	alter_column_default opclass_item opclass_drop alter_using
290 %type <ival>	add_drop opt_asc_desc opt_nulls_order
291 
292 %type <node>	alter_table_cmd alter_type_cmd opt_collate_clause
293 	   replica_identity partition_cmd index_partition_cmd
294 %type <list>	alter_table_cmds alter_type_cmds
295 %type <list>    alter_identity_column_option_list
296 %type <defelt>  alter_identity_column_option
297 
298 %type <dbehavior>	opt_drop_behavior
299 
300 %type <list>	createdb_opt_list createdb_opt_items copy_opt_list
301 				transaction_mode_list
302 				create_extension_opt_list alter_extension_opt_list
303 %type <defelt>	createdb_opt_item copy_opt_item
304 				transaction_mode_item
305 				create_extension_opt_item alter_extension_opt_item
306 
307 %type <ival>	opt_lock lock_type cast_context
308 %type <ival>	vacuum_option_list vacuum_option_elem
309 				analyze_option_list analyze_option_elem
310 %type <boolean>	opt_or_replace
311 				opt_grant_grant_option opt_grant_admin_option
312 				opt_nowait opt_if_exists opt_with_data
313 %type <ival>	opt_nowait_or_skip
314 
315 %type <list>	OptRoleList AlterOptRoleList
316 %type <defelt>	CreateOptRoleElem AlterOptRoleElem
317 
318 %type <str>		opt_type
319 %type <str>		foreign_server_version opt_foreign_server_version
320 %type <str>		opt_in_database
321 
322 %type <str>		OptSchemaName
323 %type <list>	OptSchemaEltList
324 
325 %type <boolean> TriggerForSpec TriggerForType
326 %type <ival>	TriggerActionTime
327 %type <list>	TriggerEvents TriggerOneEvent
328 %type <value>	TriggerFuncArg
329 %type <node>	TriggerWhen
330 %type <str>		TransitionRelName
331 %type <boolean>	TransitionRowOrTable TransitionOldOrNew
332 %type <node>	TriggerTransition
333 
334 %type <list>	event_trigger_when_list event_trigger_value_list
335 %type <defelt>	event_trigger_when_item
336 %type <chr>		enable_trigger
337 
338 %type <str>		copy_file_name
339 				database_name access_method_clause access_method attr_name
340 				name cursor_name file_name
341 				index_name opt_index_name cluster_index_specification
342 
343 %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
344 				opt_class opt_inline_handler opt_validator validator_clause
345 				opt_collate
346 
347 %type <range>	qualified_name insert_target OptConstrFromTable
348 
349 %type <str>		all_Op MathOp
350 
351 %type <str>		row_security_cmd RowSecurityDefaultForCmd
352 %type <boolean> RowSecurityDefaultPermissive
353 %type <node>	RowSecurityOptionalWithCheck RowSecurityOptionalExpr
354 %type <list>	RowSecurityDefaultToRole RowSecurityOptionalToRole
355 
356 %type <str>		iso_level opt_encoding
357 %type <rolespec> grantee
358 %type <list>	grantee_list
359 %type <accesspriv> privilege
360 %type <list>	privileges privilege_list
361 %type <privtarget> privilege_target
362 %type <objwithargs> function_with_argtypes aggregate_with_argtypes operator_with_argtypes
363 %type <list>	function_with_argtypes_list aggregate_with_argtypes_list operator_with_argtypes_list
364 %type <ival>	defacl_privilege_target
365 %type <defelt>	DefACLOption
366 %type <list>	DefACLOptionList
367 %type <ival>	import_qualification_type
368 %type <importqual> import_qualification
369 %type <node>	vacuum_relation
370 
371 %type <list>	stmtblock stmtmulti
372 				OptTableElementList TableElementList OptInherit definition
373 				OptTypedTableElementList TypedTableElementList
374 				reloptions opt_reloptions
375 				OptWith distinct_clause opt_all_clause opt_definition func_args func_args_list
376 				func_args_with_defaults func_args_with_defaults_list
377 				aggr_args aggr_args_list
378 				func_as createfunc_opt_list alterfunc_opt_list
379 				old_aggr_definition old_aggr_list
380 				oper_argtypes RuleActionList RuleActionMulti
381 				opt_column_list columnList opt_name_list
382 				sort_clause opt_sort_clause sortby_list index_params
383 				opt_include opt_c_include index_including_params
384 				name_list role_list from_clause from_list opt_array_bounds
385 				qualified_name_list any_name any_name_list type_name_list
386 				any_operator expr_list attrs
387 				target_list opt_target_list insert_column_list set_target_list
388 				set_clause_list set_clause
389 				def_list operator_def_list indirection opt_indirection
390 				reloption_list group_clause TriggerFuncArgs select_limit
391 				opt_select_limit opclass_item_list opclass_drop_list
392 				opclass_purpose opt_opfamily transaction_mode_list_or_empty
393 				OptTableFuncElementList TableFuncElementList opt_type_modifiers
394 				prep_type_clause
395 				execute_param_clause using_clause returning_clause
396 				opt_enum_val_list enum_val_list table_func_column_list
397 				create_generic_options alter_generic_options
398 				relation_expr_list dostmt_opt_list
399 				transform_element_list transform_type_list
400 				TriggerTransitions TriggerReferencing
401 				publication_name_list
402 				vacuum_relation_list opt_vacuum_relation_list
403 
404 %type <list>	group_by_list
405 %type <node>	group_by_item empty_grouping_set rollup_clause cube_clause
406 %type <node>	grouping_sets_clause
407 %type <node>	opt_publication_for_tables publication_for_tables
408 %type <value>	publication_name_item
409 
410 %type <list>	opt_fdw_options fdw_options
411 %type <defelt>	fdw_option
412 
413 %type <range>	OptTempTableName
414 %type <into>	into_clause create_as_target create_mv_target
415 
416 %type <defelt>	createfunc_opt_item common_func_opt_item dostmt_opt_item
417 %type <fun_param> func_arg func_arg_with_default table_func_column aggr_arg
418 %type <fun_param_mode> arg_class
419 %type <typnam>	func_return func_type
420 
421 %type <boolean>  opt_trusted opt_restart_seqs
422 %type <ival>	 OptTemp
423 %type <ival>	 OptNoLog
424 %type <oncommit> OnCommitOption
425 
426 %type <ival>	for_locking_strength
427 %type <node>	for_locking_item
428 %type <list>	for_locking_clause opt_for_locking_clause for_locking_items
429 %type <list>	locked_rels_list
430 %type <boolean>	all_or_distinct
431 
432 %type <node>	join_outer join_qual
433 %type <jtype>	join_type
434 
435 %type <list>	extract_list overlay_list position_list
436 %type <list>	substr_list trim_list
437 %type <list>	opt_interval interval_second
438 %type <node>	overlay_placing substr_from substr_for
439 
440 %type <boolean> opt_instead
441 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
442 %type <boolean> opt_freeze opt_analyze opt_default opt_recheck
443 %type <defelt>	opt_binary opt_oids copy_delimiter
444 
445 %type <boolean> copy_from opt_program
446 
447 %type <ival>	opt_column event cursor_options opt_hold opt_set_data
448 %type <objtype>	drop_type_any_name drop_type_name drop_type_name_on_any_name
449 				comment_type_any_name comment_type_name
450 				security_label_type_any_name security_label_type_name
451 
452 %type <node>	fetch_args limit_clause select_limit_value
453 				offset_clause select_offset_value
454 				select_fetch_first_value I_or_F_const
455 %type <ival>	row_or_rows first_or_next
456 
457 %type <list>	OptSeqOptList SeqOptList OptParenthesizedSeqOptList
458 %type <defelt>	SeqOptElem
459 
460 %type <istmt>	insert_rest
461 %type <infer>	opt_conf_expr
462 %type <onconflict> opt_on_conflict
463 
464 %type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
465 				 SetResetClause FunctionSetResetClause
466 
467 %type <node>	TableElement TypedTableElement ConstraintElem TableFuncElement
468 %type <node>	columnDef columnOptions
469 %type <defelt>	def_elem reloption_elem old_aggr_elem operator_def_elem
470 %type <node>	def_arg columnElem where_clause where_or_current_clause
471 				a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
472 				columnref in_expr having_clause func_table xmltable array_expr
473 				ExclusionWhereClause operator_def_arg
474 %type <list>	rowsfrom_item rowsfrom_list opt_col_def_list
475 %type <boolean> opt_ordinality
476 %type <list>	ExclusionConstraintList ExclusionConstraintElem
477 %type <list>	func_arg_list
478 %type <node>	func_arg_expr
479 %type <list>	row explicit_row implicit_row type_list array_expr_list
480 %type <node>	case_expr case_arg when_clause case_default
481 %type <list>	when_clause_list
482 %type <ival>	sub_type
483 %type <value>	NumericOnly
484 %type <list>	NumericOnly_list
485 %type <alias>	alias_clause opt_alias_clause
486 %type <list>	func_alias_clause
487 %type <sortby>	sortby
488 %type <ielem>	index_elem
489 %type <node>	table_ref
490 %type <jexpr>	joined_table
491 %type <range>	relation_expr
492 %type <range>	relation_expr_opt_alias
493 %type <node>	tablesample_clause opt_repeatable_clause
494 %type <target>	target_el set_target insert_column_item
495 
496 %type <str>		generic_option_name
497 %type <node>	generic_option_arg
498 %type <defelt>	generic_option_elem alter_generic_option_elem
499 %type <list>	generic_option_list alter_generic_option_list
500 %type <str>		explain_option_name
501 %type <node>	explain_option_arg
502 %type <defelt>	explain_option_elem
503 %type <list>	explain_option_list
504 
505 %type <ival>	reindex_target_type reindex_target_multitable
506 %type <ival>	reindex_option_list reindex_option_elem
507 
508 %type <node>	copy_generic_opt_arg copy_generic_opt_arg_list_item
509 %type <defelt>	copy_generic_opt_elem
510 %type <list>	copy_generic_opt_list copy_generic_opt_arg_list
511 %type <list>	copy_options
512 
513 %type <typnam>	Typename SimpleTypename ConstTypename
514 				GenericType Numeric opt_float
515 				Character ConstCharacter
516 				CharacterWithLength CharacterWithoutLength
517 				ConstDatetime ConstInterval
518 				Bit ConstBit BitWithLength BitWithoutLength
519 %type <str>		character
520 %type <str>		extract_arg
521 %type <boolean> opt_varying opt_timezone opt_no_inherit
522 
523 %type <ival>	Iconst SignedIconst
524 %type <str>		Sconst comment_text notify_payload
525 %type <str>		RoleId opt_boolean_or_string
526 %type <list>	var_list
527 %type <str>		ColId ColLabel var_name type_function_name param_name
528 %type <str>		NonReservedWord NonReservedWord_or_Sconst
529 %type <str>		createdb_opt_name
530 %type <node>	var_value zone_value
531 %type <rolespec> auth_ident RoleSpec opt_granted_by
532 
533 %type <keyword> unreserved_keyword type_func_name_keyword
534 %type <keyword> col_name_keyword reserved_keyword
535 
536 %type <node>	TableConstraint TableLikeClause
537 %type <ival>	TableLikeOptionList TableLikeOption
538 %type <list>	ColQualList
539 %type <node>	ColConstraint ColConstraintElem ConstraintAttr
540 %type <ival>	key_actions key_delete key_match key_update key_action
541 %type <ival>	ConstraintAttributeSpec ConstraintAttributeElem
542 %type <str>		ExistingIndex
543 
544 %type <list>	constraints_set_list
545 %type <boolean> constraints_set_mode
546 %type <str>		OptTableSpace OptConsTableSpace
547 %type <rolespec> OptTableSpaceOwner
548 %type <ival>	opt_check_option
549 
550 %type <str>		opt_provider security_label
551 
552 %type <target>	xml_attribute_el
553 %type <list>	xml_attribute_list xml_attributes
554 %type <node>	xml_root_version opt_xml_root_standalone
555 %type <node>	xmlexists_argument
556 %type <ival>	document_or_content
557 %type <boolean> xml_whitespace_option
558 %type <list>	xmltable_column_list xmltable_column_option_list
559 %type <node>	xmltable_column_el
560 %type <defelt>	xmltable_column_option_el
561 %type <list>	xml_namespace_list
562 %type <target>	xml_namespace_el
563 
564 %type <node>	func_application func_expr_common_subexpr
565 %type <node>	func_expr func_expr_windowless
566 %type <node>	common_table_expr
567 %type <with>	with_clause opt_with_clause
568 %type <list>	cte_list
569 
570 %type <list>	within_group_clause
571 %type <node>	filter_clause
572 %type <list>	window_clause window_definition_list opt_partition_clause
573 %type <windef>	window_definition over_clause window_specification
574 				opt_frame_clause frame_extent frame_bound
575 %type <ival>	opt_window_exclusion_clause
576 %type <str>		opt_existing_window_name
577 %type <boolean> opt_if_not_exists
578 %type <ival>	generated_when override_kind
579 %type <partspec>	PartitionSpec OptPartitionSpec
580 %type <str>			part_strategy
581 %type <partelem>	part_elem
582 %type <list>		part_params
583 %type <partboundspec> PartitionBoundSpec
584 %type <node>		partbound_datum PartitionRangeDatum
585 %type <list>		hash_partbound partbound_datum_list range_datum_list
586 %type <defelt>		hash_partbound_elem
587 
588 /*
589  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
590  * They must be listed first so that their numeric codes do not depend on
591  * the set of keywords.  PL/pgSQL depends on this so that it can share the
592  * same lexer.  If you add/change tokens here, fix PL/pgSQL to match!
593  *
594  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
595  * parse errors.  It is needed by PL/pgSQL.
596  */
597 %token <str>	IDENT FCONST SCONST BCONST XCONST Op
598 %token <ival>	ICONST PARAM
599 %token			TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
600 %token			LESS_EQUALS GREATER_EQUALS NOT_EQUALS
601 
602 /*
603  * If you want to make any keyword changes, update the keyword table in
604  * src/include/parser/kwlist.h and add new keywords to the appropriate one
605  * of the reserved-or-not-so-reserved keyword lists, below; search
606  * this file for "Keyword category lists".
607  */
608 
609 /* ordinary key words in alphabetical order */
610 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
611 	AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
612 	ASSERTION ASSIGNMENT ASYMMETRIC AT ATTACH ATTRIBUTE AUTHORIZATION
613 
614 	BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
615 	BOOLEAN_P BOTH BY
616 
617 	CACHE CALL CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
618 	CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
619 	CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMENT COMMENTS COMMIT
620 	COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION CONSTRAINT
621 	CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE
622 	CROSS CSV CUBE CURRENT_P
623 	CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
624 	CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
625 
626 	DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
627 	DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DESC
628 	DETACH DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P
629 	DOUBLE_P DROP
630 
631 	EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EVENT EXCEPT
632 	EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
633 	EXTENSION EXTERNAL EXTRACT
634 
635 	FALSE_P FAMILY FETCH FILTER FIRST_P FLOAT_P FOLLOWING FOR
636 	FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
637 
638 	GENERATED GLOBAL GRANT GRANTED GREATEST GROUP_P GROUPING GROUPS
639 
640 	HANDLER HAVING HEADER_P HOLD HOUR_P
641 
642 	IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P INCLUDE
643 	INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
644 	INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
645 	INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
646 
647 	JOIN
648 
649 	KEY
650 
651 	LABEL LANGUAGE LARGE_P LAST_P LATERAL_P
652 	LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL
653 	LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED
654 
655 	MAPPING MATCH MATERIALIZED MAXVALUE METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE
656 
657 	NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NONE
658 	NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
659 	NULLS_P NUMERIC
660 
661 	OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
662 	ORDER ORDINALITY OTHERS OUT_P OUTER_P
663 	OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
664 
665 	PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PLACING PLANS POLICY
666 	POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
667 	PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROCEDURES PROGRAM PUBLICATION
668 
669 	QUOTE
670 
671 	RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REFERENCING
672 	REFRESH REINDEX RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
673 	RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP
674 	ROUTINE ROUTINES ROW ROWS RULE
675 
676 	SAVEPOINT SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
677 	SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
678 	SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P
679 	START STATEMENT STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P
680 	SUBSCRIPTION SUBSTRING SYMMETRIC SYSID SYSTEM_P
681 
682 	TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
683 	TIES TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM
684 	TREAT TRIGGER TRIM TRUE_P
685 	TRUNCATE TRUSTED TYPE_P TYPES_P
686 
687 	UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNLOGGED
688 	UNTIL UPDATE USER USING
689 
690 	VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
691 	VERBOSE VERSION_P VIEW VIEWS VOLATILE
692 
693 	WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE
694 
695 	XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLNAMESPACES
696 	XMLPARSE XMLPI XMLROOT XMLSERIALIZE XMLTABLE
697 
698 	YEAR_P YES_P
699 
700 	ZONE
701 
702 /*
703  * The grammar thinks these are keywords, but they are not in the kwlist.h
704  * list and so can never be entered directly.  The filter in parser.c
705  * creates these tokens when required (based on looking one token ahead).
706  *
707  * NOT_LA exists so that productions such as NOT LIKE can be given the same
708  * precedence as LIKE; otherwise they'd effectively have the same precedence
709  * as NOT, at least with respect to their left-hand subexpression.
710  * NULLS_LA and WITH_LA are needed to make the grammar LALR(1).
711  */
712 %token		NOT_LA NULLS_LA WITH_LA
713 
714 
715 /* Precedence: lowest to highest */
716 %nonassoc	SET				/* see relation_expr_opt_alias */
717 %left		UNION EXCEPT
718 %left		INTERSECT
719 %left		OR
720 %left		AND
721 %right		NOT
722 %nonassoc	IS ISNULL NOTNULL	/* IS sets precedence for IS NULL, etc */
723 %nonassoc	'<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
724 %nonassoc	BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA
725 %nonassoc	ESCAPE			/* ESCAPE must be just above LIKE/ILIKE/SIMILAR */
726 %left		POSTFIXOP		/* dummy for postfix Op rules */
727 /*
728  * To support target_el without AS, we must give IDENT an explicit priority
729  * between POSTFIXOP and Op.  We can safely assign the same priority to
730  * various unreserved keywords as needed to resolve ambiguities (this can't
731  * have any bad effects since obviously the keywords will still behave the
732  * same as if they weren't keywords).  We need to do this:
733  * for PARTITION, RANGE, ROWS, GROUPS to support opt_existing_window_name;
734  * for RANGE, ROWS, GROUPS so that they can follow a_expr without creating
735  * postfix-operator problems;
736  * for GENERATED so that it can follow b_expr;
737  * and for NULL so that it can follow b_expr in ColQualList without creating
738  * postfix-operator problems.
739  *
740  * To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
741  * an explicit priority lower than '(', so that a rule with CUBE '(' will shift
742  * rather than reducing a conflicting rule that takes CUBE as a function name.
743  * Using the same precedence as IDENT seems right for the reasons given above.
744  *
745  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
746  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
747  * there is no principled way to distinguish these from the productions
748  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
749  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
750  * appear to cause UNBOUNDED to be treated differently from other unreserved
751  * keywords anywhere else in the grammar, but it's definitely risky.  We can
752  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
753  */
754 %nonassoc	UNBOUNDED		/* ideally should have same precedence as IDENT */
755 %nonassoc	IDENT GENERATED NULL_P PARTITION RANGE ROWS GROUPS PRECEDING FOLLOWING CUBE ROLLUP
756 %left		Op OPERATOR		/* multi-character ops and user-defined operators */
757 %left		'+' '-'
758 %left		'*' '/' '%'
759 %left		'^'
760 /* Unary Operators */
761 %left		AT				/* sets precedence for AT TIME ZONE */
762 %left		COLLATE
763 %right		UMINUS
764 %left		'[' ']'
765 %left		'(' ')'
766 %left		TYPECAST
767 %left		'.'
768 /*
769  * These might seem to be low-precedence, but actually they are not part
770  * of the arithmetic hierarchy at all in their use as JOIN operators.
771  * We make them high-precedence to support their use as function names.
772  * They wouldn't be given a precedence at all, were it not that we need
773  * left-associativity among the JOIN rules themselves.
774  */
775 %left		JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
776 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
777 %right		PRESERVE STRIP_P
778 
779 %%
780 
781 /*
782  *	The target production for the whole parse.
783  */
784 stmtblock:	stmtmulti
785 			{
786 				pg_yyget_extra(yyscanner)->parsetree = $1;
787 			}
788 		;
789 
790 /*
791  * At top level, we wrap each stmt with a RawStmt node carrying start location
792  * and length of the stmt's text.  Notice that the start loc/len are driven
793  * entirely from semicolon locations (@2).  It would seem natural to use
794  * @1 or @3 to get the true start location of a stmt, but that doesn't work
795  * for statements that can start with empty nonterminals (opt_with_clause is
796  * the main offender here); as noted in the comments for YYLLOC_DEFAULT,
797  * we'd get -1 for the location in such cases.
798  * We also take care to discard empty statements entirely.
799  */
800 stmtmulti:	stmtmulti ';' stmt
801 				{
802 					if ($1 != NIL)
803 					{
804 						/* update length of previous stmt */
805 						updateRawStmtEnd(llast_node(RawStmt, $1), @2);
806 					}
807 					if ($3 != NULL)
808 						$$ = lappend($1, makeRawStmt($3, @2 + 1));
809 					else
810 						$$ = $1;
811 				}
812 			| stmt
813 				{
814 					if ($1 != NULL)
815 						$$ = list_make1(makeRawStmt($1, 0));
816 					else
817 						$$ = NIL;
818 				}
819 		;
820 
821 stmt :
822 			AlterEventTrigStmt
823 			| AlterCollationStmt
824 			| AlterDatabaseStmt
825 			| AlterDatabaseSetStmt
826 			| AlterDefaultPrivilegesStmt
827 			| AlterDomainStmt
828 			| AlterEnumStmt
829 			| AlterExtensionStmt
830 			| AlterExtensionContentsStmt
831 			| AlterFdwStmt
832 			| AlterForeignServerStmt
833 			| AlterForeignTableStmt
834 			| AlterFunctionStmt
835 			| AlterGroupStmt
836 			| AlterObjectDependsStmt
837 			| AlterObjectSchemaStmt
838 			| AlterOwnerStmt
839 			| AlterOperatorStmt
840 			| AlterPolicyStmt
841 			| AlterSeqStmt
842 			| AlterSystemStmt
843 			| AlterTableStmt
844 			| AlterTblSpcStmt
845 			| AlterCompositeTypeStmt
846 			| AlterPublicationStmt
847 			| AlterRoleSetStmt
848 			| AlterRoleStmt
849 			| AlterSubscriptionStmt
850 			| AlterTSConfigurationStmt
851 			| AlterTSDictionaryStmt
852 			| AlterUserMappingStmt
853 			| AnalyzeStmt
854 			| CallStmt
855 			| CheckPointStmt
856 			| ClosePortalStmt
857 			| ClusterStmt
858 			| CommentStmt
859 			| ConstraintsSetStmt
860 			| CopyStmt
861 			| CreateAmStmt
862 			| CreateAsStmt
863 			| CreateAssertStmt
864 			| CreateCastStmt
865 			| CreateConversionStmt
866 			| CreateDomainStmt
867 			| CreateExtensionStmt
868 			| CreateFdwStmt
869 			| CreateForeignServerStmt
870 			| CreateForeignTableStmt
871 			| CreateFunctionStmt
872 			| CreateGroupStmt
873 			| CreateMatViewStmt
874 			| CreateOpClassStmt
875 			| CreateOpFamilyStmt
876 			| CreatePublicationStmt
877 			| AlterOpFamilyStmt
878 			| CreatePolicyStmt
879 			| CreatePLangStmt
880 			| CreateSchemaStmt
881 			| CreateSeqStmt
882 			| CreateStmt
883 			| CreateSubscriptionStmt
884 			| CreateStatsStmt
885 			| CreateTableSpaceStmt
886 			| CreateTransformStmt
887 			| CreateTrigStmt
888 			| CreateEventTrigStmt
889 			| CreateRoleStmt
890 			| CreateUserStmt
891 			| CreateUserMappingStmt
892 			| CreatedbStmt
893 			| DeallocateStmt
894 			| DeclareCursorStmt
895 			| DefineStmt
896 			| DeleteStmt
897 			| DiscardStmt
898 			| DoStmt
899 			| DropAssertStmt
900 			| DropCastStmt
901 			| DropOpClassStmt
902 			| DropOpFamilyStmt
903 			| DropOwnedStmt
904 			| DropPLangStmt
905 			| DropStmt
906 			| DropSubscriptionStmt
907 			| DropTableSpaceStmt
908 			| DropTransformStmt
909 			| DropRoleStmt
910 			| DropUserMappingStmt
911 			| DropdbStmt
912 			| ExecuteStmt
913 			| ExplainStmt
914 			| FetchStmt
915 			| GrantStmt
916 			| GrantRoleStmt
917 			| ImportForeignSchemaStmt
918 			| IndexStmt
919 			| InsertStmt
920 			| ListenStmt
921 			| RefreshMatViewStmt
922 			| LoadStmt
923 			| LockStmt
924 			| NotifyStmt
925 			| PrepareStmt
926 			| ReassignOwnedStmt
927 			| ReindexStmt
928 			| RemoveAggrStmt
929 			| RemoveFuncStmt
930 			| RemoveOperStmt
931 			| RenameStmt
932 			| RevokeStmt
933 			| RevokeRoleStmt
934 			| RuleStmt
935 			| SecLabelStmt
936 			| SelectStmt
937 			| TransactionStmt
938 			| TruncateStmt
939 			| UnlistenStmt
940 			| UpdateStmt
941 			| VacuumStmt
942 			| VariableResetStmt
943 			| VariableSetStmt
944 			| VariableShowStmt
945 			| ViewStmt
946 			| /*EMPTY*/
947 				{ $$ = NULL; }
948 		;
949 
950 /*****************************************************************************
951  *
952  * CALL statement
953  *
954  *****************************************************************************/
955 
956 CallStmt:	CALL func_application
957 				{
958 					CallStmt *n = makeNode(CallStmt);
959 					n->funccall = castNode(FuncCall, $2);
960 					$$ = (Node *)n;
961 				}
962 		;
963 
964 /*****************************************************************************
965  *
966  * Create a new Postgres DBMS role
967  *
968  *****************************************************************************/
969 
970 CreateRoleStmt:
971 			CREATE ROLE RoleId opt_with OptRoleList
972 				{
973 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
974 					n->stmt_type = ROLESTMT_ROLE;
975 					n->role = $3;
976 					n->options = $5;
977 					$$ = (Node *)n;
978 				}
979 		;
980 
981 
982 opt_with:	WITH									{}
983 			| WITH_LA								{}
984 			| /*EMPTY*/								{}
985 		;
986 
987 /*
988  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
989  * for backwards compatibility).  Note: the only option required by SQL99
990  * is "WITH ADMIN name".
991  */
992 OptRoleList:
993 			OptRoleList CreateOptRoleElem			{ $$ = lappend($1, $2); }
994 			| /* EMPTY */							{ $$ = NIL; }
995 		;
996 
997 AlterOptRoleList:
998 			AlterOptRoleList AlterOptRoleElem		{ $$ = lappend($1, $2); }
999 			| /* EMPTY */							{ $$ = NIL; }
1000 		;
1001 
1002 AlterOptRoleElem:
1003 			PASSWORD Sconst
1004 				{
1005 					$$ = makeDefElem("password",
1006 									 (Node *)makeString($2), @1);
1007 				}
1008 			| PASSWORD NULL_P
1009 				{
1010 					$$ = makeDefElem("password", NULL, @1);
1011 				}
1012 			| ENCRYPTED PASSWORD Sconst
1013 				{
1014 					/*
1015 					 * These days, passwords are always stored in encrypted
1016 					 * form, so there is no difference between PASSWORD and
1017 					 * ENCRYPTED PASSWORD.
1018 					 */
1019 					$$ = makeDefElem("password",
1020 									 (Node *)makeString($3), @1);
1021 				}
1022 			| UNENCRYPTED PASSWORD Sconst
1023 				{
1024 					ereport(ERROR,
1025 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1026 							 errmsg("UNENCRYPTED PASSWORD is no longer supported"),
1027 							 errhint("Remove UNENCRYPTED to store the password in encrypted form instead."),
1028 							 parser_errposition(@1)));
1029 				}
1030 			| INHERIT
1031 				{
1032 					$$ = makeDefElem("inherit", (Node *)makeInteger(true), @1);
1033 				}
1034 			| CONNECTION LIMIT SignedIconst
1035 				{
1036 					$$ = makeDefElem("connectionlimit", (Node *)makeInteger($3), @1);
1037 				}
1038 			| VALID UNTIL Sconst
1039 				{
1040 					$$ = makeDefElem("validUntil", (Node *)makeString($3), @1);
1041 				}
1042 		/*	Supported but not documented for roles, for use by ALTER GROUP. */
1043 			| USER role_list
1044 				{
1045 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1046 				}
1047 			| IDENT
1048 				{
1049 					/*
1050 					 * We handle identifiers that aren't parser keywords with
1051 					 * the following special-case codes, to avoid bloating the
1052 					 * size of the main parser.
1053 					 */
1054 					if (strcmp($1, "superuser") == 0)
1055 						$$ = makeDefElem("superuser", (Node *)makeInteger(true), @1);
1056 					else if (strcmp($1, "nosuperuser") == 0)
1057 						$$ = makeDefElem("superuser", (Node *)makeInteger(false), @1);
1058 					else if (strcmp($1, "createrole") == 0)
1059 						$$ = makeDefElem("createrole", (Node *)makeInteger(true), @1);
1060 					else if (strcmp($1, "nocreaterole") == 0)
1061 						$$ = makeDefElem("createrole", (Node *)makeInteger(false), @1);
1062 					else if (strcmp($1, "replication") == 0)
1063 						$$ = makeDefElem("isreplication", (Node *)makeInteger(true), @1);
1064 					else if (strcmp($1, "noreplication") == 0)
1065 						$$ = makeDefElem("isreplication", (Node *)makeInteger(false), @1);
1066 					else if (strcmp($1, "createdb") == 0)
1067 						$$ = makeDefElem("createdb", (Node *)makeInteger(true), @1);
1068 					else if (strcmp($1, "nocreatedb") == 0)
1069 						$$ = makeDefElem("createdb", (Node *)makeInteger(false), @1);
1070 					else if (strcmp($1, "login") == 0)
1071 						$$ = makeDefElem("canlogin", (Node *)makeInteger(true), @1);
1072 					else if (strcmp($1, "nologin") == 0)
1073 						$$ = makeDefElem("canlogin", (Node *)makeInteger(false), @1);
1074 					else if (strcmp($1, "bypassrls") == 0)
1075 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(true), @1);
1076 					else if (strcmp($1, "nobypassrls") == 0)
1077 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(false), @1);
1078 					else if (strcmp($1, "noinherit") == 0)
1079 					{
1080 						/*
1081 						 * Note that INHERIT is a keyword, so it's handled by main parser, but
1082 						 * NOINHERIT is handled here.
1083 						 */
1084 						$$ = makeDefElem("inherit", (Node *)makeInteger(false), @1);
1085 					}
1086 					else
1087 						ereport(ERROR,
1088 								(errcode(ERRCODE_SYNTAX_ERROR),
1089 								 errmsg("unrecognized role option \"%s\"", $1),
1090 									 parser_errposition(@1)));
1091 				}
1092 		;
1093 
1094 CreateOptRoleElem:
1095 			AlterOptRoleElem			{ $$ = $1; }
1096 			/* The following are not supported by ALTER ROLE/USER/GROUP */
1097 			| SYSID Iconst
1098 				{
1099 					$$ = makeDefElem("sysid", (Node *)makeInteger($2), @1);
1100 				}
1101 			| ADMIN role_list
1102 				{
1103 					$$ = makeDefElem("adminmembers", (Node *)$2, @1);
1104 				}
1105 			| ROLE role_list
1106 				{
1107 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1108 				}
1109 			| IN_P ROLE role_list
1110 				{
1111 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1112 				}
1113 			| IN_P GROUP_P role_list
1114 				{
1115 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1116 				}
1117 		;
1118 
1119 
1120 /*****************************************************************************
1121  *
1122  * Create a new Postgres DBMS user (role with implied login ability)
1123  *
1124  *****************************************************************************/
1125 
1126 CreateUserStmt:
1127 			CREATE USER RoleId opt_with OptRoleList
1128 				{
1129 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1130 					n->stmt_type = ROLESTMT_USER;
1131 					n->role = $3;
1132 					n->options = $5;
1133 					$$ = (Node *)n;
1134 				}
1135 		;
1136 
1137 
1138 /*****************************************************************************
1139  *
1140  * Alter a postgresql DBMS role
1141  *
1142  *****************************************************************************/
1143 
1144 AlterRoleStmt:
1145 			ALTER ROLE RoleSpec opt_with AlterOptRoleList
1146 				 {
1147 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1148 					n->role = $3;
1149 					n->action = +1;	/* add, if there are members */
1150 					n->options = $5;
1151 					$$ = (Node *)n;
1152 				 }
1153 			| ALTER USER RoleSpec opt_with AlterOptRoleList
1154 				 {
1155 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1156 					n->role = $3;
1157 					n->action = +1;	/* add, if there are members */
1158 					n->options = $5;
1159 					$$ = (Node *)n;
1160 				 }
1161 		;
1162 
1163 opt_in_database:
1164 			   /* EMPTY */					{ $$ = NULL; }
1165 			| IN_P DATABASE database_name	{ $$ = $3; }
1166 		;
1167 
1168 AlterRoleSetStmt:
1169 			ALTER ROLE RoleSpec opt_in_database SetResetClause
1170 				{
1171 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1172 					n->role = $3;
1173 					n->database = $4;
1174 					n->setstmt = $5;
1175 					$$ = (Node *)n;
1176 				}
1177 			| ALTER ROLE ALL opt_in_database SetResetClause
1178 				{
1179 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1180 					n->role = NULL;
1181 					n->database = $4;
1182 					n->setstmt = $5;
1183 					$$ = (Node *)n;
1184 				}
1185 			| ALTER USER RoleSpec opt_in_database SetResetClause
1186 				{
1187 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1188 					n->role = $3;
1189 					n->database = $4;
1190 					n->setstmt = $5;
1191 					$$ = (Node *)n;
1192 				}
1193 			| ALTER USER ALL opt_in_database SetResetClause
1194 				{
1195 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1196 					n->role = NULL;
1197 					n->database = $4;
1198 					n->setstmt = $5;
1199 					$$ = (Node *)n;
1200 				}
1201 		;
1202 
1203 
1204 /*****************************************************************************
1205  *
1206  * Drop a postgresql DBMS role
1207  *
1208  * XXX Ideally this would have CASCADE/RESTRICT options, but a role
1209  * might own objects in multiple databases, and there is presently no way to
1210  * implement cascading to other databases.  So we always behave as RESTRICT.
1211  *****************************************************************************/
1212 
1213 DropRoleStmt:
1214 			DROP ROLE role_list
1215 				{
1216 					DropRoleStmt *n = makeNode(DropRoleStmt);
1217 					n->missing_ok = false;
1218 					n->roles = $3;
1219 					$$ = (Node *)n;
1220 				}
1221 			| DROP ROLE IF_P EXISTS role_list
1222 				{
1223 					DropRoleStmt *n = makeNode(DropRoleStmt);
1224 					n->missing_ok = true;
1225 					n->roles = $5;
1226 					$$ = (Node *)n;
1227 				}
1228 			| DROP USER role_list
1229 				{
1230 					DropRoleStmt *n = makeNode(DropRoleStmt);
1231 					n->missing_ok = false;
1232 					n->roles = $3;
1233 					$$ = (Node *)n;
1234 				}
1235 			| DROP USER IF_P EXISTS role_list
1236 				{
1237 					DropRoleStmt *n = makeNode(DropRoleStmt);
1238 					n->roles = $5;
1239 					n->missing_ok = true;
1240 					$$ = (Node *)n;
1241 				}
1242 			| DROP GROUP_P role_list
1243 				{
1244 					DropRoleStmt *n = makeNode(DropRoleStmt);
1245 					n->missing_ok = false;
1246 					n->roles = $3;
1247 					$$ = (Node *)n;
1248 				}
1249 			| DROP GROUP_P IF_P EXISTS role_list
1250 				{
1251 					DropRoleStmt *n = makeNode(DropRoleStmt);
1252 					n->missing_ok = true;
1253 					n->roles = $5;
1254 					$$ = (Node *)n;
1255 				}
1256 			;
1257 
1258 
1259 /*****************************************************************************
1260  *
1261  * Create a postgresql group (role without login ability)
1262  *
1263  *****************************************************************************/
1264 
1265 CreateGroupStmt:
1266 			CREATE GROUP_P RoleId opt_with OptRoleList
1267 				{
1268 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1269 					n->stmt_type = ROLESTMT_GROUP;
1270 					n->role = $3;
1271 					n->options = $5;
1272 					$$ = (Node *)n;
1273 				}
1274 		;
1275 
1276 
1277 /*****************************************************************************
1278  *
1279  * Alter a postgresql group
1280  *
1281  *****************************************************************************/
1282 
1283 AlterGroupStmt:
1284 			ALTER GROUP_P RoleSpec add_drop USER role_list
1285 				{
1286 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1287 					n->role = $3;
1288 					n->action = $4;
1289 					n->options = list_make1(makeDefElem("rolemembers",
1290 														(Node *)$6, @6));
1291 					$$ = (Node *)n;
1292 				}
1293 		;
1294 
1295 add_drop:	ADD_P									{ $$ = +1; }
1296 			| DROP									{ $$ = -1; }
1297 		;
1298 
1299 
1300 /*****************************************************************************
1301  *
1302  * Manipulate a schema
1303  *
1304  *****************************************************************************/
1305 
1306 CreateSchemaStmt:
1307 			CREATE SCHEMA OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1308 				{
1309 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1310 					/* One can omit the schema name or the authorization id. */
1311 					n->schemaname = $3;
1312 					n->authrole = $5;
1313 					n->schemaElts = $6;
1314 					n->if_not_exists = false;
1315 					$$ = (Node *)n;
1316 				}
1317 			| CREATE SCHEMA ColId OptSchemaEltList
1318 				{
1319 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1320 					/* ...but not both */
1321 					n->schemaname = $3;
1322 					n->authrole = NULL;
1323 					n->schemaElts = $4;
1324 					n->if_not_exists = false;
1325 					$$ = (Node *)n;
1326 				}
1327 			| CREATE SCHEMA IF_P NOT EXISTS OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1328 				{
1329 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1330 					/* schema name can be omitted here, too */
1331 					n->schemaname = $6;
1332 					n->authrole = $8;
1333 					if ($9 != NIL)
1334 						ereport(ERROR,
1335 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1336 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1337 								 parser_errposition(@9)));
1338 					n->schemaElts = $9;
1339 					n->if_not_exists = true;
1340 					$$ = (Node *)n;
1341 				}
1342 			| CREATE SCHEMA IF_P NOT EXISTS ColId OptSchemaEltList
1343 				{
1344 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1345 					/* ...but not here */
1346 					n->schemaname = $6;
1347 					n->authrole = NULL;
1348 					if ($7 != NIL)
1349 						ereport(ERROR,
1350 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1351 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1352 								 parser_errposition(@7)));
1353 					n->schemaElts = $7;
1354 					n->if_not_exists = true;
1355 					$$ = (Node *)n;
1356 				}
1357 		;
1358 
1359 OptSchemaName:
1360 			ColId									{ $$ = $1; }
1361 			| /* EMPTY */							{ $$ = NULL; }
1362 		;
1363 
1364 OptSchemaEltList:
1365 			OptSchemaEltList schema_stmt
1366 				{
1367 					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
1368 						@$ = @2;
1369 					$$ = lappend($1, $2);
1370 				}
1371 			| /* EMPTY */
1372 				{ $$ = NIL; }
1373 		;
1374 
1375 /*
1376  *	schema_stmt are the ones that can show up inside a CREATE SCHEMA
1377  *	statement (in addition to by themselves).
1378  */
1379 schema_stmt:
1380 			CreateStmt
1381 			| IndexStmt
1382 			| CreateSeqStmt
1383 			| CreateTrigStmt
1384 			| GrantStmt
1385 			| ViewStmt
1386 		;
1387 
1388 
1389 /*****************************************************************************
1390  *
1391  * Set PG internal variable
1392  *	  SET name TO 'var_value'
1393  * Include SQL syntax (thomas 1997-10-22):
1394  *	  SET TIME ZONE 'var_value'
1395  *
1396  *****************************************************************************/
1397 
1398 VariableSetStmt:
1399 			SET set_rest
1400 				{
1401 					VariableSetStmt *n = $2;
1402 					n->is_local = false;
1403 					$$ = (Node *) n;
1404 				}
1405 			| SET LOCAL set_rest
1406 				{
1407 					VariableSetStmt *n = $3;
1408 					n->is_local = true;
1409 					$$ = (Node *) n;
1410 				}
1411 			| SET SESSION set_rest
1412 				{
1413 					VariableSetStmt *n = $3;
1414 					n->is_local = false;
1415 					$$ = (Node *) n;
1416 				}
1417 		;
1418 
1419 set_rest:
1420 			TRANSACTION transaction_mode_list
1421 				{
1422 					VariableSetStmt *n = makeNode(VariableSetStmt);
1423 					n->kind = VAR_SET_MULTI;
1424 					n->name = "TRANSACTION";
1425 					n->args = $2;
1426 					$$ = n;
1427 				}
1428 			| SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1429 				{
1430 					VariableSetStmt *n = makeNode(VariableSetStmt);
1431 					n->kind = VAR_SET_MULTI;
1432 					n->name = "SESSION CHARACTERISTICS";
1433 					n->args = $5;
1434 					$$ = n;
1435 				}
1436 			| set_rest_more
1437 			;
1438 
1439 generic_set:
1440 			var_name TO var_list
1441 				{
1442 					VariableSetStmt *n = makeNode(VariableSetStmt);
1443 					n->kind = VAR_SET_VALUE;
1444 					n->name = $1;
1445 					n->args = $3;
1446 					$$ = n;
1447 				}
1448 			| var_name '=' var_list
1449 				{
1450 					VariableSetStmt *n = makeNode(VariableSetStmt);
1451 					n->kind = VAR_SET_VALUE;
1452 					n->name = $1;
1453 					n->args = $3;
1454 					$$ = n;
1455 				}
1456 			| var_name TO DEFAULT
1457 				{
1458 					VariableSetStmt *n = makeNode(VariableSetStmt);
1459 					n->kind = VAR_SET_DEFAULT;
1460 					n->name = $1;
1461 					$$ = n;
1462 				}
1463 			| var_name '=' DEFAULT
1464 				{
1465 					VariableSetStmt *n = makeNode(VariableSetStmt);
1466 					n->kind = VAR_SET_DEFAULT;
1467 					n->name = $1;
1468 					$$ = n;
1469 				}
1470 		;
1471 
1472 set_rest_more:	/* Generic SET syntaxes: */
1473 			generic_set 						{$$ = $1;}
1474 			| var_name FROM CURRENT_P
1475 				{
1476 					VariableSetStmt *n = makeNode(VariableSetStmt);
1477 					n->kind = VAR_SET_CURRENT;
1478 					n->name = $1;
1479 					$$ = n;
1480 				}
1481 			/* Special syntaxes mandated by SQL standard: */
1482 			| TIME ZONE zone_value
1483 				{
1484 					VariableSetStmt *n = makeNode(VariableSetStmt);
1485 					n->kind = VAR_SET_VALUE;
1486 					n->name = "timezone";
1487 					if ($3 != NULL)
1488 						n->args = list_make1($3);
1489 					else
1490 						n->kind = VAR_SET_DEFAULT;
1491 					$$ = n;
1492 				}
1493 			| CATALOG_P Sconst
1494 				{
1495 					ereport(ERROR,
1496 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1497 							 errmsg("current database cannot be changed"),
1498 							 parser_errposition(@2)));
1499 					$$ = NULL; /*not reached*/
1500 				}
1501 			| SCHEMA Sconst
1502 				{
1503 					VariableSetStmt *n = makeNode(VariableSetStmt);
1504 					n->kind = VAR_SET_VALUE;
1505 					n->name = "search_path";
1506 					n->args = list_make1(makeStringConst($2, @2));
1507 					$$ = n;
1508 				}
1509 			| NAMES opt_encoding
1510 				{
1511 					VariableSetStmt *n = makeNode(VariableSetStmt);
1512 					n->kind = VAR_SET_VALUE;
1513 					n->name = "client_encoding";
1514 					if ($2 != NULL)
1515 						n->args = list_make1(makeStringConst($2, @2));
1516 					else
1517 						n->kind = VAR_SET_DEFAULT;
1518 					$$ = n;
1519 				}
1520 			| ROLE NonReservedWord_or_Sconst
1521 				{
1522 					VariableSetStmt *n = makeNode(VariableSetStmt);
1523 					n->kind = VAR_SET_VALUE;
1524 					n->name = "role";
1525 					n->args = list_make1(makeStringConst($2, @2));
1526 					$$ = n;
1527 				}
1528 			| SESSION AUTHORIZATION NonReservedWord_or_Sconst
1529 				{
1530 					VariableSetStmt *n = makeNode(VariableSetStmt);
1531 					n->kind = VAR_SET_VALUE;
1532 					n->name = "session_authorization";
1533 					n->args = list_make1(makeStringConst($3, @3));
1534 					$$ = n;
1535 				}
1536 			| SESSION AUTHORIZATION DEFAULT
1537 				{
1538 					VariableSetStmt *n = makeNode(VariableSetStmt);
1539 					n->kind = VAR_SET_DEFAULT;
1540 					n->name = "session_authorization";
1541 					$$ = n;
1542 				}
1543 			| XML_P OPTION document_or_content
1544 				{
1545 					VariableSetStmt *n = makeNode(VariableSetStmt);
1546 					n->kind = VAR_SET_VALUE;
1547 					n->name = "xmloption";
1548 					n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1549 					$$ = n;
1550 				}
1551 			/* Special syntaxes invented by PostgreSQL: */
1552 			| TRANSACTION SNAPSHOT Sconst
1553 				{
1554 					VariableSetStmt *n = makeNode(VariableSetStmt);
1555 					n->kind = VAR_SET_MULTI;
1556 					n->name = "TRANSACTION SNAPSHOT";
1557 					n->args = list_make1(makeStringConst($3, @3));
1558 					$$ = n;
1559 				}
1560 		;
1561 
1562 var_name:	ColId								{ $$ = $1; }
1563 			| var_name '.' ColId
1564 				{ $$ = psprintf("%s.%s", $1, $3); }
1565 		;
1566 
1567 var_list:	var_value								{ $$ = list_make1($1); }
1568 			| var_list ',' var_value				{ $$ = lappend($1, $3); }
1569 		;
1570 
1571 var_value:	opt_boolean_or_string
1572 				{ $$ = makeStringConst($1, @1); }
1573 			| NumericOnly
1574 				{ $$ = makeAConst($1, @1); }
1575 		;
1576 
1577 iso_level:	READ UNCOMMITTED						{ $$ = "read uncommitted"; }
1578 			| READ COMMITTED						{ $$ = "read committed"; }
1579 			| REPEATABLE READ						{ $$ = "repeatable read"; }
1580 			| SERIALIZABLE							{ $$ = "serializable"; }
1581 		;
1582 
1583 opt_boolean_or_string:
1584 			TRUE_P									{ $$ = "true"; }
1585 			| FALSE_P								{ $$ = "false"; }
1586 			| ON									{ $$ = "on"; }
1587 			/*
1588 			 * OFF is also accepted as a boolean value, but is handled by
1589 			 * the NonReservedWord rule.  The action for booleans and strings
1590 			 * is the same, so we don't need to distinguish them here.
1591 			 */
1592 			| NonReservedWord_or_Sconst				{ $$ = $1; }
1593 		;
1594 
1595 /* Timezone values can be:
1596  * - a string such as 'pst8pdt'
1597  * - an identifier such as "pst8pdt"
1598  * - an integer or floating point number
1599  * - a time interval per SQL99
1600  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1601  * so use IDENT (meaning we reject anything that is a key word).
1602  */
1603 zone_value:
1604 			Sconst
1605 				{
1606 					$$ = makeStringConst($1, @1);
1607 				}
1608 			| IDENT
1609 				{
1610 					$$ = makeStringConst($1, @1);
1611 				}
1612 			| ConstInterval Sconst opt_interval
1613 				{
1614 					TypeName *t = $1;
1615 					if ($3 != NIL)
1616 					{
1617 						A_Const *n = (A_Const *) linitial($3);
1618 						if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1619 							ereport(ERROR,
1620 									(errcode(ERRCODE_SYNTAX_ERROR),
1621 									 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1622 									 parser_errposition(@3)));
1623 					}
1624 					t->typmods = $3;
1625 					$$ = makeStringConstCast($2, @2, t);
1626 				}
1627 			| ConstInterval '(' Iconst ')' Sconst
1628 				{
1629 					TypeName *t = $1;
1630 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1631 											makeIntConst($3, @3));
1632 					$$ = makeStringConstCast($5, @5, t);
1633 				}
1634 			| NumericOnly							{ $$ = makeAConst($1, @1); }
1635 			| DEFAULT								{ $$ = NULL; }
1636 			| LOCAL									{ $$ = NULL; }
1637 		;
1638 
1639 opt_encoding:
1640 			Sconst									{ $$ = $1; }
1641 			| DEFAULT								{ $$ = NULL; }
1642 			| /*EMPTY*/								{ $$ = NULL; }
1643 		;
1644 
1645 NonReservedWord_or_Sconst:
1646 			NonReservedWord							{ $$ = $1; }
1647 			| Sconst								{ $$ = $1; }
1648 		;
1649 
1650 VariableResetStmt:
1651 			RESET reset_rest						{ $$ = (Node *) $2; }
1652 		;
1653 
1654 reset_rest:
1655 			generic_reset							{ $$ = $1; }
1656 			| TIME ZONE
1657 				{
1658 					VariableSetStmt *n = makeNode(VariableSetStmt);
1659 					n->kind = VAR_RESET;
1660 					n->name = "timezone";
1661 					$$ = n;
1662 				}
1663 			| TRANSACTION ISOLATION LEVEL
1664 				{
1665 					VariableSetStmt *n = makeNode(VariableSetStmt);
1666 					n->kind = VAR_RESET;
1667 					n->name = "transaction_isolation";
1668 					$$ = n;
1669 				}
1670 			| SESSION AUTHORIZATION
1671 				{
1672 					VariableSetStmt *n = makeNode(VariableSetStmt);
1673 					n->kind = VAR_RESET;
1674 					n->name = "session_authorization";
1675 					$$ = n;
1676 				}
1677 		;
1678 
1679 generic_reset:
1680 			var_name
1681 				{
1682 					VariableSetStmt *n = makeNode(VariableSetStmt);
1683 					n->kind = VAR_RESET;
1684 					n->name = $1;
1685 					$$ = n;
1686 				}
1687 			| ALL
1688 				{
1689 					VariableSetStmt *n = makeNode(VariableSetStmt);
1690 					n->kind = VAR_RESET_ALL;
1691 					$$ = n;
1692 				}
1693 		;
1694 
1695 /* SetResetClause allows SET or RESET without LOCAL */
1696 SetResetClause:
1697 			SET set_rest					{ $$ = $2; }
1698 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1699 		;
1700 
1701 /* SetResetClause allows SET or RESET without LOCAL */
1702 FunctionSetResetClause:
1703 			SET set_rest_more				{ $$ = $2; }
1704 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1705 		;
1706 
1707 
1708 VariableShowStmt:
1709 			SHOW var_name
1710 				{
1711 					VariableShowStmt *n = makeNode(VariableShowStmt);
1712 					n->name = $2;
1713 					$$ = (Node *) n;
1714 				}
1715 			| SHOW TIME ZONE
1716 				{
1717 					VariableShowStmt *n = makeNode(VariableShowStmt);
1718 					n->name = "timezone";
1719 					$$ = (Node *) n;
1720 				}
1721 			| SHOW TRANSACTION ISOLATION LEVEL
1722 				{
1723 					VariableShowStmt *n = makeNode(VariableShowStmt);
1724 					n->name = "transaction_isolation";
1725 					$$ = (Node *) n;
1726 				}
1727 			| SHOW SESSION AUTHORIZATION
1728 				{
1729 					VariableShowStmt *n = makeNode(VariableShowStmt);
1730 					n->name = "session_authorization";
1731 					$$ = (Node *) n;
1732 				}
1733 			| SHOW ALL
1734 				{
1735 					VariableShowStmt *n = makeNode(VariableShowStmt);
1736 					n->name = "all";
1737 					$$ = (Node *) n;
1738 				}
1739 		;
1740 
1741 
1742 ConstraintsSetStmt:
1743 			SET CONSTRAINTS constraints_set_list constraints_set_mode
1744 				{
1745 					ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1746 					n->constraints = $3;
1747 					n->deferred = $4;
1748 					$$ = (Node *) n;
1749 				}
1750 		;
1751 
1752 constraints_set_list:
1753 			ALL										{ $$ = NIL; }
1754 			| qualified_name_list					{ $$ = $1; }
1755 		;
1756 
1757 constraints_set_mode:
1758 			DEFERRED								{ $$ = true; }
1759 			| IMMEDIATE								{ $$ = false; }
1760 		;
1761 
1762 
1763 /*
1764  * Checkpoint statement
1765  */
1766 CheckPointStmt:
1767 			CHECKPOINT
1768 				{
1769 					CheckPointStmt *n = makeNode(CheckPointStmt);
1770 					$$ = (Node *)n;
1771 				}
1772 		;
1773 
1774 
1775 /*****************************************************************************
1776  *
1777  * DISCARD { ALL | TEMP | PLANS | SEQUENCES }
1778  *
1779  *****************************************************************************/
1780 
1781 DiscardStmt:
1782 			DISCARD ALL
1783 				{
1784 					DiscardStmt *n = makeNode(DiscardStmt);
1785 					n->target = DISCARD_ALL;
1786 					$$ = (Node *) n;
1787 				}
1788 			| DISCARD TEMP
1789 				{
1790 					DiscardStmt *n = makeNode(DiscardStmt);
1791 					n->target = DISCARD_TEMP;
1792 					$$ = (Node *) n;
1793 				}
1794 			| DISCARD TEMPORARY
1795 				{
1796 					DiscardStmt *n = makeNode(DiscardStmt);
1797 					n->target = DISCARD_TEMP;
1798 					$$ = (Node *) n;
1799 				}
1800 			| DISCARD PLANS
1801 				{
1802 					DiscardStmt *n = makeNode(DiscardStmt);
1803 					n->target = DISCARD_PLANS;
1804 					$$ = (Node *) n;
1805 				}
1806 			| DISCARD SEQUENCES
1807 				{
1808 					DiscardStmt *n = makeNode(DiscardStmt);
1809 					n->target = DISCARD_SEQUENCES;
1810 					$$ = (Node *) n;
1811 				}
1812 
1813 		;
1814 
1815 
1816 /*****************************************************************************
1817  *
1818  *	ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW ] variations
1819  *
1820  * Note: we accept all subcommands for each of the five variants, and sort
1821  * out what's really legal at execution time.
1822  *****************************************************************************/
1823 
1824 AlterTableStmt:
1825 			ALTER TABLE relation_expr alter_table_cmds
1826 				{
1827 					AlterTableStmt *n = makeNode(AlterTableStmt);
1828 					n->relation = $3;
1829 					n->cmds = $4;
1830 					n->relkind = OBJECT_TABLE;
1831 					n->missing_ok = false;
1832 					$$ = (Node *)n;
1833 				}
1834 		|	ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
1835 				{
1836 					AlterTableStmt *n = makeNode(AlterTableStmt);
1837 					n->relation = $5;
1838 					n->cmds = $6;
1839 					n->relkind = OBJECT_TABLE;
1840 					n->missing_ok = true;
1841 					$$ = (Node *)n;
1842 				}
1843 		|	ALTER TABLE relation_expr partition_cmd
1844 				{
1845 					AlterTableStmt *n = makeNode(AlterTableStmt);
1846 					n->relation = $3;
1847 					n->cmds = list_make1($4);
1848 					n->relkind = OBJECT_TABLE;
1849 					n->missing_ok = false;
1850 					$$ = (Node *)n;
1851 				}
1852 		|	ALTER TABLE IF_P EXISTS relation_expr partition_cmd
1853 				{
1854 					AlterTableStmt *n = makeNode(AlterTableStmt);
1855 					n->relation = $5;
1856 					n->cmds = list_make1($6);
1857 					n->relkind = OBJECT_TABLE;
1858 					n->missing_ok = true;
1859 					$$ = (Node *)n;
1860 				}
1861 		|	ALTER TABLE ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1862 				{
1863 					AlterTableMoveAllStmt *n =
1864 						makeNode(AlterTableMoveAllStmt);
1865 					n->orig_tablespacename = $6;
1866 					n->objtype = OBJECT_TABLE;
1867 					n->roles = NIL;
1868 					n->new_tablespacename = $9;
1869 					n->nowait = $10;
1870 					$$ = (Node *)n;
1871 				}
1872 		|	ALTER TABLE ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1873 				{
1874 					AlterTableMoveAllStmt *n =
1875 						makeNode(AlterTableMoveAllStmt);
1876 					n->orig_tablespacename = $6;
1877 					n->objtype = OBJECT_TABLE;
1878 					n->roles = $9;
1879 					n->new_tablespacename = $12;
1880 					n->nowait = $13;
1881 					$$ = (Node *)n;
1882 				}
1883 		|	ALTER INDEX qualified_name alter_table_cmds
1884 				{
1885 					AlterTableStmt *n = makeNode(AlterTableStmt);
1886 					n->relation = $3;
1887 					n->cmds = $4;
1888 					n->relkind = OBJECT_INDEX;
1889 					n->missing_ok = false;
1890 					$$ = (Node *)n;
1891 				}
1892 		|	ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
1893 				{
1894 					AlterTableStmt *n = makeNode(AlterTableStmt);
1895 					n->relation = $5;
1896 					n->cmds = $6;
1897 					n->relkind = OBJECT_INDEX;
1898 					n->missing_ok = true;
1899 					$$ = (Node *)n;
1900 				}
1901 		|	ALTER INDEX qualified_name index_partition_cmd
1902 				{
1903 					AlterTableStmt *n = makeNode(AlterTableStmt);
1904 					n->relation = $3;
1905 					n->cmds = list_make1($4);
1906 					n->relkind = OBJECT_INDEX;
1907 					n->missing_ok = false;
1908 					$$ = (Node *)n;
1909 				}
1910 		|	ALTER INDEX ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1911 				{
1912 					AlterTableMoveAllStmt *n =
1913 						makeNode(AlterTableMoveAllStmt);
1914 					n->orig_tablespacename = $6;
1915 					n->objtype = OBJECT_INDEX;
1916 					n->roles = NIL;
1917 					n->new_tablespacename = $9;
1918 					n->nowait = $10;
1919 					$$ = (Node *)n;
1920 				}
1921 		|	ALTER INDEX ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1922 				{
1923 					AlterTableMoveAllStmt *n =
1924 						makeNode(AlterTableMoveAllStmt);
1925 					n->orig_tablespacename = $6;
1926 					n->objtype = OBJECT_INDEX;
1927 					n->roles = $9;
1928 					n->new_tablespacename = $12;
1929 					n->nowait = $13;
1930 					$$ = (Node *)n;
1931 				}
1932 		|	ALTER SEQUENCE qualified_name alter_table_cmds
1933 				{
1934 					AlterTableStmt *n = makeNode(AlterTableStmt);
1935 					n->relation = $3;
1936 					n->cmds = $4;
1937 					n->relkind = OBJECT_SEQUENCE;
1938 					n->missing_ok = false;
1939 					$$ = (Node *)n;
1940 				}
1941 		|	ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
1942 				{
1943 					AlterTableStmt *n = makeNode(AlterTableStmt);
1944 					n->relation = $5;
1945 					n->cmds = $6;
1946 					n->relkind = OBJECT_SEQUENCE;
1947 					n->missing_ok = true;
1948 					$$ = (Node *)n;
1949 				}
1950 		|	ALTER VIEW qualified_name alter_table_cmds
1951 				{
1952 					AlterTableStmt *n = makeNode(AlterTableStmt);
1953 					n->relation = $3;
1954 					n->cmds = $4;
1955 					n->relkind = OBJECT_VIEW;
1956 					n->missing_ok = false;
1957 					$$ = (Node *)n;
1958 				}
1959 		|	ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
1960 				{
1961 					AlterTableStmt *n = makeNode(AlterTableStmt);
1962 					n->relation = $5;
1963 					n->cmds = $6;
1964 					n->relkind = OBJECT_VIEW;
1965 					n->missing_ok = true;
1966 					$$ = (Node *)n;
1967 				}
1968 		|	ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
1969 				{
1970 					AlterTableStmt *n = makeNode(AlterTableStmt);
1971 					n->relation = $4;
1972 					n->cmds = $5;
1973 					n->relkind = OBJECT_MATVIEW;
1974 					n->missing_ok = false;
1975 					$$ = (Node *)n;
1976 				}
1977 		|	ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
1978 				{
1979 					AlterTableStmt *n = makeNode(AlterTableStmt);
1980 					n->relation = $6;
1981 					n->cmds = $7;
1982 					n->relkind = OBJECT_MATVIEW;
1983 					n->missing_ok = true;
1984 					$$ = (Node *)n;
1985 				}
1986 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1987 				{
1988 					AlterTableMoveAllStmt *n =
1989 						makeNode(AlterTableMoveAllStmt);
1990 					n->orig_tablespacename = $7;
1991 					n->objtype = OBJECT_MATVIEW;
1992 					n->roles = NIL;
1993 					n->new_tablespacename = $10;
1994 					n->nowait = $11;
1995 					$$ = (Node *)n;
1996 				}
1997 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1998 				{
1999 					AlterTableMoveAllStmt *n =
2000 						makeNode(AlterTableMoveAllStmt);
2001 					n->orig_tablespacename = $7;
2002 					n->objtype = OBJECT_MATVIEW;
2003 					n->roles = $10;
2004 					n->new_tablespacename = $13;
2005 					n->nowait = $14;
2006 					$$ = (Node *)n;
2007 				}
2008 		;
2009 
2010 alter_table_cmds:
2011 			alter_table_cmd							{ $$ = list_make1($1); }
2012 			| alter_table_cmds ',' alter_table_cmd	{ $$ = lappend($1, $3); }
2013 		;
2014 
2015 partition_cmd:
2016 			/* ALTER TABLE <name> ATTACH PARTITION <table_name> FOR VALUES */
2017 			ATTACH PARTITION qualified_name PartitionBoundSpec
2018 				{
2019 					AlterTableCmd *n = makeNode(AlterTableCmd);
2020 					PartitionCmd *cmd = makeNode(PartitionCmd);
2021 
2022 					n->subtype = AT_AttachPartition;
2023 					cmd->name = $3;
2024 					cmd->bound = $4;
2025 					n->def = (Node *) cmd;
2026 
2027 					$$ = (Node *) n;
2028 				}
2029 			/* ALTER TABLE <name> DETACH PARTITION <partition_name> */
2030 			| DETACH PARTITION qualified_name
2031 				{
2032 					AlterTableCmd *n = makeNode(AlterTableCmd);
2033 					PartitionCmd *cmd = makeNode(PartitionCmd);
2034 
2035 					n->subtype = AT_DetachPartition;
2036 					cmd->name = $3;
2037 					cmd->bound = NULL;
2038 					n->def = (Node *) cmd;
2039 
2040 					$$ = (Node *) n;
2041 				}
2042 		;
2043 
2044 index_partition_cmd:
2045 			/* ALTER INDEX <name> ATTACH PARTITION <index_name> */
2046 			ATTACH PARTITION qualified_name
2047 				{
2048 					AlterTableCmd *n = makeNode(AlterTableCmd);
2049 					PartitionCmd *cmd = makeNode(PartitionCmd);
2050 
2051 					n->subtype = AT_AttachPartition;
2052 					cmd->name = $3;
2053 					cmd->bound = NULL;
2054 					n->def = (Node *) cmd;
2055 
2056 					$$ = (Node *) n;
2057 				}
2058 		;
2059 
2060 alter_table_cmd:
2061 			/* ALTER TABLE <name> ADD <coldef> */
2062 			ADD_P columnDef
2063 				{
2064 					AlterTableCmd *n = makeNode(AlterTableCmd);
2065 					n->subtype = AT_AddColumn;
2066 					n->def = $2;
2067 					n->missing_ok = false;
2068 					$$ = (Node *)n;
2069 				}
2070 			/* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
2071 			| ADD_P IF_P NOT EXISTS columnDef
2072 				{
2073 					AlterTableCmd *n = makeNode(AlterTableCmd);
2074 					n->subtype = AT_AddColumn;
2075 					n->def = $5;
2076 					n->missing_ok = true;
2077 					$$ = (Node *)n;
2078 				}
2079 			/* ALTER TABLE <name> ADD COLUMN <coldef> */
2080 			| ADD_P COLUMN columnDef
2081 				{
2082 					AlterTableCmd *n = makeNode(AlterTableCmd);
2083 					n->subtype = AT_AddColumn;
2084 					n->def = $3;
2085 					n->missing_ok = false;
2086 					$$ = (Node *)n;
2087 				}
2088 			/* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
2089 			| ADD_P COLUMN IF_P NOT EXISTS columnDef
2090 				{
2091 					AlterTableCmd *n = makeNode(AlterTableCmd);
2092 					n->subtype = AT_AddColumn;
2093 					n->def = $6;
2094 					n->missing_ok = true;
2095 					$$ = (Node *)n;
2096 				}
2097 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
2098 			| ALTER opt_column ColId alter_column_default
2099 				{
2100 					AlterTableCmd *n = makeNode(AlterTableCmd);
2101 					n->subtype = AT_ColumnDefault;
2102 					n->name = $3;
2103 					n->def = $4;
2104 					$$ = (Node *)n;
2105 				}
2106 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
2107 			| ALTER opt_column ColId DROP NOT NULL_P
2108 				{
2109 					AlterTableCmd *n = makeNode(AlterTableCmd);
2110 					n->subtype = AT_DropNotNull;
2111 					n->name = $3;
2112 					$$ = (Node *)n;
2113 				}
2114 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
2115 			| ALTER opt_column ColId SET NOT NULL_P
2116 				{
2117 					AlterTableCmd *n = makeNode(AlterTableCmd);
2118 					n->subtype = AT_SetNotNull;
2119 					n->name = $3;
2120 					$$ = (Node *)n;
2121 				}
2122 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
2123 			| ALTER opt_column ColId SET STATISTICS SignedIconst
2124 				{
2125 					AlterTableCmd *n = makeNode(AlterTableCmd);
2126 					n->subtype = AT_SetStatistics;
2127 					n->name = $3;
2128 					n->def = (Node *) makeInteger($6);
2129 					$$ = (Node *)n;
2130 				}
2131 			/* ALTER TABLE <name> ALTER [COLUMN] <colnum> SET STATISTICS <SignedIconst> */
2132 			| ALTER opt_column Iconst SET STATISTICS SignedIconst
2133 				{
2134 					AlterTableCmd *n = makeNode(AlterTableCmd);
2135 
2136 					if ($3 <= 0 || $3 > PG_INT16_MAX)
2137 						ereport(ERROR,
2138 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2139 								 errmsg("column number must be in range from 1 to %d", PG_INT16_MAX),
2140 								 parser_errposition(@3)));
2141 
2142 					n->subtype = AT_SetStatistics;
2143 					n->num = (int16) $3;
2144 					n->def = (Node *) makeInteger($6);
2145 					$$ = (Node *)n;
2146 				}
2147 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
2148 			| ALTER opt_column ColId SET reloptions
2149 				{
2150 					AlterTableCmd *n = makeNode(AlterTableCmd);
2151 					n->subtype = AT_SetOptions;
2152 					n->name = $3;
2153 					n->def = (Node *) $5;
2154 					$$ = (Node *)n;
2155 				}
2156 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
2157 			| ALTER opt_column ColId RESET reloptions
2158 				{
2159 					AlterTableCmd *n = makeNode(AlterTableCmd);
2160 					n->subtype = AT_ResetOptions;
2161 					n->name = $3;
2162 					n->def = (Node *) $5;
2163 					$$ = (Node *)n;
2164 				}
2165 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
2166 			| ALTER opt_column ColId SET STORAGE ColId
2167 				{
2168 					AlterTableCmd *n = makeNode(AlterTableCmd);
2169 					n->subtype = AT_SetStorage;
2170 					n->name = $3;
2171 					n->def = (Node *) makeString($6);
2172 					$$ = (Node *)n;
2173 				}
2174 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> ADD GENERATED ... AS IDENTITY ... */
2175 			| ALTER opt_column ColId ADD_P GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
2176 				{
2177 					AlterTableCmd *n = makeNode(AlterTableCmd);
2178 					Constraint *c = makeNode(Constraint);
2179 
2180 					c->contype = CONSTR_IDENTITY;
2181 					c->generated_when = $6;
2182 					c->options = $9;
2183 					c->location = @5;
2184 
2185 					n->subtype = AT_AddIdentity;
2186 					n->name = $3;
2187 					n->def = (Node *) c;
2188 
2189 					$$ = (Node *)n;
2190 				}
2191 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET <sequence options>/RESET */
2192 			| ALTER opt_column ColId alter_identity_column_option_list
2193 				{
2194 					AlterTableCmd *n = makeNode(AlterTableCmd);
2195 					n->subtype = AT_SetIdentity;
2196 					n->name = $3;
2197 					n->def = (Node *) $4;
2198 					$$ = (Node *)n;
2199 				}
2200 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY */
2201 			| ALTER opt_column ColId DROP IDENTITY_P
2202 				{
2203 					AlterTableCmd *n = makeNode(AlterTableCmd);
2204 					n->subtype = AT_DropIdentity;
2205 					n->name = $3;
2206 					n->missing_ok = false;
2207 					$$ = (Node *)n;
2208 				}
2209 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY IF EXISTS */
2210 			| ALTER opt_column ColId DROP IDENTITY_P IF_P EXISTS
2211 				{
2212 					AlterTableCmd *n = makeNode(AlterTableCmd);
2213 					n->subtype = AT_DropIdentity;
2214 					n->name = $3;
2215 					n->missing_ok = true;
2216 					$$ = (Node *)n;
2217 				}
2218 			/* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
2219 			| DROP opt_column IF_P EXISTS ColId opt_drop_behavior
2220 				{
2221 					AlterTableCmd *n = makeNode(AlterTableCmd);
2222 					n->subtype = AT_DropColumn;
2223 					n->name = $5;
2224 					n->behavior = $6;
2225 					n->missing_ok = true;
2226 					$$ = (Node *)n;
2227 				}
2228 			/* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
2229 			| DROP opt_column ColId opt_drop_behavior
2230 				{
2231 					AlterTableCmd *n = makeNode(AlterTableCmd);
2232 					n->subtype = AT_DropColumn;
2233 					n->name = $3;
2234 					n->behavior = $4;
2235 					n->missing_ok = false;
2236 					$$ = (Node *)n;
2237 				}
2238 			/*
2239 			 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
2240 			 *		[ USING <expression> ]
2241 			 */
2242 			| ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
2243 				{
2244 					AlterTableCmd *n = makeNode(AlterTableCmd);
2245 					ColumnDef *def = makeNode(ColumnDef);
2246 					n->subtype = AT_AlterColumnType;
2247 					n->name = $3;
2248 					n->def = (Node *) def;
2249 					/* We only use these fields of the ColumnDef node */
2250 					def->typeName = $6;
2251 					def->collClause = (CollateClause *) $7;
2252 					def->raw_default = $8;
2253 					def->location = @3;
2254 					$$ = (Node *)n;
2255 				}
2256 			/* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
2257 			| ALTER opt_column ColId alter_generic_options
2258 				{
2259 					AlterTableCmd *n = makeNode(AlterTableCmd);
2260 					n->subtype = AT_AlterColumnGenericOptions;
2261 					n->name = $3;
2262 					n->def = (Node *) $4;
2263 					$$ = (Node *)n;
2264 				}
2265 			/* ALTER TABLE <name> ADD CONSTRAINT ... */
2266 			| ADD_P TableConstraint
2267 				{
2268 					AlterTableCmd *n = makeNode(AlterTableCmd);
2269 					n->subtype = AT_AddConstraint;
2270 					n->def = $2;
2271 					$$ = (Node *)n;
2272 				}
2273 			/* ALTER TABLE <name> ALTER CONSTRAINT ... */
2274 			| ALTER CONSTRAINT name ConstraintAttributeSpec
2275 				{
2276 					AlterTableCmd *n = makeNode(AlterTableCmd);
2277 					Constraint *c = makeNode(Constraint);
2278 					n->subtype = AT_AlterConstraint;
2279 					n->def = (Node *) c;
2280 					c->contype = CONSTR_FOREIGN; /* others not supported, yet */
2281 					c->conname = $3;
2282 					processCASbits($4, @4, "ALTER CONSTRAINT statement",
2283 									&c->deferrable,
2284 									&c->initdeferred,
2285 									NULL, NULL, yyscanner);
2286 					$$ = (Node *)n;
2287 				}
2288 			/* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
2289 			| VALIDATE CONSTRAINT name
2290 				{
2291 					AlterTableCmd *n = makeNode(AlterTableCmd);
2292 					n->subtype = AT_ValidateConstraint;
2293 					n->name = $3;
2294 					$$ = (Node *)n;
2295 				}
2296 			/* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
2297 			| DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
2298 				{
2299 					AlterTableCmd *n = makeNode(AlterTableCmd);
2300 					n->subtype = AT_DropConstraint;
2301 					n->name = $5;
2302 					n->behavior = $6;
2303 					n->missing_ok = true;
2304 					$$ = (Node *)n;
2305 				}
2306 			/* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
2307 			| DROP CONSTRAINT name opt_drop_behavior
2308 				{
2309 					AlterTableCmd *n = makeNode(AlterTableCmd);
2310 					n->subtype = AT_DropConstraint;
2311 					n->name = $3;
2312 					n->behavior = $4;
2313 					n->missing_ok = false;
2314 					$$ = (Node *)n;
2315 				}
2316 			/* ALTER TABLE <name> SET WITH OIDS  */
2317 			| SET WITH OIDS
2318 				{
2319 					AlterTableCmd *n = makeNode(AlterTableCmd);
2320 					n->subtype = AT_AddOids;
2321 					$$ = (Node *)n;
2322 				}
2323 			/* ALTER TABLE <name> SET WITHOUT OIDS  */
2324 			| SET WITHOUT OIDS
2325 				{
2326 					AlterTableCmd *n = makeNode(AlterTableCmd);
2327 					n->subtype = AT_DropOids;
2328 					$$ = (Node *)n;
2329 				}
2330 			/* ALTER TABLE <name> CLUSTER ON <indexname> */
2331 			| CLUSTER ON name
2332 				{
2333 					AlterTableCmd *n = makeNode(AlterTableCmd);
2334 					n->subtype = AT_ClusterOn;
2335 					n->name = $3;
2336 					$$ = (Node *)n;
2337 				}
2338 			/* ALTER TABLE <name> SET WITHOUT CLUSTER */
2339 			| SET WITHOUT CLUSTER
2340 				{
2341 					AlterTableCmd *n = makeNode(AlterTableCmd);
2342 					n->subtype = AT_DropCluster;
2343 					n->name = NULL;
2344 					$$ = (Node *)n;
2345 				}
2346 			/* ALTER TABLE <name> SET LOGGED  */
2347 			| SET LOGGED
2348 				{
2349 					AlterTableCmd *n = makeNode(AlterTableCmd);
2350 					n->subtype = AT_SetLogged;
2351 					$$ = (Node *)n;
2352 				}
2353 			/* ALTER TABLE <name> SET UNLOGGED  */
2354 			| SET UNLOGGED
2355 				{
2356 					AlterTableCmd *n = makeNode(AlterTableCmd);
2357 					n->subtype = AT_SetUnLogged;
2358 					$$ = (Node *)n;
2359 				}
2360 			/* ALTER TABLE <name> ENABLE TRIGGER <trig> */
2361 			| ENABLE_P TRIGGER name
2362 				{
2363 					AlterTableCmd *n = makeNode(AlterTableCmd);
2364 					n->subtype = AT_EnableTrig;
2365 					n->name = $3;
2366 					$$ = (Node *)n;
2367 				}
2368 			/* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
2369 			| ENABLE_P ALWAYS TRIGGER name
2370 				{
2371 					AlterTableCmd *n = makeNode(AlterTableCmd);
2372 					n->subtype = AT_EnableAlwaysTrig;
2373 					n->name = $4;
2374 					$$ = (Node *)n;
2375 				}
2376 			/* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
2377 			| ENABLE_P REPLICA TRIGGER name
2378 				{
2379 					AlterTableCmd *n = makeNode(AlterTableCmd);
2380 					n->subtype = AT_EnableReplicaTrig;
2381 					n->name = $4;
2382 					$$ = (Node *)n;
2383 				}
2384 			/* ALTER TABLE <name> ENABLE TRIGGER ALL */
2385 			| ENABLE_P TRIGGER ALL
2386 				{
2387 					AlterTableCmd *n = makeNode(AlterTableCmd);
2388 					n->subtype = AT_EnableTrigAll;
2389 					$$ = (Node *)n;
2390 				}
2391 			/* ALTER TABLE <name> ENABLE TRIGGER USER */
2392 			| ENABLE_P TRIGGER USER
2393 				{
2394 					AlterTableCmd *n = makeNode(AlterTableCmd);
2395 					n->subtype = AT_EnableTrigUser;
2396 					$$ = (Node *)n;
2397 				}
2398 			/* ALTER TABLE <name> DISABLE TRIGGER <trig> */
2399 			| DISABLE_P TRIGGER name
2400 				{
2401 					AlterTableCmd *n = makeNode(AlterTableCmd);
2402 					n->subtype = AT_DisableTrig;
2403 					n->name = $3;
2404 					$$ = (Node *)n;
2405 				}
2406 			/* ALTER TABLE <name> DISABLE TRIGGER ALL */
2407 			| DISABLE_P TRIGGER ALL
2408 				{
2409 					AlterTableCmd *n = makeNode(AlterTableCmd);
2410 					n->subtype = AT_DisableTrigAll;
2411 					$$ = (Node *)n;
2412 				}
2413 			/* ALTER TABLE <name> DISABLE TRIGGER USER */
2414 			| DISABLE_P TRIGGER USER
2415 				{
2416 					AlterTableCmd *n = makeNode(AlterTableCmd);
2417 					n->subtype = AT_DisableTrigUser;
2418 					$$ = (Node *)n;
2419 				}
2420 			/* ALTER TABLE <name> ENABLE RULE <rule> */
2421 			| ENABLE_P RULE name
2422 				{
2423 					AlterTableCmd *n = makeNode(AlterTableCmd);
2424 					n->subtype = AT_EnableRule;
2425 					n->name = $3;
2426 					$$ = (Node *)n;
2427 				}
2428 			/* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
2429 			| ENABLE_P ALWAYS RULE name
2430 				{
2431 					AlterTableCmd *n = makeNode(AlterTableCmd);
2432 					n->subtype = AT_EnableAlwaysRule;
2433 					n->name = $4;
2434 					$$ = (Node *)n;
2435 				}
2436 			/* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
2437 			| ENABLE_P REPLICA RULE name
2438 				{
2439 					AlterTableCmd *n = makeNode(AlterTableCmd);
2440 					n->subtype = AT_EnableReplicaRule;
2441 					n->name = $4;
2442 					$$ = (Node *)n;
2443 				}
2444 			/* ALTER TABLE <name> DISABLE RULE <rule> */
2445 			| DISABLE_P RULE name
2446 				{
2447 					AlterTableCmd *n = makeNode(AlterTableCmd);
2448 					n->subtype = AT_DisableRule;
2449 					n->name = $3;
2450 					$$ = (Node *)n;
2451 				}
2452 			/* ALTER TABLE <name> INHERIT <parent> */
2453 			| INHERIT qualified_name
2454 				{
2455 					AlterTableCmd *n = makeNode(AlterTableCmd);
2456 					n->subtype = AT_AddInherit;
2457 					n->def = (Node *) $2;
2458 					$$ = (Node *)n;
2459 				}
2460 			/* ALTER TABLE <name> NO INHERIT <parent> */
2461 			| NO INHERIT qualified_name
2462 				{
2463 					AlterTableCmd *n = makeNode(AlterTableCmd);
2464 					n->subtype = AT_DropInherit;
2465 					n->def = (Node *) $3;
2466 					$$ = (Node *)n;
2467 				}
2468 			/* ALTER TABLE <name> OF <type_name> */
2469 			| OF any_name
2470 				{
2471 					AlterTableCmd *n = makeNode(AlterTableCmd);
2472 					TypeName *def = makeTypeNameFromNameList($2);
2473 					def->location = @2;
2474 					n->subtype = AT_AddOf;
2475 					n->def = (Node *) def;
2476 					$$ = (Node *)n;
2477 				}
2478 			/* ALTER TABLE <name> NOT OF */
2479 			| NOT OF
2480 				{
2481 					AlterTableCmd *n = makeNode(AlterTableCmd);
2482 					n->subtype = AT_DropOf;
2483 					$$ = (Node *)n;
2484 				}
2485 			/* ALTER TABLE <name> OWNER TO RoleSpec */
2486 			| OWNER TO RoleSpec
2487 				{
2488 					AlterTableCmd *n = makeNode(AlterTableCmd);
2489 					n->subtype = AT_ChangeOwner;
2490 					n->newowner = $3;
2491 					$$ = (Node *)n;
2492 				}
2493 			/* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
2494 			| SET TABLESPACE name
2495 				{
2496 					AlterTableCmd *n = makeNode(AlterTableCmd);
2497 					n->subtype = AT_SetTableSpace;
2498 					n->name = $3;
2499 					$$ = (Node *)n;
2500 				}
2501 			/* ALTER TABLE <name> SET (...) */
2502 			| SET reloptions
2503 				{
2504 					AlterTableCmd *n = makeNode(AlterTableCmd);
2505 					n->subtype = AT_SetRelOptions;
2506 					n->def = (Node *)$2;
2507 					$$ = (Node *)n;
2508 				}
2509 			/* ALTER TABLE <name> RESET (...) */
2510 			| RESET reloptions
2511 				{
2512 					AlterTableCmd *n = makeNode(AlterTableCmd);
2513 					n->subtype = AT_ResetRelOptions;
2514 					n->def = (Node *)$2;
2515 					$$ = (Node *)n;
2516 				}
2517 			/* ALTER TABLE <name> REPLICA IDENTITY  */
2518 			| REPLICA IDENTITY_P replica_identity
2519 				{
2520 					AlterTableCmd *n = makeNode(AlterTableCmd);
2521 					n->subtype = AT_ReplicaIdentity;
2522 					n->def = $3;
2523 					$$ = (Node *)n;
2524 				}
2525 			/* ALTER TABLE <name> ENABLE ROW LEVEL SECURITY */
2526 			| ENABLE_P ROW LEVEL SECURITY
2527 				{
2528 					AlterTableCmd *n = makeNode(AlterTableCmd);
2529 					n->subtype = AT_EnableRowSecurity;
2530 					$$ = (Node *)n;
2531 				}
2532 			/* ALTER TABLE <name> DISABLE ROW LEVEL SECURITY */
2533 			| DISABLE_P ROW LEVEL SECURITY
2534 				{
2535 					AlterTableCmd *n = makeNode(AlterTableCmd);
2536 					n->subtype = AT_DisableRowSecurity;
2537 					$$ = (Node *)n;
2538 				}
2539 			/* ALTER TABLE <name> FORCE ROW LEVEL SECURITY */
2540 			| FORCE ROW LEVEL SECURITY
2541 				{
2542 					AlterTableCmd *n = makeNode(AlterTableCmd);
2543 					n->subtype = AT_ForceRowSecurity;
2544 					$$ = (Node *)n;
2545 				}
2546 			/* ALTER TABLE <name> NO FORCE ROW LEVEL SECURITY */
2547 			| NO FORCE ROW LEVEL SECURITY
2548 				{
2549 					AlterTableCmd *n = makeNode(AlterTableCmd);
2550 					n->subtype = AT_NoForceRowSecurity;
2551 					$$ = (Node *)n;
2552 				}
2553 			| alter_generic_options
2554 				{
2555 					AlterTableCmd *n = makeNode(AlterTableCmd);
2556 					n->subtype = AT_GenericOptions;
2557 					n->def = (Node *)$1;
2558 					$$ = (Node *) n;
2559 				}
2560 		;
2561 
2562 alter_column_default:
2563 			SET DEFAULT a_expr			{ $$ = $3; }
2564 			| DROP DEFAULT				{ $$ = NULL; }
2565 		;
2566 
2567 opt_drop_behavior:
2568 			CASCADE						{ $$ = DROP_CASCADE; }
2569 			| RESTRICT					{ $$ = DROP_RESTRICT; }
2570 			| /* EMPTY */				{ $$ = DROP_RESTRICT; /* default */ }
2571 		;
2572 
2573 opt_collate_clause:
2574 			COLLATE any_name
2575 				{
2576 					CollateClause *n = makeNode(CollateClause);
2577 					n->arg = NULL;
2578 					n->collname = $2;
2579 					n->location = @1;
2580 					$$ = (Node *) n;
2581 				}
2582 			| /* EMPTY */				{ $$ = NULL; }
2583 		;
2584 
2585 alter_using:
2586 			USING a_expr				{ $$ = $2; }
2587 			| /* EMPTY */				{ $$ = NULL; }
2588 		;
2589 
2590 replica_identity:
2591 			NOTHING
2592 				{
2593 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2594 					n->identity_type = REPLICA_IDENTITY_NOTHING;
2595 					n->name = NULL;
2596 					$$ = (Node *) n;
2597 				}
2598 			| FULL
2599 				{
2600 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2601 					n->identity_type = REPLICA_IDENTITY_FULL;
2602 					n->name = NULL;
2603 					$$ = (Node *) n;
2604 				}
2605 			| DEFAULT
2606 				{
2607 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2608 					n->identity_type = REPLICA_IDENTITY_DEFAULT;
2609 					n->name = NULL;
2610 					$$ = (Node *) n;
2611 				}
2612 			| USING INDEX name
2613 				{
2614 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2615 					n->identity_type = REPLICA_IDENTITY_INDEX;
2616 					n->name = $3;
2617 					$$ = (Node *) n;
2618 				}
2619 ;
2620 
2621 reloptions:
2622 			'(' reloption_list ')'					{ $$ = $2; }
2623 		;
2624 
2625 opt_reloptions:		WITH reloptions					{ $$ = $2; }
2626 			 |		/* EMPTY */						{ $$ = NIL; }
2627 		;
2628 
2629 reloption_list:
2630 			reloption_elem							{ $$ = list_make1($1); }
2631 			| reloption_list ',' reloption_elem		{ $$ = lappend($1, $3); }
2632 		;
2633 
2634 /* This should match def_elem and also allow qualified names */
2635 reloption_elem:
2636 			ColLabel '=' def_arg
2637 				{
2638 					$$ = makeDefElem($1, (Node *) $3, @1);
2639 				}
2640 			| ColLabel
2641 				{
2642 					$$ = makeDefElem($1, NULL, @1);
2643 				}
2644 			| ColLabel '.' ColLabel '=' def_arg
2645 				{
2646 					$$ = makeDefElemExtended($1, $3, (Node *) $5,
2647 											 DEFELEM_UNSPEC, @1);
2648 				}
2649 			| ColLabel '.' ColLabel
2650 				{
2651 					$$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC, @1);
2652 				}
2653 		;
2654 
2655 alter_identity_column_option_list:
2656 			alter_identity_column_option
2657 				{ $$ = list_make1($1); }
2658 			| alter_identity_column_option_list alter_identity_column_option
2659 				{ $$ = lappend($1, $2); }
2660 		;
2661 
2662 alter_identity_column_option:
2663 			RESTART
2664 				{
2665 					$$ = makeDefElem("restart", NULL, @1);
2666 				}
2667 			| RESTART opt_with NumericOnly
2668 				{
2669 					$$ = makeDefElem("restart", (Node *)$3, @1);
2670 				}
2671 			| SET SeqOptElem
2672 				{
2673 					if (strcmp($2->defname, "as") == 0 ||
2674 						strcmp($2->defname, "restart") == 0 ||
2675 						strcmp($2->defname, "owned_by") == 0)
2676 						ereport(ERROR,
2677 								(errcode(ERRCODE_SYNTAX_ERROR),
2678 								 errmsg("sequence option \"%s\" not supported here", $2->defname),
2679 								 parser_errposition(@2)));
2680 					$$ = $2;
2681 				}
2682 			| SET GENERATED generated_when
2683 				{
2684 					$$ = makeDefElem("generated", (Node *) makeInteger($3), @1);
2685 				}
2686 		;
2687 
2688 PartitionBoundSpec:
2689 			/* a HASH partition */
2690 			FOR VALUES WITH '(' hash_partbound ')'
2691 				{
2692 					ListCell   *lc;
2693 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2694 
2695 					n->strategy = PARTITION_STRATEGY_HASH;
2696 					n->modulus = n->remainder = -1;
2697 
foreach(lc,$5)2698 					foreach (lc, $5)
2699 					{
2700 						DefElem    *opt = lfirst_node(DefElem, lc);
2701 
2702 						if (strcmp(opt->defname, "modulus") == 0)
2703 						{
2704 							if (n->modulus != -1)
2705 								ereport(ERROR,
2706 										(errcode(ERRCODE_DUPLICATE_OBJECT),
2707 										 errmsg("modulus for hash partition provided more than once"),
2708 										 parser_errposition(opt->location)));
2709 							n->modulus = defGetInt32(opt);
2710 						}
2711 						else if (strcmp(opt->defname, "remainder") == 0)
2712 						{
2713 							if (n->remainder != -1)
2714 								ereport(ERROR,
2715 										(errcode(ERRCODE_DUPLICATE_OBJECT),
2716 										 errmsg("remainder for hash partition provided more than once"),
2717 										 parser_errposition(opt->location)));
2718 							n->remainder = defGetInt32(opt);
2719 						}
2720 						else
2721 							ereport(ERROR,
2722 									(errcode(ERRCODE_SYNTAX_ERROR),
2723 									 errmsg("unrecognized hash partition bound specification \"%s\"",
2724 											opt->defname),
2725 									 parser_errposition(opt->location)));
2726 					}
2727 
2728 					if (n->modulus == -1)
2729 						ereport(ERROR,
2730 								(errcode(ERRCODE_SYNTAX_ERROR),
2731 								 errmsg("modulus for hash partition must be specified")));
2732 					if (n->remainder == -1)
2733 						ereport(ERROR,
2734 								(errcode(ERRCODE_SYNTAX_ERROR),
2735 								 errmsg("remainder for hash partition must be specified")));
2736 
2737 					n->location = @3;
2738 
2739 					$$ = n;
2740 				}
2741 
2742 			/* a LIST partition */
2743 			| FOR VALUES IN_P '(' partbound_datum_list ')'
2744 				{
2745 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2746 
2747 					n->strategy = PARTITION_STRATEGY_LIST;
2748 					n->is_default = false;
2749 					n->listdatums = $5;
2750 					n->location = @3;
2751 
2752 					$$ = n;
2753 				}
2754 
2755 			/* a RANGE partition */
2756 			| FOR VALUES FROM '(' range_datum_list ')' TO '(' range_datum_list ')'
2757 				{
2758 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2759 
2760 					n->strategy = PARTITION_STRATEGY_RANGE;
2761 					n->is_default = false;
2762 					n->lowerdatums = $5;
2763 					n->upperdatums = $9;
2764 					n->location = @3;
2765 
2766 					$$ = n;
2767 				}
2768 
2769 			/* a DEFAULT partition */
2770 			| DEFAULT
2771 				{
2772 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2773 
2774 					n->is_default = true;
2775 					n->location = @1;
2776 
2777 					$$ = n;
2778 				}
2779 		;
2780 
2781 hash_partbound_elem:
2782 		NonReservedWord Iconst
2783 			{
2784 				$$ = makeDefElem($1, (Node *)makeInteger($2), @1);
2785 			}
2786 		;
2787 
2788 hash_partbound:
2789 		hash_partbound_elem
2790 			{
2791 				$$ = list_make1($1);
2792 			}
2793 		| hash_partbound ',' hash_partbound_elem
2794 			{
2795 				$$ = lappend($1, $3);
2796 			}
2797 		;
2798 
2799 partbound_datum:
2800 			Sconst			{ $$ = makeStringConst($1, @1); }
2801 			| NumericOnly	{ $$ = makeAConst($1, @1); }
2802 			| TRUE_P		{ $$ = makeStringConst(pstrdup("true"), @1); }
2803 			| FALSE_P		{ $$ = makeStringConst(pstrdup("false"), @1); }
2804 			| NULL_P		{ $$ = makeNullAConst(@1); }
2805 		;
2806 
2807 partbound_datum_list:
2808 			partbound_datum						{ $$ = list_make1($1); }
2809 			| partbound_datum_list ',' partbound_datum
2810 												{ $$ = lappend($1, $3); }
2811 		;
2812 
2813 range_datum_list:
2814 			PartitionRangeDatum					{ $$ = list_make1($1); }
2815 			| range_datum_list ',' PartitionRangeDatum
2816 												{ $$ = lappend($1, $3); }
2817 		;
2818 
2819 PartitionRangeDatum:
2820 			MINVALUE
2821 				{
2822 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2823 
2824 					n->kind = PARTITION_RANGE_DATUM_MINVALUE;
2825 					n->value = NULL;
2826 					n->location = @1;
2827 
2828 					$$ = (Node *) n;
2829 				}
2830 			| MAXVALUE
2831 				{
2832 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2833 
2834 					n->kind = PARTITION_RANGE_DATUM_MAXVALUE;
2835 					n->value = NULL;
2836 					n->location = @1;
2837 
2838 					$$ = (Node *) n;
2839 				}
2840 			| partbound_datum
2841 				{
2842 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2843 
2844 					n->kind = PARTITION_RANGE_DATUM_VALUE;
2845 					n->value = $1;
2846 					n->location = @1;
2847 
2848 					$$ = (Node *) n;
2849 				}
2850 		;
2851 
2852 /*****************************************************************************
2853  *
2854  *	ALTER TYPE
2855  *
2856  * really variants of the ALTER TABLE subcommands with different spellings
2857  *****************************************************************************/
2858 
2859 AlterCompositeTypeStmt:
2860 			ALTER TYPE_P any_name alter_type_cmds
2861 				{
2862 					AlterTableStmt *n = makeNode(AlterTableStmt);
2863 
2864 					/* can't use qualified_name, sigh */
2865 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
2866 					n->cmds = $4;
2867 					n->relkind = OBJECT_TYPE;
2868 					$$ = (Node *)n;
2869 				}
2870 			;
2871 
2872 alter_type_cmds:
2873 			alter_type_cmd							{ $$ = list_make1($1); }
2874 			| alter_type_cmds ',' alter_type_cmd	{ $$ = lappend($1, $3); }
2875 		;
2876 
2877 alter_type_cmd:
2878 			/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
2879 			ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
2880 				{
2881 					AlterTableCmd *n = makeNode(AlterTableCmd);
2882 					n->subtype = AT_AddColumn;
2883 					n->def = $3;
2884 					n->behavior = $4;
2885 					$$ = (Node *)n;
2886 				}
2887 			/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
2888 			| DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
2889 				{
2890 					AlterTableCmd *n = makeNode(AlterTableCmd);
2891 					n->subtype = AT_DropColumn;
2892 					n->name = $5;
2893 					n->behavior = $6;
2894 					n->missing_ok = true;
2895 					$$ = (Node *)n;
2896 				}
2897 			/* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
2898 			| DROP ATTRIBUTE ColId opt_drop_behavior
2899 				{
2900 					AlterTableCmd *n = makeNode(AlterTableCmd);
2901 					n->subtype = AT_DropColumn;
2902 					n->name = $3;
2903 					n->behavior = $4;
2904 					n->missing_ok = false;
2905 					$$ = (Node *)n;
2906 				}
2907 			/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
2908 			| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
2909 				{
2910 					AlterTableCmd *n = makeNode(AlterTableCmd);
2911 					ColumnDef *def = makeNode(ColumnDef);
2912 					n->subtype = AT_AlterColumnType;
2913 					n->name = $3;
2914 					n->def = (Node *) def;
2915 					n->behavior = $8;
2916 					/* We only use these fields of the ColumnDef node */
2917 					def->typeName = $6;
2918 					def->collClause = (CollateClause *) $7;
2919 					def->raw_default = NULL;
2920 					def->location = @3;
2921 					$$ = (Node *)n;
2922 				}
2923 		;
2924 
2925 
2926 /*****************************************************************************
2927  *
2928  *		QUERY :
2929  *				close <portalname>
2930  *
2931  *****************************************************************************/
2932 
2933 ClosePortalStmt:
2934 			CLOSE cursor_name
2935 				{
2936 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2937 					n->portalname = $2;
2938 					$$ = (Node *)n;
2939 				}
2940 			| CLOSE ALL
2941 				{
2942 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2943 					n->portalname = NULL;
2944 					$$ = (Node *)n;
2945 				}
2946 		;
2947 
2948 
2949 /*****************************************************************************
2950  *
2951  *		QUERY :
2952  *				COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
2953  *				COPY ( query ) TO file	[WITH] [(options)]
2954  *
2955  *				where 'query' can be one of:
2956  *				{ SELECT | UPDATE | INSERT | DELETE }
2957  *
2958  *				and 'file' can be one of:
2959  *				{ PROGRAM 'command' | STDIN | STDOUT | 'filename' }
2960  *
2961  *				In the preferred syntax the options are comma-separated
2962  *				and use generic identifiers instead of keywords.  The pre-9.0
2963  *				syntax had a hard-wired, space-separated set of options.
2964  *
2965  *				Really old syntax, from versions 7.2 and prior:
2966  *				COPY [ BINARY ] table [ WITH OIDS ] FROM/TO file
2967  *					[ [ USING ] DELIMITERS 'delimiter' ] ]
2968  *					[ WITH NULL AS 'null string' ]
2969  *				This option placement is not supported with COPY (query...).
2970  *
2971  *****************************************************************************/
2972 
2973 CopyStmt:	COPY opt_binary qualified_name opt_column_list opt_oids
2974 			copy_from opt_program copy_file_name copy_delimiter opt_with copy_options
2975 				{
2976 					CopyStmt *n = makeNode(CopyStmt);
2977 					n->relation = $3;
2978 					n->query = NULL;
2979 					n->attlist = $4;
2980 					n->is_from = $6;
2981 					n->is_program = $7;
2982 					n->filename = $8;
2983 
2984 					if (n->is_program && n->filename == NULL)
2985 						ereport(ERROR,
2986 								(errcode(ERRCODE_SYNTAX_ERROR),
2987 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2988 								 parser_errposition(@8)));
2989 
2990 					n->options = NIL;
2991 					/* Concatenate user-supplied flags */
2992 					if ($2)
2993 						n->options = lappend(n->options, $2);
2994 					if ($5)
2995 						n->options = lappend(n->options, $5);
2996 					if ($9)
2997 						n->options = lappend(n->options, $9);
2998 					if ($11)
2999 						n->options = list_concat(n->options, $11);
3000 					$$ = (Node *)n;
3001 				}
3002 			| COPY '(' PreparableStmt ')' TO opt_program copy_file_name opt_with copy_options
3003 				{
3004 					CopyStmt *n = makeNode(CopyStmt);
3005 					n->relation = NULL;
3006 					n->query = $3;
3007 					n->attlist = NIL;
3008 					n->is_from = false;
3009 					n->is_program = $6;
3010 					n->filename = $7;
3011 					n->options = $9;
3012 
3013 					if (n->is_program && n->filename == NULL)
3014 						ereport(ERROR,
3015 								(errcode(ERRCODE_SYNTAX_ERROR),
3016 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
3017 								 parser_errposition(@5)));
3018 
3019 					$$ = (Node *)n;
3020 				}
3021 		;
3022 
3023 copy_from:
3024 			FROM									{ $$ = true; }
3025 			| TO									{ $$ = false; }
3026 		;
3027 
3028 opt_program:
3029 			PROGRAM									{ $$ = true; }
3030 			| /* EMPTY */							{ $$ = false; }
3031 		;
3032 
3033 /*
3034  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
3035  * used depends on the direction. (It really doesn't make sense to copy from
3036  * stdout. We silently correct the "typo".)		 - AY 9/94
3037  */
3038 copy_file_name:
3039 			Sconst									{ $$ = $1; }
3040 			| STDIN									{ $$ = NULL; }
3041 			| STDOUT								{ $$ = NULL; }
3042 		;
3043 
3044 copy_options: copy_opt_list							{ $$ = $1; }
3045 			| '(' copy_generic_opt_list ')'			{ $$ = $2; }
3046 		;
3047 
3048 /* old COPY option syntax */
3049 copy_opt_list:
3050 			copy_opt_list copy_opt_item				{ $$ = lappend($1, $2); }
3051 			| /* EMPTY */							{ $$ = NIL; }
3052 		;
3053 
3054 copy_opt_item:
3055 			BINARY
3056 				{
3057 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3058 				}
3059 			| OIDS
3060 				{
3061 					$$ = makeDefElem("oids", (Node *)makeInteger(true), @1);
3062 				}
3063 			| FREEZE
3064 				{
3065 					$$ = makeDefElem("freeze", (Node *)makeInteger(true), @1);
3066 				}
3067 			| DELIMITER opt_as Sconst
3068 				{
3069 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @1);
3070 				}
3071 			| NULL_P opt_as Sconst
3072 				{
3073 					$$ = makeDefElem("null", (Node *)makeString($3), @1);
3074 				}
3075 			| CSV
3076 				{
3077 					$$ = makeDefElem("format", (Node *)makeString("csv"), @1);
3078 				}
3079 			| HEADER_P
3080 				{
3081 					$$ = makeDefElem("header", (Node *)makeInteger(true), @1);
3082 				}
3083 			| QUOTE opt_as Sconst
3084 				{
3085 					$$ = makeDefElem("quote", (Node *)makeString($3), @1);
3086 				}
3087 			| ESCAPE opt_as Sconst
3088 				{
3089 					$$ = makeDefElem("escape", (Node *)makeString($3), @1);
3090 				}
3091 			| FORCE QUOTE columnList
3092 				{
3093 					$$ = makeDefElem("force_quote", (Node *)$3, @1);
3094 				}
3095 			| FORCE QUOTE '*'
3096 				{
3097 					$$ = makeDefElem("force_quote", (Node *)makeNode(A_Star), @1);
3098 				}
3099 			| FORCE NOT NULL_P columnList
3100 				{
3101 					$$ = makeDefElem("force_not_null", (Node *)$4, @1);
3102 				}
3103 			| FORCE NULL_P columnList
3104 				{
3105 					$$ = makeDefElem("force_null", (Node *)$3, @1);
3106 				}
3107 			| ENCODING Sconst
3108 				{
3109 					$$ = makeDefElem("encoding", (Node *)makeString($2), @1);
3110 				}
3111 		;
3112 
3113 /* The following exist for backward compatibility with very old versions */
3114 
3115 opt_binary:
3116 			BINARY
3117 				{
3118 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3119 				}
3120 			| /*EMPTY*/								{ $$ = NULL; }
3121 		;
3122 
3123 opt_oids:
3124 			WITH OIDS
3125 				{
3126 					$$ = makeDefElem("oids", (Node *)makeInteger(true), @1);
3127 				}
3128 			| /*EMPTY*/								{ $$ = NULL; }
3129 		;
3130 
3131 copy_delimiter:
3132 			opt_using DELIMITERS Sconst
3133 				{
3134 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @2);
3135 				}
3136 			| /*EMPTY*/								{ $$ = NULL; }
3137 		;
3138 
3139 opt_using:
3140 			USING									{}
3141 			| /*EMPTY*/								{}
3142 		;
3143 
3144 /* new COPY option syntax */
3145 copy_generic_opt_list:
3146 			copy_generic_opt_elem
3147 				{
3148 					$$ = list_make1($1);
3149 				}
3150 			| copy_generic_opt_list ',' copy_generic_opt_elem
3151 				{
3152 					$$ = lappend($1, $3);
3153 				}
3154 		;
3155 
3156 copy_generic_opt_elem:
3157 			ColLabel copy_generic_opt_arg
3158 				{
3159 					$$ = makeDefElem($1, $2, @1);
3160 				}
3161 		;
3162 
3163 copy_generic_opt_arg:
3164 			opt_boolean_or_string			{ $$ = (Node *) makeString($1); }
3165 			| NumericOnly					{ $$ = (Node *) $1; }
3166 			| '*'							{ $$ = (Node *) makeNode(A_Star); }
3167 			| '(' copy_generic_opt_arg_list ')'		{ $$ = (Node *) $2; }
3168 			| /* EMPTY */					{ $$ = NULL; }
3169 		;
3170 
3171 copy_generic_opt_arg_list:
3172 			  copy_generic_opt_arg_list_item
3173 				{
3174 					$$ = list_make1($1);
3175 				}
3176 			| copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
3177 				{
3178 					$$ = lappend($1, $3);
3179 				}
3180 		;
3181 
3182 /* beware of emitting non-string list elements here; see commands/define.c */
3183 copy_generic_opt_arg_list_item:
3184 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
3185 		;
3186 
3187 
3188 /*****************************************************************************
3189  *
3190  *		QUERY :
3191  *				CREATE TABLE relname
3192  *
3193  *****************************************************************************/
3194 
3195 CreateStmt:	CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
3196 			OptInherit OptPartitionSpec OptWith OnCommitOption OptTableSpace
3197 				{
3198 					CreateStmt *n = makeNode(CreateStmt);
3199 					$4->relpersistence = $2;
3200 					n->relation = $4;
3201 					n->tableElts = $6;
3202 					n->inhRelations = $8;
3203 					n->partspec = $9;
3204 					n->ofTypename = NULL;
3205 					n->constraints = NIL;
3206 					n->options = $10;
3207 					n->oncommit = $11;
3208 					n->tablespacename = $12;
3209 					n->if_not_exists = false;
3210 					$$ = (Node *)n;
3211 				}
3212 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
3213 			OptTableElementList ')' OptInherit OptPartitionSpec OptWith
3214 			OnCommitOption OptTableSpace
3215 				{
3216 					CreateStmt *n = makeNode(CreateStmt);
3217 					$7->relpersistence = $2;
3218 					n->relation = $7;
3219 					n->tableElts = $9;
3220 					n->inhRelations = $11;
3221 					n->partspec = $12;
3222 					n->ofTypename = NULL;
3223 					n->constraints = NIL;
3224 					n->options = $13;
3225 					n->oncommit = $14;
3226 					n->tablespacename = $15;
3227 					n->if_not_exists = true;
3228 					$$ = (Node *)n;
3229 				}
3230 		| CREATE OptTemp TABLE qualified_name OF any_name
3231 			OptTypedTableElementList OptPartitionSpec OptWith OnCommitOption
3232 			OptTableSpace
3233 				{
3234 					CreateStmt *n = makeNode(CreateStmt);
3235 					$4->relpersistence = $2;
3236 					n->relation = $4;
3237 					n->tableElts = $7;
3238 					n->inhRelations = NIL;
3239 					n->partspec = $8;
3240 					n->ofTypename = makeTypeNameFromNameList($6);
3241 					n->ofTypename->location = @6;
3242 					n->constraints = NIL;
3243 					n->options = $9;
3244 					n->oncommit = $10;
3245 					n->tablespacename = $11;
3246 					n->if_not_exists = false;
3247 					$$ = (Node *)n;
3248 				}
3249 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
3250 			OptTypedTableElementList OptPartitionSpec OptWith OnCommitOption
3251 			OptTableSpace
3252 				{
3253 					CreateStmt *n = makeNode(CreateStmt);
3254 					$7->relpersistence = $2;
3255 					n->relation = $7;
3256 					n->tableElts = $10;
3257 					n->inhRelations = NIL;
3258 					n->partspec = $11;
3259 					n->ofTypename = makeTypeNameFromNameList($9);
3260 					n->ofTypename->location = @9;
3261 					n->constraints = NIL;
3262 					n->options = $12;
3263 					n->oncommit = $13;
3264 					n->tablespacename = $14;
3265 					n->if_not_exists = true;
3266 					$$ = (Node *)n;
3267 				}
3268 		| CREATE OptTemp TABLE qualified_name PARTITION OF qualified_name
3269 			OptTypedTableElementList PartitionBoundSpec OptPartitionSpec OptWith
3270 			OnCommitOption OptTableSpace
3271 				{
3272 					CreateStmt *n = makeNode(CreateStmt);
3273 					$4->relpersistence = $2;
3274 					n->relation = $4;
3275 					n->tableElts = $8;
3276 					n->inhRelations = list_make1($7);
3277 					n->partbound = $9;
3278 					n->partspec = $10;
3279 					n->ofTypename = NULL;
3280 					n->constraints = NIL;
3281 					n->options = $11;
3282 					n->oncommit = $12;
3283 					n->tablespacename = $13;
3284 					n->if_not_exists = false;
3285 					$$ = (Node *)n;
3286 				}
3287 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name PARTITION OF
3288 			qualified_name OptTypedTableElementList PartitionBoundSpec OptPartitionSpec
3289 			OptWith OnCommitOption OptTableSpace
3290 				{
3291 					CreateStmt *n = makeNode(CreateStmt);
3292 					$7->relpersistence = $2;
3293 					n->relation = $7;
3294 					n->tableElts = $11;
3295 					n->inhRelations = list_make1($10);
3296 					n->partbound = $12;
3297 					n->partspec = $13;
3298 					n->ofTypename = NULL;
3299 					n->constraints = NIL;
3300 					n->options = $14;
3301 					n->oncommit = $15;
3302 					n->tablespacename = $16;
3303 					n->if_not_exists = true;
3304 					$$ = (Node *)n;
3305 				}
3306 		;
3307 
3308 /*
3309  * Redundancy here is needed to avoid shift/reduce conflicts,
3310  * since TEMP is not a reserved word.  See also OptTempTableName.
3311  *
3312  * NOTE: we accept both GLOBAL and LOCAL options.  They currently do nothing,
3313  * but future versions might consider GLOBAL to request SQL-spec-compliant
3314  * temp table behavior, so warn about that.  Since we have no modules the
3315  * LOCAL keyword is really meaningless; furthermore, some other products
3316  * implement LOCAL as meaning the same as our default temp table behavior,
3317  * so we'll probably continue to treat LOCAL as a noise word.
3318  */
3319 OptTemp:	TEMPORARY					{ $$ = RELPERSISTENCE_TEMP; }
3320 			| TEMP						{ $$ = RELPERSISTENCE_TEMP; }
3321 			| LOCAL TEMPORARY			{ $$ = RELPERSISTENCE_TEMP; }
3322 			| LOCAL TEMP				{ $$ = RELPERSISTENCE_TEMP; }
3323 			| GLOBAL TEMPORARY
3324 				{
3325 					ereport(WARNING,
3326 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3327 							 parser_errposition(@1)));
3328 					$$ = RELPERSISTENCE_TEMP;
3329 				}
3330 			| GLOBAL TEMP
3331 				{
3332 					ereport(WARNING,
3333 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3334 							 parser_errposition(@1)));
3335 					$$ = RELPERSISTENCE_TEMP;
3336 				}
3337 			| UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
3338 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
3339 		;
3340 
3341 OptTableElementList:
3342 			TableElementList					{ $$ = $1; }
3343 			| /*EMPTY*/							{ $$ = NIL; }
3344 		;
3345 
3346 OptTypedTableElementList:
3347 			'(' TypedTableElementList ')'		{ $$ = $2; }
3348 			| /*EMPTY*/							{ $$ = NIL; }
3349 		;
3350 
3351 TableElementList:
3352 			TableElement
3353 				{
3354 					$$ = list_make1($1);
3355 				}
3356 			| TableElementList ',' TableElement
3357 				{
3358 					$$ = lappend($1, $3);
3359 				}
3360 		;
3361 
3362 TypedTableElementList:
3363 			TypedTableElement
3364 				{
3365 					$$ = list_make1($1);
3366 				}
3367 			| TypedTableElementList ',' TypedTableElement
3368 				{
3369 					$$ = lappend($1, $3);
3370 				}
3371 		;
3372 
3373 TableElement:
3374 			columnDef							{ $$ = $1; }
3375 			| TableLikeClause					{ $$ = $1; }
3376 			| TableConstraint					{ $$ = $1; }
3377 		;
3378 
3379 TypedTableElement:
3380 			columnOptions						{ $$ = $1; }
3381 			| TableConstraint					{ $$ = $1; }
3382 		;
3383 
3384 columnDef:	ColId Typename create_generic_options ColQualList
3385 				{
3386 					ColumnDef *n = makeNode(ColumnDef);
3387 					n->colname = $1;
3388 					n->typeName = $2;
3389 					n->inhcount = 0;
3390 					n->is_local = true;
3391 					n->is_not_null = false;
3392 					n->is_from_type = false;
3393 					n->storage = 0;
3394 					n->raw_default = NULL;
3395 					n->cooked_default = NULL;
3396 					n->collOid = InvalidOid;
3397 					n->fdwoptions = $3;
3398 					SplitColQualList($4, &n->constraints, &n->collClause,
3399 									 yyscanner);
3400 					n->location = @1;
3401 					$$ = (Node *)n;
3402 				}
3403 		;
3404 
3405 columnOptions:	ColId ColQualList
3406 				{
3407 					ColumnDef *n = makeNode(ColumnDef);
3408 					n->colname = $1;
3409 					n->typeName = NULL;
3410 					n->inhcount = 0;
3411 					n->is_local = true;
3412 					n->is_not_null = false;
3413 					n->is_from_type = false;
3414 					n->storage = 0;
3415 					n->raw_default = NULL;
3416 					n->cooked_default = NULL;
3417 					n->collOid = InvalidOid;
3418 					SplitColQualList($2, &n->constraints, &n->collClause,
3419 									 yyscanner);
3420 					n->location = @1;
3421 					$$ = (Node *)n;
3422 				}
3423 				| ColId WITH OPTIONS ColQualList
3424 				{
3425 					ColumnDef *n = makeNode(ColumnDef);
3426 					n->colname = $1;
3427 					n->typeName = NULL;
3428 					n->inhcount = 0;
3429 					n->is_local = true;
3430 					n->is_not_null = false;
3431 					n->is_from_type = false;
3432 					n->storage = 0;
3433 					n->raw_default = NULL;
3434 					n->cooked_default = NULL;
3435 					n->collOid = InvalidOid;
3436 					SplitColQualList($4, &n->constraints, &n->collClause,
3437 									 yyscanner);
3438 					n->location = @1;
3439 					$$ = (Node *)n;
3440 				}
3441 		;
3442 
3443 ColQualList:
3444 			ColQualList ColConstraint				{ $$ = lappend($1, $2); }
3445 			| /*EMPTY*/								{ $$ = NIL; }
3446 		;
3447 
3448 ColConstraint:
3449 			CONSTRAINT name ColConstraintElem
3450 				{
3451 					Constraint *n = castNode(Constraint, $3);
3452 					n->conname = $2;
3453 					n->location = @1;
3454 					$$ = (Node *) n;
3455 				}
3456 			| ColConstraintElem						{ $$ = $1; }
3457 			| ConstraintAttr						{ $$ = $1; }
3458 			| COLLATE any_name
3459 				{
3460 					/*
3461 					 * Note: the CollateClause is momentarily included in
3462 					 * the list built by ColQualList, but we split it out
3463 					 * again in SplitColQualList.
3464 					 */
3465 					CollateClause *n = makeNode(CollateClause);
3466 					n->arg = NULL;
3467 					n->collname = $2;
3468 					n->location = @1;
3469 					$$ = (Node *) n;
3470 				}
3471 		;
3472 
3473 /* DEFAULT NULL is already the default for Postgres.
3474  * But define it here and carry it forward into the system
3475  * to make it explicit.
3476  * - thomas 1998-09-13
3477  *
3478  * WITH NULL and NULL are not SQL-standard syntax elements,
3479  * so leave them out. Use DEFAULT NULL to explicitly indicate
3480  * that a column may have that value. WITH NULL leads to
3481  * shift/reduce conflicts with WITH TIME ZONE anyway.
3482  * - thomas 1999-01-08
3483  *
3484  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
3485  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
3486  * or be part of a_expr NOT LIKE or similar constructs).
3487  */
3488 ColConstraintElem:
3489 			NOT NULL_P
3490 				{
3491 					Constraint *n = makeNode(Constraint);
3492 					n->contype = CONSTR_NOTNULL;
3493 					n->location = @1;
3494 					$$ = (Node *)n;
3495 				}
3496 			| NULL_P
3497 				{
3498 					Constraint *n = makeNode(Constraint);
3499 					n->contype = CONSTR_NULL;
3500 					n->location = @1;
3501 					$$ = (Node *)n;
3502 				}
3503 			| UNIQUE opt_definition OptConsTableSpace
3504 				{
3505 					Constraint *n = makeNode(Constraint);
3506 					n->contype = CONSTR_UNIQUE;
3507 					n->location = @1;
3508 					n->keys = NULL;
3509 					n->options = $2;
3510 					n->indexname = NULL;
3511 					n->indexspace = $3;
3512 					$$ = (Node *)n;
3513 				}
3514 			| PRIMARY KEY opt_definition OptConsTableSpace
3515 				{
3516 					Constraint *n = makeNode(Constraint);
3517 					n->contype = CONSTR_PRIMARY;
3518 					n->location = @1;
3519 					n->keys = NULL;
3520 					n->options = $3;
3521 					n->indexname = NULL;
3522 					n->indexspace = $4;
3523 					$$ = (Node *)n;
3524 				}
3525 			| CHECK '(' a_expr ')' opt_no_inherit
3526 				{
3527 					Constraint *n = makeNode(Constraint);
3528 					n->contype = CONSTR_CHECK;
3529 					n->location = @1;
3530 					n->is_no_inherit = $5;
3531 					n->raw_expr = $3;
3532 					n->cooked_expr = NULL;
3533 					n->skip_validation = false;
3534 					n->initially_valid = true;
3535 					$$ = (Node *)n;
3536 				}
3537 			| DEFAULT b_expr
3538 				{
3539 					Constraint *n = makeNode(Constraint);
3540 					n->contype = CONSTR_DEFAULT;
3541 					n->location = @1;
3542 					n->raw_expr = $2;
3543 					n->cooked_expr = NULL;
3544 					$$ = (Node *)n;
3545 				}
3546 			| GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
3547 				{
3548 					Constraint *n = makeNode(Constraint);
3549 					n->contype = CONSTR_IDENTITY;
3550 					n->generated_when = $2;
3551 					n->options = $5;
3552 					n->location = @1;
3553 					$$ = (Node *)n;
3554 				}
3555 			| REFERENCES qualified_name opt_column_list key_match key_actions
3556 				{
3557 					Constraint *n = makeNode(Constraint);
3558 					n->contype = CONSTR_FOREIGN;
3559 					n->location = @1;
3560 					n->pktable			= $2;
3561 					n->fk_attrs			= NIL;
3562 					n->pk_attrs			= $3;
3563 					n->fk_matchtype		= $4;
3564 					n->fk_upd_action	= (char) ($5 >> 8);
3565 					n->fk_del_action	= (char) ($5 & 0xFF);
3566 					n->skip_validation  = false;
3567 					n->initially_valid  = true;
3568 					$$ = (Node *)n;
3569 				}
3570 		;
3571 
3572 generated_when:
3573 			ALWAYS			{ $$ = ATTRIBUTE_IDENTITY_ALWAYS; }
3574 			| BY DEFAULT	{ $$ = ATTRIBUTE_IDENTITY_BY_DEFAULT; }
3575 		;
3576 
3577 /*
3578  * ConstraintAttr represents constraint attributes, which we parse as if
3579  * they were independent constraint clauses, in order to avoid shift/reduce
3580  * conflicts (since NOT might start either an independent NOT NULL clause
3581  * or an attribute).  parse_utilcmd.c is responsible for attaching the
3582  * attribute information to the preceding "real" constraint node, and for
3583  * complaining if attribute clauses appear in the wrong place or wrong
3584  * combinations.
3585  *
3586  * See also ConstraintAttributeSpec, which can be used in places where
3587  * there is no parsing conflict.  (Note: currently, NOT VALID and NO INHERIT
3588  * are allowed clauses in ConstraintAttributeSpec, but not here.  Someday we
3589  * might need to allow them here too, but for the moment it doesn't seem
3590  * useful in the statements that use ConstraintAttr.)
3591  */
3592 ConstraintAttr:
3593 			DEFERRABLE
3594 				{
3595 					Constraint *n = makeNode(Constraint);
3596 					n->contype = CONSTR_ATTR_DEFERRABLE;
3597 					n->location = @1;
3598 					$$ = (Node *)n;
3599 				}
3600 			| NOT DEFERRABLE
3601 				{
3602 					Constraint *n = makeNode(Constraint);
3603 					n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
3604 					n->location = @1;
3605 					$$ = (Node *)n;
3606 				}
3607 			| INITIALLY DEFERRED
3608 				{
3609 					Constraint *n = makeNode(Constraint);
3610 					n->contype = CONSTR_ATTR_DEFERRED;
3611 					n->location = @1;
3612 					$$ = (Node *)n;
3613 				}
3614 			| INITIALLY IMMEDIATE
3615 				{
3616 					Constraint *n = makeNode(Constraint);
3617 					n->contype = CONSTR_ATTR_IMMEDIATE;
3618 					n->location = @1;
3619 					$$ = (Node *)n;
3620 				}
3621 		;
3622 
3623 
3624 TableLikeClause:
3625 			LIKE qualified_name TableLikeOptionList
3626 				{
3627 					TableLikeClause *n = makeNode(TableLikeClause);
3628 					n->relation = $2;
3629 					n->options = $3;
3630 					n->relationOid = InvalidOid;
3631 					$$ = (Node *)n;
3632 				}
3633 		;
3634 
3635 TableLikeOptionList:
3636 				TableLikeOptionList INCLUDING TableLikeOption	{ $$ = $1 | $3; }
3637 				| TableLikeOptionList EXCLUDING TableLikeOption	{ $$ = $1 & ~$3; }
3638 				| /* EMPTY */						{ $$ = 0; }
3639 		;
3640 
3641 TableLikeOption:
3642 				COMMENTS			{ $$ = CREATE_TABLE_LIKE_COMMENTS; }
3643 				| CONSTRAINTS		{ $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
3644 				| DEFAULTS			{ $$ = CREATE_TABLE_LIKE_DEFAULTS; }
3645 				| IDENTITY_P		{ $$ = CREATE_TABLE_LIKE_IDENTITY; }
3646 				| INDEXES			{ $$ = CREATE_TABLE_LIKE_INDEXES; }
3647 				| STATISTICS		{ $$ = CREATE_TABLE_LIKE_STATISTICS; }
3648 				| STORAGE			{ $$ = CREATE_TABLE_LIKE_STORAGE; }
3649 				| ALL				{ $$ = CREATE_TABLE_LIKE_ALL; }
3650 		;
3651 
3652 
3653 /* ConstraintElem specifies constraint syntax which is not embedded into
3654  *	a column definition. ColConstraintElem specifies the embedded form.
3655  * - thomas 1997-12-03
3656  */
3657 TableConstraint:
3658 			CONSTRAINT name ConstraintElem
3659 				{
3660 					Constraint *n = castNode(Constraint, $3);
3661 					n->conname = $2;
3662 					n->location = @1;
3663 					$$ = (Node *) n;
3664 				}
3665 			| ConstraintElem						{ $$ = $1; }
3666 		;
3667 
3668 ConstraintElem:
3669 			CHECK '(' a_expr ')' ConstraintAttributeSpec
3670 				{
3671 					Constraint *n = makeNode(Constraint);
3672 					n->contype = CONSTR_CHECK;
3673 					n->location = @1;
3674 					n->raw_expr = $3;
3675 					n->cooked_expr = NULL;
3676 					processCASbits($5, @5, "CHECK",
3677 								   NULL, NULL, &n->skip_validation,
3678 								   &n->is_no_inherit, yyscanner);
3679 					n->initially_valid = !n->skip_validation;
3680 					$$ = (Node *)n;
3681 				}
3682 			| UNIQUE '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3683 				ConstraintAttributeSpec
3684 				{
3685 					Constraint *n = makeNode(Constraint);
3686 					n->contype = CONSTR_UNIQUE;
3687 					n->location = @1;
3688 					n->keys = $3;
3689 					n->including = $5;
3690 					n->options = $6;
3691 					n->indexname = NULL;
3692 					n->indexspace = $7;
3693 					processCASbits($8, @8, "UNIQUE",
3694 								   &n->deferrable, &n->initdeferred, NULL,
3695 								   NULL, yyscanner);
3696 					$$ = (Node *)n;
3697 				}
3698 			| UNIQUE ExistingIndex ConstraintAttributeSpec
3699 				{
3700 					Constraint *n = makeNode(Constraint);
3701 					n->contype = CONSTR_UNIQUE;
3702 					n->location = @1;
3703 					n->keys = NIL;
3704 					n->including = NIL;
3705 					n->options = NIL;
3706 					n->indexname = $2;
3707 					n->indexspace = NULL;
3708 					processCASbits($3, @3, "UNIQUE",
3709 								   &n->deferrable, &n->initdeferred, NULL,
3710 								   NULL, yyscanner);
3711 					$$ = (Node *)n;
3712 				}
3713 			| PRIMARY KEY '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3714 				ConstraintAttributeSpec
3715 				{
3716 					Constraint *n = makeNode(Constraint);
3717 					n->contype = CONSTR_PRIMARY;
3718 					n->location = @1;
3719 					n->keys = $4;
3720 					n->including = $6;
3721 					n->options = $7;
3722 					n->indexname = NULL;
3723 					n->indexspace = $8;
3724 					processCASbits($9, @9, "PRIMARY KEY",
3725 								   &n->deferrable, &n->initdeferred, NULL,
3726 								   NULL, yyscanner);
3727 					$$ = (Node *)n;
3728 				}
3729 			| PRIMARY KEY ExistingIndex ConstraintAttributeSpec
3730 				{
3731 					Constraint *n = makeNode(Constraint);
3732 					n->contype = CONSTR_PRIMARY;
3733 					n->location = @1;
3734 					n->keys = NIL;
3735 					n->including = NIL;
3736 					n->options = NIL;
3737 					n->indexname = $3;
3738 					n->indexspace = NULL;
3739 					processCASbits($4, @4, "PRIMARY KEY",
3740 								   &n->deferrable, &n->initdeferred, NULL,
3741 								   NULL, yyscanner);
3742 					$$ = (Node *)n;
3743 				}
3744 			| EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
3745 				opt_c_include opt_definition OptConsTableSpace  ExclusionWhereClause
3746 				ConstraintAttributeSpec
3747 				{
3748 					Constraint *n = makeNode(Constraint);
3749 					n->contype = CONSTR_EXCLUSION;
3750 					n->location = @1;
3751 					n->access_method	= $2;
3752 					n->exclusions		= $4;
3753 					n->including		= $6;
3754 					n->options			= $7;
3755 					n->indexname		= NULL;
3756 					n->indexspace		= $8;
3757 					n->where_clause		= $9;
3758 					processCASbits($10, @10, "EXCLUDE",
3759 								   &n->deferrable, &n->initdeferred, NULL,
3760 								   NULL, yyscanner);
3761 					$$ = (Node *)n;
3762 				}
3763 			| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
3764 				opt_column_list key_match key_actions ConstraintAttributeSpec
3765 				{
3766 					Constraint *n = makeNode(Constraint);
3767 					n->contype = CONSTR_FOREIGN;
3768 					n->location = @1;
3769 					n->pktable			= $7;
3770 					n->fk_attrs			= $4;
3771 					n->pk_attrs			= $8;
3772 					n->fk_matchtype		= $9;
3773 					n->fk_upd_action	= (char) ($10 >> 8);
3774 					n->fk_del_action	= (char) ($10 & 0xFF);
3775 					processCASbits($11, @11, "FOREIGN KEY",
3776 								   &n->deferrable, &n->initdeferred,
3777 								   &n->skip_validation, NULL,
3778 								   yyscanner);
3779 					n->initially_valid = !n->skip_validation;
3780 					$$ = (Node *)n;
3781 				}
3782 		;
3783 
3784 opt_no_inherit:	NO INHERIT							{  $$ = true; }
3785 			| /* EMPTY */							{  $$ = false; }
3786 		;
3787 
3788 opt_column_list:
3789 			'(' columnList ')'						{ $$ = $2; }
3790 			| /*EMPTY*/								{ $$ = NIL; }
3791 		;
3792 
3793 columnList:
3794 			columnElem								{ $$ = list_make1($1); }
3795 			| columnList ',' columnElem				{ $$ = lappend($1, $3); }
3796 		;
3797 
3798 columnElem: ColId
3799 				{
3800 					$$ = (Node *) makeString($1);
3801 				}
3802 		;
3803 
3804 opt_c_include:	INCLUDE '(' columnList ')'			{ $$ = $3; }
3805 			 |		/* EMPTY */						{ $$ = NIL; }
3806 		;
3807 
3808 key_match:  MATCH FULL
3809 			{
3810 				$$ = FKCONSTR_MATCH_FULL;
3811 			}
3812 		| MATCH PARTIAL
3813 			{
3814 				ereport(ERROR,
3815 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3816 						 errmsg("MATCH PARTIAL not yet implemented"),
3817 						 parser_errposition(@1)));
3818 				$$ = FKCONSTR_MATCH_PARTIAL;
3819 			}
3820 		| MATCH SIMPLE
3821 			{
3822 				$$ = FKCONSTR_MATCH_SIMPLE;
3823 			}
3824 		| /*EMPTY*/
3825 			{
3826 				$$ = FKCONSTR_MATCH_SIMPLE;
3827 			}
3828 		;
3829 
3830 ExclusionConstraintList:
3831 			ExclusionConstraintElem					{ $$ = list_make1($1); }
3832 			| ExclusionConstraintList ',' ExclusionConstraintElem
3833 													{ $$ = lappend($1, $3); }
3834 		;
3835 
3836 ExclusionConstraintElem: index_elem WITH any_operator
3837 			{
3838 				$$ = list_make2($1, $3);
3839 			}
3840 			/* allow OPERATOR() decoration for the benefit of ruleutils.c */
3841 			| index_elem WITH OPERATOR '(' any_operator ')'
3842 			{
3843 				$$ = list_make2($1, $5);
3844 			}
3845 		;
3846 
3847 ExclusionWhereClause:
3848 			WHERE '(' a_expr ')'					{ $$ = $3; }
3849 			| /*EMPTY*/								{ $$ = NULL; }
3850 		;
3851 
3852 /*
3853  * We combine the update and delete actions into one value temporarily
3854  * for simplicity of parsing, and then break them down again in the
3855  * calling production.  update is in the left 8 bits, delete in the right.
3856  * Note that NOACTION is the default.
3857  */
3858 key_actions:
3859 			key_update
3860 				{ $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3861 			| key_delete
3862 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
3863 			| key_update key_delete
3864 				{ $$ = ($1 << 8) | ($2 & 0xFF); }
3865 			| key_delete key_update
3866 				{ $$ = ($2 << 8) | ($1 & 0xFF); }
3867 			| /*EMPTY*/
3868 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3869 		;
3870 
3871 key_update: ON UPDATE key_action		{ $$ = $3; }
3872 		;
3873 
3874 key_delete: ON DELETE_P key_action		{ $$ = $3; }
3875 		;
3876 
3877 key_action:
3878 			NO ACTION					{ $$ = FKCONSTR_ACTION_NOACTION; }
3879 			| RESTRICT					{ $$ = FKCONSTR_ACTION_RESTRICT; }
3880 			| CASCADE					{ $$ = FKCONSTR_ACTION_CASCADE; }
3881 			| SET NULL_P				{ $$ = FKCONSTR_ACTION_SETNULL; }
3882 			| SET DEFAULT				{ $$ = FKCONSTR_ACTION_SETDEFAULT; }
3883 		;
3884 
3885 OptInherit: INHERITS '(' qualified_name_list ')'	{ $$ = $3; }
3886 			| /*EMPTY*/								{ $$ = NIL; }
3887 		;
3888 
3889 /* Optional partition key specification */
3890 OptPartitionSpec: PartitionSpec	{ $$ = $1; }
3891 			| /*EMPTY*/			{ $$ = NULL; }
3892 		;
3893 
3894 PartitionSpec: PARTITION BY part_strategy '(' part_params ')'
3895 				{
3896 					PartitionSpec *n = makeNode(PartitionSpec);
3897 
3898 					n->strategy = $3;
3899 					n->partParams = $5;
3900 					n->location = @1;
3901 
3902 					$$ = n;
3903 				}
3904 		;
3905 
3906 part_strategy:	IDENT					{ $$ = $1; }
3907 				| unreserved_keyword	{ $$ = pstrdup($1); }
3908 		;
3909 
3910 part_params:	part_elem						{ $$ = list_make1($1); }
3911 			| part_params ',' part_elem			{ $$ = lappend($1, $3); }
3912 		;
3913 
3914 part_elem: ColId opt_collate opt_class
3915 				{
3916 					PartitionElem *n = makeNode(PartitionElem);
3917 
3918 					n->name = $1;
3919 					n->expr = NULL;
3920 					n->collation = $2;
3921 					n->opclass = $3;
3922 					n->location = @1;
3923 					$$ = n;
3924 				}
3925 			| func_expr_windowless opt_collate opt_class
3926 				{
3927 					PartitionElem *n = makeNode(PartitionElem);
3928 
3929 					n->name = NULL;
3930 					n->expr = $1;
3931 					n->collation = $2;
3932 					n->opclass = $3;
3933 					n->location = @1;
3934 					$$ = n;
3935 				}
3936 			| '(' a_expr ')' opt_collate opt_class
3937 				{
3938 					PartitionElem *n = makeNode(PartitionElem);
3939 
3940 					n->name = NULL;
3941 					n->expr = $2;
3942 					n->collation = $4;
3943 					n->opclass = $5;
3944 					n->location = @1;
3945 					$$ = n;
3946 				}
3947 		;
3948 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
3949 OptWith:
3950 			WITH reloptions				{ $$ = $2; }
3951 			| WITH OIDS					{ $$ = list_make1(makeDefElem("oids", (Node *) makeInteger(true), @1)); }
3952 			| WITHOUT OIDS				{ $$ = list_make1(makeDefElem("oids", (Node *) makeInteger(false), @1)); }
3953 			| /*EMPTY*/					{ $$ = NIL; }
3954 		;
3955 
3956 OnCommitOption:  ON COMMIT DROP				{ $$ = ONCOMMIT_DROP; }
3957 			| ON COMMIT DELETE_P ROWS		{ $$ = ONCOMMIT_DELETE_ROWS; }
3958 			| ON COMMIT PRESERVE ROWS		{ $$ = ONCOMMIT_PRESERVE_ROWS; }
3959 			| /*EMPTY*/						{ $$ = ONCOMMIT_NOOP; }
3960 		;
3961 
3962 OptTableSpace:   TABLESPACE name					{ $$ = $2; }
3963 			| /*EMPTY*/								{ $$ = NULL; }
3964 		;
3965 
3966 OptConsTableSpace:   USING INDEX TABLESPACE name	{ $$ = $4; }
3967 			| /*EMPTY*/								{ $$ = NULL; }
3968 		;
3969 
3970 ExistingIndex:   USING INDEX index_name				{ $$ = $3; }
3971 		;
3972 
3973 /*****************************************************************************
3974  *
3975  *		QUERY :
3976  *				CREATE STATISTICS [IF NOT EXISTS] stats_name [(stat types)]
3977  *					ON expression-list FROM from_list
3978  *
3979  * Note: the expectation here is that the clauses after ON are a subset of
3980  * SELECT syntax, allowing for expressions and joined tables, and probably
3981  * someday a WHERE clause.  Much less than that is currently implemented,
3982  * but the grammar accepts it and then we'll throw FEATURE_NOT_SUPPORTED
3983  * errors as necessary at execution.
3984  *
3985  *****************************************************************************/
3986 
3987 CreateStatsStmt:
3988 			CREATE STATISTICS any_name
3989 			opt_name_list ON expr_list FROM from_list
3990 				{
3991 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
3992 					n->defnames = $3;
3993 					n->stat_types = $4;
3994 					n->exprs = $6;
3995 					n->relations = $8;
3996 					n->stxcomment = NULL;
3997 					n->if_not_exists = false;
3998 					$$ = (Node *)n;
3999 				}
4000 			| CREATE STATISTICS IF_P NOT EXISTS any_name
4001 			opt_name_list ON expr_list FROM from_list
4002 				{
4003 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
4004 					n->defnames = $6;
4005 					n->stat_types = $7;
4006 					n->exprs = $9;
4007 					n->relations = $11;
4008 					n->stxcomment = NULL;
4009 					n->if_not_exists = true;
4010 					$$ = (Node *)n;
4011 				}
4012 			;
4013 
4014 /*****************************************************************************
4015  *
4016  *		QUERY :
4017  *				CREATE TABLE relname AS SelectStmt [ WITH [NO] DATA ]
4018  *
4019  *
4020  * Note: SELECT ... INTO is a now-deprecated alternative for this.
4021  *
4022  *****************************************************************************/
4023 
4024 CreateAsStmt:
4025 		CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
4026 				{
4027 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4028 					ctas->query = $6;
4029 					ctas->into = $4;
4030 					ctas->relkind = OBJECT_TABLE;
4031 					ctas->is_select_into = false;
4032 					ctas->if_not_exists = false;
4033 					/* cram additional flags into the IntoClause */
4034 					$4->rel->relpersistence = $2;
4035 					$4->skipData = !($7);
4036 					$$ = (Node *) ctas;
4037 				}
4038 		| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS SelectStmt opt_with_data
4039 				{
4040 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4041 					ctas->query = $9;
4042 					ctas->into = $7;
4043 					ctas->relkind = OBJECT_TABLE;
4044 					ctas->is_select_into = false;
4045 					ctas->if_not_exists = true;
4046 					/* cram additional flags into the IntoClause */
4047 					$7->rel->relpersistence = $2;
4048 					$7->skipData = !($10);
4049 					$$ = (Node *) ctas;
4050 				}
4051 		;
4052 
4053 create_as_target:
4054 			qualified_name opt_column_list OptWith OnCommitOption OptTableSpace
4055 				{
4056 					$$ = makeNode(IntoClause);
4057 					$$->rel = $1;
4058 					$$->colNames = $2;
4059 					$$->options = $3;
4060 					$$->onCommit = $4;
4061 					$$->tableSpaceName = $5;
4062 					$$->viewQuery = NULL;
4063 					$$->skipData = false;		/* might get changed later */
4064 				}
4065 		;
4066 
4067 opt_with_data:
4068 			WITH DATA_P								{ $$ = true; }
4069 			| WITH NO DATA_P						{ $$ = false; }
4070 			| /*EMPTY*/								{ $$ = true; }
4071 		;
4072 
4073 
4074 /*****************************************************************************
4075  *
4076  *		QUERY :
4077  *				CREATE MATERIALIZED VIEW relname AS SelectStmt
4078  *
4079  *****************************************************************************/
4080 
4081 CreateMatViewStmt:
4082 		CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data
4083 				{
4084 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4085 					ctas->query = $7;
4086 					ctas->into = $5;
4087 					ctas->relkind = OBJECT_MATVIEW;
4088 					ctas->is_select_into = false;
4089 					ctas->if_not_exists = false;
4090 					/* cram additional flags into the IntoClause */
4091 					$5->rel->relpersistence = $2;
4092 					$5->skipData = !($8);
4093 					$$ = (Node *) ctas;
4094 				}
4095 		| CREATE OptNoLog MATERIALIZED VIEW IF_P NOT EXISTS create_mv_target AS SelectStmt opt_with_data
4096 				{
4097 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4098 					ctas->query = $10;
4099 					ctas->into = $8;
4100 					ctas->relkind = OBJECT_MATVIEW;
4101 					ctas->is_select_into = false;
4102 					ctas->if_not_exists = true;
4103 					/* cram additional flags into the IntoClause */
4104 					$8->rel->relpersistence = $2;
4105 					$8->skipData = !($11);
4106 					$$ = (Node *) ctas;
4107 				}
4108 		;
4109 
4110 create_mv_target:
4111 			qualified_name opt_column_list opt_reloptions OptTableSpace
4112 				{
4113 					$$ = makeNode(IntoClause);
4114 					$$->rel = $1;
4115 					$$->colNames = $2;
4116 					$$->options = $3;
4117 					$$->onCommit = ONCOMMIT_NOOP;
4118 					$$->tableSpaceName = $4;
4119 					$$->viewQuery = NULL;		/* filled at analysis time */
4120 					$$->skipData = false;		/* might get changed later */
4121 				}
4122 		;
4123 
4124 OptNoLog:	UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
4125 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
4126 		;
4127 
4128 
4129 /*****************************************************************************
4130  *
4131  *		QUERY :
4132  *				REFRESH MATERIALIZED VIEW qualified_name
4133  *
4134  *****************************************************************************/
4135 
4136 RefreshMatViewStmt:
4137 			REFRESH MATERIALIZED VIEW opt_concurrently qualified_name opt_with_data
4138 				{
4139 					RefreshMatViewStmt *n = makeNode(RefreshMatViewStmt);
4140 					n->concurrent = $4;
4141 					n->relation = $5;
4142 					n->skipData = !($6);
4143 					$$ = (Node *) n;
4144 				}
4145 		;
4146 
4147 
4148 /*****************************************************************************
4149  *
4150  *		QUERY :
4151  *				CREATE SEQUENCE seqname
4152  *				ALTER SEQUENCE seqname
4153  *
4154  *****************************************************************************/
4155 
4156 CreateSeqStmt:
4157 			CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
4158 				{
4159 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4160 					$4->relpersistence = $2;
4161 					n->sequence = $4;
4162 					n->options = $5;
4163 					n->ownerId = InvalidOid;
4164 					n->if_not_exists = false;
4165 					$$ = (Node *)n;
4166 				}
4167 			| CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
4168 				{
4169 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4170 					$7->relpersistence = $2;
4171 					n->sequence = $7;
4172 					n->options = $8;
4173 					n->ownerId = InvalidOid;
4174 					n->if_not_exists = true;
4175 					$$ = (Node *)n;
4176 				}
4177 		;
4178 
4179 AlterSeqStmt:
4180 			ALTER SEQUENCE qualified_name SeqOptList
4181 				{
4182 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4183 					n->sequence = $3;
4184 					n->options = $4;
4185 					n->missing_ok = false;
4186 					$$ = (Node *)n;
4187 				}
4188 			| ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
4189 				{
4190 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4191 					n->sequence = $5;
4192 					n->options = $6;
4193 					n->missing_ok = true;
4194 					$$ = (Node *)n;
4195 				}
4196 
4197 		;
4198 
4199 OptSeqOptList: SeqOptList							{ $$ = $1; }
4200 			| /*EMPTY*/								{ $$ = NIL; }
4201 		;
4202 
4203 OptParenthesizedSeqOptList: '(' SeqOptList ')'		{ $$ = $2; }
4204 			| /*EMPTY*/								{ $$ = NIL; }
4205 		;
4206 
4207 SeqOptList: SeqOptElem								{ $$ = list_make1($1); }
4208 			| SeqOptList SeqOptElem					{ $$ = lappend($1, $2); }
4209 		;
4210 
4211 SeqOptElem: AS SimpleTypename
4212 				{
4213 					$$ = makeDefElem("as", (Node *)$2, @1);
4214 				}
4215 			| CACHE NumericOnly
4216 				{
4217 					$$ = makeDefElem("cache", (Node *)$2, @1);
4218 				}
4219 			| CYCLE
4220 				{
4221 					$$ = makeDefElem("cycle", (Node *)makeInteger(true), @1);
4222 				}
4223 			| NO CYCLE
4224 				{
4225 					$$ = makeDefElem("cycle", (Node *)makeInteger(false), @1);
4226 				}
4227 			| INCREMENT opt_by NumericOnly
4228 				{
4229 					$$ = makeDefElem("increment", (Node *)$3, @1);
4230 				}
4231 			| MAXVALUE NumericOnly
4232 				{
4233 					$$ = makeDefElem("maxvalue", (Node *)$2, @1);
4234 				}
4235 			| MINVALUE NumericOnly
4236 				{
4237 					$$ = makeDefElem("minvalue", (Node *)$2, @1);
4238 				}
4239 			| NO MAXVALUE
4240 				{
4241 					$$ = makeDefElem("maxvalue", NULL, @1);
4242 				}
4243 			| NO MINVALUE
4244 				{
4245 					$$ = makeDefElem("minvalue", NULL, @1);
4246 				}
4247 			| OWNED BY any_name
4248 				{
4249 					$$ = makeDefElem("owned_by", (Node *)$3, @1);
4250 				}
4251 			| SEQUENCE NAME_P any_name
4252 				{
4253 					/* not documented, only used by pg_dump */
4254 					$$ = makeDefElem("sequence_name", (Node *)$3, @1);
4255 				}
4256 			| START opt_with NumericOnly
4257 				{
4258 					$$ = makeDefElem("start", (Node *)$3, @1);
4259 				}
4260 			| RESTART
4261 				{
4262 					$$ = makeDefElem("restart", NULL, @1);
4263 				}
4264 			| RESTART opt_with NumericOnly
4265 				{
4266 					$$ = makeDefElem("restart", (Node *)$3, @1);
4267 				}
4268 		;
4269 
4270 opt_by:		BY				{}
4271 			| /* empty */	{}
4272 	  ;
4273 
4274 NumericOnly:
4275 			FCONST								{ $$ = makeFloat($1); }
4276 			| '+' FCONST						{ $$ = makeFloat($2); }
4277 			| '-' FCONST
4278 				{
4279 					$$ = makeFloat($2);
4280 					doNegateFloat($$);
4281 				}
4282 			| SignedIconst						{ $$ = makeInteger($1); }
4283 		;
4284 
4285 NumericOnly_list:	NumericOnly						{ $$ = list_make1($1); }
4286 				| NumericOnly_list ',' NumericOnly	{ $$ = lappend($1, $3); }
4287 		;
4288 
4289 /*****************************************************************************
4290  *
4291  *		QUERIES :
4292  *				CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
4293  *				DROP [PROCEDURAL] LANGUAGE ...
4294  *
4295  *****************************************************************************/
4296 
4297 CreatePLangStmt:
4298 			CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4299 			{
4300 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4301 				n->replace = $2;
4302 				n->plname = $6;
4303 				/* parameters are all to be supplied by system */
4304 				n->plhandler = NIL;
4305 				n->plinline = NIL;
4306 				n->plvalidator = NIL;
4307 				n->pltrusted = false;
4308 				$$ = (Node *)n;
4309 			}
4310 			| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4311 			  HANDLER handler_name opt_inline_handler opt_validator
4312 			{
4313 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4314 				n->replace = $2;
4315 				n->plname = $6;
4316 				n->plhandler = $8;
4317 				n->plinline = $9;
4318 				n->plvalidator = $10;
4319 				n->pltrusted = $3;
4320 				$$ = (Node *)n;
4321 			}
4322 		;
4323 
4324 opt_trusted:
4325 			TRUSTED									{ $$ = true; }
4326 			| /*EMPTY*/								{ $$ = false; }
4327 		;
4328 
4329 /* This ought to be just func_name, but that causes reduce/reduce conflicts
4330  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
4331  * Work around by using simple names, instead.
4332  */
4333 handler_name:
4334 			name						{ $$ = list_make1(makeString($1)); }
4335 			| name attrs				{ $$ = lcons(makeString($1), $2); }
4336 		;
4337 
4338 opt_inline_handler:
4339 			INLINE_P handler_name					{ $$ = $2; }
4340 			| /*EMPTY*/								{ $$ = NIL; }
4341 		;
4342 
4343 validator_clause:
4344 			VALIDATOR handler_name					{ $$ = $2; }
4345 			| NO VALIDATOR							{ $$ = NIL; }
4346 		;
4347 
4348 opt_validator:
4349 			validator_clause						{ $$ = $1; }
4350 			| /*EMPTY*/								{ $$ = NIL; }
4351 		;
4352 
4353 DropPLangStmt:
4354 			DROP opt_procedural LANGUAGE NonReservedWord_or_Sconst opt_drop_behavior
4355 				{
4356 					DropStmt *n = makeNode(DropStmt);
4357 					n->removeType = OBJECT_LANGUAGE;
4358 					n->objects = list_make1(makeString($4));
4359 					n->behavior = $5;
4360 					n->missing_ok = false;
4361 					n->concurrent = false;
4362 					$$ = (Node *)n;
4363 				}
4364 			| DROP opt_procedural LANGUAGE IF_P EXISTS NonReservedWord_or_Sconst opt_drop_behavior
4365 				{
4366 					DropStmt *n = makeNode(DropStmt);
4367 					n->removeType = OBJECT_LANGUAGE;
4368 					n->objects = list_make1(makeString($6));
4369 					n->behavior = $7;
4370 					n->missing_ok = true;
4371 					n->concurrent = false;
4372 					$$ = (Node *)n;
4373 				}
4374 		;
4375 
4376 opt_procedural:
4377 			PROCEDURAL								{}
4378 			| /*EMPTY*/								{}
4379 		;
4380 
4381 /*****************************************************************************
4382  *
4383  *		QUERY:
4384  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
4385  *
4386  *****************************************************************************/
4387 
4388 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst opt_reloptions
4389 				{
4390 					CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
4391 					n->tablespacename = $3;
4392 					n->owner = $4;
4393 					n->location = $6;
4394 					n->options = $7;
4395 					$$ = (Node *) n;
4396 				}
4397 		;
4398 
4399 OptTableSpaceOwner: OWNER RoleSpec		{ $$ = $2; }
4400 			| /*EMPTY */				{ $$ = NULL; }
4401 		;
4402 
4403 /*****************************************************************************
4404  *
4405  *		QUERY :
4406  *				DROP TABLESPACE <tablespace>
4407  *
4408  *		No need for drop behaviour as we cannot implement dependencies for
4409  *		objects in other databases; we can only support RESTRICT.
4410  *
4411  ****************************************************************************/
4412 
4413 DropTableSpaceStmt: DROP TABLESPACE name
4414 				{
4415 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4416 					n->tablespacename = $3;
4417 					n->missing_ok = false;
4418 					$$ = (Node *) n;
4419 				}
4420 				|  DROP TABLESPACE IF_P EXISTS name
4421 				{
4422 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4423 					n->tablespacename = $5;
4424 					n->missing_ok = true;
4425 					$$ = (Node *) n;
4426 				}
4427 		;
4428 
4429 /*****************************************************************************
4430  *
4431  *		QUERY:
4432  *             CREATE EXTENSION extension
4433  *             [ WITH ] [ SCHEMA schema ] [ VERSION version ] [ FROM oldversion ]
4434  *
4435  *****************************************************************************/
4436 
4437 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
4438 				{
4439 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4440 					n->extname = $3;
4441 					n->if_not_exists = false;
4442 					n->options = $5;
4443 					$$ = (Node *) n;
4444 				}
4445 				| CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
4446 				{
4447 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4448 					n->extname = $6;
4449 					n->if_not_exists = true;
4450 					n->options = $8;
4451 					$$ = (Node *) n;
4452 				}
4453 		;
4454 
4455 create_extension_opt_list:
4456 			create_extension_opt_list create_extension_opt_item
4457 				{ $$ = lappend($1, $2); }
4458 			| /* EMPTY */
4459 				{ $$ = NIL; }
4460 		;
4461 
4462 create_extension_opt_item:
4463 			SCHEMA name
4464 				{
4465 					$$ = makeDefElem("schema", (Node *)makeString($2), @1);
4466 				}
4467 			| VERSION_P NonReservedWord_or_Sconst
4468 				{
4469 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4470 				}
4471 			| FROM NonReservedWord_or_Sconst
4472 				{
4473 					$$ = makeDefElem("old_version", (Node *)makeString($2), @1);
4474 				}
4475 			| CASCADE
4476 				{
4477 					$$ = makeDefElem("cascade", (Node *)makeInteger(true), @1);
4478 				}
4479 		;
4480 
4481 /*****************************************************************************
4482  *
4483  * ALTER EXTENSION name UPDATE [ TO version ]
4484  *
4485  *****************************************************************************/
4486 
4487 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
4488 				{
4489 					AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
4490 					n->extname = $3;
4491 					n->options = $5;
4492 					$$ = (Node *) n;
4493 				}
4494 		;
4495 
4496 alter_extension_opt_list:
4497 			alter_extension_opt_list alter_extension_opt_item
4498 				{ $$ = lappend($1, $2); }
4499 			| /* EMPTY */
4500 				{ $$ = NIL; }
4501 		;
4502 
4503 alter_extension_opt_item:
4504 			TO NonReservedWord_or_Sconst
4505 				{
4506 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4507 				}
4508 		;
4509 
4510 /*****************************************************************************
4511  *
4512  * ALTER EXTENSION name ADD/DROP object-identifier
4513  *
4514  *****************************************************************************/
4515 
4516 AlterExtensionContentsStmt:
4517 			ALTER EXTENSION name add_drop ACCESS METHOD name
4518 				{
4519 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4520 					n->extname = $3;
4521 					n->action = $4;
4522 					n->objtype = OBJECT_ACCESS_METHOD;
4523 					n->object = (Node *) makeString($7);
4524 					$$ = (Node *)n;
4525 				}
4526 			| ALTER EXTENSION name add_drop AGGREGATE aggregate_with_argtypes
4527 				{
4528 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4529 					n->extname = $3;
4530 					n->action = $4;
4531 					n->objtype = OBJECT_AGGREGATE;
4532 					n->object = (Node *) $6;
4533 					$$ = (Node *)n;
4534 				}
4535 			| ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
4536 				{
4537 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4538 					n->extname = $3;
4539 					n->action = $4;
4540 					n->objtype = OBJECT_CAST;
4541 					n->object = (Node *) list_make2($7, $9);
4542 					$$ = (Node *) n;
4543 				}
4544 			| ALTER EXTENSION name add_drop COLLATION any_name
4545 				{
4546 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4547 					n->extname = $3;
4548 					n->action = $4;
4549 					n->objtype = OBJECT_COLLATION;
4550 					n->object = (Node *) $6;
4551 					$$ = (Node *)n;
4552 				}
4553 			| ALTER EXTENSION name add_drop CONVERSION_P any_name
4554 				{
4555 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4556 					n->extname = $3;
4557 					n->action = $4;
4558 					n->objtype = OBJECT_CONVERSION;
4559 					n->object = (Node *) $6;
4560 					$$ = (Node *)n;
4561 				}
4562 			| ALTER EXTENSION name add_drop DOMAIN_P Typename
4563 				{
4564 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4565 					n->extname = $3;
4566 					n->action = $4;
4567 					n->objtype = OBJECT_DOMAIN;
4568 					n->object = (Node *) $6;
4569 					$$ = (Node *)n;
4570 				}
4571 			| ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
4572 				{
4573 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4574 					n->extname = $3;
4575 					n->action = $4;
4576 					n->objtype = OBJECT_FUNCTION;
4577 					n->object = (Node *) $6;
4578 					$$ = (Node *)n;
4579 				}
4580 			| ALTER EXTENSION name add_drop opt_procedural LANGUAGE name
4581 				{
4582 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4583 					n->extname = $3;
4584 					n->action = $4;
4585 					n->objtype = OBJECT_LANGUAGE;
4586 					n->object = (Node *) makeString($7);
4587 					$$ = (Node *)n;
4588 				}
4589 			| ALTER EXTENSION name add_drop OPERATOR operator_with_argtypes
4590 				{
4591 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4592 					n->extname = $3;
4593 					n->action = $4;
4594 					n->objtype = OBJECT_OPERATOR;
4595 					n->object = (Node *) $6;
4596 					$$ = (Node *)n;
4597 				}
4598 			| ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING access_method
4599 				{
4600 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4601 					n->extname = $3;
4602 					n->action = $4;
4603 					n->objtype = OBJECT_OPCLASS;
4604 					n->object = (Node *) lcons(makeString($9), $7);
4605 					$$ = (Node *)n;
4606 				}
4607 			| ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING access_method
4608 				{
4609 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4610 					n->extname = $3;
4611 					n->action = $4;
4612 					n->objtype = OBJECT_OPFAMILY;
4613 					n->object = (Node *) lcons(makeString($9), $7);
4614 					$$ = (Node *)n;
4615 				}
4616 			| ALTER EXTENSION name add_drop PROCEDURE function_with_argtypes
4617 				{
4618 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4619 					n->extname = $3;
4620 					n->action = $4;
4621 					n->objtype = OBJECT_PROCEDURE;
4622 					n->object = (Node *) $6;
4623 					$$ = (Node *)n;
4624 				}
4625 			| ALTER EXTENSION name add_drop ROUTINE function_with_argtypes
4626 				{
4627 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4628 					n->extname = $3;
4629 					n->action = $4;
4630 					n->objtype = OBJECT_ROUTINE;
4631 					n->object = (Node *) $6;
4632 					$$ = (Node *)n;
4633 				}
4634 			| ALTER EXTENSION name add_drop SCHEMA name
4635 				{
4636 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4637 					n->extname = $3;
4638 					n->action = $4;
4639 					n->objtype = OBJECT_SCHEMA;
4640 					n->object = (Node *) makeString($6);
4641 					$$ = (Node *)n;
4642 				}
4643 			| ALTER EXTENSION name add_drop EVENT TRIGGER name
4644 				{
4645 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4646 					n->extname = $3;
4647 					n->action = $4;
4648 					n->objtype = OBJECT_EVENT_TRIGGER;
4649 					n->object = (Node *) makeString($7);
4650 					$$ = (Node *)n;
4651 				}
4652 			| ALTER EXTENSION name add_drop TABLE any_name
4653 				{
4654 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4655 					n->extname = $3;
4656 					n->action = $4;
4657 					n->objtype = OBJECT_TABLE;
4658 					n->object = (Node *) $6;
4659 					$$ = (Node *)n;
4660 				}
4661 			| ALTER EXTENSION name add_drop TEXT_P SEARCH PARSER any_name
4662 				{
4663 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4664 					n->extname = $3;
4665 					n->action = $4;
4666 					n->objtype = OBJECT_TSPARSER;
4667 					n->object = (Node *) $8;
4668 					$$ = (Node *)n;
4669 				}
4670 			| ALTER EXTENSION name add_drop TEXT_P SEARCH DICTIONARY any_name
4671 				{
4672 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4673 					n->extname = $3;
4674 					n->action = $4;
4675 					n->objtype = OBJECT_TSDICTIONARY;
4676 					n->object = (Node *) $8;
4677 					$$ = (Node *)n;
4678 				}
4679 			| ALTER EXTENSION name add_drop TEXT_P SEARCH TEMPLATE any_name
4680 				{
4681 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4682 					n->extname = $3;
4683 					n->action = $4;
4684 					n->objtype = OBJECT_TSTEMPLATE;
4685 					n->object = (Node *) $8;
4686 					$$ = (Node *)n;
4687 				}
4688 			| ALTER EXTENSION name add_drop TEXT_P SEARCH CONFIGURATION any_name
4689 				{
4690 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4691 					n->extname = $3;
4692 					n->action = $4;
4693 					n->objtype = OBJECT_TSCONFIGURATION;
4694 					n->object = (Node *) $8;
4695 					$$ = (Node *)n;
4696 				}
4697 			| ALTER EXTENSION name add_drop SEQUENCE any_name
4698 				{
4699 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4700 					n->extname = $3;
4701 					n->action = $4;
4702 					n->objtype = OBJECT_SEQUENCE;
4703 					n->object = (Node *) $6;
4704 					$$ = (Node *)n;
4705 				}
4706 			| ALTER EXTENSION name add_drop VIEW any_name
4707 				{
4708 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4709 					n->extname = $3;
4710 					n->action = $4;
4711 					n->objtype = OBJECT_VIEW;
4712 					n->object = (Node *) $6;
4713 					$$ = (Node *)n;
4714 				}
4715 			| ALTER EXTENSION name add_drop MATERIALIZED VIEW any_name
4716 				{
4717 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4718 					n->extname = $3;
4719 					n->action = $4;
4720 					n->objtype = OBJECT_MATVIEW;
4721 					n->object = (Node *) $7;
4722 					$$ = (Node *)n;
4723 				}
4724 			| ALTER EXTENSION name add_drop FOREIGN TABLE any_name
4725 				{
4726 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4727 					n->extname = $3;
4728 					n->action = $4;
4729 					n->objtype = OBJECT_FOREIGN_TABLE;
4730 					n->object = (Node *) $7;
4731 					$$ = (Node *)n;
4732 				}
4733 			| ALTER EXTENSION name add_drop FOREIGN DATA_P WRAPPER name
4734 				{
4735 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4736 					n->extname = $3;
4737 					n->action = $4;
4738 					n->objtype = OBJECT_FDW;
4739 					n->object = (Node *) makeString($8);
4740 					$$ = (Node *)n;
4741 				}
4742 			| ALTER EXTENSION name add_drop SERVER name
4743 				{
4744 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4745 					n->extname = $3;
4746 					n->action = $4;
4747 					n->objtype = OBJECT_FOREIGN_SERVER;
4748 					n->object = (Node *) makeString($6);
4749 					$$ = (Node *)n;
4750 				}
4751 			| ALTER EXTENSION name add_drop TRANSFORM FOR Typename LANGUAGE name
4752 				{
4753 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4754 					n->extname = $3;
4755 					n->action = $4;
4756 					n->objtype = OBJECT_TRANSFORM;
4757 					n->object = (Node *) list_make2($7, makeString($9));
4758 					$$ = (Node *)n;
4759 				}
4760 			| ALTER EXTENSION name add_drop TYPE_P Typename
4761 				{
4762 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4763 					n->extname = $3;
4764 					n->action = $4;
4765 					n->objtype = OBJECT_TYPE;
4766 					n->object = (Node *) $6;
4767 					$$ = (Node *)n;
4768 				}
4769 		;
4770 
4771 /*****************************************************************************
4772  *
4773  *		QUERY:
4774  *             CREATE FOREIGN DATA WRAPPER name options
4775  *
4776  *****************************************************************************/
4777 
4778 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
4779 				{
4780 					CreateFdwStmt *n = makeNode(CreateFdwStmt);
4781 					n->fdwname = $5;
4782 					n->func_options = $6;
4783 					n->options = $7;
4784 					$$ = (Node *) n;
4785 				}
4786 		;
4787 
4788 fdw_option:
4789 			HANDLER handler_name				{ $$ = makeDefElem("handler", (Node *)$2, @1); }
4790 			| NO HANDLER						{ $$ = makeDefElem("handler", NULL, @1); }
4791 			| VALIDATOR handler_name			{ $$ = makeDefElem("validator", (Node *)$2, @1); }
4792 			| NO VALIDATOR						{ $$ = makeDefElem("validator", NULL, @1); }
4793 		;
4794 
4795 fdw_options:
4796 			fdw_option							{ $$ = list_make1($1); }
4797 			| fdw_options fdw_option			{ $$ = lappend($1, $2); }
4798 		;
4799 
4800 opt_fdw_options:
4801 			fdw_options							{ $$ = $1; }
4802 			| /*EMPTY*/							{ $$ = NIL; }
4803 		;
4804 
4805 /*****************************************************************************
4806  *
4807  *		QUERY :
4808  *				ALTER FOREIGN DATA WRAPPER name options
4809  *
4810  ****************************************************************************/
4811 
4812 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
4813 				{
4814 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4815 					n->fdwname = $5;
4816 					n->func_options = $6;
4817 					n->options = $7;
4818 					$$ = (Node *) n;
4819 				}
4820 			| ALTER FOREIGN DATA_P WRAPPER name fdw_options
4821 				{
4822 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4823 					n->fdwname = $5;
4824 					n->func_options = $6;
4825 					n->options = NIL;
4826 					$$ = (Node *) n;
4827 				}
4828 		;
4829 
4830 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
4831 create_generic_options:
4832 			OPTIONS '(' generic_option_list ')'			{ $$ = $3; }
4833 			| /*EMPTY*/									{ $$ = NIL; }
4834 		;
4835 
4836 generic_option_list:
4837 			generic_option_elem
4838 				{
4839 					$$ = list_make1($1);
4840 				}
4841 			| generic_option_list ',' generic_option_elem
4842 				{
4843 					$$ = lappend($1, $3);
4844 				}
4845 		;
4846 
4847 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
4848 alter_generic_options:
4849 			OPTIONS	'(' alter_generic_option_list ')'		{ $$ = $3; }
4850 		;
4851 
4852 alter_generic_option_list:
4853 			alter_generic_option_elem
4854 				{
4855 					$$ = list_make1($1);
4856 				}
4857 			| alter_generic_option_list ',' alter_generic_option_elem
4858 				{
4859 					$$ = lappend($1, $3);
4860 				}
4861 		;
4862 
4863 alter_generic_option_elem:
4864 			generic_option_elem
4865 				{
4866 					$$ = $1;
4867 				}
4868 			| SET generic_option_elem
4869 				{
4870 					$$ = $2;
4871 					$$->defaction = DEFELEM_SET;
4872 				}
4873 			| ADD_P generic_option_elem
4874 				{
4875 					$$ = $2;
4876 					$$->defaction = DEFELEM_ADD;
4877 				}
4878 			| DROP generic_option_name
4879 				{
4880 					$$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP, @2);
4881 				}
4882 		;
4883 
4884 generic_option_elem:
4885 			generic_option_name generic_option_arg
4886 				{
4887 					$$ = makeDefElem($1, $2, @1);
4888 				}
4889 		;
4890 
4891 generic_option_name:
4892 				ColLabel			{ $$ = $1; }
4893 		;
4894 
4895 /* We could use def_arg here, but the spec only requires string literals */
4896 generic_option_arg:
4897 				Sconst				{ $$ = (Node *) makeString($1); }
4898 		;
4899 
4900 /*****************************************************************************
4901  *
4902  *		QUERY:
4903  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
4904  *
4905  *****************************************************************************/
4906 
4907 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
4908 						 FOREIGN DATA_P WRAPPER name create_generic_options
4909 				{
4910 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4911 					n->servername = $3;
4912 					n->servertype = $4;
4913 					n->version = $5;
4914 					n->fdwname = $9;
4915 					n->options = $10;
4916 					n->if_not_exists = false;
4917 					$$ = (Node *) n;
4918 				}
4919 				| CREATE SERVER IF_P NOT EXISTS name opt_type opt_foreign_server_version
4920 						 FOREIGN DATA_P WRAPPER name create_generic_options
4921 				{
4922 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4923 					n->servername = $6;
4924 					n->servertype = $7;
4925 					n->version = $8;
4926 					n->fdwname = $12;
4927 					n->options = $13;
4928 					n->if_not_exists = true;
4929 					$$ = (Node *) n;
4930 				}
4931 		;
4932 
4933 opt_type:
4934 			TYPE_P Sconst			{ $$ = $2; }
4935 			| /*EMPTY*/				{ $$ = NULL; }
4936 		;
4937 
4938 
4939 foreign_server_version:
4940 			VERSION_P Sconst		{ $$ = $2; }
4941 		|	VERSION_P NULL_P		{ $$ = NULL; }
4942 		;
4943 
4944 opt_foreign_server_version:
4945 			foreign_server_version	{ $$ = $1; }
4946 			| /*EMPTY*/				{ $$ = NULL; }
4947 		;
4948 
4949 /*****************************************************************************
4950  *
4951  *		QUERY :
4952  *				ALTER SERVER name [VERSION] [OPTIONS]
4953  *
4954  ****************************************************************************/
4955 
4956 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
4957 				{
4958 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4959 					n->servername = $3;
4960 					n->version = $4;
4961 					n->options = $5;
4962 					n->has_version = true;
4963 					$$ = (Node *) n;
4964 				}
4965 			| ALTER SERVER name foreign_server_version
4966 				{
4967 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4968 					n->servername = $3;
4969 					n->version = $4;
4970 					n->has_version = true;
4971 					$$ = (Node *) n;
4972 				}
4973 			| ALTER SERVER name alter_generic_options
4974 				{
4975 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4976 					n->servername = $3;
4977 					n->options = $4;
4978 					$$ = (Node *) n;
4979 				}
4980 		;
4981 
4982 /*****************************************************************************
4983  *
4984  *		QUERY:
4985  *             CREATE FOREIGN TABLE relname (...) SERVER name (...)
4986  *
4987  *****************************************************************************/
4988 
4989 CreateForeignTableStmt:
4990 		CREATE FOREIGN TABLE qualified_name
4991 			'(' OptTableElementList ')'
4992 			OptInherit SERVER name create_generic_options
4993 				{
4994 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4995 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
4996 					n->base.relation = $4;
4997 					n->base.tableElts = $6;
4998 					n->base.inhRelations = $8;
4999 					n->base.ofTypename = NULL;
5000 					n->base.constraints = NIL;
5001 					n->base.options = NIL;
5002 					n->base.oncommit = ONCOMMIT_NOOP;
5003 					n->base.tablespacename = NULL;
5004 					n->base.if_not_exists = false;
5005 					/* FDW-specific data */
5006 					n->servername = $10;
5007 					n->options = $11;
5008 					$$ = (Node *) n;
5009 				}
5010 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5011 			'(' OptTableElementList ')'
5012 			OptInherit SERVER name create_generic_options
5013 				{
5014 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5015 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
5016 					n->base.relation = $7;
5017 					n->base.tableElts = $9;
5018 					n->base.inhRelations = $11;
5019 					n->base.ofTypename = NULL;
5020 					n->base.constraints = NIL;
5021 					n->base.options = NIL;
5022 					n->base.oncommit = ONCOMMIT_NOOP;
5023 					n->base.tablespacename = NULL;
5024 					n->base.if_not_exists = true;
5025 					/* FDW-specific data */
5026 					n->servername = $13;
5027 					n->options = $14;
5028 					$$ = (Node *) n;
5029 				}
5030 		| CREATE FOREIGN TABLE qualified_name
5031 			PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5032 			SERVER name create_generic_options
5033 				{
5034 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5035 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
5036 					n->base.relation = $4;
5037 					n->base.inhRelations = list_make1($7);
5038 					n->base.tableElts = $8;
5039 					n->base.partbound = $9;
5040 					n->base.ofTypename = NULL;
5041 					n->base.constraints = NIL;
5042 					n->base.options = NIL;
5043 					n->base.oncommit = ONCOMMIT_NOOP;
5044 					n->base.tablespacename = NULL;
5045 					n->base.if_not_exists = false;
5046 					/* FDW-specific data */
5047 					n->servername = $11;
5048 					n->options = $12;
5049 					$$ = (Node *) n;
5050 				}
5051 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5052 			PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5053 			SERVER name create_generic_options
5054 				{
5055 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5056 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
5057 					n->base.relation = $7;
5058 					n->base.inhRelations = list_make1($10);
5059 					n->base.tableElts = $11;
5060 					n->base.partbound = $12;
5061 					n->base.ofTypename = NULL;
5062 					n->base.constraints = NIL;
5063 					n->base.options = NIL;
5064 					n->base.oncommit = ONCOMMIT_NOOP;
5065 					n->base.tablespacename = NULL;
5066 					n->base.if_not_exists = true;
5067 					/* FDW-specific data */
5068 					n->servername = $14;
5069 					n->options = $15;
5070 					$$ = (Node *) n;
5071 				}
5072 		;
5073 
5074 /*****************************************************************************
5075  *
5076  *		QUERY:
5077  *             ALTER FOREIGN TABLE relname [...]
5078  *
5079  *****************************************************************************/
5080 
5081 AlterForeignTableStmt:
5082 			ALTER FOREIGN TABLE relation_expr alter_table_cmds
5083 				{
5084 					AlterTableStmt *n = makeNode(AlterTableStmt);
5085 					n->relation = $4;
5086 					n->cmds = $5;
5087 					n->relkind = OBJECT_FOREIGN_TABLE;
5088 					n->missing_ok = false;
5089 					$$ = (Node *)n;
5090 				}
5091 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
5092 				{
5093 					AlterTableStmt *n = makeNode(AlterTableStmt);
5094 					n->relation = $6;
5095 					n->cmds = $7;
5096 					n->relkind = OBJECT_FOREIGN_TABLE;
5097 					n->missing_ok = true;
5098 					$$ = (Node *)n;
5099 				}
5100 		;
5101 
5102 /*****************************************************************************
5103  *
5104  *		QUERY:
5105  *				IMPORT FOREIGN SCHEMA remote_schema
5106  *				[ { LIMIT TO | EXCEPT } ( table_list ) ]
5107  *				FROM SERVER server_name INTO local_schema [ OPTIONS (...) ]
5108  *
5109  ****************************************************************************/
5110 
5111 ImportForeignSchemaStmt:
5112 		IMPORT_P FOREIGN SCHEMA name import_qualification
5113 		  FROM SERVER name INTO name create_generic_options
5114 			{
5115 				ImportForeignSchemaStmt *n = makeNode(ImportForeignSchemaStmt);
5116 				n->server_name = $8;
5117 				n->remote_schema = $4;
5118 				n->local_schema = $10;
5119 				n->list_type = $5->type;
5120 				n->table_list = $5->table_names;
5121 				n->options = $11;
5122 				$$ = (Node *) n;
5123 			}
5124 		;
5125 
5126 import_qualification_type:
5127 		LIMIT TO 				{ $$ = FDW_IMPORT_SCHEMA_LIMIT_TO; }
5128 		| EXCEPT 				{ $$ = FDW_IMPORT_SCHEMA_EXCEPT; }
5129 		;
5130 
5131 import_qualification:
5132 		import_qualification_type '(' relation_expr_list ')'
5133 			{
5134 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5135 				n->type = $1;
5136 				n->table_names = $3;
5137 				$$ = n;
5138 			}
5139 		| /*EMPTY*/
5140 			{
5141 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5142 				n->type = FDW_IMPORT_SCHEMA_ALL;
5143 				n->table_names = NIL;
5144 				$$ = n;
5145 			}
5146 		;
5147 
5148 /*****************************************************************************
5149  *
5150  *		QUERY:
5151  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
5152  *
5153  *****************************************************************************/
5154 
5155 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
5156 				{
5157 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5158 					n->user = $5;
5159 					n->servername = $7;
5160 					n->options = $8;
5161 					n->if_not_exists = false;
5162 					$$ = (Node *) n;
5163 				}
5164 				| CREATE USER MAPPING IF_P NOT EXISTS FOR auth_ident SERVER name create_generic_options
5165 				{
5166 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5167 					n->user = $8;
5168 					n->servername = $10;
5169 					n->options = $11;
5170 					n->if_not_exists = true;
5171 					$$ = (Node *) n;
5172 				}
5173 		;
5174 
5175 /* User mapping authorization identifier */
5176 auth_ident: RoleSpec			{ $$ = $1; }
5177 			| USER				{ $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1); }
5178 		;
5179 
5180 /*****************************************************************************
5181  *
5182  *		QUERY :
5183  *				DROP USER MAPPING FOR auth_ident SERVER name
5184  *
5185  * XXX you'd think this should have a CASCADE/RESTRICT option, even if it's
5186  * only pro forma; but the SQL standard doesn't show one.
5187  ****************************************************************************/
5188 
5189 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
5190 				{
5191 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5192 					n->user = $5;
5193 					n->servername = $7;
5194 					n->missing_ok = false;
5195 					$$ = (Node *) n;
5196 				}
5197 				|  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
5198 				{
5199 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5200 					n->user = $7;
5201 					n->servername = $9;
5202 					n->missing_ok = true;
5203 					$$ = (Node *) n;
5204 				}
5205 		;
5206 
5207 /*****************************************************************************
5208  *
5209  *		QUERY :
5210  *				ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
5211  *
5212  ****************************************************************************/
5213 
5214 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
5215 				{
5216 					AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
5217 					n->user = $5;
5218 					n->servername = $7;
5219 					n->options = $8;
5220 					$$ = (Node *) n;
5221 				}
5222 		;
5223 
5224 /*****************************************************************************
5225  *
5226  *		QUERIES:
5227  *				CREATE POLICY name ON table
5228  *					[AS { PERMISSIVE | RESTRICTIVE } ]
5229  *					[FOR { SELECT | INSERT | UPDATE | DELETE } ]
5230  *					[TO role, ...]
5231  *					[USING (qual)] [WITH CHECK (with check qual)]
5232  *				ALTER POLICY name ON table [TO role, ...]
5233  *					[USING (qual)] [WITH CHECK (with check qual)]
5234  *
5235  *****************************************************************************/
5236 
5237 CreatePolicyStmt:
5238 			CREATE POLICY name ON qualified_name RowSecurityDefaultPermissive
5239 				RowSecurityDefaultForCmd RowSecurityDefaultToRole
5240 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5241 				{
5242 					CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
5243 					n->policy_name = $3;
5244 					n->table = $5;
5245 					n->permissive = $6;
5246 					n->cmd_name = $7;
5247 					n->roles = $8;
5248 					n->qual = $9;
5249 					n->with_check = $10;
5250 					$$ = (Node *) n;
5251 				}
5252 		;
5253 
5254 AlterPolicyStmt:
5255 			ALTER POLICY name ON qualified_name RowSecurityOptionalToRole
5256 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5257 				{
5258 					AlterPolicyStmt *n = makeNode(AlterPolicyStmt);
5259 					n->policy_name = $3;
5260 					n->table = $5;
5261 					n->roles = $6;
5262 					n->qual = $7;
5263 					n->with_check = $8;
5264 					$$ = (Node *) n;
5265 				}
5266 		;
5267 
5268 RowSecurityOptionalExpr:
5269 			USING '(' a_expr ')'	{ $$ = $3; }
5270 			| /* EMPTY */			{ $$ = NULL; }
5271 		;
5272 
5273 RowSecurityOptionalWithCheck:
5274 			WITH CHECK '(' a_expr ')'		{ $$ = $4; }
5275 			| /* EMPTY */					{ $$ = NULL; }
5276 		;
5277 
5278 RowSecurityDefaultToRole:
5279 			TO role_list			{ $$ = $2; }
5280 			| /* EMPTY */			{ $$ = list_make1(makeRoleSpec(ROLESPEC_PUBLIC, -1)); }
5281 		;
5282 
5283 RowSecurityOptionalToRole:
5284 			TO role_list			{ $$ = $2; }
5285 			| /* EMPTY */			{ $$ = NULL; }
5286 		;
5287 
5288 RowSecurityDefaultPermissive:
5289 			AS IDENT
5290 				{
5291 					if (strcmp($2, "permissive") == 0)
5292 						$$ = true;
5293 					else if (strcmp($2, "restrictive") == 0)
5294 						$$ = false;
5295 					else
5296 						ereport(ERROR,
5297 								(errcode(ERRCODE_SYNTAX_ERROR),
5298 							 errmsg("unrecognized row security option \"%s\"", $2),
5299 								 errhint("Only PERMISSIVE or RESTRICTIVE policies are supported currently."),
5300 									 parser_errposition(@2)));
5301 
5302 				}
5303 			| /* EMPTY */			{ $$ = true; }
5304 		;
5305 
5306 RowSecurityDefaultForCmd:
5307 			FOR row_security_cmd	{ $$ = $2; }
5308 			| /* EMPTY */			{ $$ = "all"; }
5309 		;
5310 
5311 row_security_cmd:
5312 			ALL				{ $$ = "all"; }
5313 		|	SELECT			{ $$ = "select"; }
5314 		|	INSERT			{ $$ = "insert"; }
5315 		|	UPDATE			{ $$ = "update"; }
5316 		|	DELETE_P		{ $$ = "delete"; }
5317 		;
5318 
5319 /*****************************************************************************
5320  *
5321  *		QUERY:
5322  *             CREATE ACCESS METHOD name HANDLER handler_name
5323  *
5324  *****************************************************************************/
5325 
5326 CreateAmStmt: CREATE ACCESS METHOD name TYPE_P INDEX HANDLER handler_name
5327 				{
5328 					CreateAmStmt *n = makeNode(CreateAmStmt);
5329 					n->amname = $4;
5330 					n->handler_name = $8;
5331 					n->amtype = AMTYPE_INDEX;
5332 					$$ = (Node *) n;
5333 				}
5334 		;
5335 
5336 /*****************************************************************************
5337  *
5338  *		QUERIES :
5339  *				CREATE TRIGGER ...
5340  *
5341  *****************************************************************************/
5342 
5343 CreateTrigStmt:
5344 			CREATE TRIGGER name TriggerActionTime TriggerEvents ON
5345 			qualified_name TriggerReferencing TriggerForSpec TriggerWhen
5346 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' TriggerFuncArgs ')'
5347 				{
5348 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5349 					n->trigname = $3;
5350 					n->relation = $7;
5351 					n->funcname = $13;
5352 					n->args = $15;
5353 					n->row = $9;
5354 					n->timing = $4;
5355 					n->events = intVal(linitial($5));
5356 					n->columns = (List *) lsecond($5);
5357 					n->whenClause = $10;
5358 					n->transitionRels = $8;
5359 					n->isconstraint  = false;
5360 					n->deferrable	 = false;
5361 					n->initdeferred  = false;
5362 					n->constrrel = NULL;
5363 					$$ = (Node *)n;
5364 				}
5365 			| CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
5366 			qualified_name OptConstrFromTable ConstraintAttributeSpec
5367 			FOR EACH ROW TriggerWhen
5368 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' TriggerFuncArgs ')'
5369 				{
5370 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5371 					n->trigname = $4;
5372 					n->relation = $8;
5373 					n->funcname = $17;
5374 					n->args = $19;
5375 					n->row = true;
5376 					n->timing = TRIGGER_TYPE_AFTER;
5377 					n->events = intVal(linitial($6));
5378 					n->columns = (List *) lsecond($6);
5379 					n->whenClause = $14;
5380 					n->transitionRels = NIL;
5381 					n->isconstraint  = true;
5382 					processCASbits($10, @10, "TRIGGER",
5383 								   &n->deferrable, &n->initdeferred, NULL,
5384 								   NULL, yyscanner);
5385 					n->constrrel = $9;
5386 					$$ = (Node *)n;
5387 				}
5388 		;
5389 
5390 TriggerActionTime:
5391 			BEFORE								{ $$ = TRIGGER_TYPE_BEFORE; }
5392 			| AFTER								{ $$ = TRIGGER_TYPE_AFTER; }
5393 			| INSTEAD OF						{ $$ = TRIGGER_TYPE_INSTEAD; }
5394 		;
5395 
5396 TriggerEvents:
5397 			TriggerOneEvent
5398 				{ $$ = $1; }
5399 			| TriggerEvents OR TriggerOneEvent
5400 				{
5401 					int		events1 = intVal(linitial($1));
5402 					int		events2 = intVal(linitial($3));
5403 					List   *columns1 = (List *) lsecond($1);
5404 					List   *columns2 = (List *) lsecond($3);
5405 
5406 					if (events1 & events2)
5407 						parser_yyerror("duplicate trigger events specified");
5408 					/*
5409 					 * concat'ing the columns lists loses information about
5410 					 * which columns went with which event, but so long as
5411 					 * only UPDATE carries columns and we disallow multiple
5412 					 * UPDATE items, it doesn't matter.  Command execution
5413 					 * should just ignore the columns for non-UPDATE events.
5414 					 */
5415 					$$ = list_make2(makeInteger(events1 | events2),
5416 									list_concat(columns1, columns2));
5417 				}
5418 		;
5419 
5420 TriggerOneEvent:
5421 			INSERT
5422 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
5423 			| DELETE_P
5424 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
5425 			| UPDATE
5426 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
5427 			| UPDATE OF columnList
5428 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
5429 			| TRUNCATE
5430 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
5431 		;
5432 
5433 TriggerReferencing:
5434 			REFERENCING TriggerTransitions			{ $$ = $2; }
5435 			| /*EMPTY*/								{ $$ = NIL; }
5436 		;
5437 
5438 TriggerTransitions:
5439 			TriggerTransition						{ $$ = list_make1($1); }
5440 			| TriggerTransitions TriggerTransition	{ $$ = lappend($1, $2); }
5441 		;
5442 
5443 TriggerTransition:
5444 			TransitionOldOrNew TransitionRowOrTable opt_as TransitionRelName
5445 				{
5446 					TriggerTransition *n = makeNode(TriggerTransition);
5447 					n->name = $4;
5448 					n->isNew = $1;
5449 					n->isTable = $2;
5450 					$$ = (Node *)n;
5451 				}
5452 		;
5453 
5454 TransitionOldOrNew:
5455 			NEW										{ $$ = true; }
5456 			| OLD									{ $$ = false; }
5457 		;
5458 
5459 TransitionRowOrTable:
5460 			TABLE									{ $$ = true; }
5461 			/*
5462 			 * According to the standard, lack of a keyword here implies ROW.
5463 			 * Support for that would require prohibiting ROW entirely here,
5464 			 * reserving the keyword ROW, and/or requiring AS (instead of
5465 			 * allowing it to be optional, as the standard specifies) as the
5466 			 * next token.  Requiring ROW seems cleanest and easiest to
5467 			 * explain.
5468 			 */
5469 			| ROW									{ $$ = false; }
5470 		;
5471 
5472 TransitionRelName:
5473 			ColId									{ $$ = $1; }
5474 		;
5475 
5476 TriggerForSpec:
5477 			FOR TriggerForOptEach TriggerForType
5478 				{
5479 					$$ = $3;
5480 				}
5481 			| /* EMPTY */
5482 				{
5483 					/*
5484 					 * If ROW/STATEMENT not specified, default to
5485 					 * STATEMENT, per SQL
5486 					 */
5487 					$$ = false;
5488 				}
5489 		;
5490 
5491 TriggerForOptEach:
5492 			EACH									{}
5493 			| /*EMPTY*/								{}
5494 		;
5495 
5496 TriggerForType:
5497 			ROW										{ $$ = true; }
5498 			| STATEMENT								{ $$ = false; }
5499 		;
5500 
5501 TriggerWhen:
5502 			WHEN '(' a_expr ')'						{ $$ = $3; }
5503 			| /*EMPTY*/								{ $$ = NULL; }
5504 		;
5505 
5506 FUNCTION_or_PROCEDURE:
5507 			FUNCTION
5508 		|	PROCEDURE
5509 		;
5510 
5511 TriggerFuncArgs:
5512 			TriggerFuncArg							{ $$ = list_make1($1); }
5513 			| TriggerFuncArgs ',' TriggerFuncArg	{ $$ = lappend($1, $3); }
5514 			| /*EMPTY*/								{ $$ = NIL; }
5515 		;
5516 
5517 TriggerFuncArg:
5518 			Iconst
5519 				{
5520 					$$ = makeString(psprintf("%d", $1));
5521 				}
5522 			| FCONST								{ $$ = makeString($1); }
5523 			| Sconst								{ $$ = makeString($1); }
5524 			| ColLabel								{ $$ = makeString($1); }
5525 		;
5526 
5527 OptConstrFromTable:
5528 			FROM qualified_name						{ $$ = $2; }
5529 			| /*EMPTY*/								{ $$ = NULL; }
5530 		;
5531 
5532 ConstraintAttributeSpec:
5533 			/*EMPTY*/
5534 				{ $$ = 0; }
5535 			| ConstraintAttributeSpec ConstraintAttributeElem
5536 				{
5537 					/*
5538 					 * We must complain about conflicting options.
5539 					 * We could, but choose not to, complain about redundant
5540 					 * options (ie, where $2's bit is already set in $1).
5541 					 */
5542 					int		newspec = $1 | $2;
5543 
5544 					/* special message for this case */
5545 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED)) == (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED))
5546 						ereport(ERROR,
5547 								(errcode(ERRCODE_SYNTAX_ERROR),
5548 								 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
5549 								 parser_errposition(@2)));
5550 					/* generic message for other conflicts */
5551 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE)) == (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE) ||
5552 						(newspec & (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED)) == (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED))
5553 						ereport(ERROR,
5554 								(errcode(ERRCODE_SYNTAX_ERROR),
5555 								 errmsg("conflicting constraint properties"),
5556 								 parser_errposition(@2)));
5557 					$$ = newspec;
5558 				}
5559 		;
5560 
5561 ConstraintAttributeElem:
5562 			NOT DEFERRABLE					{ $$ = CAS_NOT_DEFERRABLE; }
5563 			| DEFERRABLE					{ $$ = CAS_DEFERRABLE; }
5564 			| INITIALLY IMMEDIATE			{ $$ = CAS_INITIALLY_IMMEDIATE; }
5565 			| INITIALLY DEFERRED			{ $$ = CAS_INITIALLY_DEFERRED; }
5566 			| NOT VALID						{ $$ = CAS_NOT_VALID; }
5567 			| NO INHERIT					{ $$ = CAS_NO_INHERIT; }
5568 		;
5569 
5570 
5571 /*****************************************************************************
5572  *
5573  *		QUERIES :
5574  *				CREATE EVENT TRIGGER ...
5575  *				ALTER EVENT TRIGGER ...
5576  *
5577  *****************************************************************************/
5578 
5579 CreateEventTrigStmt:
5580 			CREATE EVENT TRIGGER name ON ColLabel
5581 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' ')'
5582 				{
5583 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5584 					n->trigname = $4;
5585 					n->eventname = $6;
5586 					n->whenclause = NULL;
5587 					n->funcname = $9;
5588 					$$ = (Node *)n;
5589 				}
5590 		  | CREATE EVENT TRIGGER name ON ColLabel
5591 			WHEN event_trigger_when_list
5592 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' ')'
5593 				{
5594 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5595 					n->trigname = $4;
5596 					n->eventname = $6;
5597 					n->whenclause = $8;
5598 					n->funcname = $11;
5599 					$$ = (Node *)n;
5600 				}
5601 		;
5602 
5603 event_trigger_when_list:
5604 		  event_trigger_when_item
5605 			{ $$ = list_make1($1); }
5606 		| event_trigger_when_list AND event_trigger_when_item
5607 			{ $$ = lappend($1, $3); }
5608 		;
5609 
5610 event_trigger_when_item:
5611 		ColId IN_P '(' event_trigger_value_list ')'
5612 			{ $$ = makeDefElem($1, (Node *) $4, @1); }
5613 		;
5614 
5615 event_trigger_value_list:
5616 		  SCONST
5617 			{ $$ = list_make1(makeString($1)); }
5618 		| event_trigger_value_list ',' SCONST
5619 			{ $$ = lappend($1, makeString($3)); }
5620 		;
5621 
5622 AlterEventTrigStmt:
5623 			ALTER EVENT TRIGGER name enable_trigger
5624 				{
5625 					AlterEventTrigStmt *n = makeNode(AlterEventTrigStmt);
5626 					n->trigname = $4;
5627 					n->tgenabled = $5;
5628 					$$ = (Node *) n;
5629 				}
5630 		;
5631 
5632 enable_trigger:
5633 			ENABLE_P					{ $$ = TRIGGER_FIRES_ON_ORIGIN; }
5634 			| ENABLE_P REPLICA			{ $$ = TRIGGER_FIRES_ON_REPLICA; }
5635 			| ENABLE_P ALWAYS			{ $$ = TRIGGER_FIRES_ALWAYS; }
5636 			| DISABLE_P					{ $$ = TRIGGER_DISABLED; }
5637 		;
5638 
5639 /*****************************************************************************
5640  *
5641  *		QUERIES :
5642  *				CREATE ASSERTION ...
5643  *				DROP ASSERTION ...
5644  *
5645  *****************************************************************************/
5646 
5647 CreateAssertStmt:
5648 			CREATE ASSERTION name CHECK '(' a_expr ')'
5649 			ConstraintAttributeSpec
5650 				{
5651 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5652 					n->trigname = $3;
5653 					n->args = list_make1($6);
5654 					n->isconstraint  = true;
5655 					processCASbits($8, @8, "ASSERTION",
5656 								   &n->deferrable, &n->initdeferred, NULL,
5657 								   NULL, yyscanner);
5658 
5659 					ereport(ERROR,
5660 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5661 							 errmsg("CREATE ASSERTION is not yet implemented")));
5662 
5663 					$$ = (Node *)n;
5664 				}
5665 		;
5666 
5667 DropAssertStmt:
5668 			DROP ASSERTION name opt_drop_behavior
5669 				{
5670 					DropStmt *n = makeNode(DropStmt);
5671 					n->objects = NIL;
5672 					n->behavior = $4;
5673 					n->removeType = OBJECT_TRIGGER; /* XXX */
5674 					ereport(ERROR,
5675 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5676 							 errmsg("DROP ASSERTION is not yet implemented")));
5677 					$$ = (Node *) n;
5678 				}
5679 		;
5680 
5681 
5682 /*****************************************************************************
5683  *
5684  *		QUERY :
5685  *				define (aggregate,operator,type)
5686  *
5687  *****************************************************************************/
5688 
5689 DefineStmt:
5690 			CREATE AGGREGATE func_name aggr_args definition
5691 				{
5692 					DefineStmt *n = makeNode(DefineStmt);
5693 					n->kind = OBJECT_AGGREGATE;
5694 					n->oldstyle = false;
5695 					n->defnames = $3;
5696 					n->args = $4;
5697 					n->definition = $5;
5698 					$$ = (Node *)n;
5699 				}
5700 			| CREATE AGGREGATE func_name old_aggr_definition
5701 				{
5702 					/* old-style (pre-8.2) syntax for CREATE AGGREGATE */
5703 					DefineStmt *n = makeNode(DefineStmt);
5704 					n->kind = OBJECT_AGGREGATE;
5705 					n->oldstyle = true;
5706 					n->defnames = $3;
5707 					n->args = NIL;
5708 					n->definition = $4;
5709 					$$ = (Node *)n;
5710 				}
5711 			| CREATE OPERATOR any_operator definition
5712 				{
5713 					DefineStmt *n = makeNode(DefineStmt);
5714 					n->kind = OBJECT_OPERATOR;
5715 					n->oldstyle = false;
5716 					n->defnames = $3;
5717 					n->args = NIL;
5718 					n->definition = $4;
5719 					$$ = (Node *)n;
5720 				}
5721 			| CREATE TYPE_P any_name definition
5722 				{
5723 					DefineStmt *n = makeNode(DefineStmt);
5724 					n->kind = OBJECT_TYPE;
5725 					n->oldstyle = false;
5726 					n->defnames = $3;
5727 					n->args = NIL;
5728 					n->definition = $4;
5729 					$$ = (Node *)n;
5730 				}
5731 			| CREATE TYPE_P any_name
5732 				{
5733 					/* Shell type (identified by lack of definition) */
5734 					DefineStmt *n = makeNode(DefineStmt);
5735 					n->kind = OBJECT_TYPE;
5736 					n->oldstyle = false;
5737 					n->defnames = $3;
5738 					n->args = NIL;
5739 					n->definition = NIL;
5740 					$$ = (Node *)n;
5741 				}
5742 			| CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
5743 				{
5744 					CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
5745 
5746 					/* can't use qualified_name, sigh */
5747 					n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
5748 					n->coldeflist = $6;
5749 					$$ = (Node *)n;
5750 				}
5751 			| CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
5752 				{
5753 					CreateEnumStmt *n = makeNode(CreateEnumStmt);
5754 					n->typeName = $3;
5755 					n->vals = $7;
5756 					$$ = (Node *)n;
5757 				}
5758 			| CREATE TYPE_P any_name AS RANGE definition
5759 				{
5760 					CreateRangeStmt *n = makeNode(CreateRangeStmt);
5761 					n->typeName = $3;
5762 					n->params	= $6;
5763 					$$ = (Node *)n;
5764 				}
5765 			| CREATE TEXT_P SEARCH PARSER any_name definition
5766 				{
5767 					DefineStmt *n = makeNode(DefineStmt);
5768 					n->kind = OBJECT_TSPARSER;
5769 					n->args = NIL;
5770 					n->defnames = $5;
5771 					n->definition = $6;
5772 					$$ = (Node *)n;
5773 				}
5774 			| CREATE TEXT_P SEARCH DICTIONARY any_name definition
5775 				{
5776 					DefineStmt *n = makeNode(DefineStmt);
5777 					n->kind = OBJECT_TSDICTIONARY;
5778 					n->args = NIL;
5779 					n->defnames = $5;
5780 					n->definition = $6;
5781 					$$ = (Node *)n;
5782 				}
5783 			| CREATE TEXT_P SEARCH TEMPLATE any_name definition
5784 				{
5785 					DefineStmt *n = makeNode(DefineStmt);
5786 					n->kind = OBJECT_TSTEMPLATE;
5787 					n->args = NIL;
5788 					n->defnames = $5;
5789 					n->definition = $6;
5790 					$$ = (Node *)n;
5791 				}
5792 			| CREATE TEXT_P SEARCH CONFIGURATION any_name definition
5793 				{
5794 					DefineStmt *n = makeNode(DefineStmt);
5795 					n->kind = OBJECT_TSCONFIGURATION;
5796 					n->args = NIL;
5797 					n->defnames = $5;
5798 					n->definition = $6;
5799 					$$ = (Node *)n;
5800 				}
5801 			| CREATE COLLATION any_name definition
5802 				{
5803 					DefineStmt *n = makeNode(DefineStmt);
5804 					n->kind = OBJECT_COLLATION;
5805 					n->args = NIL;
5806 					n->defnames = $3;
5807 					n->definition = $4;
5808 					$$ = (Node *)n;
5809 				}
5810 			| CREATE COLLATION IF_P NOT EXISTS any_name definition
5811 				{
5812 					DefineStmt *n = makeNode(DefineStmt);
5813 					n->kind = OBJECT_COLLATION;
5814 					n->args = NIL;
5815 					n->defnames = $6;
5816 					n->definition = $7;
5817 					n->if_not_exists = true;
5818 					$$ = (Node *)n;
5819 				}
5820 			| CREATE COLLATION any_name FROM any_name
5821 				{
5822 					DefineStmt *n = makeNode(DefineStmt);
5823 					n->kind = OBJECT_COLLATION;
5824 					n->args = NIL;
5825 					n->defnames = $3;
5826 					n->definition = list_make1(makeDefElem("from", (Node *) $5, @5));
5827 					$$ = (Node *)n;
5828 				}
5829 			| CREATE COLLATION IF_P NOT EXISTS any_name FROM any_name
5830 				{
5831 					DefineStmt *n = makeNode(DefineStmt);
5832 					n->kind = OBJECT_COLLATION;
5833 					n->args = NIL;
5834 					n->defnames = $6;
5835 					n->definition = list_make1(makeDefElem("from", (Node *) $8, @8));
5836 					n->if_not_exists = true;
5837 					$$ = (Node *)n;
5838 				}
5839 		;
5840 
5841 definition: '(' def_list ')'						{ $$ = $2; }
5842 		;
5843 
5844 def_list:	def_elem								{ $$ = list_make1($1); }
5845 			| def_list ',' def_elem					{ $$ = lappend($1, $3); }
5846 		;
5847 
5848 def_elem:	ColLabel '=' def_arg
5849 				{
5850 					$$ = makeDefElem($1, (Node *) $3, @1);
5851 				}
5852 			| ColLabel
5853 				{
5854 					$$ = makeDefElem($1, NULL, @1);
5855 				}
5856 		;
5857 
5858 /* Note: any simple identifier will be returned as a type name! */
5859 def_arg:	func_type						{ $$ = (Node *)$1; }
5860 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
5861 			| qual_all_Op					{ $$ = (Node *)$1; }
5862 			| NumericOnly					{ $$ = (Node *)$1; }
5863 			| Sconst						{ $$ = (Node *)makeString($1); }
5864 			| NONE							{ $$ = (Node *)makeString(pstrdup($1)); }
5865 		;
5866 
5867 old_aggr_definition: '(' old_aggr_list ')'			{ $$ = $2; }
5868 		;
5869 
5870 old_aggr_list: old_aggr_elem						{ $$ = list_make1($1); }
5871 			| old_aggr_list ',' old_aggr_elem		{ $$ = lappend($1, $3); }
5872 		;
5873 
5874 /*
5875  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
5876  * the item names needed in old aggregate definitions are likely to become
5877  * SQL keywords.
5878  */
5879 old_aggr_elem:  IDENT '=' def_arg
5880 				{
5881 					$$ = makeDefElem($1, (Node *)$3, @1);
5882 				}
5883 		;
5884 
5885 opt_enum_val_list:
5886 		enum_val_list							{ $$ = $1; }
5887 		| /*EMPTY*/								{ $$ = NIL; }
5888 		;
5889 
5890 enum_val_list:	Sconst
5891 				{ $$ = list_make1(makeString($1)); }
5892 			| enum_val_list ',' Sconst
5893 				{ $$ = lappend($1, makeString($3)); }
5894 		;
5895 
5896 /*****************************************************************************
5897  *
5898  *	ALTER TYPE enumtype ADD ...
5899  *
5900  *****************************************************************************/
5901 
5902 AlterEnumStmt:
5903 		ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst
5904 			{
5905 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5906 				n->typeName = $3;
5907 				n->oldVal = NULL;
5908 				n->newVal = $7;
5909 				n->newValNeighbor = NULL;
5910 				n->newValIsAfter = true;
5911 				n->skipIfNewValExists = $6;
5912 				$$ = (Node *) n;
5913 			}
5914 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst BEFORE Sconst
5915 			{
5916 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5917 				n->typeName = $3;
5918 				n->oldVal = NULL;
5919 				n->newVal = $7;
5920 				n->newValNeighbor = $9;
5921 				n->newValIsAfter = false;
5922 				n->skipIfNewValExists = $6;
5923 				$$ = (Node *) n;
5924 			}
5925 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst AFTER Sconst
5926 			{
5927 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5928 				n->typeName = $3;
5929 				n->oldVal = NULL;
5930 				n->newVal = $7;
5931 				n->newValNeighbor = $9;
5932 				n->newValIsAfter = true;
5933 				n->skipIfNewValExists = $6;
5934 				$$ = (Node *) n;
5935 			}
5936 		 | ALTER TYPE_P any_name RENAME VALUE_P Sconst TO Sconst
5937 			{
5938 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5939 				n->typeName = $3;
5940 				n->oldVal = $6;
5941 				n->newVal = $8;
5942 				n->newValNeighbor = NULL;
5943 				n->newValIsAfter = false;
5944 				n->skipIfNewValExists = false;
5945 				$$ = (Node *) n;
5946 			}
5947 		 ;
5948 
5949 opt_if_not_exists: IF_P NOT EXISTS              { $$ = true; }
5950 		| /* empty */                          { $$ = false; }
5951 		;
5952 
5953 
5954 /*****************************************************************************
5955  *
5956  *		QUERIES :
5957  *				CREATE OPERATOR CLASS ...
5958  *				CREATE OPERATOR FAMILY ...
5959  *				ALTER OPERATOR FAMILY ...
5960  *				DROP OPERATOR CLASS ...
5961  *				DROP OPERATOR FAMILY ...
5962  *
5963  *****************************************************************************/
5964 
5965 CreateOpClassStmt:
5966 			CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
5967 			USING access_method opt_opfamily AS opclass_item_list
5968 				{
5969 					CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
5970 					n->opclassname = $4;
5971 					n->isDefault = $5;
5972 					n->datatype = $8;
5973 					n->amname = $10;
5974 					n->opfamilyname = $11;
5975 					n->items = $13;
5976 					$$ = (Node *) n;
5977 				}
5978 		;
5979 
5980 opclass_item_list:
5981 			opclass_item							{ $$ = list_make1($1); }
5982 			| opclass_item_list ',' opclass_item	{ $$ = lappend($1, $3); }
5983 		;
5984 
5985 opclass_item:
5986 			OPERATOR Iconst any_operator opclass_purpose opt_recheck
5987 				{
5988 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5989 					ObjectWithArgs *owa = makeNode(ObjectWithArgs);
5990 					owa->objname = $3;
5991 					owa->objargs = NIL;
5992 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5993 					n->name = owa;
5994 					n->number = $2;
5995 					n->order_family = $4;
5996 					$$ = (Node *) n;
5997 				}
5998 			| OPERATOR Iconst operator_with_argtypes opclass_purpose
5999 			  opt_recheck
6000 				{
6001 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6002 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6003 					n->name = $3;
6004 					n->number = $2;
6005 					n->order_family = $4;
6006 					$$ = (Node *) n;
6007 				}
6008 			| FUNCTION Iconst function_with_argtypes
6009 				{
6010 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6011 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6012 					n->name = $3;
6013 					n->number = $2;
6014 					$$ = (Node *) n;
6015 				}
6016 			| FUNCTION Iconst '(' type_list ')' function_with_argtypes
6017 				{
6018 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6019 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6020 					n->name = $6;
6021 					n->number = $2;
6022 					n->class_args = $4;
6023 					$$ = (Node *) n;
6024 				}
6025 			| STORAGE Typename
6026 				{
6027 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6028 					n->itemtype = OPCLASS_ITEM_STORAGETYPE;
6029 					n->storedtype = $2;
6030 					$$ = (Node *) n;
6031 				}
6032 		;
6033 
6034 opt_default:	DEFAULT						{ $$ = true; }
6035 			| /*EMPTY*/						{ $$ = false; }
6036 		;
6037 
6038 opt_opfamily:	FAMILY any_name				{ $$ = $2; }
6039 			| /*EMPTY*/						{ $$ = NIL; }
6040 		;
6041 
6042 opclass_purpose: FOR SEARCH					{ $$ = NIL; }
6043 			| FOR ORDER BY any_name			{ $$ = $4; }
6044 			| /*EMPTY*/						{ $$ = NIL; }
6045 		;
6046 
6047 opt_recheck:	RECHECK
6048 				{
6049 					/*
6050 					 * RECHECK no longer does anything in opclass definitions,
6051 					 * but we still accept it to ease porting of old database
6052 					 * dumps.
6053 					 */
6054 					ereport(NOTICE,
6055 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6056 							 errmsg("RECHECK is no longer required"),
6057 							 errhint("Update your data type."),
6058 							 parser_errposition(@1)));
6059 					$$ = true;
6060 				}
6061 			| /*EMPTY*/						{ $$ = false; }
6062 		;
6063 
6064 
6065 CreateOpFamilyStmt:
6066 			CREATE OPERATOR FAMILY any_name USING access_method
6067 				{
6068 					CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
6069 					n->opfamilyname = $4;
6070 					n->amname = $6;
6071 					$$ = (Node *) n;
6072 				}
6073 		;
6074 
6075 AlterOpFamilyStmt:
6076 			ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
6077 				{
6078 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6079 					n->opfamilyname = $4;
6080 					n->amname = $6;
6081 					n->isDrop = false;
6082 					n->items = $8;
6083 					$$ = (Node *) n;
6084 				}
6085 			| ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
6086 				{
6087 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6088 					n->opfamilyname = $4;
6089 					n->amname = $6;
6090 					n->isDrop = true;
6091 					n->items = $8;
6092 					$$ = (Node *) n;
6093 				}
6094 		;
6095 
6096 opclass_drop_list:
6097 			opclass_drop							{ $$ = list_make1($1); }
6098 			| opclass_drop_list ',' opclass_drop	{ $$ = lappend($1, $3); }
6099 		;
6100 
6101 opclass_drop:
6102 			OPERATOR Iconst '(' type_list ')'
6103 				{
6104 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6105 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6106 					n->number = $2;
6107 					n->class_args = $4;
6108 					$$ = (Node *) n;
6109 				}
6110 			| FUNCTION Iconst '(' type_list ')'
6111 				{
6112 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6113 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6114 					n->number = $2;
6115 					n->class_args = $4;
6116 					$$ = (Node *) n;
6117 				}
6118 		;
6119 
6120 
6121 DropOpClassStmt:
6122 			DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
6123 				{
6124 					DropStmt *n = makeNode(DropStmt);
6125 					n->objects = list_make1(lcons(makeString($6), $4));
6126 					n->removeType = OBJECT_OPCLASS;
6127 					n->behavior = $7;
6128 					n->missing_ok = false;
6129 					n->concurrent = false;
6130 					$$ = (Node *) n;
6131 				}
6132 			| DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
6133 				{
6134 					DropStmt *n = makeNode(DropStmt);
6135 					n->objects = list_make1(lcons(makeString($8), $6));
6136 					n->removeType = OBJECT_OPCLASS;
6137 					n->behavior = $9;
6138 					n->missing_ok = true;
6139 					n->concurrent = false;
6140 					$$ = (Node *) n;
6141 				}
6142 		;
6143 
6144 DropOpFamilyStmt:
6145 			DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
6146 				{
6147 					DropStmt *n = makeNode(DropStmt);
6148 					n->objects = list_make1(lcons(makeString($6), $4));
6149 					n->removeType = OBJECT_OPFAMILY;
6150 					n->behavior = $7;
6151 					n->missing_ok = false;
6152 					n->concurrent = false;
6153 					$$ = (Node *) n;
6154 				}
6155 			| DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
6156 				{
6157 					DropStmt *n = makeNode(DropStmt);
6158 					n->objects = list_make1(lcons(makeString($8), $6));
6159 					n->removeType = OBJECT_OPFAMILY;
6160 					n->behavior = $9;
6161 					n->missing_ok = true;
6162 					n->concurrent = false;
6163 					$$ = (Node *) n;
6164 				}
6165 		;
6166 
6167 
6168 /*****************************************************************************
6169  *
6170  *		QUERY:
6171  *
6172  *		DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
6173  *		REASSIGN OWNED BY username [, username ...] TO username
6174  *
6175  *****************************************************************************/
6176 DropOwnedStmt:
6177 			DROP OWNED BY role_list opt_drop_behavior
6178 				{
6179 					DropOwnedStmt *n = makeNode(DropOwnedStmt);
6180 					n->roles = $4;
6181 					n->behavior = $5;
6182 					$$ = (Node *)n;
6183 				}
6184 		;
6185 
6186 ReassignOwnedStmt:
6187 			REASSIGN OWNED BY role_list TO RoleSpec
6188 				{
6189 					ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
6190 					n->roles = $4;
6191 					n->newrole = $6;
6192 					$$ = (Node *)n;
6193 				}
6194 		;
6195 
6196 /*****************************************************************************
6197  *
6198  *		QUERY:
6199  *
6200  *		DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
6201  *           [ RESTRICT | CASCADE ]
6202  *
6203  *****************************************************************************/
6204 
6205 DropStmt:	DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
6206 				{
6207 					DropStmt *n = makeNode(DropStmt);
6208 					n->removeType = $2;
6209 					n->missing_ok = true;
6210 					n->objects = $5;
6211 					n->behavior = $6;
6212 					n->concurrent = false;
6213 					$$ = (Node *)n;
6214 				}
6215 			| DROP drop_type_any_name any_name_list opt_drop_behavior
6216 				{
6217 					DropStmt *n = makeNode(DropStmt);
6218 					n->removeType = $2;
6219 					n->missing_ok = false;
6220 					n->objects = $3;
6221 					n->behavior = $4;
6222 					n->concurrent = false;
6223 					$$ = (Node *)n;
6224 				}
6225 			| DROP drop_type_name IF_P EXISTS name_list opt_drop_behavior
6226 				{
6227 					DropStmt *n = makeNode(DropStmt);
6228 					n->removeType = $2;
6229 					n->missing_ok = true;
6230 					n->objects = $5;
6231 					n->behavior = $6;
6232 					n->concurrent = false;
6233 					$$ = (Node *)n;
6234 				}
6235 			| DROP drop_type_name name_list opt_drop_behavior
6236 				{
6237 					DropStmt *n = makeNode(DropStmt);
6238 					n->removeType = $2;
6239 					n->missing_ok = false;
6240 					n->objects = $3;
6241 					n->behavior = $4;
6242 					n->concurrent = false;
6243 					$$ = (Node *)n;
6244 				}
6245 			| DROP drop_type_name_on_any_name name ON any_name opt_drop_behavior
6246 				{
6247 					DropStmt *n = makeNode(DropStmt);
6248 					n->removeType = $2;
6249 					n->objects = list_make1(lappend($5, makeString($3)));
6250 					n->behavior = $6;
6251 					n->missing_ok = false;
6252 					n->concurrent = false;
6253 					$$ = (Node *) n;
6254 				}
6255 			| DROP drop_type_name_on_any_name IF_P EXISTS name ON any_name opt_drop_behavior
6256 				{
6257 					DropStmt *n = makeNode(DropStmt);
6258 					n->removeType = $2;
6259 					n->objects = list_make1(lappend($7, makeString($5)));
6260 					n->behavior = $8;
6261 					n->missing_ok = true;
6262 					n->concurrent = false;
6263 					$$ = (Node *) n;
6264 				}
6265 			| DROP TYPE_P type_name_list opt_drop_behavior
6266 				{
6267 					DropStmt *n = makeNode(DropStmt);
6268 					n->removeType = OBJECT_TYPE;
6269 					n->missing_ok = false;
6270 					n->objects = $3;
6271 					n->behavior = $4;
6272 					n->concurrent = false;
6273 					$$ = (Node *) n;
6274 				}
6275 			| DROP TYPE_P IF_P EXISTS type_name_list opt_drop_behavior
6276 				{
6277 					DropStmt *n = makeNode(DropStmt);
6278 					n->removeType = OBJECT_TYPE;
6279 					n->missing_ok = true;
6280 					n->objects = $5;
6281 					n->behavior = $6;
6282 					n->concurrent = false;
6283 					$$ = (Node *) n;
6284 				}
6285 			| DROP DOMAIN_P type_name_list opt_drop_behavior
6286 				{
6287 					DropStmt *n = makeNode(DropStmt);
6288 					n->removeType = OBJECT_DOMAIN;
6289 					n->missing_ok = false;
6290 					n->objects = $3;
6291 					n->behavior = $4;
6292 					n->concurrent = false;
6293 					$$ = (Node *) n;
6294 				}
6295 			| DROP DOMAIN_P IF_P EXISTS type_name_list opt_drop_behavior
6296 				{
6297 					DropStmt *n = makeNode(DropStmt);
6298 					n->removeType = OBJECT_DOMAIN;
6299 					n->missing_ok = true;
6300 					n->objects = $5;
6301 					n->behavior = $6;
6302 					n->concurrent = false;
6303 					$$ = (Node *) n;
6304 				}
6305 			| DROP INDEX CONCURRENTLY any_name_list opt_drop_behavior
6306 				{
6307 					DropStmt *n = makeNode(DropStmt);
6308 					n->removeType = OBJECT_INDEX;
6309 					n->missing_ok = false;
6310 					n->objects = $4;
6311 					n->behavior = $5;
6312 					n->concurrent = true;
6313 					$$ = (Node *)n;
6314 				}
6315 			| DROP INDEX CONCURRENTLY IF_P EXISTS any_name_list opt_drop_behavior
6316 				{
6317 					DropStmt *n = makeNode(DropStmt);
6318 					n->removeType = OBJECT_INDEX;
6319 					n->missing_ok = true;
6320 					n->objects = $6;
6321 					n->behavior = $7;
6322 					n->concurrent = true;
6323 					$$ = (Node *)n;
6324 				}
6325 		;
6326 
6327 /* object types taking any_name_list */
6328 drop_type_any_name:
6329 			TABLE									{ $$ = OBJECT_TABLE; }
6330 			| SEQUENCE								{ $$ = OBJECT_SEQUENCE; }
6331 			| VIEW									{ $$ = OBJECT_VIEW; }
6332 			| MATERIALIZED VIEW						{ $$ = OBJECT_MATVIEW; }
6333 			| INDEX									{ $$ = OBJECT_INDEX; }
6334 			| FOREIGN TABLE							{ $$ = OBJECT_FOREIGN_TABLE; }
6335 			| COLLATION								{ $$ = OBJECT_COLLATION; }
6336 			| CONVERSION_P							{ $$ = OBJECT_CONVERSION; }
6337 			| STATISTICS							{ $$ = OBJECT_STATISTIC_EXT; }
6338 			| TEXT_P SEARCH PARSER					{ $$ = OBJECT_TSPARSER; }
6339 			| TEXT_P SEARCH DICTIONARY				{ $$ = OBJECT_TSDICTIONARY; }
6340 			| TEXT_P SEARCH TEMPLATE				{ $$ = OBJECT_TSTEMPLATE; }
6341 			| TEXT_P SEARCH CONFIGURATION			{ $$ = OBJECT_TSCONFIGURATION; }
6342 		;
6343 
6344 /* object types taking name_list */
6345 drop_type_name:
6346 			ACCESS METHOD							{ $$ = OBJECT_ACCESS_METHOD; }
6347 			| EVENT TRIGGER							{ $$ = OBJECT_EVENT_TRIGGER; }
6348 			| EXTENSION								{ $$ = OBJECT_EXTENSION; }
6349 			| FOREIGN DATA_P WRAPPER				{ $$ = OBJECT_FDW; }
6350 			| PUBLICATION							{ $$ = OBJECT_PUBLICATION; }
6351 			| SCHEMA								{ $$ = OBJECT_SCHEMA; }
6352 			| SERVER								{ $$ = OBJECT_FOREIGN_SERVER; }
6353 		;
6354 
6355 /* object types attached to a table */
6356 drop_type_name_on_any_name:
6357 			POLICY									{ $$ = OBJECT_POLICY; }
6358 			| RULE									{ $$ = OBJECT_RULE; }
6359 			| TRIGGER								{ $$ = OBJECT_TRIGGER; }
6360 		;
6361 
6362 any_name_list:
6363 			any_name								{ $$ = list_make1($1); }
6364 			| any_name_list ',' any_name			{ $$ = lappend($1, $3); }
6365 		;
6366 
6367 any_name:	ColId						{ $$ = list_make1(makeString($1)); }
6368 			| ColId attrs				{ $$ = lcons(makeString($1), $2); }
6369 		;
6370 
6371 attrs:		'.' attr_name
6372 					{ $$ = list_make1(makeString($2)); }
6373 			| attrs '.' attr_name
6374 					{ $$ = lappend($1, makeString($3)); }
6375 		;
6376 
6377 type_name_list:
6378 			Typename								{ $$ = list_make1($1); }
6379 			| type_name_list ',' Typename			{ $$ = lappend($1, $3); }
6380 		;
6381 
6382 /*****************************************************************************
6383  *
6384  *		QUERY:
6385  *				truncate table relname1, relname2, ...
6386  *
6387  *****************************************************************************/
6388 
6389 TruncateStmt:
6390 			TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
6391 				{
6392 					TruncateStmt *n = makeNode(TruncateStmt);
6393 					n->relations = $3;
6394 					n->restart_seqs = $4;
6395 					n->behavior = $5;
6396 					$$ = (Node *)n;
6397 				}
6398 		;
6399 
6400 opt_restart_seqs:
6401 			CONTINUE_P IDENTITY_P		{ $$ = false; }
6402 			| RESTART IDENTITY_P		{ $$ = true; }
6403 			| /* EMPTY */				{ $$ = false; }
6404 		;
6405 
6406 /*****************************************************************************
6407  *
6408  *	The COMMENT ON statement can take different forms based upon the type of
6409  *	the object associated with the comment. The form of the statement is:
6410  *
6411  *	COMMENT ON [ [ ACCESS METHOD | CONVERSION | COLLATION |
6412  *                 DATABASE | DOMAIN |
6413  *                 EXTENSION | EVENT TRIGGER | FOREIGN DATA WRAPPER |
6414  *                 FOREIGN TABLE | INDEX | [PROCEDURAL] LANGUAGE |
6415  *                 MATERIALIZED VIEW | POLICY | ROLE | SCHEMA | SEQUENCE |
6416  *                 SERVER | STATISTICS | TABLE | TABLESPACE |
6417  *                 TEXT SEARCH CONFIGURATION | TEXT SEARCH DICTIONARY |
6418  *                 TEXT SEARCH PARSER | TEXT SEARCH TEMPLATE | TYPE |
6419  *                 VIEW] <objname> |
6420  *				 AGGREGATE <aggname> (arg1, ...) |
6421  *				 CAST (<src type> AS <dst type>) |
6422  *				 COLUMN <relname>.<colname> |
6423  *				 CONSTRAINT <constraintname> ON <relname> |
6424  *				 CONSTRAINT <constraintname> ON DOMAIN <domainname> |
6425  *				 FUNCTION <funcname> (arg1, arg2, ...) |
6426  *				 LARGE OBJECT <oid> |
6427  *				 OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
6428  *				 OPERATOR CLASS <name> USING <access-method> |
6429  *				 OPERATOR FAMILY <name> USING <access-method> |
6430  *				 RULE <rulename> ON <relname> |
6431  *				 TRIGGER <triggername> ON <relname> ]
6432  *			   IS { 'text' | NULL }
6433  *
6434  *****************************************************************************/
6435 
6436 CommentStmt:
6437 			COMMENT ON comment_type_any_name any_name IS comment_text
6438 				{
6439 					CommentStmt *n = makeNode(CommentStmt);
6440 					n->objtype = $3;
6441 					n->object = (Node *) $4;
6442 					n->comment = $6;
6443 					$$ = (Node *) n;
6444 				}
6445 			| COMMENT ON comment_type_name name IS comment_text
6446 				{
6447 					CommentStmt *n = makeNode(CommentStmt);
6448 					n->objtype = $3;
6449 					n->object = (Node *) makeString($4);
6450 					n->comment = $6;
6451 					$$ = (Node *) n;
6452 				}
6453 			| COMMENT ON TYPE_P Typename IS comment_text
6454 				{
6455 					CommentStmt *n = makeNode(CommentStmt);
6456 					n->objtype = OBJECT_TYPE;
6457 					n->object = (Node *) $4;
6458 					n->comment = $6;
6459 					$$ = (Node *) n;
6460 				}
6461 			| COMMENT ON DOMAIN_P Typename IS comment_text
6462 				{
6463 					CommentStmt *n = makeNode(CommentStmt);
6464 					n->objtype = OBJECT_DOMAIN;
6465 					n->object = (Node *) $4;
6466 					n->comment = $6;
6467 					$$ = (Node *) n;
6468 				}
6469 			| COMMENT ON AGGREGATE aggregate_with_argtypes IS comment_text
6470 				{
6471 					CommentStmt *n = makeNode(CommentStmt);
6472 					n->objtype = OBJECT_AGGREGATE;
6473 					n->object = (Node *) $4;
6474 					n->comment = $6;
6475 					$$ = (Node *) n;
6476 				}
6477 			| COMMENT ON FUNCTION function_with_argtypes IS comment_text
6478 				{
6479 					CommentStmt *n = makeNode(CommentStmt);
6480 					n->objtype = OBJECT_FUNCTION;
6481 					n->object = (Node *) $4;
6482 					n->comment = $6;
6483 					$$ = (Node *) n;
6484 				}
6485 			| COMMENT ON OPERATOR operator_with_argtypes IS comment_text
6486 				{
6487 					CommentStmt *n = makeNode(CommentStmt);
6488 					n->objtype = OBJECT_OPERATOR;
6489 					n->object = (Node *) $4;
6490 					n->comment = $6;
6491 					$$ = (Node *) n;
6492 				}
6493 			| COMMENT ON CONSTRAINT name ON any_name IS comment_text
6494 				{
6495 					CommentStmt *n = makeNode(CommentStmt);
6496 					n->objtype = OBJECT_TABCONSTRAINT;
6497 					n->object = (Node *) lappend($6, makeString($4));
6498 					n->comment = $8;
6499 					$$ = (Node *) n;
6500 				}
6501 			| COMMENT ON CONSTRAINT name ON DOMAIN_P any_name IS comment_text
6502 				{
6503 					CommentStmt *n = makeNode(CommentStmt);
6504 					n->objtype = OBJECT_DOMCONSTRAINT;
6505 					/*
6506 					 * should use Typename not any_name in the production, but
6507 					 * there's a shift/reduce conflict if we do that, so fix it
6508 					 * up here.
6509 					 */
6510 					n->object = (Node *) list_make2(makeTypeNameFromNameList($7), makeString($4));
6511 					n->comment = $9;
6512 					$$ = (Node *) n;
6513 				}
6514 			| COMMENT ON POLICY name ON any_name IS comment_text
6515 				{
6516 					CommentStmt *n = makeNode(CommentStmt);
6517 					n->objtype = OBJECT_POLICY;
6518 					n->object = (Node *) lappend($6, makeString($4));
6519 					n->comment = $8;
6520 					$$ = (Node *) n;
6521 				}
6522 			| COMMENT ON PROCEDURE function_with_argtypes IS comment_text
6523 				{
6524 					CommentStmt *n = makeNode(CommentStmt);
6525 					n->objtype = OBJECT_PROCEDURE;
6526 					n->object = (Node *) $4;
6527 					n->comment = $6;
6528 					$$ = (Node *) n;
6529 				}
6530 			| COMMENT ON ROUTINE function_with_argtypes IS comment_text
6531 				{
6532 					CommentStmt *n = makeNode(CommentStmt);
6533 					n->objtype = OBJECT_ROUTINE;
6534 					n->object = (Node *) $4;
6535 					n->comment = $6;
6536 					$$ = (Node *) n;
6537 				}
6538 			| COMMENT ON RULE name ON any_name IS comment_text
6539 				{
6540 					CommentStmt *n = makeNode(CommentStmt);
6541 					n->objtype = OBJECT_RULE;
6542 					n->object = (Node *) lappend($6, makeString($4));
6543 					n->comment = $8;
6544 					$$ = (Node *) n;
6545 				}
6546 			| COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
6547 				{
6548 					CommentStmt *n = makeNode(CommentStmt);
6549 					n->objtype = OBJECT_TRANSFORM;
6550 					n->object = (Node *) list_make2($5, makeString($7));
6551 					n->comment = $9;
6552 					$$ = (Node *) n;
6553 				}
6554 			| COMMENT ON TRIGGER name ON any_name IS comment_text
6555 				{
6556 					CommentStmt *n = makeNode(CommentStmt);
6557 					n->objtype = OBJECT_TRIGGER;
6558 					n->object = (Node *) lappend($6, makeString($4));
6559 					n->comment = $8;
6560 					$$ = (Node *) n;
6561 				}
6562 			| COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
6563 				{
6564 					CommentStmt *n = makeNode(CommentStmt);
6565 					n->objtype = OBJECT_OPCLASS;
6566 					n->object = (Node *) lcons(makeString($7), $5);
6567 					n->comment = $9;
6568 					$$ = (Node *) n;
6569 				}
6570 			| COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
6571 				{
6572 					CommentStmt *n = makeNode(CommentStmt);
6573 					n->objtype = OBJECT_OPFAMILY;
6574 					n->object = (Node *) lcons(makeString($7), $5);
6575 					n->comment = $9;
6576 					$$ = (Node *) n;
6577 				}
6578 			| COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
6579 				{
6580 					CommentStmt *n = makeNode(CommentStmt);
6581 					n->objtype = OBJECT_LARGEOBJECT;
6582 					n->object = (Node *) $5;
6583 					n->comment = $7;
6584 					$$ = (Node *) n;
6585 				}
6586 			| COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
6587 				{
6588 					CommentStmt *n = makeNode(CommentStmt);
6589 					n->objtype = OBJECT_CAST;
6590 					n->object = (Node *) list_make2($5, $7);
6591 					n->comment = $10;
6592 					$$ = (Node *) n;
6593 				}
6594 		;
6595 
6596 /* object types taking any_name */
6597 comment_type_any_name:
6598 			COLUMN								{ $$ = OBJECT_COLUMN; }
6599 			| INDEX								{ $$ = OBJECT_INDEX; }
6600 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6601 			| STATISTICS						{ $$ = OBJECT_STATISTIC_EXT; }
6602 			| TABLE								{ $$ = OBJECT_TABLE; }
6603 			| VIEW								{ $$ = OBJECT_VIEW; }
6604 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6605 			| COLLATION							{ $$ = OBJECT_COLLATION; }
6606 			| CONVERSION_P						{ $$ = OBJECT_CONVERSION; }
6607 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6608 			| TEXT_P SEARCH CONFIGURATION		{ $$ = OBJECT_TSCONFIGURATION; }
6609 			| TEXT_P SEARCH DICTIONARY			{ $$ = OBJECT_TSDICTIONARY; }
6610 			| TEXT_P SEARCH PARSER				{ $$ = OBJECT_TSPARSER; }
6611 			| TEXT_P SEARCH TEMPLATE			{ $$ = OBJECT_TSTEMPLATE; }
6612 		;
6613 
6614 /* object types taking name */
6615 comment_type_name:
6616 			ACCESS METHOD						{ $$ = OBJECT_ACCESS_METHOD; }
6617 			| DATABASE							{ $$ = OBJECT_DATABASE; }
6618 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6619 			| EXTENSION							{ $$ = OBJECT_EXTENSION; }
6620 			| FOREIGN DATA_P WRAPPER			{ $$ = OBJECT_FDW; }
6621 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6622 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6623 			| ROLE								{ $$ = OBJECT_ROLE; }
6624 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6625 			| SERVER							{ $$ = OBJECT_FOREIGN_SERVER; }
6626 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6627 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6628 		;
6629 
6630 comment_text:
6631 			Sconst								{ $$ = $1; }
6632 			| NULL_P							{ $$ = NULL; }
6633 		;
6634 
6635 
6636 /*****************************************************************************
6637  *
6638  *  SECURITY LABEL [FOR <provider>] ON <object> IS <label>
6639  *
6640  *  As with COMMENT ON, <object> can refer to various types of database
6641  *  objects (e.g. TABLE, COLUMN, etc.).
6642  *
6643  *****************************************************************************/
6644 
6645 SecLabelStmt:
6646 			SECURITY LABEL opt_provider ON security_label_type_any_name any_name
6647 			IS security_label
6648 				{
6649 					SecLabelStmt *n = makeNode(SecLabelStmt);
6650 					n->provider = $3;
6651 					n->objtype = $5;
6652 					n->object = (Node *) $6;
6653 					n->label = $8;
6654 					$$ = (Node *) n;
6655 				}
6656 			| SECURITY LABEL opt_provider ON security_label_type_name name
6657 			  IS security_label
6658 				{
6659 					SecLabelStmt *n = makeNode(SecLabelStmt);
6660 					n->provider = $3;
6661 					n->objtype = $5;
6662 					n->object = (Node *) makeString($6);
6663 					n->label = $8;
6664 					$$ = (Node *) n;
6665 				}
6666 			| SECURITY LABEL opt_provider ON TYPE_P Typename
6667 			  IS security_label
6668 				{
6669 					SecLabelStmt *n = makeNode(SecLabelStmt);
6670 					n->provider = $3;
6671 					n->objtype = OBJECT_TYPE;
6672 					n->object = (Node *) $6;
6673 					n->label = $8;
6674 					$$ = (Node *) n;
6675 				}
6676 			| SECURITY LABEL opt_provider ON DOMAIN_P Typename
6677 			  IS security_label
6678 				{
6679 					SecLabelStmt *n = makeNode(SecLabelStmt);
6680 					n->provider = $3;
6681 					n->objtype = OBJECT_DOMAIN;
6682 					n->object = (Node *) $6;
6683 					n->label = $8;
6684 					$$ = (Node *) n;
6685 				}
6686 			| SECURITY LABEL opt_provider ON AGGREGATE aggregate_with_argtypes
6687 			  IS security_label
6688 				{
6689 					SecLabelStmt *n = makeNode(SecLabelStmt);
6690 					n->provider = $3;
6691 					n->objtype = OBJECT_AGGREGATE;
6692 					n->object = (Node *) $6;
6693 					n->label = $8;
6694 					$$ = (Node *) n;
6695 				}
6696 			| SECURITY LABEL opt_provider ON FUNCTION function_with_argtypes
6697 			  IS security_label
6698 				{
6699 					SecLabelStmt *n = makeNode(SecLabelStmt);
6700 					n->provider = $3;
6701 					n->objtype = OBJECT_FUNCTION;
6702 					n->object = (Node *) $6;
6703 					n->label = $8;
6704 					$$ = (Node *) n;
6705 				}
6706 			| SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
6707 			  IS security_label
6708 				{
6709 					SecLabelStmt *n = makeNode(SecLabelStmt);
6710 					n->provider = $3;
6711 					n->objtype = OBJECT_LARGEOBJECT;
6712 					n->object = (Node *) $7;
6713 					n->label = $9;
6714 					$$ = (Node *) n;
6715 				}
6716 			| SECURITY LABEL opt_provider ON PROCEDURE function_with_argtypes
6717 			  IS security_label
6718 				{
6719 					SecLabelStmt *n = makeNode(SecLabelStmt);
6720 					n->provider = $3;
6721 					n->objtype = OBJECT_PROCEDURE;
6722 					n->object = (Node *) $6;
6723 					n->label = $8;
6724 					$$ = (Node *) n;
6725 				}
6726 			| SECURITY LABEL opt_provider ON ROUTINE function_with_argtypes
6727 			  IS security_label
6728 				{
6729 					SecLabelStmt *n = makeNode(SecLabelStmt);
6730 					n->provider = $3;
6731 					n->objtype = OBJECT_ROUTINE;
6732 					n->object = (Node *) $6;
6733 					n->label = $8;
6734 					$$ = (Node *) n;
6735 				}
6736 		;
6737 
6738 opt_provider:	FOR NonReservedWord_or_Sconst	{ $$ = $2; }
6739 				| /* empty */					{ $$ = NULL; }
6740 		;
6741 
6742 /* object types taking any_name */
6743 security_label_type_any_name:
6744 			COLUMN								{ $$ = OBJECT_COLUMN; }
6745 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6746 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6747 			| TABLE								{ $$ = OBJECT_TABLE; }
6748 			| VIEW								{ $$ = OBJECT_VIEW; }
6749 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6750 		;
6751 
6752 /* object types taking name */
6753 security_label_type_name:
6754 			DATABASE							{ $$ = OBJECT_DATABASE; }
6755 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6756 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6757 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6758 			| ROLE								{ $$ = OBJECT_ROLE; }
6759 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6760 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6761 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6762 		;
6763 
6764 security_label:	Sconst				{ $$ = $1; }
6765 				| NULL_P			{ $$ = NULL; }
6766 		;
6767 
6768 /*****************************************************************************
6769  *
6770  *		QUERY:
6771  *			fetch/move
6772  *
6773  *****************************************************************************/
6774 
6775 FetchStmt:	FETCH fetch_args
6776 				{
6777 					FetchStmt *n = (FetchStmt *) $2;
6778 					n->ismove = false;
6779 					$$ = (Node *)n;
6780 				}
6781 			| MOVE fetch_args
6782 				{
6783 					FetchStmt *n = (FetchStmt *) $2;
6784 					n->ismove = true;
6785 					$$ = (Node *)n;
6786 				}
6787 		;
6788 
6789 fetch_args:	cursor_name
6790 				{
6791 					FetchStmt *n = makeNode(FetchStmt);
6792 					n->portalname = $1;
6793 					n->direction = FETCH_FORWARD;
6794 					n->howMany = 1;
6795 					$$ = (Node *)n;
6796 				}
6797 			| from_in cursor_name
6798 				{
6799 					FetchStmt *n = makeNode(FetchStmt);
6800 					n->portalname = $2;
6801 					n->direction = FETCH_FORWARD;
6802 					n->howMany = 1;
6803 					$$ = (Node *)n;
6804 				}
6805 			| NEXT opt_from_in cursor_name
6806 				{
6807 					FetchStmt *n = makeNode(FetchStmt);
6808 					n->portalname = $3;
6809 					n->direction = FETCH_FORWARD;
6810 					n->howMany = 1;
6811 					$$ = (Node *)n;
6812 				}
6813 			| PRIOR opt_from_in cursor_name
6814 				{
6815 					FetchStmt *n = makeNode(FetchStmt);
6816 					n->portalname = $3;
6817 					n->direction = FETCH_BACKWARD;
6818 					n->howMany = 1;
6819 					$$ = (Node *)n;
6820 				}
6821 			| FIRST_P opt_from_in cursor_name
6822 				{
6823 					FetchStmt *n = makeNode(FetchStmt);
6824 					n->portalname = $3;
6825 					n->direction = FETCH_ABSOLUTE;
6826 					n->howMany = 1;
6827 					$$ = (Node *)n;
6828 				}
6829 			| LAST_P opt_from_in cursor_name
6830 				{
6831 					FetchStmt *n = makeNode(FetchStmt);
6832 					n->portalname = $3;
6833 					n->direction = FETCH_ABSOLUTE;
6834 					n->howMany = -1;
6835 					$$ = (Node *)n;
6836 				}
6837 			| ABSOLUTE_P SignedIconst opt_from_in cursor_name
6838 				{
6839 					FetchStmt *n = makeNode(FetchStmt);
6840 					n->portalname = $4;
6841 					n->direction = FETCH_ABSOLUTE;
6842 					n->howMany = $2;
6843 					$$ = (Node *)n;
6844 				}
6845 			| RELATIVE_P SignedIconst opt_from_in cursor_name
6846 				{
6847 					FetchStmt *n = makeNode(FetchStmt);
6848 					n->portalname = $4;
6849 					n->direction = FETCH_RELATIVE;
6850 					n->howMany = $2;
6851 					$$ = (Node *)n;
6852 				}
6853 			| SignedIconst opt_from_in cursor_name
6854 				{
6855 					FetchStmt *n = makeNode(FetchStmt);
6856 					n->portalname = $3;
6857 					n->direction = FETCH_FORWARD;
6858 					n->howMany = $1;
6859 					$$ = (Node *)n;
6860 				}
6861 			| ALL opt_from_in cursor_name
6862 				{
6863 					FetchStmt *n = makeNode(FetchStmt);
6864 					n->portalname = $3;
6865 					n->direction = FETCH_FORWARD;
6866 					n->howMany = FETCH_ALL;
6867 					$$ = (Node *)n;
6868 				}
6869 			| FORWARD opt_from_in cursor_name
6870 				{
6871 					FetchStmt *n = makeNode(FetchStmt);
6872 					n->portalname = $3;
6873 					n->direction = FETCH_FORWARD;
6874 					n->howMany = 1;
6875 					$$ = (Node *)n;
6876 				}
6877 			| FORWARD SignedIconst opt_from_in cursor_name
6878 				{
6879 					FetchStmt *n = makeNode(FetchStmt);
6880 					n->portalname = $4;
6881 					n->direction = FETCH_FORWARD;
6882 					n->howMany = $2;
6883 					$$ = (Node *)n;
6884 				}
6885 			| FORWARD ALL opt_from_in cursor_name
6886 				{
6887 					FetchStmt *n = makeNode(FetchStmt);
6888 					n->portalname = $4;
6889 					n->direction = FETCH_FORWARD;
6890 					n->howMany = FETCH_ALL;
6891 					$$ = (Node *)n;
6892 				}
6893 			| BACKWARD opt_from_in cursor_name
6894 				{
6895 					FetchStmt *n = makeNode(FetchStmt);
6896 					n->portalname = $3;
6897 					n->direction = FETCH_BACKWARD;
6898 					n->howMany = 1;
6899 					$$ = (Node *)n;
6900 				}
6901 			| BACKWARD SignedIconst opt_from_in cursor_name
6902 				{
6903 					FetchStmt *n = makeNode(FetchStmt);
6904 					n->portalname = $4;
6905 					n->direction = FETCH_BACKWARD;
6906 					n->howMany = $2;
6907 					$$ = (Node *)n;
6908 				}
6909 			| BACKWARD ALL opt_from_in cursor_name
6910 				{
6911 					FetchStmt *n = makeNode(FetchStmt);
6912 					n->portalname = $4;
6913 					n->direction = FETCH_BACKWARD;
6914 					n->howMany = FETCH_ALL;
6915 					$$ = (Node *)n;
6916 				}
6917 		;
6918 
6919 from_in:	FROM									{}
6920 			| IN_P									{}
6921 		;
6922 
6923 opt_from_in:	from_in								{}
6924 			| /* EMPTY */							{}
6925 		;
6926 
6927 
6928 /*****************************************************************************
6929  *
6930  * GRANT and REVOKE statements
6931  *
6932  *****************************************************************************/
6933 
6934 GrantStmt:	GRANT privileges ON privilege_target TO grantee_list
6935 			opt_grant_grant_option
6936 				{
6937 					GrantStmt *n = makeNode(GrantStmt);
6938 					n->is_grant = true;
6939 					n->privileges = $2;
6940 					n->targtype = ($4)->targtype;
6941 					n->objtype = ($4)->objtype;
6942 					n->objects = ($4)->objs;
6943 					n->grantees = $6;
6944 					n->grant_option = $7;
6945 					$$ = (Node*)n;
6946 				}
6947 		;
6948 
6949 RevokeStmt:
6950 			REVOKE privileges ON privilege_target
6951 			FROM grantee_list opt_drop_behavior
6952 				{
6953 					GrantStmt *n = makeNode(GrantStmt);
6954 					n->is_grant = false;
6955 					n->grant_option = false;
6956 					n->privileges = $2;
6957 					n->targtype = ($4)->targtype;
6958 					n->objtype = ($4)->objtype;
6959 					n->objects = ($4)->objs;
6960 					n->grantees = $6;
6961 					n->behavior = $7;
6962 					$$ = (Node *)n;
6963 				}
6964 			| REVOKE GRANT OPTION FOR privileges ON privilege_target
6965 			FROM grantee_list opt_drop_behavior
6966 				{
6967 					GrantStmt *n = makeNode(GrantStmt);
6968 					n->is_grant = false;
6969 					n->grant_option = true;
6970 					n->privileges = $5;
6971 					n->targtype = ($7)->targtype;
6972 					n->objtype = ($7)->objtype;
6973 					n->objects = ($7)->objs;
6974 					n->grantees = $9;
6975 					n->behavior = $10;
6976 					$$ = (Node *)n;
6977 				}
6978 		;
6979 
6980 
6981 /*
6982  * Privilege names are represented as strings; the validity of the privilege
6983  * names gets checked at execution.  This is a bit annoying but we have little
6984  * choice because of the syntactic conflict with lists of role names in
6985  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
6986  * production any reserved keywords that need to be usable as privilege names.
6987  */
6988 
6989 /* either ALL [PRIVILEGES] or a list of individual privileges */
6990 privileges: privilege_list
6991 				{ $$ = $1; }
6992 			| ALL
6993 				{ $$ = NIL; }
6994 			| ALL PRIVILEGES
6995 				{ $$ = NIL; }
6996 			| ALL '(' columnList ')'
6997 				{
6998 					AccessPriv *n = makeNode(AccessPriv);
6999 					n->priv_name = NULL;
7000 					n->cols = $3;
7001 					$$ = list_make1(n);
7002 				}
7003 			| ALL PRIVILEGES '(' columnList ')'
7004 				{
7005 					AccessPriv *n = makeNode(AccessPriv);
7006 					n->priv_name = NULL;
7007 					n->cols = $4;
7008 					$$ = list_make1(n);
7009 				}
7010 		;
7011 
7012 privilege_list:	privilege							{ $$ = list_make1($1); }
7013 			| privilege_list ',' privilege			{ $$ = lappend($1, $3); }
7014 		;
7015 
7016 privilege:	SELECT opt_column_list
7017 			{
7018 				AccessPriv *n = makeNode(AccessPriv);
7019 				n->priv_name = pstrdup($1);
7020 				n->cols = $2;
7021 				$$ = n;
7022 			}
7023 		| REFERENCES opt_column_list
7024 			{
7025 				AccessPriv *n = makeNode(AccessPriv);
7026 				n->priv_name = pstrdup($1);
7027 				n->cols = $2;
7028 				$$ = n;
7029 			}
7030 		| CREATE opt_column_list
7031 			{
7032 				AccessPriv *n = makeNode(AccessPriv);
7033 				n->priv_name = pstrdup($1);
7034 				n->cols = $2;
7035 				$$ = n;
7036 			}
7037 		| ColId opt_column_list
7038 			{
7039 				AccessPriv *n = makeNode(AccessPriv);
7040 				n->priv_name = $1;
7041 				n->cols = $2;
7042 				$$ = n;
7043 			}
7044 		;
7045 
7046 
7047 /* Don't bother trying to fold the first two rules into one using
7048  * opt_table.  You're going to get conflicts.
7049  */
7050 privilege_target:
7051 			qualified_name_list
7052 				{
7053 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7054 					n->targtype = ACL_TARGET_OBJECT;
7055 					n->objtype = OBJECT_TABLE;
7056 					n->objs = $1;
7057 					$$ = n;
7058 				}
7059 			| TABLE qualified_name_list
7060 				{
7061 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7062 					n->targtype = ACL_TARGET_OBJECT;
7063 					n->objtype = OBJECT_TABLE;
7064 					n->objs = $2;
7065 					$$ = n;
7066 				}
7067 			| SEQUENCE qualified_name_list
7068 				{
7069 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7070 					n->targtype = ACL_TARGET_OBJECT;
7071 					n->objtype = OBJECT_SEQUENCE;
7072 					n->objs = $2;
7073 					$$ = n;
7074 				}
7075 			| FOREIGN DATA_P WRAPPER name_list
7076 				{
7077 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7078 					n->targtype = ACL_TARGET_OBJECT;
7079 					n->objtype = OBJECT_FDW;
7080 					n->objs = $4;
7081 					$$ = n;
7082 				}
7083 			| FOREIGN SERVER name_list
7084 				{
7085 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7086 					n->targtype = ACL_TARGET_OBJECT;
7087 					n->objtype = OBJECT_FOREIGN_SERVER;
7088 					n->objs = $3;
7089 					$$ = n;
7090 				}
7091 			| FUNCTION function_with_argtypes_list
7092 				{
7093 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7094 					n->targtype = ACL_TARGET_OBJECT;
7095 					n->objtype = OBJECT_FUNCTION;
7096 					n->objs = $2;
7097 					$$ = n;
7098 				}
7099 			| PROCEDURE function_with_argtypes_list
7100 				{
7101 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7102 					n->targtype = ACL_TARGET_OBJECT;
7103 					n->objtype = OBJECT_PROCEDURE;
7104 					n->objs = $2;
7105 					$$ = n;
7106 				}
7107 			| ROUTINE function_with_argtypes_list
7108 				{
7109 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7110 					n->targtype = ACL_TARGET_OBJECT;
7111 					n->objtype = OBJECT_ROUTINE;
7112 					n->objs = $2;
7113 					$$ = n;
7114 				}
7115 			| DATABASE name_list
7116 				{
7117 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7118 					n->targtype = ACL_TARGET_OBJECT;
7119 					n->objtype = OBJECT_DATABASE;
7120 					n->objs = $2;
7121 					$$ = n;
7122 				}
7123 			| DOMAIN_P any_name_list
7124 				{
7125 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7126 					n->targtype = ACL_TARGET_OBJECT;
7127 					n->objtype = OBJECT_DOMAIN;
7128 					n->objs = $2;
7129 					$$ = n;
7130 				}
7131 			| LANGUAGE name_list
7132 				{
7133 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7134 					n->targtype = ACL_TARGET_OBJECT;
7135 					n->objtype = OBJECT_LANGUAGE;
7136 					n->objs = $2;
7137 					$$ = n;
7138 				}
7139 			| LARGE_P OBJECT_P NumericOnly_list
7140 				{
7141 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7142 					n->targtype = ACL_TARGET_OBJECT;
7143 					n->objtype = OBJECT_LARGEOBJECT;
7144 					n->objs = $3;
7145 					$$ = n;
7146 				}
7147 			| SCHEMA name_list
7148 				{
7149 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7150 					n->targtype = ACL_TARGET_OBJECT;
7151 					n->objtype = OBJECT_SCHEMA;
7152 					n->objs = $2;
7153 					$$ = n;
7154 				}
7155 			| TABLESPACE name_list
7156 				{
7157 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7158 					n->targtype = ACL_TARGET_OBJECT;
7159 					n->objtype = OBJECT_TABLESPACE;
7160 					n->objs = $2;
7161 					$$ = n;
7162 				}
7163 			| TYPE_P any_name_list
7164 				{
7165 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7166 					n->targtype = ACL_TARGET_OBJECT;
7167 					n->objtype = OBJECT_TYPE;
7168 					n->objs = $2;
7169 					$$ = n;
7170 				}
7171 			| ALL TABLES IN_P SCHEMA name_list
7172 				{
7173 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7174 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7175 					n->objtype = OBJECT_TABLE;
7176 					n->objs = $5;
7177 					$$ = n;
7178 				}
7179 			| ALL SEQUENCES IN_P SCHEMA name_list
7180 				{
7181 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7182 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7183 					n->objtype = OBJECT_SEQUENCE;
7184 					n->objs = $5;
7185 					$$ = n;
7186 				}
7187 			| ALL FUNCTIONS IN_P SCHEMA name_list
7188 				{
7189 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7190 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7191 					n->objtype = OBJECT_FUNCTION;
7192 					n->objs = $5;
7193 					$$ = n;
7194 				}
7195 			| ALL PROCEDURES IN_P SCHEMA name_list
7196 				{
7197 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7198 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7199 					n->objtype = OBJECT_PROCEDURE;
7200 					n->objs = $5;
7201 					$$ = n;
7202 				}
7203 			| ALL ROUTINES IN_P SCHEMA name_list
7204 				{
7205 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7206 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7207 					n->objtype = OBJECT_ROUTINE;
7208 					n->objs = $5;
7209 					$$ = n;
7210 				}
7211 		;
7212 
7213 
7214 grantee_list:
7215 			grantee									{ $$ = list_make1($1); }
7216 			| grantee_list ',' grantee				{ $$ = lappend($1, $3); }
7217 		;
7218 
7219 grantee:
7220 			RoleSpec								{ $$ = $1; }
7221 			| GROUP_P RoleSpec						{ $$ = $2; }
7222 		;
7223 
7224 
7225 opt_grant_grant_option:
7226 			WITH GRANT OPTION { $$ = true; }
7227 			| /*EMPTY*/ { $$ = false; }
7228 		;
7229 
7230 /*****************************************************************************
7231  *
7232  * GRANT and REVOKE ROLE statements
7233  *
7234  *****************************************************************************/
7235 
7236 GrantRoleStmt:
7237 			GRANT privilege_list TO role_list opt_grant_admin_option opt_granted_by
7238 				{
7239 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7240 					n->is_grant = true;
7241 					n->granted_roles = $2;
7242 					n->grantee_roles = $4;
7243 					n->admin_opt = $5;
7244 					n->grantor = $6;
7245 					$$ = (Node*)n;
7246 				}
7247 		;
7248 
7249 RevokeRoleStmt:
7250 			REVOKE privilege_list FROM role_list opt_granted_by opt_drop_behavior
7251 				{
7252 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7253 					n->is_grant = false;
7254 					n->admin_opt = false;
7255 					n->granted_roles = $2;
7256 					n->grantee_roles = $4;
7257 					n->behavior = $6;
7258 					$$ = (Node*)n;
7259 				}
7260 			| REVOKE ADMIN OPTION FOR privilege_list FROM role_list opt_granted_by opt_drop_behavior
7261 				{
7262 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7263 					n->is_grant = false;
7264 					n->admin_opt = true;
7265 					n->granted_roles = $5;
7266 					n->grantee_roles = $7;
7267 					n->behavior = $9;
7268 					$$ = (Node*)n;
7269 				}
7270 		;
7271 
7272 opt_grant_admin_option: WITH ADMIN OPTION				{ $$ = true; }
7273 			| /*EMPTY*/									{ $$ = false; }
7274 		;
7275 
7276 opt_granted_by: GRANTED BY RoleSpec						{ $$ = $3; }
7277 			| /*EMPTY*/									{ $$ = NULL; }
7278 		;
7279 
7280 /*****************************************************************************
7281  *
7282  * ALTER DEFAULT PRIVILEGES statement
7283  *
7284  *****************************************************************************/
7285 
7286 AlterDefaultPrivilegesStmt:
7287 			ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
7288 				{
7289 					AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
7290 					n->options = $4;
7291 					n->action = (GrantStmt *) $5;
7292 					$$ = (Node*)n;
7293 				}
7294 		;
7295 
7296 DefACLOptionList:
7297 			DefACLOptionList DefACLOption			{ $$ = lappend($1, $2); }
7298 			| /* EMPTY */							{ $$ = NIL; }
7299 		;
7300 
7301 DefACLOption:
7302 			IN_P SCHEMA name_list
7303 				{
7304 					$$ = makeDefElem("schemas", (Node *)$3, @1);
7305 				}
7306 			| FOR ROLE role_list
7307 				{
7308 					$$ = makeDefElem("roles", (Node *)$3, @1);
7309 				}
7310 			| FOR USER role_list
7311 				{
7312 					$$ = makeDefElem("roles", (Node *)$3, @1);
7313 				}
7314 		;
7315 
7316 /*
7317  * This should match GRANT/REVOKE, except that individual target objects
7318  * are not mentioned and we only allow a subset of object types.
7319  */
7320 DefACLAction:
7321 			GRANT privileges ON defacl_privilege_target TO grantee_list
7322 			opt_grant_grant_option
7323 				{
7324 					GrantStmt *n = makeNode(GrantStmt);
7325 					n->is_grant = true;
7326 					n->privileges = $2;
7327 					n->targtype = ACL_TARGET_DEFAULTS;
7328 					n->objtype = $4;
7329 					n->objects = NIL;
7330 					n->grantees = $6;
7331 					n->grant_option = $7;
7332 					$$ = (Node*)n;
7333 				}
7334 			| REVOKE privileges ON defacl_privilege_target
7335 			FROM grantee_list opt_drop_behavior
7336 				{
7337 					GrantStmt *n = makeNode(GrantStmt);
7338 					n->is_grant = false;
7339 					n->grant_option = false;
7340 					n->privileges = $2;
7341 					n->targtype = ACL_TARGET_DEFAULTS;
7342 					n->objtype = $4;
7343 					n->objects = NIL;
7344 					n->grantees = $6;
7345 					n->behavior = $7;
7346 					$$ = (Node *)n;
7347 				}
7348 			| REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
7349 			FROM grantee_list opt_drop_behavior
7350 				{
7351 					GrantStmt *n = makeNode(GrantStmt);
7352 					n->is_grant = false;
7353 					n->grant_option = true;
7354 					n->privileges = $5;
7355 					n->targtype = ACL_TARGET_DEFAULTS;
7356 					n->objtype = $7;
7357 					n->objects = NIL;
7358 					n->grantees = $9;
7359 					n->behavior = $10;
7360 					$$ = (Node *)n;
7361 				}
7362 		;
7363 
7364 defacl_privilege_target:
7365 			TABLES			{ $$ = OBJECT_TABLE; }
7366 			| FUNCTIONS		{ $$ = OBJECT_FUNCTION; }
7367 			| ROUTINES		{ $$ = OBJECT_FUNCTION; }
7368 			| SEQUENCES		{ $$ = OBJECT_SEQUENCE; }
7369 			| TYPES_P		{ $$ = OBJECT_TYPE; }
7370 			| SCHEMAS		{ $$ = OBJECT_SCHEMA; }
7371 		;
7372 
7373 
7374 /*****************************************************************************
7375  *
7376  *		QUERY: CREATE INDEX
7377  *
7378  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
7379  * willing to make TABLESPACE a fully reserved word.
7380  *****************************************************************************/
7381 
7382 IndexStmt:	CREATE opt_unique INDEX opt_concurrently opt_index_name
7383 			ON relation_expr access_method_clause '(' index_params ')'
7384 			opt_include opt_reloptions OptTableSpace where_clause
7385 				{
7386 					IndexStmt *n = makeNode(IndexStmt);
7387 					n->unique = $2;
7388 					n->concurrent = $4;
7389 					n->idxname = $5;
7390 					n->relation = $7;
7391 					n->relationId = InvalidOid;
7392 					n->accessMethod = $8;
7393 					n->indexParams = $10;
7394 					n->indexIncludingParams = $12;
7395 					n->options = $13;
7396 					n->tableSpace = $14;
7397 					n->whereClause = $15;
7398 					n->excludeOpNames = NIL;
7399 					n->idxcomment = NULL;
7400 					n->indexOid = InvalidOid;
7401 					n->oldNode = InvalidOid;
7402 					n->primary = false;
7403 					n->isconstraint = false;
7404 					n->deferrable = false;
7405 					n->initdeferred = false;
7406 					n->transformed = false;
7407 					n->if_not_exists = false;
7408 					$$ = (Node *)n;
7409 				}
7410 			| CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS index_name
7411 			ON relation_expr access_method_clause '(' index_params ')'
7412 			opt_include opt_reloptions OptTableSpace where_clause
7413 				{
7414 					IndexStmt *n = makeNode(IndexStmt);
7415 					n->unique = $2;
7416 					n->concurrent = $4;
7417 					n->idxname = $8;
7418 					n->relation = $10;
7419 					n->relationId = InvalidOid;
7420 					n->accessMethod = $11;
7421 					n->indexParams = $13;
7422 					n->indexIncludingParams = $15;
7423 					n->options = $16;
7424 					n->tableSpace = $17;
7425 					n->whereClause = $18;
7426 					n->excludeOpNames = NIL;
7427 					n->idxcomment = NULL;
7428 					n->indexOid = InvalidOid;
7429 					n->oldNode = InvalidOid;
7430 					n->primary = false;
7431 					n->isconstraint = false;
7432 					n->deferrable = false;
7433 					n->initdeferred = false;
7434 					n->transformed = false;
7435 					n->if_not_exists = true;
7436 					$$ = (Node *)n;
7437 				}
7438 		;
7439 
7440 opt_unique:
7441 			UNIQUE									{ $$ = true; }
7442 			| /*EMPTY*/								{ $$ = false; }
7443 		;
7444 
7445 opt_concurrently:
7446 			CONCURRENTLY							{ $$ = true; }
7447 			| /*EMPTY*/								{ $$ = false; }
7448 		;
7449 
7450 opt_index_name:
7451 			index_name								{ $$ = $1; }
7452 			| /*EMPTY*/								{ $$ = NULL; }
7453 		;
7454 
7455 access_method_clause:
7456 			USING access_method						{ $$ = $2; }
7457 			| /*EMPTY*/								{ $$ = DEFAULT_INDEX_TYPE; }
7458 		;
7459 
7460 index_params:	index_elem							{ $$ = list_make1($1); }
7461 			| index_params ',' index_elem			{ $$ = lappend($1, $3); }
7462 		;
7463 
7464 /*
7465  * Index attributes can be either simple column references, or arbitrary
7466  * expressions in parens.  For backwards-compatibility reasons, we allow
7467  * an expression that's just a function call to be written without parens.
7468  */
7469 index_elem:	ColId opt_collate opt_class opt_asc_desc opt_nulls_order
7470 				{
7471 					$$ = makeNode(IndexElem);
7472 					$$->name = $1;
7473 					$$->expr = NULL;
7474 					$$->indexcolname = NULL;
7475 					$$->collation = $2;
7476 					$$->opclass = $3;
7477 					$$->ordering = $4;
7478 					$$->nulls_ordering = $5;
7479 				}
7480 			| func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order
7481 				{
7482 					$$ = makeNode(IndexElem);
7483 					$$->name = NULL;
7484 					$$->expr = $1;
7485 					$$->indexcolname = NULL;
7486 					$$->collation = $2;
7487 					$$->opclass = $3;
7488 					$$->ordering = $4;
7489 					$$->nulls_ordering = $5;
7490 				}
7491 			| '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order
7492 				{
7493 					$$ = makeNode(IndexElem);
7494 					$$->name = NULL;
7495 					$$->expr = $2;
7496 					$$->indexcolname = NULL;
7497 					$$->collation = $4;
7498 					$$->opclass = $5;
7499 					$$->ordering = $6;
7500 					$$->nulls_ordering = $7;
7501 				}
7502 		;
7503 
7504 opt_include:		INCLUDE '(' index_including_params ')'			{ $$ = $3; }
7505 			 |		/* EMPTY */						{ $$ = NIL; }
7506 		;
7507 
7508 index_including_params:	index_elem						{ $$ = list_make1($1); }
7509 			| index_including_params ',' index_elem		{ $$ = lappend($1, $3); }
7510 		;
7511 
7512 opt_collate: COLLATE any_name						{ $$ = $2; }
7513 			| /*EMPTY*/								{ $$ = NIL; }
7514 		;
7515 
7516 opt_class:	any_name								{ $$ = $1; }
7517 			| /*EMPTY*/								{ $$ = NIL; }
7518 		;
7519 
7520 opt_asc_desc: ASC							{ $$ = SORTBY_ASC; }
7521 			| DESC							{ $$ = SORTBY_DESC; }
7522 			| /*EMPTY*/						{ $$ = SORTBY_DEFAULT; }
7523 		;
7524 
7525 opt_nulls_order: NULLS_LA FIRST_P			{ $$ = SORTBY_NULLS_FIRST; }
7526 			| NULLS_LA LAST_P				{ $$ = SORTBY_NULLS_LAST; }
7527 			| /*EMPTY*/						{ $$ = SORTBY_NULLS_DEFAULT; }
7528 		;
7529 
7530 
7531 /*****************************************************************************
7532  *
7533  *		QUERY:
7534  *				create [or replace] function <fname>
7535  *						[(<type-1> { , <type-n>})]
7536  *						returns <type-r>
7537  *						as <filename or code in language as appropriate>
7538  *						language <lang> [with parameters]
7539  *
7540  *****************************************************************************/
7541 
7542 CreateFunctionStmt:
7543 			CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7544 			RETURNS func_return createfunc_opt_list
7545 				{
7546 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7547 					n->is_procedure = false;
7548 					n->replace = $2;
7549 					n->funcname = $4;
7550 					n->parameters = $5;
7551 					n->returnType = $7;
7552 					n->options = $8;
7553 					$$ = (Node *)n;
7554 				}
7555 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7556 			  RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list
7557 				{
7558 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7559 					n->is_procedure = false;
7560 					n->replace = $2;
7561 					n->funcname = $4;
7562 					n->parameters = mergeTableFuncParameters($5, $9);
7563 					n->returnType = TableFuncTypeName($9);
7564 					n->returnType->location = @7;
7565 					n->options = $11;
7566 					$$ = (Node *)n;
7567 				}
7568 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7569 			  createfunc_opt_list
7570 				{
7571 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7572 					n->is_procedure = false;
7573 					n->replace = $2;
7574 					n->funcname = $4;
7575 					n->parameters = $5;
7576 					n->returnType = NULL;
7577 					n->options = $6;
7578 					$$ = (Node *)n;
7579 				}
7580 			| CREATE opt_or_replace PROCEDURE func_name func_args_with_defaults
7581 			  createfunc_opt_list
7582 				{
7583 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7584 					n->is_procedure = true;
7585 					n->replace = $2;
7586 					n->funcname = $4;
7587 					n->parameters = $5;
7588 					n->returnType = NULL;
7589 					n->options = $6;
7590 					$$ = (Node *)n;
7591 				}
7592 		;
7593 
7594 opt_or_replace:
7595 			OR REPLACE								{ $$ = true; }
7596 			| /*EMPTY*/								{ $$ = false; }
7597 		;
7598 
7599 func_args:	'(' func_args_list ')'					{ $$ = $2; }
7600 			| '(' ')'								{ $$ = NIL; }
7601 		;
7602 
7603 func_args_list:
7604 			func_arg								{ $$ = list_make1($1); }
7605 			| func_args_list ',' func_arg			{ $$ = lappend($1, $3); }
7606 		;
7607 
7608 function_with_argtypes_list:
7609 			function_with_argtypes					{ $$ = list_make1($1); }
7610 			| function_with_argtypes_list ',' function_with_argtypes
7611 													{ $$ = lappend($1, $3); }
7612 		;
7613 
7614 function_with_argtypes:
7615 			func_name func_args
7616 				{
7617 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7618 					n->objname = $1;
7619 					n->objargs = extractArgTypes($2);
7620 					$$ = n;
7621 				}
7622 			/*
7623 			 * Because of reduce/reduce conflicts, we can't use func_name
7624 			 * below, but we can write it out the long way, which actually
7625 			 * allows more cases.
7626 			 */
7627 			| type_func_name_keyword
7628 				{
7629 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7630 					n->objname = list_make1(makeString(pstrdup($1)));
7631 					n->args_unspecified = true;
7632 					$$ = n;
7633 				}
7634 			| ColId
7635 				{
7636 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7637 					n->objname = list_make1(makeString($1));
7638 					n->args_unspecified = true;
7639 					$$ = n;
7640 				}
7641 			| ColId indirection
7642 				{
7643 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7644 					n->objname = check_func_name(lcons(makeString($1), $2),
7645 												  yyscanner);
7646 					n->args_unspecified = true;
7647 					$$ = n;
7648 				}
7649 		;
7650 
7651 /*
7652  * func_args_with_defaults is separate because we only want to accept
7653  * defaults in CREATE FUNCTION, not in ALTER etc.
7654  */
7655 func_args_with_defaults:
7656 		'(' func_args_with_defaults_list ')'		{ $$ = $2; }
7657 		| '(' ')'									{ $$ = NIL; }
7658 		;
7659 
7660 func_args_with_defaults_list:
7661 		func_arg_with_default						{ $$ = list_make1($1); }
7662 		| func_args_with_defaults_list ',' func_arg_with_default
7663 													{ $$ = lappend($1, $3); }
7664 		;
7665 
7666 /*
7667  * The style with arg_class first is SQL99 standard, but Oracle puts
7668  * param_name first; accept both since it's likely people will try both
7669  * anyway.  Don't bother trying to save productions by letting arg_class
7670  * have an empty alternative ... you'll get shift/reduce conflicts.
7671  *
7672  * We can catch over-specified arguments here if we want to,
7673  * but for now better to silently swallow typmod, etc.
7674  * - thomas 2000-03-22
7675  */
7676 func_arg:
7677 			arg_class param_name func_type
7678 				{
7679 					FunctionParameter *n = makeNode(FunctionParameter);
7680 					n->name = $2;
7681 					n->argType = $3;
7682 					n->mode = $1;
7683 					n->defexpr = NULL;
7684 					$$ = n;
7685 				}
7686 			| param_name arg_class func_type
7687 				{
7688 					FunctionParameter *n = makeNode(FunctionParameter);
7689 					n->name = $1;
7690 					n->argType = $3;
7691 					n->mode = $2;
7692 					n->defexpr = NULL;
7693 					$$ = n;
7694 				}
7695 			| param_name func_type
7696 				{
7697 					FunctionParameter *n = makeNode(FunctionParameter);
7698 					n->name = $1;
7699 					n->argType = $2;
7700 					n->mode = FUNC_PARAM_IN;
7701 					n->defexpr = NULL;
7702 					$$ = n;
7703 				}
7704 			| arg_class func_type
7705 				{
7706 					FunctionParameter *n = makeNode(FunctionParameter);
7707 					n->name = NULL;
7708 					n->argType = $2;
7709 					n->mode = $1;
7710 					n->defexpr = NULL;
7711 					$$ = n;
7712 				}
7713 			| func_type
7714 				{
7715 					FunctionParameter *n = makeNode(FunctionParameter);
7716 					n->name = NULL;
7717 					n->argType = $1;
7718 					n->mode = FUNC_PARAM_IN;
7719 					n->defexpr = NULL;
7720 					$$ = n;
7721 				}
7722 		;
7723 
7724 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
7725 arg_class:	IN_P								{ $$ = FUNC_PARAM_IN; }
7726 			| OUT_P								{ $$ = FUNC_PARAM_OUT; }
7727 			| INOUT								{ $$ = FUNC_PARAM_INOUT; }
7728 			| IN_P OUT_P						{ $$ = FUNC_PARAM_INOUT; }
7729 			| VARIADIC							{ $$ = FUNC_PARAM_VARIADIC; }
7730 		;
7731 
7732 /*
7733  * Ideally param_name should be ColId, but that causes too many conflicts.
7734  */
7735 param_name:	type_function_name
7736 		;
7737 
7738 func_return:
7739 			func_type
7740 				{
7741 					/* We can catch over-specified results here if we want to,
7742 					 * but for now better to silently swallow typmod, etc.
7743 					 * - thomas 2000-03-22
7744 					 */
7745 					$$ = $1;
7746 				}
7747 		;
7748 
7749 /*
7750  * We would like to make the %TYPE productions here be ColId attrs etc,
7751  * but that causes reduce/reduce conflicts.  type_function_name
7752  * is next best choice.
7753  */
7754 func_type:	Typename								{ $$ = $1; }
7755 			| type_function_name attrs '%' TYPE_P
7756 				{
7757 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7758 					$$->pct_type = true;
7759 					$$->location = @1;
7760 				}
7761 			| SETOF type_function_name attrs '%' TYPE_P
7762 				{
7763 					$$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
7764 					$$->pct_type = true;
7765 					$$->setof = true;
7766 					$$->location = @2;
7767 				}
7768 		;
7769 
7770 func_arg_with_default:
7771 		func_arg
7772 				{
7773 					$$ = $1;
7774 				}
7775 		| func_arg DEFAULT a_expr
7776 				{
7777 					$$ = $1;
7778 					$$->defexpr = $3;
7779 				}
7780 		| func_arg '=' a_expr
7781 				{
7782 					$$ = $1;
7783 					$$->defexpr = $3;
7784 				}
7785 		;
7786 
7787 /* Aggregate args can be most things that function args can be */
7788 aggr_arg:	func_arg
7789 				{
7790 					if (!($1->mode == FUNC_PARAM_IN ||
7791 						  $1->mode == FUNC_PARAM_VARIADIC))
7792 						ereport(ERROR,
7793 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7794 								 errmsg("aggregates cannot have output arguments"),
7795 								 parser_errposition(@1)));
7796 					$$ = $1;
7797 				}
7798 		;
7799 
7800 /*
7801  * The SQL standard offers no guidance on how to declare aggregate argument
7802  * lists, since it doesn't have CREATE AGGREGATE etc.  We accept these cases:
7803  *
7804  * (*)									- normal agg with no args
7805  * (aggr_arg,...)						- normal agg with args
7806  * (ORDER BY aggr_arg,...)				- ordered-set agg with no direct args
7807  * (aggr_arg,... ORDER BY aggr_arg,...)	- ordered-set agg with direct args
7808  *
7809  * The zero-argument case is spelled with '*' for consistency with COUNT(*).
7810  *
7811  * An additional restriction is that if the direct-args list ends in a
7812  * VARIADIC item, the ordered-args list must contain exactly one item that
7813  * is also VARIADIC with the same type.  This allows us to collapse the two
7814  * VARIADIC items into one, which is necessary to represent the aggregate in
7815  * pg_proc.  We check this at the grammar stage so that we can return a list
7816  * in which the second VARIADIC item is already discarded, avoiding extra work
7817  * in cases such as DROP AGGREGATE.
7818  *
7819  * The return value of this production is a two-element list, in which the
7820  * first item is a sublist of FunctionParameter nodes (with any duplicate
7821  * VARIADIC item already dropped, as per above) and the second is an integer
7822  * Value node, containing -1 if there was no ORDER BY and otherwise the number
7823  * of argument declarations before the ORDER BY.  (If this number is equal
7824  * to the first sublist's length, then we dropped a duplicate VARIADIC item.)
7825  * This representation is passed as-is to CREATE AGGREGATE; for operations
7826  * on existing aggregates, we can just apply extractArgTypes to the first
7827  * sublist.
7828  */
7829 aggr_args:	'(' '*' ')'
7830 				{
7831 					$$ = list_make2(NIL, makeInteger(-1));
7832 				}
7833 			| '(' aggr_args_list ')'
7834 				{
7835 					$$ = list_make2($2, makeInteger(-1));
7836 				}
7837 			| '(' ORDER BY aggr_args_list ')'
7838 				{
7839 					$$ = list_make2($4, makeInteger(0));
7840 				}
7841 			| '(' aggr_args_list ORDER BY aggr_args_list ')'
7842 				{
7843 					/* this is the only case requiring consistency checking */
7844 					$$ = makeOrderedSetArgs($2, $5, yyscanner);
7845 				}
7846 		;
7847 
7848 aggr_args_list:
7849 			aggr_arg								{ $$ = list_make1($1); }
7850 			| aggr_args_list ',' aggr_arg			{ $$ = lappend($1, $3); }
7851 		;
7852 
7853 aggregate_with_argtypes:
7854 			func_name aggr_args
7855 				{
7856 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7857 					n->objname = $1;
7858 					n->objargs = extractAggrArgTypes($2);
7859 					$$ = n;
7860 				}
7861 		;
7862 
7863 aggregate_with_argtypes_list:
7864 			aggregate_with_argtypes					{ $$ = list_make1($1); }
7865 			| aggregate_with_argtypes_list ',' aggregate_with_argtypes
7866 													{ $$ = lappend($1, $3); }
7867 		;
7868 
7869 createfunc_opt_list:
7870 			/* Must be at least one to prevent conflict */
7871 			createfunc_opt_item						{ $$ = list_make1($1); }
7872 			| createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
7873 	;
7874 
7875 /*
7876  * Options common to both CREATE FUNCTION and ALTER FUNCTION
7877  */
7878 common_func_opt_item:
7879 			CALLED ON NULL_P INPUT_P
7880 				{
7881 					$$ = makeDefElem("strict", (Node *)makeInteger(false), @1);
7882 				}
7883 			| RETURNS NULL_P ON NULL_P INPUT_P
7884 				{
7885 					$$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7886 				}
7887 			| STRICT_P
7888 				{
7889 					$$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7890 				}
7891 			| IMMUTABLE
7892 				{
7893 					$$ = makeDefElem("volatility", (Node *)makeString("immutable"), @1);
7894 				}
7895 			| STABLE
7896 				{
7897 					$$ = makeDefElem("volatility", (Node *)makeString("stable"), @1);
7898 				}
7899 			| VOLATILE
7900 				{
7901 					$$ = makeDefElem("volatility", (Node *)makeString("volatile"), @1);
7902 				}
7903 			| EXTERNAL SECURITY DEFINER
7904 				{
7905 					$$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7906 				}
7907 			| EXTERNAL SECURITY INVOKER
7908 				{
7909 					$$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7910 				}
7911 			| SECURITY DEFINER
7912 				{
7913 					$$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7914 				}
7915 			| SECURITY INVOKER
7916 				{
7917 					$$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7918 				}
7919 			| LEAKPROOF
7920 				{
7921 					$$ = makeDefElem("leakproof", (Node *)makeInteger(true), @1);
7922 				}
7923 			| NOT LEAKPROOF
7924 				{
7925 					$$ = makeDefElem("leakproof", (Node *)makeInteger(false), @1);
7926 				}
7927 			| COST NumericOnly
7928 				{
7929 					$$ = makeDefElem("cost", (Node *)$2, @1);
7930 				}
7931 			| ROWS NumericOnly
7932 				{
7933 					$$ = makeDefElem("rows", (Node *)$2, @1);
7934 				}
7935 			| FunctionSetResetClause
7936 				{
7937 					/* we abuse the normal content of a DefElem here */
7938 					$$ = makeDefElem("set", (Node *)$1, @1);
7939 				}
7940 			| PARALLEL ColId
7941 				{
7942 					$$ = makeDefElem("parallel", (Node *)makeString($2), @1);
7943 				}
7944 		;
7945 
7946 createfunc_opt_item:
7947 			AS func_as
7948 				{
7949 					$$ = makeDefElem("as", (Node *)$2, @1);
7950 				}
7951 			| LANGUAGE NonReservedWord_or_Sconst
7952 				{
7953 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
7954 				}
7955 			| TRANSFORM transform_type_list
7956 				{
7957 					$$ = makeDefElem("transform", (Node *)$2, @1);
7958 				}
7959 			| WINDOW
7960 				{
7961 					$$ = makeDefElem("window", (Node *)makeInteger(true), @1);
7962 				}
7963 			| common_func_opt_item
7964 				{
7965 					$$ = $1;
7966 				}
7967 		;
7968 
7969 func_as:	Sconst						{ $$ = list_make1(makeString($1)); }
7970 			| Sconst ',' Sconst
7971 				{
7972 					$$ = list_make2(makeString($1), makeString($3));
7973 				}
7974 		;
7975 
7976 transform_type_list:
7977 			FOR TYPE_P Typename { $$ = list_make1($3); }
7978 			| transform_type_list ',' FOR TYPE_P Typename { $$ = lappend($1, $5); }
7979 		;
7980 
7981 opt_definition:
7982 			WITH definition							{ $$ = $2; }
7983 			| /*EMPTY*/								{ $$ = NIL; }
7984 		;
7985 
7986 table_func_column:	param_name func_type
7987 				{
7988 					FunctionParameter *n = makeNode(FunctionParameter);
7989 					n->name = $1;
7990 					n->argType = $2;
7991 					n->mode = FUNC_PARAM_TABLE;
7992 					n->defexpr = NULL;
7993 					$$ = n;
7994 				}
7995 		;
7996 
7997 table_func_column_list:
7998 			table_func_column
7999 				{
8000 					$$ = list_make1($1);
8001 				}
8002 			| table_func_column_list ',' table_func_column
8003 				{
8004 					$$ = lappend($1, $3);
8005 				}
8006 		;
8007 
8008 /*****************************************************************************
8009  * ALTER FUNCTION / ALTER PROCEDURE / ALTER ROUTINE
8010  *
8011  * RENAME and OWNER subcommands are already provided by the generic
8012  * ALTER infrastructure, here we just specify alterations that can
8013  * only be applied to functions.
8014  *
8015  *****************************************************************************/
8016 AlterFunctionStmt:
8017 			ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
8018 				{
8019 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8020 					n->objtype = OBJECT_FUNCTION;
8021 					n->func = $3;
8022 					n->actions = $4;
8023 					$$ = (Node *) n;
8024 				}
8025 			| ALTER PROCEDURE function_with_argtypes alterfunc_opt_list opt_restrict
8026 				{
8027 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8028 					n->objtype = OBJECT_PROCEDURE;
8029 					n->func = $3;
8030 					n->actions = $4;
8031 					$$ = (Node *) n;
8032 				}
8033 			| ALTER ROUTINE function_with_argtypes alterfunc_opt_list opt_restrict
8034 				{
8035 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8036 					n->objtype = OBJECT_ROUTINE;
8037 					n->func = $3;
8038 					n->actions = $4;
8039 					$$ = (Node *) n;
8040 				}
8041 		;
8042 
8043 alterfunc_opt_list:
8044 			/* At least one option must be specified */
8045 			common_func_opt_item					{ $$ = list_make1($1); }
8046 			| alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
8047 		;
8048 
8049 /* Ignored, merely for SQL compliance */
8050 opt_restrict:
8051 			RESTRICT
8052 			| /* EMPTY */
8053 		;
8054 
8055 
8056 /*****************************************************************************
8057  *
8058  *		QUERY:
8059  *
8060  *		DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8061  *		DROP PROCEDURE procname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8062  *		DROP ROUTINE routname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8063  *		DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
8064  *		DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
8065  *
8066  *****************************************************************************/
8067 
8068 RemoveFuncStmt:
8069 			DROP FUNCTION function_with_argtypes_list opt_drop_behavior
8070 				{
8071 					DropStmt *n = makeNode(DropStmt);
8072 					n->removeType = OBJECT_FUNCTION;
8073 					n->objects = $3;
8074 					n->behavior = $4;
8075 					n->missing_ok = false;
8076 					n->concurrent = false;
8077 					$$ = (Node *)n;
8078 				}
8079 			| DROP FUNCTION IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8080 				{
8081 					DropStmt *n = makeNode(DropStmt);
8082 					n->removeType = OBJECT_FUNCTION;
8083 					n->objects = $5;
8084 					n->behavior = $6;
8085 					n->missing_ok = true;
8086 					n->concurrent = false;
8087 					$$ = (Node *)n;
8088 				}
8089 			| DROP PROCEDURE function_with_argtypes_list opt_drop_behavior
8090 				{
8091 					DropStmt *n = makeNode(DropStmt);
8092 					n->removeType = OBJECT_PROCEDURE;
8093 					n->objects = $3;
8094 					n->behavior = $4;
8095 					n->missing_ok = false;
8096 					n->concurrent = false;
8097 					$$ = (Node *)n;
8098 				}
8099 			| DROP PROCEDURE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8100 				{
8101 					DropStmt *n = makeNode(DropStmt);
8102 					n->removeType = OBJECT_PROCEDURE;
8103 					n->objects = $5;
8104 					n->behavior = $6;
8105 					n->missing_ok = true;
8106 					n->concurrent = false;
8107 					$$ = (Node *)n;
8108 				}
8109 			| DROP ROUTINE function_with_argtypes_list opt_drop_behavior
8110 				{
8111 					DropStmt *n = makeNode(DropStmt);
8112 					n->removeType = OBJECT_ROUTINE;
8113 					n->objects = $3;
8114 					n->behavior = $4;
8115 					n->missing_ok = false;
8116 					n->concurrent = false;
8117 					$$ = (Node *)n;
8118 				}
8119 			| DROP ROUTINE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8120 				{
8121 					DropStmt *n = makeNode(DropStmt);
8122 					n->removeType = OBJECT_ROUTINE;
8123 					n->objects = $5;
8124 					n->behavior = $6;
8125 					n->missing_ok = true;
8126 					n->concurrent = false;
8127 					$$ = (Node *)n;
8128 				}
8129 		;
8130 
8131 RemoveAggrStmt:
8132 			DROP AGGREGATE aggregate_with_argtypes_list opt_drop_behavior
8133 				{
8134 					DropStmt *n = makeNode(DropStmt);
8135 					n->removeType = OBJECT_AGGREGATE;
8136 					n->objects = $3;
8137 					n->behavior = $4;
8138 					n->missing_ok = false;
8139 					n->concurrent = false;
8140 					$$ = (Node *)n;
8141 				}
8142 			| DROP AGGREGATE IF_P EXISTS aggregate_with_argtypes_list opt_drop_behavior
8143 				{
8144 					DropStmt *n = makeNode(DropStmt);
8145 					n->removeType = OBJECT_AGGREGATE;
8146 					n->objects = $5;
8147 					n->behavior = $6;
8148 					n->missing_ok = true;
8149 					n->concurrent = false;
8150 					$$ = (Node *)n;
8151 				}
8152 		;
8153 
8154 RemoveOperStmt:
8155 			DROP OPERATOR operator_with_argtypes_list opt_drop_behavior
8156 				{
8157 					DropStmt *n = makeNode(DropStmt);
8158 					n->removeType = OBJECT_OPERATOR;
8159 					n->objects = $3;
8160 					n->behavior = $4;
8161 					n->missing_ok = false;
8162 					n->concurrent = false;
8163 					$$ = (Node *)n;
8164 				}
8165 			| DROP OPERATOR IF_P EXISTS operator_with_argtypes_list opt_drop_behavior
8166 				{
8167 					DropStmt *n = makeNode(DropStmt);
8168 					n->removeType = OBJECT_OPERATOR;
8169 					n->objects = $5;
8170 					n->behavior = $6;
8171 					n->missing_ok = true;
8172 					n->concurrent = false;
8173 					$$ = (Node *)n;
8174 				}
8175 		;
8176 
8177 oper_argtypes:
8178 			'(' Typename ')'
8179 				{
8180 				   ereport(ERROR,
8181 						   (errcode(ERRCODE_SYNTAX_ERROR),
8182 							errmsg("missing argument"),
8183 							errhint("Use NONE to denote the missing argument of a unary operator."),
8184 							parser_errposition(@3)));
8185 				}
8186 			| '(' Typename ',' Typename ')'
8187 					{ $$ = list_make2($2, $4); }
8188 			| '(' NONE ',' Typename ')'					/* left unary */
8189 					{ $$ = list_make2(NULL, $4); }
8190 			| '(' Typename ',' NONE ')'					/* right unary */
8191 					{ $$ = list_make2($2, NULL); }
8192 		;
8193 
8194 any_operator:
8195 			all_Op
8196 					{ $$ = list_make1(makeString($1)); }
8197 			| ColId '.' any_operator
8198 					{ $$ = lcons(makeString($1), $3); }
8199 		;
8200 
8201 operator_with_argtypes_list:
8202 			operator_with_argtypes					{ $$ = list_make1($1); }
8203 			| operator_with_argtypes_list ',' operator_with_argtypes
8204 													{ $$ = lappend($1, $3); }
8205 		;
8206 
8207 operator_with_argtypes:
8208 			any_operator oper_argtypes
8209 				{
8210 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
8211 					n->objname = $1;
8212 					n->objargs = $2;
8213 					$$ = n;
8214 				}
8215 		;
8216 
8217 /*****************************************************************************
8218  *
8219  *		DO <anonymous code block> [ LANGUAGE language ]
8220  *
8221  * We use a DefElem list for future extensibility, and to allow flexibility
8222  * in the clause order.
8223  *
8224  *****************************************************************************/
8225 
8226 DoStmt: DO dostmt_opt_list
8227 				{
8228 					DoStmt *n = makeNode(DoStmt);
8229 					n->args = $2;
8230 					$$ = (Node *)n;
8231 				}
8232 		;
8233 
8234 dostmt_opt_list:
8235 			dostmt_opt_item						{ $$ = list_make1($1); }
8236 			| dostmt_opt_list dostmt_opt_item	{ $$ = lappend($1, $2); }
8237 		;
8238 
8239 dostmt_opt_item:
8240 			Sconst
8241 				{
8242 					$$ = makeDefElem("as", (Node *)makeString($1), @1);
8243 				}
8244 			| LANGUAGE NonReservedWord_or_Sconst
8245 				{
8246 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
8247 				}
8248 		;
8249 
8250 /*****************************************************************************
8251  *
8252  *		CREATE CAST / DROP CAST
8253  *
8254  *****************************************************************************/
8255 
8256 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
8257 					WITH FUNCTION function_with_argtypes cast_context
8258 				{
8259 					CreateCastStmt *n = makeNode(CreateCastStmt);
8260 					n->sourcetype = $4;
8261 					n->targettype = $6;
8262 					n->func = $10;
8263 					n->context = (CoercionContext) $11;
8264 					n->inout = false;
8265 					$$ = (Node *)n;
8266 				}
8267 			| CREATE CAST '(' Typename AS Typename ')'
8268 					WITHOUT FUNCTION cast_context
8269 				{
8270 					CreateCastStmt *n = makeNode(CreateCastStmt);
8271 					n->sourcetype = $4;
8272 					n->targettype = $6;
8273 					n->func = NULL;
8274 					n->context = (CoercionContext) $10;
8275 					n->inout = false;
8276 					$$ = (Node *)n;
8277 				}
8278 			| CREATE CAST '(' Typename AS Typename ')'
8279 					WITH INOUT cast_context
8280 				{
8281 					CreateCastStmt *n = makeNode(CreateCastStmt);
8282 					n->sourcetype = $4;
8283 					n->targettype = $6;
8284 					n->func = NULL;
8285 					n->context = (CoercionContext) $10;
8286 					n->inout = true;
8287 					$$ = (Node *)n;
8288 				}
8289 		;
8290 
8291 cast_context:  AS IMPLICIT_P					{ $$ = COERCION_IMPLICIT; }
8292 		| AS ASSIGNMENT							{ $$ = COERCION_ASSIGNMENT; }
8293 		| /*EMPTY*/								{ $$ = COERCION_EXPLICIT; }
8294 		;
8295 
8296 
8297 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
8298 				{
8299 					DropStmt *n = makeNode(DropStmt);
8300 					n->removeType = OBJECT_CAST;
8301 					n->objects = list_make1(list_make2($5, $7));
8302 					n->behavior = $9;
8303 					n->missing_ok = $3;
8304 					n->concurrent = false;
8305 					$$ = (Node *)n;
8306 				}
8307 		;
8308 
8309 opt_if_exists: IF_P EXISTS						{ $$ = true; }
8310 		| /*EMPTY*/								{ $$ = false; }
8311 		;
8312 
8313 
8314 /*****************************************************************************
8315  *
8316  *		CREATE TRANSFORM / DROP TRANSFORM
8317  *
8318  *****************************************************************************/
8319 
8320 CreateTransformStmt: CREATE opt_or_replace TRANSFORM FOR Typename LANGUAGE name '(' transform_element_list ')'
8321 				{
8322 					CreateTransformStmt *n = makeNode(CreateTransformStmt);
8323 					n->replace = $2;
8324 					n->type_name = $5;
8325 					n->lang = $7;
8326 					n->fromsql = linitial($9);
8327 					n->tosql = lsecond($9);
8328 					$$ = (Node *)n;
8329 				}
8330 		;
8331 
8332 transform_element_list: FROM SQL_P WITH FUNCTION function_with_argtypes ',' TO SQL_P WITH FUNCTION function_with_argtypes
8333 				{
8334 					$$ = list_make2($5, $11);
8335 				}
8336 				| TO SQL_P WITH FUNCTION function_with_argtypes ',' FROM SQL_P WITH FUNCTION function_with_argtypes
8337 				{
8338 					$$ = list_make2($11, $5);
8339 				}
8340 				| FROM SQL_P WITH FUNCTION function_with_argtypes
8341 				{
8342 					$$ = list_make2($5, NULL);
8343 				}
8344 				| TO SQL_P WITH FUNCTION function_with_argtypes
8345 				{
8346 					$$ = list_make2(NULL, $5);
8347 				}
8348 		;
8349 
8350 
8351 DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_drop_behavior
8352 				{
8353 					DropStmt *n = makeNode(DropStmt);
8354 					n->removeType = OBJECT_TRANSFORM;
8355 					n->objects = list_make1(list_make2($5, makeString($7)));
8356 					n->behavior = $8;
8357 					n->missing_ok = $3;
8358 					$$ = (Node *)n;
8359 				}
8360 		;
8361 
8362 
8363 /*****************************************************************************
8364  *
8365  *		QUERY:
8366  *
8367  *		REINDEX [ (options) ] type <name>
8368  *****************************************************************************/
8369 
8370 ReindexStmt:
8371 			REINDEX reindex_target_type qualified_name
8372 				{
8373 					ReindexStmt *n = makeNode(ReindexStmt);
8374 					n->kind = $2;
8375 					n->relation = $3;
8376 					n->name = NULL;
8377 					n->options = 0;
8378 					$$ = (Node *)n;
8379 				}
8380 			| REINDEX reindex_target_multitable name
8381 				{
8382 					ReindexStmt *n = makeNode(ReindexStmt);
8383 					n->kind = $2;
8384 					n->name = $3;
8385 					n->relation = NULL;
8386 					n->options = 0;
8387 					$$ = (Node *)n;
8388 				}
8389 			| REINDEX '(' reindex_option_list ')' reindex_target_type qualified_name
8390 				{
8391 					ReindexStmt *n = makeNode(ReindexStmt);
8392 					n->kind = $5;
8393 					n->relation = $6;
8394 					n->name = NULL;
8395 					n->options = $3;
8396 					$$ = (Node *)n;
8397 				}
8398 			| REINDEX '(' reindex_option_list ')' reindex_target_multitable name
8399 				{
8400 					ReindexStmt *n = makeNode(ReindexStmt);
8401 					n->kind = $5;
8402 					n->name = $6;
8403 					n->relation = NULL;
8404 					n->options = $3;
8405 					$$ = (Node *)n;
8406 				}
8407 		;
8408 reindex_target_type:
8409 			INDEX					{ $$ = REINDEX_OBJECT_INDEX; }
8410 			| TABLE					{ $$ = REINDEX_OBJECT_TABLE; }
8411 		;
8412 reindex_target_multitable:
8413 			SCHEMA					{ $$ = REINDEX_OBJECT_SCHEMA; }
8414 			| SYSTEM_P				{ $$ = REINDEX_OBJECT_SYSTEM; }
8415 			| DATABASE				{ $$ = REINDEX_OBJECT_DATABASE; }
8416 		;
8417 reindex_option_list:
8418 			reindex_option_elem								{ $$ = $1; }
8419 			| reindex_option_list ',' reindex_option_elem	{ $$ = $1 | $3; }
8420 		;
8421 reindex_option_elem:
8422 			VERBOSE	{ $$ = REINDEXOPT_VERBOSE; }
8423 		;
8424 
8425 /*****************************************************************************
8426  *
8427  * ALTER TABLESPACE
8428  *
8429  *****************************************************************************/
8430 
8431 AlterTblSpcStmt:
8432 			ALTER TABLESPACE name SET reloptions
8433 				{
8434 					AlterTableSpaceOptionsStmt *n =
8435 						makeNode(AlterTableSpaceOptionsStmt);
8436 					n->tablespacename = $3;
8437 					n->options = $5;
8438 					n->isReset = false;
8439 					$$ = (Node *)n;
8440 				}
8441 			| ALTER TABLESPACE name RESET reloptions
8442 				{
8443 					AlterTableSpaceOptionsStmt *n =
8444 						makeNode(AlterTableSpaceOptionsStmt);
8445 					n->tablespacename = $3;
8446 					n->options = $5;
8447 					n->isReset = true;
8448 					$$ = (Node *)n;
8449 				}
8450 		;
8451 
8452 /*****************************************************************************
8453  *
8454  * ALTER THING name RENAME TO newname
8455  *
8456  *****************************************************************************/
8457 
8458 RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
8459 				{
8460 					RenameStmt *n = makeNode(RenameStmt);
8461 					n->renameType = OBJECT_AGGREGATE;
8462 					n->object = (Node *) $3;
8463 					n->newname = $6;
8464 					n->missing_ok = false;
8465 					$$ = (Node *)n;
8466 				}
8467 			| ALTER COLLATION any_name RENAME TO name
8468 				{
8469 					RenameStmt *n = makeNode(RenameStmt);
8470 					n->renameType = OBJECT_COLLATION;
8471 					n->object = (Node *) $3;
8472 					n->newname = $6;
8473 					n->missing_ok = false;
8474 					$$ = (Node *)n;
8475 				}
8476 			| ALTER CONVERSION_P any_name RENAME TO name
8477 				{
8478 					RenameStmt *n = makeNode(RenameStmt);
8479 					n->renameType = OBJECT_CONVERSION;
8480 					n->object = (Node *) $3;
8481 					n->newname = $6;
8482 					n->missing_ok = false;
8483 					$$ = (Node *)n;
8484 				}
8485 			| ALTER DATABASE database_name RENAME TO database_name
8486 				{
8487 					RenameStmt *n = makeNode(RenameStmt);
8488 					n->renameType = OBJECT_DATABASE;
8489 					n->subname = $3;
8490 					n->newname = $6;
8491 					n->missing_ok = false;
8492 					$$ = (Node *)n;
8493 				}
8494 			| ALTER DOMAIN_P any_name RENAME TO name
8495 				{
8496 					RenameStmt *n = makeNode(RenameStmt);
8497 					n->renameType = OBJECT_DOMAIN;
8498 					n->object = (Node *) $3;
8499 					n->newname = $6;
8500 					n->missing_ok = false;
8501 					$$ = (Node *)n;
8502 				}
8503 			| ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
8504 				{
8505 					RenameStmt *n = makeNode(RenameStmt);
8506 					n->renameType = OBJECT_DOMCONSTRAINT;
8507 					n->object = (Node *) $3;
8508 					n->subname = $6;
8509 					n->newname = $8;
8510 					$$ = (Node *)n;
8511 				}
8512 			| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
8513 				{
8514 					RenameStmt *n = makeNode(RenameStmt);
8515 					n->renameType = OBJECT_FDW;
8516 					n->object = (Node *) makeString($5);
8517 					n->newname = $8;
8518 					n->missing_ok = false;
8519 					$$ = (Node *)n;
8520 				}
8521 			| ALTER FUNCTION function_with_argtypes RENAME TO name
8522 				{
8523 					RenameStmt *n = makeNode(RenameStmt);
8524 					n->renameType = OBJECT_FUNCTION;
8525 					n->object = (Node *) $3;
8526 					n->newname = $6;
8527 					n->missing_ok = false;
8528 					$$ = (Node *)n;
8529 				}
8530 			| ALTER GROUP_P RoleId RENAME TO RoleId
8531 				{
8532 					RenameStmt *n = makeNode(RenameStmt);
8533 					n->renameType = OBJECT_ROLE;
8534 					n->subname = $3;
8535 					n->newname = $6;
8536 					n->missing_ok = false;
8537 					$$ = (Node *)n;
8538 				}
8539 			| ALTER opt_procedural LANGUAGE name RENAME TO name
8540 				{
8541 					RenameStmt *n = makeNode(RenameStmt);
8542 					n->renameType = OBJECT_LANGUAGE;
8543 					n->object = (Node *) makeString($4);
8544 					n->newname = $7;
8545 					n->missing_ok = false;
8546 					$$ = (Node *)n;
8547 				}
8548 			| ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
8549 				{
8550 					RenameStmt *n = makeNode(RenameStmt);
8551 					n->renameType = OBJECT_OPCLASS;
8552 					n->object = (Node *) lcons(makeString($6), $4);
8553 					n->newname = $9;
8554 					n->missing_ok = false;
8555 					$$ = (Node *)n;
8556 				}
8557 			| ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
8558 				{
8559 					RenameStmt *n = makeNode(RenameStmt);
8560 					n->renameType = OBJECT_OPFAMILY;
8561 					n->object = (Node *) lcons(makeString($6), $4);
8562 					n->newname = $9;
8563 					n->missing_ok = false;
8564 					$$ = (Node *)n;
8565 				}
8566 			| ALTER POLICY name ON qualified_name RENAME TO name
8567 				{
8568 					RenameStmt *n = makeNode(RenameStmt);
8569 					n->renameType = OBJECT_POLICY;
8570 					n->relation = $5;
8571 					n->subname = $3;
8572 					n->newname = $8;
8573 					n->missing_ok = false;
8574 					$$ = (Node *)n;
8575 				}
8576 			| ALTER POLICY IF_P EXISTS name ON qualified_name RENAME TO name
8577 				{
8578 					RenameStmt *n = makeNode(RenameStmt);
8579 					n->renameType = OBJECT_POLICY;
8580 					n->relation = $7;
8581 					n->subname = $5;
8582 					n->newname = $10;
8583 					n->missing_ok = true;
8584 					$$ = (Node *)n;
8585 				}
8586 			| ALTER PROCEDURE function_with_argtypes RENAME TO name
8587 				{
8588 					RenameStmt *n = makeNode(RenameStmt);
8589 					n->renameType = OBJECT_PROCEDURE;
8590 					n->object = (Node *) $3;
8591 					n->newname = $6;
8592 					n->missing_ok = false;
8593 					$$ = (Node *)n;
8594 				}
8595 			| ALTER PUBLICATION name RENAME TO name
8596 				{
8597 					RenameStmt *n = makeNode(RenameStmt);
8598 					n->renameType = OBJECT_PUBLICATION;
8599 					n->object = (Node *) makeString($3);
8600 					n->newname = $6;
8601 					n->missing_ok = false;
8602 					$$ = (Node *)n;
8603 				}
8604 			| ALTER ROUTINE function_with_argtypes RENAME TO name
8605 				{
8606 					RenameStmt *n = makeNode(RenameStmt);
8607 					n->renameType = OBJECT_ROUTINE;
8608 					n->object = (Node *) $3;
8609 					n->newname = $6;
8610 					n->missing_ok = false;
8611 					$$ = (Node *)n;
8612 				}
8613 			| ALTER SCHEMA name RENAME TO name
8614 				{
8615 					RenameStmt *n = makeNode(RenameStmt);
8616 					n->renameType = OBJECT_SCHEMA;
8617 					n->subname = $3;
8618 					n->newname = $6;
8619 					n->missing_ok = false;
8620 					$$ = (Node *)n;
8621 				}
8622 			| ALTER SERVER name RENAME TO name
8623 				{
8624 					RenameStmt *n = makeNode(RenameStmt);
8625 					n->renameType = OBJECT_FOREIGN_SERVER;
8626 					n->object = (Node *) makeString($3);
8627 					n->newname = $6;
8628 					n->missing_ok = false;
8629 					$$ = (Node *)n;
8630 				}
8631 			| ALTER SUBSCRIPTION name RENAME TO name
8632 				{
8633 					RenameStmt *n = makeNode(RenameStmt);
8634 					n->renameType = OBJECT_SUBSCRIPTION;
8635 					n->object = (Node *) makeString($3);
8636 					n->newname = $6;
8637 					n->missing_ok = false;
8638 					$$ = (Node *)n;
8639 				}
8640 			| ALTER TABLE relation_expr RENAME TO name
8641 				{
8642 					RenameStmt *n = makeNode(RenameStmt);
8643 					n->renameType = OBJECT_TABLE;
8644 					n->relation = $3;
8645 					n->subname = NULL;
8646 					n->newname = $6;
8647 					n->missing_ok = false;
8648 					$$ = (Node *)n;
8649 				}
8650 			| ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
8651 				{
8652 					RenameStmt *n = makeNode(RenameStmt);
8653 					n->renameType = OBJECT_TABLE;
8654 					n->relation = $5;
8655 					n->subname = NULL;
8656 					n->newname = $8;
8657 					n->missing_ok = true;
8658 					$$ = (Node *)n;
8659 				}
8660 			| ALTER SEQUENCE qualified_name RENAME TO name
8661 				{
8662 					RenameStmt *n = makeNode(RenameStmt);
8663 					n->renameType = OBJECT_SEQUENCE;
8664 					n->relation = $3;
8665 					n->subname = NULL;
8666 					n->newname = $6;
8667 					n->missing_ok = false;
8668 					$$ = (Node *)n;
8669 				}
8670 			| ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
8671 				{
8672 					RenameStmt *n = makeNode(RenameStmt);
8673 					n->renameType = OBJECT_SEQUENCE;
8674 					n->relation = $5;
8675 					n->subname = NULL;
8676 					n->newname = $8;
8677 					n->missing_ok = true;
8678 					$$ = (Node *)n;
8679 				}
8680 			| ALTER VIEW qualified_name RENAME TO name
8681 				{
8682 					RenameStmt *n = makeNode(RenameStmt);
8683 					n->renameType = OBJECT_VIEW;
8684 					n->relation = $3;
8685 					n->subname = NULL;
8686 					n->newname = $6;
8687 					n->missing_ok = false;
8688 					$$ = (Node *)n;
8689 				}
8690 			| ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
8691 				{
8692 					RenameStmt *n = makeNode(RenameStmt);
8693 					n->renameType = OBJECT_VIEW;
8694 					n->relation = $5;
8695 					n->subname = NULL;
8696 					n->newname = $8;
8697 					n->missing_ok = true;
8698 					$$ = (Node *)n;
8699 				}
8700 			| ALTER MATERIALIZED VIEW qualified_name RENAME TO name
8701 				{
8702 					RenameStmt *n = makeNode(RenameStmt);
8703 					n->renameType = OBJECT_MATVIEW;
8704 					n->relation = $4;
8705 					n->subname = NULL;
8706 					n->newname = $7;
8707 					n->missing_ok = false;
8708 					$$ = (Node *)n;
8709 				}
8710 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
8711 				{
8712 					RenameStmt *n = makeNode(RenameStmt);
8713 					n->renameType = OBJECT_MATVIEW;
8714 					n->relation = $6;
8715 					n->subname = NULL;
8716 					n->newname = $9;
8717 					n->missing_ok = true;
8718 					$$ = (Node *)n;
8719 				}
8720 			| ALTER INDEX qualified_name RENAME TO name
8721 				{
8722 					RenameStmt *n = makeNode(RenameStmt);
8723 					n->renameType = OBJECT_INDEX;
8724 					n->relation = $3;
8725 					n->subname = NULL;
8726 					n->newname = $6;
8727 					n->missing_ok = false;
8728 					$$ = (Node *)n;
8729 				}
8730 			| ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
8731 				{
8732 					RenameStmt *n = makeNode(RenameStmt);
8733 					n->renameType = OBJECT_INDEX;
8734 					n->relation = $5;
8735 					n->subname = NULL;
8736 					n->newname = $8;
8737 					n->missing_ok = true;
8738 					$$ = (Node *)n;
8739 				}
8740 			| ALTER FOREIGN TABLE relation_expr RENAME TO name
8741 				{
8742 					RenameStmt *n = makeNode(RenameStmt);
8743 					n->renameType = OBJECT_FOREIGN_TABLE;
8744 					n->relation = $4;
8745 					n->subname = NULL;
8746 					n->newname = $7;
8747 					n->missing_ok = false;
8748 					$$ = (Node *)n;
8749 				}
8750 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
8751 				{
8752 					RenameStmt *n = makeNode(RenameStmt);
8753 					n->renameType = OBJECT_FOREIGN_TABLE;
8754 					n->relation = $6;
8755 					n->subname = NULL;
8756 					n->newname = $9;
8757 					n->missing_ok = true;
8758 					$$ = (Node *)n;
8759 				}
8760 			| ALTER TABLE relation_expr RENAME opt_column name TO name
8761 				{
8762 					RenameStmt *n = makeNode(RenameStmt);
8763 					n->renameType = OBJECT_COLUMN;
8764 					n->relationType = OBJECT_TABLE;
8765 					n->relation = $3;
8766 					n->subname = $6;
8767 					n->newname = $8;
8768 					n->missing_ok = false;
8769 					$$ = (Node *)n;
8770 				}
8771 			| ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8772 				{
8773 					RenameStmt *n = makeNode(RenameStmt);
8774 					n->renameType = OBJECT_COLUMN;
8775 					n->relationType = OBJECT_TABLE;
8776 					n->relation = $5;
8777 					n->subname = $8;
8778 					n->newname = $10;
8779 					n->missing_ok = true;
8780 					$$ = (Node *)n;
8781 				}
8782 			| ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
8783 				{
8784 					RenameStmt *n = makeNode(RenameStmt);
8785 					n->renameType = OBJECT_COLUMN;
8786 					n->relationType = OBJECT_MATVIEW;
8787 					n->relation = $4;
8788 					n->subname = $7;
8789 					n->newname = $9;
8790 					n->missing_ok = false;
8791 					$$ = (Node *)n;
8792 				}
8793 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
8794 				{
8795 					RenameStmt *n = makeNode(RenameStmt);
8796 					n->renameType = OBJECT_COLUMN;
8797 					n->relationType = OBJECT_MATVIEW;
8798 					n->relation = $6;
8799 					n->subname = $9;
8800 					n->newname = $11;
8801 					n->missing_ok = true;
8802 					$$ = (Node *)n;
8803 				}
8804 			| ALTER TABLE relation_expr RENAME CONSTRAINT name TO name
8805 				{
8806 					RenameStmt *n = makeNode(RenameStmt);
8807 					n->renameType = OBJECT_TABCONSTRAINT;
8808 					n->relation = $3;
8809 					n->subname = $6;
8810 					n->newname = $8;
8811 					n->missing_ok = false;
8812 					$$ = (Node *)n;
8813 				}
8814 			| ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
8815 				{
8816 					RenameStmt *n = makeNode(RenameStmt);
8817 					n->renameType = OBJECT_TABCONSTRAINT;
8818 					n->relation = $5;
8819 					n->subname = $8;
8820 					n->newname = $10;
8821 					n->missing_ok = true;
8822 					$$ = (Node *)n;
8823 				}
8824 			| ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
8825 				{
8826 					RenameStmt *n = makeNode(RenameStmt);
8827 					n->renameType = OBJECT_COLUMN;
8828 					n->relationType = OBJECT_FOREIGN_TABLE;
8829 					n->relation = $4;
8830 					n->subname = $7;
8831 					n->newname = $9;
8832 					n->missing_ok = false;
8833 					$$ = (Node *)n;
8834 				}
8835 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8836 				{
8837 					RenameStmt *n = makeNode(RenameStmt);
8838 					n->renameType = OBJECT_COLUMN;
8839 					n->relationType = OBJECT_FOREIGN_TABLE;
8840 					n->relation = $6;
8841 					n->subname = $9;
8842 					n->newname = $11;
8843 					n->missing_ok = true;
8844 					$$ = (Node *)n;
8845 				}
8846 			| ALTER RULE name ON qualified_name RENAME TO name
8847 				{
8848 					RenameStmt *n = makeNode(RenameStmt);
8849 					n->renameType = OBJECT_RULE;
8850 					n->relation = $5;
8851 					n->subname = $3;
8852 					n->newname = $8;
8853 					n->missing_ok = false;
8854 					$$ = (Node *)n;
8855 				}
8856 			| ALTER TRIGGER name ON qualified_name RENAME TO name
8857 				{
8858 					RenameStmt *n = makeNode(RenameStmt);
8859 					n->renameType = OBJECT_TRIGGER;
8860 					n->relation = $5;
8861 					n->subname = $3;
8862 					n->newname = $8;
8863 					n->missing_ok = false;
8864 					$$ = (Node *)n;
8865 				}
8866 			| ALTER EVENT TRIGGER name RENAME TO name
8867 				{
8868 					RenameStmt *n = makeNode(RenameStmt);
8869 					n->renameType = OBJECT_EVENT_TRIGGER;
8870 					n->object = (Node *) makeString($4);
8871 					n->newname = $7;
8872 					$$ = (Node *)n;
8873 				}
8874 			| ALTER ROLE RoleId RENAME TO RoleId
8875 				{
8876 					RenameStmt *n = makeNode(RenameStmt);
8877 					n->renameType = OBJECT_ROLE;
8878 					n->subname = $3;
8879 					n->newname = $6;
8880 					n->missing_ok = false;
8881 					$$ = (Node *)n;
8882 				}
8883 			| ALTER USER RoleId RENAME TO RoleId
8884 				{
8885 					RenameStmt *n = makeNode(RenameStmt);
8886 					n->renameType = OBJECT_ROLE;
8887 					n->subname = $3;
8888 					n->newname = $6;
8889 					n->missing_ok = false;
8890 					$$ = (Node *)n;
8891 				}
8892 			| ALTER TABLESPACE name RENAME TO name
8893 				{
8894 					RenameStmt *n = makeNode(RenameStmt);
8895 					n->renameType = OBJECT_TABLESPACE;
8896 					n->subname = $3;
8897 					n->newname = $6;
8898 					n->missing_ok = false;
8899 					$$ = (Node *)n;
8900 				}
8901 			| ALTER STATISTICS any_name RENAME TO name
8902 				{
8903 					RenameStmt *n = makeNode(RenameStmt);
8904 					n->renameType = OBJECT_STATISTIC_EXT;
8905 					n->object = (Node *) $3;
8906 					n->newname = $6;
8907 					n->missing_ok = false;
8908 					$$ = (Node *)n;
8909 				}
8910 			| ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
8911 				{
8912 					RenameStmt *n = makeNode(RenameStmt);
8913 					n->renameType = OBJECT_TSPARSER;
8914 					n->object = (Node *) $5;
8915 					n->newname = $8;
8916 					n->missing_ok = false;
8917 					$$ = (Node *)n;
8918 				}
8919 			| ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
8920 				{
8921 					RenameStmt *n = makeNode(RenameStmt);
8922 					n->renameType = OBJECT_TSDICTIONARY;
8923 					n->object = (Node *) $5;
8924 					n->newname = $8;
8925 					n->missing_ok = false;
8926 					$$ = (Node *)n;
8927 				}
8928 			| ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
8929 				{
8930 					RenameStmt *n = makeNode(RenameStmt);
8931 					n->renameType = OBJECT_TSTEMPLATE;
8932 					n->object = (Node *) $5;
8933 					n->newname = $8;
8934 					n->missing_ok = false;
8935 					$$ = (Node *)n;
8936 				}
8937 			| ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
8938 				{
8939 					RenameStmt *n = makeNode(RenameStmt);
8940 					n->renameType = OBJECT_TSCONFIGURATION;
8941 					n->object = (Node *) $5;
8942 					n->newname = $8;
8943 					n->missing_ok = false;
8944 					$$ = (Node *)n;
8945 				}
8946 			| ALTER TYPE_P any_name RENAME TO name
8947 				{
8948 					RenameStmt *n = makeNode(RenameStmt);
8949 					n->renameType = OBJECT_TYPE;
8950 					n->object = (Node *) $3;
8951 					n->newname = $6;
8952 					n->missing_ok = false;
8953 					$$ = (Node *)n;
8954 				}
8955 			| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
8956 				{
8957 					RenameStmt *n = makeNode(RenameStmt);
8958 					n->renameType = OBJECT_ATTRIBUTE;
8959 					n->relationType = OBJECT_TYPE;
8960 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
8961 					n->subname = $6;
8962 					n->newname = $8;
8963 					n->behavior = $9;
8964 					n->missing_ok = false;
8965 					$$ = (Node *)n;
8966 				}
8967 		;
8968 
8969 opt_column: COLUMN									{ $$ = COLUMN; }
8970 			| /*EMPTY*/								{ $$ = 0; }
8971 		;
8972 
8973 opt_set_data: SET DATA_P							{ $$ = 1; }
8974 			| /*EMPTY*/								{ $$ = 0; }
8975 		;
8976 
8977 /*****************************************************************************
8978  *
8979  * ALTER THING name DEPENDS ON EXTENSION name
8980  *
8981  *****************************************************************************/
8982 
8983 AlterObjectDependsStmt:
8984 			ALTER FUNCTION function_with_argtypes DEPENDS ON EXTENSION name
8985 				{
8986 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8987 					n->objectType = OBJECT_FUNCTION;
8988 					n->object = (Node *) $3;
8989 					n->extname = makeString($7);
8990 					$$ = (Node *)n;
8991 				}
8992 			| ALTER PROCEDURE function_with_argtypes DEPENDS ON EXTENSION name
8993 				{
8994 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8995 					n->objectType = OBJECT_PROCEDURE;
8996 					n->object = (Node *) $3;
8997 					n->extname = makeString($7);
8998 					$$ = (Node *)n;
8999 				}
9000 			| ALTER ROUTINE function_with_argtypes DEPENDS ON EXTENSION name
9001 				{
9002 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9003 					n->objectType = OBJECT_ROUTINE;
9004 					n->object = (Node *) $3;
9005 					n->extname = makeString($7);
9006 					$$ = (Node *)n;
9007 				}
9008 			| ALTER TRIGGER name ON qualified_name DEPENDS ON EXTENSION name
9009 				{
9010 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9011 					n->objectType = OBJECT_TRIGGER;
9012 					n->relation = $5;
9013 					n->object = (Node *) list_make1(makeString($3));
9014 					n->extname = makeString($9);
9015 					$$ = (Node *)n;
9016 				}
9017 			| ALTER MATERIALIZED VIEW qualified_name DEPENDS ON EXTENSION name
9018 				{
9019 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9020 					n->objectType = OBJECT_MATVIEW;
9021 					n->relation = $4;
9022 					n->extname = makeString($8);
9023 					$$ = (Node *)n;
9024 				}
9025 			| ALTER INDEX qualified_name DEPENDS ON EXTENSION name
9026 				{
9027 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9028 					n->objectType = OBJECT_INDEX;
9029 					n->relation = $3;
9030 					n->extname = makeString($7);
9031 					$$ = (Node *)n;
9032 				}
9033 		;
9034 
9035 /*****************************************************************************
9036  *
9037  * ALTER THING name SET SCHEMA name
9038  *
9039  *****************************************************************************/
9040 
9041 AlterObjectSchemaStmt:
9042 			ALTER AGGREGATE aggregate_with_argtypes SET SCHEMA name
9043 				{
9044 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9045 					n->objectType = OBJECT_AGGREGATE;
9046 					n->object = (Node *) $3;
9047 					n->newschema = $6;
9048 					n->missing_ok = false;
9049 					$$ = (Node *)n;
9050 				}
9051 			| ALTER COLLATION any_name SET SCHEMA name
9052 				{
9053 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9054 					n->objectType = OBJECT_COLLATION;
9055 					n->object = (Node *) $3;
9056 					n->newschema = $6;
9057 					n->missing_ok = false;
9058 					$$ = (Node *)n;
9059 				}
9060 			| ALTER CONVERSION_P any_name SET SCHEMA name
9061 				{
9062 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9063 					n->objectType = OBJECT_CONVERSION;
9064 					n->object = (Node *) $3;
9065 					n->newschema = $6;
9066 					n->missing_ok = false;
9067 					$$ = (Node *)n;
9068 				}
9069 			| ALTER DOMAIN_P any_name SET SCHEMA name
9070 				{
9071 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9072 					n->objectType = OBJECT_DOMAIN;
9073 					n->object = (Node *) $3;
9074 					n->newschema = $6;
9075 					n->missing_ok = false;
9076 					$$ = (Node *)n;
9077 				}
9078 			| ALTER EXTENSION name SET SCHEMA name
9079 				{
9080 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9081 					n->objectType = OBJECT_EXTENSION;
9082 					n->object = (Node *) makeString($3);
9083 					n->newschema = $6;
9084 					n->missing_ok = false;
9085 					$$ = (Node *)n;
9086 				}
9087 			| ALTER FUNCTION function_with_argtypes SET SCHEMA name
9088 				{
9089 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9090 					n->objectType = OBJECT_FUNCTION;
9091 					n->object = (Node *) $3;
9092 					n->newschema = $6;
9093 					n->missing_ok = false;
9094 					$$ = (Node *)n;
9095 				}
9096 			| ALTER OPERATOR operator_with_argtypes SET SCHEMA name
9097 				{
9098 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9099 					n->objectType = OBJECT_OPERATOR;
9100 					n->object = (Node *) $3;
9101 					n->newschema = $6;
9102 					n->missing_ok = false;
9103 					$$ = (Node *)n;
9104 				}
9105 			| ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
9106 				{
9107 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9108 					n->objectType = OBJECT_OPCLASS;
9109 					n->object = (Node *) lcons(makeString($6), $4);
9110 					n->newschema = $9;
9111 					n->missing_ok = false;
9112 					$$ = (Node *)n;
9113 				}
9114 			| ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
9115 				{
9116 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9117 					n->objectType = OBJECT_OPFAMILY;
9118 					n->object = (Node *) lcons(makeString($6), $4);
9119 					n->newschema = $9;
9120 					n->missing_ok = false;
9121 					$$ = (Node *)n;
9122 				}
9123 			| ALTER PROCEDURE function_with_argtypes SET SCHEMA name
9124 				{
9125 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9126 					n->objectType = OBJECT_PROCEDURE;
9127 					n->object = (Node *) $3;
9128 					n->newschema = $6;
9129 					n->missing_ok = false;
9130 					$$ = (Node *)n;
9131 				}
9132 			| ALTER ROUTINE function_with_argtypes SET SCHEMA name
9133 				{
9134 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9135 					n->objectType = OBJECT_ROUTINE;
9136 					n->object = (Node *) $3;
9137 					n->newschema = $6;
9138 					n->missing_ok = false;
9139 					$$ = (Node *)n;
9140 				}
9141 			| ALTER TABLE relation_expr SET SCHEMA name
9142 				{
9143 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9144 					n->objectType = OBJECT_TABLE;
9145 					n->relation = $3;
9146 					n->newschema = $6;
9147 					n->missing_ok = false;
9148 					$$ = (Node *)n;
9149 				}
9150 			| ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
9151 				{
9152 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9153 					n->objectType = OBJECT_TABLE;
9154 					n->relation = $5;
9155 					n->newschema = $8;
9156 					n->missing_ok = true;
9157 					$$ = (Node *)n;
9158 				}
9159 			| ALTER STATISTICS any_name SET SCHEMA name
9160 				{
9161 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9162 					n->objectType = OBJECT_STATISTIC_EXT;
9163 					n->object = (Node *) $3;
9164 					n->newschema = $6;
9165 					n->missing_ok = false;
9166 					$$ = (Node *)n;
9167 				}
9168 			| ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
9169 				{
9170 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9171 					n->objectType = OBJECT_TSPARSER;
9172 					n->object = (Node *) $5;
9173 					n->newschema = $8;
9174 					n->missing_ok = false;
9175 					$$ = (Node *)n;
9176 				}
9177 			| ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
9178 				{
9179 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9180 					n->objectType = OBJECT_TSDICTIONARY;
9181 					n->object = (Node *) $5;
9182 					n->newschema = $8;
9183 					n->missing_ok = false;
9184 					$$ = (Node *)n;
9185 				}
9186 			| ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
9187 				{
9188 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9189 					n->objectType = OBJECT_TSTEMPLATE;
9190 					n->object = (Node *) $5;
9191 					n->newschema = $8;
9192 					n->missing_ok = false;
9193 					$$ = (Node *)n;
9194 				}
9195 			| ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
9196 				{
9197 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9198 					n->objectType = OBJECT_TSCONFIGURATION;
9199 					n->object = (Node *) $5;
9200 					n->newschema = $8;
9201 					n->missing_ok = false;
9202 					$$ = (Node *)n;
9203 				}
9204 			| ALTER SEQUENCE qualified_name SET SCHEMA name
9205 				{
9206 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9207 					n->objectType = OBJECT_SEQUENCE;
9208 					n->relation = $3;
9209 					n->newschema = $6;
9210 					n->missing_ok = false;
9211 					$$ = (Node *)n;
9212 				}
9213 			| ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
9214 				{
9215 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9216 					n->objectType = OBJECT_SEQUENCE;
9217 					n->relation = $5;
9218 					n->newschema = $8;
9219 					n->missing_ok = true;
9220 					$$ = (Node *)n;
9221 				}
9222 			| ALTER VIEW qualified_name SET SCHEMA name
9223 				{
9224 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9225 					n->objectType = OBJECT_VIEW;
9226 					n->relation = $3;
9227 					n->newschema = $6;
9228 					n->missing_ok = false;
9229 					$$ = (Node *)n;
9230 				}
9231 			| ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
9232 				{
9233 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9234 					n->objectType = OBJECT_VIEW;
9235 					n->relation = $5;
9236 					n->newschema = $8;
9237 					n->missing_ok = true;
9238 					$$ = (Node *)n;
9239 				}
9240 			| ALTER MATERIALIZED VIEW qualified_name SET SCHEMA name
9241 				{
9242 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9243 					n->objectType = OBJECT_MATVIEW;
9244 					n->relation = $4;
9245 					n->newschema = $7;
9246 					n->missing_ok = false;
9247 					$$ = (Node *)n;
9248 				}
9249 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name SET SCHEMA name
9250 				{
9251 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9252 					n->objectType = OBJECT_MATVIEW;
9253 					n->relation = $6;
9254 					n->newschema = $9;
9255 					n->missing_ok = true;
9256 					$$ = (Node *)n;
9257 				}
9258 			| ALTER FOREIGN TABLE relation_expr SET SCHEMA name
9259 				{
9260 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9261 					n->objectType = OBJECT_FOREIGN_TABLE;
9262 					n->relation = $4;
9263 					n->newschema = $7;
9264 					n->missing_ok = false;
9265 					$$ = (Node *)n;
9266 				}
9267 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
9268 				{
9269 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9270 					n->objectType = OBJECT_FOREIGN_TABLE;
9271 					n->relation = $6;
9272 					n->newschema = $9;
9273 					n->missing_ok = true;
9274 					$$ = (Node *)n;
9275 				}
9276 			| ALTER TYPE_P any_name SET SCHEMA name
9277 				{
9278 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9279 					n->objectType = OBJECT_TYPE;
9280 					n->object = (Node *) $3;
9281 					n->newschema = $6;
9282 					n->missing_ok = false;
9283 					$$ = (Node *)n;
9284 				}
9285 		;
9286 
9287 /*****************************************************************************
9288  *
9289  * ALTER OPERATOR name SET define
9290  *
9291  *****************************************************************************/
9292 
9293 AlterOperatorStmt:
9294 			ALTER OPERATOR operator_with_argtypes SET '(' operator_def_list ')'
9295 				{
9296 					AlterOperatorStmt *n = makeNode(AlterOperatorStmt);
9297 					n->opername = $3;
9298 					n->options = $6;
9299 					$$ = (Node *)n;
9300 				}
9301 		;
9302 
9303 operator_def_list:	operator_def_elem								{ $$ = list_make1($1); }
9304 			| operator_def_list ',' operator_def_elem				{ $$ = lappend($1, $3); }
9305 		;
9306 
9307 operator_def_elem: ColLabel '=' NONE
9308 						{ $$ = makeDefElem($1, NULL, @1); }
9309 				   | ColLabel '=' operator_def_arg
9310 						{ $$ = makeDefElem($1, (Node *) $3, @1); }
9311 		;
9312 
9313 /* must be similar enough to def_arg to avoid reduce/reduce conflicts */
9314 operator_def_arg:
9315 			func_type						{ $$ = (Node *)$1; }
9316 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
9317 			| qual_all_Op					{ $$ = (Node *)$1; }
9318 			| NumericOnly					{ $$ = (Node *)$1; }
9319 			| Sconst						{ $$ = (Node *)makeString($1); }
9320 		;
9321 
9322 /*****************************************************************************
9323  *
9324  * ALTER THING name OWNER TO newname
9325  *
9326  *****************************************************************************/
9327 
9328 AlterOwnerStmt: ALTER AGGREGATE aggregate_with_argtypes OWNER TO RoleSpec
9329 				{
9330 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9331 					n->objectType = OBJECT_AGGREGATE;
9332 					n->object = (Node *) $3;
9333 					n->newowner = $6;
9334 					$$ = (Node *)n;
9335 				}
9336 			| ALTER COLLATION any_name OWNER TO RoleSpec
9337 				{
9338 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9339 					n->objectType = OBJECT_COLLATION;
9340 					n->object = (Node *) $3;
9341 					n->newowner = $6;
9342 					$$ = (Node *)n;
9343 				}
9344 			| ALTER CONVERSION_P any_name OWNER TO RoleSpec
9345 				{
9346 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9347 					n->objectType = OBJECT_CONVERSION;
9348 					n->object = (Node *) $3;
9349 					n->newowner = $6;
9350 					$$ = (Node *)n;
9351 				}
9352 			| ALTER DATABASE database_name OWNER TO RoleSpec
9353 				{
9354 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9355 					n->objectType = OBJECT_DATABASE;
9356 					n->object = (Node *) makeString($3);
9357 					n->newowner = $6;
9358 					$$ = (Node *)n;
9359 				}
9360 			| ALTER DOMAIN_P any_name OWNER TO RoleSpec
9361 				{
9362 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9363 					n->objectType = OBJECT_DOMAIN;
9364 					n->object = (Node *) $3;
9365 					n->newowner = $6;
9366 					$$ = (Node *)n;
9367 				}
9368 			| ALTER FUNCTION function_with_argtypes OWNER TO RoleSpec
9369 				{
9370 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9371 					n->objectType = OBJECT_FUNCTION;
9372 					n->object = (Node *) $3;
9373 					n->newowner = $6;
9374 					$$ = (Node *)n;
9375 				}
9376 			| ALTER opt_procedural LANGUAGE name OWNER TO RoleSpec
9377 				{
9378 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9379 					n->objectType = OBJECT_LANGUAGE;
9380 					n->object = (Node *) makeString($4);
9381 					n->newowner = $7;
9382 					$$ = (Node *)n;
9383 				}
9384 			| ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleSpec
9385 				{
9386 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9387 					n->objectType = OBJECT_LARGEOBJECT;
9388 					n->object = (Node *) $4;
9389 					n->newowner = $7;
9390 					$$ = (Node *)n;
9391 				}
9392 			| ALTER OPERATOR operator_with_argtypes OWNER TO RoleSpec
9393 				{
9394 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9395 					n->objectType = OBJECT_OPERATOR;
9396 					n->object = (Node *) $3;
9397 					n->newowner = $6;
9398 					$$ = (Node *)n;
9399 				}
9400 			| ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleSpec
9401 				{
9402 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9403 					n->objectType = OBJECT_OPCLASS;
9404 					n->object = (Node *) lcons(makeString($6), $4);
9405 					n->newowner = $9;
9406 					$$ = (Node *)n;
9407 				}
9408 			| ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleSpec
9409 				{
9410 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9411 					n->objectType = OBJECT_OPFAMILY;
9412 					n->object = (Node *) lcons(makeString($6), $4);
9413 					n->newowner = $9;
9414 					$$ = (Node *)n;
9415 				}
9416 			| ALTER PROCEDURE function_with_argtypes OWNER TO RoleSpec
9417 				{
9418 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9419 					n->objectType = OBJECT_PROCEDURE;
9420 					n->object = (Node *) $3;
9421 					n->newowner = $6;
9422 					$$ = (Node *)n;
9423 				}
9424 			| ALTER ROUTINE function_with_argtypes OWNER TO RoleSpec
9425 				{
9426 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9427 					n->objectType = OBJECT_ROUTINE;
9428 					n->object = (Node *) $3;
9429 					n->newowner = $6;
9430 					$$ = (Node *)n;
9431 				}
9432 			| ALTER SCHEMA name OWNER TO RoleSpec
9433 				{
9434 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9435 					n->objectType = OBJECT_SCHEMA;
9436 					n->object = (Node *) makeString($3);
9437 					n->newowner = $6;
9438 					$$ = (Node *)n;
9439 				}
9440 			| ALTER TYPE_P any_name OWNER TO RoleSpec
9441 				{
9442 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9443 					n->objectType = OBJECT_TYPE;
9444 					n->object = (Node *) $3;
9445 					n->newowner = $6;
9446 					$$ = (Node *)n;
9447 				}
9448 			| ALTER TABLESPACE name OWNER TO RoleSpec
9449 				{
9450 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9451 					n->objectType = OBJECT_TABLESPACE;
9452 					n->object = (Node *) makeString($3);
9453 					n->newowner = $6;
9454 					$$ = (Node *)n;
9455 				}
9456 			| ALTER STATISTICS any_name OWNER TO RoleSpec
9457 				{
9458 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9459 					n->objectType = OBJECT_STATISTIC_EXT;
9460 					n->object = (Node *) $3;
9461 					n->newowner = $6;
9462 					$$ = (Node *)n;
9463 				}
9464 			| ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleSpec
9465 				{
9466 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9467 					n->objectType = OBJECT_TSDICTIONARY;
9468 					n->object = (Node *) $5;
9469 					n->newowner = $8;
9470 					$$ = (Node *)n;
9471 				}
9472 			| ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleSpec
9473 				{
9474 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9475 					n->objectType = OBJECT_TSCONFIGURATION;
9476 					n->object = (Node *) $5;
9477 					n->newowner = $8;
9478 					$$ = (Node *)n;
9479 				}
9480 			| ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleSpec
9481 				{
9482 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9483 					n->objectType = OBJECT_FDW;
9484 					n->object = (Node *) makeString($5);
9485 					n->newowner = $8;
9486 					$$ = (Node *)n;
9487 				}
9488 			| ALTER SERVER name OWNER TO RoleSpec
9489 				{
9490 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9491 					n->objectType = OBJECT_FOREIGN_SERVER;
9492 					n->object = (Node *) makeString($3);
9493 					n->newowner = $6;
9494 					$$ = (Node *)n;
9495 				}
9496 			| ALTER EVENT TRIGGER name OWNER TO RoleSpec
9497 				{
9498 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9499 					n->objectType = OBJECT_EVENT_TRIGGER;
9500 					n->object = (Node *) makeString($4);
9501 					n->newowner = $7;
9502 					$$ = (Node *)n;
9503 				}
9504 			| ALTER PUBLICATION name OWNER TO RoleSpec
9505 				{
9506 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9507 					n->objectType = OBJECT_PUBLICATION;
9508 					n->object = (Node *) makeString($3);
9509 					n->newowner = $6;
9510 					$$ = (Node *)n;
9511 				}
9512 			| ALTER SUBSCRIPTION name OWNER TO RoleSpec
9513 				{
9514 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9515 					n->objectType = OBJECT_SUBSCRIPTION;
9516 					n->object = (Node *) makeString($3);
9517 					n->newowner = $6;
9518 					$$ = (Node *)n;
9519 				}
9520 		;
9521 
9522 
9523 /*****************************************************************************
9524  *
9525  * CREATE PUBLICATION name [ FOR TABLE ] [ WITH options ]
9526  *
9527  *****************************************************************************/
9528 
9529 CreatePublicationStmt:
9530 			CREATE PUBLICATION name opt_publication_for_tables opt_definition
9531 				{
9532 					CreatePublicationStmt *n = makeNode(CreatePublicationStmt);
9533 					n->pubname = $3;
9534 					n->options = $5;
9535 					if ($4 != NULL)
9536 					{
9537 						/* FOR TABLE */
9538 						if (IsA($4, List))
9539 							n->tables = (List *)$4;
9540 						/* FOR ALL TABLES */
9541 						else
9542 							n->for_all_tables = true;
9543 					}
9544 					$$ = (Node *)n;
9545 				}
9546 		;
9547 
9548 opt_publication_for_tables:
9549 			publication_for_tables					{ $$ = $1; }
9550 			| /* EMPTY */							{ $$ = NULL; }
9551 		;
9552 
9553 publication_for_tables:
9554 			FOR TABLE relation_expr_list
9555 				{
9556 					$$ = (Node *) $3;
9557 				}
9558 			| FOR ALL TABLES
9559 				{
9560 					$$ = (Node *) makeInteger(true);
9561 				}
9562 		;
9563 
9564 
9565 /*****************************************************************************
9566  *
9567  * ALTER PUBLICATION name SET ( options )
9568  *
9569  * ALTER PUBLICATION name ADD TABLE table [, table2]
9570  *
9571  * ALTER PUBLICATION name DROP TABLE table [, table2]
9572  *
9573  * ALTER PUBLICATION name SET TABLE table [, table2]
9574  *
9575  *****************************************************************************/
9576 
9577 AlterPublicationStmt:
9578 			ALTER PUBLICATION name SET definition
9579 				{
9580 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9581 					n->pubname = $3;
9582 					n->options = $5;
9583 					$$ = (Node *)n;
9584 				}
9585 			| ALTER PUBLICATION name ADD_P TABLE relation_expr_list
9586 				{
9587 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9588 					n->pubname = $3;
9589 					n->tables = $6;
9590 					n->tableAction = DEFELEM_ADD;
9591 					$$ = (Node *)n;
9592 				}
9593 			| ALTER PUBLICATION name SET TABLE relation_expr_list
9594 				{
9595 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9596 					n->pubname = $3;
9597 					n->tables = $6;
9598 					n->tableAction = DEFELEM_SET;
9599 					$$ = (Node *)n;
9600 				}
9601 			| ALTER PUBLICATION name DROP TABLE relation_expr_list
9602 				{
9603 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9604 					n->pubname = $3;
9605 					n->tables = $6;
9606 					n->tableAction = DEFELEM_DROP;
9607 					$$ = (Node *)n;
9608 				}
9609 		;
9610 
9611 /*****************************************************************************
9612  *
9613  * CREATE SUBSCRIPTION name ...
9614  *
9615  *****************************************************************************/
9616 
9617 CreateSubscriptionStmt:
9618 			CREATE SUBSCRIPTION name CONNECTION Sconst PUBLICATION publication_name_list opt_definition
9619 				{
9620 					CreateSubscriptionStmt *n =
9621 						makeNode(CreateSubscriptionStmt);
9622 					n->subname = $3;
9623 					n->conninfo = $5;
9624 					n->publication = $7;
9625 					n->options = $8;
9626 					$$ = (Node *)n;
9627 				}
9628 		;
9629 
9630 publication_name_list:
9631 			publication_name_item
9632 				{
9633 					$$ = list_make1($1);
9634 				}
9635 			| publication_name_list ',' publication_name_item
9636 				{
9637 					$$ = lappend($1, $3);
9638 				}
9639 		;
9640 
9641 publication_name_item:
9642 			ColLabel			{ $$ = makeString($1); };
9643 
9644 /*****************************************************************************
9645  *
9646  * ALTER SUBSCRIPTION name ...
9647  *
9648  *****************************************************************************/
9649 
9650 AlterSubscriptionStmt:
9651 			ALTER SUBSCRIPTION name SET definition
9652 				{
9653 					AlterSubscriptionStmt *n =
9654 						makeNode(AlterSubscriptionStmt);
9655 					n->kind = ALTER_SUBSCRIPTION_OPTIONS;
9656 					n->subname = $3;
9657 					n->options = $5;
9658 					$$ = (Node *)n;
9659 				}
9660 			| ALTER SUBSCRIPTION name CONNECTION Sconst
9661 				{
9662 					AlterSubscriptionStmt *n =
9663 						makeNode(AlterSubscriptionStmt);
9664 					n->kind = ALTER_SUBSCRIPTION_CONNECTION;
9665 					n->subname = $3;
9666 					n->conninfo = $5;
9667 					$$ = (Node *)n;
9668 				}
9669 			| ALTER SUBSCRIPTION name REFRESH PUBLICATION opt_definition
9670 				{
9671 					AlterSubscriptionStmt *n =
9672 						makeNode(AlterSubscriptionStmt);
9673 					n->kind = ALTER_SUBSCRIPTION_REFRESH;
9674 					n->subname = $3;
9675 					n->options = $6;
9676 					$$ = (Node *)n;
9677 				}
9678 			| ALTER SUBSCRIPTION name SET PUBLICATION publication_name_list opt_definition
9679 				{
9680 					AlterSubscriptionStmt *n =
9681 						makeNode(AlterSubscriptionStmt);
9682 					n->kind = ALTER_SUBSCRIPTION_PUBLICATION;
9683 					n->subname = $3;
9684 					n->publication = $6;
9685 					n->options = $7;
9686 					$$ = (Node *)n;
9687 				}
9688 			| ALTER SUBSCRIPTION name ENABLE_P
9689 				{
9690 					AlterSubscriptionStmt *n =
9691 						makeNode(AlterSubscriptionStmt);
9692 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9693 					n->subname = $3;
9694 					n->options = list_make1(makeDefElem("enabled",
9695 											(Node *)makeInteger(true), @1));
9696 					$$ = (Node *)n;
9697 				}
9698 			| ALTER SUBSCRIPTION name DISABLE_P
9699 				{
9700 					AlterSubscriptionStmt *n =
9701 						makeNode(AlterSubscriptionStmt);
9702 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9703 					n->subname = $3;
9704 					n->options = list_make1(makeDefElem("enabled",
9705 											(Node *)makeInteger(false), @1));
9706 					$$ = (Node *)n;
9707 				}
9708 		;
9709 
9710 /*****************************************************************************
9711  *
9712  * DROP SUBSCRIPTION [ IF EXISTS ] name
9713  *
9714  *****************************************************************************/
9715 
9716 DropSubscriptionStmt: DROP SUBSCRIPTION name opt_drop_behavior
9717 				{
9718 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9719 					n->subname = $3;
9720 					n->missing_ok = false;
9721 					n->behavior = $4;
9722 					$$ = (Node *) n;
9723 				}
9724 				|  DROP SUBSCRIPTION IF_P EXISTS name opt_drop_behavior
9725 				{
9726 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9727 					n->subname = $5;
9728 					n->missing_ok = true;
9729 					n->behavior = $6;
9730 					$$ = (Node *) n;
9731 				}
9732 		;
9733 
9734 /*****************************************************************************
9735  *
9736  *		QUERY:	Define Rewrite Rule
9737  *
9738  *****************************************************************************/
9739 
9740 RuleStmt:	CREATE opt_or_replace RULE name AS
9741 			ON event TO qualified_name where_clause
9742 			DO opt_instead RuleActionList
9743 				{
9744 					RuleStmt *n = makeNode(RuleStmt);
9745 					n->replace = $2;
9746 					n->relation = $9;
9747 					n->rulename = $4;
9748 					n->whereClause = $10;
9749 					n->event = $7;
9750 					n->instead = $12;
9751 					n->actions = $13;
9752 					$$ = (Node *)n;
9753 				}
9754 		;
9755 
9756 RuleActionList:
9757 			NOTHING									{ $$ = NIL; }
9758 			| RuleActionStmt						{ $$ = list_make1($1); }
9759 			| '(' RuleActionMulti ')'				{ $$ = $2; }
9760 		;
9761 
9762 /* the thrashing around here is to discard "empty" statements... */
9763 RuleActionMulti:
9764 			RuleActionMulti ';' RuleActionStmtOrEmpty
9765 				{ if ($3 != NULL)
9766 					$$ = lappend($1, $3);
9767 				  else
9768 					$$ = $1;
9769 				}
9770 			| RuleActionStmtOrEmpty
9771 				{ if ($1 != NULL)
9772 					$$ = list_make1($1);
9773 				  else
9774 					$$ = NIL;
9775 				}
9776 		;
9777 
9778 RuleActionStmt:
9779 			SelectStmt
9780 			| InsertStmt
9781 			| UpdateStmt
9782 			| DeleteStmt
9783 			| NotifyStmt
9784 		;
9785 
9786 RuleActionStmtOrEmpty:
9787 			RuleActionStmt							{ $$ = $1; }
9788 			|	/*EMPTY*/							{ $$ = NULL; }
9789 		;
9790 
9791 event:		SELECT									{ $$ = CMD_SELECT; }
9792 			| UPDATE								{ $$ = CMD_UPDATE; }
9793 			| DELETE_P								{ $$ = CMD_DELETE; }
9794 			| INSERT								{ $$ = CMD_INSERT; }
9795 		 ;
9796 
9797 opt_instead:
9798 			INSTEAD									{ $$ = true; }
9799 			| ALSO									{ $$ = false; }
9800 			| /*EMPTY*/								{ $$ = false; }
9801 		;
9802 
9803 
9804 /*****************************************************************************
9805  *
9806  *		QUERY:
9807  *				NOTIFY <identifier> can appear both in rule bodies and
9808  *				as a query-level command
9809  *
9810  *****************************************************************************/
9811 
9812 NotifyStmt: NOTIFY ColId notify_payload
9813 				{
9814 					NotifyStmt *n = makeNode(NotifyStmt);
9815 					n->conditionname = $2;
9816 					n->payload = $3;
9817 					$$ = (Node *)n;
9818 				}
9819 		;
9820 
9821 notify_payload:
9822 			',' Sconst							{ $$ = $2; }
9823 			| /*EMPTY*/							{ $$ = NULL; }
9824 		;
9825 
9826 ListenStmt: LISTEN ColId
9827 				{
9828 					ListenStmt *n = makeNode(ListenStmt);
9829 					n->conditionname = $2;
9830 					$$ = (Node *)n;
9831 				}
9832 		;
9833 
9834 UnlistenStmt:
9835 			UNLISTEN ColId
9836 				{
9837 					UnlistenStmt *n = makeNode(UnlistenStmt);
9838 					n->conditionname = $2;
9839 					$$ = (Node *)n;
9840 				}
9841 			| UNLISTEN '*'
9842 				{
9843 					UnlistenStmt *n = makeNode(UnlistenStmt);
9844 					n->conditionname = NULL;
9845 					$$ = (Node *)n;
9846 				}
9847 		;
9848 
9849 
9850 /*****************************************************************************
9851  *
9852  *		Transactions:
9853  *
9854  *		BEGIN / COMMIT / ROLLBACK
9855  *		(also older versions END / ABORT)
9856  *
9857  *****************************************************************************/
9858 
9859 TransactionStmt:
9860 			ABORT_P opt_transaction
9861 				{
9862 					TransactionStmt *n = makeNode(TransactionStmt);
9863 					n->kind = TRANS_STMT_ROLLBACK;
9864 					n->options = NIL;
9865 					$$ = (Node *)n;
9866 				}
9867 			| BEGIN_P opt_transaction transaction_mode_list_or_empty
9868 				{
9869 					TransactionStmt *n = makeNode(TransactionStmt);
9870 					n->kind = TRANS_STMT_BEGIN;
9871 					n->options = $3;
9872 					$$ = (Node *)n;
9873 				}
9874 			| START TRANSACTION transaction_mode_list_or_empty
9875 				{
9876 					TransactionStmt *n = makeNode(TransactionStmt);
9877 					n->kind = TRANS_STMT_START;
9878 					n->options = $3;
9879 					$$ = (Node *)n;
9880 				}
9881 			| COMMIT opt_transaction
9882 				{
9883 					TransactionStmt *n = makeNode(TransactionStmt);
9884 					n->kind = TRANS_STMT_COMMIT;
9885 					n->options = NIL;
9886 					$$ = (Node *)n;
9887 				}
9888 			| END_P opt_transaction
9889 				{
9890 					TransactionStmt *n = makeNode(TransactionStmt);
9891 					n->kind = TRANS_STMT_COMMIT;
9892 					n->options = NIL;
9893 					$$ = (Node *)n;
9894 				}
9895 			| ROLLBACK opt_transaction
9896 				{
9897 					TransactionStmt *n = makeNode(TransactionStmt);
9898 					n->kind = TRANS_STMT_ROLLBACK;
9899 					n->options = NIL;
9900 					$$ = (Node *)n;
9901 				}
9902 			| SAVEPOINT ColId
9903 				{
9904 					TransactionStmt *n = makeNode(TransactionStmt);
9905 					n->kind = TRANS_STMT_SAVEPOINT;
9906 					n->savepoint_name = $2;
9907 					$$ = (Node *)n;
9908 				}
9909 			| RELEASE SAVEPOINT ColId
9910 				{
9911 					TransactionStmt *n = makeNode(TransactionStmt);
9912 					n->kind = TRANS_STMT_RELEASE;
9913 					n->savepoint_name = $3;
9914 					$$ = (Node *)n;
9915 				}
9916 			| RELEASE ColId
9917 				{
9918 					TransactionStmt *n = makeNode(TransactionStmt);
9919 					n->kind = TRANS_STMT_RELEASE;
9920 					n->savepoint_name = $2;
9921 					$$ = (Node *)n;
9922 				}
9923 			| ROLLBACK opt_transaction TO SAVEPOINT ColId
9924 				{
9925 					TransactionStmt *n = makeNode(TransactionStmt);
9926 					n->kind = TRANS_STMT_ROLLBACK_TO;
9927 					n->savepoint_name = $5;
9928 					$$ = (Node *)n;
9929 				}
9930 			| ROLLBACK opt_transaction TO ColId
9931 				{
9932 					TransactionStmt *n = makeNode(TransactionStmt);
9933 					n->kind = TRANS_STMT_ROLLBACK_TO;
9934 					n->savepoint_name = $4;
9935 					$$ = (Node *)n;
9936 				}
9937 			| PREPARE TRANSACTION Sconst
9938 				{
9939 					TransactionStmt *n = makeNode(TransactionStmt);
9940 					n->kind = TRANS_STMT_PREPARE;
9941 					n->gid = $3;
9942 					$$ = (Node *)n;
9943 				}
9944 			| COMMIT PREPARED Sconst
9945 				{
9946 					TransactionStmt *n = makeNode(TransactionStmt);
9947 					n->kind = TRANS_STMT_COMMIT_PREPARED;
9948 					n->gid = $3;
9949 					$$ = (Node *)n;
9950 				}
9951 			| ROLLBACK PREPARED Sconst
9952 				{
9953 					TransactionStmt *n = makeNode(TransactionStmt);
9954 					n->kind = TRANS_STMT_ROLLBACK_PREPARED;
9955 					n->gid = $3;
9956 					$$ = (Node *)n;
9957 				}
9958 		;
9959 
9960 opt_transaction:	WORK							{}
9961 			| TRANSACTION							{}
9962 			| /*EMPTY*/								{}
9963 		;
9964 
9965 transaction_mode_item:
9966 			ISOLATION LEVEL iso_level
9967 					{ $$ = makeDefElem("transaction_isolation",
9968 									   makeStringConst($3, @3), @1); }
9969 			| READ ONLY
9970 					{ $$ = makeDefElem("transaction_read_only",
9971 									   makeIntConst(true, @1), @1); }
9972 			| READ WRITE
9973 					{ $$ = makeDefElem("transaction_read_only",
9974 									   makeIntConst(false, @1), @1); }
9975 			| DEFERRABLE
9976 					{ $$ = makeDefElem("transaction_deferrable",
9977 									   makeIntConst(true, @1), @1); }
9978 			| NOT DEFERRABLE
9979 					{ $$ = makeDefElem("transaction_deferrable",
9980 									   makeIntConst(false, @1), @1); }
9981 		;
9982 
9983 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
9984 transaction_mode_list:
9985 			transaction_mode_item
9986 					{ $$ = list_make1($1); }
9987 			| transaction_mode_list ',' transaction_mode_item
9988 					{ $$ = lappend($1, $3); }
9989 			| transaction_mode_list transaction_mode_item
9990 					{ $$ = lappend($1, $2); }
9991 		;
9992 
9993 transaction_mode_list_or_empty:
9994 			transaction_mode_list
9995 			| /* EMPTY */
9996 					{ $$ = NIL; }
9997 		;
9998 
9999 
10000 /*****************************************************************************
10001  *
10002  *	QUERY:
10003  *		CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
10004  *			AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
10005  *
10006  *****************************************************************************/
10007 
10008 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10009 				AS SelectStmt opt_check_option
10010 				{
10011 					ViewStmt *n = makeNode(ViewStmt);
10012 					n->view = $4;
10013 					n->view->relpersistence = $2;
10014 					n->aliases = $5;
10015 					n->query = $8;
10016 					n->replace = false;
10017 					n->options = $6;
10018 					n->withCheckOption = $9;
10019 					$$ = (Node *) n;
10020 				}
10021 		| CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10022 				AS SelectStmt opt_check_option
10023 				{
10024 					ViewStmt *n = makeNode(ViewStmt);
10025 					n->view = $6;
10026 					n->view->relpersistence = $4;
10027 					n->aliases = $7;
10028 					n->query = $10;
10029 					n->replace = true;
10030 					n->options = $8;
10031 					n->withCheckOption = $11;
10032 					$$ = (Node *) n;
10033 				}
10034 		| CREATE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10035 				AS SelectStmt opt_check_option
10036 				{
10037 					ViewStmt *n = makeNode(ViewStmt);
10038 					n->view = $5;
10039 					n->view->relpersistence = $2;
10040 					n->aliases = $7;
10041 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $11);
10042 					n->replace = false;
10043 					n->options = $9;
10044 					n->withCheckOption = $12;
10045 					if (n->withCheckOption != NO_CHECK_OPTION)
10046 						ereport(ERROR,
10047 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10048 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
10049 								 parser_errposition(@12)));
10050 					$$ = (Node *) n;
10051 				}
10052 		| CREATE OR REPLACE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10053 				AS SelectStmt opt_check_option
10054 				{
10055 					ViewStmt *n = makeNode(ViewStmt);
10056 					n->view = $7;
10057 					n->view->relpersistence = $4;
10058 					n->aliases = $9;
10059 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $13);
10060 					n->replace = true;
10061 					n->options = $11;
10062 					n->withCheckOption = $14;
10063 					if (n->withCheckOption != NO_CHECK_OPTION)
10064 						ereport(ERROR,
10065 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10066 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
10067 								 parser_errposition(@14)));
10068 					$$ = (Node *) n;
10069 				}
10070 		;
10071 
10072 opt_check_option:
10073 		WITH CHECK OPTION				{ $$ = CASCADED_CHECK_OPTION; }
10074 		| WITH CASCADED CHECK OPTION	{ $$ = CASCADED_CHECK_OPTION; }
10075 		| WITH LOCAL CHECK OPTION		{ $$ = LOCAL_CHECK_OPTION; }
10076 		| /* EMPTY */					{ $$ = NO_CHECK_OPTION; }
10077 		;
10078 
10079 /*****************************************************************************
10080  *
10081  *		QUERY:
10082  *				LOAD "filename"
10083  *
10084  *****************************************************************************/
10085 
10086 LoadStmt:	LOAD file_name
10087 				{
10088 					LoadStmt *n = makeNode(LoadStmt);
10089 					n->filename = $2;
10090 					$$ = (Node *)n;
10091 				}
10092 		;
10093 
10094 
10095 /*****************************************************************************
10096  *
10097  *		CREATE DATABASE
10098  *
10099  *****************************************************************************/
10100 
10101 CreatedbStmt:
10102 			CREATE DATABASE database_name opt_with createdb_opt_list
10103 				{
10104 					CreatedbStmt *n = makeNode(CreatedbStmt);
10105 					n->dbname = $3;
10106 					n->options = $5;
10107 					$$ = (Node *)n;
10108 				}
10109 		;
10110 
10111 createdb_opt_list:
10112 			createdb_opt_items						{ $$ = $1; }
10113 			| /* EMPTY */							{ $$ = NIL; }
10114 		;
10115 
10116 createdb_opt_items:
10117 			createdb_opt_item						{ $$ = list_make1($1); }
10118 			| createdb_opt_items createdb_opt_item	{ $$ = lappend($1, $2); }
10119 		;
10120 
10121 createdb_opt_item:
10122 			createdb_opt_name opt_equal SignedIconst
10123 				{
10124 					$$ = makeDefElem($1, (Node *)makeInteger($3), @1);
10125 				}
10126 			| createdb_opt_name opt_equal opt_boolean_or_string
10127 				{
10128 					$$ = makeDefElem($1, (Node *)makeString($3), @1);
10129 				}
10130 			| createdb_opt_name opt_equal DEFAULT
10131 				{
10132 					$$ = makeDefElem($1, NULL, @1);
10133 				}
10134 		;
10135 
10136 /*
10137  * Ideally we'd use ColId here, but that causes shift/reduce conflicts against
10138  * the ALTER DATABASE SET/RESET syntaxes.  Instead call out specific keywords
10139  * we need, and allow IDENT so that database option names don't have to be
10140  * parser keywords unless they are already keywords for other reasons.
10141  *
10142  * XXX this coding technique is fragile since if someone makes a formerly
10143  * non-keyword option name into a keyword and forgets to add it here, the
10144  * option will silently break.  Best defense is to provide a regression test
10145  * exercising every such option, at least at the syntax level.
10146  */
10147 createdb_opt_name:
10148 			IDENT							{ $$ = $1; }
10149 			| CONNECTION LIMIT				{ $$ = pstrdup("connection_limit"); }
10150 			| ENCODING						{ $$ = pstrdup($1); }
10151 			| LOCATION						{ $$ = pstrdup($1); }
10152 			| OWNER							{ $$ = pstrdup($1); }
10153 			| TABLESPACE					{ $$ = pstrdup($1); }
10154 			| TEMPLATE						{ $$ = pstrdup($1); }
10155 		;
10156 
10157 /*
10158  *	Though the equals sign doesn't match other WITH options, pg_dump uses
10159  *	equals for backward compatibility, and it doesn't seem worth removing it.
10160  */
10161 opt_equal:	'='										{}
10162 			| /*EMPTY*/								{}
10163 		;
10164 
10165 
10166 /*****************************************************************************
10167  *
10168  *		ALTER DATABASE
10169  *
10170  *****************************************************************************/
10171 
10172 AlterDatabaseStmt:
10173 			ALTER DATABASE database_name WITH createdb_opt_list
10174 				 {
10175 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10176 					n->dbname = $3;
10177 					n->options = $5;
10178 					$$ = (Node *)n;
10179 				 }
10180 			| ALTER DATABASE database_name createdb_opt_list
10181 				 {
10182 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10183 					n->dbname = $3;
10184 					n->options = $4;
10185 					$$ = (Node *)n;
10186 				 }
10187 			| ALTER DATABASE database_name SET TABLESPACE name
10188 				 {
10189 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10190 					n->dbname = $3;
10191 					n->options = list_make1(makeDefElem("tablespace",
10192 														(Node *)makeString($6), @6));
10193 					$$ = (Node *)n;
10194 				 }
10195 		;
10196 
10197 AlterDatabaseSetStmt:
10198 			ALTER DATABASE database_name SetResetClause
10199 				{
10200 					AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
10201 					n->dbname = $3;
10202 					n->setstmt = $4;
10203 					$$ = (Node *)n;
10204 				}
10205 		;
10206 
10207 
10208 /*****************************************************************************
10209  *
10210  *		DROP DATABASE [ IF EXISTS ]
10211  *
10212  * This is implicitly CASCADE, no need for drop behavior
10213  *****************************************************************************/
10214 
10215 DropdbStmt: DROP DATABASE database_name
10216 				{
10217 					DropdbStmt *n = makeNode(DropdbStmt);
10218 					n->dbname = $3;
10219 					n->missing_ok = false;
10220 					$$ = (Node *)n;
10221 				}
10222 			| DROP DATABASE IF_P EXISTS database_name
10223 				{
10224 					DropdbStmt *n = makeNode(DropdbStmt);
10225 					n->dbname = $5;
10226 					n->missing_ok = true;
10227 					$$ = (Node *)n;
10228 				}
10229 		;
10230 
10231 
10232 /*****************************************************************************
10233  *
10234  *		ALTER COLLATION
10235  *
10236  *****************************************************************************/
10237 
10238 AlterCollationStmt: ALTER COLLATION any_name REFRESH VERSION_P
10239 				{
10240 					AlterCollationStmt *n = makeNode(AlterCollationStmt);
10241 					n->collname = $3;
10242 					$$ = (Node *)n;
10243 				}
10244 		;
10245 
10246 
10247 /*****************************************************************************
10248  *
10249  *		ALTER SYSTEM
10250  *
10251  * This is used to change configuration parameters persistently.
10252  *****************************************************************************/
10253 
10254 AlterSystemStmt:
10255 			ALTER SYSTEM_P SET generic_set
10256 				{
10257 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
10258 					n->setstmt = $4;
10259 					$$ = (Node *)n;
10260 				}
10261 			| ALTER SYSTEM_P RESET generic_reset
10262 				{
10263 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
10264 					n->setstmt = $4;
10265 					$$ = (Node *)n;
10266 				}
10267 		;
10268 
10269 
10270 /*****************************************************************************
10271  *
10272  * Manipulate a domain
10273  *
10274  *****************************************************************************/
10275 
10276 CreateDomainStmt:
10277 			CREATE DOMAIN_P any_name opt_as Typename ColQualList
10278 				{
10279 					CreateDomainStmt *n = makeNode(CreateDomainStmt);
10280 					n->domainname = $3;
10281 					n->typeName = $5;
10282 					SplitColQualList($6, &n->constraints, &n->collClause,
10283 									 yyscanner);
10284 					$$ = (Node *)n;
10285 				}
10286 		;
10287 
10288 AlterDomainStmt:
10289 			/* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
10290 			ALTER DOMAIN_P any_name alter_column_default
10291 				{
10292 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10293 					n->subtype = 'T';
10294 					n->typeName = $3;
10295 					n->def = $4;
10296 					$$ = (Node *)n;
10297 				}
10298 			/* ALTER DOMAIN <domain> DROP NOT NULL */
10299 			| ALTER DOMAIN_P any_name DROP NOT NULL_P
10300 				{
10301 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10302 					n->subtype = 'N';
10303 					n->typeName = $3;
10304 					$$ = (Node *)n;
10305 				}
10306 			/* ALTER DOMAIN <domain> SET NOT NULL */
10307 			| ALTER DOMAIN_P any_name SET NOT NULL_P
10308 				{
10309 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10310 					n->subtype = 'O';
10311 					n->typeName = $3;
10312 					$$ = (Node *)n;
10313 				}
10314 			/* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
10315 			| ALTER DOMAIN_P any_name ADD_P TableConstraint
10316 				{
10317 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10318 					n->subtype = 'C';
10319 					n->typeName = $3;
10320 					n->def = $5;
10321 					$$ = (Node *)n;
10322 				}
10323 			/* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
10324 			| ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
10325 				{
10326 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10327 					n->subtype = 'X';
10328 					n->typeName = $3;
10329 					n->name = $6;
10330 					n->behavior = $7;
10331 					n->missing_ok = false;
10332 					$$ = (Node *)n;
10333 				}
10334 			/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
10335 			| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
10336 				{
10337 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10338 					n->subtype = 'X';
10339 					n->typeName = $3;
10340 					n->name = $8;
10341 					n->behavior = $9;
10342 					n->missing_ok = true;
10343 					$$ = (Node *)n;
10344 				}
10345 			/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
10346 			| ALTER DOMAIN_P any_name VALIDATE CONSTRAINT name
10347 				{
10348 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10349 					n->subtype = 'V';
10350 					n->typeName = $3;
10351 					n->name = $6;
10352 					$$ = (Node *)n;
10353 				}
10354 			;
10355 
10356 opt_as:		AS										{}
10357 			| /* EMPTY */							{}
10358 		;
10359 
10360 
10361 /*****************************************************************************
10362  *
10363  * Manipulate a text search dictionary or configuration
10364  *
10365  *****************************************************************************/
10366 
10367 AlterTSDictionaryStmt:
10368 			ALTER TEXT_P SEARCH DICTIONARY any_name definition
10369 				{
10370 					AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
10371 					n->dictname = $5;
10372 					n->options = $6;
10373 					$$ = (Node *)n;
10374 				}
10375 		;
10376 
10377 AlterTSConfigurationStmt:
10378 			ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list any_with any_name_list
10379 				{
10380 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10381 					n->kind = ALTER_TSCONFIG_ADD_MAPPING;
10382 					n->cfgname = $5;
10383 					n->tokentype = $9;
10384 					n->dicts = $11;
10385 					n->override = false;
10386 					n->replace = false;
10387 					$$ = (Node*)n;
10388 				}
10389 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list any_with any_name_list
10390 				{
10391 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10392 					n->kind = ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN;
10393 					n->cfgname = $5;
10394 					n->tokentype = $9;
10395 					n->dicts = $11;
10396 					n->override = true;
10397 					n->replace = false;
10398 					$$ = (Node*)n;
10399 				}
10400 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name any_with any_name
10401 				{
10402 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10403 					n->kind = ALTER_TSCONFIG_REPLACE_DICT;
10404 					n->cfgname = $5;
10405 					n->tokentype = NIL;
10406 					n->dicts = list_make2($9,$11);
10407 					n->override = false;
10408 					n->replace = true;
10409 					$$ = (Node*)n;
10410 				}
10411 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name any_with any_name
10412 				{
10413 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10414 					n->kind = ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN;
10415 					n->cfgname = $5;
10416 					n->tokentype = $9;
10417 					n->dicts = list_make2($11,$13);
10418 					n->override = false;
10419 					n->replace = true;
10420 					$$ = (Node*)n;
10421 				}
10422 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
10423 				{
10424 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10425 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10426 					n->cfgname = $5;
10427 					n->tokentype = $9;
10428 					n->missing_ok = false;
10429 					$$ = (Node*)n;
10430 				}
10431 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
10432 				{
10433 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10434 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10435 					n->cfgname = $5;
10436 					n->tokentype = $11;
10437 					n->missing_ok = true;
10438 					$$ = (Node*)n;
10439 				}
10440 		;
10441 
10442 /* Use this if TIME or ORDINALITY after WITH should be taken as an identifier */
10443 any_with:	WITH									{}
10444 			| WITH_LA								{}
10445 		;
10446 
10447 
10448 /*****************************************************************************
10449  *
10450  * Manipulate a conversion
10451  *
10452  *		CREATE [DEFAULT] CONVERSION <conversion_name>
10453  *		FOR <encoding_name> TO <encoding_name> FROM <func_name>
10454  *
10455  *****************************************************************************/
10456 
10457 CreateConversionStmt:
10458 			CREATE opt_default CONVERSION_P any_name FOR Sconst
10459 			TO Sconst FROM any_name
10460 			{
10461 				CreateConversionStmt *n = makeNode(CreateConversionStmt);
10462 				n->conversion_name = $4;
10463 				n->for_encoding_name = $6;
10464 				n->to_encoding_name = $8;
10465 				n->func_name = $10;
10466 				n->def = $2;
10467 				$$ = (Node *)n;
10468 			}
10469 		;
10470 
10471 /*****************************************************************************
10472  *
10473  *		QUERY:
10474  *				CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
10475  *				CLUSTER [VERBOSE]
10476  *				CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
10477  *
10478  *****************************************************************************/
10479 
10480 ClusterStmt:
10481 			CLUSTER opt_verbose qualified_name cluster_index_specification
10482 				{
10483 					ClusterStmt *n = makeNode(ClusterStmt);
10484 					n->relation = $3;
10485 					n->indexname = $4;
10486 					n->verbose = $2;
10487 					$$ = (Node*)n;
10488 				}
10489 			| CLUSTER opt_verbose
10490 				{
10491 					ClusterStmt *n = makeNode(ClusterStmt);
10492 					n->relation = NULL;
10493 					n->indexname = NULL;
10494 					n->verbose = $2;
10495 					$$ = (Node*)n;
10496 				}
10497 			/* kept for pre-8.3 compatibility */
10498 			| CLUSTER opt_verbose index_name ON qualified_name
10499 				{
10500 					ClusterStmt *n = makeNode(ClusterStmt);
10501 					n->relation = $5;
10502 					n->indexname = $3;
10503 					n->verbose = $2;
10504 					$$ = (Node*)n;
10505 				}
10506 		;
10507 
10508 cluster_index_specification:
10509 			USING index_name		{ $$ = $2; }
10510 			| /*EMPTY*/				{ $$ = NULL; }
10511 		;
10512 
10513 
10514 /*****************************************************************************
10515  *
10516  *		QUERY:
10517  *				VACUUM
10518  *				ANALYZE
10519  *
10520  *****************************************************************************/
10521 
10522 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose opt_analyze opt_vacuum_relation_list
10523 				{
10524 					VacuumStmt *n = makeNode(VacuumStmt);
10525 					n->options = VACOPT_VACUUM;
10526 					if ($2)
10527 						n->options |= VACOPT_FULL;
10528 					if ($3)
10529 						n->options |= VACOPT_FREEZE;
10530 					if ($4)
10531 						n->options |= VACOPT_VERBOSE;
10532 					if ($5)
10533 						n->options |= VACOPT_ANALYZE;
10534 					n->rels = $6;
10535 					$$ = (Node *)n;
10536 				}
10537 			| VACUUM '(' vacuum_option_list ')' opt_vacuum_relation_list
10538 				{
10539 					VacuumStmt *n = makeNode(VacuumStmt);
10540 					n->options = VACOPT_VACUUM | $3;
10541 					n->rels = $5;
10542 					$$ = (Node *) n;
10543 				}
10544 		;
10545 
10546 vacuum_option_list:
10547 			vacuum_option_elem								{ $$ = $1; }
10548 			| vacuum_option_list ',' vacuum_option_elem		{ $$ = $1 | $3; }
10549 		;
10550 
10551 vacuum_option_elem:
10552 			analyze_keyword		{ $$ = VACOPT_ANALYZE; }
10553 			| VERBOSE			{ $$ = VACOPT_VERBOSE; }
10554 			| FREEZE			{ $$ = VACOPT_FREEZE; }
10555 			| FULL				{ $$ = VACOPT_FULL; }
10556 			| IDENT
10557 				{
10558 					if (strcmp($1, "disable_page_skipping") == 0)
10559 						$$ = VACOPT_DISABLE_PAGE_SKIPPING;
10560 					else
10561 						ereport(ERROR,
10562 								(errcode(ERRCODE_SYNTAX_ERROR),
10563 							 errmsg("unrecognized VACUUM option \"%s\"", $1),
10564 									 parser_errposition(@1)));
10565 				}
10566 		;
10567 
10568 AnalyzeStmt: analyze_keyword opt_verbose opt_vacuum_relation_list
10569 				{
10570 					VacuumStmt *n = makeNode(VacuumStmt);
10571 					n->options = VACOPT_ANALYZE;
10572 					if ($2)
10573 						n->options |= VACOPT_VERBOSE;
10574 					n->rels = $3;
10575 					$$ = (Node *)n;
10576 				}
10577 			| analyze_keyword '(' analyze_option_list ')' opt_vacuum_relation_list
10578 				{
10579 					VacuumStmt *n = makeNode(VacuumStmt);
10580 					n->options = VACOPT_ANALYZE | $3;
10581 					n->rels = $5;
10582 					$$ = (Node *) n;
10583 				}
10584 		;
10585 
10586 analyze_option_list:
10587 			analyze_option_elem								{ $$ = $1; }
10588 			| analyze_option_list ',' analyze_option_elem	{ $$ = $1 | $3; }
10589 		;
10590 
10591 analyze_option_elem:
10592 			VERBOSE				{ $$ = VACOPT_VERBOSE; }
10593 		;
10594 
10595 analyze_keyword:
10596 			ANALYZE									{}
10597 			| ANALYSE /* British */					{}
10598 		;
10599 
10600 opt_analyze:
10601 			analyze_keyword							{ $$ = true; }
10602 			| /*EMPTY*/								{ $$ = false; }
10603 		;
10604 
10605 opt_verbose:
10606 			VERBOSE									{ $$ = true; }
10607 			| /*EMPTY*/								{ $$ = false; }
10608 		;
10609 
10610 opt_full:	FULL									{ $$ = true; }
10611 			| /*EMPTY*/								{ $$ = false; }
10612 		;
10613 
10614 opt_freeze: FREEZE									{ $$ = true; }
10615 			| /*EMPTY*/								{ $$ = false; }
10616 		;
10617 
10618 opt_name_list:
10619 			'(' name_list ')'						{ $$ = $2; }
10620 			| /*EMPTY*/								{ $$ = NIL; }
10621 		;
10622 
10623 vacuum_relation:
10624 			qualified_name opt_name_list
10625 				{
10626 					$$ = (Node *) makeVacuumRelation($1, InvalidOid, $2);
10627 				}
10628 		;
10629 
10630 vacuum_relation_list:
10631 			vacuum_relation
10632 					{ $$ = list_make1($1); }
10633 			| vacuum_relation_list ',' vacuum_relation
10634 					{ $$ = lappend($1, $3); }
10635 		;
10636 
10637 opt_vacuum_relation_list:
10638 			vacuum_relation_list					{ $$ = $1; }
10639 			| /*EMPTY*/								{ $$ = NIL; }
10640 		;
10641 
10642 
10643 /*****************************************************************************
10644  *
10645  *		QUERY:
10646  *				EXPLAIN [ANALYZE] [VERBOSE] query
10647  *				EXPLAIN ( options ) query
10648  *
10649  *****************************************************************************/
10650 
10651 ExplainStmt:
10652 		EXPLAIN ExplainableStmt
10653 				{
10654 					ExplainStmt *n = makeNode(ExplainStmt);
10655 					n->query = $2;
10656 					n->options = NIL;
10657 					$$ = (Node *) n;
10658 				}
10659 		| EXPLAIN analyze_keyword opt_verbose ExplainableStmt
10660 				{
10661 					ExplainStmt *n = makeNode(ExplainStmt);
10662 					n->query = $4;
10663 					n->options = list_make1(makeDefElem("analyze", NULL, @2));
10664 					if ($3)
10665 						n->options = lappend(n->options,
10666 											 makeDefElem("verbose", NULL, @3));
10667 					$$ = (Node *) n;
10668 				}
10669 		| EXPLAIN VERBOSE ExplainableStmt
10670 				{
10671 					ExplainStmt *n = makeNode(ExplainStmt);
10672 					n->query = $3;
10673 					n->options = list_make1(makeDefElem("verbose", NULL, @2));
10674 					$$ = (Node *) n;
10675 				}
10676 		| EXPLAIN '(' explain_option_list ')' ExplainableStmt
10677 				{
10678 					ExplainStmt *n = makeNode(ExplainStmt);
10679 					n->query = $5;
10680 					n->options = $3;
10681 					$$ = (Node *) n;
10682 				}
10683 		;
10684 
10685 ExplainableStmt:
10686 			SelectStmt
10687 			| InsertStmt
10688 			| UpdateStmt
10689 			| DeleteStmt
10690 			| DeclareCursorStmt
10691 			| CreateAsStmt
10692 			| CreateMatViewStmt
10693 			| RefreshMatViewStmt
10694 			| ExecuteStmt					/* by default all are $$=$1 */
10695 		;
10696 
10697 explain_option_list:
10698 			explain_option_elem
10699 				{
10700 					$$ = list_make1($1);
10701 				}
10702 			| explain_option_list ',' explain_option_elem
10703 				{
10704 					$$ = lappend($1, $3);
10705 				}
10706 		;
10707 
10708 explain_option_elem:
10709 			explain_option_name explain_option_arg
10710 				{
10711 					$$ = makeDefElem($1, $2, @1);
10712 				}
10713 		;
10714 
10715 explain_option_name:
10716 			NonReservedWord			{ $$ = $1; }
10717 			| analyze_keyword		{ $$ = "analyze"; }
10718 		;
10719 
10720 explain_option_arg:
10721 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
10722 			| NumericOnly			{ $$ = (Node *) $1; }
10723 			| /* EMPTY */			{ $$ = NULL; }
10724 		;
10725 
10726 /*****************************************************************************
10727  *
10728  *		QUERY:
10729  *				PREPARE <plan_name> [(args, ...)] AS <query>
10730  *
10731  *****************************************************************************/
10732 
10733 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
10734 				{
10735 					PrepareStmt *n = makeNode(PrepareStmt);
10736 					n->name = $2;
10737 					n->argtypes = $3;
10738 					n->query = $5;
10739 					$$ = (Node *) n;
10740 				}
10741 		;
10742 
10743 prep_type_clause: '(' type_list ')'			{ $$ = $2; }
10744 				| /* EMPTY */				{ $$ = NIL; }
10745 		;
10746 
10747 PreparableStmt:
10748 			SelectStmt
10749 			| InsertStmt
10750 			| UpdateStmt
10751 			| DeleteStmt					/* by default all are $$=$1 */
10752 		;
10753 
10754 /*****************************************************************************
10755  *
10756  * EXECUTE <plan_name> [(params, ...)]
10757  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
10758  *
10759  *****************************************************************************/
10760 
10761 ExecuteStmt: EXECUTE name execute_param_clause
10762 				{
10763 					ExecuteStmt *n = makeNode(ExecuteStmt);
10764 					n->name = $2;
10765 					n->params = $3;
10766 					$$ = (Node *) n;
10767 				}
10768 			| CREATE OptTemp TABLE create_as_target AS
10769 				EXECUTE name execute_param_clause opt_with_data
10770 				{
10771 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10772 					ExecuteStmt *n = makeNode(ExecuteStmt);
10773 					n->name = $7;
10774 					n->params = $8;
10775 					ctas->query = (Node *) n;
10776 					ctas->into = $4;
10777 					ctas->relkind = OBJECT_TABLE;
10778 					ctas->is_select_into = false;
10779 					ctas->if_not_exists = false;
10780 					/* cram additional flags into the IntoClause */
10781 					$4->rel->relpersistence = $2;
10782 					$4->skipData = !($9);
10783 					$$ = (Node *) ctas;
10784 				}
10785 			| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS
10786 				EXECUTE name execute_param_clause opt_with_data
10787 				{
10788 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10789 					ExecuteStmt *n = makeNode(ExecuteStmt);
10790 					n->name = $10;
10791 					n->params = $11;
10792 					ctas->query = (Node *) n;
10793 					ctas->into = $7;
10794 					ctas->relkind = OBJECT_TABLE;
10795 					ctas->is_select_into = false;
10796 					ctas->if_not_exists = true;
10797 					/* cram additional flags into the IntoClause */
10798 					$7->rel->relpersistence = $2;
10799 					$7->skipData = !($12);
10800 					$$ = (Node *) ctas;
10801 				}
10802 		;
10803 
10804 execute_param_clause: '(' expr_list ')'				{ $$ = $2; }
10805 					| /* EMPTY */					{ $$ = NIL; }
10806 					;
10807 
10808 /*****************************************************************************
10809  *
10810  *		QUERY:
10811  *				DEALLOCATE [PREPARE] <plan_name>
10812  *
10813  *****************************************************************************/
10814 
10815 DeallocateStmt: DEALLOCATE name
10816 					{
10817 						DeallocateStmt *n = makeNode(DeallocateStmt);
10818 						n->name = $2;
10819 						$$ = (Node *) n;
10820 					}
10821 				| DEALLOCATE PREPARE name
10822 					{
10823 						DeallocateStmt *n = makeNode(DeallocateStmt);
10824 						n->name = $3;
10825 						$$ = (Node *) n;
10826 					}
10827 				| DEALLOCATE ALL
10828 					{
10829 						DeallocateStmt *n = makeNode(DeallocateStmt);
10830 						n->name = NULL;
10831 						$$ = (Node *) n;
10832 					}
10833 				| DEALLOCATE PREPARE ALL
10834 					{
10835 						DeallocateStmt *n = makeNode(DeallocateStmt);
10836 						n->name = NULL;
10837 						$$ = (Node *) n;
10838 					}
10839 		;
10840 
10841 /*****************************************************************************
10842  *
10843  *		QUERY:
10844  *				INSERT STATEMENTS
10845  *
10846  *****************************************************************************/
10847 
10848 InsertStmt:
10849 			opt_with_clause INSERT INTO insert_target insert_rest
10850 			opt_on_conflict returning_clause
10851 				{
10852 					$5->relation = $4;
10853 					$5->onConflictClause = $6;
10854 					$5->returningList = $7;
10855 					$5->withClause = $1;
10856 					$$ = (Node *) $5;
10857 				}
10858 		;
10859 
10860 /*
10861  * Can't easily make AS optional here, because VALUES in insert_rest would
10862  * have a shift/reduce conflict with VALUES as an optional alias.  We could
10863  * easily allow unreserved_keywords as optional aliases, but that'd be an odd
10864  * divergence from other places.  So just require AS for now.
10865  */
10866 insert_target:
10867 			qualified_name
10868 				{
10869 					$$ = $1;
10870 				}
10871 			| qualified_name AS ColId
10872 				{
10873 					$1->alias = makeAlias($3, NIL);
10874 					$$ = $1;
10875 				}
10876 		;
10877 
10878 insert_rest:
10879 			SelectStmt
10880 				{
10881 					$$ = makeNode(InsertStmt);
10882 					$$->cols = NIL;
10883 					$$->selectStmt = $1;
10884 				}
10885 			| OVERRIDING override_kind VALUE_P SelectStmt
10886 				{
10887 					$$ = makeNode(InsertStmt);
10888 					$$->cols = NIL;
10889 					$$->override = $2;
10890 					$$->selectStmt = $4;
10891 				}
10892 			| '(' insert_column_list ')' SelectStmt
10893 				{
10894 					$$ = makeNode(InsertStmt);
10895 					$$->cols = $2;
10896 					$$->selectStmt = $4;
10897 				}
10898 			| '(' insert_column_list ')' OVERRIDING override_kind VALUE_P SelectStmt
10899 				{
10900 					$$ = makeNode(InsertStmt);
10901 					$$->cols = $2;
10902 					$$->override = $5;
10903 					$$->selectStmt = $7;
10904 				}
10905 			| DEFAULT VALUES
10906 				{
10907 					$$ = makeNode(InsertStmt);
10908 					$$->cols = NIL;
10909 					$$->selectStmt = NULL;
10910 				}
10911 		;
10912 
10913 override_kind:
10914 			USER		{ $$ = OVERRIDING_USER_VALUE; }
10915 			| SYSTEM_P	{ $$ = OVERRIDING_SYSTEM_VALUE; }
10916 		;
10917 
10918 insert_column_list:
10919 			insert_column_item
10920 					{ $$ = list_make1($1); }
10921 			| insert_column_list ',' insert_column_item
10922 					{ $$ = lappend($1, $3); }
10923 		;
10924 
10925 insert_column_item:
10926 			ColId opt_indirection
10927 				{
10928 					$$ = makeNode(ResTarget);
10929 					$$->name = $1;
10930 					$$->indirection = check_indirection($2, yyscanner);
10931 					$$->val = NULL;
10932 					$$->location = @1;
10933 				}
10934 		;
10935 
10936 opt_on_conflict:
10937 			ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list	where_clause
10938 				{
10939 					$$ = makeNode(OnConflictClause);
10940 					$$->action = ONCONFLICT_UPDATE;
10941 					$$->infer = $3;
10942 					$$->targetList = $7;
10943 					$$->whereClause = $8;
10944 					$$->location = @1;
10945 				}
10946 			|
10947 			ON CONFLICT opt_conf_expr DO NOTHING
10948 				{
10949 					$$ = makeNode(OnConflictClause);
10950 					$$->action = ONCONFLICT_NOTHING;
10951 					$$->infer = $3;
10952 					$$->targetList = NIL;
10953 					$$->whereClause = NULL;
10954 					$$->location = @1;
10955 				}
10956 			| /*EMPTY*/
10957 				{
10958 					$$ = NULL;
10959 				}
10960 		;
10961 
10962 opt_conf_expr:
10963 			'(' index_params ')' where_clause
10964 				{
10965 					$$ = makeNode(InferClause);
10966 					$$->indexElems = $2;
10967 					$$->whereClause = $4;
10968 					$$->conname = NULL;
10969 					$$->location = @1;
10970 				}
10971 			|
10972 			ON CONSTRAINT name
10973 				{
10974 					$$ = makeNode(InferClause);
10975 					$$->indexElems = NIL;
10976 					$$->whereClause = NULL;
10977 					$$->conname = $3;
10978 					$$->location = @1;
10979 				}
10980 			| /*EMPTY*/
10981 				{
10982 					$$ = NULL;
10983 				}
10984 		;
10985 
10986 returning_clause:
10987 			RETURNING target_list		{ $$ = $2; }
10988 			| /* EMPTY */				{ $$ = NIL; }
10989 		;
10990 
10991 
10992 /*****************************************************************************
10993  *
10994  *		QUERY:
10995  *				DELETE STATEMENTS
10996  *
10997  *****************************************************************************/
10998 
10999 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
11000 			using_clause where_or_current_clause returning_clause
11001 				{
11002 					DeleteStmt *n = makeNode(DeleteStmt);
11003 					n->relation = $4;
11004 					n->usingClause = $5;
11005 					n->whereClause = $6;
11006 					n->returningList = $7;
11007 					n->withClause = $1;
11008 					$$ = (Node *)n;
11009 				}
11010 		;
11011 
11012 using_clause:
11013 				USING from_list						{ $$ = $2; }
11014 			| /*EMPTY*/								{ $$ = NIL; }
11015 		;
11016 
11017 
11018 /*****************************************************************************
11019  *
11020  *		QUERY:
11021  *				LOCK TABLE
11022  *
11023  *****************************************************************************/
11024 
11025 LockStmt:	LOCK_P opt_table relation_expr_list opt_lock opt_nowait
11026 				{
11027 					LockStmt *n = makeNode(LockStmt);
11028 
11029 					n->relations = $3;
11030 					n->mode = $4;
11031 					n->nowait = $5;
11032 					$$ = (Node *)n;
11033 				}
11034 		;
11035 
11036 opt_lock:	IN_P lock_type MODE				{ $$ = $2; }
11037 			| /*EMPTY*/						{ $$ = AccessExclusiveLock; }
11038 		;
11039 
11040 lock_type:	ACCESS SHARE					{ $$ = AccessShareLock; }
11041 			| ROW SHARE						{ $$ = RowShareLock; }
11042 			| ROW EXCLUSIVE					{ $$ = RowExclusiveLock; }
11043 			| SHARE UPDATE EXCLUSIVE		{ $$ = ShareUpdateExclusiveLock; }
11044 			| SHARE							{ $$ = ShareLock; }
11045 			| SHARE ROW EXCLUSIVE			{ $$ = ShareRowExclusiveLock; }
11046 			| EXCLUSIVE						{ $$ = ExclusiveLock; }
11047 			| ACCESS EXCLUSIVE				{ $$ = AccessExclusiveLock; }
11048 		;
11049 
11050 opt_nowait:	NOWAIT							{ $$ = true; }
11051 			| /*EMPTY*/						{ $$ = false; }
11052 		;
11053 
11054 opt_nowait_or_skip:
11055 			NOWAIT							{ $$ = LockWaitError; }
11056 			| SKIP LOCKED					{ $$ = LockWaitSkip; }
11057 			| /*EMPTY*/						{ $$ = LockWaitBlock; }
11058 		;
11059 
11060 
11061 /*****************************************************************************
11062  *
11063  *		QUERY:
11064  *				UpdateStmt (UPDATE)
11065  *
11066  *****************************************************************************/
11067 
11068 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
11069 			SET set_clause_list
11070 			from_clause
11071 			where_or_current_clause
11072 			returning_clause
11073 				{
11074 					UpdateStmt *n = makeNode(UpdateStmt);
11075 					n->relation = $3;
11076 					n->targetList = $5;
11077 					n->fromClause = $6;
11078 					n->whereClause = $7;
11079 					n->returningList = $8;
11080 					n->withClause = $1;
11081 					$$ = (Node *)n;
11082 				}
11083 		;
11084 
11085 set_clause_list:
11086 			set_clause							{ $$ = $1; }
11087 			| set_clause_list ',' set_clause	{ $$ = list_concat($1,$3); }
11088 		;
11089 
11090 set_clause:
11091 			set_target '=' a_expr
11092 				{
11093 					$1->val = (Node *) $3;
11094 					$$ = list_make1($1);
11095 				}
11096 			| '(' set_target_list ')' '=' a_expr
11097 				{
11098 					int ncolumns = list_length($2);
11099 					int i = 1;
11100 					ListCell *col_cell;
11101 
11102 					/* Create a MultiAssignRef source for each target */
foreach(col_cell,$2)11103 					foreach(col_cell, $2)
11104 					{
11105 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
11106 						MultiAssignRef *r = makeNode(MultiAssignRef);
11107 
11108 						r->source = (Node *) $5;
11109 						r->colno = i;
11110 						r->ncolumns = ncolumns;
11111 						res_col->val = (Node *) r;
11112 						i++;
11113 					}
11114 
11115 					$$ = $2;
11116 				}
11117 		;
11118 
11119 set_target:
11120 			ColId opt_indirection
11121 				{
11122 					$$ = makeNode(ResTarget);
11123 					$$->name = $1;
11124 					$$->indirection = check_indirection($2, yyscanner);
11125 					$$->val = NULL;	/* upper production sets this */
11126 					$$->location = @1;
11127 				}
11128 		;
11129 
11130 set_target_list:
11131 			set_target								{ $$ = list_make1($1); }
11132 			| set_target_list ',' set_target		{ $$ = lappend($1,$3); }
11133 		;
11134 
11135 
11136 /*****************************************************************************
11137  *
11138  *		QUERY:
11139  *				CURSOR STATEMENTS
11140  *
11141  *****************************************************************************/
11142 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
11143 				{
11144 					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
11145 					n->portalname = $2;
11146 					/* currently we always set FAST_PLAN option */
11147 					n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
11148 					n->query = $7;
11149 					$$ = (Node *)n;
11150 				}
11151 		;
11152 
11153 cursor_name:	name						{ $$ = $1; }
11154 		;
11155 
11156 cursor_options: /*EMPTY*/					{ $$ = 0; }
11157 			| cursor_options NO SCROLL		{ $$ = $1 | CURSOR_OPT_NO_SCROLL; }
11158 			| cursor_options SCROLL			{ $$ = $1 | CURSOR_OPT_SCROLL; }
11159 			| cursor_options BINARY			{ $$ = $1 | CURSOR_OPT_BINARY; }
11160 			| cursor_options INSENSITIVE	{ $$ = $1 | CURSOR_OPT_INSENSITIVE; }
11161 		;
11162 
11163 opt_hold: /* EMPTY */						{ $$ = 0; }
11164 			| WITH HOLD						{ $$ = CURSOR_OPT_HOLD; }
11165 			| WITHOUT HOLD					{ $$ = 0; }
11166 		;
11167 
11168 /*****************************************************************************
11169  *
11170  *		QUERY:
11171  *				SELECT STATEMENTS
11172  *
11173  *****************************************************************************/
11174 
11175 /* A complete SELECT statement looks like this.
11176  *
11177  * The rule returns either a single SelectStmt node or a tree of them,
11178  * representing a set-operation tree.
11179  *
11180  * There is an ambiguity when a sub-SELECT is within an a_expr and there
11181  * are excess parentheses: do the parentheses belong to the sub-SELECT or
11182  * to the surrounding a_expr?  We don't really care, but bison wants to know.
11183  * To resolve the ambiguity, we are careful to define the grammar so that
11184  * the decision is staved off as long as possible: as long as we can keep
11185  * absorbing parentheses into the sub-SELECT, we will do so, and only when
11186  * it's no longer possible to do that will we decide that parens belong to
11187  * the expression.	For example, in "SELECT (((SELECT 2)) + 3)" the extra
11188  * parentheses are treated as part of the sub-select.  The necessity of doing
11189  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".	Had we
11190  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
11191  * SELECT viewpoint when we see the UNION.
11192  *
11193  * This approach is implemented by defining a nonterminal select_with_parens,
11194  * which represents a SELECT with at least one outer layer of parentheses,
11195  * and being careful to use select_with_parens, never '(' SelectStmt ')',
11196  * in the expression grammar.  We will then have shift-reduce conflicts
11197  * which we can resolve in favor of always treating '(' <select> ')' as
11198  * a select_with_parens.  To resolve the conflicts, the productions that
11199  * conflict with the select_with_parens productions are manually given
11200  * precedences lower than the precedence of ')', thereby ensuring that we
11201  * shift ')' (and then reduce to select_with_parens) rather than trying to
11202  * reduce the inner <select> nonterminal to something else.  We use UMINUS
11203  * precedence for this, which is a fairly arbitrary choice.
11204  *
11205  * To be able to define select_with_parens itself without ambiguity, we need
11206  * a nonterminal select_no_parens that represents a SELECT structure with no
11207  * outermost parentheses.  This is a little bit tedious, but it works.
11208  *
11209  * In non-expression contexts, we use SelectStmt which can represent a SELECT
11210  * with or without outer parentheses.
11211  */
11212 
11213 SelectStmt: select_no_parens			%prec UMINUS
11214 			| select_with_parens		%prec UMINUS
11215 		;
11216 
11217 select_with_parens:
11218 			'(' select_no_parens ')'				{ $$ = $2; }
11219 			| '(' select_with_parens ')'			{ $$ = $2; }
11220 		;
11221 
11222 /*
11223  * This rule parses the equivalent of the standard's <query expression>.
11224  * The duplicative productions are annoying, but hard to get rid of without
11225  * creating shift/reduce conflicts.
11226  *
11227  *	The locking clause (FOR UPDATE etc) may be before or after LIMIT/OFFSET.
11228  *	In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
11229  *	We now support both orderings, but prefer LIMIT/OFFSET before the locking
11230  * clause.
11231  *	2002-08-28 bjm
11232  */
11233 select_no_parens:
11234 			simple_select						{ $$ = $1; }
11235 			| select_clause sort_clause
11236 				{
11237 					insertSelectOptions((SelectStmt *) $1, $2, NIL,
11238 										NULL, NULL, NULL,
11239 										yyscanner);
11240 					$$ = $1;
11241 				}
11242 			| select_clause opt_sort_clause for_locking_clause opt_select_limit
11243 				{
11244 					insertSelectOptions((SelectStmt *) $1, $2, $3,
11245 										list_nth($4, 0), list_nth($4, 1),
11246 										NULL,
11247 										yyscanner);
11248 					$$ = $1;
11249 				}
11250 			| select_clause opt_sort_clause select_limit opt_for_locking_clause
11251 				{
11252 					insertSelectOptions((SelectStmt *) $1, $2, $4,
11253 										list_nth($3, 0), list_nth($3, 1),
11254 										NULL,
11255 										yyscanner);
11256 					$$ = $1;
11257 				}
11258 			| with_clause select_clause
11259 				{
11260 					insertSelectOptions((SelectStmt *) $2, NULL, NIL,
11261 										NULL, NULL,
11262 										$1,
11263 										yyscanner);
11264 					$$ = $2;
11265 				}
11266 			| with_clause select_clause sort_clause
11267 				{
11268 					insertSelectOptions((SelectStmt *) $2, $3, NIL,
11269 										NULL, NULL,
11270 										$1,
11271 										yyscanner);
11272 					$$ = $2;
11273 				}
11274 			| with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
11275 				{
11276 					insertSelectOptions((SelectStmt *) $2, $3, $4,
11277 										list_nth($5, 0), list_nth($5, 1),
11278 										$1,
11279 										yyscanner);
11280 					$$ = $2;
11281 				}
11282 			| with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
11283 				{
11284 					insertSelectOptions((SelectStmt *) $2, $3, $5,
11285 										list_nth($4, 0), list_nth($4, 1),
11286 										$1,
11287 										yyscanner);
11288 					$$ = $2;
11289 				}
11290 		;
11291 
11292 select_clause:
11293 			simple_select							{ $$ = $1; }
11294 			| select_with_parens					{ $$ = $1; }
11295 		;
11296 
11297 /*
11298  * This rule parses SELECT statements that can appear within set operations,
11299  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
11300  * the ordering of the set operations.	Without '(' and ')' we want the
11301  * operations to be ordered per the precedence specs at the head of this file.
11302  *
11303  * As with select_no_parens, simple_select cannot have outer parentheses,
11304  * but can have parenthesized subclauses.
11305  *
11306  * Note that sort clauses cannot be included at this level --- SQL requires
11307  *		SELECT foo UNION SELECT bar ORDER BY baz
11308  * to be parsed as
11309  *		(SELECT foo UNION SELECT bar) ORDER BY baz
11310  * not
11311  *		SELECT foo UNION (SELECT bar ORDER BY baz)
11312  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
11313  * described as part of the select_no_parens production, not simple_select.
11314  * This does not limit functionality, because you can reintroduce these
11315  * clauses inside parentheses.
11316  *
11317  * NOTE: only the leftmost component SelectStmt should have INTO.
11318  * However, this is not checked by the grammar; parse analysis must check it.
11319  */
11320 simple_select:
11321 			SELECT opt_all_clause opt_target_list
11322 			into_clause from_clause where_clause
11323 			group_clause having_clause window_clause
11324 				{
11325 					SelectStmt *n = makeNode(SelectStmt);
11326 					n->targetList = $3;
11327 					n->intoClause = $4;
11328 					n->fromClause = $5;
11329 					n->whereClause = $6;
11330 					n->groupClause = $7;
11331 					n->havingClause = $8;
11332 					n->windowClause = $9;
11333 					$$ = (Node *)n;
11334 				}
11335 			| SELECT distinct_clause target_list
11336 			into_clause from_clause where_clause
11337 			group_clause having_clause window_clause
11338 				{
11339 					SelectStmt *n = makeNode(SelectStmt);
11340 					n->distinctClause = $2;
11341 					n->targetList = $3;
11342 					n->intoClause = $4;
11343 					n->fromClause = $5;
11344 					n->whereClause = $6;
11345 					n->groupClause = $7;
11346 					n->havingClause = $8;
11347 					n->windowClause = $9;
11348 					$$ = (Node *)n;
11349 				}
11350 			| values_clause							{ $$ = $1; }
11351 			| TABLE relation_expr
11352 				{
11353 					/* same as SELECT * FROM relation_expr */
11354 					ColumnRef *cr = makeNode(ColumnRef);
11355 					ResTarget *rt = makeNode(ResTarget);
11356 					SelectStmt *n = makeNode(SelectStmt);
11357 
11358 					cr->fields = list_make1(makeNode(A_Star));
11359 					cr->location = -1;
11360 
11361 					rt->name = NULL;
11362 					rt->indirection = NIL;
11363 					rt->val = (Node *)cr;
11364 					rt->location = -1;
11365 
11366 					n->targetList = list_make1(rt);
11367 					n->fromClause = list_make1($2);
11368 					$$ = (Node *)n;
11369 				}
11370 			| select_clause UNION all_or_distinct select_clause
11371 				{
11372 					$$ = makeSetOp(SETOP_UNION, $3, $1, $4);
11373 				}
11374 			| select_clause INTERSECT all_or_distinct select_clause
11375 				{
11376 					$$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
11377 				}
11378 			| select_clause EXCEPT all_or_distinct select_clause
11379 				{
11380 					$$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
11381 				}
11382 		;
11383 
11384 /*
11385  * SQL standard WITH clause looks like:
11386  *
11387  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
11388  *		AS (query) [ SEARCH or CYCLE clause ]
11389  *
11390  * We don't currently support the SEARCH or CYCLE clause.
11391  *
11392  * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY.
11393  */
11394 with_clause:
11395 		WITH cte_list
11396 			{
11397 				$$ = makeNode(WithClause);
11398 				$$->ctes = $2;
11399 				$$->recursive = false;
11400 				$$->location = @1;
11401 			}
11402 		| WITH_LA cte_list
11403 			{
11404 				$$ = makeNode(WithClause);
11405 				$$->ctes = $2;
11406 				$$->recursive = false;
11407 				$$->location = @1;
11408 			}
11409 		| WITH RECURSIVE cte_list
11410 			{
11411 				$$ = makeNode(WithClause);
11412 				$$->ctes = $3;
11413 				$$->recursive = true;
11414 				$$->location = @1;
11415 			}
11416 		;
11417 
11418 cte_list:
11419 		common_table_expr						{ $$ = list_make1($1); }
11420 		| cte_list ',' common_table_expr		{ $$ = lappend($1, $3); }
11421 		;
11422 
11423 common_table_expr:  name opt_name_list AS '(' PreparableStmt ')'
11424 			{
11425 				CommonTableExpr *n = makeNode(CommonTableExpr);
11426 				n->ctename = $1;
11427 				n->aliascolnames = $2;
11428 				n->ctequery = $5;
11429 				n->location = @1;
11430 				$$ = (Node *) n;
11431 			}
11432 		;
11433 
11434 opt_with_clause:
11435 		with_clause								{ $$ = $1; }
11436 		| /*EMPTY*/								{ $$ = NULL; }
11437 		;
11438 
11439 into_clause:
11440 			INTO OptTempTableName
11441 				{
11442 					$$ = makeNode(IntoClause);
11443 					$$->rel = $2;
11444 					$$->colNames = NIL;
11445 					$$->options = NIL;
11446 					$$->onCommit = ONCOMMIT_NOOP;
11447 					$$->tableSpaceName = NULL;
11448 					$$->viewQuery = NULL;
11449 					$$->skipData = false;
11450 				}
11451 			| /*EMPTY*/
11452 				{ $$ = NULL; }
11453 		;
11454 
11455 /*
11456  * Redundancy here is needed to avoid shift/reduce conflicts,
11457  * since TEMP is not a reserved word.  See also OptTemp.
11458  */
11459 OptTempTableName:
11460 			TEMPORARY opt_table qualified_name
11461 				{
11462 					$$ = $3;
11463 					$$->relpersistence = RELPERSISTENCE_TEMP;
11464 				}
11465 			| TEMP opt_table qualified_name
11466 				{
11467 					$$ = $3;
11468 					$$->relpersistence = RELPERSISTENCE_TEMP;
11469 				}
11470 			| LOCAL TEMPORARY opt_table qualified_name
11471 				{
11472 					$$ = $4;
11473 					$$->relpersistence = RELPERSISTENCE_TEMP;
11474 				}
11475 			| LOCAL TEMP opt_table qualified_name
11476 				{
11477 					$$ = $4;
11478 					$$->relpersistence = RELPERSISTENCE_TEMP;
11479 				}
11480 			| GLOBAL TEMPORARY opt_table qualified_name
11481 				{
11482 					ereport(WARNING,
11483 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11484 							 parser_errposition(@1)));
11485 					$$ = $4;
11486 					$$->relpersistence = RELPERSISTENCE_TEMP;
11487 				}
11488 			| GLOBAL TEMP opt_table qualified_name
11489 				{
11490 					ereport(WARNING,
11491 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11492 							 parser_errposition(@1)));
11493 					$$ = $4;
11494 					$$->relpersistence = RELPERSISTENCE_TEMP;
11495 				}
11496 			| UNLOGGED opt_table qualified_name
11497 				{
11498 					$$ = $3;
11499 					$$->relpersistence = RELPERSISTENCE_UNLOGGED;
11500 				}
11501 			| TABLE qualified_name
11502 				{
11503 					$$ = $2;
11504 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11505 				}
11506 			| qualified_name
11507 				{
11508 					$$ = $1;
11509 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11510 				}
11511 		;
11512 
11513 opt_table:	TABLE									{}
11514 			| /*EMPTY*/								{}
11515 		;
11516 
11517 all_or_distinct:
11518 			ALL										{ $$ = true; }
11519 			| DISTINCT								{ $$ = false; }
11520 			| /*EMPTY*/								{ $$ = false; }
11521 		;
11522 
11523 /* We use (NIL) as a placeholder to indicate that all target expressions
11524  * should be placed in the DISTINCT list during parsetree analysis.
11525  */
11526 distinct_clause:
11527 			DISTINCT								{ $$ = list_make1(NIL); }
11528 			| DISTINCT ON '(' expr_list ')'			{ $$ = $4; }
11529 		;
11530 
11531 opt_all_clause:
11532 			ALL										{ $$ = NIL;}
11533 			| /*EMPTY*/								{ $$ = NIL; }
11534 		;
11535 
11536 opt_sort_clause:
11537 			sort_clause								{ $$ = $1;}
11538 			| /*EMPTY*/								{ $$ = NIL; }
11539 		;
11540 
11541 sort_clause:
11542 			ORDER BY sortby_list					{ $$ = $3; }
11543 		;
11544 
11545 sortby_list:
11546 			sortby									{ $$ = list_make1($1); }
11547 			| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
11548 		;
11549 
11550 sortby:		a_expr USING qual_all_Op opt_nulls_order
11551 				{
11552 					$$ = makeNode(SortBy);
11553 					$$->node = $1;
11554 					$$->sortby_dir = SORTBY_USING;
11555 					$$->sortby_nulls = $4;
11556 					$$->useOp = $3;
11557 					$$->location = @3;
11558 				}
11559 			| a_expr opt_asc_desc opt_nulls_order
11560 				{
11561 					$$ = makeNode(SortBy);
11562 					$$->node = $1;
11563 					$$->sortby_dir = $2;
11564 					$$->sortby_nulls = $3;
11565 					$$->useOp = NIL;
11566 					$$->location = -1;		/* no operator */
11567 				}
11568 		;
11569 
11570 
11571 select_limit:
11572 			limit_clause offset_clause			{ $$ = list_make2($2, $1); }
11573 			| offset_clause limit_clause		{ $$ = list_make2($1, $2); }
11574 			| limit_clause						{ $$ = list_make2(NULL, $1); }
11575 			| offset_clause						{ $$ = list_make2($1, NULL); }
11576 		;
11577 
11578 opt_select_limit:
11579 			select_limit						{ $$ = $1; }
11580 			| /* EMPTY */						{ $$ = list_make2(NULL,NULL); }
11581 		;
11582 
11583 limit_clause:
11584 			LIMIT select_limit_value
11585 				{ $$ = $2; }
11586 			| LIMIT select_limit_value ',' select_offset_value
11587 				{
11588 					/* Disabled because it was too confusing, bjm 2002-02-18 */
11589 					ereport(ERROR,
11590 							(errcode(ERRCODE_SYNTAX_ERROR),
11591 							 errmsg("LIMIT #,# syntax is not supported"),
11592 							 errhint("Use separate LIMIT and OFFSET clauses."),
11593 							 parser_errposition(@1)));
11594 				}
11595 			/* SQL:2008 syntax */
11596 			/* to avoid shift/reduce conflicts, handle the optional value with
11597 			 * a separate production rather than an opt_ expression.  The fact
11598 			 * that ONLY is fully reserved means that this way, we defer any
11599 			 * decision about what rule reduces ROW or ROWS to the point where
11600 			 * we can see the ONLY token in the lookahead slot.
11601 			 */
11602 			| FETCH first_or_next select_fetch_first_value row_or_rows ONLY
11603 				{ $$ = $3; }
11604 			| FETCH first_or_next row_or_rows ONLY
11605 				{ $$ = makeIntConst(1, -1); }
11606 		;
11607 
11608 offset_clause:
11609 			OFFSET select_offset_value
11610 				{ $$ = $2; }
11611 			/* SQL:2008 syntax */
11612 			| OFFSET select_fetch_first_value row_or_rows
11613 				{ $$ = $2; }
11614 		;
11615 
11616 select_limit_value:
11617 			a_expr									{ $$ = $1; }
11618 			| ALL
11619 				{
11620 					/* LIMIT ALL is represented as a NULL constant */
11621 					$$ = makeNullAConst(@1);
11622 				}
11623 		;
11624 
11625 select_offset_value:
11626 			a_expr									{ $$ = $1; }
11627 		;
11628 
11629 /*
11630  * Allowing full expressions without parentheses causes various parsing
11631  * problems with the trailing ROW/ROWS key words.  SQL spec only calls for
11632  * <simple value specification>, which is either a literal or a parameter (but
11633  * an <SQL parameter reference> could be an identifier, bringing up conflicts
11634  * with ROW/ROWS). We solve this by leveraging the presence of ONLY (see above)
11635  * to determine whether the expression is missing rather than trying to make it
11636  * optional in this rule.
11637  *
11638  * c_expr covers almost all the spec-required cases (and more), but it doesn't
11639  * cover signed numeric literals, which are allowed by the spec. So we include
11640  * those here explicitly. We need FCONST as well as ICONST because values that
11641  * don't fit in the platform's "long", but do fit in bigint, should still be
11642  * accepted here. (This is possible in 64-bit Windows as well as all 32-bit
11643  * builds.)
11644  */
11645 select_fetch_first_value:
11646 			c_expr									{ $$ = $1; }
11647 			| '+' I_or_F_const
11648 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
11649 			| '-' I_or_F_const
11650 				{ $$ = doNegate($2, @1); }
11651 		;
11652 
11653 I_or_F_const:
11654 			Iconst									{ $$ = makeIntConst($1,@1); }
11655 			| FCONST								{ $$ = makeFloatConst($1,@1); }
11656 		;
11657 
11658 /* noise words */
11659 row_or_rows: ROW									{ $$ = 0; }
11660 			| ROWS									{ $$ = 0; }
11661 		;
11662 
11663 first_or_next: FIRST_P								{ $$ = 0; }
11664 			| NEXT									{ $$ = 0; }
11665 		;
11666 
11667 
11668 /*
11669  * This syntax for group_clause tries to follow the spec quite closely.
11670  * However, the spec allows only column references, not expressions,
11671  * which introduces an ambiguity between implicit row constructors
11672  * (a,b) and lists of column references.
11673  *
11674  * We handle this by using the a_expr production for what the spec calls
11675  * <ordinary grouping set>, which in the spec represents either one column
11676  * reference or a parenthesized list of column references. Then, we check the
11677  * top node of the a_expr to see if it's an implicit RowExpr, and if so, just
11678  * grab and use the list, discarding the node. (this is done in parse analysis,
11679  * not here)
11680  *
11681  * (we abuse the row_format field of RowExpr to distinguish implicit and
11682  * explicit row constructors; it's debatable if anyone sanely wants to use them
11683  * in a group clause, but if they have a reason to, we make it possible.)
11684  *
11685  * Each item in the group_clause list is either an expression tree or a
11686  * GroupingSet node of some type.
11687  */
11688 group_clause:
11689 			GROUP_P BY group_by_list				{ $$ = $3; }
11690 			| /*EMPTY*/								{ $$ = NIL; }
11691 		;
11692 
11693 group_by_list:
11694 			group_by_item							{ $$ = list_make1($1); }
11695 			| group_by_list ',' group_by_item		{ $$ = lappend($1,$3); }
11696 		;
11697 
11698 group_by_item:
11699 			a_expr									{ $$ = $1; }
11700 			| empty_grouping_set					{ $$ = $1; }
11701 			| cube_clause							{ $$ = $1; }
11702 			| rollup_clause							{ $$ = $1; }
11703 			| grouping_sets_clause					{ $$ = $1; }
11704 		;
11705 
11706 empty_grouping_set:
11707 			'(' ')'
11708 				{
11709 					$$ = (Node *) makeGroupingSet(GROUPING_SET_EMPTY, NIL, @1);
11710 				}
11711 		;
11712 
11713 /*
11714  * These hacks rely on setting precedence of CUBE and ROLLUP below that of '(',
11715  * so that they shift in these rules rather than reducing the conflicting
11716  * unreserved_keyword rule.
11717  */
11718 
11719 rollup_clause:
11720 			ROLLUP '(' expr_list ')'
11721 				{
11722 					$$ = (Node *) makeGroupingSet(GROUPING_SET_ROLLUP, $3, @1);
11723 				}
11724 		;
11725 
11726 cube_clause:
11727 			CUBE '(' expr_list ')'
11728 				{
11729 					$$ = (Node *) makeGroupingSet(GROUPING_SET_CUBE, $3, @1);
11730 				}
11731 		;
11732 
11733 grouping_sets_clause:
11734 			GROUPING SETS '(' group_by_list ')'
11735 				{
11736 					$$ = (Node *) makeGroupingSet(GROUPING_SET_SETS, $4, @1);
11737 				}
11738 		;
11739 
11740 having_clause:
11741 			HAVING a_expr							{ $$ = $2; }
11742 			| /*EMPTY*/								{ $$ = NULL; }
11743 		;
11744 
11745 for_locking_clause:
11746 			for_locking_items						{ $$ = $1; }
11747 			| FOR READ ONLY							{ $$ = NIL; }
11748 		;
11749 
11750 opt_for_locking_clause:
11751 			for_locking_clause						{ $$ = $1; }
11752 			| /* EMPTY */							{ $$ = NIL; }
11753 		;
11754 
11755 for_locking_items:
11756 			for_locking_item						{ $$ = list_make1($1); }
11757 			| for_locking_items for_locking_item	{ $$ = lappend($1, $2); }
11758 		;
11759 
11760 for_locking_item:
11761 			for_locking_strength locked_rels_list opt_nowait_or_skip
11762 				{
11763 					LockingClause *n = makeNode(LockingClause);
11764 					n->lockedRels = $2;
11765 					n->strength = $1;
11766 					n->waitPolicy = $3;
11767 					$$ = (Node *) n;
11768 				}
11769 		;
11770 
11771 for_locking_strength:
11772 			FOR UPDATE 							{ $$ = LCS_FORUPDATE; }
11773 			| FOR NO KEY UPDATE 				{ $$ = LCS_FORNOKEYUPDATE; }
11774 			| FOR SHARE 						{ $$ = LCS_FORSHARE; }
11775 			| FOR KEY SHARE 					{ $$ = LCS_FORKEYSHARE; }
11776 		;
11777 
11778 locked_rels_list:
11779 			OF qualified_name_list					{ $$ = $2; }
11780 			| /* EMPTY */							{ $$ = NIL; }
11781 		;
11782 
11783 
11784 /*
11785  * We should allow ROW '(' expr_list ')' too, but that seems to require
11786  * making VALUES a fully reserved word, which will probably break more apps
11787  * than allowing the noise-word is worth.
11788  */
11789 values_clause:
11790 			VALUES '(' expr_list ')'
11791 				{
11792 					SelectStmt *n = makeNode(SelectStmt);
11793 					n->valuesLists = list_make1($3);
11794 					$$ = (Node *) n;
11795 				}
11796 			| values_clause ',' '(' expr_list ')'
11797 				{
11798 					SelectStmt *n = (SelectStmt *) $1;
11799 					n->valuesLists = lappend(n->valuesLists, $4);
11800 					$$ = (Node *) n;
11801 				}
11802 		;
11803 
11804 
11805 /*****************************************************************************
11806  *
11807  *	clauses common to all Optimizable Stmts:
11808  *		from_clause		- allow list of both JOIN expressions and table names
11809  *		where_clause	- qualifications for joins or restrictions
11810  *
11811  *****************************************************************************/
11812 
11813 from_clause:
11814 			FROM from_list							{ $$ = $2; }
11815 			| /*EMPTY*/								{ $$ = NIL; }
11816 		;
11817 
11818 from_list:
11819 			table_ref								{ $$ = list_make1($1); }
11820 			| from_list ',' table_ref				{ $$ = lappend($1, $3); }
11821 		;
11822 
11823 /*
11824  * table_ref is where an alias clause can be attached.
11825  */
11826 table_ref:	relation_expr opt_alias_clause
11827 				{
11828 					$1->alias = $2;
11829 					$$ = (Node *) $1;
11830 				}
11831 			| relation_expr opt_alias_clause tablesample_clause
11832 				{
11833 					RangeTableSample *n = (RangeTableSample *) $3;
11834 					$1->alias = $2;
11835 					/* relation_expr goes inside the RangeTableSample node */
11836 					n->relation = (Node *) $1;
11837 					$$ = (Node *) n;
11838 				}
11839 			| func_table func_alias_clause
11840 				{
11841 					RangeFunction *n = (RangeFunction *) $1;
11842 					n->alias = linitial($2);
11843 					n->coldeflist = lsecond($2);
11844 					$$ = (Node *) n;
11845 				}
11846 			| LATERAL_P func_table func_alias_clause
11847 				{
11848 					RangeFunction *n = (RangeFunction *) $2;
11849 					n->lateral = true;
11850 					n->alias = linitial($3);
11851 					n->coldeflist = lsecond($3);
11852 					$$ = (Node *) n;
11853 				}
11854 			| xmltable opt_alias_clause
11855 				{
11856 					RangeTableFunc *n = (RangeTableFunc *) $1;
11857 					n->alias = $2;
11858 					$$ = (Node *) n;
11859 				}
11860 			| LATERAL_P xmltable opt_alias_clause
11861 				{
11862 					RangeTableFunc *n = (RangeTableFunc *) $2;
11863 					n->lateral = true;
11864 					n->alias = $3;
11865 					$$ = (Node *) n;
11866 				}
11867 			| select_with_parens opt_alias_clause
11868 				{
11869 					RangeSubselect *n = makeNode(RangeSubselect);
11870 					n->lateral = false;
11871 					n->subquery = $1;
11872 					n->alias = $2;
11873 					/*
11874 					 * The SQL spec does not permit a subselect
11875 					 * (<derived_table>) without an alias clause,
11876 					 * so we don't either.  This avoids the problem
11877 					 * of needing to invent a unique refname for it.
11878 					 * That could be surmounted if there's sufficient
11879 					 * popular demand, but for now let's just implement
11880 					 * the spec and see if anyone complains.
11881 					 * However, it does seem like a good idea to emit
11882 					 * an error message that's better than "syntax error".
11883 					 */
11884 					if ($2 == NULL)
11885 					{
11886 						if (IsA($1, SelectStmt) &&
11887 							((SelectStmt *) $1)->valuesLists)
11888 							ereport(ERROR,
11889 									(errcode(ERRCODE_SYNTAX_ERROR),
11890 									 errmsg("VALUES in FROM must have an alias"),
11891 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
11892 									 parser_errposition(@1)));
11893 						else
11894 							ereport(ERROR,
11895 									(errcode(ERRCODE_SYNTAX_ERROR),
11896 									 errmsg("subquery in FROM must have an alias"),
11897 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
11898 									 parser_errposition(@1)));
11899 					}
11900 					$$ = (Node *) n;
11901 				}
11902 			| LATERAL_P select_with_parens opt_alias_clause
11903 				{
11904 					RangeSubselect *n = makeNode(RangeSubselect);
11905 					n->lateral = true;
11906 					n->subquery = $2;
11907 					n->alias = $3;
11908 					/* same comment as above */
11909 					if ($3 == NULL)
11910 					{
11911 						if (IsA($2, SelectStmt) &&
11912 							((SelectStmt *) $2)->valuesLists)
11913 							ereport(ERROR,
11914 									(errcode(ERRCODE_SYNTAX_ERROR),
11915 									 errmsg("VALUES in FROM must have an alias"),
11916 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
11917 									 parser_errposition(@2)));
11918 						else
11919 							ereport(ERROR,
11920 									(errcode(ERRCODE_SYNTAX_ERROR),
11921 									 errmsg("subquery in FROM must have an alias"),
11922 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
11923 									 parser_errposition(@2)));
11924 					}
11925 					$$ = (Node *) n;
11926 				}
11927 			| joined_table
11928 				{
11929 					$$ = (Node *) $1;
11930 				}
11931 			| '(' joined_table ')' alias_clause
11932 				{
11933 					$2->alias = $4;
11934 					$$ = (Node *) $2;
11935 				}
11936 		;
11937 
11938 
11939 /*
11940  * It may seem silly to separate joined_table from table_ref, but there is
11941  * method in SQL's madness: if you don't do it this way you get reduce-
11942  * reduce conflicts, because it's not clear to the parser generator whether
11943  * to expect alias_clause after ')' or not.  For the same reason we must
11944  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
11945  * join_type to expand to empty; if we try it, the parser generator can't
11946  * figure out when to reduce an empty join_type right after table_ref.
11947  *
11948  * Note that a CROSS JOIN is the same as an unqualified
11949  * INNER JOIN, and an INNER JOIN/ON has the same shape
11950  * but a qualification expression to limit membership.
11951  * A NATURAL JOIN implicitly matches column names between
11952  * tables and the shape is determined by which columns are
11953  * in common. We'll collect columns during the later transformations.
11954  */
11955 
11956 joined_table:
11957 			'(' joined_table ')'
11958 				{
11959 					$$ = $2;
11960 				}
11961 			| table_ref CROSS JOIN table_ref
11962 				{
11963 					/* CROSS JOIN is same as unqualified inner join */
11964 					JoinExpr *n = makeNode(JoinExpr);
11965 					n->jointype = JOIN_INNER;
11966 					n->isNatural = false;
11967 					n->larg = $1;
11968 					n->rarg = $4;
11969 					n->usingClause = NIL;
11970 					n->quals = NULL;
11971 					$$ = n;
11972 				}
11973 			| table_ref join_type JOIN table_ref join_qual
11974 				{
11975 					JoinExpr *n = makeNode(JoinExpr);
11976 					n->jointype = $2;
11977 					n->isNatural = false;
11978 					n->larg = $1;
11979 					n->rarg = $4;
11980 					if ($5 != NULL && IsA($5, List))
11981 						n->usingClause = (List *) $5; /* USING clause */
11982 					else
11983 						n->quals = $5; /* ON clause */
11984 					$$ = n;
11985 				}
11986 			| table_ref JOIN table_ref join_qual
11987 				{
11988 					/* letting join_type reduce to empty doesn't work */
11989 					JoinExpr *n = makeNode(JoinExpr);
11990 					n->jointype = JOIN_INNER;
11991 					n->isNatural = false;
11992 					n->larg = $1;
11993 					n->rarg = $3;
11994 					if ($4 != NULL && IsA($4, List))
11995 						n->usingClause = (List *) $4; /* USING clause */
11996 					else
11997 						n->quals = $4; /* ON clause */
11998 					$$ = n;
11999 				}
12000 			| table_ref NATURAL join_type JOIN table_ref
12001 				{
12002 					JoinExpr *n = makeNode(JoinExpr);
12003 					n->jointype = $3;
12004 					n->isNatural = true;
12005 					n->larg = $1;
12006 					n->rarg = $5;
12007 					n->usingClause = NIL; /* figure out which columns later... */
12008 					n->quals = NULL; /* fill later */
12009 					$$ = n;
12010 				}
12011 			| table_ref NATURAL JOIN table_ref
12012 				{
12013 					/* letting join_type reduce to empty doesn't work */
12014 					JoinExpr *n = makeNode(JoinExpr);
12015 					n->jointype = JOIN_INNER;
12016 					n->isNatural = true;
12017 					n->larg = $1;
12018 					n->rarg = $4;
12019 					n->usingClause = NIL; /* figure out which columns later... */
12020 					n->quals = NULL; /* fill later */
12021 					$$ = n;
12022 				}
12023 		;
12024 
12025 alias_clause:
12026 			AS ColId '(' name_list ')'
12027 				{
12028 					$$ = makeNode(Alias);
12029 					$$->aliasname = $2;
12030 					$$->colnames = $4;
12031 				}
12032 			| AS ColId
12033 				{
12034 					$$ = makeNode(Alias);
12035 					$$->aliasname = $2;
12036 				}
12037 			| ColId '(' name_list ')'
12038 				{
12039 					$$ = makeNode(Alias);
12040 					$$->aliasname = $1;
12041 					$$->colnames = $3;
12042 				}
12043 			| ColId
12044 				{
12045 					$$ = makeNode(Alias);
12046 					$$->aliasname = $1;
12047 				}
12048 		;
12049 
12050 opt_alias_clause: alias_clause						{ $$ = $1; }
12051 			| /*EMPTY*/								{ $$ = NULL; }
12052 		;
12053 
12054 /*
12055  * func_alias_clause can include both an Alias and a coldeflist, so we make it
12056  * return a 2-element list that gets disassembled by calling production.
12057  */
12058 func_alias_clause:
12059 			alias_clause
12060 				{
12061 					$$ = list_make2($1, NIL);
12062 				}
12063 			| AS '(' TableFuncElementList ')'
12064 				{
12065 					$$ = list_make2(NULL, $3);
12066 				}
12067 			| AS ColId '(' TableFuncElementList ')'
12068 				{
12069 					Alias *a = makeNode(Alias);
12070 					a->aliasname = $2;
12071 					$$ = list_make2(a, $4);
12072 				}
12073 			| ColId '(' TableFuncElementList ')'
12074 				{
12075 					Alias *a = makeNode(Alias);
12076 					a->aliasname = $1;
12077 					$$ = list_make2(a, $3);
12078 				}
12079 			| /*EMPTY*/
12080 				{
12081 					$$ = list_make2(NULL, NIL);
12082 				}
12083 		;
12084 
12085 join_type:	FULL join_outer							{ $$ = JOIN_FULL; }
12086 			| LEFT join_outer						{ $$ = JOIN_LEFT; }
12087 			| RIGHT join_outer						{ $$ = JOIN_RIGHT; }
12088 			| INNER_P								{ $$ = JOIN_INNER; }
12089 		;
12090 
12091 /* OUTER is just noise... */
12092 join_outer: OUTER_P									{ $$ = NULL; }
12093 			| /*EMPTY*/								{ $$ = NULL; }
12094 		;
12095 
12096 /* JOIN qualification clauses
12097  * Possibilities are:
12098  *	USING ( column list ) allows only unqualified column names,
12099  *						  which must match between tables.
12100  *	ON expr allows more general qualifications.
12101  *
12102  * We return USING as a List node, while an ON-expr will not be a List.
12103  */
12104 
12105 join_qual:	USING '(' name_list ')'					{ $$ = (Node *) $3; }
12106 			| ON a_expr								{ $$ = $2; }
12107 		;
12108 
12109 
12110 relation_expr:
12111 			qualified_name
12112 				{
12113 					/* inheritance query, implicitly */
12114 					$$ = $1;
12115 					$$->inh = true;
12116 					$$->alias = NULL;
12117 				}
12118 			| qualified_name '*'
12119 				{
12120 					/* inheritance query, explicitly */
12121 					$$ = $1;
12122 					$$->inh = true;
12123 					$$->alias = NULL;
12124 				}
12125 			| ONLY qualified_name
12126 				{
12127 					/* no inheritance */
12128 					$$ = $2;
12129 					$$->inh = false;
12130 					$$->alias = NULL;
12131 				}
12132 			| ONLY '(' qualified_name ')'
12133 				{
12134 					/* no inheritance, SQL99-style syntax */
12135 					$$ = $3;
12136 					$$->inh = false;
12137 					$$->alias = NULL;
12138 				}
12139 		;
12140 
12141 
12142 relation_expr_list:
12143 			relation_expr							{ $$ = list_make1($1); }
12144 			| relation_expr_list ',' relation_expr	{ $$ = lappend($1, $3); }
12145 		;
12146 
12147 
12148 /*
12149  * Given "UPDATE foo set set ...", we have to decide without looking any
12150  * further ahead whether the first "set" is an alias or the UPDATE's SET
12151  * keyword.  Since "set" is allowed as a column name both interpretations
12152  * are feasible.  We resolve the shift/reduce conflict by giving the first
12153  * relation_expr_opt_alias production a higher precedence than the SET token
12154  * has, causing the parser to prefer to reduce, in effect assuming that the
12155  * SET is not an alias.
12156  */
12157 relation_expr_opt_alias: relation_expr					%prec UMINUS
12158 				{
12159 					$$ = $1;
12160 				}
12161 			| relation_expr ColId
12162 				{
12163 					Alias *alias = makeNode(Alias);
12164 					alias->aliasname = $2;
12165 					$1->alias = alias;
12166 					$$ = $1;
12167 				}
12168 			| relation_expr AS ColId
12169 				{
12170 					Alias *alias = makeNode(Alias);
12171 					alias->aliasname = $3;
12172 					$1->alias = alias;
12173 					$$ = $1;
12174 				}
12175 		;
12176 
12177 /*
12178  * TABLESAMPLE decoration in a FROM item
12179  */
12180 tablesample_clause:
12181 			TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause
12182 				{
12183 					RangeTableSample *n = makeNode(RangeTableSample);
12184 					/* n->relation will be filled in later */
12185 					n->method = $2;
12186 					n->args = $4;
12187 					n->repeatable = $6;
12188 					n->location = @2;
12189 					$$ = (Node *) n;
12190 				}
12191 		;
12192 
12193 opt_repeatable_clause:
12194 			REPEATABLE '(' a_expr ')'	{ $$ = (Node *) $3; }
12195 			| /*EMPTY*/					{ $$ = NULL; }
12196 		;
12197 
12198 /*
12199  * func_table represents a function invocation in a FROM list. It can be
12200  * a plain function call, like "foo(...)", or a ROWS FROM expression with
12201  * one or more function calls, "ROWS FROM (foo(...), bar(...))",
12202  * optionally with WITH ORDINALITY attached.
12203  * In the ROWS FROM syntax, a column definition list can be given for each
12204  * function, for example:
12205  *     ROWS FROM (foo() AS (foo_res_a text, foo_res_b text),
12206  *                bar() AS (bar_res_a text, bar_res_b text))
12207  * It's also possible to attach a column definition list to the RangeFunction
12208  * as a whole, but that's handled by the table_ref production.
12209  */
12210 func_table: func_expr_windowless opt_ordinality
12211 				{
12212 					RangeFunction *n = makeNode(RangeFunction);
12213 					n->lateral = false;
12214 					n->ordinality = $2;
12215 					n->is_rowsfrom = false;
12216 					n->functions = list_make1(list_make2($1, NIL));
12217 					/* alias and coldeflist are set by table_ref production */
12218 					$$ = (Node *) n;
12219 				}
12220 			| ROWS FROM '(' rowsfrom_list ')' opt_ordinality
12221 				{
12222 					RangeFunction *n = makeNode(RangeFunction);
12223 					n->lateral = false;
12224 					n->ordinality = $6;
12225 					n->is_rowsfrom = true;
12226 					n->functions = $4;
12227 					/* alias and coldeflist are set by table_ref production */
12228 					$$ = (Node *) n;
12229 				}
12230 		;
12231 
12232 rowsfrom_item: func_expr_windowless opt_col_def_list
12233 				{ $$ = list_make2($1, $2); }
12234 		;
12235 
12236 rowsfrom_list:
12237 			rowsfrom_item						{ $$ = list_make1($1); }
12238 			| rowsfrom_list ',' rowsfrom_item	{ $$ = lappend($1, $3); }
12239 		;
12240 
12241 opt_col_def_list: AS '(' TableFuncElementList ')'	{ $$ = $3; }
12242 			| /*EMPTY*/								{ $$ = NIL; }
12243 		;
12244 
12245 opt_ordinality: WITH_LA ORDINALITY					{ $$ = true; }
12246 			| /*EMPTY*/								{ $$ = false; }
12247 		;
12248 
12249 
12250 where_clause:
12251 			WHERE a_expr							{ $$ = $2; }
12252 			| /*EMPTY*/								{ $$ = NULL; }
12253 		;
12254 
12255 /* variant for UPDATE and DELETE */
12256 where_or_current_clause:
12257 			WHERE a_expr							{ $$ = $2; }
12258 			| WHERE CURRENT_P OF cursor_name
12259 				{
12260 					CurrentOfExpr *n = makeNode(CurrentOfExpr);
12261 					/* cvarno is filled in by parse analysis */
12262 					n->cursor_name = $4;
12263 					n->cursor_param = 0;
12264 					$$ = (Node *) n;
12265 				}
12266 			| /*EMPTY*/								{ $$ = NULL; }
12267 		;
12268 
12269 
12270 OptTableFuncElementList:
12271 			TableFuncElementList				{ $$ = $1; }
12272 			| /*EMPTY*/							{ $$ = NIL; }
12273 		;
12274 
12275 TableFuncElementList:
12276 			TableFuncElement
12277 				{
12278 					$$ = list_make1($1);
12279 				}
12280 			| TableFuncElementList ',' TableFuncElement
12281 				{
12282 					$$ = lappend($1, $3);
12283 				}
12284 		;
12285 
12286 TableFuncElement:	ColId Typename opt_collate_clause
12287 				{
12288 					ColumnDef *n = makeNode(ColumnDef);
12289 					n->colname = $1;
12290 					n->typeName = $2;
12291 					n->inhcount = 0;
12292 					n->is_local = true;
12293 					n->is_not_null = false;
12294 					n->is_from_type = false;
12295 					n->storage = 0;
12296 					n->raw_default = NULL;
12297 					n->cooked_default = NULL;
12298 					n->collClause = (CollateClause *) $3;
12299 					n->collOid = InvalidOid;
12300 					n->constraints = NIL;
12301 					n->location = @1;
12302 					$$ = (Node *)n;
12303 				}
12304 		;
12305 
12306 /*
12307  * XMLTABLE
12308  */
12309 xmltable:
12310 			XMLTABLE '(' c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12311 				{
12312 					RangeTableFunc *n = makeNode(RangeTableFunc);
12313 					n->rowexpr = $3;
12314 					n->docexpr = $4;
12315 					n->columns = $6;
12316 					n->namespaces = NIL;
12317 					n->location = @1;
12318 					$$ = (Node *)n;
12319 				}
12320 			| XMLTABLE '(' XMLNAMESPACES '(' xml_namespace_list ')' ','
12321 				c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12322 				{
12323 					RangeTableFunc *n = makeNode(RangeTableFunc);
12324 					n->rowexpr = $8;
12325 					n->docexpr = $9;
12326 					n->columns = $11;
12327 					n->namespaces = $5;
12328 					n->location = @1;
12329 					$$ = (Node *)n;
12330 				}
12331 		;
12332 
12333 xmltable_column_list: xmltable_column_el					{ $$ = list_make1($1); }
12334 			| xmltable_column_list ',' xmltable_column_el	{ $$ = lappend($1, $3); }
12335 		;
12336 
12337 xmltable_column_el:
12338 			ColId Typename
12339 				{
12340 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12341 
12342 					fc->colname = $1;
12343 					fc->for_ordinality = false;
12344 					fc->typeName = $2;
12345 					fc->is_not_null = false;
12346 					fc->colexpr = NULL;
12347 					fc->coldefexpr = NULL;
12348 					fc->location = @1;
12349 
12350 					$$ = (Node *) fc;
12351 				}
12352 			| ColId Typename xmltable_column_option_list
12353 				{
12354 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12355 					ListCell		   *option;
12356 					bool				nullability_seen = false;
12357 
12358 					fc->colname = $1;
12359 					fc->typeName = $2;
12360 					fc->for_ordinality = false;
12361 					fc->is_not_null = false;
12362 					fc->colexpr = NULL;
12363 					fc->coldefexpr = NULL;
12364 					fc->location = @1;
12365 
foreach(option,$3)12366 					foreach(option, $3)
12367 					{
12368 						DefElem   *defel = (DefElem *) lfirst(option);
12369 
12370 						if (strcmp(defel->defname, "default") == 0)
12371 						{
12372 							if (fc->coldefexpr != NULL)
12373 								ereport(ERROR,
12374 										(errcode(ERRCODE_SYNTAX_ERROR),
12375 										 errmsg("only one DEFAULT value is allowed"),
12376 										 parser_errposition(defel->location)));
12377 							fc->coldefexpr = defel->arg;
12378 						}
12379 						else if (strcmp(defel->defname, "path") == 0)
12380 						{
12381 							if (fc->colexpr != NULL)
12382 								ereport(ERROR,
12383 										(errcode(ERRCODE_SYNTAX_ERROR),
12384 										 errmsg("only one PATH value per column is allowed"),
12385 										 parser_errposition(defel->location)));
12386 							fc->colexpr = defel->arg;
12387 						}
12388 						else if (strcmp(defel->defname, "is_not_null") == 0)
12389 						{
12390 							if (nullability_seen)
12391 								ereport(ERROR,
12392 										(errcode(ERRCODE_SYNTAX_ERROR),
12393 										 errmsg("conflicting or redundant NULL / NOT NULL declarations for column \"%s\"", fc->colname),
12394 										 parser_errposition(defel->location)));
12395 							fc->is_not_null = intVal(defel->arg);
12396 							nullability_seen = true;
12397 						}
12398 						else
12399 						{
12400 							ereport(ERROR,
12401 									(errcode(ERRCODE_SYNTAX_ERROR),
12402 									 errmsg("unrecognized column option \"%s\"",
12403 											defel->defname),
12404 									 parser_errposition(defel->location)));
12405 						}
12406 					}
12407 					$$ = (Node *) fc;
12408 				}
12409 			| ColId FOR ORDINALITY
12410 				{
12411 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12412 
12413 					fc->colname = $1;
12414 					fc->for_ordinality = true;
12415 					/* other fields are ignored, initialized by makeNode */
12416 					fc->location = @1;
12417 
12418 					$$ = (Node *) fc;
12419 				}
12420 		;
12421 
12422 xmltable_column_option_list:
12423 			xmltable_column_option_el
12424 				{ $$ = list_make1($1); }
12425 			| xmltable_column_option_list xmltable_column_option_el
12426 				{ $$ = lappend($1, $2); }
12427 		;
12428 
12429 xmltable_column_option_el:
12430 			IDENT b_expr
12431 				{ $$ = makeDefElem($1, $2, @1); }
12432 			| DEFAULT b_expr
12433 				{ $$ = makeDefElem("default", $2, @1); }
12434 			| NOT NULL_P
12435 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(true), @1); }
12436 			| NULL_P
12437 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(false), @1); }
12438 		;
12439 
12440 xml_namespace_list:
12441 			xml_namespace_el
12442 				{ $$ = list_make1($1); }
12443 			| xml_namespace_list ',' xml_namespace_el
12444 				{ $$ = lappend($1, $3); }
12445 		;
12446 
12447 xml_namespace_el:
12448 			b_expr AS ColLabel
12449 				{
12450 					$$ = makeNode(ResTarget);
12451 					$$->name = $3;
12452 					$$->indirection = NIL;
12453 					$$->val = $1;
12454 					$$->location = @1;
12455 				}
12456 			| DEFAULT b_expr
12457 				{
12458 					$$ = makeNode(ResTarget);
12459 					$$->name = NULL;
12460 					$$->indirection = NIL;
12461 					$$->val = $2;
12462 					$$->location = @1;
12463 				}
12464 		;
12465 
12466 /*****************************************************************************
12467  *
12468  *	Type syntax
12469  *		SQL introduces a large amount of type-specific syntax.
12470  *		Define individual clauses to handle these cases, and use
12471  *		 the generic case to handle regular type-extensible Postgres syntax.
12472  *		- thomas 1997-10-10
12473  *
12474  *****************************************************************************/
12475 
12476 Typename:	SimpleTypename opt_array_bounds
12477 				{
12478 					$$ = $1;
12479 					$$->arrayBounds = $2;
12480 				}
12481 			| SETOF SimpleTypename opt_array_bounds
12482 				{
12483 					$$ = $2;
12484 					$$->arrayBounds = $3;
12485 					$$->setof = true;
12486 				}
12487 			/* SQL standard syntax, currently only one-dimensional */
12488 			| SimpleTypename ARRAY '[' Iconst ']'
12489 				{
12490 					$$ = $1;
12491 					$$->arrayBounds = list_make1(makeInteger($4));
12492 				}
12493 			| SETOF SimpleTypename ARRAY '[' Iconst ']'
12494 				{
12495 					$$ = $2;
12496 					$$->arrayBounds = list_make1(makeInteger($5));
12497 					$$->setof = true;
12498 				}
12499 			| SimpleTypename ARRAY
12500 				{
12501 					$$ = $1;
12502 					$$->arrayBounds = list_make1(makeInteger(-1));
12503 				}
12504 			| SETOF SimpleTypename ARRAY
12505 				{
12506 					$$ = $2;
12507 					$$->arrayBounds = list_make1(makeInteger(-1));
12508 					$$->setof = true;
12509 				}
12510 		;
12511 
12512 opt_array_bounds:
12513 			opt_array_bounds '[' ']'
12514 					{  $$ = lappend($1, makeInteger(-1)); }
12515 			| opt_array_bounds '[' Iconst ']'
12516 					{  $$ = lappend($1, makeInteger($3)); }
12517 			| /*EMPTY*/
12518 					{  $$ = NIL; }
12519 		;
12520 
12521 SimpleTypename:
12522 			GenericType								{ $$ = $1; }
12523 			| Numeric								{ $$ = $1; }
12524 			| Bit									{ $$ = $1; }
12525 			| Character								{ $$ = $1; }
12526 			| ConstDatetime							{ $$ = $1; }
12527 			| ConstInterval opt_interval
12528 				{
12529 					$$ = $1;
12530 					$$->typmods = $2;
12531 				}
12532 			| ConstInterval '(' Iconst ')'
12533 				{
12534 					$$ = $1;
12535 					$$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
12536 											 makeIntConst($3, @3));
12537 				}
12538 		;
12539 
12540 /* We have a separate ConstTypename to allow defaulting fixed-length
12541  * types such as CHAR() and BIT() to an unspecified length.
12542  * SQL9x requires that these default to a length of one, but this
12543  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
12544  * where there is an obvious better choice to make.
12545  * Note that ConstInterval is not included here since it must
12546  * be pushed up higher in the rules to accommodate the postfix
12547  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
12548  * the generic-type-name case in AExprConst to avoid premature
12549  * reduce/reduce conflicts against function names.
12550  */
12551 ConstTypename:
12552 			Numeric									{ $$ = $1; }
12553 			| ConstBit								{ $$ = $1; }
12554 			| ConstCharacter						{ $$ = $1; }
12555 			| ConstDatetime							{ $$ = $1; }
12556 		;
12557 
12558 /*
12559  * GenericType covers all type names that don't have special syntax mandated
12560  * by the standard, including qualified names.  We also allow type modifiers.
12561  * To avoid parsing conflicts against function invocations, the modifiers
12562  * have to be shown as expr_list here, but parse analysis will only accept
12563  * constants for them.
12564  */
12565 GenericType:
12566 			type_function_name opt_type_modifiers
12567 				{
12568 					$$ = makeTypeName($1);
12569 					$$->typmods = $2;
12570 					$$->location = @1;
12571 				}
12572 			| type_function_name attrs opt_type_modifiers
12573 				{
12574 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
12575 					$$->typmods = $3;
12576 					$$->location = @1;
12577 				}
12578 		;
12579 
12580 opt_type_modifiers: '(' expr_list ')'				{ $$ = $2; }
12581 					| /* EMPTY */					{ $$ = NIL; }
12582 		;
12583 
12584 /*
12585  * SQL numeric data types
12586  */
12587 Numeric:	INT_P
12588 				{
12589 					$$ = SystemTypeName("int4");
12590 					$$->location = @1;
12591 				}
12592 			| INTEGER
12593 				{
12594 					$$ = SystemTypeName("int4");
12595 					$$->location = @1;
12596 				}
12597 			| SMALLINT
12598 				{
12599 					$$ = SystemTypeName("int2");
12600 					$$->location = @1;
12601 				}
12602 			| BIGINT
12603 				{
12604 					$$ = SystemTypeName("int8");
12605 					$$->location = @1;
12606 				}
12607 			| REAL
12608 				{
12609 					$$ = SystemTypeName("float4");
12610 					$$->location = @1;
12611 				}
12612 			| FLOAT_P opt_float
12613 				{
12614 					$$ = $2;
12615 					$$->location = @1;
12616 				}
12617 			| DOUBLE_P PRECISION
12618 				{
12619 					$$ = SystemTypeName("float8");
12620 					$$->location = @1;
12621 				}
12622 			| DECIMAL_P opt_type_modifiers
12623 				{
12624 					$$ = SystemTypeName("numeric");
12625 					$$->typmods = $2;
12626 					$$->location = @1;
12627 				}
12628 			| DEC opt_type_modifiers
12629 				{
12630 					$$ = SystemTypeName("numeric");
12631 					$$->typmods = $2;
12632 					$$->location = @1;
12633 				}
12634 			| NUMERIC opt_type_modifiers
12635 				{
12636 					$$ = SystemTypeName("numeric");
12637 					$$->typmods = $2;
12638 					$$->location = @1;
12639 				}
12640 			| BOOLEAN_P
12641 				{
12642 					$$ = SystemTypeName("bool");
12643 					$$->location = @1;
12644 				}
12645 		;
12646 
12647 opt_float:	'(' Iconst ')'
12648 				{
12649 					/*
12650 					 * Check FLOAT() precision limits assuming IEEE floating
12651 					 * types - thomas 1997-09-18
12652 					 */
12653 					if ($2 < 1)
12654 						ereport(ERROR,
12655 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12656 								 errmsg("precision for type float must be at least 1 bit"),
12657 								 parser_errposition(@2)));
12658 					else if ($2 <= 24)
12659 						$$ = SystemTypeName("float4");
12660 					else if ($2 <= 53)
12661 						$$ = SystemTypeName("float8");
12662 					else
12663 						ereport(ERROR,
12664 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12665 								 errmsg("precision for type float must be less than 54 bits"),
12666 								 parser_errposition(@2)));
12667 				}
12668 			| /*EMPTY*/
12669 				{
12670 					$$ = SystemTypeName("float8");
12671 				}
12672 		;
12673 
12674 /*
12675  * SQL bit-field data types
12676  * The following implements BIT() and BIT VARYING().
12677  */
12678 Bit:		BitWithLength
12679 				{
12680 					$$ = $1;
12681 				}
12682 			| BitWithoutLength
12683 				{
12684 					$$ = $1;
12685 				}
12686 		;
12687 
12688 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
12689 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
12690 ConstBit:	BitWithLength
12691 				{
12692 					$$ = $1;
12693 				}
12694 			| BitWithoutLength
12695 				{
12696 					$$ = $1;
12697 					$$->typmods = NIL;
12698 				}
12699 		;
12700 
12701 BitWithLength:
12702 			BIT opt_varying '(' expr_list ')'
12703 				{
12704 					char *typname;
12705 
12706 					typname = $2 ? "varbit" : "bit";
12707 					$$ = SystemTypeName(typname);
12708 					$$->typmods = $4;
12709 					$$->location = @1;
12710 				}
12711 		;
12712 
12713 BitWithoutLength:
12714 			BIT opt_varying
12715 				{
12716 					/* bit defaults to bit(1), varbit to no limit */
12717 					if ($2)
12718 					{
12719 						$$ = SystemTypeName("varbit");
12720 					}
12721 					else
12722 					{
12723 						$$ = SystemTypeName("bit");
12724 						$$->typmods = list_make1(makeIntConst(1, -1));
12725 					}
12726 					$$->location = @1;
12727 				}
12728 		;
12729 
12730 
12731 /*
12732  * SQL character data types
12733  * The following implements CHAR() and VARCHAR().
12734  */
12735 Character:  CharacterWithLength
12736 				{
12737 					$$ = $1;
12738 				}
12739 			| CharacterWithoutLength
12740 				{
12741 					$$ = $1;
12742 				}
12743 		;
12744 
12745 ConstCharacter:  CharacterWithLength
12746 				{
12747 					$$ = $1;
12748 				}
12749 			| CharacterWithoutLength
12750 				{
12751 					/* Length was not specified so allow to be unrestricted.
12752 					 * This handles problems with fixed-length (bpchar) strings
12753 					 * which in column definitions must default to a length
12754 					 * of one, but should not be constrained if the length
12755 					 * was not specified.
12756 					 */
12757 					$$ = $1;
12758 					$$->typmods = NIL;
12759 				}
12760 		;
12761 
12762 CharacterWithLength:  character '(' Iconst ')'
12763 				{
12764 					$$ = SystemTypeName($1);
12765 					$$->typmods = list_make1(makeIntConst($3, @3));
12766 					$$->location = @1;
12767 				}
12768 		;
12769 
12770 CharacterWithoutLength:	 character
12771 				{
12772 					$$ = SystemTypeName($1);
12773 					/* char defaults to char(1), varchar to no limit */
12774 					if (strcmp($1, "bpchar") == 0)
12775 						$$->typmods = list_make1(makeIntConst(1, -1));
12776 					$$->location = @1;
12777 				}
12778 		;
12779 
12780 character:	CHARACTER opt_varying
12781 										{ $$ = $2 ? "varchar": "bpchar"; }
12782 			| CHAR_P opt_varying
12783 										{ $$ = $2 ? "varchar": "bpchar"; }
12784 			| VARCHAR
12785 										{ $$ = "varchar"; }
12786 			| NATIONAL CHARACTER opt_varying
12787 										{ $$ = $3 ? "varchar": "bpchar"; }
12788 			| NATIONAL CHAR_P opt_varying
12789 										{ $$ = $3 ? "varchar": "bpchar"; }
12790 			| NCHAR opt_varying
12791 										{ $$ = $2 ? "varchar": "bpchar"; }
12792 		;
12793 
12794 opt_varying:
12795 			VARYING									{ $$ = true; }
12796 			| /*EMPTY*/								{ $$ = false; }
12797 		;
12798 
12799 /*
12800  * SQL date/time types
12801  */
12802 ConstDatetime:
12803 			TIMESTAMP '(' Iconst ')' opt_timezone
12804 				{
12805 					if ($5)
12806 						$$ = SystemTypeName("timestamptz");
12807 					else
12808 						$$ = SystemTypeName("timestamp");
12809 					$$->typmods = list_make1(makeIntConst($3, @3));
12810 					$$->location = @1;
12811 				}
12812 			| TIMESTAMP opt_timezone
12813 				{
12814 					if ($2)
12815 						$$ = SystemTypeName("timestamptz");
12816 					else
12817 						$$ = SystemTypeName("timestamp");
12818 					$$->location = @1;
12819 				}
12820 			| TIME '(' Iconst ')' opt_timezone
12821 				{
12822 					if ($5)
12823 						$$ = SystemTypeName("timetz");
12824 					else
12825 						$$ = SystemTypeName("time");
12826 					$$->typmods = list_make1(makeIntConst($3, @3));
12827 					$$->location = @1;
12828 				}
12829 			| TIME opt_timezone
12830 				{
12831 					if ($2)
12832 						$$ = SystemTypeName("timetz");
12833 					else
12834 						$$ = SystemTypeName("time");
12835 					$$->location = @1;
12836 				}
12837 		;
12838 
12839 ConstInterval:
12840 			INTERVAL
12841 				{
12842 					$$ = SystemTypeName("interval");
12843 					$$->location = @1;
12844 				}
12845 		;
12846 
12847 opt_timezone:
12848 			WITH_LA TIME ZONE						{ $$ = true; }
12849 			| WITHOUT TIME ZONE						{ $$ = false; }
12850 			| /*EMPTY*/								{ $$ = false; }
12851 		;
12852 
12853 opt_interval:
12854 			YEAR_P
12855 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
12856 			| MONTH_P
12857 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
12858 			| DAY_P
12859 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
12860 			| HOUR_P
12861 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
12862 			| MINUTE_P
12863 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
12864 			| interval_second
12865 				{ $$ = $1; }
12866 			| YEAR_P TO MONTH_P
12867 				{
12868 					$$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
12869 												 INTERVAL_MASK(MONTH), @1));
12870 				}
12871 			| DAY_P TO HOUR_P
12872 				{
12873 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12874 												 INTERVAL_MASK(HOUR), @1));
12875 				}
12876 			| DAY_P TO MINUTE_P
12877 				{
12878 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12879 												 INTERVAL_MASK(HOUR) |
12880 												 INTERVAL_MASK(MINUTE), @1));
12881 				}
12882 			| DAY_P TO interval_second
12883 				{
12884 					$$ = $3;
12885 					linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
12886 												INTERVAL_MASK(HOUR) |
12887 												INTERVAL_MASK(MINUTE) |
12888 												INTERVAL_MASK(SECOND), @1);
12889 				}
12890 			| HOUR_P TO MINUTE_P
12891 				{
12892 					$$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
12893 												 INTERVAL_MASK(MINUTE), @1));
12894 				}
12895 			| HOUR_P TO interval_second
12896 				{
12897 					$$ = $3;
12898 					linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
12899 												INTERVAL_MASK(MINUTE) |
12900 												INTERVAL_MASK(SECOND), @1);
12901 				}
12902 			| MINUTE_P TO interval_second
12903 				{
12904 					$$ = $3;
12905 					linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
12906 												INTERVAL_MASK(SECOND), @1);
12907 				}
12908 			| /*EMPTY*/
12909 				{ $$ = NIL; }
12910 		;
12911 
12912 interval_second:
12913 			SECOND_P
12914 				{
12915 					$$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
12916 				}
12917 			| SECOND_P '(' Iconst ')'
12918 				{
12919 					$$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
12920 									makeIntConst($3, @3));
12921 				}
12922 		;
12923 
12924 
12925 /*****************************************************************************
12926  *
12927  *	expression grammar
12928  *
12929  *****************************************************************************/
12930 
12931 /*
12932  * General expressions
12933  * This is the heart of the expression syntax.
12934  *
12935  * We have two expression types: a_expr is the unrestricted kind, and
12936  * b_expr is a subset that must be used in some places to avoid shift/reduce
12937  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
12938  * because that use of AND conflicts with AND as a boolean operator.  So,
12939  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
12940  *
12941  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
12942  * always be used by surrounding it with parens.
12943  *
12944  * c_expr is all the productions that are common to a_expr and b_expr;
12945  * it's factored out just to eliminate redundant coding.
12946  *
12947  * Be careful of productions involving more than one terminal token.
12948  * By default, bison will assign such productions the precedence of their
12949  * last terminal, but in nearly all cases you want it to be the precedence
12950  * of the first terminal instead; otherwise you will not get the behavior
12951  * you expect!  So we use %prec annotations freely to set precedences.
12952  */
12953 a_expr:		c_expr									{ $$ = $1; }
12954 			| a_expr TYPECAST Typename
12955 					{ $$ = makeTypeCast($1, $3, @2); }
12956 			| a_expr COLLATE any_name
12957 				{
12958 					CollateClause *n = makeNode(CollateClause);
12959 					n->arg = $1;
12960 					n->collname = $3;
12961 					n->location = @2;
12962 					$$ = (Node *) n;
12963 				}
12964 			| a_expr AT TIME ZONE a_expr			%prec AT
12965 				{
12966 					$$ = (Node *) makeFuncCall(SystemFuncName("timezone"),
12967 											   list_make2($5, $1),
12968 											   @2);
12969 				}
12970 		/*
12971 		 * These operators must be called out explicitly in order to make use
12972 		 * of bison's automatic operator-precedence handling.  All other
12973 		 * operator names are handled by the generic productions using "Op",
12974 		 * below; and all those operators will have the same precedence.
12975 		 *
12976 		 * If you add more explicitly-known operators, be sure to add them
12977 		 * also to b_expr and to the MathOp list below.
12978 		 */
12979 			| '+' a_expr					%prec UMINUS
12980 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
12981 			| '-' a_expr					%prec UMINUS
12982 				{ $$ = doNegate($2, @1); }
12983 			| a_expr '+' a_expr
12984 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
12985 			| a_expr '-' a_expr
12986 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
12987 			| a_expr '*' a_expr
12988 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
12989 			| a_expr '/' a_expr
12990 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
12991 			| a_expr '%' a_expr
12992 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
12993 			| a_expr '^' a_expr
12994 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
12995 			| a_expr '<' a_expr
12996 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
12997 			| a_expr '>' a_expr
12998 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
12999 			| a_expr '=' a_expr
13000 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13001 			| a_expr LESS_EQUALS a_expr
13002 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13003 			| a_expr GREATER_EQUALS a_expr
13004 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13005 			| a_expr NOT_EQUALS a_expr
13006 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13007 
13008 			| a_expr qual_Op a_expr				%prec Op
13009 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13010 			| qual_Op a_expr					%prec Op
13011 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13012 			| a_expr qual_Op					%prec POSTFIXOP
13013 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13014 
13015 			| a_expr AND a_expr
13016 				{ $$ = makeAndExpr($1, $3, @2); }
13017 			| a_expr OR a_expr
13018 				{ $$ = makeOrExpr($1, $3, @2); }
13019 			| NOT a_expr
13020 				{ $$ = makeNotExpr($2, @1); }
13021 			| NOT_LA a_expr						%prec NOT
13022 				{ $$ = makeNotExpr($2, @1); }
13023 
13024 			| a_expr LIKE a_expr
13025 				{
13026 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13027 												   $1, $3, @2);
13028 				}
13029 			| a_expr LIKE a_expr ESCAPE a_expr					%prec LIKE
13030 				{
13031 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13032 											   list_make2($3, $5),
13033 											   @2);
13034 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13035 												   $1, (Node *) n, @2);
13036 				}
13037 			| a_expr NOT_LA LIKE a_expr							%prec NOT_LA
13038 				{
13039 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13040 												   $1, $4, @2);
13041 				}
13042 			| a_expr NOT_LA LIKE a_expr ESCAPE a_expr			%prec NOT_LA
13043 				{
13044 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13045 											   list_make2($4, $6),
13046 											   @2);
13047 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13048 												   $1, (Node *) n, @2);
13049 				}
13050 			| a_expr ILIKE a_expr
13051 				{
13052 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13053 												   $1, $3, @2);
13054 				}
13055 			| a_expr ILIKE a_expr ESCAPE a_expr					%prec ILIKE
13056 				{
13057 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13058 											   list_make2($3, $5),
13059 											   @2);
13060 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13061 												   $1, (Node *) n, @2);
13062 				}
13063 			| a_expr NOT_LA ILIKE a_expr						%prec NOT_LA
13064 				{
13065 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13066 												   $1, $4, @2);
13067 				}
13068 			| a_expr NOT_LA ILIKE a_expr ESCAPE a_expr			%prec NOT_LA
13069 				{
13070 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13071 											   list_make2($4, $6),
13072 											   @2);
13073 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13074 												   $1, (Node *) n, @2);
13075 				}
13076 
13077 			| a_expr SIMILAR TO a_expr							%prec SIMILAR
13078 				{
13079 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13080 											   list_make2($4, makeNullAConst(-1)),
13081 											   @2);
13082 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13083 												   $1, (Node *) n, @2);
13084 				}
13085 			| a_expr SIMILAR TO a_expr ESCAPE a_expr			%prec SIMILAR
13086 				{
13087 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13088 											   list_make2($4, $6),
13089 											   @2);
13090 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13091 												   $1, (Node *) n, @2);
13092 				}
13093 			| a_expr NOT_LA SIMILAR TO a_expr					%prec NOT_LA
13094 				{
13095 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13096 											   list_make2($5, makeNullAConst(-1)),
13097 											   @2);
13098 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13099 												   $1, (Node *) n, @2);
13100 				}
13101 			| a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr		%prec NOT_LA
13102 				{
13103 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13104 											   list_make2($5, $7),
13105 											   @2);
13106 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13107 												   $1, (Node *) n, @2);
13108 				}
13109 
13110 			/* NullTest clause
13111 			 * Define SQL-style Null test clause.
13112 			 * Allow two forms described in the standard:
13113 			 *	a IS NULL
13114 			 *	a IS NOT NULL
13115 			 * Allow two SQL extensions
13116 			 *	a ISNULL
13117 			 *	a NOTNULL
13118 			 */
13119 			| a_expr IS NULL_P							%prec IS
13120 				{
13121 					NullTest *n = makeNode(NullTest);
13122 					n->arg = (Expr *) $1;
13123 					n->nulltesttype = IS_NULL;
13124 					n->location = @2;
13125 					$$ = (Node *)n;
13126 				}
13127 			| a_expr ISNULL
13128 				{
13129 					NullTest *n = makeNode(NullTest);
13130 					n->arg = (Expr *) $1;
13131 					n->nulltesttype = IS_NULL;
13132 					n->location = @2;
13133 					$$ = (Node *)n;
13134 				}
13135 			| a_expr IS NOT NULL_P						%prec IS
13136 				{
13137 					NullTest *n = makeNode(NullTest);
13138 					n->arg = (Expr *) $1;
13139 					n->nulltesttype = IS_NOT_NULL;
13140 					n->location = @2;
13141 					$$ = (Node *)n;
13142 				}
13143 			| a_expr NOTNULL
13144 				{
13145 					NullTest *n = makeNode(NullTest);
13146 					n->arg = (Expr *) $1;
13147 					n->nulltesttype = IS_NOT_NULL;
13148 					n->location = @2;
13149 					$$ = (Node *)n;
13150 				}
13151 			| row OVERLAPS row
13152 				{
13153 					if (list_length($1) != 2)
13154 						ereport(ERROR,
13155 								(errcode(ERRCODE_SYNTAX_ERROR),
13156 								 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
13157 								 parser_errposition(@1)));
13158 					if (list_length($3) != 2)
13159 						ereport(ERROR,
13160 								(errcode(ERRCODE_SYNTAX_ERROR),
13161 								 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
13162 								 parser_errposition(@3)));
13163 					$$ = (Node *) makeFuncCall(SystemFuncName("overlaps"),
13164 											   list_concat($1, $3),
13165 											   @2);
13166 				}
13167 			| a_expr IS TRUE_P							%prec IS
13168 				{
13169 					BooleanTest *b = makeNode(BooleanTest);
13170 					b->arg = (Expr *) $1;
13171 					b->booltesttype = IS_TRUE;
13172 					b->location = @2;
13173 					$$ = (Node *)b;
13174 				}
13175 			| a_expr IS NOT TRUE_P						%prec IS
13176 				{
13177 					BooleanTest *b = makeNode(BooleanTest);
13178 					b->arg = (Expr *) $1;
13179 					b->booltesttype = IS_NOT_TRUE;
13180 					b->location = @2;
13181 					$$ = (Node *)b;
13182 				}
13183 			| a_expr IS FALSE_P							%prec IS
13184 				{
13185 					BooleanTest *b = makeNode(BooleanTest);
13186 					b->arg = (Expr *) $1;
13187 					b->booltesttype = IS_FALSE;
13188 					b->location = @2;
13189 					$$ = (Node *)b;
13190 				}
13191 			| a_expr IS NOT FALSE_P						%prec IS
13192 				{
13193 					BooleanTest *b = makeNode(BooleanTest);
13194 					b->arg = (Expr *) $1;
13195 					b->booltesttype = IS_NOT_FALSE;
13196 					b->location = @2;
13197 					$$ = (Node *)b;
13198 				}
13199 			| a_expr IS UNKNOWN							%prec IS
13200 				{
13201 					BooleanTest *b = makeNode(BooleanTest);
13202 					b->arg = (Expr *) $1;
13203 					b->booltesttype = IS_UNKNOWN;
13204 					b->location = @2;
13205 					$$ = (Node *)b;
13206 				}
13207 			| a_expr IS NOT UNKNOWN						%prec IS
13208 				{
13209 					BooleanTest *b = makeNode(BooleanTest);
13210 					b->arg = (Expr *) $1;
13211 					b->booltesttype = IS_NOT_UNKNOWN;
13212 					b->location = @2;
13213 					$$ = (Node *)b;
13214 				}
13215 			| a_expr IS DISTINCT FROM a_expr			%prec IS
13216 				{
13217 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13218 				}
13219 			| a_expr IS NOT DISTINCT FROM a_expr		%prec IS
13220 				{
13221 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13222 				}
13223 			| a_expr IS OF '(' type_list ')'			%prec IS
13224 				{
13225 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13226 				}
13227 			| a_expr IS NOT OF '(' type_list ')'		%prec IS
13228 				{
13229 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13230 				}
13231 			| a_expr BETWEEN opt_asymmetric b_expr AND a_expr		%prec BETWEEN
13232 				{
13233 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN,
13234 												   "BETWEEN",
13235 												   $1,
13236 												   (Node *) list_make2($4, $6),
13237 												   @2);
13238 				}
13239 			| a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr %prec NOT_LA
13240 				{
13241 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN,
13242 												   "NOT BETWEEN",
13243 												   $1,
13244 												   (Node *) list_make2($5, $7),
13245 												   @2);
13246 				}
13247 			| a_expr BETWEEN SYMMETRIC b_expr AND a_expr			%prec BETWEEN
13248 				{
13249 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN_SYM,
13250 												   "BETWEEN SYMMETRIC",
13251 												   $1,
13252 												   (Node *) list_make2($4, $6),
13253 												   @2);
13254 				}
13255 			| a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr		%prec NOT_LA
13256 				{
13257 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN_SYM,
13258 												   "NOT BETWEEN SYMMETRIC",
13259 												   $1,
13260 												   (Node *) list_make2($5, $7),
13261 												   @2);
13262 				}
13263 			| a_expr IN_P in_expr
13264 				{
13265 					/* in_expr returns a SubLink or a list of a_exprs */
13266 					if (IsA($3, SubLink))
13267 					{
13268 						/* generate foo = ANY (subquery) */
13269 						SubLink *n = (SubLink *) $3;
13270 						n->subLinkType = ANY_SUBLINK;
13271 						n->subLinkId = 0;
13272 						n->testexpr = $1;
13273 						n->operName = NIL;		/* show it's IN not = ANY */
13274 						n->location = @2;
13275 						$$ = (Node *)n;
13276 					}
13277 					else
13278 					{
13279 						/* generate scalar IN expression */
13280 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
13281 					}
13282 				}
13283 			| a_expr NOT_LA IN_P in_expr						%prec NOT_LA
13284 				{
13285 					/* in_expr returns a SubLink or a list of a_exprs */
13286 					if (IsA($4, SubLink))
13287 					{
13288 						/* generate NOT (foo = ANY (subquery)) */
13289 						/* Make an = ANY node */
13290 						SubLink *n = (SubLink *) $4;
13291 						n->subLinkType = ANY_SUBLINK;
13292 						n->subLinkId = 0;
13293 						n->testexpr = $1;
13294 						n->operName = NIL;		/* show it's IN not = ANY */
13295 						n->location = @2;
13296 						/* Stick a NOT on top; must have same parse location */
13297 						$$ = makeNotExpr((Node *) n, @2);
13298 					}
13299 					else
13300 					{
13301 						/* generate scalar NOT IN expression */
13302 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
13303 					}
13304 				}
13305 			| a_expr subquery_Op sub_type select_with_parens	%prec Op
13306 				{
13307 					SubLink *n = makeNode(SubLink);
13308 					n->subLinkType = $3;
13309 					n->subLinkId = 0;
13310 					n->testexpr = $1;
13311 					n->operName = $2;
13312 					n->subselect = $4;
13313 					n->location = @2;
13314 					$$ = (Node *)n;
13315 				}
13316 			| a_expr subquery_Op sub_type '(' a_expr ')'		%prec Op
13317 				{
13318 					if ($3 == ANY_SUBLINK)
13319 						$$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
13320 					else
13321 						$$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
13322 				}
13323 			| UNIQUE select_with_parens
13324 				{
13325 					/* Not sure how to get rid of the parentheses
13326 					 * but there are lots of shift/reduce errors without them.
13327 					 *
13328 					 * Should be able to implement this by plopping the entire
13329 					 * select into a node, then transforming the target expressions
13330 					 * from whatever they are into count(*), and testing the
13331 					 * entire result equal to one.
13332 					 * But, will probably implement a separate node in the executor.
13333 					 */
13334 					ereport(ERROR,
13335 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13336 							 errmsg("UNIQUE predicate is not yet implemented"),
13337 							 parser_errposition(@1)));
13338 				}
13339 			| a_expr IS DOCUMENT_P					%prec IS
13340 				{
13341 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13342 									 list_make1($1), @2);
13343 				}
13344 			| a_expr IS NOT DOCUMENT_P				%prec IS
13345 				{
13346 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13347 												 list_make1($1), @2),
13348 									 @2);
13349 				}
13350 			| DEFAULT
13351 				{
13352 					/*
13353 					 * The SQL spec only allows DEFAULT in "contextually typed
13354 					 * expressions", but for us, it's easier to allow it in
13355 					 * any a_expr and then throw error during parse analysis
13356 					 * if it's in an inappropriate context.  This way also
13357 					 * lets us say something smarter than "syntax error".
13358 					 */
13359 					SetToDefault *n = makeNode(SetToDefault);
13360 					/* parse analysis will fill in the rest */
13361 					n->location = @1;
13362 					$$ = (Node *)n;
13363 				}
13364 		;
13365 
13366 /*
13367  * Restricted expressions
13368  *
13369  * b_expr is a subset of the complete expression syntax defined by a_expr.
13370  *
13371  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
13372  * cause trouble in the places where b_expr is used.  For simplicity, we
13373  * just eliminate all the boolean-keyword-operator productions from b_expr.
13374  */
13375 b_expr:		c_expr
13376 				{ $$ = $1; }
13377 			| b_expr TYPECAST Typename
13378 				{ $$ = makeTypeCast($1, $3, @2); }
13379 			| '+' b_expr					%prec UMINUS
13380 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13381 			| '-' b_expr					%prec UMINUS
13382 				{ $$ = doNegate($2, @1); }
13383 			| b_expr '+' b_expr
13384 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13385 			| b_expr '-' b_expr
13386 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13387 			| b_expr '*' b_expr
13388 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13389 			| b_expr '/' b_expr
13390 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13391 			| b_expr '%' b_expr
13392 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13393 			| b_expr '^' b_expr
13394 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13395 			| b_expr '<' b_expr
13396 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13397 			| b_expr '>' b_expr
13398 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13399 			| b_expr '=' b_expr
13400 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13401 			| b_expr LESS_EQUALS b_expr
13402 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13403 			| b_expr GREATER_EQUALS b_expr
13404 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13405 			| b_expr NOT_EQUALS b_expr
13406 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13407 			| b_expr qual_Op b_expr				%prec Op
13408 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13409 			| qual_Op b_expr					%prec Op
13410 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13411 			| b_expr qual_Op					%prec POSTFIXOP
13412 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13413 			| b_expr IS DISTINCT FROM b_expr		%prec IS
13414 				{
13415 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13416 				}
13417 			| b_expr IS NOT DISTINCT FROM b_expr	%prec IS
13418 				{
13419 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13420 				}
13421 			| b_expr IS OF '(' type_list ')'		%prec IS
13422 				{
13423 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13424 				}
13425 			| b_expr IS NOT OF '(' type_list ')'	%prec IS
13426 				{
13427 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13428 				}
13429 			| b_expr IS DOCUMENT_P					%prec IS
13430 				{
13431 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13432 									 list_make1($1), @2);
13433 				}
13434 			| b_expr IS NOT DOCUMENT_P				%prec IS
13435 				{
13436 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13437 												 list_make1($1), @2),
13438 									 @2);
13439 				}
13440 		;
13441 
13442 /*
13443  * Productions that can be used in both a_expr and b_expr.
13444  *
13445  * Note: productions that refer recursively to a_expr or b_expr mostly
13446  * cannot appear here.	However, it's OK to refer to a_exprs that occur
13447  * inside parentheses, such as function arguments; that cannot introduce
13448  * ambiguity to the b_expr syntax.
13449  */
13450 c_expr:		columnref								{ $$ = $1; }
13451 			| AexprConst							{ $$ = $1; }
13452 			| PARAM opt_indirection
13453 				{
13454 					ParamRef *p = makeNode(ParamRef);
13455 					p->number = $1;
13456 					p->location = @1;
13457 					if ($2)
13458 					{
13459 						A_Indirection *n = makeNode(A_Indirection);
13460 						n->arg = (Node *) p;
13461 						n->indirection = check_indirection($2, yyscanner);
13462 						$$ = (Node *) n;
13463 					}
13464 					else
13465 						$$ = (Node *) p;
13466 				}
13467 			| '(' a_expr ')' opt_indirection
13468 				{
13469 					if ($4)
13470 					{
13471 						A_Indirection *n = makeNode(A_Indirection);
13472 						n->arg = $2;
13473 						n->indirection = check_indirection($4, yyscanner);
13474 						$$ = (Node *)n;
13475 					}
13476 					else if (operator_precedence_warning)
13477 					{
13478 						/*
13479 						 * If precedence warnings are enabled, insert
13480 						 * AEXPR_PAREN nodes wrapping all explicitly
13481 						 * parenthesized subexpressions; this prevents bogus
13482 						 * warnings from being issued when the ordering has
13483 						 * been forced by parentheses.  Take care that an
13484 						 * AEXPR_PAREN node has the same exprLocation as its
13485 						 * child, so as not to cause surprising changes in
13486 						 * error cursor positioning.
13487 						 *
13488 						 * In principle we should not be relying on a GUC to
13489 						 * decide whether to insert AEXPR_PAREN nodes.
13490 						 * However, since they have no effect except to
13491 						 * suppress warnings, it's probably safe enough; and
13492 						 * we'd just as soon not waste cycles on dummy parse
13493 						 * nodes if we don't have to.
13494 						 */
13495 						$$ = (Node *) makeA_Expr(AEXPR_PAREN, NIL, $2, NULL,
13496 												 exprLocation($2));
13497 					}
13498 					else
13499 						$$ = $2;
13500 				}
13501 			| case_expr
13502 				{ $$ = $1; }
13503 			| func_expr
13504 				{ $$ = $1; }
13505 			| select_with_parens			%prec UMINUS
13506 				{
13507 					SubLink *n = makeNode(SubLink);
13508 					n->subLinkType = EXPR_SUBLINK;
13509 					n->subLinkId = 0;
13510 					n->testexpr = NULL;
13511 					n->operName = NIL;
13512 					n->subselect = $1;
13513 					n->location = @1;
13514 					$$ = (Node *)n;
13515 				}
13516 			| select_with_parens indirection
13517 				{
13518 					/*
13519 					 * Because the select_with_parens nonterminal is designed
13520 					 * to "eat" as many levels of parens as possible, the
13521 					 * '(' a_expr ')' opt_indirection production above will
13522 					 * fail to match a sub-SELECT with indirection decoration;
13523 					 * the sub-SELECT won't be regarded as an a_expr as long
13524 					 * as there are parens around it.  To support applying
13525 					 * subscripting or field selection to a sub-SELECT result,
13526 					 * we need this redundant-looking production.
13527 					 */
13528 					SubLink *n = makeNode(SubLink);
13529 					A_Indirection *a = makeNode(A_Indirection);
13530 					n->subLinkType = EXPR_SUBLINK;
13531 					n->subLinkId = 0;
13532 					n->testexpr = NULL;
13533 					n->operName = NIL;
13534 					n->subselect = $1;
13535 					n->location = @1;
13536 					a->arg = (Node *)n;
13537 					a->indirection = check_indirection($2, yyscanner);
13538 					$$ = (Node *)a;
13539 				}
13540 			| EXISTS select_with_parens
13541 				{
13542 					SubLink *n = makeNode(SubLink);
13543 					n->subLinkType = EXISTS_SUBLINK;
13544 					n->subLinkId = 0;
13545 					n->testexpr = NULL;
13546 					n->operName = NIL;
13547 					n->subselect = $2;
13548 					n->location = @1;
13549 					$$ = (Node *)n;
13550 				}
13551 			| ARRAY select_with_parens
13552 				{
13553 					SubLink *n = makeNode(SubLink);
13554 					n->subLinkType = ARRAY_SUBLINK;
13555 					n->subLinkId = 0;
13556 					n->testexpr = NULL;
13557 					n->operName = NIL;
13558 					n->subselect = $2;
13559 					n->location = @1;
13560 					$$ = (Node *)n;
13561 				}
13562 			| ARRAY array_expr
13563 				{
13564 					A_ArrayExpr *n = castNode(A_ArrayExpr, $2);
13565 					/* point outermost A_ArrayExpr to the ARRAY keyword */
13566 					n->location = @1;
13567 					$$ = (Node *)n;
13568 				}
13569 			| explicit_row
13570 				{
13571 					RowExpr *r = makeNode(RowExpr);
13572 					r->args = $1;
13573 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13574 					r->colnames = NIL;	/* to be filled in during analysis */
13575 					r->row_format = COERCE_EXPLICIT_CALL; /* abuse */
13576 					r->location = @1;
13577 					$$ = (Node *)r;
13578 				}
13579 			| implicit_row
13580 				{
13581 					RowExpr *r = makeNode(RowExpr);
13582 					r->args = $1;
13583 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13584 					r->colnames = NIL;	/* to be filled in during analysis */
13585 					r->row_format = COERCE_IMPLICIT_CAST; /* abuse */
13586 					r->location = @1;
13587 					$$ = (Node *)r;
13588 				}
13589 			| GROUPING '(' expr_list ')'
13590 			  {
13591 				  GroupingFunc *g = makeNode(GroupingFunc);
13592 				  g->args = $3;
13593 				  g->location = @1;
13594 				  $$ = (Node *)g;
13595 			  }
13596 		;
13597 
13598 func_application: func_name '(' ')'
13599 				{
13600 					$$ = (Node *) makeFuncCall($1, NIL, @1);
13601 				}
13602 			| func_name '(' func_arg_list opt_sort_clause ')'
13603 				{
13604 					FuncCall *n = makeFuncCall($1, $3, @1);
13605 					n->agg_order = $4;
13606 					$$ = (Node *)n;
13607 				}
13608 			| func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'
13609 				{
13610 					FuncCall *n = makeFuncCall($1, list_make1($4), @1);
13611 					n->func_variadic = true;
13612 					n->agg_order = $5;
13613 					$$ = (Node *)n;
13614 				}
13615 			| func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'
13616 				{
13617 					FuncCall *n = makeFuncCall($1, lappend($3, $6), @1);
13618 					n->func_variadic = true;
13619 					n->agg_order = $7;
13620 					$$ = (Node *)n;
13621 				}
13622 			| func_name '(' ALL func_arg_list opt_sort_clause ')'
13623 				{
13624 					FuncCall *n = makeFuncCall($1, $4, @1);
13625 					n->agg_order = $5;
13626 					/* Ideally we'd mark the FuncCall node to indicate
13627 					 * "must be an aggregate", but there's no provision
13628 					 * for that in FuncCall at the moment.
13629 					 */
13630 					$$ = (Node *)n;
13631 				}
13632 			| func_name '(' DISTINCT func_arg_list opt_sort_clause ')'
13633 				{
13634 					FuncCall *n = makeFuncCall($1, $4, @1);
13635 					n->agg_order = $5;
13636 					n->agg_distinct = true;
13637 					$$ = (Node *)n;
13638 				}
13639 			| func_name '(' '*' ')'
13640 				{
13641 					/*
13642 					 * We consider AGGREGATE(*) to invoke a parameterless
13643 					 * aggregate.  This does the right thing for COUNT(*),
13644 					 * and there are no other aggregates in SQL that accept
13645 					 * '*' as parameter.
13646 					 *
13647 					 * The FuncCall node is also marked agg_star = true,
13648 					 * so that later processing can detect what the argument
13649 					 * really was.
13650 					 */
13651 					FuncCall *n = makeFuncCall($1, NIL, @1);
13652 					n->agg_star = true;
13653 					$$ = (Node *)n;
13654 				}
13655 		;
13656 
13657 
13658 /*
13659  * func_expr and its cousin func_expr_windowless are split out from c_expr just
13660  * so that we have classifications for "everything that is a function call or
13661  * looks like one".  This isn't very important, but it saves us having to
13662  * document which variants are legal in places like "FROM function()" or the
13663  * backwards-compatible functional-index syntax for CREATE INDEX.
13664  * (Note that many of the special SQL functions wouldn't actually make any
13665  * sense as functional index entries, but we ignore that consideration here.)
13666  */
13667 func_expr: func_application within_group_clause filter_clause over_clause
13668 				{
13669 					FuncCall *n = (FuncCall *) $1;
13670 					/*
13671 					 * The order clause for WITHIN GROUP and the one for
13672 					 * plain-aggregate ORDER BY share a field, so we have to
13673 					 * check here that at most one is present.  We also check
13674 					 * for DISTINCT and VARIADIC here to give a better error
13675 					 * location.  Other consistency checks are deferred to
13676 					 * parse analysis.
13677 					 */
13678 					if ($2 != NIL)
13679 					{
13680 						if (n->agg_order != NIL)
13681 							ereport(ERROR,
13682 									(errcode(ERRCODE_SYNTAX_ERROR),
13683 									 errmsg("cannot use multiple ORDER BY clauses with WITHIN GROUP"),
13684 									 parser_errposition(@2)));
13685 						if (n->agg_distinct)
13686 							ereport(ERROR,
13687 									(errcode(ERRCODE_SYNTAX_ERROR),
13688 									 errmsg("cannot use DISTINCT with WITHIN GROUP"),
13689 									 parser_errposition(@2)));
13690 						if (n->func_variadic)
13691 							ereport(ERROR,
13692 									(errcode(ERRCODE_SYNTAX_ERROR),
13693 									 errmsg("cannot use VARIADIC with WITHIN GROUP"),
13694 									 parser_errposition(@2)));
13695 						n->agg_order = $2;
13696 						n->agg_within_group = true;
13697 					}
13698 					n->agg_filter = $3;
13699 					n->over = $4;
13700 					$$ = (Node *) n;
13701 				}
13702 			| func_expr_common_subexpr
13703 				{ $$ = $1; }
13704 		;
13705 
13706 /*
13707  * As func_expr but does not accept WINDOW functions directly
13708  * (but they can still be contained in arguments for functions etc).
13709  * Use this when window expressions are not allowed, where needed to
13710  * disambiguate the grammar (e.g. in CREATE INDEX).
13711  */
13712 func_expr_windowless:
13713 			func_application						{ $$ = $1; }
13714 			| func_expr_common_subexpr				{ $$ = $1; }
13715 		;
13716 
13717 /*
13718  * Special expressions that are considered to be functions.
13719  */
13720 func_expr_common_subexpr:
13721 			COLLATION FOR '(' a_expr ')'
13722 				{
13723 					$$ = (Node *) makeFuncCall(SystemFuncName("pg_collation_for"),
13724 											   list_make1($4),
13725 											   @1);
13726 				}
13727 			| CURRENT_DATE
13728 				{
13729 					$$ = makeSQLValueFunction(SVFOP_CURRENT_DATE, -1, @1);
13730 				}
13731 			| CURRENT_TIME
13732 				{
13733 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME, -1, @1);
13734 				}
13735 			| CURRENT_TIME '(' Iconst ')'
13736 				{
13737 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME_N, $3, @1);
13738 				}
13739 			| CURRENT_TIMESTAMP
13740 				{
13741 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP, -1, @1);
13742 				}
13743 			| CURRENT_TIMESTAMP '(' Iconst ')'
13744 				{
13745 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP_N, $3, @1);
13746 				}
13747 			| LOCALTIME
13748 				{
13749 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME, -1, @1);
13750 				}
13751 			| LOCALTIME '(' Iconst ')'
13752 				{
13753 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME_N, $3, @1);
13754 				}
13755 			| LOCALTIMESTAMP
13756 				{
13757 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP, -1, @1);
13758 				}
13759 			| LOCALTIMESTAMP '(' Iconst ')'
13760 				{
13761 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP_N, $3, @1);
13762 				}
13763 			| CURRENT_ROLE
13764 				{
13765 					$$ = makeSQLValueFunction(SVFOP_CURRENT_ROLE, -1, @1);
13766 				}
13767 			| CURRENT_USER
13768 				{
13769 					$$ = makeSQLValueFunction(SVFOP_CURRENT_USER, -1, @1);
13770 				}
13771 			| SESSION_USER
13772 				{
13773 					$$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1);
13774 				}
13775 			| USER
13776 				{
13777 					$$ = makeSQLValueFunction(SVFOP_USER, -1, @1);
13778 				}
13779 			| CURRENT_CATALOG
13780 				{
13781 					$$ = makeSQLValueFunction(SVFOP_CURRENT_CATALOG, -1, @1);
13782 				}
13783 			| CURRENT_SCHEMA
13784 				{
13785 					$$ = makeSQLValueFunction(SVFOP_CURRENT_SCHEMA, -1, @1);
13786 				}
13787 			| CAST '(' a_expr AS Typename ')'
13788 				{ $$ = makeTypeCast($3, $5, @1); }
13789 			| EXTRACT '(' extract_list ')'
13790 				{
13791 					$$ = (Node *) makeFuncCall(SystemFuncName("date_part"), $3, @1);
13792 				}
13793 			| OVERLAY '(' overlay_list ')'
13794 				{
13795 					/* overlay(A PLACING B FROM C FOR D) is converted to
13796 					 * overlay(A, B, C, D)
13797 					 * overlay(A PLACING B FROM C) is converted to
13798 					 * overlay(A, B, C)
13799 					 */
13800 					$$ = (Node *) makeFuncCall(SystemFuncName("overlay"), $3, @1);
13801 				}
13802 			| POSITION '(' position_list ')'
13803 				{
13804 					/* position(A in B) is converted to position(B, A) */
13805 					$$ = (Node *) makeFuncCall(SystemFuncName("position"), $3, @1);
13806 				}
13807 			| SUBSTRING '(' substr_list ')'
13808 				{
13809 					/* substring(A from B for C) is converted to
13810 					 * substring(A, B, C) - thomas 2000-11-28
13811 					 */
13812 					$$ = (Node *) makeFuncCall(SystemFuncName("substring"), $3, @1);
13813 				}
13814 			| TREAT '(' a_expr AS Typename ')'
13815 				{
13816 					/* TREAT(expr AS target) converts expr of a particular type to target,
13817 					 * which is defined to be a subtype of the original expression.
13818 					 * In SQL99, this is intended for use with structured UDTs,
13819 					 * but let's make this a generally useful form allowing stronger
13820 					 * coercions than are handled by implicit casting.
13821 					 *
13822 					 * Convert SystemTypeName() to SystemFuncName() even though
13823 					 * at the moment they result in the same thing.
13824 					 */
13825 					$$ = (Node *) makeFuncCall(SystemFuncName(((Value *)llast($5->names))->val.str),
13826 												list_make1($3),
13827 												@1);
13828 				}
13829 			| TRIM '(' BOTH trim_list ')'
13830 				{
13831 					/* various trim expressions are defined in SQL
13832 					 * - thomas 1997-07-19
13833 					 */
13834 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $4, @1);
13835 				}
13836 			| TRIM '(' LEADING trim_list ')'
13837 				{
13838 					$$ = (Node *) makeFuncCall(SystemFuncName("ltrim"), $4, @1);
13839 				}
13840 			| TRIM '(' TRAILING trim_list ')'
13841 				{
13842 					$$ = (Node *) makeFuncCall(SystemFuncName("rtrim"), $4, @1);
13843 				}
13844 			| TRIM '(' trim_list ')'
13845 				{
13846 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $3, @1);
13847 				}
13848 			| NULLIF '(' a_expr ',' a_expr ')'
13849 				{
13850 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
13851 				}
13852 			| COALESCE '(' expr_list ')'
13853 				{
13854 					CoalesceExpr *c = makeNode(CoalesceExpr);
13855 					c->args = $3;
13856 					c->location = @1;
13857 					$$ = (Node *)c;
13858 				}
13859 			| GREATEST '(' expr_list ')'
13860 				{
13861 					MinMaxExpr *v = makeNode(MinMaxExpr);
13862 					v->args = $3;
13863 					v->op = IS_GREATEST;
13864 					v->location = @1;
13865 					$$ = (Node *)v;
13866 				}
13867 			| LEAST '(' expr_list ')'
13868 				{
13869 					MinMaxExpr *v = makeNode(MinMaxExpr);
13870 					v->args = $3;
13871 					v->op = IS_LEAST;
13872 					v->location = @1;
13873 					$$ = (Node *)v;
13874 				}
13875 			| XMLCONCAT '(' expr_list ')'
13876 				{
13877 					$$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
13878 				}
13879 			| XMLELEMENT '(' NAME_P ColLabel ')'
13880 				{
13881 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
13882 				}
13883 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
13884 				{
13885 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
13886 				}
13887 			| XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
13888 				{
13889 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
13890 				}
13891 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
13892 				{
13893 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
13894 				}
13895 			| XMLEXISTS '(' c_expr xmlexists_argument ')'
13896 				{
13897 					/* xmlexists(A PASSING [BY REF] B [BY REF]) is
13898 					 * converted to xmlexists(A, B)*/
13899 					$$ = (Node *) makeFuncCall(SystemFuncName("xmlexists"), list_make2($3, $4), @1);
13900 				}
13901 			| XMLFOREST '(' xml_attribute_list ')'
13902 				{
13903 					$$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
13904 				}
13905 			| XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
13906 				{
13907 					XmlExpr *x = (XmlExpr *)
13908 						makeXmlExpr(IS_XMLPARSE, NULL, NIL,
13909 									list_make2($4, makeBoolAConst($5, -1)),
13910 									@1);
13911 					x->xmloption = $3;
13912 					$$ = (Node *)x;
13913 				}
13914 			| XMLPI '(' NAME_P ColLabel ')'
13915 				{
13916 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
13917 				}
13918 			| XMLPI '(' NAME_P ColLabel ',' a_expr ')'
13919 				{
13920 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
13921 				}
13922 			| XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
13923 				{
13924 					$$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
13925 									 list_make3($3, $5, $6), @1);
13926 				}
13927 			| XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
13928 				{
13929 					XmlSerialize *n = makeNode(XmlSerialize);
13930 					n->xmloption = $3;
13931 					n->expr = $4;
13932 					n->typeName = $6;
13933 					n->location = @1;
13934 					$$ = (Node *)n;
13935 				}
13936 		;
13937 
13938 /*
13939  * SQL/XML support
13940  */
13941 xml_root_version: VERSION_P a_expr
13942 				{ $$ = $2; }
13943 			| VERSION_P NO VALUE_P
13944 				{ $$ = makeNullAConst(-1); }
13945 		;
13946 
13947 opt_xml_root_standalone: ',' STANDALONE_P YES_P
13948 				{ $$ = makeIntConst(XML_STANDALONE_YES, -1); }
13949 			| ',' STANDALONE_P NO
13950 				{ $$ = makeIntConst(XML_STANDALONE_NO, -1); }
13951 			| ',' STANDALONE_P NO VALUE_P
13952 				{ $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
13953 			| /*EMPTY*/
13954 				{ $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
13955 		;
13956 
13957 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'	{ $$ = $3; }
13958 		;
13959 
13960 xml_attribute_list:	xml_attribute_el					{ $$ = list_make1($1); }
13961 			| xml_attribute_list ',' xml_attribute_el	{ $$ = lappend($1, $3); }
13962 		;
13963 
13964 xml_attribute_el: a_expr AS ColLabel
13965 				{
13966 					$$ = makeNode(ResTarget);
13967 					$$->name = $3;
13968 					$$->indirection = NIL;
13969 					$$->val = (Node *) $1;
13970 					$$->location = @1;
13971 				}
13972 			| a_expr
13973 				{
13974 					$$ = makeNode(ResTarget);
13975 					$$->name = NULL;
13976 					$$->indirection = NIL;
13977 					$$->val = (Node *) $1;
13978 					$$->location = @1;
13979 				}
13980 		;
13981 
13982 document_or_content: DOCUMENT_P						{ $$ = XMLOPTION_DOCUMENT; }
13983 			| CONTENT_P								{ $$ = XMLOPTION_CONTENT; }
13984 		;
13985 
13986 xml_whitespace_option: PRESERVE WHITESPACE_P		{ $$ = true; }
13987 			| STRIP_P WHITESPACE_P					{ $$ = false; }
13988 			| /*EMPTY*/								{ $$ = false; }
13989 		;
13990 
13991 /* We allow several variants for SQL and other compatibility. */
13992 xmlexists_argument:
13993 			PASSING c_expr
13994 				{
13995 					$$ = $2;
13996 				}
13997 			| PASSING c_expr BY REF
13998 				{
13999 					$$ = $2;
14000 				}
14001 			| PASSING BY REF c_expr
14002 				{
14003 					$$ = $4;
14004 				}
14005 			| PASSING BY REF c_expr BY REF
14006 				{
14007 					$$ = $4;
14008 				}
14009 		;
14010 
14011 
14012 /*
14013  * Aggregate decoration clauses
14014  */
14015 within_group_clause:
14016 			WITHIN GROUP_P '(' sort_clause ')'		{ $$ = $4; }
14017 			| /*EMPTY*/								{ $$ = NIL; }
14018 		;
14019 
14020 filter_clause:
14021 			FILTER '(' WHERE a_expr ')'				{ $$ = $4; }
14022 			| /*EMPTY*/								{ $$ = NULL; }
14023 		;
14024 
14025 
14026 /*
14027  * Window Definitions
14028  */
14029 window_clause:
14030 			WINDOW window_definition_list			{ $$ = $2; }
14031 			| /*EMPTY*/								{ $$ = NIL; }
14032 		;
14033 
14034 window_definition_list:
14035 			window_definition						{ $$ = list_make1($1); }
14036 			| window_definition_list ',' window_definition
14037 													{ $$ = lappend($1, $3); }
14038 		;
14039 
14040 window_definition:
14041 			ColId AS window_specification
14042 				{
14043 					WindowDef *n = $3;
14044 					n->name = $1;
14045 					$$ = n;
14046 				}
14047 		;
14048 
14049 over_clause: OVER window_specification
14050 				{ $$ = $2; }
14051 			| OVER ColId
14052 				{
14053 					WindowDef *n = makeNode(WindowDef);
14054 					n->name = $2;
14055 					n->refname = NULL;
14056 					n->partitionClause = NIL;
14057 					n->orderClause = NIL;
14058 					n->frameOptions = FRAMEOPTION_DEFAULTS;
14059 					n->startOffset = NULL;
14060 					n->endOffset = NULL;
14061 					n->location = @2;
14062 					$$ = n;
14063 				}
14064 			| /*EMPTY*/
14065 				{ $$ = NULL; }
14066 		;
14067 
14068 window_specification: '(' opt_existing_window_name opt_partition_clause
14069 						opt_sort_clause opt_frame_clause ')'
14070 				{
14071 					WindowDef *n = makeNode(WindowDef);
14072 					n->name = NULL;
14073 					n->refname = $2;
14074 					n->partitionClause = $3;
14075 					n->orderClause = $4;
14076 					/* copy relevant fields of opt_frame_clause */
14077 					n->frameOptions = $5->frameOptions;
14078 					n->startOffset = $5->startOffset;
14079 					n->endOffset = $5->endOffset;
14080 					n->location = @1;
14081 					$$ = n;
14082 				}
14083 		;
14084 
14085 /*
14086  * If we see PARTITION, RANGE, ROWS or GROUPS as the first token after the '('
14087  * of a window_specification, we want the assumption to be that there is
14088  * no existing_window_name; but those keywords are unreserved and so could
14089  * be ColIds.  We fix this by making them have the same precedence as IDENT
14090  * and giving the empty production here a slightly higher precedence, so
14091  * that the shift/reduce conflict is resolved in favor of reducing the rule.
14092  * These keywords are thus precluded from being an existing_window_name but
14093  * are not reserved for any other purpose.
14094  */
14095 opt_existing_window_name: ColId						{ $$ = $1; }
14096 			| /*EMPTY*/				%prec Op		{ $$ = NULL; }
14097 		;
14098 
14099 opt_partition_clause: PARTITION BY expr_list		{ $$ = $3; }
14100 			| /*EMPTY*/								{ $$ = NIL; }
14101 		;
14102 
14103 /*
14104  * For frame clauses, we return a WindowDef, but only some fields are used:
14105  * frameOptions, startOffset, and endOffset.
14106  */
14107 opt_frame_clause:
14108 			RANGE frame_extent opt_window_exclusion_clause
14109 				{
14110 					WindowDef *n = $2;
14111 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
14112 					n->frameOptions |= $3;
14113 					$$ = n;
14114 				}
14115 			| ROWS frame_extent opt_window_exclusion_clause
14116 				{
14117 					WindowDef *n = $2;
14118 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
14119 					n->frameOptions |= $3;
14120 					$$ = n;
14121 				}
14122 			| GROUPS frame_extent opt_window_exclusion_clause
14123 				{
14124 					WindowDef *n = $2;
14125 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_GROUPS;
14126 					n->frameOptions |= $3;
14127 					$$ = n;
14128 				}
14129 			| /*EMPTY*/
14130 				{
14131 					WindowDef *n = makeNode(WindowDef);
14132 					n->frameOptions = FRAMEOPTION_DEFAULTS;
14133 					n->startOffset = NULL;
14134 					n->endOffset = NULL;
14135 					$$ = n;
14136 				}
14137 		;
14138 
14139 frame_extent: frame_bound
14140 				{
14141 					WindowDef *n = $1;
14142 					/* reject invalid cases */
14143 					if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14144 						ereport(ERROR,
14145 								(errcode(ERRCODE_WINDOWING_ERROR),
14146 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14147 								 parser_errposition(@1)));
14148 					if (n->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
14149 						ereport(ERROR,
14150 								(errcode(ERRCODE_WINDOWING_ERROR),
14151 								 errmsg("frame starting from following row cannot end with current row"),
14152 								 parser_errposition(@1)));
14153 					n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
14154 					$$ = n;
14155 				}
14156 			| BETWEEN frame_bound AND frame_bound
14157 				{
14158 					WindowDef *n1 = $2;
14159 					WindowDef *n2 = $4;
14160 					/* form merged options */
14161 					int		frameOptions = n1->frameOptions;
14162 					/* shift converts START_ options to END_ options */
14163 					frameOptions |= n2->frameOptions << 1;
14164 					frameOptions |= FRAMEOPTION_BETWEEN;
14165 					/* reject invalid cases */
14166 					if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14167 						ereport(ERROR,
14168 								(errcode(ERRCODE_WINDOWING_ERROR),
14169 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14170 								 parser_errposition(@2)));
14171 					if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
14172 						ereport(ERROR,
14173 								(errcode(ERRCODE_WINDOWING_ERROR),
14174 								 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
14175 								 parser_errposition(@4)));
14176 					if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
14177 						(frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING))
14178 						ereport(ERROR,
14179 								(errcode(ERRCODE_WINDOWING_ERROR),
14180 								 errmsg("frame starting from current row cannot have preceding rows"),
14181 								 parser_errposition(@4)));
14182 					if ((frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING) &&
14183 						(frameOptions & (FRAMEOPTION_END_OFFSET_PRECEDING |
14184 										 FRAMEOPTION_END_CURRENT_ROW)))
14185 						ereport(ERROR,
14186 								(errcode(ERRCODE_WINDOWING_ERROR),
14187 								 errmsg("frame starting from following row cannot have preceding rows"),
14188 								 parser_errposition(@4)));
14189 					n1->frameOptions = frameOptions;
14190 					n1->endOffset = n2->startOffset;
14191 					$$ = n1;
14192 				}
14193 		;
14194 
14195 /*
14196  * This is used for both frame start and frame end, with output set up on
14197  * the assumption it's frame start; the frame_extent productions must reject
14198  * invalid cases.
14199  */
14200 frame_bound:
14201 			UNBOUNDED PRECEDING
14202 				{
14203 					WindowDef *n = makeNode(WindowDef);
14204 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
14205 					n->startOffset = NULL;
14206 					n->endOffset = NULL;
14207 					$$ = n;
14208 				}
14209 			| UNBOUNDED FOLLOWING
14210 				{
14211 					WindowDef *n = makeNode(WindowDef);
14212 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
14213 					n->startOffset = NULL;
14214 					n->endOffset = NULL;
14215 					$$ = n;
14216 				}
14217 			| CURRENT_P ROW
14218 				{
14219 					WindowDef *n = makeNode(WindowDef);
14220 					n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
14221 					n->startOffset = NULL;
14222 					n->endOffset = NULL;
14223 					$$ = n;
14224 				}
14225 			| a_expr PRECEDING
14226 				{
14227 					WindowDef *n = makeNode(WindowDef);
14228 					n->frameOptions = FRAMEOPTION_START_OFFSET_PRECEDING;
14229 					n->startOffset = $1;
14230 					n->endOffset = NULL;
14231 					$$ = n;
14232 				}
14233 			| a_expr FOLLOWING
14234 				{
14235 					WindowDef *n = makeNode(WindowDef);
14236 					n->frameOptions = FRAMEOPTION_START_OFFSET_FOLLOWING;
14237 					n->startOffset = $1;
14238 					n->endOffset = NULL;
14239 					$$ = n;
14240 				}
14241 		;
14242 
14243 opt_window_exclusion_clause:
14244 			EXCLUDE CURRENT_P ROW	{ $$ = FRAMEOPTION_EXCLUDE_CURRENT_ROW; }
14245 			| EXCLUDE GROUP_P		{ $$ = FRAMEOPTION_EXCLUDE_GROUP; }
14246 			| EXCLUDE TIES			{ $$ = FRAMEOPTION_EXCLUDE_TIES; }
14247 			| EXCLUDE NO OTHERS		{ $$ = 0; }
14248 			| /*EMPTY*/				{ $$ = 0; }
14249 		;
14250 
14251 
14252 /*
14253  * Supporting nonterminals for expressions.
14254  */
14255 
14256 /* Explicit row production.
14257  *
14258  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
14259  * without conflicting with the parenthesized a_expr production.  Without the
14260  * ROW keyword, there must be more than one a_expr inside the parens.
14261  */
14262 row:		ROW '(' expr_list ')'					{ $$ = $3; }
14263 			| ROW '(' ')'							{ $$ = NIL; }
14264 			| '(' expr_list ',' a_expr ')'			{ $$ = lappend($2, $4); }
14265 		;
14266 
14267 explicit_row:	ROW '(' expr_list ')'				{ $$ = $3; }
14268 			| ROW '(' ')'							{ $$ = NIL; }
14269 		;
14270 
14271 implicit_row:	'(' expr_list ',' a_expr ')'		{ $$ = lappend($2, $4); }
14272 		;
14273 
14274 sub_type:	ANY										{ $$ = ANY_SUBLINK; }
14275 			| SOME									{ $$ = ANY_SUBLINK; }
14276 			| ALL									{ $$ = ALL_SUBLINK; }
14277 		;
14278 
14279 all_Op:		Op										{ $$ = $1; }
14280 			| MathOp								{ $$ = $1; }
14281 		;
14282 
14283 MathOp:		 '+'									{ $$ = "+"; }
14284 			| '-'									{ $$ = "-"; }
14285 			| '*'									{ $$ = "*"; }
14286 			| '/'									{ $$ = "/"; }
14287 			| '%'									{ $$ = "%"; }
14288 			| '^'									{ $$ = "^"; }
14289 			| '<'									{ $$ = "<"; }
14290 			| '>'									{ $$ = ">"; }
14291 			| '='									{ $$ = "="; }
14292 			| LESS_EQUALS							{ $$ = "<="; }
14293 			| GREATER_EQUALS						{ $$ = ">="; }
14294 			| NOT_EQUALS							{ $$ = "<>"; }
14295 		;
14296 
14297 qual_Op:	Op
14298 					{ $$ = list_make1(makeString($1)); }
14299 			| OPERATOR '(' any_operator ')'
14300 					{ $$ = $3; }
14301 		;
14302 
14303 qual_all_Op:
14304 			all_Op
14305 					{ $$ = list_make1(makeString($1)); }
14306 			| OPERATOR '(' any_operator ')'
14307 					{ $$ = $3; }
14308 		;
14309 
14310 subquery_Op:
14311 			all_Op
14312 					{ $$ = list_make1(makeString($1)); }
14313 			| OPERATOR '(' any_operator ')'
14314 					{ $$ = $3; }
14315 			| LIKE
14316 					{ $$ = list_make1(makeString("~~")); }
14317 			| NOT_LA LIKE
14318 					{ $$ = list_make1(makeString("!~~")); }
14319 			| ILIKE
14320 					{ $$ = list_make1(makeString("~~*")); }
14321 			| NOT_LA ILIKE
14322 					{ $$ = list_make1(makeString("!~~*")); }
14323 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
14324  * the regular expression is preprocessed by a function (similar_escape),
14325  * and the ~ operator for posix regular expressions is used.
14326  *        x SIMILAR TO y     ->    x ~ similar_escape(y)
14327  * this transformation is made on the fly by the parser upwards.
14328  * however the SubLink structure which handles any/some/all stuff
14329  * is not ready for such a thing.
14330  */
14331 			;
14332 
14333 expr_list:	a_expr
14334 				{
14335 					$$ = list_make1($1);
14336 				}
14337 			| expr_list ',' a_expr
14338 				{
14339 					$$ = lappend($1, $3);
14340 				}
14341 		;
14342 
14343 /* function arguments can have names */
14344 func_arg_list:  func_arg_expr
14345 				{
14346 					$$ = list_make1($1);
14347 				}
14348 			| func_arg_list ',' func_arg_expr
14349 				{
14350 					$$ = lappend($1, $3);
14351 				}
14352 		;
14353 
14354 func_arg_expr:  a_expr
14355 				{
14356 					$$ = $1;
14357 				}
14358 			| param_name COLON_EQUALS a_expr
14359 				{
14360 					NamedArgExpr *na = makeNode(NamedArgExpr);
14361 					na->name = $1;
14362 					na->arg = (Expr *) $3;
14363 					na->argnumber = -1;		/* until determined */
14364 					na->location = @1;
14365 					$$ = (Node *) na;
14366 				}
14367 			| param_name EQUALS_GREATER a_expr
14368 				{
14369 					NamedArgExpr *na = makeNode(NamedArgExpr);
14370 					na->name = $1;
14371 					na->arg = (Expr *) $3;
14372 					na->argnumber = -1;		/* until determined */
14373 					na->location = @1;
14374 					$$ = (Node *) na;
14375 				}
14376 		;
14377 
14378 type_list:	Typename								{ $$ = list_make1($1); }
14379 			| type_list ',' Typename				{ $$ = lappend($1, $3); }
14380 		;
14381 
14382 array_expr: '[' expr_list ']'
14383 				{
14384 					$$ = makeAArrayExpr($2, @1);
14385 				}
14386 			| '[' array_expr_list ']'
14387 				{
14388 					$$ = makeAArrayExpr($2, @1);
14389 				}
14390 			| '[' ']'
14391 				{
14392 					$$ = makeAArrayExpr(NIL, @1);
14393 				}
14394 		;
14395 
14396 array_expr_list: array_expr							{ $$ = list_make1($1); }
14397 			| array_expr_list ',' array_expr		{ $$ = lappend($1, $3); }
14398 		;
14399 
14400 
14401 extract_list:
14402 			extract_arg FROM a_expr
14403 				{
14404 					$$ = list_make2(makeStringConst($1, @1), $3);
14405 				}
14406 			| /*EMPTY*/								{ $$ = NIL; }
14407 		;
14408 
14409 /* Allow delimited string Sconst in extract_arg as an SQL extension.
14410  * - thomas 2001-04-12
14411  */
14412 extract_arg:
14413 			IDENT									{ $$ = $1; }
14414 			| YEAR_P								{ $$ = "year"; }
14415 			| MONTH_P								{ $$ = "month"; }
14416 			| DAY_P									{ $$ = "day"; }
14417 			| HOUR_P								{ $$ = "hour"; }
14418 			| MINUTE_P								{ $$ = "minute"; }
14419 			| SECOND_P								{ $$ = "second"; }
14420 			| Sconst								{ $$ = $1; }
14421 		;
14422 
14423 /* OVERLAY() arguments
14424  * SQL99 defines the OVERLAY() function:
14425  * o overlay(text placing text from int for int)
14426  * o overlay(text placing text from int)
14427  * and similarly for binary strings
14428  */
14429 overlay_list:
14430 			a_expr overlay_placing substr_from substr_for
14431 				{
14432 					$$ = list_make4($1, $2, $3, $4);
14433 				}
14434 			| a_expr overlay_placing substr_from
14435 				{
14436 					$$ = list_make3($1, $2, $3);
14437 				}
14438 		;
14439 
14440 overlay_placing:
14441 			PLACING a_expr
14442 				{ $$ = $2; }
14443 		;
14444 
14445 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
14446 
14447 position_list:
14448 			b_expr IN_P b_expr						{ $$ = list_make2($3, $1); }
14449 			| /*EMPTY*/								{ $$ = NIL; }
14450 		;
14451 
14452 /* SUBSTRING() arguments
14453  * SQL9x defines a specific syntax for arguments to SUBSTRING():
14454  * o substring(text from int for int)
14455  * o substring(text from int) get entire string from starting point "int"
14456  * o substring(text for int) get first "int" characters of string
14457  * o substring(text from pattern) get entire string matching pattern
14458  * o substring(text from pattern for escape) same with specified escape char
14459  * We also want to support generic substring functions which accept
14460  * the usual generic list of arguments. So we will accept both styles
14461  * here, and convert the SQL9x style to the generic list for further
14462  * processing. - thomas 2000-11-28
14463  */
14464 substr_list:
14465 			a_expr substr_from substr_for
14466 				{
14467 					$$ = list_make3($1, $2, $3);
14468 				}
14469 			| a_expr substr_for substr_from
14470 				{
14471 					/* not legal per SQL99, but might as well allow it */
14472 					$$ = list_make3($1, $3, $2);
14473 				}
14474 			| a_expr substr_from
14475 				{
14476 					$$ = list_make2($1, $2);
14477 				}
14478 			| a_expr substr_for
14479 				{
14480 					/*
14481 					 * Since there are no cases where this syntax allows
14482 					 * a textual FOR value, we forcibly cast the argument
14483 					 * to int4.  The possible matches in pg_proc are
14484 					 * substring(text,int4) and substring(text,text),
14485 					 * and we don't want the parser to choose the latter,
14486 					 * which it is likely to do if the second argument
14487 					 * is unknown or doesn't have an implicit cast to int4.
14488 					 */
14489 					$$ = list_make3($1, makeIntConst(1, -1),
14490 									makeTypeCast($2,
14491 												 SystemTypeName("int4"), -1));
14492 				}
14493 			| expr_list
14494 				{
14495 					$$ = $1;
14496 				}
14497 			| /*EMPTY*/
14498 				{ $$ = NIL; }
14499 		;
14500 
14501 substr_from:
14502 			FROM a_expr								{ $$ = $2; }
14503 		;
14504 
14505 substr_for: FOR a_expr								{ $$ = $2; }
14506 		;
14507 
14508 trim_list:	a_expr FROM expr_list					{ $$ = lappend($3, $1); }
14509 			| FROM expr_list						{ $$ = $2; }
14510 			| expr_list								{ $$ = $1; }
14511 		;
14512 
14513 in_expr:	select_with_parens
14514 				{
14515 					SubLink *n = makeNode(SubLink);
14516 					n->subselect = $1;
14517 					/* other fields will be filled later */
14518 					$$ = (Node *)n;
14519 				}
14520 			| '(' expr_list ')'						{ $$ = (Node *)$2; }
14521 		;
14522 
14523 /*
14524  * Define SQL-style CASE clause.
14525  * - Full specification
14526  *	CASE WHEN a = b THEN c ... ELSE d END
14527  * - Implicit argument
14528  *	CASE a WHEN b THEN c ... ELSE d END
14529  */
14530 case_expr:	CASE case_arg when_clause_list case_default END_P
14531 				{
14532 					CaseExpr *c = makeNode(CaseExpr);
14533 					c->casetype = InvalidOid; /* not analyzed yet */
14534 					c->arg = (Expr *) $2;
14535 					c->args = $3;
14536 					c->defresult = (Expr *) $4;
14537 					c->location = @1;
14538 					$$ = (Node *)c;
14539 				}
14540 		;
14541 
14542 when_clause_list:
14543 			/* There must be at least one */
14544 			when_clause								{ $$ = list_make1($1); }
14545 			| when_clause_list when_clause			{ $$ = lappend($1, $2); }
14546 		;
14547 
14548 when_clause:
14549 			WHEN a_expr THEN a_expr
14550 				{
14551 					CaseWhen *w = makeNode(CaseWhen);
14552 					w->expr = (Expr *) $2;
14553 					w->result = (Expr *) $4;
14554 					w->location = @1;
14555 					$$ = (Node *)w;
14556 				}
14557 		;
14558 
14559 case_default:
14560 			ELSE a_expr								{ $$ = $2; }
14561 			| /*EMPTY*/								{ $$ = NULL; }
14562 		;
14563 
14564 case_arg:	a_expr									{ $$ = $1; }
14565 			| /*EMPTY*/								{ $$ = NULL; }
14566 		;
14567 
14568 columnref:	ColId
14569 				{
14570 					$$ = makeColumnRef($1, NIL, @1, yyscanner);
14571 				}
14572 			| ColId indirection
14573 				{
14574 					$$ = makeColumnRef($1, $2, @1, yyscanner);
14575 				}
14576 		;
14577 
14578 indirection_el:
14579 			'.' attr_name
14580 				{
14581 					$$ = (Node *) makeString($2);
14582 				}
14583 			| '.' '*'
14584 				{
14585 					$$ = (Node *) makeNode(A_Star);
14586 				}
14587 			| '[' a_expr ']'
14588 				{
14589 					A_Indices *ai = makeNode(A_Indices);
14590 					ai->is_slice = false;
14591 					ai->lidx = NULL;
14592 					ai->uidx = $2;
14593 					$$ = (Node *) ai;
14594 				}
14595 			| '[' opt_slice_bound ':' opt_slice_bound ']'
14596 				{
14597 					A_Indices *ai = makeNode(A_Indices);
14598 					ai->is_slice = true;
14599 					ai->lidx = $2;
14600 					ai->uidx = $4;
14601 					$$ = (Node *) ai;
14602 				}
14603 		;
14604 
14605 opt_slice_bound:
14606 			a_expr									{ $$ = $1; }
14607 			| /*EMPTY*/								{ $$ = NULL; }
14608 		;
14609 
14610 indirection:
14611 			indirection_el							{ $$ = list_make1($1); }
14612 			| indirection indirection_el			{ $$ = lappend($1, $2); }
14613 		;
14614 
14615 opt_indirection:
14616 			/*EMPTY*/								{ $$ = NIL; }
14617 			| opt_indirection indirection_el		{ $$ = lappend($1, $2); }
14618 		;
14619 
14620 opt_asymmetric: ASYMMETRIC
14621 			| /*EMPTY*/
14622 		;
14623 
14624 
14625 /*****************************************************************************
14626  *
14627  *	target list for SELECT
14628  *
14629  *****************************************************************************/
14630 
14631 opt_target_list: target_list						{ $$ = $1; }
14632 			| /* EMPTY */							{ $$ = NIL; }
14633 		;
14634 
14635 target_list:
14636 			target_el								{ $$ = list_make1($1); }
14637 			| target_list ',' target_el				{ $$ = lappend($1, $3); }
14638 		;
14639 
14640 target_el:	a_expr AS ColLabel
14641 				{
14642 					$$ = makeNode(ResTarget);
14643 					$$->name = $3;
14644 					$$->indirection = NIL;
14645 					$$->val = (Node *)$1;
14646 					$$->location = @1;
14647 				}
14648 			/*
14649 			 * We support omitting AS only for column labels that aren't
14650 			 * any known keyword.  There is an ambiguity against postfix
14651 			 * operators: is "a ! b" an infix expression, or a postfix
14652 			 * expression and a column label?  We prefer to resolve this
14653 			 * as an infix expression, which we accomplish by assigning
14654 			 * IDENT a precedence higher than POSTFIXOP.
14655 			 */
14656 			| a_expr IDENT
14657 				{
14658 					$$ = makeNode(ResTarget);
14659 					$$->name = $2;
14660 					$$->indirection = NIL;
14661 					$$->val = (Node *)$1;
14662 					$$->location = @1;
14663 				}
14664 			| a_expr
14665 				{
14666 					$$ = makeNode(ResTarget);
14667 					$$->name = NULL;
14668 					$$->indirection = NIL;
14669 					$$->val = (Node *)$1;
14670 					$$->location = @1;
14671 				}
14672 			| '*'
14673 				{
14674 					ColumnRef *n = makeNode(ColumnRef);
14675 					n->fields = list_make1(makeNode(A_Star));
14676 					n->location = @1;
14677 
14678 					$$ = makeNode(ResTarget);
14679 					$$->name = NULL;
14680 					$$->indirection = NIL;
14681 					$$->val = (Node *)n;
14682 					$$->location = @1;
14683 				}
14684 		;
14685 
14686 
14687 /*****************************************************************************
14688  *
14689  *	Names and constants
14690  *
14691  *****************************************************************************/
14692 
14693 qualified_name_list:
14694 			qualified_name							{ $$ = list_make1($1); }
14695 			| qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
14696 		;
14697 
14698 /*
14699  * The production for a qualified relation name has to exactly match the
14700  * production for a qualified func_name, because in a FROM clause we cannot
14701  * tell which we are parsing until we see what comes after it ('(' for a
14702  * func_name, something else for a relation). Therefore we allow 'indirection'
14703  * which may contain subscripts, and reject that case in the C code.
14704  */
14705 qualified_name:
14706 			ColId
14707 				{
14708 					$$ = makeRangeVar(NULL, $1, @1);
14709 				}
14710 			| ColId indirection
14711 				{
14712 					check_qualified_name($2, yyscanner);
14713 					$$ = makeRangeVar(NULL, NULL, @1);
14714 					switch (list_length($2))
14715 					{
14716 						case 1:
14717 							$$->catalogname = NULL;
14718 							$$->schemaname = $1;
14719 							$$->relname = strVal(linitial($2));
14720 							break;
14721 						case 2:
14722 							$$->catalogname = $1;
14723 							$$->schemaname = strVal(linitial($2));
14724 							$$->relname = strVal(lsecond($2));
14725 							break;
14726 						default:
14727 							ereport(ERROR,
14728 									(errcode(ERRCODE_SYNTAX_ERROR),
14729 									 errmsg("improper qualified name (too many dotted names): %s",
14730 											NameListToString(lcons(makeString($1), $2))),
14731 									 parser_errposition(@1)));
14732 							break;
14733 					}
14734 				}
14735 		;
14736 
14737 name_list:	name
14738 					{ $$ = list_make1(makeString($1)); }
14739 			| name_list ',' name
14740 					{ $$ = lappend($1, makeString($3)); }
14741 		;
14742 
14743 
14744 name:		ColId									{ $$ = $1; };
14745 
14746 database_name:
14747 			ColId									{ $$ = $1; };
14748 
14749 access_method:
14750 			ColId									{ $$ = $1; };
14751 
14752 attr_name:	ColLabel								{ $$ = $1; };
14753 
14754 index_name: ColId									{ $$ = $1; };
14755 
14756 file_name:	Sconst									{ $$ = $1; };
14757 
14758 /*
14759  * The production for a qualified func_name has to exactly match the
14760  * production for a qualified columnref, because we cannot tell which we
14761  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
14762  * anything else for a columnref).  Therefore we allow 'indirection' which
14763  * may contain subscripts, and reject that case in the C code.  (If we
14764  * ever implement SQL99-like methods, such syntax may actually become legal!)
14765  */
14766 func_name:	type_function_name
14767 					{ $$ = list_make1(makeString($1)); }
14768 			| ColId indirection
14769 					{
14770 						$$ = check_func_name(lcons(makeString($1), $2),
14771 											 yyscanner);
14772 					}
14773 		;
14774 
14775 
14776 /*
14777  * Constants
14778  */
14779 AexprConst: Iconst
14780 				{
14781 					$$ = makeIntConst($1, @1);
14782 				}
14783 			| FCONST
14784 				{
14785 					$$ = makeFloatConst($1, @1);
14786 				}
14787 			| Sconst
14788 				{
14789 					$$ = makeStringConst($1, @1);
14790 				}
14791 			| BCONST
14792 				{
14793 					$$ = makeBitStringConst($1, @1);
14794 				}
14795 			| XCONST
14796 				{
14797 					/* This is a bit constant per SQL99:
14798 					 * Without Feature F511, "BIT data type",
14799 					 * a <general literal> shall not be a
14800 					 * <bit string literal> or a <hex string literal>.
14801 					 */
14802 					$$ = makeBitStringConst($1, @1);
14803 				}
14804 			| func_name Sconst
14805 				{
14806 					/* generic type 'literal' syntax */
14807 					TypeName *t = makeTypeNameFromNameList($1);
14808 					t->location = @1;
14809 					$$ = makeStringConstCast($2, @2, t);
14810 				}
14811 			| func_name '(' func_arg_list opt_sort_clause ')' Sconst
14812 				{
14813 					/* generic syntax with a type modifier */
14814 					TypeName *t = makeTypeNameFromNameList($1);
14815 					ListCell *lc;
14816 
14817 					/*
14818 					 * We must use func_arg_list and opt_sort_clause in the
14819 					 * production to avoid reduce/reduce conflicts, but we
14820 					 * don't actually wish to allow NamedArgExpr in this
14821 					 * context, nor ORDER BY.
14822 					 */
foreach(lc,$3)14823 					foreach(lc, $3)
14824 					{
14825 						NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
14826 
14827 						if (IsA(arg, NamedArgExpr))
14828 							ereport(ERROR,
14829 									(errcode(ERRCODE_SYNTAX_ERROR),
14830 									 errmsg("type modifier cannot have parameter name"),
14831 									 parser_errposition(arg->location)));
14832 					}
14833 					if ($4 != NIL)
14834 							ereport(ERROR,
14835 									(errcode(ERRCODE_SYNTAX_ERROR),
14836 									 errmsg("type modifier cannot have ORDER BY"),
14837 									 parser_errposition(@4)));
14838 
14839 					t->typmods = $3;
14840 					t->location = @1;
14841 					$$ = makeStringConstCast($6, @6, t);
14842 				}
14843 			| ConstTypename Sconst
14844 				{
14845 					$$ = makeStringConstCast($2, @2, $1);
14846 				}
14847 			| ConstInterval Sconst opt_interval
14848 				{
14849 					TypeName *t = $1;
14850 					t->typmods = $3;
14851 					$$ = makeStringConstCast($2, @2, t);
14852 				}
14853 			| ConstInterval '(' Iconst ')' Sconst
14854 				{
14855 					TypeName *t = $1;
14856 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
14857 											makeIntConst($3, @3));
14858 					$$ = makeStringConstCast($5, @5, t);
14859 				}
14860 			| TRUE_P
14861 				{
14862 					$$ = makeBoolAConst(true, @1);
14863 				}
14864 			| FALSE_P
14865 				{
14866 					$$ = makeBoolAConst(false, @1);
14867 				}
14868 			| NULL_P
14869 				{
14870 					$$ = makeNullAConst(@1);
14871 				}
14872 		;
14873 
14874 Iconst:		ICONST									{ $$ = $1; };
14875 Sconst:		SCONST									{ $$ = $1; };
14876 
14877 SignedIconst: Iconst								{ $$ = $1; }
14878 			| '+' Iconst							{ $$ = + $2; }
14879 			| '-' Iconst							{ $$ = - $2; }
14880 		;
14881 
14882 /* Role specifications */
14883 RoleId:		RoleSpec
14884 				{
14885 					RoleSpec *spc = (RoleSpec *) $1;
14886 					switch (spc->roletype)
14887 					{
14888 						case ROLESPEC_CSTRING:
14889 							$$ = spc->rolename;
14890 							break;
14891 						case ROLESPEC_PUBLIC:
14892 							ereport(ERROR,
14893 									(errcode(ERRCODE_RESERVED_NAME),
14894 									 errmsg("role name \"%s\" is reserved",
14895 											"public"),
14896 									 parser_errposition(@1)));
14897 							break;
14898 						case ROLESPEC_SESSION_USER:
14899 							ereport(ERROR,
14900 									(errcode(ERRCODE_RESERVED_NAME),
14901 									 errmsg("%s cannot be used as a role name here",
14902 											"SESSION_USER"),
14903 									 parser_errposition(@1)));
14904 							break;
14905 						case ROLESPEC_CURRENT_USER:
14906 							ereport(ERROR,
14907 									(errcode(ERRCODE_RESERVED_NAME),
14908 									 errmsg("%s cannot be used as a role name here",
14909 											"CURRENT_USER"),
14910 									 parser_errposition(@1)));
14911 							break;
14912 					}
14913 				}
14914 			;
14915 
14916 RoleSpec:	NonReservedWord
14917 					{
14918 						/*
14919 						 * "public" and "none" are not keywords, but they must
14920 						 * be treated specially here.
14921 						 */
14922 						RoleSpec *n;
14923 						if (strcmp($1, "public") == 0)
14924 						{
14925 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_PUBLIC, @1);
14926 							n->roletype = ROLESPEC_PUBLIC;
14927 						}
14928 						else if (strcmp($1, "none") == 0)
14929 						{
14930 							ereport(ERROR,
14931 									(errcode(ERRCODE_RESERVED_NAME),
14932 									 errmsg("role name \"%s\" is reserved",
14933 											"none"),
14934 									 parser_errposition(@1)));
14935 						}
14936 						else
14937 						{
14938 							n = makeRoleSpec(ROLESPEC_CSTRING, @1);
14939 							n->rolename = pstrdup($1);
14940 						}
14941 						$$ = n;
14942 					}
14943 			| CURRENT_USER
14944 					{
14945 						$$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1);
14946 					}
14947 			| SESSION_USER
14948 					{
14949 						$$ = makeRoleSpec(ROLESPEC_SESSION_USER, @1);
14950 					}
14951 		;
14952 
14953 role_list:	RoleSpec
14954 					{ $$ = list_make1($1); }
14955 			| role_list ',' RoleSpec
14956 					{ $$ = lappend($1, $3); }
14957 		;
14958 
14959 /*
14960  * Name classification hierarchy.
14961  *
14962  * IDENT is the lexeme returned by the lexer for identifiers that match
14963  * no known keyword.  In most cases, we can accept certain keywords as
14964  * names, not only IDENTs.	We prefer to accept as many such keywords
14965  * as possible to minimize the impact of "reserved words" on programmers.
14966  * So, we divide names into several possible classes.  The classification
14967  * is chosen in part to make keywords acceptable as names wherever possible.
14968  */
14969 
14970 /* Column identifier --- names that can be column, table, etc names.
14971  */
14972 ColId:		IDENT									{ $$ = $1; }
14973 			| unreserved_keyword					{ $$ = pstrdup($1); }
14974 			| col_name_keyword						{ $$ = pstrdup($1); }
14975 		;
14976 
14977 /* Type/function identifier --- names that can be type or function names.
14978  */
14979 type_function_name:	IDENT							{ $$ = $1; }
14980 			| unreserved_keyword					{ $$ = pstrdup($1); }
14981 			| type_func_name_keyword				{ $$ = pstrdup($1); }
14982 		;
14983 
14984 /* Any not-fully-reserved word --- these names can be, eg, role names.
14985  */
14986 NonReservedWord:	IDENT							{ $$ = $1; }
14987 			| unreserved_keyword					{ $$ = pstrdup($1); }
14988 			| col_name_keyword						{ $$ = pstrdup($1); }
14989 			| type_func_name_keyword				{ $$ = pstrdup($1); }
14990 		;
14991 
14992 /* Column label --- allowed labels in "AS" clauses.
14993  * This presently includes *all* Postgres keywords.
14994  */
14995 ColLabel:	IDENT									{ $$ = $1; }
14996 			| unreserved_keyword					{ $$ = pstrdup($1); }
14997 			| col_name_keyword						{ $$ = pstrdup($1); }
14998 			| type_func_name_keyword				{ $$ = pstrdup($1); }
14999 			| reserved_keyword						{ $$ = pstrdup($1); }
15000 		;
15001 
15002 
15003 /*
15004  * Keyword category lists.  Generally, every keyword present in
15005  * the Postgres grammar should appear in exactly one of these lists.
15006  *
15007  * Put a new keyword into the first list that it can go into without causing
15008  * shift or reduce conflicts.  The earlier lists define "less reserved"
15009  * categories of keywords.
15010  *
15011  * Make sure that each keyword's category in kwlist.h matches where
15012  * it is listed here.  (Someday we may be able to generate these lists and
15013  * kwlist.h's table from a common master list.)
15014  */
15015 
15016 /* "Unreserved" keywords --- available for use as any kind of name.
15017  */
15018 unreserved_keyword:
15019 			  ABORT_P
15020 			| ABSOLUTE_P
15021 			| ACCESS
15022 			| ACTION
15023 			| ADD_P
15024 			| ADMIN
15025 			| AFTER
15026 			| AGGREGATE
15027 			| ALSO
15028 			| ALTER
15029 			| ALWAYS
15030 			| ASSERTION
15031 			| ASSIGNMENT
15032 			| AT
15033 			| ATTACH
15034 			| ATTRIBUTE
15035 			| BACKWARD
15036 			| BEFORE
15037 			| BEGIN_P
15038 			| BY
15039 			| CACHE
15040 			| CALL
15041 			| CALLED
15042 			| CASCADE
15043 			| CASCADED
15044 			| CATALOG_P
15045 			| CHAIN
15046 			| CHARACTERISTICS
15047 			| CHECKPOINT
15048 			| CLASS
15049 			| CLOSE
15050 			| CLUSTER
15051 			| COLUMNS
15052 			| COMMENT
15053 			| COMMENTS
15054 			| COMMIT
15055 			| COMMITTED
15056 			| CONFIGURATION
15057 			| CONFLICT
15058 			| CONNECTION
15059 			| CONSTRAINTS
15060 			| CONTENT_P
15061 			| CONTINUE_P
15062 			| CONVERSION_P
15063 			| COPY
15064 			| COST
15065 			| CSV
15066 			| CUBE
15067 			| CURRENT_P
15068 			| CURSOR
15069 			| CYCLE
15070 			| DATA_P
15071 			| DATABASE
15072 			| DAY_P
15073 			| DEALLOCATE
15074 			| DECLARE
15075 			| DEFAULTS
15076 			| DEFERRED
15077 			| DEFINER
15078 			| DELETE_P
15079 			| DELIMITER
15080 			| DELIMITERS
15081 			| DEPENDS
15082 			| DETACH
15083 			| DICTIONARY
15084 			| DISABLE_P
15085 			| DISCARD
15086 			| DOCUMENT_P
15087 			| DOMAIN_P
15088 			| DOUBLE_P
15089 			| DROP
15090 			| EACH
15091 			| ENABLE_P
15092 			| ENCODING
15093 			| ENCRYPTED
15094 			| ENUM_P
15095 			| ESCAPE
15096 			| EVENT
15097 			| EXCLUDE
15098 			| EXCLUDING
15099 			| EXCLUSIVE
15100 			| EXECUTE
15101 			| EXPLAIN
15102 			| EXTENSION
15103 			| EXTERNAL
15104 			| FAMILY
15105 			| FILTER
15106 			| FIRST_P
15107 			| FOLLOWING
15108 			| FORCE
15109 			| FORWARD
15110 			| FUNCTION
15111 			| FUNCTIONS
15112 			| GENERATED
15113 			| GLOBAL
15114 			| GRANTED
15115 			| GROUPS
15116 			| HANDLER
15117 			| HEADER_P
15118 			| HOLD
15119 			| HOUR_P
15120 			| IDENTITY_P
15121 			| IF_P
15122 			| IMMEDIATE
15123 			| IMMUTABLE
15124 			| IMPLICIT_P
15125 			| IMPORT_P
15126 			| INCLUDE
15127 			| INCLUDING
15128 			| INCREMENT
15129 			| INDEX
15130 			| INDEXES
15131 			| INHERIT
15132 			| INHERITS
15133 			| INLINE_P
15134 			| INPUT_P
15135 			| INSENSITIVE
15136 			| INSERT
15137 			| INSTEAD
15138 			| INVOKER
15139 			| ISOLATION
15140 			| KEY
15141 			| LABEL
15142 			| LANGUAGE
15143 			| LARGE_P
15144 			| LAST_P
15145 			| LEAKPROOF
15146 			| LEVEL
15147 			| LISTEN
15148 			| LOAD
15149 			| LOCAL
15150 			| LOCATION
15151 			| LOCK_P
15152 			| LOCKED
15153 			| LOGGED
15154 			| MAPPING
15155 			| MATCH
15156 			| MATERIALIZED
15157 			| MAXVALUE
15158 			| METHOD
15159 			| MINUTE_P
15160 			| MINVALUE
15161 			| MODE
15162 			| MONTH_P
15163 			| MOVE
15164 			| NAME_P
15165 			| NAMES
15166 			| NEW
15167 			| NEXT
15168 			| NO
15169 			| NOTHING
15170 			| NOTIFY
15171 			| NOWAIT
15172 			| NULLS_P
15173 			| OBJECT_P
15174 			| OF
15175 			| OFF
15176 			| OIDS
15177 			| OLD
15178 			| OPERATOR
15179 			| OPTION
15180 			| OPTIONS
15181 			| ORDINALITY
15182 			| OTHERS
15183 			| OVER
15184 			| OVERRIDING
15185 			| OWNED
15186 			| OWNER
15187 			| PARALLEL
15188 			| PARSER
15189 			| PARTIAL
15190 			| PARTITION
15191 			| PASSING
15192 			| PASSWORD
15193 			| PLANS
15194 			| POLICY
15195 			| PRECEDING
15196 			| PREPARE
15197 			| PREPARED
15198 			| PRESERVE
15199 			| PRIOR
15200 			| PRIVILEGES
15201 			| PROCEDURAL
15202 			| PROCEDURE
15203 			| PROCEDURES
15204 			| PROGRAM
15205 			| PUBLICATION
15206 			| QUOTE
15207 			| RANGE
15208 			| READ
15209 			| REASSIGN
15210 			| RECHECK
15211 			| RECURSIVE
15212 			| REF
15213 			| REFERENCING
15214 			| REFRESH
15215 			| REINDEX
15216 			| RELATIVE_P
15217 			| RELEASE
15218 			| RENAME
15219 			| REPEATABLE
15220 			| REPLACE
15221 			| REPLICA
15222 			| RESET
15223 			| RESTART
15224 			| RESTRICT
15225 			| RETURNS
15226 			| REVOKE
15227 			| ROLE
15228 			| ROLLBACK
15229 			| ROLLUP
15230 			| ROUTINE
15231 			| ROUTINES
15232 			| ROWS
15233 			| RULE
15234 			| SAVEPOINT
15235 			| SCHEMA
15236 			| SCHEMAS
15237 			| SCROLL
15238 			| SEARCH
15239 			| SECOND_P
15240 			| SECURITY
15241 			| SEQUENCE
15242 			| SEQUENCES
15243 			| SERIALIZABLE
15244 			| SERVER
15245 			| SESSION
15246 			| SET
15247 			| SETS
15248 			| SHARE
15249 			| SHOW
15250 			| SIMPLE
15251 			| SKIP
15252 			| SNAPSHOT
15253 			| SQL_P
15254 			| STABLE
15255 			| STANDALONE_P
15256 			| START
15257 			| STATEMENT
15258 			| STATISTICS
15259 			| STDIN
15260 			| STDOUT
15261 			| STORAGE
15262 			| STRICT_P
15263 			| STRIP_P
15264 			| SUBSCRIPTION
15265 			| SYSID
15266 			| SYSTEM_P
15267 			| TABLES
15268 			| TABLESPACE
15269 			| TEMP
15270 			| TEMPLATE
15271 			| TEMPORARY
15272 			| TEXT_P
15273 			| TIES
15274 			| TRANSACTION
15275 			| TRANSFORM
15276 			| TRIGGER
15277 			| TRUNCATE
15278 			| TRUSTED
15279 			| TYPE_P
15280 			| TYPES_P
15281 			| UNBOUNDED
15282 			| UNCOMMITTED
15283 			| UNENCRYPTED
15284 			| UNKNOWN
15285 			| UNLISTEN
15286 			| UNLOGGED
15287 			| UNTIL
15288 			| UPDATE
15289 			| VACUUM
15290 			| VALID
15291 			| VALIDATE
15292 			| VALIDATOR
15293 			| VALUE_P
15294 			| VARYING
15295 			| VERSION_P
15296 			| VIEW
15297 			| VIEWS
15298 			| VOLATILE
15299 			| WHITESPACE_P
15300 			| WITHIN
15301 			| WITHOUT
15302 			| WORK
15303 			| WRAPPER
15304 			| WRITE
15305 			| XML_P
15306 			| YEAR_P
15307 			| YES_P
15308 			| ZONE
15309 		;
15310 
15311 /* Column identifier --- keywords that can be column, table, etc names.
15312  *
15313  * Many of these keywords will in fact be recognized as type or function
15314  * names too; but they have special productions for the purpose, and so
15315  * can't be treated as "generic" type or function names.
15316  *
15317  * The type names appearing here are not usable as function names
15318  * because they can be followed by '(' in typename productions, which
15319  * looks too much like a function call for an LR(1) parser.
15320  */
15321 col_name_keyword:
15322 			  BETWEEN
15323 			| BIGINT
15324 			| BIT
15325 			| BOOLEAN_P
15326 			| CHAR_P
15327 			| CHARACTER
15328 			| COALESCE
15329 			| DEC
15330 			| DECIMAL_P
15331 			| EXISTS
15332 			| EXTRACT
15333 			| FLOAT_P
15334 			| GREATEST
15335 			| GROUPING
15336 			| INOUT
15337 			| INT_P
15338 			| INTEGER
15339 			| INTERVAL
15340 			| LEAST
15341 			| NATIONAL
15342 			| NCHAR
15343 			| NONE
15344 			| NULLIF
15345 			| NUMERIC
15346 			| OUT_P
15347 			| OVERLAY
15348 			| POSITION
15349 			| PRECISION
15350 			| REAL
15351 			| ROW
15352 			| SETOF
15353 			| SMALLINT
15354 			| SUBSTRING
15355 			| TIME
15356 			| TIMESTAMP
15357 			| TREAT
15358 			| TRIM
15359 			| VALUES
15360 			| VARCHAR
15361 			| XMLATTRIBUTES
15362 			| XMLCONCAT
15363 			| XMLELEMENT
15364 			| XMLEXISTS
15365 			| XMLFOREST
15366 			| XMLNAMESPACES
15367 			| XMLPARSE
15368 			| XMLPI
15369 			| XMLROOT
15370 			| XMLSERIALIZE
15371 			| XMLTABLE
15372 		;
15373 
15374 /* Type/function identifier --- keywords that can be type or function names.
15375  *
15376  * Most of these are keywords that are used as operators in expressions;
15377  * in general such keywords can't be column names because they would be
15378  * ambiguous with variables, but they are unambiguous as function identifiers.
15379  *
15380  * Do not include POSITION, SUBSTRING, etc here since they have explicit
15381  * productions in a_expr to support the goofy SQL9x argument syntax.
15382  * - thomas 2000-11-28
15383  */
15384 type_func_name_keyword:
15385 			  AUTHORIZATION
15386 			| BINARY
15387 			| COLLATION
15388 			| CONCURRENTLY
15389 			| CROSS
15390 			| CURRENT_SCHEMA
15391 			| FREEZE
15392 			| FULL
15393 			| ILIKE
15394 			| INNER_P
15395 			| IS
15396 			| ISNULL
15397 			| JOIN
15398 			| LEFT
15399 			| LIKE
15400 			| NATURAL
15401 			| NOTNULL
15402 			| OUTER_P
15403 			| OVERLAPS
15404 			| RIGHT
15405 			| SIMILAR
15406 			| TABLESAMPLE
15407 			| VERBOSE
15408 		;
15409 
15410 /* Reserved keyword --- these keywords are usable only as a ColLabel.
15411  *
15412  * Keywords appear here if they could not be distinguished from variable,
15413  * type, or function names in some contexts.  Don't put things here unless
15414  * forced to.
15415  */
15416 reserved_keyword:
15417 			  ALL
15418 			| ANALYSE
15419 			| ANALYZE
15420 			| AND
15421 			| ANY
15422 			| ARRAY
15423 			| AS
15424 			| ASC
15425 			| ASYMMETRIC
15426 			| BOTH
15427 			| CASE
15428 			| CAST
15429 			| CHECK
15430 			| COLLATE
15431 			| COLUMN
15432 			| CONSTRAINT
15433 			| CREATE
15434 			| CURRENT_CATALOG
15435 			| CURRENT_DATE
15436 			| CURRENT_ROLE
15437 			| CURRENT_TIME
15438 			| CURRENT_TIMESTAMP
15439 			| CURRENT_USER
15440 			| DEFAULT
15441 			| DEFERRABLE
15442 			| DESC
15443 			| DISTINCT
15444 			| DO
15445 			| ELSE
15446 			| END_P
15447 			| EXCEPT
15448 			| FALSE_P
15449 			| FETCH
15450 			| FOR
15451 			| FOREIGN
15452 			| FROM
15453 			| GRANT
15454 			| GROUP_P
15455 			| HAVING
15456 			| IN_P
15457 			| INITIALLY
15458 			| INTERSECT
15459 			| INTO
15460 			| LATERAL_P
15461 			| LEADING
15462 			| LIMIT
15463 			| LOCALTIME
15464 			| LOCALTIMESTAMP
15465 			| NOT
15466 			| NULL_P
15467 			| OFFSET
15468 			| ON
15469 			| ONLY
15470 			| OR
15471 			| ORDER
15472 			| PLACING
15473 			| PRIMARY
15474 			| REFERENCES
15475 			| RETURNING
15476 			| SELECT
15477 			| SESSION_USER
15478 			| SOME
15479 			| SYMMETRIC
15480 			| TABLE
15481 			| THEN
15482 			| TO
15483 			| TRAILING
15484 			| TRUE_P
15485 			| UNION
15486 			| UNIQUE
15487 			| USER
15488 			| USING
15489 			| VARIADIC
15490 			| WHEN
15491 			| WHERE
15492 			| WINDOW
15493 			| WITH
15494 		;
15495 
15496 %%
15497 
15498 /*
15499  * The signature of this function is required by bison.  However, we
15500  * ignore the passed yylloc and instead use the last token position
15501  * available from the scanner.
15502  */
15503 static void
15504 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
15505 {
15506 	parser_yyerror(msg);
15507 }
15508 
15509 static RawStmt *
makeRawStmt(Node * stmt,int stmt_location)15510 makeRawStmt(Node *stmt, int stmt_location)
15511 {
15512 	RawStmt    *rs = makeNode(RawStmt);
15513 
15514 	rs->stmt = stmt;
15515 	rs->stmt_location = stmt_location;
15516 	rs->stmt_len = 0;			/* might get changed later */
15517 	return rs;
15518 }
15519 
15520 /* Adjust a RawStmt to reflect that it doesn't run to the end of the string */
15521 static void
updateRawStmtEnd(RawStmt * rs,int end_location)15522 updateRawStmtEnd(RawStmt *rs, int end_location)
15523 {
15524 	/*
15525 	 * If we already set the length, don't change it.  This is for situations
15526 	 * like "select foo ;; select bar" where the same statement will be last
15527 	 * in the string for more than one semicolon.
15528 	 */
15529 	if (rs->stmt_len > 0)
15530 		return;
15531 
15532 	/* OK, update length of RawStmt */
15533 	rs->stmt_len = end_location - rs->stmt_location;
15534 }
15535 
15536 static Node *
makeColumnRef(char * colname,List * indirection,int location,core_yyscan_t yyscanner)15537 makeColumnRef(char *colname, List *indirection,
15538 			  int location, core_yyscan_t yyscanner)
15539 {
15540 	/*
15541 	 * Generate a ColumnRef node, with an A_Indirection node added if there
15542 	 * is any subscripting in the specified indirection list.  However,
15543 	 * any field selection at the start of the indirection list must be
15544 	 * transposed into the "fields" part of the ColumnRef node.
15545 	 */
15546 	ColumnRef  *c = makeNode(ColumnRef);
15547 	int		nfields = 0;
15548 	ListCell *l;
15549 
15550 	c->location = location;
15551 	foreach(l, indirection)
15552 	{
15553 		if (IsA(lfirst(l), A_Indices))
15554 		{
15555 			A_Indirection *i = makeNode(A_Indirection);
15556 
15557 			if (nfields == 0)
15558 			{
15559 				/* easy case - all indirection goes to A_Indirection */
15560 				c->fields = list_make1(makeString(colname));
15561 				i->indirection = check_indirection(indirection, yyscanner);
15562 			}
15563 			else
15564 			{
15565 				/* got to split the list in two */
15566 				i->indirection = check_indirection(list_copy_tail(indirection,
15567 																  nfields),
15568 												   yyscanner);
15569 				indirection = list_truncate(indirection, nfields);
15570 				c->fields = lcons(makeString(colname), indirection);
15571 			}
15572 			i->arg = (Node *) c;
15573 			return (Node *) i;
15574 		}
15575 		else if (IsA(lfirst(l), A_Star))
15576 		{
15577 			/* We only allow '*' at the end of a ColumnRef */
15578 			if (lnext(l) != NULL)
15579 				parser_yyerror("improper use of \"*\"");
15580 		}
15581 		nfields++;
15582 	}
15583 	/* No subscripting, so all indirection gets added to field list */
15584 	c->fields = lcons(makeString(colname), indirection);
15585 	return (Node *) c;
15586 }
15587 
15588 static Node *
makeTypeCast(Node * arg,TypeName * typename,int location)15589 makeTypeCast(Node *arg, TypeName *typename, int location)
15590 {
15591 	TypeCast *n = makeNode(TypeCast);
15592 	n->arg = arg;
15593 	n->typeName = typename;
15594 	n->location = location;
15595 	return (Node *) n;
15596 }
15597 
15598 static Node *
makeStringConst(char * str,int location)15599 makeStringConst(char *str, int location)
15600 {
15601 	A_Const *n = makeNode(A_Const);
15602 
15603 	n->val.type = T_String;
15604 	n->val.val.str = str;
15605 	n->location = location;
15606 
15607 	return (Node *)n;
15608 }
15609 
15610 static Node *
makeStringConstCast(char * str,int location,TypeName * typename)15611 makeStringConstCast(char *str, int location, TypeName *typename)
15612 {
15613 	Node *s = makeStringConst(str, location);
15614 
15615 	return makeTypeCast(s, typename, -1);
15616 }
15617 
15618 static Node *
makeIntConst(int val,int location)15619 makeIntConst(int val, int location)
15620 {
15621 	A_Const *n = makeNode(A_Const);
15622 
15623 	n->val.type = T_Integer;
15624 	n->val.val.ival = val;
15625 	n->location = location;
15626 
15627 	return (Node *)n;
15628 }
15629 
15630 static Node *
makeFloatConst(char * str,int location)15631 makeFloatConst(char *str, int location)
15632 {
15633 	A_Const *n = makeNode(A_Const);
15634 
15635 	n->val.type = T_Float;
15636 	n->val.val.str = str;
15637 	n->location = location;
15638 
15639 	return (Node *)n;
15640 }
15641 
15642 static Node *
makeBitStringConst(char * str,int location)15643 makeBitStringConst(char *str, int location)
15644 {
15645 	A_Const *n = makeNode(A_Const);
15646 
15647 	n->val.type = T_BitString;
15648 	n->val.val.str = str;
15649 	n->location = location;
15650 
15651 	return (Node *)n;
15652 }
15653 
15654 static Node *
makeNullAConst(int location)15655 makeNullAConst(int location)
15656 {
15657 	A_Const *n = makeNode(A_Const);
15658 
15659 	n->val.type = T_Null;
15660 	n->location = location;
15661 
15662 	return (Node *)n;
15663 }
15664 
15665 static Node *
makeAConst(Value * v,int location)15666 makeAConst(Value *v, int location)
15667 {
15668 	Node *n;
15669 
15670 	switch (v->type)
15671 	{
15672 		case T_Float:
15673 			n = makeFloatConst(v->val.str, location);
15674 			break;
15675 
15676 		case T_Integer:
15677 			n = makeIntConst(v->val.ival, location);
15678 			break;
15679 
15680 		case T_String:
15681 		default:
15682 			n = makeStringConst(v->val.str, location);
15683 			break;
15684 	}
15685 
15686 	return n;
15687 }
15688 
15689 /* makeBoolAConst()
15690  * Create an A_Const string node and put it inside a boolean cast.
15691  */
15692 static Node *
makeBoolAConst(bool state,int location)15693 makeBoolAConst(bool state, int location)
15694 {
15695 	A_Const *n = makeNode(A_Const);
15696 
15697 	n->val.type = T_String;
15698 	n->val.val.str = (state ? "t" : "f");
15699 	n->location = location;
15700 
15701 	return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
15702 }
15703 
15704 /* makeRoleSpec
15705  * Create a RoleSpec with the given type
15706  */
15707 static RoleSpec *
makeRoleSpec(RoleSpecType type,int location)15708 makeRoleSpec(RoleSpecType type, int location)
15709 {
15710 	RoleSpec *spec = makeNode(RoleSpec);
15711 
15712 	spec->roletype = type;
15713 	spec->location = location;
15714 
15715 	return spec;
15716 }
15717 
15718 /* check_qualified_name --- check the result of qualified_name production
15719  *
15720  * It's easiest to let the grammar production for qualified_name allow
15721  * subscripts and '*', which we then must reject here.
15722  */
15723 static void
check_qualified_name(List * names,core_yyscan_t yyscanner)15724 check_qualified_name(List *names, core_yyscan_t yyscanner)
15725 {
15726 	ListCell   *i;
15727 
15728 	foreach(i, names)
15729 	{
15730 		if (!IsA(lfirst(i), String))
15731 			parser_yyerror("syntax error");
15732 	}
15733 }
15734 
15735 /* check_func_name --- check the result of func_name production
15736  *
15737  * It's easiest to let the grammar production for func_name allow subscripts
15738  * and '*', which we then must reject here.
15739  */
15740 static List *
check_func_name(List * names,core_yyscan_t yyscanner)15741 check_func_name(List *names, core_yyscan_t yyscanner)
15742 {
15743 	ListCell   *i;
15744 
15745 	foreach(i, names)
15746 	{
15747 		if (!IsA(lfirst(i), String))
15748 			parser_yyerror("syntax error");
15749 	}
15750 	return names;
15751 }
15752 
15753 /* check_indirection --- check the result of indirection production
15754  *
15755  * We only allow '*' at the end of the list, but it's hard to enforce that
15756  * in the grammar, so do it here.
15757  */
15758 static List *
check_indirection(List * indirection,core_yyscan_t yyscanner)15759 check_indirection(List *indirection, core_yyscan_t yyscanner)
15760 {
15761 	ListCell *l;
15762 
15763 	foreach(l, indirection)
15764 	{
15765 		if (IsA(lfirst(l), A_Star))
15766 		{
15767 			if (lnext(l) != NULL)
15768 				parser_yyerror("improper use of \"*\"");
15769 		}
15770 	}
15771 	return indirection;
15772 }
15773 
15774 /* extractArgTypes()
15775  * Given a list of FunctionParameter nodes, extract a list of just the
15776  * argument types (TypeNames) for input parameters only.  This is what
15777  * is needed to look up an existing function, which is what is wanted by
15778  * the productions that use this call.
15779  */
15780 static List *
extractArgTypes(List * parameters)15781 extractArgTypes(List *parameters)
15782 {
15783 	List	   *result = NIL;
15784 	ListCell   *i;
15785 
15786 	foreach(i, parameters)
15787 	{
15788 		FunctionParameter *p = (FunctionParameter *) lfirst(i);
15789 
15790 		if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
15791 			result = lappend(result, p->argType);
15792 	}
15793 	return result;
15794 }
15795 
15796 /* extractAggrArgTypes()
15797  * As above, but work from the output of the aggr_args production.
15798  */
15799 static List *
extractAggrArgTypes(List * aggrargs)15800 extractAggrArgTypes(List *aggrargs)
15801 {
15802 	Assert(list_length(aggrargs) == 2);
15803 	return extractArgTypes((List *) linitial(aggrargs));
15804 }
15805 
15806 /* makeOrderedSetArgs()
15807  * Build the result of the aggr_args production (which see the comments for).
15808  * This handles only the case where both given lists are nonempty, so that
15809  * we have to deal with multiple VARIADIC arguments.
15810  */
15811 static List *
makeOrderedSetArgs(List * directargs,List * orderedargs,core_yyscan_t yyscanner)15812 makeOrderedSetArgs(List *directargs, List *orderedargs,
15813 				   core_yyscan_t yyscanner)
15814 {
15815 	FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
15816 	Value	   *ndirectargs;
15817 
15818 	/* No restriction unless last direct arg is VARIADIC */
15819 	if (lastd->mode == FUNC_PARAM_VARIADIC)
15820 	{
15821 		FunctionParameter *firsto = (FunctionParameter *) linitial(orderedargs);
15822 
15823 		/*
15824 		 * We ignore the names, though the aggr_arg production allows them;
15825 		 * it doesn't allow default values, so those need not be checked.
15826 		 */
15827 		if (list_length(orderedargs) != 1 ||
15828 			firsto->mode != FUNC_PARAM_VARIADIC ||
15829 			!equal(lastd->argType, firsto->argType))
15830 			ereport(ERROR,
15831 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15832 					 errmsg("an ordered-set aggregate with a VARIADIC direct argument must have one VARIADIC aggregated argument of the same data type"),
15833 					 parser_errposition(exprLocation((Node *) firsto))));
15834 
15835 		/* OK, drop the duplicate VARIADIC argument from the internal form */
15836 		orderedargs = NIL;
15837 	}
15838 
15839 	/* don't merge into the next line, as list_concat changes directargs */
15840 	ndirectargs = makeInteger(list_length(directargs));
15841 
15842 	return list_make2(list_concat(directargs, orderedargs),
15843 					  ndirectargs);
15844 }
15845 
15846 /* insertSelectOptions()
15847  * Insert ORDER BY, etc into an already-constructed SelectStmt.
15848  *
15849  * This routine is just to avoid duplicating code in SelectStmt productions.
15850  */
15851 static void
insertSelectOptions(SelectStmt * stmt,List * sortClause,List * lockingClause,Node * limitOffset,Node * limitCount,WithClause * withClause,core_yyscan_t yyscanner)15852 insertSelectOptions(SelectStmt *stmt,
15853 					List *sortClause, List *lockingClause,
15854 					Node *limitOffset, Node *limitCount,
15855 					WithClause *withClause,
15856 					core_yyscan_t yyscanner)
15857 {
15858 	Assert(IsA(stmt, SelectStmt));
15859 
15860 	/*
15861 	 * Tests here are to reject constructs like
15862 	 *	(SELECT foo ORDER BY bar) ORDER BY baz
15863 	 */
15864 	if (sortClause)
15865 	{
15866 		if (stmt->sortClause)
15867 			ereport(ERROR,
15868 					(errcode(ERRCODE_SYNTAX_ERROR),
15869 					 errmsg("multiple ORDER BY clauses not allowed"),
15870 					 parser_errposition(exprLocation((Node *) sortClause))));
15871 		stmt->sortClause = sortClause;
15872 	}
15873 	/* We can handle multiple locking clauses, though */
15874 	stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
15875 	if (limitOffset)
15876 	{
15877 		if (stmt->limitOffset)
15878 			ereport(ERROR,
15879 					(errcode(ERRCODE_SYNTAX_ERROR),
15880 					 errmsg("multiple OFFSET clauses not allowed"),
15881 					 parser_errposition(exprLocation(limitOffset))));
15882 		stmt->limitOffset = limitOffset;
15883 	}
15884 	if (limitCount)
15885 	{
15886 		if (stmt->limitCount)
15887 			ereport(ERROR,
15888 					(errcode(ERRCODE_SYNTAX_ERROR),
15889 					 errmsg("multiple LIMIT clauses not allowed"),
15890 					 parser_errposition(exprLocation(limitCount))));
15891 		stmt->limitCount = limitCount;
15892 	}
15893 	if (withClause)
15894 	{
15895 		if (stmt->withClause)
15896 			ereport(ERROR,
15897 					(errcode(ERRCODE_SYNTAX_ERROR),
15898 					 errmsg("multiple WITH clauses not allowed"),
15899 					 parser_errposition(exprLocation((Node *) withClause))));
15900 		stmt->withClause = withClause;
15901 	}
15902 }
15903 
15904 static Node *
makeSetOp(SetOperation op,bool all,Node * larg,Node * rarg)15905 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
15906 {
15907 	SelectStmt *n = makeNode(SelectStmt);
15908 
15909 	n->op = op;
15910 	n->all = all;
15911 	n->larg = (SelectStmt *) larg;
15912 	n->rarg = (SelectStmt *) rarg;
15913 	return (Node *) n;
15914 }
15915 
15916 /* SystemFuncName()
15917  * Build a properly-qualified reference to a built-in function.
15918  */
15919 List *
SystemFuncName(char * name)15920 SystemFuncName(char *name)
15921 {
15922 	return list_make2(makeString("pg_catalog"), makeString(name));
15923 }
15924 
15925 /* SystemTypeName()
15926  * Build a properly-qualified reference to a built-in type.
15927  *
15928  * typmod is defaulted, but may be changed afterwards by caller.
15929  * Likewise for the location.
15930  */
15931 TypeName *
SystemTypeName(char * name)15932 SystemTypeName(char *name)
15933 {
15934 	return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
15935 											   makeString(name)));
15936 }
15937 
15938 /* doNegate()
15939  * Handle negation of a numeric constant.
15940  *
15941  * Formerly, we did this here because the optimizer couldn't cope with
15942  * indexquals that looked like "var = -4" --- it wants "var = const"
15943  * and a unary minus operator applied to a constant didn't qualify.
15944  * As of Postgres 7.0, that problem doesn't exist anymore because there
15945  * is a constant-subexpression simplifier in the optimizer.  However,
15946  * there's still a good reason for doing this here, which is that we can
15947  * postpone committing to a particular internal representation for simple
15948  * negative constants.	It's better to leave "-123.456" in string form
15949  * until we know what the desired type is.
15950  */
15951 static Node *
doNegate(Node * n,int location)15952 doNegate(Node *n, int location)
15953 {
15954 	if (IsA(n, A_Const))
15955 	{
15956 		A_Const *con = (A_Const *)n;
15957 
15958 		/* report the constant's location as that of the '-' sign */
15959 		con->location = location;
15960 
15961 		if (con->val.type == T_Integer)
15962 		{
15963 			con->val.val.ival = -con->val.val.ival;
15964 			return n;
15965 		}
15966 		if (con->val.type == T_Float)
15967 		{
15968 			doNegateFloat(&con->val);
15969 			return n;
15970 		}
15971 	}
15972 
15973 	return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
15974 }
15975 
15976 static void
doNegateFloat(Value * v)15977 doNegateFloat(Value *v)
15978 {
15979 	char   *oldval = v->val.str;
15980 
15981 	Assert(IsA(v, Float));
15982 	if (*oldval == '+')
15983 		oldval++;
15984 	if (*oldval == '-')
15985 		v->val.str = oldval+1;	/* just strip the '-' */
15986 	else
15987 		v->val.str = psprintf("-%s", oldval);
15988 }
15989 
15990 static Node *
makeAndExpr(Node * lexpr,Node * rexpr,int location)15991 makeAndExpr(Node *lexpr, Node *rexpr, int location)
15992 {
15993 	Node	   *lexp = lexpr;
15994 
15995 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
15996 	while (IsA(lexp, A_Expr) &&
15997 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
15998 		lexp = ((A_Expr *) lexp)->lexpr;
15999 	/* Flatten "a AND b AND c ..." to a single BoolExpr on sight */
16000 	if (IsA(lexp, BoolExpr))
16001 	{
16002 		BoolExpr *blexpr = (BoolExpr *) lexp;
16003 
16004 		if (blexpr->boolop == AND_EXPR)
16005 		{
16006 			blexpr->args = lappend(blexpr->args, rexpr);
16007 			return (Node *) blexpr;
16008 		}
16009 	}
16010 	return (Node *) makeBoolExpr(AND_EXPR, list_make2(lexpr, rexpr), location);
16011 }
16012 
16013 static Node *
makeOrExpr(Node * lexpr,Node * rexpr,int location)16014 makeOrExpr(Node *lexpr, Node *rexpr, int location)
16015 {
16016 	Node	   *lexp = lexpr;
16017 
16018 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
16019 	while (IsA(lexp, A_Expr) &&
16020 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
16021 		lexp = ((A_Expr *) lexp)->lexpr;
16022 	/* Flatten "a OR b OR c ..." to a single BoolExpr on sight */
16023 	if (IsA(lexp, BoolExpr))
16024 	{
16025 		BoolExpr *blexpr = (BoolExpr *) lexp;
16026 
16027 		if (blexpr->boolop == OR_EXPR)
16028 		{
16029 			blexpr->args = lappend(blexpr->args, rexpr);
16030 			return (Node *) blexpr;
16031 		}
16032 	}
16033 	return (Node *) makeBoolExpr(OR_EXPR, list_make2(lexpr, rexpr), location);
16034 }
16035 
16036 static Node *
makeNotExpr(Node * expr,int location)16037 makeNotExpr(Node *expr, int location)
16038 {
16039 	return (Node *) makeBoolExpr(NOT_EXPR, list_make1(expr), location);
16040 }
16041 
16042 static Node *
makeAArrayExpr(List * elements,int location)16043 makeAArrayExpr(List *elements, int location)
16044 {
16045 	A_ArrayExpr *n = makeNode(A_ArrayExpr);
16046 
16047 	n->elements = elements;
16048 	n->location = location;
16049 	return (Node *) n;
16050 }
16051 
16052 static Node *
makeSQLValueFunction(SQLValueFunctionOp op,int32 typmod,int location)16053 makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location)
16054 {
16055 	SQLValueFunction *svf = makeNode(SQLValueFunction);
16056 
16057 	svf->op = op;
16058 	/* svf->type will be filled during parse analysis */
16059 	svf->typmod = typmod;
16060 	svf->location = location;
16061 	return (Node *) svf;
16062 }
16063 
16064 static Node *
makeXmlExpr(XmlExprOp op,char * name,List * named_args,List * args,int location)16065 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
16066 			int location)
16067 {
16068 	XmlExpr		*x = makeNode(XmlExpr);
16069 
16070 	x->op = op;
16071 	x->name = name;
16072 	/*
16073 	 * named_args is a list of ResTarget; it'll be split apart into separate
16074 	 * expression and name lists in transformXmlExpr().
16075 	 */
16076 	x->named_args = named_args;
16077 	x->arg_names = NIL;
16078 	x->args = args;
16079 	/* xmloption, if relevant, must be filled in by caller */
16080 	/* type and typmod will be filled in during parse analysis */
16081 	x->type = InvalidOid;			/* marks the node as not analyzed */
16082 	x->location = location;
16083 	return (Node *) x;
16084 }
16085 
16086 /*
16087  * Merge the input and output parameters of a table function.
16088  */
16089 static List *
mergeTableFuncParameters(List * func_args,List * columns)16090 mergeTableFuncParameters(List *func_args, List *columns)
16091 {
16092 	ListCell   *lc;
16093 
16094 	/* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
16095 	foreach(lc, func_args)
16096 	{
16097 		FunctionParameter *p = (FunctionParameter *) lfirst(lc);
16098 
16099 		if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
16100 			ereport(ERROR,
16101 					(errcode(ERRCODE_SYNTAX_ERROR),
16102 					 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
16103 	}
16104 
16105 	return list_concat(func_args, columns);
16106 }
16107 
16108 /*
16109  * Determine return type of a TABLE function.  A single result column
16110  * returns setof that column's type; otherwise return setof record.
16111  */
16112 static TypeName *
TableFuncTypeName(List * columns)16113 TableFuncTypeName(List *columns)
16114 {
16115 	TypeName *result;
16116 
16117 	if (list_length(columns) == 1)
16118 	{
16119 		FunctionParameter *p = (FunctionParameter *) linitial(columns);
16120 
16121 		result = copyObject(p->argType);
16122 	}
16123 	else
16124 		result = SystemTypeName("record");
16125 
16126 	result->setof = true;
16127 
16128 	return result;
16129 }
16130 
16131 /*
16132  * Convert a list of (dotted) names to a RangeVar (like
16133  * makeRangeVarFromNameList, but with position support).  The
16134  * "AnyName" refers to the any_name production in the grammar.
16135  */
16136 static RangeVar *
makeRangeVarFromAnyName(List * names,int position,core_yyscan_t yyscanner)16137 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
16138 {
16139 	RangeVar *r = makeNode(RangeVar);
16140 
16141 	switch (list_length(names))
16142 	{
16143 		case 1:
16144 			r->catalogname = NULL;
16145 			r->schemaname = NULL;
16146 			r->relname = strVal(linitial(names));
16147 			break;
16148 		case 2:
16149 			r->catalogname = NULL;
16150 			r->schemaname = strVal(linitial(names));
16151 			r->relname = strVal(lsecond(names));
16152 			break;
16153 		case 3:
16154 			r->catalogname = strVal(linitial(names));
16155 			r->schemaname = strVal(lsecond(names));
16156 			r->relname = strVal(lthird(names));
16157 			break;
16158 		default:
16159 			ereport(ERROR,
16160 					(errcode(ERRCODE_SYNTAX_ERROR),
16161 					 errmsg("improper qualified name (too many dotted names): %s",
16162 							NameListToString(names)),
16163 					 parser_errposition(position)));
16164 			break;
16165 	}
16166 
16167 	r->relpersistence = RELPERSISTENCE_PERMANENT;
16168 	r->location = position;
16169 
16170 	return r;
16171 }
16172 
16173 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
16174 static void
SplitColQualList(List * qualList,List ** constraintList,CollateClause ** collClause,core_yyscan_t yyscanner)16175 SplitColQualList(List *qualList,
16176 				 List **constraintList, CollateClause **collClause,
16177 				 core_yyscan_t yyscanner)
16178 {
16179 	ListCell   *cell;
16180 	ListCell   *prev;
16181 	ListCell   *next;
16182 
16183 	*collClause = NULL;
16184 	prev = NULL;
16185 	for (cell = list_head(qualList); cell; cell = next)
16186 	{
16187 		Node   *n = (Node *) lfirst(cell);
16188 
16189 		next = lnext(cell);
16190 		if (IsA(n, Constraint))
16191 		{
16192 			/* keep it in list */
16193 			prev = cell;
16194 			continue;
16195 		}
16196 		if (IsA(n, CollateClause))
16197 		{
16198 			CollateClause *c = (CollateClause *) n;
16199 
16200 			if (*collClause)
16201 				ereport(ERROR,
16202 						(errcode(ERRCODE_SYNTAX_ERROR),
16203 						 errmsg("multiple COLLATE clauses not allowed"),
16204 						 parser_errposition(c->location)));
16205 			*collClause = c;
16206 		}
16207 		else
16208 			elog(ERROR, "unexpected node type %d", (int) n->type);
16209 		/* remove non-Constraint nodes from qualList */
16210 		qualList = list_delete_cell(qualList, cell, prev);
16211 	}
16212 	*constraintList = qualList;
16213 }
16214 
16215 /*
16216  * Process result of ConstraintAttributeSpec, and set appropriate bool flags
16217  * in the output command node.  Pass NULL for any flags the particular
16218  * command doesn't support.
16219  */
16220 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)16221 processCASbits(int cas_bits, int location, const char *constrType,
16222 			   bool *deferrable, bool *initdeferred, bool *not_valid,
16223 			   bool *no_inherit, core_yyscan_t yyscanner)
16224 {
16225 	/* defaults */
16226 	if (deferrable)
16227 		*deferrable = false;
16228 	if (initdeferred)
16229 		*initdeferred = false;
16230 	if (not_valid)
16231 		*not_valid = false;
16232 
16233 	if (cas_bits & (CAS_DEFERRABLE | CAS_INITIALLY_DEFERRED))
16234 	{
16235 		if (deferrable)
16236 			*deferrable = true;
16237 		else
16238 			ereport(ERROR,
16239 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16240 					 /* translator: %s is CHECK, UNIQUE, or similar */
16241 					 errmsg("%s constraints cannot be marked DEFERRABLE",
16242 							constrType),
16243 					 parser_errposition(location)));
16244 	}
16245 
16246 	if (cas_bits & CAS_INITIALLY_DEFERRED)
16247 	{
16248 		if (initdeferred)
16249 			*initdeferred = true;
16250 		else
16251 			ereport(ERROR,
16252 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16253 					 /* translator: %s is CHECK, UNIQUE, or similar */
16254 					 errmsg("%s constraints cannot be marked DEFERRABLE",
16255 							constrType),
16256 					 parser_errposition(location)));
16257 	}
16258 
16259 	if (cas_bits & CAS_NOT_VALID)
16260 	{
16261 		if (not_valid)
16262 			*not_valid = true;
16263 		else
16264 			ereport(ERROR,
16265 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16266 					 /* translator: %s is CHECK, UNIQUE, or similar */
16267 					 errmsg("%s constraints cannot be marked NOT VALID",
16268 							constrType),
16269 					 parser_errposition(location)));
16270 	}
16271 
16272 	if (cas_bits & CAS_NO_INHERIT)
16273 	{
16274 		if (no_inherit)
16275 			*no_inherit = true;
16276 		else
16277 			ereport(ERROR,
16278 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16279 					 /* translator: %s is CHECK, UNIQUE, or similar */
16280 					 errmsg("%s constraints cannot be marked NO INHERIT",
16281 							constrType),
16282 					 parser_errposition(location)));
16283 	}
16284 }
16285 
16286 /*----------
16287  * Recursive view transformation
16288  *
16289  * Convert
16290  *
16291  *     CREATE RECURSIVE VIEW relname (aliases) AS query
16292  *
16293  * to
16294  *
16295  *     CREATE VIEW relname (aliases) AS
16296  *         WITH RECURSIVE relname (aliases) AS (query)
16297  *         SELECT aliases FROM relname
16298  *
16299  * Actually, just the WITH ... part, which is then inserted into the original
16300  * view definition as the query.
16301  * ----------
16302  */
16303 static Node *
makeRecursiveViewSelect(char * relname,List * aliases,Node * query)16304 makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
16305 {
16306 	SelectStmt *s = makeNode(SelectStmt);
16307 	WithClause *w = makeNode(WithClause);
16308 	CommonTableExpr *cte = makeNode(CommonTableExpr);
16309 	List	   *tl = NIL;
16310 	ListCell   *lc;
16311 
16312 	/* create common table expression */
16313 	cte->ctename = relname;
16314 	cte->aliascolnames = aliases;
16315 	cte->ctequery = query;
16316 	cte->location = -1;
16317 
16318 	/* create WITH clause and attach CTE */
16319 	w->recursive = true;
16320 	w->ctes = list_make1(cte);
16321 	w->location = -1;
16322 
16323 	/* create target list for the new SELECT from the alias list of the
16324 	 * recursive view specification */
16325 	foreach (lc, aliases)
16326 	{
16327 		ResTarget *rt = makeNode(ResTarget);
16328 
16329 		rt->name = NULL;
16330 		rt->indirection = NIL;
16331 		rt->val = makeColumnRef(strVal(lfirst(lc)), NIL, -1, 0);
16332 		rt->location = -1;
16333 
16334 		tl = lappend(tl, rt);
16335 	}
16336 
16337 	/* create new SELECT combining WITH clause, target list, and fake FROM
16338 	 * clause */
16339 	s->withClause = w;
16340 	s->targetList = tl;
16341 	s->fromClause = list_make1(makeRangeVar(NULL, relname, -1));
16342 
16343 	return (Node *) s;
16344 }
16345 
16346 /* parser_init()
16347  * Initialize to parse one query string
16348  */
16349 void
parser_init(base_yy_extra_type * yyext)16350 parser_init(base_yy_extra_type *yyext)
16351 {
16352 	yyext->parsetree = NIL;		/* in case grammar forgets to set it */
16353 }
16354