1 %{
2 
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5  *
6  * gram.y
7  *	  POSTGRESQL BISON rules/actions
8  *
9  * Portions Copyright (c) 1996-2017, 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 	GrantObjectType 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 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
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 %type <boolean>	opt_or_replace
310 				opt_grant_grant_option opt_grant_admin_option
311 				opt_nowait opt_if_exists opt_with_data
312 %type <ival>	opt_nowait_or_skip
313 
314 %type <list>	OptRoleList AlterOptRoleList
315 %type <defelt>	CreateOptRoleElem AlterOptRoleElem
316 
317 %type <str>		opt_type
318 %type <str>		foreign_server_version opt_foreign_server_version
319 %type <str>		opt_in_database
320 
321 %type <str>		OptSchemaName
322 %type <list>	OptSchemaEltList
323 
324 %type <boolean> TriggerForSpec TriggerForType
325 %type <ival>	TriggerActionTime
326 %type <list>	TriggerEvents TriggerOneEvent
327 %type <value>	TriggerFuncArg
328 %type <node>	TriggerWhen
329 %type <str>		TransitionRelName
330 %type <boolean>	TransitionRowOrTable TransitionOldOrNew
331 %type <node>	TriggerTransition
332 
333 %type <list>	event_trigger_when_list event_trigger_value_list
334 %type <defelt>	event_trigger_when_item
335 %type <chr>		enable_trigger
336 
337 %type <str>		copy_file_name
338 				database_name access_method_clause access_method attr_name
339 				name cursor_name file_name
340 				index_name opt_index_name cluster_index_specification
341 
342 %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
343 				opt_class opt_inline_handler opt_validator validator_clause
344 				opt_collate
345 
346 %type <range>	qualified_name insert_target OptConstrFromTable
347 
348 %type <str>		all_Op MathOp
349 
350 %type <str>		row_security_cmd RowSecurityDefaultForCmd
351 %type <boolean> RowSecurityDefaultPermissive
352 %type <node>	RowSecurityOptionalWithCheck RowSecurityOptionalExpr
353 %type <list>	RowSecurityDefaultToRole RowSecurityOptionalToRole
354 
355 %type <str>		iso_level opt_encoding
356 %type <rolespec> grantee
357 %type <list>	grantee_list
358 %type <accesspriv> privilege
359 %type <list>	privileges privilege_list
360 %type <privtarget> privilege_target
361 %type <objwithargs> function_with_argtypes aggregate_with_argtypes operator_with_argtypes
362 %type <list>	function_with_argtypes_list aggregate_with_argtypes_list operator_with_argtypes_list
363 %type <ival>	defacl_privilege_target
364 %type <defelt>	DefACLOption
365 %type <list>	DefACLOptionList
366 %type <ival>	import_qualification_type
367 %type <importqual> import_qualification
368 
369 %type <list>	stmtblock stmtmulti
370 				OptTableElementList TableElementList OptInherit definition
371 				OptTypedTableElementList TypedTableElementList
372 				reloptions opt_reloptions
373 				OptWith distinct_clause opt_all_clause opt_definition func_args func_args_list
374 				func_args_with_defaults func_args_with_defaults_list
375 				aggr_args aggr_args_list
376 				func_as createfunc_opt_list alterfunc_opt_list
377 				old_aggr_definition old_aggr_list
378 				oper_argtypes RuleActionList RuleActionMulti
379 				opt_column_list columnList opt_name_list
380 				sort_clause opt_sort_clause sortby_list index_params
381 				name_list role_list from_clause from_list opt_array_bounds
382 				qualified_name_list any_name any_name_list type_name_list
383 				any_operator expr_list attrs
384 				target_list opt_target_list insert_column_list set_target_list
385 				set_clause_list set_clause
386 				def_list operator_def_list indirection opt_indirection
387 				reloption_list group_clause TriggerFuncArgs select_limit
388 				opt_select_limit opclass_item_list opclass_drop_list
389 				opclass_purpose opt_opfamily transaction_mode_list_or_empty
390 				OptTableFuncElementList TableFuncElementList opt_type_modifiers
391 				prep_type_clause
392 				execute_param_clause using_clause returning_clause
393 				opt_enum_val_list enum_val_list table_func_column_list
394 				create_generic_options alter_generic_options
395 				relation_expr_list dostmt_opt_list
396 				transform_element_list transform_type_list
397 				TriggerTransitions TriggerReferencing
398 				publication_name_list
399 
400 %type <list>	group_by_list
401 %type <node>	group_by_item empty_grouping_set rollup_clause cube_clause
402 %type <node>	grouping_sets_clause
403 %type <node>	opt_publication_for_tables publication_for_tables
404 %type <value>	publication_name_item
405 
406 %type <list>	opt_fdw_options fdw_options
407 %type <defelt>	fdw_option
408 
409 %type <range>	OptTempTableName
410 %type <into>	into_clause create_as_target create_mv_target
411 
412 %type <defelt>	createfunc_opt_item common_func_opt_item dostmt_opt_item
413 %type <fun_param> func_arg func_arg_with_default table_func_column aggr_arg
414 %type <fun_param_mode> arg_class
415 %type <typnam>	func_return func_type
416 
417 %type <boolean>  opt_trusted opt_restart_seqs
418 %type <ival>	 OptTemp
419 %type <ival>	 OptNoLog
420 %type <oncommit> OnCommitOption
421 
422 %type <ival>	for_locking_strength
423 %type <node>	for_locking_item
424 %type <list>	for_locking_clause opt_for_locking_clause for_locking_items
425 %type <list>	locked_rels_list
426 %type <boolean>	all_or_distinct
427 
428 %type <node>	join_outer join_qual
429 %type <jtype>	join_type
430 
431 %type <list>	extract_list overlay_list position_list
432 %type <list>	substr_list trim_list
433 %type <list>	opt_interval interval_second
434 %type <node>	overlay_placing substr_from substr_for
435 
436 %type <boolean> opt_instead
437 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
438 %type <boolean> opt_freeze opt_default opt_recheck
439 %type <defelt>	opt_binary opt_oids copy_delimiter
440 
441 %type <boolean> copy_from opt_program
442 
443 %type <ival>	opt_column event cursor_options opt_hold opt_set_data
444 %type <objtype>	drop_type_any_name drop_type_name drop_type_name_on_any_name
445 				comment_type_any_name comment_type_name
446 				security_label_type_any_name security_label_type_name
447 
448 %type <node>	fetch_args limit_clause select_limit_value
449 				offset_clause select_offset_value
450 				select_fetch_first_value I_or_F_const
451 %type <ival>	row_or_rows first_or_next
452 
453 %type <list>	OptSeqOptList SeqOptList OptParenthesizedSeqOptList
454 %type <defelt>	SeqOptElem
455 
456 %type <istmt>	insert_rest
457 %type <infer>	opt_conf_expr
458 %type <onconflict> opt_on_conflict
459 
460 %type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
461 				 SetResetClause FunctionSetResetClause
462 
463 %type <node>	TableElement TypedTableElement ConstraintElem TableFuncElement
464 %type <node>	columnDef columnOptions
465 %type <defelt>	def_elem reloption_elem old_aggr_elem operator_def_elem
466 %type <node>	def_arg columnElem where_clause where_or_current_clause
467 				a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
468 				columnref in_expr having_clause func_table xmltable array_expr
469 				ExclusionWhereClause operator_def_arg
470 %type <list>	rowsfrom_item rowsfrom_list opt_col_def_list
471 %type <boolean> opt_ordinality
472 %type <list>	ExclusionConstraintList ExclusionConstraintElem
473 %type <list>	func_arg_list
474 %type <node>	func_arg_expr
475 %type <list>	row explicit_row implicit_row type_list array_expr_list
476 %type <node>	case_expr case_arg when_clause case_default
477 %type <list>	when_clause_list
478 %type <ival>	sub_type
479 %type <value>	NumericOnly
480 %type <list>	NumericOnly_list
481 %type <alias>	alias_clause opt_alias_clause
482 %type <list>	func_alias_clause
483 %type <sortby>	sortby
484 %type <ielem>	index_elem
485 %type <node>	table_ref
486 %type <jexpr>	joined_table
487 %type <range>	relation_expr
488 %type <range>	relation_expr_opt_alias
489 %type <node>	tablesample_clause opt_repeatable_clause
490 %type <target>	target_el set_target insert_column_item
491 
492 %type <str>		generic_option_name
493 %type <node>	generic_option_arg
494 %type <defelt>	generic_option_elem alter_generic_option_elem
495 %type <list>	generic_option_list alter_generic_option_list
496 %type <str>		explain_option_name
497 %type <node>	explain_option_arg
498 %type <defelt>	explain_option_elem
499 %type <list>	explain_option_list
500 
501 %type <ival>	reindex_target_type reindex_target_multitable
502 %type <ival>	reindex_option_list reindex_option_elem
503 
504 %type <node>	copy_generic_opt_arg copy_generic_opt_arg_list_item
505 %type <defelt>	copy_generic_opt_elem
506 %type <list>	copy_generic_opt_list copy_generic_opt_arg_list
507 %type <list>	copy_options
508 
509 %type <typnam>	Typename SimpleTypename ConstTypename
510 				GenericType Numeric opt_float
511 				Character ConstCharacter
512 				CharacterWithLength CharacterWithoutLength
513 				ConstDatetime ConstInterval
514 				Bit ConstBit BitWithLength BitWithoutLength
515 %type <str>		character
516 %type <str>		extract_arg
517 %type <boolean> opt_varying opt_timezone opt_no_inherit
518 
519 %type <ival>	Iconst SignedIconst
520 %type <str>		Sconst comment_text notify_payload
521 %type <str>		RoleId opt_boolean_or_string
522 %type <list>	var_list
523 %type <str>		ColId ColLabel var_name type_function_name param_name
524 %type <str>		NonReservedWord NonReservedWord_or_Sconst
525 %type <str>		createdb_opt_name
526 %type <node>	var_value zone_value
527 %type <rolespec> auth_ident RoleSpec opt_granted_by
528 
529 %type <keyword> unreserved_keyword type_func_name_keyword
530 %type <keyword> col_name_keyword reserved_keyword
531 
532 %type <node>	TableConstraint TableLikeClause
533 %type <ival>	TableLikeOptionList TableLikeOption
534 %type <list>	ColQualList
535 %type <node>	ColConstraint ColConstraintElem ConstraintAttr
536 %type <ival>	key_actions key_delete key_match key_update key_action
537 %type <ival>	ConstraintAttributeSpec ConstraintAttributeElem
538 %type <str>		ExistingIndex
539 
540 %type <list>	constraints_set_list
541 %type <boolean> constraints_set_mode
542 %type <str>		OptTableSpace OptConsTableSpace
543 %type <rolespec> OptTableSpaceOwner
544 %type <ival>	opt_check_option
545 
546 %type <str>		opt_provider security_label
547 
548 %type <target>	xml_attribute_el
549 %type <list>	xml_attribute_list xml_attributes
550 %type <node>	xml_root_version opt_xml_root_standalone
551 %type <node>	xmlexists_argument
552 %type <ival>	document_or_content
553 %type <boolean> xml_whitespace_option
554 %type <list>	xmltable_column_list xmltable_column_option_list
555 %type <node>	xmltable_column_el
556 %type <defelt>	xmltable_column_option_el
557 %type <list>	xml_namespace_list
558 %type <target>	xml_namespace_el
559 
560 %type <node>	func_application func_expr_common_subexpr
561 %type <node>	func_expr func_expr_windowless
562 %type <node>	common_table_expr
563 %type <with>	with_clause opt_with_clause
564 %type <list>	cte_list
565 
566 %type <list>	within_group_clause
567 %type <node>	filter_clause
568 %type <list>	window_clause window_definition_list opt_partition_clause
569 %type <windef>	window_definition over_clause window_specification
570 				opt_frame_clause frame_extent frame_bound
571 %type <str>		opt_existing_window_name
572 %type <boolean> opt_if_not_exists
573 %type <ival>	generated_when override_kind
574 %type <partspec>	PartitionSpec OptPartitionSpec
575 %type <str>			part_strategy
576 %type <partelem>	part_elem
577 %type <list>		part_params
578 %type <partboundspec> ForValues
579 %type <node>		partbound_datum PartitionRangeDatum
580 %type <list>		partbound_datum_list range_datum_list
581 
582 /*
583  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
584  * They must be listed first so that their numeric codes do not depend on
585  * the set of keywords.  PL/pgSQL depends on this so that it can share the
586  * same lexer.  If you add/change tokens here, fix PL/pgSQL to match!
587  *
588  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
589  * parse errors.  It is needed by PL/pgSQL.
590  */
591 %token <str>	IDENT FCONST SCONST BCONST XCONST Op
592 %token <ival>	ICONST PARAM
593 %token			TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
594 %token			LESS_EQUALS GREATER_EQUALS NOT_EQUALS
595 
596 /*
597  * If you want to make any keyword changes, update the keyword table in
598  * src/include/parser/kwlist.h and add new keywords to the appropriate one
599  * of the reserved-or-not-so-reserved keyword lists, below; search
600  * this file for "Keyword category lists".
601  */
602 
603 /* ordinary key words in alphabetical order */
604 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
605 	AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
606 	ASSERTION ASSIGNMENT ASYMMETRIC AT ATTACH ATTRIBUTE AUTHORIZATION
607 
608 	BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
609 	BOOLEAN_P BOTH BY
610 
611 	CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
612 	CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
613 	CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMENT COMMENTS COMMIT
614 	COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION CONSTRAINT
615 	CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE
616 	CROSS CSV CUBE CURRENT_P
617 	CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
618 	CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
619 
620 	DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
621 	DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DESC
622 	DETACH DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P
623 	DOUBLE_P DROP
624 
625 	EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EVENT EXCEPT
626 	EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
627 	EXTENSION EXTERNAL EXTRACT
628 
629 	FALSE_P FAMILY FETCH FILTER FIRST_P FLOAT_P FOLLOWING FOR
630 	FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
631 
632 	GENERATED GLOBAL GRANT GRANTED GREATEST GROUP_P GROUPING
633 
634 	HANDLER HAVING HEADER_P HOLD HOUR_P
635 
636 	IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P
637 	INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
638 	INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
639 	INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
640 
641 	JOIN
642 
643 	KEY
644 
645 	LABEL LANGUAGE LARGE_P LAST_P LATERAL_P
646 	LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL
647 	LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED
648 
649 	MAPPING MATCH MATERIALIZED MAXVALUE METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE
650 
651 	NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NONE
652 	NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
653 	NULLS_P NUMERIC
654 
655 	OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
656 	ORDER ORDINALITY OUT_P OUTER_P OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
657 
658 	PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PLACING PLANS POLICY
659 	POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
660 	PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROGRAM PUBLICATION
661 
662 	QUOTE
663 
664 	RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REFERENCING
665 	REFRESH REINDEX RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
666 	RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP
667 	ROW ROWS RULE
668 
669 	SAVEPOINT SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
670 	SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
671 	SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P
672 	START STATEMENT STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P
673 	SUBSCRIPTION SUBSTRING SYMMETRIC SYSID SYSTEM_P
674 
675 	TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
676 	TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM TREAT TRIGGER TRIM TRUE_P
677 	TRUNCATE TRUSTED TYPE_P TYPES_P
678 
679 	UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNLOGGED
680 	UNTIL UPDATE USER USING
681 
682 	VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
683 	VERBOSE VERSION_P VIEW VIEWS VOLATILE
684 
685 	WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE
686 
687 	XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLNAMESPACES
688 	XMLPARSE XMLPI XMLROOT XMLSERIALIZE XMLTABLE
689 
690 	YEAR_P YES_P
691 
692 	ZONE
693 
694 /*
695  * The grammar thinks these are keywords, but they are not in the kwlist.h
696  * list and so can never be entered directly.  The filter in parser.c
697  * creates these tokens when required (based on looking one token ahead).
698  *
699  * NOT_LA exists so that productions such as NOT LIKE can be given the same
700  * precedence as LIKE; otherwise they'd effectively have the same precedence
701  * as NOT, at least with respect to their left-hand subexpression.
702  * NULLS_LA and WITH_LA are needed to make the grammar LALR(1).
703  */
704 %token		NOT_LA NULLS_LA WITH_LA
705 
706 
707 /* Precedence: lowest to highest */
708 %nonassoc	SET				/* see relation_expr_opt_alias */
709 %left		UNION EXCEPT
710 %left		INTERSECT
711 %left		OR
712 %left		AND
713 %right		NOT
714 %nonassoc	IS ISNULL NOTNULL	/* IS sets precedence for IS NULL, etc */
715 %nonassoc	'<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
716 %nonassoc	BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA
717 %nonassoc	ESCAPE			/* ESCAPE must be just above LIKE/ILIKE/SIMILAR */
718 %left		POSTFIXOP		/* dummy for postfix Op rules */
719 /*
720  * To support target_el without AS, we must give IDENT an explicit priority
721  * between POSTFIXOP and Op.  We can safely assign the same priority to
722  * various unreserved keywords as needed to resolve ambiguities (this can't
723  * have any bad effects since obviously the keywords will still behave the
724  * same as if they weren't keywords).  We need to do this for PARTITION,
725  * RANGE, ROWS to support opt_existing_window_name; and for RANGE, ROWS
726  * so that they can follow a_expr without creating postfix-operator problems;
727  * for GENERATED so that it can follow b_expr;
728  * and for NULL so that it can follow b_expr in ColQualList without creating
729  * postfix-operator problems.
730  *
731  * To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
732  * an explicit priority lower than '(', so that a rule with CUBE '(' will shift
733  * rather than reducing a conflicting rule that takes CUBE as a function name.
734  * Using the same precedence as IDENT seems right for the reasons given above.
735  *
736  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
737  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
738  * there is no principled way to distinguish these from the productions
739  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
740  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
741  * appear to cause UNBOUNDED to be treated differently from other unreserved
742  * keywords anywhere else in the grammar, but it's definitely risky.  We can
743  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
744  */
745 %nonassoc	UNBOUNDED		/* ideally should have same precedence as IDENT */
746 %nonassoc	IDENT GENERATED NULL_P PARTITION RANGE ROWS PRECEDING FOLLOWING CUBE ROLLUP
747 %left		Op OPERATOR		/* multi-character ops and user-defined operators */
748 %left		'+' '-'
749 %left		'*' '/' '%'
750 %left		'^'
751 /* Unary Operators */
752 %left		AT				/* sets precedence for AT TIME ZONE */
753 %left		COLLATE
754 %right		UMINUS
755 %left		'[' ']'
756 %left		'(' ')'
757 %left		TYPECAST
758 %left		'.'
759 /*
760  * These might seem to be low-precedence, but actually they are not part
761  * of the arithmetic hierarchy at all in their use as JOIN operators.
762  * We make them high-precedence to support their use as function names.
763  * They wouldn't be given a precedence at all, were it not that we need
764  * left-associativity among the JOIN rules themselves.
765  */
766 %left		JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
767 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
768 %right		PRESERVE STRIP_P
769 
770 %%
771 
772 /*
773  *	The target production for the whole parse.
774  */
775 stmtblock:	stmtmulti
776 			{
777 				pg_yyget_extra(yyscanner)->parsetree = $1;
778 			}
779 		;
780 
781 /*
782  * At top level, we wrap each stmt with a RawStmt node carrying start location
783  * and length of the stmt's text.  Notice that the start loc/len are driven
784  * entirely from semicolon locations (@2).  It would seem natural to use
785  * @1 or @3 to get the true start location of a stmt, but that doesn't work
786  * for statements that can start with empty nonterminals (opt_with_clause is
787  * the main offender here); as noted in the comments for YYLLOC_DEFAULT,
788  * we'd get -1 for the location in such cases.
789  * We also take care to discard empty statements entirely.
790  */
791 stmtmulti:	stmtmulti ';' stmt
792 				{
793 					if ($1 != NIL)
794 					{
795 						/* update length of previous stmt */
796 						updateRawStmtEnd(llast_node(RawStmt, $1), @2);
797 					}
798 					if ($3 != NULL)
799 						$$ = lappend($1, makeRawStmt($3, @2 + 1));
800 					else
801 						$$ = $1;
802 				}
803 			| stmt
804 				{
805 					if ($1 != NULL)
806 						$$ = list_make1(makeRawStmt($1, 0));
807 					else
808 						$$ = NIL;
809 				}
810 		;
811 
812 stmt :
813 			AlterEventTrigStmt
814 			| AlterCollationStmt
815 			| AlterDatabaseStmt
816 			| AlterDatabaseSetStmt
817 			| AlterDefaultPrivilegesStmt
818 			| AlterDomainStmt
819 			| AlterEnumStmt
820 			| AlterExtensionStmt
821 			| AlterExtensionContentsStmt
822 			| AlterFdwStmt
823 			| AlterForeignServerStmt
824 			| AlterForeignTableStmt
825 			| AlterFunctionStmt
826 			| AlterGroupStmt
827 			| AlterObjectDependsStmt
828 			| AlterObjectSchemaStmt
829 			| AlterOwnerStmt
830 			| AlterOperatorStmt
831 			| AlterPolicyStmt
832 			| AlterSeqStmt
833 			| AlterSystemStmt
834 			| AlterTableStmt
835 			| AlterTblSpcStmt
836 			| AlterCompositeTypeStmt
837 			| AlterPublicationStmt
838 			| AlterRoleSetStmt
839 			| AlterRoleStmt
840 			| AlterSubscriptionStmt
841 			| AlterTSConfigurationStmt
842 			| AlterTSDictionaryStmt
843 			| AlterUserMappingStmt
844 			| AnalyzeStmt
845 			| CheckPointStmt
846 			| ClosePortalStmt
847 			| ClusterStmt
848 			| CommentStmt
849 			| ConstraintsSetStmt
850 			| CopyStmt
851 			| CreateAmStmt
852 			| CreateAsStmt
853 			| CreateAssertStmt
854 			| CreateCastStmt
855 			| CreateConversionStmt
856 			| CreateDomainStmt
857 			| CreateExtensionStmt
858 			| CreateFdwStmt
859 			| CreateForeignServerStmt
860 			| CreateForeignTableStmt
861 			| CreateFunctionStmt
862 			| CreateGroupStmt
863 			| CreateMatViewStmt
864 			| CreateOpClassStmt
865 			| CreateOpFamilyStmt
866 			| CreatePublicationStmt
867 			| AlterOpFamilyStmt
868 			| CreatePolicyStmt
869 			| CreatePLangStmt
870 			| CreateSchemaStmt
871 			| CreateSeqStmt
872 			| CreateStmt
873 			| CreateSubscriptionStmt
874 			| CreateStatsStmt
875 			| CreateTableSpaceStmt
876 			| CreateTransformStmt
877 			| CreateTrigStmt
878 			| CreateEventTrigStmt
879 			| CreateRoleStmt
880 			| CreateUserStmt
881 			| CreateUserMappingStmt
882 			| CreatedbStmt
883 			| DeallocateStmt
884 			| DeclareCursorStmt
885 			| DefineStmt
886 			| DeleteStmt
887 			| DiscardStmt
888 			| DoStmt
889 			| DropAssertStmt
890 			| DropCastStmt
891 			| DropOpClassStmt
892 			| DropOpFamilyStmt
893 			| DropOwnedStmt
894 			| DropPLangStmt
895 			| DropStmt
896 			| DropSubscriptionStmt
897 			| DropTableSpaceStmt
898 			| DropTransformStmt
899 			| DropRoleStmt
900 			| DropUserMappingStmt
901 			| DropdbStmt
902 			| ExecuteStmt
903 			| ExplainStmt
904 			| FetchStmt
905 			| GrantStmt
906 			| GrantRoleStmt
907 			| ImportForeignSchemaStmt
908 			| IndexStmt
909 			| InsertStmt
910 			| ListenStmt
911 			| RefreshMatViewStmt
912 			| LoadStmt
913 			| LockStmt
914 			| NotifyStmt
915 			| PrepareStmt
916 			| ReassignOwnedStmt
917 			| ReindexStmt
918 			| RemoveAggrStmt
919 			| RemoveFuncStmt
920 			| RemoveOperStmt
921 			| RenameStmt
922 			| RevokeStmt
923 			| RevokeRoleStmt
924 			| RuleStmt
925 			| SecLabelStmt
926 			| SelectStmt
927 			| TransactionStmt
928 			| TruncateStmt
929 			| UnlistenStmt
930 			| UpdateStmt
931 			| VacuumStmt
932 			| VariableResetStmt
933 			| VariableSetStmt
934 			| VariableShowStmt
935 			| ViewStmt
936 			| /*EMPTY*/
937 				{ $$ = NULL; }
938 		;
939 
940 /*****************************************************************************
941  *
942  * Create a new Postgres DBMS role
943  *
944  *****************************************************************************/
945 
946 CreateRoleStmt:
947 			CREATE ROLE RoleId opt_with OptRoleList
948 				{
949 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
950 					n->stmt_type = ROLESTMT_ROLE;
951 					n->role = $3;
952 					n->options = $5;
953 					$$ = (Node *)n;
954 				}
955 		;
956 
957 
958 opt_with:	WITH									{}
959 			| WITH_LA								{}
960 			| /*EMPTY*/								{}
961 		;
962 
963 /*
964  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
965  * for backwards compatibility).  Note: the only option required by SQL99
966  * is "WITH ADMIN name".
967  */
968 OptRoleList:
969 			OptRoleList CreateOptRoleElem			{ $$ = lappend($1, $2); }
970 			| /* EMPTY */							{ $$ = NIL; }
971 		;
972 
973 AlterOptRoleList:
974 			AlterOptRoleList AlterOptRoleElem		{ $$ = lappend($1, $2); }
975 			| /* EMPTY */							{ $$ = NIL; }
976 		;
977 
978 AlterOptRoleElem:
979 			PASSWORD Sconst
980 				{
981 					$$ = makeDefElem("password",
982 									 (Node *)makeString($2), @1);
983 				}
984 			| PASSWORD NULL_P
985 				{
986 					$$ = makeDefElem("password", NULL, @1);
987 				}
988 			| ENCRYPTED PASSWORD Sconst
989 				{
990 					/*
991 					 * These days, passwords are always stored in encrypted
992 					 * form, so there is no difference between PASSWORD and
993 					 * ENCRYPTED PASSWORD.
994 					 */
995 					$$ = makeDefElem("password",
996 									 (Node *)makeString($3), @1);
997 				}
998 			| UNENCRYPTED PASSWORD Sconst
999 				{
1000 					ereport(ERROR,
1001 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1002 							 errmsg("UNENCRYPTED PASSWORD is no longer supported"),
1003 							 errhint("Remove UNENCRYPTED to store the password in encrypted form instead."),
1004 							 parser_errposition(@1)));
1005 				}
1006 			| INHERIT
1007 				{
1008 					$$ = makeDefElem("inherit", (Node *)makeInteger(TRUE), @1);
1009 				}
1010 			| CONNECTION LIMIT SignedIconst
1011 				{
1012 					$$ = makeDefElem("connectionlimit", (Node *)makeInteger($3), @1);
1013 				}
1014 			| VALID UNTIL Sconst
1015 				{
1016 					$$ = makeDefElem("validUntil", (Node *)makeString($3), @1);
1017 				}
1018 		/*	Supported but not documented for roles, for use by ALTER GROUP. */
1019 			| USER role_list
1020 				{
1021 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1022 				}
1023 			| IDENT
1024 				{
1025 					/*
1026 					 * We handle identifiers that aren't parser keywords with
1027 					 * the following special-case codes, to avoid bloating the
1028 					 * size of the main parser.
1029 					 */
1030 					if (strcmp($1, "superuser") == 0)
1031 						$$ = makeDefElem("superuser", (Node *)makeInteger(TRUE), @1);
1032 					else if (strcmp($1, "nosuperuser") == 0)
1033 						$$ = makeDefElem("superuser", (Node *)makeInteger(FALSE), @1);
1034 					else if (strcmp($1, "createrole") == 0)
1035 						$$ = makeDefElem("createrole", (Node *)makeInteger(TRUE), @1);
1036 					else if (strcmp($1, "nocreaterole") == 0)
1037 						$$ = makeDefElem("createrole", (Node *)makeInteger(FALSE), @1);
1038 					else if (strcmp($1, "replication") == 0)
1039 						$$ = makeDefElem("isreplication", (Node *)makeInteger(TRUE), @1);
1040 					else if (strcmp($1, "noreplication") == 0)
1041 						$$ = makeDefElem("isreplication", (Node *)makeInteger(FALSE), @1);
1042 					else if (strcmp($1, "createdb") == 0)
1043 						$$ = makeDefElem("createdb", (Node *)makeInteger(TRUE), @1);
1044 					else if (strcmp($1, "nocreatedb") == 0)
1045 						$$ = makeDefElem("createdb", (Node *)makeInteger(FALSE), @1);
1046 					else if (strcmp($1, "login") == 0)
1047 						$$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE), @1);
1048 					else if (strcmp($1, "nologin") == 0)
1049 						$$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE), @1);
1050 					else if (strcmp($1, "bypassrls") == 0)
1051 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(TRUE), @1);
1052 					else if (strcmp($1, "nobypassrls") == 0)
1053 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(FALSE), @1);
1054 					else if (strcmp($1, "noinherit") == 0)
1055 					{
1056 						/*
1057 						 * Note that INHERIT is a keyword, so it's handled by main parser, but
1058 						 * NOINHERIT is handled here.
1059 						 */
1060 						$$ = makeDefElem("inherit", (Node *)makeInteger(FALSE), @1);
1061 					}
1062 					else
1063 						ereport(ERROR,
1064 								(errcode(ERRCODE_SYNTAX_ERROR),
1065 								 errmsg("unrecognized role option \"%s\"", $1),
1066 									 parser_errposition(@1)));
1067 				}
1068 		;
1069 
1070 CreateOptRoleElem:
1071 			AlterOptRoleElem			{ $$ = $1; }
1072 			/* The following are not supported by ALTER ROLE/USER/GROUP */
1073 			| SYSID Iconst
1074 				{
1075 					$$ = makeDefElem("sysid", (Node *)makeInteger($2), @1);
1076 				}
1077 			| ADMIN role_list
1078 				{
1079 					$$ = makeDefElem("adminmembers", (Node *)$2, @1);
1080 				}
1081 			| ROLE role_list
1082 				{
1083 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1084 				}
1085 			| IN_P ROLE role_list
1086 				{
1087 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1088 				}
1089 			| IN_P GROUP_P role_list
1090 				{
1091 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1092 				}
1093 		;
1094 
1095 
1096 /*****************************************************************************
1097  *
1098  * Create a new Postgres DBMS user (role with implied login ability)
1099  *
1100  *****************************************************************************/
1101 
1102 CreateUserStmt:
1103 			CREATE USER RoleId opt_with OptRoleList
1104 				{
1105 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1106 					n->stmt_type = ROLESTMT_USER;
1107 					n->role = $3;
1108 					n->options = $5;
1109 					$$ = (Node *)n;
1110 				}
1111 		;
1112 
1113 
1114 /*****************************************************************************
1115  *
1116  * Alter a postgresql DBMS role
1117  *
1118  *****************************************************************************/
1119 
1120 AlterRoleStmt:
1121 			ALTER ROLE RoleSpec opt_with AlterOptRoleList
1122 				 {
1123 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1124 					n->role = $3;
1125 					n->action = +1;	/* add, if there are members */
1126 					n->options = $5;
1127 					$$ = (Node *)n;
1128 				 }
1129 			| ALTER USER RoleSpec opt_with AlterOptRoleList
1130 				 {
1131 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1132 					n->role = $3;
1133 					n->action = +1;	/* add, if there are members */
1134 					n->options = $5;
1135 					$$ = (Node *)n;
1136 				 }
1137 		;
1138 
1139 opt_in_database:
1140 			   /* EMPTY */					{ $$ = NULL; }
1141 			| IN_P DATABASE database_name	{ $$ = $3; }
1142 		;
1143 
1144 AlterRoleSetStmt:
1145 			ALTER ROLE RoleSpec opt_in_database SetResetClause
1146 				{
1147 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1148 					n->role = $3;
1149 					n->database = $4;
1150 					n->setstmt = $5;
1151 					$$ = (Node *)n;
1152 				}
1153 			| ALTER ROLE ALL opt_in_database SetResetClause
1154 				{
1155 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1156 					n->role = NULL;
1157 					n->database = $4;
1158 					n->setstmt = $5;
1159 					$$ = (Node *)n;
1160 				}
1161 			| ALTER USER RoleSpec opt_in_database SetResetClause
1162 				{
1163 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1164 					n->role = $3;
1165 					n->database = $4;
1166 					n->setstmt = $5;
1167 					$$ = (Node *)n;
1168 				}
1169 			| ALTER USER ALL opt_in_database SetResetClause
1170 				{
1171 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1172 					n->role = NULL;
1173 					n->database = $4;
1174 					n->setstmt = $5;
1175 					$$ = (Node *)n;
1176 				}
1177 		;
1178 
1179 
1180 /*****************************************************************************
1181  *
1182  * Drop a postgresql DBMS role
1183  *
1184  * XXX Ideally this would have CASCADE/RESTRICT options, but a role
1185  * might own objects in multiple databases, and there is presently no way to
1186  * implement cascading to other databases.  So we always behave as RESTRICT.
1187  *****************************************************************************/
1188 
1189 DropRoleStmt:
1190 			DROP ROLE role_list
1191 				{
1192 					DropRoleStmt *n = makeNode(DropRoleStmt);
1193 					n->missing_ok = FALSE;
1194 					n->roles = $3;
1195 					$$ = (Node *)n;
1196 				}
1197 			| DROP ROLE IF_P EXISTS role_list
1198 				{
1199 					DropRoleStmt *n = makeNode(DropRoleStmt);
1200 					n->missing_ok = TRUE;
1201 					n->roles = $5;
1202 					$$ = (Node *)n;
1203 				}
1204 			| DROP USER role_list
1205 				{
1206 					DropRoleStmt *n = makeNode(DropRoleStmt);
1207 					n->missing_ok = FALSE;
1208 					n->roles = $3;
1209 					$$ = (Node *)n;
1210 				}
1211 			| DROP USER IF_P EXISTS role_list
1212 				{
1213 					DropRoleStmt *n = makeNode(DropRoleStmt);
1214 					n->roles = $5;
1215 					n->missing_ok = TRUE;
1216 					$$ = (Node *)n;
1217 				}
1218 			| DROP GROUP_P role_list
1219 				{
1220 					DropRoleStmt *n = makeNode(DropRoleStmt);
1221 					n->missing_ok = FALSE;
1222 					n->roles = $3;
1223 					$$ = (Node *)n;
1224 				}
1225 			| DROP GROUP_P IF_P EXISTS role_list
1226 				{
1227 					DropRoleStmt *n = makeNode(DropRoleStmt);
1228 					n->missing_ok = TRUE;
1229 					n->roles = $5;
1230 					$$ = (Node *)n;
1231 				}
1232 			;
1233 
1234 
1235 /*****************************************************************************
1236  *
1237  * Create a postgresql group (role without login ability)
1238  *
1239  *****************************************************************************/
1240 
1241 CreateGroupStmt:
1242 			CREATE GROUP_P RoleId opt_with OptRoleList
1243 				{
1244 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1245 					n->stmt_type = ROLESTMT_GROUP;
1246 					n->role = $3;
1247 					n->options = $5;
1248 					$$ = (Node *)n;
1249 				}
1250 		;
1251 
1252 
1253 /*****************************************************************************
1254  *
1255  * Alter a postgresql group
1256  *
1257  *****************************************************************************/
1258 
1259 AlterGroupStmt:
1260 			ALTER GROUP_P RoleSpec add_drop USER role_list
1261 				{
1262 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1263 					n->role = $3;
1264 					n->action = $4;
1265 					n->options = list_make1(makeDefElem("rolemembers",
1266 														(Node *)$6, @6));
1267 					$$ = (Node *)n;
1268 				}
1269 		;
1270 
1271 add_drop:	ADD_P									{ $$ = +1; }
1272 			| DROP									{ $$ = -1; }
1273 		;
1274 
1275 
1276 /*****************************************************************************
1277  *
1278  * Manipulate a schema
1279  *
1280  *****************************************************************************/
1281 
1282 CreateSchemaStmt:
1283 			CREATE SCHEMA OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1284 				{
1285 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1286 					/* One can omit the schema name or the authorization id. */
1287 					n->schemaname = $3;
1288 					n->authrole = $5;
1289 					n->schemaElts = $6;
1290 					n->if_not_exists = false;
1291 					$$ = (Node *)n;
1292 				}
1293 			| CREATE SCHEMA ColId OptSchemaEltList
1294 				{
1295 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1296 					/* ...but not both */
1297 					n->schemaname = $3;
1298 					n->authrole = NULL;
1299 					n->schemaElts = $4;
1300 					n->if_not_exists = false;
1301 					$$ = (Node *)n;
1302 				}
1303 			| CREATE SCHEMA IF_P NOT EXISTS OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1304 				{
1305 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1306 					/* schema name can be omitted here, too */
1307 					n->schemaname = $6;
1308 					n->authrole = $8;
1309 					if ($9 != NIL)
1310 						ereport(ERROR,
1311 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1312 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1313 								 parser_errposition(@9)));
1314 					n->schemaElts = $9;
1315 					n->if_not_exists = true;
1316 					$$ = (Node *)n;
1317 				}
1318 			| CREATE SCHEMA IF_P NOT EXISTS ColId OptSchemaEltList
1319 				{
1320 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1321 					/* ...but not here */
1322 					n->schemaname = $6;
1323 					n->authrole = NULL;
1324 					if ($7 != NIL)
1325 						ereport(ERROR,
1326 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1327 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1328 								 parser_errposition(@7)));
1329 					n->schemaElts = $7;
1330 					n->if_not_exists = true;
1331 					$$ = (Node *)n;
1332 				}
1333 		;
1334 
1335 OptSchemaName:
1336 			ColId									{ $$ = $1; }
1337 			| /* EMPTY */							{ $$ = NULL; }
1338 		;
1339 
1340 OptSchemaEltList:
1341 			OptSchemaEltList schema_stmt
1342 				{
1343 					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
1344 						@$ = @2;
1345 					$$ = lappend($1, $2);
1346 				}
1347 			| /* EMPTY */
1348 				{ $$ = NIL; }
1349 		;
1350 
1351 /*
1352  *	schema_stmt are the ones that can show up inside a CREATE SCHEMA
1353  *	statement (in addition to by themselves).
1354  */
1355 schema_stmt:
1356 			CreateStmt
1357 			| IndexStmt
1358 			| CreateSeqStmt
1359 			| CreateTrigStmt
1360 			| GrantStmt
1361 			| ViewStmt
1362 		;
1363 
1364 
1365 /*****************************************************************************
1366  *
1367  * Set PG internal variable
1368  *	  SET name TO 'var_value'
1369  * Include SQL syntax (thomas 1997-10-22):
1370  *	  SET TIME ZONE 'var_value'
1371  *
1372  *****************************************************************************/
1373 
1374 VariableSetStmt:
1375 			SET set_rest
1376 				{
1377 					VariableSetStmt *n = $2;
1378 					n->is_local = false;
1379 					$$ = (Node *) n;
1380 				}
1381 			| SET LOCAL set_rest
1382 				{
1383 					VariableSetStmt *n = $3;
1384 					n->is_local = true;
1385 					$$ = (Node *) n;
1386 				}
1387 			| SET SESSION set_rest
1388 				{
1389 					VariableSetStmt *n = $3;
1390 					n->is_local = false;
1391 					$$ = (Node *) n;
1392 				}
1393 		;
1394 
1395 set_rest:
1396 			TRANSACTION transaction_mode_list
1397 				{
1398 					VariableSetStmt *n = makeNode(VariableSetStmt);
1399 					n->kind = VAR_SET_MULTI;
1400 					n->name = "TRANSACTION";
1401 					n->args = $2;
1402 					$$ = n;
1403 				}
1404 			| SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1405 				{
1406 					VariableSetStmt *n = makeNode(VariableSetStmt);
1407 					n->kind = VAR_SET_MULTI;
1408 					n->name = "SESSION CHARACTERISTICS";
1409 					n->args = $5;
1410 					$$ = n;
1411 				}
1412 			| set_rest_more
1413 			;
1414 
1415 generic_set:
1416 			var_name TO var_list
1417 				{
1418 					VariableSetStmt *n = makeNode(VariableSetStmt);
1419 					n->kind = VAR_SET_VALUE;
1420 					n->name = $1;
1421 					n->args = $3;
1422 					$$ = n;
1423 				}
1424 			| var_name '=' var_list
1425 				{
1426 					VariableSetStmt *n = makeNode(VariableSetStmt);
1427 					n->kind = VAR_SET_VALUE;
1428 					n->name = $1;
1429 					n->args = $3;
1430 					$$ = n;
1431 				}
1432 			| var_name TO DEFAULT
1433 				{
1434 					VariableSetStmt *n = makeNode(VariableSetStmt);
1435 					n->kind = VAR_SET_DEFAULT;
1436 					n->name = $1;
1437 					$$ = n;
1438 				}
1439 			| var_name '=' DEFAULT
1440 				{
1441 					VariableSetStmt *n = makeNode(VariableSetStmt);
1442 					n->kind = VAR_SET_DEFAULT;
1443 					n->name = $1;
1444 					$$ = n;
1445 				}
1446 		;
1447 
1448 set_rest_more:	/* Generic SET syntaxes: */
1449 			generic_set 						{$$ = $1;}
1450 			| var_name FROM CURRENT_P
1451 				{
1452 					VariableSetStmt *n = makeNode(VariableSetStmt);
1453 					n->kind = VAR_SET_CURRENT;
1454 					n->name = $1;
1455 					$$ = n;
1456 				}
1457 			/* Special syntaxes mandated by SQL standard: */
1458 			| TIME ZONE zone_value
1459 				{
1460 					VariableSetStmt *n = makeNode(VariableSetStmt);
1461 					n->kind = VAR_SET_VALUE;
1462 					n->name = "timezone";
1463 					if ($3 != NULL)
1464 						n->args = list_make1($3);
1465 					else
1466 						n->kind = VAR_SET_DEFAULT;
1467 					$$ = n;
1468 				}
1469 			| CATALOG_P Sconst
1470 				{
1471 					ereport(ERROR,
1472 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1473 							 errmsg("current database cannot be changed"),
1474 							 parser_errposition(@2)));
1475 					$$ = NULL; /*not reached*/
1476 				}
1477 			| SCHEMA Sconst
1478 				{
1479 					VariableSetStmt *n = makeNode(VariableSetStmt);
1480 					n->kind = VAR_SET_VALUE;
1481 					n->name = "search_path";
1482 					n->args = list_make1(makeStringConst($2, @2));
1483 					$$ = n;
1484 				}
1485 			| NAMES opt_encoding
1486 				{
1487 					VariableSetStmt *n = makeNode(VariableSetStmt);
1488 					n->kind = VAR_SET_VALUE;
1489 					n->name = "client_encoding";
1490 					if ($2 != NULL)
1491 						n->args = list_make1(makeStringConst($2, @2));
1492 					else
1493 						n->kind = VAR_SET_DEFAULT;
1494 					$$ = n;
1495 				}
1496 			| ROLE NonReservedWord_or_Sconst
1497 				{
1498 					VariableSetStmt *n = makeNode(VariableSetStmt);
1499 					n->kind = VAR_SET_VALUE;
1500 					n->name = "role";
1501 					n->args = list_make1(makeStringConst($2, @2));
1502 					$$ = n;
1503 				}
1504 			| SESSION AUTHORIZATION NonReservedWord_or_Sconst
1505 				{
1506 					VariableSetStmt *n = makeNode(VariableSetStmt);
1507 					n->kind = VAR_SET_VALUE;
1508 					n->name = "session_authorization";
1509 					n->args = list_make1(makeStringConst($3, @3));
1510 					$$ = n;
1511 				}
1512 			| SESSION AUTHORIZATION DEFAULT
1513 				{
1514 					VariableSetStmt *n = makeNode(VariableSetStmt);
1515 					n->kind = VAR_SET_DEFAULT;
1516 					n->name = "session_authorization";
1517 					$$ = n;
1518 				}
1519 			| XML_P OPTION document_or_content
1520 				{
1521 					VariableSetStmt *n = makeNode(VariableSetStmt);
1522 					n->kind = VAR_SET_VALUE;
1523 					n->name = "xmloption";
1524 					n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1525 					$$ = n;
1526 				}
1527 			/* Special syntaxes invented by PostgreSQL: */
1528 			| TRANSACTION SNAPSHOT Sconst
1529 				{
1530 					VariableSetStmt *n = makeNode(VariableSetStmt);
1531 					n->kind = VAR_SET_MULTI;
1532 					n->name = "TRANSACTION SNAPSHOT";
1533 					n->args = list_make1(makeStringConst($3, @3));
1534 					$$ = n;
1535 				}
1536 		;
1537 
1538 var_name:	ColId								{ $$ = $1; }
1539 			| var_name '.' ColId
1540 				{ $$ = psprintf("%s.%s", $1, $3); }
1541 		;
1542 
1543 var_list:	var_value								{ $$ = list_make1($1); }
1544 			| var_list ',' var_value				{ $$ = lappend($1, $3); }
1545 		;
1546 
1547 var_value:	opt_boolean_or_string
1548 				{ $$ = makeStringConst($1, @1); }
1549 			| NumericOnly
1550 				{ $$ = makeAConst($1, @1); }
1551 		;
1552 
1553 iso_level:	READ UNCOMMITTED						{ $$ = "read uncommitted"; }
1554 			| READ COMMITTED						{ $$ = "read committed"; }
1555 			| REPEATABLE READ						{ $$ = "repeatable read"; }
1556 			| SERIALIZABLE							{ $$ = "serializable"; }
1557 		;
1558 
1559 opt_boolean_or_string:
1560 			TRUE_P									{ $$ = "true"; }
1561 			| FALSE_P								{ $$ = "false"; }
1562 			| ON									{ $$ = "on"; }
1563 			/*
1564 			 * OFF is also accepted as a boolean value, but is handled by
1565 			 * the NonReservedWord rule.  The action for booleans and strings
1566 			 * is the same, so we don't need to distinguish them here.
1567 			 */
1568 			| NonReservedWord_or_Sconst				{ $$ = $1; }
1569 		;
1570 
1571 /* Timezone values can be:
1572  * - a string such as 'pst8pdt'
1573  * - an identifier such as "pst8pdt"
1574  * - an integer or floating point number
1575  * - a time interval per SQL99
1576  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1577  * so use IDENT (meaning we reject anything that is a key word).
1578  */
1579 zone_value:
1580 			Sconst
1581 				{
1582 					$$ = makeStringConst($1, @1);
1583 				}
1584 			| IDENT
1585 				{
1586 					$$ = makeStringConst($1, @1);
1587 				}
1588 			| ConstInterval Sconst opt_interval
1589 				{
1590 					TypeName *t = $1;
1591 					if ($3 != NIL)
1592 					{
1593 						A_Const *n = (A_Const *) linitial($3);
1594 						if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1595 							ereport(ERROR,
1596 									(errcode(ERRCODE_SYNTAX_ERROR),
1597 									 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1598 									 parser_errposition(@3)));
1599 					}
1600 					t->typmods = $3;
1601 					$$ = makeStringConstCast($2, @2, t);
1602 				}
1603 			| ConstInterval '(' Iconst ')' Sconst
1604 				{
1605 					TypeName *t = $1;
1606 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1607 											makeIntConst($3, @3));
1608 					$$ = makeStringConstCast($5, @5, t);
1609 				}
1610 			| NumericOnly							{ $$ = makeAConst($1, @1); }
1611 			| DEFAULT								{ $$ = NULL; }
1612 			| LOCAL									{ $$ = NULL; }
1613 		;
1614 
1615 opt_encoding:
1616 			Sconst									{ $$ = $1; }
1617 			| DEFAULT								{ $$ = NULL; }
1618 			| /*EMPTY*/								{ $$ = NULL; }
1619 		;
1620 
1621 NonReservedWord_or_Sconst:
1622 			NonReservedWord							{ $$ = $1; }
1623 			| Sconst								{ $$ = $1; }
1624 		;
1625 
1626 VariableResetStmt:
1627 			RESET reset_rest						{ $$ = (Node *) $2; }
1628 		;
1629 
1630 reset_rest:
1631 			generic_reset							{ $$ = $1; }
1632 			| TIME ZONE
1633 				{
1634 					VariableSetStmt *n = makeNode(VariableSetStmt);
1635 					n->kind = VAR_RESET;
1636 					n->name = "timezone";
1637 					$$ = n;
1638 				}
1639 			| TRANSACTION ISOLATION LEVEL
1640 				{
1641 					VariableSetStmt *n = makeNode(VariableSetStmt);
1642 					n->kind = VAR_RESET;
1643 					n->name = "transaction_isolation";
1644 					$$ = n;
1645 				}
1646 			| SESSION AUTHORIZATION
1647 				{
1648 					VariableSetStmt *n = makeNode(VariableSetStmt);
1649 					n->kind = VAR_RESET;
1650 					n->name = "session_authorization";
1651 					$$ = n;
1652 				}
1653 		;
1654 
1655 generic_reset:
1656 			var_name
1657 				{
1658 					VariableSetStmt *n = makeNode(VariableSetStmt);
1659 					n->kind = VAR_RESET;
1660 					n->name = $1;
1661 					$$ = n;
1662 				}
1663 			| ALL
1664 				{
1665 					VariableSetStmt *n = makeNode(VariableSetStmt);
1666 					n->kind = VAR_RESET_ALL;
1667 					$$ = n;
1668 				}
1669 		;
1670 
1671 /* SetResetClause allows SET or RESET without LOCAL */
1672 SetResetClause:
1673 			SET set_rest					{ $$ = $2; }
1674 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1675 		;
1676 
1677 /* SetResetClause allows SET or RESET without LOCAL */
1678 FunctionSetResetClause:
1679 			SET set_rest_more				{ $$ = $2; }
1680 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1681 		;
1682 
1683 
1684 VariableShowStmt:
1685 			SHOW var_name
1686 				{
1687 					VariableShowStmt *n = makeNode(VariableShowStmt);
1688 					n->name = $2;
1689 					$$ = (Node *) n;
1690 				}
1691 			| SHOW TIME ZONE
1692 				{
1693 					VariableShowStmt *n = makeNode(VariableShowStmt);
1694 					n->name = "timezone";
1695 					$$ = (Node *) n;
1696 				}
1697 			| SHOW TRANSACTION ISOLATION LEVEL
1698 				{
1699 					VariableShowStmt *n = makeNode(VariableShowStmt);
1700 					n->name = "transaction_isolation";
1701 					$$ = (Node *) n;
1702 				}
1703 			| SHOW SESSION AUTHORIZATION
1704 				{
1705 					VariableShowStmt *n = makeNode(VariableShowStmt);
1706 					n->name = "session_authorization";
1707 					$$ = (Node *) n;
1708 				}
1709 			| SHOW ALL
1710 				{
1711 					VariableShowStmt *n = makeNode(VariableShowStmt);
1712 					n->name = "all";
1713 					$$ = (Node *) n;
1714 				}
1715 		;
1716 
1717 
1718 ConstraintsSetStmt:
1719 			SET CONSTRAINTS constraints_set_list constraints_set_mode
1720 				{
1721 					ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1722 					n->constraints = $3;
1723 					n->deferred = $4;
1724 					$$ = (Node *) n;
1725 				}
1726 		;
1727 
1728 constraints_set_list:
1729 			ALL										{ $$ = NIL; }
1730 			| qualified_name_list					{ $$ = $1; }
1731 		;
1732 
1733 constraints_set_mode:
1734 			DEFERRED								{ $$ = TRUE; }
1735 			| IMMEDIATE								{ $$ = FALSE; }
1736 		;
1737 
1738 
1739 /*
1740  * Checkpoint statement
1741  */
1742 CheckPointStmt:
1743 			CHECKPOINT
1744 				{
1745 					CheckPointStmt *n = makeNode(CheckPointStmt);
1746 					$$ = (Node *)n;
1747 				}
1748 		;
1749 
1750 
1751 /*****************************************************************************
1752  *
1753  * DISCARD { ALL | TEMP | PLANS | SEQUENCES }
1754  *
1755  *****************************************************************************/
1756 
1757 DiscardStmt:
1758 			DISCARD ALL
1759 				{
1760 					DiscardStmt *n = makeNode(DiscardStmt);
1761 					n->target = DISCARD_ALL;
1762 					$$ = (Node *) n;
1763 				}
1764 			| DISCARD TEMP
1765 				{
1766 					DiscardStmt *n = makeNode(DiscardStmt);
1767 					n->target = DISCARD_TEMP;
1768 					$$ = (Node *) n;
1769 				}
1770 			| DISCARD TEMPORARY
1771 				{
1772 					DiscardStmt *n = makeNode(DiscardStmt);
1773 					n->target = DISCARD_TEMP;
1774 					$$ = (Node *) n;
1775 				}
1776 			| DISCARD PLANS
1777 				{
1778 					DiscardStmt *n = makeNode(DiscardStmt);
1779 					n->target = DISCARD_PLANS;
1780 					$$ = (Node *) n;
1781 				}
1782 			| DISCARD SEQUENCES
1783 				{
1784 					DiscardStmt *n = makeNode(DiscardStmt);
1785 					n->target = DISCARD_SEQUENCES;
1786 					$$ = (Node *) n;
1787 				}
1788 
1789 		;
1790 
1791 
1792 /*****************************************************************************
1793  *
1794  *	ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW ] variations
1795  *
1796  * Note: we accept all subcommands for each of the five variants, and sort
1797  * out what's really legal at execution time.
1798  *****************************************************************************/
1799 
1800 AlterTableStmt:
1801 			ALTER TABLE relation_expr alter_table_cmds
1802 				{
1803 					AlterTableStmt *n = makeNode(AlterTableStmt);
1804 					n->relation = $3;
1805 					n->cmds = $4;
1806 					n->relkind = OBJECT_TABLE;
1807 					n->missing_ok = false;
1808 					$$ = (Node *)n;
1809 				}
1810 		|	ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
1811 				{
1812 					AlterTableStmt *n = makeNode(AlterTableStmt);
1813 					n->relation = $5;
1814 					n->cmds = $6;
1815 					n->relkind = OBJECT_TABLE;
1816 					n->missing_ok = true;
1817 					$$ = (Node *)n;
1818 				}
1819 		|	ALTER TABLE relation_expr partition_cmd
1820 				{
1821 					AlterTableStmt *n = makeNode(AlterTableStmt);
1822 					n->relation = $3;
1823 					n->cmds = list_make1($4);
1824 					n->relkind = OBJECT_TABLE;
1825 					n->missing_ok = false;
1826 					$$ = (Node *)n;
1827 				}
1828 		|	ALTER TABLE IF_P EXISTS relation_expr partition_cmd
1829 				{
1830 					AlterTableStmt *n = makeNode(AlterTableStmt);
1831 					n->relation = $5;
1832 					n->cmds = list_make1($6);
1833 					n->relkind = OBJECT_TABLE;
1834 					n->missing_ok = true;
1835 					$$ = (Node *)n;
1836 				}
1837 		|	ALTER TABLE ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1838 				{
1839 					AlterTableMoveAllStmt *n =
1840 						makeNode(AlterTableMoveAllStmt);
1841 					n->orig_tablespacename = $6;
1842 					n->objtype = OBJECT_TABLE;
1843 					n->roles = NIL;
1844 					n->new_tablespacename = $9;
1845 					n->nowait = $10;
1846 					$$ = (Node *)n;
1847 				}
1848 		|	ALTER TABLE ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1849 				{
1850 					AlterTableMoveAllStmt *n =
1851 						makeNode(AlterTableMoveAllStmt);
1852 					n->orig_tablespacename = $6;
1853 					n->objtype = OBJECT_TABLE;
1854 					n->roles = $9;
1855 					n->new_tablespacename = $12;
1856 					n->nowait = $13;
1857 					$$ = (Node *)n;
1858 				}
1859 		|	ALTER INDEX qualified_name alter_table_cmds
1860 				{
1861 					AlterTableStmt *n = makeNode(AlterTableStmt);
1862 					n->relation = $3;
1863 					n->cmds = $4;
1864 					n->relkind = OBJECT_INDEX;
1865 					n->missing_ok = false;
1866 					$$ = (Node *)n;
1867 				}
1868 		|	ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
1869 				{
1870 					AlterTableStmt *n = makeNode(AlterTableStmt);
1871 					n->relation = $5;
1872 					n->cmds = $6;
1873 					n->relkind = OBJECT_INDEX;
1874 					n->missing_ok = true;
1875 					$$ = (Node *)n;
1876 				}
1877 		|	ALTER INDEX ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1878 				{
1879 					AlterTableMoveAllStmt *n =
1880 						makeNode(AlterTableMoveAllStmt);
1881 					n->orig_tablespacename = $6;
1882 					n->objtype = OBJECT_INDEX;
1883 					n->roles = NIL;
1884 					n->new_tablespacename = $9;
1885 					n->nowait = $10;
1886 					$$ = (Node *)n;
1887 				}
1888 		|	ALTER INDEX ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1889 				{
1890 					AlterTableMoveAllStmt *n =
1891 						makeNode(AlterTableMoveAllStmt);
1892 					n->orig_tablespacename = $6;
1893 					n->objtype = OBJECT_INDEX;
1894 					n->roles = $9;
1895 					n->new_tablespacename = $12;
1896 					n->nowait = $13;
1897 					$$ = (Node *)n;
1898 				}
1899 		|	ALTER SEQUENCE qualified_name alter_table_cmds
1900 				{
1901 					AlterTableStmt *n = makeNode(AlterTableStmt);
1902 					n->relation = $3;
1903 					n->cmds = $4;
1904 					n->relkind = OBJECT_SEQUENCE;
1905 					n->missing_ok = false;
1906 					$$ = (Node *)n;
1907 				}
1908 		|	ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
1909 				{
1910 					AlterTableStmt *n = makeNode(AlterTableStmt);
1911 					n->relation = $5;
1912 					n->cmds = $6;
1913 					n->relkind = OBJECT_SEQUENCE;
1914 					n->missing_ok = true;
1915 					$$ = (Node *)n;
1916 				}
1917 		|	ALTER VIEW qualified_name alter_table_cmds
1918 				{
1919 					AlterTableStmt *n = makeNode(AlterTableStmt);
1920 					n->relation = $3;
1921 					n->cmds = $4;
1922 					n->relkind = OBJECT_VIEW;
1923 					n->missing_ok = false;
1924 					$$ = (Node *)n;
1925 				}
1926 		|	ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
1927 				{
1928 					AlterTableStmt *n = makeNode(AlterTableStmt);
1929 					n->relation = $5;
1930 					n->cmds = $6;
1931 					n->relkind = OBJECT_VIEW;
1932 					n->missing_ok = true;
1933 					$$ = (Node *)n;
1934 				}
1935 		|	ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
1936 				{
1937 					AlterTableStmt *n = makeNode(AlterTableStmt);
1938 					n->relation = $4;
1939 					n->cmds = $5;
1940 					n->relkind = OBJECT_MATVIEW;
1941 					n->missing_ok = false;
1942 					$$ = (Node *)n;
1943 				}
1944 		|	ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
1945 				{
1946 					AlterTableStmt *n = makeNode(AlterTableStmt);
1947 					n->relation = $6;
1948 					n->cmds = $7;
1949 					n->relkind = OBJECT_MATVIEW;
1950 					n->missing_ok = true;
1951 					$$ = (Node *)n;
1952 				}
1953 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1954 				{
1955 					AlterTableMoveAllStmt *n =
1956 						makeNode(AlterTableMoveAllStmt);
1957 					n->orig_tablespacename = $7;
1958 					n->objtype = OBJECT_MATVIEW;
1959 					n->roles = NIL;
1960 					n->new_tablespacename = $10;
1961 					n->nowait = $11;
1962 					$$ = (Node *)n;
1963 				}
1964 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1965 				{
1966 					AlterTableMoveAllStmt *n =
1967 						makeNode(AlterTableMoveAllStmt);
1968 					n->orig_tablespacename = $7;
1969 					n->objtype = OBJECT_MATVIEW;
1970 					n->roles = $10;
1971 					n->new_tablespacename = $13;
1972 					n->nowait = $14;
1973 					$$ = (Node *)n;
1974 				}
1975 		;
1976 
1977 alter_table_cmds:
1978 			alter_table_cmd							{ $$ = list_make1($1); }
1979 			| alter_table_cmds ',' alter_table_cmd	{ $$ = lappend($1, $3); }
1980 		;
1981 
1982 partition_cmd:
1983 			/* ALTER TABLE <name> ATTACH PARTITION <table_name> FOR VALUES */
1984 			ATTACH PARTITION qualified_name ForValues
1985 				{
1986 					AlterTableCmd *n = makeNode(AlterTableCmd);
1987 					PartitionCmd *cmd = makeNode(PartitionCmd);
1988 
1989 					n->subtype = AT_AttachPartition;
1990 					cmd->name = $3;
1991 					cmd->bound = $4;
1992 					n->def = (Node *) cmd;
1993 
1994 					$$ = (Node *) n;
1995 				}
1996 			/* ALTER TABLE <name> DETACH PARTITION <partition_name> */
1997 			| DETACH PARTITION qualified_name
1998 				{
1999 					AlterTableCmd *n = makeNode(AlterTableCmd);
2000 					PartitionCmd *cmd = makeNode(PartitionCmd);
2001 
2002 					n->subtype = AT_DetachPartition;
2003 					cmd->name = $3;
2004 					cmd->bound = NULL;
2005 					n->def = (Node *) cmd;
2006 
2007 					$$ = (Node *) n;
2008 				}
2009 		;
2010 
2011 alter_table_cmd:
2012 			/* ALTER TABLE <name> ADD <coldef> */
2013 			ADD_P columnDef
2014 				{
2015 					AlterTableCmd *n = makeNode(AlterTableCmd);
2016 					n->subtype = AT_AddColumn;
2017 					n->def = $2;
2018 					n->missing_ok = false;
2019 					$$ = (Node *)n;
2020 				}
2021 			/* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
2022 			| ADD_P IF_P NOT EXISTS columnDef
2023 				{
2024 					AlterTableCmd *n = makeNode(AlterTableCmd);
2025 					n->subtype = AT_AddColumn;
2026 					n->def = $5;
2027 					n->missing_ok = true;
2028 					$$ = (Node *)n;
2029 				}
2030 			/* ALTER TABLE <name> ADD COLUMN <coldef> */
2031 			| ADD_P COLUMN columnDef
2032 				{
2033 					AlterTableCmd *n = makeNode(AlterTableCmd);
2034 					n->subtype = AT_AddColumn;
2035 					n->def = $3;
2036 					n->missing_ok = false;
2037 					$$ = (Node *)n;
2038 				}
2039 			/* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
2040 			| ADD_P COLUMN IF_P NOT EXISTS columnDef
2041 				{
2042 					AlterTableCmd *n = makeNode(AlterTableCmd);
2043 					n->subtype = AT_AddColumn;
2044 					n->def = $6;
2045 					n->missing_ok = true;
2046 					$$ = (Node *)n;
2047 				}
2048 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
2049 			| ALTER opt_column ColId alter_column_default
2050 				{
2051 					AlterTableCmd *n = makeNode(AlterTableCmd);
2052 					n->subtype = AT_ColumnDefault;
2053 					n->name = $3;
2054 					n->def = $4;
2055 					$$ = (Node *)n;
2056 				}
2057 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
2058 			| ALTER opt_column ColId DROP NOT NULL_P
2059 				{
2060 					AlterTableCmd *n = makeNode(AlterTableCmd);
2061 					n->subtype = AT_DropNotNull;
2062 					n->name = $3;
2063 					$$ = (Node *)n;
2064 				}
2065 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
2066 			| ALTER opt_column ColId SET NOT NULL_P
2067 				{
2068 					AlterTableCmd *n = makeNode(AlterTableCmd);
2069 					n->subtype = AT_SetNotNull;
2070 					n->name = $3;
2071 					$$ = (Node *)n;
2072 				}
2073 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
2074 			| ALTER opt_column ColId SET STATISTICS SignedIconst
2075 				{
2076 					AlterTableCmd *n = makeNode(AlterTableCmd);
2077 					n->subtype = AT_SetStatistics;
2078 					n->name = $3;
2079 					n->def = (Node *) makeInteger($6);
2080 					$$ = (Node *)n;
2081 				}
2082 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
2083 			| ALTER opt_column ColId SET reloptions
2084 				{
2085 					AlterTableCmd *n = makeNode(AlterTableCmd);
2086 					n->subtype = AT_SetOptions;
2087 					n->name = $3;
2088 					n->def = (Node *) $5;
2089 					$$ = (Node *)n;
2090 				}
2091 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
2092 			| ALTER opt_column ColId RESET reloptions
2093 				{
2094 					AlterTableCmd *n = makeNode(AlterTableCmd);
2095 					n->subtype = AT_ResetOptions;
2096 					n->name = $3;
2097 					n->def = (Node *) $5;
2098 					$$ = (Node *)n;
2099 				}
2100 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
2101 			| ALTER opt_column ColId SET STORAGE ColId
2102 				{
2103 					AlterTableCmd *n = makeNode(AlterTableCmd);
2104 					n->subtype = AT_SetStorage;
2105 					n->name = $3;
2106 					n->def = (Node *) makeString($6);
2107 					$$ = (Node *)n;
2108 				}
2109 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> ADD GENERATED ... AS IDENTITY ... */
2110 			| ALTER opt_column ColId ADD_P GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
2111 				{
2112 					AlterTableCmd *n = makeNode(AlterTableCmd);
2113 					Constraint *c = makeNode(Constraint);
2114 
2115 					c->contype = CONSTR_IDENTITY;
2116 					c->generated_when = $6;
2117 					c->options = $9;
2118 					c->location = @5;
2119 
2120 					n->subtype = AT_AddIdentity;
2121 					n->name = $3;
2122 					n->def = (Node *) c;
2123 
2124 					$$ = (Node *)n;
2125 				}
2126 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET <sequence options>/RESET */
2127 			| ALTER opt_column ColId alter_identity_column_option_list
2128 				{
2129 					AlterTableCmd *n = makeNode(AlterTableCmd);
2130 					n->subtype = AT_SetIdentity;
2131 					n->name = $3;
2132 					n->def = (Node *) $4;
2133 					$$ = (Node *)n;
2134 				}
2135 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY */
2136 			| ALTER opt_column ColId DROP IDENTITY_P
2137 				{
2138 					AlterTableCmd *n = makeNode(AlterTableCmd);
2139 					n->subtype = AT_DropIdentity;
2140 					n->name = $3;
2141 					n->missing_ok = false;
2142 					$$ = (Node *)n;
2143 				}
2144 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY IF EXISTS */
2145 			| ALTER opt_column ColId DROP IDENTITY_P IF_P EXISTS
2146 				{
2147 					AlterTableCmd *n = makeNode(AlterTableCmd);
2148 					n->subtype = AT_DropIdentity;
2149 					n->name = $3;
2150 					n->missing_ok = true;
2151 					$$ = (Node *)n;
2152 				}
2153 			/* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
2154 			| DROP opt_column IF_P EXISTS ColId opt_drop_behavior
2155 				{
2156 					AlterTableCmd *n = makeNode(AlterTableCmd);
2157 					n->subtype = AT_DropColumn;
2158 					n->name = $5;
2159 					n->behavior = $6;
2160 					n->missing_ok = TRUE;
2161 					$$ = (Node *)n;
2162 				}
2163 			/* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
2164 			| DROP opt_column ColId opt_drop_behavior
2165 				{
2166 					AlterTableCmd *n = makeNode(AlterTableCmd);
2167 					n->subtype = AT_DropColumn;
2168 					n->name = $3;
2169 					n->behavior = $4;
2170 					n->missing_ok = FALSE;
2171 					$$ = (Node *)n;
2172 				}
2173 			/*
2174 			 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
2175 			 *		[ USING <expression> ]
2176 			 */
2177 			| ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
2178 				{
2179 					AlterTableCmd *n = makeNode(AlterTableCmd);
2180 					ColumnDef *def = makeNode(ColumnDef);
2181 					n->subtype = AT_AlterColumnType;
2182 					n->name = $3;
2183 					n->def = (Node *) def;
2184 					/* We only use these fields of the ColumnDef node */
2185 					def->typeName = $6;
2186 					def->collClause = (CollateClause *) $7;
2187 					def->raw_default = $8;
2188 					def->location = @3;
2189 					$$ = (Node *)n;
2190 				}
2191 			/* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
2192 			| ALTER opt_column ColId alter_generic_options
2193 				{
2194 					AlterTableCmd *n = makeNode(AlterTableCmd);
2195 					n->subtype = AT_AlterColumnGenericOptions;
2196 					n->name = $3;
2197 					n->def = (Node *) $4;
2198 					$$ = (Node *)n;
2199 				}
2200 			/* ALTER TABLE <name> ADD CONSTRAINT ... */
2201 			| ADD_P TableConstraint
2202 				{
2203 					AlterTableCmd *n = makeNode(AlterTableCmd);
2204 					n->subtype = AT_AddConstraint;
2205 					n->def = $2;
2206 					$$ = (Node *)n;
2207 				}
2208 			/* ALTER TABLE <name> ALTER CONSTRAINT ... */
2209 			| ALTER CONSTRAINT name ConstraintAttributeSpec
2210 				{
2211 					AlterTableCmd *n = makeNode(AlterTableCmd);
2212 					Constraint *c = makeNode(Constraint);
2213 					n->subtype = AT_AlterConstraint;
2214 					n->def = (Node *) c;
2215 					c->contype = CONSTR_FOREIGN; /* others not supported, yet */
2216 					c->conname = $3;
2217 					processCASbits($4, @4, "ALTER CONSTRAINT statement",
2218 									&c->deferrable,
2219 									&c->initdeferred,
2220 									NULL, NULL, yyscanner);
2221 					$$ = (Node *)n;
2222 				}
2223 			/* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
2224 			| VALIDATE CONSTRAINT name
2225 				{
2226 					AlterTableCmd *n = makeNode(AlterTableCmd);
2227 					n->subtype = AT_ValidateConstraint;
2228 					n->name = $3;
2229 					$$ = (Node *)n;
2230 				}
2231 			/* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
2232 			| DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
2233 				{
2234 					AlterTableCmd *n = makeNode(AlterTableCmd);
2235 					n->subtype = AT_DropConstraint;
2236 					n->name = $5;
2237 					n->behavior = $6;
2238 					n->missing_ok = TRUE;
2239 					$$ = (Node *)n;
2240 				}
2241 			/* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
2242 			| DROP CONSTRAINT name opt_drop_behavior
2243 				{
2244 					AlterTableCmd *n = makeNode(AlterTableCmd);
2245 					n->subtype = AT_DropConstraint;
2246 					n->name = $3;
2247 					n->behavior = $4;
2248 					n->missing_ok = FALSE;
2249 					$$ = (Node *)n;
2250 				}
2251 			/* ALTER TABLE <name> SET WITH OIDS  */
2252 			| SET WITH OIDS
2253 				{
2254 					AlterTableCmd *n = makeNode(AlterTableCmd);
2255 					n->subtype = AT_AddOids;
2256 					$$ = (Node *)n;
2257 				}
2258 			/* ALTER TABLE <name> SET WITHOUT OIDS  */
2259 			| SET WITHOUT OIDS
2260 				{
2261 					AlterTableCmd *n = makeNode(AlterTableCmd);
2262 					n->subtype = AT_DropOids;
2263 					$$ = (Node *)n;
2264 				}
2265 			/* ALTER TABLE <name> CLUSTER ON <indexname> */
2266 			| CLUSTER ON name
2267 				{
2268 					AlterTableCmd *n = makeNode(AlterTableCmd);
2269 					n->subtype = AT_ClusterOn;
2270 					n->name = $3;
2271 					$$ = (Node *)n;
2272 				}
2273 			/* ALTER TABLE <name> SET WITHOUT CLUSTER */
2274 			| SET WITHOUT CLUSTER
2275 				{
2276 					AlterTableCmd *n = makeNode(AlterTableCmd);
2277 					n->subtype = AT_DropCluster;
2278 					n->name = NULL;
2279 					$$ = (Node *)n;
2280 				}
2281 			/* ALTER TABLE <name> SET LOGGED  */
2282 			| SET LOGGED
2283 				{
2284 					AlterTableCmd *n = makeNode(AlterTableCmd);
2285 					n->subtype = AT_SetLogged;
2286 					$$ = (Node *)n;
2287 				}
2288 			/* ALTER TABLE <name> SET UNLOGGED  */
2289 			| SET UNLOGGED
2290 				{
2291 					AlterTableCmd *n = makeNode(AlterTableCmd);
2292 					n->subtype = AT_SetUnLogged;
2293 					$$ = (Node *)n;
2294 				}
2295 			/* ALTER TABLE <name> ENABLE TRIGGER <trig> */
2296 			| ENABLE_P TRIGGER name
2297 				{
2298 					AlterTableCmd *n = makeNode(AlterTableCmd);
2299 					n->subtype = AT_EnableTrig;
2300 					n->name = $3;
2301 					$$ = (Node *)n;
2302 				}
2303 			/* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
2304 			| ENABLE_P ALWAYS TRIGGER name
2305 				{
2306 					AlterTableCmd *n = makeNode(AlterTableCmd);
2307 					n->subtype = AT_EnableAlwaysTrig;
2308 					n->name = $4;
2309 					$$ = (Node *)n;
2310 				}
2311 			/* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
2312 			| ENABLE_P REPLICA TRIGGER name
2313 				{
2314 					AlterTableCmd *n = makeNode(AlterTableCmd);
2315 					n->subtype = AT_EnableReplicaTrig;
2316 					n->name = $4;
2317 					$$ = (Node *)n;
2318 				}
2319 			/* ALTER TABLE <name> ENABLE TRIGGER ALL */
2320 			| ENABLE_P TRIGGER ALL
2321 				{
2322 					AlterTableCmd *n = makeNode(AlterTableCmd);
2323 					n->subtype = AT_EnableTrigAll;
2324 					$$ = (Node *)n;
2325 				}
2326 			/* ALTER TABLE <name> ENABLE TRIGGER USER */
2327 			| ENABLE_P TRIGGER USER
2328 				{
2329 					AlterTableCmd *n = makeNode(AlterTableCmd);
2330 					n->subtype = AT_EnableTrigUser;
2331 					$$ = (Node *)n;
2332 				}
2333 			/* ALTER TABLE <name> DISABLE TRIGGER <trig> */
2334 			| DISABLE_P TRIGGER name
2335 				{
2336 					AlterTableCmd *n = makeNode(AlterTableCmd);
2337 					n->subtype = AT_DisableTrig;
2338 					n->name = $3;
2339 					$$ = (Node *)n;
2340 				}
2341 			/* ALTER TABLE <name> DISABLE TRIGGER ALL */
2342 			| DISABLE_P TRIGGER ALL
2343 				{
2344 					AlterTableCmd *n = makeNode(AlterTableCmd);
2345 					n->subtype = AT_DisableTrigAll;
2346 					$$ = (Node *)n;
2347 				}
2348 			/* ALTER TABLE <name> DISABLE TRIGGER USER */
2349 			| DISABLE_P TRIGGER USER
2350 				{
2351 					AlterTableCmd *n = makeNode(AlterTableCmd);
2352 					n->subtype = AT_DisableTrigUser;
2353 					$$ = (Node *)n;
2354 				}
2355 			/* ALTER TABLE <name> ENABLE RULE <rule> */
2356 			| ENABLE_P RULE name
2357 				{
2358 					AlterTableCmd *n = makeNode(AlterTableCmd);
2359 					n->subtype = AT_EnableRule;
2360 					n->name = $3;
2361 					$$ = (Node *)n;
2362 				}
2363 			/* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
2364 			| ENABLE_P ALWAYS RULE name
2365 				{
2366 					AlterTableCmd *n = makeNode(AlterTableCmd);
2367 					n->subtype = AT_EnableAlwaysRule;
2368 					n->name = $4;
2369 					$$ = (Node *)n;
2370 				}
2371 			/* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
2372 			| ENABLE_P REPLICA RULE name
2373 				{
2374 					AlterTableCmd *n = makeNode(AlterTableCmd);
2375 					n->subtype = AT_EnableReplicaRule;
2376 					n->name = $4;
2377 					$$ = (Node *)n;
2378 				}
2379 			/* ALTER TABLE <name> DISABLE RULE <rule> */
2380 			| DISABLE_P RULE name
2381 				{
2382 					AlterTableCmd *n = makeNode(AlterTableCmd);
2383 					n->subtype = AT_DisableRule;
2384 					n->name = $3;
2385 					$$ = (Node *)n;
2386 				}
2387 			/* ALTER TABLE <name> INHERIT <parent> */
2388 			| INHERIT qualified_name
2389 				{
2390 					AlterTableCmd *n = makeNode(AlterTableCmd);
2391 					n->subtype = AT_AddInherit;
2392 					n->def = (Node *) $2;
2393 					$$ = (Node *)n;
2394 				}
2395 			/* ALTER TABLE <name> NO INHERIT <parent> */
2396 			| NO INHERIT qualified_name
2397 				{
2398 					AlterTableCmd *n = makeNode(AlterTableCmd);
2399 					n->subtype = AT_DropInherit;
2400 					n->def = (Node *) $3;
2401 					$$ = (Node *)n;
2402 				}
2403 			/* ALTER TABLE <name> OF <type_name> */
2404 			| OF any_name
2405 				{
2406 					AlterTableCmd *n = makeNode(AlterTableCmd);
2407 					TypeName *def = makeTypeNameFromNameList($2);
2408 					def->location = @2;
2409 					n->subtype = AT_AddOf;
2410 					n->def = (Node *) def;
2411 					$$ = (Node *)n;
2412 				}
2413 			/* ALTER TABLE <name> NOT OF */
2414 			| NOT OF
2415 				{
2416 					AlterTableCmd *n = makeNode(AlterTableCmd);
2417 					n->subtype = AT_DropOf;
2418 					$$ = (Node *)n;
2419 				}
2420 			/* ALTER TABLE <name> OWNER TO RoleSpec */
2421 			| OWNER TO RoleSpec
2422 				{
2423 					AlterTableCmd *n = makeNode(AlterTableCmd);
2424 					n->subtype = AT_ChangeOwner;
2425 					n->newowner = $3;
2426 					$$ = (Node *)n;
2427 				}
2428 			/* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
2429 			| SET TABLESPACE name
2430 				{
2431 					AlterTableCmd *n = makeNode(AlterTableCmd);
2432 					n->subtype = AT_SetTableSpace;
2433 					n->name = $3;
2434 					$$ = (Node *)n;
2435 				}
2436 			/* ALTER TABLE <name> SET (...) */
2437 			| SET reloptions
2438 				{
2439 					AlterTableCmd *n = makeNode(AlterTableCmd);
2440 					n->subtype = AT_SetRelOptions;
2441 					n->def = (Node *)$2;
2442 					$$ = (Node *)n;
2443 				}
2444 			/* ALTER TABLE <name> RESET (...) */
2445 			| RESET reloptions
2446 				{
2447 					AlterTableCmd *n = makeNode(AlterTableCmd);
2448 					n->subtype = AT_ResetRelOptions;
2449 					n->def = (Node *)$2;
2450 					$$ = (Node *)n;
2451 				}
2452 			/* ALTER TABLE <name> REPLICA IDENTITY  */
2453 			| REPLICA IDENTITY_P replica_identity
2454 				{
2455 					AlterTableCmd *n = makeNode(AlterTableCmd);
2456 					n->subtype = AT_ReplicaIdentity;
2457 					n->def = $3;
2458 					$$ = (Node *)n;
2459 				}
2460 			/* ALTER TABLE <name> ENABLE ROW LEVEL SECURITY */
2461 			| ENABLE_P ROW LEVEL SECURITY
2462 				{
2463 					AlterTableCmd *n = makeNode(AlterTableCmd);
2464 					n->subtype = AT_EnableRowSecurity;
2465 					$$ = (Node *)n;
2466 				}
2467 			/* ALTER TABLE <name> DISABLE ROW LEVEL SECURITY */
2468 			| DISABLE_P ROW LEVEL SECURITY
2469 				{
2470 					AlterTableCmd *n = makeNode(AlterTableCmd);
2471 					n->subtype = AT_DisableRowSecurity;
2472 					$$ = (Node *)n;
2473 				}
2474 			/* ALTER TABLE <name> FORCE ROW LEVEL SECURITY */
2475 			| FORCE ROW LEVEL SECURITY
2476 				{
2477 					AlterTableCmd *n = makeNode(AlterTableCmd);
2478 					n->subtype = AT_ForceRowSecurity;
2479 					$$ = (Node *)n;
2480 				}
2481 			/* ALTER TABLE <name> NO FORCE ROW LEVEL SECURITY */
2482 			| NO FORCE ROW LEVEL SECURITY
2483 				{
2484 					AlterTableCmd *n = makeNode(AlterTableCmd);
2485 					n->subtype = AT_NoForceRowSecurity;
2486 					$$ = (Node *)n;
2487 				}
2488 			| alter_generic_options
2489 				{
2490 					AlterTableCmd *n = makeNode(AlterTableCmd);
2491 					n->subtype = AT_GenericOptions;
2492 					n->def = (Node *)$1;
2493 					$$ = (Node *) n;
2494 				}
2495 		;
2496 
2497 alter_column_default:
2498 			SET DEFAULT a_expr			{ $$ = $3; }
2499 			| DROP DEFAULT				{ $$ = NULL; }
2500 		;
2501 
2502 opt_drop_behavior:
2503 			CASCADE						{ $$ = DROP_CASCADE; }
2504 			| RESTRICT					{ $$ = DROP_RESTRICT; }
2505 			| /* EMPTY */				{ $$ = DROP_RESTRICT; /* default */ }
2506 		;
2507 
2508 opt_collate_clause:
2509 			COLLATE any_name
2510 				{
2511 					CollateClause *n = makeNode(CollateClause);
2512 					n->arg = NULL;
2513 					n->collname = $2;
2514 					n->location = @1;
2515 					$$ = (Node *) n;
2516 				}
2517 			| /* EMPTY */				{ $$ = NULL; }
2518 		;
2519 
2520 alter_using:
2521 			USING a_expr				{ $$ = $2; }
2522 			| /* EMPTY */				{ $$ = NULL; }
2523 		;
2524 
2525 replica_identity:
2526 			NOTHING
2527 				{
2528 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2529 					n->identity_type = REPLICA_IDENTITY_NOTHING;
2530 					n->name = NULL;
2531 					$$ = (Node *) n;
2532 				}
2533 			| FULL
2534 				{
2535 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2536 					n->identity_type = REPLICA_IDENTITY_FULL;
2537 					n->name = NULL;
2538 					$$ = (Node *) n;
2539 				}
2540 			| DEFAULT
2541 				{
2542 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2543 					n->identity_type = REPLICA_IDENTITY_DEFAULT;
2544 					n->name = NULL;
2545 					$$ = (Node *) n;
2546 				}
2547 			| USING INDEX name
2548 				{
2549 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2550 					n->identity_type = REPLICA_IDENTITY_INDEX;
2551 					n->name = $3;
2552 					$$ = (Node *) n;
2553 				}
2554 ;
2555 
2556 reloptions:
2557 			'(' reloption_list ')'					{ $$ = $2; }
2558 		;
2559 
2560 opt_reloptions:		WITH reloptions					{ $$ = $2; }
2561 			 |		/* EMPTY */						{ $$ = NIL; }
2562 		;
2563 
2564 reloption_list:
2565 			reloption_elem							{ $$ = list_make1($1); }
2566 			| reloption_list ',' reloption_elem		{ $$ = lappend($1, $3); }
2567 		;
2568 
2569 /* This should match def_elem and also allow qualified names */
2570 reloption_elem:
2571 			ColLabel '=' def_arg
2572 				{
2573 					$$ = makeDefElem($1, (Node *) $3, @1);
2574 				}
2575 			| ColLabel
2576 				{
2577 					$$ = makeDefElem($1, NULL, @1);
2578 				}
2579 			| ColLabel '.' ColLabel '=' def_arg
2580 				{
2581 					$$ = makeDefElemExtended($1, $3, (Node *) $5,
2582 											 DEFELEM_UNSPEC, @1);
2583 				}
2584 			| ColLabel '.' ColLabel
2585 				{
2586 					$$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC, @1);
2587 				}
2588 		;
2589 
2590 alter_identity_column_option_list:
2591 			alter_identity_column_option
2592 				{ $$ = list_make1($1); }
2593 			| alter_identity_column_option_list alter_identity_column_option
2594 				{ $$ = lappend($1, $2); }
2595 		;
2596 
2597 alter_identity_column_option:
2598 			RESTART
2599 				{
2600 					$$ = makeDefElem("restart", NULL, @1);
2601 				}
2602 			| RESTART opt_with NumericOnly
2603 				{
2604 					$$ = makeDefElem("restart", (Node *)$3, @1);
2605 				}
2606 			| SET SeqOptElem
2607 				{
2608 					if (strcmp($2->defname, "as") == 0 ||
2609 						strcmp($2->defname, "restart") == 0 ||
2610 						strcmp($2->defname, "owned_by") == 0)
2611 						ereport(ERROR,
2612 								(errcode(ERRCODE_SYNTAX_ERROR),
2613 								 errmsg("sequence option \"%s\" not supported here", $2->defname),
2614 								 parser_errposition(@2)));
2615 					$$ = $2;
2616 				}
2617 			| SET GENERATED generated_when
2618 				{
2619 					$$ = makeDefElem("generated", (Node *) makeInteger($3), @1);
2620 				}
2621 		;
2622 
2623 ForValues:
2624 			/* a LIST partition */
2625 			FOR VALUES IN_P '(' partbound_datum_list ')'
2626 				{
2627 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2628 
2629 					n->strategy = PARTITION_STRATEGY_LIST;
2630 					n->listdatums = $5;
2631 					n->location = @3;
2632 
2633 					$$ = n;
2634 				}
2635 
2636 			/* a RANGE partition */
2637 			| FOR VALUES FROM '(' range_datum_list ')' TO '(' range_datum_list ')'
2638 				{
2639 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2640 
2641 					n->strategy = PARTITION_STRATEGY_RANGE;
2642 					n->lowerdatums = $5;
2643 					n->upperdatums = $9;
2644 					n->location = @3;
2645 
2646 					$$ = n;
2647 				}
2648 		;
2649 
2650 partbound_datum:
2651 			Sconst			{ $$ = makeStringConst($1, @1); }
2652 			| NumericOnly	{ $$ = makeAConst($1, @1); }
2653 			| TRUE_P		{ $$ = makeStringConst(pstrdup("true"), @1); }
2654 			| FALSE_P		{ $$ = makeStringConst(pstrdup("false"), @1); }
2655 			| NULL_P		{ $$ = makeNullAConst(@1); }
2656 		;
2657 
2658 partbound_datum_list:
2659 			partbound_datum						{ $$ = list_make1($1); }
2660 			| partbound_datum_list ',' partbound_datum
2661 												{ $$ = lappend($1, $3); }
2662 		;
2663 
2664 range_datum_list:
2665 			PartitionRangeDatum					{ $$ = list_make1($1); }
2666 			| range_datum_list ',' PartitionRangeDatum
2667 												{ $$ = lappend($1, $3); }
2668 		;
2669 
2670 PartitionRangeDatum:
2671 			MINVALUE
2672 				{
2673 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2674 
2675 					n->kind = PARTITION_RANGE_DATUM_MINVALUE;
2676 					n->value = NULL;
2677 					n->location = @1;
2678 
2679 					$$ = (Node *) n;
2680 				}
2681 			| MAXVALUE
2682 				{
2683 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2684 
2685 					n->kind = PARTITION_RANGE_DATUM_MAXVALUE;
2686 					n->value = NULL;
2687 					n->location = @1;
2688 
2689 					$$ = (Node *) n;
2690 				}
2691 			| partbound_datum
2692 				{
2693 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2694 
2695 					n->kind = PARTITION_RANGE_DATUM_VALUE;
2696 					n->value = $1;
2697 					n->location = @1;
2698 
2699 					$$ = (Node *) n;
2700 				}
2701 		;
2702 
2703 /*****************************************************************************
2704  *
2705  *	ALTER TYPE
2706  *
2707  * really variants of the ALTER TABLE subcommands with different spellings
2708  *****************************************************************************/
2709 
2710 AlterCompositeTypeStmt:
2711 			ALTER TYPE_P any_name alter_type_cmds
2712 				{
2713 					AlterTableStmt *n = makeNode(AlterTableStmt);
2714 
2715 					/* can't use qualified_name, sigh */
2716 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
2717 					n->cmds = $4;
2718 					n->relkind = OBJECT_TYPE;
2719 					$$ = (Node *)n;
2720 				}
2721 			;
2722 
2723 alter_type_cmds:
2724 			alter_type_cmd							{ $$ = list_make1($1); }
2725 			| alter_type_cmds ',' alter_type_cmd	{ $$ = lappend($1, $3); }
2726 		;
2727 
2728 alter_type_cmd:
2729 			/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
2730 			ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
2731 				{
2732 					AlterTableCmd *n = makeNode(AlterTableCmd);
2733 					n->subtype = AT_AddColumn;
2734 					n->def = $3;
2735 					n->behavior = $4;
2736 					$$ = (Node *)n;
2737 				}
2738 			/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
2739 			| DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
2740 				{
2741 					AlterTableCmd *n = makeNode(AlterTableCmd);
2742 					n->subtype = AT_DropColumn;
2743 					n->name = $5;
2744 					n->behavior = $6;
2745 					n->missing_ok = TRUE;
2746 					$$ = (Node *)n;
2747 				}
2748 			/* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
2749 			| DROP ATTRIBUTE ColId opt_drop_behavior
2750 				{
2751 					AlterTableCmd *n = makeNode(AlterTableCmd);
2752 					n->subtype = AT_DropColumn;
2753 					n->name = $3;
2754 					n->behavior = $4;
2755 					n->missing_ok = FALSE;
2756 					$$ = (Node *)n;
2757 				}
2758 			/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
2759 			| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
2760 				{
2761 					AlterTableCmd *n = makeNode(AlterTableCmd);
2762 					ColumnDef *def = makeNode(ColumnDef);
2763 					n->subtype = AT_AlterColumnType;
2764 					n->name = $3;
2765 					n->def = (Node *) def;
2766 					n->behavior = $8;
2767 					/* We only use these fields of the ColumnDef node */
2768 					def->typeName = $6;
2769 					def->collClause = (CollateClause *) $7;
2770 					def->raw_default = NULL;
2771 					def->location = @3;
2772 					$$ = (Node *)n;
2773 				}
2774 		;
2775 
2776 
2777 /*****************************************************************************
2778  *
2779  *		QUERY :
2780  *				close <portalname>
2781  *
2782  *****************************************************************************/
2783 
2784 ClosePortalStmt:
2785 			CLOSE cursor_name
2786 				{
2787 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2788 					n->portalname = $2;
2789 					$$ = (Node *)n;
2790 				}
2791 			| CLOSE ALL
2792 				{
2793 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2794 					n->portalname = NULL;
2795 					$$ = (Node *)n;
2796 				}
2797 		;
2798 
2799 
2800 /*****************************************************************************
2801  *
2802  *		QUERY :
2803  *				COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
2804  *				COPY ( query ) TO file	[WITH] [(options)]
2805  *
2806  *				where 'query' can be one of:
2807  *				{ SELECT | UPDATE | INSERT | DELETE }
2808  *
2809  *				and 'file' can be one of:
2810  *				{ PROGRAM 'command' | STDIN | STDOUT | 'filename' }
2811  *
2812  *				In the preferred syntax the options are comma-separated
2813  *				and use generic identifiers instead of keywords.  The pre-9.0
2814  *				syntax had a hard-wired, space-separated set of options.
2815  *
2816  *				Really old syntax, from versions 7.2 and prior:
2817  *				COPY [ BINARY ] table [ WITH OIDS ] FROM/TO file
2818  *					[ [ USING ] DELIMITERS 'delimiter' ] ]
2819  *					[ WITH NULL AS 'null string' ]
2820  *				This option placement is not supported with COPY (query...).
2821  *
2822  *****************************************************************************/
2823 
2824 CopyStmt:	COPY opt_binary qualified_name opt_column_list opt_oids
2825 			copy_from opt_program copy_file_name copy_delimiter opt_with copy_options
2826 				{
2827 					CopyStmt *n = makeNode(CopyStmt);
2828 					n->relation = $3;
2829 					n->query = NULL;
2830 					n->attlist = $4;
2831 					n->is_from = $6;
2832 					n->is_program = $7;
2833 					n->filename = $8;
2834 
2835 					if (n->is_program && n->filename == NULL)
2836 						ereport(ERROR,
2837 								(errcode(ERRCODE_SYNTAX_ERROR),
2838 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2839 								 parser_errposition(@8)));
2840 
2841 					n->options = NIL;
2842 					/* Concatenate user-supplied flags */
2843 					if ($2)
2844 						n->options = lappend(n->options, $2);
2845 					if ($5)
2846 						n->options = lappend(n->options, $5);
2847 					if ($9)
2848 						n->options = lappend(n->options, $9);
2849 					if ($11)
2850 						n->options = list_concat(n->options, $11);
2851 					$$ = (Node *)n;
2852 				}
2853 			| COPY '(' PreparableStmt ')' TO opt_program copy_file_name opt_with copy_options
2854 				{
2855 					CopyStmt *n = makeNode(CopyStmt);
2856 					n->relation = NULL;
2857 					n->query = $3;
2858 					n->attlist = NIL;
2859 					n->is_from = false;
2860 					n->is_program = $6;
2861 					n->filename = $7;
2862 					n->options = $9;
2863 
2864 					if (n->is_program && n->filename == NULL)
2865 						ereport(ERROR,
2866 								(errcode(ERRCODE_SYNTAX_ERROR),
2867 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2868 								 parser_errposition(@5)));
2869 
2870 					$$ = (Node *)n;
2871 				}
2872 		;
2873 
2874 copy_from:
2875 			FROM									{ $$ = TRUE; }
2876 			| TO									{ $$ = FALSE; }
2877 		;
2878 
2879 opt_program:
2880 			PROGRAM									{ $$ = TRUE; }
2881 			| /* EMPTY */							{ $$ = FALSE; }
2882 		;
2883 
2884 /*
2885  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
2886  * used depends on the direction. (It really doesn't make sense to copy from
2887  * stdout. We silently correct the "typo".)		 - AY 9/94
2888  */
2889 copy_file_name:
2890 			Sconst									{ $$ = $1; }
2891 			| STDIN									{ $$ = NULL; }
2892 			| STDOUT								{ $$ = NULL; }
2893 		;
2894 
2895 copy_options: copy_opt_list							{ $$ = $1; }
2896 			| '(' copy_generic_opt_list ')'			{ $$ = $2; }
2897 		;
2898 
2899 /* old COPY option syntax */
2900 copy_opt_list:
2901 			copy_opt_list copy_opt_item				{ $$ = lappend($1, $2); }
2902 			| /* EMPTY */							{ $$ = NIL; }
2903 		;
2904 
2905 copy_opt_item:
2906 			BINARY
2907 				{
2908 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
2909 				}
2910 			| OIDS
2911 				{
2912 					$$ = makeDefElem("oids", (Node *)makeInteger(TRUE), @1);
2913 				}
2914 			| FREEZE
2915 				{
2916 					$$ = makeDefElem("freeze", (Node *)makeInteger(TRUE), @1);
2917 				}
2918 			| DELIMITER opt_as Sconst
2919 				{
2920 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @1);
2921 				}
2922 			| NULL_P opt_as Sconst
2923 				{
2924 					$$ = makeDefElem("null", (Node *)makeString($3), @1);
2925 				}
2926 			| CSV
2927 				{
2928 					$$ = makeDefElem("format", (Node *)makeString("csv"), @1);
2929 				}
2930 			| HEADER_P
2931 				{
2932 					$$ = makeDefElem("header", (Node *)makeInteger(TRUE), @1);
2933 				}
2934 			| QUOTE opt_as Sconst
2935 				{
2936 					$$ = makeDefElem("quote", (Node *)makeString($3), @1);
2937 				}
2938 			| ESCAPE opt_as Sconst
2939 				{
2940 					$$ = makeDefElem("escape", (Node *)makeString($3), @1);
2941 				}
2942 			| FORCE QUOTE columnList
2943 				{
2944 					$$ = makeDefElem("force_quote", (Node *)$3, @1);
2945 				}
2946 			| FORCE QUOTE '*'
2947 				{
2948 					$$ = makeDefElem("force_quote", (Node *)makeNode(A_Star), @1);
2949 				}
2950 			| FORCE NOT NULL_P columnList
2951 				{
2952 					$$ = makeDefElem("force_not_null", (Node *)$4, @1);
2953 				}
2954 			| FORCE NULL_P columnList
2955 				{
2956 					$$ = makeDefElem("force_null", (Node *)$3, @1);
2957 				}
2958 			| ENCODING Sconst
2959 				{
2960 					$$ = makeDefElem("encoding", (Node *)makeString($2), @1);
2961 				}
2962 		;
2963 
2964 /* The following exist for backward compatibility with very old versions */
2965 
2966 opt_binary:
2967 			BINARY
2968 				{
2969 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
2970 				}
2971 			| /*EMPTY*/								{ $$ = NULL; }
2972 		;
2973 
2974 opt_oids:
2975 			WITH OIDS
2976 				{
2977 					$$ = makeDefElem("oids", (Node *)makeInteger(TRUE), @1);
2978 				}
2979 			| /*EMPTY*/								{ $$ = NULL; }
2980 		;
2981 
2982 copy_delimiter:
2983 			opt_using DELIMITERS Sconst
2984 				{
2985 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @2);
2986 				}
2987 			| /*EMPTY*/								{ $$ = NULL; }
2988 		;
2989 
2990 opt_using:
2991 			USING									{}
2992 			| /*EMPTY*/								{}
2993 		;
2994 
2995 /* new COPY option syntax */
2996 copy_generic_opt_list:
2997 			copy_generic_opt_elem
2998 				{
2999 					$$ = list_make1($1);
3000 				}
3001 			| copy_generic_opt_list ',' copy_generic_opt_elem
3002 				{
3003 					$$ = lappend($1, $3);
3004 				}
3005 		;
3006 
3007 copy_generic_opt_elem:
3008 			ColLabel copy_generic_opt_arg
3009 				{
3010 					$$ = makeDefElem($1, $2, @1);
3011 				}
3012 		;
3013 
3014 copy_generic_opt_arg:
3015 			opt_boolean_or_string			{ $$ = (Node *) makeString($1); }
3016 			| NumericOnly					{ $$ = (Node *) $1; }
3017 			| '*'							{ $$ = (Node *) makeNode(A_Star); }
3018 			| '(' copy_generic_opt_arg_list ')'		{ $$ = (Node *) $2; }
3019 			| /* EMPTY */					{ $$ = NULL; }
3020 		;
3021 
3022 copy_generic_opt_arg_list:
3023 			  copy_generic_opt_arg_list_item
3024 				{
3025 					$$ = list_make1($1);
3026 				}
3027 			| copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
3028 				{
3029 					$$ = lappend($1, $3);
3030 				}
3031 		;
3032 
3033 /* beware of emitting non-string list elements here; see commands/define.c */
3034 copy_generic_opt_arg_list_item:
3035 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
3036 		;
3037 
3038 
3039 /*****************************************************************************
3040  *
3041  *		QUERY :
3042  *				CREATE TABLE relname
3043  *
3044  *****************************************************************************/
3045 
3046 CreateStmt:	CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
3047 			OptInherit OptPartitionSpec OptWith OnCommitOption OptTableSpace
3048 				{
3049 					CreateStmt *n = makeNode(CreateStmt);
3050 					$4->relpersistence = $2;
3051 					n->relation = $4;
3052 					n->tableElts = $6;
3053 					n->inhRelations = $8;
3054 					n->partspec = $9;
3055 					n->ofTypename = NULL;
3056 					n->constraints = NIL;
3057 					n->options = $10;
3058 					n->oncommit = $11;
3059 					n->tablespacename = $12;
3060 					n->if_not_exists = false;
3061 					$$ = (Node *)n;
3062 				}
3063 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
3064 			OptTableElementList ')' OptInherit OptPartitionSpec OptWith
3065 			OnCommitOption OptTableSpace
3066 				{
3067 					CreateStmt *n = makeNode(CreateStmt);
3068 					$7->relpersistence = $2;
3069 					n->relation = $7;
3070 					n->tableElts = $9;
3071 					n->inhRelations = $11;
3072 					n->partspec = $12;
3073 					n->ofTypename = NULL;
3074 					n->constraints = NIL;
3075 					n->options = $13;
3076 					n->oncommit = $14;
3077 					n->tablespacename = $15;
3078 					n->if_not_exists = true;
3079 					$$ = (Node *)n;
3080 				}
3081 		| CREATE OptTemp TABLE qualified_name OF any_name
3082 			OptTypedTableElementList OptPartitionSpec OptWith OnCommitOption
3083 			OptTableSpace
3084 				{
3085 					CreateStmt *n = makeNode(CreateStmt);
3086 					$4->relpersistence = $2;
3087 					n->relation = $4;
3088 					n->tableElts = $7;
3089 					n->inhRelations = NIL;
3090 					n->partspec = $8;
3091 					n->ofTypename = makeTypeNameFromNameList($6);
3092 					n->ofTypename->location = @6;
3093 					n->constraints = NIL;
3094 					n->options = $9;
3095 					n->oncommit = $10;
3096 					n->tablespacename = $11;
3097 					n->if_not_exists = false;
3098 					$$ = (Node *)n;
3099 				}
3100 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
3101 			OptTypedTableElementList OptPartitionSpec OptWith OnCommitOption
3102 			OptTableSpace
3103 				{
3104 					CreateStmt *n = makeNode(CreateStmt);
3105 					$7->relpersistence = $2;
3106 					n->relation = $7;
3107 					n->tableElts = $10;
3108 					n->inhRelations = NIL;
3109 					n->partspec = $11;
3110 					n->ofTypename = makeTypeNameFromNameList($9);
3111 					n->ofTypename->location = @9;
3112 					n->constraints = NIL;
3113 					n->options = $12;
3114 					n->oncommit = $13;
3115 					n->tablespacename = $14;
3116 					n->if_not_exists = true;
3117 					$$ = (Node *)n;
3118 				}
3119 		| CREATE OptTemp TABLE qualified_name PARTITION OF qualified_name
3120 			OptTypedTableElementList ForValues OptPartitionSpec OptWith
3121 			OnCommitOption OptTableSpace
3122 				{
3123 					CreateStmt *n = makeNode(CreateStmt);
3124 					$4->relpersistence = $2;
3125 					n->relation = $4;
3126 					n->tableElts = $8;
3127 					n->inhRelations = list_make1($7);
3128 					n->partbound = $9;
3129 					n->partspec = $10;
3130 					n->ofTypename = NULL;
3131 					n->constraints = NIL;
3132 					n->options = $11;
3133 					n->oncommit = $12;
3134 					n->tablespacename = $13;
3135 					n->if_not_exists = false;
3136 					$$ = (Node *)n;
3137 				}
3138 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name PARTITION OF
3139 			qualified_name OptTypedTableElementList ForValues OptPartitionSpec
3140 			OptWith OnCommitOption OptTableSpace
3141 				{
3142 					CreateStmt *n = makeNode(CreateStmt);
3143 					$7->relpersistence = $2;
3144 					n->relation = $7;
3145 					n->tableElts = $11;
3146 					n->inhRelations = list_make1($10);
3147 					n->partbound = $12;
3148 					n->partspec = $13;
3149 					n->ofTypename = NULL;
3150 					n->constraints = NIL;
3151 					n->options = $14;
3152 					n->oncommit = $15;
3153 					n->tablespacename = $16;
3154 					n->if_not_exists = true;
3155 					$$ = (Node *)n;
3156 				}
3157 		;
3158 
3159 /*
3160  * Redundancy here is needed to avoid shift/reduce conflicts,
3161  * since TEMP is not a reserved word.  See also OptTempTableName.
3162  *
3163  * NOTE: we accept both GLOBAL and LOCAL options.  They currently do nothing,
3164  * but future versions might consider GLOBAL to request SQL-spec-compliant
3165  * temp table behavior, so warn about that.  Since we have no modules the
3166  * LOCAL keyword is really meaningless; furthermore, some other products
3167  * implement LOCAL as meaning the same as our default temp table behavior,
3168  * so we'll probably continue to treat LOCAL as a noise word.
3169  */
3170 OptTemp:	TEMPORARY					{ $$ = RELPERSISTENCE_TEMP; }
3171 			| TEMP						{ $$ = RELPERSISTENCE_TEMP; }
3172 			| LOCAL TEMPORARY			{ $$ = RELPERSISTENCE_TEMP; }
3173 			| LOCAL TEMP				{ $$ = RELPERSISTENCE_TEMP; }
3174 			| GLOBAL TEMPORARY
3175 				{
3176 					ereport(WARNING,
3177 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3178 							 parser_errposition(@1)));
3179 					$$ = RELPERSISTENCE_TEMP;
3180 				}
3181 			| GLOBAL TEMP
3182 				{
3183 					ereport(WARNING,
3184 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3185 							 parser_errposition(@1)));
3186 					$$ = RELPERSISTENCE_TEMP;
3187 				}
3188 			| UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
3189 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
3190 		;
3191 
3192 OptTableElementList:
3193 			TableElementList					{ $$ = $1; }
3194 			| /*EMPTY*/							{ $$ = NIL; }
3195 		;
3196 
3197 OptTypedTableElementList:
3198 			'(' TypedTableElementList ')'		{ $$ = $2; }
3199 			| /*EMPTY*/							{ $$ = NIL; }
3200 		;
3201 
3202 TableElementList:
3203 			TableElement
3204 				{
3205 					$$ = list_make1($1);
3206 				}
3207 			| TableElementList ',' TableElement
3208 				{
3209 					$$ = lappend($1, $3);
3210 				}
3211 		;
3212 
3213 TypedTableElementList:
3214 			TypedTableElement
3215 				{
3216 					$$ = list_make1($1);
3217 				}
3218 			| TypedTableElementList ',' TypedTableElement
3219 				{
3220 					$$ = lappend($1, $3);
3221 				}
3222 		;
3223 
3224 TableElement:
3225 			columnDef							{ $$ = $1; }
3226 			| TableLikeClause					{ $$ = $1; }
3227 			| TableConstraint					{ $$ = $1; }
3228 		;
3229 
3230 TypedTableElement:
3231 			columnOptions						{ $$ = $1; }
3232 			| TableConstraint					{ $$ = $1; }
3233 		;
3234 
3235 columnDef:	ColId Typename create_generic_options ColQualList
3236 				{
3237 					ColumnDef *n = makeNode(ColumnDef);
3238 					n->colname = $1;
3239 					n->typeName = $2;
3240 					n->inhcount = 0;
3241 					n->is_local = true;
3242 					n->is_not_null = false;
3243 					n->is_from_type = false;
3244 					n->storage = 0;
3245 					n->raw_default = NULL;
3246 					n->cooked_default = NULL;
3247 					n->collOid = InvalidOid;
3248 					n->fdwoptions = $3;
3249 					SplitColQualList($4, &n->constraints, &n->collClause,
3250 									 yyscanner);
3251 					n->location = @1;
3252 					$$ = (Node *)n;
3253 				}
3254 		;
3255 
3256 columnOptions:	ColId ColQualList
3257 				{
3258 					ColumnDef *n = makeNode(ColumnDef);
3259 					n->colname = $1;
3260 					n->typeName = NULL;
3261 					n->inhcount = 0;
3262 					n->is_local = true;
3263 					n->is_not_null = false;
3264 					n->is_from_type = false;
3265 					n->storage = 0;
3266 					n->raw_default = NULL;
3267 					n->cooked_default = NULL;
3268 					n->collOid = InvalidOid;
3269 					SplitColQualList($2, &n->constraints, &n->collClause,
3270 									 yyscanner);
3271 					n->location = @1;
3272 					$$ = (Node *)n;
3273 				}
3274 				| ColId WITH OPTIONS ColQualList
3275 				{
3276 					ColumnDef *n = makeNode(ColumnDef);
3277 					n->colname = $1;
3278 					n->typeName = NULL;
3279 					n->inhcount = 0;
3280 					n->is_local = true;
3281 					n->is_not_null = false;
3282 					n->is_from_type = false;
3283 					n->storage = 0;
3284 					n->raw_default = NULL;
3285 					n->cooked_default = NULL;
3286 					n->collOid = InvalidOid;
3287 					SplitColQualList($4, &n->constraints, &n->collClause,
3288 									 yyscanner);
3289 					n->location = @1;
3290 					$$ = (Node *)n;
3291 				}
3292 		;
3293 
3294 ColQualList:
3295 			ColQualList ColConstraint				{ $$ = lappend($1, $2); }
3296 			| /*EMPTY*/								{ $$ = NIL; }
3297 		;
3298 
3299 ColConstraint:
3300 			CONSTRAINT name ColConstraintElem
3301 				{
3302 					Constraint *n = castNode(Constraint, $3);
3303 					n->conname = $2;
3304 					n->location = @1;
3305 					$$ = (Node *) n;
3306 				}
3307 			| ColConstraintElem						{ $$ = $1; }
3308 			| ConstraintAttr						{ $$ = $1; }
3309 			| COLLATE any_name
3310 				{
3311 					/*
3312 					 * Note: the CollateClause is momentarily included in
3313 					 * the list built by ColQualList, but we split it out
3314 					 * again in SplitColQualList.
3315 					 */
3316 					CollateClause *n = makeNode(CollateClause);
3317 					n->arg = NULL;
3318 					n->collname = $2;
3319 					n->location = @1;
3320 					$$ = (Node *) n;
3321 				}
3322 		;
3323 
3324 /* DEFAULT NULL is already the default for Postgres.
3325  * But define it here and carry it forward into the system
3326  * to make it explicit.
3327  * - thomas 1998-09-13
3328  *
3329  * WITH NULL and NULL are not SQL-standard syntax elements,
3330  * so leave them out. Use DEFAULT NULL to explicitly indicate
3331  * that a column may have that value. WITH NULL leads to
3332  * shift/reduce conflicts with WITH TIME ZONE anyway.
3333  * - thomas 1999-01-08
3334  *
3335  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
3336  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
3337  * or be part of a_expr NOT LIKE or similar constructs).
3338  */
3339 ColConstraintElem:
3340 			NOT NULL_P
3341 				{
3342 					Constraint *n = makeNode(Constraint);
3343 					n->contype = CONSTR_NOTNULL;
3344 					n->location = @1;
3345 					$$ = (Node *)n;
3346 				}
3347 			| NULL_P
3348 				{
3349 					Constraint *n = makeNode(Constraint);
3350 					n->contype = CONSTR_NULL;
3351 					n->location = @1;
3352 					$$ = (Node *)n;
3353 				}
3354 			| UNIQUE opt_definition OptConsTableSpace
3355 				{
3356 					Constraint *n = makeNode(Constraint);
3357 					n->contype = CONSTR_UNIQUE;
3358 					n->location = @1;
3359 					n->keys = NULL;
3360 					n->options = $2;
3361 					n->indexname = NULL;
3362 					n->indexspace = $3;
3363 					$$ = (Node *)n;
3364 				}
3365 			| PRIMARY KEY opt_definition OptConsTableSpace
3366 				{
3367 					Constraint *n = makeNode(Constraint);
3368 					n->contype = CONSTR_PRIMARY;
3369 					n->location = @1;
3370 					n->keys = NULL;
3371 					n->options = $3;
3372 					n->indexname = NULL;
3373 					n->indexspace = $4;
3374 					$$ = (Node *)n;
3375 				}
3376 			| CHECK '(' a_expr ')' opt_no_inherit
3377 				{
3378 					Constraint *n = makeNode(Constraint);
3379 					n->contype = CONSTR_CHECK;
3380 					n->location = @1;
3381 					n->is_no_inherit = $5;
3382 					n->raw_expr = $3;
3383 					n->cooked_expr = NULL;
3384 					n->skip_validation = false;
3385 					n->initially_valid = true;
3386 					$$ = (Node *)n;
3387 				}
3388 			| DEFAULT b_expr
3389 				{
3390 					Constraint *n = makeNode(Constraint);
3391 					n->contype = CONSTR_DEFAULT;
3392 					n->location = @1;
3393 					n->raw_expr = $2;
3394 					n->cooked_expr = NULL;
3395 					$$ = (Node *)n;
3396 				}
3397 			| GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
3398 				{
3399 					Constraint *n = makeNode(Constraint);
3400 					n->contype = CONSTR_IDENTITY;
3401 					n->generated_when = $2;
3402 					n->options = $5;
3403 					n->location = @1;
3404 					$$ = (Node *)n;
3405 				}
3406 			| REFERENCES qualified_name opt_column_list key_match key_actions
3407 				{
3408 					Constraint *n = makeNode(Constraint);
3409 					n->contype = CONSTR_FOREIGN;
3410 					n->location = @1;
3411 					n->pktable			= $2;
3412 					n->fk_attrs			= NIL;
3413 					n->pk_attrs			= $3;
3414 					n->fk_matchtype		= $4;
3415 					n->fk_upd_action	= (char) ($5 >> 8);
3416 					n->fk_del_action	= (char) ($5 & 0xFF);
3417 					n->skip_validation  = false;
3418 					n->initially_valid  = true;
3419 					$$ = (Node *)n;
3420 				}
3421 		;
3422 
3423 generated_when:
3424 			ALWAYS			{ $$ = ATTRIBUTE_IDENTITY_ALWAYS; }
3425 			| BY DEFAULT	{ $$ = ATTRIBUTE_IDENTITY_BY_DEFAULT; }
3426 		;
3427 
3428 /*
3429  * ConstraintAttr represents constraint attributes, which we parse as if
3430  * they were independent constraint clauses, in order to avoid shift/reduce
3431  * conflicts (since NOT might start either an independent NOT NULL clause
3432  * or an attribute).  parse_utilcmd.c is responsible for attaching the
3433  * attribute information to the preceding "real" constraint node, and for
3434  * complaining if attribute clauses appear in the wrong place or wrong
3435  * combinations.
3436  *
3437  * See also ConstraintAttributeSpec, which can be used in places where
3438  * there is no parsing conflict.  (Note: currently, NOT VALID and NO INHERIT
3439  * are allowed clauses in ConstraintAttributeSpec, but not here.  Someday we
3440  * might need to allow them here too, but for the moment it doesn't seem
3441  * useful in the statements that use ConstraintAttr.)
3442  */
3443 ConstraintAttr:
3444 			DEFERRABLE
3445 				{
3446 					Constraint *n = makeNode(Constraint);
3447 					n->contype = CONSTR_ATTR_DEFERRABLE;
3448 					n->location = @1;
3449 					$$ = (Node *)n;
3450 				}
3451 			| NOT DEFERRABLE
3452 				{
3453 					Constraint *n = makeNode(Constraint);
3454 					n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
3455 					n->location = @1;
3456 					$$ = (Node *)n;
3457 				}
3458 			| INITIALLY DEFERRED
3459 				{
3460 					Constraint *n = makeNode(Constraint);
3461 					n->contype = CONSTR_ATTR_DEFERRED;
3462 					n->location = @1;
3463 					$$ = (Node *)n;
3464 				}
3465 			| INITIALLY IMMEDIATE
3466 				{
3467 					Constraint *n = makeNode(Constraint);
3468 					n->contype = CONSTR_ATTR_IMMEDIATE;
3469 					n->location = @1;
3470 					$$ = (Node *)n;
3471 				}
3472 		;
3473 
3474 
3475 TableLikeClause:
3476 			LIKE qualified_name TableLikeOptionList
3477 				{
3478 					TableLikeClause *n = makeNode(TableLikeClause);
3479 					n->relation = $2;
3480 					n->options = $3;
3481 					n->relationOid = InvalidOid;
3482 					$$ = (Node *)n;
3483 				}
3484 		;
3485 
3486 TableLikeOptionList:
3487 				TableLikeOptionList INCLUDING TableLikeOption	{ $$ = $1 | $3; }
3488 				| TableLikeOptionList EXCLUDING TableLikeOption	{ $$ = $1 & ~$3; }
3489 				| /* EMPTY */						{ $$ = 0; }
3490 		;
3491 
3492 TableLikeOption:
3493 				COMMENTS			{ $$ = CREATE_TABLE_LIKE_COMMENTS; }
3494 				| CONSTRAINTS		{ $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
3495 				| DEFAULTS			{ $$ = CREATE_TABLE_LIKE_DEFAULTS; }
3496 				| IDENTITY_P		{ $$ = CREATE_TABLE_LIKE_IDENTITY; }
3497 				| INDEXES			{ $$ = CREATE_TABLE_LIKE_INDEXES; }
3498 				| STATISTICS		{ $$ = CREATE_TABLE_LIKE_STATISTICS; }
3499 				| STORAGE			{ $$ = CREATE_TABLE_LIKE_STORAGE; }
3500 				| ALL				{ $$ = CREATE_TABLE_LIKE_ALL; }
3501 		;
3502 
3503 
3504 /* ConstraintElem specifies constraint syntax which is not embedded into
3505  *	a column definition. ColConstraintElem specifies the embedded form.
3506  * - thomas 1997-12-03
3507  */
3508 TableConstraint:
3509 			CONSTRAINT name ConstraintElem
3510 				{
3511 					Constraint *n = castNode(Constraint, $3);
3512 					n->conname = $2;
3513 					n->location = @1;
3514 					$$ = (Node *) n;
3515 				}
3516 			| ConstraintElem						{ $$ = $1; }
3517 		;
3518 
3519 ConstraintElem:
3520 			CHECK '(' a_expr ')' ConstraintAttributeSpec
3521 				{
3522 					Constraint *n = makeNode(Constraint);
3523 					n->contype = CONSTR_CHECK;
3524 					n->location = @1;
3525 					n->raw_expr = $3;
3526 					n->cooked_expr = NULL;
3527 					processCASbits($5, @5, "CHECK",
3528 								   NULL, NULL, &n->skip_validation,
3529 								   &n->is_no_inherit, yyscanner);
3530 					n->initially_valid = !n->skip_validation;
3531 					$$ = (Node *)n;
3532 				}
3533 			| UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
3534 				ConstraintAttributeSpec
3535 				{
3536 					Constraint *n = makeNode(Constraint);
3537 					n->contype = CONSTR_UNIQUE;
3538 					n->location = @1;
3539 					n->keys = $3;
3540 					n->options = $5;
3541 					n->indexname = NULL;
3542 					n->indexspace = $6;
3543 					processCASbits($7, @7, "UNIQUE",
3544 								   &n->deferrable, &n->initdeferred, NULL,
3545 								   NULL, yyscanner);
3546 					$$ = (Node *)n;
3547 				}
3548 			| UNIQUE ExistingIndex ConstraintAttributeSpec
3549 				{
3550 					Constraint *n = makeNode(Constraint);
3551 					n->contype = CONSTR_UNIQUE;
3552 					n->location = @1;
3553 					n->keys = NIL;
3554 					n->options = NIL;
3555 					n->indexname = $2;
3556 					n->indexspace = NULL;
3557 					processCASbits($3, @3, "UNIQUE",
3558 								   &n->deferrable, &n->initdeferred, NULL,
3559 								   NULL, yyscanner);
3560 					$$ = (Node *)n;
3561 				}
3562 			| PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
3563 				ConstraintAttributeSpec
3564 				{
3565 					Constraint *n = makeNode(Constraint);
3566 					n->contype = CONSTR_PRIMARY;
3567 					n->location = @1;
3568 					n->keys = $4;
3569 					n->options = $6;
3570 					n->indexname = NULL;
3571 					n->indexspace = $7;
3572 					processCASbits($8, @8, "PRIMARY KEY",
3573 								   &n->deferrable, &n->initdeferred, NULL,
3574 								   NULL, yyscanner);
3575 					$$ = (Node *)n;
3576 				}
3577 			| PRIMARY KEY ExistingIndex ConstraintAttributeSpec
3578 				{
3579 					Constraint *n = makeNode(Constraint);
3580 					n->contype = CONSTR_PRIMARY;
3581 					n->location = @1;
3582 					n->keys = NIL;
3583 					n->options = NIL;
3584 					n->indexname = $3;
3585 					n->indexspace = NULL;
3586 					processCASbits($4, @4, "PRIMARY KEY",
3587 								   &n->deferrable, &n->initdeferred, NULL,
3588 								   NULL, yyscanner);
3589 					$$ = (Node *)n;
3590 				}
3591 			| EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
3592 				opt_definition OptConsTableSpace ExclusionWhereClause
3593 				ConstraintAttributeSpec
3594 				{
3595 					Constraint *n = makeNode(Constraint);
3596 					n->contype = CONSTR_EXCLUSION;
3597 					n->location = @1;
3598 					n->access_method	= $2;
3599 					n->exclusions		= $4;
3600 					n->options			= $6;
3601 					n->indexname		= NULL;
3602 					n->indexspace		= $7;
3603 					n->where_clause		= $8;
3604 					processCASbits($9, @9, "EXCLUDE",
3605 								   &n->deferrable, &n->initdeferred, NULL,
3606 								   NULL, yyscanner);
3607 					$$ = (Node *)n;
3608 				}
3609 			| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
3610 				opt_column_list key_match key_actions ConstraintAttributeSpec
3611 				{
3612 					Constraint *n = makeNode(Constraint);
3613 					n->contype = CONSTR_FOREIGN;
3614 					n->location = @1;
3615 					n->pktable			= $7;
3616 					n->fk_attrs			= $4;
3617 					n->pk_attrs			= $8;
3618 					n->fk_matchtype		= $9;
3619 					n->fk_upd_action	= (char) ($10 >> 8);
3620 					n->fk_del_action	= (char) ($10 & 0xFF);
3621 					processCASbits($11, @11, "FOREIGN KEY",
3622 								   &n->deferrable, &n->initdeferred,
3623 								   &n->skip_validation, NULL,
3624 								   yyscanner);
3625 					n->initially_valid = !n->skip_validation;
3626 					$$ = (Node *)n;
3627 				}
3628 		;
3629 
3630 opt_no_inherit:	NO INHERIT							{  $$ = TRUE; }
3631 			| /* EMPTY */							{  $$ = FALSE; }
3632 		;
3633 
3634 opt_column_list:
3635 			'(' columnList ')'						{ $$ = $2; }
3636 			| /*EMPTY*/								{ $$ = NIL; }
3637 		;
3638 
3639 columnList:
3640 			columnElem								{ $$ = list_make1($1); }
3641 			| columnList ',' columnElem				{ $$ = lappend($1, $3); }
3642 		;
3643 
3644 columnElem: ColId
3645 				{
3646 					$$ = (Node *) makeString($1);
3647 				}
3648 		;
3649 
3650 key_match:  MATCH FULL
3651 			{
3652 				$$ = FKCONSTR_MATCH_FULL;
3653 			}
3654 		| MATCH PARTIAL
3655 			{
3656 				ereport(ERROR,
3657 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3658 						 errmsg("MATCH PARTIAL not yet implemented"),
3659 						 parser_errposition(@1)));
3660 				$$ = FKCONSTR_MATCH_PARTIAL;
3661 			}
3662 		| MATCH SIMPLE
3663 			{
3664 				$$ = FKCONSTR_MATCH_SIMPLE;
3665 			}
3666 		| /*EMPTY*/
3667 			{
3668 				$$ = FKCONSTR_MATCH_SIMPLE;
3669 			}
3670 		;
3671 
3672 ExclusionConstraintList:
3673 			ExclusionConstraintElem					{ $$ = list_make1($1); }
3674 			| ExclusionConstraintList ',' ExclusionConstraintElem
3675 													{ $$ = lappend($1, $3); }
3676 		;
3677 
3678 ExclusionConstraintElem: index_elem WITH any_operator
3679 			{
3680 				$$ = list_make2($1, $3);
3681 			}
3682 			/* allow OPERATOR() decoration for the benefit of ruleutils.c */
3683 			| index_elem WITH OPERATOR '(' any_operator ')'
3684 			{
3685 				$$ = list_make2($1, $5);
3686 			}
3687 		;
3688 
3689 ExclusionWhereClause:
3690 			WHERE '(' a_expr ')'					{ $$ = $3; }
3691 			| /*EMPTY*/								{ $$ = NULL; }
3692 		;
3693 
3694 /*
3695  * We combine the update and delete actions into one value temporarily
3696  * for simplicity of parsing, and then break them down again in the
3697  * calling production.  update is in the left 8 bits, delete in the right.
3698  * Note that NOACTION is the default.
3699  */
3700 key_actions:
3701 			key_update
3702 				{ $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3703 			| key_delete
3704 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
3705 			| key_update key_delete
3706 				{ $$ = ($1 << 8) | ($2 & 0xFF); }
3707 			| key_delete key_update
3708 				{ $$ = ($2 << 8) | ($1 & 0xFF); }
3709 			| /*EMPTY*/
3710 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3711 		;
3712 
3713 key_update: ON UPDATE key_action		{ $$ = $3; }
3714 		;
3715 
3716 key_delete: ON DELETE_P key_action		{ $$ = $3; }
3717 		;
3718 
3719 key_action:
3720 			NO ACTION					{ $$ = FKCONSTR_ACTION_NOACTION; }
3721 			| RESTRICT					{ $$ = FKCONSTR_ACTION_RESTRICT; }
3722 			| CASCADE					{ $$ = FKCONSTR_ACTION_CASCADE; }
3723 			| SET NULL_P				{ $$ = FKCONSTR_ACTION_SETNULL; }
3724 			| SET DEFAULT				{ $$ = FKCONSTR_ACTION_SETDEFAULT; }
3725 		;
3726 
3727 OptInherit: INHERITS '(' qualified_name_list ')'	{ $$ = $3; }
3728 			| /*EMPTY*/								{ $$ = NIL; }
3729 		;
3730 
3731 /* Optional partition key specification */
3732 OptPartitionSpec: PartitionSpec	{ $$ = $1; }
3733 			| /*EMPTY*/			{ $$ = NULL; }
3734 		;
3735 
3736 PartitionSpec: PARTITION BY part_strategy '(' part_params ')'
3737 				{
3738 					PartitionSpec *n = makeNode(PartitionSpec);
3739 
3740 					n->strategy = $3;
3741 					n->partParams = $5;
3742 					n->location = @1;
3743 
3744 					$$ = n;
3745 				}
3746 		;
3747 
3748 part_strategy:	IDENT					{ $$ = $1; }
3749 				| unreserved_keyword	{ $$ = pstrdup($1); }
3750 		;
3751 
3752 part_params:	part_elem						{ $$ = list_make1($1); }
3753 			| part_params ',' part_elem			{ $$ = lappend($1, $3); }
3754 		;
3755 
3756 part_elem: ColId opt_collate opt_class
3757 				{
3758 					PartitionElem *n = makeNode(PartitionElem);
3759 
3760 					n->name = $1;
3761 					n->expr = NULL;
3762 					n->collation = $2;
3763 					n->opclass = $3;
3764 					n->location = @1;
3765 					$$ = n;
3766 				}
3767 			| func_expr_windowless opt_collate opt_class
3768 				{
3769 					PartitionElem *n = makeNode(PartitionElem);
3770 
3771 					n->name = NULL;
3772 					n->expr = $1;
3773 					n->collation = $2;
3774 					n->opclass = $3;
3775 					n->location = @1;
3776 					$$ = n;
3777 				}
3778 			| '(' a_expr ')' opt_collate opt_class
3779 				{
3780 					PartitionElem *n = makeNode(PartitionElem);
3781 
3782 					n->name = NULL;
3783 					n->expr = $2;
3784 					n->collation = $4;
3785 					n->opclass = $5;
3786 					n->location = @1;
3787 					$$ = n;
3788 				}
3789 		;
3790 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
3791 OptWith:
3792 			WITH reloptions				{ $$ = $2; }
3793 			| WITH OIDS					{ $$ = list_make1(makeDefElem("oids", (Node *) makeInteger(true), @1)); }
3794 			| WITHOUT OIDS				{ $$ = list_make1(makeDefElem("oids", (Node *) makeInteger(false), @1)); }
3795 			| /*EMPTY*/					{ $$ = NIL; }
3796 		;
3797 
3798 OnCommitOption:  ON COMMIT DROP				{ $$ = ONCOMMIT_DROP; }
3799 			| ON COMMIT DELETE_P ROWS		{ $$ = ONCOMMIT_DELETE_ROWS; }
3800 			| ON COMMIT PRESERVE ROWS		{ $$ = ONCOMMIT_PRESERVE_ROWS; }
3801 			| /*EMPTY*/						{ $$ = ONCOMMIT_NOOP; }
3802 		;
3803 
3804 OptTableSpace:   TABLESPACE name					{ $$ = $2; }
3805 			| /*EMPTY*/								{ $$ = NULL; }
3806 		;
3807 
3808 OptConsTableSpace:   USING INDEX TABLESPACE name	{ $$ = $4; }
3809 			| /*EMPTY*/								{ $$ = NULL; }
3810 		;
3811 
3812 ExistingIndex:   USING INDEX index_name				{ $$ = $3; }
3813 		;
3814 
3815 /*****************************************************************************
3816  *
3817  *		QUERY :
3818  *				CREATE STATISTICS [IF NOT EXISTS] stats_name [(stat types)]
3819  *					ON expression-list FROM from_list
3820  *
3821  * Note: the expectation here is that the clauses after ON are a subset of
3822  * SELECT syntax, allowing for expressions and joined tables, and probably
3823  * someday a WHERE clause.  Much less than that is currently implemented,
3824  * but the grammar accepts it and then we'll throw FEATURE_NOT_SUPPORTED
3825  * errors as necessary at execution.
3826  *
3827  *****************************************************************************/
3828 
3829 CreateStatsStmt:
3830 			CREATE STATISTICS any_name
3831 			opt_name_list ON expr_list FROM from_list
3832 				{
3833 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
3834 					n->defnames = $3;
3835 					n->stat_types = $4;
3836 					n->exprs = $6;
3837 					n->relations = $8;
3838 					n->if_not_exists = false;
3839 					$$ = (Node *)n;
3840 				}
3841 			| CREATE STATISTICS IF_P NOT EXISTS any_name
3842 			opt_name_list ON expr_list FROM from_list
3843 				{
3844 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
3845 					n->defnames = $6;
3846 					n->stat_types = $7;
3847 					n->exprs = $9;
3848 					n->relations = $11;
3849 					n->if_not_exists = true;
3850 					$$ = (Node *)n;
3851 				}
3852 			;
3853 
3854 /*****************************************************************************
3855  *
3856  *		QUERY :
3857  *				CREATE TABLE relname AS SelectStmt [ WITH [NO] DATA ]
3858  *
3859  *
3860  * Note: SELECT ... INTO is a now-deprecated alternative for this.
3861  *
3862  *****************************************************************************/
3863 
3864 CreateAsStmt:
3865 		CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
3866 				{
3867 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3868 					ctas->query = $6;
3869 					ctas->into = $4;
3870 					ctas->relkind = OBJECT_TABLE;
3871 					ctas->is_select_into = false;
3872 					ctas->if_not_exists = false;
3873 					/* cram additional flags into the IntoClause */
3874 					$4->rel->relpersistence = $2;
3875 					$4->skipData = !($7);
3876 					$$ = (Node *) ctas;
3877 				}
3878 		| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS SelectStmt opt_with_data
3879 				{
3880 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3881 					ctas->query = $9;
3882 					ctas->into = $7;
3883 					ctas->relkind = OBJECT_TABLE;
3884 					ctas->is_select_into = false;
3885 					ctas->if_not_exists = true;
3886 					/* cram additional flags into the IntoClause */
3887 					$7->rel->relpersistence = $2;
3888 					$7->skipData = !($10);
3889 					$$ = (Node *) ctas;
3890 				}
3891 		;
3892 
3893 create_as_target:
3894 			qualified_name opt_column_list OptWith OnCommitOption OptTableSpace
3895 				{
3896 					$$ = makeNode(IntoClause);
3897 					$$->rel = $1;
3898 					$$->colNames = $2;
3899 					$$->options = $3;
3900 					$$->onCommit = $4;
3901 					$$->tableSpaceName = $5;
3902 					$$->viewQuery = NULL;
3903 					$$->skipData = false;		/* might get changed later */
3904 				}
3905 		;
3906 
3907 opt_with_data:
3908 			WITH DATA_P								{ $$ = TRUE; }
3909 			| WITH NO DATA_P						{ $$ = FALSE; }
3910 			| /*EMPTY*/								{ $$ = TRUE; }
3911 		;
3912 
3913 
3914 /*****************************************************************************
3915  *
3916  *		QUERY :
3917  *				CREATE MATERIALIZED VIEW relname AS SelectStmt
3918  *
3919  *****************************************************************************/
3920 
3921 CreateMatViewStmt:
3922 		CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data
3923 				{
3924 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3925 					ctas->query = $7;
3926 					ctas->into = $5;
3927 					ctas->relkind = OBJECT_MATVIEW;
3928 					ctas->is_select_into = false;
3929 					ctas->if_not_exists = false;
3930 					/* cram additional flags into the IntoClause */
3931 					$5->rel->relpersistence = $2;
3932 					$5->skipData = !($8);
3933 					$$ = (Node *) ctas;
3934 				}
3935 		| CREATE OptNoLog MATERIALIZED VIEW IF_P NOT EXISTS create_mv_target AS SelectStmt opt_with_data
3936 				{
3937 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3938 					ctas->query = $10;
3939 					ctas->into = $8;
3940 					ctas->relkind = OBJECT_MATVIEW;
3941 					ctas->is_select_into = false;
3942 					ctas->if_not_exists = true;
3943 					/* cram additional flags into the IntoClause */
3944 					$8->rel->relpersistence = $2;
3945 					$8->skipData = !($11);
3946 					$$ = (Node *) ctas;
3947 				}
3948 		;
3949 
3950 create_mv_target:
3951 			qualified_name opt_column_list opt_reloptions OptTableSpace
3952 				{
3953 					$$ = makeNode(IntoClause);
3954 					$$->rel = $1;
3955 					$$->colNames = $2;
3956 					$$->options = $3;
3957 					$$->onCommit = ONCOMMIT_NOOP;
3958 					$$->tableSpaceName = $4;
3959 					$$->viewQuery = NULL;		/* filled at analysis time */
3960 					$$->skipData = false;		/* might get changed later */
3961 				}
3962 		;
3963 
3964 OptNoLog:	UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
3965 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
3966 		;
3967 
3968 
3969 /*****************************************************************************
3970  *
3971  *		QUERY :
3972  *				REFRESH MATERIALIZED VIEW qualified_name
3973  *
3974  *****************************************************************************/
3975 
3976 RefreshMatViewStmt:
3977 			REFRESH MATERIALIZED VIEW opt_concurrently qualified_name opt_with_data
3978 				{
3979 					RefreshMatViewStmt *n = makeNode(RefreshMatViewStmt);
3980 					n->concurrent = $4;
3981 					n->relation = $5;
3982 					n->skipData = !($6);
3983 					$$ = (Node *) n;
3984 				}
3985 		;
3986 
3987 
3988 /*****************************************************************************
3989  *
3990  *		QUERY :
3991  *				CREATE SEQUENCE seqname
3992  *				ALTER SEQUENCE seqname
3993  *
3994  *****************************************************************************/
3995 
3996 CreateSeqStmt:
3997 			CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
3998 				{
3999 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4000 					$4->relpersistence = $2;
4001 					n->sequence = $4;
4002 					n->options = $5;
4003 					n->ownerId = InvalidOid;
4004 					n->if_not_exists = false;
4005 					$$ = (Node *)n;
4006 				}
4007 			| CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
4008 				{
4009 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4010 					$7->relpersistence = $2;
4011 					n->sequence = $7;
4012 					n->options = $8;
4013 					n->ownerId = InvalidOid;
4014 					n->if_not_exists = true;
4015 					$$ = (Node *)n;
4016 				}
4017 		;
4018 
4019 AlterSeqStmt:
4020 			ALTER SEQUENCE qualified_name SeqOptList
4021 				{
4022 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4023 					n->sequence = $3;
4024 					n->options = $4;
4025 					n->missing_ok = false;
4026 					$$ = (Node *)n;
4027 				}
4028 			| ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
4029 				{
4030 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4031 					n->sequence = $5;
4032 					n->options = $6;
4033 					n->missing_ok = true;
4034 					$$ = (Node *)n;
4035 				}
4036 
4037 		;
4038 
4039 OptSeqOptList: SeqOptList							{ $$ = $1; }
4040 			| /*EMPTY*/								{ $$ = NIL; }
4041 		;
4042 
4043 OptParenthesizedSeqOptList: '(' SeqOptList ')'		{ $$ = $2; }
4044 			| /*EMPTY*/								{ $$ = NIL; }
4045 		;
4046 
4047 SeqOptList: SeqOptElem								{ $$ = list_make1($1); }
4048 			| SeqOptList SeqOptElem					{ $$ = lappend($1, $2); }
4049 		;
4050 
4051 SeqOptElem: AS SimpleTypename
4052 				{
4053 					$$ = makeDefElem("as", (Node *)$2, @1);
4054 				}
4055 			| CACHE NumericOnly
4056 				{
4057 					$$ = makeDefElem("cache", (Node *)$2, @1);
4058 				}
4059 			| CYCLE
4060 				{
4061 					$$ = makeDefElem("cycle", (Node *)makeInteger(TRUE), @1);
4062 				}
4063 			| NO CYCLE
4064 				{
4065 					$$ = makeDefElem("cycle", (Node *)makeInteger(FALSE), @1);
4066 				}
4067 			| INCREMENT opt_by NumericOnly
4068 				{
4069 					$$ = makeDefElem("increment", (Node *)$3, @1);
4070 				}
4071 			| MAXVALUE NumericOnly
4072 				{
4073 					$$ = makeDefElem("maxvalue", (Node *)$2, @1);
4074 				}
4075 			| MINVALUE NumericOnly
4076 				{
4077 					$$ = makeDefElem("minvalue", (Node *)$2, @1);
4078 				}
4079 			| NO MAXVALUE
4080 				{
4081 					$$ = makeDefElem("maxvalue", NULL, @1);
4082 				}
4083 			| NO MINVALUE
4084 				{
4085 					$$ = makeDefElem("minvalue", NULL, @1);
4086 				}
4087 			| OWNED BY any_name
4088 				{
4089 					$$ = makeDefElem("owned_by", (Node *)$3, @1);
4090 				}
4091 			| SEQUENCE NAME_P any_name
4092 				{
4093 					/* not documented, only used by pg_dump */
4094 					$$ = makeDefElem("sequence_name", (Node *)$3, @1);
4095 				}
4096 			| START opt_with NumericOnly
4097 				{
4098 					$$ = makeDefElem("start", (Node *)$3, @1);
4099 				}
4100 			| RESTART
4101 				{
4102 					$$ = makeDefElem("restart", NULL, @1);
4103 				}
4104 			| RESTART opt_with NumericOnly
4105 				{
4106 					$$ = makeDefElem("restart", (Node *)$3, @1);
4107 				}
4108 		;
4109 
4110 opt_by:		BY				{}
4111 			| /* empty */	{}
4112 	  ;
4113 
4114 NumericOnly:
4115 			FCONST								{ $$ = makeFloat($1); }
4116 			| '+' FCONST						{ $$ = makeFloat($2); }
4117 			| '-' FCONST
4118 				{
4119 					$$ = makeFloat($2);
4120 					doNegateFloat($$);
4121 				}
4122 			| SignedIconst						{ $$ = makeInteger($1); }
4123 		;
4124 
4125 NumericOnly_list:	NumericOnly						{ $$ = list_make1($1); }
4126 				| NumericOnly_list ',' NumericOnly	{ $$ = lappend($1, $3); }
4127 		;
4128 
4129 /*****************************************************************************
4130  *
4131  *		QUERIES :
4132  *				CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
4133  *				DROP [PROCEDURAL] LANGUAGE ...
4134  *
4135  *****************************************************************************/
4136 
4137 CreatePLangStmt:
4138 			CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4139 			{
4140 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4141 				n->replace = $2;
4142 				n->plname = $6;
4143 				/* parameters are all to be supplied by system */
4144 				n->plhandler = NIL;
4145 				n->plinline = NIL;
4146 				n->plvalidator = NIL;
4147 				n->pltrusted = false;
4148 				$$ = (Node *)n;
4149 			}
4150 			| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4151 			  HANDLER handler_name opt_inline_handler opt_validator
4152 			{
4153 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4154 				n->replace = $2;
4155 				n->plname = $6;
4156 				n->plhandler = $8;
4157 				n->plinline = $9;
4158 				n->plvalidator = $10;
4159 				n->pltrusted = $3;
4160 				$$ = (Node *)n;
4161 			}
4162 		;
4163 
4164 opt_trusted:
4165 			TRUSTED									{ $$ = TRUE; }
4166 			| /*EMPTY*/								{ $$ = FALSE; }
4167 		;
4168 
4169 /* This ought to be just func_name, but that causes reduce/reduce conflicts
4170  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
4171  * Work around by using simple names, instead.
4172  */
4173 handler_name:
4174 			name						{ $$ = list_make1(makeString($1)); }
4175 			| name attrs				{ $$ = lcons(makeString($1), $2); }
4176 		;
4177 
4178 opt_inline_handler:
4179 			INLINE_P handler_name					{ $$ = $2; }
4180 			| /*EMPTY*/								{ $$ = NIL; }
4181 		;
4182 
4183 validator_clause:
4184 			VALIDATOR handler_name					{ $$ = $2; }
4185 			| NO VALIDATOR							{ $$ = NIL; }
4186 		;
4187 
4188 opt_validator:
4189 			validator_clause						{ $$ = $1; }
4190 			| /*EMPTY*/								{ $$ = NIL; }
4191 		;
4192 
4193 DropPLangStmt:
4194 			DROP opt_procedural LANGUAGE NonReservedWord_or_Sconst opt_drop_behavior
4195 				{
4196 					DropStmt *n = makeNode(DropStmt);
4197 					n->removeType = OBJECT_LANGUAGE;
4198 					n->objects = list_make1(makeString($4));
4199 					n->behavior = $5;
4200 					n->missing_ok = false;
4201 					n->concurrent = false;
4202 					$$ = (Node *)n;
4203 				}
4204 			| DROP opt_procedural LANGUAGE IF_P EXISTS NonReservedWord_or_Sconst opt_drop_behavior
4205 				{
4206 					DropStmt *n = makeNode(DropStmt);
4207 					n->removeType = OBJECT_LANGUAGE;
4208 					n->objects = list_make1(makeString($6));
4209 					n->behavior = $7;
4210 					n->missing_ok = true;
4211 					n->concurrent = false;
4212 					$$ = (Node *)n;
4213 				}
4214 		;
4215 
4216 opt_procedural:
4217 			PROCEDURAL								{}
4218 			| /*EMPTY*/								{}
4219 		;
4220 
4221 /*****************************************************************************
4222  *
4223  *		QUERY:
4224  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
4225  *
4226  *****************************************************************************/
4227 
4228 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst opt_reloptions
4229 				{
4230 					CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
4231 					n->tablespacename = $3;
4232 					n->owner = $4;
4233 					n->location = $6;
4234 					n->options = $7;
4235 					$$ = (Node *) n;
4236 				}
4237 		;
4238 
4239 OptTableSpaceOwner: OWNER RoleSpec		{ $$ = $2; }
4240 			| /*EMPTY */				{ $$ = NULL; }
4241 		;
4242 
4243 /*****************************************************************************
4244  *
4245  *		QUERY :
4246  *				DROP TABLESPACE <tablespace>
4247  *
4248  *		No need for drop behaviour as we cannot implement dependencies for
4249  *		objects in other databases; we can only support RESTRICT.
4250  *
4251  ****************************************************************************/
4252 
4253 DropTableSpaceStmt: DROP TABLESPACE name
4254 				{
4255 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4256 					n->tablespacename = $3;
4257 					n->missing_ok = false;
4258 					$$ = (Node *) n;
4259 				}
4260 				|  DROP TABLESPACE IF_P EXISTS name
4261 				{
4262 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4263 					n->tablespacename = $5;
4264 					n->missing_ok = true;
4265 					$$ = (Node *) n;
4266 				}
4267 		;
4268 
4269 /*****************************************************************************
4270  *
4271  *		QUERY:
4272  *             CREATE EXTENSION extension
4273  *             [ WITH ] [ SCHEMA schema ] [ VERSION version ] [ FROM oldversion ]
4274  *
4275  *****************************************************************************/
4276 
4277 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
4278 				{
4279 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4280 					n->extname = $3;
4281 					n->if_not_exists = false;
4282 					n->options = $5;
4283 					$$ = (Node *) n;
4284 				}
4285 				| CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
4286 				{
4287 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4288 					n->extname = $6;
4289 					n->if_not_exists = true;
4290 					n->options = $8;
4291 					$$ = (Node *) n;
4292 				}
4293 		;
4294 
4295 create_extension_opt_list:
4296 			create_extension_opt_list create_extension_opt_item
4297 				{ $$ = lappend($1, $2); }
4298 			| /* EMPTY */
4299 				{ $$ = NIL; }
4300 		;
4301 
4302 create_extension_opt_item:
4303 			SCHEMA name
4304 				{
4305 					$$ = makeDefElem("schema", (Node *)makeString($2), @1);
4306 				}
4307 			| VERSION_P NonReservedWord_or_Sconst
4308 				{
4309 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4310 				}
4311 			| FROM NonReservedWord_or_Sconst
4312 				{
4313 					$$ = makeDefElem("old_version", (Node *)makeString($2), @1);
4314 				}
4315 			| CASCADE
4316 				{
4317 					$$ = makeDefElem("cascade", (Node *)makeInteger(TRUE), @1);
4318 				}
4319 		;
4320 
4321 /*****************************************************************************
4322  *
4323  * ALTER EXTENSION name UPDATE [ TO version ]
4324  *
4325  *****************************************************************************/
4326 
4327 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
4328 				{
4329 					AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
4330 					n->extname = $3;
4331 					n->options = $5;
4332 					$$ = (Node *) n;
4333 				}
4334 		;
4335 
4336 alter_extension_opt_list:
4337 			alter_extension_opt_list alter_extension_opt_item
4338 				{ $$ = lappend($1, $2); }
4339 			| /* EMPTY */
4340 				{ $$ = NIL; }
4341 		;
4342 
4343 alter_extension_opt_item:
4344 			TO NonReservedWord_or_Sconst
4345 				{
4346 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4347 				}
4348 		;
4349 
4350 /*****************************************************************************
4351  *
4352  * ALTER EXTENSION name ADD/DROP object-identifier
4353  *
4354  *****************************************************************************/
4355 
4356 AlterExtensionContentsStmt:
4357 			ALTER EXTENSION name add_drop ACCESS METHOD name
4358 				{
4359 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4360 					n->extname = $3;
4361 					n->action = $4;
4362 					n->objtype = OBJECT_ACCESS_METHOD;
4363 					n->object = (Node *) makeString($7);
4364 					$$ = (Node *)n;
4365 				}
4366 			| ALTER EXTENSION name add_drop AGGREGATE aggregate_with_argtypes
4367 				{
4368 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4369 					n->extname = $3;
4370 					n->action = $4;
4371 					n->objtype = OBJECT_AGGREGATE;
4372 					n->object = (Node *) $6;
4373 					$$ = (Node *)n;
4374 				}
4375 			| ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
4376 				{
4377 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4378 					n->extname = $3;
4379 					n->action = $4;
4380 					n->objtype = OBJECT_CAST;
4381 					n->object = (Node *) list_make2($7, $9);
4382 					$$ = (Node *) n;
4383 				}
4384 			| ALTER EXTENSION name add_drop COLLATION any_name
4385 				{
4386 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4387 					n->extname = $3;
4388 					n->action = $4;
4389 					n->objtype = OBJECT_COLLATION;
4390 					n->object = (Node *) $6;
4391 					$$ = (Node *)n;
4392 				}
4393 			| ALTER EXTENSION name add_drop CONVERSION_P any_name
4394 				{
4395 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4396 					n->extname = $3;
4397 					n->action = $4;
4398 					n->objtype = OBJECT_CONVERSION;
4399 					n->object = (Node *) $6;
4400 					$$ = (Node *)n;
4401 				}
4402 			| ALTER EXTENSION name add_drop DOMAIN_P Typename
4403 				{
4404 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4405 					n->extname = $3;
4406 					n->action = $4;
4407 					n->objtype = OBJECT_DOMAIN;
4408 					n->object = (Node *) $6;
4409 					$$ = (Node *)n;
4410 				}
4411 			| ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
4412 				{
4413 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4414 					n->extname = $3;
4415 					n->action = $4;
4416 					n->objtype = OBJECT_FUNCTION;
4417 					n->object = (Node *) $6;
4418 					$$ = (Node *)n;
4419 				}
4420 			| ALTER EXTENSION name add_drop opt_procedural LANGUAGE name
4421 				{
4422 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4423 					n->extname = $3;
4424 					n->action = $4;
4425 					n->objtype = OBJECT_LANGUAGE;
4426 					n->object = (Node *) makeString($7);
4427 					$$ = (Node *)n;
4428 				}
4429 			| ALTER EXTENSION name add_drop OPERATOR operator_with_argtypes
4430 				{
4431 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4432 					n->extname = $3;
4433 					n->action = $4;
4434 					n->objtype = OBJECT_OPERATOR;
4435 					n->object = (Node *) $6;
4436 					$$ = (Node *)n;
4437 				}
4438 			| ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING access_method
4439 				{
4440 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4441 					n->extname = $3;
4442 					n->action = $4;
4443 					n->objtype = OBJECT_OPCLASS;
4444 					n->object = (Node *) lcons(makeString($9), $7);
4445 					$$ = (Node *)n;
4446 				}
4447 			| ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING access_method
4448 				{
4449 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4450 					n->extname = $3;
4451 					n->action = $4;
4452 					n->objtype = OBJECT_OPFAMILY;
4453 					n->object = (Node *) lcons(makeString($9), $7);
4454 					$$ = (Node *)n;
4455 				}
4456 			| ALTER EXTENSION name add_drop SCHEMA name
4457 				{
4458 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4459 					n->extname = $3;
4460 					n->action = $4;
4461 					n->objtype = OBJECT_SCHEMA;
4462 					n->object = (Node *) makeString($6);
4463 					$$ = (Node *)n;
4464 				}
4465 			| ALTER EXTENSION name add_drop EVENT TRIGGER name
4466 				{
4467 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4468 					n->extname = $3;
4469 					n->action = $4;
4470 					n->objtype = OBJECT_EVENT_TRIGGER;
4471 					n->object = (Node *) makeString($7);
4472 					$$ = (Node *)n;
4473 				}
4474 			| ALTER EXTENSION name add_drop TABLE any_name
4475 				{
4476 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4477 					n->extname = $3;
4478 					n->action = $4;
4479 					n->objtype = OBJECT_TABLE;
4480 					n->object = (Node *) $6;
4481 					$$ = (Node *)n;
4482 				}
4483 			| ALTER EXTENSION name add_drop TEXT_P SEARCH PARSER any_name
4484 				{
4485 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4486 					n->extname = $3;
4487 					n->action = $4;
4488 					n->objtype = OBJECT_TSPARSER;
4489 					n->object = (Node *) $8;
4490 					$$ = (Node *)n;
4491 				}
4492 			| ALTER EXTENSION name add_drop TEXT_P SEARCH DICTIONARY any_name
4493 				{
4494 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4495 					n->extname = $3;
4496 					n->action = $4;
4497 					n->objtype = OBJECT_TSDICTIONARY;
4498 					n->object = (Node *) $8;
4499 					$$ = (Node *)n;
4500 				}
4501 			| ALTER EXTENSION name add_drop TEXT_P SEARCH TEMPLATE any_name
4502 				{
4503 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4504 					n->extname = $3;
4505 					n->action = $4;
4506 					n->objtype = OBJECT_TSTEMPLATE;
4507 					n->object = (Node *) $8;
4508 					$$ = (Node *)n;
4509 				}
4510 			| ALTER EXTENSION name add_drop TEXT_P SEARCH CONFIGURATION any_name
4511 				{
4512 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4513 					n->extname = $3;
4514 					n->action = $4;
4515 					n->objtype = OBJECT_TSCONFIGURATION;
4516 					n->object = (Node *) $8;
4517 					$$ = (Node *)n;
4518 				}
4519 			| ALTER EXTENSION name add_drop SEQUENCE any_name
4520 				{
4521 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4522 					n->extname = $3;
4523 					n->action = $4;
4524 					n->objtype = OBJECT_SEQUENCE;
4525 					n->object = (Node *) $6;
4526 					$$ = (Node *)n;
4527 				}
4528 			| ALTER EXTENSION name add_drop VIEW any_name
4529 				{
4530 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4531 					n->extname = $3;
4532 					n->action = $4;
4533 					n->objtype = OBJECT_VIEW;
4534 					n->object = (Node *) $6;
4535 					$$ = (Node *)n;
4536 				}
4537 			| ALTER EXTENSION name add_drop MATERIALIZED VIEW any_name
4538 				{
4539 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4540 					n->extname = $3;
4541 					n->action = $4;
4542 					n->objtype = OBJECT_MATVIEW;
4543 					n->object = (Node *) $7;
4544 					$$ = (Node *)n;
4545 				}
4546 			| ALTER EXTENSION name add_drop FOREIGN TABLE any_name
4547 				{
4548 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4549 					n->extname = $3;
4550 					n->action = $4;
4551 					n->objtype = OBJECT_FOREIGN_TABLE;
4552 					n->object = (Node *) $7;
4553 					$$ = (Node *)n;
4554 				}
4555 			| ALTER EXTENSION name add_drop FOREIGN DATA_P WRAPPER name
4556 				{
4557 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4558 					n->extname = $3;
4559 					n->action = $4;
4560 					n->objtype = OBJECT_FDW;
4561 					n->object = (Node *) makeString($8);
4562 					$$ = (Node *)n;
4563 				}
4564 			| ALTER EXTENSION name add_drop SERVER name
4565 				{
4566 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4567 					n->extname = $3;
4568 					n->action = $4;
4569 					n->objtype = OBJECT_FOREIGN_SERVER;
4570 					n->object = (Node *) makeString($6);
4571 					$$ = (Node *)n;
4572 				}
4573 			| ALTER EXTENSION name add_drop TRANSFORM FOR Typename LANGUAGE name
4574 				{
4575 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4576 					n->extname = $3;
4577 					n->action = $4;
4578 					n->objtype = OBJECT_TRANSFORM;
4579 					n->object = (Node *) list_make2($7, makeString($9));
4580 					$$ = (Node *)n;
4581 				}
4582 			| ALTER EXTENSION name add_drop TYPE_P Typename
4583 				{
4584 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4585 					n->extname = $3;
4586 					n->action = $4;
4587 					n->objtype = OBJECT_TYPE;
4588 					n->object = (Node *) $6;
4589 					$$ = (Node *)n;
4590 				}
4591 		;
4592 
4593 /*****************************************************************************
4594  *
4595  *		QUERY:
4596  *             CREATE FOREIGN DATA WRAPPER name options
4597  *
4598  *****************************************************************************/
4599 
4600 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
4601 				{
4602 					CreateFdwStmt *n = makeNode(CreateFdwStmt);
4603 					n->fdwname = $5;
4604 					n->func_options = $6;
4605 					n->options = $7;
4606 					$$ = (Node *) n;
4607 				}
4608 		;
4609 
4610 fdw_option:
4611 			HANDLER handler_name				{ $$ = makeDefElem("handler", (Node *)$2, @1); }
4612 			| NO HANDLER						{ $$ = makeDefElem("handler", NULL, @1); }
4613 			| VALIDATOR handler_name			{ $$ = makeDefElem("validator", (Node *)$2, @1); }
4614 			| NO VALIDATOR						{ $$ = makeDefElem("validator", NULL, @1); }
4615 		;
4616 
4617 fdw_options:
4618 			fdw_option							{ $$ = list_make1($1); }
4619 			| fdw_options fdw_option			{ $$ = lappend($1, $2); }
4620 		;
4621 
4622 opt_fdw_options:
4623 			fdw_options							{ $$ = $1; }
4624 			| /*EMPTY*/							{ $$ = NIL; }
4625 		;
4626 
4627 /*****************************************************************************
4628  *
4629  *		QUERY :
4630  *				ALTER FOREIGN DATA WRAPPER name options
4631  *
4632  ****************************************************************************/
4633 
4634 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
4635 				{
4636 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4637 					n->fdwname = $5;
4638 					n->func_options = $6;
4639 					n->options = $7;
4640 					$$ = (Node *) n;
4641 				}
4642 			| ALTER FOREIGN DATA_P WRAPPER name fdw_options
4643 				{
4644 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4645 					n->fdwname = $5;
4646 					n->func_options = $6;
4647 					n->options = NIL;
4648 					$$ = (Node *) n;
4649 				}
4650 		;
4651 
4652 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
4653 create_generic_options:
4654 			OPTIONS '(' generic_option_list ')'			{ $$ = $3; }
4655 			| /*EMPTY*/									{ $$ = NIL; }
4656 		;
4657 
4658 generic_option_list:
4659 			generic_option_elem
4660 				{
4661 					$$ = list_make1($1);
4662 				}
4663 			| generic_option_list ',' generic_option_elem
4664 				{
4665 					$$ = lappend($1, $3);
4666 				}
4667 		;
4668 
4669 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
4670 alter_generic_options:
4671 			OPTIONS	'(' alter_generic_option_list ')'		{ $$ = $3; }
4672 		;
4673 
4674 alter_generic_option_list:
4675 			alter_generic_option_elem
4676 				{
4677 					$$ = list_make1($1);
4678 				}
4679 			| alter_generic_option_list ',' alter_generic_option_elem
4680 				{
4681 					$$ = lappend($1, $3);
4682 				}
4683 		;
4684 
4685 alter_generic_option_elem:
4686 			generic_option_elem
4687 				{
4688 					$$ = $1;
4689 				}
4690 			| SET generic_option_elem
4691 				{
4692 					$$ = $2;
4693 					$$->defaction = DEFELEM_SET;
4694 				}
4695 			| ADD_P generic_option_elem
4696 				{
4697 					$$ = $2;
4698 					$$->defaction = DEFELEM_ADD;
4699 				}
4700 			| DROP generic_option_name
4701 				{
4702 					$$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP, @2);
4703 				}
4704 		;
4705 
4706 generic_option_elem:
4707 			generic_option_name generic_option_arg
4708 				{
4709 					$$ = makeDefElem($1, $2, @1);
4710 				}
4711 		;
4712 
4713 generic_option_name:
4714 				ColLabel			{ $$ = $1; }
4715 		;
4716 
4717 /* We could use def_arg here, but the spec only requires string literals */
4718 generic_option_arg:
4719 				Sconst				{ $$ = (Node *) makeString($1); }
4720 		;
4721 
4722 /*****************************************************************************
4723  *
4724  *		QUERY:
4725  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
4726  *
4727  *****************************************************************************/
4728 
4729 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
4730 						 FOREIGN DATA_P WRAPPER name create_generic_options
4731 				{
4732 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4733 					n->servername = $3;
4734 					n->servertype = $4;
4735 					n->version = $5;
4736 					n->fdwname = $9;
4737 					n->options = $10;
4738 					n->if_not_exists = false;
4739 					$$ = (Node *) n;
4740 				}
4741 				| CREATE SERVER IF_P NOT EXISTS name opt_type opt_foreign_server_version
4742 						 FOREIGN DATA_P WRAPPER name create_generic_options
4743 				{
4744 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4745 					n->servername = $6;
4746 					n->servertype = $7;
4747 					n->version = $8;
4748 					n->fdwname = $12;
4749 					n->options = $13;
4750 					n->if_not_exists = true;
4751 					$$ = (Node *) n;
4752 				}
4753 		;
4754 
4755 opt_type:
4756 			TYPE_P Sconst			{ $$ = $2; }
4757 			| /*EMPTY*/				{ $$ = NULL; }
4758 		;
4759 
4760 
4761 foreign_server_version:
4762 			VERSION_P Sconst		{ $$ = $2; }
4763 		|	VERSION_P NULL_P		{ $$ = NULL; }
4764 		;
4765 
4766 opt_foreign_server_version:
4767 			foreign_server_version	{ $$ = $1; }
4768 			| /*EMPTY*/				{ $$ = NULL; }
4769 		;
4770 
4771 /*****************************************************************************
4772  *
4773  *		QUERY :
4774  *				ALTER SERVER name [VERSION] [OPTIONS]
4775  *
4776  ****************************************************************************/
4777 
4778 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
4779 				{
4780 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4781 					n->servername = $3;
4782 					n->version = $4;
4783 					n->options = $5;
4784 					n->has_version = true;
4785 					$$ = (Node *) n;
4786 				}
4787 			| ALTER SERVER name foreign_server_version
4788 				{
4789 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4790 					n->servername = $3;
4791 					n->version = $4;
4792 					n->has_version = true;
4793 					$$ = (Node *) n;
4794 				}
4795 			| ALTER SERVER name alter_generic_options
4796 				{
4797 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4798 					n->servername = $3;
4799 					n->options = $4;
4800 					$$ = (Node *) n;
4801 				}
4802 		;
4803 
4804 /*****************************************************************************
4805  *
4806  *		QUERY:
4807  *             CREATE FOREIGN TABLE relname (...) SERVER name (...)
4808  *
4809  *****************************************************************************/
4810 
4811 CreateForeignTableStmt:
4812 		CREATE FOREIGN TABLE qualified_name
4813 			'(' OptTableElementList ')'
4814 			OptInherit SERVER name create_generic_options
4815 				{
4816 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4817 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
4818 					n->base.relation = $4;
4819 					n->base.tableElts = $6;
4820 					n->base.inhRelations = $8;
4821 					n->base.ofTypename = NULL;
4822 					n->base.constraints = NIL;
4823 					n->base.options = NIL;
4824 					n->base.oncommit = ONCOMMIT_NOOP;
4825 					n->base.tablespacename = NULL;
4826 					n->base.if_not_exists = false;
4827 					/* FDW-specific data */
4828 					n->servername = $10;
4829 					n->options = $11;
4830 					$$ = (Node *) n;
4831 				}
4832 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
4833 			'(' OptTableElementList ')'
4834 			OptInherit SERVER name create_generic_options
4835 				{
4836 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4837 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
4838 					n->base.relation = $7;
4839 					n->base.tableElts = $9;
4840 					n->base.inhRelations = $11;
4841 					n->base.ofTypename = NULL;
4842 					n->base.constraints = NIL;
4843 					n->base.options = NIL;
4844 					n->base.oncommit = ONCOMMIT_NOOP;
4845 					n->base.tablespacename = NULL;
4846 					n->base.if_not_exists = true;
4847 					/* FDW-specific data */
4848 					n->servername = $13;
4849 					n->options = $14;
4850 					$$ = (Node *) n;
4851 				}
4852 		| CREATE FOREIGN TABLE qualified_name
4853 			PARTITION OF qualified_name OptTypedTableElementList ForValues
4854 			SERVER name create_generic_options
4855 				{
4856 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4857 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
4858 					n->base.relation = $4;
4859 					n->base.inhRelations = list_make1($7);
4860 					n->base.tableElts = $8;
4861 					n->base.partbound = $9;
4862 					n->base.ofTypename = NULL;
4863 					n->base.constraints = NIL;
4864 					n->base.options = NIL;
4865 					n->base.oncommit = ONCOMMIT_NOOP;
4866 					n->base.tablespacename = NULL;
4867 					n->base.if_not_exists = false;
4868 					/* FDW-specific data */
4869 					n->servername = $11;
4870 					n->options = $12;
4871 					$$ = (Node *) n;
4872 				}
4873 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
4874 			PARTITION OF qualified_name OptTypedTableElementList ForValues
4875 			SERVER name create_generic_options
4876 				{
4877 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4878 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
4879 					n->base.relation = $7;
4880 					n->base.inhRelations = list_make1($10);
4881 					n->base.tableElts = $11;
4882 					n->base.partbound = $12;
4883 					n->base.ofTypename = NULL;
4884 					n->base.constraints = NIL;
4885 					n->base.options = NIL;
4886 					n->base.oncommit = ONCOMMIT_NOOP;
4887 					n->base.tablespacename = NULL;
4888 					n->base.if_not_exists = true;
4889 					/* FDW-specific data */
4890 					n->servername = $14;
4891 					n->options = $15;
4892 					$$ = (Node *) n;
4893 				}
4894 		;
4895 
4896 /*****************************************************************************
4897  *
4898  *		QUERY:
4899  *             ALTER FOREIGN TABLE relname [...]
4900  *
4901  *****************************************************************************/
4902 
4903 AlterForeignTableStmt:
4904 			ALTER FOREIGN TABLE relation_expr alter_table_cmds
4905 				{
4906 					AlterTableStmt *n = makeNode(AlterTableStmt);
4907 					n->relation = $4;
4908 					n->cmds = $5;
4909 					n->relkind = OBJECT_FOREIGN_TABLE;
4910 					n->missing_ok = false;
4911 					$$ = (Node *)n;
4912 				}
4913 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
4914 				{
4915 					AlterTableStmt *n = makeNode(AlterTableStmt);
4916 					n->relation = $6;
4917 					n->cmds = $7;
4918 					n->relkind = OBJECT_FOREIGN_TABLE;
4919 					n->missing_ok = true;
4920 					$$ = (Node *)n;
4921 				}
4922 		;
4923 
4924 /*****************************************************************************
4925  *
4926  *		QUERY:
4927  *				IMPORT FOREIGN SCHEMA remote_schema
4928  *				[ { LIMIT TO | EXCEPT } ( table_list ) ]
4929  *				FROM SERVER server_name INTO local_schema [ OPTIONS (...) ]
4930  *
4931  ****************************************************************************/
4932 
4933 ImportForeignSchemaStmt:
4934 		IMPORT_P FOREIGN SCHEMA name import_qualification
4935 		  FROM SERVER name INTO name create_generic_options
4936 			{
4937 				ImportForeignSchemaStmt *n = makeNode(ImportForeignSchemaStmt);
4938 				n->server_name = $8;
4939 				n->remote_schema = $4;
4940 				n->local_schema = $10;
4941 				n->list_type = $5->type;
4942 				n->table_list = $5->table_names;
4943 				n->options = $11;
4944 				$$ = (Node *) n;
4945 			}
4946 		;
4947 
4948 import_qualification_type:
4949 		LIMIT TO 				{ $$ = FDW_IMPORT_SCHEMA_LIMIT_TO; }
4950 		| EXCEPT 				{ $$ = FDW_IMPORT_SCHEMA_EXCEPT; }
4951 		;
4952 
4953 import_qualification:
4954 		import_qualification_type '(' relation_expr_list ')'
4955 			{
4956 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
4957 				n->type = $1;
4958 				n->table_names = $3;
4959 				$$ = n;
4960 			}
4961 		| /*EMPTY*/
4962 			{
4963 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
4964 				n->type = FDW_IMPORT_SCHEMA_ALL;
4965 				n->table_names = NIL;
4966 				$$ = n;
4967 			}
4968 		;
4969 
4970 /*****************************************************************************
4971  *
4972  *		QUERY:
4973  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
4974  *
4975  *****************************************************************************/
4976 
4977 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
4978 				{
4979 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
4980 					n->user = $5;
4981 					n->servername = $7;
4982 					n->options = $8;
4983 					n->if_not_exists = false;
4984 					$$ = (Node *) n;
4985 				}
4986 				| CREATE USER MAPPING IF_P NOT EXISTS FOR auth_ident SERVER name create_generic_options
4987 				{
4988 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
4989 					n->user = $8;
4990 					n->servername = $10;
4991 					n->options = $11;
4992 					n->if_not_exists = true;
4993 					$$ = (Node *) n;
4994 				}
4995 		;
4996 
4997 /* User mapping authorization identifier */
4998 auth_ident: RoleSpec			{ $$ = $1; }
4999 			| USER				{ $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1); }
5000 		;
5001 
5002 /*****************************************************************************
5003  *
5004  *		QUERY :
5005  *				DROP USER MAPPING FOR auth_ident SERVER name
5006  *
5007  * XXX you'd think this should have a CASCADE/RESTRICT option, even if it's
5008  * only pro forma; but the SQL standard doesn't show one.
5009  ****************************************************************************/
5010 
5011 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
5012 				{
5013 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5014 					n->user = $5;
5015 					n->servername = $7;
5016 					n->missing_ok = false;
5017 					$$ = (Node *) n;
5018 				}
5019 				|  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
5020 				{
5021 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5022 					n->user = $7;
5023 					n->servername = $9;
5024 					n->missing_ok = true;
5025 					$$ = (Node *) n;
5026 				}
5027 		;
5028 
5029 /*****************************************************************************
5030  *
5031  *		QUERY :
5032  *				ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
5033  *
5034  ****************************************************************************/
5035 
5036 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
5037 				{
5038 					AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
5039 					n->user = $5;
5040 					n->servername = $7;
5041 					n->options = $8;
5042 					$$ = (Node *) n;
5043 				}
5044 		;
5045 
5046 /*****************************************************************************
5047  *
5048  *		QUERIES:
5049  *				CREATE POLICY name ON table
5050  *					[AS { PERMISSIVE | RESTRICTIVE } ]
5051  *					[FOR { SELECT | INSERT | UPDATE | DELETE } ]
5052  *					[TO role, ...]
5053  *					[USING (qual)] [WITH CHECK (with check qual)]
5054  *				ALTER POLICY name ON table [TO role, ...]
5055  *					[USING (qual)] [WITH CHECK (with check qual)]
5056  *
5057  *****************************************************************************/
5058 
5059 CreatePolicyStmt:
5060 			CREATE POLICY name ON qualified_name RowSecurityDefaultPermissive
5061 				RowSecurityDefaultForCmd RowSecurityDefaultToRole
5062 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5063 				{
5064 					CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
5065 					n->policy_name = $3;
5066 					n->table = $5;
5067 					n->permissive = $6;
5068 					n->cmd_name = $7;
5069 					n->roles = $8;
5070 					n->qual = $9;
5071 					n->with_check = $10;
5072 					$$ = (Node *) n;
5073 				}
5074 		;
5075 
5076 AlterPolicyStmt:
5077 			ALTER POLICY name ON qualified_name RowSecurityOptionalToRole
5078 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5079 				{
5080 					AlterPolicyStmt *n = makeNode(AlterPolicyStmt);
5081 					n->policy_name = $3;
5082 					n->table = $5;
5083 					n->roles = $6;
5084 					n->qual = $7;
5085 					n->with_check = $8;
5086 					$$ = (Node *) n;
5087 				}
5088 		;
5089 
5090 RowSecurityOptionalExpr:
5091 			USING '(' a_expr ')'	{ $$ = $3; }
5092 			| /* EMPTY */			{ $$ = NULL; }
5093 		;
5094 
5095 RowSecurityOptionalWithCheck:
5096 			WITH CHECK '(' a_expr ')'		{ $$ = $4; }
5097 			| /* EMPTY */					{ $$ = NULL; }
5098 		;
5099 
5100 RowSecurityDefaultToRole:
5101 			TO role_list			{ $$ = $2; }
5102 			| /* EMPTY */			{ $$ = list_make1(makeRoleSpec(ROLESPEC_PUBLIC, -1)); }
5103 		;
5104 
5105 RowSecurityOptionalToRole:
5106 			TO role_list			{ $$ = $2; }
5107 			| /* EMPTY */			{ $$ = NULL; }
5108 		;
5109 
5110 RowSecurityDefaultPermissive:
5111 			AS IDENT
5112 				{
5113 					if (strcmp($2, "permissive") == 0)
5114 						$$ = true;
5115 					else if (strcmp($2, "restrictive") == 0)
5116 						$$ = false;
5117 					else
5118 						ereport(ERROR,
5119 								(errcode(ERRCODE_SYNTAX_ERROR),
5120 							 errmsg("unrecognized row security option \"%s\"", $2),
5121 								 errhint("Only PERMISSIVE or RESTRICTIVE policies are supported currently."),
5122 									 parser_errposition(@2)));
5123 
5124 				}
5125 			| /* EMPTY */			{ $$ = true; }
5126 		;
5127 
5128 RowSecurityDefaultForCmd:
5129 			FOR row_security_cmd	{ $$ = $2; }
5130 			| /* EMPTY */			{ $$ = "all"; }
5131 		;
5132 
5133 row_security_cmd:
5134 			ALL				{ $$ = "all"; }
5135 		|	SELECT			{ $$ = "select"; }
5136 		|	INSERT			{ $$ = "insert"; }
5137 		|	UPDATE			{ $$ = "update"; }
5138 		|	DELETE_P		{ $$ = "delete"; }
5139 		;
5140 
5141 /*****************************************************************************
5142  *
5143  *		QUERY:
5144  *             CREATE ACCESS METHOD name HANDLER handler_name
5145  *
5146  *****************************************************************************/
5147 
5148 CreateAmStmt: CREATE ACCESS METHOD name TYPE_P INDEX HANDLER handler_name
5149 				{
5150 					CreateAmStmt *n = makeNode(CreateAmStmt);
5151 					n->amname = $4;
5152 					n->handler_name = $8;
5153 					n->amtype = AMTYPE_INDEX;
5154 					$$ = (Node *) n;
5155 				}
5156 		;
5157 
5158 /*****************************************************************************
5159  *
5160  *		QUERIES :
5161  *				CREATE TRIGGER ...
5162  *
5163  *****************************************************************************/
5164 
5165 CreateTrigStmt:
5166 			CREATE TRIGGER name TriggerActionTime TriggerEvents ON
5167 			qualified_name TriggerReferencing TriggerForSpec TriggerWhen
5168 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
5169 				{
5170 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5171 					n->trigname = $3;
5172 					n->relation = $7;
5173 					n->funcname = $13;
5174 					n->args = $15;
5175 					n->row = $9;
5176 					n->timing = $4;
5177 					n->events = intVal(linitial($5));
5178 					n->columns = (List *) lsecond($5);
5179 					n->whenClause = $10;
5180 					n->transitionRels = $8;
5181 					n->isconstraint  = FALSE;
5182 					n->deferrable	 = FALSE;
5183 					n->initdeferred  = FALSE;
5184 					n->constrrel = NULL;
5185 					$$ = (Node *)n;
5186 				}
5187 			| CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
5188 			qualified_name OptConstrFromTable ConstraintAttributeSpec
5189 			FOR EACH ROW TriggerWhen
5190 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
5191 				{
5192 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5193 					n->trigname = $4;
5194 					n->relation = $8;
5195 					n->funcname = $17;
5196 					n->args = $19;
5197 					n->row = TRUE;
5198 					n->timing = TRIGGER_TYPE_AFTER;
5199 					n->events = intVal(linitial($6));
5200 					n->columns = (List *) lsecond($6);
5201 					n->whenClause = $14;
5202 					n->transitionRels = NIL;
5203 					n->isconstraint  = TRUE;
5204 					processCASbits($10, @10, "TRIGGER",
5205 								   &n->deferrable, &n->initdeferred, NULL,
5206 								   NULL, yyscanner);
5207 					n->constrrel = $9;
5208 					$$ = (Node *)n;
5209 				}
5210 		;
5211 
5212 TriggerActionTime:
5213 			BEFORE								{ $$ = TRIGGER_TYPE_BEFORE; }
5214 			| AFTER								{ $$ = TRIGGER_TYPE_AFTER; }
5215 			| INSTEAD OF						{ $$ = TRIGGER_TYPE_INSTEAD; }
5216 		;
5217 
5218 TriggerEvents:
5219 			TriggerOneEvent
5220 				{ $$ = $1; }
5221 			| TriggerEvents OR TriggerOneEvent
5222 				{
5223 					int		events1 = intVal(linitial($1));
5224 					int		events2 = intVal(linitial($3));
5225 					List   *columns1 = (List *) lsecond($1);
5226 					List   *columns2 = (List *) lsecond($3);
5227 
5228 					if (events1 & events2)
5229 						parser_yyerror("duplicate trigger events specified");
5230 					/*
5231 					 * concat'ing the columns lists loses information about
5232 					 * which columns went with which event, but so long as
5233 					 * only UPDATE carries columns and we disallow multiple
5234 					 * UPDATE items, it doesn't matter.  Command execution
5235 					 * should just ignore the columns for non-UPDATE events.
5236 					 */
5237 					$$ = list_make2(makeInteger(events1 | events2),
5238 									list_concat(columns1, columns2));
5239 				}
5240 		;
5241 
5242 TriggerOneEvent:
5243 			INSERT
5244 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
5245 			| DELETE_P
5246 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
5247 			| UPDATE
5248 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
5249 			| UPDATE OF columnList
5250 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
5251 			| TRUNCATE
5252 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
5253 		;
5254 
5255 TriggerReferencing:
5256 			REFERENCING TriggerTransitions			{ $$ = $2; }
5257 			| /*EMPTY*/								{ $$ = NIL; }
5258 		;
5259 
5260 TriggerTransitions:
5261 			TriggerTransition						{ $$ = list_make1($1); }
5262 			| TriggerTransitions TriggerTransition	{ $$ = lappend($1, $2); }
5263 		;
5264 
5265 TriggerTransition:
5266 			TransitionOldOrNew TransitionRowOrTable opt_as TransitionRelName
5267 				{
5268 					TriggerTransition *n = makeNode(TriggerTransition);
5269 					n->name = $4;
5270 					n->isNew = $1;
5271 					n->isTable = $2;
5272 					$$ = (Node *)n;
5273 				}
5274 		;
5275 
5276 TransitionOldOrNew:
5277 			NEW										{ $$ = TRUE; }
5278 			| OLD									{ $$ = FALSE; }
5279 		;
5280 
5281 TransitionRowOrTable:
5282 			TABLE									{ $$ = TRUE; }
5283 			/*
5284 			 * According to the standard, lack of a keyword here implies ROW.
5285 			 * Support for that would require prohibiting ROW entirely here,
5286 			 * reserving the keyword ROW, and/or requiring AS (instead of
5287 			 * allowing it to be optional, as the standard specifies) as the
5288 			 * next token.  Requiring ROW seems cleanest and easiest to
5289 			 * explain.
5290 			 */
5291 			| ROW									{ $$ = FALSE; }
5292 		;
5293 
5294 TransitionRelName:
5295 			ColId									{ $$ = $1; }
5296 		;
5297 
5298 TriggerForSpec:
5299 			FOR TriggerForOptEach TriggerForType
5300 				{
5301 					$$ = $3;
5302 				}
5303 			| /* EMPTY */
5304 				{
5305 					/*
5306 					 * If ROW/STATEMENT not specified, default to
5307 					 * STATEMENT, per SQL
5308 					 */
5309 					$$ = FALSE;
5310 				}
5311 		;
5312 
5313 TriggerForOptEach:
5314 			EACH									{}
5315 			| /*EMPTY*/								{}
5316 		;
5317 
5318 TriggerForType:
5319 			ROW										{ $$ = TRUE; }
5320 			| STATEMENT								{ $$ = FALSE; }
5321 		;
5322 
5323 TriggerWhen:
5324 			WHEN '(' a_expr ')'						{ $$ = $3; }
5325 			| /*EMPTY*/								{ $$ = NULL; }
5326 		;
5327 
5328 TriggerFuncArgs:
5329 			TriggerFuncArg							{ $$ = list_make1($1); }
5330 			| TriggerFuncArgs ',' TriggerFuncArg	{ $$ = lappend($1, $3); }
5331 			| /*EMPTY*/								{ $$ = NIL; }
5332 		;
5333 
5334 TriggerFuncArg:
5335 			Iconst
5336 				{
5337 					$$ = makeString(psprintf("%d", $1));
5338 				}
5339 			| FCONST								{ $$ = makeString($1); }
5340 			| Sconst								{ $$ = makeString($1); }
5341 			| ColLabel								{ $$ = makeString($1); }
5342 		;
5343 
5344 OptConstrFromTable:
5345 			FROM qualified_name						{ $$ = $2; }
5346 			| /*EMPTY*/								{ $$ = NULL; }
5347 		;
5348 
5349 ConstraintAttributeSpec:
5350 			/*EMPTY*/
5351 				{ $$ = 0; }
5352 			| ConstraintAttributeSpec ConstraintAttributeElem
5353 				{
5354 					/*
5355 					 * We must complain about conflicting options.
5356 					 * We could, but choose not to, complain about redundant
5357 					 * options (ie, where $2's bit is already set in $1).
5358 					 */
5359 					int		newspec = $1 | $2;
5360 
5361 					/* special message for this case */
5362 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED)) == (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED))
5363 						ereport(ERROR,
5364 								(errcode(ERRCODE_SYNTAX_ERROR),
5365 								 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
5366 								 parser_errposition(@2)));
5367 					/* generic message for other conflicts */
5368 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE)) == (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE) ||
5369 						(newspec & (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED)) == (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED))
5370 						ereport(ERROR,
5371 								(errcode(ERRCODE_SYNTAX_ERROR),
5372 								 errmsg("conflicting constraint properties"),
5373 								 parser_errposition(@2)));
5374 					$$ = newspec;
5375 				}
5376 		;
5377 
5378 ConstraintAttributeElem:
5379 			NOT DEFERRABLE					{ $$ = CAS_NOT_DEFERRABLE; }
5380 			| DEFERRABLE					{ $$ = CAS_DEFERRABLE; }
5381 			| INITIALLY IMMEDIATE			{ $$ = CAS_INITIALLY_IMMEDIATE; }
5382 			| INITIALLY DEFERRED			{ $$ = CAS_INITIALLY_DEFERRED; }
5383 			| NOT VALID						{ $$ = CAS_NOT_VALID; }
5384 			| NO INHERIT					{ $$ = CAS_NO_INHERIT; }
5385 		;
5386 
5387 
5388 /*****************************************************************************
5389  *
5390  *		QUERIES :
5391  *				CREATE EVENT TRIGGER ...
5392  *				ALTER EVENT TRIGGER ...
5393  *
5394  *****************************************************************************/
5395 
5396 CreateEventTrigStmt:
5397 			CREATE EVENT TRIGGER name ON ColLabel
5398 			EXECUTE PROCEDURE func_name '(' ')'
5399 				{
5400 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5401 					n->trigname = $4;
5402 					n->eventname = $6;
5403 					n->whenclause = NULL;
5404 					n->funcname = $9;
5405 					$$ = (Node *)n;
5406 				}
5407 		  | CREATE EVENT TRIGGER name ON ColLabel
5408 			WHEN event_trigger_when_list
5409 			EXECUTE PROCEDURE func_name '(' ')'
5410 				{
5411 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5412 					n->trigname = $4;
5413 					n->eventname = $6;
5414 					n->whenclause = $8;
5415 					n->funcname = $11;
5416 					$$ = (Node *)n;
5417 				}
5418 		;
5419 
5420 event_trigger_when_list:
5421 		  event_trigger_when_item
5422 			{ $$ = list_make1($1); }
5423 		| event_trigger_when_list AND event_trigger_when_item
5424 			{ $$ = lappend($1, $3); }
5425 		;
5426 
5427 event_trigger_when_item:
5428 		ColId IN_P '(' event_trigger_value_list ')'
5429 			{ $$ = makeDefElem($1, (Node *) $4, @1); }
5430 		;
5431 
5432 event_trigger_value_list:
5433 		  SCONST
5434 			{ $$ = list_make1(makeString($1)); }
5435 		| event_trigger_value_list ',' SCONST
5436 			{ $$ = lappend($1, makeString($3)); }
5437 		;
5438 
5439 AlterEventTrigStmt:
5440 			ALTER EVENT TRIGGER name enable_trigger
5441 				{
5442 					AlterEventTrigStmt *n = makeNode(AlterEventTrigStmt);
5443 					n->trigname = $4;
5444 					n->tgenabled = $5;
5445 					$$ = (Node *) n;
5446 				}
5447 		;
5448 
5449 enable_trigger:
5450 			ENABLE_P					{ $$ = TRIGGER_FIRES_ON_ORIGIN; }
5451 			| ENABLE_P REPLICA			{ $$ = TRIGGER_FIRES_ON_REPLICA; }
5452 			| ENABLE_P ALWAYS			{ $$ = TRIGGER_FIRES_ALWAYS; }
5453 			| DISABLE_P					{ $$ = TRIGGER_DISABLED; }
5454 		;
5455 
5456 /*****************************************************************************
5457  *
5458  *		QUERIES :
5459  *				CREATE ASSERTION ...
5460  *				DROP ASSERTION ...
5461  *
5462  *****************************************************************************/
5463 
5464 CreateAssertStmt:
5465 			CREATE ASSERTION name CHECK '(' a_expr ')'
5466 			ConstraintAttributeSpec
5467 				{
5468 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5469 					n->trigname = $3;
5470 					n->args = list_make1($6);
5471 					n->isconstraint  = TRUE;
5472 					processCASbits($8, @8, "ASSERTION",
5473 								   &n->deferrable, &n->initdeferred, NULL,
5474 								   NULL, yyscanner);
5475 
5476 					ereport(ERROR,
5477 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5478 							 errmsg("CREATE ASSERTION is not yet implemented")));
5479 
5480 					$$ = (Node *)n;
5481 				}
5482 		;
5483 
5484 DropAssertStmt:
5485 			DROP ASSERTION name opt_drop_behavior
5486 				{
5487 					DropStmt *n = makeNode(DropStmt);
5488 					n->objects = NIL;
5489 					n->behavior = $4;
5490 					n->removeType = OBJECT_TRIGGER; /* XXX */
5491 					ereport(ERROR,
5492 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5493 							 errmsg("DROP ASSERTION is not yet implemented")));
5494 					$$ = (Node *) n;
5495 				}
5496 		;
5497 
5498 
5499 /*****************************************************************************
5500  *
5501  *		QUERY :
5502  *				define (aggregate,operator,type)
5503  *
5504  *****************************************************************************/
5505 
5506 DefineStmt:
5507 			CREATE AGGREGATE func_name aggr_args definition
5508 				{
5509 					DefineStmt *n = makeNode(DefineStmt);
5510 					n->kind = OBJECT_AGGREGATE;
5511 					n->oldstyle = false;
5512 					n->defnames = $3;
5513 					n->args = $4;
5514 					n->definition = $5;
5515 					$$ = (Node *)n;
5516 				}
5517 			| CREATE AGGREGATE func_name old_aggr_definition
5518 				{
5519 					/* old-style (pre-8.2) syntax for CREATE AGGREGATE */
5520 					DefineStmt *n = makeNode(DefineStmt);
5521 					n->kind = OBJECT_AGGREGATE;
5522 					n->oldstyle = true;
5523 					n->defnames = $3;
5524 					n->args = NIL;
5525 					n->definition = $4;
5526 					$$ = (Node *)n;
5527 				}
5528 			| CREATE OPERATOR any_operator definition
5529 				{
5530 					DefineStmt *n = makeNode(DefineStmt);
5531 					n->kind = OBJECT_OPERATOR;
5532 					n->oldstyle = false;
5533 					n->defnames = $3;
5534 					n->args = NIL;
5535 					n->definition = $4;
5536 					$$ = (Node *)n;
5537 				}
5538 			| CREATE TYPE_P any_name definition
5539 				{
5540 					DefineStmt *n = makeNode(DefineStmt);
5541 					n->kind = OBJECT_TYPE;
5542 					n->oldstyle = false;
5543 					n->defnames = $3;
5544 					n->args = NIL;
5545 					n->definition = $4;
5546 					$$ = (Node *)n;
5547 				}
5548 			| CREATE TYPE_P any_name
5549 				{
5550 					/* Shell type (identified by lack of definition) */
5551 					DefineStmt *n = makeNode(DefineStmt);
5552 					n->kind = OBJECT_TYPE;
5553 					n->oldstyle = false;
5554 					n->defnames = $3;
5555 					n->args = NIL;
5556 					n->definition = NIL;
5557 					$$ = (Node *)n;
5558 				}
5559 			| CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
5560 				{
5561 					CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
5562 
5563 					/* can't use qualified_name, sigh */
5564 					n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
5565 					n->coldeflist = $6;
5566 					$$ = (Node *)n;
5567 				}
5568 			| CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
5569 				{
5570 					CreateEnumStmt *n = makeNode(CreateEnumStmt);
5571 					n->typeName = $3;
5572 					n->vals = $7;
5573 					$$ = (Node *)n;
5574 				}
5575 			| CREATE TYPE_P any_name AS RANGE definition
5576 				{
5577 					CreateRangeStmt *n = makeNode(CreateRangeStmt);
5578 					n->typeName = $3;
5579 					n->params	= $6;
5580 					$$ = (Node *)n;
5581 				}
5582 			| CREATE TEXT_P SEARCH PARSER any_name definition
5583 				{
5584 					DefineStmt *n = makeNode(DefineStmt);
5585 					n->kind = OBJECT_TSPARSER;
5586 					n->args = NIL;
5587 					n->defnames = $5;
5588 					n->definition = $6;
5589 					$$ = (Node *)n;
5590 				}
5591 			| CREATE TEXT_P SEARCH DICTIONARY any_name definition
5592 				{
5593 					DefineStmt *n = makeNode(DefineStmt);
5594 					n->kind = OBJECT_TSDICTIONARY;
5595 					n->args = NIL;
5596 					n->defnames = $5;
5597 					n->definition = $6;
5598 					$$ = (Node *)n;
5599 				}
5600 			| CREATE TEXT_P SEARCH TEMPLATE any_name definition
5601 				{
5602 					DefineStmt *n = makeNode(DefineStmt);
5603 					n->kind = OBJECT_TSTEMPLATE;
5604 					n->args = NIL;
5605 					n->defnames = $5;
5606 					n->definition = $6;
5607 					$$ = (Node *)n;
5608 				}
5609 			| CREATE TEXT_P SEARCH CONFIGURATION any_name definition
5610 				{
5611 					DefineStmt *n = makeNode(DefineStmt);
5612 					n->kind = OBJECT_TSCONFIGURATION;
5613 					n->args = NIL;
5614 					n->defnames = $5;
5615 					n->definition = $6;
5616 					$$ = (Node *)n;
5617 				}
5618 			| CREATE COLLATION any_name definition
5619 				{
5620 					DefineStmt *n = makeNode(DefineStmt);
5621 					n->kind = OBJECT_COLLATION;
5622 					n->args = NIL;
5623 					n->defnames = $3;
5624 					n->definition = $4;
5625 					$$ = (Node *)n;
5626 				}
5627 			| CREATE COLLATION IF_P NOT EXISTS any_name definition
5628 				{
5629 					DefineStmt *n = makeNode(DefineStmt);
5630 					n->kind = OBJECT_COLLATION;
5631 					n->args = NIL;
5632 					n->defnames = $6;
5633 					n->definition = $7;
5634 					n->if_not_exists = true;
5635 					$$ = (Node *)n;
5636 				}
5637 			| CREATE COLLATION any_name FROM any_name
5638 				{
5639 					DefineStmt *n = makeNode(DefineStmt);
5640 					n->kind = OBJECT_COLLATION;
5641 					n->args = NIL;
5642 					n->defnames = $3;
5643 					n->definition = list_make1(makeDefElem("from", (Node *) $5, @5));
5644 					$$ = (Node *)n;
5645 				}
5646 			| CREATE COLLATION IF_P NOT EXISTS any_name FROM any_name
5647 				{
5648 					DefineStmt *n = makeNode(DefineStmt);
5649 					n->kind = OBJECT_COLLATION;
5650 					n->args = NIL;
5651 					n->defnames = $6;
5652 					n->definition = list_make1(makeDefElem("from", (Node *) $8, @8));
5653 					n->if_not_exists = true;
5654 					$$ = (Node *)n;
5655 				}
5656 		;
5657 
5658 definition: '(' def_list ')'						{ $$ = $2; }
5659 		;
5660 
5661 def_list:	def_elem								{ $$ = list_make1($1); }
5662 			| def_list ',' def_elem					{ $$ = lappend($1, $3); }
5663 		;
5664 
5665 def_elem:	ColLabel '=' def_arg
5666 				{
5667 					$$ = makeDefElem($1, (Node *) $3, @1);
5668 				}
5669 			| ColLabel
5670 				{
5671 					$$ = makeDefElem($1, NULL, @1);
5672 				}
5673 		;
5674 
5675 /* Note: any simple identifier will be returned as a type name! */
5676 def_arg:	func_type						{ $$ = (Node *)$1; }
5677 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
5678 			| qual_all_Op					{ $$ = (Node *)$1; }
5679 			| NumericOnly					{ $$ = (Node *)$1; }
5680 			| Sconst						{ $$ = (Node *)makeString($1); }
5681 			| NONE							{ $$ = (Node *)makeString(pstrdup($1)); }
5682 		;
5683 
5684 old_aggr_definition: '(' old_aggr_list ')'			{ $$ = $2; }
5685 		;
5686 
5687 old_aggr_list: old_aggr_elem						{ $$ = list_make1($1); }
5688 			| old_aggr_list ',' old_aggr_elem		{ $$ = lappend($1, $3); }
5689 		;
5690 
5691 /*
5692  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
5693  * the item names needed in old aggregate definitions are likely to become
5694  * SQL keywords.
5695  */
5696 old_aggr_elem:  IDENT '=' def_arg
5697 				{
5698 					$$ = makeDefElem($1, (Node *)$3, @1);
5699 				}
5700 		;
5701 
5702 opt_enum_val_list:
5703 		enum_val_list							{ $$ = $1; }
5704 		| /*EMPTY*/								{ $$ = NIL; }
5705 		;
5706 
5707 enum_val_list:	Sconst
5708 				{ $$ = list_make1(makeString($1)); }
5709 			| enum_val_list ',' Sconst
5710 				{ $$ = lappend($1, makeString($3)); }
5711 		;
5712 
5713 /*****************************************************************************
5714  *
5715  *	ALTER TYPE enumtype ADD ...
5716  *
5717  *****************************************************************************/
5718 
5719 AlterEnumStmt:
5720 		ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst
5721 			{
5722 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5723 				n->typeName = $3;
5724 				n->oldVal = NULL;
5725 				n->newVal = $7;
5726 				n->newValNeighbor = NULL;
5727 				n->newValIsAfter = true;
5728 				n->skipIfNewValExists = $6;
5729 				$$ = (Node *) n;
5730 			}
5731 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst BEFORE Sconst
5732 			{
5733 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5734 				n->typeName = $3;
5735 				n->oldVal = NULL;
5736 				n->newVal = $7;
5737 				n->newValNeighbor = $9;
5738 				n->newValIsAfter = false;
5739 				n->skipIfNewValExists = $6;
5740 				$$ = (Node *) n;
5741 			}
5742 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst AFTER Sconst
5743 			{
5744 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5745 				n->typeName = $3;
5746 				n->oldVal = NULL;
5747 				n->newVal = $7;
5748 				n->newValNeighbor = $9;
5749 				n->newValIsAfter = true;
5750 				n->skipIfNewValExists = $6;
5751 				$$ = (Node *) n;
5752 			}
5753 		 | ALTER TYPE_P any_name RENAME VALUE_P Sconst TO Sconst
5754 			{
5755 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5756 				n->typeName = $3;
5757 				n->oldVal = $6;
5758 				n->newVal = $8;
5759 				n->newValNeighbor = NULL;
5760 				n->newValIsAfter = false;
5761 				n->skipIfNewValExists = false;
5762 				$$ = (Node *) n;
5763 			}
5764 		 ;
5765 
5766 opt_if_not_exists: IF_P NOT EXISTS              { $$ = true; }
5767 		| /* empty */                          { $$ = false; }
5768 		;
5769 
5770 
5771 /*****************************************************************************
5772  *
5773  *		QUERIES :
5774  *				CREATE OPERATOR CLASS ...
5775  *				CREATE OPERATOR FAMILY ...
5776  *				ALTER OPERATOR FAMILY ...
5777  *				DROP OPERATOR CLASS ...
5778  *				DROP OPERATOR FAMILY ...
5779  *
5780  *****************************************************************************/
5781 
5782 CreateOpClassStmt:
5783 			CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
5784 			USING access_method opt_opfamily AS opclass_item_list
5785 				{
5786 					CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
5787 					n->opclassname = $4;
5788 					n->isDefault = $5;
5789 					n->datatype = $8;
5790 					n->amname = $10;
5791 					n->opfamilyname = $11;
5792 					n->items = $13;
5793 					$$ = (Node *) n;
5794 				}
5795 		;
5796 
5797 opclass_item_list:
5798 			opclass_item							{ $$ = list_make1($1); }
5799 			| opclass_item_list ',' opclass_item	{ $$ = lappend($1, $3); }
5800 		;
5801 
5802 opclass_item:
5803 			OPERATOR Iconst any_operator opclass_purpose opt_recheck
5804 				{
5805 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5806 					ObjectWithArgs *owa = makeNode(ObjectWithArgs);
5807 					owa->objname = $3;
5808 					owa->objargs = NIL;
5809 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5810 					n->name = owa;
5811 					n->number = $2;
5812 					n->order_family = $4;
5813 					$$ = (Node *) n;
5814 				}
5815 			| OPERATOR Iconst operator_with_argtypes opclass_purpose
5816 			  opt_recheck
5817 				{
5818 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5819 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5820 					n->name = $3;
5821 					n->number = $2;
5822 					n->order_family = $4;
5823 					$$ = (Node *) n;
5824 				}
5825 			| FUNCTION Iconst function_with_argtypes
5826 				{
5827 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5828 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5829 					n->name = $3;
5830 					n->number = $2;
5831 					$$ = (Node *) n;
5832 				}
5833 			| FUNCTION Iconst '(' type_list ')' function_with_argtypes
5834 				{
5835 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5836 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5837 					n->name = $6;
5838 					n->number = $2;
5839 					n->class_args = $4;
5840 					$$ = (Node *) n;
5841 				}
5842 			| STORAGE Typename
5843 				{
5844 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5845 					n->itemtype = OPCLASS_ITEM_STORAGETYPE;
5846 					n->storedtype = $2;
5847 					$$ = (Node *) n;
5848 				}
5849 		;
5850 
5851 opt_default:	DEFAULT						{ $$ = TRUE; }
5852 			| /*EMPTY*/						{ $$ = FALSE; }
5853 		;
5854 
5855 opt_opfamily:	FAMILY any_name				{ $$ = $2; }
5856 			| /*EMPTY*/						{ $$ = NIL; }
5857 		;
5858 
5859 opclass_purpose: FOR SEARCH					{ $$ = NIL; }
5860 			| FOR ORDER BY any_name			{ $$ = $4; }
5861 			| /*EMPTY*/						{ $$ = NIL; }
5862 		;
5863 
5864 opt_recheck:	RECHECK
5865 				{
5866 					/*
5867 					 * RECHECK no longer does anything in opclass definitions,
5868 					 * but we still accept it to ease porting of old database
5869 					 * dumps.
5870 					 */
5871 					ereport(NOTICE,
5872 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5873 							 errmsg("RECHECK is no longer required"),
5874 							 errhint("Update your data type."),
5875 							 parser_errposition(@1)));
5876 					$$ = TRUE;
5877 				}
5878 			| /*EMPTY*/						{ $$ = FALSE; }
5879 		;
5880 
5881 
5882 CreateOpFamilyStmt:
5883 			CREATE OPERATOR FAMILY any_name USING access_method
5884 				{
5885 					CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
5886 					n->opfamilyname = $4;
5887 					n->amname = $6;
5888 					$$ = (Node *) n;
5889 				}
5890 		;
5891 
5892 AlterOpFamilyStmt:
5893 			ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
5894 				{
5895 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
5896 					n->opfamilyname = $4;
5897 					n->amname = $6;
5898 					n->isDrop = false;
5899 					n->items = $8;
5900 					$$ = (Node *) n;
5901 				}
5902 			| ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
5903 				{
5904 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
5905 					n->opfamilyname = $4;
5906 					n->amname = $6;
5907 					n->isDrop = true;
5908 					n->items = $8;
5909 					$$ = (Node *) n;
5910 				}
5911 		;
5912 
5913 opclass_drop_list:
5914 			opclass_drop							{ $$ = list_make1($1); }
5915 			| opclass_drop_list ',' opclass_drop	{ $$ = lappend($1, $3); }
5916 		;
5917 
5918 opclass_drop:
5919 			OPERATOR Iconst '(' type_list ')'
5920 				{
5921 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5922 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5923 					n->number = $2;
5924 					n->class_args = $4;
5925 					$$ = (Node *) n;
5926 				}
5927 			| FUNCTION Iconst '(' type_list ')'
5928 				{
5929 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5930 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5931 					n->number = $2;
5932 					n->class_args = $4;
5933 					$$ = (Node *) n;
5934 				}
5935 		;
5936 
5937 
5938 DropOpClassStmt:
5939 			DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
5940 				{
5941 					DropStmt *n = makeNode(DropStmt);
5942 					n->objects = list_make1(lcons(makeString($6), $4));
5943 					n->removeType = OBJECT_OPCLASS;
5944 					n->behavior = $7;
5945 					n->missing_ok = false;
5946 					n->concurrent = false;
5947 					$$ = (Node *) n;
5948 				}
5949 			| DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
5950 				{
5951 					DropStmt *n = makeNode(DropStmt);
5952 					n->objects = list_make1(lcons(makeString($8), $6));
5953 					n->removeType = OBJECT_OPCLASS;
5954 					n->behavior = $9;
5955 					n->missing_ok = true;
5956 					n->concurrent = false;
5957 					$$ = (Node *) n;
5958 				}
5959 		;
5960 
5961 DropOpFamilyStmt:
5962 			DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
5963 				{
5964 					DropStmt *n = makeNode(DropStmt);
5965 					n->objects = list_make1(lcons(makeString($6), $4));
5966 					n->removeType = OBJECT_OPFAMILY;
5967 					n->behavior = $7;
5968 					n->missing_ok = false;
5969 					n->concurrent = false;
5970 					$$ = (Node *) n;
5971 				}
5972 			| DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
5973 				{
5974 					DropStmt *n = makeNode(DropStmt);
5975 					n->objects = list_make1(lcons(makeString($8), $6));
5976 					n->removeType = OBJECT_OPFAMILY;
5977 					n->behavior = $9;
5978 					n->missing_ok = true;
5979 					n->concurrent = false;
5980 					$$ = (Node *) n;
5981 				}
5982 		;
5983 
5984 
5985 /*****************************************************************************
5986  *
5987  *		QUERY:
5988  *
5989  *		DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
5990  *		REASSIGN OWNED BY username [, username ...] TO username
5991  *
5992  *****************************************************************************/
5993 DropOwnedStmt:
5994 			DROP OWNED BY role_list opt_drop_behavior
5995 				{
5996 					DropOwnedStmt *n = makeNode(DropOwnedStmt);
5997 					n->roles = $4;
5998 					n->behavior = $5;
5999 					$$ = (Node *)n;
6000 				}
6001 		;
6002 
6003 ReassignOwnedStmt:
6004 			REASSIGN OWNED BY role_list TO RoleSpec
6005 				{
6006 					ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
6007 					n->roles = $4;
6008 					n->newrole = $6;
6009 					$$ = (Node *)n;
6010 				}
6011 		;
6012 
6013 /*****************************************************************************
6014  *
6015  *		QUERY:
6016  *
6017  *		DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
6018  *           [ RESTRICT | CASCADE ]
6019  *
6020  *****************************************************************************/
6021 
6022 DropStmt:	DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
6023 				{
6024 					DropStmt *n = makeNode(DropStmt);
6025 					n->removeType = $2;
6026 					n->missing_ok = TRUE;
6027 					n->objects = $5;
6028 					n->behavior = $6;
6029 					n->concurrent = false;
6030 					$$ = (Node *)n;
6031 				}
6032 			| DROP drop_type_any_name any_name_list opt_drop_behavior
6033 				{
6034 					DropStmt *n = makeNode(DropStmt);
6035 					n->removeType = $2;
6036 					n->missing_ok = FALSE;
6037 					n->objects = $3;
6038 					n->behavior = $4;
6039 					n->concurrent = false;
6040 					$$ = (Node *)n;
6041 				}
6042 			| DROP drop_type_name IF_P EXISTS name_list opt_drop_behavior
6043 				{
6044 					DropStmt *n = makeNode(DropStmt);
6045 					n->removeType = $2;
6046 					n->missing_ok = TRUE;
6047 					n->objects = $5;
6048 					n->behavior = $6;
6049 					n->concurrent = false;
6050 					$$ = (Node *)n;
6051 				}
6052 			| DROP drop_type_name name_list opt_drop_behavior
6053 				{
6054 					DropStmt *n = makeNode(DropStmt);
6055 					n->removeType = $2;
6056 					n->missing_ok = FALSE;
6057 					n->objects = $3;
6058 					n->behavior = $4;
6059 					n->concurrent = false;
6060 					$$ = (Node *)n;
6061 				}
6062 			| DROP drop_type_name_on_any_name name ON any_name opt_drop_behavior
6063 				{
6064 					DropStmt *n = makeNode(DropStmt);
6065 					n->removeType = $2;
6066 					n->objects = list_make1(lappend($5, makeString($3)));
6067 					n->behavior = $6;
6068 					n->missing_ok = false;
6069 					n->concurrent = false;
6070 					$$ = (Node *) n;
6071 				}
6072 			| DROP drop_type_name_on_any_name IF_P EXISTS name ON any_name opt_drop_behavior
6073 				{
6074 					DropStmt *n = makeNode(DropStmt);
6075 					n->removeType = $2;
6076 					n->objects = list_make1(lappend($7, makeString($5)));
6077 					n->behavior = $8;
6078 					n->missing_ok = true;
6079 					n->concurrent = false;
6080 					$$ = (Node *) n;
6081 				}
6082 			| DROP TYPE_P type_name_list opt_drop_behavior
6083 				{
6084 					DropStmt *n = makeNode(DropStmt);
6085 					n->removeType = OBJECT_TYPE;
6086 					n->missing_ok = FALSE;
6087 					n->objects = $3;
6088 					n->behavior = $4;
6089 					n->concurrent = false;
6090 					$$ = (Node *) n;
6091 				}
6092 			| DROP TYPE_P IF_P EXISTS type_name_list opt_drop_behavior
6093 				{
6094 					DropStmt *n = makeNode(DropStmt);
6095 					n->removeType = OBJECT_TYPE;
6096 					n->missing_ok = TRUE;
6097 					n->objects = $5;
6098 					n->behavior = $6;
6099 					n->concurrent = false;
6100 					$$ = (Node *) n;
6101 				}
6102 			| DROP DOMAIN_P type_name_list opt_drop_behavior
6103 				{
6104 					DropStmt *n = makeNode(DropStmt);
6105 					n->removeType = OBJECT_DOMAIN;
6106 					n->missing_ok = FALSE;
6107 					n->objects = $3;
6108 					n->behavior = $4;
6109 					n->concurrent = false;
6110 					$$ = (Node *) n;
6111 				}
6112 			| DROP DOMAIN_P IF_P EXISTS type_name_list opt_drop_behavior
6113 				{
6114 					DropStmt *n = makeNode(DropStmt);
6115 					n->removeType = OBJECT_DOMAIN;
6116 					n->missing_ok = TRUE;
6117 					n->objects = $5;
6118 					n->behavior = $6;
6119 					n->concurrent = false;
6120 					$$ = (Node *) n;
6121 				}
6122 			| DROP INDEX CONCURRENTLY any_name_list opt_drop_behavior
6123 				{
6124 					DropStmt *n = makeNode(DropStmt);
6125 					n->removeType = OBJECT_INDEX;
6126 					n->missing_ok = FALSE;
6127 					n->objects = $4;
6128 					n->behavior = $5;
6129 					n->concurrent = true;
6130 					$$ = (Node *)n;
6131 				}
6132 			| DROP INDEX CONCURRENTLY IF_P EXISTS any_name_list opt_drop_behavior
6133 				{
6134 					DropStmt *n = makeNode(DropStmt);
6135 					n->removeType = OBJECT_INDEX;
6136 					n->missing_ok = TRUE;
6137 					n->objects = $6;
6138 					n->behavior = $7;
6139 					n->concurrent = true;
6140 					$$ = (Node *)n;
6141 				}
6142 		;
6143 
6144 /* object types taking any_name_list */
6145 drop_type_any_name:
6146 			TABLE									{ $$ = OBJECT_TABLE; }
6147 			| SEQUENCE								{ $$ = OBJECT_SEQUENCE; }
6148 			| VIEW									{ $$ = OBJECT_VIEW; }
6149 			| MATERIALIZED VIEW						{ $$ = OBJECT_MATVIEW; }
6150 			| INDEX									{ $$ = OBJECT_INDEX; }
6151 			| FOREIGN TABLE							{ $$ = OBJECT_FOREIGN_TABLE; }
6152 			| COLLATION								{ $$ = OBJECT_COLLATION; }
6153 			| CONVERSION_P							{ $$ = OBJECT_CONVERSION; }
6154 			| STATISTICS							{ $$ = OBJECT_STATISTIC_EXT; }
6155 			| TEXT_P SEARCH PARSER					{ $$ = OBJECT_TSPARSER; }
6156 			| TEXT_P SEARCH DICTIONARY				{ $$ = OBJECT_TSDICTIONARY; }
6157 			| TEXT_P SEARCH TEMPLATE				{ $$ = OBJECT_TSTEMPLATE; }
6158 			| TEXT_P SEARCH CONFIGURATION			{ $$ = OBJECT_TSCONFIGURATION; }
6159 		;
6160 
6161 /* object types taking name_list */
6162 drop_type_name:
6163 			ACCESS METHOD							{ $$ = OBJECT_ACCESS_METHOD; }
6164 			| EVENT TRIGGER							{ $$ = OBJECT_EVENT_TRIGGER; }
6165 			| EXTENSION								{ $$ = OBJECT_EXTENSION; }
6166 			| FOREIGN DATA_P WRAPPER				{ $$ = OBJECT_FDW; }
6167 			| PUBLICATION							{ $$ = OBJECT_PUBLICATION; }
6168 			| SCHEMA								{ $$ = OBJECT_SCHEMA; }
6169 			| SERVER								{ $$ = OBJECT_FOREIGN_SERVER; }
6170 		;
6171 
6172 /* object types attached to a table */
6173 drop_type_name_on_any_name:
6174 			POLICY									{ $$ = OBJECT_POLICY; }
6175 			| RULE									{ $$ = OBJECT_RULE; }
6176 			| TRIGGER								{ $$ = OBJECT_TRIGGER; }
6177 		;
6178 
6179 any_name_list:
6180 			any_name								{ $$ = list_make1($1); }
6181 			| any_name_list ',' any_name			{ $$ = lappend($1, $3); }
6182 		;
6183 
6184 any_name:	ColId						{ $$ = list_make1(makeString($1)); }
6185 			| ColId attrs				{ $$ = lcons(makeString($1), $2); }
6186 		;
6187 
6188 attrs:		'.' attr_name
6189 					{ $$ = list_make1(makeString($2)); }
6190 			| attrs '.' attr_name
6191 					{ $$ = lappend($1, makeString($3)); }
6192 		;
6193 
6194 type_name_list:
6195 			Typename								{ $$ = list_make1($1); }
6196 			| type_name_list ',' Typename			{ $$ = lappend($1, $3); }
6197 		;
6198 
6199 /*****************************************************************************
6200  *
6201  *		QUERY:
6202  *				truncate table relname1, relname2, ...
6203  *
6204  *****************************************************************************/
6205 
6206 TruncateStmt:
6207 			TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
6208 				{
6209 					TruncateStmt *n = makeNode(TruncateStmt);
6210 					n->relations = $3;
6211 					n->restart_seqs = $4;
6212 					n->behavior = $5;
6213 					$$ = (Node *)n;
6214 				}
6215 		;
6216 
6217 opt_restart_seqs:
6218 			CONTINUE_P IDENTITY_P		{ $$ = false; }
6219 			| RESTART IDENTITY_P		{ $$ = true; }
6220 			| /* EMPTY */				{ $$ = false; }
6221 		;
6222 
6223 /*****************************************************************************
6224  *
6225  *	The COMMENT ON statement can take different forms based upon the type of
6226  *	the object associated with the comment. The form of the statement is:
6227  *
6228  *	COMMENT ON [ [ ACCESS METHOD | CONVERSION | COLLATION |
6229  *                 DATABASE | DOMAIN |
6230  *                 EXTENSION | EVENT TRIGGER | FOREIGN DATA WRAPPER |
6231  *                 FOREIGN TABLE | INDEX | [PROCEDURAL] LANGUAGE |
6232  *                 MATERIALIZED VIEW | POLICY | ROLE | SCHEMA | SEQUENCE |
6233  *                 SERVER | STATISTICS | TABLE | TABLESPACE |
6234  *                 TEXT SEARCH CONFIGURATION | TEXT SEARCH DICTIONARY |
6235  *                 TEXT SEARCH PARSER | TEXT SEARCH TEMPLATE | TYPE |
6236  *                 VIEW] <objname> |
6237  *				 AGGREGATE <aggname> (arg1, ...) |
6238  *				 CAST (<src type> AS <dst type>) |
6239  *				 COLUMN <relname>.<colname> |
6240  *				 CONSTRAINT <constraintname> ON <relname> |
6241  *				 CONSTRAINT <constraintname> ON DOMAIN <domainname> |
6242  *				 FUNCTION <funcname> (arg1, arg2, ...) |
6243  *				 LARGE OBJECT <oid> |
6244  *				 OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
6245  *				 OPERATOR CLASS <name> USING <access-method> |
6246  *				 OPERATOR FAMILY <name> USING <access-method> |
6247  *				 RULE <rulename> ON <relname> |
6248  *				 TRIGGER <triggername> ON <relname> ]
6249  *			   IS { 'text' | NULL }
6250  *
6251  *****************************************************************************/
6252 
6253 CommentStmt:
6254 			COMMENT ON comment_type_any_name any_name IS comment_text
6255 				{
6256 					CommentStmt *n = makeNode(CommentStmt);
6257 					n->objtype = $3;
6258 					n->object = (Node *) $4;
6259 					n->comment = $6;
6260 					$$ = (Node *) n;
6261 				}
6262 			| COMMENT ON comment_type_name name IS comment_text
6263 				{
6264 					CommentStmt *n = makeNode(CommentStmt);
6265 					n->objtype = $3;
6266 					n->object = (Node *) makeString($4);
6267 					n->comment = $6;
6268 					$$ = (Node *) n;
6269 				}
6270 			| COMMENT ON TYPE_P Typename IS comment_text
6271 				{
6272 					CommentStmt *n = makeNode(CommentStmt);
6273 					n->objtype = OBJECT_TYPE;
6274 					n->object = (Node *) $4;
6275 					n->comment = $6;
6276 					$$ = (Node *) n;
6277 				}
6278 			| COMMENT ON DOMAIN_P Typename IS comment_text
6279 				{
6280 					CommentStmt *n = makeNode(CommentStmt);
6281 					n->objtype = OBJECT_DOMAIN;
6282 					n->object = (Node *) $4;
6283 					n->comment = $6;
6284 					$$ = (Node *) n;
6285 				}
6286 			| COMMENT ON AGGREGATE aggregate_with_argtypes IS comment_text
6287 				{
6288 					CommentStmt *n = makeNode(CommentStmt);
6289 					n->objtype = OBJECT_AGGREGATE;
6290 					n->object = (Node *) $4;
6291 					n->comment = $6;
6292 					$$ = (Node *) n;
6293 				}
6294 			| COMMENT ON FUNCTION function_with_argtypes IS comment_text
6295 				{
6296 					CommentStmt *n = makeNode(CommentStmt);
6297 					n->objtype = OBJECT_FUNCTION;
6298 					n->object = (Node *) $4;
6299 					n->comment = $6;
6300 					$$ = (Node *) n;
6301 				}
6302 			| COMMENT ON OPERATOR operator_with_argtypes IS comment_text
6303 				{
6304 					CommentStmt *n = makeNode(CommentStmt);
6305 					n->objtype = OBJECT_OPERATOR;
6306 					n->object = (Node *) $4;
6307 					n->comment = $6;
6308 					$$ = (Node *) n;
6309 				}
6310 			| COMMENT ON CONSTRAINT name ON any_name IS comment_text
6311 				{
6312 					CommentStmt *n = makeNode(CommentStmt);
6313 					n->objtype = OBJECT_TABCONSTRAINT;
6314 					n->object = (Node *) lappend($6, makeString($4));
6315 					n->comment = $8;
6316 					$$ = (Node *) n;
6317 				}
6318 			| COMMENT ON CONSTRAINT name ON DOMAIN_P any_name IS comment_text
6319 				{
6320 					CommentStmt *n = makeNode(CommentStmt);
6321 					n->objtype = OBJECT_DOMCONSTRAINT;
6322 					/*
6323 					 * should use Typename not any_name in the production, but
6324 					 * there's a shift/reduce conflict if we do that, so fix it
6325 					 * up here.
6326 					 */
6327 					n->object = (Node *) list_make2(makeTypeNameFromNameList($7), makeString($4));
6328 					n->comment = $9;
6329 					$$ = (Node *) n;
6330 				}
6331 			| COMMENT ON POLICY name ON any_name IS comment_text
6332 				{
6333 					CommentStmt *n = makeNode(CommentStmt);
6334 					n->objtype = OBJECT_POLICY;
6335 					n->object = (Node *) lappend($6, makeString($4));
6336 					n->comment = $8;
6337 					$$ = (Node *) n;
6338 				}
6339 			| COMMENT ON RULE name ON any_name IS comment_text
6340 				{
6341 					CommentStmt *n = makeNode(CommentStmt);
6342 					n->objtype = OBJECT_RULE;
6343 					n->object = (Node *) lappend($6, makeString($4));
6344 					n->comment = $8;
6345 					$$ = (Node *) n;
6346 				}
6347 			| COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
6348 				{
6349 					CommentStmt *n = makeNode(CommentStmt);
6350 					n->objtype = OBJECT_TRANSFORM;
6351 					n->object = (Node *) list_make2($5, makeString($7));
6352 					n->comment = $9;
6353 					$$ = (Node *) n;
6354 				}
6355 			| COMMENT ON TRIGGER name ON any_name IS comment_text
6356 				{
6357 					CommentStmt *n = makeNode(CommentStmt);
6358 					n->objtype = OBJECT_TRIGGER;
6359 					n->object = (Node *) lappend($6, makeString($4));
6360 					n->comment = $8;
6361 					$$ = (Node *) n;
6362 				}
6363 			| COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
6364 				{
6365 					CommentStmt *n = makeNode(CommentStmt);
6366 					n->objtype = OBJECT_OPCLASS;
6367 					n->object = (Node *) lcons(makeString($7), $5);
6368 					n->comment = $9;
6369 					$$ = (Node *) n;
6370 				}
6371 			| COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
6372 				{
6373 					CommentStmt *n = makeNode(CommentStmt);
6374 					n->objtype = OBJECT_OPFAMILY;
6375 					n->object = (Node *) lcons(makeString($7), $5);
6376 					n->comment = $9;
6377 					$$ = (Node *) n;
6378 				}
6379 			| COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
6380 				{
6381 					CommentStmt *n = makeNode(CommentStmt);
6382 					n->objtype = OBJECT_LARGEOBJECT;
6383 					n->object = (Node *) $5;
6384 					n->comment = $7;
6385 					$$ = (Node *) n;
6386 				}
6387 			| COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
6388 				{
6389 					CommentStmt *n = makeNode(CommentStmt);
6390 					n->objtype = OBJECT_CAST;
6391 					n->object = (Node *) list_make2($5, $7);
6392 					n->comment = $10;
6393 					$$ = (Node *) n;
6394 				}
6395 		;
6396 
6397 /* object types taking any_name */
6398 comment_type_any_name:
6399 			COLUMN								{ $$ = OBJECT_COLUMN; }
6400 			| INDEX								{ $$ = OBJECT_INDEX; }
6401 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6402 			| STATISTICS						{ $$ = OBJECT_STATISTIC_EXT; }
6403 			| TABLE								{ $$ = OBJECT_TABLE; }
6404 			| VIEW								{ $$ = OBJECT_VIEW; }
6405 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6406 			| COLLATION							{ $$ = OBJECT_COLLATION; }
6407 			| CONVERSION_P						{ $$ = OBJECT_CONVERSION; }
6408 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6409 			| TEXT_P SEARCH CONFIGURATION		{ $$ = OBJECT_TSCONFIGURATION; }
6410 			| TEXT_P SEARCH DICTIONARY			{ $$ = OBJECT_TSDICTIONARY; }
6411 			| TEXT_P SEARCH PARSER				{ $$ = OBJECT_TSPARSER; }
6412 			| TEXT_P SEARCH TEMPLATE			{ $$ = OBJECT_TSTEMPLATE; }
6413 		;
6414 
6415 /* object types taking name */
6416 comment_type_name:
6417 			ACCESS METHOD						{ $$ = OBJECT_ACCESS_METHOD; }
6418 			| DATABASE							{ $$ = OBJECT_DATABASE; }
6419 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6420 			| EXTENSION							{ $$ = OBJECT_EXTENSION; }
6421 			| FOREIGN DATA_P WRAPPER			{ $$ = OBJECT_FDW; }
6422 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6423 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6424 			| ROLE								{ $$ = OBJECT_ROLE; }
6425 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6426 			| SERVER							{ $$ = OBJECT_FOREIGN_SERVER; }
6427 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6428 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6429 		;
6430 
6431 comment_text:
6432 			Sconst								{ $$ = $1; }
6433 			| NULL_P							{ $$ = NULL; }
6434 		;
6435 
6436 
6437 /*****************************************************************************
6438  *
6439  *  SECURITY LABEL [FOR <provider>] ON <object> IS <label>
6440  *
6441  *  As with COMMENT ON, <object> can refer to various types of database
6442  *  objects (e.g. TABLE, COLUMN, etc.).
6443  *
6444  *****************************************************************************/
6445 
6446 SecLabelStmt:
6447 			SECURITY LABEL opt_provider ON security_label_type_any_name any_name
6448 			IS security_label
6449 				{
6450 					SecLabelStmt *n = makeNode(SecLabelStmt);
6451 					n->provider = $3;
6452 					n->objtype = $5;
6453 					n->object = (Node *) $6;
6454 					n->label = $8;
6455 					$$ = (Node *) n;
6456 				}
6457 			| SECURITY LABEL opt_provider ON security_label_type_name name
6458 			  IS security_label
6459 				{
6460 					SecLabelStmt *n = makeNode(SecLabelStmt);
6461 					n->provider = $3;
6462 					n->objtype = $5;
6463 					n->object = (Node *) makeString($6);
6464 					n->label = $8;
6465 					$$ = (Node *) n;
6466 				}
6467 			| SECURITY LABEL opt_provider ON TYPE_P Typename
6468 			  IS security_label
6469 				{
6470 					SecLabelStmt *n = makeNode(SecLabelStmt);
6471 					n->provider = $3;
6472 					n->objtype = OBJECT_TYPE;
6473 					n->object = (Node *) $6;
6474 					n->label = $8;
6475 					$$ = (Node *) n;
6476 				}
6477 			| SECURITY LABEL opt_provider ON DOMAIN_P Typename
6478 			  IS security_label
6479 				{
6480 					SecLabelStmt *n = makeNode(SecLabelStmt);
6481 					n->provider = $3;
6482 					n->objtype = OBJECT_DOMAIN;
6483 					n->object = (Node *) $6;
6484 					n->label = $8;
6485 					$$ = (Node *) n;
6486 				}
6487 			| SECURITY LABEL opt_provider ON AGGREGATE aggregate_with_argtypes
6488 			  IS security_label
6489 				{
6490 					SecLabelStmt *n = makeNode(SecLabelStmt);
6491 					n->provider = $3;
6492 					n->objtype = OBJECT_AGGREGATE;
6493 					n->object = (Node *) $6;
6494 					n->label = $8;
6495 					$$ = (Node *) n;
6496 				}
6497 			| SECURITY LABEL opt_provider ON FUNCTION function_with_argtypes
6498 			  IS security_label
6499 				{
6500 					SecLabelStmt *n = makeNode(SecLabelStmt);
6501 					n->provider = $3;
6502 					n->objtype = OBJECT_FUNCTION;
6503 					n->object = (Node *) $6;
6504 					n->label = $8;
6505 					$$ = (Node *) n;
6506 				}
6507 			| SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
6508 			  IS security_label
6509 				{
6510 					SecLabelStmt *n = makeNode(SecLabelStmt);
6511 					n->provider = $3;
6512 					n->objtype = OBJECT_LARGEOBJECT;
6513 					n->object = (Node *) $7;
6514 					n->label = $9;
6515 					$$ = (Node *) n;
6516 				}
6517 		;
6518 
6519 opt_provider:	FOR NonReservedWord_or_Sconst	{ $$ = $2; }
6520 				| /* empty */					{ $$ = NULL; }
6521 		;
6522 
6523 /* object types taking any_name */
6524 security_label_type_any_name:
6525 			COLUMN								{ $$ = OBJECT_COLUMN; }
6526 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6527 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6528 			| TABLE								{ $$ = OBJECT_TABLE; }
6529 			| VIEW								{ $$ = OBJECT_VIEW; }
6530 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6531 		;
6532 
6533 /* object types taking name */
6534 security_label_type_name:
6535 			DATABASE							{ $$ = OBJECT_DATABASE; }
6536 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6537 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6538 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6539 			| ROLE								{ $$ = OBJECT_ROLE; }
6540 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6541 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6542 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6543 		;
6544 
6545 security_label:	Sconst				{ $$ = $1; }
6546 				| NULL_P			{ $$ = NULL; }
6547 		;
6548 
6549 /*****************************************************************************
6550  *
6551  *		QUERY:
6552  *			fetch/move
6553  *
6554  *****************************************************************************/
6555 
6556 FetchStmt:	FETCH fetch_args
6557 				{
6558 					FetchStmt *n = (FetchStmt *) $2;
6559 					n->ismove = FALSE;
6560 					$$ = (Node *)n;
6561 				}
6562 			| MOVE fetch_args
6563 				{
6564 					FetchStmt *n = (FetchStmt *) $2;
6565 					n->ismove = TRUE;
6566 					$$ = (Node *)n;
6567 				}
6568 		;
6569 
6570 fetch_args:	cursor_name
6571 				{
6572 					FetchStmt *n = makeNode(FetchStmt);
6573 					n->portalname = $1;
6574 					n->direction = FETCH_FORWARD;
6575 					n->howMany = 1;
6576 					$$ = (Node *)n;
6577 				}
6578 			| from_in cursor_name
6579 				{
6580 					FetchStmt *n = makeNode(FetchStmt);
6581 					n->portalname = $2;
6582 					n->direction = FETCH_FORWARD;
6583 					n->howMany = 1;
6584 					$$ = (Node *)n;
6585 				}
6586 			| NEXT opt_from_in cursor_name
6587 				{
6588 					FetchStmt *n = makeNode(FetchStmt);
6589 					n->portalname = $3;
6590 					n->direction = FETCH_FORWARD;
6591 					n->howMany = 1;
6592 					$$ = (Node *)n;
6593 				}
6594 			| PRIOR opt_from_in cursor_name
6595 				{
6596 					FetchStmt *n = makeNode(FetchStmt);
6597 					n->portalname = $3;
6598 					n->direction = FETCH_BACKWARD;
6599 					n->howMany = 1;
6600 					$$ = (Node *)n;
6601 				}
6602 			| FIRST_P opt_from_in cursor_name
6603 				{
6604 					FetchStmt *n = makeNode(FetchStmt);
6605 					n->portalname = $3;
6606 					n->direction = FETCH_ABSOLUTE;
6607 					n->howMany = 1;
6608 					$$ = (Node *)n;
6609 				}
6610 			| LAST_P opt_from_in cursor_name
6611 				{
6612 					FetchStmt *n = makeNode(FetchStmt);
6613 					n->portalname = $3;
6614 					n->direction = FETCH_ABSOLUTE;
6615 					n->howMany = -1;
6616 					$$ = (Node *)n;
6617 				}
6618 			| ABSOLUTE_P SignedIconst opt_from_in cursor_name
6619 				{
6620 					FetchStmt *n = makeNode(FetchStmt);
6621 					n->portalname = $4;
6622 					n->direction = FETCH_ABSOLUTE;
6623 					n->howMany = $2;
6624 					$$ = (Node *)n;
6625 				}
6626 			| RELATIVE_P SignedIconst opt_from_in cursor_name
6627 				{
6628 					FetchStmt *n = makeNode(FetchStmt);
6629 					n->portalname = $4;
6630 					n->direction = FETCH_RELATIVE;
6631 					n->howMany = $2;
6632 					$$ = (Node *)n;
6633 				}
6634 			| SignedIconst opt_from_in cursor_name
6635 				{
6636 					FetchStmt *n = makeNode(FetchStmt);
6637 					n->portalname = $3;
6638 					n->direction = FETCH_FORWARD;
6639 					n->howMany = $1;
6640 					$$ = (Node *)n;
6641 				}
6642 			| ALL opt_from_in cursor_name
6643 				{
6644 					FetchStmt *n = makeNode(FetchStmt);
6645 					n->portalname = $3;
6646 					n->direction = FETCH_FORWARD;
6647 					n->howMany = FETCH_ALL;
6648 					$$ = (Node *)n;
6649 				}
6650 			| FORWARD opt_from_in cursor_name
6651 				{
6652 					FetchStmt *n = makeNode(FetchStmt);
6653 					n->portalname = $3;
6654 					n->direction = FETCH_FORWARD;
6655 					n->howMany = 1;
6656 					$$ = (Node *)n;
6657 				}
6658 			| FORWARD SignedIconst opt_from_in cursor_name
6659 				{
6660 					FetchStmt *n = makeNode(FetchStmt);
6661 					n->portalname = $4;
6662 					n->direction = FETCH_FORWARD;
6663 					n->howMany = $2;
6664 					$$ = (Node *)n;
6665 				}
6666 			| FORWARD ALL opt_from_in cursor_name
6667 				{
6668 					FetchStmt *n = makeNode(FetchStmt);
6669 					n->portalname = $4;
6670 					n->direction = FETCH_FORWARD;
6671 					n->howMany = FETCH_ALL;
6672 					$$ = (Node *)n;
6673 				}
6674 			| BACKWARD opt_from_in cursor_name
6675 				{
6676 					FetchStmt *n = makeNode(FetchStmt);
6677 					n->portalname = $3;
6678 					n->direction = FETCH_BACKWARD;
6679 					n->howMany = 1;
6680 					$$ = (Node *)n;
6681 				}
6682 			| BACKWARD SignedIconst opt_from_in cursor_name
6683 				{
6684 					FetchStmt *n = makeNode(FetchStmt);
6685 					n->portalname = $4;
6686 					n->direction = FETCH_BACKWARD;
6687 					n->howMany = $2;
6688 					$$ = (Node *)n;
6689 				}
6690 			| BACKWARD ALL opt_from_in cursor_name
6691 				{
6692 					FetchStmt *n = makeNode(FetchStmt);
6693 					n->portalname = $4;
6694 					n->direction = FETCH_BACKWARD;
6695 					n->howMany = FETCH_ALL;
6696 					$$ = (Node *)n;
6697 				}
6698 		;
6699 
6700 from_in:	FROM									{}
6701 			| IN_P									{}
6702 		;
6703 
6704 opt_from_in:	from_in								{}
6705 			| /* EMPTY */							{}
6706 		;
6707 
6708 
6709 /*****************************************************************************
6710  *
6711  * GRANT and REVOKE statements
6712  *
6713  *****************************************************************************/
6714 
6715 GrantStmt:	GRANT privileges ON privilege_target TO grantee_list
6716 			opt_grant_grant_option
6717 				{
6718 					GrantStmt *n = makeNode(GrantStmt);
6719 					n->is_grant = true;
6720 					n->privileges = $2;
6721 					n->targtype = ($4)->targtype;
6722 					n->objtype = ($4)->objtype;
6723 					n->objects = ($4)->objs;
6724 					n->grantees = $6;
6725 					n->grant_option = $7;
6726 					$$ = (Node*)n;
6727 				}
6728 		;
6729 
6730 RevokeStmt:
6731 			REVOKE privileges ON privilege_target
6732 			FROM grantee_list opt_drop_behavior
6733 				{
6734 					GrantStmt *n = makeNode(GrantStmt);
6735 					n->is_grant = false;
6736 					n->grant_option = false;
6737 					n->privileges = $2;
6738 					n->targtype = ($4)->targtype;
6739 					n->objtype = ($4)->objtype;
6740 					n->objects = ($4)->objs;
6741 					n->grantees = $6;
6742 					n->behavior = $7;
6743 					$$ = (Node *)n;
6744 				}
6745 			| REVOKE GRANT OPTION FOR privileges ON privilege_target
6746 			FROM grantee_list opt_drop_behavior
6747 				{
6748 					GrantStmt *n = makeNode(GrantStmt);
6749 					n->is_grant = false;
6750 					n->grant_option = true;
6751 					n->privileges = $5;
6752 					n->targtype = ($7)->targtype;
6753 					n->objtype = ($7)->objtype;
6754 					n->objects = ($7)->objs;
6755 					n->grantees = $9;
6756 					n->behavior = $10;
6757 					$$ = (Node *)n;
6758 				}
6759 		;
6760 
6761 
6762 /*
6763  * Privilege names are represented as strings; the validity of the privilege
6764  * names gets checked at execution.  This is a bit annoying but we have little
6765  * choice because of the syntactic conflict with lists of role names in
6766  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
6767  * production any reserved keywords that need to be usable as privilege names.
6768  */
6769 
6770 /* either ALL [PRIVILEGES] or a list of individual privileges */
6771 privileges: privilege_list
6772 				{ $$ = $1; }
6773 			| ALL
6774 				{ $$ = NIL; }
6775 			| ALL PRIVILEGES
6776 				{ $$ = NIL; }
6777 			| ALL '(' columnList ')'
6778 				{
6779 					AccessPriv *n = makeNode(AccessPriv);
6780 					n->priv_name = NULL;
6781 					n->cols = $3;
6782 					$$ = list_make1(n);
6783 				}
6784 			| ALL PRIVILEGES '(' columnList ')'
6785 				{
6786 					AccessPriv *n = makeNode(AccessPriv);
6787 					n->priv_name = NULL;
6788 					n->cols = $4;
6789 					$$ = list_make1(n);
6790 				}
6791 		;
6792 
6793 privilege_list:	privilege							{ $$ = list_make1($1); }
6794 			| privilege_list ',' privilege			{ $$ = lappend($1, $3); }
6795 		;
6796 
6797 privilege:	SELECT opt_column_list
6798 			{
6799 				AccessPriv *n = makeNode(AccessPriv);
6800 				n->priv_name = pstrdup($1);
6801 				n->cols = $2;
6802 				$$ = n;
6803 			}
6804 		| REFERENCES opt_column_list
6805 			{
6806 				AccessPriv *n = makeNode(AccessPriv);
6807 				n->priv_name = pstrdup($1);
6808 				n->cols = $2;
6809 				$$ = n;
6810 			}
6811 		| CREATE opt_column_list
6812 			{
6813 				AccessPriv *n = makeNode(AccessPriv);
6814 				n->priv_name = pstrdup($1);
6815 				n->cols = $2;
6816 				$$ = n;
6817 			}
6818 		| ColId opt_column_list
6819 			{
6820 				AccessPriv *n = makeNode(AccessPriv);
6821 				n->priv_name = $1;
6822 				n->cols = $2;
6823 				$$ = n;
6824 			}
6825 		;
6826 
6827 
6828 /* Don't bother trying to fold the first two rules into one using
6829  * opt_table.  You're going to get conflicts.
6830  */
6831 privilege_target:
6832 			qualified_name_list
6833 				{
6834 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6835 					n->targtype = ACL_TARGET_OBJECT;
6836 					n->objtype = ACL_OBJECT_RELATION;
6837 					n->objs = $1;
6838 					$$ = n;
6839 				}
6840 			| TABLE qualified_name_list
6841 				{
6842 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6843 					n->targtype = ACL_TARGET_OBJECT;
6844 					n->objtype = ACL_OBJECT_RELATION;
6845 					n->objs = $2;
6846 					$$ = n;
6847 				}
6848 			| SEQUENCE qualified_name_list
6849 				{
6850 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6851 					n->targtype = ACL_TARGET_OBJECT;
6852 					n->objtype = ACL_OBJECT_SEQUENCE;
6853 					n->objs = $2;
6854 					$$ = n;
6855 				}
6856 			| FOREIGN DATA_P WRAPPER name_list
6857 				{
6858 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6859 					n->targtype = ACL_TARGET_OBJECT;
6860 					n->objtype = ACL_OBJECT_FDW;
6861 					n->objs = $4;
6862 					$$ = n;
6863 				}
6864 			| FOREIGN SERVER name_list
6865 				{
6866 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6867 					n->targtype = ACL_TARGET_OBJECT;
6868 					n->objtype = ACL_OBJECT_FOREIGN_SERVER;
6869 					n->objs = $3;
6870 					$$ = n;
6871 				}
6872 			| FUNCTION function_with_argtypes_list
6873 				{
6874 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6875 					n->targtype = ACL_TARGET_OBJECT;
6876 					n->objtype = ACL_OBJECT_FUNCTION;
6877 					n->objs = $2;
6878 					$$ = n;
6879 				}
6880 			| DATABASE name_list
6881 				{
6882 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6883 					n->targtype = ACL_TARGET_OBJECT;
6884 					n->objtype = ACL_OBJECT_DATABASE;
6885 					n->objs = $2;
6886 					$$ = n;
6887 				}
6888 			| DOMAIN_P any_name_list
6889 				{
6890 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6891 					n->targtype = ACL_TARGET_OBJECT;
6892 					n->objtype = ACL_OBJECT_DOMAIN;
6893 					n->objs = $2;
6894 					$$ = n;
6895 				}
6896 			| LANGUAGE name_list
6897 				{
6898 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6899 					n->targtype = ACL_TARGET_OBJECT;
6900 					n->objtype = ACL_OBJECT_LANGUAGE;
6901 					n->objs = $2;
6902 					$$ = n;
6903 				}
6904 			| LARGE_P OBJECT_P NumericOnly_list
6905 				{
6906 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6907 					n->targtype = ACL_TARGET_OBJECT;
6908 					n->objtype = ACL_OBJECT_LARGEOBJECT;
6909 					n->objs = $3;
6910 					$$ = n;
6911 				}
6912 			| SCHEMA name_list
6913 				{
6914 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6915 					n->targtype = ACL_TARGET_OBJECT;
6916 					n->objtype = ACL_OBJECT_NAMESPACE;
6917 					n->objs = $2;
6918 					$$ = n;
6919 				}
6920 			| TABLESPACE name_list
6921 				{
6922 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6923 					n->targtype = ACL_TARGET_OBJECT;
6924 					n->objtype = ACL_OBJECT_TABLESPACE;
6925 					n->objs = $2;
6926 					$$ = n;
6927 				}
6928 			| TYPE_P any_name_list
6929 				{
6930 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6931 					n->targtype = ACL_TARGET_OBJECT;
6932 					n->objtype = ACL_OBJECT_TYPE;
6933 					n->objs = $2;
6934 					$$ = n;
6935 				}
6936 			| ALL TABLES IN_P SCHEMA name_list
6937 				{
6938 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6939 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
6940 					n->objtype = ACL_OBJECT_RELATION;
6941 					n->objs = $5;
6942 					$$ = n;
6943 				}
6944 			| ALL SEQUENCES IN_P SCHEMA name_list
6945 				{
6946 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6947 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
6948 					n->objtype = ACL_OBJECT_SEQUENCE;
6949 					n->objs = $5;
6950 					$$ = n;
6951 				}
6952 			| ALL FUNCTIONS IN_P SCHEMA name_list
6953 				{
6954 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6955 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
6956 					n->objtype = ACL_OBJECT_FUNCTION;
6957 					n->objs = $5;
6958 					$$ = n;
6959 				}
6960 		;
6961 
6962 
6963 grantee_list:
6964 			grantee									{ $$ = list_make1($1); }
6965 			| grantee_list ',' grantee				{ $$ = lappend($1, $3); }
6966 		;
6967 
6968 grantee:
6969 			RoleSpec								{ $$ = $1; }
6970 			| GROUP_P RoleSpec						{ $$ = $2; }
6971 		;
6972 
6973 
6974 opt_grant_grant_option:
6975 			WITH GRANT OPTION { $$ = TRUE; }
6976 			| /*EMPTY*/ { $$ = FALSE; }
6977 		;
6978 
6979 /*****************************************************************************
6980  *
6981  * GRANT and REVOKE ROLE statements
6982  *
6983  *****************************************************************************/
6984 
6985 GrantRoleStmt:
6986 			GRANT privilege_list TO role_list opt_grant_admin_option opt_granted_by
6987 				{
6988 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
6989 					n->is_grant = true;
6990 					n->granted_roles = $2;
6991 					n->grantee_roles = $4;
6992 					n->admin_opt = $5;
6993 					n->grantor = $6;
6994 					$$ = (Node*)n;
6995 				}
6996 		;
6997 
6998 RevokeRoleStmt:
6999 			REVOKE privilege_list FROM role_list opt_granted_by opt_drop_behavior
7000 				{
7001 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7002 					n->is_grant = false;
7003 					n->admin_opt = false;
7004 					n->granted_roles = $2;
7005 					n->grantee_roles = $4;
7006 					n->behavior = $6;
7007 					$$ = (Node*)n;
7008 				}
7009 			| REVOKE ADMIN OPTION FOR privilege_list FROM role_list opt_granted_by opt_drop_behavior
7010 				{
7011 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7012 					n->is_grant = false;
7013 					n->admin_opt = true;
7014 					n->granted_roles = $5;
7015 					n->grantee_roles = $7;
7016 					n->behavior = $9;
7017 					$$ = (Node*)n;
7018 				}
7019 		;
7020 
7021 opt_grant_admin_option: WITH ADMIN OPTION				{ $$ = TRUE; }
7022 			| /*EMPTY*/									{ $$ = FALSE; }
7023 		;
7024 
7025 opt_granted_by: GRANTED BY RoleSpec						{ $$ = $3; }
7026 			| /*EMPTY*/									{ $$ = NULL; }
7027 		;
7028 
7029 /*****************************************************************************
7030  *
7031  * ALTER DEFAULT PRIVILEGES statement
7032  *
7033  *****************************************************************************/
7034 
7035 AlterDefaultPrivilegesStmt:
7036 			ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
7037 				{
7038 					AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
7039 					n->options = $4;
7040 					n->action = (GrantStmt *) $5;
7041 					$$ = (Node*)n;
7042 				}
7043 		;
7044 
7045 DefACLOptionList:
7046 			DefACLOptionList DefACLOption			{ $$ = lappend($1, $2); }
7047 			| /* EMPTY */							{ $$ = NIL; }
7048 		;
7049 
7050 DefACLOption:
7051 			IN_P SCHEMA name_list
7052 				{
7053 					$$ = makeDefElem("schemas", (Node *)$3, @1);
7054 				}
7055 			| FOR ROLE role_list
7056 				{
7057 					$$ = makeDefElem("roles", (Node *)$3, @1);
7058 				}
7059 			| FOR USER role_list
7060 				{
7061 					$$ = makeDefElem("roles", (Node *)$3, @1);
7062 				}
7063 		;
7064 
7065 /*
7066  * This should match GRANT/REVOKE, except that individual target objects
7067  * are not mentioned and we only allow a subset of object types.
7068  */
7069 DefACLAction:
7070 			GRANT privileges ON defacl_privilege_target TO grantee_list
7071 			opt_grant_grant_option
7072 				{
7073 					GrantStmt *n = makeNode(GrantStmt);
7074 					n->is_grant = true;
7075 					n->privileges = $2;
7076 					n->targtype = ACL_TARGET_DEFAULTS;
7077 					n->objtype = $4;
7078 					n->objects = NIL;
7079 					n->grantees = $6;
7080 					n->grant_option = $7;
7081 					$$ = (Node*)n;
7082 				}
7083 			| REVOKE privileges ON defacl_privilege_target
7084 			FROM grantee_list opt_drop_behavior
7085 				{
7086 					GrantStmt *n = makeNode(GrantStmt);
7087 					n->is_grant = false;
7088 					n->grant_option = false;
7089 					n->privileges = $2;
7090 					n->targtype = ACL_TARGET_DEFAULTS;
7091 					n->objtype = $4;
7092 					n->objects = NIL;
7093 					n->grantees = $6;
7094 					n->behavior = $7;
7095 					$$ = (Node *)n;
7096 				}
7097 			| REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
7098 			FROM grantee_list opt_drop_behavior
7099 				{
7100 					GrantStmt *n = makeNode(GrantStmt);
7101 					n->is_grant = false;
7102 					n->grant_option = true;
7103 					n->privileges = $5;
7104 					n->targtype = ACL_TARGET_DEFAULTS;
7105 					n->objtype = $7;
7106 					n->objects = NIL;
7107 					n->grantees = $9;
7108 					n->behavior = $10;
7109 					$$ = (Node *)n;
7110 				}
7111 		;
7112 
7113 defacl_privilege_target:
7114 			TABLES			{ $$ = ACL_OBJECT_RELATION; }
7115 			| FUNCTIONS		{ $$ = ACL_OBJECT_FUNCTION; }
7116 			| SEQUENCES		{ $$ = ACL_OBJECT_SEQUENCE; }
7117 			| TYPES_P		{ $$ = ACL_OBJECT_TYPE; }
7118 			| SCHEMAS		{ $$ = ACL_OBJECT_NAMESPACE; }
7119 		;
7120 
7121 
7122 /*****************************************************************************
7123  *
7124  *		QUERY: CREATE INDEX
7125  *
7126  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
7127  * willing to make TABLESPACE a fully reserved word.
7128  *****************************************************************************/
7129 
7130 IndexStmt:	CREATE opt_unique INDEX opt_concurrently opt_index_name
7131 			ON qualified_name access_method_clause '(' index_params ')'
7132 			opt_reloptions OptTableSpace where_clause
7133 				{
7134 					IndexStmt *n = makeNode(IndexStmt);
7135 					n->unique = $2;
7136 					n->concurrent = $4;
7137 					n->idxname = $5;
7138 					n->relation = $7;
7139 					n->accessMethod = $8;
7140 					n->indexParams = $10;
7141 					n->options = $12;
7142 					n->tableSpace = $13;
7143 					n->whereClause = $14;
7144 					n->excludeOpNames = NIL;
7145 					n->idxcomment = NULL;
7146 					n->indexOid = InvalidOid;
7147 					n->oldNode = InvalidOid;
7148 					n->primary = false;
7149 					n->isconstraint = false;
7150 					n->deferrable = false;
7151 					n->initdeferred = false;
7152 					n->transformed = false;
7153 					n->if_not_exists = false;
7154 					$$ = (Node *)n;
7155 				}
7156 			| CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS index_name
7157 			ON qualified_name access_method_clause '(' index_params ')'
7158 			opt_reloptions OptTableSpace where_clause
7159 				{
7160 					IndexStmt *n = makeNode(IndexStmt);
7161 					n->unique = $2;
7162 					n->concurrent = $4;
7163 					n->idxname = $8;
7164 					n->relation = $10;
7165 					n->accessMethod = $11;
7166 					n->indexParams = $13;
7167 					n->options = $15;
7168 					n->tableSpace = $16;
7169 					n->whereClause = $17;
7170 					n->excludeOpNames = NIL;
7171 					n->idxcomment = NULL;
7172 					n->indexOid = InvalidOid;
7173 					n->oldNode = InvalidOid;
7174 					n->primary = false;
7175 					n->isconstraint = false;
7176 					n->deferrable = false;
7177 					n->initdeferred = false;
7178 					n->transformed = false;
7179 					n->if_not_exists = true;
7180 					$$ = (Node *)n;
7181 				}
7182 		;
7183 
7184 opt_unique:
7185 			UNIQUE									{ $$ = TRUE; }
7186 			| /*EMPTY*/								{ $$ = FALSE; }
7187 		;
7188 
7189 opt_concurrently:
7190 			CONCURRENTLY							{ $$ = TRUE; }
7191 			| /*EMPTY*/								{ $$ = FALSE; }
7192 		;
7193 
7194 opt_index_name:
7195 			index_name								{ $$ = $1; }
7196 			| /*EMPTY*/								{ $$ = NULL; }
7197 		;
7198 
7199 access_method_clause:
7200 			USING access_method						{ $$ = $2; }
7201 			| /*EMPTY*/								{ $$ = DEFAULT_INDEX_TYPE; }
7202 		;
7203 
7204 index_params:	index_elem							{ $$ = list_make1($1); }
7205 			| index_params ',' index_elem			{ $$ = lappend($1, $3); }
7206 		;
7207 
7208 /*
7209  * Index attributes can be either simple column references, or arbitrary
7210  * expressions in parens.  For backwards-compatibility reasons, we allow
7211  * an expression that's just a function call to be written without parens.
7212  */
7213 index_elem:	ColId opt_collate opt_class opt_asc_desc opt_nulls_order
7214 				{
7215 					$$ = makeNode(IndexElem);
7216 					$$->name = $1;
7217 					$$->expr = NULL;
7218 					$$->indexcolname = NULL;
7219 					$$->collation = $2;
7220 					$$->opclass = $3;
7221 					$$->ordering = $4;
7222 					$$->nulls_ordering = $5;
7223 				}
7224 			| func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order
7225 				{
7226 					$$ = makeNode(IndexElem);
7227 					$$->name = NULL;
7228 					$$->expr = $1;
7229 					$$->indexcolname = NULL;
7230 					$$->collation = $2;
7231 					$$->opclass = $3;
7232 					$$->ordering = $4;
7233 					$$->nulls_ordering = $5;
7234 				}
7235 			| '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order
7236 				{
7237 					$$ = makeNode(IndexElem);
7238 					$$->name = NULL;
7239 					$$->expr = $2;
7240 					$$->indexcolname = NULL;
7241 					$$->collation = $4;
7242 					$$->opclass = $5;
7243 					$$->ordering = $6;
7244 					$$->nulls_ordering = $7;
7245 				}
7246 		;
7247 
7248 opt_collate: COLLATE any_name						{ $$ = $2; }
7249 			| /*EMPTY*/								{ $$ = NIL; }
7250 		;
7251 
7252 opt_class:	any_name								{ $$ = $1; }
7253 			| /*EMPTY*/								{ $$ = NIL; }
7254 		;
7255 
7256 opt_asc_desc: ASC							{ $$ = SORTBY_ASC; }
7257 			| DESC							{ $$ = SORTBY_DESC; }
7258 			| /*EMPTY*/						{ $$ = SORTBY_DEFAULT; }
7259 		;
7260 
7261 opt_nulls_order: NULLS_LA FIRST_P			{ $$ = SORTBY_NULLS_FIRST; }
7262 			| NULLS_LA LAST_P				{ $$ = SORTBY_NULLS_LAST; }
7263 			| /*EMPTY*/						{ $$ = SORTBY_NULLS_DEFAULT; }
7264 		;
7265 
7266 
7267 /*****************************************************************************
7268  *
7269  *		QUERY:
7270  *				create [or replace] function <fname>
7271  *						[(<type-1> { , <type-n>})]
7272  *						returns <type-r>
7273  *						as <filename or code in language as appropriate>
7274  *						language <lang> [with parameters]
7275  *
7276  *****************************************************************************/
7277 
7278 CreateFunctionStmt:
7279 			CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7280 			RETURNS func_return createfunc_opt_list opt_definition
7281 				{
7282 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7283 					n->replace = $2;
7284 					n->funcname = $4;
7285 					n->parameters = $5;
7286 					n->returnType = $7;
7287 					n->options = $8;
7288 					n->withClause = $9;
7289 					$$ = (Node *)n;
7290 				}
7291 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7292 			  RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
7293 				{
7294 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7295 					n->replace = $2;
7296 					n->funcname = $4;
7297 					n->parameters = mergeTableFuncParameters($5, $9);
7298 					n->returnType = TableFuncTypeName($9);
7299 					n->returnType->location = @7;
7300 					n->options = $11;
7301 					n->withClause = $12;
7302 					$$ = (Node *)n;
7303 				}
7304 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7305 			  createfunc_opt_list opt_definition
7306 				{
7307 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7308 					n->replace = $2;
7309 					n->funcname = $4;
7310 					n->parameters = $5;
7311 					n->returnType = NULL;
7312 					n->options = $6;
7313 					n->withClause = $7;
7314 					$$ = (Node *)n;
7315 				}
7316 		;
7317 
7318 opt_or_replace:
7319 			OR REPLACE								{ $$ = TRUE; }
7320 			| /*EMPTY*/								{ $$ = FALSE; }
7321 		;
7322 
7323 func_args:	'(' func_args_list ')'					{ $$ = $2; }
7324 			| '(' ')'								{ $$ = NIL; }
7325 		;
7326 
7327 func_args_list:
7328 			func_arg								{ $$ = list_make1($1); }
7329 			| func_args_list ',' func_arg			{ $$ = lappend($1, $3); }
7330 		;
7331 
7332 function_with_argtypes_list:
7333 			function_with_argtypes					{ $$ = list_make1($1); }
7334 			| function_with_argtypes_list ',' function_with_argtypes
7335 													{ $$ = lappend($1, $3); }
7336 		;
7337 
7338 function_with_argtypes:
7339 			func_name func_args
7340 				{
7341 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7342 					n->objname = $1;
7343 					n->objargs = extractArgTypes($2);
7344 					$$ = n;
7345 				}
7346 			/*
7347 			 * Because of reduce/reduce conflicts, we can't use func_name
7348 			 * below, but we can write it out the long way, which actually
7349 			 * allows more cases.
7350 			 */
7351 			| type_func_name_keyword
7352 				{
7353 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7354 					n->objname = list_make1(makeString(pstrdup($1)));
7355 					n->args_unspecified = true;
7356 					$$ = n;
7357 				}
7358 			| ColId
7359 				{
7360 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7361 					n->objname = list_make1(makeString($1));
7362 					n->args_unspecified = true;
7363 					$$ = n;
7364 				}
7365 			| ColId indirection
7366 				{
7367 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7368 					n->objname = check_func_name(lcons(makeString($1), $2),
7369 												  yyscanner);
7370 					n->args_unspecified = true;
7371 					$$ = n;
7372 				}
7373 		;
7374 
7375 /*
7376  * func_args_with_defaults is separate because we only want to accept
7377  * defaults in CREATE FUNCTION, not in ALTER etc.
7378  */
7379 func_args_with_defaults:
7380 		'(' func_args_with_defaults_list ')'		{ $$ = $2; }
7381 		| '(' ')'									{ $$ = NIL; }
7382 		;
7383 
7384 func_args_with_defaults_list:
7385 		func_arg_with_default						{ $$ = list_make1($1); }
7386 		| func_args_with_defaults_list ',' func_arg_with_default
7387 													{ $$ = lappend($1, $3); }
7388 		;
7389 
7390 /*
7391  * The style with arg_class first is SQL99 standard, but Oracle puts
7392  * param_name first; accept both since it's likely people will try both
7393  * anyway.  Don't bother trying to save productions by letting arg_class
7394  * have an empty alternative ... you'll get shift/reduce conflicts.
7395  *
7396  * We can catch over-specified arguments here if we want to,
7397  * but for now better to silently swallow typmod, etc.
7398  * - thomas 2000-03-22
7399  */
7400 func_arg:
7401 			arg_class param_name func_type
7402 				{
7403 					FunctionParameter *n = makeNode(FunctionParameter);
7404 					n->name = $2;
7405 					n->argType = $3;
7406 					n->mode = $1;
7407 					n->defexpr = NULL;
7408 					$$ = n;
7409 				}
7410 			| param_name arg_class func_type
7411 				{
7412 					FunctionParameter *n = makeNode(FunctionParameter);
7413 					n->name = $1;
7414 					n->argType = $3;
7415 					n->mode = $2;
7416 					n->defexpr = NULL;
7417 					$$ = n;
7418 				}
7419 			| param_name func_type
7420 				{
7421 					FunctionParameter *n = makeNode(FunctionParameter);
7422 					n->name = $1;
7423 					n->argType = $2;
7424 					n->mode = FUNC_PARAM_IN;
7425 					n->defexpr = NULL;
7426 					$$ = n;
7427 				}
7428 			| arg_class func_type
7429 				{
7430 					FunctionParameter *n = makeNode(FunctionParameter);
7431 					n->name = NULL;
7432 					n->argType = $2;
7433 					n->mode = $1;
7434 					n->defexpr = NULL;
7435 					$$ = n;
7436 				}
7437 			| func_type
7438 				{
7439 					FunctionParameter *n = makeNode(FunctionParameter);
7440 					n->name = NULL;
7441 					n->argType = $1;
7442 					n->mode = FUNC_PARAM_IN;
7443 					n->defexpr = NULL;
7444 					$$ = n;
7445 				}
7446 		;
7447 
7448 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
7449 arg_class:	IN_P								{ $$ = FUNC_PARAM_IN; }
7450 			| OUT_P								{ $$ = FUNC_PARAM_OUT; }
7451 			| INOUT								{ $$ = FUNC_PARAM_INOUT; }
7452 			| IN_P OUT_P						{ $$ = FUNC_PARAM_INOUT; }
7453 			| VARIADIC							{ $$ = FUNC_PARAM_VARIADIC; }
7454 		;
7455 
7456 /*
7457  * Ideally param_name should be ColId, but that causes too many conflicts.
7458  */
7459 param_name:	type_function_name
7460 		;
7461 
7462 func_return:
7463 			func_type
7464 				{
7465 					/* We can catch over-specified results here if we want to,
7466 					 * but for now better to silently swallow typmod, etc.
7467 					 * - thomas 2000-03-22
7468 					 */
7469 					$$ = $1;
7470 				}
7471 		;
7472 
7473 /*
7474  * We would like to make the %TYPE productions here be ColId attrs etc,
7475  * but that causes reduce/reduce conflicts.  type_function_name
7476  * is next best choice.
7477  */
7478 func_type:	Typename								{ $$ = $1; }
7479 			| type_function_name attrs '%' TYPE_P
7480 				{
7481 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7482 					$$->pct_type = true;
7483 					$$->location = @1;
7484 				}
7485 			| SETOF type_function_name attrs '%' TYPE_P
7486 				{
7487 					$$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
7488 					$$->pct_type = true;
7489 					$$->setof = TRUE;
7490 					$$->location = @2;
7491 				}
7492 		;
7493 
7494 func_arg_with_default:
7495 		func_arg
7496 				{
7497 					$$ = $1;
7498 				}
7499 		| func_arg DEFAULT a_expr
7500 				{
7501 					$$ = $1;
7502 					$$->defexpr = $3;
7503 				}
7504 		| func_arg '=' a_expr
7505 				{
7506 					$$ = $1;
7507 					$$->defexpr = $3;
7508 				}
7509 		;
7510 
7511 /* Aggregate args can be most things that function args can be */
7512 aggr_arg:	func_arg
7513 				{
7514 					if (!($1->mode == FUNC_PARAM_IN ||
7515 						  $1->mode == FUNC_PARAM_VARIADIC))
7516 						ereport(ERROR,
7517 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7518 								 errmsg("aggregates cannot have output arguments"),
7519 								 parser_errposition(@1)));
7520 					$$ = $1;
7521 				}
7522 		;
7523 
7524 /*
7525  * The SQL standard offers no guidance on how to declare aggregate argument
7526  * lists, since it doesn't have CREATE AGGREGATE etc.  We accept these cases:
7527  *
7528  * (*)									- normal agg with no args
7529  * (aggr_arg,...)						- normal agg with args
7530  * (ORDER BY aggr_arg,...)				- ordered-set agg with no direct args
7531  * (aggr_arg,... ORDER BY aggr_arg,...)	- ordered-set agg with direct args
7532  *
7533  * The zero-argument case is spelled with '*' for consistency with COUNT(*).
7534  *
7535  * An additional restriction is that if the direct-args list ends in a
7536  * VARIADIC item, the ordered-args list must contain exactly one item that
7537  * is also VARIADIC with the same type.  This allows us to collapse the two
7538  * VARIADIC items into one, which is necessary to represent the aggregate in
7539  * pg_proc.  We check this at the grammar stage so that we can return a list
7540  * in which the second VARIADIC item is already discarded, avoiding extra work
7541  * in cases such as DROP AGGREGATE.
7542  *
7543  * The return value of this production is a two-element list, in which the
7544  * first item is a sublist of FunctionParameter nodes (with any duplicate
7545  * VARIADIC item already dropped, as per above) and the second is an integer
7546  * Value node, containing -1 if there was no ORDER BY and otherwise the number
7547  * of argument declarations before the ORDER BY.  (If this number is equal
7548  * to the first sublist's length, then we dropped a duplicate VARIADIC item.)
7549  * This representation is passed as-is to CREATE AGGREGATE; for operations
7550  * on existing aggregates, we can just apply extractArgTypes to the first
7551  * sublist.
7552  */
7553 aggr_args:	'(' '*' ')'
7554 				{
7555 					$$ = list_make2(NIL, makeInteger(-1));
7556 				}
7557 			| '(' aggr_args_list ')'
7558 				{
7559 					$$ = list_make2($2, makeInteger(-1));
7560 				}
7561 			| '(' ORDER BY aggr_args_list ')'
7562 				{
7563 					$$ = list_make2($4, makeInteger(0));
7564 				}
7565 			| '(' aggr_args_list ORDER BY aggr_args_list ')'
7566 				{
7567 					/* this is the only case requiring consistency checking */
7568 					$$ = makeOrderedSetArgs($2, $5, yyscanner);
7569 				}
7570 		;
7571 
7572 aggr_args_list:
7573 			aggr_arg								{ $$ = list_make1($1); }
7574 			| aggr_args_list ',' aggr_arg			{ $$ = lappend($1, $3); }
7575 		;
7576 
7577 aggregate_with_argtypes:
7578 			func_name aggr_args
7579 				{
7580 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7581 					n->objname = $1;
7582 					n->objargs = extractAggrArgTypes($2);
7583 					$$ = n;
7584 				}
7585 		;
7586 
7587 aggregate_with_argtypes_list:
7588 			aggregate_with_argtypes					{ $$ = list_make1($1); }
7589 			| aggregate_with_argtypes_list ',' aggregate_with_argtypes
7590 													{ $$ = lappend($1, $3); }
7591 		;
7592 
7593 createfunc_opt_list:
7594 			/* Must be at least one to prevent conflict */
7595 			createfunc_opt_item						{ $$ = list_make1($1); }
7596 			| createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
7597 	;
7598 
7599 /*
7600  * Options common to both CREATE FUNCTION and ALTER FUNCTION
7601  */
7602 common_func_opt_item:
7603 			CALLED ON NULL_P INPUT_P
7604 				{
7605 					$$ = makeDefElem("strict", (Node *)makeInteger(FALSE), @1);
7606 				}
7607 			| RETURNS NULL_P ON NULL_P INPUT_P
7608 				{
7609 					$$ = makeDefElem("strict", (Node *)makeInteger(TRUE), @1);
7610 				}
7611 			| STRICT_P
7612 				{
7613 					$$ = makeDefElem("strict", (Node *)makeInteger(TRUE), @1);
7614 				}
7615 			| IMMUTABLE
7616 				{
7617 					$$ = makeDefElem("volatility", (Node *)makeString("immutable"), @1);
7618 				}
7619 			| STABLE
7620 				{
7621 					$$ = makeDefElem("volatility", (Node *)makeString("stable"), @1);
7622 				}
7623 			| VOLATILE
7624 				{
7625 					$$ = makeDefElem("volatility", (Node *)makeString("volatile"), @1);
7626 				}
7627 			| EXTERNAL SECURITY DEFINER
7628 				{
7629 					$$ = makeDefElem("security", (Node *)makeInteger(TRUE), @1);
7630 				}
7631 			| EXTERNAL SECURITY INVOKER
7632 				{
7633 					$$ = makeDefElem("security", (Node *)makeInteger(FALSE), @1);
7634 				}
7635 			| SECURITY DEFINER
7636 				{
7637 					$$ = makeDefElem("security", (Node *)makeInteger(TRUE), @1);
7638 				}
7639 			| SECURITY INVOKER
7640 				{
7641 					$$ = makeDefElem("security", (Node *)makeInteger(FALSE), @1);
7642 				}
7643 			| LEAKPROOF
7644 				{
7645 					$$ = makeDefElem("leakproof", (Node *)makeInteger(TRUE), @1);
7646 				}
7647 			| NOT LEAKPROOF
7648 				{
7649 					$$ = makeDefElem("leakproof", (Node *)makeInteger(FALSE), @1);
7650 				}
7651 			| COST NumericOnly
7652 				{
7653 					$$ = makeDefElem("cost", (Node *)$2, @1);
7654 				}
7655 			| ROWS NumericOnly
7656 				{
7657 					$$ = makeDefElem("rows", (Node *)$2, @1);
7658 				}
7659 			| FunctionSetResetClause
7660 				{
7661 					/* we abuse the normal content of a DefElem here */
7662 					$$ = makeDefElem("set", (Node *)$1, @1);
7663 				}
7664 			| PARALLEL ColId
7665 				{
7666 					$$ = makeDefElem("parallel", (Node *)makeString($2), @1);
7667 				}
7668 		;
7669 
7670 createfunc_opt_item:
7671 			AS func_as
7672 				{
7673 					$$ = makeDefElem("as", (Node *)$2, @1);
7674 				}
7675 			| LANGUAGE NonReservedWord_or_Sconst
7676 				{
7677 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
7678 				}
7679 			| TRANSFORM transform_type_list
7680 				{
7681 					$$ = makeDefElem("transform", (Node *)$2, @1);
7682 				}
7683 			| WINDOW
7684 				{
7685 					$$ = makeDefElem("window", (Node *)makeInteger(TRUE), @1);
7686 				}
7687 			| common_func_opt_item
7688 				{
7689 					$$ = $1;
7690 				}
7691 		;
7692 
7693 func_as:	Sconst						{ $$ = list_make1(makeString($1)); }
7694 			| Sconst ',' Sconst
7695 				{
7696 					$$ = list_make2(makeString($1), makeString($3));
7697 				}
7698 		;
7699 
7700 transform_type_list:
7701 			FOR TYPE_P Typename { $$ = list_make1($3); }
7702 			| transform_type_list ',' FOR TYPE_P Typename { $$ = lappend($1, $5); }
7703 		;
7704 
7705 opt_definition:
7706 			WITH definition							{ $$ = $2; }
7707 			| /*EMPTY*/								{ $$ = NIL; }
7708 		;
7709 
7710 table_func_column:	param_name func_type
7711 				{
7712 					FunctionParameter *n = makeNode(FunctionParameter);
7713 					n->name = $1;
7714 					n->argType = $2;
7715 					n->mode = FUNC_PARAM_TABLE;
7716 					n->defexpr = NULL;
7717 					$$ = n;
7718 				}
7719 		;
7720 
7721 table_func_column_list:
7722 			table_func_column
7723 				{
7724 					$$ = list_make1($1);
7725 				}
7726 			| table_func_column_list ',' table_func_column
7727 				{
7728 					$$ = lappend($1, $3);
7729 				}
7730 		;
7731 
7732 /*****************************************************************************
7733  * ALTER FUNCTION
7734  *
7735  * RENAME and OWNER subcommands are already provided by the generic
7736  * ALTER infrastructure, here we just specify alterations that can
7737  * only be applied to functions.
7738  *
7739  *****************************************************************************/
7740 AlterFunctionStmt:
7741 			ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
7742 				{
7743 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
7744 					n->func = $3;
7745 					n->actions = $4;
7746 					$$ = (Node *) n;
7747 				}
7748 		;
7749 
7750 alterfunc_opt_list:
7751 			/* At least one option must be specified */
7752 			common_func_opt_item					{ $$ = list_make1($1); }
7753 			| alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
7754 		;
7755 
7756 /* Ignored, merely for SQL compliance */
7757 opt_restrict:
7758 			RESTRICT
7759 			| /* EMPTY */
7760 		;
7761 
7762 
7763 /*****************************************************************************
7764  *
7765  *		QUERY:
7766  *
7767  *		DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
7768  *		DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
7769  *		DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
7770  *
7771  *****************************************************************************/
7772 
7773 RemoveFuncStmt:
7774 			DROP FUNCTION function_with_argtypes_list opt_drop_behavior
7775 				{
7776 					DropStmt *n = makeNode(DropStmt);
7777 					n->removeType = OBJECT_FUNCTION;
7778 					n->objects = $3;
7779 					n->behavior = $4;
7780 					n->missing_ok = false;
7781 					n->concurrent = false;
7782 					$$ = (Node *)n;
7783 				}
7784 			| DROP FUNCTION IF_P EXISTS function_with_argtypes_list opt_drop_behavior
7785 				{
7786 					DropStmt *n = makeNode(DropStmt);
7787 					n->removeType = OBJECT_FUNCTION;
7788 					n->objects = $5;
7789 					n->behavior = $6;
7790 					n->missing_ok = true;
7791 					n->concurrent = false;
7792 					$$ = (Node *)n;
7793 				}
7794 		;
7795 
7796 RemoveAggrStmt:
7797 			DROP AGGREGATE aggregate_with_argtypes_list opt_drop_behavior
7798 				{
7799 					DropStmt *n = makeNode(DropStmt);
7800 					n->removeType = OBJECT_AGGREGATE;
7801 					n->objects = $3;
7802 					n->behavior = $4;
7803 					n->missing_ok = false;
7804 					n->concurrent = false;
7805 					$$ = (Node *)n;
7806 				}
7807 			| DROP AGGREGATE IF_P EXISTS aggregate_with_argtypes_list opt_drop_behavior
7808 				{
7809 					DropStmt *n = makeNode(DropStmt);
7810 					n->removeType = OBJECT_AGGREGATE;
7811 					n->objects = $5;
7812 					n->behavior = $6;
7813 					n->missing_ok = true;
7814 					n->concurrent = false;
7815 					$$ = (Node *)n;
7816 				}
7817 		;
7818 
7819 RemoveOperStmt:
7820 			DROP OPERATOR operator_with_argtypes_list opt_drop_behavior
7821 				{
7822 					DropStmt *n = makeNode(DropStmt);
7823 					n->removeType = OBJECT_OPERATOR;
7824 					n->objects = $3;
7825 					n->behavior = $4;
7826 					n->missing_ok = false;
7827 					n->concurrent = false;
7828 					$$ = (Node *)n;
7829 				}
7830 			| DROP OPERATOR IF_P EXISTS operator_with_argtypes_list opt_drop_behavior
7831 				{
7832 					DropStmt *n = makeNode(DropStmt);
7833 					n->removeType = OBJECT_OPERATOR;
7834 					n->objects = $5;
7835 					n->behavior = $6;
7836 					n->missing_ok = true;
7837 					n->concurrent = false;
7838 					$$ = (Node *)n;
7839 				}
7840 		;
7841 
7842 oper_argtypes:
7843 			'(' Typename ')'
7844 				{
7845 				   ereport(ERROR,
7846 						   (errcode(ERRCODE_SYNTAX_ERROR),
7847 							errmsg("missing argument"),
7848 							errhint("Use NONE to denote the missing argument of a unary operator."),
7849 							parser_errposition(@3)));
7850 				}
7851 			| '(' Typename ',' Typename ')'
7852 					{ $$ = list_make2($2, $4); }
7853 			| '(' NONE ',' Typename ')'					/* left unary */
7854 					{ $$ = list_make2(NULL, $4); }
7855 			| '(' Typename ',' NONE ')'					/* right unary */
7856 					{ $$ = list_make2($2, NULL); }
7857 		;
7858 
7859 any_operator:
7860 			all_Op
7861 					{ $$ = list_make1(makeString($1)); }
7862 			| ColId '.' any_operator
7863 					{ $$ = lcons(makeString($1), $3); }
7864 		;
7865 
7866 operator_with_argtypes_list:
7867 			operator_with_argtypes					{ $$ = list_make1($1); }
7868 			| operator_with_argtypes_list ',' operator_with_argtypes
7869 													{ $$ = lappend($1, $3); }
7870 		;
7871 
7872 operator_with_argtypes:
7873 			any_operator oper_argtypes
7874 				{
7875 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7876 					n->objname = $1;
7877 					n->objargs = $2;
7878 					$$ = n;
7879 				}
7880 		;
7881 
7882 /*****************************************************************************
7883  *
7884  *		DO <anonymous code block> [ LANGUAGE language ]
7885  *
7886  * We use a DefElem list for future extensibility, and to allow flexibility
7887  * in the clause order.
7888  *
7889  *****************************************************************************/
7890 
7891 DoStmt: DO dostmt_opt_list
7892 				{
7893 					DoStmt *n = makeNode(DoStmt);
7894 					n->args = $2;
7895 					$$ = (Node *)n;
7896 				}
7897 		;
7898 
7899 dostmt_opt_list:
7900 			dostmt_opt_item						{ $$ = list_make1($1); }
7901 			| dostmt_opt_list dostmt_opt_item	{ $$ = lappend($1, $2); }
7902 		;
7903 
7904 dostmt_opt_item:
7905 			Sconst
7906 				{
7907 					$$ = makeDefElem("as", (Node *)makeString($1), @1);
7908 				}
7909 			| LANGUAGE NonReservedWord_or_Sconst
7910 				{
7911 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
7912 				}
7913 		;
7914 
7915 /*****************************************************************************
7916  *
7917  *		CREATE CAST / DROP CAST
7918  *
7919  *****************************************************************************/
7920 
7921 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
7922 					WITH FUNCTION function_with_argtypes cast_context
7923 				{
7924 					CreateCastStmt *n = makeNode(CreateCastStmt);
7925 					n->sourcetype = $4;
7926 					n->targettype = $6;
7927 					n->func = $10;
7928 					n->context = (CoercionContext) $11;
7929 					n->inout = false;
7930 					$$ = (Node *)n;
7931 				}
7932 			| CREATE CAST '(' Typename AS Typename ')'
7933 					WITHOUT FUNCTION cast_context
7934 				{
7935 					CreateCastStmt *n = makeNode(CreateCastStmt);
7936 					n->sourcetype = $4;
7937 					n->targettype = $6;
7938 					n->func = NULL;
7939 					n->context = (CoercionContext) $10;
7940 					n->inout = false;
7941 					$$ = (Node *)n;
7942 				}
7943 			| CREATE CAST '(' Typename AS Typename ')'
7944 					WITH INOUT cast_context
7945 				{
7946 					CreateCastStmt *n = makeNode(CreateCastStmt);
7947 					n->sourcetype = $4;
7948 					n->targettype = $6;
7949 					n->func = NULL;
7950 					n->context = (CoercionContext) $10;
7951 					n->inout = true;
7952 					$$ = (Node *)n;
7953 				}
7954 		;
7955 
7956 cast_context:  AS IMPLICIT_P					{ $$ = COERCION_IMPLICIT; }
7957 		| AS ASSIGNMENT							{ $$ = COERCION_ASSIGNMENT; }
7958 		| /*EMPTY*/								{ $$ = COERCION_EXPLICIT; }
7959 		;
7960 
7961 
7962 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
7963 				{
7964 					DropStmt *n = makeNode(DropStmt);
7965 					n->removeType = OBJECT_CAST;
7966 					n->objects = list_make1(list_make2($5, $7));
7967 					n->behavior = $9;
7968 					n->missing_ok = $3;
7969 					n->concurrent = false;
7970 					$$ = (Node *)n;
7971 				}
7972 		;
7973 
7974 opt_if_exists: IF_P EXISTS						{ $$ = TRUE; }
7975 		| /*EMPTY*/								{ $$ = FALSE; }
7976 		;
7977 
7978 
7979 /*****************************************************************************
7980  *
7981  *		CREATE TRANSFORM / DROP TRANSFORM
7982  *
7983  *****************************************************************************/
7984 
7985 CreateTransformStmt: CREATE opt_or_replace TRANSFORM FOR Typename LANGUAGE name '(' transform_element_list ')'
7986 				{
7987 					CreateTransformStmt *n = makeNode(CreateTransformStmt);
7988 					n->replace = $2;
7989 					n->type_name = $5;
7990 					n->lang = $7;
7991 					n->fromsql = linitial($9);
7992 					n->tosql = lsecond($9);
7993 					$$ = (Node *)n;
7994 				}
7995 		;
7996 
7997 transform_element_list: FROM SQL_P WITH FUNCTION function_with_argtypes ',' TO SQL_P WITH FUNCTION function_with_argtypes
7998 				{
7999 					$$ = list_make2($5, $11);
8000 				}
8001 				| TO SQL_P WITH FUNCTION function_with_argtypes ',' FROM SQL_P WITH FUNCTION function_with_argtypes
8002 				{
8003 					$$ = list_make2($11, $5);
8004 				}
8005 				| FROM SQL_P WITH FUNCTION function_with_argtypes
8006 				{
8007 					$$ = list_make2($5, NULL);
8008 				}
8009 				| TO SQL_P WITH FUNCTION function_with_argtypes
8010 				{
8011 					$$ = list_make2(NULL, $5);
8012 				}
8013 		;
8014 
8015 
8016 DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_drop_behavior
8017 				{
8018 					DropStmt *n = makeNode(DropStmt);
8019 					n->removeType = OBJECT_TRANSFORM;
8020 					n->objects = list_make1(list_make2($5, makeString($7)));
8021 					n->behavior = $8;
8022 					n->missing_ok = $3;
8023 					$$ = (Node *)n;
8024 				}
8025 		;
8026 
8027 
8028 /*****************************************************************************
8029  *
8030  *		QUERY:
8031  *
8032  *		REINDEX [ (options) ] type <name>
8033  *****************************************************************************/
8034 
8035 ReindexStmt:
8036 			REINDEX reindex_target_type qualified_name
8037 				{
8038 					ReindexStmt *n = makeNode(ReindexStmt);
8039 					n->kind = $2;
8040 					n->relation = $3;
8041 					n->name = NULL;
8042 					n->options = 0;
8043 					$$ = (Node *)n;
8044 				}
8045 			| REINDEX reindex_target_multitable name
8046 				{
8047 					ReindexStmt *n = makeNode(ReindexStmt);
8048 					n->kind = $2;
8049 					n->name = $3;
8050 					n->relation = NULL;
8051 					n->options = 0;
8052 					$$ = (Node *)n;
8053 				}
8054 			| REINDEX '(' reindex_option_list ')' reindex_target_type qualified_name
8055 				{
8056 					ReindexStmt *n = makeNode(ReindexStmt);
8057 					n->kind = $5;
8058 					n->relation = $6;
8059 					n->name = NULL;
8060 					n->options = $3;
8061 					$$ = (Node *)n;
8062 				}
8063 			| REINDEX '(' reindex_option_list ')' reindex_target_multitable name
8064 				{
8065 					ReindexStmt *n = makeNode(ReindexStmt);
8066 					n->kind = $5;
8067 					n->name = $6;
8068 					n->relation = NULL;
8069 					n->options = $3;
8070 					$$ = (Node *)n;
8071 				}
8072 		;
8073 reindex_target_type:
8074 			INDEX					{ $$ = REINDEX_OBJECT_INDEX; }
8075 			| TABLE					{ $$ = REINDEX_OBJECT_TABLE; }
8076 		;
8077 reindex_target_multitable:
8078 			SCHEMA					{ $$ = REINDEX_OBJECT_SCHEMA; }
8079 			| SYSTEM_P				{ $$ = REINDEX_OBJECT_SYSTEM; }
8080 			| DATABASE				{ $$ = REINDEX_OBJECT_DATABASE; }
8081 		;
8082 reindex_option_list:
8083 			reindex_option_elem								{ $$ = $1; }
8084 			| reindex_option_list ',' reindex_option_elem	{ $$ = $1 | $3; }
8085 		;
8086 reindex_option_elem:
8087 			VERBOSE	{ $$ = REINDEXOPT_VERBOSE; }
8088 		;
8089 
8090 /*****************************************************************************
8091  *
8092  * ALTER TABLESPACE
8093  *
8094  *****************************************************************************/
8095 
8096 AlterTblSpcStmt:
8097 			ALTER TABLESPACE name SET reloptions
8098 				{
8099 					AlterTableSpaceOptionsStmt *n =
8100 						makeNode(AlterTableSpaceOptionsStmt);
8101 					n->tablespacename = $3;
8102 					n->options = $5;
8103 					n->isReset = FALSE;
8104 					$$ = (Node *)n;
8105 				}
8106 			| ALTER TABLESPACE name RESET reloptions
8107 				{
8108 					AlterTableSpaceOptionsStmt *n =
8109 						makeNode(AlterTableSpaceOptionsStmt);
8110 					n->tablespacename = $3;
8111 					n->options = $5;
8112 					n->isReset = TRUE;
8113 					$$ = (Node *)n;
8114 				}
8115 		;
8116 
8117 /*****************************************************************************
8118  *
8119  * ALTER THING name RENAME TO newname
8120  *
8121  *****************************************************************************/
8122 
8123 RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
8124 				{
8125 					RenameStmt *n = makeNode(RenameStmt);
8126 					n->renameType = OBJECT_AGGREGATE;
8127 					n->object = (Node *) $3;
8128 					n->newname = $6;
8129 					n->missing_ok = false;
8130 					$$ = (Node *)n;
8131 				}
8132 			| ALTER COLLATION any_name RENAME TO name
8133 				{
8134 					RenameStmt *n = makeNode(RenameStmt);
8135 					n->renameType = OBJECT_COLLATION;
8136 					n->object = (Node *) $3;
8137 					n->newname = $6;
8138 					n->missing_ok = false;
8139 					$$ = (Node *)n;
8140 				}
8141 			| ALTER CONVERSION_P any_name RENAME TO name
8142 				{
8143 					RenameStmt *n = makeNode(RenameStmt);
8144 					n->renameType = OBJECT_CONVERSION;
8145 					n->object = (Node *) $3;
8146 					n->newname = $6;
8147 					n->missing_ok = false;
8148 					$$ = (Node *)n;
8149 				}
8150 			| ALTER DATABASE database_name RENAME TO database_name
8151 				{
8152 					RenameStmt *n = makeNode(RenameStmt);
8153 					n->renameType = OBJECT_DATABASE;
8154 					n->subname = $3;
8155 					n->newname = $6;
8156 					n->missing_ok = false;
8157 					$$ = (Node *)n;
8158 				}
8159 			| ALTER DOMAIN_P any_name RENAME TO name
8160 				{
8161 					RenameStmt *n = makeNode(RenameStmt);
8162 					n->renameType = OBJECT_DOMAIN;
8163 					n->object = (Node *) $3;
8164 					n->newname = $6;
8165 					n->missing_ok = false;
8166 					$$ = (Node *)n;
8167 				}
8168 			| ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
8169 				{
8170 					RenameStmt *n = makeNode(RenameStmt);
8171 					n->renameType = OBJECT_DOMCONSTRAINT;
8172 					n->object = (Node *) $3;
8173 					n->subname = $6;
8174 					n->newname = $8;
8175 					$$ = (Node *)n;
8176 				}
8177 			| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
8178 				{
8179 					RenameStmt *n = makeNode(RenameStmt);
8180 					n->renameType = OBJECT_FDW;
8181 					n->object = (Node *) makeString($5);
8182 					n->newname = $8;
8183 					n->missing_ok = false;
8184 					$$ = (Node *)n;
8185 				}
8186 			| ALTER FUNCTION function_with_argtypes RENAME TO name
8187 				{
8188 					RenameStmt *n = makeNode(RenameStmt);
8189 					n->renameType = OBJECT_FUNCTION;
8190 					n->object = (Node *) $3;
8191 					n->newname = $6;
8192 					n->missing_ok = false;
8193 					$$ = (Node *)n;
8194 				}
8195 			| ALTER GROUP_P RoleId RENAME TO RoleId
8196 				{
8197 					RenameStmt *n = makeNode(RenameStmt);
8198 					n->renameType = OBJECT_ROLE;
8199 					n->subname = $3;
8200 					n->newname = $6;
8201 					n->missing_ok = false;
8202 					$$ = (Node *)n;
8203 				}
8204 			| ALTER opt_procedural LANGUAGE name RENAME TO name
8205 				{
8206 					RenameStmt *n = makeNode(RenameStmt);
8207 					n->renameType = OBJECT_LANGUAGE;
8208 					n->object = (Node *) makeString($4);
8209 					n->newname = $7;
8210 					n->missing_ok = false;
8211 					$$ = (Node *)n;
8212 				}
8213 			| ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
8214 				{
8215 					RenameStmt *n = makeNode(RenameStmt);
8216 					n->renameType = OBJECT_OPCLASS;
8217 					n->object = (Node *) lcons(makeString($6), $4);
8218 					n->newname = $9;
8219 					n->missing_ok = false;
8220 					$$ = (Node *)n;
8221 				}
8222 			| ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
8223 				{
8224 					RenameStmt *n = makeNode(RenameStmt);
8225 					n->renameType = OBJECT_OPFAMILY;
8226 					n->object = (Node *) lcons(makeString($6), $4);
8227 					n->newname = $9;
8228 					n->missing_ok = false;
8229 					$$ = (Node *)n;
8230 				}
8231 			| ALTER POLICY name ON qualified_name RENAME TO name
8232 				{
8233 					RenameStmt *n = makeNode(RenameStmt);
8234 					n->renameType = OBJECT_POLICY;
8235 					n->relation = $5;
8236 					n->subname = $3;
8237 					n->newname = $8;
8238 					n->missing_ok = false;
8239 					$$ = (Node *)n;
8240 				}
8241 			| ALTER POLICY IF_P EXISTS name ON qualified_name RENAME TO name
8242 				{
8243 					RenameStmt *n = makeNode(RenameStmt);
8244 					n->renameType = OBJECT_POLICY;
8245 					n->relation = $7;
8246 					n->subname = $5;
8247 					n->newname = $10;
8248 					n->missing_ok = true;
8249 					$$ = (Node *)n;
8250 				}
8251 			| ALTER PUBLICATION name RENAME TO name
8252 				{
8253 					RenameStmt *n = makeNode(RenameStmt);
8254 					n->renameType = OBJECT_PUBLICATION;
8255 					n->object = (Node *) makeString($3);
8256 					n->newname = $6;
8257 					n->missing_ok = false;
8258 					$$ = (Node *)n;
8259 				}
8260 			| ALTER SCHEMA name RENAME TO name
8261 				{
8262 					RenameStmt *n = makeNode(RenameStmt);
8263 					n->renameType = OBJECT_SCHEMA;
8264 					n->subname = $3;
8265 					n->newname = $6;
8266 					n->missing_ok = false;
8267 					$$ = (Node *)n;
8268 				}
8269 			| ALTER SERVER name RENAME TO name
8270 				{
8271 					RenameStmt *n = makeNode(RenameStmt);
8272 					n->renameType = OBJECT_FOREIGN_SERVER;
8273 					n->object = (Node *) makeString($3);
8274 					n->newname = $6;
8275 					n->missing_ok = false;
8276 					$$ = (Node *)n;
8277 				}
8278 			| ALTER SUBSCRIPTION name RENAME TO name
8279 				{
8280 					RenameStmt *n = makeNode(RenameStmt);
8281 					n->renameType = OBJECT_SUBSCRIPTION;
8282 					n->object = (Node *) makeString($3);
8283 					n->newname = $6;
8284 					n->missing_ok = false;
8285 					$$ = (Node *)n;
8286 				}
8287 			| ALTER TABLE relation_expr RENAME TO name
8288 				{
8289 					RenameStmt *n = makeNode(RenameStmt);
8290 					n->renameType = OBJECT_TABLE;
8291 					n->relation = $3;
8292 					n->subname = NULL;
8293 					n->newname = $6;
8294 					n->missing_ok = false;
8295 					$$ = (Node *)n;
8296 				}
8297 			| ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
8298 				{
8299 					RenameStmt *n = makeNode(RenameStmt);
8300 					n->renameType = OBJECT_TABLE;
8301 					n->relation = $5;
8302 					n->subname = NULL;
8303 					n->newname = $8;
8304 					n->missing_ok = true;
8305 					$$ = (Node *)n;
8306 				}
8307 			| ALTER SEQUENCE qualified_name RENAME TO name
8308 				{
8309 					RenameStmt *n = makeNode(RenameStmt);
8310 					n->renameType = OBJECT_SEQUENCE;
8311 					n->relation = $3;
8312 					n->subname = NULL;
8313 					n->newname = $6;
8314 					n->missing_ok = false;
8315 					$$ = (Node *)n;
8316 				}
8317 			| ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
8318 				{
8319 					RenameStmt *n = makeNode(RenameStmt);
8320 					n->renameType = OBJECT_SEQUENCE;
8321 					n->relation = $5;
8322 					n->subname = NULL;
8323 					n->newname = $8;
8324 					n->missing_ok = true;
8325 					$$ = (Node *)n;
8326 				}
8327 			| ALTER VIEW qualified_name RENAME TO name
8328 				{
8329 					RenameStmt *n = makeNode(RenameStmt);
8330 					n->renameType = OBJECT_VIEW;
8331 					n->relation = $3;
8332 					n->subname = NULL;
8333 					n->newname = $6;
8334 					n->missing_ok = false;
8335 					$$ = (Node *)n;
8336 				}
8337 			| ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
8338 				{
8339 					RenameStmt *n = makeNode(RenameStmt);
8340 					n->renameType = OBJECT_VIEW;
8341 					n->relation = $5;
8342 					n->subname = NULL;
8343 					n->newname = $8;
8344 					n->missing_ok = true;
8345 					$$ = (Node *)n;
8346 				}
8347 			| ALTER MATERIALIZED VIEW qualified_name RENAME TO name
8348 				{
8349 					RenameStmt *n = makeNode(RenameStmt);
8350 					n->renameType = OBJECT_MATVIEW;
8351 					n->relation = $4;
8352 					n->subname = NULL;
8353 					n->newname = $7;
8354 					n->missing_ok = false;
8355 					$$ = (Node *)n;
8356 				}
8357 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
8358 				{
8359 					RenameStmt *n = makeNode(RenameStmt);
8360 					n->renameType = OBJECT_MATVIEW;
8361 					n->relation = $6;
8362 					n->subname = NULL;
8363 					n->newname = $9;
8364 					n->missing_ok = true;
8365 					$$ = (Node *)n;
8366 				}
8367 			| ALTER INDEX qualified_name RENAME TO name
8368 				{
8369 					RenameStmt *n = makeNode(RenameStmt);
8370 					n->renameType = OBJECT_INDEX;
8371 					n->relation = $3;
8372 					n->subname = NULL;
8373 					n->newname = $6;
8374 					n->missing_ok = false;
8375 					$$ = (Node *)n;
8376 				}
8377 			| ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
8378 				{
8379 					RenameStmt *n = makeNode(RenameStmt);
8380 					n->renameType = OBJECT_INDEX;
8381 					n->relation = $5;
8382 					n->subname = NULL;
8383 					n->newname = $8;
8384 					n->missing_ok = true;
8385 					$$ = (Node *)n;
8386 				}
8387 			| ALTER FOREIGN TABLE relation_expr RENAME TO name
8388 				{
8389 					RenameStmt *n = makeNode(RenameStmt);
8390 					n->renameType = OBJECT_FOREIGN_TABLE;
8391 					n->relation = $4;
8392 					n->subname = NULL;
8393 					n->newname = $7;
8394 					n->missing_ok = false;
8395 					$$ = (Node *)n;
8396 				}
8397 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
8398 				{
8399 					RenameStmt *n = makeNode(RenameStmt);
8400 					n->renameType = OBJECT_FOREIGN_TABLE;
8401 					n->relation = $6;
8402 					n->subname = NULL;
8403 					n->newname = $9;
8404 					n->missing_ok = true;
8405 					$$ = (Node *)n;
8406 				}
8407 			| ALTER TABLE relation_expr RENAME opt_column name TO name
8408 				{
8409 					RenameStmt *n = makeNode(RenameStmt);
8410 					n->renameType = OBJECT_COLUMN;
8411 					n->relationType = OBJECT_TABLE;
8412 					n->relation = $3;
8413 					n->subname = $6;
8414 					n->newname = $8;
8415 					n->missing_ok = false;
8416 					$$ = (Node *)n;
8417 				}
8418 			| ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8419 				{
8420 					RenameStmt *n = makeNode(RenameStmt);
8421 					n->renameType = OBJECT_COLUMN;
8422 					n->relationType = OBJECT_TABLE;
8423 					n->relation = $5;
8424 					n->subname = $8;
8425 					n->newname = $10;
8426 					n->missing_ok = true;
8427 					$$ = (Node *)n;
8428 				}
8429 			| ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
8430 				{
8431 					RenameStmt *n = makeNode(RenameStmt);
8432 					n->renameType = OBJECT_COLUMN;
8433 					n->relationType = OBJECT_MATVIEW;
8434 					n->relation = $4;
8435 					n->subname = $7;
8436 					n->newname = $9;
8437 					n->missing_ok = false;
8438 					$$ = (Node *)n;
8439 				}
8440 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
8441 				{
8442 					RenameStmt *n = makeNode(RenameStmt);
8443 					n->renameType = OBJECT_COLUMN;
8444 					n->relationType = OBJECT_MATVIEW;
8445 					n->relation = $6;
8446 					n->subname = $9;
8447 					n->newname = $11;
8448 					n->missing_ok = true;
8449 					$$ = (Node *)n;
8450 				}
8451 			| ALTER TABLE relation_expr RENAME CONSTRAINT name TO name
8452 				{
8453 					RenameStmt *n = makeNode(RenameStmt);
8454 					n->renameType = OBJECT_TABCONSTRAINT;
8455 					n->relation = $3;
8456 					n->subname = $6;
8457 					n->newname = $8;
8458 					n->missing_ok = false;
8459 					$$ = (Node *)n;
8460 				}
8461 			| ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
8462 				{
8463 					RenameStmt *n = makeNode(RenameStmt);
8464 					n->renameType = OBJECT_TABCONSTRAINT;
8465 					n->relation = $5;
8466 					n->subname = $8;
8467 					n->newname = $10;
8468 					n->missing_ok = true;
8469 					$$ = (Node *)n;
8470 				}
8471 			| ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
8472 				{
8473 					RenameStmt *n = makeNode(RenameStmt);
8474 					n->renameType = OBJECT_COLUMN;
8475 					n->relationType = OBJECT_FOREIGN_TABLE;
8476 					n->relation = $4;
8477 					n->subname = $7;
8478 					n->newname = $9;
8479 					n->missing_ok = false;
8480 					$$ = (Node *)n;
8481 				}
8482 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8483 				{
8484 					RenameStmt *n = makeNode(RenameStmt);
8485 					n->renameType = OBJECT_COLUMN;
8486 					n->relationType = OBJECT_FOREIGN_TABLE;
8487 					n->relation = $6;
8488 					n->subname = $9;
8489 					n->newname = $11;
8490 					n->missing_ok = true;
8491 					$$ = (Node *)n;
8492 				}
8493 			| ALTER RULE name ON qualified_name RENAME TO name
8494 				{
8495 					RenameStmt *n = makeNode(RenameStmt);
8496 					n->renameType = OBJECT_RULE;
8497 					n->relation = $5;
8498 					n->subname = $3;
8499 					n->newname = $8;
8500 					n->missing_ok = false;
8501 					$$ = (Node *)n;
8502 				}
8503 			| ALTER TRIGGER name ON qualified_name RENAME TO name
8504 				{
8505 					RenameStmt *n = makeNode(RenameStmt);
8506 					n->renameType = OBJECT_TRIGGER;
8507 					n->relation = $5;
8508 					n->subname = $3;
8509 					n->newname = $8;
8510 					n->missing_ok = false;
8511 					$$ = (Node *)n;
8512 				}
8513 			| ALTER EVENT TRIGGER name RENAME TO name
8514 				{
8515 					RenameStmt *n = makeNode(RenameStmt);
8516 					n->renameType = OBJECT_EVENT_TRIGGER;
8517 					n->object = (Node *) makeString($4);
8518 					n->newname = $7;
8519 					$$ = (Node *)n;
8520 				}
8521 			| ALTER ROLE RoleId RENAME TO RoleId
8522 				{
8523 					RenameStmt *n = makeNode(RenameStmt);
8524 					n->renameType = OBJECT_ROLE;
8525 					n->subname = $3;
8526 					n->newname = $6;
8527 					n->missing_ok = false;
8528 					$$ = (Node *)n;
8529 				}
8530 			| ALTER USER 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 TABLESPACE name RENAME TO name
8540 				{
8541 					RenameStmt *n = makeNode(RenameStmt);
8542 					n->renameType = OBJECT_TABLESPACE;
8543 					n->subname = $3;
8544 					n->newname = $6;
8545 					n->missing_ok = false;
8546 					$$ = (Node *)n;
8547 				}
8548 			| ALTER STATISTICS any_name RENAME TO name
8549 				{
8550 					RenameStmt *n = makeNode(RenameStmt);
8551 					n->renameType = OBJECT_STATISTIC_EXT;
8552 					n->object = (Node *) $3;
8553 					n->newname = $6;
8554 					n->missing_ok = false;
8555 					$$ = (Node *)n;
8556 				}
8557 			| ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
8558 				{
8559 					RenameStmt *n = makeNode(RenameStmt);
8560 					n->renameType = OBJECT_TSPARSER;
8561 					n->object = (Node *) $5;
8562 					n->newname = $8;
8563 					n->missing_ok = false;
8564 					$$ = (Node *)n;
8565 				}
8566 			| ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
8567 				{
8568 					RenameStmt *n = makeNode(RenameStmt);
8569 					n->renameType = OBJECT_TSDICTIONARY;
8570 					n->object = (Node *) $5;
8571 					n->newname = $8;
8572 					n->missing_ok = false;
8573 					$$ = (Node *)n;
8574 				}
8575 			| ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
8576 				{
8577 					RenameStmt *n = makeNode(RenameStmt);
8578 					n->renameType = OBJECT_TSTEMPLATE;
8579 					n->object = (Node *) $5;
8580 					n->newname = $8;
8581 					n->missing_ok = false;
8582 					$$ = (Node *)n;
8583 				}
8584 			| ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
8585 				{
8586 					RenameStmt *n = makeNode(RenameStmt);
8587 					n->renameType = OBJECT_TSCONFIGURATION;
8588 					n->object = (Node *) $5;
8589 					n->newname = $8;
8590 					n->missing_ok = false;
8591 					$$ = (Node *)n;
8592 				}
8593 			| ALTER TYPE_P any_name RENAME TO name
8594 				{
8595 					RenameStmt *n = makeNode(RenameStmt);
8596 					n->renameType = OBJECT_TYPE;
8597 					n->object = (Node *) $3;
8598 					n->newname = $6;
8599 					n->missing_ok = false;
8600 					$$ = (Node *)n;
8601 				}
8602 			| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
8603 				{
8604 					RenameStmt *n = makeNode(RenameStmt);
8605 					n->renameType = OBJECT_ATTRIBUTE;
8606 					n->relationType = OBJECT_TYPE;
8607 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
8608 					n->subname = $6;
8609 					n->newname = $8;
8610 					n->behavior = $9;
8611 					n->missing_ok = false;
8612 					$$ = (Node *)n;
8613 				}
8614 		;
8615 
8616 opt_column: COLUMN									{ $$ = COLUMN; }
8617 			| /*EMPTY*/								{ $$ = 0; }
8618 		;
8619 
8620 opt_set_data: SET DATA_P							{ $$ = 1; }
8621 			| /*EMPTY*/								{ $$ = 0; }
8622 		;
8623 
8624 /*****************************************************************************
8625  *
8626  * ALTER THING name DEPENDS ON EXTENSION name
8627  *
8628  *****************************************************************************/
8629 
8630 AlterObjectDependsStmt:
8631 			ALTER FUNCTION function_with_argtypes DEPENDS ON EXTENSION name
8632 				{
8633 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8634 					n->objectType = OBJECT_FUNCTION;
8635 					n->object = (Node *) $3;
8636 					n->extname = makeString($7);
8637 					$$ = (Node *)n;
8638 				}
8639 			| ALTER TRIGGER name ON qualified_name DEPENDS ON EXTENSION name
8640 				{
8641 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8642 					n->objectType = OBJECT_TRIGGER;
8643 					n->relation = $5;
8644 					n->object = (Node *) list_make1(makeString($3));
8645 					n->extname = makeString($9);
8646 					$$ = (Node *)n;
8647 				}
8648 			| ALTER MATERIALIZED VIEW qualified_name DEPENDS ON EXTENSION name
8649 				{
8650 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8651 					n->objectType = OBJECT_MATVIEW;
8652 					n->relation = $4;
8653 					n->extname = makeString($8);
8654 					$$ = (Node *)n;
8655 				}
8656 			| ALTER INDEX qualified_name DEPENDS ON EXTENSION name
8657 				{
8658 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8659 					n->objectType = OBJECT_INDEX;
8660 					n->relation = $3;
8661 					n->extname = makeString($7);
8662 					$$ = (Node *)n;
8663 				}
8664 		;
8665 
8666 /*****************************************************************************
8667  *
8668  * ALTER THING name SET SCHEMA name
8669  *
8670  *****************************************************************************/
8671 
8672 AlterObjectSchemaStmt:
8673 			ALTER AGGREGATE aggregate_with_argtypes SET SCHEMA name
8674 				{
8675 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8676 					n->objectType = OBJECT_AGGREGATE;
8677 					n->object = (Node *) $3;
8678 					n->newschema = $6;
8679 					n->missing_ok = false;
8680 					$$ = (Node *)n;
8681 				}
8682 			| ALTER COLLATION any_name SET SCHEMA name
8683 				{
8684 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8685 					n->objectType = OBJECT_COLLATION;
8686 					n->object = (Node *) $3;
8687 					n->newschema = $6;
8688 					n->missing_ok = false;
8689 					$$ = (Node *)n;
8690 				}
8691 			| ALTER CONVERSION_P any_name SET SCHEMA name
8692 				{
8693 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8694 					n->objectType = OBJECT_CONVERSION;
8695 					n->object = (Node *) $3;
8696 					n->newschema = $6;
8697 					n->missing_ok = false;
8698 					$$ = (Node *)n;
8699 				}
8700 			| ALTER DOMAIN_P any_name SET SCHEMA name
8701 				{
8702 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8703 					n->objectType = OBJECT_DOMAIN;
8704 					n->object = (Node *) $3;
8705 					n->newschema = $6;
8706 					n->missing_ok = false;
8707 					$$ = (Node *)n;
8708 				}
8709 			| ALTER EXTENSION name SET SCHEMA name
8710 				{
8711 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8712 					n->objectType = OBJECT_EXTENSION;
8713 					n->object = (Node *) makeString($3);
8714 					n->newschema = $6;
8715 					n->missing_ok = false;
8716 					$$ = (Node *)n;
8717 				}
8718 			| ALTER FUNCTION function_with_argtypes SET SCHEMA name
8719 				{
8720 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8721 					n->objectType = OBJECT_FUNCTION;
8722 					n->object = (Node *) $3;
8723 					n->newschema = $6;
8724 					n->missing_ok = false;
8725 					$$ = (Node *)n;
8726 				}
8727 			| ALTER OPERATOR operator_with_argtypes SET SCHEMA name
8728 				{
8729 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8730 					n->objectType = OBJECT_OPERATOR;
8731 					n->object = (Node *) $3;
8732 					n->newschema = $6;
8733 					n->missing_ok = false;
8734 					$$ = (Node *)n;
8735 				}
8736 			| ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
8737 				{
8738 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8739 					n->objectType = OBJECT_OPCLASS;
8740 					n->object = (Node *) lcons(makeString($6), $4);
8741 					n->newschema = $9;
8742 					n->missing_ok = false;
8743 					$$ = (Node *)n;
8744 				}
8745 			| ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
8746 				{
8747 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8748 					n->objectType = OBJECT_OPFAMILY;
8749 					n->object = (Node *) lcons(makeString($6), $4);
8750 					n->newschema = $9;
8751 					n->missing_ok = false;
8752 					$$ = (Node *)n;
8753 				}
8754 			| ALTER TABLE relation_expr SET SCHEMA name
8755 				{
8756 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8757 					n->objectType = OBJECT_TABLE;
8758 					n->relation = $3;
8759 					n->newschema = $6;
8760 					n->missing_ok = false;
8761 					$$ = (Node *)n;
8762 				}
8763 			| ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
8764 				{
8765 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8766 					n->objectType = OBJECT_TABLE;
8767 					n->relation = $5;
8768 					n->newschema = $8;
8769 					n->missing_ok = true;
8770 					$$ = (Node *)n;
8771 				}
8772 			| ALTER STATISTICS any_name SET SCHEMA name
8773 				{
8774 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8775 					n->objectType = OBJECT_STATISTIC_EXT;
8776 					n->object = (Node *) $3;
8777 					n->newschema = $6;
8778 					n->missing_ok = false;
8779 					$$ = (Node *)n;
8780 				}
8781 			| ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
8782 				{
8783 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8784 					n->objectType = OBJECT_TSPARSER;
8785 					n->object = (Node *) $5;
8786 					n->newschema = $8;
8787 					n->missing_ok = false;
8788 					$$ = (Node *)n;
8789 				}
8790 			| ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
8791 				{
8792 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8793 					n->objectType = OBJECT_TSDICTIONARY;
8794 					n->object = (Node *) $5;
8795 					n->newschema = $8;
8796 					n->missing_ok = false;
8797 					$$ = (Node *)n;
8798 				}
8799 			| ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
8800 				{
8801 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8802 					n->objectType = OBJECT_TSTEMPLATE;
8803 					n->object = (Node *) $5;
8804 					n->newschema = $8;
8805 					n->missing_ok = false;
8806 					$$ = (Node *)n;
8807 				}
8808 			| ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
8809 				{
8810 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8811 					n->objectType = OBJECT_TSCONFIGURATION;
8812 					n->object = (Node *) $5;
8813 					n->newschema = $8;
8814 					n->missing_ok = false;
8815 					$$ = (Node *)n;
8816 				}
8817 			| ALTER SEQUENCE qualified_name SET SCHEMA name
8818 				{
8819 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8820 					n->objectType = OBJECT_SEQUENCE;
8821 					n->relation = $3;
8822 					n->newschema = $6;
8823 					n->missing_ok = false;
8824 					$$ = (Node *)n;
8825 				}
8826 			| ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
8827 				{
8828 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8829 					n->objectType = OBJECT_SEQUENCE;
8830 					n->relation = $5;
8831 					n->newschema = $8;
8832 					n->missing_ok = true;
8833 					$$ = (Node *)n;
8834 				}
8835 			| ALTER VIEW qualified_name SET SCHEMA name
8836 				{
8837 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8838 					n->objectType = OBJECT_VIEW;
8839 					n->relation = $3;
8840 					n->newschema = $6;
8841 					n->missing_ok = false;
8842 					$$ = (Node *)n;
8843 				}
8844 			| ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
8845 				{
8846 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8847 					n->objectType = OBJECT_VIEW;
8848 					n->relation = $5;
8849 					n->newschema = $8;
8850 					n->missing_ok = true;
8851 					$$ = (Node *)n;
8852 				}
8853 			| ALTER MATERIALIZED VIEW qualified_name SET SCHEMA name
8854 				{
8855 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8856 					n->objectType = OBJECT_MATVIEW;
8857 					n->relation = $4;
8858 					n->newschema = $7;
8859 					n->missing_ok = false;
8860 					$$ = (Node *)n;
8861 				}
8862 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name SET SCHEMA name
8863 				{
8864 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8865 					n->objectType = OBJECT_MATVIEW;
8866 					n->relation = $6;
8867 					n->newschema = $9;
8868 					n->missing_ok = true;
8869 					$$ = (Node *)n;
8870 				}
8871 			| ALTER FOREIGN TABLE relation_expr SET SCHEMA name
8872 				{
8873 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8874 					n->objectType = OBJECT_FOREIGN_TABLE;
8875 					n->relation = $4;
8876 					n->newschema = $7;
8877 					n->missing_ok = false;
8878 					$$ = (Node *)n;
8879 				}
8880 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
8881 				{
8882 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8883 					n->objectType = OBJECT_FOREIGN_TABLE;
8884 					n->relation = $6;
8885 					n->newschema = $9;
8886 					n->missing_ok = true;
8887 					$$ = (Node *)n;
8888 				}
8889 			| ALTER TYPE_P any_name SET SCHEMA name
8890 				{
8891 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8892 					n->objectType = OBJECT_TYPE;
8893 					n->object = (Node *) $3;
8894 					n->newschema = $6;
8895 					n->missing_ok = false;
8896 					$$ = (Node *)n;
8897 				}
8898 		;
8899 
8900 /*****************************************************************************
8901  *
8902  * ALTER OPERATOR name SET define
8903  *
8904  *****************************************************************************/
8905 
8906 AlterOperatorStmt:
8907 			ALTER OPERATOR operator_with_argtypes SET '(' operator_def_list ')'
8908 				{
8909 					AlterOperatorStmt *n = makeNode(AlterOperatorStmt);
8910 					n->opername = $3;
8911 					n->options = $6;
8912 					$$ = (Node *)n;
8913 				}
8914 		;
8915 
8916 operator_def_list:	operator_def_elem								{ $$ = list_make1($1); }
8917 			| operator_def_list ',' operator_def_elem				{ $$ = lappend($1, $3); }
8918 		;
8919 
8920 operator_def_elem: ColLabel '=' NONE
8921 						{ $$ = makeDefElem($1, NULL, @1); }
8922 				   | ColLabel '=' operator_def_arg
8923 						{ $$ = makeDefElem($1, (Node *) $3, @1); }
8924 		;
8925 
8926 /* must be similar enough to def_arg to avoid reduce/reduce conflicts */
8927 operator_def_arg:
8928 			func_type						{ $$ = (Node *)$1; }
8929 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
8930 			| qual_all_Op					{ $$ = (Node *)$1; }
8931 			| NumericOnly					{ $$ = (Node *)$1; }
8932 			| Sconst						{ $$ = (Node *)makeString($1); }
8933 		;
8934 
8935 /*****************************************************************************
8936  *
8937  * ALTER THING name OWNER TO newname
8938  *
8939  *****************************************************************************/
8940 
8941 AlterOwnerStmt: ALTER AGGREGATE aggregate_with_argtypes OWNER TO RoleSpec
8942 				{
8943 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8944 					n->objectType = OBJECT_AGGREGATE;
8945 					n->object = (Node *) $3;
8946 					n->newowner = $6;
8947 					$$ = (Node *)n;
8948 				}
8949 			| ALTER COLLATION any_name OWNER TO RoleSpec
8950 				{
8951 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8952 					n->objectType = OBJECT_COLLATION;
8953 					n->object = (Node *) $3;
8954 					n->newowner = $6;
8955 					$$ = (Node *)n;
8956 				}
8957 			| ALTER CONVERSION_P any_name OWNER TO RoleSpec
8958 				{
8959 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8960 					n->objectType = OBJECT_CONVERSION;
8961 					n->object = (Node *) $3;
8962 					n->newowner = $6;
8963 					$$ = (Node *)n;
8964 				}
8965 			| ALTER DATABASE database_name OWNER TO RoleSpec
8966 				{
8967 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8968 					n->objectType = OBJECT_DATABASE;
8969 					n->object = (Node *) makeString($3);
8970 					n->newowner = $6;
8971 					$$ = (Node *)n;
8972 				}
8973 			| ALTER DOMAIN_P any_name OWNER TO RoleSpec
8974 				{
8975 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8976 					n->objectType = OBJECT_DOMAIN;
8977 					n->object = (Node *) $3;
8978 					n->newowner = $6;
8979 					$$ = (Node *)n;
8980 				}
8981 			| ALTER FUNCTION function_with_argtypes OWNER TO RoleSpec
8982 				{
8983 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8984 					n->objectType = OBJECT_FUNCTION;
8985 					n->object = (Node *) $3;
8986 					n->newowner = $6;
8987 					$$ = (Node *)n;
8988 				}
8989 			| ALTER opt_procedural LANGUAGE name OWNER TO RoleSpec
8990 				{
8991 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8992 					n->objectType = OBJECT_LANGUAGE;
8993 					n->object = (Node *) makeString($4);
8994 					n->newowner = $7;
8995 					$$ = (Node *)n;
8996 				}
8997 			| ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleSpec
8998 				{
8999 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9000 					n->objectType = OBJECT_LARGEOBJECT;
9001 					n->object = (Node *) $4;
9002 					n->newowner = $7;
9003 					$$ = (Node *)n;
9004 				}
9005 			| ALTER OPERATOR operator_with_argtypes OWNER TO RoleSpec
9006 				{
9007 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9008 					n->objectType = OBJECT_OPERATOR;
9009 					n->object = (Node *) $3;
9010 					n->newowner = $6;
9011 					$$ = (Node *)n;
9012 				}
9013 			| ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleSpec
9014 				{
9015 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9016 					n->objectType = OBJECT_OPCLASS;
9017 					n->object = (Node *) lcons(makeString($6), $4);
9018 					n->newowner = $9;
9019 					$$ = (Node *)n;
9020 				}
9021 			| ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleSpec
9022 				{
9023 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9024 					n->objectType = OBJECT_OPFAMILY;
9025 					n->object = (Node *) lcons(makeString($6), $4);
9026 					n->newowner = $9;
9027 					$$ = (Node *)n;
9028 				}
9029 			| ALTER SCHEMA name OWNER TO RoleSpec
9030 				{
9031 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9032 					n->objectType = OBJECT_SCHEMA;
9033 					n->object = (Node *) makeString($3);
9034 					n->newowner = $6;
9035 					$$ = (Node *)n;
9036 				}
9037 			| ALTER TYPE_P any_name OWNER TO RoleSpec
9038 				{
9039 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9040 					n->objectType = OBJECT_TYPE;
9041 					n->object = (Node *) $3;
9042 					n->newowner = $6;
9043 					$$ = (Node *)n;
9044 				}
9045 			| ALTER TABLESPACE name OWNER TO RoleSpec
9046 				{
9047 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9048 					n->objectType = OBJECT_TABLESPACE;
9049 					n->object = (Node *) makeString($3);
9050 					n->newowner = $6;
9051 					$$ = (Node *)n;
9052 				}
9053 			| ALTER STATISTICS any_name OWNER TO RoleSpec
9054 				{
9055 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9056 					n->objectType = OBJECT_STATISTIC_EXT;
9057 					n->object = (Node *) $3;
9058 					n->newowner = $6;
9059 					$$ = (Node *)n;
9060 				}
9061 			| ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleSpec
9062 				{
9063 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9064 					n->objectType = OBJECT_TSDICTIONARY;
9065 					n->object = (Node *) $5;
9066 					n->newowner = $8;
9067 					$$ = (Node *)n;
9068 				}
9069 			| ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleSpec
9070 				{
9071 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9072 					n->objectType = OBJECT_TSCONFIGURATION;
9073 					n->object = (Node *) $5;
9074 					n->newowner = $8;
9075 					$$ = (Node *)n;
9076 				}
9077 			| ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleSpec
9078 				{
9079 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9080 					n->objectType = OBJECT_FDW;
9081 					n->object = (Node *) makeString($5);
9082 					n->newowner = $8;
9083 					$$ = (Node *)n;
9084 				}
9085 			| ALTER SERVER name OWNER TO RoleSpec
9086 				{
9087 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9088 					n->objectType = OBJECT_FOREIGN_SERVER;
9089 					n->object = (Node *) makeString($3);
9090 					n->newowner = $6;
9091 					$$ = (Node *)n;
9092 				}
9093 			| ALTER EVENT TRIGGER name OWNER TO RoleSpec
9094 				{
9095 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9096 					n->objectType = OBJECT_EVENT_TRIGGER;
9097 					n->object = (Node *) makeString($4);
9098 					n->newowner = $7;
9099 					$$ = (Node *)n;
9100 				}
9101 			| ALTER PUBLICATION name OWNER TO RoleSpec
9102 				{
9103 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9104 					n->objectType = OBJECT_PUBLICATION;
9105 					n->object = (Node *) makeString($3);
9106 					n->newowner = $6;
9107 					$$ = (Node *)n;
9108 				}
9109 			| ALTER SUBSCRIPTION name OWNER TO RoleSpec
9110 				{
9111 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9112 					n->objectType = OBJECT_SUBSCRIPTION;
9113 					n->object = (Node *) makeString($3);
9114 					n->newowner = $6;
9115 					$$ = (Node *)n;
9116 				}
9117 		;
9118 
9119 
9120 /*****************************************************************************
9121  *
9122  * CREATE PUBLICATION name [ FOR TABLE ] [ WITH options ]
9123  *
9124  *****************************************************************************/
9125 
9126 CreatePublicationStmt:
9127 			CREATE PUBLICATION name opt_publication_for_tables opt_definition
9128 				{
9129 					CreatePublicationStmt *n = makeNode(CreatePublicationStmt);
9130 					n->pubname = $3;
9131 					n->options = $5;
9132 					if ($4 != NULL)
9133 					{
9134 						/* FOR TABLE */
9135 						if (IsA($4, List))
9136 							n->tables = (List *)$4;
9137 						/* FOR ALL TABLES */
9138 						else
9139 							n->for_all_tables = TRUE;
9140 					}
9141 					$$ = (Node *)n;
9142 				}
9143 		;
9144 
9145 opt_publication_for_tables:
9146 			publication_for_tables					{ $$ = $1; }
9147 			| /* EMPTY */							{ $$ = NULL; }
9148 		;
9149 
9150 publication_for_tables:
9151 			FOR TABLE relation_expr_list
9152 				{
9153 					$$ = (Node *) $3;
9154 				}
9155 			| FOR ALL TABLES
9156 				{
9157 					$$ = (Node *) makeInteger(TRUE);
9158 				}
9159 		;
9160 
9161 
9162 /*****************************************************************************
9163  *
9164  * ALTER PUBLICATION name SET ( options )
9165  *
9166  * ALTER PUBLICATION name ADD TABLE table [, table2]
9167  *
9168  * ALTER PUBLICATION name DROP TABLE table [, table2]
9169  *
9170  * ALTER PUBLICATION name SET TABLE table [, table2]
9171  *
9172  *****************************************************************************/
9173 
9174 AlterPublicationStmt:
9175 			ALTER PUBLICATION name SET definition
9176 				{
9177 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9178 					n->pubname = $3;
9179 					n->options = $5;
9180 					$$ = (Node *)n;
9181 				}
9182 			| ALTER PUBLICATION name ADD_P TABLE relation_expr_list
9183 				{
9184 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9185 					n->pubname = $3;
9186 					n->tables = $6;
9187 					n->tableAction = DEFELEM_ADD;
9188 					$$ = (Node *)n;
9189 				}
9190 			| ALTER PUBLICATION name SET TABLE relation_expr_list
9191 				{
9192 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9193 					n->pubname = $3;
9194 					n->tables = $6;
9195 					n->tableAction = DEFELEM_SET;
9196 					$$ = (Node *)n;
9197 				}
9198 			| ALTER PUBLICATION name DROP TABLE relation_expr_list
9199 				{
9200 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9201 					n->pubname = $3;
9202 					n->tables = $6;
9203 					n->tableAction = DEFELEM_DROP;
9204 					$$ = (Node *)n;
9205 				}
9206 		;
9207 
9208 /*****************************************************************************
9209  *
9210  * CREATE SUBSCRIPTION name ...
9211  *
9212  *****************************************************************************/
9213 
9214 CreateSubscriptionStmt:
9215 			CREATE SUBSCRIPTION name CONNECTION Sconst PUBLICATION publication_name_list opt_definition
9216 				{
9217 					CreateSubscriptionStmt *n =
9218 						makeNode(CreateSubscriptionStmt);
9219 					n->subname = $3;
9220 					n->conninfo = $5;
9221 					n->publication = $7;
9222 					n->options = $8;
9223 					$$ = (Node *)n;
9224 				}
9225 		;
9226 
9227 publication_name_list:
9228 			publication_name_item
9229 				{
9230 					$$ = list_make1($1);
9231 				}
9232 			| publication_name_list ',' publication_name_item
9233 				{
9234 					$$ = lappend($1, $3);
9235 				}
9236 		;
9237 
9238 publication_name_item:
9239 			ColLabel			{ $$ = makeString($1); };
9240 
9241 /*****************************************************************************
9242  *
9243  * ALTER SUBSCRIPTION name ...
9244  *
9245  *****************************************************************************/
9246 
9247 AlterSubscriptionStmt:
9248 			ALTER SUBSCRIPTION name SET definition
9249 				{
9250 					AlterSubscriptionStmt *n =
9251 						makeNode(AlterSubscriptionStmt);
9252 					n->kind = ALTER_SUBSCRIPTION_OPTIONS;
9253 					n->subname = $3;
9254 					n->options = $5;
9255 					$$ = (Node *)n;
9256 				}
9257 			| ALTER SUBSCRIPTION name CONNECTION Sconst
9258 				{
9259 					AlterSubscriptionStmt *n =
9260 						makeNode(AlterSubscriptionStmt);
9261 					n->kind = ALTER_SUBSCRIPTION_CONNECTION;
9262 					n->subname = $3;
9263 					n->conninfo = $5;
9264 					$$ = (Node *)n;
9265 				}
9266 			| ALTER SUBSCRIPTION name REFRESH PUBLICATION opt_definition
9267 				{
9268 					AlterSubscriptionStmt *n =
9269 						makeNode(AlterSubscriptionStmt);
9270 					n->kind = ALTER_SUBSCRIPTION_REFRESH;
9271 					n->subname = $3;
9272 					n->options = $6;
9273 					$$ = (Node *)n;
9274 				}
9275 			| ALTER SUBSCRIPTION name SET PUBLICATION publication_name_list opt_definition
9276 				{
9277 					AlterSubscriptionStmt *n =
9278 						makeNode(AlterSubscriptionStmt);
9279 					n->kind = ALTER_SUBSCRIPTION_PUBLICATION;
9280 					n->subname = $3;
9281 					n->publication = $6;
9282 					n->options = $7;
9283 					$$ = (Node *)n;
9284 				}
9285 			| ALTER SUBSCRIPTION name ENABLE_P
9286 				{
9287 					AlterSubscriptionStmt *n =
9288 						makeNode(AlterSubscriptionStmt);
9289 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9290 					n->subname = $3;
9291 					n->options = list_make1(makeDefElem("enabled",
9292 											(Node *)makeInteger(TRUE), @1));
9293 					$$ = (Node *)n;
9294 				}
9295 			| ALTER SUBSCRIPTION name DISABLE_P
9296 				{
9297 					AlterSubscriptionStmt *n =
9298 						makeNode(AlterSubscriptionStmt);
9299 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9300 					n->subname = $3;
9301 					n->options = list_make1(makeDefElem("enabled",
9302 											(Node *)makeInteger(FALSE), @1));
9303 					$$ = (Node *)n;
9304 				}
9305 		;
9306 
9307 /*****************************************************************************
9308  *
9309  * DROP SUBSCRIPTION [ IF EXISTS ] name
9310  *
9311  *****************************************************************************/
9312 
9313 DropSubscriptionStmt: DROP SUBSCRIPTION name opt_drop_behavior
9314 				{
9315 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9316 					n->subname = $3;
9317 					n->missing_ok = false;
9318 					n->behavior = $4;
9319 					$$ = (Node *) n;
9320 				}
9321 				|  DROP SUBSCRIPTION IF_P EXISTS name opt_drop_behavior
9322 				{
9323 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9324 					n->subname = $5;
9325 					n->missing_ok = true;
9326 					n->behavior = $6;
9327 					$$ = (Node *) n;
9328 				}
9329 		;
9330 
9331 /*****************************************************************************
9332  *
9333  *		QUERY:	Define Rewrite Rule
9334  *
9335  *****************************************************************************/
9336 
9337 RuleStmt:	CREATE opt_or_replace RULE name AS
9338 			ON event TO qualified_name where_clause
9339 			DO opt_instead RuleActionList
9340 				{
9341 					RuleStmt *n = makeNode(RuleStmt);
9342 					n->replace = $2;
9343 					n->relation = $9;
9344 					n->rulename = $4;
9345 					n->whereClause = $10;
9346 					n->event = $7;
9347 					n->instead = $12;
9348 					n->actions = $13;
9349 					$$ = (Node *)n;
9350 				}
9351 		;
9352 
9353 RuleActionList:
9354 			NOTHING									{ $$ = NIL; }
9355 			| RuleActionStmt						{ $$ = list_make1($1); }
9356 			| '(' RuleActionMulti ')'				{ $$ = $2; }
9357 		;
9358 
9359 /* the thrashing around here is to discard "empty" statements... */
9360 RuleActionMulti:
9361 			RuleActionMulti ';' RuleActionStmtOrEmpty
9362 				{ if ($3 != NULL)
9363 					$$ = lappend($1, $3);
9364 				  else
9365 					$$ = $1;
9366 				}
9367 			| RuleActionStmtOrEmpty
9368 				{ if ($1 != NULL)
9369 					$$ = list_make1($1);
9370 				  else
9371 					$$ = NIL;
9372 				}
9373 		;
9374 
9375 RuleActionStmt:
9376 			SelectStmt
9377 			| InsertStmt
9378 			| UpdateStmt
9379 			| DeleteStmt
9380 			| NotifyStmt
9381 		;
9382 
9383 RuleActionStmtOrEmpty:
9384 			RuleActionStmt							{ $$ = $1; }
9385 			|	/*EMPTY*/							{ $$ = NULL; }
9386 		;
9387 
9388 event:		SELECT									{ $$ = CMD_SELECT; }
9389 			| UPDATE								{ $$ = CMD_UPDATE; }
9390 			| DELETE_P								{ $$ = CMD_DELETE; }
9391 			| INSERT								{ $$ = CMD_INSERT; }
9392 		 ;
9393 
9394 opt_instead:
9395 			INSTEAD									{ $$ = TRUE; }
9396 			| ALSO									{ $$ = FALSE; }
9397 			| /*EMPTY*/								{ $$ = FALSE; }
9398 		;
9399 
9400 
9401 /*****************************************************************************
9402  *
9403  *		QUERY:
9404  *				NOTIFY <identifier> can appear both in rule bodies and
9405  *				as a query-level command
9406  *
9407  *****************************************************************************/
9408 
9409 NotifyStmt: NOTIFY ColId notify_payload
9410 				{
9411 					NotifyStmt *n = makeNode(NotifyStmt);
9412 					n->conditionname = $2;
9413 					n->payload = $3;
9414 					$$ = (Node *)n;
9415 				}
9416 		;
9417 
9418 notify_payload:
9419 			',' Sconst							{ $$ = $2; }
9420 			| /*EMPTY*/							{ $$ = NULL; }
9421 		;
9422 
9423 ListenStmt: LISTEN ColId
9424 				{
9425 					ListenStmt *n = makeNode(ListenStmt);
9426 					n->conditionname = $2;
9427 					$$ = (Node *)n;
9428 				}
9429 		;
9430 
9431 UnlistenStmt:
9432 			UNLISTEN ColId
9433 				{
9434 					UnlistenStmt *n = makeNode(UnlistenStmt);
9435 					n->conditionname = $2;
9436 					$$ = (Node *)n;
9437 				}
9438 			| UNLISTEN '*'
9439 				{
9440 					UnlistenStmt *n = makeNode(UnlistenStmt);
9441 					n->conditionname = NULL;
9442 					$$ = (Node *)n;
9443 				}
9444 		;
9445 
9446 
9447 /*****************************************************************************
9448  *
9449  *		Transactions:
9450  *
9451  *		BEGIN / COMMIT / ROLLBACK
9452  *		(also older versions END / ABORT)
9453  *
9454  *****************************************************************************/
9455 
9456 TransactionStmt:
9457 			ABORT_P opt_transaction
9458 				{
9459 					TransactionStmt *n = makeNode(TransactionStmt);
9460 					n->kind = TRANS_STMT_ROLLBACK;
9461 					n->options = NIL;
9462 					$$ = (Node *)n;
9463 				}
9464 			| BEGIN_P opt_transaction transaction_mode_list_or_empty
9465 				{
9466 					TransactionStmt *n = makeNode(TransactionStmt);
9467 					n->kind = TRANS_STMT_BEGIN;
9468 					n->options = $3;
9469 					$$ = (Node *)n;
9470 				}
9471 			| START TRANSACTION transaction_mode_list_or_empty
9472 				{
9473 					TransactionStmt *n = makeNode(TransactionStmt);
9474 					n->kind = TRANS_STMT_START;
9475 					n->options = $3;
9476 					$$ = (Node *)n;
9477 				}
9478 			| COMMIT opt_transaction
9479 				{
9480 					TransactionStmt *n = makeNode(TransactionStmt);
9481 					n->kind = TRANS_STMT_COMMIT;
9482 					n->options = NIL;
9483 					$$ = (Node *)n;
9484 				}
9485 			| END_P opt_transaction
9486 				{
9487 					TransactionStmt *n = makeNode(TransactionStmt);
9488 					n->kind = TRANS_STMT_COMMIT;
9489 					n->options = NIL;
9490 					$$ = (Node *)n;
9491 				}
9492 			| ROLLBACK opt_transaction
9493 				{
9494 					TransactionStmt *n = makeNode(TransactionStmt);
9495 					n->kind = TRANS_STMT_ROLLBACK;
9496 					n->options = NIL;
9497 					$$ = (Node *)n;
9498 				}
9499 			| SAVEPOINT ColId
9500 				{
9501 					TransactionStmt *n = makeNode(TransactionStmt);
9502 					n->kind = TRANS_STMT_SAVEPOINT;
9503 					n->options = list_make1(makeDefElem("savepoint_name",
9504 														(Node *)makeString($2), @1));
9505 					$$ = (Node *)n;
9506 				}
9507 			| RELEASE SAVEPOINT ColId
9508 				{
9509 					TransactionStmt *n = makeNode(TransactionStmt);
9510 					n->kind = TRANS_STMT_RELEASE;
9511 					n->options = list_make1(makeDefElem("savepoint_name",
9512 														(Node *)makeString($3), @1));
9513 					$$ = (Node *)n;
9514 				}
9515 			| RELEASE ColId
9516 				{
9517 					TransactionStmt *n = makeNode(TransactionStmt);
9518 					n->kind = TRANS_STMT_RELEASE;
9519 					n->options = list_make1(makeDefElem("savepoint_name",
9520 														(Node *)makeString($2), @1));
9521 					$$ = (Node *)n;
9522 				}
9523 			| ROLLBACK opt_transaction TO SAVEPOINT ColId
9524 				{
9525 					TransactionStmt *n = makeNode(TransactionStmt);
9526 					n->kind = TRANS_STMT_ROLLBACK_TO;
9527 					n->options = list_make1(makeDefElem("savepoint_name",
9528 														(Node *)makeString($5), @1));
9529 					$$ = (Node *)n;
9530 				}
9531 			| ROLLBACK opt_transaction TO ColId
9532 				{
9533 					TransactionStmt *n = makeNode(TransactionStmt);
9534 					n->kind = TRANS_STMT_ROLLBACK_TO;
9535 					n->options = list_make1(makeDefElem("savepoint_name",
9536 														(Node *)makeString($4), @1));
9537 					$$ = (Node *)n;
9538 				}
9539 			| PREPARE TRANSACTION Sconst
9540 				{
9541 					TransactionStmt *n = makeNode(TransactionStmt);
9542 					n->kind = TRANS_STMT_PREPARE;
9543 					n->gid = $3;
9544 					$$ = (Node *)n;
9545 				}
9546 			| COMMIT PREPARED Sconst
9547 				{
9548 					TransactionStmt *n = makeNode(TransactionStmt);
9549 					n->kind = TRANS_STMT_COMMIT_PREPARED;
9550 					n->gid = $3;
9551 					$$ = (Node *)n;
9552 				}
9553 			| ROLLBACK PREPARED Sconst
9554 				{
9555 					TransactionStmt *n = makeNode(TransactionStmt);
9556 					n->kind = TRANS_STMT_ROLLBACK_PREPARED;
9557 					n->gid = $3;
9558 					$$ = (Node *)n;
9559 				}
9560 		;
9561 
9562 opt_transaction:	WORK							{}
9563 			| TRANSACTION							{}
9564 			| /*EMPTY*/								{}
9565 		;
9566 
9567 transaction_mode_item:
9568 			ISOLATION LEVEL iso_level
9569 					{ $$ = makeDefElem("transaction_isolation",
9570 									   makeStringConst($3, @3), @1); }
9571 			| READ ONLY
9572 					{ $$ = makeDefElem("transaction_read_only",
9573 									   makeIntConst(TRUE, @1), @1); }
9574 			| READ WRITE
9575 					{ $$ = makeDefElem("transaction_read_only",
9576 									   makeIntConst(FALSE, @1), @1); }
9577 			| DEFERRABLE
9578 					{ $$ = makeDefElem("transaction_deferrable",
9579 									   makeIntConst(TRUE, @1), @1); }
9580 			| NOT DEFERRABLE
9581 					{ $$ = makeDefElem("transaction_deferrable",
9582 									   makeIntConst(FALSE, @1), @1); }
9583 		;
9584 
9585 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
9586 transaction_mode_list:
9587 			transaction_mode_item
9588 					{ $$ = list_make1($1); }
9589 			| transaction_mode_list ',' transaction_mode_item
9590 					{ $$ = lappend($1, $3); }
9591 			| transaction_mode_list transaction_mode_item
9592 					{ $$ = lappend($1, $2); }
9593 		;
9594 
9595 transaction_mode_list_or_empty:
9596 			transaction_mode_list
9597 			| /* EMPTY */
9598 					{ $$ = NIL; }
9599 		;
9600 
9601 
9602 /*****************************************************************************
9603  *
9604  *	QUERY:
9605  *		CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
9606  *			AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
9607  *
9608  *****************************************************************************/
9609 
9610 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list opt_reloptions
9611 				AS SelectStmt opt_check_option
9612 				{
9613 					ViewStmt *n = makeNode(ViewStmt);
9614 					n->view = $4;
9615 					n->view->relpersistence = $2;
9616 					n->aliases = $5;
9617 					n->query = $8;
9618 					n->replace = false;
9619 					n->options = $6;
9620 					n->withCheckOption = $9;
9621 					$$ = (Node *) n;
9622 				}
9623 		| CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list opt_reloptions
9624 				AS SelectStmt opt_check_option
9625 				{
9626 					ViewStmt *n = makeNode(ViewStmt);
9627 					n->view = $6;
9628 					n->view->relpersistence = $4;
9629 					n->aliases = $7;
9630 					n->query = $10;
9631 					n->replace = true;
9632 					n->options = $8;
9633 					n->withCheckOption = $11;
9634 					$$ = (Node *) n;
9635 				}
9636 		| CREATE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
9637 				AS SelectStmt opt_check_option
9638 				{
9639 					ViewStmt *n = makeNode(ViewStmt);
9640 					n->view = $5;
9641 					n->view->relpersistence = $2;
9642 					n->aliases = $7;
9643 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $11);
9644 					n->replace = false;
9645 					n->options = $9;
9646 					n->withCheckOption = $12;
9647 					if (n->withCheckOption != NO_CHECK_OPTION)
9648 						ereport(ERROR,
9649 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9650 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
9651 								 parser_errposition(@12)));
9652 					$$ = (Node *) n;
9653 				}
9654 		| CREATE OR REPLACE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
9655 				AS SelectStmt opt_check_option
9656 				{
9657 					ViewStmt *n = makeNode(ViewStmt);
9658 					n->view = $7;
9659 					n->view->relpersistence = $4;
9660 					n->aliases = $9;
9661 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $13);
9662 					n->replace = true;
9663 					n->options = $11;
9664 					n->withCheckOption = $14;
9665 					if (n->withCheckOption != NO_CHECK_OPTION)
9666 						ereport(ERROR,
9667 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9668 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
9669 								 parser_errposition(@14)));
9670 					$$ = (Node *) n;
9671 				}
9672 		;
9673 
9674 opt_check_option:
9675 		WITH CHECK OPTION				{ $$ = CASCADED_CHECK_OPTION; }
9676 		| WITH CASCADED CHECK OPTION	{ $$ = CASCADED_CHECK_OPTION; }
9677 		| WITH LOCAL CHECK OPTION		{ $$ = LOCAL_CHECK_OPTION; }
9678 		| /* EMPTY */					{ $$ = NO_CHECK_OPTION; }
9679 		;
9680 
9681 /*****************************************************************************
9682  *
9683  *		QUERY:
9684  *				LOAD "filename"
9685  *
9686  *****************************************************************************/
9687 
9688 LoadStmt:	LOAD file_name
9689 				{
9690 					LoadStmt *n = makeNode(LoadStmt);
9691 					n->filename = $2;
9692 					$$ = (Node *)n;
9693 				}
9694 		;
9695 
9696 
9697 /*****************************************************************************
9698  *
9699  *		CREATE DATABASE
9700  *
9701  *****************************************************************************/
9702 
9703 CreatedbStmt:
9704 			CREATE DATABASE database_name opt_with createdb_opt_list
9705 				{
9706 					CreatedbStmt *n = makeNode(CreatedbStmt);
9707 					n->dbname = $3;
9708 					n->options = $5;
9709 					$$ = (Node *)n;
9710 				}
9711 		;
9712 
9713 createdb_opt_list:
9714 			createdb_opt_items						{ $$ = $1; }
9715 			| /* EMPTY */							{ $$ = NIL; }
9716 		;
9717 
9718 createdb_opt_items:
9719 			createdb_opt_item						{ $$ = list_make1($1); }
9720 			| createdb_opt_items createdb_opt_item	{ $$ = lappend($1, $2); }
9721 		;
9722 
9723 createdb_opt_item:
9724 			createdb_opt_name opt_equal SignedIconst
9725 				{
9726 					$$ = makeDefElem($1, (Node *)makeInteger($3), @1);
9727 				}
9728 			| createdb_opt_name opt_equal opt_boolean_or_string
9729 				{
9730 					$$ = makeDefElem($1, (Node *)makeString($3), @1);
9731 				}
9732 			| createdb_opt_name opt_equal DEFAULT
9733 				{
9734 					$$ = makeDefElem($1, NULL, @1);
9735 				}
9736 		;
9737 
9738 /*
9739  * Ideally we'd use ColId here, but that causes shift/reduce conflicts against
9740  * the ALTER DATABASE SET/RESET syntaxes.  Instead call out specific keywords
9741  * we need, and allow IDENT so that database option names don't have to be
9742  * parser keywords unless they are already keywords for other reasons.
9743  *
9744  * XXX this coding technique is fragile since if someone makes a formerly
9745  * non-keyword option name into a keyword and forgets to add it here, the
9746  * option will silently break.  Best defense is to provide a regression test
9747  * exercising every such option, at least at the syntax level.
9748  */
9749 createdb_opt_name:
9750 			IDENT							{ $$ = $1; }
9751 			| CONNECTION LIMIT				{ $$ = pstrdup("connection_limit"); }
9752 			| ENCODING						{ $$ = pstrdup($1); }
9753 			| LOCATION						{ $$ = pstrdup($1); }
9754 			| OWNER							{ $$ = pstrdup($1); }
9755 			| TABLESPACE					{ $$ = pstrdup($1); }
9756 			| TEMPLATE						{ $$ = pstrdup($1); }
9757 		;
9758 
9759 /*
9760  *	Though the equals sign doesn't match other WITH options, pg_dump uses
9761  *	equals for backward compatibility, and it doesn't seem worth removing it.
9762  */
9763 opt_equal:	'='										{}
9764 			| /*EMPTY*/								{}
9765 		;
9766 
9767 
9768 /*****************************************************************************
9769  *
9770  *		ALTER DATABASE
9771  *
9772  *****************************************************************************/
9773 
9774 AlterDatabaseStmt:
9775 			ALTER DATABASE database_name WITH createdb_opt_list
9776 				 {
9777 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9778 					n->dbname = $3;
9779 					n->options = $5;
9780 					$$ = (Node *)n;
9781 				 }
9782 			| ALTER DATABASE database_name createdb_opt_list
9783 				 {
9784 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9785 					n->dbname = $3;
9786 					n->options = $4;
9787 					$$ = (Node *)n;
9788 				 }
9789 			| ALTER DATABASE database_name SET TABLESPACE name
9790 				 {
9791 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9792 					n->dbname = $3;
9793 					n->options = list_make1(makeDefElem("tablespace",
9794 														(Node *)makeString($6), @6));
9795 					$$ = (Node *)n;
9796 				 }
9797 		;
9798 
9799 AlterDatabaseSetStmt:
9800 			ALTER DATABASE database_name SetResetClause
9801 				{
9802 					AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
9803 					n->dbname = $3;
9804 					n->setstmt = $4;
9805 					$$ = (Node *)n;
9806 				}
9807 		;
9808 
9809 
9810 /*****************************************************************************
9811  *
9812  *		DROP DATABASE [ IF EXISTS ]
9813  *
9814  * This is implicitly CASCADE, no need for drop behavior
9815  *****************************************************************************/
9816 
9817 DropdbStmt: DROP DATABASE database_name
9818 				{
9819 					DropdbStmt *n = makeNode(DropdbStmt);
9820 					n->dbname = $3;
9821 					n->missing_ok = FALSE;
9822 					$$ = (Node *)n;
9823 				}
9824 			| DROP DATABASE IF_P EXISTS database_name
9825 				{
9826 					DropdbStmt *n = makeNode(DropdbStmt);
9827 					n->dbname = $5;
9828 					n->missing_ok = TRUE;
9829 					$$ = (Node *)n;
9830 				}
9831 		;
9832 
9833 
9834 /*****************************************************************************
9835  *
9836  *		ALTER COLLATION
9837  *
9838  *****************************************************************************/
9839 
9840 AlterCollationStmt: ALTER COLLATION any_name REFRESH VERSION_P
9841 				{
9842 					AlterCollationStmt *n = makeNode(AlterCollationStmt);
9843 					n->collname = $3;
9844 					$$ = (Node *)n;
9845 				}
9846 		;
9847 
9848 
9849 /*****************************************************************************
9850  *
9851  *		ALTER SYSTEM
9852  *
9853  * This is used to change configuration parameters persistently.
9854  *****************************************************************************/
9855 
9856 AlterSystemStmt:
9857 			ALTER SYSTEM_P SET generic_set
9858 				{
9859 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
9860 					n->setstmt = $4;
9861 					$$ = (Node *)n;
9862 				}
9863 			| ALTER SYSTEM_P RESET generic_reset
9864 				{
9865 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
9866 					n->setstmt = $4;
9867 					$$ = (Node *)n;
9868 				}
9869 		;
9870 
9871 
9872 /*****************************************************************************
9873  *
9874  * Manipulate a domain
9875  *
9876  *****************************************************************************/
9877 
9878 CreateDomainStmt:
9879 			CREATE DOMAIN_P any_name opt_as Typename ColQualList
9880 				{
9881 					CreateDomainStmt *n = makeNode(CreateDomainStmt);
9882 					n->domainname = $3;
9883 					n->typeName = $5;
9884 					SplitColQualList($6, &n->constraints, &n->collClause,
9885 									 yyscanner);
9886 					$$ = (Node *)n;
9887 				}
9888 		;
9889 
9890 AlterDomainStmt:
9891 			/* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
9892 			ALTER DOMAIN_P any_name alter_column_default
9893 				{
9894 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9895 					n->subtype = 'T';
9896 					n->typeName = $3;
9897 					n->def = $4;
9898 					$$ = (Node *)n;
9899 				}
9900 			/* ALTER DOMAIN <domain> DROP NOT NULL */
9901 			| ALTER DOMAIN_P any_name DROP NOT NULL_P
9902 				{
9903 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9904 					n->subtype = 'N';
9905 					n->typeName = $3;
9906 					$$ = (Node *)n;
9907 				}
9908 			/* ALTER DOMAIN <domain> SET NOT NULL */
9909 			| ALTER DOMAIN_P any_name SET NOT NULL_P
9910 				{
9911 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9912 					n->subtype = 'O';
9913 					n->typeName = $3;
9914 					$$ = (Node *)n;
9915 				}
9916 			/* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
9917 			| ALTER DOMAIN_P any_name ADD_P TableConstraint
9918 				{
9919 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9920 					n->subtype = 'C';
9921 					n->typeName = $3;
9922 					n->def = $5;
9923 					$$ = (Node *)n;
9924 				}
9925 			/* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
9926 			| ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
9927 				{
9928 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9929 					n->subtype = 'X';
9930 					n->typeName = $3;
9931 					n->name = $6;
9932 					n->behavior = $7;
9933 					n->missing_ok = false;
9934 					$$ = (Node *)n;
9935 				}
9936 			/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
9937 			| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
9938 				{
9939 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9940 					n->subtype = 'X';
9941 					n->typeName = $3;
9942 					n->name = $8;
9943 					n->behavior = $9;
9944 					n->missing_ok = true;
9945 					$$ = (Node *)n;
9946 				}
9947 			/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
9948 			| ALTER DOMAIN_P any_name VALIDATE CONSTRAINT name
9949 				{
9950 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9951 					n->subtype = 'V';
9952 					n->typeName = $3;
9953 					n->name = $6;
9954 					$$ = (Node *)n;
9955 				}
9956 			;
9957 
9958 opt_as:		AS										{}
9959 			| /* EMPTY */							{}
9960 		;
9961 
9962 
9963 /*****************************************************************************
9964  *
9965  * Manipulate a text search dictionary or configuration
9966  *
9967  *****************************************************************************/
9968 
9969 AlterTSDictionaryStmt:
9970 			ALTER TEXT_P SEARCH DICTIONARY any_name definition
9971 				{
9972 					AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
9973 					n->dictname = $5;
9974 					n->options = $6;
9975 					$$ = (Node *)n;
9976 				}
9977 		;
9978 
9979 AlterTSConfigurationStmt:
9980 			ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list any_with any_name_list
9981 				{
9982 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9983 					n->kind = ALTER_TSCONFIG_ADD_MAPPING;
9984 					n->cfgname = $5;
9985 					n->tokentype = $9;
9986 					n->dicts = $11;
9987 					n->override = false;
9988 					n->replace = false;
9989 					$$ = (Node*)n;
9990 				}
9991 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list any_with any_name_list
9992 				{
9993 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9994 					n->kind = ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN;
9995 					n->cfgname = $5;
9996 					n->tokentype = $9;
9997 					n->dicts = $11;
9998 					n->override = true;
9999 					n->replace = false;
10000 					$$ = (Node*)n;
10001 				}
10002 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name any_with any_name
10003 				{
10004 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10005 					n->kind = ALTER_TSCONFIG_REPLACE_DICT;
10006 					n->cfgname = $5;
10007 					n->tokentype = NIL;
10008 					n->dicts = list_make2($9,$11);
10009 					n->override = false;
10010 					n->replace = true;
10011 					$$ = (Node*)n;
10012 				}
10013 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name any_with any_name
10014 				{
10015 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10016 					n->kind = ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN;
10017 					n->cfgname = $5;
10018 					n->tokentype = $9;
10019 					n->dicts = list_make2($11,$13);
10020 					n->override = false;
10021 					n->replace = true;
10022 					$$ = (Node*)n;
10023 				}
10024 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
10025 				{
10026 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10027 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10028 					n->cfgname = $5;
10029 					n->tokentype = $9;
10030 					n->missing_ok = false;
10031 					$$ = (Node*)n;
10032 				}
10033 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
10034 				{
10035 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10036 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10037 					n->cfgname = $5;
10038 					n->tokentype = $11;
10039 					n->missing_ok = true;
10040 					$$ = (Node*)n;
10041 				}
10042 		;
10043 
10044 /* Use this if TIME or ORDINALITY after WITH should be taken as an identifier */
10045 any_with:	WITH									{}
10046 			| WITH_LA								{}
10047 		;
10048 
10049 
10050 /*****************************************************************************
10051  *
10052  * Manipulate a conversion
10053  *
10054  *		CREATE [DEFAULT] CONVERSION <conversion_name>
10055  *		FOR <encoding_name> TO <encoding_name> FROM <func_name>
10056  *
10057  *****************************************************************************/
10058 
10059 CreateConversionStmt:
10060 			CREATE opt_default CONVERSION_P any_name FOR Sconst
10061 			TO Sconst FROM any_name
10062 			{
10063 				CreateConversionStmt *n = makeNode(CreateConversionStmt);
10064 				n->conversion_name = $4;
10065 				n->for_encoding_name = $6;
10066 				n->to_encoding_name = $8;
10067 				n->func_name = $10;
10068 				n->def = $2;
10069 				$$ = (Node *)n;
10070 			}
10071 		;
10072 
10073 /*****************************************************************************
10074  *
10075  *		QUERY:
10076  *				CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
10077  *				CLUSTER [VERBOSE]
10078  *				CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
10079  *
10080  *****************************************************************************/
10081 
10082 ClusterStmt:
10083 			CLUSTER opt_verbose qualified_name cluster_index_specification
10084 				{
10085 					ClusterStmt *n = makeNode(ClusterStmt);
10086 					n->relation = $3;
10087 					n->indexname = $4;
10088 					n->verbose = $2;
10089 					$$ = (Node*)n;
10090 				}
10091 			| CLUSTER opt_verbose
10092 				{
10093 					ClusterStmt *n = makeNode(ClusterStmt);
10094 					n->relation = NULL;
10095 					n->indexname = NULL;
10096 					n->verbose = $2;
10097 					$$ = (Node*)n;
10098 				}
10099 			/* kept for pre-8.3 compatibility */
10100 			| CLUSTER opt_verbose index_name ON qualified_name
10101 				{
10102 					ClusterStmt *n = makeNode(ClusterStmt);
10103 					n->relation = $5;
10104 					n->indexname = $3;
10105 					n->verbose = $2;
10106 					$$ = (Node*)n;
10107 				}
10108 		;
10109 
10110 cluster_index_specification:
10111 			USING index_name		{ $$ = $2; }
10112 			| /*EMPTY*/				{ $$ = NULL; }
10113 		;
10114 
10115 
10116 /*****************************************************************************
10117  *
10118  *		QUERY:
10119  *				VACUUM
10120  *				ANALYZE
10121  *
10122  *****************************************************************************/
10123 
10124 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
10125 				{
10126 					VacuumStmt *n = makeNode(VacuumStmt);
10127 					n->options = VACOPT_VACUUM;
10128 					if ($2)
10129 						n->options |= VACOPT_FULL;
10130 					if ($3)
10131 						n->options |= VACOPT_FREEZE;
10132 					if ($4)
10133 						n->options |= VACOPT_VERBOSE;
10134 					n->relation = NULL;
10135 					n->va_cols = NIL;
10136 					$$ = (Node *)n;
10137 				}
10138 			| VACUUM opt_full opt_freeze opt_verbose qualified_name
10139 				{
10140 					VacuumStmt *n = makeNode(VacuumStmt);
10141 					n->options = VACOPT_VACUUM;
10142 					if ($2)
10143 						n->options |= VACOPT_FULL;
10144 					if ($3)
10145 						n->options |= VACOPT_FREEZE;
10146 					if ($4)
10147 						n->options |= VACOPT_VERBOSE;
10148 					n->relation = $5;
10149 					n->va_cols = NIL;
10150 					$$ = (Node *)n;
10151 				}
10152 			| VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
10153 				{
10154 					VacuumStmt *n = (VacuumStmt *) $5;
10155 					n->options |= VACOPT_VACUUM;
10156 					if ($2)
10157 						n->options |= VACOPT_FULL;
10158 					if ($3)
10159 						n->options |= VACOPT_FREEZE;
10160 					if ($4)
10161 						n->options |= VACOPT_VERBOSE;
10162 					$$ = (Node *)n;
10163 				}
10164 			| VACUUM '(' vacuum_option_list ')'
10165 				{
10166 					VacuumStmt *n = makeNode(VacuumStmt);
10167 					n->options = VACOPT_VACUUM | $3;
10168 					n->relation = NULL;
10169 					n->va_cols = NIL;
10170 					$$ = (Node *) n;
10171 				}
10172 			| VACUUM '(' vacuum_option_list ')' qualified_name opt_name_list
10173 				{
10174 					VacuumStmt *n = makeNode(VacuumStmt);
10175 					n->options = VACOPT_VACUUM | $3;
10176 					n->relation = $5;
10177 					n->va_cols = $6;
10178 					if (n->va_cols != NIL)	/* implies analyze */
10179 						n->options |= VACOPT_ANALYZE;
10180 					$$ = (Node *) n;
10181 				}
10182 		;
10183 
10184 vacuum_option_list:
10185 			vacuum_option_elem								{ $$ = $1; }
10186 			| vacuum_option_list ',' vacuum_option_elem		{ $$ = $1 | $3; }
10187 		;
10188 
10189 vacuum_option_elem:
10190 			analyze_keyword		{ $$ = VACOPT_ANALYZE; }
10191 			| VERBOSE			{ $$ = VACOPT_VERBOSE; }
10192 			| FREEZE			{ $$ = VACOPT_FREEZE; }
10193 			| FULL				{ $$ = VACOPT_FULL; }
10194 			| IDENT
10195 				{
10196 					if (strcmp($1, "disable_page_skipping") == 0)
10197 						$$ = VACOPT_DISABLE_PAGE_SKIPPING;
10198 					else
10199 						ereport(ERROR,
10200 								(errcode(ERRCODE_SYNTAX_ERROR),
10201 							 errmsg("unrecognized VACUUM option \"%s\"", $1),
10202 									 parser_errposition(@1)));
10203 				}
10204 		;
10205 
10206 AnalyzeStmt:
10207 			analyze_keyword opt_verbose
10208 				{
10209 					VacuumStmt *n = makeNode(VacuumStmt);
10210 					n->options = VACOPT_ANALYZE;
10211 					if ($2)
10212 						n->options |= VACOPT_VERBOSE;
10213 					n->relation = NULL;
10214 					n->va_cols = NIL;
10215 					$$ = (Node *)n;
10216 				}
10217 			| analyze_keyword opt_verbose qualified_name opt_name_list
10218 				{
10219 					VacuumStmt *n = makeNode(VacuumStmt);
10220 					n->options = VACOPT_ANALYZE;
10221 					if ($2)
10222 						n->options |= VACOPT_VERBOSE;
10223 					n->relation = $3;
10224 					n->va_cols = $4;
10225 					$$ = (Node *)n;
10226 				}
10227 		;
10228 
10229 analyze_keyword:
10230 			ANALYZE									{}
10231 			| ANALYSE /* British */					{}
10232 		;
10233 
10234 opt_verbose:
10235 			VERBOSE									{ $$ = TRUE; }
10236 			| /*EMPTY*/								{ $$ = FALSE; }
10237 		;
10238 
10239 opt_full:	FULL									{ $$ = TRUE; }
10240 			| /*EMPTY*/								{ $$ = FALSE; }
10241 		;
10242 
10243 opt_freeze: FREEZE									{ $$ = TRUE; }
10244 			| /*EMPTY*/								{ $$ = FALSE; }
10245 		;
10246 
10247 opt_name_list:
10248 			'(' name_list ')'						{ $$ = $2; }
10249 			| /*EMPTY*/								{ $$ = NIL; }
10250 		;
10251 
10252 
10253 /*****************************************************************************
10254  *
10255  *		QUERY:
10256  *				EXPLAIN [ANALYZE] [VERBOSE] query
10257  *				EXPLAIN ( options ) query
10258  *
10259  *****************************************************************************/
10260 
10261 ExplainStmt:
10262 		EXPLAIN ExplainableStmt
10263 				{
10264 					ExplainStmt *n = makeNode(ExplainStmt);
10265 					n->query = $2;
10266 					n->options = NIL;
10267 					$$ = (Node *) n;
10268 				}
10269 		| EXPLAIN analyze_keyword opt_verbose ExplainableStmt
10270 				{
10271 					ExplainStmt *n = makeNode(ExplainStmt);
10272 					n->query = $4;
10273 					n->options = list_make1(makeDefElem("analyze", NULL, @2));
10274 					if ($3)
10275 						n->options = lappend(n->options,
10276 											 makeDefElem("verbose", NULL, @3));
10277 					$$ = (Node *) n;
10278 				}
10279 		| EXPLAIN VERBOSE ExplainableStmt
10280 				{
10281 					ExplainStmt *n = makeNode(ExplainStmt);
10282 					n->query = $3;
10283 					n->options = list_make1(makeDefElem("verbose", NULL, @2));
10284 					$$ = (Node *) n;
10285 				}
10286 		| EXPLAIN '(' explain_option_list ')' ExplainableStmt
10287 				{
10288 					ExplainStmt *n = makeNode(ExplainStmt);
10289 					n->query = $5;
10290 					n->options = $3;
10291 					$$ = (Node *) n;
10292 				}
10293 		;
10294 
10295 ExplainableStmt:
10296 			SelectStmt
10297 			| InsertStmt
10298 			| UpdateStmt
10299 			| DeleteStmt
10300 			| DeclareCursorStmt
10301 			| CreateAsStmt
10302 			| CreateMatViewStmt
10303 			| RefreshMatViewStmt
10304 			| ExecuteStmt					/* by default all are $$=$1 */
10305 		;
10306 
10307 explain_option_list:
10308 			explain_option_elem
10309 				{
10310 					$$ = list_make1($1);
10311 				}
10312 			| explain_option_list ',' explain_option_elem
10313 				{
10314 					$$ = lappend($1, $3);
10315 				}
10316 		;
10317 
10318 explain_option_elem:
10319 			explain_option_name explain_option_arg
10320 				{
10321 					$$ = makeDefElem($1, $2, @1);
10322 				}
10323 		;
10324 
10325 explain_option_name:
10326 			NonReservedWord			{ $$ = $1; }
10327 			| analyze_keyword		{ $$ = "analyze"; }
10328 		;
10329 
10330 explain_option_arg:
10331 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
10332 			| NumericOnly			{ $$ = (Node *) $1; }
10333 			| /* EMPTY */			{ $$ = NULL; }
10334 		;
10335 
10336 /*****************************************************************************
10337  *
10338  *		QUERY:
10339  *				PREPARE <plan_name> [(args, ...)] AS <query>
10340  *
10341  *****************************************************************************/
10342 
10343 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
10344 				{
10345 					PrepareStmt *n = makeNode(PrepareStmt);
10346 					n->name = $2;
10347 					n->argtypes = $3;
10348 					n->query = $5;
10349 					$$ = (Node *) n;
10350 				}
10351 		;
10352 
10353 prep_type_clause: '(' type_list ')'			{ $$ = $2; }
10354 				| /* EMPTY */				{ $$ = NIL; }
10355 		;
10356 
10357 PreparableStmt:
10358 			SelectStmt
10359 			| InsertStmt
10360 			| UpdateStmt
10361 			| DeleteStmt					/* by default all are $$=$1 */
10362 		;
10363 
10364 /*****************************************************************************
10365  *
10366  * EXECUTE <plan_name> [(params, ...)]
10367  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
10368  *
10369  *****************************************************************************/
10370 
10371 ExecuteStmt: EXECUTE name execute_param_clause
10372 				{
10373 					ExecuteStmt *n = makeNode(ExecuteStmt);
10374 					n->name = $2;
10375 					n->params = $3;
10376 					$$ = (Node *) n;
10377 				}
10378 			| CREATE OptTemp TABLE create_as_target AS
10379 				EXECUTE name execute_param_clause opt_with_data
10380 				{
10381 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10382 					ExecuteStmt *n = makeNode(ExecuteStmt);
10383 					n->name = $7;
10384 					n->params = $8;
10385 					ctas->query = (Node *) n;
10386 					ctas->into = $4;
10387 					ctas->relkind = OBJECT_TABLE;
10388 					ctas->is_select_into = false;
10389 					ctas->if_not_exists = false;
10390 					/* cram additional flags into the IntoClause */
10391 					$4->rel->relpersistence = $2;
10392 					$4->skipData = !($9);
10393 					$$ = (Node *) ctas;
10394 				}
10395 			| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS
10396 				EXECUTE name execute_param_clause opt_with_data
10397 				{
10398 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10399 					ExecuteStmt *n = makeNode(ExecuteStmt);
10400 					n->name = $10;
10401 					n->params = $11;
10402 					ctas->query = (Node *) n;
10403 					ctas->into = $7;
10404 					ctas->relkind = OBJECT_TABLE;
10405 					ctas->is_select_into = false;
10406 					ctas->if_not_exists = true;
10407 					/* cram additional flags into the IntoClause */
10408 					$7->rel->relpersistence = $2;
10409 					$7->skipData = !($12);
10410 					$$ = (Node *) ctas;
10411 				}
10412 		;
10413 
10414 execute_param_clause: '(' expr_list ')'				{ $$ = $2; }
10415 					| /* EMPTY */					{ $$ = NIL; }
10416 					;
10417 
10418 /*****************************************************************************
10419  *
10420  *		QUERY:
10421  *				DEALLOCATE [PREPARE] <plan_name>
10422  *
10423  *****************************************************************************/
10424 
10425 DeallocateStmt: DEALLOCATE name
10426 					{
10427 						DeallocateStmt *n = makeNode(DeallocateStmt);
10428 						n->name = $2;
10429 						$$ = (Node *) n;
10430 					}
10431 				| DEALLOCATE PREPARE name
10432 					{
10433 						DeallocateStmt *n = makeNode(DeallocateStmt);
10434 						n->name = $3;
10435 						$$ = (Node *) n;
10436 					}
10437 				| DEALLOCATE ALL
10438 					{
10439 						DeallocateStmt *n = makeNode(DeallocateStmt);
10440 						n->name = NULL;
10441 						$$ = (Node *) n;
10442 					}
10443 				| DEALLOCATE PREPARE ALL
10444 					{
10445 						DeallocateStmt *n = makeNode(DeallocateStmt);
10446 						n->name = NULL;
10447 						$$ = (Node *) n;
10448 					}
10449 		;
10450 
10451 /*****************************************************************************
10452  *
10453  *		QUERY:
10454  *				INSERT STATEMENTS
10455  *
10456  *****************************************************************************/
10457 
10458 InsertStmt:
10459 			opt_with_clause INSERT INTO insert_target insert_rest
10460 			opt_on_conflict returning_clause
10461 				{
10462 					$5->relation = $4;
10463 					$5->onConflictClause = $6;
10464 					$5->returningList = $7;
10465 					$5->withClause = $1;
10466 					$$ = (Node *) $5;
10467 				}
10468 		;
10469 
10470 /*
10471  * Can't easily make AS optional here, because VALUES in insert_rest would
10472  * have a shift/reduce conflict with VALUES as an optional alias.  We could
10473  * easily allow unreserved_keywords as optional aliases, but that'd be an odd
10474  * divergence from other places.  So just require AS for now.
10475  */
10476 insert_target:
10477 			qualified_name
10478 				{
10479 					$$ = $1;
10480 				}
10481 			| qualified_name AS ColId
10482 				{
10483 					$1->alias = makeAlias($3, NIL);
10484 					$$ = $1;
10485 				}
10486 		;
10487 
10488 insert_rest:
10489 			SelectStmt
10490 				{
10491 					$$ = makeNode(InsertStmt);
10492 					$$->cols = NIL;
10493 					$$->selectStmt = $1;
10494 				}
10495 			| OVERRIDING override_kind VALUE_P SelectStmt
10496 				{
10497 					$$ = makeNode(InsertStmt);
10498 					$$->cols = NIL;
10499 					$$->override = $2;
10500 					$$->selectStmt = $4;
10501 				}
10502 			| '(' insert_column_list ')' SelectStmt
10503 				{
10504 					$$ = makeNode(InsertStmt);
10505 					$$->cols = $2;
10506 					$$->selectStmt = $4;
10507 				}
10508 			| '(' insert_column_list ')' OVERRIDING override_kind VALUE_P SelectStmt
10509 				{
10510 					$$ = makeNode(InsertStmt);
10511 					$$->cols = $2;
10512 					$$->override = $5;
10513 					$$->selectStmt = $7;
10514 				}
10515 			| DEFAULT VALUES
10516 				{
10517 					$$ = makeNode(InsertStmt);
10518 					$$->cols = NIL;
10519 					$$->selectStmt = NULL;
10520 				}
10521 		;
10522 
10523 override_kind:
10524 			USER		{ $$ = OVERRIDING_USER_VALUE; }
10525 			| SYSTEM_P	{ $$ = OVERRIDING_SYSTEM_VALUE; }
10526 		;
10527 
10528 insert_column_list:
10529 			insert_column_item
10530 					{ $$ = list_make1($1); }
10531 			| insert_column_list ',' insert_column_item
10532 					{ $$ = lappend($1, $3); }
10533 		;
10534 
10535 insert_column_item:
10536 			ColId opt_indirection
10537 				{
10538 					$$ = makeNode(ResTarget);
10539 					$$->name = $1;
10540 					$$->indirection = check_indirection($2, yyscanner);
10541 					$$->val = NULL;
10542 					$$->location = @1;
10543 				}
10544 		;
10545 
10546 opt_on_conflict:
10547 			ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list	where_clause
10548 				{
10549 					$$ = makeNode(OnConflictClause);
10550 					$$->action = ONCONFLICT_UPDATE;
10551 					$$->infer = $3;
10552 					$$->targetList = $7;
10553 					$$->whereClause = $8;
10554 					$$->location = @1;
10555 				}
10556 			|
10557 			ON CONFLICT opt_conf_expr DO NOTHING
10558 				{
10559 					$$ = makeNode(OnConflictClause);
10560 					$$->action = ONCONFLICT_NOTHING;
10561 					$$->infer = $3;
10562 					$$->targetList = NIL;
10563 					$$->whereClause = NULL;
10564 					$$->location = @1;
10565 				}
10566 			| /*EMPTY*/
10567 				{
10568 					$$ = NULL;
10569 				}
10570 		;
10571 
10572 opt_conf_expr:
10573 			'(' index_params ')' where_clause
10574 				{
10575 					$$ = makeNode(InferClause);
10576 					$$->indexElems = $2;
10577 					$$->whereClause = $4;
10578 					$$->conname = NULL;
10579 					$$->location = @1;
10580 				}
10581 			|
10582 			ON CONSTRAINT name
10583 				{
10584 					$$ = makeNode(InferClause);
10585 					$$->indexElems = NIL;
10586 					$$->whereClause = NULL;
10587 					$$->conname = $3;
10588 					$$->location = @1;
10589 				}
10590 			| /*EMPTY*/
10591 				{
10592 					$$ = NULL;
10593 				}
10594 		;
10595 
10596 returning_clause:
10597 			RETURNING target_list		{ $$ = $2; }
10598 			| /* EMPTY */				{ $$ = NIL; }
10599 		;
10600 
10601 
10602 /*****************************************************************************
10603  *
10604  *		QUERY:
10605  *				DELETE STATEMENTS
10606  *
10607  *****************************************************************************/
10608 
10609 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
10610 			using_clause where_or_current_clause returning_clause
10611 				{
10612 					DeleteStmt *n = makeNode(DeleteStmt);
10613 					n->relation = $4;
10614 					n->usingClause = $5;
10615 					n->whereClause = $6;
10616 					n->returningList = $7;
10617 					n->withClause = $1;
10618 					$$ = (Node *)n;
10619 				}
10620 		;
10621 
10622 using_clause:
10623 				USING from_list						{ $$ = $2; }
10624 			| /*EMPTY*/								{ $$ = NIL; }
10625 		;
10626 
10627 
10628 /*****************************************************************************
10629  *
10630  *		QUERY:
10631  *				LOCK TABLE
10632  *
10633  *****************************************************************************/
10634 
10635 LockStmt:	LOCK_P opt_table relation_expr_list opt_lock opt_nowait
10636 				{
10637 					LockStmt *n = makeNode(LockStmt);
10638 
10639 					n->relations = $3;
10640 					n->mode = $4;
10641 					n->nowait = $5;
10642 					$$ = (Node *)n;
10643 				}
10644 		;
10645 
10646 opt_lock:	IN_P lock_type MODE				{ $$ = $2; }
10647 			| /*EMPTY*/						{ $$ = AccessExclusiveLock; }
10648 		;
10649 
10650 lock_type:	ACCESS SHARE					{ $$ = AccessShareLock; }
10651 			| ROW SHARE						{ $$ = RowShareLock; }
10652 			| ROW EXCLUSIVE					{ $$ = RowExclusiveLock; }
10653 			| SHARE UPDATE EXCLUSIVE		{ $$ = ShareUpdateExclusiveLock; }
10654 			| SHARE							{ $$ = ShareLock; }
10655 			| SHARE ROW EXCLUSIVE			{ $$ = ShareRowExclusiveLock; }
10656 			| EXCLUSIVE						{ $$ = ExclusiveLock; }
10657 			| ACCESS EXCLUSIVE				{ $$ = AccessExclusiveLock; }
10658 		;
10659 
10660 opt_nowait:	NOWAIT							{ $$ = TRUE; }
10661 			| /*EMPTY*/						{ $$ = FALSE; }
10662 		;
10663 
10664 opt_nowait_or_skip:
10665 			NOWAIT							{ $$ = LockWaitError; }
10666 			| SKIP LOCKED					{ $$ = LockWaitSkip; }
10667 			| /*EMPTY*/						{ $$ = LockWaitBlock; }
10668 		;
10669 
10670 
10671 /*****************************************************************************
10672  *
10673  *		QUERY:
10674  *				UpdateStmt (UPDATE)
10675  *
10676  *****************************************************************************/
10677 
10678 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
10679 			SET set_clause_list
10680 			from_clause
10681 			where_or_current_clause
10682 			returning_clause
10683 				{
10684 					UpdateStmt *n = makeNode(UpdateStmt);
10685 					n->relation = $3;
10686 					n->targetList = $5;
10687 					n->fromClause = $6;
10688 					n->whereClause = $7;
10689 					n->returningList = $8;
10690 					n->withClause = $1;
10691 					$$ = (Node *)n;
10692 				}
10693 		;
10694 
10695 set_clause_list:
10696 			set_clause							{ $$ = $1; }
10697 			| set_clause_list ',' set_clause	{ $$ = list_concat($1,$3); }
10698 		;
10699 
10700 set_clause:
10701 			set_target '=' a_expr
10702 				{
10703 					$1->val = (Node *) $3;
10704 					$$ = list_make1($1);
10705 				}
10706 			| '(' set_target_list ')' '=' a_expr
10707 				{
10708 					int ncolumns = list_length($2);
10709 					int i = 1;
10710 					ListCell *col_cell;
10711 
10712 					/* Create a MultiAssignRef source for each target */
foreach(col_cell,$2)10713 					foreach(col_cell, $2)
10714 					{
10715 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
10716 						MultiAssignRef *r = makeNode(MultiAssignRef);
10717 
10718 						r->source = (Node *) $5;
10719 						r->colno = i;
10720 						r->ncolumns = ncolumns;
10721 						res_col->val = (Node *) r;
10722 						i++;
10723 					}
10724 
10725 					$$ = $2;
10726 				}
10727 		;
10728 
10729 set_target:
10730 			ColId opt_indirection
10731 				{
10732 					$$ = makeNode(ResTarget);
10733 					$$->name = $1;
10734 					$$->indirection = check_indirection($2, yyscanner);
10735 					$$->val = NULL;	/* upper production sets this */
10736 					$$->location = @1;
10737 				}
10738 		;
10739 
10740 set_target_list:
10741 			set_target								{ $$ = list_make1($1); }
10742 			| set_target_list ',' set_target		{ $$ = lappend($1,$3); }
10743 		;
10744 
10745 
10746 /*****************************************************************************
10747  *
10748  *		QUERY:
10749  *				CURSOR STATEMENTS
10750  *
10751  *****************************************************************************/
10752 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
10753 				{
10754 					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
10755 					n->portalname = $2;
10756 					/* currently we always set FAST_PLAN option */
10757 					n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
10758 					n->query = $7;
10759 					$$ = (Node *)n;
10760 				}
10761 		;
10762 
10763 cursor_name:	name						{ $$ = $1; }
10764 		;
10765 
10766 cursor_options: /*EMPTY*/					{ $$ = 0; }
10767 			| cursor_options NO SCROLL		{ $$ = $1 | CURSOR_OPT_NO_SCROLL; }
10768 			| cursor_options SCROLL			{ $$ = $1 | CURSOR_OPT_SCROLL; }
10769 			| cursor_options BINARY			{ $$ = $1 | CURSOR_OPT_BINARY; }
10770 			| cursor_options INSENSITIVE	{ $$ = $1 | CURSOR_OPT_INSENSITIVE; }
10771 		;
10772 
10773 opt_hold: /* EMPTY */						{ $$ = 0; }
10774 			| WITH HOLD						{ $$ = CURSOR_OPT_HOLD; }
10775 			| WITHOUT HOLD					{ $$ = 0; }
10776 		;
10777 
10778 /*****************************************************************************
10779  *
10780  *		QUERY:
10781  *				SELECT STATEMENTS
10782  *
10783  *****************************************************************************/
10784 
10785 /* A complete SELECT statement looks like this.
10786  *
10787  * The rule returns either a single SelectStmt node or a tree of them,
10788  * representing a set-operation tree.
10789  *
10790  * There is an ambiguity when a sub-SELECT is within an a_expr and there
10791  * are excess parentheses: do the parentheses belong to the sub-SELECT or
10792  * to the surrounding a_expr?  We don't really care, but bison wants to know.
10793  * To resolve the ambiguity, we are careful to define the grammar so that
10794  * the decision is staved off as long as possible: as long as we can keep
10795  * absorbing parentheses into the sub-SELECT, we will do so, and only when
10796  * it's no longer possible to do that will we decide that parens belong to
10797  * the expression.	For example, in "SELECT (((SELECT 2)) + 3)" the extra
10798  * parentheses are treated as part of the sub-select.  The necessity of doing
10799  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".	Had we
10800  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
10801  * SELECT viewpoint when we see the UNION.
10802  *
10803  * This approach is implemented by defining a nonterminal select_with_parens,
10804  * which represents a SELECT with at least one outer layer of parentheses,
10805  * and being careful to use select_with_parens, never '(' SelectStmt ')',
10806  * in the expression grammar.  We will then have shift-reduce conflicts
10807  * which we can resolve in favor of always treating '(' <select> ')' as
10808  * a select_with_parens.  To resolve the conflicts, the productions that
10809  * conflict with the select_with_parens productions are manually given
10810  * precedences lower than the precedence of ')', thereby ensuring that we
10811  * shift ')' (and then reduce to select_with_parens) rather than trying to
10812  * reduce the inner <select> nonterminal to something else.  We use UMINUS
10813  * precedence for this, which is a fairly arbitrary choice.
10814  *
10815  * To be able to define select_with_parens itself without ambiguity, we need
10816  * a nonterminal select_no_parens that represents a SELECT structure with no
10817  * outermost parentheses.  This is a little bit tedious, but it works.
10818  *
10819  * In non-expression contexts, we use SelectStmt which can represent a SELECT
10820  * with or without outer parentheses.
10821  */
10822 
10823 SelectStmt: select_no_parens			%prec UMINUS
10824 			| select_with_parens		%prec UMINUS
10825 		;
10826 
10827 select_with_parens:
10828 			'(' select_no_parens ')'				{ $$ = $2; }
10829 			| '(' select_with_parens ')'			{ $$ = $2; }
10830 		;
10831 
10832 /*
10833  * This rule parses the equivalent of the standard's <query expression>.
10834  * The duplicative productions are annoying, but hard to get rid of without
10835  * creating shift/reduce conflicts.
10836  *
10837  *	The locking clause (FOR UPDATE etc) may be before or after LIMIT/OFFSET.
10838  *	In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
10839  *	We now support both orderings, but prefer LIMIT/OFFSET before the locking
10840  * clause.
10841  *	2002-08-28 bjm
10842  */
10843 select_no_parens:
10844 			simple_select						{ $$ = $1; }
10845 			| select_clause sort_clause
10846 				{
10847 					insertSelectOptions((SelectStmt *) $1, $2, NIL,
10848 										NULL, NULL, NULL,
10849 										yyscanner);
10850 					$$ = $1;
10851 				}
10852 			| select_clause opt_sort_clause for_locking_clause opt_select_limit
10853 				{
10854 					insertSelectOptions((SelectStmt *) $1, $2, $3,
10855 										list_nth($4, 0), list_nth($4, 1),
10856 										NULL,
10857 										yyscanner);
10858 					$$ = $1;
10859 				}
10860 			| select_clause opt_sort_clause select_limit opt_for_locking_clause
10861 				{
10862 					insertSelectOptions((SelectStmt *) $1, $2, $4,
10863 										list_nth($3, 0), list_nth($3, 1),
10864 										NULL,
10865 										yyscanner);
10866 					$$ = $1;
10867 				}
10868 			| with_clause select_clause
10869 				{
10870 					insertSelectOptions((SelectStmt *) $2, NULL, NIL,
10871 										NULL, NULL,
10872 										$1,
10873 										yyscanner);
10874 					$$ = $2;
10875 				}
10876 			| with_clause select_clause sort_clause
10877 				{
10878 					insertSelectOptions((SelectStmt *) $2, $3, NIL,
10879 										NULL, NULL,
10880 										$1,
10881 										yyscanner);
10882 					$$ = $2;
10883 				}
10884 			| with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
10885 				{
10886 					insertSelectOptions((SelectStmt *) $2, $3, $4,
10887 										list_nth($5, 0), list_nth($5, 1),
10888 										$1,
10889 										yyscanner);
10890 					$$ = $2;
10891 				}
10892 			| with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
10893 				{
10894 					insertSelectOptions((SelectStmt *) $2, $3, $5,
10895 										list_nth($4, 0), list_nth($4, 1),
10896 										$1,
10897 										yyscanner);
10898 					$$ = $2;
10899 				}
10900 		;
10901 
10902 select_clause:
10903 			simple_select							{ $$ = $1; }
10904 			| select_with_parens					{ $$ = $1; }
10905 		;
10906 
10907 /*
10908  * This rule parses SELECT statements that can appear within set operations,
10909  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
10910  * the ordering of the set operations.	Without '(' and ')' we want the
10911  * operations to be ordered per the precedence specs at the head of this file.
10912  *
10913  * As with select_no_parens, simple_select cannot have outer parentheses,
10914  * but can have parenthesized subclauses.
10915  *
10916  * Note that sort clauses cannot be included at this level --- SQL requires
10917  *		SELECT foo UNION SELECT bar ORDER BY baz
10918  * to be parsed as
10919  *		(SELECT foo UNION SELECT bar) ORDER BY baz
10920  * not
10921  *		SELECT foo UNION (SELECT bar ORDER BY baz)
10922  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
10923  * described as part of the select_no_parens production, not simple_select.
10924  * This does not limit functionality, because you can reintroduce these
10925  * clauses inside parentheses.
10926  *
10927  * NOTE: only the leftmost component SelectStmt should have INTO.
10928  * However, this is not checked by the grammar; parse analysis must check it.
10929  */
10930 simple_select:
10931 			SELECT opt_all_clause opt_target_list
10932 			into_clause from_clause where_clause
10933 			group_clause having_clause window_clause
10934 				{
10935 					SelectStmt *n = makeNode(SelectStmt);
10936 					n->targetList = $3;
10937 					n->intoClause = $4;
10938 					n->fromClause = $5;
10939 					n->whereClause = $6;
10940 					n->groupClause = $7;
10941 					n->havingClause = $8;
10942 					n->windowClause = $9;
10943 					$$ = (Node *)n;
10944 				}
10945 			| SELECT distinct_clause target_list
10946 			into_clause from_clause where_clause
10947 			group_clause having_clause window_clause
10948 				{
10949 					SelectStmt *n = makeNode(SelectStmt);
10950 					n->distinctClause = $2;
10951 					n->targetList = $3;
10952 					n->intoClause = $4;
10953 					n->fromClause = $5;
10954 					n->whereClause = $6;
10955 					n->groupClause = $7;
10956 					n->havingClause = $8;
10957 					n->windowClause = $9;
10958 					$$ = (Node *)n;
10959 				}
10960 			| values_clause							{ $$ = $1; }
10961 			| TABLE relation_expr
10962 				{
10963 					/* same as SELECT * FROM relation_expr */
10964 					ColumnRef *cr = makeNode(ColumnRef);
10965 					ResTarget *rt = makeNode(ResTarget);
10966 					SelectStmt *n = makeNode(SelectStmt);
10967 
10968 					cr->fields = list_make1(makeNode(A_Star));
10969 					cr->location = -1;
10970 
10971 					rt->name = NULL;
10972 					rt->indirection = NIL;
10973 					rt->val = (Node *)cr;
10974 					rt->location = -1;
10975 
10976 					n->targetList = list_make1(rt);
10977 					n->fromClause = list_make1($2);
10978 					$$ = (Node *)n;
10979 				}
10980 			| select_clause UNION all_or_distinct select_clause
10981 				{
10982 					$$ = makeSetOp(SETOP_UNION, $3, $1, $4);
10983 				}
10984 			| select_clause INTERSECT all_or_distinct select_clause
10985 				{
10986 					$$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
10987 				}
10988 			| select_clause EXCEPT all_or_distinct select_clause
10989 				{
10990 					$$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
10991 				}
10992 		;
10993 
10994 /*
10995  * SQL standard WITH clause looks like:
10996  *
10997  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
10998  *		AS (query) [ SEARCH or CYCLE clause ]
10999  *
11000  * We don't currently support the SEARCH or CYCLE clause.
11001  *
11002  * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY.
11003  */
11004 with_clause:
11005 		WITH cte_list
11006 			{
11007 				$$ = makeNode(WithClause);
11008 				$$->ctes = $2;
11009 				$$->recursive = false;
11010 				$$->location = @1;
11011 			}
11012 		| WITH_LA cte_list
11013 			{
11014 				$$ = makeNode(WithClause);
11015 				$$->ctes = $2;
11016 				$$->recursive = false;
11017 				$$->location = @1;
11018 			}
11019 		| WITH RECURSIVE cte_list
11020 			{
11021 				$$ = makeNode(WithClause);
11022 				$$->ctes = $3;
11023 				$$->recursive = true;
11024 				$$->location = @1;
11025 			}
11026 		;
11027 
11028 cte_list:
11029 		common_table_expr						{ $$ = list_make1($1); }
11030 		| cte_list ',' common_table_expr		{ $$ = lappend($1, $3); }
11031 		;
11032 
11033 common_table_expr:  name opt_name_list AS '(' PreparableStmt ')'
11034 			{
11035 				CommonTableExpr *n = makeNode(CommonTableExpr);
11036 				n->ctename = $1;
11037 				n->aliascolnames = $2;
11038 				n->ctequery = $5;
11039 				n->location = @1;
11040 				$$ = (Node *) n;
11041 			}
11042 		;
11043 
11044 opt_with_clause:
11045 		with_clause								{ $$ = $1; }
11046 		| /*EMPTY*/								{ $$ = NULL; }
11047 		;
11048 
11049 into_clause:
11050 			INTO OptTempTableName
11051 				{
11052 					$$ = makeNode(IntoClause);
11053 					$$->rel = $2;
11054 					$$->colNames = NIL;
11055 					$$->options = NIL;
11056 					$$->onCommit = ONCOMMIT_NOOP;
11057 					$$->tableSpaceName = NULL;
11058 					$$->viewQuery = NULL;
11059 					$$->skipData = false;
11060 				}
11061 			| /*EMPTY*/
11062 				{ $$ = NULL; }
11063 		;
11064 
11065 /*
11066  * Redundancy here is needed to avoid shift/reduce conflicts,
11067  * since TEMP is not a reserved word.  See also OptTemp.
11068  */
11069 OptTempTableName:
11070 			TEMPORARY opt_table qualified_name
11071 				{
11072 					$$ = $3;
11073 					$$->relpersistence = RELPERSISTENCE_TEMP;
11074 				}
11075 			| TEMP opt_table qualified_name
11076 				{
11077 					$$ = $3;
11078 					$$->relpersistence = RELPERSISTENCE_TEMP;
11079 				}
11080 			| LOCAL TEMPORARY opt_table qualified_name
11081 				{
11082 					$$ = $4;
11083 					$$->relpersistence = RELPERSISTENCE_TEMP;
11084 				}
11085 			| LOCAL TEMP opt_table qualified_name
11086 				{
11087 					$$ = $4;
11088 					$$->relpersistence = RELPERSISTENCE_TEMP;
11089 				}
11090 			| GLOBAL TEMPORARY opt_table qualified_name
11091 				{
11092 					ereport(WARNING,
11093 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11094 							 parser_errposition(@1)));
11095 					$$ = $4;
11096 					$$->relpersistence = RELPERSISTENCE_TEMP;
11097 				}
11098 			| GLOBAL TEMP opt_table qualified_name
11099 				{
11100 					ereport(WARNING,
11101 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11102 							 parser_errposition(@1)));
11103 					$$ = $4;
11104 					$$->relpersistence = RELPERSISTENCE_TEMP;
11105 				}
11106 			| UNLOGGED opt_table qualified_name
11107 				{
11108 					$$ = $3;
11109 					$$->relpersistence = RELPERSISTENCE_UNLOGGED;
11110 				}
11111 			| TABLE qualified_name
11112 				{
11113 					$$ = $2;
11114 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11115 				}
11116 			| qualified_name
11117 				{
11118 					$$ = $1;
11119 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11120 				}
11121 		;
11122 
11123 opt_table:	TABLE									{}
11124 			| /*EMPTY*/								{}
11125 		;
11126 
11127 all_or_distinct:
11128 			ALL										{ $$ = TRUE; }
11129 			| DISTINCT								{ $$ = FALSE; }
11130 			| /*EMPTY*/								{ $$ = FALSE; }
11131 		;
11132 
11133 /* We use (NIL) as a placeholder to indicate that all target expressions
11134  * should be placed in the DISTINCT list during parsetree analysis.
11135  */
11136 distinct_clause:
11137 			DISTINCT								{ $$ = list_make1(NIL); }
11138 			| DISTINCT ON '(' expr_list ')'			{ $$ = $4; }
11139 		;
11140 
11141 opt_all_clause:
11142 			ALL										{ $$ = NIL;}
11143 			| /*EMPTY*/								{ $$ = NIL; }
11144 		;
11145 
11146 opt_sort_clause:
11147 			sort_clause								{ $$ = $1;}
11148 			| /*EMPTY*/								{ $$ = NIL; }
11149 		;
11150 
11151 sort_clause:
11152 			ORDER BY sortby_list					{ $$ = $3; }
11153 		;
11154 
11155 sortby_list:
11156 			sortby									{ $$ = list_make1($1); }
11157 			| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
11158 		;
11159 
11160 sortby:		a_expr USING qual_all_Op opt_nulls_order
11161 				{
11162 					$$ = makeNode(SortBy);
11163 					$$->node = $1;
11164 					$$->sortby_dir = SORTBY_USING;
11165 					$$->sortby_nulls = $4;
11166 					$$->useOp = $3;
11167 					$$->location = @3;
11168 				}
11169 			| a_expr opt_asc_desc opt_nulls_order
11170 				{
11171 					$$ = makeNode(SortBy);
11172 					$$->node = $1;
11173 					$$->sortby_dir = $2;
11174 					$$->sortby_nulls = $3;
11175 					$$->useOp = NIL;
11176 					$$->location = -1;		/* no operator */
11177 				}
11178 		;
11179 
11180 
11181 select_limit:
11182 			limit_clause offset_clause			{ $$ = list_make2($2, $1); }
11183 			| offset_clause limit_clause		{ $$ = list_make2($1, $2); }
11184 			| limit_clause						{ $$ = list_make2(NULL, $1); }
11185 			| offset_clause						{ $$ = list_make2($1, NULL); }
11186 		;
11187 
11188 opt_select_limit:
11189 			select_limit						{ $$ = $1; }
11190 			| /* EMPTY */						{ $$ = list_make2(NULL,NULL); }
11191 		;
11192 
11193 limit_clause:
11194 			LIMIT select_limit_value
11195 				{ $$ = $2; }
11196 			| LIMIT select_limit_value ',' select_offset_value
11197 				{
11198 					/* Disabled because it was too confusing, bjm 2002-02-18 */
11199 					ereport(ERROR,
11200 							(errcode(ERRCODE_SYNTAX_ERROR),
11201 							 errmsg("LIMIT #,# syntax is not supported"),
11202 							 errhint("Use separate LIMIT and OFFSET clauses."),
11203 							 parser_errposition(@1)));
11204 				}
11205 			/* SQL:2008 syntax */
11206 			/* to avoid shift/reduce conflicts, handle the optional value with
11207 			 * a separate production rather than an opt_ expression.  The fact
11208 			 * that ONLY is fully reserved means that this way, we defer any
11209 			 * decision about what rule reduces ROW or ROWS to the point where
11210 			 * we can see the ONLY token in the lookahead slot.
11211 			 */
11212 			| FETCH first_or_next select_fetch_first_value row_or_rows ONLY
11213 				{ $$ = $3; }
11214 			| FETCH first_or_next row_or_rows ONLY
11215 				{ $$ = makeIntConst(1, -1); }
11216 		;
11217 
11218 offset_clause:
11219 			OFFSET select_offset_value
11220 				{ $$ = $2; }
11221 			/* SQL:2008 syntax */
11222 			| OFFSET select_fetch_first_value row_or_rows
11223 				{ $$ = $2; }
11224 		;
11225 
11226 select_limit_value:
11227 			a_expr									{ $$ = $1; }
11228 			| ALL
11229 				{
11230 					/* LIMIT ALL is represented as a NULL constant */
11231 					$$ = makeNullAConst(@1);
11232 				}
11233 		;
11234 
11235 select_offset_value:
11236 			a_expr									{ $$ = $1; }
11237 		;
11238 
11239 /*
11240  * Allowing full expressions without parentheses causes various parsing
11241  * problems with the trailing ROW/ROWS key words.  SQL spec only calls for
11242  * <simple value specification>, which is either a literal or a parameter (but
11243  * an <SQL parameter reference> could be an identifier, bringing up conflicts
11244  * with ROW/ROWS). We solve this by leveraging the presence of ONLY (see above)
11245  * to determine whether the expression is missing rather than trying to make it
11246  * optional in this rule.
11247  *
11248  * c_expr covers almost all the spec-required cases (and more), but it doesn't
11249  * cover signed numeric literals, which are allowed by the spec. So we include
11250  * those here explicitly. We need FCONST as well as ICONST because values that
11251  * don't fit in the platform's "long", but do fit in bigint, should still be
11252  * accepted here. (This is possible in 64-bit Windows as well as all 32-bit
11253  * builds.)
11254  */
11255 select_fetch_first_value:
11256 			c_expr									{ $$ = $1; }
11257 			| '+' I_or_F_const
11258 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
11259 			| '-' I_or_F_const
11260 				{ $$ = doNegate($2, @1); }
11261 		;
11262 
11263 I_or_F_const:
11264 			Iconst									{ $$ = makeIntConst($1,@1); }
11265 			| FCONST								{ $$ = makeFloatConst($1,@1); }
11266 		;
11267 
11268 /* noise words */
11269 row_or_rows: ROW									{ $$ = 0; }
11270 			| ROWS									{ $$ = 0; }
11271 		;
11272 
11273 first_or_next: FIRST_P								{ $$ = 0; }
11274 			| NEXT									{ $$ = 0; }
11275 		;
11276 
11277 
11278 /*
11279  * This syntax for group_clause tries to follow the spec quite closely.
11280  * However, the spec allows only column references, not expressions,
11281  * which introduces an ambiguity between implicit row constructors
11282  * (a,b) and lists of column references.
11283  *
11284  * We handle this by using the a_expr production for what the spec calls
11285  * <ordinary grouping set>, which in the spec represents either one column
11286  * reference or a parenthesized list of column references. Then, we check the
11287  * top node of the a_expr to see if it's an implicit RowExpr, and if so, just
11288  * grab and use the list, discarding the node. (this is done in parse analysis,
11289  * not here)
11290  *
11291  * (we abuse the row_format field of RowExpr to distinguish implicit and
11292  * explicit row constructors; it's debatable if anyone sanely wants to use them
11293  * in a group clause, but if they have a reason to, we make it possible.)
11294  *
11295  * Each item in the group_clause list is either an expression tree or a
11296  * GroupingSet node of some type.
11297  */
11298 group_clause:
11299 			GROUP_P BY group_by_list				{ $$ = $3; }
11300 			| /*EMPTY*/								{ $$ = NIL; }
11301 		;
11302 
11303 group_by_list:
11304 			group_by_item							{ $$ = list_make1($1); }
11305 			| group_by_list ',' group_by_item		{ $$ = lappend($1,$3); }
11306 		;
11307 
11308 group_by_item:
11309 			a_expr									{ $$ = $1; }
11310 			| empty_grouping_set					{ $$ = $1; }
11311 			| cube_clause							{ $$ = $1; }
11312 			| rollup_clause							{ $$ = $1; }
11313 			| grouping_sets_clause					{ $$ = $1; }
11314 		;
11315 
11316 empty_grouping_set:
11317 			'(' ')'
11318 				{
11319 					$$ = (Node *) makeGroupingSet(GROUPING_SET_EMPTY, NIL, @1);
11320 				}
11321 		;
11322 
11323 /*
11324  * These hacks rely on setting precedence of CUBE and ROLLUP below that of '(',
11325  * so that they shift in these rules rather than reducing the conflicting
11326  * unreserved_keyword rule.
11327  */
11328 
11329 rollup_clause:
11330 			ROLLUP '(' expr_list ')'
11331 				{
11332 					$$ = (Node *) makeGroupingSet(GROUPING_SET_ROLLUP, $3, @1);
11333 				}
11334 		;
11335 
11336 cube_clause:
11337 			CUBE '(' expr_list ')'
11338 				{
11339 					$$ = (Node *) makeGroupingSet(GROUPING_SET_CUBE, $3, @1);
11340 				}
11341 		;
11342 
11343 grouping_sets_clause:
11344 			GROUPING SETS '(' group_by_list ')'
11345 				{
11346 					$$ = (Node *) makeGroupingSet(GROUPING_SET_SETS, $4, @1);
11347 				}
11348 		;
11349 
11350 having_clause:
11351 			HAVING a_expr							{ $$ = $2; }
11352 			| /*EMPTY*/								{ $$ = NULL; }
11353 		;
11354 
11355 for_locking_clause:
11356 			for_locking_items						{ $$ = $1; }
11357 			| FOR READ ONLY							{ $$ = NIL; }
11358 		;
11359 
11360 opt_for_locking_clause:
11361 			for_locking_clause						{ $$ = $1; }
11362 			| /* EMPTY */							{ $$ = NIL; }
11363 		;
11364 
11365 for_locking_items:
11366 			for_locking_item						{ $$ = list_make1($1); }
11367 			| for_locking_items for_locking_item	{ $$ = lappend($1, $2); }
11368 		;
11369 
11370 for_locking_item:
11371 			for_locking_strength locked_rels_list opt_nowait_or_skip
11372 				{
11373 					LockingClause *n = makeNode(LockingClause);
11374 					n->lockedRels = $2;
11375 					n->strength = $1;
11376 					n->waitPolicy = $3;
11377 					$$ = (Node *) n;
11378 				}
11379 		;
11380 
11381 for_locking_strength:
11382 			FOR UPDATE 							{ $$ = LCS_FORUPDATE; }
11383 			| FOR NO KEY UPDATE 				{ $$ = LCS_FORNOKEYUPDATE; }
11384 			| FOR SHARE 						{ $$ = LCS_FORSHARE; }
11385 			| FOR KEY SHARE 					{ $$ = LCS_FORKEYSHARE; }
11386 		;
11387 
11388 locked_rels_list:
11389 			OF qualified_name_list					{ $$ = $2; }
11390 			| /* EMPTY */							{ $$ = NIL; }
11391 		;
11392 
11393 
11394 /*
11395  * We should allow ROW '(' expr_list ')' too, but that seems to require
11396  * making VALUES a fully reserved word, which will probably break more apps
11397  * than allowing the noise-word is worth.
11398  */
11399 values_clause:
11400 			VALUES '(' expr_list ')'
11401 				{
11402 					SelectStmt *n = makeNode(SelectStmt);
11403 					n->valuesLists = list_make1($3);
11404 					$$ = (Node *) n;
11405 				}
11406 			| values_clause ',' '(' expr_list ')'
11407 				{
11408 					SelectStmt *n = (SelectStmt *) $1;
11409 					n->valuesLists = lappend(n->valuesLists, $4);
11410 					$$ = (Node *) n;
11411 				}
11412 		;
11413 
11414 
11415 /*****************************************************************************
11416  *
11417  *	clauses common to all Optimizable Stmts:
11418  *		from_clause		- allow list of both JOIN expressions and table names
11419  *		where_clause	- qualifications for joins or restrictions
11420  *
11421  *****************************************************************************/
11422 
11423 from_clause:
11424 			FROM from_list							{ $$ = $2; }
11425 			| /*EMPTY*/								{ $$ = NIL; }
11426 		;
11427 
11428 from_list:
11429 			table_ref								{ $$ = list_make1($1); }
11430 			| from_list ',' table_ref				{ $$ = lappend($1, $3); }
11431 		;
11432 
11433 /*
11434  * table_ref is where an alias clause can be attached.
11435  */
11436 table_ref:	relation_expr opt_alias_clause
11437 				{
11438 					$1->alias = $2;
11439 					$$ = (Node *) $1;
11440 				}
11441 			| relation_expr opt_alias_clause tablesample_clause
11442 				{
11443 					RangeTableSample *n = (RangeTableSample *) $3;
11444 					$1->alias = $2;
11445 					/* relation_expr goes inside the RangeTableSample node */
11446 					n->relation = (Node *) $1;
11447 					$$ = (Node *) n;
11448 				}
11449 			| func_table func_alias_clause
11450 				{
11451 					RangeFunction *n = (RangeFunction *) $1;
11452 					n->alias = linitial($2);
11453 					n->coldeflist = lsecond($2);
11454 					$$ = (Node *) n;
11455 				}
11456 			| LATERAL_P func_table func_alias_clause
11457 				{
11458 					RangeFunction *n = (RangeFunction *) $2;
11459 					n->lateral = true;
11460 					n->alias = linitial($3);
11461 					n->coldeflist = lsecond($3);
11462 					$$ = (Node *) n;
11463 				}
11464 			| xmltable opt_alias_clause
11465 				{
11466 					RangeTableFunc *n = (RangeTableFunc *) $1;
11467 					n->alias = $2;
11468 					$$ = (Node *) n;
11469 				}
11470 			| LATERAL_P xmltable opt_alias_clause
11471 				{
11472 					RangeTableFunc *n = (RangeTableFunc *) $2;
11473 					n->lateral = true;
11474 					n->alias = $3;
11475 					$$ = (Node *) n;
11476 				}
11477 			| select_with_parens opt_alias_clause
11478 				{
11479 					RangeSubselect *n = makeNode(RangeSubselect);
11480 					n->lateral = false;
11481 					n->subquery = $1;
11482 					n->alias = $2;
11483 					/*
11484 					 * The SQL spec does not permit a subselect
11485 					 * (<derived_table>) without an alias clause,
11486 					 * so we don't either.  This avoids the problem
11487 					 * of needing to invent a unique refname for it.
11488 					 * That could be surmounted if there's sufficient
11489 					 * popular demand, but for now let's just implement
11490 					 * the spec and see if anyone complains.
11491 					 * However, it does seem like a good idea to emit
11492 					 * an error message that's better than "syntax error".
11493 					 */
11494 					if ($2 == NULL)
11495 					{
11496 						if (IsA($1, SelectStmt) &&
11497 							((SelectStmt *) $1)->valuesLists)
11498 							ereport(ERROR,
11499 									(errcode(ERRCODE_SYNTAX_ERROR),
11500 									 errmsg("VALUES in FROM must have an alias"),
11501 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
11502 									 parser_errposition(@1)));
11503 						else
11504 							ereport(ERROR,
11505 									(errcode(ERRCODE_SYNTAX_ERROR),
11506 									 errmsg("subquery in FROM must have an alias"),
11507 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
11508 									 parser_errposition(@1)));
11509 					}
11510 					$$ = (Node *) n;
11511 				}
11512 			| LATERAL_P select_with_parens opt_alias_clause
11513 				{
11514 					RangeSubselect *n = makeNode(RangeSubselect);
11515 					n->lateral = true;
11516 					n->subquery = $2;
11517 					n->alias = $3;
11518 					/* same comment as above */
11519 					if ($3 == NULL)
11520 					{
11521 						if (IsA($2, SelectStmt) &&
11522 							((SelectStmt *) $2)->valuesLists)
11523 							ereport(ERROR,
11524 									(errcode(ERRCODE_SYNTAX_ERROR),
11525 									 errmsg("VALUES in FROM must have an alias"),
11526 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
11527 									 parser_errposition(@2)));
11528 						else
11529 							ereport(ERROR,
11530 									(errcode(ERRCODE_SYNTAX_ERROR),
11531 									 errmsg("subquery in FROM must have an alias"),
11532 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
11533 									 parser_errposition(@2)));
11534 					}
11535 					$$ = (Node *) n;
11536 				}
11537 			| joined_table
11538 				{
11539 					$$ = (Node *) $1;
11540 				}
11541 			| '(' joined_table ')' alias_clause
11542 				{
11543 					$2->alias = $4;
11544 					$$ = (Node *) $2;
11545 				}
11546 		;
11547 
11548 
11549 /*
11550  * It may seem silly to separate joined_table from table_ref, but there is
11551  * method in SQL's madness: if you don't do it this way you get reduce-
11552  * reduce conflicts, because it's not clear to the parser generator whether
11553  * to expect alias_clause after ')' or not.  For the same reason we must
11554  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
11555  * join_type to expand to empty; if we try it, the parser generator can't
11556  * figure out when to reduce an empty join_type right after table_ref.
11557  *
11558  * Note that a CROSS JOIN is the same as an unqualified
11559  * INNER JOIN, and an INNER JOIN/ON has the same shape
11560  * but a qualification expression to limit membership.
11561  * A NATURAL JOIN implicitly matches column names between
11562  * tables and the shape is determined by which columns are
11563  * in common. We'll collect columns during the later transformations.
11564  */
11565 
11566 joined_table:
11567 			'(' joined_table ')'
11568 				{
11569 					$$ = $2;
11570 				}
11571 			| table_ref CROSS JOIN table_ref
11572 				{
11573 					/* CROSS JOIN is same as unqualified inner join */
11574 					JoinExpr *n = makeNode(JoinExpr);
11575 					n->jointype = JOIN_INNER;
11576 					n->isNatural = FALSE;
11577 					n->larg = $1;
11578 					n->rarg = $4;
11579 					n->usingClause = NIL;
11580 					n->quals = NULL;
11581 					$$ = n;
11582 				}
11583 			| table_ref join_type JOIN table_ref join_qual
11584 				{
11585 					JoinExpr *n = makeNode(JoinExpr);
11586 					n->jointype = $2;
11587 					n->isNatural = FALSE;
11588 					n->larg = $1;
11589 					n->rarg = $4;
11590 					if ($5 != NULL && IsA($5, List))
11591 						n->usingClause = (List *) $5; /* USING clause */
11592 					else
11593 						n->quals = $5; /* ON clause */
11594 					$$ = n;
11595 				}
11596 			| table_ref JOIN table_ref join_qual
11597 				{
11598 					/* letting join_type reduce to empty doesn't work */
11599 					JoinExpr *n = makeNode(JoinExpr);
11600 					n->jointype = JOIN_INNER;
11601 					n->isNatural = FALSE;
11602 					n->larg = $1;
11603 					n->rarg = $3;
11604 					if ($4 != NULL && IsA($4, List))
11605 						n->usingClause = (List *) $4; /* USING clause */
11606 					else
11607 						n->quals = $4; /* ON clause */
11608 					$$ = n;
11609 				}
11610 			| table_ref NATURAL join_type JOIN table_ref
11611 				{
11612 					JoinExpr *n = makeNode(JoinExpr);
11613 					n->jointype = $3;
11614 					n->isNatural = TRUE;
11615 					n->larg = $1;
11616 					n->rarg = $5;
11617 					n->usingClause = NIL; /* figure out which columns later... */
11618 					n->quals = NULL; /* fill later */
11619 					$$ = n;
11620 				}
11621 			| table_ref NATURAL JOIN table_ref
11622 				{
11623 					/* letting join_type reduce to empty doesn't work */
11624 					JoinExpr *n = makeNode(JoinExpr);
11625 					n->jointype = JOIN_INNER;
11626 					n->isNatural = TRUE;
11627 					n->larg = $1;
11628 					n->rarg = $4;
11629 					n->usingClause = NIL; /* figure out which columns later... */
11630 					n->quals = NULL; /* fill later */
11631 					$$ = n;
11632 				}
11633 		;
11634 
11635 alias_clause:
11636 			AS ColId '(' name_list ')'
11637 				{
11638 					$$ = makeNode(Alias);
11639 					$$->aliasname = $2;
11640 					$$->colnames = $4;
11641 				}
11642 			| AS ColId
11643 				{
11644 					$$ = makeNode(Alias);
11645 					$$->aliasname = $2;
11646 				}
11647 			| ColId '(' name_list ')'
11648 				{
11649 					$$ = makeNode(Alias);
11650 					$$->aliasname = $1;
11651 					$$->colnames = $3;
11652 				}
11653 			| ColId
11654 				{
11655 					$$ = makeNode(Alias);
11656 					$$->aliasname = $1;
11657 				}
11658 		;
11659 
11660 opt_alias_clause: alias_clause						{ $$ = $1; }
11661 			| /*EMPTY*/								{ $$ = NULL; }
11662 		;
11663 
11664 /*
11665  * func_alias_clause can include both an Alias and a coldeflist, so we make it
11666  * return a 2-element list that gets disassembled by calling production.
11667  */
11668 func_alias_clause:
11669 			alias_clause
11670 				{
11671 					$$ = list_make2($1, NIL);
11672 				}
11673 			| AS '(' TableFuncElementList ')'
11674 				{
11675 					$$ = list_make2(NULL, $3);
11676 				}
11677 			| AS ColId '(' TableFuncElementList ')'
11678 				{
11679 					Alias *a = makeNode(Alias);
11680 					a->aliasname = $2;
11681 					$$ = list_make2(a, $4);
11682 				}
11683 			| ColId '(' TableFuncElementList ')'
11684 				{
11685 					Alias *a = makeNode(Alias);
11686 					a->aliasname = $1;
11687 					$$ = list_make2(a, $3);
11688 				}
11689 			| /*EMPTY*/
11690 				{
11691 					$$ = list_make2(NULL, NIL);
11692 				}
11693 		;
11694 
11695 join_type:	FULL join_outer							{ $$ = JOIN_FULL; }
11696 			| LEFT join_outer						{ $$ = JOIN_LEFT; }
11697 			| RIGHT join_outer						{ $$ = JOIN_RIGHT; }
11698 			| INNER_P								{ $$ = JOIN_INNER; }
11699 		;
11700 
11701 /* OUTER is just noise... */
11702 join_outer: OUTER_P									{ $$ = NULL; }
11703 			| /*EMPTY*/								{ $$ = NULL; }
11704 		;
11705 
11706 /* JOIN qualification clauses
11707  * Possibilities are:
11708  *	USING ( column list ) allows only unqualified column names,
11709  *						  which must match between tables.
11710  *	ON expr allows more general qualifications.
11711  *
11712  * We return USING as a List node, while an ON-expr will not be a List.
11713  */
11714 
11715 join_qual:	USING '(' name_list ')'					{ $$ = (Node *) $3; }
11716 			| ON a_expr								{ $$ = $2; }
11717 		;
11718 
11719 
11720 relation_expr:
11721 			qualified_name
11722 				{
11723 					/* inheritance query, implicitly */
11724 					$$ = $1;
11725 					$$->inh = true;
11726 					$$->alias = NULL;
11727 				}
11728 			| qualified_name '*'
11729 				{
11730 					/* inheritance query, explicitly */
11731 					$$ = $1;
11732 					$$->inh = true;
11733 					$$->alias = NULL;
11734 				}
11735 			| ONLY qualified_name
11736 				{
11737 					/* no inheritance */
11738 					$$ = $2;
11739 					$$->inh = false;
11740 					$$->alias = NULL;
11741 				}
11742 			| ONLY '(' qualified_name ')'
11743 				{
11744 					/* no inheritance, SQL99-style syntax */
11745 					$$ = $3;
11746 					$$->inh = false;
11747 					$$->alias = NULL;
11748 				}
11749 		;
11750 
11751 
11752 relation_expr_list:
11753 			relation_expr							{ $$ = list_make1($1); }
11754 			| relation_expr_list ',' relation_expr	{ $$ = lappend($1, $3); }
11755 		;
11756 
11757 
11758 /*
11759  * Given "UPDATE foo set set ...", we have to decide without looking any
11760  * further ahead whether the first "set" is an alias or the UPDATE's SET
11761  * keyword.  Since "set" is allowed as a column name both interpretations
11762  * are feasible.  We resolve the shift/reduce conflict by giving the first
11763  * relation_expr_opt_alias production a higher precedence than the SET token
11764  * has, causing the parser to prefer to reduce, in effect assuming that the
11765  * SET is not an alias.
11766  */
11767 relation_expr_opt_alias: relation_expr					%prec UMINUS
11768 				{
11769 					$$ = $1;
11770 				}
11771 			| relation_expr ColId
11772 				{
11773 					Alias *alias = makeNode(Alias);
11774 					alias->aliasname = $2;
11775 					$1->alias = alias;
11776 					$$ = $1;
11777 				}
11778 			| relation_expr AS ColId
11779 				{
11780 					Alias *alias = makeNode(Alias);
11781 					alias->aliasname = $3;
11782 					$1->alias = alias;
11783 					$$ = $1;
11784 				}
11785 		;
11786 
11787 /*
11788  * TABLESAMPLE decoration in a FROM item
11789  */
11790 tablesample_clause:
11791 			TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause
11792 				{
11793 					RangeTableSample *n = makeNode(RangeTableSample);
11794 					/* n->relation will be filled in later */
11795 					n->method = $2;
11796 					n->args = $4;
11797 					n->repeatable = $6;
11798 					n->location = @2;
11799 					$$ = (Node *) n;
11800 				}
11801 		;
11802 
11803 opt_repeatable_clause:
11804 			REPEATABLE '(' a_expr ')'	{ $$ = (Node *) $3; }
11805 			| /*EMPTY*/					{ $$ = NULL; }
11806 		;
11807 
11808 /*
11809  * func_table represents a function invocation in a FROM list. It can be
11810  * a plain function call, like "foo(...)", or a ROWS FROM expression with
11811  * one or more function calls, "ROWS FROM (foo(...), bar(...))",
11812  * optionally with WITH ORDINALITY attached.
11813  * In the ROWS FROM syntax, a column definition list can be given for each
11814  * function, for example:
11815  *     ROWS FROM (foo() AS (foo_res_a text, foo_res_b text),
11816  *                bar() AS (bar_res_a text, bar_res_b text))
11817  * It's also possible to attach a column definition list to the RangeFunction
11818  * as a whole, but that's handled by the table_ref production.
11819  */
11820 func_table: func_expr_windowless opt_ordinality
11821 				{
11822 					RangeFunction *n = makeNode(RangeFunction);
11823 					n->lateral = false;
11824 					n->ordinality = $2;
11825 					n->is_rowsfrom = false;
11826 					n->functions = list_make1(list_make2($1, NIL));
11827 					/* alias and coldeflist are set by table_ref production */
11828 					$$ = (Node *) n;
11829 				}
11830 			| ROWS FROM '(' rowsfrom_list ')' opt_ordinality
11831 				{
11832 					RangeFunction *n = makeNode(RangeFunction);
11833 					n->lateral = false;
11834 					n->ordinality = $6;
11835 					n->is_rowsfrom = true;
11836 					n->functions = $4;
11837 					/* alias and coldeflist are set by table_ref production */
11838 					$$ = (Node *) n;
11839 				}
11840 		;
11841 
11842 rowsfrom_item: func_expr_windowless opt_col_def_list
11843 				{ $$ = list_make2($1, $2); }
11844 		;
11845 
11846 rowsfrom_list:
11847 			rowsfrom_item						{ $$ = list_make1($1); }
11848 			| rowsfrom_list ',' rowsfrom_item	{ $$ = lappend($1, $3); }
11849 		;
11850 
11851 opt_col_def_list: AS '(' TableFuncElementList ')'	{ $$ = $3; }
11852 			| /*EMPTY*/								{ $$ = NIL; }
11853 		;
11854 
11855 opt_ordinality: WITH_LA ORDINALITY					{ $$ = true; }
11856 			| /*EMPTY*/								{ $$ = false; }
11857 		;
11858 
11859 
11860 where_clause:
11861 			WHERE a_expr							{ $$ = $2; }
11862 			| /*EMPTY*/								{ $$ = NULL; }
11863 		;
11864 
11865 /* variant for UPDATE and DELETE */
11866 where_or_current_clause:
11867 			WHERE a_expr							{ $$ = $2; }
11868 			| WHERE CURRENT_P OF cursor_name
11869 				{
11870 					CurrentOfExpr *n = makeNode(CurrentOfExpr);
11871 					/* cvarno is filled in by parse analysis */
11872 					n->cursor_name = $4;
11873 					n->cursor_param = 0;
11874 					$$ = (Node *) n;
11875 				}
11876 			| /*EMPTY*/								{ $$ = NULL; }
11877 		;
11878 
11879 
11880 OptTableFuncElementList:
11881 			TableFuncElementList				{ $$ = $1; }
11882 			| /*EMPTY*/							{ $$ = NIL; }
11883 		;
11884 
11885 TableFuncElementList:
11886 			TableFuncElement
11887 				{
11888 					$$ = list_make1($1);
11889 				}
11890 			| TableFuncElementList ',' TableFuncElement
11891 				{
11892 					$$ = lappend($1, $3);
11893 				}
11894 		;
11895 
11896 TableFuncElement:	ColId Typename opt_collate_clause
11897 				{
11898 					ColumnDef *n = makeNode(ColumnDef);
11899 					n->colname = $1;
11900 					n->typeName = $2;
11901 					n->inhcount = 0;
11902 					n->is_local = true;
11903 					n->is_not_null = false;
11904 					n->is_from_type = false;
11905 					n->storage = 0;
11906 					n->raw_default = NULL;
11907 					n->cooked_default = NULL;
11908 					n->collClause = (CollateClause *) $3;
11909 					n->collOid = InvalidOid;
11910 					n->constraints = NIL;
11911 					n->location = @1;
11912 					$$ = (Node *)n;
11913 				}
11914 		;
11915 
11916 /*
11917  * XMLTABLE
11918  */
11919 xmltable:
11920 			XMLTABLE '(' c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
11921 				{
11922 					RangeTableFunc *n = makeNode(RangeTableFunc);
11923 					n->rowexpr = $3;
11924 					n->docexpr = $4;
11925 					n->columns = $6;
11926 					n->namespaces = NIL;
11927 					n->location = @1;
11928 					$$ = (Node *)n;
11929 				}
11930 			| XMLTABLE '(' XMLNAMESPACES '(' xml_namespace_list ')' ','
11931 				c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
11932 				{
11933 					RangeTableFunc *n = makeNode(RangeTableFunc);
11934 					n->rowexpr = $8;
11935 					n->docexpr = $9;
11936 					n->columns = $11;
11937 					n->namespaces = $5;
11938 					n->location = @1;
11939 					$$ = (Node *)n;
11940 				}
11941 		;
11942 
11943 xmltable_column_list: xmltable_column_el					{ $$ = list_make1($1); }
11944 			| xmltable_column_list ',' xmltable_column_el	{ $$ = lappend($1, $3); }
11945 		;
11946 
11947 xmltable_column_el:
11948 			ColId Typename
11949 				{
11950 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
11951 
11952 					fc->colname = $1;
11953 					fc->for_ordinality = false;
11954 					fc->typeName = $2;
11955 					fc->is_not_null = false;
11956 					fc->colexpr = NULL;
11957 					fc->coldefexpr = NULL;
11958 					fc->location = @1;
11959 
11960 					$$ = (Node *) fc;
11961 				}
11962 			| ColId Typename xmltable_column_option_list
11963 				{
11964 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
11965 					ListCell		   *option;
11966 					bool				nullability_seen = false;
11967 
11968 					fc->colname = $1;
11969 					fc->typeName = $2;
11970 					fc->for_ordinality = false;
11971 					fc->is_not_null = false;
11972 					fc->colexpr = NULL;
11973 					fc->coldefexpr = NULL;
11974 					fc->location = @1;
11975 
foreach(option,$3)11976 					foreach(option, $3)
11977 					{
11978 						DefElem   *defel = (DefElem *) lfirst(option);
11979 
11980 						if (strcmp(defel->defname, "default") == 0)
11981 						{
11982 							if (fc->coldefexpr != NULL)
11983 								ereport(ERROR,
11984 										(errcode(ERRCODE_SYNTAX_ERROR),
11985 										 errmsg("only one DEFAULT value is allowed"),
11986 										 parser_errposition(defel->location)));
11987 							fc->coldefexpr = defel->arg;
11988 						}
11989 						else if (strcmp(defel->defname, "path") == 0)
11990 						{
11991 							if (fc->colexpr != NULL)
11992 								ereport(ERROR,
11993 										(errcode(ERRCODE_SYNTAX_ERROR),
11994 										 errmsg("only one PATH value per column is allowed"),
11995 										 parser_errposition(defel->location)));
11996 							fc->colexpr = defel->arg;
11997 						}
11998 						else if (strcmp(defel->defname, "is_not_null") == 0)
11999 						{
12000 							if (nullability_seen)
12001 								ereport(ERROR,
12002 										(errcode(ERRCODE_SYNTAX_ERROR),
12003 										 errmsg("conflicting or redundant NULL / NOT NULL declarations for column \"%s\"", fc->colname),
12004 										 parser_errposition(defel->location)));
12005 							fc->is_not_null = intVal(defel->arg);
12006 							nullability_seen = true;
12007 						}
12008 						else
12009 						{
12010 							ereport(ERROR,
12011 									(errcode(ERRCODE_SYNTAX_ERROR),
12012 									 errmsg("unrecognized column option \"%s\"",
12013 											defel->defname),
12014 									 parser_errposition(defel->location)));
12015 						}
12016 					}
12017 					$$ = (Node *) fc;
12018 				}
12019 			| ColId FOR ORDINALITY
12020 				{
12021 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12022 
12023 					fc->colname = $1;
12024 					fc->for_ordinality = true;
12025 					/* other fields are ignored, initialized by makeNode */
12026 					fc->location = @1;
12027 
12028 					$$ = (Node *) fc;
12029 				}
12030 		;
12031 
12032 xmltable_column_option_list:
12033 			xmltable_column_option_el
12034 				{ $$ = list_make1($1); }
12035 			| xmltable_column_option_list xmltable_column_option_el
12036 				{ $$ = lappend($1, $2); }
12037 		;
12038 
12039 xmltable_column_option_el:
12040 			IDENT b_expr
12041 				{ $$ = makeDefElem($1, $2, @1); }
12042 			| DEFAULT b_expr
12043 				{ $$ = makeDefElem("default", $2, @1); }
12044 			| NOT NULL_P
12045 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(true), @1); }
12046 			| NULL_P
12047 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(false), @1); }
12048 		;
12049 
12050 xml_namespace_list:
12051 			xml_namespace_el
12052 				{ $$ = list_make1($1); }
12053 			| xml_namespace_list ',' xml_namespace_el
12054 				{ $$ = lappend($1, $3); }
12055 		;
12056 
12057 xml_namespace_el:
12058 			b_expr AS ColLabel
12059 				{
12060 					$$ = makeNode(ResTarget);
12061 					$$->name = $3;
12062 					$$->indirection = NIL;
12063 					$$->val = $1;
12064 					$$->location = @1;
12065 				}
12066 			| DEFAULT b_expr
12067 				{
12068 					$$ = makeNode(ResTarget);
12069 					$$->name = NULL;
12070 					$$->indirection = NIL;
12071 					$$->val = $2;
12072 					$$->location = @1;
12073 				}
12074 		;
12075 
12076 /*****************************************************************************
12077  *
12078  *	Type syntax
12079  *		SQL introduces a large amount of type-specific syntax.
12080  *		Define individual clauses to handle these cases, and use
12081  *		 the generic case to handle regular type-extensible Postgres syntax.
12082  *		- thomas 1997-10-10
12083  *
12084  *****************************************************************************/
12085 
12086 Typename:	SimpleTypename opt_array_bounds
12087 				{
12088 					$$ = $1;
12089 					$$->arrayBounds = $2;
12090 				}
12091 			| SETOF SimpleTypename opt_array_bounds
12092 				{
12093 					$$ = $2;
12094 					$$->arrayBounds = $3;
12095 					$$->setof = TRUE;
12096 				}
12097 			/* SQL standard syntax, currently only one-dimensional */
12098 			| SimpleTypename ARRAY '[' Iconst ']'
12099 				{
12100 					$$ = $1;
12101 					$$->arrayBounds = list_make1(makeInteger($4));
12102 				}
12103 			| SETOF SimpleTypename ARRAY '[' Iconst ']'
12104 				{
12105 					$$ = $2;
12106 					$$->arrayBounds = list_make1(makeInteger($5));
12107 					$$->setof = TRUE;
12108 				}
12109 			| SimpleTypename ARRAY
12110 				{
12111 					$$ = $1;
12112 					$$->arrayBounds = list_make1(makeInteger(-1));
12113 				}
12114 			| SETOF SimpleTypename ARRAY
12115 				{
12116 					$$ = $2;
12117 					$$->arrayBounds = list_make1(makeInteger(-1));
12118 					$$->setof = TRUE;
12119 				}
12120 		;
12121 
12122 opt_array_bounds:
12123 			opt_array_bounds '[' ']'
12124 					{  $$ = lappend($1, makeInteger(-1)); }
12125 			| opt_array_bounds '[' Iconst ']'
12126 					{  $$ = lappend($1, makeInteger($3)); }
12127 			| /*EMPTY*/
12128 					{  $$ = NIL; }
12129 		;
12130 
12131 SimpleTypename:
12132 			GenericType								{ $$ = $1; }
12133 			| Numeric								{ $$ = $1; }
12134 			| Bit									{ $$ = $1; }
12135 			| Character								{ $$ = $1; }
12136 			| ConstDatetime							{ $$ = $1; }
12137 			| ConstInterval opt_interval
12138 				{
12139 					$$ = $1;
12140 					$$->typmods = $2;
12141 				}
12142 			| ConstInterval '(' Iconst ')'
12143 				{
12144 					$$ = $1;
12145 					$$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
12146 											 makeIntConst($3, @3));
12147 				}
12148 		;
12149 
12150 /* We have a separate ConstTypename to allow defaulting fixed-length
12151  * types such as CHAR() and BIT() to an unspecified length.
12152  * SQL9x requires that these default to a length of one, but this
12153  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
12154  * where there is an obvious better choice to make.
12155  * Note that ConstInterval is not included here since it must
12156  * be pushed up higher in the rules to accommodate the postfix
12157  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
12158  * the generic-type-name case in AExprConst to avoid premature
12159  * reduce/reduce conflicts against function names.
12160  */
12161 ConstTypename:
12162 			Numeric									{ $$ = $1; }
12163 			| ConstBit								{ $$ = $1; }
12164 			| ConstCharacter						{ $$ = $1; }
12165 			| ConstDatetime							{ $$ = $1; }
12166 		;
12167 
12168 /*
12169  * GenericType covers all type names that don't have special syntax mandated
12170  * by the standard, including qualified names.  We also allow type modifiers.
12171  * To avoid parsing conflicts against function invocations, the modifiers
12172  * have to be shown as expr_list here, but parse analysis will only accept
12173  * constants for them.
12174  */
12175 GenericType:
12176 			type_function_name opt_type_modifiers
12177 				{
12178 					$$ = makeTypeName($1);
12179 					$$->typmods = $2;
12180 					$$->location = @1;
12181 				}
12182 			| type_function_name attrs opt_type_modifiers
12183 				{
12184 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
12185 					$$->typmods = $3;
12186 					$$->location = @1;
12187 				}
12188 		;
12189 
12190 opt_type_modifiers: '(' expr_list ')'				{ $$ = $2; }
12191 					| /* EMPTY */					{ $$ = NIL; }
12192 		;
12193 
12194 /*
12195  * SQL numeric data types
12196  */
12197 Numeric:	INT_P
12198 				{
12199 					$$ = SystemTypeName("int4");
12200 					$$->location = @1;
12201 				}
12202 			| INTEGER
12203 				{
12204 					$$ = SystemTypeName("int4");
12205 					$$->location = @1;
12206 				}
12207 			| SMALLINT
12208 				{
12209 					$$ = SystemTypeName("int2");
12210 					$$->location = @1;
12211 				}
12212 			| BIGINT
12213 				{
12214 					$$ = SystemTypeName("int8");
12215 					$$->location = @1;
12216 				}
12217 			| REAL
12218 				{
12219 					$$ = SystemTypeName("float4");
12220 					$$->location = @1;
12221 				}
12222 			| FLOAT_P opt_float
12223 				{
12224 					$$ = $2;
12225 					$$->location = @1;
12226 				}
12227 			| DOUBLE_P PRECISION
12228 				{
12229 					$$ = SystemTypeName("float8");
12230 					$$->location = @1;
12231 				}
12232 			| DECIMAL_P opt_type_modifiers
12233 				{
12234 					$$ = SystemTypeName("numeric");
12235 					$$->typmods = $2;
12236 					$$->location = @1;
12237 				}
12238 			| DEC opt_type_modifiers
12239 				{
12240 					$$ = SystemTypeName("numeric");
12241 					$$->typmods = $2;
12242 					$$->location = @1;
12243 				}
12244 			| NUMERIC opt_type_modifiers
12245 				{
12246 					$$ = SystemTypeName("numeric");
12247 					$$->typmods = $2;
12248 					$$->location = @1;
12249 				}
12250 			| BOOLEAN_P
12251 				{
12252 					$$ = SystemTypeName("bool");
12253 					$$->location = @1;
12254 				}
12255 		;
12256 
12257 opt_float:	'(' Iconst ')'
12258 				{
12259 					/*
12260 					 * Check FLOAT() precision limits assuming IEEE floating
12261 					 * types - thomas 1997-09-18
12262 					 */
12263 					if ($2 < 1)
12264 						ereport(ERROR,
12265 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12266 								 errmsg("precision for type float must be at least 1 bit"),
12267 								 parser_errposition(@2)));
12268 					else if ($2 <= 24)
12269 						$$ = SystemTypeName("float4");
12270 					else if ($2 <= 53)
12271 						$$ = SystemTypeName("float8");
12272 					else
12273 						ereport(ERROR,
12274 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12275 								 errmsg("precision for type float must be less than 54 bits"),
12276 								 parser_errposition(@2)));
12277 				}
12278 			| /*EMPTY*/
12279 				{
12280 					$$ = SystemTypeName("float8");
12281 				}
12282 		;
12283 
12284 /*
12285  * SQL bit-field data types
12286  * The following implements BIT() and BIT VARYING().
12287  */
12288 Bit:		BitWithLength
12289 				{
12290 					$$ = $1;
12291 				}
12292 			| BitWithoutLength
12293 				{
12294 					$$ = $1;
12295 				}
12296 		;
12297 
12298 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
12299 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
12300 ConstBit:	BitWithLength
12301 				{
12302 					$$ = $1;
12303 				}
12304 			| BitWithoutLength
12305 				{
12306 					$$ = $1;
12307 					$$->typmods = NIL;
12308 				}
12309 		;
12310 
12311 BitWithLength:
12312 			BIT opt_varying '(' expr_list ')'
12313 				{
12314 					char *typname;
12315 
12316 					typname = $2 ? "varbit" : "bit";
12317 					$$ = SystemTypeName(typname);
12318 					$$->typmods = $4;
12319 					$$->location = @1;
12320 				}
12321 		;
12322 
12323 BitWithoutLength:
12324 			BIT opt_varying
12325 				{
12326 					/* bit defaults to bit(1), varbit to no limit */
12327 					if ($2)
12328 					{
12329 						$$ = SystemTypeName("varbit");
12330 					}
12331 					else
12332 					{
12333 						$$ = SystemTypeName("bit");
12334 						$$->typmods = list_make1(makeIntConst(1, -1));
12335 					}
12336 					$$->location = @1;
12337 				}
12338 		;
12339 
12340 
12341 /*
12342  * SQL character data types
12343  * The following implements CHAR() and VARCHAR().
12344  */
12345 Character:  CharacterWithLength
12346 				{
12347 					$$ = $1;
12348 				}
12349 			| CharacterWithoutLength
12350 				{
12351 					$$ = $1;
12352 				}
12353 		;
12354 
12355 ConstCharacter:  CharacterWithLength
12356 				{
12357 					$$ = $1;
12358 				}
12359 			| CharacterWithoutLength
12360 				{
12361 					/* Length was not specified so allow to be unrestricted.
12362 					 * This handles problems with fixed-length (bpchar) strings
12363 					 * which in column definitions must default to a length
12364 					 * of one, but should not be constrained if the length
12365 					 * was not specified.
12366 					 */
12367 					$$ = $1;
12368 					$$->typmods = NIL;
12369 				}
12370 		;
12371 
12372 CharacterWithLength:  character '(' Iconst ')'
12373 				{
12374 					$$ = SystemTypeName($1);
12375 					$$->typmods = list_make1(makeIntConst($3, @3));
12376 					$$->location = @1;
12377 				}
12378 		;
12379 
12380 CharacterWithoutLength:	 character
12381 				{
12382 					$$ = SystemTypeName($1);
12383 					/* char defaults to char(1), varchar to no limit */
12384 					if (strcmp($1, "bpchar") == 0)
12385 						$$->typmods = list_make1(makeIntConst(1, -1));
12386 					$$->location = @1;
12387 				}
12388 		;
12389 
12390 character:	CHARACTER opt_varying
12391 										{ $$ = $2 ? "varchar": "bpchar"; }
12392 			| CHAR_P opt_varying
12393 										{ $$ = $2 ? "varchar": "bpchar"; }
12394 			| VARCHAR
12395 										{ $$ = "varchar"; }
12396 			| NATIONAL CHARACTER opt_varying
12397 										{ $$ = $3 ? "varchar": "bpchar"; }
12398 			| NATIONAL CHAR_P opt_varying
12399 										{ $$ = $3 ? "varchar": "bpchar"; }
12400 			| NCHAR opt_varying
12401 										{ $$ = $2 ? "varchar": "bpchar"; }
12402 		;
12403 
12404 opt_varying:
12405 			VARYING									{ $$ = TRUE; }
12406 			| /*EMPTY*/								{ $$ = FALSE; }
12407 		;
12408 
12409 /*
12410  * SQL date/time types
12411  */
12412 ConstDatetime:
12413 			TIMESTAMP '(' Iconst ')' opt_timezone
12414 				{
12415 					if ($5)
12416 						$$ = SystemTypeName("timestamptz");
12417 					else
12418 						$$ = SystemTypeName("timestamp");
12419 					$$->typmods = list_make1(makeIntConst($3, @3));
12420 					$$->location = @1;
12421 				}
12422 			| TIMESTAMP opt_timezone
12423 				{
12424 					if ($2)
12425 						$$ = SystemTypeName("timestamptz");
12426 					else
12427 						$$ = SystemTypeName("timestamp");
12428 					$$->location = @1;
12429 				}
12430 			| TIME '(' Iconst ')' opt_timezone
12431 				{
12432 					if ($5)
12433 						$$ = SystemTypeName("timetz");
12434 					else
12435 						$$ = SystemTypeName("time");
12436 					$$->typmods = list_make1(makeIntConst($3, @3));
12437 					$$->location = @1;
12438 				}
12439 			| TIME opt_timezone
12440 				{
12441 					if ($2)
12442 						$$ = SystemTypeName("timetz");
12443 					else
12444 						$$ = SystemTypeName("time");
12445 					$$->location = @1;
12446 				}
12447 		;
12448 
12449 ConstInterval:
12450 			INTERVAL
12451 				{
12452 					$$ = SystemTypeName("interval");
12453 					$$->location = @1;
12454 				}
12455 		;
12456 
12457 opt_timezone:
12458 			WITH_LA TIME ZONE						{ $$ = TRUE; }
12459 			| WITHOUT TIME ZONE						{ $$ = FALSE; }
12460 			| /*EMPTY*/								{ $$ = FALSE; }
12461 		;
12462 
12463 opt_interval:
12464 			YEAR_P
12465 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
12466 			| MONTH_P
12467 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
12468 			| DAY_P
12469 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
12470 			| HOUR_P
12471 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
12472 			| MINUTE_P
12473 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
12474 			| interval_second
12475 				{ $$ = $1; }
12476 			| YEAR_P TO MONTH_P
12477 				{
12478 					$$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
12479 												 INTERVAL_MASK(MONTH), @1));
12480 				}
12481 			| DAY_P TO HOUR_P
12482 				{
12483 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12484 												 INTERVAL_MASK(HOUR), @1));
12485 				}
12486 			| DAY_P TO MINUTE_P
12487 				{
12488 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12489 												 INTERVAL_MASK(HOUR) |
12490 												 INTERVAL_MASK(MINUTE), @1));
12491 				}
12492 			| DAY_P TO interval_second
12493 				{
12494 					$$ = $3;
12495 					linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
12496 												INTERVAL_MASK(HOUR) |
12497 												INTERVAL_MASK(MINUTE) |
12498 												INTERVAL_MASK(SECOND), @1);
12499 				}
12500 			| HOUR_P TO MINUTE_P
12501 				{
12502 					$$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
12503 												 INTERVAL_MASK(MINUTE), @1));
12504 				}
12505 			| HOUR_P TO interval_second
12506 				{
12507 					$$ = $3;
12508 					linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
12509 												INTERVAL_MASK(MINUTE) |
12510 												INTERVAL_MASK(SECOND), @1);
12511 				}
12512 			| MINUTE_P TO interval_second
12513 				{
12514 					$$ = $3;
12515 					linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
12516 												INTERVAL_MASK(SECOND), @1);
12517 				}
12518 			| /*EMPTY*/
12519 				{ $$ = NIL; }
12520 		;
12521 
12522 interval_second:
12523 			SECOND_P
12524 				{
12525 					$$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
12526 				}
12527 			| SECOND_P '(' Iconst ')'
12528 				{
12529 					$$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
12530 									makeIntConst($3, @3));
12531 				}
12532 		;
12533 
12534 
12535 /*****************************************************************************
12536  *
12537  *	expression grammar
12538  *
12539  *****************************************************************************/
12540 
12541 /*
12542  * General expressions
12543  * This is the heart of the expression syntax.
12544  *
12545  * We have two expression types: a_expr is the unrestricted kind, and
12546  * b_expr is a subset that must be used in some places to avoid shift/reduce
12547  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
12548  * because that use of AND conflicts with AND as a boolean operator.  So,
12549  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
12550  *
12551  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
12552  * always be used by surrounding it with parens.
12553  *
12554  * c_expr is all the productions that are common to a_expr and b_expr;
12555  * it's factored out just to eliminate redundant coding.
12556  *
12557  * Be careful of productions involving more than one terminal token.
12558  * By default, bison will assign such productions the precedence of their
12559  * last terminal, but in nearly all cases you want it to be the precedence
12560  * of the first terminal instead; otherwise you will not get the behavior
12561  * you expect!  So we use %prec annotations freely to set precedences.
12562  */
12563 a_expr:		c_expr									{ $$ = $1; }
12564 			| a_expr TYPECAST Typename
12565 					{ $$ = makeTypeCast($1, $3, @2); }
12566 			| a_expr COLLATE any_name
12567 				{
12568 					CollateClause *n = makeNode(CollateClause);
12569 					n->arg = $1;
12570 					n->collname = $3;
12571 					n->location = @2;
12572 					$$ = (Node *) n;
12573 				}
12574 			| a_expr AT TIME ZONE a_expr			%prec AT
12575 				{
12576 					$$ = (Node *) makeFuncCall(SystemFuncName("timezone"),
12577 											   list_make2($5, $1),
12578 											   @2);
12579 				}
12580 		/*
12581 		 * These operators must be called out explicitly in order to make use
12582 		 * of bison's automatic operator-precedence handling.  All other
12583 		 * operator names are handled by the generic productions using "Op",
12584 		 * below; and all those operators will have the same precedence.
12585 		 *
12586 		 * If you add more explicitly-known operators, be sure to add them
12587 		 * also to b_expr and to the MathOp list below.
12588 		 */
12589 			| '+' a_expr					%prec UMINUS
12590 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
12591 			| '-' a_expr					%prec UMINUS
12592 				{ $$ = doNegate($2, @1); }
12593 			| a_expr '+' a_expr
12594 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
12595 			| a_expr '-' a_expr
12596 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
12597 			| a_expr '*' a_expr
12598 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
12599 			| a_expr '/' a_expr
12600 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
12601 			| a_expr '%' a_expr
12602 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
12603 			| a_expr '^' a_expr
12604 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
12605 			| a_expr '<' a_expr
12606 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
12607 			| a_expr '>' a_expr
12608 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
12609 			| a_expr '=' a_expr
12610 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
12611 			| a_expr LESS_EQUALS a_expr
12612 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
12613 			| a_expr GREATER_EQUALS a_expr
12614 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
12615 			| a_expr NOT_EQUALS a_expr
12616 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
12617 
12618 			| a_expr qual_Op a_expr				%prec Op
12619 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
12620 			| qual_Op a_expr					%prec Op
12621 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
12622 			| a_expr qual_Op					%prec POSTFIXOP
12623 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
12624 
12625 			| a_expr AND a_expr
12626 				{ $$ = makeAndExpr($1, $3, @2); }
12627 			| a_expr OR a_expr
12628 				{ $$ = makeOrExpr($1, $3, @2); }
12629 			| NOT a_expr
12630 				{ $$ = makeNotExpr($2, @1); }
12631 			| NOT_LA a_expr						%prec NOT
12632 				{ $$ = makeNotExpr($2, @1); }
12633 
12634 			| a_expr LIKE a_expr
12635 				{
12636 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
12637 												   $1, $3, @2);
12638 				}
12639 			| a_expr LIKE a_expr ESCAPE a_expr					%prec LIKE
12640 				{
12641 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
12642 											   list_make2($3, $5),
12643 											   @2);
12644 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
12645 												   $1, (Node *) n, @2);
12646 				}
12647 			| a_expr NOT_LA LIKE a_expr							%prec NOT_LA
12648 				{
12649 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
12650 												   $1, $4, @2);
12651 				}
12652 			| a_expr NOT_LA LIKE a_expr ESCAPE a_expr			%prec NOT_LA
12653 				{
12654 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
12655 											   list_make2($4, $6),
12656 											   @2);
12657 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
12658 												   $1, (Node *) n, @2);
12659 				}
12660 			| a_expr ILIKE a_expr
12661 				{
12662 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
12663 												   $1, $3, @2);
12664 				}
12665 			| a_expr ILIKE a_expr ESCAPE a_expr					%prec ILIKE
12666 				{
12667 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
12668 											   list_make2($3, $5),
12669 											   @2);
12670 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
12671 												   $1, (Node *) n, @2);
12672 				}
12673 			| a_expr NOT_LA ILIKE a_expr						%prec NOT_LA
12674 				{
12675 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
12676 												   $1, $4, @2);
12677 				}
12678 			| a_expr NOT_LA ILIKE a_expr ESCAPE a_expr			%prec NOT_LA
12679 				{
12680 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
12681 											   list_make2($4, $6),
12682 											   @2);
12683 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
12684 												   $1, (Node *) n, @2);
12685 				}
12686 
12687 			| a_expr SIMILAR TO a_expr							%prec SIMILAR
12688 				{
12689 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
12690 											   list_make2($4, makeNullAConst(-1)),
12691 											   @2);
12692 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
12693 												   $1, (Node *) n, @2);
12694 				}
12695 			| a_expr SIMILAR TO a_expr ESCAPE a_expr			%prec SIMILAR
12696 				{
12697 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
12698 											   list_make2($4, $6),
12699 											   @2);
12700 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
12701 												   $1, (Node *) n, @2);
12702 				}
12703 			| a_expr NOT_LA SIMILAR TO a_expr					%prec NOT_LA
12704 				{
12705 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
12706 											   list_make2($5, makeNullAConst(-1)),
12707 											   @2);
12708 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
12709 												   $1, (Node *) n, @2);
12710 				}
12711 			| a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr		%prec NOT_LA
12712 				{
12713 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
12714 											   list_make2($5, $7),
12715 											   @2);
12716 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
12717 												   $1, (Node *) n, @2);
12718 				}
12719 
12720 			/* NullTest clause
12721 			 * Define SQL-style Null test clause.
12722 			 * Allow two forms described in the standard:
12723 			 *	a IS NULL
12724 			 *	a IS NOT NULL
12725 			 * Allow two SQL extensions
12726 			 *	a ISNULL
12727 			 *	a NOTNULL
12728 			 */
12729 			| a_expr IS NULL_P							%prec IS
12730 				{
12731 					NullTest *n = makeNode(NullTest);
12732 					n->arg = (Expr *) $1;
12733 					n->nulltesttype = IS_NULL;
12734 					n->location = @2;
12735 					$$ = (Node *)n;
12736 				}
12737 			| a_expr ISNULL
12738 				{
12739 					NullTest *n = makeNode(NullTest);
12740 					n->arg = (Expr *) $1;
12741 					n->nulltesttype = IS_NULL;
12742 					n->location = @2;
12743 					$$ = (Node *)n;
12744 				}
12745 			| a_expr IS NOT NULL_P						%prec IS
12746 				{
12747 					NullTest *n = makeNode(NullTest);
12748 					n->arg = (Expr *) $1;
12749 					n->nulltesttype = IS_NOT_NULL;
12750 					n->location = @2;
12751 					$$ = (Node *)n;
12752 				}
12753 			| a_expr NOTNULL
12754 				{
12755 					NullTest *n = makeNode(NullTest);
12756 					n->arg = (Expr *) $1;
12757 					n->nulltesttype = IS_NOT_NULL;
12758 					n->location = @2;
12759 					$$ = (Node *)n;
12760 				}
12761 			| row OVERLAPS row
12762 				{
12763 					if (list_length($1) != 2)
12764 						ereport(ERROR,
12765 								(errcode(ERRCODE_SYNTAX_ERROR),
12766 								 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
12767 								 parser_errposition(@1)));
12768 					if (list_length($3) != 2)
12769 						ereport(ERROR,
12770 								(errcode(ERRCODE_SYNTAX_ERROR),
12771 								 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
12772 								 parser_errposition(@3)));
12773 					$$ = (Node *) makeFuncCall(SystemFuncName("overlaps"),
12774 											   list_concat($1, $3),
12775 											   @2);
12776 				}
12777 			| a_expr IS TRUE_P							%prec IS
12778 				{
12779 					BooleanTest *b = makeNode(BooleanTest);
12780 					b->arg = (Expr *) $1;
12781 					b->booltesttype = IS_TRUE;
12782 					b->location = @2;
12783 					$$ = (Node *)b;
12784 				}
12785 			| a_expr IS NOT TRUE_P						%prec IS
12786 				{
12787 					BooleanTest *b = makeNode(BooleanTest);
12788 					b->arg = (Expr *) $1;
12789 					b->booltesttype = IS_NOT_TRUE;
12790 					b->location = @2;
12791 					$$ = (Node *)b;
12792 				}
12793 			| a_expr IS FALSE_P							%prec IS
12794 				{
12795 					BooleanTest *b = makeNode(BooleanTest);
12796 					b->arg = (Expr *) $1;
12797 					b->booltesttype = IS_FALSE;
12798 					b->location = @2;
12799 					$$ = (Node *)b;
12800 				}
12801 			| a_expr IS NOT FALSE_P						%prec IS
12802 				{
12803 					BooleanTest *b = makeNode(BooleanTest);
12804 					b->arg = (Expr *) $1;
12805 					b->booltesttype = IS_NOT_FALSE;
12806 					b->location = @2;
12807 					$$ = (Node *)b;
12808 				}
12809 			| a_expr IS UNKNOWN							%prec IS
12810 				{
12811 					BooleanTest *b = makeNode(BooleanTest);
12812 					b->arg = (Expr *) $1;
12813 					b->booltesttype = IS_UNKNOWN;
12814 					b->location = @2;
12815 					$$ = (Node *)b;
12816 				}
12817 			| a_expr IS NOT UNKNOWN						%prec IS
12818 				{
12819 					BooleanTest *b = makeNode(BooleanTest);
12820 					b->arg = (Expr *) $1;
12821 					b->booltesttype = IS_NOT_UNKNOWN;
12822 					b->location = @2;
12823 					$$ = (Node *)b;
12824 				}
12825 			| a_expr IS DISTINCT FROM a_expr			%prec IS
12826 				{
12827 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
12828 				}
12829 			| a_expr IS NOT DISTINCT FROM a_expr		%prec IS
12830 				{
12831 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
12832 				}
12833 			| a_expr IS OF '(' type_list ')'			%prec IS
12834 				{
12835 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
12836 				}
12837 			| a_expr IS NOT OF '(' type_list ')'		%prec IS
12838 				{
12839 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
12840 				}
12841 			| a_expr BETWEEN opt_asymmetric b_expr AND a_expr		%prec BETWEEN
12842 				{
12843 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN,
12844 												   "BETWEEN",
12845 												   $1,
12846 												   (Node *) list_make2($4, $6),
12847 												   @2);
12848 				}
12849 			| a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr %prec NOT_LA
12850 				{
12851 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN,
12852 												   "NOT BETWEEN",
12853 												   $1,
12854 												   (Node *) list_make2($5, $7),
12855 												   @2);
12856 				}
12857 			| a_expr BETWEEN SYMMETRIC b_expr AND a_expr			%prec BETWEEN
12858 				{
12859 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN_SYM,
12860 												   "BETWEEN SYMMETRIC",
12861 												   $1,
12862 												   (Node *) list_make2($4, $6),
12863 												   @2);
12864 				}
12865 			| a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr		%prec NOT_LA
12866 				{
12867 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN_SYM,
12868 												   "NOT BETWEEN SYMMETRIC",
12869 												   $1,
12870 												   (Node *) list_make2($5, $7),
12871 												   @2);
12872 				}
12873 			| a_expr IN_P in_expr
12874 				{
12875 					/* in_expr returns a SubLink or a list of a_exprs */
12876 					if (IsA($3, SubLink))
12877 					{
12878 						/* generate foo = ANY (subquery) */
12879 						SubLink *n = (SubLink *) $3;
12880 						n->subLinkType = ANY_SUBLINK;
12881 						n->subLinkId = 0;
12882 						n->testexpr = $1;
12883 						n->operName = NIL;		/* show it's IN not = ANY */
12884 						n->location = @2;
12885 						$$ = (Node *)n;
12886 					}
12887 					else
12888 					{
12889 						/* generate scalar IN expression */
12890 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
12891 					}
12892 				}
12893 			| a_expr NOT_LA IN_P in_expr						%prec NOT_LA
12894 				{
12895 					/* in_expr returns a SubLink or a list of a_exprs */
12896 					if (IsA($4, SubLink))
12897 					{
12898 						/* generate NOT (foo = ANY (subquery)) */
12899 						/* Make an = ANY node */
12900 						SubLink *n = (SubLink *) $4;
12901 						n->subLinkType = ANY_SUBLINK;
12902 						n->subLinkId = 0;
12903 						n->testexpr = $1;
12904 						n->operName = NIL;		/* show it's IN not = ANY */
12905 						n->location = @2;
12906 						/* Stick a NOT on top; must have same parse location */
12907 						$$ = makeNotExpr((Node *) n, @2);
12908 					}
12909 					else
12910 					{
12911 						/* generate scalar NOT IN expression */
12912 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
12913 					}
12914 				}
12915 			| a_expr subquery_Op sub_type select_with_parens	%prec Op
12916 				{
12917 					SubLink *n = makeNode(SubLink);
12918 					n->subLinkType = $3;
12919 					n->subLinkId = 0;
12920 					n->testexpr = $1;
12921 					n->operName = $2;
12922 					n->subselect = $4;
12923 					n->location = @2;
12924 					$$ = (Node *)n;
12925 				}
12926 			| a_expr subquery_Op sub_type '(' a_expr ')'		%prec Op
12927 				{
12928 					if ($3 == ANY_SUBLINK)
12929 						$$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
12930 					else
12931 						$$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
12932 				}
12933 			| UNIQUE select_with_parens
12934 				{
12935 					/* Not sure how to get rid of the parentheses
12936 					 * but there are lots of shift/reduce errors without them.
12937 					 *
12938 					 * Should be able to implement this by plopping the entire
12939 					 * select into a node, then transforming the target expressions
12940 					 * from whatever they are into count(*), and testing the
12941 					 * entire result equal to one.
12942 					 * But, will probably implement a separate node in the executor.
12943 					 */
12944 					ereport(ERROR,
12945 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
12946 							 errmsg("UNIQUE predicate is not yet implemented"),
12947 							 parser_errposition(@1)));
12948 				}
12949 			| a_expr IS DOCUMENT_P					%prec IS
12950 				{
12951 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12952 									 list_make1($1), @2);
12953 				}
12954 			| a_expr IS NOT DOCUMENT_P				%prec IS
12955 				{
12956 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12957 												 list_make1($1), @2),
12958 									 @2);
12959 				}
12960 			| DEFAULT
12961 				{
12962 					/*
12963 					 * The SQL spec only allows DEFAULT in "contextually typed
12964 					 * expressions", but for us, it's easier to allow it in
12965 					 * any a_expr and then throw error during parse analysis
12966 					 * if it's in an inappropriate context.  This way also
12967 					 * lets us say something smarter than "syntax error".
12968 					 */
12969 					SetToDefault *n = makeNode(SetToDefault);
12970 					/* parse analysis will fill in the rest */
12971 					n->location = @1;
12972 					$$ = (Node *)n;
12973 				}
12974 		;
12975 
12976 /*
12977  * Restricted expressions
12978  *
12979  * b_expr is a subset of the complete expression syntax defined by a_expr.
12980  *
12981  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
12982  * cause trouble in the places where b_expr is used.  For simplicity, we
12983  * just eliminate all the boolean-keyword-operator productions from b_expr.
12984  */
12985 b_expr:		c_expr
12986 				{ $$ = $1; }
12987 			| b_expr TYPECAST Typename
12988 				{ $$ = makeTypeCast($1, $3, @2); }
12989 			| '+' b_expr					%prec UMINUS
12990 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
12991 			| '-' b_expr					%prec UMINUS
12992 				{ $$ = doNegate($2, @1); }
12993 			| b_expr '+' b_expr
12994 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
12995 			| b_expr '-' b_expr
12996 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
12997 			| b_expr '*' b_expr
12998 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
12999 			| b_expr '/' b_expr
13000 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13001 			| b_expr '%' b_expr
13002 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13003 			| b_expr '^' b_expr
13004 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13005 			| b_expr '<' b_expr
13006 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13007 			| b_expr '>' b_expr
13008 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13009 			| b_expr '=' b_expr
13010 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13011 			| b_expr LESS_EQUALS b_expr
13012 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13013 			| b_expr GREATER_EQUALS b_expr
13014 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13015 			| b_expr NOT_EQUALS b_expr
13016 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13017 			| b_expr qual_Op b_expr				%prec Op
13018 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13019 			| qual_Op b_expr					%prec Op
13020 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13021 			| b_expr qual_Op					%prec POSTFIXOP
13022 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13023 			| b_expr IS DISTINCT FROM b_expr		%prec IS
13024 				{
13025 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13026 				}
13027 			| b_expr IS NOT DISTINCT FROM b_expr	%prec IS
13028 				{
13029 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13030 				}
13031 			| b_expr IS OF '(' type_list ')'		%prec IS
13032 				{
13033 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13034 				}
13035 			| b_expr IS NOT OF '(' type_list ')'	%prec IS
13036 				{
13037 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13038 				}
13039 			| b_expr IS DOCUMENT_P					%prec IS
13040 				{
13041 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13042 									 list_make1($1), @2);
13043 				}
13044 			| b_expr IS NOT DOCUMENT_P				%prec IS
13045 				{
13046 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13047 												 list_make1($1), @2),
13048 									 @2);
13049 				}
13050 		;
13051 
13052 /*
13053  * Productions that can be used in both a_expr and b_expr.
13054  *
13055  * Note: productions that refer recursively to a_expr or b_expr mostly
13056  * cannot appear here.	However, it's OK to refer to a_exprs that occur
13057  * inside parentheses, such as function arguments; that cannot introduce
13058  * ambiguity to the b_expr syntax.
13059  */
13060 c_expr:		columnref								{ $$ = $1; }
13061 			| AexprConst							{ $$ = $1; }
13062 			| PARAM opt_indirection
13063 				{
13064 					ParamRef *p = makeNode(ParamRef);
13065 					p->number = $1;
13066 					p->location = @1;
13067 					if ($2)
13068 					{
13069 						A_Indirection *n = makeNode(A_Indirection);
13070 						n->arg = (Node *) p;
13071 						n->indirection = check_indirection($2, yyscanner);
13072 						$$ = (Node *) n;
13073 					}
13074 					else
13075 						$$ = (Node *) p;
13076 				}
13077 			| '(' a_expr ')' opt_indirection
13078 				{
13079 					if ($4)
13080 					{
13081 						A_Indirection *n = makeNode(A_Indirection);
13082 						n->arg = $2;
13083 						n->indirection = check_indirection($4, yyscanner);
13084 						$$ = (Node *)n;
13085 					}
13086 					else if (operator_precedence_warning)
13087 					{
13088 						/*
13089 						 * If precedence warnings are enabled, insert
13090 						 * AEXPR_PAREN nodes wrapping all explicitly
13091 						 * parenthesized subexpressions; this prevents bogus
13092 						 * warnings from being issued when the ordering has
13093 						 * been forced by parentheses.  Take care that an
13094 						 * AEXPR_PAREN node has the same exprLocation as its
13095 						 * child, so as not to cause surprising changes in
13096 						 * error cursor positioning.
13097 						 *
13098 						 * In principle we should not be relying on a GUC to
13099 						 * decide whether to insert AEXPR_PAREN nodes.
13100 						 * However, since they have no effect except to
13101 						 * suppress warnings, it's probably safe enough; and
13102 						 * we'd just as soon not waste cycles on dummy parse
13103 						 * nodes if we don't have to.
13104 						 */
13105 						$$ = (Node *) makeA_Expr(AEXPR_PAREN, NIL, $2, NULL,
13106 												 exprLocation($2));
13107 					}
13108 					else
13109 						$$ = $2;
13110 				}
13111 			| case_expr
13112 				{ $$ = $1; }
13113 			| func_expr
13114 				{ $$ = $1; }
13115 			| select_with_parens			%prec UMINUS
13116 				{
13117 					SubLink *n = makeNode(SubLink);
13118 					n->subLinkType = EXPR_SUBLINK;
13119 					n->subLinkId = 0;
13120 					n->testexpr = NULL;
13121 					n->operName = NIL;
13122 					n->subselect = $1;
13123 					n->location = @1;
13124 					$$ = (Node *)n;
13125 				}
13126 			| select_with_parens indirection
13127 				{
13128 					/*
13129 					 * Because the select_with_parens nonterminal is designed
13130 					 * to "eat" as many levels of parens as possible, the
13131 					 * '(' a_expr ')' opt_indirection production above will
13132 					 * fail to match a sub-SELECT with indirection decoration;
13133 					 * the sub-SELECT won't be regarded as an a_expr as long
13134 					 * as there are parens around it.  To support applying
13135 					 * subscripting or field selection to a sub-SELECT result,
13136 					 * we need this redundant-looking production.
13137 					 */
13138 					SubLink *n = makeNode(SubLink);
13139 					A_Indirection *a = makeNode(A_Indirection);
13140 					n->subLinkType = EXPR_SUBLINK;
13141 					n->subLinkId = 0;
13142 					n->testexpr = NULL;
13143 					n->operName = NIL;
13144 					n->subselect = $1;
13145 					n->location = @1;
13146 					a->arg = (Node *)n;
13147 					a->indirection = check_indirection($2, yyscanner);
13148 					$$ = (Node *)a;
13149 				}
13150 			| EXISTS select_with_parens
13151 				{
13152 					SubLink *n = makeNode(SubLink);
13153 					n->subLinkType = EXISTS_SUBLINK;
13154 					n->subLinkId = 0;
13155 					n->testexpr = NULL;
13156 					n->operName = NIL;
13157 					n->subselect = $2;
13158 					n->location = @1;
13159 					$$ = (Node *)n;
13160 				}
13161 			| ARRAY select_with_parens
13162 				{
13163 					SubLink *n = makeNode(SubLink);
13164 					n->subLinkType = ARRAY_SUBLINK;
13165 					n->subLinkId = 0;
13166 					n->testexpr = NULL;
13167 					n->operName = NIL;
13168 					n->subselect = $2;
13169 					n->location = @1;
13170 					$$ = (Node *)n;
13171 				}
13172 			| ARRAY array_expr
13173 				{
13174 					A_ArrayExpr *n = castNode(A_ArrayExpr, $2);
13175 					/* point outermost A_ArrayExpr to the ARRAY keyword */
13176 					n->location = @1;
13177 					$$ = (Node *)n;
13178 				}
13179 			| explicit_row
13180 				{
13181 					RowExpr *r = makeNode(RowExpr);
13182 					r->args = $1;
13183 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13184 					r->colnames = NIL;	/* to be filled in during analysis */
13185 					r->row_format = COERCE_EXPLICIT_CALL; /* abuse */
13186 					r->location = @1;
13187 					$$ = (Node *)r;
13188 				}
13189 			| implicit_row
13190 				{
13191 					RowExpr *r = makeNode(RowExpr);
13192 					r->args = $1;
13193 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13194 					r->colnames = NIL;	/* to be filled in during analysis */
13195 					r->row_format = COERCE_IMPLICIT_CAST; /* abuse */
13196 					r->location = @1;
13197 					$$ = (Node *)r;
13198 				}
13199 			| GROUPING '(' expr_list ')'
13200 			  {
13201 				  GroupingFunc *g = makeNode(GroupingFunc);
13202 				  g->args = $3;
13203 				  g->location = @1;
13204 				  $$ = (Node *)g;
13205 			  }
13206 		;
13207 
13208 func_application: func_name '(' ')'
13209 				{
13210 					$$ = (Node *) makeFuncCall($1, NIL, @1);
13211 				}
13212 			| func_name '(' func_arg_list opt_sort_clause ')'
13213 				{
13214 					FuncCall *n = makeFuncCall($1, $3, @1);
13215 					n->agg_order = $4;
13216 					$$ = (Node *)n;
13217 				}
13218 			| func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'
13219 				{
13220 					FuncCall *n = makeFuncCall($1, list_make1($4), @1);
13221 					n->func_variadic = TRUE;
13222 					n->agg_order = $5;
13223 					$$ = (Node *)n;
13224 				}
13225 			| func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'
13226 				{
13227 					FuncCall *n = makeFuncCall($1, lappend($3, $6), @1);
13228 					n->func_variadic = TRUE;
13229 					n->agg_order = $7;
13230 					$$ = (Node *)n;
13231 				}
13232 			| func_name '(' ALL func_arg_list opt_sort_clause ')'
13233 				{
13234 					FuncCall *n = makeFuncCall($1, $4, @1);
13235 					n->agg_order = $5;
13236 					/* Ideally we'd mark the FuncCall node to indicate
13237 					 * "must be an aggregate", but there's no provision
13238 					 * for that in FuncCall at the moment.
13239 					 */
13240 					$$ = (Node *)n;
13241 				}
13242 			| func_name '(' DISTINCT func_arg_list opt_sort_clause ')'
13243 				{
13244 					FuncCall *n = makeFuncCall($1, $4, @1);
13245 					n->agg_order = $5;
13246 					n->agg_distinct = TRUE;
13247 					$$ = (Node *)n;
13248 				}
13249 			| func_name '(' '*' ')'
13250 				{
13251 					/*
13252 					 * We consider AGGREGATE(*) to invoke a parameterless
13253 					 * aggregate.  This does the right thing for COUNT(*),
13254 					 * and there are no other aggregates in SQL that accept
13255 					 * '*' as parameter.
13256 					 *
13257 					 * The FuncCall node is also marked agg_star = true,
13258 					 * so that later processing can detect what the argument
13259 					 * really was.
13260 					 */
13261 					FuncCall *n = makeFuncCall($1, NIL, @1);
13262 					n->agg_star = TRUE;
13263 					$$ = (Node *)n;
13264 				}
13265 		;
13266 
13267 
13268 /*
13269  * func_expr and its cousin func_expr_windowless are split out from c_expr just
13270  * so that we have classifications for "everything that is a function call or
13271  * looks like one".  This isn't very important, but it saves us having to
13272  * document which variants are legal in places like "FROM function()" or the
13273  * backwards-compatible functional-index syntax for CREATE INDEX.
13274  * (Note that many of the special SQL functions wouldn't actually make any
13275  * sense as functional index entries, but we ignore that consideration here.)
13276  */
13277 func_expr: func_application within_group_clause filter_clause over_clause
13278 				{
13279 					FuncCall *n = (FuncCall *) $1;
13280 					/*
13281 					 * The order clause for WITHIN GROUP and the one for
13282 					 * plain-aggregate ORDER BY share a field, so we have to
13283 					 * check here that at most one is present.  We also check
13284 					 * for DISTINCT and VARIADIC here to give a better error
13285 					 * location.  Other consistency checks are deferred to
13286 					 * parse analysis.
13287 					 */
13288 					if ($2 != NIL)
13289 					{
13290 						if (n->agg_order != NIL)
13291 							ereport(ERROR,
13292 									(errcode(ERRCODE_SYNTAX_ERROR),
13293 									 errmsg("cannot use multiple ORDER BY clauses with WITHIN GROUP"),
13294 									 parser_errposition(@2)));
13295 						if (n->agg_distinct)
13296 							ereport(ERROR,
13297 									(errcode(ERRCODE_SYNTAX_ERROR),
13298 									 errmsg("cannot use DISTINCT with WITHIN GROUP"),
13299 									 parser_errposition(@2)));
13300 						if (n->func_variadic)
13301 							ereport(ERROR,
13302 									(errcode(ERRCODE_SYNTAX_ERROR),
13303 									 errmsg("cannot use VARIADIC with WITHIN GROUP"),
13304 									 parser_errposition(@2)));
13305 						n->agg_order = $2;
13306 						n->agg_within_group = TRUE;
13307 					}
13308 					n->agg_filter = $3;
13309 					n->over = $4;
13310 					$$ = (Node *) n;
13311 				}
13312 			| func_expr_common_subexpr
13313 				{ $$ = $1; }
13314 		;
13315 
13316 /*
13317  * As func_expr but does not accept WINDOW functions directly
13318  * (but they can still be contained in arguments for functions etc).
13319  * Use this when window expressions are not allowed, where needed to
13320  * disambiguate the grammar (e.g. in CREATE INDEX).
13321  */
13322 func_expr_windowless:
13323 			func_application						{ $$ = $1; }
13324 			| func_expr_common_subexpr				{ $$ = $1; }
13325 		;
13326 
13327 /*
13328  * Special expressions that are considered to be functions.
13329  */
13330 func_expr_common_subexpr:
13331 			COLLATION FOR '(' a_expr ')'
13332 				{
13333 					$$ = (Node *) makeFuncCall(SystemFuncName("pg_collation_for"),
13334 											   list_make1($4),
13335 											   @1);
13336 				}
13337 			| CURRENT_DATE
13338 				{
13339 					$$ = makeSQLValueFunction(SVFOP_CURRENT_DATE, -1, @1);
13340 				}
13341 			| CURRENT_TIME
13342 				{
13343 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME, -1, @1);
13344 				}
13345 			| CURRENT_TIME '(' Iconst ')'
13346 				{
13347 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME_N, $3, @1);
13348 				}
13349 			| CURRENT_TIMESTAMP
13350 				{
13351 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP, -1, @1);
13352 				}
13353 			| CURRENT_TIMESTAMP '(' Iconst ')'
13354 				{
13355 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP_N, $3, @1);
13356 				}
13357 			| LOCALTIME
13358 				{
13359 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME, -1, @1);
13360 				}
13361 			| LOCALTIME '(' Iconst ')'
13362 				{
13363 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME_N, $3, @1);
13364 				}
13365 			| LOCALTIMESTAMP
13366 				{
13367 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP, -1, @1);
13368 				}
13369 			| LOCALTIMESTAMP '(' Iconst ')'
13370 				{
13371 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP_N, $3, @1);
13372 				}
13373 			| CURRENT_ROLE
13374 				{
13375 					$$ = makeSQLValueFunction(SVFOP_CURRENT_ROLE, -1, @1);
13376 				}
13377 			| CURRENT_USER
13378 				{
13379 					$$ = makeSQLValueFunction(SVFOP_CURRENT_USER, -1, @1);
13380 				}
13381 			| SESSION_USER
13382 				{
13383 					$$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1);
13384 				}
13385 			| USER
13386 				{
13387 					$$ = makeSQLValueFunction(SVFOP_USER, -1, @1);
13388 				}
13389 			| CURRENT_CATALOG
13390 				{
13391 					$$ = makeSQLValueFunction(SVFOP_CURRENT_CATALOG, -1, @1);
13392 				}
13393 			| CURRENT_SCHEMA
13394 				{
13395 					$$ = makeSQLValueFunction(SVFOP_CURRENT_SCHEMA, -1, @1);
13396 				}
13397 			| CAST '(' a_expr AS Typename ')'
13398 				{ $$ = makeTypeCast($3, $5, @1); }
13399 			| EXTRACT '(' extract_list ')'
13400 				{
13401 					$$ = (Node *) makeFuncCall(SystemFuncName("date_part"), $3, @1);
13402 				}
13403 			| OVERLAY '(' overlay_list ')'
13404 				{
13405 					/* overlay(A PLACING B FROM C FOR D) is converted to
13406 					 * overlay(A, B, C, D)
13407 					 * overlay(A PLACING B FROM C) is converted to
13408 					 * overlay(A, B, C)
13409 					 */
13410 					$$ = (Node *) makeFuncCall(SystemFuncName("overlay"), $3, @1);
13411 				}
13412 			| POSITION '(' position_list ')'
13413 				{
13414 					/* position(A in B) is converted to position(B, A) */
13415 					$$ = (Node *) makeFuncCall(SystemFuncName("position"), $3, @1);
13416 				}
13417 			| SUBSTRING '(' substr_list ')'
13418 				{
13419 					/* substring(A from B for C) is converted to
13420 					 * substring(A, B, C) - thomas 2000-11-28
13421 					 */
13422 					$$ = (Node *) makeFuncCall(SystemFuncName("substring"), $3, @1);
13423 				}
13424 			| TREAT '(' a_expr AS Typename ')'
13425 				{
13426 					/* TREAT(expr AS target) converts expr of a particular type to target,
13427 					 * which is defined to be a subtype of the original expression.
13428 					 * In SQL99, this is intended for use with structured UDTs,
13429 					 * but let's make this a generally useful form allowing stronger
13430 					 * coercions than are handled by implicit casting.
13431 					 *
13432 					 * Convert SystemTypeName() to SystemFuncName() even though
13433 					 * at the moment they result in the same thing.
13434 					 */
13435 					$$ = (Node *) makeFuncCall(SystemFuncName(((Value *)llast($5->names))->val.str),
13436 												list_make1($3),
13437 												@1);
13438 				}
13439 			| TRIM '(' BOTH trim_list ')'
13440 				{
13441 					/* various trim expressions are defined in SQL
13442 					 * - thomas 1997-07-19
13443 					 */
13444 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $4, @1);
13445 				}
13446 			| TRIM '(' LEADING trim_list ')'
13447 				{
13448 					$$ = (Node *) makeFuncCall(SystemFuncName("ltrim"), $4, @1);
13449 				}
13450 			| TRIM '(' TRAILING trim_list ')'
13451 				{
13452 					$$ = (Node *) makeFuncCall(SystemFuncName("rtrim"), $4, @1);
13453 				}
13454 			| TRIM '(' trim_list ')'
13455 				{
13456 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $3, @1);
13457 				}
13458 			| NULLIF '(' a_expr ',' a_expr ')'
13459 				{
13460 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
13461 				}
13462 			| COALESCE '(' expr_list ')'
13463 				{
13464 					CoalesceExpr *c = makeNode(CoalesceExpr);
13465 					c->args = $3;
13466 					c->location = @1;
13467 					$$ = (Node *)c;
13468 				}
13469 			| GREATEST '(' expr_list ')'
13470 				{
13471 					MinMaxExpr *v = makeNode(MinMaxExpr);
13472 					v->args = $3;
13473 					v->op = IS_GREATEST;
13474 					v->location = @1;
13475 					$$ = (Node *)v;
13476 				}
13477 			| LEAST '(' expr_list ')'
13478 				{
13479 					MinMaxExpr *v = makeNode(MinMaxExpr);
13480 					v->args = $3;
13481 					v->op = IS_LEAST;
13482 					v->location = @1;
13483 					$$ = (Node *)v;
13484 				}
13485 			| XMLCONCAT '(' expr_list ')'
13486 				{
13487 					$$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
13488 				}
13489 			| XMLELEMENT '(' NAME_P ColLabel ')'
13490 				{
13491 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
13492 				}
13493 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
13494 				{
13495 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
13496 				}
13497 			| XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
13498 				{
13499 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
13500 				}
13501 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
13502 				{
13503 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
13504 				}
13505 			| XMLEXISTS '(' c_expr xmlexists_argument ')'
13506 				{
13507 					/* xmlexists(A PASSING [BY REF] B [BY REF]) is
13508 					 * converted to xmlexists(A, B)*/
13509 					$$ = (Node *) makeFuncCall(SystemFuncName("xmlexists"), list_make2($3, $4), @1);
13510 				}
13511 			| XMLFOREST '(' xml_attribute_list ')'
13512 				{
13513 					$$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
13514 				}
13515 			| XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
13516 				{
13517 					XmlExpr *x = (XmlExpr *)
13518 						makeXmlExpr(IS_XMLPARSE, NULL, NIL,
13519 									list_make2($4, makeBoolAConst($5, -1)),
13520 									@1);
13521 					x->xmloption = $3;
13522 					$$ = (Node *)x;
13523 				}
13524 			| XMLPI '(' NAME_P ColLabel ')'
13525 				{
13526 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
13527 				}
13528 			| XMLPI '(' NAME_P ColLabel ',' a_expr ')'
13529 				{
13530 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
13531 				}
13532 			| XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
13533 				{
13534 					$$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
13535 									 list_make3($3, $5, $6), @1);
13536 				}
13537 			| XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
13538 				{
13539 					XmlSerialize *n = makeNode(XmlSerialize);
13540 					n->xmloption = $3;
13541 					n->expr = $4;
13542 					n->typeName = $6;
13543 					n->location = @1;
13544 					$$ = (Node *)n;
13545 				}
13546 		;
13547 
13548 /*
13549  * SQL/XML support
13550  */
13551 xml_root_version: VERSION_P a_expr
13552 				{ $$ = $2; }
13553 			| VERSION_P NO VALUE_P
13554 				{ $$ = makeNullAConst(-1); }
13555 		;
13556 
13557 opt_xml_root_standalone: ',' STANDALONE_P YES_P
13558 				{ $$ = makeIntConst(XML_STANDALONE_YES, -1); }
13559 			| ',' STANDALONE_P NO
13560 				{ $$ = makeIntConst(XML_STANDALONE_NO, -1); }
13561 			| ',' STANDALONE_P NO VALUE_P
13562 				{ $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
13563 			| /*EMPTY*/
13564 				{ $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
13565 		;
13566 
13567 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'	{ $$ = $3; }
13568 		;
13569 
13570 xml_attribute_list:	xml_attribute_el					{ $$ = list_make1($1); }
13571 			| xml_attribute_list ',' xml_attribute_el	{ $$ = lappend($1, $3); }
13572 		;
13573 
13574 xml_attribute_el: a_expr AS ColLabel
13575 				{
13576 					$$ = makeNode(ResTarget);
13577 					$$->name = $3;
13578 					$$->indirection = NIL;
13579 					$$->val = (Node *) $1;
13580 					$$->location = @1;
13581 				}
13582 			| a_expr
13583 				{
13584 					$$ = makeNode(ResTarget);
13585 					$$->name = NULL;
13586 					$$->indirection = NIL;
13587 					$$->val = (Node *) $1;
13588 					$$->location = @1;
13589 				}
13590 		;
13591 
13592 document_or_content: DOCUMENT_P						{ $$ = XMLOPTION_DOCUMENT; }
13593 			| CONTENT_P								{ $$ = XMLOPTION_CONTENT; }
13594 		;
13595 
13596 xml_whitespace_option: PRESERVE WHITESPACE_P		{ $$ = TRUE; }
13597 			| STRIP_P WHITESPACE_P					{ $$ = FALSE; }
13598 			| /*EMPTY*/								{ $$ = FALSE; }
13599 		;
13600 
13601 /* We allow several variants for SQL and other compatibility. */
13602 xmlexists_argument:
13603 			PASSING c_expr
13604 				{
13605 					$$ = $2;
13606 				}
13607 			| PASSING c_expr BY REF
13608 				{
13609 					$$ = $2;
13610 				}
13611 			| PASSING BY REF c_expr
13612 				{
13613 					$$ = $4;
13614 				}
13615 			| PASSING BY REF c_expr BY REF
13616 				{
13617 					$$ = $4;
13618 				}
13619 		;
13620 
13621 
13622 /*
13623  * Aggregate decoration clauses
13624  */
13625 within_group_clause:
13626 			WITHIN GROUP_P '(' sort_clause ')'		{ $$ = $4; }
13627 			| /*EMPTY*/								{ $$ = NIL; }
13628 		;
13629 
13630 filter_clause:
13631 			FILTER '(' WHERE a_expr ')'				{ $$ = $4; }
13632 			| /*EMPTY*/								{ $$ = NULL; }
13633 		;
13634 
13635 
13636 /*
13637  * Window Definitions
13638  */
13639 window_clause:
13640 			WINDOW window_definition_list			{ $$ = $2; }
13641 			| /*EMPTY*/								{ $$ = NIL; }
13642 		;
13643 
13644 window_definition_list:
13645 			window_definition						{ $$ = list_make1($1); }
13646 			| window_definition_list ',' window_definition
13647 													{ $$ = lappend($1, $3); }
13648 		;
13649 
13650 window_definition:
13651 			ColId AS window_specification
13652 				{
13653 					WindowDef *n = $3;
13654 					n->name = $1;
13655 					$$ = n;
13656 				}
13657 		;
13658 
13659 over_clause: OVER window_specification
13660 				{ $$ = $2; }
13661 			| OVER ColId
13662 				{
13663 					WindowDef *n = makeNode(WindowDef);
13664 					n->name = $2;
13665 					n->refname = NULL;
13666 					n->partitionClause = NIL;
13667 					n->orderClause = NIL;
13668 					n->frameOptions = FRAMEOPTION_DEFAULTS;
13669 					n->startOffset = NULL;
13670 					n->endOffset = NULL;
13671 					n->location = @2;
13672 					$$ = n;
13673 				}
13674 			| /*EMPTY*/
13675 				{ $$ = NULL; }
13676 		;
13677 
13678 window_specification: '(' opt_existing_window_name opt_partition_clause
13679 						opt_sort_clause opt_frame_clause ')'
13680 				{
13681 					WindowDef *n = makeNode(WindowDef);
13682 					n->name = NULL;
13683 					n->refname = $2;
13684 					n->partitionClause = $3;
13685 					n->orderClause = $4;
13686 					/* copy relevant fields of opt_frame_clause */
13687 					n->frameOptions = $5->frameOptions;
13688 					n->startOffset = $5->startOffset;
13689 					n->endOffset = $5->endOffset;
13690 					n->location = @1;
13691 					$$ = n;
13692 				}
13693 		;
13694 
13695 /*
13696  * If we see PARTITION, RANGE, or ROWS as the first token after the '('
13697  * of a window_specification, we want the assumption to be that there is
13698  * no existing_window_name; but those keywords are unreserved and so could
13699  * be ColIds.  We fix this by making them have the same precedence as IDENT
13700  * and giving the empty production here a slightly higher precedence, so
13701  * that the shift/reduce conflict is resolved in favor of reducing the rule.
13702  * These keywords are thus precluded from being an existing_window_name but
13703  * are not reserved for any other purpose.
13704  */
13705 opt_existing_window_name: ColId						{ $$ = $1; }
13706 			| /*EMPTY*/				%prec Op		{ $$ = NULL; }
13707 		;
13708 
13709 opt_partition_clause: PARTITION BY expr_list		{ $$ = $3; }
13710 			| /*EMPTY*/								{ $$ = NIL; }
13711 		;
13712 
13713 /*
13714  * For frame clauses, we return a WindowDef, but only some fields are used:
13715  * frameOptions, startOffset, and endOffset.
13716  *
13717  * This is only a subset of the full SQL:2008 frame_clause grammar.
13718  * We don't support <window frame exclusion> yet.
13719  */
13720 opt_frame_clause:
13721 			RANGE frame_extent
13722 				{
13723 					WindowDef *n = $2;
13724 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
13725 					if (n->frameOptions & (FRAMEOPTION_START_VALUE_PRECEDING |
13726 										   FRAMEOPTION_END_VALUE_PRECEDING))
13727 						ereport(ERROR,
13728 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13729 								 errmsg("RANGE PRECEDING is only supported with UNBOUNDED"),
13730 								 parser_errposition(@1)));
13731 					if (n->frameOptions & (FRAMEOPTION_START_VALUE_FOLLOWING |
13732 										   FRAMEOPTION_END_VALUE_FOLLOWING))
13733 						ereport(ERROR,
13734 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13735 								 errmsg("RANGE FOLLOWING is only supported with UNBOUNDED"),
13736 								 parser_errposition(@1)));
13737 					$$ = n;
13738 				}
13739 			| ROWS frame_extent
13740 				{
13741 					WindowDef *n = $2;
13742 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
13743 					$$ = n;
13744 				}
13745 			| /*EMPTY*/
13746 				{
13747 					WindowDef *n = makeNode(WindowDef);
13748 					n->frameOptions = FRAMEOPTION_DEFAULTS;
13749 					n->startOffset = NULL;
13750 					n->endOffset = NULL;
13751 					$$ = n;
13752 				}
13753 		;
13754 
13755 frame_extent: frame_bound
13756 				{
13757 					WindowDef *n = $1;
13758 					/* reject invalid cases */
13759 					if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
13760 						ereport(ERROR,
13761 								(errcode(ERRCODE_WINDOWING_ERROR),
13762 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
13763 								 parser_errposition(@1)));
13764 					if (n->frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING)
13765 						ereport(ERROR,
13766 								(errcode(ERRCODE_WINDOWING_ERROR),
13767 								 errmsg("frame starting from following row cannot end with current row"),
13768 								 parser_errposition(@1)));
13769 					n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
13770 					$$ = n;
13771 				}
13772 			| BETWEEN frame_bound AND frame_bound
13773 				{
13774 					WindowDef *n1 = $2;
13775 					WindowDef *n2 = $4;
13776 					/* form merged options */
13777 					int		frameOptions = n1->frameOptions;
13778 					/* shift converts START_ options to END_ options */
13779 					frameOptions |= n2->frameOptions << 1;
13780 					frameOptions |= FRAMEOPTION_BETWEEN;
13781 					/* reject invalid cases */
13782 					if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
13783 						ereport(ERROR,
13784 								(errcode(ERRCODE_WINDOWING_ERROR),
13785 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
13786 								 parser_errposition(@2)));
13787 					if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
13788 						ereport(ERROR,
13789 								(errcode(ERRCODE_WINDOWING_ERROR),
13790 								 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
13791 								 parser_errposition(@4)));
13792 					if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
13793 						(frameOptions & FRAMEOPTION_END_VALUE_PRECEDING))
13794 						ereport(ERROR,
13795 								(errcode(ERRCODE_WINDOWING_ERROR),
13796 								 errmsg("frame starting from current row cannot have preceding rows"),
13797 								 parser_errposition(@4)));
13798 					if ((frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING) &&
13799 						(frameOptions & (FRAMEOPTION_END_VALUE_PRECEDING |
13800 										 FRAMEOPTION_END_CURRENT_ROW)))
13801 						ereport(ERROR,
13802 								(errcode(ERRCODE_WINDOWING_ERROR),
13803 								 errmsg("frame starting from following row cannot have preceding rows"),
13804 								 parser_errposition(@4)));
13805 					n1->frameOptions = frameOptions;
13806 					n1->endOffset = n2->startOffset;
13807 					$$ = n1;
13808 				}
13809 		;
13810 
13811 /*
13812  * This is used for both frame start and frame end, with output set up on
13813  * the assumption it's frame start; the frame_extent productions must reject
13814  * invalid cases.
13815  */
13816 frame_bound:
13817 			UNBOUNDED PRECEDING
13818 				{
13819 					WindowDef *n = makeNode(WindowDef);
13820 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
13821 					n->startOffset = NULL;
13822 					n->endOffset = NULL;
13823 					$$ = n;
13824 				}
13825 			| UNBOUNDED FOLLOWING
13826 				{
13827 					WindowDef *n = makeNode(WindowDef);
13828 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
13829 					n->startOffset = NULL;
13830 					n->endOffset = NULL;
13831 					$$ = n;
13832 				}
13833 			| CURRENT_P ROW
13834 				{
13835 					WindowDef *n = makeNode(WindowDef);
13836 					n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
13837 					n->startOffset = NULL;
13838 					n->endOffset = NULL;
13839 					$$ = n;
13840 				}
13841 			| a_expr PRECEDING
13842 				{
13843 					WindowDef *n = makeNode(WindowDef);
13844 					n->frameOptions = FRAMEOPTION_START_VALUE_PRECEDING;
13845 					n->startOffset = $1;
13846 					n->endOffset = NULL;
13847 					$$ = n;
13848 				}
13849 			| a_expr FOLLOWING
13850 				{
13851 					WindowDef *n = makeNode(WindowDef);
13852 					n->frameOptions = FRAMEOPTION_START_VALUE_FOLLOWING;
13853 					n->startOffset = $1;
13854 					n->endOffset = NULL;
13855 					$$ = n;
13856 				}
13857 		;
13858 
13859 
13860 /*
13861  * Supporting nonterminals for expressions.
13862  */
13863 
13864 /* Explicit row production.
13865  *
13866  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
13867  * without conflicting with the parenthesized a_expr production.  Without the
13868  * ROW keyword, there must be more than one a_expr inside the parens.
13869  */
13870 row:		ROW '(' expr_list ')'					{ $$ = $3; }
13871 			| ROW '(' ')'							{ $$ = NIL; }
13872 			| '(' expr_list ',' a_expr ')'			{ $$ = lappend($2, $4); }
13873 		;
13874 
13875 explicit_row:	ROW '(' expr_list ')'				{ $$ = $3; }
13876 			| ROW '(' ')'							{ $$ = NIL; }
13877 		;
13878 
13879 implicit_row:	'(' expr_list ',' a_expr ')'		{ $$ = lappend($2, $4); }
13880 		;
13881 
13882 sub_type:	ANY										{ $$ = ANY_SUBLINK; }
13883 			| SOME									{ $$ = ANY_SUBLINK; }
13884 			| ALL									{ $$ = ALL_SUBLINK; }
13885 		;
13886 
13887 all_Op:		Op										{ $$ = $1; }
13888 			| MathOp								{ $$ = $1; }
13889 		;
13890 
13891 MathOp:		 '+'									{ $$ = "+"; }
13892 			| '-'									{ $$ = "-"; }
13893 			| '*'									{ $$ = "*"; }
13894 			| '/'									{ $$ = "/"; }
13895 			| '%'									{ $$ = "%"; }
13896 			| '^'									{ $$ = "^"; }
13897 			| '<'									{ $$ = "<"; }
13898 			| '>'									{ $$ = ">"; }
13899 			| '='									{ $$ = "="; }
13900 			| LESS_EQUALS							{ $$ = "<="; }
13901 			| GREATER_EQUALS						{ $$ = ">="; }
13902 			| NOT_EQUALS							{ $$ = "<>"; }
13903 		;
13904 
13905 qual_Op:	Op
13906 					{ $$ = list_make1(makeString($1)); }
13907 			| OPERATOR '(' any_operator ')'
13908 					{ $$ = $3; }
13909 		;
13910 
13911 qual_all_Op:
13912 			all_Op
13913 					{ $$ = list_make1(makeString($1)); }
13914 			| OPERATOR '(' any_operator ')'
13915 					{ $$ = $3; }
13916 		;
13917 
13918 subquery_Op:
13919 			all_Op
13920 					{ $$ = list_make1(makeString($1)); }
13921 			| OPERATOR '(' any_operator ')'
13922 					{ $$ = $3; }
13923 			| LIKE
13924 					{ $$ = list_make1(makeString("~~")); }
13925 			| NOT_LA LIKE
13926 					{ $$ = list_make1(makeString("!~~")); }
13927 			| ILIKE
13928 					{ $$ = list_make1(makeString("~~*")); }
13929 			| NOT_LA ILIKE
13930 					{ $$ = list_make1(makeString("!~~*")); }
13931 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
13932  * the regular expression is preprocessed by a function (similar_escape),
13933  * and the ~ operator for posix regular expressions is used.
13934  *        x SIMILAR TO y     ->    x ~ similar_escape(y)
13935  * this transformation is made on the fly by the parser upwards.
13936  * however the SubLink structure which handles any/some/all stuff
13937  * is not ready for such a thing.
13938  */
13939 			;
13940 
13941 expr_list:	a_expr
13942 				{
13943 					$$ = list_make1($1);
13944 				}
13945 			| expr_list ',' a_expr
13946 				{
13947 					$$ = lappend($1, $3);
13948 				}
13949 		;
13950 
13951 /* function arguments can have names */
13952 func_arg_list:  func_arg_expr
13953 				{
13954 					$$ = list_make1($1);
13955 				}
13956 			| func_arg_list ',' func_arg_expr
13957 				{
13958 					$$ = lappend($1, $3);
13959 				}
13960 		;
13961 
13962 func_arg_expr:  a_expr
13963 				{
13964 					$$ = $1;
13965 				}
13966 			| param_name COLON_EQUALS a_expr
13967 				{
13968 					NamedArgExpr *na = makeNode(NamedArgExpr);
13969 					na->name = $1;
13970 					na->arg = (Expr *) $3;
13971 					na->argnumber = -1;		/* until determined */
13972 					na->location = @1;
13973 					$$ = (Node *) na;
13974 				}
13975 			| param_name EQUALS_GREATER a_expr
13976 				{
13977 					NamedArgExpr *na = makeNode(NamedArgExpr);
13978 					na->name = $1;
13979 					na->arg = (Expr *) $3;
13980 					na->argnumber = -1;		/* until determined */
13981 					na->location = @1;
13982 					$$ = (Node *) na;
13983 				}
13984 		;
13985 
13986 type_list:	Typename								{ $$ = list_make1($1); }
13987 			| type_list ',' Typename				{ $$ = lappend($1, $3); }
13988 		;
13989 
13990 array_expr: '[' expr_list ']'
13991 				{
13992 					$$ = makeAArrayExpr($2, @1);
13993 				}
13994 			| '[' array_expr_list ']'
13995 				{
13996 					$$ = makeAArrayExpr($2, @1);
13997 				}
13998 			| '[' ']'
13999 				{
14000 					$$ = makeAArrayExpr(NIL, @1);
14001 				}
14002 		;
14003 
14004 array_expr_list: array_expr							{ $$ = list_make1($1); }
14005 			| array_expr_list ',' array_expr		{ $$ = lappend($1, $3); }
14006 		;
14007 
14008 
14009 extract_list:
14010 			extract_arg FROM a_expr
14011 				{
14012 					$$ = list_make2(makeStringConst($1, @1), $3);
14013 				}
14014 			| /*EMPTY*/								{ $$ = NIL; }
14015 		;
14016 
14017 /* Allow delimited string Sconst in extract_arg as an SQL extension.
14018  * - thomas 2001-04-12
14019  */
14020 extract_arg:
14021 			IDENT									{ $$ = $1; }
14022 			| YEAR_P								{ $$ = "year"; }
14023 			| MONTH_P								{ $$ = "month"; }
14024 			| DAY_P									{ $$ = "day"; }
14025 			| HOUR_P								{ $$ = "hour"; }
14026 			| MINUTE_P								{ $$ = "minute"; }
14027 			| SECOND_P								{ $$ = "second"; }
14028 			| Sconst								{ $$ = $1; }
14029 		;
14030 
14031 /* OVERLAY() arguments
14032  * SQL99 defines the OVERLAY() function:
14033  * o overlay(text placing text from int for int)
14034  * o overlay(text placing text from int)
14035  * and similarly for binary strings
14036  */
14037 overlay_list:
14038 			a_expr overlay_placing substr_from substr_for
14039 				{
14040 					$$ = list_make4($1, $2, $3, $4);
14041 				}
14042 			| a_expr overlay_placing substr_from
14043 				{
14044 					$$ = list_make3($1, $2, $3);
14045 				}
14046 		;
14047 
14048 overlay_placing:
14049 			PLACING a_expr
14050 				{ $$ = $2; }
14051 		;
14052 
14053 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
14054 
14055 position_list:
14056 			b_expr IN_P b_expr						{ $$ = list_make2($3, $1); }
14057 			| /*EMPTY*/								{ $$ = NIL; }
14058 		;
14059 
14060 /* SUBSTRING() arguments
14061  * SQL9x defines a specific syntax for arguments to SUBSTRING():
14062  * o substring(text from int for int)
14063  * o substring(text from int) get entire string from starting point "int"
14064  * o substring(text for int) get first "int" characters of string
14065  * o substring(text from pattern) get entire string matching pattern
14066  * o substring(text from pattern for escape) same with specified escape char
14067  * We also want to support generic substring functions which accept
14068  * the usual generic list of arguments. So we will accept both styles
14069  * here, and convert the SQL9x style to the generic list for further
14070  * processing. - thomas 2000-11-28
14071  */
14072 substr_list:
14073 			a_expr substr_from substr_for
14074 				{
14075 					$$ = list_make3($1, $2, $3);
14076 				}
14077 			| a_expr substr_for substr_from
14078 				{
14079 					/* not legal per SQL99, but might as well allow it */
14080 					$$ = list_make3($1, $3, $2);
14081 				}
14082 			| a_expr substr_from
14083 				{
14084 					$$ = list_make2($1, $2);
14085 				}
14086 			| a_expr substr_for
14087 				{
14088 					/*
14089 					 * Since there are no cases where this syntax allows
14090 					 * a textual FOR value, we forcibly cast the argument
14091 					 * to int4.  The possible matches in pg_proc are
14092 					 * substring(text,int4) and substring(text,text),
14093 					 * and we don't want the parser to choose the latter,
14094 					 * which it is likely to do if the second argument
14095 					 * is unknown or doesn't have an implicit cast to int4.
14096 					 */
14097 					$$ = list_make3($1, makeIntConst(1, -1),
14098 									makeTypeCast($2,
14099 												 SystemTypeName("int4"), -1));
14100 				}
14101 			| expr_list
14102 				{
14103 					$$ = $1;
14104 				}
14105 			| /*EMPTY*/
14106 				{ $$ = NIL; }
14107 		;
14108 
14109 substr_from:
14110 			FROM a_expr								{ $$ = $2; }
14111 		;
14112 
14113 substr_for: FOR a_expr								{ $$ = $2; }
14114 		;
14115 
14116 trim_list:	a_expr FROM expr_list					{ $$ = lappend($3, $1); }
14117 			| FROM expr_list						{ $$ = $2; }
14118 			| expr_list								{ $$ = $1; }
14119 		;
14120 
14121 in_expr:	select_with_parens
14122 				{
14123 					SubLink *n = makeNode(SubLink);
14124 					n->subselect = $1;
14125 					/* other fields will be filled later */
14126 					$$ = (Node *)n;
14127 				}
14128 			| '(' expr_list ')'						{ $$ = (Node *)$2; }
14129 		;
14130 
14131 /*
14132  * Define SQL-style CASE clause.
14133  * - Full specification
14134  *	CASE WHEN a = b THEN c ... ELSE d END
14135  * - Implicit argument
14136  *	CASE a WHEN b THEN c ... ELSE d END
14137  */
14138 case_expr:	CASE case_arg when_clause_list case_default END_P
14139 				{
14140 					CaseExpr *c = makeNode(CaseExpr);
14141 					c->casetype = InvalidOid; /* not analyzed yet */
14142 					c->arg = (Expr *) $2;
14143 					c->args = $3;
14144 					c->defresult = (Expr *) $4;
14145 					c->location = @1;
14146 					$$ = (Node *)c;
14147 				}
14148 		;
14149 
14150 when_clause_list:
14151 			/* There must be at least one */
14152 			when_clause								{ $$ = list_make1($1); }
14153 			| when_clause_list when_clause			{ $$ = lappend($1, $2); }
14154 		;
14155 
14156 when_clause:
14157 			WHEN a_expr THEN a_expr
14158 				{
14159 					CaseWhen *w = makeNode(CaseWhen);
14160 					w->expr = (Expr *) $2;
14161 					w->result = (Expr *) $4;
14162 					w->location = @1;
14163 					$$ = (Node *)w;
14164 				}
14165 		;
14166 
14167 case_default:
14168 			ELSE a_expr								{ $$ = $2; }
14169 			| /*EMPTY*/								{ $$ = NULL; }
14170 		;
14171 
14172 case_arg:	a_expr									{ $$ = $1; }
14173 			| /*EMPTY*/								{ $$ = NULL; }
14174 		;
14175 
14176 columnref:	ColId
14177 				{
14178 					$$ = makeColumnRef($1, NIL, @1, yyscanner);
14179 				}
14180 			| ColId indirection
14181 				{
14182 					$$ = makeColumnRef($1, $2, @1, yyscanner);
14183 				}
14184 		;
14185 
14186 indirection_el:
14187 			'.' attr_name
14188 				{
14189 					$$ = (Node *) makeString($2);
14190 				}
14191 			| '.' '*'
14192 				{
14193 					$$ = (Node *) makeNode(A_Star);
14194 				}
14195 			| '[' a_expr ']'
14196 				{
14197 					A_Indices *ai = makeNode(A_Indices);
14198 					ai->is_slice = false;
14199 					ai->lidx = NULL;
14200 					ai->uidx = $2;
14201 					$$ = (Node *) ai;
14202 				}
14203 			| '[' opt_slice_bound ':' opt_slice_bound ']'
14204 				{
14205 					A_Indices *ai = makeNode(A_Indices);
14206 					ai->is_slice = true;
14207 					ai->lidx = $2;
14208 					ai->uidx = $4;
14209 					$$ = (Node *) ai;
14210 				}
14211 		;
14212 
14213 opt_slice_bound:
14214 			a_expr									{ $$ = $1; }
14215 			| /*EMPTY*/								{ $$ = NULL; }
14216 		;
14217 
14218 indirection:
14219 			indirection_el							{ $$ = list_make1($1); }
14220 			| indirection indirection_el			{ $$ = lappend($1, $2); }
14221 		;
14222 
14223 opt_indirection:
14224 			/*EMPTY*/								{ $$ = NIL; }
14225 			| opt_indirection indirection_el		{ $$ = lappend($1, $2); }
14226 		;
14227 
14228 opt_asymmetric: ASYMMETRIC
14229 			| /*EMPTY*/
14230 		;
14231 
14232 
14233 /*****************************************************************************
14234  *
14235  *	target list for SELECT
14236  *
14237  *****************************************************************************/
14238 
14239 opt_target_list: target_list						{ $$ = $1; }
14240 			| /* EMPTY */							{ $$ = NIL; }
14241 		;
14242 
14243 target_list:
14244 			target_el								{ $$ = list_make1($1); }
14245 			| target_list ',' target_el				{ $$ = lappend($1, $3); }
14246 		;
14247 
14248 target_el:	a_expr AS ColLabel
14249 				{
14250 					$$ = makeNode(ResTarget);
14251 					$$->name = $3;
14252 					$$->indirection = NIL;
14253 					$$->val = (Node *)$1;
14254 					$$->location = @1;
14255 				}
14256 			/*
14257 			 * We support omitting AS only for column labels that aren't
14258 			 * any known keyword.  There is an ambiguity against postfix
14259 			 * operators: is "a ! b" an infix expression, or a postfix
14260 			 * expression and a column label?  We prefer to resolve this
14261 			 * as an infix expression, which we accomplish by assigning
14262 			 * IDENT a precedence higher than POSTFIXOP.
14263 			 */
14264 			| a_expr IDENT
14265 				{
14266 					$$ = makeNode(ResTarget);
14267 					$$->name = $2;
14268 					$$->indirection = NIL;
14269 					$$->val = (Node *)$1;
14270 					$$->location = @1;
14271 				}
14272 			| a_expr
14273 				{
14274 					$$ = makeNode(ResTarget);
14275 					$$->name = NULL;
14276 					$$->indirection = NIL;
14277 					$$->val = (Node *)$1;
14278 					$$->location = @1;
14279 				}
14280 			| '*'
14281 				{
14282 					ColumnRef *n = makeNode(ColumnRef);
14283 					n->fields = list_make1(makeNode(A_Star));
14284 					n->location = @1;
14285 
14286 					$$ = makeNode(ResTarget);
14287 					$$->name = NULL;
14288 					$$->indirection = NIL;
14289 					$$->val = (Node *)n;
14290 					$$->location = @1;
14291 				}
14292 		;
14293 
14294 
14295 /*****************************************************************************
14296  *
14297  *	Names and constants
14298  *
14299  *****************************************************************************/
14300 
14301 qualified_name_list:
14302 			qualified_name							{ $$ = list_make1($1); }
14303 			| qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
14304 		;
14305 
14306 /*
14307  * The production for a qualified relation name has to exactly match the
14308  * production for a qualified func_name, because in a FROM clause we cannot
14309  * tell which we are parsing until we see what comes after it ('(' for a
14310  * func_name, something else for a relation). Therefore we allow 'indirection'
14311  * which may contain subscripts, and reject that case in the C code.
14312  */
14313 qualified_name:
14314 			ColId
14315 				{
14316 					$$ = makeRangeVar(NULL, $1, @1);
14317 				}
14318 			| ColId indirection
14319 				{
14320 					check_qualified_name($2, yyscanner);
14321 					$$ = makeRangeVar(NULL, NULL, @1);
14322 					switch (list_length($2))
14323 					{
14324 						case 1:
14325 							$$->catalogname = NULL;
14326 							$$->schemaname = $1;
14327 							$$->relname = strVal(linitial($2));
14328 							break;
14329 						case 2:
14330 							$$->catalogname = $1;
14331 							$$->schemaname = strVal(linitial($2));
14332 							$$->relname = strVal(lsecond($2));
14333 							break;
14334 						default:
14335 							ereport(ERROR,
14336 									(errcode(ERRCODE_SYNTAX_ERROR),
14337 									 errmsg("improper qualified name (too many dotted names): %s",
14338 											NameListToString(lcons(makeString($1), $2))),
14339 									 parser_errposition(@1)));
14340 							break;
14341 					}
14342 				}
14343 		;
14344 
14345 name_list:	name
14346 					{ $$ = list_make1(makeString($1)); }
14347 			| name_list ',' name
14348 					{ $$ = lappend($1, makeString($3)); }
14349 		;
14350 
14351 
14352 name:		ColId									{ $$ = $1; };
14353 
14354 database_name:
14355 			ColId									{ $$ = $1; };
14356 
14357 access_method:
14358 			ColId									{ $$ = $1; };
14359 
14360 attr_name:	ColLabel								{ $$ = $1; };
14361 
14362 index_name: ColId									{ $$ = $1; };
14363 
14364 file_name:	Sconst									{ $$ = $1; };
14365 
14366 /*
14367  * The production for a qualified func_name has to exactly match the
14368  * production for a qualified columnref, because we cannot tell which we
14369  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
14370  * anything else for a columnref).  Therefore we allow 'indirection' which
14371  * may contain subscripts, and reject that case in the C code.  (If we
14372  * ever implement SQL99-like methods, such syntax may actually become legal!)
14373  */
14374 func_name:	type_function_name
14375 					{ $$ = list_make1(makeString($1)); }
14376 			| ColId indirection
14377 					{
14378 						$$ = check_func_name(lcons(makeString($1), $2),
14379 											 yyscanner);
14380 					}
14381 		;
14382 
14383 
14384 /*
14385  * Constants
14386  */
14387 AexprConst: Iconst
14388 				{
14389 					$$ = makeIntConst($1, @1);
14390 				}
14391 			| FCONST
14392 				{
14393 					$$ = makeFloatConst($1, @1);
14394 				}
14395 			| Sconst
14396 				{
14397 					$$ = makeStringConst($1, @1);
14398 				}
14399 			| BCONST
14400 				{
14401 					$$ = makeBitStringConst($1, @1);
14402 				}
14403 			| XCONST
14404 				{
14405 					/* This is a bit constant per SQL99:
14406 					 * Without Feature F511, "BIT data type",
14407 					 * a <general literal> shall not be a
14408 					 * <bit string literal> or a <hex string literal>.
14409 					 */
14410 					$$ = makeBitStringConst($1, @1);
14411 				}
14412 			| func_name Sconst
14413 				{
14414 					/* generic type 'literal' syntax */
14415 					TypeName *t = makeTypeNameFromNameList($1);
14416 					t->location = @1;
14417 					$$ = makeStringConstCast($2, @2, t);
14418 				}
14419 			| func_name '(' func_arg_list opt_sort_clause ')' Sconst
14420 				{
14421 					/* generic syntax with a type modifier */
14422 					TypeName *t = makeTypeNameFromNameList($1);
14423 					ListCell *lc;
14424 
14425 					/*
14426 					 * We must use func_arg_list and opt_sort_clause in the
14427 					 * production to avoid reduce/reduce conflicts, but we
14428 					 * don't actually wish to allow NamedArgExpr in this
14429 					 * context, nor ORDER BY.
14430 					 */
foreach(lc,$3)14431 					foreach(lc, $3)
14432 					{
14433 						NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
14434 
14435 						if (IsA(arg, NamedArgExpr))
14436 							ereport(ERROR,
14437 									(errcode(ERRCODE_SYNTAX_ERROR),
14438 									 errmsg("type modifier cannot have parameter name"),
14439 									 parser_errposition(arg->location)));
14440 					}
14441 					if ($4 != NIL)
14442 							ereport(ERROR,
14443 									(errcode(ERRCODE_SYNTAX_ERROR),
14444 									 errmsg("type modifier cannot have ORDER BY"),
14445 									 parser_errposition(@4)));
14446 
14447 					t->typmods = $3;
14448 					t->location = @1;
14449 					$$ = makeStringConstCast($6, @6, t);
14450 				}
14451 			| ConstTypename Sconst
14452 				{
14453 					$$ = makeStringConstCast($2, @2, $1);
14454 				}
14455 			| ConstInterval Sconst opt_interval
14456 				{
14457 					TypeName *t = $1;
14458 					t->typmods = $3;
14459 					$$ = makeStringConstCast($2, @2, t);
14460 				}
14461 			| ConstInterval '(' Iconst ')' Sconst
14462 				{
14463 					TypeName *t = $1;
14464 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
14465 											makeIntConst($3, @3));
14466 					$$ = makeStringConstCast($5, @5, t);
14467 				}
14468 			| TRUE_P
14469 				{
14470 					$$ = makeBoolAConst(TRUE, @1);
14471 				}
14472 			| FALSE_P
14473 				{
14474 					$$ = makeBoolAConst(FALSE, @1);
14475 				}
14476 			| NULL_P
14477 				{
14478 					$$ = makeNullAConst(@1);
14479 				}
14480 		;
14481 
14482 Iconst:		ICONST									{ $$ = $1; };
14483 Sconst:		SCONST									{ $$ = $1; };
14484 
14485 SignedIconst: Iconst								{ $$ = $1; }
14486 			| '+' Iconst							{ $$ = + $2; }
14487 			| '-' Iconst							{ $$ = - $2; }
14488 		;
14489 
14490 /* Role specifications */
14491 RoleId:		RoleSpec
14492 				{
14493 					RoleSpec *spc = (RoleSpec *) $1;
14494 					switch (spc->roletype)
14495 					{
14496 						case ROLESPEC_CSTRING:
14497 							$$ = spc->rolename;
14498 							break;
14499 						case ROLESPEC_PUBLIC:
14500 							ereport(ERROR,
14501 									(errcode(ERRCODE_RESERVED_NAME),
14502 									 errmsg("role name \"%s\" is reserved",
14503 											"public"),
14504 									 parser_errposition(@1)));
14505 						case ROLESPEC_SESSION_USER:
14506 							ereport(ERROR,
14507 									(errcode(ERRCODE_RESERVED_NAME),
14508 									 errmsg("%s cannot be used as a role name here",
14509 											"SESSION_USER"),
14510 									 parser_errposition(@1)));
14511 						case ROLESPEC_CURRENT_USER:
14512 							ereport(ERROR,
14513 									(errcode(ERRCODE_RESERVED_NAME),
14514 									 errmsg("%s cannot be used as a role name here",
14515 											"CURRENT_USER"),
14516 									 parser_errposition(@1)));
14517 					}
14518 				}
14519 			;
14520 
14521 RoleSpec:	NonReservedWord
14522 					{
14523 						/*
14524 						 * "public" and "none" are not keywords, but they must
14525 						 * be treated specially here.
14526 						 */
14527 						RoleSpec *n;
14528 						if (strcmp($1, "public") == 0)
14529 						{
14530 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_PUBLIC, @1);
14531 							n->roletype = ROLESPEC_PUBLIC;
14532 						}
14533 						else if (strcmp($1, "none") == 0)
14534 						{
14535 							ereport(ERROR,
14536 									(errcode(ERRCODE_RESERVED_NAME),
14537 									 errmsg("role name \"%s\" is reserved",
14538 											"none"),
14539 									 parser_errposition(@1)));
14540 						}
14541 						else
14542 						{
14543 							n = makeRoleSpec(ROLESPEC_CSTRING, @1);
14544 							n->rolename = pstrdup($1);
14545 						}
14546 						$$ = n;
14547 					}
14548 			| CURRENT_USER
14549 					{
14550 						$$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1);
14551 					}
14552 			| SESSION_USER
14553 					{
14554 						$$ = makeRoleSpec(ROLESPEC_SESSION_USER, @1);
14555 					}
14556 		;
14557 
14558 role_list:	RoleSpec
14559 					{ $$ = list_make1($1); }
14560 			| role_list ',' RoleSpec
14561 					{ $$ = lappend($1, $3); }
14562 		;
14563 
14564 /*
14565  * Name classification hierarchy.
14566  *
14567  * IDENT is the lexeme returned by the lexer for identifiers that match
14568  * no known keyword.  In most cases, we can accept certain keywords as
14569  * names, not only IDENTs.	We prefer to accept as many such keywords
14570  * as possible to minimize the impact of "reserved words" on programmers.
14571  * So, we divide names into several possible classes.  The classification
14572  * is chosen in part to make keywords acceptable as names wherever possible.
14573  */
14574 
14575 /* Column identifier --- names that can be column, table, etc names.
14576  */
14577 ColId:		IDENT									{ $$ = $1; }
14578 			| unreserved_keyword					{ $$ = pstrdup($1); }
14579 			| col_name_keyword						{ $$ = pstrdup($1); }
14580 		;
14581 
14582 /* Type/function identifier --- names that can be type or function names.
14583  */
14584 type_function_name:	IDENT							{ $$ = $1; }
14585 			| unreserved_keyword					{ $$ = pstrdup($1); }
14586 			| type_func_name_keyword				{ $$ = pstrdup($1); }
14587 		;
14588 
14589 /* Any not-fully-reserved word --- these names can be, eg, role names.
14590  */
14591 NonReservedWord:	IDENT							{ $$ = $1; }
14592 			| unreserved_keyword					{ $$ = pstrdup($1); }
14593 			| col_name_keyword						{ $$ = pstrdup($1); }
14594 			| type_func_name_keyword				{ $$ = pstrdup($1); }
14595 		;
14596 
14597 /* Column label --- allowed labels in "AS" clauses.
14598  * This presently includes *all* Postgres keywords.
14599  */
14600 ColLabel:	IDENT									{ $$ = $1; }
14601 			| unreserved_keyword					{ $$ = pstrdup($1); }
14602 			| col_name_keyword						{ $$ = pstrdup($1); }
14603 			| type_func_name_keyword				{ $$ = pstrdup($1); }
14604 			| reserved_keyword						{ $$ = pstrdup($1); }
14605 		;
14606 
14607 
14608 /*
14609  * Keyword category lists.  Generally, every keyword present in
14610  * the Postgres grammar should appear in exactly one of these lists.
14611  *
14612  * Put a new keyword into the first list that it can go into without causing
14613  * shift or reduce conflicts.  The earlier lists define "less reserved"
14614  * categories of keywords.
14615  *
14616  * Make sure that each keyword's category in kwlist.h matches where
14617  * it is listed here.  (Someday we may be able to generate these lists and
14618  * kwlist.h's table from a common master list.)
14619  */
14620 
14621 /* "Unreserved" keywords --- available for use as any kind of name.
14622  */
14623 unreserved_keyword:
14624 			  ABORT_P
14625 			| ABSOLUTE_P
14626 			| ACCESS
14627 			| ACTION
14628 			| ADD_P
14629 			| ADMIN
14630 			| AFTER
14631 			| AGGREGATE
14632 			| ALSO
14633 			| ALTER
14634 			| ALWAYS
14635 			| ASSERTION
14636 			| ASSIGNMENT
14637 			| AT
14638 			| ATTACH
14639 			| ATTRIBUTE
14640 			| BACKWARD
14641 			| BEFORE
14642 			| BEGIN_P
14643 			| BY
14644 			| CACHE
14645 			| CALLED
14646 			| CASCADE
14647 			| CASCADED
14648 			| CATALOG_P
14649 			| CHAIN
14650 			| CHARACTERISTICS
14651 			| CHECKPOINT
14652 			| CLASS
14653 			| CLOSE
14654 			| CLUSTER
14655 			| COLUMNS
14656 			| COMMENT
14657 			| COMMENTS
14658 			| COMMIT
14659 			| COMMITTED
14660 			| CONFIGURATION
14661 			| CONFLICT
14662 			| CONNECTION
14663 			| CONSTRAINTS
14664 			| CONTENT_P
14665 			| CONTINUE_P
14666 			| CONVERSION_P
14667 			| COPY
14668 			| COST
14669 			| CSV
14670 			| CUBE
14671 			| CURRENT_P
14672 			| CURSOR
14673 			| CYCLE
14674 			| DATA_P
14675 			| DATABASE
14676 			| DAY_P
14677 			| DEALLOCATE
14678 			| DECLARE
14679 			| DEFAULTS
14680 			| DEFERRED
14681 			| DEFINER
14682 			| DELETE_P
14683 			| DELIMITER
14684 			| DELIMITERS
14685 			| DEPENDS
14686 			| DETACH
14687 			| DICTIONARY
14688 			| DISABLE_P
14689 			| DISCARD
14690 			| DOCUMENT_P
14691 			| DOMAIN_P
14692 			| DOUBLE_P
14693 			| DROP
14694 			| EACH
14695 			| ENABLE_P
14696 			| ENCODING
14697 			| ENCRYPTED
14698 			| ENUM_P
14699 			| ESCAPE
14700 			| EVENT
14701 			| EXCLUDE
14702 			| EXCLUDING
14703 			| EXCLUSIVE
14704 			| EXECUTE
14705 			| EXPLAIN
14706 			| EXTENSION
14707 			| EXTERNAL
14708 			| FAMILY
14709 			| FILTER
14710 			| FIRST_P
14711 			| FOLLOWING
14712 			| FORCE
14713 			| FORWARD
14714 			| FUNCTION
14715 			| FUNCTIONS
14716 			| GENERATED
14717 			| GLOBAL
14718 			| GRANTED
14719 			| HANDLER
14720 			| HEADER_P
14721 			| HOLD
14722 			| HOUR_P
14723 			| IDENTITY_P
14724 			| IF_P
14725 			| IMMEDIATE
14726 			| IMMUTABLE
14727 			| IMPLICIT_P
14728 			| IMPORT_P
14729 			| INCLUDING
14730 			| INCREMENT
14731 			| INDEX
14732 			| INDEXES
14733 			| INHERIT
14734 			| INHERITS
14735 			| INLINE_P
14736 			| INPUT_P
14737 			| INSENSITIVE
14738 			| INSERT
14739 			| INSTEAD
14740 			| INVOKER
14741 			| ISOLATION
14742 			| KEY
14743 			| LABEL
14744 			| LANGUAGE
14745 			| LARGE_P
14746 			| LAST_P
14747 			| LEAKPROOF
14748 			| LEVEL
14749 			| LISTEN
14750 			| LOAD
14751 			| LOCAL
14752 			| LOCATION
14753 			| LOCK_P
14754 			| LOCKED
14755 			| LOGGED
14756 			| MAPPING
14757 			| MATCH
14758 			| MATERIALIZED
14759 			| MAXVALUE
14760 			| METHOD
14761 			| MINUTE_P
14762 			| MINVALUE
14763 			| MODE
14764 			| MONTH_P
14765 			| MOVE
14766 			| NAME_P
14767 			| NAMES
14768 			| NEW
14769 			| NEXT
14770 			| NO
14771 			| NOTHING
14772 			| NOTIFY
14773 			| NOWAIT
14774 			| NULLS_P
14775 			| OBJECT_P
14776 			| OF
14777 			| OFF
14778 			| OIDS
14779 			| OLD
14780 			| OPERATOR
14781 			| OPTION
14782 			| OPTIONS
14783 			| ORDINALITY
14784 			| OVER
14785 			| OVERRIDING
14786 			| OWNED
14787 			| OWNER
14788 			| PARALLEL
14789 			| PARSER
14790 			| PARTIAL
14791 			| PARTITION
14792 			| PASSING
14793 			| PASSWORD
14794 			| PLANS
14795 			| POLICY
14796 			| PRECEDING
14797 			| PREPARE
14798 			| PREPARED
14799 			| PRESERVE
14800 			| PRIOR
14801 			| PRIVILEGES
14802 			| PROCEDURAL
14803 			| PROCEDURE
14804 			| PROGRAM
14805 			| PUBLICATION
14806 			| QUOTE
14807 			| RANGE
14808 			| READ
14809 			| REASSIGN
14810 			| RECHECK
14811 			| RECURSIVE
14812 			| REF
14813 			| REFERENCING
14814 			| REFRESH
14815 			| REINDEX
14816 			| RELATIVE_P
14817 			| RELEASE
14818 			| RENAME
14819 			| REPEATABLE
14820 			| REPLACE
14821 			| REPLICA
14822 			| RESET
14823 			| RESTART
14824 			| RESTRICT
14825 			| RETURNS
14826 			| REVOKE
14827 			| ROLE
14828 			| ROLLBACK
14829 			| ROLLUP
14830 			| ROWS
14831 			| RULE
14832 			| SAVEPOINT
14833 			| SCHEMA
14834 			| SCHEMAS
14835 			| SCROLL
14836 			| SEARCH
14837 			| SECOND_P
14838 			| SECURITY
14839 			| SEQUENCE
14840 			| SEQUENCES
14841 			| SERIALIZABLE
14842 			| SERVER
14843 			| SESSION
14844 			| SET
14845 			| SETS
14846 			| SHARE
14847 			| SHOW
14848 			| SIMPLE
14849 			| SKIP
14850 			| SNAPSHOT
14851 			| SQL_P
14852 			| STABLE
14853 			| STANDALONE_P
14854 			| START
14855 			| STATEMENT
14856 			| STATISTICS
14857 			| STDIN
14858 			| STDOUT
14859 			| STORAGE
14860 			| STRICT_P
14861 			| STRIP_P
14862 			| SUBSCRIPTION
14863 			| SYSID
14864 			| SYSTEM_P
14865 			| TABLES
14866 			| TABLESPACE
14867 			| TEMP
14868 			| TEMPLATE
14869 			| TEMPORARY
14870 			| TEXT_P
14871 			| TRANSACTION
14872 			| TRANSFORM
14873 			| TRIGGER
14874 			| TRUNCATE
14875 			| TRUSTED
14876 			| TYPE_P
14877 			| TYPES_P
14878 			| UNBOUNDED
14879 			| UNCOMMITTED
14880 			| UNENCRYPTED
14881 			| UNKNOWN
14882 			| UNLISTEN
14883 			| UNLOGGED
14884 			| UNTIL
14885 			| UPDATE
14886 			| VACUUM
14887 			| VALID
14888 			| VALIDATE
14889 			| VALIDATOR
14890 			| VALUE_P
14891 			| VARYING
14892 			| VERSION_P
14893 			| VIEW
14894 			| VIEWS
14895 			| VOLATILE
14896 			| WHITESPACE_P
14897 			| WITHIN
14898 			| WITHOUT
14899 			| WORK
14900 			| WRAPPER
14901 			| WRITE
14902 			| XML_P
14903 			| YEAR_P
14904 			| YES_P
14905 			| ZONE
14906 		;
14907 
14908 /* Column identifier --- keywords that can be column, table, etc names.
14909  *
14910  * Many of these keywords will in fact be recognized as type or function
14911  * names too; but they have special productions for the purpose, and so
14912  * can't be treated as "generic" type or function names.
14913  *
14914  * The type names appearing here are not usable as function names
14915  * because they can be followed by '(' in typename productions, which
14916  * looks too much like a function call for an LR(1) parser.
14917  */
14918 col_name_keyword:
14919 			  BETWEEN
14920 			| BIGINT
14921 			| BIT
14922 			| BOOLEAN_P
14923 			| CHAR_P
14924 			| CHARACTER
14925 			| COALESCE
14926 			| DEC
14927 			| DECIMAL_P
14928 			| EXISTS
14929 			| EXTRACT
14930 			| FLOAT_P
14931 			| GREATEST
14932 			| GROUPING
14933 			| INOUT
14934 			| INT_P
14935 			| INTEGER
14936 			| INTERVAL
14937 			| LEAST
14938 			| NATIONAL
14939 			| NCHAR
14940 			| NONE
14941 			| NULLIF
14942 			| NUMERIC
14943 			| OUT_P
14944 			| OVERLAY
14945 			| POSITION
14946 			| PRECISION
14947 			| REAL
14948 			| ROW
14949 			| SETOF
14950 			| SMALLINT
14951 			| SUBSTRING
14952 			| TIME
14953 			| TIMESTAMP
14954 			| TREAT
14955 			| TRIM
14956 			| VALUES
14957 			| VARCHAR
14958 			| XMLATTRIBUTES
14959 			| XMLCONCAT
14960 			| XMLELEMENT
14961 			| XMLEXISTS
14962 			| XMLFOREST
14963 			| XMLNAMESPACES
14964 			| XMLPARSE
14965 			| XMLPI
14966 			| XMLROOT
14967 			| XMLSERIALIZE
14968 			| XMLTABLE
14969 		;
14970 
14971 /* Type/function identifier --- keywords that can be type or function names.
14972  *
14973  * Most of these are keywords that are used as operators in expressions;
14974  * in general such keywords can't be column names because they would be
14975  * ambiguous with variables, but they are unambiguous as function identifiers.
14976  *
14977  * Do not include POSITION, SUBSTRING, etc here since they have explicit
14978  * productions in a_expr to support the goofy SQL9x argument syntax.
14979  * - thomas 2000-11-28
14980  */
14981 type_func_name_keyword:
14982 			  AUTHORIZATION
14983 			| BINARY
14984 			| COLLATION
14985 			| CONCURRENTLY
14986 			| CROSS
14987 			| CURRENT_SCHEMA
14988 			| FREEZE
14989 			| FULL
14990 			| ILIKE
14991 			| INNER_P
14992 			| IS
14993 			| ISNULL
14994 			| JOIN
14995 			| LEFT
14996 			| LIKE
14997 			| NATURAL
14998 			| NOTNULL
14999 			| OUTER_P
15000 			| OVERLAPS
15001 			| RIGHT
15002 			| SIMILAR
15003 			| TABLESAMPLE
15004 			| VERBOSE
15005 		;
15006 
15007 /* Reserved keyword --- these keywords are usable only as a ColLabel.
15008  *
15009  * Keywords appear here if they could not be distinguished from variable,
15010  * type, or function names in some contexts.  Don't put things here unless
15011  * forced to.
15012  */
15013 reserved_keyword:
15014 			  ALL
15015 			| ANALYSE
15016 			| ANALYZE
15017 			| AND
15018 			| ANY
15019 			| ARRAY
15020 			| AS
15021 			| ASC
15022 			| ASYMMETRIC
15023 			| BOTH
15024 			| CASE
15025 			| CAST
15026 			| CHECK
15027 			| COLLATE
15028 			| COLUMN
15029 			| CONSTRAINT
15030 			| CREATE
15031 			| CURRENT_CATALOG
15032 			| CURRENT_DATE
15033 			| CURRENT_ROLE
15034 			| CURRENT_TIME
15035 			| CURRENT_TIMESTAMP
15036 			| CURRENT_USER
15037 			| DEFAULT
15038 			| DEFERRABLE
15039 			| DESC
15040 			| DISTINCT
15041 			| DO
15042 			| ELSE
15043 			| END_P
15044 			| EXCEPT
15045 			| FALSE_P
15046 			| FETCH
15047 			| FOR
15048 			| FOREIGN
15049 			| FROM
15050 			| GRANT
15051 			| GROUP_P
15052 			| HAVING
15053 			| IN_P
15054 			| INITIALLY
15055 			| INTERSECT
15056 			| INTO
15057 			| LATERAL_P
15058 			| LEADING
15059 			| LIMIT
15060 			| LOCALTIME
15061 			| LOCALTIMESTAMP
15062 			| NOT
15063 			| NULL_P
15064 			| OFFSET
15065 			| ON
15066 			| ONLY
15067 			| OR
15068 			| ORDER
15069 			| PLACING
15070 			| PRIMARY
15071 			| REFERENCES
15072 			| RETURNING
15073 			| SELECT
15074 			| SESSION_USER
15075 			| SOME
15076 			| SYMMETRIC
15077 			| TABLE
15078 			| THEN
15079 			| TO
15080 			| TRAILING
15081 			| TRUE_P
15082 			| UNION
15083 			| UNIQUE
15084 			| USER
15085 			| USING
15086 			| VARIADIC
15087 			| WHEN
15088 			| WHERE
15089 			| WINDOW
15090 			| WITH
15091 		;
15092 
15093 %%
15094 
15095 /*
15096  * The signature of this function is required by bison.  However, we
15097  * ignore the passed yylloc and instead use the last token position
15098  * available from the scanner.
15099  */
15100 static void
15101 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
15102 {
15103 	parser_yyerror(msg);
15104 }
15105 
15106 static RawStmt *
makeRawStmt(Node * stmt,int stmt_location)15107 makeRawStmt(Node *stmt, int stmt_location)
15108 {
15109 	RawStmt    *rs = makeNode(RawStmt);
15110 
15111 	rs->stmt = stmt;
15112 	rs->stmt_location = stmt_location;
15113 	rs->stmt_len = 0;			/* might get changed later */
15114 	return rs;
15115 }
15116 
15117 /* Adjust a RawStmt to reflect that it doesn't run to the end of the string */
15118 static void
updateRawStmtEnd(RawStmt * rs,int end_location)15119 updateRawStmtEnd(RawStmt *rs, int end_location)
15120 {
15121 	/*
15122 	 * If we already set the length, don't change it.  This is for situations
15123 	 * like "select foo ;; select bar" where the same statement will be last
15124 	 * in the string for more than one semicolon.
15125 	 */
15126 	if (rs->stmt_len > 0)
15127 		return;
15128 
15129 	/* OK, update length of RawStmt */
15130 	rs->stmt_len = end_location - rs->stmt_location;
15131 }
15132 
15133 static Node *
makeColumnRef(char * colname,List * indirection,int location,core_yyscan_t yyscanner)15134 makeColumnRef(char *colname, List *indirection,
15135 			  int location, core_yyscan_t yyscanner)
15136 {
15137 	/*
15138 	 * Generate a ColumnRef node, with an A_Indirection node added if there
15139 	 * is any subscripting in the specified indirection list.  However,
15140 	 * any field selection at the start of the indirection list must be
15141 	 * transposed into the "fields" part of the ColumnRef node.
15142 	 */
15143 	ColumnRef  *c = makeNode(ColumnRef);
15144 	int		nfields = 0;
15145 	ListCell *l;
15146 
15147 	c->location = location;
15148 	foreach(l, indirection)
15149 	{
15150 		if (IsA(lfirst(l), A_Indices))
15151 		{
15152 			A_Indirection *i = makeNode(A_Indirection);
15153 
15154 			if (nfields == 0)
15155 			{
15156 				/* easy case - all indirection goes to A_Indirection */
15157 				c->fields = list_make1(makeString(colname));
15158 				i->indirection = check_indirection(indirection, yyscanner);
15159 			}
15160 			else
15161 			{
15162 				/* got to split the list in two */
15163 				i->indirection = check_indirection(list_copy_tail(indirection,
15164 																  nfields),
15165 												   yyscanner);
15166 				indirection = list_truncate(indirection, nfields);
15167 				c->fields = lcons(makeString(colname), indirection);
15168 			}
15169 			i->arg = (Node *) c;
15170 			return (Node *) i;
15171 		}
15172 		else if (IsA(lfirst(l), A_Star))
15173 		{
15174 			/* We only allow '*' at the end of a ColumnRef */
15175 			if (lnext(l) != NULL)
15176 				parser_yyerror("improper use of \"*\"");
15177 		}
15178 		nfields++;
15179 	}
15180 	/* No subscripting, so all indirection gets added to field list */
15181 	c->fields = lcons(makeString(colname), indirection);
15182 	return (Node *) c;
15183 }
15184 
15185 static Node *
makeTypeCast(Node * arg,TypeName * typename,int location)15186 makeTypeCast(Node *arg, TypeName *typename, int location)
15187 {
15188 	TypeCast *n = makeNode(TypeCast);
15189 	n->arg = arg;
15190 	n->typeName = typename;
15191 	n->location = location;
15192 	return (Node *) n;
15193 }
15194 
15195 static Node *
makeStringConst(char * str,int location)15196 makeStringConst(char *str, int location)
15197 {
15198 	A_Const *n = makeNode(A_Const);
15199 
15200 	n->val.type = T_String;
15201 	n->val.val.str = str;
15202 	n->location = location;
15203 
15204 	return (Node *)n;
15205 }
15206 
15207 static Node *
makeStringConstCast(char * str,int location,TypeName * typename)15208 makeStringConstCast(char *str, int location, TypeName *typename)
15209 {
15210 	Node *s = makeStringConst(str, location);
15211 
15212 	return makeTypeCast(s, typename, -1);
15213 }
15214 
15215 static Node *
makeIntConst(int val,int location)15216 makeIntConst(int val, int location)
15217 {
15218 	A_Const *n = makeNode(A_Const);
15219 
15220 	n->val.type = T_Integer;
15221 	n->val.val.ival = val;
15222 	n->location = location;
15223 
15224 	return (Node *)n;
15225 }
15226 
15227 static Node *
makeFloatConst(char * str,int location)15228 makeFloatConst(char *str, int location)
15229 {
15230 	A_Const *n = makeNode(A_Const);
15231 
15232 	n->val.type = T_Float;
15233 	n->val.val.str = str;
15234 	n->location = location;
15235 
15236 	return (Node *)n;
15237 }
15238 
15239 static Node *
makeBitStringConst(char * str,int location)15240 makeBitStringConst(char *str, int location)
15241 {
15242 	A_Const *n = makeNode(A_Const);
15243 
15244 	n->val.type = T_BitString;
15245 	n->val.val.str = str;
15246 	n->location = location;
15247 
15248 	return (Node *)n;
15249 }
15250 
15251 static Node *
makeNullAConst(int location)15252 makeNullAConst(int location)
15253 {
15254 	A_Const *n = makeNode(A_Const);
15255 
15256 	n->val.type = T_Null;
15257 	n->location = location;
15258 
15259 	return (Node *)n;
15260 }
15261 
15262 static Node *
makeAConst(Value * v,int location)15263 makeAConst(Value *v, int location)
15264 {
15265 	Node *n;
15266 
15267 	switch (v->type)
15268 	{
15269 		case T_Float:
15270 			n = makeFloatConst(v->val.str, location);
15271 			break;
15272 
15273 		case T_Integer:
15274 			n = makeIntConst(v->val.ival, location);
15275 			break;
15276 
15277 		case T_String:
15278 		default:
15279 			n = makeStringConst(v->val.str, location);
15280 			break;
15281 	}
15282 
15283 	return n;
15284 }
15285 
15286 /* makeBoolAConst()
15287  * Create an A_Const string node and put it inside a boolean cast.
15288  */
15289 static Node *
makeBoolAConst(bool state,int location)15290 makeBoolAConst(bool state, int location)
15291 {
15292 	A_Const *n = makeNode(A_Const);
15293 
15294 	n->val.type = T_String;
15295 	n->val.val.str = (state ? "t" : "f");
15296 	n->location = location;
15297 
15298 	return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
15299 }
15300 
15301 /* makeRoleSpec
15302  * Create a RoleSpec with the given type
15303  */
15304 static RoleSpec *
makeRoleSpec(RoleSpecType type,int location)15305 makeRoleSpec(RoleSpecType type, int location)
15306 {
15307 	RoleSpec *spec = makeNode(RoleSpec);
15308 
15309 	spec->roletype = type;
15310 	spec->location = location;
15311 
15312 	return spec;
15313 }
15314 
15315 /* check_qualified_name --- check the result of qualified_name production
15316  *
15317  * It's easiest to let the grammar production for qualified_name allow
15318  * subscripts and '*', which we then must reject here.
15319  */
15320 static void
check_qualified_name(List * names,core_yyscan_t yyscanner)15321 check_qualified_name(List *names, core_yyscan_t yyscanner)
15322 {
15323 	ListCell   *i;
15324 
15325 	foreach(i, names)
15326 	{
15327 		if (!IsA(lfirst(i), String))
15328 			parser_yyerror("syntax error");
15329 	}
15330 }
15331 
15332 /* check_func_name --- check the result of func_name production
15333  *
15334  * It's easiest to let the grammar production for func_name allow subscripts
15335  * and '*', which we then must reject here.
15336  */
15337 static List *
check_func_name(List * names,core_yyscan_t yyscanner)15338 check_func_name(List *names, core_yyscan_t yyscanner)
15339 {
15340 	ListCell   *i;
15341 
15342 	foreach(i, names)
15343 	{
15344 		if (!IsA(lfirst(i), String))
15345 			parser_yyerror("syntax error");
15346 	}
15347 	return names;
15348 }
15349 
15350 /* check_indirection --- check the result of indirection production
15351  *
15352  * We only allow '*' at the end of the list, but it's hard to enforce that
15353  * in the grammar, so do it here.
15354  */
15355 static List *
check_indirection(List * indirection,core_yyscan_t yyscanner)15356 check_indirection(List *indirection, core_yyscan_t yyscanner)
15357 {
15358 	ListCell *l;
15359 
15360 	foreach(l, indirection)
15361 	{
15362 		if (IsA(lfirst(l), A_Star))
15363 		{
15364 			if (lnext(l) != NULL)
15365 				parser_yyerror("improper use of \"*\"");
15366 		}
15367 	}
15368 	return indirection;
15369 }
15370 
15371 /* extractArgTypes()
15372  * Given a list of FunctionParameter nodes, extract a list of just the
15373  * argument types (TypeNames) for input parameters only.  This is what
15374  * is needed to look up an existing function, which is what is wanted by
15375  * the productions that use this call.
15376  */
15377 static List *
extractArgTypes(List * parameters)15378 extractArgTypes(List *parameters)
15379 {
15380 	List	   *result = NIL;
15381 	ListCell   *i;
15382 
15383 	foreach(i, parameters)
15384 	{
15385 		FunctionParameter *p = (FunctionParameter *) lfirst(i);
15386 
15387 		if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
15388 			result = lappend(result, p->argType);
15389 	}
15390 	return result;
15391 }
15392 
15393 /* extractAggrArgTypes()
15394  * As above, but work from the output of the aggr_args production.
15395  */
15396 static List *
extractAggrArgTypes(List * aggrargs)15397 extractAggrArgTypes(List *aggrargs)
15398 {
15399 	Assert(list_length(aggrargs) == 2);
15400 	return extractArgTypes((List *) linitial(aggrargs));
15401 }
15402 
15403 /* makeOrderedSetArgs()
15404  * Build the result of the aggr_args production (which see the comments for).
15405  * This handles only the case where both given lists are nonempty, so that
15406  * we have to deal with multiple VARIADIC arguments.
15407  */
15408 static List *
makeOrderedSetArgs(List * directargs,List * orderedargs,core_yyscan_t yyscanner)15409 makeOrderedSetArgs(List *directargs, List *orderedargs,
15410 				   core_yyscan_t yyscanner)
15411 {
15412 	FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
15413 	Value	   *ndirectargs;
15414 
15415 	/* No restriction unless last direct arg is VARIADIC */
15416 	if (lastd->mode == FUNC_PARAM_VARIADIC)
15417 	{
15418 		FunctionParameter *firsto = (FunctionParameter *) linitial(orderedargs);
15419 
15420 		/*
15421 		 * We ignore the names, though the aggr_arg production allows them;
15422 		 * it doesn't allow default values, so those need not be checked.
15423 		 */
15424 		if (list_length(orderedargs) != 1 ||
15425 			firsto->mode != FUNC_PARAM_VARIADIC ||
15426 			!equal(lastd->argType, firsto->argType))
15427 			ereport(ERROR,
15428 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15429 					 errmsg("an ordered-set aggregate with a VARIADIC direct argument must have one VARIADIC aggregated argument of the same data type"),
15430 					 parser_errposition(exprLocation((Node *) firsto))));
15431 
15432 		/* OK, drop the duplicate VARIADIC argument from the internal form */
15433 		orderedargs = NIL;
15434 	}
15435 
15436 	/* don't merge into the next line, as list_concat changes directargs */
15437 	ndirectargs = makeInteger(list_length(directargs));
15438 
15439 	return list_make2(list_concat(directargs, orderedargs),
15440 					  ndirectargs);
15441 }
15442 
15443 /* insertSelectOptions()
15444  * Insert ORDER BY, etc into an already-constructed SelectStmt.
15445  *
15446  * This routine is just to avoid duplicating code in SelectStmt productions.
15447  */
15448 static void
insertSelectOptions(SelectStmt * stmt,List * sortClause,List * lockingClause,Node * limitOffset,Node * limitCount,WithClause * withClause,core_yyscan_t yyscanner)15449 insertSelectOptions(SelectStmt *stmt,
15450 					List *sortClause, List *lockingClause,
15451 					Node *limitOffset, Node *limitCount,
15452 					WithClause *withClause,
15453 					core_yyscan_t yyscanner)
15454 {
15455 	Assert(IsA(stmt, SelectStmt));
15456 
15457 	/*
15458 	 * Tests here are to reject constructs like
15459 	 *	(SELECT foo ORDER BY bar) ORDER BY baz
15460 	 */
15461 	if (sortClause)
15462 	{
15463 		if (stmt->sortClause)
15464 			ereport(ERROR,
15465 					(errcode(ERRCODE_SYNTAX_ERROR),
15466 					 errmsg("multiple ORDER BY clauses not allowed"),
15467 					 parser_errposition(exprLocation((Node *) sortClause))));
15468 		stmt->sortClause = sortClause;
15469 	}
15470 	/* We can handle multiple locking clauses, though */
15471 	stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
15472 	if (limitOffset)
15473 	{
15474 		if (stmt->limitOffset)
15475 			ereport(ERROR,
15476 					(errcode(ERRCODE_SYNTAX_ERROR),
15477 					 errmsg("multiple OFFSET clauses not allowed"),
15478 					 parser_errposition(exprLocation(limitOffset))));
15479 		stmt->limitOffset = limitOffset;
15480 	}
15481 	if (limitCount)
15482 	{
15483 		if (stmt->limitCount)
15484 			ereport(ERROR,
15485 					(errcode(ERRCODE_SYNTAX_ERROR),
15486 					 errmsg("multiple LIMIT clauses not allowed"),
15487 					 parser_errposition(exprLocation(limitCount))));
15488 		stmt->limitCount = limitCount;
15489 	}
15490 	if (withClause)
15491 	{
15492 		if (stmt->withClause)
15493 			ereport(ERROR,
15494 					(errcode(ERRCODE_SYNTAX_ERROR),
15495 					 errmsg("multiple WITH clauses not allowed"),
15496 					 parser_errposition(exprLocation((Node *) withClause))));
15497 		stmt->withClause = withClause;
15498 	}
15499 }
15500 
15501 static Node *
makeSetOp(SetOperation op,bool all,Node * larg,Node * rarg)15502 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
15503 {
15504 	SelectStmt *n = makeNode(SelectStmt);
15505 
15506 	n->op = op;
15507 	n->all = all;
15508 	n->larg = (SelectStmt *) larg;
15509 	n->rarg = (SelectStmt *) rarg;
15510 	return (Node *) n;
15511 }
15512 
15513 /* SystemFuncName()
15514  * Build a properly-qualified reference to a built-in function.
15515  */
15516 List *
SystemFuncName(char * name)15517 SystemFuncName(char *name)
15518 {
15519 	return list_make2(makeString("pg_catalog"), makeString(name));
15520 }
15521 
15522 /* SystemTypeName()
15523  * Build a properly-qualified reference to a built-in type.
15524  *
15525  * typmod is defaulted, but may be changed afterwards by caller.
15526  * Likewise for the location.
15527  */
15528 TypeName *
SystemTypeName(char * name)15529 SystemTypeName(char *name)
15530 {
15531 	return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
15532 											   makeString(name)));
15533 }
15534 
15535 /* doNegate()
15536  * Handle negation of a numeric constant.
15537  *
15538  * Formerly, we did this here because the optimizer couldn't cope with
15539  * indexquals that looked like "var = -4" --- it wants "var = const"
15540  * and a unary minus operator applied to a constant didn't qualify.
15541  * As of Postgres 7.0, that problem doesn't exist anymore because there
15542  * is a constant-subexpression simplifier in the optimizer.  However,
15543  * there's still a good reason for doing this here, which is that we can
15544  * postpone committing to a particular internal representation for simple
15545  * negative constants.	It's better to leave "-123.456" in string form
15546  * until we know what the desired type is.
15547  */
15548 static Node *
doNegate(Node * n,int location)15549 doNegate(Node *n, int location)
15550 {
15551 	if (IsA(n, A_Const))
15552 	{
15553 		A_Const *con = (A_Const *)n;
15554 
15555 		/* report the constant's location as that of the '-' sign */
15556 		con->location = location;
15557 
15558 		if (con->val.type == T_Integer)
15559 		{
15560 			con->val.val.ival = -con->val.val.ival;
15561 			return n;
15562 		}
15563 		if (con->val.type == T_Float)
15564 		{
15565 			doNegateFloat(&con->val);
15566 			return n;
15567 		}
15568 	}
15569 
15570 	return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
15571 }
15572 
15573 static void
doNegateFloat(Value * v)15574 doNegateFloat(Value *v)
15575 {
15576 	char   *oldval = v->val.str;
15577 
15578 	Assert(IsA(v, Float));
15579 	if (*oldval == '+')
15580 		oldval++;
15581 	if (*oldval == '-')
15582 		v->val.str = oldval+1;	/* just strip the '-' */
15583 	else
15584 		v->val.str = psprintf("-%s", oldval);
15585 }
15586 
15587 static Node *
makeAndExpr(Node * lexpr,Node * rexpr,int location)15588 makeAndExpr(Node *lexpr, Node *rexpr, int location)
15589 {
15590 	Node	   *lexp = lexpr;
15591 
15592 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
15593 	while (IsA(lexp, A_Expr) &&
15594 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
15595 		lexp = ((A_Expr *) lexp)->lexpr;
15596 	/* Flatten "a AND b AND c ..." to a single BoolExpr on sight */
15597 	if (IsA(lexp, BoolExpr))
15598 	{
15599 		BoolExpr *blexpr = (BoolExpr *) lexp;
15600 
15601 		if (blexpr->boolop == AND_EXPR)
15602 		{
15603 			blexpr->args = lappend(blexpr->args, rexpr);
15604 			return (Node *) blexpr;
15605 		}
15606 	}
15607 	return (Node *) makeBoolExpr(AND_EXPR, list_make2(lexpr, rexpr), location);
15608 }
15609 
15610 static Node *
makeOrExpr(Node * lexpr,Node * rexpr,int location)15611 makeOrExpr(Node *lexpr, Node *rexpr, int location)
15612 {
15613 	Node	   *lexp = lexpr;
15614 
15615 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
15616 	while (IsA(lexp, A_Expr) &&
15617 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
15618 		lexp = ((A_Expr *) lexp)->lexpr;
15619 	/* Flatten "a OR b OR c ..." to a single BoolExpr on sight */
15620 	if (IsA(lexp, BoolExpr))
15621 	{
15622 		BoolExpr *blexpr = (BoolExpr *) lexp;
15623 
15624 		if (blexpr->boolop == OR_EXPR)
15625 		{
15626 			blexpr->args = lappend(blexpr->args, rexpr);
15627 			return (Node *) blexpr;
15628 		}
15629 	}
15630 	return (Node *) makeBoolExpr(OR_EXPR, list_make2(lexpr, rexpr), location);
15631 }
15632 
15633 static Node *
makeNotExpr(Node * expr,int location)15634 makeNotExpr(Node *expr, int location)
15635 {
15636 	return (Node *) makeBoolExpr(NOT_EXPR, list_make1(expr), location);
15637 }
15638 
15639 static Node *
makeAArrayExpr(List * elements,int location)15640 makeAArrayExpr(List *elements, int location)
15641 {
15642 	A_ArrayExpr *n = makeNode(A_ArrayExpr);
15643 
15644 	n->elements = elements;
15645 	n->location = location;
15646 	return (Node *) n;
15647 }
15648 
15649 static Node *
makeSQLValueFunction(SQLValueFunctionOp op,int32 typmod,int location)15650 makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location)
15651 {
15652 	SQLValueFunction *svf = makeNode(SQLValueFunction);
15653 
15654 	svf->op = op;
15655 	/* svf->type will be filled during parse analysis */
15656 	svf->typmod = typmod;
15657 	svf->location = location;
15658 	return (Node *) svf;
15659 }
15660 
15661 static Node *
makeXmlExpr(XmlExprOp op,char * name,List * named_args,List * args,int location)15662 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
15663 			int location)
15664 {
15665 	XmlExpr		*x = makeNode(XmlExpr);
15666 
15667 	x->op = op;
15668 	x->name = name;
15669 	/*
15670 	 * named_args is a list of ResTarget; it'll be split apart into separate
15671 	 * expression and name lists in transformXmlExpr().
15672 	 */
15673 	x->named_args = named_args;
15674 	x->arg_names = NIL;
15675 	x->args = args;
15676 	/* xmloption, if relevant, must be filled in by caller */
15677 	/* type and typmod will be filled in during parse analysis */
15678 	x->type = InvalidOid;			/* marks the node as not analyzed */
15679 	x->location = location;
15680 	return (Node *) x;
15681 }
15682 
15683 /*
15684  * Merge the input and output parameters of a table function.
15685  */
15686 static List *
mergeTableFuncParameters(List * func_args,List * columns)15687 mergeTableFuncParameters(List *func_args, List *columns)
15688 {
15689 	ListCell   *lc;
15690 
15691 	/* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
15692 	foreach(lc, func_args)
15693 	{
15694 		FunctionParameter *p = (FunctionParameter *) lfirst(lc);
15695 
15696 		if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
15697 			ereport(ERROR,
15698 					(errcode(ERRCODE_SYNTAX_ERROR),
15699 					 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
15700 	}
15701 
15702 	return list_concat(func_args, columns);
15703 }
15704 
15705 /*
15706  * Determine return type of a TABLE function.  A single result column
15707  * returns setof that column's type; otherwise return setof record.
15708  */
15709 static TypeName *
TableFuncTypeName(List * columns)15710 TableFuncTypeName(List *columns)
15711 {
15712 	TypeName *result;
15713 
15714 	if (list_length(columns) == 1)
15715 	{
15716 		FunctionParameter *p = (FunctionParameter *) linitial(columns);
15717 
15718 		result = copyObject(p->argType);
15719 	}
15720 	else
15721 		result = SystemTypeName("record");
15722 
15723 	result->setof = true;
15724 
15725 	return result;
15726 }
15727 
15728 /*
15729  * Convert a list of (dotted) names to a RangeVar (like
15730  * makeRangeVarFromNameList, but with position support).  The
15731  * "AnyName" refers to the any_name production in the grammar.
15732  */
15733 static RangeVar *
makeRangeVarFromAnyName(List * names,int position,core_yyscan_t yyscanner)15734 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
15735 {
15736 	RangeVar *r = makeNode(RangeVar);
15737 
15738 	switch (list_length(names))
15739 	{
15740 		case 1:
15741 			r->catalogname = NULL;
15742 			r->schemaname = NULL;
15743 			r->relname = strVal(linitial(names));
15744 			break;
15745 		case 2:
15746 			r->catalogname = NULL;
15747 			r->schemaname = strVal(linitial(names));
15748 			r->relname = strVal(lsecond(names));
15749 			break;
15750 		case 3:
15751 			r->catalogname = strVal(linitial(names));
15752 			r->schemaname = strVal(lsecond(names));
15753 			r->relname = strVal(lthird(names));
15754 			break;
15755 		default:
15756 			ereport(ERROR,
15757 					(errcode(ERRCODE_SYNTAX_ERROR),
15758 					 errmsg("improper qualified name (too many dotted names): %s",
15759 							NameListToString(names)),
15760 					 parser_errposition(position)));
15761 			break;
15762 	}
15763 
15764 	r->relpersistence = RELPERSISTENCE_PERMANENT;
15765 	r->location = position;
15766 
15767 	return r;
15768 }
15769 
15770 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
15771 static void
SplitColQualList(List * qualList,List ** constraintList,CollateClause ** collClause,core_yyscan_t yyscanner)15772 SplitColQualList(List *qualList,
15773 				 List **constraintList, CollateClause **collClause,
15774 				 core_yyscan_t yyscanner)
15775 {
15776 	ListCell   *cell;
15777 	ListCell   *prev;
15778 	ListCell   *next;
15779 
15780 	*collClause = NULL;
15781 	prev = NULL;
15782 	for (cell = list_head(qualList); cell; cell = next)
15783 	{
15784 		Node   *n = (Node *) lfirst(cell);
15785 
15786 		next = lnext(cell);
15787 		if (IsA(n, Constraint))
15788 		{
15789 			/* keep it in list */
15790 			prev = cell;
15791 			continue;
15792 		}
15793 		if (IsA(n, CollateClause))
15794 		{
15795 			CollateClause *c = (CollateClause *) n;
15796 
15797 			if (*collClause)
15798 				ereport(ERROR,
15799 						(errcode(ERRCODE_SYNTAX_ERROR),
15800 						 errmsg("multiple COLLATE clauses not allowed"),
15801 						 parser_errposition(c->location)));
15802 			*collClause = c;
15803 		}
15804 		else
15805 			elog(ERROR, "unexpected node type %d", (int) n->type);
15806 		/* remove non-Constraint nodes from qualList */
15807 		qualList = list_delete_cell(qualList, cell, prev);
15808 	}
15809 	*constraintList = qualList;
15810 }
15811 
15812 /*
15813  * Process result of ConstraintAttributeSpec, and set appropriate bool flags
15814  * in the output command node.  Pass NULL for any flags the particular
15815  * command doesn't support.
15816  */
15817 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)15818 processCASbits(int cas_bits, int location, const char *constrType,
15819 			   bool *deferrable, bool *initdeferred, bool *not_valid,
15820 			   bool *no_inherit, core_yyscan_t yyscanner)
15821 {
15822 	/* defaults */
15823 	if (deferrable)
15824 		*deferrable = false;
15825 	if (initdeferred)
15826 		*initdeferred = false;
15827 	if (not_valid)
15828 		*not_valid = false;
15829 
15830 	if (cas_bits & (CAS_DEFERRABLE | CAS_INITIALLY_DEFERRED))
15831 	{
15832 		if (deferrable)
15833 			*deferrable = true;
15834 		else
15835 			ereport(ERROR,
15836 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15837 					 /* translator: %s is CHECK, UNIQUE, or similar */
15838 					 errmsg("%s constraints cannot be marked DEFERRABLE",
15839 							constrType),
15840 					 parser_errposition(location)));
15841 	}
15842 
15843 	if (cas_bits & CAS_INITIALLY_DEFERRED)
15844 	{
15845 		if (initdeferred)
15846 			*initdeferred = true;
15847 		else
15848 			ereport(ERROR,
15849 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15850 					 /* translator: %s is CHECK, UNIQUE, or similar */
15851 					 errmsg("%s constraints cannot be marked DEFERRABLE",
15852 							constrType),
15853 					 parser_errposition(location)));
15854 	}
15855 
15856 	if (cas_bits & CAS_NOT_VALID)
15857 	{
15858 		if (not_valid)
15859 			*not_valid = true;
15860 		else
15861 			ereport(ERROR,
15862 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15863 					 /* translator: %s is CHECK, UNIQUE, or similar */
15864 					 errmsg("%s constraints cannot be marked NOT VALID",
15865 							constrType),
15866 					 parser_errposition(location)));
15867 	}
15868 
15869 	if (cas_bits & CAS_NO_INHERIT)
15870 	{
15871 		if (no_inherit)
15872 			*no_inherit = true;
15873 		else
15874 			ereport(ERROR,
15875 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15876 					 /* translator: %s is CHECK, UNIQUE, or similar */
15877 					 errmsg("%s constraints cannot be marked NO INHERIT",
15878 							constrType),
15879 					 parser_errposition(location)));
15880 	}
15881 }
15882 
15883 /*----------
15884  * Recursive view transformation
15885  *
15886  * Convert
15887  *
15888  *     CREATE RECURSIVE VIEW relname (aliases) AS query
15889  *
15890  * to
15891  *
15892  *     CREATE VIEW relname (aliases) AS
15893  *         WITH RECURSIVE relname (aliases) AS (query)
15894  *         SELECT aliases FROM relname
15895  *
15896  * Actually, just the WITH ... part, which is then inserted into the original
15897  * view definition as the query.
15898  * ----------
15899  */
15900 static Node *
makeRecursiveViewSelect(char * relname,List * aliases,Node * query)15901 makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
15902 {
15903 	SelectStmt *s = makeNode(SelectStmt);
15904 	WithClause *w = makeNode(WithClause);
15905 	CommonTableExpr *cte = makeNode(CommonTableExpr);
15906 	List	   *tl = NIL;
15907 	ListCell   *lc;
15908 
15909 	/* create common table expression */
15910 	cte->ctename = relname;
15911 	cte->aliascolnames = aliases;
15912 	cte->ctequery = query;
15913 	cte->location = -1;
15914 
15915 	/* create WITH clause and attach CTE */
15916 	w->recursive = true;
15917 	w->ctes = list_make1(cte);
15918 	w->location = -1;
15919 
15920 	/* create target list for the new SELECT from the alias list of the
15921 	 * recursive view specification */
15922 	foreach (lc, aliases)
15923 	{
15924 		ResTarget *rt = makeNode(ResTarget);
15925 
15926 		rt->name = NULL;
15927 		rt->indirection = NIL;
15928 		rt->val = makeColumnRef(strVal(lfirst(lc)), NIL, -1, 0);
15929 		rt->location = -1;
15930 
15931 		tl = lappend(tl, rt);
15932 	}
15933 
15934 	/* create new SELECT combining WITH clause, target list, and fake FROM
15935 	 * clause */
15936 	s->withClause = w;
15937 	s->targetList = tl;
15938 	s->fromClause = list_make1(makeRangeVar(NULL, relname, -1));
15939 
15940 	return (Node *) s;
15941 }
15942 
15943 /* parser_init()
15944  * Initialize to parse one query string
15945  */
15946 void
parser_init(base_yy_extra_type * yyext)15947 parser_init(base_yy_extra_type *yyext)
15948 {
15949 	yyext->parsetree = NIL;		/* in case grammar forgets to set it */
15950 }
15951