1 %{
2 
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5  *
6  * gram.y
7  *	  POSTGRESQL BISON rules/actions
8  *
9  * Portions Copyright (c) 1996-2016, 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 SQL_inheritance 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.  The handling of SQL_inheritance is
36  *	  a good example.
37  *
38  * WARNINGS
39  *	  If you use a list, make sure the datum is a node so that the printing
40  *	  routines work.
41  *
42  *	  Sometimes we assign constants to makeStrings. Make sure we don't free
43  *	  those.
44  *
45  *-------------------------------------------------------------------------
46  */
47 #include "postgres.h"
48 
49 #include <ctype.h>
50 #include <limits.h>
51 
52 #include "catalog/index.h"
53 #include "catalog/namespace.h"
54 #include "catalog/pg_am.h"
55 #include "catalog/pg_trigger.h"
56 #include "commands/defrem.h"
57 #include "commands/trigger.h"
58 #include "nodes/makefuncs.h"
59 #include "nodes/nodeFuncs.h"
60 #include "parser/gramparse.h"
61 #include "parser/parser.h"
62 #include "parser/parse_expr.h"
63 #include "storage/lmgr.h"
64 #include "utils/date.h"
65 #include "utils/datetime.h"
66 #include "utils/numeric.h"
67 #include "utils/xml.h"
68 
69 
70 /*
71  * Location tracking support --- simpler than bison's default, since we only
72  * want to track the start position not the end position of each nonterminal.
73  */
74 #define YYLLOC_DEFAULT(Current, Rhs, N) \
75 	do { \
76 		if ((N) > 0) \
77 			(Current) = (Rhs)[1]; \
78 		else \
79 			(Current) = (-1); \
80 	} while (0)
81 
82 /*
83  * The above macro assigns -1 (unknown) as the parse location of any
84  * nonterminal that 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 
97 /*
98  * Bison doesn't allocate anything that needs to live across parser calls,
99  * so we can easily have it use palloc instead of malloc.  This prevents
100  * memory leaks if we error out during parsing.  Note this only works with
101  * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
102  * if possible, so there's not really much problem anyhow, at least if
103  * you're building with gcc.
104  */
105 #define YYMALLOC palloc
106 #define YYFREE   pfree
107 
108 /* Private struct for the result of privilege_target production */
109 typedef struct PrivTarget
110 {
111 	GrantTargetType targtype;
112 	GrantObjectType objtype;
113 	List	   *objs;
114 } PrivTarget;
115 
116 /* Private struct for the result of import_qualification production */
117 typedef struct ImportQual
118 {
119 	ImportForeignSchemaType type;
120 	List	   *table_names;
121 } ImportQual;
122 
123 /* ConstraintAttributeSpec yields an integer bitmask of these flags: */
124 #define CAS_NOT_DEFERRABLE			0x01
125 #define CAS_DEFERRABLE				0x02
126 #define CAS_INITIALLY_IMMEDIATE		0x04
127 #define CAS_INITIALLY_DEFERRED		0x08
128 #define CAS_NOT_VALID				0x10
129 #define CAS_NO_INHERIT				0x20
130 
131 
132 #define parser_yyerror(msg)  scanner_yyerror(msg, yyscanner)
133 #define parser_errposition(pos)  scanner_errposition(pos, yyscanner)
134 
135 static void base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner,
136 						 const char *msg);
137 static Node *makeColumnRef(char *colname, List *indirection,
138 						   int location, core_yyscan_t yyscanner);
139 static Node *makeTypeCast(Node *arg, TypeName *typename, int location);
140 static Node *makeStringConst(char *str, int location);
141 static Node *makeStringConstCast(char *str, int location, TypeName *typename);
142 static Node *makeIntConst(int val, int location);
143 static Node *makeFloatConst(char *str, int location);
144 static Node *makeBitStringConst(char *str, int location);
145 static Node *makeNullAConst(int location);
146 static Node *makeAConst(Value *v, int location);
147 static Node *makeBoolAConst(bool state, int location);
148 static Node *makeRoleSpec(RoleSpecType type, int location);
149 static void check_qualified_name(List *names, core_yyscan_t yyscanner);
150 static List *check_func_name(List *names, core_yyscan_t yyscanner);
151 static List *check_indirection(List *indirection, core_yyscan_t yyscanner);
152 static List *extractArgTypes(List *parameters);
153 static List *extractAggrArgTypes(List *aggrargs);
154 static List *makeOrderedSetArgs(List *directargs, List *orderedargs,
155 								core_yyscan_t yyscanner);
156 static void insertSelectOptions(SelectStmt *stmt,
157 								List *sortClause, List *lockingClause,
158 								Node *limitOffset, Node *limitCount,
159 								WithClause *withClause,
160 								core_yyscan_t yyscanner);
161 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
162 static Node *doNegate(Node *n, int location);
163 static void doNegateFloat(Value *v);
164 static Node *makeAndExpr(Node *lexpr, Node *rexpr, int location);
165 static Node *makeOrExpr(Node *lexpr, Node *rexpr, int location);
166 static Node *makeNotExpr(Node *expr, int location);
167 static Node *makeAArrayExpr(List *elements, int location);
168 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
169 						 List *args, int location);
170 static List *mergeTableFuncParameters(List *func_args, List *columns);
171 static TypeName *TableFuncTypeName(List *columns);
172 static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner);
173 static void SplitColQualList(List *qualList,
174 							 List **constraintList, CollateClause **collClause,
175 							 core_yyscan_t yyscanner);
176 static void processCASbits(int cas_bits, int location, const char *constrType,
177 			   bool *deferrable, bool *initdeferred, bool *not_valid,
178 			   bool *no_inherit, core_yyscan_t yyscanner);
179 static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
180 
181 %}
182 
183 %pure-parser
184 %expect 0
185 %name-prefix="base_yy"
186 %locations
187 
188 %parse-param {core_yyscan_t yyscanner}
189 %lex-param   {core_yyscan_t yyscanner}
190 
191 %union
192 {
193 	core_YYSTYPE		core_yystype;
194 	/* these fields must match core_YYSTYPE: */
195 	int					ival;
196 	char				*str;
197 	const char			*keyword;
198 
199 	char				chr;
200 	bool				boolean;
201 	JoinType			jtype;
202 	DropBehavior		dbehavior;
203 	OnCommitAction		oncommit;
204 	List				*list;
205 	Node				*node;
206 	Value				*value;
207 	ObjectType			objtype;
208 	TypeName			*typnam;
209 	FunctionParameter   *fun_param;
210 	FunctionParameterMode fun_param_mode;
211 	FuncWithArgs		*funwithargs;
212 	DefElem				*defelt;
213 	SortBy				*sortby;
214 	WindowDef			*windef;
215 	JoinExpr			*jexpr;
216 	IndexElem			*ielem;
217 	Alias				*alias;
218 	RangeVar			*range;
219 	IntoClause			*into;
220 	WithClause			*with;
221 	InferClause			*infer;
222 	OnConflictClause	*onconflict;
223 	A_Indices			*aind;
224 	ResTarget			*target;
225 	struct PrivTarget	*privtarget;
226 	AccessPriv			*accesspriv;
227 	struct ImportQual	*importqual;
228 	InsertStmt			*istmt;
229 	VariableSetStmt		*vsetstmt;
230 }
231 
232 %type <node>	stmt schema_stmt
233 		AlterEventTrigStmt
234 		AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterEnumStmt
235 		AlterFdwStmt AlterForeignServerStmt AlterGroupStmt
236 		AlterObjectDependsStmt AlterObjectSchemaStmt AlterOwnerStmt
237 		AlterOperatorStmt AlterSeqStmt AlterSystemStmt AlterTableStmt
238 		AlterTblSpcStmt AlterExtensionStmt AlterExtensionContentsStmt AlterForeignTableStmt
239 		AlterCompositeTypeStmt AlterUserStmt AlterUserMappingStmt AlterUserSetStmt
240 		AlterRoleStmt AlterRoleSetStmt AlterPolicyStmt
241 		AlterDefaultPrivilegesStmt DefACLAction
242 		AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
243 		ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
244 		CreateDomainStmt CreateExtensionStmt CreateGroupStmt CreateOpClassStmt
245 		CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
246 		CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
247 		CreateFdwStmt CreateForeignServerStmt CreateForeignTableStmt
248 		CreateAssertStmt CreateTransformStmt CreateTrigStmt CreateEventTrigStmt
249 		CreateUserStmt CreateUserMappingStmt CreateRoleStmt CreatePolicyStmt
250 		CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
251 		DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
252 		DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
253 		DropPolicyStmt DropUserStmt DropdbStmt DropTableSpaceStmt DropFdwStmt
254 		DropTransformStmt
255 		DropForeignServerStmt DropUserMappingStmt ExplainStmt FetchStmt
256 		GrantStmt GrantRoleStmt ImportForeignSchemaStmt IndexStmt InsertStmt
257 		ListenStmt LoadStmt LockStmt NotifyStmt ExplainableStmt PreparableStmt
258 		CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
259 		RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
260 		RuleActionStmt RuleActionStmtOrEmpty RuleStmt
261 		SecLabelStmt SelectStmt TransactionStmt TruncateStmt
262 		UnlistenStmt UpdateStmt VacuumStmt
263 		VariableResetStmt VariableSetStmt VariableShowStmt
264 		ViewStmt CheckPointStmt CreateConversionStmt
265 		DeallocateStmt PrepareStmt ExecuteStmt
266 		DropOwnedStmt ReassignOwnedStmt
267 		AlterTSConfigurationStmt AlterTSDictionaryStmt
268 		CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
269 
270 %type <node>	select_no_parens select_with_parens select_clause
271 				simple_select values_clause
272 
273 %type <node>	alter_column_default opclass_item opclass_drop alter_using
274 %type <ival>	add_drop opt_asc_desc opt_nulls_order
275 
276 %type <node>	alter_table_cmd alter_type_cmd opt_collate_clause
277 	   replica_identity
278 %type <list>	alter_table_cmds alter_type_cmds
279 
280 %type <dbehavior>	opt_drop_behavior
281 
282 %type <list>	createdb_opt_list createdb_opt_items copy_opt_list
283 				transaction_mode_list
284 				create_extension_opt_list alter_extension_opt_list
285 %type <defelt>	createdb_opt_item copy_opt_item
286 				transaction_mode_item
287 				create_extension_opt_item alter_extension_opt_item
288 
289 %type <ival>	opt_lock lock_type cast_context
290 %type <ival>	vacuum_option_list vacuum_option_elem
291 %type <boolean>	opt_or_replace
292 				opt_grant_grant_option opt_grant_admin_option
293 				opt_nowait opt_if_exists opt_with_data
294 %type <ival>	opt_nowait_or_skip
295 
296 %type <list>	OptRoleList AlterOptRoleList
297 %type <defelt>	CreateOptRoleElem AlterOptRoleElem
298 
299 %type <str>		opt_type
300 %type <str>		foreign_server_version opt_foreign_server_version
301 %type <str>		opt_in_database
302 
303 %type <str>		OptSchemaName
304 %type <list>	OptSchemaEltList
305 
306 %type <boolean> TriggerForSpec TriggerForType
307 %type <ival>	TriggerActionTime
308 %type <list>	TriggerEvents TriggerOneEvent
309 %type <value>	TriggerFuncArg
310 %type <node>	TriggerWhen
311 
312 %type <list>	event_trigger_when_list event_trigger_value_list
313 %type <defelt>	event_trigger_when_item
314 %type <chr>		enable_trigger
315 
316 %type <str>		copy_file_name
317 				database_name access_method_clause access_method attr_name
318 				name cursor_name file_name
319 				index_name opt_index_name cluster_index_specification
320 
321 %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
322 				opt_class opt_inline_handler opt_validator validator_clause
323 				opt_collate
324 
325 %type <range>	qualified_name insert_target OptConstrFromTable
326 
327 %type <str>		all_Op MathOp
328 
329 %type <str>		row_security_cmd RowSecurityDefaultForCmd
330 %type <node>	RowSecurityOptionalWithCheck RowSecurityOptionalExpr
331 %type <list>	RowSecurityDefaultToRole RowSecurityOptionalToRole
332 
333 %type <str>		iso_level opt_encoding
334 %type <node>	grantee
335 %type <list>	grantee_list
336 %type <accesspriv> privilege
337 %type <list>	privileges privilege_list
338 %type <privtarget> privilege_target
339 %type <funwithargs> function_with_argtypes
340 %type <list>	function_with_argtypes_list
341 %type <ival>	defacl_privilege_target
342 %type <defelt>	DefACLOption
343 %type <list>	DefACLOptionList
344 %type <ival>	import_qualification_type
345 %type <importqual> import_qualification
346 
347 %type <list>	stmtblock stmtmulti
348 				OptTableElementList TableElementList OptInherit definition
349 				OptTypedTableElementList TypedTableElementList
350 				reloptions opt_reloptions
351 				OptWith distinct_clause opt_all_clause opt_definition func_args func_args_list
352 				func_args_with_defaults func_args_with_defaults_list
353 				aggr_args aggr_args_list
354 				func_as createfunc_opt_list alterfunc_opt_list
355 				old_aggr_definition old_aggr_list
356 				oper_argtypes RuleActionList RuleActionMulti
357 				opt_column_list columnList opt_name_list
358 				sort_clause opt_sort_clause sortby_list index_params
359 				name_list role_list from_clause from_list opt_array_bounds
360 				qualified_name_list any_name any_name_list type_name_list
361 				any_operator expr_list attrs
362 				target_list opt_target_list insert_column_list set_target_list
363 				set_clause_list set_clause multiple_set_clause
364 				ctext_expr_list ctext_row def_list operator_def_list indirection opt_indirection
365 				reloption_list group_clause TriggerFuncArgs select_limit
366 				opt_select_limit opclass_item_list opclass_drop_list
367 				opclass_purpose opt_opfamily transaction_mode_list_or_empty
368 				OptTableFuncElementList TableFuncElementList opt_type_modifiers
369 				prep_type_clause
370 				execute_param_clause using_clause returning_clause
371 				opt_enum_val_list enum_val_list table_func_column_list
372 				create_generic_options alter_generic_options
373 				relation_expr_list dostmt_opt_list
374 				transform_element_list transform_type_list
375 
376 %type <list>	group_by_list
377 %type <node>	group_by_item empty_grouping_set rollup_clause cube_clause
378 %type <node>	grouping_sets_clause
379 
380 %type <list>	opt_fdw_options fdw_options
381 %type <defelt>	fdw_option
382 
383 %type <range>	OptTempTableName
384 %type <into>	into_clause create_as_target create_mv_target
385 
386 %type <defelt>	createfunc_opt_item common_func_opt_item dostmt_opt_item
387 %type <fun_param> func_arg func_arg_with_default table_func_column aggr_arg
388 %type <fun_param_mode> arg_class
389 %type <typnam>	func_return func_type
390 
391 %type <boolean>  opt_trusted opt_restart_seqs
392 %type <ival>	 OptTemp
393 %type <ival>	 OptNoLog
394 %type <oncommit> OnCommitOption
395 
396 %type <ival>	for_locking_strength
397 %type <node>	for_locking_item
398 %type <list>	for_locking_clause opt_for_locking_clause for_locking_items
399 %type <list>	locked_rels_list
400 %type <boolean>	all_or_distinct
401 
402 %type <node>	join_outer join_qual
403 %type <jtype>	join_type
404 
405 %type <list>	extract_list overlay_list position_list
406 %type <list>	substr_list trim_list
407 %type <list>	opt_interval interval_second
408 %type <node>	overlay_placing substr_from substr_for
409 
410 %type <boolean> opt_instead
411 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
412 %type <boolean> opt_freeze opt_default opt_recheck
413 %type <defelt>	opt_binary opt_oids copy_delimiter
414 
415 %type <boolean> copy_from opt_program
416 
417 %type <ival>	opt_column event cursor_options opt_hold opt_set_data
418 %type <objtype>	drop_type comment_type security_label_type
419 
420 %type <node>	fetch_args limit_clause select_limit_value
421 				offset_clause select_offset_value
422 				select_fetch_first_value I_or_F_const
423 %type <ival>	row_or_rows first_or_next
424 
425 %type <list>	OptSeqOptList SeqOptList
426 %type <defelt>	SeqOptElem
427 
428 %type <istmt>	insert_rest
429 %type <infer>	opt_conf_expr
430 %type <onconflict> opt_on_conflict
431 
432 %type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
433 				 SetResetClause FunctionSetResetClause
434 
435 %type <node>	TableElement TypedTableElement ConstraintElem TableFuncElement
436 %type <node>	columnDef columnOptions
437 %type <defelt>	def_elem reloption_elem old_aggr_elem operator_def_elem
438 %type <node>	def_arg columnElem where_clause where_or_current_clause
439 				a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
440 				columnref in_expr having_clause func_table array_expr
441 				ExclusionWhereClause
442 %type <list>	rowsfrom_item rowsfrom_list opt_col_def_list
443 %type <boolean> opt_ordinality
444 %type <list>	ExclusionConstraintList ExclusionConstraintElem
445 %type <list>	func_arg_list
446 %type <node>	func_arg_expr
447 %type <list>	row explicit_row implicit_row type_list array_expr_list
448 %type <node>	case_expr case_arg when_clause case_default
449 %type <list>	when_clause_list
450 %type <ival>	sub_type
451 %type <node>	ctext_expr
452 %type <value>	NumericOnly
453 %type <list>	NumericOnly_list
454 %type <alias>	alias_clause opt_alias_clause
455 %type <list>	func_alias_clause
456 %type <sortby>	sortby
457 %type <ielem>	index_elem
458 %type <node>	table_ref
459 %type <jexpr>	joined_table
460 %type <range>	relation_expr
461 %type <range>	relation_expr_opt_alias
462 %type <node>	tablesample_clause opt_repeatable_clause
463 %type <target>	target_el single_set_clause set_target insert_column_item
464 
465 %type <str>		generic_option_name
466 %type <node>	generic_option_arg
467 %type <defelt>	generic_option_elem alter_generic_option_elem
468 %type <list>	generic_option_list alter_generic_option_list
469 %type <str>		explain_option_name
470 %type <node>	explain_option_arg
471 %type <defelt>	explain_option_elem
472 %type <list>	explain_option_list
473 
474 %type <ival>	reindex_target_type reindex_target_multitable
475 %type <ival>	reindex_option_list reindex_option_elem
476 
477 %type <node>	copy_generic_opt_arg copy_generic_opt_arg_list_item
478 %type <defelt>	copy_generic_opt_elem
479 %type <list>	copy_generic_opt_list copy_generic_opt_arg_list
480 %type <list>	copy_options
481 
482 %type <typnam>	Typename SimpleTypename ConstTypename
483 				GenericType Numeric opt_float
484 				Character ConstCharacter
485 				CharacterWithLength CharacterWithoutLength
486 				ConstDatetime ConstInterval
487 				Bit ConstBit BitWithLength BitWithoutLength
488 %type <str>		character
489 %type <str>		extract_arg
490 %type <str>		opt_charset
491 %type <boolean> opt_varying opt_timezone opt_no_inherit
492 
493 %type <ival>	Iconst SignedIconst
494 %type <str>		Sconst comment_text notify_payload
495 %type <str>		RoleId opt_boolean_or_string
496 %type <list>	var_list
497 %type <str>		ColId ColLabel var_name type_function_name param_name
498 %type <str>		NonReservedWord NonReservedWord_or_Sconst
499 %type <str>		createdb_opt_name
500 %type <node>	var_value zone_value
501 %type <node>	auth_ident RoleSpec opt_granted_by
502 
503 %type <keyword> unreserved_keyword type_func_name_keyword
504 %type <keyword> col_name_keyword reserved_keyword
505 
506 %type <node>	TableConstraint TableLikeClause
507 %type <ival>	TableLikeOptionList TableLikeOption
508 %type <list>	ColQualList
509 %type <node>	ColConstraint ColConstraintElem ConstraintAttr
510 %type <ival>	key_actions key_delete key_match key_update key_action
511 %type <ival>	ConstraintAttributeSpec ConstraintAttributeElem
512 %type <str>		ExistingIndex
513 
514 %type <list>	constraints_set_list
515 %type <boolean> constraints_set_mode
516 %type <str>		OptTableSpace OptConsTableSpace
517 %type <node>	OptTableSpaceOwner
518 %type <ival>	opt_check_option
519 
520 %type <str>		opt_provider security_label
521 
522 %type <target>	xml_attribute_el
523 %type <list>	xml_attribute_list xml_attributes
524 %type <node>	xml_root_version opt_xml_root_standalone
525 %type <node>	xmlexists_argument
526 %type <ival>	document_or_content
527 %type <boolean> xml_whitespace_option
528 
529 %type <node>	func_application func_expr_common_subexpr
530 %type <node>	func_expr func_expr_windowless
531 %type <node>	common_table_expr
532 %type <with>	with_clause opt_with_clause
533 %type <list>	cte_list
534 
535 %type <list>	within_group_clause
536 %type <node>	filter_clause
537 %type <list>	window_clause window_definition_list opt_partition_clause
538 %type <windef>	window_definition over_clause window_specification
539 				opt_frame_clause frame_extent frame_bound
540 %type <str>		opt_existing_window_name
541 %type <boolean> opt_if_not_exists
542 
543 /*
544  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
545  * They must be listed first so that their numeric codes do not depend on
546  * the set of keywords.  PL/pgsql depends on this so that it can share the
547  * same lexer.  If you add/change tokens here, fix PL/pgsql to match!
548  *
549  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
550  * parse errors.  It is needed by PL/pgsql.
551  */
552 %token <str>	IDENT FCONST SCONST BCONST XCONST Op
553 %token <ival>	ICONST PARAM
554 %token			TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
555 %token			LESS_EQUALS GREATER_EQUALS NOT_EQUALS
556 
557 /*
558  * If you want to make any keyword changes, update the keyword table in
559  * src/include/parser/kwlist.h and add new keywords to the appropriate one
560  * of the reserved-or-not-so-reserved keyword lists, below; search
561  * this file for "Keyword category lists".
562  */
563 
564 /* ordinary key words in alphabetical order */
565 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
566 	AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
567 	ASSERTION ASSIGNMENT ASYMMETRIC AT ATTRIBUTE AUTHORIZATION
568 
569 	BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
570 	BOOLEAN_P BOTH BY
571 
572 	CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
573 	CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
574 	CLUSTER COALESCE COLLATE COLLATION COLUMN COMMENT COMMENTS COMMIT
575 	COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION CONSTRAINT
576 	CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE
577 	CROSS CSV CUBE CURRENT_P
578 	CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
579 	CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
580 
581 	DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
582 	DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DESC
583 	DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
584 
585 	EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EVENT EXCEPT
586 	EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
587 	EXTENSION EXTERNAL EXTRACT
588 
589 	FALSE_P FAMILY FETCH FILTER FIRST_P FLOAT_P FOLLOWING FOR
590 	FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
591 
592 	GLOBAL GRANT GRANTED GREATEST GROUP_P GROUPING
593 
594 	HANDLER HAVING HEADER_P HOLD HOUR_P
595 
596 	IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P
597 	INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
598 	INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
599 	INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
600 
601 	JOIN
602 
603 	KEY
604 
605 	LABEL LANGUAGE LARGE_P LAST_P LATERAL_P
606 	LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL
607 	LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED
608 
609 	MAPPING MATCH MATERIALIZED MAXVALUE METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE
610 
611 	NAME_P NAMES NATIONAL NATURAL NCHAR NEXT NO NONE
612 	NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
613 	NULLS_P NUMERIC
614 
615 	OBJECT_P OF OFF OFFSET OIDS ON ONLY OPERATOR OPTION OPTIONS OR
616 	ORDER ORDINALITY OUT_P OUTER_P OVER OVERLAPS OVERLAY OWNED OWNER
617 
618 	PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PLACING PLANS POLICY
619 	POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
620 	PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROGRAM
621 
622 	QUOTE
623 
624 	RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REFRESH REINDEX
625 	RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
626 	RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP
627 	ROW ROWS RULE
628 
629 	SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
630 	SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
631 	SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P START
632 	STATEMENT STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING
633 	SYMMETRIC SYSID SYSTEM_P
634 
635 	TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
636 	TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM TREAT TRIGGER TRIM TRUE_P
637 	TRUNCATE TRUSTED TYPE_P TYPES_P
638 
639 	UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNLOGGED
640 	UNTIL UPDATE USER USING
641 
642 	VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
643 	VERBOSE VERSION_P VIEW VIEWS VOLATILE
644 
645 	WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE
646 
647 	XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLPARSE
648 	XMLPI XMLROOT XMLSERIALIZE
649 
650 	YEAR_P YES_P
651 
652 	ZONE
653 
654 /*
655  * The grammar thinks these are keywords, but they are not in the kwlist.h
656  * list and so can never be entered directly.  The filter in parser.c
657  * creates these tokens when required (based on looking one token ahead).
658  *
659  * NOT_LA exists so that productions such as NOT LIKE can be given the same
660  * precedence as LIKE; otherwise they'd effectively have the same precedence
661  * as NOT, at least with respect to their left-hand subexpression.
662  * NULLS_LA and WITH_LA are needed to make the grammar LALR(1).
663  */
664 %token		NOT_LA NULLS_LA WITH_LA
665 
666 
667 /* Precedence: lowest to highest */
668 %nonassoc	SET				/* see relation_expr_opt_alias */
669 %left		UNION EXCEPT
670 %left		INTERSECT
671 %left		OR
672 %left		AND
673 %right		NOT
674 %nonassoc	IS ISNULL NOTNULL	/* IS sets precedence for IS NULL, etc */
675 %nonassoc	'<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
676 %nonassoc	BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA
677 %nonassoc	ESCAPE			/* ESCAPE must be just above LIKE/ILIKE/SIMILAR */
678 %left		POSTFIXOP		/* dummy for postfix Op rules */
679 /*
680  * To support target_el without AS, we must give IDENT an explicit priority
681  * between POSTFIXOP and Op.  We can safely assign the same priority to
682  * various unreserved keywords as needed to resolve ambiguities (this can't
683  * have any bad effects since obviously the keywords will still behave the
684  * same as if they weren't keywords).  We need to do this for PARTITION,
685  * RANGE, ROWS to support opt_existing_window_name; and for RANGE, ROWS
686  * so that they can follow a_expr without creating postfix-operator problems;
687  * and for NULL so that it can follow b_expr in ColQualList without creating
688  * postfix-operator problems.
689  *
690  * To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
691  * an explicit priority lower than '(', so that a rule with CUBE '(' will shift
692  * rather than reducing a conflicting rule that takes CUBE as a function name.
693  * Using the same precedence as IDENT seems right for the reasons given above.
694  *
695  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
696  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
697  * there is no principled way to distinguish these from the productions
698  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
699  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
700  * appear to cause UNBOUNDED to be treated differently from other unreserved
701  * keywords anywhere else in the grammar, but it's definitely risky.  We can
702  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
703  */
704 %nonassoc	UNBOUNDED		/* ideally should have same precedence as IDENT */
705 %nonassoc	IDENT NULL_P PARTITION RANGE ROWS PRECEDING FOLLOWING CUBE ROLLUP
706 %left		Op OPERATOR		/* multi-character ops and user-defined operators */
707 %left		'+' '-'
708 %left		'*' '/' '%'
709 %left		'^'
710 /* Unary Operators */
711 %left		AT				/* sets precedence for AT TIME ZONE */
712 %left		COLLATE
713 %right		UMINUS
714 %left		'[' ']'
715 %left		'(' ')'
716 %left		TYPECAST
717 %left		'.'
718 /*
719  * These might seem to be low-precedence, but actually they are not part
720  * of the arithmetic hierarchy at all in their use as JOIN operators.
721  * We make them high-precedence to support their use as function names.
722  * They wouldn't be given a precedence at all, were it not that we need
723  * left-associativity among the JOIN rules themselves.
724  */
725 %left		JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
726 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
727 %right		PRESERVE STRIP_P
728 
729 %%
730 
731 /*
732  *	The target production for the whole parse.
733  */
734 stmtblock:	stmtmulti
735 			{
736 				pg_yyget_extra(yyscanner)->parsetree = $1;
737 			}
738 		;
739 
740 /* the thrashing around here is to discard "empty" statements... */
741 stmtmulti:	stmtmulti ';' stmt
742 				{
743 					if ($3 != NULL)
744 						$$ = lappend($1, $3);
745 					else
746 						$$ = $1;
747 				}
748 			| stmt
749 				{
750 					if ($1 != NULL)
751 						$$ = list_make1($1);
752 					else
753 						$$ = NIL;
754 				}
755 		;
756 
757 stmt :
758 			AlterEventTrigStmt
759 			| AlterDatabaseStmt
760 			| AlterDatabaseSetStmt
761 			| AlterDefaultPrivilegesStmt
762 			| AlterDomainStmt
763 			| AlterEnumStmt
764 			| AlterExtensionStmt
765 			| AlterExtensionContentsStmt
766 			| AlterFdwStmt
767 			| AlterForeignServerStmt
768 			| AlterForeignTableStmt
769 			| AlterFunctionStmt
770 			| AlterGroupStmt
771 			| AlterObjectDependsStmt
772 			| AlterObjectSchemaStmt
773 			| AlterOwnerStmt
774 			| AlterOperatorStmt
775 			| AlterPolicyStmt
776 			| AlterSeqStmt
777 			| AlterSystemStmt
778 			| AlterTableStmt
779 			| AlterTblSpcStmt
780 			| AlterCompositeTypeStmt
781 			| AlterRoleSetStmt
782 			| AlterRoleStmt
783 			| AlterTSConfigurationStmt
784 			| AlterTSDictionaryStmt
785 			| AlterUserMappingStmt
786 			| AlterUserSetStmt
787 			| AlterUserStmt
788 			| AnalyzeStmt
789 			| CheckPointStmt
790 			| ClosePortalStmt
791 			| ClusterStmt
792 			| CommentStmt
793 			| ConstraintsSetStmt
794 			| CopyStmt
795 			| CreateAmStmt
796 			| CreateAsStmt
797 			| CreateAssertStmt
798 			| CreateCastStmt
799 			| CreateConversionStmt
800 			| CreateDomainStmt
801 			| CreateExtensionStmt
802 			| CreateFdwStmt
803 			| CreateForeignServerStmt
804 			| CreateForeignTableStmt
805 			| CreateFunctionStmt
806 			| CreateGroupStmt
807 			| CreateMatViewStmt
808 			| CreateOpClassStmt
809 			| CreateOpFamilyStmt
810 			| AlterOpFamilyStmt
811 			| CreatePolicyStmt
812 			| CreatePLangStmt
813 			| CreateSchemaStmt
814 			| CreateSeqStmt
815 			| CreateStmt
816 			| CreateTableSpaceStmt
817 			| CreateTransformStmt
818 			| CreateTrigStmt
819 			| CreateEventTrigStmt
820 			| CreateRoleStmt
821 			| CreateUserStmt
822 			| CreateUserMappingStmt
823 			| CreatedbStmt
824 			| DeallocateStmt
825 			| DeclareCursorStmt
826 			| DefineStmt
827 			| DeleteStmt
828 			| DiscardStmt
829 			| DoStmt
830 			| DropAssertStmt
831 			| DropCastStmt
832 			| DropFdwStmt
833 			| DropForeignServerStmt
834 			| DropGroupStmt
835 			| DropOpClassStmt
836 			| DropOpFamilyStmt
837 			| DropOwnedStmt
838 			| DropPolicyStmt
839 			| DropPLangStmt
840 			| DropRuleStmt
841 			| DropStmt
842 			| DropTableSpaceStmt
843 			| DropTransformStmt
844 			| DropTrigStmt
845 			| DropRoleStmt
846 			| DropUserStmt
847 			| DropUserMappingStmt
848 			| DropdbStmt
849 			| ExecuteStmt
850 			| ExplainStmt
851 			| FetchStmt
852 			| GrantStmt
853 			| GrantRoleStmt
854 			| ImportForeignSchemaStmt
855 			| IndexStmt
856 			| InsertStmt
857 			| ListenStmt
858 			| RefreshMatViewStmt
859 			| LoadStmt
860 			| LockStmt
861 			| NotifyStmt
862 			| PrepareStmt
863 			| ReassignOwnedStmt
864 			| ReindexStmt
865 			| RemoveAggrStmt
866 			| RemoveFuncStmt
867 			| RemoveOperStmt
868 			| RenameStmt
869 			| RevokeStmt
870 			| RevokeRoleStmt
871 			| RuleStmt
872 			| SecLabelStmt
873 			| SelectStmt
874 			| TransactionStmt
875 			| TruncateStmt
876 			| UnlistenStmt
877 			| UpdateStmt
878 			| VacuumStmt
879 			| VariableResetStmt
880 			| VariableSetStmt
881 			| VariableShowStmt
882 			| ViewStmt
883 			| /*EMPTY*/
884 				{ $$ = NULL; }
885 		;
886 
887 /*****************************************************************************
888  *
889  * Create a new Postgres DBMS role
890  *
891  *****************************************************************************/
892 
893 CreateRoleStmt:
894 			CREATE ROLE RoleId opt_with OptRoleList
895 				{
896 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
897 					n->stmt_type = ROLESTMT_ROLE;
898 					n->role = $3;
899 					n->options = $5;
900 					$$ = (Node *)n;
901 				}
902 		;
903 
904 
905 opt_with:	WITH									{}
906 			| WITH_LA								{}
907 			| /*EMPTY*/								{}
908 		;
909 
910 /*
911  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
912  * for backwards compatibility).  Note: the only option required by SQL99
913  * is "WITH ADMIN name".
914  */
915 OptRoleList:
916 			OptRoleList CreateOptRoleElem			{ $$ = lappend($1, $2); }
917 			| /* EMPTY */							{ $$ = NIL; }
918 		;
919 
920 AlterOptRoleList:
921 			AlterOptRoleList AlterOptRoleElem		{ $$ = lappend($1, $2); }
922 			| /* EMPTY */							{ $$ = NIL; }
923 		;
924 
925 AlterOptRoleElem:
926 			PASSWORD Sconst
927 				{
928 					$$ = makeDefElem("password",
929 									 (Node *)makeString($2));
930 				}
931 			| PASSWORD NULL_P
932 				{
933 					$$ = makeDefElem("password", NULL);
934 				}
935 			| ENCRYPTED PASSWORD Sconst
936 				{
937 					$$ = makeDefElem("encryptedPassword",
938 									 (Node *)makeString($3));
939 				}
940 			| UNENCRYPTED PASSWORD Sconst
941 				{
942 					$$ = makeDefElem("unencryptedPassword",
943 									 (Node *)makeString($3));
944 				}
945 			| INHERIT
946 				{
947 					$$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
948 				}
949 			| CONNECTION LIMIT SignedIconst
950 				{
951 					$$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
952 				}
953 			| VALID UNTIL Sconst
954 				{
955 					$$ = makeDefElem("validUntil", (Node *)makeString($3));
956 				}
957 		/*	Supported but not documented for roles, for use by ALTER GROUP. */
958 			| USER role_list
959 				{
960 					$$ = makeDefElem("rolemembers", (Node *)$2);
961 				}
962 			| IDENT
963 				{
964 					/*
965 					 * We handle identifiers that aren't parser keywords with
966 					 * the following special-case codes, to avoid bloating the
967 					 * size of the main parser.
968 					 */
969 					if (strcmp($1, "superuser") == 0)
970 						$$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
971 					else if (strcmp($1, "nosuperuser") == 0)
972 						$$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
973 					else if (strcmp($1, "createrole") == 0)
974 						$$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
975 					else if (strcmp($1, "nocreaterole") == 0)
976 						$$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
977 					else if (strcmp($1, "replication") == 0)
978 						$$ = makeDefElem("isreplication", (Node *)makeInteger(TRUE));
979 					else if (strcmp($1, "noreplication") == 0)
980 						$$ = makeDefElem("isreplication", (Node *)makeInteger(FALSE));
981 					else if (strcmp($1, "createdb") == 0)
982 						$$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
983 					else if (strcmp($1, "nocreatedb") == 0)
984 						$$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
985 					else if (strcmp($1, "login") == 0)
986 						$$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
987 					else if (strcmp($1, "nologin") == 0)
988 						$$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
989 					else if (strcmp($1, "bypassrls") == 0)
990 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(TRUE));
991 					else if (strcmp($1, "nobypassrls") == 0)
992 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(FALSE));
993 					else if (strcmp($1, "noinherit") == 0)
994 					{
995 						/*
996 						 * Note that INHERIT is a keyword, so it's handled by main parser, but
997 						 * NOINHERIT is handled here.
998 						 */
999 						$$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
1000 					}
1001 					else
1002 						ereport(ERROR,
1003 								(errcode(ERRCODE_SYNTAX_ERROR),
1004 								 errmsg("unrecognized role option \"%s\"", $1),
1005 									 parser_errposition(@1)));
1006 				}
1007 		;
1008 
1009 CreateOptRoleElem:
1010 			AlterOptRoleElem			{ $$ = $1; }
1011 			/* The following are not supported by ALTER ROLE/USER/GROUP */
1012 			| SYSID Iconst
1013 				{
1014 					$$ = makeDefElem("sysid", (Node *)makeInteger($2));
1015 				}
1016 			| ADMIN role_list
1017 				{
1018 					$$ = makeDefElem("adminmembers", (Node *)$2);
1019 				}
1020 			| ROLE role_list
1021 				{
1022 					$$ = makeDefElem("rolemembers", (Node *)$2);
1023 				}
1024 			| IN_P ROLE role_list
1025 				{
1026 					$$ = makeDefElem("addroleto", (Node *)$3);
1027 				}
1028 			| IN_P GROUP_P role_list
1029 				{
1030 					$$ = makeDefElem("addroleto", (Node *)$3);
1031 				}
1032 		;
1033 
1034 
1035 /*****************************************************************************
1036  *
1037  * Create a new Postgres DBMS user (role with implied login ability)
1038  *
1039  *****************************************************************************/
1040 
1041 CreateUserStmt:
1042 			CREATE USER RoleId opt_with OptRoleList
1043 				{
1044 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1045 					n->stmt_type = ROLESTMT_USER;
1046 					n->role = $3;
1047 					n->options = $5;
1048 					$$ = (Node *)n;
1049 				}
1050 		;
1051 
1052 
1053 /*****************************************************************************
1054  *
1055  * Alter a postgresql DBMS role
1056  *
1057  *****************************************************************************/
1058 
1059 AlterRoleStmt:
1060 			ALTER ROLE RoleSpec opt_with AlterOptRoleList
1061 				 {
1062 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1063 					n->role = $3;
1064 					n->action = +1;	/* add, if there are members */
1065 					n->options = $5;
1066 					$$ = (Node *)n;
1067 				 }
1068 		;
1069 
1070 opt_in_database:
1071 			   /* EMPTY */					{ $$ = NULL; }
1072 			| IN_P DATABASE database_name	{ $$ = $3; }
1073 		;
1074 
1075 AlterRoleSetStmt:
1076 			ALTER ROLE RoleSpec opt_in_database SetResetClause
1077 				{
1078 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1079 					n->role = $3;
1080 					n->database = $4;
1081 					n->setstmt = $5;
1082 					$$ = (Node *)n;
1083 				}
1084 			| ALTER ROLE ALL opt_in_database SetResetClause
1085 				{
1086 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1087 					n->role = NULL;
1088 					n->database = $4;
1089 					n->setstmt = $5;
1090 					$$ = (Node *)n;
1091 				}
1092 		;
1093 
1094 
1095 /*****************************************************************************
1096  *
1097  * Alter a postgresql DBMS user
1098  *
1099  *****************************************************************************/
1100 
1101 AlterUserStmt:
1102 			ALTER USER RoleSpec opt_with AlterOptRoleList
1103 				 {
1104 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1105 					n->role = $3;
1106 					n->action = +1;	/* add, if there are members */
1107 					n->options = $5;
1108 					$$ = (Node *)n;
1109 				 }
1110 		;
1111 
1112 
1113 AlterUserSetStmt:
1114 			ALTER USER RoleSpec opt_in_database SetResetClause
1115 				{
1116 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1117 					n->role = $3;
1118 					n->database = $4;
1119 					n->setstmt = $5;
1120 					$$ = (Node *)n;
1121 				}
1122 			| ALTER USER ALL opt_in_database SetResetClause
1123 				{
1124 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1125 					n->role = NULL;
1126 					n->database = $4;
1127 					n->setstmt = $5;
1128 					$$ = (Node *)n;
1129 				}
1130 			;
1131 
1132 
1133 /*****************************************************************************
1134  *
1135  * Drop a postgresql DBMS role
1136  *
1137  * XXX Ideally this would have CASCADE/RESTRICT options, but a role
1138  * might own objects in multiple databases, and there is presently no way to
1139  * implement cascading to other databases.  So we always behave as RESTRICT.
1140  *****************************************************************************/
1141 
1142 DropRoleStmt:
1143 			DROP ROLE role_list
1144 				{
1145 					DropRoleStmt *n = makeNode(DropRoleStmt);
1146 					n->missing_ok = FALSE;
1147 					n->roles = $3;
1148 					$$ = (Node *)n;
1149 				}
1150 			| DROP ROLE IF_P EXISTS role_list
1151 				{
1152 					DropRoleStmt *n = makeNode(DropRoleStmt);
1153 					n->missing_ok = TRUE;
1154 					n->roles = $5;
1155 					$$ = (Node *)n;
1156 				}
1157 			;
1158 
1159 /*****************************************************************************
1160  *
1161  * Drop a postgresql DBMS user
1162  *
1163  * XXX As with DROP ROLE, no CASCADE/RESTRICT here.
1164  *****************************************************************************/
1165 
1166 DropUserStmt:
1167 			DROP USER role_list
1168 				{
1169 					DropRoleStmt *n = makeNode(DropRoleStmt);
1170 					n->missing_ok = FALSE;
1171 					n->roles = $3;
1172 					$$ = (Node *)n;
1173 				}
1174 			| DROP USER IF_P EXISTS role_list
1175 				{
1176 					DropRoleStmt *n = makeNode(DropRoleStmt);
1177 					n->roles = $5;
1178 					n->missing_ok = TRUE;
1179 					$$ = (Node *)n;
1180 				}
1181 			;
1182 
1183 
1184 /*****************************************************************************
1185  *
1186  * Create a postgresql group (role without login ability)
1187  *
1188  *****************************************************************************/
1189 
1190 CreateGroupStmt:
1191 			CREATE GROUP_P RoleId opt_with OptRoleList
1192 				{
1193 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1194 					n->stmt_type = ROLESTMT_GROUP;
1195 					n->role = $3;
1196 					n->options = $5;
1197 					$$ = (Node *)n;
1198 				}
1199 		;
1200 
1201 
1202 /*****************************************************************************
1203  *
1204  * Alter a postgresql group
1205  *
1206  *****************************************************************************/
1207 
1208 AlterGroupStmt:
1209 			ALTER GROUP_P RoleSpec add_drop USER role_list
1210 				{
1211 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1212 					n->role = $3;
1213 					n->action = $4;
1214 					n->options = list_make1(makeDefElem("rolemembers",
1215 														(Node *)$6));
1216 					$$ = (Node *)n;
1217 				}
1218 		;
1219 
1220 add_drop:	ADD_P									{ $$ = +1; }
1221 			| DROP									{ $$ = -1; }
1222 		;
1223 
1224 
1225 /*****************************************************************************
1226  *
1227  * Drop a postgresql group
1228  *
1229  * XXX As with DROP ROLE, no CASCADE/RESTRICT here.
1230  *****************************************************************************/
1231 
1232 DropGroupStmt:
1233 			DROP GROUP_P role_list
1234 				{
1235 					DropRoleStmt *n = makeNode(DropRoleStmt);
1236 					n->missing_ok = FALSE;
1237 					n->roles = $3;
1238 					$$ = (Node *)n;
1239 				}
1240 			| DROP GROUP_P IF_P EXISTS role_list
1241 				{
1242 					DropRoleStmt *n = makeNode(DropRoleStmt);
1243 					n->missing_ok = TRUE;
1244 					n->roles = $5;
1245 					$$ = (Node *)n;
1246 				}
1247 		;
1248 
1249 
1250 /*****************************************************************************
1251  *
1252  * Manipulate a schema
1253  *
1254  *****************************************************************************/
1255 
1256 CreateSchemaStmt:
1257 			CREATE SCHEMA OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1258 				{
1259 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1260 					/* One can omit the schema name or the authorization id. */
1261 					n->schemaname = $3;
1262 					n->authrole = $5;
1263 					n->schemaElts = $6;
1264 					n->if_not_exists = false;
1265 					$$ = (Node *)n;
1266 				}
1267 			| CREATE SCHEMA ColId OptSchemaEltList
1268 				{
1269 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1270 					/* ...but not both */
1271 					n->schemaname = $3;
1272 					n->authrole = NULL;
1273 					n->schemaElts = $4;
1274 					n->if_not_exists = false;
1275 					$$ = (Node *)n;
1276 				}
1277 			| CREATE SCHEMA IF_P NOT EXISTS OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1278 				{
1279 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1280 					/* schema name can be omitted here, too */
1281 					n->schemaname = $6;
1282 					n->authrole = $8;
1283 					if ($9 != NIL)
1284 						ereport(ERROR,
1285 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1286 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1287 								 parser_errposition(@9)));
1288 					n->schemaElts = $9;
1289 					n->if_not_exists = true;
1290 					$$ = (Node *)n;
1291 				}
1292 			| CREATE SCHEMA IF_P NOT EXISTS ColId OptSchemaEltList
1293 				{
1294 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1295 					/* ...but not here */
1296 					n->schemaname = $6;
1297 					n->authrole = NULL;
1298 					if ($7 != NIL)
1299 						ereport(ERROR,
1300 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1301 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1302 								 parser_errposition(@7)));
1303 					n->schemaElts = $7;
1304 					n->if_not_exists = true;
1305 					$$ = (Node *)n;
1306 				}
1307 		;
1308 
1309 OptSchemaName:
1310 			ColId									{ $$ = $1; }
1311 			| /* EMPTY */							{ $$ = NULL; }
1312 		;
1313 
1314 OptSchemaEltList:
1315 			OptSchemaEltList schema_stmt
1316 				{
1317 					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
1318 						@$ = @2;
1319 					$$ = lappend($1, $2);
1320 				}
1321 			| /* EMPTY */
1322 				{ $$ = NIL; }
1323 		;
1324 
1325 /*
1326  *	schema_stmt are the ones that can show up inside a CREATE SCHEMA
1327  *	statement (in addition to by themselves).
1328  */
1329 schema_stmt:
1330 			CreateStmt
1331 			| IndexStmt
1332 			| CreateSeqStmt
1333 			| CreateTrigStmt
1334 			| GrantStmt
1335 			| ViewStmt
1336 		;
1337 
1338 
1339 /*****************************************************************************
1340  *
1341  * Set PG internal variable
1342  *	  SET name TO 'var_value'
1343  * Include SQL syntax (thomas 1997-10-22):
1344  *	  SET TIME ZONE 'var_value'
1345  *
1346  *****************************************************************************/
1347 
1348 VariableSetStmt:
1349 			SET set_rest
1350 				{
1351 					VariableSetStmt *n = $2;
1352 					n->is_local = false;
1353 					$$ = (Node *) n;
1354 				}
1355 			| SET LOCAL set_rest
1356 				{
1357 					VariableSetStmt *n = $3;
1358 					n->is_local = true;
1359 					$$ = (Node *) n;
1360 				}
1361 			| SET SESSION set_rest
1362 				{
1363 					VariableSetStmt *n = $3;
1364 					n->is_local = false;
1365 					$$ = (Node *) n;
1366 				}
1367 		;
1368 
1369 set_rest:
1370 			TRANSACTION transaction_mode_list
1371 				{
1372 					VariableSetStmt *n = makeNode(VariableSetStmt);
1373 					n->kind = VAR_SET_MULTI;
1374 					n->name = "TRANSACTION";
1375 					n->args = $2;
1376 					$$ = n;
1377 				}
1378 			| SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1379 				{
1380 					VariableSetStmt *n = makeNode(VariableSetStmt);
1381 					n->kind = VAR_SET_MULTI;
1382 					n->name = "SESSION CHARACTERISTICS";
1383 					n->args = $5;
1384 					$$ = n;
1385 				}
1386 			| set_rest_more
1387 			;
1388 
1389 generic_set:
1390 			var_name TO var_list
1391 				{
1392 					VariableSetStmt *n = makeNode(VariableSetStmt);
1393 					n->kind = VAR_SET_VALUE;
1394 					n->name = $1;
1395 					n->args = $3;
1396 					$$ = n;
1397 				}
1398 			| var_name '=' var_list
1399 				{
1400 					VariableSetStmt *n = makeNode(VariableSetStmt);
1401 					n->kind = VAR_SET_VALUE;
1402 					n->name = $1;
1403 					n->args = $3;
1404 					$$ = n;
1405 				}
1406 			| var_name TO DEFAULT
1407 				{
1408 					VariableSetStmt *n = makeNode(VariableSetStmt);
1409 					n->kind = VAR_SET_DEFAULT;
1410 					n->name = $1;
1411 					$$ = n;
1412 				}
1413 			| var_name '=' DEFAULT
1414 				{
1415 					VariableSetStmt *n = makeNode(VariableSetStmt);
1416 					n->kind = VAR_SET_DEFAULT;
1417 					n->name = $1;
1418 					$$ = n;
1419 				}
1420 		;
1421 
1422 set_rest_more:	/* Generic SET syntaxes: */
1423 			generic_set 						{$$ = $1;}
1424 			| var_name FROM CURRENT_P
1425 				{
1426 					VariableSetStmt *n = makeNode(VariableSetStmt);
1427 					n->kind = VAR_SET_CURRENT;
1428 					n->name = $1;
1429 					$$ = n;
1430 				}
1431 			/* Special syntaxes mandated by SQL standard: */
1432 			| TIME ZONE zone_value
1433 				{
1434 					VariableSetStmt *n = makeNode(VariableSetStmt);
1435 					n->kind = VAR_SET_VALUE;
1436 					n->name = "timezone";
1437 					if ($3 != NULL)
1438 						n->args = list_make1($3);
1439 					else
1440 						n->kind = VAR_SET_DEFAULT;
1441 					$$ = n;
1442 				}
1443 			| CATALOG_P Sconst
1444 				{
1445 					ereport(ERROR,
1446 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1447 							 errmsg("current database cannot be changed"),
1448 							 parser_errposition(@2)));
1449 					$$ = NULL; /*not reached*/
1450 				}
1451 			| SCHEMA Sconst
1452 				{
1453 					VariableSetStmt *n = makeNode(VariableSetStmt);
1454 					n->kind = VAR_SET_VALUE;
1455 					n->name = "search_path";
1456 					n->args = list_make1(makeStringConst($2, @2));
1457 					$$ = n;
1458 				}
1459 			| NAMES opt_encoding
1460 				{
1461 					VariableSetStmt *n = makeNode(VariableSetStmt);
1462 					n->kind = VAR_SET_VALUE;
1463 					n->name = "client_encoding";
1464 					if ($2 != NULL)
1465 						n->args = list_make1(makeStringConst($2, @2));
1466 					else
1467 						n->kind = VAR_SET_DEFAULT;
1468 					$$ = n;
1469 				}
1470 			| ROLE NonReservedWord_or_Sconst
1471 				{
1472 					VariableSetStmt *n = makeNode(VariableSetStmt);
1473 					n->kind = VAR_SET_VALUE;
1474 					n->name = "role";
1475 					n->args = list_make1(makeStringConst($2, @2));
1476 					$$ = n;
1477 				}
1478 			| SESSION AUTHORIZATION NonReservedWord_or_Sconst
1479 				{
1480 					VariableSetStmt *n = makeNode(VariableSetStmt);
1481 					n->kind = VAR_SET_VALUE;
1482 					n->name = "session_authorization";
1483 					n->args = list_make1(makeStringConst($3, @3));
1484 					$$ = n;
1485 				}
1486 			| SESSION AUTHORIZATION DEFAULT
1487 				{
1488 					VariableSetStmt *n = makeNode(VariableSetStmt);
1489 					n->kind = VAR_SET_DEFAULT;
1490 					n->name = "session_authorization";
1491 					$$ = n;
1492 				}
1493 			| XML_P OPTION document_or_content
1494 				{
1495 					VariableSetStmt *n = makeNode(VariableSetStmt);
1496 					n->kind = VAR_SET_VALUE;
1497 					n->name = "xmloption";
1498 					n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1499 					$$ = n;
1500 				}
1501 			/* Special syntaxes invented by PostgreSQL: */
1502 			| TRANSACTION SNAPSHOT Sconst
1503 				{
1504 					VariableSetStmt *n = makeNode(VariableSetStmt);
1505 					n->kind = VAR_SET_MULTI;
1506 					n->name = "TRANSACTION SNAPSHOT";
1507 					n->args = list_make1(makeStringConst($3, @3));
1508 					$$ = n;
1509 				}
1510 		;
1511 
1512 var_name:	ColId								{ $$ = $1; }
1513 			| var_name '.' ColId
1514 				{ $$ = psprintf("%s.%s", $1, $3); }
1515 		;
1516 
1517 var_list:	var_value								{ $$ = list_make1($1); }
1518 			| var_list ',' var_value				{ $$ = lappend($1, $3); }
1519 		;
1520 
1521 var_value:	opt_boolean_or_string
1522 				{ $$ = makeStringConst($1, @1); }
1523 			| NumericOnly
1524 				{ $$ = makeAConst($1, @1); }
1525 		;
1526 
1527 iso_level:	READ UNCOMMITTED						{ $$ = "read uncommitted"; }
1528 			| READ COMMITTED						{ $$ = "read committed"; }
1529 			| REPEATABLE READ						{ $$ = "repeatable read"; }
1530 			| SERIALIZABLE							{ $$ = "serializable"; }
1531 		;
1532 
1533 opt_boolean_or_string:
1534 			TRUE_P									{ $$ = "true"; }
1535 			| FALSE_P								{ $$ = "false"; }
1536 			| ON									{ $$ = "on"; }
1537 			/*
1538 			 * OFF is also accepted as a boolean value, but is handled by
1539 			 * the NonReservedWord rule.  The action for booleans and strings
1540 			 * is the same, so we don't need to distinguish them here.
1541 			 */
1542 			| NonReservedWord_or_Sconst				{ $$ = $1; }
1543 		;
1544 
1545 /* Timezone values can be:
1546  * - a string such as 'pst8pdt'
1547  * - an identifier such as "pst8pdt"
1548  * - an integer or floating point number
1549  * - a time interval per SQL99
1550  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1551  * so use IDENT (meaning we reject anything that is a key word).
1552  */
1553 zone_value:
1554 			Sconst
1555 				{
1556 					$$ = makeStringConst($1, @1);
1557 				}
1558 			| IDENT
1559 				{
1560 					$$ = makeStringConst($1, @1);
1561 				}
1562 			| ConstInterval Sconst opt_interval
1563 				{
1564 					TypeName *t = $1;
1565 					if ($3 != NIL)
1566 					{
1567 						A_Const *n = (A_Const *) linitial($3);
1568 						if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1569 							ereport(ERROR,
1570 									(errcode(ERRCODE_SYNTAX_ERROR),
1571 									 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1572 									 parser_errposition(@3)));
1573 					}
1574 					t->typmods = $3;
1575 					$$ = makeStringConstCast($2, @2, t);
1576 				}
1577 			| ConstInterval '(' Iconst ')' Sconst
1578 				{
1579 					TypeName *t = $1;
1580 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1581 											makeIntConst($3, @3));
1582 					$$ = makeStringConstCast($5, @5, t);
1583 				}
1584 			| NumericOnly							{ $$ = makeAConst($1, @1); }
1585 			| DEFAULT								{ $$ = NULL; }
1586 			| LOCAL									{ $$ = NULL; }
1587 		;
1588 
1589 opt_encoding:
1590 			Sconst									{ $$ = $1; }
1591 			| DEFAULT								{ $$ = NULL; }
1592 			| /*EMPTY*/								{ $$ = NULL; }
1593 		;
1594 
1595 NonReservedWord_or_Sconst:
1596 			NonReservedWord							{ $$ = $1; }
1597 			| Sconst								{ $$ = $1; }
1598 		;
1599 
1600 VariableResetStmt:
1601 			RESET reset_rest						{ $$ = (Node *) $2; }
1602 		;
1603 
1604 reset_rest:
1605 			generic_reset							{ $$ = $1; }
1606 			| TIME ZONE
1607 				{
1608 					VariableSetStmt *n = makeNode(VariableSetStmt);
1609 					n->kind = VAR_RESET;
1610 					n->name = "timezone";
1611 					$$ = n;
1612 				}
1613 			| TRANSACTION ISOLATION LEVEL
1614 				{
1615 					VariableSetStmt *n = makeNode(VariableSetStmt);
1616 					n->kind = VAR_RESET;
1617 					n->name = "transaction_isolation";
1618 					$$ = n;
1619 				}
1620 			| SESSION AUTHORIZATION
1621 				{
1622 					VariableSetStmt *n = makeNode(VariableSetStmt);
1623 					n->kind = VAR_RESET;
1624 					n->name = "session_authorization";
1625 					$$ = n;
1626 				}
1627 		;
1628 
1629 generic_reset:
1630 			var_name
1631 				{
1632 					VariableSetStmt *n = makeNode(VariableSetStmt);
1633 					n->kind = VAR_RESET;
1634 					n->name = $1;
1635 					$$ = n;
1636 				}
1637 			| ALL
1638 				{
1639 					VariableSetStmt *n = makeNode(VariableSetStmt);
1640 					n->kind = VAR_RESET_ALL;
1641 					$$ = n;
1642 				}
1643 		;
1644 
1645 /* SetResetClause allows SET or RESET without LOCAL */
1646 SetResetClause:
1647 			SET set_rest					{ $$ = $2; }
1648 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1649 		;
1650 
1651 /* SetResetClause allows SET or RESET without LOCAL */
1652 FunctionSetResetClause:
1653 			SET set_rest_more				{ $$ = $2; }
1654 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1655 		;
1656 
1657 
1658 VariableShowStmt:
1659 			SHOW var_name
1660 				{
1661 					VariableShowStmt *n = makeNode(VariableShowStmt);
1662 					n->name = $2;
1663 					$$ = (Node *) n;
1664 				}
1665 			| SHOW TIME ZONE
1666 				{
1667 					VariableShowStmt *n = makeNode(VariableShowStmt);
1668 					n->name = "timezone";
1669 					$$ = (Node *) n;
1670 				}
1671 			| SHOW TRANSACTION ISOLATION LEVEL
1672 				{
1673 					VariableShowStmt *n = makeNode(VariableShowStmt);
1674 					n->name = "transaction_isolation";
1675 					$$ = (Node *) n;
1676 				}
1677 			| SHOW SESSION AUTHORIZATION
1678 				{
1679 					VariableShowStmt *n = makeNode(VariableShowStmt);
1680 					n->name = "session_authorization";
1681 					$$ = (Node *) n;
1682 				}
1683 			| SHOW ALL
1684 				{
1685 					VariableShowStmt *n = makeNode(VariableShowStmt);
1686 					n->name = "all";
1687 					$$ = (Node *) n;
1688 				}
1689 		;
1690 
1691 
1692 ConstraintsSetStmt:
1693 			SET CONSTRAINTS constraints_set_list constraints_set_mode
1694 				{
1695 					ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1696 					n->constraints = $3;
1697 					n->deferred = $4;
1698 					$$ = (Node *) n;
1699 				}
1700 		;
1701 
1702 constraints_set_list:
1703 			ALL										{ $$ = NIL; }
1704 			| qualified_name_list					{ $$ = $1; }
1705 		;
1706 
1707 constraints_set_mode:
1708 			DEFERRED								{ $$ = TRUE; }
1709 			| IMMEDIATE								{ $$ = FALSE; }
1710 		;
1711 
1712 
1713 /*
1714  * Checkpoint statement
1715  */
1716 CheckPointStmt:
1717 			CHECKPOINT
1718 				{
1719 					CheckPointStmt *n = makeNode(CheckPointStmt);
1720 					$$ = (Node *)n;
1721 				}
1722 		;
1723 
1724 
1725 /*****************************************************************************
1726  *
1727  * DISCARD { ALL | TEMP | PLANS | SEQUENCES }
1728  *
1729  *****************************************************************************/
1730 
1731 DiscardStmt:
1732 			DISCARD ALL
1733 				{
1734 					DiscardStmt *n = makeNode(DiscardStmt);
1735 					n->target = DISCARD_ALL;
1736 					$$ = (Node *) n;
1737 				}
1738 			| DISCARD TEMP
1739 				{
1740 					DiscardStmt *n = makeNode(DiscardStmt);
1741 					n->target = DISCARD_TEMP;
1742 					$$ = (Node *) n;
1743 				}
1744 			| DISCARD TEMPORARY
1745 				{
1746 					DiscardStmt *n = makeNode(DiscardStmt);
1747 					n->target = DISCARD_TEMP;
1748 					$$ = (Node *) n;
1749 				}
1750 			| DISCARD PLANS
1751 				{
1752 					DiscardStmt *n = makeNode(DiscardStmt);
1753 					n->target = DISCARD_PLANS;
1754 					$$ = (Node *) n;
1755 				}
1756 			| DISCARD SEQUENCES
1757 				{
1758 					DiscardStmt *n = makeNode(DiscardStmt);
1759 					n->target = DISCARD_SEQUENCES;
1760 					$$ = (Node *) n;
1761 				}
1762 
1763 		;
1764 
1765 
1766 /*****************************************************************************
1767  *
1768  *	ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW ] variations
1769  *
1770  * Note: we accept all subcommands for each of the five variants, and sort
1771  * out what's really legal at execution time.
1772  *****************************************************************************/
1773 
1774 AlterTableStmt:
1775 			ALTER TABLE relation_expr alter_table_cmds
1776 				{
1777 					AlterTableStmt *n = makeNode(AlterTableStmt);
1778 					n->relation = $3;
1779 					n->cmds = $4;
1780 					n->relkind = OBJECT_TABLE;
1781 					n->missing_ok = false;
1782 					$$ = (Node *)n;
1783 				}
1784 		|	ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
1785 				{
1786 					AlterTableStmt *n = makeNode(AlterTableStmt);
1787 					n->relation = $5;
1788 					n->cmds = $6;
1789 					n->relkind = OBJECT_TABLE;
1790 					n->missing_ok = true;
1791 					$$ = (Node *)n;
1792 				}
1793 		|	ALTER TABLE ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1794 				{
1795 					AlterTableMoveAllStmt *n =
1796 						makeNode(AlterTableMoveAllStmt);
1797 					n->orig_tablespacename = $6;
1798 					n->objtype = OBJECT_TABLE;
1799 					n->roles = NIL;
1800 					n->new_tablespacename = $9;
1801 					n->nowait = $10;
1802 					$$ = (Node *)n;
1803 				}
1804 		|	ALTER TABLE ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1805 				{
1806 					AlterTableMoveAllStmt *n =
1807 						makeNode(AlterTableMoveAllStmt);
1808 					n->orig_tablespacename = $6;
1809 					n->objtype = OBJECT_TABLE;
1810 					n->roles = $9;
1811 					n->new_tablespacename = $12;
1812 					n->nowait = $13;
1813 					$$ = (Node *)n;
1814 				}
1815 		|	ALTER INDEX qualified_name alter_table_cmds
1816 				{
1817 					AlterTableStmt *n = makeNode(AlterTableStmt);
1818 					n->relation = $3;
1819 					n->cmds = $4;
1820 					n->relkind = OBJECT_INDEX;
1821 					n->missing_ok = false;
1822 					$$ = (Node *)n;
1823 				}
1824 		|	ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
1825 				{
1826 					AlterTableStmt *n = makeNode(AlterTableStmt);
1827 					n->relation = $5;
1828 					n->cmds = $6;
1829 					n->relkind = OBJECT_INDEX;
1830 					n->missing_ok = true;
1831 					$$ = (Node *)n;
1832 				}
1833 		|	ALTER INDEX ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1834 				{
1835 					AlterTableMoveAllStmt *n =
1836 						makeNode(AlterTableMoveAllStmt);
1837 					n->orig_tablespacename = $6;
1838 					n->objtype = OBJECT_INDEX;
1839 					n->roles = NIL;
1840 					n->new_tablespacename = $9;
1841 					n->nowait = $10;
1842 					$$ = (Node *)n;
1843 				}
1844 		|	ALTER INDEX ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1845 				{
1846 					AlterTableMoveAllStmt *n =
1847 						makeNode(AlterTableMoveAllStmt);
1848 					n->orig_tablespacename = $6;
1849 					n->objtype = OBJECT_INDEX;
1850 					n->roles = $9;
1851 					n->new_tablespacename = $12;
1852 					n->nowait = $13;
1853 					$$ = (Node *)n;
1854 				}
1855 		|	ALTER SEQUENCE qualified_name alter_table_cmds
1856 				{
1857 					AlterTableStmt *n = makeNode(AlterTableStmt);
1858 					n->relation = $3;
1859 					n->cmds = $4;
1860 					n->relkind = OBJECT_SEQUENCE;
1861 					n->missing_ok = false;
1862 					$$ = (Node *)n;
1863 				}
1864 		|	ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
1865 				{
1866 					AlterTableStmt *n = makeNode(AlterTableStmt);
1867 					n->relation = $5;
1868 					n->cmds = $6;
1869 					n->relkind = OBJECT_SEQUENCE;
1870 					n->missing_ok = true;
1871 					$$ = (Node *)n;
1872 				}
1873 		|	ALTER VIEW qualified_name alter_table_cmds
1874 				{
1875 					AlterTableStmt *n = makeNode(AlterTableStmt);
1876 					n->relation = $3;
1877 					n->cmds = $4;
1878 					n->relkind = OBJECT_VIEW;
1879 					n->missing_ok = false;
1880 					$$ = (Node *)n;
1881 				}
1882 		|	ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
1883 				{
1884 					AlterTableStmt *n = makeNode(AlterTableStmt);
1885 					n->relation = $5;
1886 					n->cmds = $6;
1887 					n->relkind = OBJECT_VIEW;
1888 					n->missing_ok = true;
1889 					$$ = (Node *)n;
1890 				}
1891 		|	ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
1892 				{
1893 					AlterTableStmt *n = makeNode(AlterTableStmt);
1894 					n->relation = $4;
1895 					n->cmds = $5;
1896 					n->relkind = OBJECT_MATVIEW;
1897 					n->missing_ok = false;
1898 					$$ = (Node *)n;
1899 				}
1900 		|	ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
1901 				{
1902 					AlterTableStmt *n = makeNode(AlterTableStmt);
1903 					n->relation = $6;
1904 					n->cmds = $7;
1905 					n->relkind = OBJECT_MATVIEW;
1906 					n->missing_ok = true;
1907 					$$ = (Node *)n;
1908 				}
1909 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1910 				{
1911 					AlterTableMoveAllStmt *n =
1912 						makeNode(AlterTableMoveAllStmt);
1913 					n->orig_tablespacename = $7;
1914 					n->objtype = OBJECT_MATVIEW;
1915 					n->roles = NIL;
1916 					n->new_tablespacename = $10;
1917 					n->nowait = $11;
1918 					$$ = (Node *)n;
1919 				}
1920 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1921 				{
1922 					AlterTableMoveAllStmt *n =
1923 						makeNode(AlterTableMoveAllStmt);
1924 					n->orig_tablespacename = $7;
1925 					n->objtype = OBJECT_MATVIEW;
1926 					n->roles = $10;
1927 					n->new_tablespacename = $13;
1928 					n->nowait = $14;
1929 					$$ = (Node *)n;
1930 				}
1931 		;
1932 
1933 alter_table_cmds:
1934 			alter_table_cmd							{ $$ = list_make1($1); }
1935 			| alter_table_cmds ',' alter_table_cmd	{ $$ = lappend($1, $3); }
1936 		;
1937 
1938 alter_table_cmd:
1939 			/* ALTER TABLE <name> ADD <coldef> */
1940 			ADD_P columnDef
1941 				{
1942 					AlterTableCmd *n = makeNode(AlterTableCmd);
1943 					n->subtype = AT_AddColumn;
1944 					n->def = $2;
1945 					n->missing_ok = false;
1946 					$$ = (Node *)n;
1947 				}
1948 			/* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
1949 			| ADD_P IF_P NOT EXISTS columnDef
1950 				{
1951 					AlterTableCmd *n = makeNode(AlterTableCmd);
1952 					n->subtype = AT_AddColumn;
1953 					n->def = $5;
1954 					n->missing_ok = true;
1955 					$$ = (Node *)n;
1956 				}
1957 			/* ALTER TABLE <name> ADD COLUMN <coldef> */
1958 			| ADD_P COLUMN columnDef
1959 				{
1960 					AlterTableCmd *n = makeNode(AlterTableCmd);
1961 					n->subtype = AT_AddColumn;
1962 					n->def = $3;
1963 					n->missing_ok = false;
1964 					$$ = (Node *)n;
1965 				}
1966 			/* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
1967 			| ADD_P COLUMN IF_P NOT EXISTS columnDef
1968 				{
1969 					AlterTableCmd *n = makeNode(AlterTableCmd);
1970 					n->subtype = AT_AddColumn;
1971 					n->def = $6;
1972 					n->missing_ok = true;
1973 					$$ = (Node *)n;
1974 				}
1975 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1976 			| ALTER opt_column ColId alter_column_default
1977 				{
1978 					AlterTableCmd *n = makeNode(AlterTableCmd);
1979 					n->subtype = AT_ColumnDefault;
1980 					n->name = $3;
1981 					n->def = $4;
1982 					$$ = (Node *)n;
1983 				}
1984 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1985 			| ALTER opt_column ColId DROP NOT NULL_P
1986 				{
1987 					AlterTableCmd *n = makeNode(AlterTableCmd);
1988 					n->subtype = AT_DropNotNull;
1989 					n->name = $3;
1990 					$$ = (Node *)n;
1991 				}
1992 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1993 			| ALTER opt_column ColId SET NOT NULL_P
1994 				{
1995 					AlterTableCmd *n = makeNode(AlterTableCmd);
1996 					n->subtype = AT_SetNotNull;
1997 					n->name = $3;
1998 					$$ = (Node *)n;
1999 				}
2000 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
2001 			| ALTER opt_column ColId SET STATISTICS SignedIconst
2002 				{
2003 					AlterTableCmd *n = makeNode(AlterTableCmd);
2004 					n->subtype = AT_SetStatistics;
2005 					n->name = $3;
2006 					n->def = (Node *) makeInteger($6);
2007 					$$ = (Node *)n;
2008 				}
2009 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
2010 			| ALTER opt_column ColId SET reloptions
2011 				{
2012 					AlterTableCmd *n = makeNode(AlterTableCmd);
2013 					n->subtype = AT_SetOptions;
2014 					n->name = $3;
2015 					n->def = (Node *) $5;
2016 					$$ = (Node *)n;
2017 				}
2018 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
2019 			| ALTER opt_column ColId RESET reloptions
2020 				{
2021 					AlterTableCmd *n = makeNode(AlterTableCmd);
2022 					n->subtype = AT_ResetOptions;
2023 					n->name = $3;
2024 					n->def = (Node *) $5;
2025 					$$ = (Node *)n;
2026 				}
2027 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
2028 			| ALTER opt_column ColId SET STORAGE ColId
2029 				{
2030 					AlterTableCmd *n = makeNode(AlterTableCmd);
2031 					n->subtype = AT_SetStorage;
2032 					n->name = $3;
2033 					n->def = (Node *) makeString($6);
2034 					$$ = (Node *)n;
2035 				}
2036 			/* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
2037 			| DROP opt_column IF_P EXISTS ColId opt_drop_behavior
2038 				{
2039 					AlterTableCmd *n = makeNode(AlterTableCmd);
2040 					n->subtype = AT_DropColumn;
2041 					n->name = $5;
2042 					n->behavior = $6;
2043 					n->missing_ok = TRUE;
2044 					$$ = (Node *)n;
2045 				}
2046 			/* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
2047 			| DROP opt_column ColId opt_drop_behavior
2048 				{
2049 					AlterTableCmd *n = makeNode(AlterTableCmd);
2050 					n->subtype = AT_DropColumn;
2051 					n->name = $3;
2052 					n->behavior = $4;
2053 					n->missing_ok = FALSE;
2054 					$$ = (Node *)n;
2055 				}
2056 			/*
2057 			 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
2058 			 *		[ USING <expression> ]
2059 			 */
2060 			| ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
2061 				{
2062 					AlterTableCmd *n = makeNode(AlterTableCmd);
2063 					ColumnDef *def = makeNode(ColumnDef);
2064 					n->subtype = AT_AlterColumnType;
2065 					n->name = $3;
2066 					n->def = (Node *) def;
2067 					/* We only use these fields of the ColumnDef node */
2068 					def->typeName = $6;
2069 					def->collClause = (CollateClause *) $7;
2070 					def->raw_default = $8;
2071 					def->location = @3;
2072 					$$ = (Node *)n;
2073 				}
2074 			/* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
2075 			| ALTER opt_column ColId alter_generic_options
2076 				{
2077 					AlterTableCmd *n = makeNode(AlterTableCmd);
2078 					n->subtype = AT_AlterColumnGenericOptions;
2079 					n->name = $3;
2080 					n->def = (Node *) $4;
2081 					$$ = (Node *)n;
2082 				}
2083 			/* ALTER TABLE <name> ADD CONSTRAINT ... */
2084 			| ADD_P TableConstraint
2085 				{
2086 					AlterTableCmd *n = makeNode(AlterTableCmd);
2087 					n->subtype = AT_AddConstraint;
2088 					n->def = $2;
2089 					$$ = (Node *)n;
2090 				}
2091 			/* ALTER TABLE <name> ALTER CONSTRAINT ... */
2092 			| ALTER CONSTRAINT name ConstraintAttributeSpec
2093 				{
2094 					AlterTableCmd *n = makeNode(AlterTableCmd);
2095 					Constraint *c = makeNode(Constraint);
2096 					n->subtype = AT_AlterConstraint;
2097 					n->def = (Node *) c;
2098 					c->contype = CONSTR_FOREIGN; /* others not supported, yet */
2099 					c->conname = $3;
2100 					processCASbits($4, @4, "ALTER CONSTRAINT statement",
2101 									&c->deferrable,
2102 									&c->initdeferred,
2103 									NULL, NULL, yyscanner);
2104 					$$ = (Node *)n;
2105 				}
2106 			/* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
2107 			| VALIDATE CONSTRAINT name
2108 				{
2109 					AlterTableCmd *n = makeNode(AlterTableCmd);
2110 					n->subtype = AT_ValidateConstraint;
2111 					n->name = $3;
2112 					$$ = (Node *)n;
2113 				}
2114 			/* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
2115 			| DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
2116 				{
2117 					AlterTableCmd *n = makeNode(AlterTableCmd);
2118 					n->subtype = AT_DropConstraint;
2119 					n->name = $5;
2120 					n->behavior = $6;
2121 					n->missing_ok = TRUE;
2122 					$$ = (Node *)n;
2123 				}
2124 			/* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
2125 			| DROP CONSTRAINT name opt_drop_behavior
2126 				{
2127 					AlterTableCmd *n = makeNode(AlterTableCmd);
2128 					n->subtype = AT_DropConstraint;
2129 					n->name = $3;
2130 					n->behavior = $4;
2131 					n->missing_ok = FALSE;
2132 					$$ = (Node *)n;
2133 				}
2134 			/* ALTER TABLE <name> SET WITH OIDS  */
2135 			| SET WITH OIDS
2136 				{
2137 					AlterTableCmd *n = makeNode(AlterTableCmd);
2138 					n->subtype = AT_AddOids;
2139 					$$ = (Node *)n;
2140 				}
2141 			/* ALTER TABLE <name> SET WITHOUT OIDS  */
2142 			| SET WITHOUT OIDS
2143 				{
2144 					AlterTableCmd *n = makeNode(AlterTableCmd);
2145 					n->subtype = AT_DropOids;
2146 					$$ = (Node *)n;
2147 				}
2148 			/* ALTER TABLE <name> CLUSTER ON <indexname> */
2149 			| CLUSTER ON name
2150 				{
2151 					AlterTableCmd *n = makeNode(AlterTableCmd);
2152 					n->subtype = AT_ClusterOn;
2153 					n->name = $3;
2154 					$$ = (Node *)n;
2155 				}
2156 			/* ALTER TABLE <name> SET WITHOUT CLUSTER */
2157 			| SET WITHOUT CLUSTER
2158 				{
2159 					AlterTableCmd *n = makeNode(AlterTableCmd);
2160 					n->subtype = AT_DropCluster;
2161 					n->name = NULL;
2162 					$$ = (Node *)n;
2163 				}
2164 			/* ALTER TABLE <name> SET LOGGED  */
2165 			| SET LOGGED
2166 				{
2167 					AlterTableCmd *n = makeNode(AlterTableCmd);
2168 					n->subtype = AT_SetLogged;
2169 					$$ = (Node *)n;
2170 				}
2171 			/* ALTER TABLE <name> SET UNLOGGED  */
2172 			| SET UNLOGGED
2173 				{
2174 					AlterTableCmd *n = makeNode(AlterTableCmd);
2175 					n->subtype = AT_SetUnLogged;
2176 					$$ = (Node *)n;
2177 				}
2178 			/* ALTER TABLE <name> ENABLE TRIGGER <trig> */
2179 			| ENABLE_P TRIGGER name
2180 				{
2181 					AlterTableCmd *n = makeNode(AlterTableCmd);
2182 					n->subtype = AT_EnableTrig;
2183 					n->name = $3;
2184 					$$ = (Node *)n;
2185 				}
2186 			/* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
2187 			| ENABLE_P ALWAYS TRIGGER name
2188 				{
2189 					AlterTableCmd *n = makeNode(AlterTableCmd);
2190 					n->subtype = AT_EnableAlwaysTrig;
2191 					n->name = $4;
2192 					$$ = (Node *)n;
2193 				}
2194 			/* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
2195 			| ENABLE_P REPLICA TRIGGER name
2196 				{
2197 					AlterTableCmd *n = makeNode(AlterTableCmd);
2198 					n->subtype = AT_EnableReplicaTrig;
2199 					n->name = $4;
2200 					$$ = (Node *)n;
2201 				}
2202 			/* ALTER TABLE <name> ENABLE TRIGGER ALL */
2203 			| ENABLE_P TRIGGER ALL
2204 				{
2205 					AlterTableCmd *n = makeNode(AlterTableCmd);
2206 					n->subtype = AT_EnableTrigAll;
2207 					$$ = (Node *)n;
2208 				}
2209 			/* ALTER TABLE <name> ENABLE TRIGGER USER */
2210 			| ENABLE_P TRIGGER USER
2211 				{
2212 					AlterTableCmd *n = makeNode(AlterTableCmd);
2213 					n->subtype = AT_EnableTrigUser;
2214 					$$ = (Node *)n;
2215 				}
2216 			/* ALTER TABLE <name> DISABLE TRIGGER <trig> */
2217 			| DISABLE_P TRIGGER name
2218 				{
2219 					AlterTableCmd *n = makeNode(AlterTableCmd);
2220 					n->subtype = AT_DisableTrig;
2221 					n->name = $3;
2222 					$$ = (Node *)n;
2223 				}
2224 			/* ALTER TABLE <name> DISABLE TRIGGER ALL */
2225 			| DISABLE_P TRIGGER ALL
2226 				{
2227 					AlterTableCmd *n = makeNode(AlterTableCmd);
2228 					n->subtype = AT_DisableTrigAll;
2229 					$$ = (Node *)n;
2230 				}
2231 			/* ALTER TABLE <name> DISABLE TRIGGER USER */
2232 			| DISABLE_P TRIGGER USER
2233 				{
2234 					AlterTableCmd *n = makeNode(AlterTableCmd);
2235 					n->subtype = AT_DisableTrigUser;
2236 					$$ = (Node *)n;
2237 				}
2238 			/* ALTER TABLE <name> ENABLE RULE <rule> */
2239 			| ENABLE_P RULE name
2240 				{
2241 					AlterTableCmd *n = makeNode(AlterTableCmd);
2242 					n->subtype = AT_EnableRule;
2243 					n->name = $3;
2244 					$$ = (Node *)n;
2245 				}
2246 			/* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
2247 			| ENABLE_P ALWAYS RULE name
2248 				{
2249 					AlterTableCmd *n = makeNode(AlterTableCmd);
2250 					n->subtype = AT_EnableAlwaysRule;
2251 					n->name = $4;
2252 					$$ = (Node *)n;
2253 				}
2254 			/* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
2255 			| ENABLE_P REPLICA RULE name
2256 				{
2257 					AlterTableCmd *n = makeNode(AlterTableCmd);
2258 					n->subtype = AT_EnableReplicaRule;
2259 					n->name = $4;
2260 					$$ = (Node *)n;
2261 				}
2262 			/* ALTER TABLE <name> DISABLE RULE <rule> */
2263 			| DISABLE_P RULE name
2264 				{
2265 					AlterTableCmd *n = makeNode(AlterTableCmd);
2266 					n->subtype = AT_DisableRule;
2267 					n->name = $3;
2268 					$$ = (Node *)n;
2269 				}
2270 			/* ALTER TABLE <name> INHERIT <parent> */
2271 			| INHERIT qualified_name
2272 				{
2273 					AlterTableCmd *n = makeNode(AlterTableCmd);
2274 					n->subtype = AT_AddInherit;
2275 					n->def = (Node *) $2;
2276 					$$ = (Node *)n;
2277 				}
2278 			/* ALTER TABLE <name> NO INHERIT <parent> */
2279 			| NO INHERIT qualified_name
2280 				{
2281 					AlterTableCmd *n = makeNode(AlterTableCmd);
2282 					n->subtype = AT_DropInherit;
2283 					n->def = (Node *) $3;
2284 					$$ = (Node *)n;
2285 				}
2286 			/* ALTER TABLE <name> OF <type_name> */
2287 			| OF any_name
2288 				{
2289 					AlterTableCmd *n = makeNode(AlterTableCmd);
2290 					TypeName *def = makeTypeNameFromNameList($2);
2291 					def->location = @2;
2292 					n->subtype = AT_AddOf;
2293 					n->def = (Node *) def;
2294 					$$ = (Node *)n;
2295 				}
2296 			/* ALTER TABLE <name> NOT OF */
2297 			| NOT OF
2298 				{
2299 					AlterTableCmd *n = makeNode(AlterTableCmd);
2300 					n->subtype = AT_DropOf;
2301 					$$ = (Node *)n;
2302 				}
2303 			/* ALTER TABLE <name> OWNER TO RoleSpec */
2304 			| OWNER TO RoleSpec
2305 				{
2306 					AlterTableCmd *n = makeNode(AlterTableCmd);
2307 					n->subtype = AT_ChangeOwner;
2308 					n->newowner = $3;
2309 					$$ = (Node *)n;
2310 				}
2311 			/* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
2312 			| SET TABLESPACE name
2313 				{
2314 					AlterTableCmd *n = makeNode(AlterTableCmd);
2315 					n->subtype = AT_SetTableSpace;
2316 					n->name = $3;
2317 					$$ = (Node *)n;
2318 				}
2319 			/* ALTER TABLE <name> SET (...) */
2320 			| SET reloptions
2321 				{
2322 					AlterTableCmd *n = makeNode(AlterTableCmd);
2323 					n->subtype = AT_SetRelOptions;
2324 					n->def = (Node *)$2;
2325 					$$ = (Node *)n;
2326 				}
2327 			/* ALTER TABLE <name> RESET (...) */
2328 			| RESET reloptions
2329 				{
2330 					AlterTableCmd *n = makeNode(AlterTableCmd);
2331 					n->subtype = AT_ResetRelOptions;
2332 					n->def = (Node *)$2;
2333 					$$ = (Node *)n;
2334 				}
2335 			/* ALTER TABLE <name> REPLICA IDENTITY  */
2336 			| REPLICA IDENTITY_P replica_identity
2337 				{
2338 					AlterTableCmd *n = makeNode(AlterTableCmd);
2339 					n->subtype = AT_ReplicaIdentity;
2340 					n->def = $3;
2341 					$$ = (Node *)n;
2342 				}
2343 			/* ALTER TABLE <name> ENABLE ROW LEVEL SECURITY */
2344 			| ENABLE_P ROW LEVEL SECURITY
2345 				{
2346 					AlterTableCmd *n = makeNode(AlterTableCmd);
2347 					n->subtype = AT_EnableRowSecurity;
2348 					$$ = (Node *)n;
2349 				}
2350 			/* ALTER TABLE <name> DISABLE ROW LEVEL SECURITY */
2351 			| DISABLE_P ROW LEVEL SECURITY
2352 				{
2353 					AlterTableCmd *n = makeNode(AlterTableCmd);
2354 					n->subtype = AT_DisableRowSecurity;
2355 					$$ = (Node *)n;
2356 				}
2357 			/* ALTER TABLE <name> FORCE ROW LEVEL SECURITY */
2358 			| FORCE ROW LEVEL SECURITY
2359 				{
2360 					AlterTableCmd *n = makeNode(AlterTableCmd);
2361 					n->subtype = AT_ForceRowSecurity;
2362 					$$ = (Node *)n;
2363 				}
2364 			/* ALTER TABLE <name> NO FORCE ROW LEVEL SECURITY */
2365 			| NO FORCE ROW LEVEL SECURITY
2366 				{
2367 					AlterTableCmd *n = makeNode(AlterTableCmd);
2368 					n->subtype = AT_NoForceRowSecurity;
2369 					$$ = (Node *)n;
2370 				}
2371 			| alter_generic_options
2372 				{
2373 					AlterTableCmd *n = makeNode(AlterTableCmd);
2374 					n->subtype = AT_GenericOptions;
2375 					n->def = (Node *)$1;
2376 					$$ = (Node *) n;
2377 				}
2378 		;
2379 
2380 alter_column_default:
2381 			SET DEFAULT a_expr			{ $$ = $3; }
2382 			| DROP DEFAULT				{ $$ = NULL; }
2383 		;
2384 
2385 opt_drop_behavior:
2386 			CASCADE						{ $$ = DROP_CASCADE; }
2387 			| RESTRICT					{ $$ = DROP_RESTRICT; }
2388 			| /* EMPTY */				{ $$ = DROP_RESTRICT; /* default */ }
2389 		;
2390 
2391 opt_collate_clause:
2392 			COLLATE any_name
2393 				{
2394 					CollateClause *n = makeNode(CollateClause);
2395 					n->arg = NULL;
2396 					n->collname = $2;
2397 					n->location = @1;
2398 					$$ = (Node *) n;
2399 				}
2400 			| /* EMPTY */				{ $$ = NULL; }
2401 		;
2402 
2403 alter_using:
2404 			USING a_expr				{ $$ = $2; }
2405 			| /* EMPTY */				{ $$ = NULL; }
2406 		;
2407 
2408 replica_identity:
2409 			NOTHING
2410 				{
2411 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2412 					n->identity_type = REPLICA_IDENTITY_NOTHING;
2413 					n->name = NULL;
2414 					$$ = (Node *) n;
2415 				}
2416 			| FULL
2417 				{
2418 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2419 					n->identity_type = REPLICA_IDENTITY_FULL;
2420 					n->name = NULL;
2421 					$$ = (Node *) n;
2422 				}
2423 			| DEFAULT
2424 				{
2425 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2426 					n->identity_type = REPLICA_IDENTITY_DEFAULT;
2427 					n->name = NULL;
2428 					$$ = (Node *) n;
2429 				}
2430 			| USING INDEX name
2431 				{
2432 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2433 					n->identity_type = REPLICA_IDENTITY_INDEX;
2434 					n->name = $3;
2435 					$$ = (Node *) n;
2436 				}
2437 ;
2438 
2439 reloptions:
2440 			'(' reloption_list ')'					{ $$ = $2; }
2441 		;
2442 
2443 opt_reloptions:		WITH reloptions					{ $$ = $2; }
2444 			 |		/* EMPTY */						{ $$ = NIL; }
2445 		;
2446 
2447 reloption_list:
2448 			reloption_elem							{ $$ = list_make1($1); }
2449 			| reloption_list ',' reloption_elem		{ $$ = lappend($1, $3); }
2450 		;
2451 
2452 /* This should match def_elem and also allow qualified names */
2453 reloption_elem:
2454 			ColLabel '=' def_arg
2455 				{
2456 					$$ = makeDefElem($1, (Node *) $3);
2457 				}
2458 			| ColLabel
2459 				{
2460 					$$ = makeDefElem($1, NULL);
2461 				}
2462 			| ColLabel '.' ColLabel '=' def_arg
2463 				{
2464 					$$ = makeDefElemExtended($1, $3, (Node *) $5,
2465 											 DEFELEM_UNSPEC);
2466 				}
2467 			| ColLabel '.' ColLabel
2468 				{
2469 					$$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC);
2470 				}
2471 		;
2472 
2473 
2474 /*****************************************************************************
2475  *
2476  *	ALTER TYPE
2477  *
2478  * really variants of the ALTER TABLE subcommands with different spellings
2479  *****************************************************************************/
2480 
2481 AlterCompositeTypeStmt:
2482 			ALTER TYPE_P any_name alter_type_cmds
2483 				{
2484 					AlterTableStmt *n = makeNode(AlterTableStmt);
2485 
2486 					/* can't use qualified_name, sigh */
2487 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
2488 					n->cmds = $4;
2489 					n->relkind = OBJECT_TYPE;
2490 					$$ = (Node *)n;
2491 				}
2492 			;
2493 
2494 alter_type_cmds:
2495 			alter_type_cmd							{ $$ = list_make1($1); }
2496 			| alter_type_cmds ',' alter_type_cmd	{ $$ = lappend($1, $3); }
2497 		;
2498 
2499 alter_type_cmd:
2500 			/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
2501 			ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
2502 				{
2503 					AlterTableCmd *n = makeNode(AlterTableCmd);
2504 					n->subtype = AT_AddColumn;
2505 					n->def = $3;
2506 					n->behavior = $4;
2507 					$$ = (Node *)n;
2508 				}
2509 			/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
2510 			| DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
2511 				{
2512 					AlterTableCmd *n = makeNode(AlterTableCmd);
2513 					n->subtype = AT_DropColumn;
2514 					n->name = $5;
2515 					n->behavior = $6;
2516 					n->missing_ok = TRUE;
2517 					$$ = (Node *)n;
2518 				}
2519 			/* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
2520 			| DROP ATTRIBUTE ColId opt_drop_behavior
2521 				{
2522 					AlterTableCmd *n = makeNode(AlterTableCmd);
2523 					n->subtype = AT_DropColumn;
2524 					n->name = $3;
2525 					n->behavior = $4;
2526 					n->missing_ok = FALSE;
2527 					$$ = (Node *)n;
2528 				}
2529 			/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
2530 			| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
2531 				{
2532 					AlterTableCmd *n = makeNode(AlterTableCmd);
2533 					ColumnDef *def = makeNode(ColumnDef);
2534 					n->subtype = AT_AlterColumnType;
2535 					n->name = $3;
2536 					n->def = (Node *) def;
2537 					n->behavior = $8;
2538 					/* We only use these fields of the ColumnDef node */
2539 					def->typeName = $6;
2540 					def->collClause = (CollateClause *) $7;
2541 					def->raw_default = NULL;
2542 					def->location = @3;
2543 					$$ = (Node *)n;
2544 				}
2545 		;
2546 
2547 
2548 /*****************************************************************************
2549  *
2550  *		QUERY :
2551  *				close <portalname>
2552  *
2553  *****************************************************************************/
2554 
2555 ClosePortalStmt:
2556 			CLOSE cursor_name
2557 				{
2558 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2559 					n->portalname = $2;
2560 					$$ = (Node *)n;
2561 				}
2562 			| CLOSE ALL
2563 				{
2564 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2565 					n->portalname = NULL;
2566 					$$ = (Node *)n;
2567 				}
2568 		;
2569 
2570 
2571 /*****************************************************************************
2572  *
2573  *		QUERY :
2574  *				COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
2575  *				COPY ( query ) TO file	[WITH] [(options)]
2576  *
2577  *				where 'query' can be one of:
2578  *				{ SELECT | UPDATE | INSERT | DELETE }
2579  *
2580  *				and 'file' can be one of:
2581  *				{ PROGRAM 'command' | STDIN | STDOUT | 'filename' }
2582  *
2583  *				In the preferred syntax the options are comma-separated
2584  *				and use generic identifiers instead of keywords.  The pre-9.0
2585  *				syntax had a hard-wired, space-separated set of options.
2586  *
2587  *				Really old syntax, from versions 7.2 and prior:
2588  *				COPY [ BINARY ] table [ WITH OIDS ] FROM/TO file
2589  *					[ [ USING ] DELIMITERS 'delimiter' ] ]
2590  *					[ WITH NULL AS 'null string' ]
2591  *				This option placement is not supported with COPY (query...).
2592  *
2593  *****************************************************************************/
2594 
2595 CopyStmt:	COPY opt_binary qualified_name opt_column_list opt_oids
2596 			copy_from opt_program copy_file_name copy_delimiter opt_with copy_options
2597 				{
2598 					CopyStmt *n = makeNode(CopyStmt);
2599 					n->relation = $3;
2600 					n->query = NULL;
2601 					n->attlist = $4;
2602 					n->is_from = $6;
2603 					n->is_program = $7;
2604 					n->filename = $8;
2605 
2606 					if (n->is_program && n->filename == NULL)
2607 						ereport(ERROR,
2608 								(errcode(ERRCODE_SYNTAX_ERROR),
2609 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2610 								 parser_errposition(@8)));
2611 
2612 					n->options = NIL;
2613 					/* Concatenate user-supplied flags */
2614 					if ($2)
2615 						n->options = lappend(n->options, $2);
2616 					if ($5)
2617 						n->options = lappend(n->options, $5);
2618 					if ($9)
2619 						n->options = lappend(n->options, $9);
2620 					if ($11)
2621 						n->options = list_concat(n->options, $11);
2622 					$$ = (Node *)n;
2623 				}
2624 			| COPY '(' PreparableStmt ')' TO opt_program copy_file_name opt_with copy_options
2625 				{
2626 					CopyStmt *n = makeNode(CopyStmt);
2627 					n->relation = NULL;
2628 					n->query = $3;
2629 					n->attlist = NIL;
2630 					n->is_from = false;
2631 					n->is_program = $6;
2632 					n->filename = $7;
2633 					n->options = $9;
2634 
2635 					if (n->is_program && n->filename == NULL)
2636 						ereport(ERROR,
2637 								(errcode(ERRCODE_SYNTAX_ERROR),
2638 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2639 								 parser_errposition(@5)));
2640 
2641 					$$ = (Node *)n;
2642 				}
2643 		;
2644 
2645 copy_from:
2646 			FROM									{ $$ = TRUE; }
2647 			| TO									{ $$ = FALSE; }
2648 		;
2649 
2650 opt_program:
2651 			PROGRAM									{ $$ = TRUE; }
2652 			| /* EMPTY */							{ $$ = FALSE; }
2653 		;
2654 
2655 /*
2656  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
2657  * used depends on the direction. (It really doesn't make sense to copy from
2658  * stdout. We silently correct the "typo".)		 - AY 9/94
2659  */
2660 copy_file_name:
2661 			Sconst									{ $$ = $1; }
2662 			| STDIN									{ $$ = NULL; }
2663 			| STDOUT								{ $$ = NULL; }
2664 		;
2665 
2666 copy_options: copy_opt_list							{ $$ = $1; }
2667 			| '(' copy_generic_opt_list ')'			{ $$ = $2; }
2668 		;
2669 
2670 /* old COPY option syntax */
2671 copy_opt_list:
2672 			copy_opt_list copy_opt_item				{ $$ = lappend($1, $2); }
2673 			| /* EMPTY */							{ $$ = NIL; }
2674 		;
2675 
2676 copy_opt_item:
2677 			BINARY
2678 				{
2679 					$$ = makeDefElem("format", (Node *)makeString("binary"));
2680 				}
2681 			| OIDS
2682 				{
2683 					$$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2684 				}
2685 			| FREEZE
2686 				{
2687 					$$ = makeDefElem("freeze", (Node *)makeInteger(TRUE));
2688 				}
2689 			| DELIMITER opt_as Sconst
2690 				{
2691 					$$ = makeDefElem("delimiter", (Node *)makeString($3));
2692 				}
2693 			| NULL_P opt_as Sconst
2694 				{
2695 					$$ = makeDefElem("null", (Node *)makeString($3));
2696 				}
2697 			| CSV
2698 				{
2699 					$$ = makeDefElem("format", (Node *)makeString("csv"));
2700 				}
2701 			| HEADER_P
2702 				{
2703 					$$ = makeDefElem("header", (Node *)makeInteger(TRUE));
2704 				}
2705 			| QUOTE opt_as Sconst
2706 				{
2707 					$$ = makeDefElem("quote", (Node *)makeString($3));
2708 				}
2709 			| ESCAPE opt_as Sconst
2710 				{
2711 					$$ = makeDefElem("escape", (Node *)makeString($3));
2712 				}
2713 			| FORCE QUOTE columnList
2714 				{
2715 					$$ = makeDefElem("force_quote", (Node *)$3);
2716 				}
2717 			| FORCE QUOTE '*'
2718 				{
2719 					$$ = makeDefElem("force_quote", (Node *)makeNode(A_Star));
2720 				}
2721 			| FORCE NOT NULL_P columnList
2722 				{
2723 					$$ = makeDefElem("force_not_null", (Node *)$4);
2724 				}
2725 			| FORCE NULL_P columnList
2726 				{
2727 					$$ = makeDefElem("force_null", (Node *)$3);
2728 				}
2729 			| ENCODING Sconst
2730 				{
2731 					$$ = makeDefElem("encoding", (Node *)makeString($2));
2732 				}
2733 		;
2734 
2735 /* The following exist for backward compatibility with very old versions */
2736 
2737 opt_binary:
2738 			BINARY
2739 				{
2740 					$$ = makeDefElem("format", (Node *)makeString("binary"));
2741 				}
2742 			| /*EMPTY*/								{ $$ = NULL; }
2743 		;
2744 
2745 opt_oids:
2746 			WITH OIDS
2747 				{
2748 					$$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2749 				}
2750 			| /*EMPTY*/								{ $$ = NULL; }
2751 		;
2752 
2753 copy_delimiter:
2754 			opt_using DELIMITERS Sconst
2755 				{
2756 					$$ = makeDefElem("delimiter", (Node *)makeString($3));
2757 				}
2758 			| /*EMPTY*/								{ $$ = NULL; }
2759 		;
2760 
2761 opt_using:
2762 			USING									{}
2763 			| /*EMPTY*/								{}
2764 		;
2765 
2766 /* new COPY option syntax */
2767 copy_generic_opt_list:
2768 			copy_generic_opt_elem
2769 				{
2770 					$$ = list_make1($1);
2771 				}
2772 			| copy_generic_opt_list ',' copy_generic_opt_elem
2773 				{
2774 					$$ = lappend($1, $3);
2775 				}
2776 		;
2777 
2778 copy_generic_opt_elem:
2779 			ColLabel copy_generic_opt_arg
2780 				{
2781 					$$ = makeDefElem($1, $2);
2782 				}
2783 		;
2784 
2785 copy_generic_opt_arg:
2786 			opt_boolean_or_string			{ $$ = (Node *) makeString($1); }
2787 			| NumericOnly					{ $$ = (Node *) $1; }
2788 			| '*'							{ $$ = (Node *) makeNode(A_Star); }
2789 			| '(' copy_generic_opt_arg_list ')'		{ $$ = (Node *) $2; }
2790 			| /* EMPTY */					{ $$ = NULL; }
2791 		;
2792 
2793 copy_generic_opt_arg_list:
2794 			  copy_generic_opt_arg_list_item
2795 				{
2796 					$$ = list_make1($1);
2797 				}
2798 			| copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
2799 				{
2800 					$$ = lappend($1, $3);
2801 				}
2802 		;
2803 
2804 /* beware of emitting non-string list elements here; see commands/define.c */
2805 copy_generic_opt_arg_list_item:
2806 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
2807 		;
2808 
2809 
2810 /*****************************************************************************
2811  *
2812  *		QUERY :
2813  *				CREATE TABLE relname
2814  *
2815  *****************************************************************************/
2816 
2817 CreateStmt:	CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
2818 			OptInherit OptWith OnCommitOption OptTableSpace
2819 				{
2820 					CreateStmt *n = makeNode(CreateStmt);
2821 					$4->relpersistence = $2;
2822 					n->relation = $4;
2823 					n->tableElts = $6;
2824 					n->inhRelations = $8;
2825 					n->ofTypename = NULL;
2826 					n->constraints = NIL;
2827 					n->options = $9;
2828 					n->oncommit = $10;
2829 					n->tablespacename = $11;
2830 					n->if_not_exists = false;
2831 					$$ = (Node *)n;
2832 				}
2833 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
2834 			OptTableElementList ')' OptInherit OptWith OnCommitOption
2835 			OptTableSpace
2836 				{
2837 					CreateStmt *n = makeNode(CreateStmt);
2838 					$7->relpersistence = $2;
2839 					n->relation = $7;
2840 					n->tableElts = $9;
2841 					n->inhRelations = $11;
2842 					n->ofTypename = NULL;
2843 					n->constraints = NIL;
2844 					n->options = $12;
2845 					n->oncommit = $13;
2846 					n->tablespacename = $14;
2847 					n->if_not_exists = true;
2848 					$$ = (Node *)n;
2849 				}
2850 		| CREATE OptTemp TABLE qualified_name OF any_name
2851 			OptTypedTableElementList OptWith OnCommitOption OptTableSpace
2852 				{
2853 					CreateStmt *n = makeNode(CreateStmt);
2854 					$4->relpersistence = $2;
2855 					n->relation = $4;
2856 					n->tableElts = $7;
2857 					n->inhRelations = NIL;
2858 					n->ofTypename = makeTypeNameFromNameList($6);
2859 					n->ofTypename->location = @6;
2860 					n->constraints = NIL;
2861 					n->options = $8;
2862 					n->oncommit = $9;
2863 					n->tablespacename = $10;
2864 					n->if_not_exists = false;
2865 					$$ = (Node *)n;
2866 				}
2867 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
2868 			OptTypedTableElementList OptWith OnCommitOption OptTableSpace
2869 				{
2870 					CreateStmt *n = makeNode(CreateStmt);
2871 					$7->relpersistence = $2;
2872 					n->relation = $7;
2873 					n->tableElts = $10;
2874 					n->inhRelations = NIL;
2875 					n->ofTypename = makeTypeNameFromNameList($9);
2876 					n->ofTypename->location = @9;
2877 					n->constraints = NIL;
2878 					n->options = $11;
2879 					n->oncommit = $12;
2880 					n->tablespacename = $13;
2881 					n->if_not_exists = true;
2882 					$$ = (Node *)n;
2883 				}
2884 		;
2885 
2886 /*
2887  * Redundancy here is needed to avoid shift/reduce conflicts,
2888  * since TEMP is not a reserved word.  See also OptTempTableName.
2889  *
2890  * NOTE: we accept both GLOBAL and LOCAL options.  They currently do nothing,
2891  * but future versions might consider GLOBAL to request SQL-spec-compliant
2892  * temp table behavior, so warn about that.  Since we have no modules the
2893  * LOCAL keyword is really meaningless; furthermore, some other products
2894  * implement LOCAL as meaning the same as our default temp table behavior,
2895  * so we'll probably continue to treat LOCAL as a noise word.
2896  */
2897 OptTemp:	TEMPORARY					{ $$ = RELPERSISTENCE_TEMP; }
2898 			| TEMP						{ $$ = RELPERSISTENCE_TEMP; }
2899 			| LOCAL TEMPORARY			{ $$ = RELPERSISTENCE_TEMP; }
2900 			| LOCAL TEMP				{ $$ = RELPERSISTENCE_TEMP; }
2901 			| GLOBAL TEMPORARY
2902 				{
2903 					ereport(WARNING,
2904 							(errmsg("GLOBAL is deprecated in temporary table creation"),
2905 							 parser_errposition(@1)));
2906 					$$ = RELPERSISTENCE_TEMP;
2907 				}
2908 			| GLOBAL TEMP
2909 				{
2910 					ereport(WARNING,
2911 							(errmsg("GLOBAL is deprecated in temporary table creation"),
2912 							 parser_errposition(@1)));
2913 					$$ = RELPERSISTENCE_TEMP;
2914 				}
2915 			| UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
2916 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
2917 		;
2918 
2919 OptTableElementList:
2920 			TableElementList					{ $$ = $1; }
2921 			| /*EMPTY*/							{ $$ = NIL; }
2922 		;
2923 
2924 OptTypedTableElementList:
2925 			'(' TypedTableElementList ')'		{ $$ = $2; }
2926 			| /*EMPTY*/							{ $$ = NIL; }
2927 		;
2928 
2929 TableElementList:
2930 			TableElement
2931 				{
2932 					$$ = list_make1($1);
2933 				}
2934 			| TableElementList ',' TableElement
2935 				{
2936 					$$ = lappend($1, $3);
2937 				}
2938 		;
2939 
2940 TypedTableElementList:
2941 			TypedTableElement
2942 				{
2943 					$$ = list_make1($1);
2944 				}
2945 			| TypedTableElementList ',' TypedTableElement
2946 				{
2947 					$$ = lappend($1, $3);
2948 				}
2949 		;
2950 
2951 TableElement:
2952 			columnDef							{ $$ = $1; }
2953 			| TableLikeClause					{ $$ = $1; }
2954 			| TableConstraint					{ $$ = $1; }
2955 		;
2956 
2957 TypedTableElement:
2958 			columnOptions						{ $$ = $1; }
2959 			| TableConstraint					{ $$ = $1; }
2960 		;
2961 
2962 columnDef:	ColId Typename create_generic_options ColQualList
2963 				{
2964 					ColumnDef *n = makeNode(ColumnDef);
2965 					n->colname = $1;
2966 					n->typeName = $2;
2967 					n->inhcount = 0;
2968 					n->is_local = true;
2969 					n->is_not_null = false;
2970 					n->is_from_type = false;
2971 					n->storage = 0;
2972 					n->raw_default = NULL;
2973 					n->cooked_default = NULL;
2974 					n->collOid = InvalidOid;
2975 					n->fdwoptions = $3;
2976 					SplitColQualList($4, &n->constraints, &n->collClause,
2977 									 yyscanner);
2978 					n->location = @1;
2979 					$$ = (Node *)n;
2980 				}
2981 		;
2982 
2983 columnOptions:	ColId WITH OPTIONS ColQualList
2984 				{
2985 					ColumnDef *n = makeNode(ColumnDef);
2986 					n->colname = $1;
2987 					n->typeName = NULL;
2988 					n->inhcount = 0;
2989 					n->is_local = true;
2990 					n->is_not_null = false;
2991 					n->is_from_type = false;
2992 					n->storage = 0;
2993 					n->raw_default = NULL;
2994 					n->cooked_default = NULL;
2995 					n->collOid = InvalidOid;
2996 					SplitColQualList($4, &n->constraints, &n->collClause,
2997 									 yyscanner);
2998 					n->location = @1;
2999 					$$ = (Node *)n;
3000 				}
3001 		;
3002 
3003 ColQualList:
3004 			ColQualList ColConstraint				{ $$ = lappend($1, $2); }
3005 			| /*EMPTY*/								{ $$ = NIL; }
3006 		;
3007 
3008 ColConstraint:
3009 			CONSTRAINT name ColConstraintElem
3010 				{
3011 					Constraint *n = (Constraint *) $3;
3012 					Assert(IsA(n, Constraint));
3013 					n->conname = $2;
3014 					n->location = @1;
3015 					$$ = (Node *) n;
3016 				}
3017 			| ColConstraintElem						{ $$ = $1; }
3018 			| ConstraintAttr						{ $$ = $1; }
3019 			| COLLATE any_name
3020 				{
3021 					/*
3022 					 * Note: the CollateClause is momentarily included in
3023 					 * the list built by ColQualList, but we split it out
3024 					 * again in SplitColQualList.
3025 					 */
3026 					CollateClause *n = makeNode(CollateClause);
3027 					n->arg = NULL;
3028 					n->collname = $2;
3029 					n->location = @1;
3030 					$$ = (Node *) n;
3031 				}
3032 		;
3033 
3034 /* DEFAULT NULL is already the default for Postgres.
3035  * But define it here and carry it forward into the system
3036  * to make it explicit.
3037  * - thomas 1998-09-13
3038  *
3039  * WITH NULL and NULL are not SQL-standard syntax elements,
3040  * so leave them out. Use DEFAULT NULL to explicitly indicate
3041  * that a column may have that value. WITH NULL leads to
3042  * shift/reduce conflicts with WITH TIME ZONE anyway.
3043  * - thomas 1999-01-08
3044  *
3045  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
3046  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
3047  * or be part of a_expr NOT LIKE or similar constructs).
3048  */
3049 ColConstraintElem:
3050 			NOT NULL_P
3051 				{
3052 					Constraint *n = makeNode(Constraint);
3053 					n->contype = CONSTR_NOTNULL;
3054 					n->location = @1;
3055 					$$ = (Node *)n;
3056 				}
3057 			| NULL_P
3058 				{
3059 					Constraint *n = makeNode(Constraint);
3060 					n->contype = CONSTR_NULL;
3061 					n->location = @1;
3062 					$$ = (Node *)n;
3063 				}
3064 			| UNIQUE opt_definition OptConsTableSpace
3065 				{
3066 					Constraint *n = makeNode(Constraint);
3067 					n->contype = CONSTR_UNIQUE;
3068 					n->location = @1;
3069 					n->keys = NULL;
3070 					n->options = $2;
3071 					n->indexname = NULL;
3072 					n->indexspace = $3;
3073 					$$ = (Node *)n;
3074 				}
3075 			| PRIMARY KEY opt_definition OptConsTableSpace
3076 				{
3077 					Constraint *n = makeNode(Constraint);
3078 					n->contype = CONSTR_PRIMARY;
3079 					n->location = @1;
3080 					n->keys = NULL;
3081 					n->options = $3;
3082 					n->indexname = NULL;
3083 					n->indexspace = $4;
3084 					$$ = (Node *)n;
3085 				}
3086 			| CHECK '(' a_expr ')' opt_no_inherit
3087 				{
3088 					Constraint *n = makeNode(Constraint);
3089 					n->contype = CONSTR_CHECK;
3090 					n->location = @1;
3091 					n->is_no_inherit = $5;
3092 					n->raw_expr = $3;
3093 					n->cooked_expr = NULL;
3094 					n->skip_validation = false;
3095 					n->initially_valid = true;
3096 					$$ = (Node *)n;
3097 				}
3098 			| DEFAULT b_expr
3099 				{
3100 					Constraint *n = makeNode(Constraint);
3101 					n->contype = CONSTR_DEFAULT;
3102 					n->location = @1;
3103 					n->raw_expr = $2;
3104 					n->cooked_expr = NULL;
3105 					$$ = (Node *)n;
3106 				}
3107 			| REFERENCES qualified_name opt_column_list key_match key_actions
3108 				{
3109 					Constraint *n = makeNode(Constraint);
3110 					n->contype = CONSTR_FOREIGN;
3111 					n->location = @1;
3112 					n->pktable			= $2;
3113 					n->fk_attrs			= NIL;
3114 					n->pk_attrs			= $3;
3115 					n->fk_matchtype		= $4;
3116 					n->fk_upd_action	= (char) ($5 >> 8);
3117 					n->fk_del_action	= (char) ($5 & 0xFF);
3118 					n->skip_validation  = false;
3119 					n->initially_valid  = true;
3120 					$$ = (Node *)n;
3121 				}
3122 		;
3123 
3124 /*
3125  * ConstraintAttr represents constraint attributes, which we parse as if
3126  * they were independent constraint clauses, in order to avoid shift/reduce
3127  * conflicts (since NOT might start either an independent NOT NULL clause
3128  * or an attribute).  parse_utilcmd.c is responsible for attaching the
3129  * attribute information to the preceding "real" constraint node, and for
3130  * complaining if attribute clauses appear in the wrong place or wrong
3131  * combinations.
3132  *
3133  * See also ConstraintAttributeSpec, which can be used in places where
3134  * there is no parsing conflict.  (Note: currently, NOT VALID and NO INHERIT
3135  * are allowed clauses in ConstraintAttributeSpec, but not here.  Someday we
3136  * might need to allow them here too, but for the moment it doesn't seem
3137  * useful in the statements that use ConstraintAttr.)
3138  */
3139 ConstraintAttr:
3140 			DEFERRABLE
3141 				{
3142 					Constraint *n = makeNode(Constraint);
3143 					n->contype = CONSTR_ATTR_DEFERRABLE;
3144 					n->location = @1;
3145 					$$ = (Node *)n;
3146 				}
3147 			| NOT DEFERRABLE
3148 				{
3149 					Constraint *n = makeNode(Constraint);
3150 					n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
3151 					n->location = @1;
3152 					$$ = (Node *)n;
3153 				}
3154 			| INITIALLY DEFERRED
3155 				{
3156 					Constraint *n = makeNode(Constraint);
3157 					n->contype = CONSTR_ATTR_DEFERRED;
3158 					n->location = @1;
3159 					$$ = (Node *)n;
3160 				}
3161 			| INITIALLY IMMEDIATE
3162 				{
3163 					Constraint *n = makeNode(Constraint);
3164 					n->contype = CONSTR_ATTR_IMMEDIATE;
3165 					n->location = @1;
3166 					$$ = (Node *)n;
3167 				}
3168 		;
3169 
3170 
3171 TableLikeClause:
3172 			LIKE qualified_name TableLikeOptionList
3173 				{
3174 					TableLikeClause *n = makeNode(TableLikeClause);
3175 					n->relation = $2;
3176 					n->options = $3;
3177 					n->relationOid = InvalidOid;
3178 					$$ = (Node *)n;
3179 				}
3180 		;
3181 
3182 TableLikeOptionList:
3183 				TableLikeOptionList INCLUDING TableLikeOption	{ $$ = $1 | $3; }
3184 				| TableLikeOptionList EXCLUDING TableLikeOption	{ $$ = $1 & ~$3; }
3185 				| /* EMPTY */						{ $$ = 0; }
3186 		;
3187 
3188 TableLikeOption:
3189 				DEFAULTS			{ $$ = CREATE_TABLE_LIKE_DEFAULTS; }
3190 				| CONSTRAINTS		{ $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
3191 				| INDEXES			{ $$ = CREATE_TABLE_LIKE_INDEXES; }
3192 				| STORAGE			{ $$ = CREATE_TABLE_LIKE_STORAGE; }
3193 				| COMMENTS			{ $$ = CREATE_TABLE_LIKE_COMMENTS; }
3194 				| ALL				{ $$ = CREATE_TABLE_LIKE_ALL; }
3195 		;
3196 
3197 
3198 /* ConstraintElem specifies constraint syntax which is not embedded into
3199  *	a column definition. ColConstraintElem specifies the embedded form.
3200  * - thomas 1997-12-03
3201  */
3202 TableConstraint:
3203 			CONSTRAINT name ConstraintElem
3204 				{
3205 					Constraint *n = (Constraint *) $3;
3206 					Assert(IsA(n, Constraint));
3207 					n->conname = $2;
3208 					n->location = @1;
3209 					$$ = (Node *) n;
3210 				}
3211 			| ConstraintElem						{ $$ = $1; }
3212 		;
3213 
3214 ConstraintElem:
3215 			CHECK '(' a_expr ')' ConstraintAttributeSpec
3216 				{
3217 					Constraint *n = makeNode(Constraint);
3218 					n->contype = CONSTR_CHECK;
3219 					n->location = @1;
3220 					n->raw_expr = $3;
3221 					n->cooked_expr = NULL;
3222 					processCASbits($5, @5, "CHECK",
3223 								   NULL, NULL, &n->skip_validation,
3224 								   &n->is_no_inherit, yyscanner);
3225 					n->initially_valid = !n->skip_validation;
3226 					$$ = (Node *)n;
3227 				}
3228 			| UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
3229 				ConstraintAttributeSpec
3230 				{
3231 					Constraint *n = makeNode(Constraint);
3232 					n->contype = CONSTR_UNIQUE;
3233 					n->location = @1;
3234 					n->keys = $3;
3235 					n->options = $5;
3236 					n->indexname = NULL;
3237 					n->indexspace = $6;
3238 					processCASbits($7, @7, "UNIQUE",
3239 								   &n->deferrable, &n->initdeferred, NULL,
3240 								   NULL, yyscanner);
3241 					$$ = (Node *)n;
3242 				}
3243 			| UNIQUE ExistingIndex ConstraintAttributeSpec
3244 				{
3245 					Constraint *n = makeNode(Constraint);
3246 					n->contype = CONSTR_UNIQUE;
3247 					n->location = @1;
3248 					n->keys = NIL;
3249 					n->options = NIL;
3250 					n->indexname = $2;
3251 					n->indexspace = NULL;
3252 					processCASbits($3, @3, "UNIQUE",
3253 								   &n->deferrable, &n->initdeferred, NULL,
3254 								   NULL, yyscanner);
3255 					$$ = (Node *)n;
3256 				}
3257 			| PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
3258 				ConstraintAttributeSpec
3259 				{
3260 					Constraint *n = makeNode(Constraint);
3261 					n->contype = CONSTR_PRIMARY;
3262 					n->location = @1;
3263 					n->keys = $4;
3264 					n->options = $6;
3265 					n->indexname = NULL;
3266 					n->indexspace = $7;
3267 					processCASbits($8, @8, "PRIMARY KEY",
3268 								   &n->deferrable, &n->initdeferred, NULL,
3269 								   NULL, yyscanner);
3270 					$$ = (Node *)n;
3271 				}
3272 			| PRIMARY KEY ExistingIndex ConstraintAttributeSpec
3273 				{
3274 					Constraint *n = makeNode(Constraint);
3275 					n->contype = CONSTR_PRIMARY;
3276 					n->location = @1;
3277 					n->keys = NIL;
3278 					n->options = NIL;
3279 					n->indexname = $3;
3280 					n->indexspace = NULL;
3281 					processCASbits($4, @4, "PRIMARY KEY",
3282 								   &n->deferrable, &n->initdeferred, NULL,
3283 								   NULL, yyscanner);
3284 					$$ = (Node *)n;
3285 				}
3286 			| EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
3287 				opt_definition OptConsTableSpace ExclusionWhereClause
3288 				ConstraintAttributeSpec
3289 				{
3290 					Constraint *n = makeNode(Constraint);
3291 					n->contype = CONSTR_EXCLUSION;
3292 					n->location = @1;
3293 					n->access_method	= $2;
3294 					n->exclusions		= $4;
3295 					n->options			= $6;
3296 					n->indexname		= NULL;
3297 					n->indexspace		= $7;
3298 					n->where_clause		= $8;
3299 					processCASbits($9, @9, "EXCLUDE",
3300 								   &n->deferrable, &n->initdeferred, NULL,
3301 								   NULL, yyscanner);
3302 					$$ = (Node *)n;
3303 				}
3304 			| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
3305 				opt_column_list key_match key_actions ConstraintAttributeSpec
3306 				{
3307 					Constraint *n = makeNode(Constraint);
3308 					n->contype = CONSTR_FOREIGN;
3309 					n->location = @1;
3310 					n->pktable			= $7;
3311 					n->fk_attrs			= $4;
3312 					n->pk_attrs			= $8;
3313 					n->fk_matchtype		= $9;
3314 					n->fk_upd_action	= (char) ($10 >> 8);
3315 					n->fk_del_action	= (char) ($10 & 0xFF);
3316 					processCASbits($11, @11, "FOREIGN KEY",
3317 								   &n->deferrable, &n->initdeferred,
3318 								   &n->skip_validation, NULL,
3319 								   yyscanner);
3320 					n->initially_valid = !n->skip_validation;
3321 					$$ = (Node *)n;
3322 				}
3323 		;
3324 
3325 opt_no_inherit:	NO INHERIT							{  $$ = TRUE; }
3326 			| /* EMPTY */							{  $$ = FALSE; }
3327 		;
3328 
3329 opt_column_list:
3330 			'(' columnList ')'						{ $$ = $2; }
3331 			| /*EMPTY*/								{ $$ = NIL; }
3332 		;
3333 
3334 columnList:
3335 			columnElem								{ $$ = list_make1($1); }
3336 			| columnList ',' columnElem				{ $$ = lappend($1, $3); }
3337 		;
3338 
3339 columnElem: ColId
3340 				{
3341 					$$ = (Node *) makeString($1);
3342 				}
3343 		;
3344 
3345 key_match:  MATCH FULL
3346 			{
3347 				$$ = FKCONSTR_MATCH_FULL;
3348 			}
3349 		| MATCH PARTIAL
3350 			{
3351 				ereport(ERROR,
3352 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3353 						 errmsg("MATCH PARTIAL not yet implemented"),
3354 						 parser_errposition(@1)));
3355 				$$ = FKCONSTR_MATCH_PARTIAL;
3356 			}
3357 		| MATCH SIMPLE
3358 			{
3359 				$$ = FKCONSTR_MATCH_SIMPLE;
3360 			}
3361 		| /*EMPTY*/
3362 			{
3363 				$$ = FKCONSTR_MATCH_SIMPLE;
3364 			}
3365 		;
3366 
3367 ExclusionConstraintList:
3368 			ExclusionConstraintElem					{ $$ = list_make1($1); }
3369 			| ExclusionConstraintList ',' ExclusionConstraintElem
3370 													{ $$ = lappend($1, $3); }
3371 		;
3372 
3373 ExclusionConstraintElem: index_elem WITH any_operator
3374 			{
3375 				$$ = list_make2($1, $3);
3376 			}
3377 			/* allow OPERATOR() decoration for the benefit of ruleutils.c */
3378 			| index_elem WITH OPERATOR '(' any_operator ')'
3379 			{
3380 				$$ = list_make2($1, $5);
3381 			}
3382 		;
3383 
3384 ExclusionWhereClause:
3385 			WHERE '(' a_expr ')'					{ $$ = $3; }
3386 			| /*EMPTY*/								{ $$ = NULL; }
3387 		;
3388 
3389 /*
3390  * We combine the update and delete actions into one value temporarily
3391  * for simplicity of parsing, and then break them down again in the
3392  * calling production.  update is in the left 8 bits, delete in the right.
3393  * Note that NOACTION is the default.
3394  */
3395 key_actions:
3396 			key_update
3397 				{ $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3398 			| key_delete
3399 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
3400 			| key_update key_delete
3401 				{ $$ = ($1 << 8) | ($2 & 0xFF); }
3402 			| key_delete key_update
3403 				{ $$ = ($2 << 8) | ($1 & 0xFF); }
3404 			| /*EMPTY*/
3405 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3406 		;
3407 
3408 key_update: ON UPDATE key_action		{ $$ = $3; }
3409 		;
3410 
3411 key_delete: ON DELETE_P key_action		{ $$ = $3; }
3412 		;
3413 
3414 key_action:
3415 			NO ACTION					{ $$ = FKCONSTR_ACTION_NOACTION; }
3416 			| RESTRICT					{ $$ = FKCONSTR_ACTION_RESTRICT; }
3417 			| CASCADE					{ $$ = FKCONSTR_ACTION_CASCADE; }
3418 			| SET NULL_P				{ $$ = FKCONSTR_ACTION_SETNULL; }
3419 			| SET DEFAULT				{ $$ = FKCONSTR_ACTION_SETDEFAULT; }
3420 		;
3421 
3422 OptInherit: INHERITS '(' qualified_name_list ')'	{ $$ = $3; }
3423 			| /*EMPTY*/								{ $$ = NIL; }
3424 		;
3425 
3426 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
3427 OptWith:
3428 			WITH reloptions				{ $$ = $2; }
3429 			| WITH OIDS					{ $$ = list_make1(defWithOids(true)); }
3430 			| WITHOUT OIDS				{ $$ = list_make1(defWithOids(false)); }
3431 			| /*EMPTY*/					{ $$ = NIL; }
3432 		;
3433 
3434 OnCommitOption:  ON COMMIT DROP				{ $$ = ONCOMMIT_DROP; }
3435 			| ON COMMIT DELETE_P ROWS		{ $$ = ONCOMMIT_DELETE_ROWS; }
3436 			| ON COMMIT PRESERVE ROWS		{ $$ = ONCOMMIT_PRESERVE_ROWS; }
3437 			| /*EMPTY*/						{ $$ = ONCOMMIT_NOOP; }
3438 		;
3439 
3440 OptTableSpace:   TABLESPACE name					{ $$ = $2; }
3441 			| /*EMPTY*/								{ $$ = NULL; }
3442 		;
3443 
3444 OptConsTableSpace:   USING INDEX TABLESPACE name	{ $$ = $4; }
3445 			| /*EMPTY*/								{ $$ = NULL; }
3446 		;
3447 
3448 ExistingIndex:   USING INDEX index_name				{ $$ = $3; }
3449 		;
3450 
3451 
3452 /*****************************************************************************
3453  *
3454  *		QUERY :
3455  *				CREATE TABLE relname AS SelectStmt [ WITH [NO] DATA ]
3456  *
3457  *
3458  * Note: SELECT ... INTO is a now-deprecated alternative for this.
3459  *
3460  *****************************************************************************/
3461 
3462 CreateAsStmt:
3463 		CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
3464 				{
3465 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3466 					ctas->query = $6;
3467 					ctas->into = $4;
3468 					ctas->relkind = OBJECT_TABLE;
3469 					ctas->is_select_into = false;
3470 					ctas->if_not_exists = false;
3471 					/* cram additional flags into the IntoClause */
3472 					$4->rel->relpersistence = $2;
3473 					$4->skipData = !($7);
3474 					$$ = (Node *) ctas;
3475 				}
3476 		| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS SelectStmt opt_with_data
3477 				{
3478 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3479 					ctas->query = $9;
3480 					ctas->into = $7;
3481 					ctas->relkind = OBJECT_TABLE;
3482 					ctas->is_select_into = false;
3483 					ctas->if_not_exists = true;
3484 					/* cram additional flags into the IntoClause */
3485 					$7->rel->relpersistence = $2;
3486 					$7->skipData = !($10);
3487 					$$ = (Node *) ctas;
3488 				}
3489 		;
3490 
3491 create_as_target:
3492 			qualified_name opt_column_list OptWith OnCommitOption OptTableSpace
3493 				{
3494 					$$ = makeNode(IntoClause);
3495 					$$->rel = $1;
3496 					$$->colNames = $2;
3497 					$$->options = $3;
3498 					$$->onCommit = $4;
3499 					$$->tableSpaceName = $5;
3500 					$$->viewQuery = NULL;
3501 					$$->skipData = false;		/* might get changed later */
3502 				}
3503 		;
3504 
3505 opt_with_data:
3506 			WITH DATA_P								{ $$ = TRUE; }
3507 			| WITH NO DATA_P						{ $$ = FALSE; }
3508 			| /*EMPTY*/								{ $$ = TRUE; }
3509 		;
3510 
3511 
3512 /*****************************************************************************
3513  *
3514  *		QUERY :
3515  *				CREATE MATERIALIZED VIEW relname AS SelectStmt
3516  *
3517  *****************************************************************************/
3518 
3519 CreateMatViewStmt:
3520 		CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data
3521 				{
3522 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3523 					ctas->query = $7;
3524 					ctas->into = $5;
3525 					ctas->relkind = OBJECT_MATVIEW;
3526 					ctas->is_select_into = false;
3527 					ctas->if_not_exists = false;
3528 					/* cram additional flags into the IntoClause */
3529 					$5->rel->relpersistence = $2;
3530 					$5->skipData = !($8);
3531 					$$ = (Node *) ctas;
3532 				}
3533 		| CREATE OptNoLog MATERIALIZED VIEW IF_P NOT EXISTS create_mv_target AS SelectStmt opt_with_data
3534 				{
3535 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3536 					ctas->query = $10;
3537 					ctas->into = $8;
3538 					ctas->relkind = OBJECT_MATVIEW;
3539 					ctas->is_select_into = false;
3540 					ctas->if_not_exists = true;
3541 					/* cram additional flags into the IntoClause */
3542 					$8->rel->relpersistence = $2;
3543 					$8->skipData = !($11);
3544 					$$ = (Node *) ctas;
3545 				}
3546 		;
3547 
3548 create_mv_target:
3549 			qualified_name opt_column_list opt_reloptions OptTableSpace
3550 				{
3551 					$$ = makeNode(IntoClause);
3552 					$$->rel = $1;
3553 					$$->colNames = $2;
3554 					$$->options = $3;
3555 					$$->onCommit = ONCOMMIT_NOOP;
3556 					$$->tableSpaceName = $4;
3557 					$$->viewQuery = NULL;		/* filled at analysis time */
3558 					$$->skipData = false;		/* might get changed later */
3559 				}
3560 		;
3561 
3562 OptNoLog:	UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
3563 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
3564 		;
3565 
3566 
3567 /*****************************************************************************
3568  *
3569  *		QUERY :
3570  *				REFRESH MATERIALIZED VIEW qualified_name
3571  *
3572  *****************************************************************************/
3573 
3574 RefreshMatViewStmt:
3575 			REFRESH MATERIALIZED VIEW opt_concurrently qualified_name opt_with_data
3576 				{
3577 					RefreshMatViewStmt *n = makeNode(RefreshMatViewStmt);
3578 					n->concurrent = $4;
3579 					n->relation = $5;
3580 					n->skipData = !($6);
3581 					$$ = (Node *) n;
3582 				}
3583 		;
3584 
3585 
3586 /*****************************************************************************
3587  *
3588  *		QUERY :
3589  *				CREATE SEQUENCE seqname
3590  *				ALTER SEQUENCE seqname
3591  *
3592  *****************************************************************************/
3593 
3594 CreateSeqStmt:
3595 			CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
3596 				{
3597 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
3598 					$4->relpersistence = $2;
3599 					n->sequence = $4;
3600 					n->options = $5;
3601 					n->ownerId = InvalidOid;
3602 					n->if_not_exists = false;
3603 					$$ = (Node *)n;
3604 				}
3605 			| CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
3606 				{
3607 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
3608 					$7->relpersistence = $2;
3609 					n->sequence = $7;
3610 					n->options = $8;
3611 					n->ownerId = InvalidOid;
3612 					n->if_not_exists = true;
3613 					$$ = (Node *)n;
3614 				}
3615 		;
3616 
3617 AlterSeqStmt:
3618 			ALTER SEQUENCE qualified_name SeqOptList
3619 				{
3620 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
3621 					n->sequence = $3;
3622 					n->options = $4;
3623 					n->missing_ok = false;
3624 					$$ = (Node *)n;
3625 				}
3626 			| ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
3627 				{
3628 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
3629 					n->sequence = $5;
3630 					n->options = $6;
3631 					n->missing_ok = true;
3632 					$$ = (Node *)n;
3633 				}
3634 
3635 		;
3636 
3637 OptSeqOptList: SeqOptList							{ $$ = $1; }
3638 			| /*EMPTY*/								{ $$ = NIL; }
3639 		;
3640 
3641 SeqOptList: SeqOptElem								{ $$ = list_make1($1); }
3642 			| SeqOptList SeqOptElem					{ $$ = lappend($1, $2); }
3643 		;
3644 
3645 SeqOptElem: CACHE NumericOnly
3646 				{
3647 					$$ = makeDefElem("cache", (Node *)$2);
3648 				}
3649 			| CYCLE
3650 				{
3651 					$$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
3652 				}
3653 			| NO CYCLE
3654 				{
3655 					$$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
3656 				}
3657 			| INCREMENT opt_by NumericOnly
3658 				{
3659 					$$ = makeDefElem("increment", (Node *)$3);
3660 				}
3661 			| MAXVALUE NumericOnly
3662 				{
3663 					$$ = makeDefElem("maxvalue", (Node *)$2);
3664 				}
3665 			| MINVALUE NumericOnly
3666 				{
3667 					$$ = makeDefElem("minvalue", (Node *)$2);
3668 				}
3669 			| NO MAXVALUE
3670 				{
3671 					$$ = makeDefElem("maxvalue", NULL);
3672 				}
3673 			| NO MINVALUE
3674 				{
3675 					$$ = makeDefElem("minvalue", NULL);
3676 				}
3677 			| OWNED BY any_name
3678 				{
3679 					$$ = makeDefElem("owned_by", (Node *)$3);
3680 				}
3681 			| START opt_with NumericOnly
3682 				{
3683 					$$ = makeDefElem("start", (Node *)$3);
3684 				}
3685 			| RESTART
3686 				{
3687 					$$ = makeDefElem("restart", NULL);
3688 				}
3689 			| RESTART opt_with NumericOnly
3690 				{
3691 					$$ = makeDefElem("restart", (Node *)$3);
3692 				}
3693 		;
3694 
3695 opt_by:		BY				{}
3696 			| /* empty */	{}
3697 	  ;
3698 
3699 NumericOnly:
3700 			FCONST								{ $$ = makeFloat($1); }
3701 			| '+' FCONST						{ $$ = makeFloat($2); }
3702 			| '-' FCONST
3703 				{
3704 					$$ = makeFloat($2);
3705 					doNegateFloat($$);
3706 				}
3707 			| SignedIconst						{ $$ = makeInteger($1); }
3708 		;
3709 
3710 NumericOnly_list:	NumericOnly						{ $$ = list_make1($1); }
3711 				| NumericOnly_list ',' NumericOnly	{ $$ = lappend($1, $3); }
3712 		;
3713 
3714 /*****************************************************************************
3715  *
3716  *		QUERIES :
3717  *				CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
3718  *				DROP [PROCEDURAL] LANGUAGE ...
3719  *
3720  *****************************************************************************/
3721 
3722 CreatePLangStmt:
3723 			CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
3724 			{
3725 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
3726 				n->replace = $2;
3727 				n->plname = $6;
3728 				/* parameters are all to be supplied by system */
3729 				n->plhandler = NIL;
3730 				n->plinline = NIL;
3731 				n->plvalidator = NIL;
3732 				n->pltrusted = false;
3733 				$$ = (Node *)n;
3734 			}
3735 			| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
3736 			  HANDLER handler_name opt_inline_handler opt_validator
3737 			{
3738 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
3739 				n->replace = $2;
3740 				n->plname = $6;
3741 				n->plhandler = $8;
3742 				n->plinline = $9;
3743 				n->plvalidator = $10;
3744 				n->pltrusted = $3;
3745 				$$ = (Node *)n;
3746 			}
3747 		;
3748 
3749 opt_trusted:
3750 			TRUSTED									{ $$ = TRUE; }
3751 			| /*EMPTY*/								{ $$ = FALSE; }
3752 		;
3753 
3754 /* This ought to be just func_name, but that causes reduce/reduce conflicts
3755  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
3756  * Work around by using simple names, instead.
3757  */
3758 handler_name:
3759 			name						{ $$ = list_make1(makeString($1)); }
3760 			| name attrs				{ $$ = lcons(makeString($1), $2); }
3761 		;
3762 
3763 opt_inline_handler:
3764 			INLINE_P handler_name					{ $$ = $2; }
3765 			| /*EMPTY*/								{ $$ = NIL; }
3766 		;
3767 
3768 validator_clause:
3769 			VALIDATOR handler_name					{ $$ = $2; }
3770 			| NO VALIDATOR							{ $$ = NIL; }
3771 		;
3772 
3773 opt_validator:
3774 			validator_clause						{ $$ = $1; }
3775 			| /*EMPTY*/								{ $$ = NIL; }
3776 		;
3777 
3778 DropPLangStmt:
3779 			DROP opt_procedural LANGUAGE NonReservedWord_or_Sconst opt_drop_behavior
3780 				{
3781 					DropStmt *n = makeNode(DropStmt);
3782 					n->removeType = OBJECT_LANGUAGE;
3783 					n->objects = list_make1(list_make1(makeString($4)));
3784 					n->arguments = NIL;
3785 					n->behavior = $5;
3786 					n->missing_ok = false;
3787 					n->concurrent = false;
3788 					$$ = (Node *)n;
3789 				}
3790 			| DROP opt_procedural LANGUAGE IF_P EXISTS NonReservedWord_or_Sconst opt_drop_behavior
3791 				{
3792 					DropStmt *n = makeNode(DropStmt);
3793 					n->removeType = OBJECT_LANGUAGE;
3794 					n->objects = list_make1(list_make1(makeString($6)));
3795 					n->behavior = $7;
3796 					n->missing_ok = true;
3797 					n->concurrent = false;
3798 					$$ = (Node *)n;
3799 				}
3800 		;
3801 
3802 opt_procedural:
3803 			PROCEDURAL								{}
3804 			| /*EMPTY*/								{}
3805 		;
3806 
3807 /*****************************************************************************
3808  *
3809  *		QUERY:
3810  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
3811  *
3812  *****************************************************************************/
3813 
3814 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst opt_reloptions
3815 				{
3816 					CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
3817 					n->tablespacename = $3;
3818 					n->owner = $4;
3819 					n->location = $6;
3820 					n->options = $7;
3821 					$$ = (Node *) n;
3822 				}
3823 		;
3824 
3825 OptTableSpaceOwner: OWNER RoleSpec		{ $$ = $2; }
3826 			| /*EMPTY */				{ $$ = NULL; }
3827 		;
3828 
3829 /*****************************************************************************
3830  *
3831  *		QUERY :
3832  *				DROP TABLESPACE <tablespace>
3833  *
3834  *		No need for drop behaviour as we cannot implement dependencies for
3835  *		objects in other databases; we can only support RESTRICT.
3836  *
3837  ****************************************************************************/
3838 
3839 DropTableSpaceStmt: DROP TABLESPACE name
3840 				{
3841 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
3842 					n->tablespacename = $3;
3843 					n->missing_ok = false;
3844 					$$ = (Node *) n;
3845 				}
3846 				|  DROP TABLESPACE IF_P EXISTS name
3847 				{
3848 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
3849 					n->tablespacename = $5;
3850 					n->missing_ok = true;
3851 					$$ = (Node *) n;
3852 				}
3853 		;
3854 
3855 /*****************************************************************************
3856  *
3857  *		QUERY:
3858  *             CREATE EXTENSION extension
3859  *             [ WITH ] [ SCHEMA schema ] [ VERSION version ] [ FROM oldversion ]
3860  *
3861  *****************************************************************************/
3862 
3863 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
3864 				{
3865 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
3866 					n->extname = $3;
3867 					n->if_not_exists = false;
3868 					n->options = $5;
3869 					$$ = (Node *) n;
3870 				}
3871 				| CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
3872 				{
3873 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
3874 					n->extname = $6;
3875 					n->if_not_exists = true;
3876 					n->options = $8;
3877 					$$ = (Node *) n;
3878 				}
3879 		;
3880 
3881 create_extension_opt_list:
3882 			create_extension_opt_list create_extension_opt_item
3883 				{ $$ = lappend($1, $2); }
3884 			| /* EMPTY */
3885 				{ $$ = NIL; }
3886 		;
3887 
3888 create_extension_opt_item:
3889 			SCHEMA name
3890 				{
3891 					$$ = makeDefElem("schema", (Node *)makeString($2));
3892 				}
3893 			| VERSION_P NonReservedWord_or_Sconst
3894 				{
3895 					$$ = makeDefElem("new_version", (Node *)makeString($2));
3896 				}
3897 			| FROM NonReservedWord_or_Sconst
3898 				{
3899 					$$ = makeDefElem("old_version", (Node *)makeString($2));
3900 				}
3901 			| CASCADE
3902 				{
3903 					$$ = makeDefElem("cascade", (Node *)makeInteger(TRUE));
3904 				}
3905 		;
3906 
3907 /*****************************************************************************
3908  *
3909  * ALTER EXTENSION name UPDATE [ TO version ]
3910  *
3911  *****************************************************************************/
3912 
3913 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
3914 				{
3915 					AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
3916 					n->extname = $3;
3917 					n->options = $5;
3918 					$$ = (Node *) n;
3919 				}
3920 		;
3921 
3922 alter_extension_opt_list:
3923 			alter_extension_opt_list alter_extension_opt_item
3924 				{ $$ = lappend($1, $2); }
3925 			| /* EMPTY */
3926 				{ $$ = NIL; }
3927 		;
3928 
3929 alter_extension_opt_item:
3930 			TO NonReservedWord_or_Sconst
3931 				{
3932 					$$ = makeDefElem("new_version", (Node *)makeString($2));
3933 				}
3934 		;
3935 
3936 /*****************************************************************************
3937  *
3938  * ALTER EXTENSION name ADD/DROP object-identifier
3939  *
3940  *****************************************************************************/
3941 
3942 AlterExtensionContentsStmt:
3943 			ALTER EXTENSION name add_drop ACCESS METHOD name
3944 				{
3945 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3946 					n->extname = $3;
3947 					n->action = $4;
3948 					n->objtype = OBJECT_ACCESS_METHOD;
3949 					n->objname = list_make1(makeString($7));
3950 					$$ = (Node *)n;
3951 				}
3952 			| ALTER EXTENSION name add_drop AGGREGATE func_name aggr_args
3953 				{
3954 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3955 					n->extname = $3;
3956 					n->action = $4;
3957 					n->objtype = OBJECT_AGGREGATE;
3958 					n->objname = $6;
3959 					n->objargs = extractAggrArgTypes($7);
3960 					$$ = (Node *)n;
3961 				}
3962 			| ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
3963 				{
3964 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3965 					n->extname = $3;
3966 					n->action = $4;
3967 					n->objtype = OBJECT_CAST;
3968 					n->objname = list_make1($7);
3969 					n->objargs = list_make1($9);
3970 					$$ = (Node *) n;
3971 				}
3972 			| ALTER EXTENSION name add_drop COLLATION any_name
3973 				{
3974 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3975 					n->extname = $3;
3976 					n->action = $4;
3977 					n->objtype = OBJECT_COLLATION;
3978 					n->objname = $6;
3979 					$$ = (Node *)n;
3980 				}
3981 			| ALTER EXTENSION name add_drop CONVERSION_P any_name
3982 				{
3983 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3984 					n->extname = $3;
3985 					n->action = $4;
3986 					n->objtype = OBJECT_CONVERSION;
3987 					n->objname = $6;
3988 					$$ = (Node *)n;
3989 				}
3990 			| ALTER EXTENSION name add_drop DOMAIN_P Typename
3991 				{
3992 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3993 					n->extname = $3;
3994 					n->action = $4;
3995 					n->objtype = OBJECT_DOMAIN;
3996 					n->objname = list_make1($6);
3997 					$$ = (Node *)n;
3998 				}
3999 			| ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
4000 				{
4001 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4002 					n->extname = $3;
4003 					n->action = $4;
4004 					n->objtype = OBJECT_FUNCTION;
4005 					n->objname = $6->funcname;
4006 					n->objargs = $6->funcargs;
4007 					$$ = (Node *)n;
4008 				}
4009 			| ALTER EXTENSION name add_drop opt_procedural LANGUAGE name
4010 				{
4011 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4012 					n->extname = $3;
4013 					n->action = $4;
4014 					n->objtype = OBJECT_LANGUAGE;
4015 					n->objname = list_make1(makeString($7));
4016 					$$ = (Node *)n;
4017 				}
4018 			| ALTER EXTENSION name add_drop OPERATOR any_operator oper_argtypes
4019 				{
4020 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4021 					n->extname = $3;
4022 					n->action = $4;
4023 					n->objtype = OBJECT_OPERATOR;
4024 					n->objname = $6;
4025 					n->objargs = $7;
4026 					$$ = (Node *)n;
4027 				}
4028 			| ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING access_method
4029 				{
4030 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4031 					n->extname = $3;
4032 					n->action = $4;
4033 					n->objtype = OBJECT_OPCLASS;
4034 					n->objname = lcons(makeString($9), $7);
4035 					$$ = (Node *)n;
4036 				}
4037 			| ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING access_method
4038 				{
4039 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4040 					n->extname = $3;
4041 					n->action = $4;
4042 					n->objtype = OBJECT_OPFAMILY;
4043 					n->objname = lcons(makeString($9), $7);
4044 					$$ = (Node *)n;
4045 				}
4046 			| ALTER EXTENSION name add_drop SCHEMA name
4047 				{
4048 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4049 					n->extname = $3;
4050 					n->action = $4;
4051 					n->objtype = OBJECT_SCHEMA;
4052 					n->objname = list_make1(makeString($6));
4053 					$$ = (Node *)n;
4054 				}
4055 			| ALTER EXTENSION name add_drop EVENT TRIGGER name
4056 				{
4057 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4058 					n->extname = $3;
4059 					n->action = $4;
4060 					n->objtype = OBJECT_EVENT_TRIGGER;
4061 					n->objname = list_make1(makeString($7));
4062 					$$ = (Node *)n;
4063 				}
4064 			| ALTER EXTENSION name add_drop TABLE any_name
4065 				{
4066 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4067 					n->extname = $3;
4068 					n->action = $4;
4069 					n->objtype = OBJECT_TABLE;
4070 					n->objname = $6;
4071 					$$ = (Node *)n;
4072 				}
4073 			| ALTER EXTENSION name add_drop TEXT_P SEARCH PARSER any_name
4074 				{
4075 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4076 					n->extname = $3;
4077 					n->action = $4;
4078 					n->objtype = OBJECT_TSPARSER;
4079 					n->objname = $8;
4080 					$$ = (Node *)n;
4081 				}
4082 			| ALTER EXTENSION name add_drop TEXT_P SEARCH DICTIONARY any_name
4083 				{
4084 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4085 					n->extname = $3;
4086 					n->action = $4;
4087 					n->objtype = OBJECT_TSDICTIONARY;
4088 					n->objname = $8;
4089 					$$ = (Node *)n;
4090 				}
4091 			| ALTER EXTENSION name add_drop TEXT_P SEARCH TEMPLATE any_name
4092 				{
4093 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4094 					n->extname = $3;
4095 					n->action = $4;
4096 					n->objtype = OBJECT_TSTEMPLATE;
4097 					n->objname = $8;
4098 					$$ = (Node *)n;
4099 				}
4100 			| ALTER EXTENSION name add_drop TEXT_P SEARCH CONFIGURATION any_name
4101 				{
4102 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4103 					n->extname = $3;
4104 					n->action = $4;
4105 					n->objtype = OBJECT_TSCONFIGURATION;
4106 					n->objname = $8;
4107 					$$ = (Node *)n;
4108 				}
4109 			| ALTER EXTENSION name add_drop SEQUENCE any_name
4110 				{
4111 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4112 					n->extname = $3;
4113 					n->action = $4;
4114 					n->objtype = OBJECT_SEQUENCE;
4115 					n->objname = $6;
4116 					$$ = (Node *)n;
4117 				}
4118 			| ALTER EXTENSION name add_drop VIEW any_name
4119 				{
4120 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4121 					n->extname = $3;
4122 					n->action = $4;
4123 					n->objtype = OBJECT_VIEW;
4124 					n->objname = $6;
4125 					$$ = (Node *)n;
4126 				}
4127 			| ALTER EXTENSION name add_drop MATERIALIZED VIEW any_name
4128 				{
4129 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4130 					n->extname = $3;
4131 					n->action = $4;
4132 					n->objtype = OBJECT_MATVIEW;
4133 					n->objname = $7;
4134 					$$ = (Node *)n;
4135 				}
4136 			| ALTER EXTENSION name add_drop FOREIGN TABLE any_name
4137 				{
4138 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4139 					n->extname = $3;
4140 					n->action = $4;
4141 					n->objtype = OBJECT_FOREIGN_TABLE;
4142 					n->objname = $7;
4143 					$$ = (Node *)n;
4144 				}
4145 			| ALTER EXTENSION name add_drop FOREIGN DATA_P WRAPPER name
4146 				{
4147 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4148 					n->extname = $3;
4149 					n->action = $4;
4150 					n->objtype = OBJECT_FDW;
4151 					n->objname = list_make1(makeString($8));
4152 					$$ = (Node *)n;
4153 				}
4154 			| ALTER EXTENSION name add_drop SERVER name
4155 				{
4156 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4157 					n->extname = $3;
4158 					n->action = $4;
4159 					n->objtype = OBJECT_FOREIGN_SERVER;
4160 					n->objname = list_make1(makeString($6));
4161 					$$ = (Node *)n;
4162 				}
4163 			| ALTER EXTENSION name add_drop TRANSFORM FOR Typename LANGUAGE name
4164 				{
4165 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4166 					n->extname = $3;
4167 					n->action = $4;
4168 					n->objtype = OBJECT_TRANSFORM;
4169 					n->objname = list_make1($7);
4170 					n->objargs = list_make1(makeString($9));
4171 					$$ = (Node *)n;
4172 				}
4173 			| ALTER EXTENSION name add_drop TYPE_P Typename
4174 				{
4175 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4176 					n->extname = $3;
4177 					n->action = $4;
4178 					n->objtype = OBJECT_TYPE;
4179 					n->objname = list_make1($6);
4180 					$$ = (Node *)n;
4181 				}
4182 		;
4183 
4184 /*****************************************************************************
4185  *
4186  *		QUERY:
4187  *             CREATE FOREIGN DATA WRAPPER name options
4188  *
4189  *****************************************************************************/
4190 
4191 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
4192 				{
4193 					CreateFdwStmt *n = makeNode(CreateFdwStmt);
4194 					n->fdwname = $5;
4195 					n->func_options = $6;
4196 					n->options = $7;
4197 					$$ = (Node *) n;
4198 				}
4199 		;
4200 
4201 fdw_option:
4202 			HANDLER handler_name				{ $$ = makeDefElem("handler", (Node *)$2); }
4203 			| NO HANDLER						{ $$ = makeDefElem("handler", NULL); }
4204 			| VALIDATOR handler_name			{ $$ = makeDefElem("validator", (Node *)$2); }
4205 			| NO VALIDATOR						{ $$ = makeDefElem("validator", NULL); }
4206 		;
4207 
4208 fdw_options:
4209 			fdw_option							{ $$ = list_make1($1); }
4210 			| fdw_options fdw_option			{ $$ = lappend($1, $2); }
4211 		;
4212 
4213 opt_fdw_options:
4214 			fdw_options							{ $$ = $1; }
4215 			| /*EMPTY*/							{ $$ = NIL; }
4216 		;
4217 
4218 /*****************************************************************************
4219  *
4220  *		QUERY :
4221  *				DROP FOREIGN DATA WRAPPER name
4222  *
4223  ****************************************************************************/
4224 
4225 DropFdwStmt: DROP FOREIGN DATA_P WRAPPER name opt_drop_behavior
4226 				{
4227 					DropStmt *n = makeNode(DropStmt);
4228 					n->removeType = OBJECT_FDW;
4229 					n->objects = list_make1(list_make1(makeString($5)));
4230 					n->arguments = NIL;
4231 					n->missing_ok = false;
4232 					n->behavior = $6;
4233 					n->concurrent = false;
4234 					$$ = (Node *) n;
4235 				}
4236 				|  DROP FOREIGN DATA_P WRAPPER IF_P EXISTS name opt_drop_behavior
4237 				{
4238 					DropStmt *n = makeNode(DropStmt);
4239 					n->removeType = OBJECT_FDW;
4240 					n->objects = list_make1(list_make1(makeString($7)));
4241 					n->arguments = NIL;
4242 					n->missing_ok = true;
4243 					n->behavior = $8;
4244 					n->concurrent = false;
4245 					$$ = (Node *) n;
4246 				}
4247 		;
4248 
4249 /*****************************************************************************
4250  *
4251  *		QUERY :
4252  *				ALTER FOREIGN DATA WRAPPER name options
4253  *
4254  ****************************************************************************/
4255 
4256 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
4257 				{
4258 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4259 					n->fdwname = $5;
4260 					n->func_options = $6;
4261 					n->options = $7;
4262 					$$ = (Node *) n;
4263 				}
4264 			| ALTER FOREIGN DATA_P WRAPPER name fdw_options
4265 				{
4266 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4267 					n->fdwname = $5;
4268 					n->func_options = $6;
4269 					n->options = NIL;
4270 					$$ = (Node *) n;
4271 				}
4272 		;
4273 
4274 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
4275 create_generic_options:
4276 			OPTIONS '(' generic_option_list ')'			{ $$ = $3; }
4277 			| /*EMPTY*/									{ $$ = NIL; }
4278 		;
4279 
4280 generic_option_list:
4281 			generic_option_elem
4282 				{
4283 					$$ = list_make1($1);
4284 				}
4285 			| generic_option_list ',' generic_option_elem
4286 				{
4287 					$$ = lappend($1, $3);
4288 				}
4289 		;
4290 
4291 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
4292 alter_generic_options:
4293 			OPTIONS	'(' alter_generic_option_list ')'		{ $$ = $3; }
4294 		;
4295 
4296 alter_generic_option_list:
4297 			alter_generic_option_elem
4298 				{
4299 					$$ = list_make1($1);
4300 				}
4301 			| alter_generic_option_list ',' alter_generic_option_elem
4302 				{
4303 					$$ = lappend($1, $3);
4304 				}
4305 		;
4306 
4307 alter_generic_option_elem:
4308 			generic_option_elem
4309 				{
4310 					$$ = $1;
4311 				}
4312 			| SET generic_option_elem
4313 				{
4314 					$$ = $2;
4315 					$$->defaction = DEFELEM_SET;
4316 				}
4317 			| ADD_P generic_option_elem
4318 				{
4319 					$$ = $2;
4320 					$$->defaction = DEFELEM_ADD;
4321 				}
4322 			| DROP generic_option_name
4323 				{
4324 					$$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP);
4325 				}
4326 		;
4327 
4328 generic_option_elem:
4329 			generic_option_name generic_option_arg
4330 				{
4331 					$$ = makeDefElem($1, $2);
4332 				}
4333 		;
4334 
4335 generic_option_name:
4336 				ColLabel			{ $$ = $1; }
4337 		;
4338 
4339 /* We could use def_arg here, but the spec only requires string literals */
4340 generic_option_arg:
4341 				Sconst				{ $$ = (Node *) makeString($1); }
4342 		;
4343 
4344 /*****************************************************************************
4345  *
4346  *		QUERY:
4347  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
4348  *
4349  *****************************************************************************/
4350 
4351 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
4352 						 FOREIGN DATA_P WRAPPER name create_generic_options
4353 				{
4354 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4355 					n->servername = $3;
4356 					n->servertype = $4;
4357 					n->version = $5;
4358 					n->fdwname = $9;
4359 					n->options = $10;
4360 					$$ = (Node *) n;
4361 				}
4362 		;
4363 
4364 opt_type:
4365 			TYPE_P Sconst			{ $$ = $2; }
4366 			| /*EMPTY*/				{ $$ = NULL; }
4367 		;
4368 
4369 
4370 foreign_server_version:
4371 			VERSION_P Sconst		{ $$ = $2; }
4372 		|	VERSION_P NULL_P		{ $$ = NULL; }
4373 		;
4374 
4375 opt_foreign_server_version:
4376 			foreign_server_version	{ $$ = $1; }
4377 			| /*EMPTY*/				{ $$ = NULL; }
4378 		;
4379 
4380 /*****************************************************************************
4381  *
4382  *		QUERY :
4383  *				DROP SERVER name
4384  *
4385  ****************************************************************************/
4386 
4387 DropForeignServerStmt: DROP SERVER name opt_drop_behavior
4388 				{
4389 					DropStmt *n = makeNode(DropStmt);
4390 					n->removeType = OBJECT_FOREIGN_SERVER;
4391 					n->objects = list_make1(list_make1(makeString($3)));
4392 					n->arguments = NIL;
4393 					n->missing_ok = false;
4394 					n->behavior = $4;
4395 					n->concurrent = false;
4396 					$$ = (Node *) n;
4397 				}
4398 				|  DROP SERVER IF_P EXISTS name opt_drop_behavior
4399 				{
4400 					DropStmt *n = makeNode(DropStmt);
4401 					n->removeType = OBJECT_FOREIGN_SERVER;
4402 					n->objects = list_make1(list_make1(makeString($5)));
4403 					n->arguments = NIL;
4404 					n->missing_ok = true;
4405 					n->behavior = $6;
4406 					n->concurrent = false;
4407 					$$ = (Node *) n;
4408 				}
4409 		;
4410 
4411 /*****************************************************************************
4412  *
4413  *		QUERY :
4414  *				ALTER SERVER name [VERSION] [OPTIONS]
4415  *
4416  ****************************************************************************/
4417 
4418 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
4419 				{
4420 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4421 					n->servername = $3;
4422 					n->version = $4;
4423 					n->options = $5;
4424 					n->has_version = true;
4425 					$$ = (Node *) n;
4426 				}
4427 			| ALTER SERVER name foreign_server_version
4428 				{
4429 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4430 					n->servername = $3;
4431 					n->version = $4;
4432 					n->has_version = true;
4433 					$$ = (Node *) n;
4434 				}
4435 			| ALTER SERVER name alter_generic_options
4436 				{
4437 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4438 					n->servername = $3;
4439 					n->options = $4;
4440 					$$ = (Node *) n;
4441 				}
4442 		;
4443 
4444 /*****************************************************************************
4445  *
4446  *		QUERY:
4447  *             CREATE FOREIGN TABLE relname (...) SERVER name (...)
4448  *
4449  *****************************************************************************/
4450 
4451 CreateForeignTableStmt:
4452 		CREATE FOREIGN TABLE qualified_name
4453 			'(' OptTableElementList ')'
4454 			OptInherit SERVER name create_generic_options
4455 				{
4456 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4457 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
4458 					n->base.relation = $4;
4459 					n->base.tableElts = $6;
4460 					n->base.inhRelations = $8;
4461 					n->base.ofTypename = NULL;
4462 					n->base.constraints = NIL;
4463 					n->base.options = NIL;
4464 					n->base.oncommit = ONCOMMIT_NOOP;
4465 					n->base.tablespacename = NULL;
4466 					n->base.if_not_exists = false;
4467 					/* FDW-specific data */
4468 					n->servername = $10;
4469 					n->options = $11;
4470 					$$ = (Node *) n;
4471 				}
4472 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
4473 			'(' OptTableElementList ')'
4474 			OptInherit SERVER name create_generic_options
4475 				{
4476 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4477 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
4478 					n->base.relation = $7;
4479 					n->base.tableElts = $9;
4480 					n->base.inhRelations = $11;
4481 					n->base.ofTypename = NULL;
4482 					n->base.constraints = NIL;
4483 					n->base.options = NIL;
4484 					n->base.oncommit = ONCOMMIT_NOOP;
4485 					n->base.tablespacename = NULL;
4486 					n->base.if_not_exists = true;
4487 					/* FDW-specific data */
4488 					n->servername = $13;
4489 					n->options = $14;
4490 					$$ = (Node *) n;
4491 				}
4492 		;
4493 
4494 /*****************************************************************************
4495  *
4496  *		QUERY:
4497  *             ALTER FOREIGN TABLE relname [...]
4498  *
4499  *****************************************************************************/
4500 
4501 AlterForeignTableStmt:
4502 			ALTER FOREIGN TABLE relation_expr alter_table_cmds
4503 				{
4504 					AlterTableStmt *n = makeNode(AlterTableStmt);
4505 					n->relation = $4;
4506 					n->cmds = $5;
4507 					n->relkind = OBJECT_FOREIGN_TABLE;
4508 					n->missing_ok = false;
4509 					$$ = (Node *)n;
4510 				}
4511 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
4512 				{
4513 					AlterTableStmt *n = makeNode(AlterTableStmt);
4514 					n->relation = $6;
4515 					n->cmds = $7;
4516 					n->relkind = OBJECT_FOREIGN_TABLE;
4517 					n->missing_ok = true;
4518 					$$ = (Node *)n;
4519 				}
4520 		;
4521 
4522 /*****************************************************************************
4523  *
4524  *		QUERY:
4525  *				IMPORT FOREIGN SCHEMA remote_schema
4526  *				[ { LIMIT TO | EXCEPT } ( table_list ) ]
4527  *				FROM SERVER server_name INTO local_schema [ OPTIONS (...) ]
4528  *
4529  ****************************************************************************/
4530 
4531 ImportForeignSchemaStmt:
4532 		IMPORT_P FOREIGN SCHEMA name import_qualification
4533 		  FROM SERVER name INTO name create_generic_options
4534 			{
4535 				ImportForeignSchemaStmt *n = makeNode(ImportForeignSchemaStmt);
4536 				n->server_name = $8;
4537 				n->remote_schema = $4;
4538 				n->local_schema = $10;
4539 				n->list_type = $5->type;
4540 				n->table_list = $5->table_names;
4541 				n->options = $11;
4542 				$$ = (Node *) n;
4543 			}
4544 		;
4545 
4546 import_qualification_type:
4547 		LIMIT TO 				{ $$ = FDW_IMPORT_SCHEMA_LIMIT_TO; }
4548 		| EXCEPT 				{ $$ = FDW_IMPORT_SCHEMA_EXCEPT; }
4549 		;
4550 
4551 import_qualification:
4552 		import_qualification_type '(' relation_expr_list ')'
4553 			{
4554 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
4555 				n->type = $1;
4556 				n->table_names = $3;
4557 				$$ = n;
4558 			}
4559 		| /*EMPTY*/
4560 			{
4561 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
4562 				n->type = FDW_IMPORT_SCHEMA_ALL;
4563 				n->table_names = NIL;
4564 				$$ = n;
4565 			}
4566 		;
4567 
4568 /*****************************************************************************
4569  *
4570  *		QUERY:
4571  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
4572  *
4573  *****************************************************************************/
4574 
4575 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
4576 				{
4577 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
4578 					n->user = $5;
4579 					n->servername = $7;
4580 					n->options = $8;
4581 					$$ = (Node *) n;
4582 				}
4583 		;
4584 
4585 /* User mapping authorization identifier */
4586 auth_ident: RoleSpec			{ $$ = $1; }
4587 			| USER				{ $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1); }
4588 		;
4589 
4590 /*****************************************************************************
4591  *
4592  *		QUERY :
4593  *				DROP USER MAPPING FOR auth_ident SERVER name
4594  *
4595  * XXX you'd think this should have a CASCADE/RESTRICT option, even if it's
4596  * only pro forma; but the SQL standard doesn't show one.
4597  ****************************************************************************/
4598 
4599 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
4600 				{
4601 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
4602 					n->user = $5;
4603 					n->servername = $7;
4604 					n->missing_ok = false;
4605 					$$ = (Node *) n;
4606 				}
4607 				|  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
4608 				{
4609 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
4610 					n->user = $7;
4611 					n->servername = $9;
4612 					n->missing_ok = true;
4613 					$$ = (Node *) n;
4614 				}
4615 		;
4616 
4617 /*****************************************************************************
4618  *
4619  *		QUERY :
4620  *				ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
4621  *
4622  ****************************************************************************/
4623 
4624 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
4625 				{
4626 					AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
4627 					n->user = $5;
4628 					n->servername = $7;
4629 					n->options = $8;
4630 					$$ = (Node *) n;
4631 				}
4632 		;
4633 
4634 /*****************************************************************************
4635  *
4636  *		QUERIES:
4637  *				CREATE POLICY name ON table [FOR cmd] [TO role, ...]
4638  *					[USING (qual)] [WITH CHECK (with_check)]
4639  *				ALTER POLICY name ON table [TO role, ...]
4640  *					[USING (qual)] [WITH CHECK (with_check)]
4641  *				DROP POLICY name ON table
4642  *
4643  *****************************************************************************/
4644 
4645 CreatePolicyStmt:
4646 			CREATE POLICY name ON qualified_name RowSecurityDefaultForCmd
4647 				RowSecurityDefaultToRole RowSecurityOptionalExpr
4648 				RowSecurityOptionalWithCheck
4649 				{
4650 					CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
4651 					n->policy_name = $3;
4652 					n->table = $5;
4653 					n->cmd_name = $6;
4654 					n->roles = $7;
4655 					n->qual = $8;
4656 					n->with_check = $9;
4657 					$$ = (Node *) n;
4658 				}
4659 		;
4660 
4661 AlterPolicyStmt:
4662 			ALTER POLICY name ON qualified_name RowSecurityOptionalToRole
4663 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
4664 				{
4665 					AlterPolicyStmt *n = makeNode(AlterPolicyStmt);
4666 					n->policy_name = $3;
4667 					n->table = $5;
4668 					n->roles = $6;
4669 					n->qual = $7;
4670 					n->with_check = $8;
4671 					$$ = (Node *) n;
4672 				}
4673 		;
4674 
4675 DropPolicyStmt:
4676 			DROP POLICY name ON any_name opt_drop_behavior
4677 				{
4678 					DropStmt *n = makeNode(DropStmt);
4679 					n->removeType = OBJECT_POLICY;
4680 					n->objects = list_make1(lappend($5, makeString($3)));
4681 					n->arguments = NIL;
4682 					n->behavior = $6;
4683 					n->missing_ok = false;
4684 					n->concurrent = false;
4685 					$$ = (Node *) n;
4686 				}
4687 			| DROP POLICY IF_P EXISTS name ON any_name opt_drop_behavior
4688 				{
4689 					DropStmt *n = makeNode(DropStmt);
4690 					n->removeType = OBJECT_POLICY;
4691 					n->objects = list_make1(lappend($7, makeString($5)));
4692 					n->arguments = NIL;
4693 					n->behavior = $8;
4694 					n->missing_ok = true;
4695 					n->concurrent = false;
4696 					$$ = (Node *) n;
4697 				}
4698 		;
4699 
4700 RowSecurityOptionalExpr:
4701 			USING '(' a_expr ')'	{ $$ = $3; }
4702 			| /* EMPTY */			{ $$ = NULL; }
4703 		;
4704 
4705 RowSecurityOptionalWithCheck:
4706 			WITH CHECK '(' a_expr ')'		{ $$ = $4; }
4707 			| /* EMPTY */					{ $$ = NULL; }
4708 		;
4709 
4710 RowSecurityDefaultToRole:
4711 			TO role_list			{ $$ = $2; }
4712 			| /* EMPTY */			{ $$ = list_make1(makeRoleSpec(ROLESPEC_PUBLIC, -1)); }
4713 		;
4714 
4715 RowSecurityOptionalToRole:
4716 			TO role_list			{ $$ = $2; }
4717 			| /* EMPTY */			{ $$ = NULL; }
4718 		;
4719 
4720 RowSecurityDefaultForCmd:
4721 			FOR row_security_cmd	{ $$ = $2; }
4722 			| /* EMPTY */			{ $$ = "all"; }
4723 		;
4724 
4725 row_security_cmd:
4726 			ALL				{ $$ = "all"; }
4727 		|	SELECT			{ $$ = "select"; }
4728 		|	INSERT			{ $$ = "insert"; }
4729 		|	UPDATE			{ $$ = "update"; }
4730 		|	DELETE_P		{ $$ = "delete"; }
4731 		;
4732 
4733 /*****************************************************************************
4734  *
4735  *		QUERY:
4736  *             CREATE ACCESS METHOD name HANDLER handler_name
4737  *
4738  *****************************************************************************/
4739 
4740 CreateAmStmt: CREATE ACCESS METHOD name TYPE_P INDEX HANDLER handler_name
4741 				{
4742 					CreateAmStmt *n = makeNode(CreateAmStmt);
4743 					n->amname = $4;
4744 					n->handler_name = $8;
4745 					n->amtype = AMTYPE_INDEX;
4746 					$$ = (Node *) n;
4747 				}
4748 		;
4749 
4750 /*****************************************************************************
4751  *
4752  *		QUERIES :
4753  *				CREATE TRIGGER ...
4754  *				DROP TRIGGER ...
4755  *
4756  *****************************************************************************/
4757 
4758 CreateTrigStmt:
4759 			CREATE TRIGGER name TriggerActionTime TriggerEvents ON
4760 			qualified_name TriggerForSpec TriggerWhen
4761 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
4762 				{
4763 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
4764 					n->trigname = $3;
4765 					n->relation = $7;
4766 					n->funcname = $12;
4767 					n->args = $14;
4768 					n->row = $8;
4769 					n->timing = $4;
4770 					n->events = intVal(linitial($5));
4771 					n->columns = (List *) lsecond($5);
4772 					n->whenClause = $9;
4773 					n->isconstraint  = FALSE;
4774 					n->deferrable	 = FALSE;
4775 					n->initdeferred  = FALSE;
4776 					n->constrrel = NULL;
4777 					$$ = (Node *)n;
4778 				}
4779 			| CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
4780 			qualified_name OptConstrFromTable ConstraintAttributeSpec
4781 			FOR EACH ROW TriggerWhen
4782 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
4783 				{
4784 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
4785 					n->trigname = $4;
4786 					n->relation = $8;
4787 					n->funcname = $17;
4788 					n->args = $19;
4789 					n->row = TRUE;
4790 					n->timing = TRIGGER_TYPE_AFTER;
4791 					n->events = intVal(linitial($6));
4792 					n->columns = (List *) lsecond($6);
4793 					n->whenClause = $14;
4794 					n->isconstraint  = TRUE;
4795 					processCASbits($10, @10, "TRIGGER",
4796 								   &n->deferrable, &n->initdeferred, NULL,
4797 								   NULL, yyscanner);
4798 					n->constrrel = $9;
4799 					$$ = (Node *)n;
4800 				}
4801 		;
4802 
4803 TriggerActionTime:
4804 			BEFORE								{ $$ = TRIGGER_TYPE_BEFORE; }
4805 			| AFTER								{ $$ = TRIGGER_TYPE_AFTER; }
4806 			| INSTEAD OF						{ $$ = TRIGGER_TYPE_INSTEAD; }
4807 		;
4808 
4809 TriggerEvents:
4810 			TriggerOneEvent
4811 				{ $$ = $1; }
4812 			| TriggerEvents OR TriggerOneEvent
4813 				{
4814 					int		events1 = intVal(linitial($1));
4815 					int		events2 = intVal(linitial($3));
4816 					List   *columns1 = (List *) lsecond($1);
4817 					List   *columns2 = (List *) lsecond($3);
4818 
4819 					if (events1 & events2)
4820 						parser_yyerror("duplicate trigger events specified");
4821 					/*
4822 					 * concat'ing the columns lists loses information about
4823 					 * which columns went with which event, but so long as
4824 					 * only UPDATE carries columns and we disallow multiple
4825 					 * UPDATE items, it doesn't matter.  Command execution
4826 					 * should just ignore the columns for non-UPDATE events.
4827 					 */
4828 					$$ = list_make2(makeInteger(events1 | events2),
4829 									list_concat(columns1, columns2));
4830 				}
4831 		;
4832 
4833 TriggerOneEvent:
4834 			INSERT
4835 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
4836 			| DELETE_P
4837 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
4838 			| UPDATE
4839 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
4840 			| UPDATE OF columnList
4841 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
4842 			| TRUNCATE
4843 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
4844 		;
4845 
4846 TriggerForSpec:
4847 			FOR TriggerForOptEach TriggerForType
4848 				{
4849 					$$ = $3;
4850 				}
4851 			| /* EMPTY */
4852 				{
4853 					/*
4854 					 * If ROW/STATEMENT not specified, default to
4855 					 * STATEMENT, per SQL
4856 					 */
4857 					$$ = FALSE;
4858 				}
4859 		;
4860 
4861 TriggerForOptEach:
4862 			EACH									{}
4863 			| /*EMPTY*/								{}
4864 		;
4865 
4866 TriggerForType:
4867 			ROW										{ $$ = TRUE; }
4868 			| STATEMENT								{ $$ = FALSE; }
4869 		;
4870 
4871 TriggerWhen:
4872 			WHEN '(' a_expr ')'						{ $$ = $3; }
4873 			| /*EMPTY*/								{ $$ = NULL; }
4874 		;
4875 
4876 TriggerFuncArgs:
4877 			TriggerFuncArg							{ $$ = list_make1($1); }
4878 			| TriggerFuncArgs ',' TriggerFuncArg	{ $$ = lappend($1, $3); }
4879 			| /*EMPTY*/								{ $$ = NIL; }
4880 		;
4881 
4882 TriggerFuncArg:
4883 			Iconst
4884 				{
4885 					$$ = makeString(psprintf("%d", $1));
4886 				}
4887 			| FCONST								{ $$ = makeString($1); }
4888 			| Sconst								{ $$ = makeString($1); }
4889 			| ColLabel								{ $$ = makeString($1); }
4890 		;
4891 
4892 OptConstrFromTable:
4893 			FROM qualified_name						{ $$ = $2; }
4894 			| /*EMPTY*/								{ $$ = NULL; }
4895 		;
4896 
4897 ConstraintAttributeSpec:
4898 			/*EMPTY*/
4899 				{ $$ = 0; }
4900 			| ConstraintAttributeSpec ConstraintAttributeElem
4901 				{
4902 					/*
4903 					 * We must complain about conflicting options.
4904 					 * We could, but choose not to, complain about redundant
4905 					 * options (ie, where $2's bit is already set in $1).
4906 					 */
4907 					int		newspec = $1 | $2;
4908 
4909 					/* special message for this case */
4910 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED)) == (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED))
4911 						ereport(ERROR,
4912 								(errcode(ERRCODE_SYNTAX_ERROR),
4913 								 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
4914 								 parser_errposition(@2)));
4915 					/* generic message for other conflicts */
4916 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE)) == (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE) ||
4917 						(newspec & (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED)) == (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED))
4918 						ereport(ERROR,
4919 								(errcode(ERRCODE_SYNTAX_ERROR),
4920 								 errmsg("conflicting constraint properties"),
4921 								 parser_errposition(@2)));
4922 					$$ = newspec;
4923 				}
4924 		;
4925 
4926 ConstraintAttributeElem:
4927 			NOT DEFERRABLE					{ $$ = CAS_NOT_DEFERRABLE; }
4928 			| DEFERRABLE					{ $$ = CAS_DEFERRABLE; }
4929 			| INITIALLY IMMEDIATE			{ $$ = CAS_INITIALLY_IMMEDIATE; }
4930 			| INITIALLY DEFERRED			{ $$ = CAS_INITIALLY_DEFERRED; }
4931 			| NOT VALID						{ $$ = CAS_NOT_VALID; }
4932 			| NO INHERIT					{ $$ = CAS_NO_INHERIT; }
4933 		;
4934 
4935 
4936 DropTrigStmt:
4937 			DROP TRIGGER name ON any_name opt_drop_behavior
4938 				{
4939 					DropStmt *n = makeNode(DropStmt);
4940 					n->removeType = OBJECT_TRIGGER;
4941 					n->objects = list_make1(lappend($5, makeString($3)));
4942 					n->arguments = NIL;
4943 					n->behavior = $6;
4944 					n->missing_ok = false;
4945 					n->concurrent = false;
4946 					$$ = (Node *) n;
4947 				}
4948 			| DROP TRIGGER IF_P EXISTS name ON any_name opt_drop_behavior
4949 				{
4950 					DropStmt *n = makeNode(DropStmt);
4951 					n->removeType = OBJECT_TRIGGER;
4952 					n->objects = list_make1(lappend($7, makeString($5)));
4953 					n->arguments = NIL;
4954 					n->behavior = $8;
4955 					n->missing_ok = true;
4956 					n->concurrent = false;
4957 					$$ = (Node *) n;
4958 				}
4959 		;
4960 
4961 
4962 /*****************************************************************************
4963  *
4964  *		QUERIES :
4965  *				CREATE EVENT TRIGGER ...
4966  *				ALTER EVENT TRIGGER ...
4967  *
4968  *****************************************************************************/
4969 
4970 CreateEventTrigStmt:
4971 			CREATE EVENT TRIGGER name ON ColLabel
4972 			EXECUTE PROCEDURE func_name '(' ')'
4973 				{
4974 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
4975 					n->trigname = $4;
4976 					n->eventname = $6;
4977 					n->whenclause = NULL;
4978 					n->funcname = $9;
4979 					$$ = (Node *)n;
4980 				}
4981 		  | CREATE EVENT TRIGGER name ON ColLabel
4982 			WHEN event_trigger_when_list
4983 			EXECUTE PROCEDURE func_name '(' ')'
4984 				{
4985 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
4986 					n->trigname = $4;
4987 					n->eventname = $6;
4988 					n->whenclause = $8;
4989 					n->funcname = $11;
4990 					$$ = (Node *)n;
4991 				}
4992 		;
4993 
4994 event_trigger_when_list:
4995 		  event_trigger_when_item
4996 			{ $$ = list_make1($1); }
4997 		| event_trigger_when_list AND event_trigger_when_item
4998 			{ $$ = lappend($1, $3); }
4999 		;
5000 
5001 event_trigger_when_item:
5002 		ColId IN_P '(' event_trigger_value_list ')'
5003 			{ $$ = makeDefElem($1, (Node *) $4); }
5004 		;
5005 
5006 event_trigger_value_list:
5007 		  SCONST
5008 			{ $$ = list_make1(makeString($1)); }
5009 		| event_trigger_value_list ',' SCONST
5010 			{ $$ = lappend($1, makeString($3)); }
5011 		;
5012 
5013 AlterEventTrigStmt:
5014 			ALTER EVENT TRIGGER name enable_trigger
5015 				{
5016 					AlterEventTrigStmt *n = makeNode(AlterEventTrigStmt);
5017 					n->trigname = $4;
5018 					n->tgenabled = $5;
5019 					$$ = (Node *) n;
5020 				}
5021 		;
5022 
5023 enable_trigger:
5024 			ENABLE_P					{ $$ = TRIGGER_FIRES_ON_ORIGIN; }
5025 			| ENABLE_P REPLICA			{ $$ = TRIGGER_FIRES_ON_REPLICA; }
5026 			| ENABLE_P ALWAYS			{ $$ = TRIGGER_FIRES_ALWAYS; }
5027 			| DISABLE_P					{ $$ = TRIGGER_DISABLED; }
5028 		;
5029 
5030 /*****************************************************************************
5031  *
5032  *		QUERIES :
5033  *				CREATE ASSERTION ...
5034  *				DROP ASSERTION ...
5035  *
5036  *****************************************************************************/
5037 
5038 CreateAssertStmt:
5039 			CREATE ASSERTION name CHECK '(' a_expr ')'
5040 			ConstraintAttributeSpec
5041 				{
5042 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5043 					n->trigname = $3;
5044 					n->args = list_make1($6);
5045 					n->isconstraint  = TRUE;
5046 					processCASbits($8, @8, "ASSERTION",
5047 								   &n->deferrable, &n->initdeferred, NULL,
5048 								   NULL, yyscanner);
5049 
5050 					ereport(ERROR,
5051 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5052 							 errmsg("CREATE ASSERTION is not yet implemented")));
5053 
5054 					$$ = (Node *)n;
5055 				}
5056 		;
5057 
5058 DropAssertStmt:
5059 			DROP ASSERTION name opt_drop_behavior
5060 				{
5061 					DropStmt *n = makeNode(DropStmt);
5062 					n->objects = NIL;
5063 					n->arguments = NIL;
5064 					n->behavior = $4;
5065 					n->removeType = OBJECT_TRIGGER; /* XXX */
5066 					ereport(ERROR,
5067 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5068 							 errmsg("DROP ASSERTION is not yet implemented")));
5069 					$$ = (Node *) n;
5070 				}
5071 		;
5072 
5073 
5074 /*****************************************************************************
5075  *
5076  *		QUERY :
5077  *				define (aggregate,operator,type)
5078  *
5079  *****************************************************************************/
5080 
5081 DefineStmt:
5082 			CREATE AGGREGATE func_name aggr_args definition
5083 				{
5084 					DefineStmt *n = makeNode(DefineStmt);
5085 					n->kind = OBJECT_AGGREGATE;
5086 					n->oldstyle = false;
5087 					n->defnames = $3;
5088 					n->args = $4;
5089 					n->definition = $5;
5090 					$$ = (Node *)n;
5091 				}
5092 			| CREATE AGGREGATE func_name old_aggr_definition
5093 				{
5094 					/* old-style (pre-8.2) syntax for CREATE AGGREGATE */
5095 					DefineStmt *n = makeNode(DefineStmt);
5096 					n->kind = OBJECT_AGGREGATE;
5097 					n->oldstyle = true;
5098 					n->defnames = $3;
5099 					n->args = NIL;
5100 					n->definition = $4;
5101 					$$ = (Node *)n;
5102 				}
5103 			| CREATE OPERATOR any_operator definition
5104 				{
5105 					DefineStmt *n = makeNode(DefineStmt);
5106 					n->kind = OBJECT_OPERATOR;
5107 					n->oldstyle = false;
5108 					n->defnames = $3;
5109 					n->args = NIL;
5110 					n->definition = $4;
5111 					$$ = (Node *)n;
5112 				}
5113 			| CREATE TYPE_P any_name definition
5114 				{
5115 					DefineStmt *n = makeNode(DefineStmt);
5116 					n->kind = OBJECT_TYPE;
5117 					n->oldstyle = false;
5118 					n->defnames = $3;
5119 					n->args = NIL;
5120 					n->definition = $4;
5121 					$$ = (Node *)n;
5122 				}
5123 			| CREATE TYPE_P any_name
5124 				{
5125 					/* Shell type (identified by lack of definition) */
5126 					DefineStmt *n = makeNode(DefineStmt);
5127 					n->kind = OBJECT_TYPE;
5128 					n->oldstyle = false;
5129 					n->defnames = $3;
5130 					n->args = NIL;
5131 					n->definition = NIL;
5132 					$$ = (Node *)n;
5133 				}
5134 			| CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
5135 				{
5136 					CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
5137 
5138 					/* can't use qualified_name, sigh */
5139 					n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
5140 					n->coldeflist = $6;
5141 					$$ = (Node *)n;
5142 				}
5143 			| CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
5144 				{
5145 					CreateEnumStmt *n = makeNode(CreateEnumStmt);
5146 					n->typeName = $3;
5147 					n->vals = $7;
5148 					$$ = (Node *)n;
5149 				}
5150 			| CREATE TYPE_P any_name AS RANGE definition
5151 				{
5152 					CreateRangeStmt *n = makeNode(CreateRangeStmt);
5153 					n->typeName = $3;
5154 					n->params	= $6;
5155 					$$ = (Node *)n;
5156 				}
5157 			| CREATE TEXT_P SEARCH PARSER any_name definition
5158 				{
5159 					DefineStmt *n = makeNode(DefineStmt);
5160 					n->kind = OBJECT_TSPARSER;
5161 					n->args = NIL;
5162 					n->defnames = $5;
5163 					n->definition = $6;
5164 					$$ = (Node *)n;
5165 				}
5166 			| CREATE TEXT_P SEARCH DICTIONARY any_name definition
5167 				{
5168 					DefineStmt *n = makeNode(DefineStmt);
5169 					n->kind = OBJECT_TSDICTIONARY;
5170 					n->args = NIL;
5171 					n->defnames = $5;
5172 					n->definition = $6;
5173 					$$ = (Node *)n;
5174 				}
5175 			| CREATE TEXT_P SEARCH TEMPLATE any_name definition
5176 				{
5177 					DefineStmt *n = makeNode(DefineStmt);
5178 					n->kind = OBJECT_TSTEMPLATE;
5179 					n->args = NIL;
5180 					n->defnames = $5;
5181 					n->definition = $6;
5182 					$$ = (Node *)n;
5183 				}
5184 			| CREATE TEXT_P SEARCH CONFIGURATION any_name definition
5185 				{
5186 					DefineStmt *n = makeNode(DefineStmt);
5187 					n->kind = OBJECT_TSCONFIGURATION;
5188 					n->args = NIL;
5189 					n->defnames = $5;
5190 					n->definition = $6;
5191 					$$ = (Node *)n;
5192 				}
5193 			| CREATE COLLATION any_name definition
5194 				{
5195 					DefineStmt *n = makeNode(DefineStmt);
5196 					n->kind = OBJECT_COLLATION;
5197 					n->args = NIL;
5198 					n->defnames = $3;
5199 					n->definition = $4;
5200 					$$ = (Node *)n;
5201 				}
5202 			| CREATE COLLATION any_name FROM any_name
5203 				{
5204 					DefineStmt *n = makeNode(DefineStmt);
5205 					n->kind = OBJECT_COLLATION;
5206 					n->args = NIL;
5207 					n->defnames = $3;
5208 					n->definition = list_make1(makeDefElem("from", (Node *) $5));
5209 					$$ = (Node *)n;
5210 				}
5211 		;
5212 
5213 definition: '(' def_list ')'						{ $$ = $2; }
5214 		;
5215 
5216 def_list:	def_elem								{ $$ = list_make1($1); }
5217 			| def_list ',' def_elem					{ $$ = lappend($1, $3); }
5218 		;
5219 
5220 def_elem:	ColLabel '=' def_arg
5221 				{
5222 					$$ = makeDefElem($1, (Node *) $3);
5223 				}
5224 			| ColLabel
5225 				{
5226 					$$ = makeDefElem($1, NULL);
5227 				}
5228 		;
5229 
5230 /* Note: any simple identifier will be returned as a type name! */
5231 def_arg:	func_type						{ $$ = (Node *)$1; }
5232 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
5233 			| qual_all_Op					{ $$ = (Node *)$1; }
5234 			| NumericOnly					{ $$ = (Node *)$1; }
5235 			| Sconst						{ $$ = (Node *)makeString($1); }
5236 		;
5237 
5238 old_aggr_definition: '(' old_aggr_list ')'			{ $$ = $2; }
5239 		;
5240 
5241 old_aggr_list: old_aggr_elem						{ $$ = list_make1($1); }
5242 			| old_aggr_list ',' old_aggr_elem		{ $$ = lappend($1, $3); }
5243 		;
5244 
5245 /*
5246  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
5247  * the item names needed in old aggregate definitions are likely to become
5248  * SQL keywords.
5249  */
5250 old_aggr_elem:  IDENT '=' def_arg
5251 				{
5252 					$$ = makeDefElem($1, (Node *)$3);
5253 				}
5254 		;
5255 
5256 opt_enum_val_list:
5257 		enum_val_list							{ $$ = $1; }
5258 		| /*EMPTY*/								{ $$ = NIL; }
5259 		;
5260 
5261 enum_val_list:	Sconst
5262 				{ $$ = list_make1(makeString($1)); }
5263 			| enum_val_list ',' Sconst
5264 				{ $$ = lappend($1, makeString($3)); }
5265 		;
5266 
5267 /*****************************************************************************
5268  *
5269  *	ALTER TYPE enumtype ADD ...
5270  *
5271  *****************************************************************************/
5272 
5273 AlterEnumStmt:
5274 		ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst
5275 			{
5276 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5277 				n->typeName = $3;
5278 				n->newVal = $7;
5279 				n->newValNeighbor = NULL;
5280 				n->newValIsAfter = true;
5281 				n->skipIfExists = $6;
5282 				$$ = (Node *) n;
5283 			}
5284 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst BEFORE Sconst
5285 			{
5286 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5287 				n->typeName = $3;
5288 				n->newVal = $7;
5289 				n->newValNeighbor = $9;
5290 				n->newValIsAfter = false;
5291 				n->skipIfExists = $6;
5292 				$$ = (Node *) n;
5293 			}
5294 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst AFTER Sconst
5295 			{
5296 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5297 				n->typeName = $3;
5298 				n->newVal = $7;
5299 				n->newValNeighbor = $9;
5300 				n->newValIsAfter = true;
5301 				n->skipIfExists = $6;
5302 				$$ = (Node *) n;
5303 			}
5304 		 ;
5305 
5306 opt_if_not_exists: IF_P NOT EXISTS              { $$ = true; }
5307 		| /* empty */                          { $$ = false; }
5308 		;
5309 
5310 
5311 /*****************************************************************************
5312  *
5313  *		QUERIES :
5314  *				CREATE OPERATOR CLASS ...
5315  *				CREATE OPERATOR FAMILY ...
5316  *				ALTER OPERATOR FAMILY ...
5317  *				DROP OPERATOR CLASS ...
5318  *				DROP OPERATOR FAMILY ...
5319  *
5320  *****************************************************************************/
5321 
5322 CreateOpClassStmt:
5323 			CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
5324 			USING access_method opt_opfamily AS opclass_item_list
5325 				{
5326 					CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
5327 					n->opclassname = $4;
5328 					n->isDefault = $5;
5329 					n->datatype = $8;
5330 					n->amname = $10;
5331 					n->opfamilyname = $11;
5332 					n->items = $13;
5333 					$$ = (Node *) n;
5334 				}
5335 		;
5336 
5337 opclass_item_list:
5338 			opclass_item							{ $$ = list_make1($1); }
5339 			| opclass_item_list ',' opclass_item	{ $$ = lappend($1, $3); }
5340 		;
5341 
5342 opclass_item:
5343 			OPERATOR Iconst any_operator opclass_purpose opt_recheck
5344 				{
5345 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5346 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5347 					n->name = $3;
5348 					n->args = NIL;
5349 					n->number = $2;
5350 					n->order_family = $4;
5351 					$$ = (Node *) n;
5352 				}
5353 			| OPERATOR Iconst any_operator oper_argtypes opclass_purpose
5354 			  opt_recheck
5355 				{
5356 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5357 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5358 					n->name = $3;
5359 					n->args = $4;
5360 					n->number = $2;
5361 					n->order_family = $5;
5362 					$$ = (Node *) n;
5363 				}
5364 			| FUNCTION Iconst func_name func_args
5365 				{
5366 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5367 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5368 					n->name = $3;
5369 					n->args = extractArgTypes($4);
5370 					n->number = $2;
5371 					$$ = (Node *) n;
5372 				}
5373 			| FUNCTION Iconst '(' type_list ')' func_name func_args
5374 				{
5375 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5376 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5377 					n->name = $6;
5378 					n->args = extractArgTypes($7);
5379 					n->number = $2;
5380 					n->class_args = $4;
5381 					$$ = (Node *) n;
5382 				}
5383 			| STORAGE Typename
5384 				{
5385 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5386 					n->itemtype = OPCLASS_ITEM_STORAGETYPE;
5387 					n->storedtype = $2;
5388 					$$ = (Node *) n;
5389 				}
5390 		;
5391 
5392 opt_default:	DEFAULT						{ $$ = TRUE; }
5393 			| /*EMPTY*/						{ $$ = FALSE; }
5394 		;
5395 
5396 opt_opfamily:	FAMILY any_name				{ $$ = $2; }
5397 			| /*EMPTY*/						{ $$ = NIL; }
5398 		;
5399 
5400 opclass_purpose: FOR SEARCH					{ $$ = NIL; }
5401 			| FOR ORDER BY any_name			{ $$ = $4; }
5402 			| /*EMPTY*/						{ $$ = NIL; }
5403 		;
5404 
5405 opt_recheck:	RECHECK
5406 				{
5407 					/*
5408 					 * RECHECK no longer does anything in opclass definitions,
5409 					 * but we still accept it to ease porting of old database
5410 					 * dumps.
5411 					 */
5412 					ereport(NOTICE,
5413 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5414 							 errmsg("RECHECK is no longer required"),
5415 							 errhint("Update your data type."),
5416 							 parser_errposition(@1)));
5417 					$$ = TRUE;
5418 				}
5419 			| /*EMPTY*/						{ $$ = FALSE; }
5420 		;
5421 
5422 
5423 CreateOpFamilyStmt:
5424 			CREATE OPERATOR FAMILY any_name USING access_method
5425 				{
5426 					CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
5427 					n->opfamilyname = $4;
5428 					n->amname = $6;
5429 					$$ = (Node *) n;
5430 				}
5431 		;
5432 
5433 AlterOpFamilyStmt:
5434 			ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
5435 				{
5436 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
5437 					n->opfamilyname = $4;
5438 					n->amname = $6;
5439 					n->isDrop = false;
5440 					n->items = $8;
5441 					$$ = (Node *) n;
5442 				}
5443 			| ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
5444 				{
5445 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
5446 					n->opfamilyname = $4;
5447 					n->amname = $6;
5448 					n->isDrop = true;
5449 					n->items = $8;
5450 					$$ = (Node *) n;
5451 				}
5452 		;
5453 
5454 opclass_drop_list:
5455 			opclass_drop							{ $$ = list_make1($1); }
5456 			| opclass_drop_list ',' opclass_drop	{ $$ = lappend($1, $3); }
5457 		;
5458 
5459 opclass_drop:
5460 			OPERATOR Iconst '(' type_list ')'
5461 				{
5462 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5463 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5464 					n->number = $2;
5465 					n->args = $4;
5466 					$$ = (Node *) n;
5467 				}
5468 			| FUNCTION Iconst '(' type_list ')'
5469 				{
5470 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5471 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5472 					n->number = $2;
5473 					n->args = $4;
5474 					$$ = (Node *) n;
5475 				}
5476 		;
5477 
5478 
5479 DropOpClassStmt:
5480 			DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
5481 				{
5482 					DropStmt *n = makeNode(DropStmt);
5483 					n->objects = list_make1(lcons(makeString($6), $4));
5484 					n->removeType = OBJECT_OPCLASS;
5485 					n->behavior = $7;
5486 					n->missing_ok = false;
5487 					n->concurrent = false;
5488 					$$ = (Node *) n;
5489 				}
5490 			| DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
5491 				{
5492 					DropStmt *n = makeNode(DropStmt);
5493 					n->objects = list_make1(lcons(makeString($8), $6));
5494 					n->removeType = OBJECT_OPCLASS;
5495 					n->behavior = $9;
5496 					n->missing_ok = true;
5497 					n->concurrent = false;
5498 					$$ = (Node *) n;
5499 				}
5500 		;
5501 
5502 DropOpFamilyStmt:
5503 			DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
5504 				{
5505 					DropStmt *n = makeNode(DropStmt);
5506 					n->objects = list_make1(lcons(makeString($6), $4));
5507 					n->removeType = OBJECT_OPFAMILY;
5508 					n->behavior = $7;
5509 					n->missing_ok = false;
5510 					n->concurrent = false;
5511 					$$ = (Node *) n;
5512 				}
5513 			| DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
5514 				{
5515 					DropStmt *n = makeNode(DropStmt);
5516 					n->objects = list_make1(lcons(makeString($8), $6));
5517 					n->removeType = OBJECT_OPFAMILY;
5518 					n->behavior = $9;
5519 					n->missing_ok = true;
5520 					n->concurrent = false;
5521 					$$ = (Node *) n;
5522 				}
5523 		;
5524 
5525 
5526 /*****************************************************************************
5527  *
5528  *		QUERY:
5529  *
5530  *		DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
5531  *		REASSIGN OWNED BY username [, username ...] TO username
5532  *
5533  *****************************************************************************/
5534 DropOwnedStmt:
5535 			DROP OWNED BY role_list opt_drop_behavior
5536 				{
5537 					DropOwnedStmt *n = makeNode(DropOwnedStmt);
5538 					n->roles = $4;
5539 					n->behavior = $5;
5540 					$$ = (Node *)n;
5541 				}
5542 		;
5543 
5544 ReassignOwnedStmt:
5545 			REASSIGN OWNED BY role_list TO RoleSpec
5546 				{
5547 					ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
5548 					n->roles = $4;
5549 					n->newrole = $6;
5550 					$$ = (Node *)n;
5551 				}
5552 		;
5553 
5554 /*****************************************************************************
5555  *
5556  *		QUERY:
5557  *
5558  *		DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
5559  *           [ RESTRICT | CASCADE ]
5560  *
5561  *****************************************************************************/
5562 
5563 DropStmt:	DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
5564 				{
5565 					DropStmt *n = makeNode(DropStmt);
5566 					n->removeType = $2;
5567 					n->missing_ok = TRUE;
5568 					n->objects = $5;
5569 					n->arguments = NIL;
5570 					n->behavior = $6;
5571 					n->concurrent = false;
5572 					$$ = (Node *)n;
5573 				}
5574 			| DROP drop_type any_name_list opt_drop_behavior
5575 				{
5576 					DropStmt *n = makeNode(DropStmt);
5577 					n->removeType = $2;
5578 					n->missing_ok = FALSE;
5579 					n->objects = $3;
5580 					n->arguments = NIL;
5581 					n->behavior = $4;
5582 					n->concurrent = false;
5583 					$$ = (Node *)n;
5584 				}
5585 			| DROP TYPE_P type_name_list opt_drop_behavior
5586 				{
5587 					DropStmt *n = makeNode(DropStmt);
5588 					n->removeType = OBJECT_TYPE;
5589 					n->missing_ok = FALSE;
5590 					n->objects = $3;
5591 					n->behavior = $4;
5592 					n->concurrent = false;
5593 					$$ = (Node *) n;
5594 				}
5595 			| DROP TYPE_P IF_P EXISTS type_name_list opt_drop_behavior
5596 				{
5597 					DropStmt *n = makeNode(DropStmt);
5598 					n->removeType = OBJECT_TYPE;
5599 					n->missing_ok = TRUE;
5600 					n->objects = $5;
5601 					n->behavior = $6;
5602 					n->concurrent = false;
5603 					$$ = (Node *) n;
5604 				}
5605 			| DROP DOMAIN_P type_name_list opt_drop_behavior
5606 				{
5607 					DropStmt *n = makeNode(DropStmt);
5608 					n->removeType = OBJECT_DOMAIN;
5609 					n->missing_ok = FALSE;
5610 					n->objects = $3;
5611 					n->behavior = $4;
5612 					n->concurrent = false;
5613 					$$ = (Node *) n;
5614 				}
5615 			| DROP DOMAIN_P IF_P EXISTS type_name_list opt_drop_behavior
5616 				{
5617 					DropStmt *n = makeNode(DropStmt);
5618 					n->removeType = OBJECT_DOMAIN;
5619 					n->missing_ok = TRUE;
5620 					n->objects = $5;
5621 					n->behavior = $6;
5622 					n->concurrent = false;
5623 					$$ = (Node *) n;
5624 				}
5625 			| DROP INDEX CONCURRENTLY any_name_list opt_drop_behavior
5626 				{
5627 					DropStmt *n = makeNode(DropStmt);
5628 					n->removeType = OBJECT_INDEX;
5629 					n->missing_ok = FALSE;
5630 					n->objects = $4;
5631 					n->arguments = NIL;
5632 					n->behavior = $5;
5633 					n->concurrent = true;
5634 					$$ = (Node *)n;
5635 				}
5636 			| DROP INDEX CONCURRENTLY IF_P EXISTS any_name_list opt_drop_behavior
5637 				{
5638 					DropStmt *n = makeNode(DropStmt);
5639 					n->removeType = OBJECT_INDEX;
5640 					n->missing_ok = TRUE;
5641 					n->objects = $6;
5642 					n->arguments = NIL;
5643 					n->behavior = $7;
5644 					n->concurrent = true;
5645 					$$ = (Node *)n;
5646 				}
5647 		;
5648 
5649 
5650 drop_type:	TABLE									{ $$ = OBJECT_TABLE; }
5651 			| SEQUENCE								{ $$ = OBJECT_SEQUENCE; }
5652 			| VIEW									{ $$ = OBJECT_VIEW; }
5653 			| MATERIALIZED VIEW						{ $$ = OBJECT_MATVIEW; }
5654 			| INDEX									{ $$ = OBJECT_INDEX; }
5655 			| FOREIGN TABLE							{ $$ = OBJECT_FOREIGN_TABLE; }
5656 			| ACCESS METHOD							{ $$ = OBJECT_ACCESS_METHOD; }
5657 			| EVENT TRIGGER 						{ $$ = OBJECT_EVENT_TRIGGER; }
5658 			| COLLATION								{ $$ = OBJECT_COLLATION; }
5659 			| CONVERSION_P							{ $$ = OBJECT_CONVERSION; }
5660 			| SCHEMA								{ $$ = OBJECT_SCHEMA; }
5661 			| EXTENSION								{ $$ = OBJECT_EXTENSION; }
5662 			| TEXT_P SEARCH PARSER					{ $$ = OBJECT_TSPARSER; }
5663 			| TEXT_P SEARCH DICTIONARY				{ $$ = OBJECT_TSDICTIONARY; }
5664 			| TEXT_P SEARCH TEMPLATE				{ $$ = OBJECT_TSTEMPLATE; }
5665 			| TEXT_P SEARCH CONFIGURATION			{ $$ = OBJECT_TSCONFIGURATION; }
5666 		;
5667 
5668 any_name_list:
5669 			any_name								{ $$ = list_make1($1); }
5670 			| any_name_list ',' any_name			{ $$ = lappend($1, $3); }
5671 		;
5672 
5673 any_name:	ColId						{ $$ = list_make1(makeString($1)); }
5674 			| ColId attrs				{ $$ = lcons(makeString($1), $2); }
5675 		;
5676 
5677 attrs:		'.' attr_name
5678 					{ $$ = list_make1(makeString($2)); }
5679 			| attrs '.' attr_name
5680 					{ $$ = lappend($1, makeString($3)); }
5681 		;
5682 
5683 type_name_list:
5684 			Typename								{ $$ = list_make1(list_make1($1)); }
5685 			| type_name_list ',' Typename			{ $$ = lappend($1, list_make1($3)); }
5686 		;
5687 
5688 /*****************************************************************************
5689  *
5690  *		QUERY:
5691  *				truncate table relname1, relname2, ...
5692  *
5693  *****************************************************************************/
5694 
5695 TruncateStmt:
5696 			TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
5697 				{
5698 					TruncateStmt *n = makeNode(TruncateStmt);
5699 					n->relations = $3;
5700 					n->restart_seqs = $4;
5701 					n->behavior = $5;
5702 					$$ = (Node *)n;
5703 				}
5704 		;
5705 
5706 opt_restart_seqs:
5707 			CONTINUE_P IDENTITY_P		{ $$ = false; }
5708 			| RESTART IDENTITY_P		{ $$ = true; }
5709 			| /* EMPTY */				{ $$ = false; }
5710 		;
5711 
5712 /*****************************************************************************
5713  *
5714  *	The COMMENT ON statement can take different forms based upon the type of
5715  *	the object associated with the comment. The form of the statement is:
5716  *
5717  *	COMMENT ON [ [ ACCESS METHOD | CONVERSION | COLLATION |
5718  *                 DATABASE | DOMAIN |
5719  *                 EXTENSION | EVENT TRIGGER | FOREIGN DATA WRAPPER |
5720  *                 FOREIGN TABLE | INDEX | [PROCEDURAL] LANGUAGE |
5721  *                 MATERIALIZED VIEW | POLICY | ROLE | SCHEMA | SEQUENCE |
5722  *                 SERVER | TABLE | TABLESPACE |
5723  *                 TEXT SEARCH CONFIGURATION | TEXT SEARCH DICTIONARY |
5724  *                 TEXT SEARCH PARSER | TEXT SEARCH TEMPLATE | TYPE |
5725  *                 VIEW] <objname> |
5726  *				 AGGREGATE <aggname> (arg1, ...) |
5727  *				 CAST (<src type> AS <dst type>) |
5728  *				 COLUMN <relname>.<colname> |
5729  *				 CONSTRAINT <constraintname> ON <relname> |
5730  *				 CONSTRAINT <constraintname> ON DOMAIN <domainname> |
5731  *				 FUNCTION <funcname> (arg1, arg2, ...) |
5732  *				 LARGE OBJECT <oid> |
5733  *				 OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
5734  *				 OPERATOR CLASS <name> USING <access-method> |
5735  *				 OPERATOR FAMILY <name> USING <access-method> |
5736  *				 RULE <rulename> ON <relname> |
5737  *				 TRIGGER <triggername> ON <relname> ]
5738  *			   IS { 'text' | NULL }
5739  *
5740  *****************************************************************************/
5741 
5742 CommentStmt:
5743 			COMMENT ON comment_type any_name IS comment_text
5744 				{
5745 					CommentStmt *n = makeNode(CommentStmt);
5746 					n->objtype = $3;
5747 					n->objname = $4;
5748 					n->objargs = NIL;
5749 					n->comment = $6;
5750 					$$ = (Node *) n;
5751 				}
5752 			| COMMENT ON TYPE_P Typename IS comment_text
5753 				{
5754 					CommentStmt *n = makeNode(CommentStmt);
5755 					n->objtype = OBJECT_TYPE;
5756 					n->objname = list_make1($4);
5757 					n->objargs = NIL;
5758 					n->comment = $6;
5759 					$$ = (Node *) n;
5760 				}
5761 			| COMMENT ON DOMAIN_P Typename IS comment_text
5762 				{
5763 					CommentStmt *n = makeNode(CommentStmt);
5764 					n->objtype = OBJECT_DOMAIN;
5765 					n->objname = list_make1($4);
5766 					n->objargs = NIL;
5767 					n->comment = $6;
5768 					$$ = (Node *) n;
5769 				}
5770 			| COMMENT ON AGGREGATE func_name aggr_args IS comment_text
5771 				{
5772 					CommentStmt *n = makeNode(CommentStmt);
5773 					n->objtype = OBJECT_AGGREGATE;
5774 					n->objname = $4;
5775 					n->objargs = extractAggrArgTypes($5);
5776 					n->comment = $7;
5777 					$$ = (Node *) n;
5778 				}
5779 			| COMMENT ON FUNCTION func_name func_args IS comment_text
5780 				{
5781 					CommentStmt *n = makeNode(CommentStmt);
5782 					n->objtype = OBJECT_FUNCTION;
5783 					n->objname = $4;
5784 					n->objargs = extractArgTypes($5);
5785 					n->comment = $7;
5786 					$$ = (Node *) n;
5787 				}
5788 			| COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
5789 				{
5790 					CommentStmt *n = makeNode(CommentStmt);
5791 					n->objtype = OBJECT_OPERATOR;
5792 					n->objname = $4;
5793 					n->objargs = $5;
5794 					n->comment = $7;
5795 					$$ = (Node *) n;
5796 				}
5797 			| COMMENT ON CONSTRAINT name ON any_name IS comment_text
5798 				{
5799 					CommentStmt *n = makeNode(CommentStmt);
5800 					n->objtype = OBJECT_TABCONSTRAINT;
5801 					n->objname = lappend($6, makeString($4));
5802 					n->objargs = NIL;
5803 					n->comment = $8;
5804 					$$ = (Node *) n;
5805 				}
5806 			| COMMENT ON CONSTRAINT name ON DOMAIN_P any_name IS comment_text
5807 				{
5808 					CommentStmt *n = makeNode(CommentStmt);
5809 					n->objtype = OBJECT_DOMCONSTRAINT;
5810 					/*
5811 					 * should use Typename not any_name in the production, but
5812 					 * there's a shift/reduce conflict if we do that, so fix it
5813 					 * up here.
5814 					 */
5815 					n->objname = list_make1(makeTypeNameFromNameList($7));
5816 					n->objargs = list_make1(makeString($4));
5817 					n->comment = $9;
5818 					$$ = (Node *) n;
5819 				}
5820 			| COMMENT ON POLICY name ON any_name IS comment_text
5821 				{
5822 					CommentStmt *n = makeNode(CommentStmt);
5823 					n->objtype = OBJECT_POLICY;
5824 					n->objname = lappend($6, makeString($4));
5825 					n->objargs = NIL;
5826 					n->comment = $8;
5827 					$$ = (Node *) n;
5828 				}
5829 			| COMMENT ON RULE name ON any_name IS comment_text
5830 				{
5831 					CommentStmt *n = makeNode(CommentStmt);
5832 					n->objtype = OBJECT_RULE;
5833 					n->objname = lappend($6, makeString($4));
5834 					n->objargs = NIL;
5835 					n->comment = $8;
5836 					$$ = (Node *) n;
5837 				}
5838 			| COMMENT ON RULE name IS comment_text
5839 				{
5840 					/* Obsolete syntax supported for awhile for compatibility */
5841 					CommentStmt *n = makeNode(CommentStmt);
5842 					n->objtype = OBJECT_RULE;
5843 					n->objname = list_make1(makeString($4));
5844 					n->objargs = NIL;
5845 					n->comment = $6;
5846 					$$ = (Node *) n;
5847 				}
5848 			| COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
5849 				{
5850 					CommentStmt *n = makeNode(CommentStmt);
5851 					n->objtype = OBJECT_TRANSFORM;
5852 					n->objname = list_make1($5);
5853 					n->objargs = list_make1(makeString($7));
5854 					n->comment = $9;
5855 					$$ = (Node *) n;
5856 				}
5857 			| COMMENT ON TRIGGER name ON any_name IS comment_text
5858 				{
5859 					CommentStmt *n = makeNode(CommentStmt);
5860 					n->objtype = OBJECT_TRIGGER;
5861 					n->objname = lappend($6, makeString($4));
5862 					n->objargs = NIL;
5863 					n->comment = $8;
5864 					$$ = (Node *) n;
5865 				}
5866 			| COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
5867 				{
5868 					CommentStmt *n = makeNode(CommentStmt);
5869 					n->objtype = OBJECT_OPCLASS;
5870 					n->objname = lcons(makeString($7), $5);
5871 					n->comment = $9;
5872 					$$ = (Node *) n;
5873 				}
5874 			| COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
5875 				{
5876 					CommentStmt *n = makeNode(CommentStmt);
5877 					n->objtype = OBJECT_OPFAMILY;
5878 					n->objname = lcons(makeString($7), $5);
5879 					n->objargs = NIL;
5880 					n->comment = $9;
5881 					$$ = (Node *) n;
5882 				}
5883 			| COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
5884 				{
5885 					CommentStmt *n = makeNode(CommentStmt);
5886 					n->objtype = OBJECT_LARGEOBJECT;
5887 					n->objname = list_make1($5);
5888 					n->objargs = NIL;
5889 					n->comment = $7;
5890 					$$ = (Node *) n;
5891 				}
5892 			| COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
5893 				{
5894 					CommentStmt *n = makeNode(CommentStmt);
5895 					n->objtype = OBJECT_CAST;
5896 					n->objname = list_make1($5);
5897 					n->objargs = list_make1($7);
5898 					n->comment = $10;
5899 					$$ = (Node *) n;
5900 				}
5901 			| COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
5902 				{
5903 					CommentStmt *n = makeNode(CommentStmt);
5904 					n->objtype = OBJECT_LANGUAGE;
5905 					n->objname = $5;
5906 					n->objargs = NIL;
5907 					n->comment = $7;
5908 					$$ = (Node *) n;
5909 				}
5910 		;
5911 
5912 comment_type:
5913 			ACCESS METHOD						{ $$ = OBJECT_ACCESS_METHOD; }
5914 			| COLUMN							{ $$ = OBJECT_COLUMN; }
5915 			| DATABASE							{ $$ = OBJECT_DATABASE; }
5916 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
5917 			| INDEX								{ $$ = OBJECT_INDEX; }
5918 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
5919 			| TABLE								{ $$ = OBJECT_TABLE; }
5920 			| VIEW								{ $$ = OBJECT_VIEW; }
5921 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
5922 			| COLLATION							{ $$ = OBJECT_COLLATION; }
5923 			| CONVERSION_P						{ $$ = OBJECT_CONVERSION; }
5924 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
5925 			| EXTENSION							{ $$ = OBJECT_EXTENSION; }
5926 			| ROLE								{ $$ = OBJECT_ROLE; }
5927 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
5928 			| SERVER							{ $$ = OBJECT_FOREIGN_SERVER; }
5929 			| FOREIGN DATA_P WRAPPER			{ $$ = OBJECT_FDW; }
5930 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
5931 			| TEXT_P SEARCH CONFIGURATION		{ $$ = OBJECT_TSCONFIGURATION; }
5932 			| TEXT_P SEARCH DICTIONARY			{ $$ = OBJECT_TSDICTIONARY; }
5933 			| TEXT_P SEARCH PARSER				{ $$ = OBJECT_TSPARSER; }
5934 			| TEXT_P SEARCH TEMPLATE			{ $$ = OBJECT_TSTEMPLATE; }
5935 		;
5936 
5937 comment_text:
5938 			Sconst								{ $$ = $1; }
5939 			| NULL_P							{ $$ = NULL; }
5940 		;
5941 
5942 
5943 /*****************************************************************************
5944  *
5945  *  SECURITY LABEL [FOR <provider>] ON <object> IS <label>
5946  *
5947  *  As with COMMENT ON, <object> can refer to various types of database
5948  *  objects (e.g. TABLE, COLUMN, etc.).
5949  *
5950  *****************************************************************************/
5951 
5952 SecLabelStmt:
5953 			SECURITY LABEL opt_provider ON security_label_type any_name
5954 			IS security_label
5955 				{
5956 					SecLabelStmt *n = makeNode(SecLabelStmt);
5957 					n->provider = $3;
5958 					n->objtype = $5;
5959 					n->objname = $6;
5960 					n->objargs = NIL;
5961 					n->label = $8;
5962 					$$ = (Node *) n;
5963 				}
5964 			| SECURITY LABEL opt_provider ON TYPE_P Typename
5965 			  IS security_label
5966 				{
5967 					SecLabelStmt *n = makeNode(SecLabelStmt);
5968 					n->provider = $3;
5969 					n->objtype = OBJECT_TYPE;
5970 					n->objname = list_make1($6);
5971 					n->objargs = NIL;
5972 					n->label = $8;
5973 					$$ = (Node *) n;
5974 				}
5975 			| SECURITY LABEL opt_provider ON DOMAIN_P Typename
5976 			  IS security_label
5977 				{
5978 					SecLabelStmt *n = makeNode(SecLabelStmt);
5979 					n->provider = $3;
5980 					n->objtype = OBJECT_TYPE;
5981 					n->objname = list_make1($6);
5982 					n->objargs = NIL;
5983 					n->label = $8;
5984 					$$ = (Node *) n;
5985 				}
5986 			| SECURITY LABEL opt_provider ON AGGREGATE func_name aggr_args
5987 			  IS security_label
5988 				{
5989 					SecLabelStmt *n = makeNode(SecLabelStmt);
5990 					n->provider = $3;
5991 					n->objtype = OBJECT_AGGREGATE;
5992 					n->objname = $6;
5993 					n->objargs = extractAggrArgTypes($7);
5994 					n->label = $9;
5995 					$$ = (Node *) n;
5996 				}
5997 			| SECURITY LABEL opt_provider ON FUNCTION func_name func_args
5998 			  IS security_label
5999 				{
6000 					SecLabelStmt *n = makeNode(SecLabelStmt);
6001 					n->provider = $3;
6002 					n->objtype = OBJECT_FUNCTION;
6003 					n->objname = $6;
6004 					n->objargs = extractArgTypes($7);
6005 					n->label = $9;
6006 					$$ = (Node *) n;
6007 				}
6008 			| SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
6009 			  IS security_label
6010 				{
6011 					SecLabelStmt *n = makeNode(SecLabelStmt);
6012 					n->provider = $3;
6013 					n->objtype = OBJECT_LARGEOBJECT;
6014 					n->objname = list_make1($7);
6015 					n->objargs = NIL;
6016 					n->label = $9;
6017 					$$ = (Node *) n;
6018 				}
6019 			| SECURITY LABEL opt_provider ON opt_procedural LANGUAGE any_name
6020 			  IS security_label
6021 				{
6022 					SecLabelStmt *n = makeNode(SecLabelStmt);
6023 					n->provider = $3;
6024 					n->objtype = OBJECT_LANGUAGE;
6025 					n->objname = $7;
6026 					n->objargs = NIL;
6027 					n->label = $9;
6028 					$$ = (Node *) n;
6029 				}
6030 		;
6031 
6032 opt_provider:	FOR NonReservedWord_or_Sconst	{ $$ = $2; }
6033 				| /* empty */					{ $$ = NULL; }
6034 		;
6035 
6036 security_label_type:
6037 			COLUMN								{ $$ = OBJECT_COLUMN; }
6038 			| DATABASE							{ $$ = OBJECT_DATABASE; }
6039 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6040 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6041 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6042 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6043 			| TABLE								{ $$ = OBJECT_TABLE; }
6044 			| ROLE								{ $$ = OBJECT_ROLE; }
6045 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6046 			| VIEW								{ $$ = OBJECT_VIEW; }
6047 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6048 		;
6049 
6050 security_label:	Sconst				{ $$ = $1; }
6051 				| NULL_P			{ $$ = NULL; }
6052 		;
6053 
6054 /*****************************************************************************
6055  *
6056  *		QUERY:
6057  *			fetch/move
6058  *
6059  *****************************************************************************/
6060 
6061 FetchStmt:	FETCH fetch_args
6062 				{
6063 					FetchStmt *n = (FetchStmt *) $2;
6064 					n->ismove = FALSE;
6065 					$$ = (Node *)n;
6066 				}
6067 			| MOVE fetch_args
6068 				{
6069 					FetchStmt *n = (FetchStmt *) $2;
6070 					n->ismove = TRUE;
6071 					$$ = (Node *)n;
6072 				}
6073 		;
6074 
6075 fetch_args:	cursor_name
6076 				{
6077 					FetchStmt *n = makeNode(FetchStmt);
6078 					n->portalname = $1;
6079 					n->direction = FETCH_FORWARD;
6080 					n->howMany = 1;
6081 					$$ = (Node *)n;
6082 				}
6083 			| from_in cursor_name
6084 				{
6085 					FetchStmt *n = makeNode(FetchStmt);
6086 					n->portalname = $2;
6087 					n->direction = FETCH_FORWARD;
6088 					n->howMany = 1;
6089 					$$ = (Node *)n;
6090 				}
6091 			| NEXT opt_from_in cursor_name
6092 				{
6093 					FetchStmt *n = makeNode(FetchStmt);
6094 					n->portalname = $3;
6095 					n->direction = FETCH_FORWARD;
6096 					n->howMany = 1;
6097 					$$ = (Node *)n;
6098 				}
6099 			| PRIOR opt_from_in cursor_name
6100 				{
6101 					FetchStmt *n = makeNode(FetchStmt);
6102 					n->portalname = $3;
6103 					n->direction = FETCH_BACKWARD;
6104 					n->howMany = 1;
6105 					$$ = (Node *)n;
6106 				}
6107 			| FIRST_P opt_from_in cursor_name
6108 				{
6109 					FetchStmt *n = makeNode(FetchStmt);
6110 					n->portalname = $3;
6111 					n->direction = FETCH_ABSOLUTE;
6112 					n->howMany = 1;
6113 					$$ = (Node *)n;
6114 				}
6115 			| LAST_P opt_from_in cursor_name
6116 				{
6117 					FetchStmt *n = makeNode(FetchStmt);
6118 					n->portalname = $3;
6119 					n->direction = FETCH_ABSOLUTE;
6120 					n->howMany = -1;
6121 					$$ = (Node *)n;
6122 				}
6123 			| ABSOLUTE_P SignedIconst opt_from_in cursor_name
6124 				{
6125 					FetchStmt *n = makeNode(FetchStmt);
6126 					n->portalname = $4;
6127 					n->direction = FETCH_ABSOLUTE;
6128 					n->howMany = $2;
6129 					$$ = (Node *)n;
6130 				}
6131 			| RELATIVE_P SignedIconst opt_from_in cursor_name
6132 				{
6133 					FetchStmt *n = makeNode(FetchStmt);
6134 					n->portalname = $4;
6135 					n->direction = FETCH_RELATIVE;
6136 					n->howMany = $2;
6137 					$$ = (Node *)n;
6138 				}
6139 			| SignedIconst opt_from_in cursor_name
6140 				{
6141 					FetchStmt *n = makeNode(FetchStmt);
6142 					n->portalname = $3;
6143 					n->direction = FETCH_FORWARD;
6144 					n->howMany = $1;
6145 					$$ = (Node *)n;
6146 				}
6147 			| ALL opt_from_in cursor_name
6148 				{
6149 					FetchStmt *n = makeNode(FetchStmt);
6150 					n->portalname = $3;
6151 					n->direction = FETCH_FORWARD;
6152 					n->howMany = FETCH_ALL;
6153 					$$ = (Node *)n;
6154 				}
6155 			| FORWARD opt_from_in cursor_name
6156 				{
6157 					FetchStmt *n = makeNode(FetchStmt);
6158 					n->portalname = $3;
6159 					n->direction = FETCH_FORWARD;
6160 					n->howMany = 1;
6161 					$$ = (Node *)n;
6162 				}
6163 			| FORWARD SignedIconst opt_from_in cursor_name
6164 				{
6165 					FetchStmt *n = makeNode(FetchStmt);
6166 					n->portalname = $4;
6167 					n->direction = FETCH_FORWARD;
6168 					n->howMany = $2;
6169 					$$ = (Node *)n;
6170 				}
6171 			| FORWARD ALL opt_from_in cursor_name
6172 				{
6173 					FetchStmt *n = makeNode(FetchStmt);
6174 					n->portalname = $4;
6175 					n->direction = FETCH_FORWARD;
6176 					n->howMany = FETCH_ALL;
6177 					$$ = (Node *)n;
6178 				}
6179 			| BACKWARD opt_from_in cursor_name
6180 				{
6181 					FetchStmt *n = makeNode(FetchStmt);
6182 					n->portalname = $3;
6183 					n->direction = FETCH_BACKWARD;
6184 					n->howMany = 1;
6185 					$$ = (Node *)n;
6186 				}
6187 			| BACKWARD SignedIconst opt_from_in cursor_name
6188 				{
6189 					FetchStmt *n = makeNode(FetchStmt);
6190 					n->portalname = $4;
6191 					n->direction = FETCH_BACKWARD;
6192 					n->howMany = $2;
6193 					$$ = (Node *)n;
6194 				}
6195 			| BACKWARD ALL opt_from_in cursor_name
6196 				{
6197 					FetchStmt *n = makeNode(FetchStmt);
6198 					n->portalname = $4;
6199 					n->direction = FETCH_BACKWARD;
6200 					n->howMany = FETCH_ALL;
6201 					$$ = (Node *)n;
6202 				}
6203 		;
6204 
6205 from_in:	FROM									{}
6206 			| IN_P									{}
6207 		;
6208 
6209 opt_from_in:	from_in								{}
6210 			| /* EMPTY */							{}
6211 		;
6212 
6213 
6214 /*****************************************************************************
6215  *
6216  * GRANT and REVOKE statements
6217  *
6218  *****************************************************************************/
6219 
6220 GrantStmt:	GRANT privileges ON privilege_target TO grantee_list
6221 			opt_grant_grant_option
6222 				{
6223 					GrantStmt *n = makeNode(GrantStmt);
6224 					n->is_grant = true;
6225 					n->privileges = $2;
6226 					n->targtype = ($4)->targtype;
6227 					n->objtype = ($4)->objtype;
6228 					n->objects = ($4)->objs;
6229 					n->grantees = $6;
6230 					n->grant_option = $7;
6231 					$$ = (Node*)n;
6232 				}
6233 		;
6234 
6235 RevokeStmt:
6236 			REVOKE privileges ON privilege_target
6237 			FROM grantee_list opt_drop_behavior
6238 				{
6239 					GrantStmt *n = makeNode(GrantStmt);
6240 					n->is_grant = false;
6241 					n->grant_option = false;
6242 					n->privileges = $2;
6243 					n->targtype = ($4)->targtype;
6244 					n->objtype = ($4)->objtype;
6245 					n->objects = ($4)->objs;
6246 					n->grantees = $6;
6247 					n->behavior = $7;
6248 					$$ = (Node *)n;
6249 				}
6250 			| REVOKE GRANT OPTION FOR privileges ON privilege_target
6251 			FROM grantee_list opt_drop_behavior
6252 				{
6253 					GrantStmt *n = makeNode(GrantStmt);
6254 					n->is_grant = false;
6255 					n->grant_option = true;
6256 					n->privileges = $5;
6257 					n->targtype = ($7)->targtype;
6258 					n->objtype = ($7)->objtype;
6259 					n->objects = ($7)->objs;
6260 					n->grantees = $9;
6261 					n->behavior = $10;
6262 					$$ = (Node *)n;
6263 				}
6264 		;
6265 
6266 
6267 /*
6268  * Privilege names are represented as strings; the validity of the privilege
6269  * names gets checked at execution.  This is a bit annoying but we have little
6270  * choice because of the syntactic conflict with lists of role names in
6271  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
6272  * production any reserved keywords that need to be usable as privilege names.
6273  */
6274 
6275 /* either ALL [PRIVILEGES] or a list of individual privileges */
6276 privileges: privilege_list
6277 				{ $$ = $1; }
6278 			| ALL
6279 				{ $$ = NIL; }
6280 			| ALL PRIVILEGES
6281 				{ $$ = NIL; }
6282 			| ALL '(' columnList ')'
6283 				{
6284 					AccessPriv *n = makeNode(AccessPriv);
6285 					n->priv_name = NULL;
6286 					n->cols = $3;
6287 					$$ = list_make1(n);
6288 				}
6289 			| ALL PRIVILEGES '(' columnList ')'
6290 				{
6291 					AccessPriv *n = makeNode(AccessPriv);
6292 					n->priv_name = NULL;
6293 					n->cols = $4;
6294 					$$ = list_make1(n);
6295 				}
6296 		;
6297 
6298 privilege_list:	privilege							{ $$ = list_make1($1); }
6299 			| privilege_list ',' privilege			{ $$ = lappend($1, $3); }
6300 		;
6301 
6302 privilege:	SELECT opt_column_list
6303 			{
6304 				AccessPriv *n = makeNode(AccessPriv);
6305 				n->priv_name = pstrdup($1);
6306 				n->cols = $2;
6307 				$$ = n;
6308 			}
6309 		| REFERENCES opt_column_list
6310 			{
6311 				AccessPriv *n = makeNode(AccessPriv);
6312 				n->priv_name = pstrdup($1);
6313 				n->cols = $2;
6314 				$$ = n;
6315 			}
6316 		| CREATE opt_column_list
6317 			{
6318 				AccessPriv *n = makeNode(AccessPriv);
6319 				n->priv_name = pstrdup($1);
6320 				n->cols = $2;
6321 				$$ = n;
6322 			}
6323 		| ColId opt_column_list
6324 			{
6325 				AccessPriv *n = makeNode(AccessPriv);
6326 				n->priv_name = $1;
6327 				n->cols = $2;
6328 				$$ = n;
6329 			}
6330 		;
6331 
6332 
6333 /* Don't bother trying to fold the first two rules into one using
6334  * opt_table.  You're going to get conflicts.
6335  */
6336 privilege_target:
6337 			qualified_name_list
6338 				{
6339 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6340 					n->targtype = ACL_TARGET_OBJECT;
6341 					n->objtype = ACL_OBJECT_RELATION;
6342 					n->objs = $1;
6343 					$$ = n;
6344 				}
6345 			| TABLE qualified_name_list
6346 				{
6347 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6348 					n->targtype = ACL_TARGET_OBJECT;
6349 					n->objtype = ACL_OBJECT_RELATION;
6350 					n->objs = $2;
6351 					$$ = n;
6352 				}
6353 			| SEQUENCE qualified_name_list
6354 				{
6355 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6356 					n->targtype = ACL_TARGET_OBJECT;
6357 					n->objtype = ACL_OBJECT_SEQUENCE;
6358 					n->objs = $2;
6359 					$$ = n;
6360 				}
6361 			| FOREIGN DATA_P WRAPPER name_list
6362 				{
6363 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6364 					n->targtype = ACL_TARGET_OBJECT;
6365 					n->objtype = ACL_OBJECT_FDW;
6366 					n->objs = $4;
6367 					$$ = n;
6368 				}
6369 			| FOREIGN SERVER name_list
6370 				{
6371 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6372 					n->targtype = ACL_TARGET_OBJECT;
6373 					n->objtype = ACL_OBJECT_FOREIGN_SERVER;
6374 					n->objs = $3;
6375 					$$ = n;
6376 				}
6377 			| FUNCTION function_with_argtypes_list
6378 				{
6379 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6380 					n->targtype = ACL_TARGET_OBJECT;
6381 					n->objtype = ACL_OBJECT_FUNCTION;
6382 					n->objs = $2;
6383 					$$ = n;
6384 				}
6385 			| DATABASE name_list
6386 				{
6387 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6388 					n->targtype = ACL_TARGET_OBJECT;
6389 					n->objtype = ACL_OBJECT_DATABASE;
6390 					n->objs = $2;
6391 					$$ = n;
6392 				}
6393 			| DOMAIN_P any_name_list
6394 				{
6395 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6396 					n->targtype = ACL_TARGET_OBJECT;
6397 					n->objtype = ACL_OBJECT_DOMAIN;
6398 					n->objs = $2;
6399 					$$ = n;
6400 				}
6401 			| LANGUAGE name_list
6402 				{
6403 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6404 					n->targtype = ACL_TARGET_OBJECT;
6405 					n->objtype = ACL_OBJECT_LANGUAGE;
6406 					n->objs = $2;
6407 					$$ = n;
6408 				}
6409 			| LARGE_P OBJECT_P NumericOnly_list
6410 				{
6411 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6412 					n->targtype = ACL_TARGET_OBJECT;
6413 					n->objtype = ACL_OBJECT_LARGEOBJECT;
6414 					n->objs = $3;
6415 					$$ = n;
6416 				}
6417 			| SCHEMA name_list
6418 				{
6419 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6420 					n->targtype = ACL_TARGET_OBJECT;
6421 					n->objtype = ACL_OBJECT_NAMESPACE;
6422 					n->objs = $2;
6423 					$$ = n;
6424 				}
6425 			| TABLESPACE name_list
6426 				{
6427 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6428 					n->targtype = ACL_TARGET_OBJECT;
6429 					n->objtype = ACL_OBJECT_TABLESPACE;
6430 					n->objs = $2;
6431 					$$ = n;
6432 				}
6433 			| TYPE_P any_name_list
6434 				{
6435 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6436 					n->targtype = ACL_TARGET_OBJECT;
6437 					n->objtype = ACL_OBJECT_TYPE;
6438 					n->objs = $2;
6439 					$$ = n;
6440 				}
6441 			| ALL TABLES IN_P SCHEMA name_list
6442 				{
6443 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6444 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
6445 					n->objtype = ACL_OBJECT_RELATION;
6446 					n->objs = $5;
6447 					$$ = n;
6448 				}
6449 			| ALL SEQUENCES IN_P SCHEMA name_list
6450 				{
6451 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6452 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
6453 					n->objtype = ACL_OBJECT_SEQUENCE;
6454 					n->objs = $5;
6455 					$$ = n;
6456 				}
6457 			| ALL FUNCTIONS IN_P SCHEMA name_list
6458 				{
6459 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6460 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
6461 					n->objtype = ACL_OBJECT_FUNCTION;
6462 					n->objs = $5;
6463 					$$ = n;
6464 				}
6465 		;
6466 
6467 
6468 grantee_list:
6469 			grantee									{ $$ = list_make1($1); }
6470 			| grantee_list ',' grantee				{ $$ = lappend($1, $3); }
6471 		;
6472 
6473 grantee:
6474 			RoleSpec								{ $$ = $1; }
6475 			| GROUP_P RoleSpec						{ $$ = $2; }
6476 		;
6477 
6478 
6479 opt_grant_grant_option:
6480 			WITH GRANT OPTION { $$ = TRUE; }
6481 			| /*EMPTY*/ { $$ = FALSE; }
6482 		;
6483 
6484 function_with_argtypes_list:
6485 			function_with_argtypes					{ $$ = list_make1($1); }
6486 			| function_with_argtypes_list ',' function_with_argtypes
6487 													{ $$ = lappend($1, $3); }
6488 		;
6489 
6490 function_with_argtypes:
6491 			func_name func_args
6492 				{
6493 					FuncWithArgs *n = makeNode(FuncWithArgs);
6494 					n->funcname = $1;
6495 					n->funcargs = extractArgTypes($2);
6496 					$$ = n;
6497 				}
6498 		;
6499 
6500 /*****************************************************************************
6501  *
6502  * GRANT and REVOKE ROLE statements
6503  *
6504  *****************************************************************************/
6505 
6506 GrantRoleStmt:
6507 			GRANT privilege_list TO role_list opt_grant_admin_option opt_granted_by
6508 				{
6509 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
6510 					n->is_grant = true;
6511 					n->granted_roles = $2;
6512 					n->grantee_roles = $4;
6513 					n->admin_opt = $5;
6514 					n->grantor = $6;
6515 					$$ = (Node*)n;
6516 				}
6517 		;
6518 
6519 RevokeRoleStmt:
6520 			REVOKE privilege_list FROM role_list opt_granted_by opt_drop_behavior
6521 				{
6522 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
6523 					n->is_grant = false;
6524 					n->admin_opt = false;
6525 					n->granted_roles = $2;
6526 					n->grantee_roles = $4;
6527 					n->behavior = $6;
6528 					$$ = (Node*)n;
6529 				}
6530 			| REVOKE ADMIN OPTION FOR privilege_list FROM role_list opt_granted_by opt_drop_behavior
6531 				{
6532 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
6533 					n->is_grant = false;
6534 					n->admin_opt = true;
6535 					n->granted_roles = $5;
6536 					n->grantee_roles = $7;
6537 					n->behavior = $9;
6538 					$$ = (Node*)n;
6539 				}
6540 		;
6541 
6542 opt_grant_admin_option: WITH ADMIN OPTION				{ $$ = TRUE; }
6543 			| /*EMPTY*/									{ $$ = FALSE; }
6544 		;
6545 
6546 opt_granted_by: GRANTED BY RoleSpec						{ $$ = $3; }
6547 			| /*EMPTY*/									{ $$ = NULL; }
6548 		;
6549 
6550 /*****************************************************************************
6551  *
6552  * ALTER DEFAULT PRIVILEGES statement
6553  *
6554  *****************************************************************************/
6555 
6556 AlterDefaultPrivilegesStmt:
6557 			ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
6558 				{
6559 					AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
6560 					n->options = $4;
6561 					n->action = (GrantStmt *) $5;
6562 					$$ = (Node*)n;
6563 				}
6564 		;
6565 
6566 DefACLOptionList:
6567 			DefACLOptionList DefACLOption			{ $$ = lappend($1, $2); }
6568 			| /* EMPTY */							{ $$ = NIL; }
6569 		;
6570 
6571 DefACLOption:
6572 			IN_P SCHEMA name_list
6573 				{
6574 					$$ = makeDefElem("schemas", (Node *)$3);
6575 				}
6576 			| FOR ROLE role_list
6577 				{
6578 					$$ = makeDefElem("roles", (Node *)$3);
6579 				}
6580 			| FOR USER role_list
6581 				{
6582 					$$ = makeDefElem("roles", (Node *)$3);
6583 				}
6584 		;
6585 
6586 /*
6587  * This should match GRANT/REVOKE, except that individual target objects
6588  * are not mentioned and we only allow a subset of object types.
6589  */
6590 DefACLAction:
6591 			GRANT privileges ON defacl_privilege_target TO grantee_list
6592 			opt_grant_grant_option
6593 				{
6594 					GrantStmt *n = makeNode(GrantStmt);
6595 					n->is_grant = true;
6596 					n->privileges = $2;
6597 					n->targtype = ACL_TARGET_DEFAULTS;
6598 					n->objtype = $4;
6599 					n->objects = NIL;
6600 					n->grantees = $6;
6601 					n->grant_option = $7;
6602 					$$ = (Node*)n;
6603 				}
6604 			| REVOKE privileges ON defacl_privilege_target
6605 			FROM grantee_list opt_drop_behavior
6606 				{
6607 					GrantStmt *n = makeNode(GrantStmt);
6608 					n->is_grant = false;
6609 					n->grant_option = false;
6610 					n->privileges = $2;
6611 					n->targtype = ACL_TARGET_DEFAULTS;
6612 					n->objtype = $4;
6613 					n->objects = NIL;
6614 					n->grantees = $6;
6615 					n->behavior = $7;
6616 					$$ = (Node *)n;
6617 				}
6618 			| REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
6619 			FROM grantee_list opt_drop_behavior
6620 				{
6621 					GrantStmt *n = makeNode(GrantStmt);
6622 					n->is_grant = false;
6623 					n->grant_option = true;
6624 					n->privileges = $5;
6625 					n->targtype = ACL_TARGET_DEFAULTS;
6626 					n->objtype = $7;
6627 					n->objects = NIL;
6628 					n->grantees = $9;
6629 					n->behavior = $10;
6630 					$$ = (Node *)n;
6631 				}
6632 		;
6633 
6634 defacl_privilege_target:
6635 			TABLES			{ $$ = ACL_OBJECT_RELATION; }
6636 			| FUNCTIONS		{ $$ = ACL_OBJECT_FUNCTION; }
6637 			| SEQUENCES		{ $$ = ACL_OBJECT_SEQUENCE; }
6638 			| TYPES_P		{ $$ = ACL_OBJECT_TYPE; }
6639 		;
6640 
6641 
6642 /*****************************************************************************
6643  *
6644  *		QUERY: CREATE INDEX
6645  *
6646  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
6647  * willing to make TABLESPACE a fully reserved word.
6648  *****************************************************************************/
6649 
6650 IndexStmt:	CREATE opt_unique INDEX opt_concurrently opt_index_name
6651 			ON qualified_name access_method_clause '(' index_params ')'
6652 			opt_reloptions OptTableSpace where_clause
6653 				{
6654 					IndexStmt *n = makeNode(IndexStmt);
6655 					n->unique = $2;
6656 					n->concurrent = $4;
6657 					n->idxname = $5;
6658 					n->relation = $7;
6659 					n->accessMethod = $8;
6660 					n->indexParams = $10;
6661 					n->options = $12;
6662 					n->tableSpace = $13;
6663 					n->whereClause = $14;
6664 					n->excludeOpNames = NIL;
6665 					n->idxcomment = NULL;
6666 					n->indexOid = InvalidOid;
6667 					n->oldNode = InvalidOid;
6668 					n->primary = false;
6669 					n->isconstraint = false;
6670 					n->deferrable = false;
6671 					n->initdeferred = false;
6672 					n->transformed = false;
6673 					n->if_not_exists = false;
6674 					$$ = (Node *)n;
6675 				}
6676 			| CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS index_name
6677 			ON qualified_name access_method_clause '(' index_params ')'
6678 			opt_reloptions OptTableSpace where_clause
6679 				{
6680 					IndexStmt *n = makeNode(IndexStmt);
6681 					n->unique = $2;
6682 					n->concurrent = $4;
6683 					n->idxname = $8;
6684 					n->relation = $10;
6685 					n->accessMethod = $11;
6686 					n->indexParams = $13;
6687 					n->options = $15;
6688 					n->tableSpace = $16;
6689 					n->whereClause = $17;
6690 					n->excludeOpNames = NIL;
6691 					n->idxcomment = NULL;
6692 					n->indexOid = InvalidOid;
6693 					n->oldNode = InvalidOid;
6694 					n->primary = false;
6695 					n->isconstraint = false;
6696 					n->deferrable = false;
6697 					n->initdeferred = false;
6698 					n->transformed = false;
6699 					n->if_not_exists = true;
6700 					$$ = (Node *)n;
6701 				}
6702 		;
6703 
6704 opt_unique:
6705 			UNIQUE									{ $$ = TRUE; }
6706 			| /*EMPTY*/								{ $$ = FALSE; }
6707 		;
6708 
6709 opt_concurrently:
6710 			CONCURRENTLY							{ $$ = TRUE; }
6711 			| /*EMPTY*/								{ $$ = FALSE; }
6712 		;
6713 
6714 opt_index_name:
6715 			index_name								{ $$ = $1; }
6716 			| /*EMPTY*/								{ $$ = NULL; }
6717 		;
6718 
6719 access_method_clause:
6720 			USING access_method						{ $$ = $2; }
6721 			| /*EMPTY*/								{ $$ = DEFAULT_INDEX_TYPE; }
6722 		;
6723 
6724 index_params:	index_elem							{ $$ = list_make1($1); }
6725 			| index_params ',' index_elem			{ $$ = lappend($1, $3); }
6726 		;
6727 
6728 /*
6729  * Index attributes can be either simple column references, or arbitrary
6730  * expressions in parens.  For backwards-compatibility reasons, we allow
6731  * an expression that's just a function call to be written without parens.
6732  */
6733 index_elem:	ColId opt_collate opt_class opt_asc_desc opt_nulls_order
6734 				{
6735 					$$ = makeNode(IndexElem);
6736 					$$->name = $1;
6737 					$$->expr = NULL;
6738 					$$->indexcolname = NULL;
6739 					$$->collation = $2;
6740 					$$->opclass = $3;
6741 					$$->ordering = $4;
6742 					$$->nulls_ordering = $5;
6743 				}
6744 			| func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order
6745 				{
6746 					$$ = makeNode(IndexElem);
6747 					$$->name = NULL;
6748 					$$->expr = $1;
6749 					$$->indexcolname = NULL;
6750 					$$->collation = $2;
6751 					$$->opclass = $3;
6752 					$$->ordering = $4;
6753 					$$->nulls_ordering = $5;
6754 				}
6755 			| '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order
6756 				{
6757 					$$ = makeNode(IndexElem);
6758 					$$->name = NULL;
6759 					$$->expr = $2;
6760 					$$->indexcolname = NULL;
6761 					$$->collation = $4;
6762 					$$->opclass = $5;
6763 					$$->ordering = $6;
6764 					$$->nulls_ordering = $7;
6765 				}
6766 		;
6767 
6768 opt_collate: COLLATE any_name						{ $$ = $2; }
6769 			| /*EMPTY*/								{ $$ = NIL; }
6770 		;
6771 
6772 opt_class:	any_name								{ $$ = $1; }
6773 			| /*EMPTY*/								{ $$ = NIL; }
6774 		;
6775 
6776 opt_asc_desc: ASC							{ $$ = SORTBY_ASC; }
6777 			| DESC							{ $$ = SORTBY_DESC; }
6778 			| /*EMPTY*/						{ $$ = SORTBY_DEFAULT; }
6779 		;
6780 
6781 opt_nulls_order: NULLS_LA FIRST_P			{ $$ = SORTBY_NULLS_FIRST; }
6782 			| NULLS_LA LAST_P				{ $$ = SORTBY_NULLS_LAST; }
6783 			| /*EMPTY*/						{ $$ = SORTBY_NULLS_DEFAULT; }
6784 		;
6785 
6786 
6787 /*****************************************************************************
6788  *
6789  *		QUERY:
6790  *				create [or replace] function <fname>
6791  *						[(<type-1> { , <type-n>})]
6792  *						returns <type-r>
6793  *						as <filename or code in language as appropriate>
6794  *						language <lang> [with parameters]
6795  *
6796  *****************************************************************************/
6797 
6798 CreateFunctionStmt:
6799 			CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
6800 			RETURNS func_return createfunc_opt_list opt_definition
6801 				{
6802 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
6803 					n->replace = $2;
6804 					n->funcname = $4;
6805 					n->parameters = $5;
6806 					n->returnType = $7;
6807 					n->options = $8;
6808 					n->withClause = $9;
6809 					$$ = (Node *)n;
6810 				}
6811 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
6812 			  RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
6813 				{
6814 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
6815 					n->replace = $2;
6816 					n->funcname = $4;
6817 					n->parameters = mergeTableFuncParameters($5, $9);
6818 					n->returnType = TableFuncTypeName($9);
6819 					n->returnType->location = @7;
6820 					n->options = $11;
6821 					n->withClause = $12;
6822 					$$ = (Node *)n;
6823 				}
6824 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
6825 			  createfunc_opt_list opt_definition
6826 				{
6827 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
6828 					n->replace = $2;
6829 					n->funcname = $4;
6830 					n->parameters = $5;
6831 					n->returnType = NULL;
6832 					n->options = $6;
6833 					n->withClause = $7;
6834 					$$ = (Node *)n;
6835 				}
6836 		;
6837 
6838 opt_or_replace:
6839 			OR REPLACE								{ $$ = TRUE; }
6840 			| /*EMPTY*/								{ $$ = FALSE; }
6841 		;
6842 
6843 func_args:	'(' func_args_list ')'					{ $$ = $2; }
6844 			| '(' ')'								{ $$ = NIL; }
6845 		;
6846 
6847 func_args_list:
6848 			func_arg								{ $$ = list_make1($1); }
6849 			| func_args_list ',' func_arg			{ $$ = lappend($1, $3); }
6850 		;
6851 
6852 /*
6853  * func_args_with_defaults is separate because we only want to accept
6854  * defaults in CREATE FUNCTION, not in ALTER etc.
6855  */
6856 func_args_with_defaults:
6857 		'(' func_args_with_defaults_list ')'		{ $$ = $2; }
6858 		| '(' ')'									{ $$ = NIL; }
6859 		;
6860 
6861 func_args_with_defaults_list:
6862 		func_arg_with_default						{ $$ = list_make1($1); }
6863 		| func_args_with_defaults_list ',' func_arg_with_default
6864 													{ $$ = lappend($1, $3); }
6865 		;
6866 
6867 /*
6868  * The style with arg_class first is SQL99 standard, but Oracle puts
6869  * param_name first; accept both since it's likely people will try both
6870  * anyway.  Don't bother trying to save productions by letting arg_class
6871  * have an empty alternative ... you'll get shift/reduce conflicts.
6872  *
6873  * We can catch over-specified arguments here if we want to,
6874  * but for now better to silently swallow typmod, etc.
6875  * - thomas 2000-03-22
6876  */
6877 func_arg:
6878 			arg_class param_name func_type
6879 				{
6880 					FunctionParameter *n = makeNode(FunctionParameter);
6881 					n->name = $2;
6882 					n->argType = $3;
6883 					n->mode = $1;
6884 					n->defexpr = NULL;
6885 					$$ = n;
6886 				}
6887 			| param_name arg_class func_type
6888 				{
6889 					FunctionParameter *n = makeNode(FunctionParameter);
6890 					n->name = $1;
6891 					n->argType = $3;
6892 					n->mode = $2;
6893 					n->defexpr = NULL;
6894 					$$ = n;
6895 				}
6896 			| param_name func_type
6897 				{
6898 					FunctionParameter *n = makeNode(FunctionParameter);
6899 					n->name = $1;
6900 					n->argType = $2;
6901 					n->mode = FUNC_PARAM_IN;
6902 					n->defexpr = NULL;
6903 					$$ = n;
6904 				}
6905 			| arg_class func_type
6906 				{
6907 					FunctionParameter *n = makeNode(FunctionParameter);
6908 					n->name = NULL;
6909 					n->argType = $2;
6910 					n->mode = $1;
6911 					n->defexpr = NULL;
6912 					$$ = n;
6913 				}
6914 			| func_type
6915 				{
6916 					FunctionParameter *n = makeNode(FunctionParameter);
6917 					n->name = NULL;
6918 					n->argType = $1;
6919 					n->mode = FUNC_PARAM_IN;
6920 					n->defexpr = NULL;
6921 					$$ = n;
6922 				}
6923 		;
6924 
6925 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
6926 arg_class:	IN_P								{ $$ = FUNC_PARAM_IN; }
6927 			| OUT_P								{ $$ = FUNC_PARAM_OUT; }
6928 			| INOUT								{ $$ = FUNC_PARAM_INOUT; }
6929 			| IN_P OUT_P						{ $$ = FUNC_PARAM_INOUT; }
6930 			| VARIADIC							{ $$ = FUNC_PARAM_VARIADIC; }
6931 		;
6932 
6933 /*
6934  * Ideally param_name should be ColId, but that causes too many conflicts.
6935  */
6936 param_name:	type_function_name
6937 		;
6938 
6939 func_return:
6940 			func_type
6941 				{
6942 					/* We can catch over-specified results here if we want to,
6943 					 * but for now better to silently swallow typmod, etc.
6944 					 * - thomas 2000-03-22
6945 					 */
6946 					$$ = $1;
6947 				}
6948 		;
6949 
6950 /*
6951  * We would like to make the %TYPE productions here be ColId attrs etc,
6952  * but that causes reduce/reduce conflicts.  type_function_name
6953  * is next best choice.
6954  */
6955 func_type:	Typename								{ $$ = $1; }
6956 			| type_function_name attrs '%' TYPE_P
6957 				{
6958 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
6959 					$$->pct_type = true;
6960 					$$->location = @1;
6961 				}
6962 			| SETOF type_function_name attrs '%' TYPE_P
6963 				{
6964 					$$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
6965 					$$->pct_type = true;
6966 					$$->setof = TRUE;
6967 					$$->location = @2;
6968 				}
6969 		;
6970 
6971 func_arg_with_default:
6972 		func_arg
6973 				{
6974 					$$ = $1;
6975 				}
6976 		| func_arg DEFAULT a_expr
6977 				{
6978 					$$ = $1;
6979 					$$->defexpr = $3;
6980 				}
6981 		| func_arg '=' a_expr
6982 				{
6983 					$$ = $1;
6984 					$$->defexpr = $3;
6985 				}
6986 		;
6987 
6988 /* Aggregate args can be most things that function args can be */
6989 aggr_arg:	func_arg
6990 				{
6991 					if (!($1->mode == FUNC_PARAM_IN ||
6992 						  $1->mode == FUNC_PARAM_VARIADIC))
6993 						ereport(ERROR,
6994 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6995 								 errmsg("aggregates cannot have output arguments"),
6996 								 parser_errposition(@1)));
6997 					$$ = $1;
6998 				}
6999 		;
7000 
7001 /*
7002  * The SQL standard offers no guidance on how to declare aggregate argument
7003  * lists, since it doesn't have CREATE AGGREGATE etc.  We accept these cases:
7004  *
7005  * (*)									- normal agg with no args
7006  * (aggr_arg,...)						- normal agg with args
7007  * (ORDER BY aggr_arg,...)				- ordered-set agg with no direct args
7008  * (aggr_arg,... ORDER BY aggr_arg,...)	- ordered-set agg with direct args
7009  *
7010  * The zero-argument case is spelled with '*' for consistency with COUNT(*).
7011  *
7012  * An additional restriction is that if the direct-args list ends in a
7013  * VARIADIC item, the ordered-args list must contain exactly one item that
7014  * is also VARIADIC with the same type.  This allows us to collapse the two
7015  * VARIADIC items into one, which is necessary to represent the aggregate in
7016  * pg_proc.  We check this at the grammar stage so that we can return a list
7017  * in which the second VARIADIC item is already discarded, avoiding extra work
7018  * in cases such as DROP AGGREGATE.
7019  *
7020  * The return value of this production is a two-element list, in which the
7021  * first item is a sublist of FunctionParameter nodes (with any duplicate
7022  * VARIADIC item already dropped, as per above) and the second is an integer
7023  * Value node, containing -1 if there was no ORDER BY and otherwise the number
7024  * of argument declarations before the ORDER BY.  (If this number is equal
7025  * to the first sublist's length, then we dropped a duplicate VARIADIC item.)
7026  * This representation is passed as-is to CREATE AGGREGATE; for operations
7027  * on existing aggregates, we can just apply extractArgTypes to the first
7028  * sublist.
7029  */
7030 aggr_args:	'(' '*' ')'
7031 				{
7032 					$$ = list_make2(NIL, makeInteger(-1));
7033 				}
7034 			| '(' aggr_args_list ')'
7035 				{
7036 					$$ = list_make2($2, makeInteger(-1));
7037 				}
7038 			| '(' ORDER BY aggr_args_list ')'
7039 				{
7040 					$$ = list_make2($4, makeInteger(0));
7041 				}
7042 			| '(' aggr_args_list ORDER BY aggr_args_list ')'
7043 				{
7044 					/* this is the only case requiring consistency checking */
7045 					$$ = makeOrderedSetArgs($2, $5, yyscanner);
7046 				}
7047 		;
7048 
7049 aggr_args_list:
7050 			aggr_arg								{ $$ = list_make1($1); }
7051 			| aggr_args_list ',' aggr_arg			{ $$ = lappend($1, $3); }
7052 		;
7053 
7054 createfunc_opt_list:
7055 			/* Must be at least one to prevent conflict */
7056 			createfunc_opt_item						{ $$ = list_make1($1); }
7057 			| createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
7058 	;
7059 
7060 /*
7061  * Options common to both CREATE FUNCTION and ALTER FUNCTION
7062  */
7063 common_func_opt_item:
7064 			CALLED ON NULL_P INPUT_P
7065 				{
7066 					$$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
7067 				}
7068 			| RETURNS NULL_P ON NULL_P INPUT_P
7069 				{
7070 					$$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
7071 				}
7072 			| STRICT_P
7073 				{
7074 					$$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
7075 				}
7076 			| IMMUTABLE
7077 				{
7078 					$$ = makeDefElem("volatility", (Node *)makeString("immutable"));
7079 				}
7080 			| STABLE
7081 				{
7082 					$$ = makeDefElem("volatility", (Node *)makeString("stable"));
7083 				}
7084 			| VOLATILE
7085 				{
7086 					$$ = makeDefElem("volatility", (Node *)makeString("volatile"));
7087 				}
7088 			| EXTERNAL SECURITY DEFINER
7089 				{
7090 					$$ = makeDefElem("security", (Node *)makeInteger(TRUE));
7091 				}
7092 			| EXTERNAL SECURITY INVOKER
7093 				{
7094 					$$ = makeDefElem("security", (Node *)makeInteger(FALSE));
7095 				}
7096 			| SECURITY DEFINER
7097 				{
7098 					$$ = makeDefElem("security", (Node *)makeInteger(TRUE));
7099 				}
7100 			| SECURITY INVOKER
7101 				{
7102 					$$ = makeDefElem("security", (Node *)makeInteger(FALSE));
7103 				}
7104 			| LEAKPROOF
7105 				{
7106 					$$ = makeDefElem("leakproof", (Node *)makeInteger(TRUE));
7107 				}
7108 			| NOT LEAKPROOF
7109 				{
7110 					$$ = makeDefElem("leakproof", (Node *)makeInteger(FALSE));
7111 				}
7112 			| COST NumericOnly
7113 				{
7114 					$$ = makeDefElem("cost", (Node *)$2);
7115 				}
7116 			| ROWS NumericOnly
7117 				{
7118 					$$ = makeDefElem("rows", (Node *)$2);
7119 				}
7120 			| FunctionSetResetClause
7121 				{
7122 					/* we abuse the normal content of a DefElem here */
7123 					$$ = makeDefElem("set", (Node *)$1);
7124 				}
7125 			| PARALLEL ColId
7126 				{
7127 					$$ = makeDefElem("parallel", (Node *)makeString($2));
7128 				}
7129 		;
7130 
7131 createfunc_opt_item:
7132 			AS func_as
7133 				{
7134 					$$ = makeDefElem("as", (Node *)$2);
7135 				}
7136 			| LANGUAGE NonReservedWord_or_Sconst
7137 				{
7138 					$$ = makeDefElem("language", (Node *)makeString($2));
7139 				}
7140 			| TRANSFORM transform_type_list
7141 				{
7142 					$$ = makeDefElem("transform", (Node *)$2);
7143 				}
7144 			| WINDOW
7145 				{
7146 					$$ = makeDefElem("window", (Node *)makeInteger(TRUE));
7147 				}
7148 			| common_func_opt_item
7149 				{
7150 					$$ = $1;
7151 				}
7152 		;
7153 
7154 func_as:	Sconst						{ $$ = list_make1(makeString($1)); }
7155 			| Sconst ',' Sconst
7156 				{
7157 					$$ = list_make2(makeString($1), makeString($3));
7158 				}
7159 		;
7160 
7161 transform_type_list:
7162 			FOR TYPE_P Typename { $$ = list_make1($3); }
7163 			| transform_type_list ',' FOR TYPE_P Typename { $$ = lappend($1, $5); }
7164 		;
7165 
7166 opt_definition:
7167 			WITH definition							{ $$ = $2; }
7168 			| /*EMPTY*/								{ $$ = NIL; }
7169 		;
7170 
7171 table_func_column:	param_name func_type
7172 				{
7173 					FunctionParameter *n = makeNode(FunctionParameter);
7174 					n->name = $1;
7175 					n->argType = $2;
7176 					n->mode = FUNC_PARAM_TABLE;
7177 					n->defexpr = NULL;
7178 					$$ = n;
7179 				}
7180 		;
7181 
7182 table_func_column_list:
7183 			table_func_column
7184 				{
7185 					$$ = list_make1($1);
7186 				}
7187 			| table_func_column_list ',' table_func_column
7188 				{
7189 					$$ = lappend($1, $3);
7190 				}
7191 		;
7192 
7193 /*****************************************************************************
7194  * ALTER FUNCTION
7195  *
7196  * RENAME and OWNER subcommands are already provided by the generic
7197  * ALTER infrastructure, here we just specify alterations that can
7198  * only be applied to functions.
7199  *
7200  *****************************************************************************/
7201 AlterFunctionStmt:
7202 			ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
7203 				{
7204 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
7205 					n->func = $3;
7206 					n->actions = $4;
7207 					$$ = (Node *) n;
7208 				}
7209 		;
7210 
7211 alterfunc_opt_list:
7212 			/* At least one option must be specified */
7213 			common_func_opt_item					{ $$ = list_make1($1); }
7214 			| alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
7215 		;
7216 
7217 /* Ignored, merely for SQL compliance */
7218 opt_restrict:
7219 			RESTRICT
7220 			| /* EMPTY */
7221 		;
7222 
7223 
7224 /*****************************************************************************
7225  *
7226  *		QUERY:
7227  *
7228  *		DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
7229  *		DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
7230  *		DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
7231  *
7232  *****************************************************************************/
7233 
7234 RemoveFuncStmt:
7235 			DROP FUNCTION func_name func_args opt_drop_behavior
7236 				{
7237 					DropStmt *n = makeNode(DropStmt);
7238 					n->removeType = OBJECT_FUNCTION;
7239 					n->objects = list_make1($3);
7240 					n->arguments = list_make1(extractArgTypes($4));
7241 					n->behavior = $5;
7242 					n->missing_ok = false;
7243 					n->concurrent = false;
7244 					$$ = (Node *)n;
7245 				}
7246 			| DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
7247 				{
7248 					DropStmt *n = makeNode(DropStmt);
7249 					n->removeType = OBJECT_FUNCTION;
7250 					n->objects = list_make1($5);
7251 					n->arguments = list_make1(extractArgTypes($6));
7252 					n->behavior = $7;
7253 					n->missing_ok = true;
7254 					n->concurrent = false;
7255 					$$ = (Node *)n;
7256 				}
7257 		;
7258 
7259 RemoveAggrStmt:
7260 			DROP AGGREGATE func_name aggr_args opt_drop_behavior
7261 				{
7262 					DropStmt *n = makeNode(DropStmt);
7263 					n->removeType = OBJECT_AGGREGATE;
7264 					n->objects = list_make1($3);
7265 					n->arguments = list_make1(extractAggrArgTypes($4));
7266 					n->behavior = $5;
7267 					n->missing_ok = false;
7268 					n->concurrent = false;
7269 					$$ = (Node *)n;
7270 				}
7271 			| DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
7272 				{
7273 					DropStmt *n = makeNode(DropStmt);
7274 					n->removeType = OBJECT_AGGREGATE;
7275 					n->objects = list_make1($5);
7276 					n->arguments = list_make1(extractAggrArgTypes($6));
7277 					n->behavior = $7;
7278 					n->missing_ok = true;
7279 					n->concurrent = false;
7280 					$$ = (Node *)n;
7281 				}
7282 		;
7283 
7284 RemoveOperStmt:
7285 			DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
7286 				{
7287 					DropStmt *n = makeNode(DropStmt);
7288 					n->removeType = OBJECT_OPERATOR;
7289 					n->objects = list_make1($3);
7290 					n->arguments = list_make1($4);
7291 					n->behavior = $5;
7292 					n->missing_ok = false;
7293 					n->concurrent = false;
7294 					$$ = (Node *)n;
7295 				}
7296 			| DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
7297 				{
7298 					DropStmt *n = makeNode(DropStmt);
7299 					n->removeType = OBJECT_OPERATOR;
7300 					n->objects = list_make1($5);
7301 					n->arguments = list_make1($6);
7302 					n->behavior = $7;
7303 					n->missing_ok = true;
7304 					n->concurrent = false;
7305 					$$ = (Node *)n;
7306 				}
7307 		;
7308 
7309 oper_argtypes:
7310 			'(' Typename ')'
7311 				{
7312 				   ereport(ERROR,
7313 						   (errcode(ERRCODE_SYNTAX_ERROR),
7314 							errmsg("missing argument"),
7315 							errhint("Use NONE to denote the missing argument of a unary operator."),
7316 							parser_errposition(@3)));
7317 				}
7318 			| '(' Typename ',' Typename ')'
7319 					{ $$ = list_make2($2, $4); }
7320 			| '(' NONE ',' Typename ')'					/* left unary */
7321 					{ $$ = list_make2(NULL, $4); }
7322 			| '(' Typename ',' NONE ')'					/* right unary */
7323 					{ $$ = list_make2($2, NULL); }
7324 		;
7325 
7326 any_operator:
7327 			all_Op
7328 					{ $$ = list_make1(makeString($1)); }
7329 			| ColId '.' any_operator
7330 					{ $$ = lcons(makeString($1), $3); }
7331 		;
7332 
7333 /*****************************************************************************
7334  *
7335  *		DO <anonymous code block> [ LANGUAGE language ]
7336  *
7337  * We use a DefElem list for future extensibility, and to allow flexibility
7338  * in the clause order.
7339  *
7340  *****************************************************************************/
7341 
7342 DoStmt: DO dostmt_opt_list
7343 				{
7344 					DoStmt *n = makeNode(DoStmt);
7345 					n->args = $2;
7346 					$$ = (Node *)n;
7347 				}
7348 		;
7349 
7350 dostmt_opt_list:
7351 			dostmt_opt_item						{ $$ = list_make1($1); }
7352 			| dostmt_opt_list dostmt_opt_item	{ $$ = lappend($1, $2); }
7353 		;
7354 
7355 dostmt_opt_item:
7356 			Sconst
7357 				{
7358 					$$ = makeDefElem("as", (Node *)makeString($1));
7359 				}
7360 			| LANGUAGE NonReservedWord_or_Sconst
7361 				{
7362 					$$ = makeDefElem("language", (Node *)makeString($2));
7363 				}
7364 		;
7365 
7366 /*****************************************************************************
7367  *
7368  *		CREATE CAST / DROP CAST
7369  *
7370  *****************************************************************************/
7371 
7372 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
7373 					WITH FUNCTION function_with_argtypes cast_context
7374 				{
7375 					CreateCastStmt *n = makeNode(CreateCastStmt);
7376 					n->sourcetype = $4;
7377 					n->targettype = $6;
7378 					n->func = $10;
7379 					n->context = (CoercionContext) $11;
7380 					n->inout = false;
7381 					$$ = (Node *)n;
7382 				}
7383 			| CREATE CAST '(' Typename AS Typename ')'
7384 					WITHOUT FUNCTION cast_context
7385 				{
7386 					CreateCastStmt *n = makeNode(CreateCastStmt);
7387 					n->sourcetype = $4;
7388 					n->targettype = $6;
7389 					n->func = NULL;
7390 					n->context = (CoercionContext) $10;
7391 					n->inout = false;
7392 					$$ = (Node *)n;
7393 				}
7394 			| CREATE CAST '(' Typename AS Typename ')'
7395 					WITH INOUT cast_context
7396 				{
7397 					CreateCastStmt *n = makeNode(CreateCastStmt);
7398 					n->sourcetype = $4;
7399 					n->targettype = $6;
7400 					n->func = NULL;
7401 					n->context = (CoercionContext) $10;
7402 					n->inout = true;
7403 					$$ = (Node *)n;
7404 				}
7405 		;
7406 
7407 cast_context:  AS IMPLICIT_P					{ $$ = COERCION_IMPLICIT; }
7408 		| AS ASSIGNMENT							{ $$ = COERCION_ASSIGNMENT; }
7409 		| /*EMPTY*/								{ $$ = COERCION_EXPLICIT; }
7410 		;
7411 
7412 
7413 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
7414 				{
7415 					DropStmt *n = makeNode(DropStmt);
7416 					n->removeType = OBJECT_CAST;
7417 					n->objects = list_make1(list_make1($5));
7418 					n->arguments = list_make1(list_make1($7));
7419 					n->behavior = $9;
7420 					n->missing_ok = $3;
7421 					n->concurrent = false;
7422 					$$ = (Node *)n;
7423 				}
7424 		;
7425 
7426 opt_if_exists: IF_P EXISTS						{ $$ = TRUE; }
7427 		| /*EMPTY*/								{ $$ = FALSE; }
7428 		;
7429 
7430 
7431 /*****************************************************************************
7432  *
7433  *		CREATE TRANSFORM / DROP TRANSFORM
7434  *
7435  *****************************************************************************/
7436 
7437 CreateTransformStmt: CREATE opt_or_replace TRANSFORM FOR Typename LANGUAGE name '(' transform_element_list ')'
7438 				{
7439 					CreateTransformStmt *n = makeNode(CreateTransformStmt);
7440 					n->replace = $2;
7441 					n->type_name = $5;
7442 					n->lang = $7;
7443 					n->fromsql = linitial($9);
7444 					n->tosql = lsecond($9);
7445 					$$ = (Node *)n;
7446 				}
7447 		;
7448 
7449 transform_element_list: FROM SQL_P WITH FUNCTION function_with_argtypes ',' TO SQL_P WITH FUNCTION function_with_argtypes
7450 				{
7451 					$$ = list_make2($5, $11);
7452 				}
7453 				| TO SQL_P WITH FUNCTION function_with_argtypes ',' FROM SQL_P WITH FUNCTION function_with_argtypes
7454 				{
7455 					$$ = list_make2($11, $5);
7456 				}
7457 				| FROM SQL_P WITH FUNCTION function_with_argtypes
7458 				{
7459 					$$ = list_make2($5, NULL);
7460 				}
7461 				| TO SQL_P WITH FUNCTION function_with_argtypes
7462 				{
7463 					$$ = list_make2(NULL, $5);
7464 				}
7465 		;
7466 
7467 
7468 DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_drop_behavior
7469 				{
7470 					DropStmt *n = makeNode(DropStmt);
7471 					n->removeType = OBJECT_TRANSFORM;
7472 					n->objects = list_make1(list_make1($5));
7473 					n->arguments = list_make1(list_make1(makeString($7)));
7474 					n->behavior = $8;
7475 					n->missing_ok = $3;
7476 					$$ = (Node *)n;
7477 				}
7478 		;
7479 
7480 
7481 /*****************************************************************************
7482  *
7483  *		QUERY:
7484  *
7485  *		REINDEX [ (options) ] type <name>
7486  *****************************************************************************/
7487 
7488 ReindexStmt:
7489 			REINDEX reindex_target_type qualified_name
7490 				{
7491 					ReindexStmt *n = makeNode(ReindexStmt);
7492 					n->kind = $2;
7493 					n->relation = $3;
7494 					n->name = NULL;
7495 					n->options = 0;
7496 					$$ = (Node *)n;
7497 				}
7498 			| REINDEX reindex_target_multitable name
7499 				{
7500 					ReindexStmt *n = makeNode(ReindexStmt);
7501 					n->kind = $2;
7502 					n->name = $3;
7503 					n->relation = NULL;
7504 					n->options = 0;
7505 					$$ = (Node *)n;
7506 				}
7507 			| REINDEX '(' reindex_option_list ')' reindex_target_type qualified_name
7508 				{
7509 					ReindexStmt *n = makeNode(ReindexStmt);
7510 					n->kind = $5;
7511 					n->relation = $6;
7512 					n->name = NULL;
7513 					n->options = $3;
7514 					$$ = (Node *)n;
7515 				}
7516 			| REINDEX '(' reindex_option_list ')' reindex_target_multitable name
7517 				{
7518 					ReindexStmt *n = makeNode(ReindexStmt);
7519 					n->kind = $5;
7520 					n->name = $6;
7521 					n->relation = NULL;
7522 					n->options = $3;
7523 					$$ = (Node *)n;
7524 				}
7525 		;
7526 reindex_target_type:
7527 			INDEX					{ $$ = REINDEX_OBJECT_INDEX; }
7528 			| TABLE					{ $$ = REINDEX_OBJECT_TABLE; }
7529 		;
7530 reindex_target_multitable:
7531 			SCHEMA					{ $$ = REINDEX_OBJECT_SCHEMA; }
7532 			| SYSTEM_P				{ $$ = REINDEX_OBJECT_SYSTEM; }
7533 			| DATABASE				{ $$ = REINDEX_OBJECT_DATABASE; }
7534 		;
7535 reindex_option_list:
7536 			reindex_option_elem								{ $$ = $1; }
7537 			| reindex_option_list ',' reindex_option_elem	{ $$ = $1 | $3; }
7538 		;
7539 reindex_option_elem:
7540 			VERBOSE	{ $$ = REINDEXOPT_VERBOSE; }
7541 		;
7542 
7543 /*****************************************************************************
7544  *
7545  * ALTER TABLESPACE
7546  *
7547  *****************************************************************************/
7548 
7549 AlterTblSpcStmt:
7550 			ALTER TABLESPACE name SET reloptions
7551 				{
7552 					AlterTableSpaceOptionsStmt *n =
7553 						makeNode(AlterTableSpaceOptionsStmt);
7554 					n->tablespacename = $3;
7555 					n->options = $5;
7556 					n->isReset = FALSE;
7557 					$$ = (Node *)n;
7558 				}
7559 			| ALTER TABLESPACE name RESET reloptions
7560 				{
7561 					AlterTableSpaceOptionsStmt *n =
7562 						makeNode(AlterTableSpaceOptionsStmt);
7563 					n->tablespacename = $3;
7564 					n->options = $5;
7565 					n->isReset = TRUE;
7566 					$$ = (Node *)n;
7567 				}
7568 		;
7569 
7570 /*****************************************************************************
7571  *
7572  * ALTER THING name RENAME TO newname
7573  *
7574  *****************************************************************************/
7575 
7576 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
7577 				{
7578 					RenameStmt *n = makeNode(RenameStmt);
7579 					n->renameType = OBJECT_AGGREGATE;
7580 					n->object = $3;
7581 					n->objarg = extractAggrArgTypes($4);
7582 					n->newname = $7;
7583 					n->missing_ok = false;
7584 					$$ = (Node *)n;
7585 				}
7586 			| ALTER COLLATION any_name RENAME TO name
7587 				{
7588 					RenameStmt *n = makeNode(RenameStmt);
7589 					n->renameType = OBJECT_COLLATION;
7590 					n->object = $3;
7591 					n->newname = $6;
7592 					n->missing_ok = false;
7593 					$$ = (Node *)n;
7594 				}
7595 			| ALTER CONVERSION_P any_name RENAME TO name
7596 				{
7597 					RenameStmt *n = makeNode(RenameStmt);
7598 					n->renameType = OBJECT_CONVERSION;
7599 					n->object = $3;
7600 					n->newname = $6;
7601 					n->missing_ok = false;
7602 					$$ = (Node *)n;
7603 				}
7604 			| ALTER DATABASE database_name RENAME TO database_name
7605 				{
7606 					RenameStmt *n = makeNode(RenameStmt);
7607 					n->renameType = OBJECT_DATABASE;
7608 					n->subname = $3;
7609 					n->newname = $6;
7610 					n->missing_ok = false;
7611 					$$ = (Node *)n;
7612 				}
7613 			| ALTER DOMAIN_P any_name RENAME TO name
7614 				{
7615 					RenameStmt *n = makeNode(RenameStmt);
7616 					n->renameType = OBJECT_DOMAIN;
7617 					n->object = $3;
7618 					n->newname = $6;
7619 					n->missing_ok = false;
7620 					$$ = (Node *)n;
7621 				}
7622 			| ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
7623 				{
7624 					RenameStmt *n = makeNode(RenameStmt);
7625 					n->renameType = OBJECT_DOMCONSTRAINT;
7626 					n->object = $3;
7627 					n->subname = $6;
7628 					n->newname = $8;
7629 					$$ = (Node *)n;
7630 				}
7631 			| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
7632 				{
7633 					RenameStmt *n = makeNode(RenameStmt);
7634 					n->renameType = OBJECT_FDW;
7635 					n->object = list_make1(makeString($5));
7636 					n->newname = $8;
7637 					n->missing_ok = false;
7638 					$$ = (Node *)n;
7639 				}
7640 			| ALTER FUNCTION function_with_argtypes RENAME TO name
7641 				{
7642 					RenameStmt *n = makeNode(RenameStmt);
7643 					n->renameType = OBJECT_FUNCTION;
7644 					n->object = $3->funcname;
7645 					n->objarg = $3->funcargs;
7646 					n->newname = $6;
7647 					n->missing_ok = false;
7648 					$$ = (Node *)n;
7649 				}
7650 			| ALTER GROUP_P RoleId RENAME TO RoleId
7651 				{
7652 					RenameStmt *n = makeNode(RenameStmt);
7653 					n->renameType = OBJECT_ROLE;
7654 					n->subname = $3;
7655 					n->newname = $6;
7656 					n->missing_ok = false;
7657 					$$ = (Node *)n;
7658 				}
7659 			| ALTER opt_procedural LANGUAGE name RENAME TO name
7660 				{
7661 					RenameStmt *n = makeNode(RenameStmt);
7662 					n->renameType = OBJECT_LANGUAGE;
7663 					n->object = list_make1(makeString($4));
7664 					n->newname = $7;
7665 					n->missing_ok = false;
7666 					$$ = (Node *)n;
7667 				}
7668 			| ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
7669 				{
7670 					RenameStmt *n = makeNode(RenameStmt);
7671 					n->renameType = OBJECT_OPCLASS;
7672 					n->object = lcons(makeString($6), $4);
7673 					n->newname = $9;
7674 					n->missing_ok = false;
7675 					$$ = (Node *)n;
7676 				}
7677 			| ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
7678 				{
7679 					RenameStmt *n = makeNode(RenameStmt);
7680 					n->renameType = OBJECT_OPFAMILY;
7681 					n->object = lcons(makeString($6), $4);
7682 					n->newname = $9;
7683 					n->missing_ok = false;
7684 					$$ = (Node *)n;
7685 				}
7686 			| ALTER POLICY name ON qualified_name RENAME TO name
7687 				{
7688 					RenameStmt *n = makeNode(RenameStmt);
7689 					n->renameType = OBJECT_POLICY;
7690 					n->relation = $5;
7691 					n->subname = $3;
7692 					n->newname = $8;
7693 					n->missing_ok = false;
7694 					$$ = (Node *)n;
7695 				}
7696 			| ALTER POLICY IF_P EXISTS name ON qualified_name RENAME TO name
7697 				{
7698 					RenameStmt *n = makeNode(RenameStmt);
7699 					n->renameType = OBJECT_POLICY;
7700 					n->relation = $7;
7701 					n->subname = $5;
7702 					n->newname = $10;
7703 					n->missing_ok = true;
7704 					$$ = (Node *)n;
7705 				}
7706 			| ALTER SCHEMA name RENAME TO name
7707 				{
7708 					RenameStmt *n = makeNode(RenameStmt);
7709 					n->renameType = OBJECT_SCHEMA;
7710 					n->subname = $3;
7711 					n->newname = $6;
7712 					n->missing_ok = false;
7713 					$$ = (Node *)n;
7714 				}
7715 			| ALTER SERVER name RENAME TO name
7716 				{
7717 					RenameStmt *n = makeNode(RenameStmt);
7718 					n->renameType = OBJECT_FOREIGN_SERVER;
7719 					n->object = list_make1(makeString($3));
7720 					n->newname = $6;
7721 					n->missing_ok = false;
7722 					$$ = (Node *)n;
7723 				}
7724 			| ALTER TABLE relation_expr RENAME TO name
7725 				{
7726 					RenameStmt *n = makeNode(RenameStmt);
7727 					n->renameType = OBJECT_TABLE;
7728 					n->relation = $3;
7729 					n->subname = NULL;
7730 					n->newname = $6;
7731 					n->missing_ok = false;
7732 					$$ = (Node *)n;
7733 				}
7734 			| ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
7735 				{
7736 					RenameStmt *n = makeNode(RenameStmt);
7737 					n->renameType = OBJECT_TABLE;
7738 					n->relation = $5;
7739 					n->subname = NULL;
7740 					n->newname = $8;
7741 					n->missing_ok = true;
7742 					$$ = (Node *)n;
7743 				}
7744 			| ALTER SEQUENCE qualified_name RENAME TO name
7745 				{
7746 					RenameStmt *n = makeNode(RenameStmt);
7747 					n->renameType = OBJECT_SEQUENCE;
7748 					n->relation = $3;
7749 					n->subname = NULL;
7750 					n->newname = $6;
7751 					n->missing_ok = false;
7752 					$$ = (Node *)n;
7753 				}
7754 			| ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
7755 				{
7756 					RenameStmt *n = makeNode(RenameStmt);
7757 					n->renameType = OBJECT_SEQUENCE;
7758 					n->relation = $5;
7759 					n->subname = NULL;
7760 					n->newname = $8;
7761 					n->missing_ok = true;
7762 					$$ = (Node *)n;
7763 				}
7764 			| ALTER VIEW qualified_name RENAME TO name
7765 				{
7766 					RenameStmt *n = makeNode(RenameStmt);
7767 					n->renameType = OBJECT_VIEW;
7768 					n->relation = $3;
7769 					n->subname = NULL;
7770 					n->newname = $6;
7771 					n->missing_ok = false;
7772 					$$ = (Node *)n;
7773 				}
7774 			| ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
7775 				{
7776 					RenameStmt *n = makeNode(RenameStmt);
7777 					n->renameType = OBJECT_VIEW;
7778 					n->relation = $5;
7779 					n->subname = NULL;
7780 					n->newname = $8;
7781 					n->missing_ok = true;
7782 					$$ = (Node *)n;
7783 				}
7784 			| ALTER MATERIALIZED VIEW qualified_name RENAME TO name
7785 				{
7786 					RenameStmt *n = makeNode(RenameStmt);
7787 					n->renameType = OBJECT_MATVIEW;
7788 					n->relation = $4;
7789 					n->subname = NULL;
7790 					n->newname = $7;
7791 					n->missing_ok = false;
7792 					$$ = (Node *)n;
7793 				}
7794 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
7795 				{
7796 					RenameStmt *n = makeNode(RenameStmt);
7797 					n->renameType = OBJECT_MATVIEW;
7798 					n->relation = $6;
7799 					n->subname = NULL;
7800 					n->newname = $9;
7801 					n->missing_ok = true;
7802 					$$ = (Node *)n;
7803 				}
7804 			| ALTER INDEX qualified_name RENAME TO name
7805 				{
7806 					RenameStmt *n = makeNode(RenameStmt);
7807 					n->renameType = OBJECT_INDEX;
7808 					n->relation = $3;
7809 					n->subname = NULL;
7810 					n->newname = $6;
7811 					n->missing_ok = false;
7812 					$$ = (Node *)n;
7813 				}
7814 			| ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
7815 				{
7816 					RenameStmt *n = makeNode(RenameStmt);
7817 					n->renameType = OBJECT_INDEX;
7818 					n->relation = $5;
7819 					n->subname = NULL;
7820 					n->newname = $8;
7821 					n->missing_ok = true;
7822 					$$ = (Node *)n;
7823 				}
7824 			| ALTER FOREIGN TABLE relation_expr RENAME TO name
7825 				{
7826 					RenameStmt *n = makeNode(RenameStmt);
7827 					n->renameType = OBJECT_FOREIGN_TABLE;
7828 					n->relation = $4;
7829 					n->subname = NULL;
7830 					n->newname = $7;
7831 					n->missing_ok = false;
7832 					$$ = (Node *)n;
7833 				}
7834 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
7835 				{
7836 					RenameStmt *n = makeNode(RenameStmt);
7837 					n->renameType = OBJECT_FOREIGN_TABLE;
7838 					n->relation = $6;
7839 					n->subname = NULL;
7840 					n->newname = $9;
7841 					n->missing_ok = true;
7842 					$$ = (Node *)n;
7843 				}
7844 			| ALTER TABLE relation_expr RENAME opt_column name TO name
7845 				{
7846 					RenameStmt *n = makeNode(RenameStmt);
7847 					n->renameType = OBJECT_COLUMN;
7848 					n->relationType = OBJECT_TABLE;
7849 					n->relation = $3;
7850 					n->subname = $6;
7851 					n->newname = $8;
7852 					n->missing_ok = false;
7853 					$$ = (Node *)n;
7854 				}
7855 			| ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
7856 				{
7857 					RenameStmt *n = makeNode(RenameStmt);
7858 					n->renameType = OBJECT_COLUMN;
7859 					n->relationType = OBJECT_TABLE;
7860 					n->relation = $5;
7861 					n->subname = $8;
7862 					n->newname = $10;
7863 					n->missing_ok = true;
7864 					$$ = (Node *)n;
7865 				}
7866 			| ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
7867 				{
7868 					RenameStmt *n = makeNode(RenameStmt);
7869 					n->renameType = OBJECT_COLUMN;
7870 					n->relationType = OBJECT_MATVIEW;
7871 					n->relation = $4;
7872 					n->subname = $7;
7873 					n->newname = $9;
7874 					n->missing_ok = false;
7875 					$$ = (Node *)n;
7876 				}
7877 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
7878 				{
7879 					RenameStmt *n = makeNode(RenameStmt);
7880 					n->renameType = OBJECT_COLUMN;
7881 					n->relationType = OBJECT_MATVIEW;
7882 					n->relation = $6;
7883 					n->subname = $9;
7884 					n->newname = $11;
7885 					n->missing_ok = true;
7886 					$$ = (Node *)n;
7887 				}
7888 			| ALTER TABLE relation_expr RENAME CONSTRAINT name TO name
7889 				{
7890 					RenameStmt *n = makeNode(RenameStmt);
7891 					n->renameType = OBJECT_TABCONSTRAINT;
7892 					n->relation = $3;
7893 					n->subname = $6;
7894 					n->newname = $8;
7895 					n->missing_ok = false;
7896 					$$ = (Node *)n;
7897 				}
7898 			| ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
7899 				{
7900 					RenameStmt *n = makeNode(RenameStmt);
7901 					n->renameType = OBJECT_TABCONSTRAINT;
7902 					n->relation = $5;
7903 					n->subname = $8;
7904 					n->newname = $10;
7905 					n->missing_ok = true;
7906 					$$ = (Node *)n;
7907 				}
7908 			| ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
7909 				{
7910 					RenameStmt *n = makeNode(RenameStmt);
7911 					n->renameType = OBJECT_COLUMN;
7912 					n->relationType = OBJECT_FOREIGN_TABLE;
7913 					n->relation = $4;
7914 					n->subname = $7;
7915 					n->newname = $9;
7916 					n->missing_ok = false;
7917 					$$ = (Node *)n;
7918 				}
7919 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
7920 				{
7921 					RenameStmt *n = makeNode(RenameStmt);
7922 					n->renameType = OBJECT_COLUMN;
7923 					n->relationType = OBJECT_FOREIGN_TABLE;
7924 					n->relation = $6;
7925 					n->subname = $9;
7926 					n->newname = $11;
7927 					n->missing_ok = true;
7928 					$$ = (Node *)n;
7929 				}
7930 			| ALTER RULE name ON qualified_name RENAME TO name
7931 				{
7932 					RenameStmt *n = makeNode(RenameStmt);
7933 					n->renameType = OBJECT_RULE;
7934 					n->relation = $5;
7935 					n->subname = $3;
7936 					n->newname = $8;
7937 					n->missing_ok = false;
7938 					$$ = (Node *)n;
7939 				}
7940 			| ALTER TRIGGER name ON qualified_name RENAME TO name
7941 				{
7942 					RenameStmt *n = makeNode(RenameStmt);
7943 					n->renameType = OBJECT_TRIGGER;
7944 					n->relation = $5;
7945 					n->subname = $3;
7946 					n->newname = $8;
7947 					n->missing_ok = false;
7948 					$$ = (Node *)n;
7949 				}
7950 			| ALTER EVENT TRIGGER name RENAME TO name
7951 				{
7952 					RenameStmt *n = makeNode(RenameStmt);
7953 					n->renameType = OBJECT_EVENT_TRIGGER;
7954 					n->object = list_make1(makeString($4));
7955 					n->newname = $7;
7956 					$$ = (Node *)n;
7957 				}
7958 			| ALTER ROLE RoleId RENAME TO RoleId
7959 				{
7960 					RenameStmt *n = makeNode(RenameStmt);
7961 					n->renameType = OBJECT_ROLE;
7962 					n->subname = $3;
7963 					n->newname = $6;
7964 					n->missing_ok = false;
7965 					$$ = (Node *)n;
7966 				}
7967 			| ALTER USER RoleId RENAME TO RoleId
7968 				{
7969 					RenameStmt *n = makeNode(RenameStmt);
7970 					n->renameType = OBJECT_ROLE;
7971 					n->subname = $3;
7972 					n->newname = $6;
7973 					n->missing_ok = false;
7974 					$$ = (Node *)n;
7975 				}
7976 			| ALTER TABLESPACE name RENAME TO name
7977 				{
7978 					RenameStmt *n = makeNode(RenameStmt);
7979 					n->renameType = OBJECT_TABLESPACE;
7980 					n->subname = $3;
7981 					n->newname = $6;
7982 					n->missing_ok = false;
7983 					$$ = (Node *)n;
7984 				}
7985 			| ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
7986 				{
7987 					RenameStmt *n = makeNode(RenameStmt);
7988 					n->renameType = OBJECT_TSPARSER;
7989 					n->object = $5;
7990 					n->newname = $8;
7991 					n->missing_ok = false;
7992 					$$ = (Node *)n;
7993 				}
7994 			| ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
7995 				{
7996 					RenameStmt *n = makeNode(RenameStmt);
7997 					n->renameType = OBJECT_TSDICTIONARY;
7998 					n->object = $5;
7999 					n->newname = $8;
8000 					n->missing_ok = false;
8001 					$$ = (Node *)n;
8002 				}
8003 			| ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
8004 				{
8005 					RenameStmt *n = makeNode(RenameStmt);
8006 					n->renameType = OBJECT_TSTEMPLATE;
8007 					n->object = $5;
8008 					n->newname = $8;
8009 					n->missing_ok = false;
8010 					$$ = (Node *)n;
8011 				}
8012 			| ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
8013 				{
8014 					RenameStmt *n = makeNode(RenameStmt);
8015 					n->renameType = OBJECT_TSCONFIGURATION;
8016 					n->object = $5;
8017 					n->newname = $8;
8018 					n->missing_ok = false;
8019 					$$ = (Node *)n;
8020 				}
8021 			| ALTER TYPE_P any_name RENAME TO name
8022 				{
8023 					RenameStmt *n = makeNode(RenameStmt);
8024 					n->renameType = OBJECT_TYPE;
8025 					n->object = $3;
8026 					n->newname = $6;
8027 					n->missing_ok = false;
8028 					$$ = (Node *)n;
8029 				}
8030 			| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
8031 				{
8032 					RenameStmt *n = makeNode(RenameStmt);
8033 					n->renameType = OBJECT_ATTRIBUTE;
8034 					n->relationType = OBJECT_TYPE;
8035 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
8036 					n->subname = $6;
8037 					n->newname = $8;
8038 					n->behavior = $9;
8039 					n->missing_ok = false;
8040 					$$ = (Node *)n;
8041 				}
8042 		;
8043 
8044 opt_column: COLUMN									{ $$ = COLUMN; }
8045 			| /*EMPTY*/								{ $$ = 0; }
8046 		;
8047 
8048 opt_set_data: SET DATA_P							{ $$ = 1; }
8049 			| /*EMPTY*/								{ $$ = 0; }
8050 		;
8051 
8052 /*****************************************************************************
8053  *
8054  * ALTER THING name DEPENDS ON EXTENSION name
8055  *
8056  *****************************************************************************/
8057 
8058 AlterObjectDependsStmt:
8059 			ALTER FUNCTION function_with_argtypes DEPENDS ON EXTENSION name
8060 				{
8061 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8062 					n->objectType = OBJECT_FUNCTION;
8063 					n->relation = NULL;
8064 					n->objname = $3->funcname;
8065 					n->objargs = $3->funcargs;
8066 					n->extname = makeString($7);
8067 					$$ = (Node *)n;
8068 				}
8069 			| ALTER TRIGGER name ON qualified_name DEPENDS ON EXTENSION name
8070 				{
8071 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8072 					n->objectType = OBJECT_TRIGGER;
8073 					n->relation = $5;
8074 					n->objname = list_make1(makeString($3));
8075 					n->objargs = NIL;
8076 					n->extname = makeString($9);
8077 					$$ = (Node *)n;
8078 				}
8079 			| ALTER MATERIALIZED VIEW qualified_name DEPENDS ON EXTENSION name
8080 				{
8081 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8082 					n->objectType = OBJECT_MATVIEW;
8083 					n->relation = $4;
8084 					n->objname = NIL;
8085 					n->objargs = NIL;
8086 					n->extname = makeString($8);
8087 					$$ = (Node *)n;
8088 				}
8089 			| ALTER INDEX qualified_name DEPENDS ON EXTENSION name
8090 				{
8091 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8092 					n->objectType = OBJECT_INDEX;
8093 					n->relation = $3;
8094 					n->objname = NIL;
8095 					n->objargs = NIL;
8096 					n->extname = makeString($7);
8097 					$$ = (Node *)n;
8098 				}
8099 		;
8100 
8101 /*****************************************************************************
8102  *
8103  * ALTER THING name SET SCHEMA name
8104  *
8105  *****************************************************************************/
8106 
8107 AlterObjectSchemaStmt:
8108 			ALTER AGGREGATE func_name aggr_args SET SCHEMA name
8109 				{
8110 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8111 					n->objectType = OBJECT_AGGREGATE;
8112 					n->object = $3;
8113 					n->objarg = extractAggrArgTypes($4);
8114 					n->newschema = $7;
8115 					n->missing_ok = false;
8116 					$$ = (Node *)n;
8117 				}
8118 			| ALTER COLLATION any_name SET SCHEMA name
8119 				{
8120 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8121 					n->objectType = OBJECT_COLLATION;
8122 					n->object = $3;
8123 					n->newschema = $6;
8124 					n->missing_ok = false;
8125 					$$ = (Node *)n;
8126 				}
8127 			| ALTER CONVERSION_P any_name SET SCHEMA name
8128 				{
8129 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8130 					n->objectType = OBJECT_CONVERSION;
8131 					n->object = $3;
8132 					n->newschema = $6;
8133 					n->missing_ok = false;
8134 					$$ = (Node *)n;
8135 				}
8136 			| ALTER DOMAIN_P any_name SET SCHEMA name
8137 				{
8138 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8139 					n->objectType = OBJECT_DOMAIN;
8140 					n->object = $3;
8141 					n->newschema = $6;
8142 					n->missing_ok = false;
8143 					$$ = (Node *)n;
8144 				}
8145 			| ALTER EXTENSION any_name SET SCHEMA name
8146 				{
8147 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8148 					n->objectType = OBJECT_EXTENSION;
8149 					n->object = $3;
8150 					n->newschema = $6;
8151 					n->missing_ok = false;
8152 					$$ = (Node *)n;
8153 				}
8154 			| ALTER FUNCTION function_with_argtypes SET SCHEMA name
8155 				{
8156 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8157 					n->objectType = OBJECT_FUNCTION;
8158 					n->object = $3->funcname;
8159 					n->objarg = $3->funcargs;
8160 					n->newschema = $6;
8161 					n->missing_ok = false;
8162 					$$ = (Node *)n;
8163 				}
8164 			| ALTER OPERATOR any_operator oper_argtypes SET SCHEMA name
8165 				{
8166 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8167 					n->objectType = OBJECT_OPERATOR;
8168 					n->object = $3;
8169 					n->objarg = $4;
8170 					n->newschema = $7;
8171 					n->missing_ok = false;
8172 					$$ = (Node *)n;
8173 				}
8174 			| ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
8175 				{
8176 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8177 					n->objectType = OBJECT_OPCLASS;
8178 					n->object = lcons(makeString($6), $4);
8179 					n->newschema = $9;
8180 					n->missing_ok = false;
8181 					$$ = (Node *)n;
8182 				}
8183 			| ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
8184 				{
8185 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8186 					n->objectType = OBJECT_OPFAMILY;
8187 					n->object = lcons(makeString($6), $4);
8188 					n->newschema = $9;
8189 					n->missing_ok = false;
8190 					$$ = (Node *)n;
8191 				}
8192 			| ALTER TABLE relation_expr SET SCHEMA name
8193 				{
8194 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8195 					n->objectType = OBJECT_TABLE;
8196 					n->relation = $3;
8197 					n->newschema = $6;
8198 					n->missing_ok = false;
8199 					$$ = (Node *)n;
8200 				}
8201 			| ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
8202 				{
8203 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8204 					n->objectType = OBJECT_TABLE;
8205 					n->relation = $5;
8206 					n->newschema = $8;
8207 					n->missing_ok = true;
8208 					$$ = (Node *)n;
8209 				}
8210 			| ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
8211 				{
8212 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8213 					n->objectType = OBJECT_TSPARSER;
8214 					n->object = $5;
8215 					n->newschema = $8;
8216 					n->missing_ok = false;
8217 					$$ = (Node *)n;
8218 				}
8219 			| ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
8220 				{
8221 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8222 					n->objectType = OBJECT_TSDICTIONARY;
8223 					n->object = $5;
8224 					n->newschema = $8;
8225 					n->missing_ok = false;
8226 					$$ = (Node *)n;
8227 				}
8228 			| ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
8229 				{
8230 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8231 					n->objectType = OBJECT_TSTEMPLATE;
8232 					n->object = $5;
8233 					n->newschema = $8;
8234 					n->missing_ok = false;
8235 					$$ = (Node *)n;
8236 				}
8237 			| ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
8238 				{
8239 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8240 					n->objectType = OBJECT_TSCONFIGURATION;
8241 					n->object = $5;
8242 					n->newschema = $8;
8243 					n->missing_ok = false;
8244 					$$ = (Node *)n;
8245 				}
8246 			| ALTER SEQUENCE qualified_name SET SCHEMA name
8247 				{
8248 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8249 					n->objectType = OBJECT_SEQUENCE;
8250 					n->relation = $3;
8251 					n->newschema = $6;
8252 					n->missing_ok = false;
8253 					$$ = (Node *)n;
8254 				}
8255 			| ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
8256 				{
8257 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8258 					n->objectType = OBJECT_SEQUENCE;
8259 					n->relation = $5;
8260 					n->newschema = $8;
8261 					n->missing_ok = true;
8262 					$$ = (Node *)n;
8263 				}
8264 			| ALTER VIEW qualified_name SET SCHEMA name
8265 				{
8266 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8267 					n->objectType = OBJECT_VIEW;
8268 					n->relation = $3;
8269 					n->newschema = $6;
8270 					n->missing_ok = false;
8271 					$$ = (Node *)n;
8272 				}
8273 			| ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
8274 				{
8275 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8276 					n->objectType = OBJECT_VIEW;
8277 					n->relation = $5;
8278 					n->newschema = $8;
8279 					n->missing_ok = true;
8280 					$$ = (Node *)n;
8281 				}
8282 			| ALTER MATERIALIZED VIEW qualified_name SET SCHEMA name
8283 				{
8284 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8285 					n->objectType = OBJECT_MATVIEW;
8286 					n->relation = $4;
8287 					n->newschema = $7;
8288 					n->missing_ok = false;
8289 					$$ = (Node *)n;
8290 				}
8291 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name SET SCHEMA name
8292 				{
8293 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8294 					n->objectType = OBJECT_MATVIEW;
8295 					n->relation = $6;
8296 					n->newschema = $9;
8297 					n->missing_ok = true;
8298 					$$ = (Node *)n;
8299 				}
8300 			| ALTER FOREIGN TABLE relation_expr SET SCHEMA name
8301 				{
8302 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8303 					n->objectType = OBJECT_FOREIGN_TABLE;
8304 					n->relation = $4;
8305 					n->newschema = $7;
8306 					n->missing_ok = false;
8307 					$$ = (Node *)n;
8308 				}
8309 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
8310 				{
8311 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8312 					n->objectType = OBJECT_FOREIGN_TABLE;
8313 					n->relation = $6;
8314 					n->newschema = $9;
8315 					n->missing_ok = true;
8316 					$$ = (Node *)n;
8317 				}
8318 			| ALTER TYPE_P any_name SET SCHEMA name
8319 				{
8320 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8321 					n->objectType = OBJECT_TYPE;
8322 					n->object = $3;
8323 					n->newschema = $6;
8324 					n->missing_ok = false;
8325 					$$ = (Node *)n;
8326 				}
8327 		;
8328 
8329 /*****************************************************************************
8330  *
8331  * ALTER OPERATOR name SET define
8332  *
8333  *****************************************************************************/
8334 
8335 AlterOperatorStmt:
8336 			ALTER OPERATOR any_operator oper_argtypes SET '(' operator_def_list ')'
8337 				{
8338 					AlterOperatorStmt *n = makeNode(AlterOperatorStmt);
8339 					n->opername = $3;
8340 					n->operargs = $4;
8341 					n->options = $7;
8342 					$$ = (Node *)n;
8343 				}
8344 		;
8345 
8346 operator_def_list:	operator_def_elem								{ $$ = list_make1($1); }
8347 			| operator_def_list ',' operator_def_elem				{ $$ = lappend($1, $3); }
8348 		;
8349 
8350 operator_def_elem: ColLabel '=' NONE
8351 						{ $$ = makeDefElem($1, NULL); }
8352 				   | ColLabel '=' def_arg
8353 						{ $$ = makeDefElem($1, (Node *) $3); }
8354 		;
8355 
8356 /*****************************************************************************
8357  *
8358  * ALTER THING name OWNER TO newname
8359  *
8360  *****************************************************************************/
8361 
8362 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleSpec
8363 				{
8364 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8365 					n->objectType = OBJECT_AGGREGATE;
8366 					n->object = $3;
8367 					n->objarg = extractAggrArgTypes($4);
8368 					n->newowner = $7;
8369 					$$ = (Node *)n;
8370 				}
8371 			| ALTER COLLATION any_name OWNER TO RoleSpec
8372 				{
8373 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8374 					n->objectType = OBJECT_COLLATION;
8375 					n->object = $3;
8376 					n->newowner = $6;
8377 					$$ = (Node *)n;
8378 				}
8379 			| ALTER CONVERSION_P any_name OWNER TO RoleSpec
8380 				{
8381 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8382 					n->objectType = OBJECT_CONVERSION;
8383 					n->object = $3;
8384 					n->newowner = $6;
8385 					$$ = (Node *)n;
8386 				}
8387 			| ALTER DATABASE database_name OWNER TO RoleSpec
8388 				{
8389 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8390 					n->objectType = OBJECT_DATABASE;
8391 					n->object = list_make1(makeString($3));
8392 					n->newowner = $6;
8393 					$$ = (Node *)n;
8394 				}
8395 			| ALTER DOMAIN_P any_name OWNER TO RoleSpec
8396 				{
8397 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8398 					n->objectType = OBJECT_DOMAIN;
8399 					n->object = $3;
8400 					n->newowner = $6;
8401 					$$ = (Node *)n;
8402 				}
8403 			| ALTER FUNCTION function_with_argtypes OWNER TO RoleSpec
8404 				{
8405 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8406 					n->objectType = OBJECT_FUNCTION;
8407 					n->object = $3->funcname;
8408 					n->objarg = $3->funcargs;
8409 					n->newowner = $6;
8410 					$$ = (Node *)n;
8411 				}
8412 			| ALTER opt_procedural LANGUAGE name OWNER TO RoleSpec
8413 				{
8414 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8415 					n->objectType = OBJECT_LANGUAGE;
8416 					n->object = list_make1(makeString($4));
8417 					n->newowner = $7;
8418 					$$ = (Node *)n;
8419 				}
8420 			| ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleSpec
8421 				{
8422 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8423 					n->objectType = OBJECT_LARGEOBJECT;
8424 					n->object = list_make1($4);
8425 					n->newowner = $7;
8426 					$$ = (Node *)n;
8427 				}
8428 			| ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleSpec
8429 				{
8430 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8431 					n->objectType = OBJECT_OPERATOR;
8432 					n->object = $3;
8433 					n->objarg = $4;
8434 					n->newowner = $7;
8435 					$$ = (Node *)n;
8436 				}
8437 			| ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleSpec
8438 				{
8439 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8440 					n->objectType = OBJECT_OPCLASS;
8441 					n->object = lcons(makeString($6), $4);
8442 					n->newowner = $9;
8443 					$$ = (Node *)n;
8444 				}
8445 			| ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleSpec
8446 				{
8447 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8448 					n->objectType = OBJECT_OPFAMILY;
8449 					n->object = lcons(makeString($6), $4);
8450 					n->newowner = $9;
8451 					$$ = (Node *)n;
8452 				}
8453 			| ALTER SCHEMA name OWNER TO RoleSpec
8454 				{
8455 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8456 					n->objectType = OBJECT_SCHEMA;
8457 					n->object = list_make1(makeString($3));
8458 					n->newowner = $6;
8459 					$$ = (Node *)n;
8460 				}
8461 			| ALTER TYPE_P any_name OWNER TO RoleSpec
8462 				{
8463 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8464 					n->objectType = OBJECT_TYPE;
8465 					n->object = $3;
8466 					n->newowner = $6;
8467 					$$ = (Node *)n;
8468 				}
8469 			| ALTER TABLESPACE name OWNER TO RoleSpec
8470 				{
8471 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8472 					n->objectType = OBJECT_TABLESPACE;
8473 					n->object = list_make1(makeString($3));
8474 					n->newowner = $6;
8475 					$$ = (Node *)n;
8476 				}
8477 			| ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleSpec
8478 				{
8479 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8480 					n->objectType = OBJECT_TSDICTIONARY;
8481 					n->object = $5;
8482 					n->newowner = $8;
8483 					$$ = (Node *)n;
8484 				}
8485 			| ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleSpec
8486 				{
8487 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8488 					n->objectType = OBJECT_TSCONFIGURATION;
8489 					n->object = $5;
8490 					n->newowner = $8;
8491 					$$ = (Node *)n;
8492 				}
8493 			| ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleSpec
8494 				{
8495 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8496 					n->objectType = OBJECT_FDW;
8497 					n->object = list_make1(makeString($5));
8498 					n->newowner = $8;
8499 					$$ = (Node *)n;
8500 				}
8501 			| ALTER SERVER name OWNER TO RoleSpec
8502 				{
8503 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8504 					n->objectType = OBJECT_FOREIGN_SERVER;
8505 					n->object = list_make1(makeString($3));
8506 					n->newowner = $6;
8507 					$$ = (Node *)n;
8508 				}
8509 			| ALTER EVENT TRIGGER name OWNER TO RoleSpec
8510 				{
8511 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8512 					n->objectType = OBJECT_EVENT_TRIGGER;
8513 					n->object = list_make1(makeString($4));
8514 					n->newowner = $7;
8515 					$$ = (Node *)n;
8516 				}
8517 		;
8518 
8519 
8520 /*****************************************************************************
8521  *
8522  *		QUERY:	Define Rewrite Rule
8523  *
8524  *****************************************************************************/
8525 
8526 RuleStmt:	CREATE opt_or_replace RULE name AS
8527 			ON event TO qualified_name where_clause
8528 			DO opt_instead RuleActionList
8529 				{
8530 					RuleStmt *n = makeNode(RuleStmt);
8531 					n->replace = $2;
8532 					n->relation = $9;
8533 					n->rulename = $4;
8534 					n->whereClause = $10;
8535 					n->event = $7;
8536 					n->instead = $12;
8537 					n->actions = $13;
8538 					$$ = (Node *)n;
8539 				}
8540 		;
8541 
8542 RuleActionList:
8543 			NOTHING									{ $$ = NIL; }
8544 			| RuleActionStmt						{ $$ = list_make1($1); }
8545 			| '(' RuleActionMulti ')'				{ $$ = $2; }
8546 		;
8547 
8548 /* the thrashing around here is to discard "empty" statements... */
8549 RuleActionMulti:
8550 			RuleActionMulti ';' RuleActionStmtOrEmpty
8551 				{ if ($3 != NULL)
8552 					$$ = lappend($1, $3);
8553 				  else
8554 					$$ = $1;
8555 				}
8556 			| RuleActionStmtOrEmpty
8557 				{ if ($1 != NULL)
8558 					$$ = list_make1($1);
8559 				  else
8560 					$$ = NIL;
8561 				}
8562 		;
8563 
8564 RuleActionStmt:
8565 			SelectStmt
8566 			| InsertStmt
8567 			| UpdateStmt
8568 			| DeleteStmt
8569 			| NotifyStmt
8570 		;
8571 
8572 RuleActionStmtOrEmpty:
8573 			RuleActionStmt							{ $$ = $1; }
8574 			|	/*EMPTY*/							{ $$ = NULL; }
8575 		;
8576 
8577 event:		SELECT									{ $$ = CMD_SELECT; }
8578 			| UPDATE								{ $$ = CMD_UPDATE; }
8579 			| DELETE_P								{ $$ = CMD_DELETE; }
8580 			| INSERT								{ $$ = CMD_INSERT; }
8581 		 ;
8582 
8583 opt_instead:
8584 			INSTEAD									{ $$ = TRUE; }
8585 			| ALSO									{ $$ = FALSE; }
8586 			| /*EMPTY*/								{ $$ = FALSE; }
8587 		;
8588 
8589 
8590 DropRuleStmt:
8591 			DROP RULE name ON any_name opt_drop_behavior
8592 				{
8593 					DropStmt *n = makeNode(DropStmt);
8594 					n->removeType = OBJECT_RULE;
8595 					n->objects = list_make1(lappend($5, makeString($3)));
8596 					n->arguments = NIL;
8597 					n->behavior = $6;
8598 					n->missing_ok = false;
8599 					n->concurrent = false;
8600 					$$ = (Node *) n;
8601 				}
8602 			| DROP RULE IF_P EXISTS name ON any_name opt_drop_behavior
8603 				{
8604 					DropStmt *n = makeNode(DropStmt);
8605 					n->removeType = OBJECT_RULE;
8606 					n->objects = list_make1(lappend($7, makeString($5)));
8607 					n->arguments = NIL;
8608 					n->behavior = $8;
8609 					n->missing_ok = true;
8610 					n->concurrent = false;
8611 					$$ = (Node *) n;
8612 				}
8613 		;
8614 
8615 
8616 /*****************************************************************************
8617  *
8618  *		QUERY:
8619  *				NOTIFY <identifier> can appear both in rule bodies and
8620  *				as a query-level command
8621  *
8622  *****************************************************************************/
8623 
8624 NotifyStmt: NOTIFY ColId notify_payload
8625 				{
8626 					NotifyStmt *n = makeNode(NotifyStmt);
8627 					n->conditionname = $2;
8628 					n->payload = $3;
8629 					$$ = (Node *)n;
8630 				}
8631 		;
8632 
8633 notify_payload:
8634 			',' Sconst							{ $$ = $2; }
8635 			| /*EMPTY*/							{ $$ = NULL; }
8636 		;
8637 
8638 ListenStmt: LISTEN ColId
8639 				{
8640 					ListenStmt *n = makeNode(ListenStmt);
8641 					n->conditionname = $2;
8642 					$$ = (Node *)n;
8643 				}
8644 		;
8645 
8646 UnlistenStmt:
8647 			UNLISTEN ColId
8648 				{
8649 					UnlistenStmt *n = makeNode(UnlistenStmt);
8650 					n->conditionname = $2;
8651 					$$ = (Node *)n;
8652 				}
8653 			| UNLISTEN '*'
8654 				{
8655 					UnlistenStmt *n = makeNode(UnlistenStmt);
8656 					n->conditionname = NULL;
8657 					$$ = (Node *)n;
8658 				}
8659 		;
8660 
8661 
8662 /*****************************************************************************
8663  *
8664  *		Transactions:
8665  *
8666  *		BEGIN / COMMIT / ROLLBACK
8667  *		(also older versions END / ABORT)
8668  *
8669  *****************************************************************************/
8670 
8671 TransactionStmt:
8672 			ABORT_P opt_transaction
8673 				{
8674 					TransactionStmt *n = makeNode(TransactionStmt);
8675 					n->kind = TRANS_STMT_ROLLBACK;
8676 					n->options = NIL;
8677 					$$ = (Node *)n;
8678 				}
8679 			| BEGIN_P opt_transaction transaction_mode_list_or_empty
8680 				{
8681 					TransactionStmt *n = makeNode(TransactionStmt);
8682 					n->kind = TRANS_STMT_BEGIN;
8683 					n->options = $3;
8684 					$$ = (Node *)n;
8685 				}
8686 			| START TRANSACTION transaction_mode_list_or_empty
8687 				{
8688 					TransactionStmt *n = makeNode(TransactionStmt);
8689 					n->kind = TRANS_STMT_START;
8690 					n->options = $3;
8691 					$$ = (Node *)n;
8692 				}
8693 			| COMMIT opt_transaction
8694 				{
8695 					TransactionStmt *n = makeNode(TransactionStmt);
8696 					n->kind = TRANS_STMT_COMMIT;
8697 					n->options = NIL;
8698 					$$ = (Node *)n;
8699 				}
8700 			| END_P opt_transaction
8701 				{
8702 					TransactionStmt *n = makeNode(TransactionStmt);
8703 					n->kind = TRANS_STMT_COMMIT;
8704 					n->options = NIL;
8705 					$$ = (Node *)n;
8706 				}
8707 			| ROLLBACK opt_transaction
8708 				{
8709 					TransactionStmt *n = makeNode(TransactionStmt);
8710 					n->kind = TRANS_STMT_ROLLBACK;
8711 					n->options = NIL;
8712 					$$ = (Node *)n;
8713 				}
8714 			| SAVEPOINT ColId
8715 				{
8716 					TransactionStmt *n = makeNode(TransactionStmt);
8717 					n->kind = TRANS_STMT_SAVEPOINT;
8718 					n->options = list_make1(makeDefElem("savepoint_name",
8719 														(Node *)makeString($2)));
8720 					$$ = (Node *)n;
8721 				}
8722 			| RELEASE SAVEPOINT ColId
8723 				{
8724 					TransactionStmt *n = makeNode(TransactionStmt);
8725 					n->kind = TRANS_STMT_RELEASE;
8726 					n->options = list_make1(makeDefElem("savepoint_name",
8727 														(Node *)makeString($3)));
8728 					$$ = (Node *)n;
8729 				}
8730 			| RELEASE ColId
8731 				{
8732 					TransactionStmt *n = makeNode(TransactionStmt);
8733 					n->kind = TRANS_STMT_RELEASE;
8734 					n->options = list_make1(makeDefElem("savepoint_name",
8735 														(Node *)makeString($2)));
8736 					$$ = (Node *)n;
8737 				}
8738 			| ROLLBACK opt_transaction TO SAVEPOINT ColId
8739 				{
8740 					TransactionStmt *n = makeNode(TransactionStmt);
8741 					n->kind = TRANS_STMT_ROLLBACK_TO;
8742 					n->options = list_make1(makeDefElem("savepoint_name",
8743 														(Node *)makeString($5)));
8744 					$$ = (Node *)n;
8745 				}
8746 			| ROLLBACK opt_transaction TO ColId
8747 				{
8748 					TransactionStmt *n = makeNode(TransactionStmt);
8749 					n->kind = TRANS_STMT_ROLLBACK_TO;
8750 					n->options = list_make1(makeDefElem("savepoint_name",
8751 														(Node *)makeString($4)));
8752 					$$ = (Node *)n;
8753 				}
8754 			| PREPARE TRANSACTION Sconst
8755 				{
8756 					TransactionStmt *n = makeNode(TransactionStmt);
8757 					n->kind = TRANS_STMT_PREPARE;
8758 					n->gid = $3;
8759 					$$ = (Node *)n;
8760 				}
8761 			| COMMIT PREPARED Sconst
8762 				{
8763 					TransactionStmt *n = makeNode(TransactionStmt);
8764 					n->kind = TRANS_STMT_COMMIT_PREPARED;
8765 					n->gid = $3;
8766 					$$ = (Node *)n;
8767 				}
8768 			| ROLLBACK PREPARED Sconst
8769 				{
8770 					TransactionStmt *n = makeNode(TransactionStmt);
8771 					n->kind = TRANS_STMT_ROLLBACK_PREPARED;
8772 					n->gid = $3;
8773 					$$ = (Node *)n;
8774 				}
8775 		;
8776 
8777 opt_transaction:	WORK							{}
8778 			| TRANSACTION							{}
8779 			| /*EMPTY*/								{}
8780 		;
8781 
8782 transaction_mode_item:
8783 			ISOLATION LEVEL iso_level
8784 					{ $$ = makeDefElem("transaction_isolation",
8785 									   makeStringConst($3, @3)); }
8786 			| READ ONLY
8787 					{ $$ = makeDefElem("transaction_read_only",
8788 									   makeIntConst(TRUE, @1)); }
8789 			| READ WRITE
8790 					{ $$ = makeDefElem("transaction_read_only",
8791 									   makeIntConst(FALSE, @1)); }
8792 			| DEFERRABLE
8793 					{ $$ = makeDefElem("transaction_deferrable",
8794 									   makeIntConst(TRUE, @1)); }
8795 			| NOT DEFERRABLE
8796 					{ $$ = makeDefElem("transaction_deferrable",
8797 									   makeIntConst(FALSE, @1)); }
8798 		;
8799 
8800 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
8801 transaction_mode_list:
8802 			transaction_mode_item
8803 					{ $$ = list_make1($1); }
8804 			| transaction_mode_list ',' transaction_mode_item
8805 					{ $$ = lappend($1, $3); }
8806 			| transaction_mode_list transaction_mode_item
8807 					{ $$ = lappend($1, $2); }
8808 		;
8809 
8810 transaction_mode_list_or_empty:
8811 			transaction_mode_list
8812 			| /* EMPTY */
8813 					{ $$ = NIL; }
8814 		;
8815 
8816 
8817 /*****************************************************************************
8818  *
8819  *	QUERY:
8820  *		CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
8821  *			AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
8822  *
8823  *****************************************************************************/
8824 
8825 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list opt_reloptions
8826 				AS SelectStmt opt_check_option
8827 				{
8828 					ViewStmt *n = makeNode(ViewStmt);
8829 					n->view = $4;
8830 					n->view->relpersistence = $2;
8831 					n->aliases = $5;
8832 					n->query = $8;
8833 					n->replace = false;
8834 					n->options = $6;
8835 					n->withCheckOption = $9;
8836 					$$ = (Node *) n;
8837 				}
8838 		| CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list opt_reloptions
8839 				AS SelectStmt opt_check_option
8840 				{
8841 					ViewStmt *n = makeNode(ViewStmt);
8842 					n->view = $6;
8843 					n->view->relpersistence = $4;
8844 					n->aliases = $7;
8845 					n->query = $10;
8846 					n->replace = true;
8847 					n->options = $8;
8848 					n->withCheckOption = $11;
8849 					$$ = (Node *) n;
8850 				}
8851 		| CREATE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
8852 				AS SelectStmt opt_check_option
8853 				{
8854 					ViewStmt *n = makeNode(ViewStmt);
8855 					n->view = $5;
8856 					n->view->relpersistence = $2;
8857 					n->aliases = $7;
8858 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $11);
8859 					n->replace = false;
8860 					n->options = $9;
8861 					n->withCheckOption = $12;
8862 					if (n->withCheckOption != NO_CHECK_OPTION)
8863 						ereport(ERROR,
8864 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8865 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
8866 								 parser_errposition(@12)));
8867 					$$ = (Node *) n;
8868 				}
8869 		| CREATE OR REPLACE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
8870 				AS SelectStmt opt_check_option
8871 				{
8872 					ViewStmt *n = makeNode(ViewStmt);
8873 					n->view = $7;
8874 					n->view->relpersistence = $4;
8875 					n->aliases = $9;
8876 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $13);
8877 					n->replace = true;
8878 					n->options = $11;
8879 					n->withCheckOption = $14;
8880 					if (n->withCheckOption != NO_CHECK_OPTION)
8881 						ereport(ERROR,
8882 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8883 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
8884 								 parser_errposition(@14)));
8885 					$$ = (Node *) n;
8886 				}
8887 		;
8888 
8889 opt_check_option:
8890 		WITH CHECK OPTION				{ $$ = CASCADED_CHECK_OPTION; }
8891 		| WITH CASCADED CHECK OPTION	{ $$ = CASCADED_CHECK_OPTION; }
8892 		| WITH LOCAL CHECK OPTION		{ $$ = LOCAL_CHECK_OPTION; }
8893 		| /* EMPTY */					{ $$ = NO_CHECK_OPTION; }
8894 		;
8895 
8896 /*****************************************************************************
8897  *
8898  *		QUERY:
8899  *				LOAD "filename"
8900  *
8901  *****************************************************************************/
8902 
8903 LoadStmt:	LOAD file_name
8904 				{
8905 					LoadStmt *n = makeNode(LoadStmt);
8906 					n->filename = $2;
8907 					$$ = (Node *)n;
8908 				}
8909 		;
8910 
8911 
8912 /*****************************************************************************
8913  *
8914  *		CREATE DATABASE
8915  *
8916  *****************************************************************************/
8917 
8918 CreatedbStmt:
8919 			CREATE DATABASE database_name opt_with createdb_opt_list
8920 				{
8921 					CreatedbStmt *n = makeNode(CreatedbStmt);
8922 					n->dbname = $3;
8923 					n->options = $5;
8924 					$$ = (Node *)n;
8925 				}
8926 		;
8927 
8928 createdb_opt_list:
8929 			createdb_opt_items						{ $$ = $1; }
8930 			| /* EMPTY */							{ $$ = NIL; }
8931 		;
8932 
8933 createdb_opt_items:
8934 			createdb_opt_item						{ $$ = list_make1($1); }
8935 			| createdb_opt_items createdb_opt_item	{ $$ = lappend($1, $2); }
8936 		;
8937 
8938 createdb_opt_item:
8939 			createdb_opt_name opt_equal SignedIconst
8940 				{
8941 					$$ = makeDefElem($1, (Node *)makeInteger($3));
8942 				}
8943 			| createdb_opt_name opt_equal opt_boolean_or_string
8944 				{
8945 					$$ = makeDefElem($1, (Node *)makeString($3));
8946 				}
8947 			| createdb_opt_name opt_equal DEFAULT
8948 				{
8949 					$$ = makeDefElem($1, NULL);
8950 				}
8951 		;
8952 
8953 /*
8954  * Ideally we'd use ColId here, but that causes shift/reduce conflicts against
8955  * the ALTER DATABASE SET/RESET syntaxes.  Instead call out specific keywords
8956  * we need, and allow IDENT so that database option names don't have to be
8957  * parser keywords unless they are already keywords for other reasons.
8958  *
8959  * XXX this coding technique is fragile since if someone makes a formerly
8960  * non-keyword option name into a keyword and forgets to add it here, the
8961  * option will silently break.  Best defense is to provide a regression test
8962  * exercising every such option, at least at the syntax level.
8963  */
8964 createdb_opt_name:
8965 			IDENT							{ $$ = $1; }
8966 			| CONNECTION LIMIT				{ $$ = pstrdup("connection_limit"); }
8967 			| ENCODING						{ $$ = pstrdup($1); }
8968 			| LOCATION						{ $$ = pstrdup($1); }
8969 			| OWNER							{ $$ = pstrdup($1); }
8970 			| TABLESPACE					{ $$ = pstrdup($1); }
8971 			| TEMPLATE						{ $$ = pstrdup($1); }
8972 		;
8973 
8974 /*
8975  *	Though the equals sign doesn't match other WITH options, pg_dump uses
8976  *	equals for backward compatibility, and it doesn't seem worth removing it.
8977  */
8978 opt_equal:	'='										{}
8979 			| /*EMPTY*/								{}
8980 		;
8981 
8982 
8983 /*****************************************************************************
8984  *
8985  *		ALTER DATABASE
8986  *
8987  *****************************************************************************/
8988 
8989 AlterDatabaseStmt:
8990 			ALTER DATABASE database_name WITH createdb_opt_list
8991 				 {
8992 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
8993 					n->dbname = $3;
8994 					n->options = $5;
8995 					$$ = (Node *)n;
8996 				 }
8997 			| ALTER DATABASE database_name createdb_opt_list
8998 				 {
8999 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9000 					n->dbname = $3;
9001 					n->options = $4;
9002 					$$ = (Node *)n;
9003 				 }
9004 			| ALTER DATABASE database_name SET TABLESPACE name
9005 				 {
9006 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9007 					n->dbname = $3;
9008 					n->options = list_make1(makeDefElem("tablespace",
9009 													(Node *)makeString($6)));
9010 					$$ = (Node *)n;
9011 				 }
9012 		;
9013 
9014 AlterDatabaseSetStmt:
9015 			ALTER DATABASE database_name SetResetClause
9016 				{
9017 					AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
9018 					n->dbname = $3;
9019 					n->setstmt = $4;
9020 					$$ = (Node *)n;
9021 				}
9022 		;
9023 
9024 
9025 /*****************************************************************************
9026  *
9027  *		DROP DATABASE [ IF EXISTS ]
9028  *
9029  * This is implicitly CASCADE, no need for drop behavior
9030  *****************************************************************************/
9031 
9032 DropdbStmt: DROP DATABASE database_name
9033 				{
9034 					DropdbStmt *n = makeNode(DropdbStmt);
9035 					n->dbname = $3;
9036 					n->missing_ok = FALSE;
9037 					$$ = (Node *)n;
9038 				}
9039 			| DROP DATABASE IF_P EXISTS database_name
9040 				{
9041 					DropdbStmt *n = makeNode(DropdbStmt);
9042 					n->dbname = $5;
9043 					n->missing_ok = TRUE;
9044 					$$ = (Node *)n;
9045 				}
9046 		;
9047 
9048 
9049 /*****************************************************************************
9050  *
9051  *		ALTER SYSTEM
9052  *
9053  * This is used to change configuration parameters persistently.
9054  *****************************************************************************/
9055 
9056 AlterSystemStmt:
9057 			ALTER SYSTEM_P SET generic_set
9058 				{
9059 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
9060 					n->setstmt = $4;
9061 					$$ = (Node *)n;
9062 				}
9063 			| ALTER SYSTEM_P RESET generic_reset
9064 				{
9065 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
9066 					n->setstmt = $4;
9067 					$$ = (Node *)n;
9068 				}
9069 		;
9070 
9071 
9072 /*****************************************************************************
9073  *
9074  * Manipulate a domain
9075  *
9076  *****************************************************************************/
9077 
9078 CreateDomainStmt:
9079 			CREATE DOMAIN_P any_name opt_as Typename ColQualList
9080 				{
9081 					CreateDomainStmt *n = makeNode(CreateDomainStmt);
9082 					n->domainname = $3;
9083 					n->typeName = $5;
9084 					SplitColQualList($6, &n->constraints, &n->collClause,
9085 									 yyscanner);
9086 					$$ = (Node *)n;
9087 				}
9088 		;
9089 
9090 AlterDomainStmt:
9091 			/* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
9092 			ALTER DOMAIN_P any_name alter_column_default
9093 				{
9094 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9095 					n->subtype = 'T';
9096 					n->typeName = $3;
9097 					n->def = $4;
9098 					$$ = (Node *)n;
9099 				}
9100 			/* ALTER DOMAIN <domain> DROP NOT NULL */
9101 			| ALTER DOMAIN_P any_name DROP NOT NULL_P
9102 				{
9103 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9104 					n->subtype = 'N';
9105 					n->typeName = $3;
9106 					$$ = (Node *)n;
9107 				}
9108 			/* ALTER DOMAIN <domain> SET NOT NULL */
9109 			| ALTER DOMAIN_P any_name SET NOT NULL_P
9110 				{
9111 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9112 					n->subtype = 'O';
9113 					n->typeName = $3;
9114 					$$ = (Node *)n;
9115 				}
9116 			/* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
9117 			| ALTER DOMAIN_P any_name ADD_P TableConstraint
9118 				{
9119 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9120 					n->subtype = 'C';
9121 					n->typeName = $3;
9122 					n->def = $5;
9123 					$$ = (Node *)n;
9124 				}
9125 			/* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
9126 			| ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
9127 				{
9128 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9129 					n->subtype = 'X';
9130 					n->typeName = $3;
9131 					n->name = $6;
9132 					n->behavior = $7;
9133 					n->missing_ok = false;
9134 					$$ = (Node *)n;
9135 				}
9136 			/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
9137 			| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
9138 				{
9139 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9140 					n->subtype = 'X';
9141 					n->typeName = $3;
9142 					n->name = $8;
9143 					n->behavior = $9;
9144 					n->missing_ok = true;
9145 					$$ = (Node *)n;
9146 				}
9147 			/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
9148 			| ALTER DOMAIN_P any_name VALIDATE CONSTRAINT name
9149 				{
9150 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9151 					n->subtype = 'V';
9152 					n->typeName = $3;
9153 					n->name = $6;
9154 					$$ = (Node *)n;
9155 				}
9156 			;
9157 
9158 opt_as:		AS										{}
9159 			| /* EMPTY */							{}
9160 		;
9161 
9162 
9163 /*****************************************************************************
9164  *
9165  * Manipulate a text search dictionary or configuration
9166  *
9167  *****************************************************************************/
9168 
9169 AlterTSDictionaryStmt:
9170 			ALTER TEXT_P SEARCH DICTIONARY any_name definition
9171 				{
9172 					AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
9173 					n->dictname = $5;
9174 					n->options = $6;
9175 					$$ = (Node *)n;
9176 				}
9177 		;
9178 
9179 AlterTSConfigurationStmt:
9180 			ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list any_with any_name_list
9181 				{
9182 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9183 					n->kind = ALTER_TSCONFIG_ADD_MAPPING;
9184 					n->cfgname = $5;
9185 					n->tokentype = $9;
9186 					n->dicts = $11;
9187 					n->override = false;
9188 					n->replace = false;
9189 					$$ = (Node*)n;
9190 				}
9191 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list any_with any_name_list
9192 				{
9193 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9194 					n->kind = ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN;
9195 					n->cfgname = $5;
9196 					n->tokentype = $9;
9197 					n->dicts = $11;
9198 					n->override = true;
9199 					n->replace = false;
9200 					$$ = (Node*)n;
9201 				}
9202 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name any_with any_name
9203 				{
9204 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9205 					n->kind = ALTER_TSCONFIG_REPLACE_DICT;
9206 					n->cfgname = $5;
9207 					n->tokentype = NIL;
9208 					n->dicts = list_make2($9,$11);
9209 					n->override = false;
9210 					n->replace = true;
9211 					$$ = (Node*)n;
9212 				}
9213 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name any_with any_name
9214 				{
9215 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9216 					n->kind = ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN;
9217 					n->cfgname = $5;
9218 					n->tokentype = $9;
9219 					n->dicts = list_make2($11,$13);
9220 					n->override = false;
9221 					n->replace = true;
9222 					$$ = (Node*)n;
9223 				}
9224 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
9225 				{
9226 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9227 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
9228 					n->cfgname = $5;
9229 					n->tokentype = $9;
9230 					n->missing_ok = false;
9231 					$$ = (Node*)n;
9232 				}
9233 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
9234 				{
9235 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9236 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
9237 					n->cfgname = $5;
9238 					n->tokentype = $11;
9239 					n->missing_ok = true;
9240 					$$ = (Node*)n;
9241 				}
9242 		;
9243 
9244 /* Use this if TIME or ORDINALITY after WITH should be taken as an identifier */
9245 any_with:	WITH									{}
9246 			| WITH_LA								{}
9247 		;
9248 
9249 
9250 /*****************************************************************************
9251  *
9252  * Manipulate a conversion
9253  *
9254  *		CREATE [DEFAULT] CONVERSION <conversion_name>
9255  *		FOR <encoding_name> TO <encoding_name> FROM <func_name>
9256  *
9257  *****************************************************************************/
9258 
9259 CreateConversionStmt:
9260 			CREATE opt_default CONVERSION_P any_name FOR Sconst
9261 			TO Sconst FROM any_name
9262 			{
9263 				CreateConversionStmt *n = makeNode(CreateConversionStmt);
9264 				n->conversion_name = $4;
9265 				n->for_encoding_name = $6;
9266 				n->to_encoding_name = $8;
9267 				n->func_name = $10;
9268 				n->def = $2;
9269 				$$ = (Node *)n;
9270 			}
9271 		;
9272 
9273 /*****************************************************************************
9274  *
9275  *		QUERY:
9276  *				CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
9277  *				CLUSTER [VERBOSE]
9278  *				CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
9279  *
9280  *****************************************************************************/
9281 
9282 ClusterStmt:
9283 			CLUSTER opt_verbose qualified_name cluster_index_specification
9284 				{
9285 					ClusterStmt *n = makeNode(ClusterStmt);
9286 					n->relation = $3;
9287 					n->indexname = $4;
9288 					n->verbose = $2;
9289 					$$ = (Node*)n;
9290 				}
9291 			| CLUSTER opt_verbose
9292 				{
9293 					ClusterStmt *n = makeNode(ClusterStmt);
9294 					n->relation = NULL;
9295 					n->indexname = NULL;
9296 					n->verbose = $2;
9297 					$$ = (Node*)n;
9298 				}
9299 			/* kept for pre-8.3 compatibility */
9300 			| CLUSTER opt_verbose index_name ON qualified_name
9301 				{
9302 					ClusterStmt *n = makeNode(ClusterStmt);
9303 					n->relation = $5;
9304 					n->indexname = $3;
9305 					n->verbose = $2;
9306 					$$ = (Node*)n;
9307 				}
9308 		;
9309 
9310 cluster_index_specification:
9311 			USING index_name		{ $$ = $2; }
9312 			| /*EMPTY*/				{ $$ = NULL; }
9313 		;
9314 
9315 
9316 /*****************************************************************************
9317  *
9318  *		QUERY:
9319  *				VACUUM
9320  *				ANALYZE
9321  *
9322  *****************************************************************************/
9323 
9324 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
9325 				{
9326 					VacuumStmt *n = makeNode(VacuumStmt);
9327 					n->options = VACOPT_VACUUM;
9328 					if ($2)
9329 						n->options |= VACOPT_FULL;
9330 					if ($3)
9331 						n->options |= VACOPT_FREEZE;
9332 					if ($4)
9333 						n->options |= VACOPT_VERBOSE;
9334 					n->relation = NULL;
9335 					n->va_cols = NIL;
9336 					$$ = (Node *)n;
9337 				}
9338 			| VACUUM opt_full opt_freeze opt_verbose qualified_name
9339 				{
9340 					VacuumStmt *n = makeNode(VacuumStmt);
9341 					n->options = VACOPT_VACUUM;
9342 					if ($2)
9343 						n->options |= VACOPT_FULL;
9344 					if ($3)
9345 						n->options |= VACOPT_FREEZE;
9346 					if ($4)
9347 						n->options |= VACOPT_VERBOSE;
9348 					n->relation = $5;
9349 					n->va_cols = NIL;
9350 					$$ = (Node *)n;
9351 				}
9352 			| VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
9353 				{
9354 					VacuumStmt *n = (VacuumStmt *) $5;
9355 					n->options |= VACOPT_VACUUM;
9356 					if ($2)
9357 						n->options |= VACOPT_FULL;
9358 					if ($3)
9359 						n->options |= VACOPT_FREEZE;
9360 					if ($4)
9361 						n->options |= VACOPT_VERBOSE;
9362 					$$ = (Node *)n;
9363 				}
9364 			| VACUUM '(' vacuum_option_list ')'
9365 				{
9366 					VacuumStmt *n = makeNode(VacuumStmt);
9367 					n->options = VACOPT_VACUUM | $3;
9368 					n->relation = NULL;
9369 					n->va_cols = NIL;
9370 					$$ = (Node *) n;
9371 				}
9372 			| VACUUM '(' vacuum_option_list ')' qualified_name opt_name_list
9373 				{
9374 					VacuumStmt *n = makeNode(VacuumStmt);
9375 					n->options = VACOPT_VACUUM | $3;
9376 					n->relation = $5;
9377 					n->va_cols = $6;
9378 					if (n->va_cols != NIL)	/* implies analyze */
9379 						n->options |= VACOPT_ANALYZE;
9380 					$$ = (Node *) n;
9381 				}
9382 		;
9383 
9384 vacuum_option_list:
9385 			vacuum_option_elem								{ $$ = $1; }
9386 			| vacuum_option_list ',' vacuum_option_elem		{ $$ = $1 | $3; }
9387 		;
9388 
9389 vacuum_option_elem:
9390 			analyze_keyword		{ $$ = VACOPT_ANALYZE; }
9391 			| VERBOSE			{ $$ = VACOPT_VERBOSE; }
9392 			| FREEZE			{ $$ = VACOPT_FREEZE; }
9393 			| FULL				{ $$ = VACOPT_FULL; }
9394 			| IDENT
9395 				{
9396 					if (strcmp($1, "disable_page_skipping") == 0)
9397 						$$ = VACOPT_DISABLE_PAGE_SKIPPING;
9398 					else
9399 						ereport(ERROR,
9400 								(errcode(ERRCODE_SYNTAX_ERROR),
9401 							 errmsg("unrecognized VACUUM option \"%s\"", $1),
9402 									 parser_errposition(@1)));
9403 				}
9404 		;
9405 
9406 AnalyzeStmt:
9407 			analyze_keyword opt_verbose
9408 				{
9409 					VacuumStmt *n = makeNode(VacuumStmt);
9410 					n->options = VACOPT_ANALYZE;
9411 					if ($2)
9412 						n->options |= VACOPT_VERBOSE;
9413 					n->relation = NULL;
9414 					n->va_cols = NIL;
9415 					$$ = (Node *)n;
9416 				}
9417 			| analyze_keyword opt_verbose qualified_name opt_name_list
9418 				{
9419 					VacuumStmt *n = makeNode(VacuumStmt);
9420 					n->options = VACOPT_ANALYZE;
9421 					if ($2)
9422 						n->options |= VACOPT_VERBOSE;
9423 					n->relation = $3;
9424 					n->va_cols = $4;
9425 					$$ = (Node *)n;
9426 				}
9427 		;
9428 
9429 analyze_keyword:
9430 			ANALYZE									{}
9431 			| ANALYSE /* British */					{}
9432 		;
9433 
9434 opt_verbose:
9435 			VERBOSE									{ $$ = TRUE; }
9436 			| /*EMPTY*/								{ $$ = FALSE; }
9437 		;
9438 
9439 opt_full:	FULL									{ $$ = TRUE; }
9440 			| /*EMPTY*/								{ $$ = FALSE; }
9441 		;
9442 
9443 opt_freeze: FREEZE									{ $$ = TRUE; }
9444 			| /*EMPTY*/								{ $$ = FALSE; }
9445 		;
9446 
9447 opt_name_list:
9448 			'(' name_list ')'						{ $$ = $2; }
9449 			| /*EMPTY*/								{ $$ = NIL; }
9450 		;
9451 
9452 
9453 /*****************************************************************************
9454  *
9455  *		QUERY:
9456  *				EXPLAIN [ANALYZE] [VERBOSE] query
9457  *				EXPLAIN ( options ) query
9458  *
9459  *****************************************************************************/
9460 
9461 ExplainStmt:
9462 		EXPLAIN ExplainableStmt
9463 				{
9464 					ExplainStmt *n = makeNode(ExplainStmt);
9465 					n->query = $2;
9466 					n->options = NIL;
9467 					$$ = (Node *) n;
9468 				}
9469 		| EXPLAIN analyze_keyword opt_verbose ExplainableStmt
9470 				{
9471 					ExplainStmt *n = makeNode(ExplainStmt);
9472 					n->query = $4;
9473 					n->options = list_make1(makeDefElem("analyze", NULL));
9474 					if ($3)
9475 						n->options = lappend(n->options,
9476 											 makeDefElem("verbose", NULL));
9477 					$$ = (Node *) n;
9478 				}
9479 		| EXPLAIN VERBOSE ExplainableStmt
9480 				{
9481 					ExplainStmt *n = makeNode(ExplainStmt);
9482 					n->query = $3;
9483 					n->options = list_make1(makeDefElem("verbose", NULL));
9484 					$$ = (Node *) n;
9485 				}
9486 		| EXPLAIN '(' explain_option_list ')' ExplainableStmt
9487 				{
9488 					ExplainStmt *n = makeNode(ExplainStmt);
9489 					n->query = $5;
9490 					n->options = $3;
9491 					$$ = (Node *) n;
9492 				}
9493 		;
9494 
9495 ExplainableStmt:
9496 			SelectStmt
9497 			| InsertStmt
9498 			| UpdateStmt
9499 			| DeleteStmt
9500 			| DeclareCursorStmt
9501 			| CreateAsStmt
9502 			| CreateMatViewStmt
9503 			| RefreshMatViewStmt
9504 			| ExecuteStmt					/* by default all are $$=$1 */
9505 		;
9506 
9507 explain_option_list:
9508 			explain_option_elem
9509 				{
9510 					$$ = list_make1($1);
9511 				}
9512 			| explain_option_list ',' explain_option_elem
9513 				{
9514 					$$ = lappend($1, $3);
9515 				}
9516 		;
9517 
9518 explain_option_elem:
9519 			explain_option_name explain_option_arg
9520 				{
9521 					$$ = makeDefElem($1, $2);
9522 				}
9523 		;
9524 
9525 explain_option_name:
9526 			NonReservedWord			{ $$ = $1; }
9527 			| analyze_keyword		{ $$ = "analyze"; }
9528 		;
9529 
9530 explain_option_arg:
9531 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
9532 			| NumericOnly			{ $$ = (Node *) $1; }
9533 			| /* EMPTY */			{ $$ = NULL; }
9534 		;
9535 
9536 /*****************************************************************************
9537  *
9538  *		QUERY:
9539  *				PREPARE <plan_name> [(args, ...)] AS <query>
9540  *
9541  *****************************************************************************/
9542 
9543 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
9544 				{
9545 					PrepareStmt *n = makeNode(PrepareStmt);
9546 					n->name = $2;
9547 					n->argtypes = $3;
9548 					n->query = $5;
9549 					$$ = (Node *) n;
9550 				}
9551 		;
9552 
9553 prep_type_clause: '(' type_list ')'			{ $$ = $2; }
9554 				| /* EMPTY */				{ $$ = NIL; }
9555 		;
9556 
9557 PreparableStmt:
9558 			SelectStmt
9559 			| InsertStmt
9560 			| UpdateStmt
9561 			| DeleteStmt					/* by default all are $$=$1 */
9562 		;
9563 
9564 /*****************************************************************************
9565  *
9566  * EXECUTE <plan_name> [(params, ...)]
9567  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
9568  *
9569  *****************************************************************************/
9570 
9571 ExecuteStmt: EXECUTE name execute_param_clause
9572 				{
9573 					ExecuteStmt *n = makeNode(ExecuteStmt);
9574 					n->name = $2;
9575 					n->params = $3;
9576 					$$ = (Node *) n;
9577 				}
9578 			| CREATE OptTemp TABLE create_as_target AS
9579 				EXECUTE name execute_param_clause opt_with_data
9580 				{
9581 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
9582 					ExecuteStmt *n = makeNode(ExecuteStmt);
9583 					n->name = $7;
9584 					n->params = $8;
9585 					ctas->query = (Node *) n;
9586 					ctas->into = $4;
9587 					ctas->relkind = OBJECT_TABLE;
9588 					ctas->is_select_into = false;
9589 					ctas->if_not_exists = false;
9590 					/* cram additional flags into the IntoClause */
9591 					$4->rel->relpersistence = $2;
9592 					$4->skipData = !($9);
9593 					$$ = (Node *) ctas;
9594 				}
9595 			| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS
9596 				EXECUTE name execute_param_clause opt_with_data
9597 				{
9598 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
9599 					ExecuteStmt *n = makeNode(ExecuteStmt);
9600 					n->name = $10;
9601 					n->params = $11;
9602 					ctas->query = (Node *) n;
9603 					ctas->into = $7;
9604 					ctas->relkind = OBJECT_TABLE;
9605 					ctas->is_select_into = false;
9606 					ctas->if_not_exists = true;
9607 					/* cram additional flags into the IntoClause */
9608 					$7->rel->relpersistence = $2;
9609 					$7->skipData = !($12);
9610 					$$ = (Node *) ctas;
9611 				}
9612 		;
9613 
9614 execute_param_clause: '(' expr_list ')'				{ $$ = $2; }
9615 					| /* EMPTY */					{ $$ = NIL; }
9616 					;
9617 
9618 /*****************************************************************************
9619  *
9620  *		QUERY:
9621  *				DEALLOCATE [PREPARE] <plan_name>
9622  *
9623  *****************************************************************************/
9624 
9625 DeallocateStmt: DEALLOCATE name
9626 					{
9627 						DeallocateStmt *n = makeNode(DeallocateStmt);
9628 						n->name = $2;
9629 						$$ = (Node *) n;
9630 					}
9631 				| DEALLOCATE PREPARE name
9632 					{
9633 						DeallocateStmt *n = makeNode(DeallocateStmt);
9634 						n->name = $3;
9635 						$$ = (Node *) n;
9636 					}
9637 				| DEALLOCATE ALL
9638 					{
9639 						DeallocateStmt *n = makeNode(DeallocateStmt);
9640 						n->name = NULL;
9641 						$$ = (Node *) n;
9642 					}
9643 				| DEALLOCATE PREPARE ALL
9644 					{
9645 						DeallocateStmt *n = makeNode(DeallocateStmt);
9646 						n->name = NULL;
9647 						$$ = (Node *) n;
9648 					}
9649 		;
9650 
9651 /*****************************************************************************
9652  *
9653  *		QUERY:
9654  *				INSERT STATEMENTS
9655  *
9656  *****************************************************************************/
9657 
9658 InsertStmt:
9659 			opt_with_clause INSERT INTO insert_target insert_rest
9660 			opt_on_conflict returning_clause
9661 				{
9662 					$5->relation = $4;
9663 					$5->onConflictClause = $6;
9664 					$5->returningList = $7;
9665 					$5->withClause = $1;
9666 					$$ = (Node *) $5;
9667 				}
9668 		;
9669 
9670 /*
9671  * Can't easily make AS optional here, because VALUES in insert_rest would
9672  * have a shift/reduce conflict with VALUES as an optional alias.  We could
9673  * easily allow unreserved_keywords as optional aliases, but that'd be an odd
9674  * divergence from other places.  So just require AS for now.
9675  */
9676 insert_target:
9677 			qualified_name
9678 				{
9679 					$$ = $1;
9680 				}
9681 			| qualified_name AS ColId
9682 				{
9683 					$1->alias = makeAlias($3, NIL);
9684 					$$ = $1;
9685 				}
9686 		;
9687 
9688 insert_rest:
9689 			SelectStmt
9690 				{
9691 					$$ = makeNode(InsertStmt);
9692 					$$->cols = NIL;
9693 					$$->selectStmt = $1;
9694 				}
9695 			| '(' insert_column_list ')' SelectStmt
9696 				{
9697 					$$ = makeNode(InsertStmt);
9698 					$$->cols = $2;
9699 					$$->selectStmt = $4;
9700 				}
9701 			| DEFAULT VALUES
9702 				{
9703 					$$ = makeNode(InsertStmt);
9704 					$$->cols = NIL;
9705 					$$->selectStmt = NULL;
9706 				}
9707 		;
9708 
9709 insert_column_list:
9710 			insert_column_item
9711 					{ $$ = list_make1($1); }
9712 			| insert_column_list ',' insert_column_item
9713 					{ $$ = lappend($1, $3); }
9714 		;
9715 
9716 insert_column_item:
9717 			ColId opt_indirection
9718 				{
9719 					$$ = makeNode(ResTarget);
9720 					$$->name = $1;
9721 					$$->indirection = check_indirection($2, yyscanner);
9722 					$$->val = NULL;
9723 					$$->location = @1;
9724 				}
9725 		;
9726 
9727 opt_on_conflict:
9728 			ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list	where_clause
9729 				{
9730 					$$ = makeNode(OnConflictClause);
9731 					$$->action = ONCONFLICT_UPDATE;
9732 					$$->infer = $3;
9733 					$$->targetList = $7;
9734 					$$->whereClause = $8;
9735 					$$->location = @1;
9736 				}
9737 			|
9738 			ON CONFLICT opt_conf_expr DO NOTHING
9739 				{
9740 					$$ = makeNode(OnConflictClause);
9741 					$$->action = ONCONFLICT_NOTHING;
9742 					$$->infer = $3;
9743 					$$->targetList = NIL;
9744 					$$->whereClause = NULL;
9745 					$$->location = @1;
9746 				}
9747 			| /*EMPTY*/
9748 				{
9749 					$$ = NULL;
9750 				}
9751 		;
9752 
9753 opt_conf_expr:
9754 			'(' index_params ')' where_clause
9755 				{
9756 					$$ = makeNode(InferClause);
9757 					$$->indexElems = $2;
9758 					$$->whereClause = $4;
9759 					$$->conname = NULL;
9760 					$$->location = @1;
9761 				}
9762 			|
9763 			ON CONSTRAINT name
9764 				{
9765 					$$ = makeNode(InferClause);
9766 					$$->indexElems = NIL;
9767 					$$->whereClause = NULL;
9768 					$$->conname = $3;
9769 					$$->location = @1;
9770 				}
9771 			| /*EMPTY*/
9772 				{
9773 					$$ = NULL;
9774 				}
9775 		;
9776 
9777 returning_clause:
9778 			RETURNING target_list		{ $$ = $2; }
9779 			| /* EMPTY */				{ $$ = NIL; }
9780 		;
9781 
9782 
9783 /*****************************************************************************
9784  *
9785  *		QUERY:
9786  *				DELETE STATEMENTS
9787  *
9788  *****************************************************************************/
9789 
9790 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
9791 			using_clause where_or_current_clause returning_clause
9792 				{
9793 					DeleteStmt *n = makeNode(DeleteStmt);
9794 					n->relation = $4;
9795 					n->usingClause = $5;
9796 					n->whereClause = $6;
9797 					n->returningList = $7;
9798 					n->withClause = $1;
9799 					$$ = (Node *)n;
9800 				}
9801 		;
9802 
9803 using_clause:
9804 				USING from_list						{ $$ = $2; }
9805 			| /*EMPTY*/								{ $$ = NIL; }
9806 		;
9807 
9808 
9809 /*****************************************************************************
9810  *
9811  *		QUERY:
9812  *				LOCK TABLE
9813  *
9814  *****************************************************************************/
9815 
9816 LockStmt:	LOCK_P opt_table relation_expr_list opt_lock opt_nowait
9817 				{
9818 					LockStmt *n = makeNode(LockStmt);
9819 
9820 					n->relations = $3;
9821 					n->mode = $4;
9822 					n->nowait = $5;
9823 					$$ = (Node *)n;
9824 				}
9825 		;
9826 
9827 opt_lock:	IN_P lock_type MODE				{ $$ = $2; }
9828 			| /*EMPTY*/						{ $$ = AccessExclusiveLock; }
9829 		;
9830 
9831 lock_type:	ACCESS SHARE					{ $$ = AccessShareLock; }
9832 			| ROW SHARE						{ $$ = RowShareLock; }
9833 			| ROW EXCLUSIVE					{ $$ = RowExclusiveLock; }
9834 			| SHARE UPDATE EXCLUSIVE		{ $$ = ShareUpdateExclusiveLock; }
9835 			| SHARE							{ $$ = ShareLock; }
9836 			| SHARE ROW EXCLUSIVE			{ $$ = ShareRowExclusiveLock; }
9837 			| EXCLUSIVE						{ $$ = ExclusiveLock; }
9838 			| ACCESS EXCLUSIVE				{ $$ = AccessExclusiveLock; }
9839 		;
9840 
9841 opt_nowait:	NOWAIT							{ $$ = TRUE; }
9842 			| /*EMPTY*/						{ $$ = FALSE; }
9843 		;
9844 
9845 opt_nowait_or_skip:
9846 			NOWAIT							{ $$ = LockWaitError; }
9847 			| SKIP LOCKED					{ $$ = LockWaitSkip; }
9848 			| /*EMPTY*/						{ $$ = LockWaitBlock; }
9849 		;
9850 
9851 
9852 /*****************************************************************************
9853  *
9854  *		QUERY:
9855  *				UpdateStmt (UPDATE)
9856  *
9857  *****************************************************************************/
9858 
9859 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
9860 			SET set_clause_list
9861 			from_clause
9862 			where_or_current_clause
9863 			returning_clause
9864 				{
9865 					UpdateStmt *n = makeNode(UpdateStmt);
9866 					n->relation = $3;
9867 					n->targetList = $5;
9868 					n->fromClause = $6;
9869 					n->whereClause = $7;
9870 					n->returningList = $8;
9871 					n->withClause = $1;
9872 					$$ = (Node *)n;
9873 				}
9874 		;
9875 
9876 set_clause_list:
9877 			set_clause							{ $$ = $1; }
9878 			| set_clause_list ',' set_clause	{ $$ = list_concat($1,$3); }
9879 		;
9880 
9881 set_clause:
9882 			single_set_clause						{ $$ = list_make1($1); }
9883 			| multiple_set_clause					{ $$ = $1; }
9884 		;
9885 
9886 single_set_clause:
9887 			set_target '=' ctext_expr
9888 				{
9889 					$$ = $1;
9890 					$$->val = (Node *) $3;
9891 				}
9892 		;
9893 
9894 /*
9895  * Ideally, we'd accept any row-valued a_expr as RHS of a multiple_set_clause.
9896  * However, per SQL spec the row-constructor case must allow DEFAULT as a row
9897  * member, and it's pretty unclear how to do that (unless perhaps we allow
9898  * DEFAULT in any a_expr and let parse analysis sort it out later?).  For the
9899  * moment, the planner/executor only support a subquery as a multiassignment
9900  * source anyhow, so we need only accept ctext_row and subqueries here.
9901  */
9902 multiple_set_clause:
9903 			'(' set_target_list ')' '=' ctext_row
9904 				{
9905 					ListCell *col_cell;
9906 					ListCell *val_cell;
9907 
9908 					/*
9909 					 * Break the ctext_row apart, merge individual expressions
9910 					 * into the destination ResTargets.  This is semantically
9911 					 * equivalent to, and much cheaper to process than, the
9912 					 * general case.
9913 					 */
9914 					if (list_length($2) != list_length($5))
9915 						ereport(ERROR,
9916 								(errcode(ERRCODE_SYNTAX_ERROR),
9917 								 errmsg("number of columns does not match number of values"),
9918 								 parser_errposition(@5)));
forboth(col_cell,$2,val_cell,$5)9919 					forboth(col_cell, $2, val_cell, $5)
9920 					{
9921 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
9922 						Node *res_val = (Node *) lfirst(val_cell);
9923 
9924 						res_col->val = res_val;
9925 					}
9926 
9927 					$$ = $2;
9928 				}
9929 			| '(' set_target_list ')' '=' select_with_parens
9930 				{
9931 					SubLink *sl = makeNode(SubLink);
9932 					int ncolumns = list_length($2);
9933 					int i = 1;
9934 					ListCell *col_cell;
9935 
9936 					/* First, convert bare SelectStmt into a SubLink */
9937 					sl->subLinkType = MULTIEXPR_SUBLINK;
9938 					sl->subLinkId = 0;		/* will be assigned later */
9939 					sl->testexpr = NULL;
9940 					sl->operName = NIL;
9941 					sl->subselect = $5;
9942 					sl->location = @5;
9943 
9944 					/* Create a MultiAssignRef source for each target */
foreach(col_cell,$2)9945 					foreach(col_cell, $2)
9946 					{
9947 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
9948 						MultiAssignRef *r = makeNode(MultiAssignRef);
9949 
9950 						r->source = (Node *) sl;
9951 						r->colno = i;
9952 						r->ncolumns = ncolumns;
9953 						res_col->val = (Node *) r;
9954 						i++;
9955 					}
9956 
9957 					$$ = $2;
9958 				}
9959 		;
9960 
9961 set_target:
9962 			ColId opt_indirection
9963 				{
9964 					$$ = makeNode(ResTarget);
9965 					$$->name = $1;
9966 					$$->indirection = check_indirection($2, yyscanner);
9967 					$$->val = NULL;	/* upper production sets this */
9968 					$$->location = @1;
9969 				}
9970 		;
9971 
9972 set_target_list:
9973 			set_target								{ $$ = list_make1($1); }
9974 			| set_target_list ',' set_target		{ $$ = lappend($1,$3); }
9975 		;
9976 
9977 
9978 /*****************************************************************************
9979  *
9980  *		QUERY:
9981  *				CURSOR STATEMENTS
9982  *
9983  *****************************************************************************/
9984 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
9985 				{
9986 					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
9987 					n->portalname = $2;
9988 					/* currently we always set FAST_PLAN option */
9989 					n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
9990 					n->query = $7;
9991 					$$ = (Node *)n;
9992 				}
9993 		;
9994 
9995 cursor_name:	name						{ $$ = $1; }
9996 		;
9997 
9998 cursor_options: /*EMPTY*/					{ $$ = 0; }
9999 			| cursor_options NO SCROLL		{ $$ = $1 | CURSOR_OPT_NO_SCROLL; }
10000 			| cursor_options SCROLL			{ $$ = $1 | CURSOR_OPT_SCROLL; }
10001 			| cursor_options BINARY			{ $$ = $1 | CURSOR_OPT_BINARY; }
10002 			| cursor_options INSENSITIVE	{ $$ = $1 | CURSOR_OPT_INSENSITIVE; }
10003 		;
10004 
10005 opt_hold: /* EMPTY */						{ $$ = 0; }
10006 			| WITH HOLD						{ $$ = CURSOR_OPT_HOLD; }
10007 			| WITHOUT HOLD					{ $$ = 0; }
10008 		;
10009 
10010 /*****************************************************************************
10011  *
10012  *		QUERY:
10013  *				SELECT STATEMENTS
10014  *
10015  *****************************************************************************/
10016 
10017 /* A complete SELECT statement looks like this.
10018  *
10019  * The rule returns either a single SelectStmt node or a tree of them,
10020  * representing a set-operation tree.
10021  *
10022  * There is an ambiguity when a sub-SELECT is within an a_expr and there
10023  * are excess parentheses: do the parentheses belong to the sub-SELECT or
10024  * to the surrounding a_expr?  We don't really care, but bison wants to know.
10025  * To resolve the ambiguity, we are careful to define the grammar so that
10026  * the decision is staved off as long as possible: as long as we can keep
10027  * absorbing parentheses into the sub-SELECT, we will do so, and only when
10028  * it's no longer possible to do that will we decide that parens belong to
10029  * the expression.	For example, in "SELECT (((SELECT 2)) + 3)" the extra
10030  * parentheses are treated as part of the sub-select.  The necessity of doing
10031  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".	Had we
10032  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
10033  * SELECT viewpoint when we see the UNION.
10034  *
10035  * This approach is implemented by defining a nonterminal select_with_parens,
10036  * which represents a SELECT with at least one outer layer of parentheses,
10037  * and being careful to use select_with_parens, never '(' SelectStmt ')',
10038  * in the expression grammar.  We will then have shift-reduce conflicts
10039  * which we can resolve in favor of always treating '(' <select> ')' as
10040  * a select_with_parens.  To resolve the conflicts, the productions that
10041  * conflict with the select_with_parens productions are manually given
10042  * precedences lower than the precedence of ')', thereby ensuring that we
10043  * shift ')' (and then reduce to select_with_parens) rather than trying to
10044  * reduce the inner <select> nonterminal to something else.  We use UMINUS
10045  * precedence for this, which is a fairly arbitrary choice.
10046  *
10047  * To be able to define select_with_parens itself without ambiguity, we need
10048  * a nonterminal select_no_parens that represents a SELECT structure with no
10049  * outermost parentheses.  This is a little bit tedious, but it works.
10050  *
10051  * In non-expression contexts, we use SelectStmt which can represent a SELECT
10052  * with or without outer parentheses.
10053  */
10054 
10055 SelectStmt: select_no_parens			%prec UMINUS
10056 			| select_with_parens		%prec UMINUS
10057 		;
10058 
10059 select_with_parens:
10060 			'(' select_no_parens ')'				{ $$ = $2; }
10061 			| '(' select_with_parens ')'			{ $$ = $2; }
10062 		;
10063 
10064 /*
10065  * This rule parses the equivalent of the standard's <query expression>.
10066  * The duplicative productions are annoying, but hard to get rid of without
10067  * creating shift/reduce conflicts.
10068  *
10069  *	The locking clause (FOR UPDATE etc) may be before or after LIMIT/OFFSET.
10070  *	In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
10071  *	We now support both orderings, but prefer LIMIT/OFFSET before the locking
10072  * clause.
10073  *	2002-08-28 bjm
10074  */
10075 select_no_parens:
10076 			simple_select						{ $$ = $1; }
10077 			| select_clause sort_clause
10078 				{
10079 					insertSelectOptions((SelectStmt *) $1, $2, NIL,
10080 										NULL, NULL, NULL,
10081 										yyscanner);
10082 					$$ = $1;
10083 				}
10084 			| select_clause opt_sort_clause for_locking_clause opt_select_limit
10085 				{
10086 					insertSelectOptions((SelectStmt *) $1, $2, $3,
10087 										list_nth($4, 0), list_nth($4, 1),
10088 										NULL,
10089 										yyscanner);
10090 					$$ = $1;
10091 				}
10092 			| select_clause opt_sort_clause select_limit opt_for_locking_clause
10093 				{
10094 					insertSelectOptions((SelectStmt *) $1, $2, $4,
10095 										list_nth($3, 0), list_nth($3, 1),
10096 										NULL,
10097 										yyscanner);
10098 					$$ = $1;
10099 				}
10100 			| with_clause select_clause
10101 				{
10102 					insertSelectOptions((SelectStmt *) $2, NULL, NIL,
10103 										NULL, NULL,
10104 										$1,
10105 										yyscanner);
10106 					$$ = $2;
10107 				}
10108 			| with_clause select_clause sort_clause
10109 				{
10110 					insertSelectOptions((SelectStmt *) $2, $3, NIL,
10111 										NULL, NULL,
10112 										$1,
10113 										yyscanner);
10114 					$$ = $2;
10115 				}
10116 			| with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
10117 				{
10118 					insertSelectOptions((SelectStmt *) $2, $3, $4,
10119 										list_nth($5, 0), list_nth($5, 1),
10120 										$1,
10121 										yyscanner);
10122 					$$ = $2;
10123 				}
10124 			| with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
10125 				{
10126 					insertSelectOptions((SelectStmt *) $2, $3, $5,
10127 										list_nth($4, 0), list_nth($4, 1),
10128 										$1,
10129 										yyscanner);
10130 					$$ = $2;
10131 				}
10132 		;
10133 
10134 select_clause:
10135 			simple_select							{ $$ = $1; }
10136 			| select_with_parens					{ $$ = $1; }
10137 		;
10138 
10139 /*
10140  * This rule parses SELECT statements that can appear within set operations,
10141  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
10142  * the ordering of the set operations.	Without '(' and ')' we want the
10143  * operations to be ordered per the precedence specs at the head of this file.
10144  *
10145  * As with select_no_parens, simple_select cannot have outer parentheses,
10146  * but can have parenthesized subclauses.
10147  *
10148  * Note that sort clauses cannot be included at this level --- SQL requires
10149  *		SELECT foo UNION SELECT bar ORDER BY baz
10150  * to be parsed as
10151  *		(SELECT foo UNION SELECT bar) ORDER BY baz
10152  * not
10153  *		SELECT foo UNION (SELECT bar ORDER BY baz)
10154  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
10155  * described as part of the select_no_parens production, not simple_select.
10156  * This does not limit functionality, because you can reintroduce these
10157  * clauses inside parentheses.
10158  *
10159  * NOTE: only the leftmost component SelectStmt should have INTO.
10160  * However, this is not checked by the grammar; parse analysis must check it.
10161  */
10162 simple_select:
10163 			SELECT opt_all_clause opt_target_list
10164 			into_clause from_clause where_clause
10165 			group_clause having_clause window_clause
10166 				{
10167 					SelectStmt *n = makeNode(SelectStmt);
10168 					n->targetList = $3;
10169 					n->intoClause = $4;
10170 					n->fromClause = $5;
10171 					n->whereClause = $6;
10172 					n->groupClause = $7;
10173 					n->havingClause = $8;
10174 					n->windowClause = $9;
10175 					$$ = (Node *)n;
10176 				}
10177 			| SELECT distinct_clause target_list
10178 			into_clause from_clause where_clause
10179 			group_clause having_clause window_clause
10180 				{
10181 					SelectStmt *n = makeNode(SelectStmt);
10182 					n->distinctClause = $2;
10183 					n->targetList = $3;
10184 					n->intoClause = $4;
10185 					n->fromClause = $5;
10186 					n->whereClause = $6;
10187 					n->groupClause = $7;
10188 					n->havingClause = $8;
10189 					n->windowClause = $9;
10190 					$$ = (Node *)n;
10191 				}
10192 			| values_clause							{ $$ = $1; }
10193 			| TABLE relation_expr
10194 				{
10195 					/* same as SELECT * FROM relation_expr */
10196 					ColumnRef *cr = makeNode(ColumnRef);
10197 					ResTarget *rt = makeNode(ResTarget);
10198 					SelectStmt *n = makeNode(SelectStmt);
10199 
10200 					cr->fields = list_make1(makeNode(A_Star));
10201 					cr->location = -1;
10202 
10203 					rt->name = NULL;
10204 					rt->indirection = NIL;
10205 					rt->val = (Node *)cr;
10206 					rt->location = -1;
10207 
10208 					n->targetList = list_make1(rt);
10209 					n->fromClause = list_make1($2);
10210 					$$ = (Node *)n;
10211 				}
10212 			| select_clause UNION all_or_distinct select_clause
10213 				{
10214 					$$ = makeSetOp(SETOP_UNION, $3, $1, $4);
10215 				}
10216 			| select_clause INTERSECT all_or_distinct select_clause
10217 				{
10218 					$$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
10219 				}
10220 			| select_clause EXCEPT all_or_distinct select_clause
10221 				{
10222 					$$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
10223 				}
10224 		;
10225 
10226 /*
10227  * SQL standard WITH clause looks like:
10228  *
10229  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
10230  *		AS (query) [ SEARCH or CYCLE clause ]
10231  *
10232  * We don't currently support the SEARCH or CYCLE clause.
10233  *
10234  * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY.
10235  */
10236 with_clause:
10237 		WITH cte_list
10238 			{
10239 				$$ = makeNode(WithClause);
10240 				$$->ctes = $2;
10241 				$$->recursive = false;
10242 				$$->location = @1;
10243 			}
10244 		| WITH_LA cte_list
10245 			{
10246 				$$ = makeNode(WithClause);
10247 				$$->ctes = $2;
10248 				$$->recursive = false;
10249 				$$->location = @1;
10250 			}
10251 		| WITH RECURSIVE cte_list
10252 			{
10253 				$$ = makeNode(WithClause);
10254 				$$->ctes = $3;
10255 				$$->recursive = true;
10256 				$$->location = @1;
10257 			}
10258 		;
10259 
10260 cte_list:
10261 		common_table_expr						{ $$ = list_make1($1); }
10262 		| cte_list ',' common_table_expr		{ $$ = lappend($1, $3); }
10263 		;
10264 
10265 common_table_expr:  name opt_name_list AS '(' PreparableStmt ')'
10266 			{
10267 				CommonTableExpr *n = makeNode(CommonTableExpr);
10268 				n->ctename = $1;
10269 				n->aliascolnames = $2;
10270 				n->ctequery = $5;
10271 				n->location = @1;
10272 				$$ = (Node *) n;
10273 			}
10274 		;
10275 
10276 opt_with_clause:
10277 		with_clause								{ $$ = $1; }
10278 		| /*EMPTY*/								{ $$ = NULL; }
10279 		;
10280 
10281 into_clause:
10282 			INTO OptTempTableName
10283 				{
10284 					$$ = makeNode(IntoClause);
10285 					$$->rel = $2;
10286 					$$->colNames = NIL;
10287 					$$->options = NIL;
10288 					$$->onCommit = ONCOMMIT_NOOP;
10289 					$$->tableSpaceName = NULL;
10290 					$$->viewQuery = NULL;
10291 					$$->skipData = false;
10292 				}
10293 			| /*EMPTY*/
10294 				{ $$ = NULL; }
10295 		;
10296 
10297 /*
10298  * Redundancy here is needed to avoid shift/reduce conflicts,
10299  * since TEMP is not a reserved word.  See also OptTemp.
10300  */
10301 OptTempTableName:
10302 			TEMPORARY opt_table qualified_name
10303 				{
10304 					$$ = $3;
10305 					$$->relpersistence = RELPERSISTENCE_TEMP;
10306 				}
10307 			| TEMP opt_table qualified_name
10308 				{
10309 					$$ = $3;
10310 					$$->relpersistence = RELPERSISTENCE_TEMP;
10311 				}
10312 			| LOCAL TEMPORARY opt_table qualified_name
10313 				{
10314 					$$ = $4;
10315 					$$->relpersistence = RELPERSISTENCE_TEMP;
10316 				}
10317 			| LOCAL TEMP opt_table qualified_name
10318 				{
10319 					$$ = $4;
10320 					$$->relpersistence = RELPERSISTENCE_TEMP;
10321 				}
10322 			| GLOBAL TEMPORARY opt_table qualified_name
10323 				{
10324 					ereport(WARNING,
10325 							(errmsg("GLOBAL is deprecated in temporary table creation"),
10326 							 parser_errposition(@1)));
10327 					$$ = $4;
10328 					$$->relpersistence = RELPERSISTENCE_TEMP;
10329 				}
10330 			| GLOBAL TEMP opt_table qualified_name
10331 				{
10332 					ereport(WARNING,
10333 							(errmsg("GLOBAL is deprecated in temporary table creation"),
10334 							 parser_errposition(@1)));
10335 					$$ = $4;
10336 					$$->relpersistence = RELPERSISTENCE_TEMP;
10337 				}
10338 			| UNLOGGED opt_table qualified_name
10339 				{
10340 					$$ = $3;
10341 					$$->relpersistence = RELPERSISTENCE_UNLOGGED;
10342 				}
10343 			| TABLE qualified_name
10344 				{
10345 					$$ = $2;
10346 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
10347 				}
10348 			| qualified_name
10349 				{
10350 					$$ = $1;
10351 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
10352 				}
10353 		;
10354 
10355 opt_table:	TABLE									{}
10356 			| /*EMPTY*/								{}
10357 		;
10358 
10359 all_or_distinct:
10360 			ALL										{ $$ = TRUE; }
10361 			| DISTINCT								{ $$ = FALSE; }
10362 			| /*EMPTY*/								{ $$ = FALSE; }
10363 		;
10364 
10365 /* We use (NIL) as a placeholder to indicate that all target expressions
10366  * should be placed in the DISTINCT list during parsetree analysis.
10367  */
10368 distinct_clause:
10369 			DISTINCT								{ $$ = list_make1(NIL); }
10370 			| DISTINCT ON '(' expr_list ')'			{ $$ = $4; }
10371 		;
10372 
10373 opt_all_clause:
10374 			ALL										{ $$ = NIL;}
10375 			| /*EMPTY*/								{ $$ = NIL; }
10376 		;
10377 
10378 opt_sort_clause:
10379 			sort_clause								{ $$ = $1;}
10380 			| /*EMPTY*/								{ $$ = NIL; }
10381 		;
10382 
10383 sort_clause:
10384 			ORDER BY sortby_list					{ $$ = $3; }
10385 		;
10386 
10387 sortby_list:
10388 			sortby									{ $$ = list_make1($1); }
10389 			| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
10390 		;
10391 
10392 sortby:		a_expr USING qual_all_Op opt_nulls_order
10393 				{
10394 					$$ = makeNode(SortBy);
10395 					$$->node = $1;
10396 					$$->sortby_dir = SORTBY_USING;
10397 					$$->sortby_nulls = $4;
10398 					$$->useOp = $3;
10399 					$$->location = @3;
10400 				}
10401 			| a_expr opt_asc_desc opt_nulls_order
10402 				{
10403 					$$ = makeNode(SortBy);
10404 					$$->node = $1;
10405 					$$->sortby_dir = $2;
10406 					$$->sortby_nulls = $3;
10407 					$$->useOp = NIL;
10408 					$$->location = -1;		/* no operator */
10409 				}
10410 		;
10411 
10412 
10413 select_limit:
10414 			limit_clause offset_clause			{ $$ = list_make2($2, $1); }
10415 			| offset_clause limit_clause		{ $$ = list_make2($1, $2); }
10416 			| limit_clause						{ $$ = list_make2(NULL, $1); }
10417 			| offset_clause						{ $$ = list_make2($1, NULL); }
10418 		;
10419 
10420 opt_select_limit:
10421 			select_limit						{ $$ = $1; }
10422 			| /* EMPTY */						{ $$ = list_make2(NULL,NULL); }
10423 		;
10424 
10425 limit_clause:
10426 			LIMIT select_limit_value
10427 				{ $$ = $2; }
10428 			| LIMIT select_limit_value ',' select_offset_value
10429 				{
10430 					/* Disabled because it was too confusing, bjm 2002-02-18 */
10431 					ereport(ERROR,
10432 							(errcode(ERRCODE_SYNTAX_ERROR),
10433 							 errmsg("LIMIT #,# syntax is not supported"),
10434 							 errhint("Use separate LIMIT and OFFSET clauses."),
10435 							 parser_errposition(@1)));
10436 				}
10437 			/* SQL:2008 syntax */
10438 			/* to avoid shift/reduce conflicts, handle the optional value with
10439 			 * a separate production rather than an opt_ expression.  The fact
10440 			 * that ONLY is fully reserved means that this way, we defer any
10441 			 * decision about what rule reduces ROW or ROWS to the point where
10442 			 * we can see the ONLY token in the lookahead slot.
10443 			 */
10444 			| FETCH first_or_next select_fetch_first_value row_or_rows ONLY
10445 				{ $$ = $3; }
10446 			| FETCH first_or_next row_or_rows ONLY
10447 				{ $$ = makeIntConst(1, -1); }
10448 		;
10449 
10450 offset_clause:
10451 			OFFSET select_offset_value
10452 				{ $$ = $2; }
10453 			/* SQL:2008 syntax */
10454 			| OFFSET select_fetch_first_value row_or_rows
10455 				{ $$ = $2; }
10456 		;
10457 
10458 select_limit_value:
10459 			a_expr									{ $$ = $1; }
10460 			| ALL
10461 				{
10462 					/* LIMIT ALL is represented as a NULL constant */
10463 					$$ = makeNullAConst(@1);
10464 				}
10465 		;
10466 
10467 select_offset_value:
10468 			a_expr									{ $$ = $1; }
10469 		;
10470 
10471 /*
10472  * Allowing full expressions without parentheses causes various parsing
10473  * problems with the trailing ROW/ROWS key words.  SQL spec only calls for
10474  * <simple value specification>, which is either a literal or a parameter (but
10475  * an <SQL parameter reference> could be an identifier, bringing up conflicts
10476  * with ROW/ROWS). We solve this by leveraging the presence of ONLY (see above)
10477  * to determine whether the expression is missing rather than trying to make it
10478  * optional in this rule.
10479  *
10480  * c_expr covers almost all the spec-required cases (and more), but it doesn't
10481  * cover signed numeric literals, which are allowed by the spec. So we include
10482  * those here explicitly. We need FCONST as well as ICONST because values that
10483  * don't fit in the platform's "long", but do fit in bigint, should still be
10484  * accepted here. (This is possible in 64-bit Windows as well as all 32-bit
10485  * builds.)
10486  */
10487 select_fetch_first_value:
10488 			c_expr									{ $$ = $1; }
10489 			| '+' I_or_F_const
10490 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
10491 			| '-' I_or_F_const
10492 				{ $$ = doNegate($2, @1); }
10493 		;
10494 
10495 I_or_F_const:
10496 			Iconst									{ $$ = makeIntConst($1,@1); }
10497 			| FCONST								{ $$ = makeFloatConst($1,@1); }
10498 		;
10499 
10500 /* noise words */
10501 row_or_rows: ROW									{ $$ = 0; }
10502 			| ROWS									{ $$ = 0; }
10503 		;
10504 
10505 first_or_next: FIRST_P								{ $$ = 0; }
10506 			| NEXT									{ $$ = 0; }
10507 		;
10508 
10509 
10510 /*
10511  * This syntax for group_clause tries to follow the spec quite closely.
10512  * However, the spec allows only column references, not expressions,
10513  * which introduces an ambiguity between implicit row constructors
10514  * (a,b) and lists of column references.
10515  *
10516  * We handle this by using the a_expr production for what the spec calls
10517  * <ordinary grouping set>, which in the spec represents either one column
10518  * reference or a parenthesized list of column references. Then, we check the
10519  * top node of the a_expr to see if it's an implicit RowExpr, and if so, just
10520  * grab and use the list, discarding the node. (this is done in parse analysis,
10521  * not here)
10522  *
10523  * (we abuse the row_format field of RowExpr to distinguish implicit and
10524  * explicit row constructors; it's debatable if anyone sanely wants to use them
10525  * in a group clause, but if they have a reason to, we make it possible.)
10526  *
10527  * Each item in the group_clause list is either an expression tree or a
10528  * GroupingSet node of some type.
10529  */
10530 group_clause:
10531 			GROUP_P BY group_by_list				{ $$ = $3; }
10532 			| /*EMPTY*/								{ $$ = NIL; }
10533 		;
10534 
10535 group_by_list:
10536 			group_by_item							{ $$ = list_make1($1); }
10537 			| group_by_list ',' group_by_item		{ $$ = lappend($1,$3); }
10538 		;
10539 
10540 group_by_item:
10541 			a_expr									{ $$ = $1; }
10542 			| empty_grouping_set					{ $$ = $1; }
10543 			| cube_clause							{ $$ = $1; }
10544 			| rollup_clause							{ $$ = $1; }
10545 			| grouping_sets_clause					{ $$ = $1; }
10546 		;
10547 
10548 empty_grouping_set:
10549 			'(' ')'
10550 				{
10551 					$$ = (Node *) makeGroupingSet(GROUPING_SET_EMPTY, NIL, @1);
10552 				}
10553 		;
10554 
10555 /*
10556  * These hacks rely on setting precedence of CUBE and ROLLUP below that of '(',
10557  * so that they shift in these rules rather than reducing the conflicting
10558  * unreserved_keyword rule.
10559  */
10560 
10561 rollup_clause:
10562 			ROLLUP '(' expr_list ')'
10563 				{
10564 					$$ = (Node *) makeGroupingSet(GROUPING_SET_ROLLUP, $3, @1);
10565 				}
10566 		;
10567 
10568 cube_clause:
10569 			CUBE '(' expr_list ')'
10570 				{
10571 					$$ = (Node *) makeGroupingSet(GROUPING_SET_CUBE, $3, @1);
10572 				}
10573 		;
10574 
10575 grouping_sets_clause:
10576 			GROUPING SETS '(' group_by_list ')'
10577 				{
10578 					$$ = (Node *) makeGroupingSet(GROUPING_SET_SETS, $4, @1);
10579 				}
10580 		;
10581 
10582 having_clause:
10583 			HAVING a_expr							{ $$ = $2; }
10584 			| /*EMPTY*/								{ $$ = NULL; }
10585 		;
10586 
10587 for_locking_clause:
10588 			for_locking_items						{ $$ = $1; }
10589 			| FOR READ ONLY							{ $$ = NIL; }
10590 		;
10591 
10592 opt_for_locking_clause:
10593 			for_locking_clause						{ $$ = $1; }
10594 			| /* EMPTY */							{ $$ = NIL; }
10595 		;
10596 
10597 for_locking_items:
10598 			for_locking_item						{ $$ = list_make1($1); }
10599 			| for_locking_items for_locking_item	{ $$ = lappend($1, $2); }
10600 		;
10601 
10602 for_locking_item:
10603 			for_locking_strength locked_rels_list opt_nowait_or_skip
10604 				{
10605 					LockingClause *n = makeNode(LockingClause);
10606 					n->lockedRels = $2;
10607 					n->strength = $1;
10608 					n->waitPolicy = $3;
10609 					$$ = (Node *) n;
10610 				}
10611 		;
10612 
10613 for_locking_strength:
10614 			FOR UPDATE 							{ $$ = LCS_FORUPDATE; }
10615 			| FOR NO KEY UPDATE 				{ $$ = LCS_FORNOKEYUPDATE; }
10616 			| FOR SHARE 						{ $$ = LCS_FORSHARE; }
10617 			| FOR KEY SHARE 					{ $$ = LCS_FORKEYSHARE; }
10618 		;
10619 
10620 locked_rels_list:
10621 			OF qualified_name_list					{ $$ = $2; }
10622 			| /* EMPTY */							{ $$ = NIL; }
10623 		;
10624 
10625 
10626 values_clause:
10627 			VALUES ctext_row
10628 				{
10629 					SelectStmt *n = makeNode(SelectStmt);
10630 					n->valuesLists = list_make1($2);
10631 					$$ = (Node *) n;
10632 				}
10633 			| values_clause ',' ctext_row
10634 				{
10635 					SelectStmt *n = (SelectStmt *) $1;
10636 					n->valuesLists = lappend(n->valuesLists, $3);
10637 					$$ = (Node *) n;
10638 				}
10639 		;
10640 
10641 
10642 /*****************************************************************************
10643  *
10644  *	clauses common to all Optimizable Stmts:
10645  *		from_clause		- allow list of both JOIN expressions and table names
10646  *		where_clause	- qualifications for joins or restrictions
10647  *
10648  *****************************************************************************/
10649 
10650 from_clause:
10651 			FROM from_list							{ $$ = $2; }
10652 			| /*EMPTY*/								{ $$ = NIL; }
10653 		;
10654 
10655 from_list:
10656 			table_ref								{ $$ = list_make1($1); }
10657 			| from_list ',' table_ref				{ $$ = lappend($1, $3); }
10658 		;
10659 
10660 /*
10661  * table_ref is where an alias clause can be attached.
10662  */
10663 table_ref:	relation_expr opt_alias_clause
10664 				{
10665 					$1->alias = $2;
10666 					$$ = (Node *) $1;
10667 				}
10668 			| relation_expr opt_alias_clause tablesample_clause
10669 				{
10670 					RangeTableSample *n = (RangeTableSample *) $3;
10671 					$1->alias = $2;
10672 					/* relation_expr goes inside the RangeTableSample node */
10673 					n->relation = (Node *) $1;
10674 					$$ = (Node *) n;
10675 				}
10676 			| func_table func_alias_clause
10677 				{
10678 					RangeFunction *n = (RangeFunction *) $1;
10679 					n->alias = linitial($2);
10680 					n->coldeflist = lsecond($2);
10681 					$$ = (Node *) n;
10682 				}
10683 			| LATERAL_P func_table func_alias_clause
10684 				{
10685 					RangeFunction *n = (RangeFunction *) $2;
10686 					n->lateral = true;
10687 					n->alias = linitial($3);
10688 					n->coldeflist = lsecond($3);
10689 					$$ = (Node *) n;
10690 				}
10691 			| select_with_parens opt_alias_clause
10692 				{
10693 					RangeSubselect *n = makeNode(RangeSubselect);
10694 					n->lateral = false;
10695 					n->subquery = $1;
10696 					n->alias = $2;
10697 					/*
10698 					 * The SQL spec does not permit a subselect
10699 					 * (<derived_table>) without an alias clause,
10700 					 * so we don't either.  This avoids the problem
10701 					 * of needing to invent a unique refname for it.
10702 					 * That could be surmounted if there's sufficient
10703 					 * popular demand, but for now let's just implement
10704 					 * the spec and see if anyone complains.
10705 					 * However, it does seem like a good idea to emit
10706 					 * an error message that's better than "syntax error".
10707 					 */
10708 					if ($2 == NULL)
10709 					{
10710 						if (IsA($1, SelectStmt) &&
10711 							((SelectStmt *) $1)->valuesLists)
10712 							ereport(ERROR,
10713 									(errcode(ERRCODE_SYNTAX_ERROR),
10714 									 errmsg("VALUES in FROM must have an alias"),
10715 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
10716 									 parser_errposition(@1)));
10717 						else
10718 							ereport(ERROR,
10719 									(errcode(ERRCODE_SYNTAX_ERROR),
10720 									 errmsg("subquery in FROM must have an alias"),
10721 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
10722 									 parser_errposition(@1)));
10723 					}
10724 					$$ = (Node *) n;
10725 				}
10726 			| LATERAL_P select_with_parens opt_alias_clause
10727 				{
10728 					RangeSubselect *n = makeNode(RangeSubselect);
10729 					n->lateral = true;
10730 					n->subquery = $2;
10731 					n->alias = $3;
10732 					/* same comment as above */
10733 					if ($3 == NULL)
10734 					{
10735 						if (IsA($2, SelectStmt) &&
10736 							((SelectStmt *) $2)->valuesLists)
10737 							ereport(ERROR,
10738 									(errcode(ERRCODE_SYNTAX_ERROR),
10739 									 errmsg("VALUES in FROM must have an alias"),
10740 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
10741 									 parser_errposition(@2)));
10742 						else
10743 							ereport(ERROR,
10744 									(errcode(ERRCODE_SYNTAX_ERROR),
10745 									 errmsg("subquery in FROM must have an alias"),
10746 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
10747 									 parser_errposition(@2)));
10748 					}
10749 					$$ = (Node *) n;
10750 				}
10751 			| joined_table
10752 				{
10753 					$$ = (Node *) $1;
10754 				}
10755 			| '(' joined_table ')' alias_clause
10756 				{
10757 					$2->alias = $4;
10758 					$$ = (Node *) $2;
10759 				}
10760 		;
10761 
10762 
10763 /*
10764  * It may seem silly to separate joined_table from table_ref, but there is
10765  * method in SQL's madness: if you don't do it this way you get reduce-
10766  * reduce conflicts, because it's not clear to the parser generator whether
10767  * to expect alias_clause after ')' or not.  For the same reason we must
10768  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
10769  * join_type to expand to empty; if we try it, the parser generator can't
10770  * figure out when to reduce an empty join_type right after table_ref.
10771  *
10772  * Note that a CROSS JOIN is the same as an unqualified
10773  * INNER JOIN, and an INNER JOIN/ON has the same shape
10774  * but a qualification expression to limit membership.
10775  * A NATURAL JOIN implicitly matches column names between
10776  * tables and the shape is determined by which columns are
10777  * in common. We'll collect columns during the later transformations.
10778  */
10779 
10780 joined_table:
10781 			'(' joined_table ')'
10782 				{
10783 					$$ = $2;
10784 				}
10785 			| table_ref CROSS JOIN table_ref
10786 				{
10787 					/* CROSS JOIN is same as unqualified inner join */
10788 					JoinExpr *n = makeNode(JoinExpr);
10789 					n->jointype = JOIN_INNER;
10790 					n->isNatural = FALSE;
10791 					n->larg = $1;
10792 					n->rarg = $4;
10793 					n->usingClause = NIL;
10794 					n->quals = NULL;
10795 					$$ = n;
10796 				}
10797 			| table_ref join_type JOIN table_ref join_qual
10798 				{
10799 					JoinExpr *n = makeNode(JoinExpr);
10800 					n->jointype = $2;
10801 					n->isNatural = FALSE;
10802 					n->larg = $1;
10803 					n->rarg = $4;
10804 					if ($5 != NULL && IsA($5, List))
10805 						n->usingClause = (List *) $5; /* USING clause */
10806 					else
10807 						n->quals = $5; /* ON clause */
10808 					$$ = n;
10809 				}
10810 			| table_ref JOIN table_ref join_qual
10811 				{
10812 					/* letting join_type reduce to empty doesn't work */
10813 					JoinExpr *n = makeNode(JoinExpr);
10814 					n->jointype = JOIN_INNER;
10815 					n->isNatural = FALSE;
10816 					n->larg = $1;
10817 					n->rarg = $3;
10818 					if ($4 != NULL && IsA($4, List))
10819 						n->usingClause = (List *) $4; /* USING clause */
10820 					else
10821 						n->quals = $4; /* ON clause */
10822 					$$ = n;
10823 				}
10824 			| table_ref NATURAL join_type JOIN table_ref
10825 				{
10826 					JoinExpr *n = makeNode(JoinExpr);
10827 					n->jointype = $3;
10828 					n->isNatural = TRUE;
10829 					n->larg = $1;
10830 					n->rarg = $5;
10831 					n->usingClause = NIL; /* figure out which columns later... */
10832 					n->quals = NULL; /* fill later */
10833 					$$ = n;
10834 				}
10835 			| table_ref NATURAL JOIN table_ref
10836 				{
10837 					/* letting join_type reduce to empty doesn't work */
10838 					JoinExpr *n = makeNode(JoinExpr);
10839 					n->jointype = JOIN_INNER;
10840 					n->isNatural = TRUE;
10841 					n->larg = $1;
10842 					n->rarg = $4;
10843 					n->usingClause = NIL; /* figure out which columns later... */
10844 					n->quals = NULL; /* fill later */
10845 					$$ = n;
10846 				}
10847 		;
10848 
10849 alias_clause:
10850 			AS ColId '(' name_list ')'
10851 				{
10852 					$$ = makeNode(Alias);
10853 					$$->aliasname = $2;
10854 					$$->colnames = $4;
10855 				}
10856 			| AS ColId
10857 				{
10858 					$$ = makeNode(Alias);
10859 					$$->aliasname = $2;
10860 				}
10861 			| ColId '(' name_list ')'
10862 				{
10863 					$$ = makeNode(Alias);
10864 					$$->aliasname = $1;
10865 					$$->colnames = $3;
10866 				}
10867 			| ColId
10868 				{
10869 					$$ = makeNode(Alias);
10870 					$$->aliasname = $1;
10871 				}
10872 		;
10873 
10874 opt_alias_clause: alias_clause						{ $$ = $1; }
10875 			| /*EMPTY*/								{ $$ = NULL; }
10876 		;
10877 
10878 /*
10879  * func_alias_clause can include both an Alias and a coldeflist, so we make it
10880  * return a 2-element list that gets disassembled by calling production.
10881  */
10882 func_alias_clause:
10883 			alias_clause
10884 				{
10885 					$$ = list_make2($1, NIL);
10886 				}
10887 			| AS '(' TableFuncElementList ')'
10888 				{
10889 					$$ = list_make2(NULL, $3);
10890 				}
10891 			| AS ColId '(' TableFuncElementList ')'
10892 				{
10893 					Alias *a = makeNode(Alias);
10894 					a->aliasname = $2;
10895 					$$ = list_make2(a, $4);
10896 				}
10897 			| ColId '(' TableFuncElementList ')'
10898 				{
10899 					Alias *a = makeNode(Alias);
10900 					a->aliasname = $1;
10901 					$$ = list_make2(a, $3);
10902 				}
10903 			| /*EMPTY*/
10904 				{
10905 					$$ = list_make2(NULL, NIL);
10906 				}
10907 		;
10908 
10909 join_type:	FULL join_outer							{ $$ = JOIN_FULL; }
10910 			| LEFT join_outer						{ $$ = JOIN_LEFT; }
10911 			| RIGHT join_outer						{ $$ = JOIN_RIGHT; }
10912 			| INNER_P								{ $$ = JOIN_INNER; }
10913 		;
10914 
10915 /* OUTER is just noise... */
10916 join_outer: OUTER_P									{ $$ = NULL; }
10917 			| /*EMPTY*/								{ $$ = NULL; }
10918 		;
10919 
10920 /* JOIN qualification clauses
10921  * Possibilities are:
10922  *	USING ( column list ) allows only unqualified column names,
10923  *						  which must match between tables.
10924  *	ON expr allows more general qualifications.
10925  *
10926  * We return USING as a List node, while an ON-expr will not be a List.
10927  */
10928 
10929 join_qual:	USING '(' name_list ')'					{ $$ = (Node *) $3; }
10930 			| ON a_expr								{ $$ = $2; }
10931 		;
10932 
10933 
10934 relation_expr:
10935 			qualified_name
10936 				{
10937 					/* default inheritance */
10938 					$$ = $1;
10939 					$$->inhOpt = INH_DEFAULT;
10940 					$$->alias = NULL;
10941 				}
10942 			| qualified_name '*'
10943 				{
10944 					/* inheritance query */
10945 					$$ = $1;
10946 					$$->inhOpt = INH_YES;
10947 					$$->alias = NULL;
10948 				}
10949 			| ONLY qualified_name
10950 				{
10951 					/* no inheritance */
10952 					$$ = $2;
10953 					$$->inhOpt = INH_NO;
10954 					$$->alias = NULL;
10955 				}
10956 			| ONLY '(' qualified_name ')'
10957 				{
10958 					/* no inheritance, SQL99-style syntax */
10959 					$$ = $3;
10960 					$$->inhOpt = INH_NO;
10961 					$$->alias = NULL;
10962 				}
10963 		;
10964 
10965 
10966 relation_expr_list:
10967 			relation_expr							{ $$ = list_make1($1); }
10968 			| relation_expr_list ',' relation_expr	{ $$ = lappend($1, $3); }
10969 		;
10970 
10971 
10972 /*
10973  * Given "UPDATE foo set set ...", we have to decide without looking any
10974  * further ahead whether the first "set" is an alias or the UPDATE's SET
10975  * keyword.  Since "set" is allowed as a column name both interpretations
10976  * are feasible.  We resolve the shift/reduce conflict by giving the first
10977  * relation_expr_opt_alias production a higher precedence than the SET token
10978  * has, causing the parser to prefer to reduce, in effect assuming that the
10979  * SET is not an alias.
10980  */
10981 relation_expr_opt_alias: relation_expr					%prec UMINUS
10982 				{
10983 					$$ = $1;
10984 				}
10985 			| relation_expr ColId
10986 				{
10987 					Alias *alias = makeNode(Alias);
10988 					alias->aliasname = $2;
10989 					$1->alias = alias;
10990 					$$ = $1;
10991 				}
10992 			| relation_expr AS ColId
10993 				{
10994 					Alias *alias = makeNode(Alias);
10995 					alias->aliasname = $3;
10996 					$1->alias = alias;
10997 					$$ = $1;
10998 				}
10999 		;
11000 
11001 /*
11002  * TABLESAMPLE decoration in a FROM item
11003  */
11004 tablesample_clause:
11005 			TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause
11006 				{
11007 					RangeTableSample *n = makeNode(RangeTableSample);
11008 					/* n->relation will be filled in later */
11009 					n->method = $2;
11010 					n->args = $4;
11011 					n->repeatable = $6;
11012 					n->location = @2;
11013 					$$ = (Node *) n;
11014 				}
11015 		;
11016 
11017 opt_repeatable_clause:
11018 			REPEATABLE '(' a_expr ')'	{ $$ = (Node *) $3; }
11019 			| /*EMPTY*/					{ $$ = NULL; }
11020 		;
11021 
11022 /*
11023  * func_table represents a function invocation in a FROM list. It can be
11024  * a plain function call, like "foo(...)", or a ROWS FROM expression with
11025  * one or more function calls, "ROWS FROM (foo(...), bar(...))",
11026  * optionally with WITH ORDINALITY attached.
11027  * In the ROWS FROM syntax, a column definition list can be given for each
11028  * function, for example:
11029  *     ROWS FROM (foo() AS (foo_res_a text, foo_res_b text),
11030  *                bar() AS (bar_res_a text, bar_res_b text))
11031  * It's also possible to attach a column definition list to the RangeFunction
11032  * as a whole, but that's handled by the table_ref production.
11033  */
11034 func_table: func_expr_windowless opt_ordinality
11035 				{
11036 					RangeFunction *n = makeNode(RangeFunction);
11037 					n->lateral = false;
11038 					n->ordinality = $2;
11039 					n->is_rowsfrom = false;
11040 					n->functions = list_make1(list_make2($1, NIL));
11041 					/* alias and coldeflist are set by table_ref production */
11042 					$$ = (Node *) n;
11043 				}
11044 			| ROWS FROM '(' rowsfrom_list ')' opt_ordinality
11045 				{
11046 					RangeFunction *n = makeNode(RangeFunction);
11047 					n->lateral = false;
11048 					n->ordinality = $6;
11049 					n->is_rowsfrom = true;
11050 					n->functions = $4;
11051 					/* alias and coldeflist are set by table_ref production */
11052 					$$ = (Node *) n;
11053 				}
11054 		;
11055 
11056 rowsfrom_item: func_expr_windowless opt_col_def_list
11057 				{ $$ = list_make2($1, $2); }
11058 		;
11059 
11060 rowsfrom_list:
11061 			rowsfrom_item						{ $$ = list_make1($1); }
11062 			| rowsfrom_list ',' rowsfrom_item	{ $$ = lappend($1, $3); }
11063 		;
11064 
11065 opt_col_def_list: AS '(' TableFuncElementList ')'	{ $$ = $3; }
11066 			| /*EMPTY*/								{ $$ = NIL; }
11067 		;
11068 
11069 opt_ordinality: WITH_LA ORDINALITY					{ $$ = true; }
11070 			| /*EMPTY*/								{ $$ = false; }
11071 		;
11072 
11073 
11074 where_clause:
11075 			WHERE a_expr							{ $$ = $2; }
11076 			| /*EMPTY*/								{ $$ = NULL; }
11077 		;
11078 
11079 /* variant for UPDATE and DELETE */
11080 where_or_current_clause:
11081 			WHERE a_expr							{ $$ = $2; }
11082 			| WHERE CURRENT_P OF cursor_name
11083 				{
11084 					CurrentOfExpr *n = makeNode(CurrentOfExpr);
11085 					/* cvarno is filled in by parse analysis */
11086 					n->cursor_name = $4;
11087 					n->cursor_param = 0;
11088 					$$ = (Node *) n;
11089 				}
11090 			| /*EMPTY*/								{ $$ = NULL; }
11091 		;
11092 
11093 
11094 OptTableFuncElementList:
11095 			TableFuncElementList				{ $$ = $1; }
11096 			| /*EMPTY*/							{ $$ = NIL; }
11097 		;
11098 
11099 TableFuncElementList:
11100 			TableFuncElement
11101 				{
11102 					$$ = list_make1($1);
11103 				}
11104 			| TableFuncElementList ',' TableFuncElement
11105 				{
11106 					$$ = lappend($1, $3);
11107 				}
11108 		;
11109 
11110 TableFuncElement:	ColId Typename opt_collate_clause
11111 				{
11112 					ColumnDef *n = makeNode(ColumnDef);
11113 					n->colname = $1;
11114 					n->typeName = $2;
11115 					n->inhcount = 0;
11116 					n->is_local = true;
11117 					n->is_not_null = false;
11118 					n->is_from_type = false;
11119 					n->storage = 0;
11120 					n->raw_default = NULL;
11121 					n->cooked_default = NULL;
11122 					n->collClause = (CollateClause *) $3;
11123 					n->collOid = InvalidOid;
11124 					n->constraints = NIL;
11125 					n->location = @1;
11126 					$$ = (Node *)n;
11127 				}
11128 		;
11129 
11130 /*****************************************************************************
11131  *
11132  *	Type syntax
11133  *		SQL introduces a large amount of type-specific syntax.
11134  *		Define individual clauses to handle these cases, and use
11135  *		 the generic case to handle regular type-extensible Postgres syntax.
11136  *		- thomas 1997-10-10
11137  *
11138  *****************************************************************************/
11139 
11140 Typename:	SimpleTypename opt_array_bounds
11141 				{
11142 					$$ = $1;
11143 					$$->arrayBounds = $2;
11144 				}
11145 			| SETOF SimpleTypename opt_array_bounds
11146 				{
11147 					$$ = $2;
11148 					$$->arrayBounds = $3;
11149 					$$->setof = TRUE;
11150 				}
11151 			/* SQL standard syntax, currently only one-dimensional */
11152 			| SimpleTypename ARRAY '[' Iconst ']'
11153 				{
11154 					$$ = $1;
11155 					$$->arrayBounds = list_make1(makeInteger($4));
11156 				}
11157 			| SETOF SimpleTypename ARRAY '[' Iconst ']'
11158 				{
11159 					$$ = $2;
11160 					$$->arrayBounds = list_make1(makeInteger($5));
11161 					$$->setof = TRUE;
11162 				}
11163 			| SimpleTypename ARRAY
11164 				{
11165 					$$ = $1;
11166 					$$->arrayBounds = list_make1(makeInteger(-1));
11167 				}
11168 			| SETOF SimpleTypename ARRAY
11169 				{
11170 					$$ = $2;
11171 					$$->arrayBounds = list_make1(makeInteger(-1));
11172 					$$->setof = TRUE;
11173 				}
11174 		;
11175 
11176 opt_array_bounds:
11177 			opt_array_bounds '[' ']'
11178 					{  $$ = lappend($1, makeInteger(-1)); }
11179 			| opt_array_bounds '[' Iconst ']'
11180 					{  $$ = lappend($1, makeInteger($3)); }
11181 			| /*EMPTY*/
11182 					{  $$ = NIL; }
11183 		;
11184 
11185 SimpleTypename:
11186 			GenericType								{ $$ = $1; }
11187 			| Numeric								{ $$ = $1; }
11188 			| Bit									{ $$ = $1; }
11189 			| Character								{ $$ = $1; }
11190 			| ConstDatetime							{ $$ = $1; }
11191 			| ConstInterval opt_interval
11192 				{
11193 					$$ = $1;
11194 					$$->typmods = $2;
11195 				}
11196 			| ConstInterval '(' Iconst ')'
11197 				{
11198 					$$ = $1;
11199 					$$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
11200 											 makeIntConst($3, @3));
11201 				}
11202 		;
11203 
11204 /* We have a separate ConstTypename to allow defaulting fixed-length
11205  * types such as CHAR() and BIT() to an unspecified length.
11206  * SQL9x requires that these default to a length of one, but this
11207  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
11208  * where there is an obvious better choice to make.
11209  * Note that ConstInterval is not included here since it must
11210  * be pushed up higher in the rules to accommodate the postfix
11211  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
11212  * the generic-type-name case in AExprConst to avoid premature
11213  * reduce/reduce conflicts against function names.
11214  */
11215 ConstTypename:
11216 			Numeric									{ $$ = $1; }
11217 			| ConstBit								{ $$ = $1; }
11218 			| ConstCharacter						{ $$ = $1; }
11219 			| ConstDatetime							{ $$ = $1; }
11220 		;
11221 
11222 /*
11223  * GenericType covers all type names that don't have special syntax mandated
11224  * by the standard, including qualified names.  We also allow type modifiers.
11225  * To avoid parsing conflicts against function invocations, the modifiers
11226  * have to be shown as expr_list here, but parse analysis will only accept
11227  * constants for them.
11228  */
11229 GenericType:
11230 			type_function_name opt_type_modifiers
11231 				{
11232 					$$ = makeTypeName($1);
11233 					$$->typmods = $2;
11234 					$$->location = @1;
11235 				}
11236 			| type_function_name attrs opt_type_modifiers
11237 				{
11238 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
11239 					$$->typmods = $3;
11240 					$$->location = @1;
11241 				}
11242 		;
11243 
11244 opt_type_modifiers: '(' expr_list ')'				{ $$ = $2; }
11245 					| /* EMPTY */					{ $$ = NIL; }
11246 		;
11247 
11248 /*
11249  * SQL numeric data types
11250  */
11251 Numeric:	INT_P
11252 				{
11253 					$$ = SystemTypeName("int4");
11254 					$$->location = @1;
11255 				}
11256 			| INTEGER
11257 				{
11258 					$$ = SystemTypeName("int4");
11259 					$$->location = @1;
11260 				}
11261 			| SMALLINT
11262 				{
11263 					$$ = SystemTypeName("int2");
11264 					$$->location = @1;
11265 				}
11266 			| BIGINT
11267 				{
11268 					$$ = SystemTypeName("int8");
11269 					$$->location = @1;
11270 				}
11271 			| REAL
11272 				{
11273 					$$ = SystemTypeName("float4");
11274 					$$->location = @1;
11275 				}
11276 			| FLOAT_P opt_float
11277 				{
11278 					$$ = $2;
11279 					$$->location = @1;
11280 				}
11281 			| DOUBLE_P PRECISION
11282 				{
11283 					$$ = SystemTypeName("float8");
11284 					$$->location = @1;
11285 				}
11286 			| DECIMAL_P opt_type_modifiers
11287 				{
11288 					$$ = SystemTypeName("numeric");
11289 					$$->typmods = $2;
11290 					$$->location = @1;
11291 				}
11292 			| DEC opt_type_modifiers
11293 				{
11294 					$$ = SystemTypeName("numeric");
11295 					$$->typmods = $2;
11296 					$$->location = @1;
11297 				}
11298 			| NUMERIC opt_type_modifiers
11299 				{
11300 					$$ = SystemTypeName("numeric");
11301 					$$->typmods = $2;
11302 					$$->location = @1;
11303 				}
11304 			| BOOLEAN_P
11305 				{
11306 					$$ = SystemTypeName("bool");
11307 					$$->location = @1;
11308 				}
11309 		;
11310 
11311 opt_float:	'(' Iconst ')'
11312 				{
11313 					/*
11314 					 * Check FLOAT() precision limits assuming IEEE floating
11315 					 * types - thomas 1997-09-18
11316 					 */
11317 					if ($2 < 1)
11318 						ereport(ERROR,
11319 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
11320 								 errmsg("precision for type float must be at least 1 bit"),
11321 								 parser_errposition(@2)));
11322 					else if ($2 <= 24)
11323 						$$ = SystemTypeName("float4");
11324 					else if ($2 <= 53)
11325 						$$ = SystemTypeName("float8");
11326 					else
11327 						ereport(ERROR,
11328 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
11329 								 errmsg("precision for type float must be less than 54 bits"),
11330 								 parser_errposition(@2)));
11331 				}
11332 			| /*EMPTY*/
11333 				{
11334 					$$ = SystemTypeName("float8");
11335 				}
11336 		;
11337 
11338 /*
11339  * SQL bit-field data types
11340  * The following implements BIT() and BIT VARYING().
11341  */
11342 Bit:		BitWithLength
11343 				{
11344 					$$ = $1;
11345 				}
11346 			| BitWithoutLength
11347 				{
11348 					$$ = $1;
11349 				}
11350 		;
11351 
11352 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
11353 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
11354 ConstBit:	BitWithLength
11355 				{
11356 					$$ = $1;
11357 				}
11358 			| BitWithoutLength
11359 				{
11360 					$$ = $1;
11361 					$$->typmods = NIL;
11362 				}
11363 		;
11364 
11365 BitWithLength:
11366 			BIT opt_varying '(' expr_list ')'
11367 				{
11368 					char *typname;
11369 
11370 					typname = $2 ? "varbit" : "bit";
11371 					$$ = SystemTypeName(typname);
11372 					$$->typmods = $4;
11373 					$$->location = @1;
11374 				}
11375 		;
11376 
11377 BitWithoutLength:
11378 			BIT opt_varying
11379 				{
11380 					/* bit defaults to bit(1), varbit to no limit */
11381 					if ($2)
11382 					{
11383 						$$ = SystemTypeName("varbit");
11384 					}
11385 					else
11386 					{
11387 						$$ = SystemTypeName("bit");
11388 						$$->typmods = list_make1(makeIntConst(1, -1));
11389 					}
11390 					$$->location = @1;
11391 				}
11392 		;
11393 
11394 
11395 /*
11396  * SQL character data types
11397  * The following implements CHAR() and VARCHAR().
11398  */
11399 Character:  CharacterWithLength
11400 				{
11401 					$$ = $1;
11402 				}
11403 			| CharacterWithoutLength
11404 				{
11405 					$$ = $1;
11406 				}
11407 		;
11408 
11409 ConstCharacter:  CharacterWithLength
11410 				{
11411 					$$ = $1;
11412 				}
11413 			| CharacterWithoutLength
11414 				{
11415 					/* Length was not specified so allow to be unrestricted.
11416 					 * This handles problems with fixed-length (bpchar) strings
11417 					 * which in column definitions must default to a length
11418 					 * of one, but should not be constrained if the length
11419 					 * was not specified.
11420 					 */
11421 					$$ = $1;
11422 					$$->typmods = NIL;
11423 				}
11424 		;
11425 
11426 CharacterWithLength:  character '(' Iconst ')' opt_charset
11427 				{
11428 					if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
11429 						$1 = psprintf("%s_%s", $1, $5);
11430 
11431 					$$ = SystemTypeName($1);
11432 					$$->typmods = list_make1(makeIntConst($3, @3));
11433 					$$->location = @1;
11434 				}
11435 		;
11436 
11437 CharacterWithoutLength:	 character opt_charset
11438 				{
11439 					if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
11440 						$1 = psprintf("%s_%s", $1, $2);
11441 
11442 					$$ = SystemTypeName($1);
11443 
11444 					/* char defaults to char(1), varchar to no limit */
11445 					if (strcmp($1, "bpchar") == 0)
11446 						$$->typmods = list_make1(makeIntConst(1, -1));
11447 
11448 					$$->location = @1;
11449 				}
11450 		;
11451 
11452 character:	CHARACTER opt_varying
11453 										{ $$ = $2 ? "varchar": "bpchar"; }
11454 			| CHAR_P opt_varying
11455 										{ $$ = $2 ? "varchar": "bpchar"; }
11456 			| VARCHAR
11457 										{ $$ = "varchar"; }
11458 			| NATIONAL CHARACTER opt_varying
11459 										{ $$ = $3 ? "varchar": "bpchar"; }
11460 			| NATIONAL CHAR_P opt_varying
11461 										{ $$ = $3 ? "varchar": "bpchar"; }
11462 			| NCHAR opt_varying
11463 										{ $$ = $2 ? "varchar": "bpchar"; }
11464 		;
11465 
11466 opt_varying:
11467 			VARYING									{ $$ = TRUE; }
11468 			| /*EMPTY*/								{ $$ = FALSE; }
11469 		;
11470 
11471 opt_charset:
11472 			CHARACTER SET ColId						{ $$ = $3; }
11473 			| /*EMPTY*/								{ $$ = NULL; }
11474 		;
11475 
11476 /*
11477  * SQL date/time types
11478  */
11479 ConstDatetime:
11480 			TIMESTAMP '(' Iconst ')' opt_timezone
11481 				{
11482 					if ($5)
11483 						$$ = SystemTypeName("timestamptz");
11484 					else
11485 						$$ = SystemTypeName("timestamp");
11486 					$$->typmods = list_make1(makeIntConst($3, @3));
11487 					$$->location = @1;
11488 				}
11489 			| TIMESTAMP opt_timezone
11490 				{
11491 					if ($2)
11492 						$$ = SystemTypeName("timestamptz");
11493 					else
11494 						$$ = SystemTypeName("timestamp");
11495 					$$->location = @1;
11496 				}
11497 			| TIME '(' Iconst ')' opt_timezone
11498 				{
11499 					if ($5)
11500 						$$ = SystemTypeName("timetz");
11501 					else
11502 						$$ = SystemTypeName("time");
11503 					$$->typmods = list_make1(makeIntConst($3, @3));
11504 					$$->location = @1;
11505 				}
11506 			| TIME opt_timezone
11507 				{
11508 					if ($2)
11509 						$$ = SystemTypeName("timetz");
11510 					else
11511 						$$ = SystemTypeName("time");
11512 					$$->location = @1;
11513 				}
11514 		;
11515 
11516 ConstInterval:
11517 			INTERVAL
11518 				{
11519 					$$ = SystemTypeName("interval");
11520 					$$->location = @1;
11521 				}
11522 		;
11523 
11524 opt_timezone:
11525 			WITH_LA TIME ZONE						{ $$ = TRUE; }
11526 			| WITHOUT TIME ZONE						{ $$ = FALSE; }
11527 			| /*EMPTY*/								{ $$ = FALSE; }
11528 		;
11529 
11530 opt_interval:
11531 			YEAR_P
11532 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
11533 			| MONTH_P
11534 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
11535 			| DAY_P
11536 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
11537 			| HOUR_P
11538 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
11539 			| MINUTE_P
11540 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
11541 			| interval_second
11542 				{ $$ = $1; }
11543 			| YEAR_P TO MONTH_P
11544 				{
11545 					$$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
11546 												 INTERVAL_MASK(MONTH), @1));
11547 				}
11548 			| DAY_P TO HOUR_P
11549 				{
11550 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
11551 												 INTERVAL_MASK(HOUR), @1));
11552 				}
11553 			| DAY_P TO MINUTE_P
11554 				{
11555 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
11556 												 INTERVAL_MASK(HOUR) |
11557 												 INTERVAL_MASK(MINUTE), @1));
11558 				}
11559 			| DAY_P TO interval_second
11560 				{
11561 					$$ = $3;
11562 					linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
11563 												INTERVAL_MASK(HOUR) |
11564 												INTERVAL_MASK(MINUTE) |
11565 												INTERVAL_MASK(SECOND), @1);
11566 				}
11567 			| HOUR_P TO MINUTE_P
11568 				{
11569 					$$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
11570 												 INTERVAL_MASK(MINUTE), @1));
11571 				}
11572 			| HOUR_P TO interval_second
11573 				{
11574 					$$ = $3;
11575 					linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
11576 												INTERVAL_MASK(MINUTE) |
11577 												INTERVAL_MASK(SECOND), @1);
11578 				}
11579 			| MINUTE_P TO interval_second
11580 				{
11581 					$$ = $3;
11582 					linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
11583 												INTERVAL_MASK(SECOND), @1);
11584 				}
11585 			| /*EMPTY*/
11586 				{ $$ = NIL; }
11587 		;
11588 
11589 interval_second:
11590 			SECOND_P
11591 				{
11592 					$$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
11593 				}
11594 			| SECOND_P '(' Iconst ')'
11595 				{
11596 					$$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
11597 									makeIntConst($3, @3));
11598 				}
11599 		;
11600 
11601 
11602 /*****************************************************************************
11603  *
11604  *	expression grammar
11605  *
11606  *****************************************************************************/
11607 
11608 /*
11609  * General expressions
11610  * This is the heart of the expression syntax.
11611  *
11612  * We have two expression types: a_expr is the unrestricted kind, and
11613  * b_expr is a subset that must be used in some places to avoid shift/reduce
11614  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
11615  * because that use of AND conflicts with AND as a boolean operator.  So,
11616  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
11617  *
11618  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
11619  * always be used by surrounding it with parens.
11620  *
11621  * c_expr is all the productions that are common to a_expr and b_expr;
11622  * it's factored out just to eliminate redundant coding.
11623  *
11624  * Be careful of productions involving more than one terminal token.
11625  * By default, bison will assign such productions the precedence of their
11626  * last terminal, but in nearly all cases you want it to be the precedence
11627  * of the first terminal instead; otherwise you will not get the behavior
11628  * you expect!  So we use %prec annotations freely to set precedences.
11629  */
11630 a_expr:		c_expr									{ $$ = $1; }
11631 			| a_expr TYPECAST Typename
11632 					{ $$ = makeTypeCast($1, $3, @2); }
11633 			| a_expr COLLATE any_name
11634 				{
11635 					CollateClause *n = makeNode(CollateClause);
11636 					n->arg = $1;
11637 					n->collname = $3;
11638 					n->location = @2;
11639 					$$ = (Node *) n;
11640 				}
11641 			| a_expr AT TIME ZONE a_expr			%prec AT
11642 				{
11643 					$$ = (Node *) makeFuncCall(SystemFuncName("timezone"),
11644 											   list_make2($5, $1),
11645 											   @2);
11646 				}
11647 		/*
11648 		 * These operators must be called out explicitly in order to make use
11649 		 * of bison's automatic operator-precedence handling.  All other
11650 		 * operator names are handled by the generic productions using "Op",
11651 		 * below; and all those operators will have the same precedence.
11652 		 *
11653 		 * If you add more explicitly-known operators, be sure to add them
11654 		 * also to b_expr and to the MathOp list below.
11655 		 */
11656 			| '+' a_expr					%prec UMINUS
11657 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
11658 			| '-' a_expr					%prec UMINUS
11659 				{ $$ = doNegate($2, @1); }
11660 			| a_expr '+' a_expr
11661 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
11662 			| a_expr '-' a_expr
11663 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
11664 			| a_expr '*' a_expr
11665 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
11666 			| a_expr '/' a_expr
11667 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
11668 			| a_expr '%' a_expr
11669 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
11670 			| a_expr '^' a_expr
11671 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
11672 			| a_expr '<' a_expr
11673 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
11674 			| a_expr '>' a_expr
11675 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
11676 			| a_expr '=' a_expr
11677 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
11678 			| a_expr LESS_EQUALS a_expr
11679 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
11680 			| a_expr GREATER_EQUALS a_expr
11681 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
11682 			| a_expr NOT_EQUALS a_expr
11683 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
11684 
11685 			| a_expr qual_Op a_expr				%prec Op
11686 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
11687 			| qual_Op a_expr					%prec Op
11688 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
11689 			| a_expr qual_Op					%prec POSTFIXOP
11690 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
11691 
11692 			| a_expr AND a_expr
11693 				{ $$ = makeAndExpr($1, $3, @2); }
11694 			| a_expr OR a_expr
11695 				{ $$ = makeOrExpr($1, $3, @2); }
11696 			| NOT a_expr
11697 				{ $$ = makeNotExpr($2, @1); }
11698 			| NOT_LA a_expr						%prec NOT
11699 				{ $$ = makeNotExpr($2, @1); }
11700 
11701 			| a_expr LIKE a_expr
11702 				{
11703 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
11704 												   $1, $3, @2);
11705 				}
11706 			| a_expr LIKE a_expr ESCAPE a_expr					%prec LIKE
11707 				{
11708 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
11709 											   list_make2($3, $5),
11710 											   @2);
11711 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
11712 												   $1, (Node *) n, @2);
11713 				}
11714 			| a_expr NOT_LA LIKE a_expr							%prec NOT_LA
11715 				{
11716 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
11717 												   $1, $4, @2);
11718 				}
11719 			| a_expr NOT_LA LIKE a_expr ESCAPE a_expr			%prec NOT_LA
11720 				{
11721 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
11722 											   list_make2($4, $6),
11723 											   @2);
11724 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
11725 												   $1, (Node *) n, @2);
11726 				}
11727 			| a_expr ILIKE a_expr
11728 				{
11729 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
11730 												   $1, $3, @2);
11731 				}
11732 			| a_expr ILIKE a_expr ESCAPE a_expr					%prec ILIKE
11733 				{
11734 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
11735 											   list_make2($3, $5),
11736 											   @2);
11737 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
11738 												   $1, (Node *) n, @2);
11739 				}
11740 			| a_expr NOT_LA ILIKE a_expr						%prec NOT_LA
11741 				{
11742 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
11743 												   $1, $4, @2);
11744 				}
11745 			| a_expr NOT_LA ILIKE a_expr ESCAPE a_expr			%prec NOT_LA
11746 				{
11747 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
11748 											   list_make2($4, $6),
11749 											   @2);
11750 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
11751 												   $1, (Node *) n, @2);
11752 				}
11753 
11754 			| a_expr SIMILAR TO a_expr							%prec SIMILAR
11755 				{
11756 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
11757 											   list_make2($4, makeNullAConst(-1)),
11758 											   @2);
11759 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
11760 												   $1, (Node *) n, @2);
11761 				}
11762 			| a_expr SIMILAR TO a_expr ESCAPE a_expr			%prec SIMILAR
11763 				{
11764 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
11765 											   list_make2($4, $6),
11766 											   @2);
11767 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
11768 												   $1, (Node *) n, @2);
11769 				}
11770 			| a_expr NOT_LA SIMILAR TO a_expr					%prec NOT_LA
11771 				{
11772 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
11773 											   list_make2($5, makeNullAConst(-1)),
11774 											   @2);
11775 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
11776 												   $1, (Node *) n, @2);
11777 				}
11778 			| a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr		%prec NOT_LA
11779 				{
11780 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
11781 											   list_make2($5, $7),
11782 											   @2);
11783 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
11784 												   $1, (Node *) n, @2);
11785 				}
11786 
11787 			/* NullTest clause
11788 			 * Define SQL-style Null test clause.
11789 			 * Allow two forms described in the standard:
11790 			 *	a IS NULL
11791 			 *	a IS NOT NULL
11792 			 * Allow two SQL extensions
11793 			 *	a ISNULL
11794 			 *	a NOTNULL
11795 			 */
11796 			| a_expr IS NULL_P							%prec IS
11797 				{
11798 					NullTest *n = makeNode(NullTest);
11799 					n->arg = (Expr *) $1;
11800 					n->nulltesttype = IS_NULL;
11801 					n->location = @2;
11802 					$$ = (Node *)n;
11803 				}
11804 			| a_expr ISNULL
11805 				{
11806 					NullTest *n = makeNode(NullTest);
11807 					n->arg = (Expr *) $1;
11808 					n->nulltesttype = IS_NULL;
11809 					n->location = @2;
11810 					$$ = (Node *)n;
11811 				}
11812 			| a_expr IS NOT NULL_P						%prec IS
11813 				{
11814 					NullTest *n = makeNode(NullTest);
11815 					n->arg = (Expr *) $1;
11816 					n->nulltesttype = IS_NOT_NULL;
11817 					n->location = @2;
11818 					$$ = (Node *)n;
11819 				}
11820 			| a_expr NOTNULL
11821 				{
11822 					NullTest *n = makeNode(NullTest);
11823 					n->arg = (Expr *) $1;
11824 					n->nulltesttype = IS_NOT_NULL;
11825 					n->location = @2;
11826 					$$ = (Node *)n;
11827 				}
11828 			| row OVERLAPS row
11829 				{
11830 					if (list_length($1) != 2)
11831 						ereport(ERROR,
11832 								(errcode(ERRCODE_SYNTAX_ERROR),
11833 								 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
11834 								 parser_errposition(@1)));
11835 					if (list_length($3) != 2)
11836 						ereport(ERROR,
11837 								(errcode(ERRCODE_SYNTAX_ERROR),
11838 								 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
11839 								 parser_errposition(@3)));
11840 					$$ = (Node *) makeFuncCall(SystemFuncName("overlaps"),
11841 											   list_concat($1, $3),
11842 											   @2);
11843 				}
11844 			| a_expr IS TRUE_P							%prec IS
11845 				{
11846 					BooleanTest *b = makeNode(BooleanTest);
11847 					b->arg = (Expr *) $1;
11848 					b->booltesttype = IS_TRUE;
11849 					b->location = @2;
11850 					$$ = (Node *)b;
11851 				}
11852 			| a_expr IS NOT TRUE_P						%prec IS
11853 				{
11854 					BooleanTest *b = makeNode(BooleanTest);
11855 					b->arg = (Expr *) $1;
11856 					b->booltesttype = IS_NOT_TRUE;
11857 					b->location = @2;
11858 					$$ = (Node *)b;
11859 				}
11860 			| a_expr IS FALSE_P							%prec IS
11861 				{
11862 					BooleanTest *b = makeNode(BooleanTest);
11863 					b->arg = (Expr *) $1;
11864 					b->booltesttype = IS_FALSE;
11865 					b->location = @2;
11866 					$$ = (Node *)b;
11867 				}
11868 			| a_expr IS NOT FALSE_P						%prec IS
11869 				{
11870 					BooleanTest *b = makeNode(BooleanTest);
11871 					b->arg = (Expr *) $1;
11872 					b->booltesttype = IS_NOT_FALSE;
11873 					b->location = @2;
11874 					$$ = (Node *)b;
11875 				}
11876 			| a_expr IS UNKNOWN							%prec IS
11877 				{
11878 					BooleanTest *b = makeNode(BooleanTest);
11879 					b->arg = (Expr *) $1;
11880 					b->booltesttype = IS_UNKNOWN;
11881 					b->location = @2;
11882 					$$ = (Node *)b;
11883 				}
11884 			| a_expr IS NOT UNKNOWN						%prec IS
11885 				{
11886 					BooleanTest *b = makeNode(BooleanTest);
11887 					b->arg = (Expr *) $1;
11888 					b->booltesttype = IS_NOT_UNKNOWN;
11889 					b->location = @2;
11890 					$$ = (Node *)b;
11891 				}
11892 			| a_expr IS DISTINCT FROM a_expr			%prec IS
11893 				{
11894 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
11895 				}
11896 			| a_expr IS NOT DISTINCT FROM a_expr		%prec IS
11897 				{
11898 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
11899 				}
11900 			| a_expr IS OF '(' type_list ')'			%prec IS
11901 				{
11902 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
11903 				}
11904 			| a_expr IS NOT OF '(' type_list ')'		%prec IS
11905 				{
11906 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
11907 				}
11908 			| a_expr BETWEEN opt_asymmetric b_expr AND a_expr		%prec BETWEEN
11909 				{
11910 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN,
11911 												   "BETWEEN",
11912 												   $1,
11913 												   (Node *) list_make2($4, $6),
11914 												   @2);
11915 				}
11916 			| a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr %prec NOT_LA
11917 				{
11918 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN,
11919 												   "NOT BETWEEN",
11920 												   $1,
11921 												   (Node *) list_make2($5, $7),
11922 												   @2);
11923 				}
11924 			| a_expr BETWEEN SYMMETRIC b_expr AND a_expr			%prec BETWEEN
11925 				{
11926 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN_SYM,
11927 												   "BETWEEN SYMMETRIC",
11928 												   $1,
11929 												   (Node *) list_make2($4, $6),
11930 												   @2);
11931 				}
11932 			| a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr		%prec NOT_LA
11933 				{
11934 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN_SYM,
11935 												   "NOT BETWEEN SYMMETRIC",
11936 												   $1,
11937 												   (Node *) list_make2($5, $7),
11938 												   @2);
11939 				}
11940 			| a_expr IN_P in_expr
11941 				{
11942 					/* in_expr returns a SubLink or a list of a_exprs */
11943 					if (IsA($3, SubLink))
11944 					{
11945 						/* generate foo = ANY (subquery) */
11946 						SubLink *n = (SubLink *) $3;
11947 						n->subLinkType = ANY_SUBLINK;
11948 						n->subLinkId = 0;
11949 						n->testexpr = $1;
11950 						n->operName = NIL;		/* show it's IN not = ANY */
11951 						n->location = @2;
11952 						$$ = (Node *)n;
11953 					}
11954 					else
11955 					{
11956 						/* generate scalar IN expression */
11957 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
11958 					}
11959 				}
11960 			| a_expr NOT_LA IN_P in_expr						%prec NOT_LA
11961 				{
11962 					/* in_expr returns a SubLink or a list of a_exprs */
11963 					if (IsA($4, SubLink))
11964 					{
11965 						/* generate NOT (foo = ANY (subquery)) */
11966 						/* Make an = ANY node */
11967 						SubLink *n = (SubLink *) $4;
11968 						n->subLinkType = ANY_SUBLINK;
11969 						n->subLinkId = 0;
11970 						n->testexpr = $1;
11971 						n->operName = NIL;		/* show it's IN not = ANY */
11972 						n->location = @2;
11973 						/* Stick a NOT on top; must have same parse location */
11974 						$$ = makeNotExpr((Node *) n, @2);
11975 					}
11976 					else
11977 					{
11978 						/* generate scalar NOT IN expression */
11979 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
11980 					}
11981 				}
11982 			| a_expr subquery_Op sub_type select_with_parens	%prec Op
11983 				{
11984 					SubLink *n = makeNode(SubLink);
11985 					n->subLinkType = $3;
11986 					n->subLinkId = 0;
11987 					n->testexpr = $1;
11988 					n->operName = $2;
11989 					n->subselect = $4;
11990 					n->location = @2;
11991 					$$ = (Node *)n;
11992 				}
11993 			| a_expr subquery_Op sub_type '(' a_expr ')'		%prec Op
11994 				{
11995 					if ($3 == ANY_SUBLINK)
11996 						$$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
11997 					else
11998 						$$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
11999 				}
12000 			| UNIQUE select_with_parens
12001 				{
12002 					/* Not sure how to get rid of the parentheses
12003 					 * but there are lots of shift/reduce errors without them.
12004 					 *
12005 					 * Should be able to implement this by plopping the entire
12006 					 * select into a node, then transforming the target expressions
12007 					 * from whatever they are into count(*), and testing the
12008 					 * entire result equal to one.
12009 					 * But, will probably implement a separate node in the executor.
12010 					 */
12011 					ereport(ERROR,
12012 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
12013 							 errmsg("UNIQUE predicate is not yet implemented"),
12014 							 parser_errposition(@1)));
12015 				}
12016 			| a_expr IS DOCUMENT_P					%prec IS
12017 				{
12018 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12019 									 list_make1($1), @2);
12020 				}
12021 			| a_expr IS NOT DOCUMENT_P				%prec IS
12022 				{
12023 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12024 												 list_make1($1), @2),
12025 									 @2);
12026 				}
12027 		;
12028 
12029 /*
12030  * Restricted expressions
12031  *
12032  * b_expr is a subset of the complete expression syntax defined by a_expr.
12033  *
12034  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
12035  * cause trouble in the places where b_expr is used.  For simplicity, we
12036  * just eliminate all the boolean-keyword-operator productions from b_expr.
12037  */
12038 b_expr:		c_expr
12039 				{ $$ = $1; }
12040 			| b_expr TYPECAST Typename
12041 				{ $$ = makeTypeCast($1, $3, @2); }
12042 			| '+' b_expr					%prec UMINUS
12043 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
12044 			| '-' b_expr					%prec UMINUS
12045 				{ $$ = doNegate($2, @1); }
12046 			| b_expr '+' b_expr
12047 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
12048 			| b_expr '-' b_expr
12049 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
12050 			| b_expr '*' b_expr
12051 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
12052 			| b_expr '/' b_expr
12053 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
12054 			| b_expr '%' b_expr
12055 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
12056 			| b_expr '^' b_expr
12057 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
12058 			| b_expr '<' b_expr
12059 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
12060 			| b_expr '>' b_expr
12061 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
12062 			| b_expr '=' b_expr
12063 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
12064 			| b_expr LESS_EQUALS b_expr
12065 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
12066 			| b_expr GREATER_EQUALS b_expr
12067 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
12068 			| b_expr NOT_EQUALS b_expr
12069 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
12070 			| b_expr qual_Op b_expr				%prec Op
12071 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
12072 			| qual_Op b_expr					%prec Op
12073 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
12074 			| b_expr qual_Op					%prec POSTFIXOP
12075 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
12076 			| b_expr IS DISTINCT FROM b_expr		%prec IS
12077 				{
12078 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
12079 				}
12080 			| b_expr IS NOT DISTINCT FROM b_expr	%prec IS
12081 				{
12082 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
12083 				}
12084 			| b_expr IS OF '(' type_list ')'		%prec IS
12085 				{
12086 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
12087 				}
12088 			| b_expr IS NOT OF '(' type_list ')'	%prec IS
12089 				{
12090 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
12091 				}
12092 			| b_expr IS DOCUMENT_P					%prec IS
12093 				{
12094 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12095 									 list_make1($1), @2);
12096 				}
12097 			| b_expr IS NOT DOCUMENT_P				%prec IS
12098 				{
12099 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12100 												 list_make1($1), @2),
12101 									 @2);
12102 				}
12103 		;
12104 
12105 /*
12106  * Productions that can be used in both a_expr and b_expr.
12107  *
12108  * Note: productions that refer recursively to a_expr or b_expr mostly
12109  * cannot appear here.	However, it's OK to refer to a_exprs that occur
12110  * inside parentheses, such as function arguments; that cannot introduce
12111  * ambiguity to the b_expr syntax.
12112  */
12113 c_expr:		columnref								{ $$ = $1; }
12114 			| AexprConst							{ $$ = $1; }
12115 			| PARAM opt_indirection
12116 				{
12117 					ParamRef *p = makeNode(ParamRef);
12118 					p->number = $1;
12119 					p->location = @1;
12120 					if ($2)
12121 					{
12122 						A_Indirection *n = makeNode(A_Indirection);
12123 						n->arg = (Node *) p;
12124 						n->indirection = check_indirection($2, yyscanner);
12125 						$$ = (Node *) n;
12126 					}
12127 					else
12128 						$$ = (Node *) p;
12129 				}
12130 			| '(' a_expr ')' opt_indirection
12131 				{
12132 					if ($4)
12133 					{
12134 						A_Indirection *n = makeNode(A_Indirection);
12135 						n->arg = $2;
12136 						n->indirection = check_indirection($4, yyscanner);
12137 						$$ = (Node *)n;
12138 					}
12139 					else if (operator_precedence_warning)
12140 					{
12141 						/*
12142 						 * If precedence warnings are enabled, insert
12143 						 * AEXPR_PAREN nodes wrapping all explicitly
12144 						 * parenthesized subexpressions; this prevents bogus
12145 						 * warnings from being issued when the ordering has
12146 						 * been forced by parentheses.  Take care that an
12147 						 * AEXPR_PAREN node has the same exprLocation as its
12148 						 * child, so as not to cause surprising changes in
12149 						 * error cursor positioning.
12150 						 *
12151 						 * In principle we should not be relying on a GUC to
12152 						 * decide whether to insert AEXPR_PAREN nodes.
12153 						 * However, since they have no effect except to
12154 						 * suppress warnings, it's probably safe enough; and
12155 						 * we'd just as soon not waste cycles on dummy parse
12156 						 * nodes if we don't have to.
12157 						 */
12158 						$$ = (Node *) makeA_Expr(AEXPR_PAREN, NIL, $2, NULL,
12159 												 exprLocation($2));
12160 					}
12161 					else
12162 						$$ = $2;
12163 				}
12164 			| case_expr
12165 				{ $$ = $1; }
12166 			| func_expr
12167 				{ $$ = $1; }
12168 			| select_with_parens			%prec UMINUS
12169 				{
12170 					SubLink *n = makeNode(SubLink);
12171 					n->subLinkType = EXPR_SUBLINK;
12172 					n->subLinkId = 0;
12173 					n->testexpr = NULL;
12174 					n->operName = NIL;
12175 					n->subselect = $1;
12176 					n->location = @1;
12177 					$$ = (Node *)n;
12178 				}
12179 			| select_with_parens indirection
12180 				{
12181 					/*
12182 					 * Because the select_with_parens nonterminal is designed
12183 					 * to "eat" as many levels of parens as possible, the
12184 					 * '(' a_expr ')' opt_indirection production above will
12185 					 * fail to match a sub-SELECT with indirection decoration;
12186 					 * the sub-SELECT won't be regarded as an a_expr as long
12187 					 * as there are parens around it.  To support applying
12188 					 * subscripting or field selection to a sub-SELECT result,
12189 					 * we need this redundant-looking production.
12190 					 */
12191 					SubLink *n = makeNode(SubLink);
12192 					A_Indirection *a = makeNode(A_Indirection);
12193 					n->subLinkType = EXPR_SUBLINK;
12194 					n->subLinkId = 0;
12195 					n->testexpr = NULL;
12196 					n->operName = NIL;
12197 					n->subselect = $1;
12198 					n->location = @1;
12199 					a->arg = (Node *)n;
12200 					a->indirection = check_indirection($2, yyscanner);
12201 					$$ = (Node *)a;
12202 				}
12203 			| EXISTS select_with_parens
12204 				{
12205 					SubLink *n = makeNode(SubLink);
12206 					n->subLinkType = EXISTS_SUBLINK;
12207 					n->subLinkId = 0;
12208 					n->testexpr = NULL;
12209 					n->operName = NIL;
12210 					n->subselect = $2;
12211 					n->location = @1;
12212 					$$ = (Node *)n;
12213 				}
12214 			| ARRAY select_with_parens
12215 				{
12216 					SubLink *n = makeNode(SubLink);
12217 					n->subLinkType = ARRAY_SUBLINK;
12218 					n->subLinkId = 0;
12219 					n->testexpr = NULL;
12220 					n->operName = NIL;
12221 					n->subselect = $2;
12222 					n->location = @1;
12223 					$$ = (Node *)n;
12224 				}
12225 			| ARRAY array_expr
12226 				{
12227 					A_ArrayExpr *n = (A_ArrayExpr *) $2;
12228 					Assert(IsA(n, A_ArrayExpr));
12229 					/* point outermost A_ArrayExpr to the ARRAY keyword */
12230 					n->location = @1;
12231 					$$ = (Node *)n;
12232 				}
12233 			| explicit_row
12234 				{
12235 					RowExpr *r = makeNode(RowExpr);
12236 					r->args = $1;
12237 					r->row_typeid = InvalidOid;	/* not analyzed yet */
12238 					r->colnames = NIL;	/* to be filled in during analysis */
12239 					r->row_format = COERCE_EXPLICIT_CALL; /* abuse */
12240 					r->location = @1;
12241 					$$ = (Node *)r;
12242 				}
12243 			| implicit_row
12244 				{
12245 					RowExpr *r = makeNode(RowExpr);
12246 					r->args = $1;
12247 					r->row_typeid = InvalidOid;	/* not analyzed yet */
12248 					r->colnames = NIL;	/* to be filled in during analysis */
12249 					r->row_format = COERCE_IMPLICIT_CAST; /* abuse */
12250 					r->location = @1;
12251 					$$ = (Node *)r;
12252 				}
12253 			| GROUPING '(' expr_list ')'
12254 			  {
12255 				  GroupingFunc *g = makeNode(GroupingFunc);
12256 				  g->args = $3;
12257 				  g->location = @1;
12258 				  $$ = (Node *)g;
12259 			  }
12260 		;
12261 
12262 func_application: func_name '(' ')'
12263 				{
12264 					$$ = (Node *) makeFuncCall($1, NIL, @1);
12265 				}
12266 			| func_name '(' func_arg_list opt_sort_clause ')'
12267 				{
12268 					FuncCall *n = makeFuncCall($1, $3, @1);
12269 					n->agg_order = $4;
12270 					$$ = (Node *)n;
12271 				}
12272 			| func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'
12273 				{
12274 					FuncCall *n = makeFuncCall($1, list_make1($4), @1);
12275 					n->func_variadic = TRUE;
12276 					n->agg_order = $5;
12277 					$$ = (Node *)n;
12278 				}
12279 			| func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'
12280 				{
12281 					FuncCall *n = makeFuncCall($1, lappend($3, $6), @1);
12282 					n->func_variadic = TRUE;
12283 					n->agg_order = $7;
12284 					$$ = (Node *)n;
12285 				}
12286 			| func_name '(' ALL func_arg_list opt_sort_clause ')'
12287 				{
12288 					FuncCall *n = makeFuncCall($1, $4, @1);
12289 					n->agg_order = $5;
12290 					/* Ideally we'd mark the FuncCall node to indicate
12291 					 * "must be an aggregate", but there's no provision
12292 					 * for that in FuncCall at the moment.
12293 					 */
12294 					$$ = (Node *)n;
12295 				}
12296 			| func_name '(' DISTINCT func_arg_list opt_sort_clause ')'
12297 				{
12298 					FuncCall *n = makeFuncCall($1, $4, @1);
12299 					n->agg_order = $5;
12300 					n->agg_distinct = TRUE;
12301 					$$ = (Node *)n;
12302 				}
12303 			| func_name '(' '*' ')'
12304 				{
12305 					/*
12306 					 * We consider AGGREGATE(*) to invoke a parameterless
12307 					 * aggregate.  This does the right thing for COUNT(*),
12308 					 * and there are no other aggregates in SQL that accept
12309 					 * '*' as parameter.
12310 					 *
12311 					 * The FuncCall node is also marked agg_star = true,
12312 					 * so that later processing can detect what the argument
12313 					 * really was.
12314 					 */
12315 					FuncCall *n = makeFuncCall($1, NIL, @1);
12316 					n->agg_star = TRUE;
12317 					$$ = (Node *)n;
12318 				}
12319 		;
12320 
12321 
12322 /*
12323  * func_expr and its cousin func_expr_windowless are split out from c_expr just
12324  * so that we have classifications for "everything that is a function call or
12325  * looks like one".  This isn't very important, but it saves us having to
12326  * document which variants are legal in places like "FROM function()" or the
12327  * backwards-compatible functional-index syntax for CREATE INDEX.
12328  * (Note that many of the special SQL functions wouldn't actually make any
12329  * sense as functional index entries, but we ignore that consideration here.)
12330  */
12331 func_expr: func_application within_group_clause filter_clause over_clause
12332 				{
12333 					FuncCall *n = (FuncCall *) $1;
12334 					/*
12335 					 * The order clause for WITHIN GROUP and the one for
12336 					 * plain-aggregate ORDER BY share a field, so we have to
12337 					 * check here that at most one is present.  We also check
12338 					 * for DISTINCT and VARIADIC here to give a better error
12339 					 * location.  Other consistency checks are deferred to
12340 					 * parse analysis.
12341 					 */
12342 					if ($2 != NIL)
12343 					{
12344 						if (n->agg_order != NIL)
12345 							ereport(ERROR,
12346 									(errcode(ERRCODE_SYNTAX_ERROR),
12347 									 errmsg("cannot use multiple ORDER BY clauses with WITHIN GROUP"),
12348 									 parser_errposition(@2)));
12349 						if (n->agg_distinct)
12350 							ereport(ERROR,
12351 									(errcode(ERRCODE_SYNTAX_ERROR),
12352 									 errmsg("cannot use DISTINCT with WITHIN GROUP"),
12353 									 parser_errposition(@2)));
12354 						if (n->func_variadic)
12355 							ereport(ERROR,
12356 									(errcode(ERRCODE_SYNTAX_ERROR),
12357 									 errmsg("cannot use VARIADIC with WITHIN GROUP"),
12358 									 parser_errposition(@2)));
12359 						n->agg_order = $2;
12360 						n->agg_within_group = TRUE;
12361 					}
12362 					n->agg_filter = $3;
12363 					n->over = $4;
12364 					$$ = (Node *) n;
12365 				}
12366 			| func_expr_common_subexpr
12367 				{ $$ = $1; }
12368 		;
12369 
12370 /*
12371  * As func_expr but does not accept WINDOW functions directly
12372  * (but they can still be contained in arguments for functions etc).
12373  * Use this when window expressions are not allowed, where needed to
12374  * disambiguate the grammar (e.g. in CREATE INDEX).
12375  */
12376 func_expr_windowless:
12377 			func_application						{ $$ = $1; }
12378 			| func_expr_common_subexpr				{ $$ = $1; }
12379 		;
12380 
12381 /*
12382  * Special expressions that are considered to be functions.
12383  */
12384 func_expr_common_subexpr:
12385 			COLLATION FOR '(' a_expr ')'
12386 				{
12387 					$$ = (Node *) makeFuncCall(SystemFuncName("pg_collation_for"),
12388 											   list_make1($4),
12389 											   @1);
12390 				}
12391 			| CURRENT_DATE
12392 				{
12393 					/*
12394 					 * Translate as "'now'::text::date".
12395 					 *
12396 					 * We cannot use "'now'::date" because coerce_type() will
12397 					 * immediately reduce that to a constant representing
12398 					 * today's date.  We need to delay the conversion until
12399 					 * runtime, else the wrong things will happen when
12400 					 * CURRENT_DATE is used in a column default value or rule.
12401 					 *
12402 					 * This could be simplified if we had a way to generate
12403 					 * an expression tree representing runtime application
12404 					 * of type-input conversion functions.  (As of PG 7.3
12405 					 * that is actually possible, but not clear that we want
12406 					 * to rely on it.)
12407 					 *
12408 					 * The token location is attached to the run-time
12409 					 * typecast, not to the Const, for the convenience of
12410 					 * pg_stat_statements (which doesn't want these constructs
12411 					 * to appear to be replaceable constants).
12412 					 */
12413 					Node *n;
12414 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12415 					$$ = makeTypeCast(n, SystemTypeName("date"), @1);
12416 				}
12417 			| CURRENT_TIME
12418 				{
12419 					/*
12420 					 * Translate as "'now'::text::timetz".
12421 					 * See comments for CURRENT_DATE.
12422 					 */
12423 					Node *n;
12424 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12425 					$$ = makeTypeCast(n, SystemTypeName("timetz"), @1);
12426 				}
12427 			| CURRENT_TIME '(' Iconst ')'
12428 				{
12429 					/*
12430 					 * Translate as "'now'::text::timetz(n)".
12431 					 * See comments for CURRENT_DATE.
12432 					 */
12433 					Node *n;
12434 					TypeName *d;
12435 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12436 					d = SystemTypeName("timetz");
12437 					d->typmods = list_make1(makeIntConst($3, @3));
12438 					$$ = makeTypeCast(n, d, @1);
12439 				}
12440 			| CURRENT_TIMESTAMP
12441 				{
12442 					/*
12443 					 * Translate as "now()", since we have a function that
12444 					 * does exactly what is needed.
12445 					 */
12446 					$$ = (Node *) makeFuncCall(SystemFuncName("now"), NIL, @1);
12447 				}
12448 			| CURRENT_TIMESTAMP '(' Iconst ')'
12449 				{
12450 					/*
12451 					 * Translate as "'now'::text::timestamptz(n)".
12452 					 * See comments for CURRENT_DATE.
12453 					 */
12454 					Node *n;
12455 					TypeName *d;
12456 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12457 					d = SystemTypeName("timestamptz");
12458 					d->typmods = list_make1(makeIntConst($3, @3));
12459 					$$ = makeTypeCast(n, d, @1);
12460 				}
12461 			| LOCALTIME
12462 				{
12463 					/*
12464 					 * Translate as "'now'::text::time".
12465 					 * See comments for CURRENT_DATE.
12466 					 */
12467 					Node *n;
12468 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12469 					$$ = makeTypeCast((Node *)n, SystemTypeName("time"), @1);
12470 				}
12471 			| LOCALTIME '(' Iconst ')'
12472 				{
12473 					/*
12474 					 * Translate as "'now'::text::time(n)".
12475 					 * See comments for CURRENT_DATE.
12476 					 */
12477 					Node *n;
12478 					TypeName *d;
12479 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12480 					d = SystemTypeName("time");
12481 					d->typmods = list_make1(makeIntConst($3, @3));
12482 					$$ = makeTypeCast((Node *)n, d, @1);
12483 				}
12484 			| LOCALTIMESTAMP
12485 				{
12486 					/*
12487 					 * Translate as "'now'::text::timestamp".
12488 					 * See comments for CURRENT_DATE.
12489 					 */
12490 					Node *n;
12491 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12492 					$$ = makeTypeCast(n, SystemTypeName("timestamp"), @1);
12493 				}
12494 			| LOCALTIMESTAMP '(' Iconst ')'
12495 				{
12496 					/*
12497 					 * Translate as "'now'::text::timestamp(n)".
12498 					 * See comments for CURRENT_DATE.
12499 					 */
12500 					Node *n;
12501 					TypeName *d;
12502 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12503 					d = SystemTypeName("timestamp");
12504 					d->typmods = list_make1(makeIntConst($3, @3));
12505 					$$ = makeTypeCast(n, d, @1);
12506 				}
12507 			| CURRENT_ROLE
12508 				{
12509 					$$ = (Node *) makeFuncCall(SystemFuncName("current_user"), NIL, @1);
12510 				}
12511 			| CURRENT_USER
12512 				{
12513 					$$ = (Node *) makeFuncCall(SystemFuncName("current_user"), NIL, @1);
12514 				}
12515 			| SESSION_USER
12516 				{
12517 					$$ = (Node *) makeFuncCall(SystemFuncName("session_user"), NIL, @1);
12518 				}
12519 			| USER
12520 				{
12521 					$$ = (Node *) makeFuncCall(SystemFuncName("current_user"), NIL, @1);
12522 				}
12523 			| CURRENT_CATALOG
12524 				{
12525 					$$ = (Node *) makeFuncCall(SystemFuncName("current_database"), NIL, @1);
12526 				}
12527 			| CURRENT_SCHEMA
12528 				{
12529 					$$ = (Node *) makeFuncCall(SystemFuncName("current_schema"), NIL, @1);
12530 				}
12531 			| CAST '(' a_expr AS Typename ')'
12532 				{ $$ = makeTypeCast($3, $5, @1); }
12533 			| EXTRACT '(' extract_list ')'
12534 				{
12535 					$$ = (Node *) makeFuncCall(SystemFuncName("date_part"), $3, @1);
12536 				}
12537 			| OVERLAY '(' overlay_list ')'
12538 				{
12539 					/* overlay(A PLACING B FROM C FOR D) is converted to
12540 					 * overlay(A, B, C, D)
12541 					 * overlay(A PLACING B FROM C) is converted to
12542 					 * overlay(A, B, C)
12543 					 */
12544 					$$ = (Node *) makeFuncCall(SystemFuncName("overlay"), $3, @1);
12545 				}
12546 			| POSITION '(' position_list ')'
12547 				{
12548 					/* position(A in B) is converted to position(B, A) */
12549 					$$ = (Node *) makeFuncCall(SystemFuncName("position"), $3, @1);
12550 				}
12551 			| SUBSTRING '(' substr_list ')'
12552 				{
12553 					/* substring(A from B for C) is converted to
12554 					 * substring(A, B, C) - thomas 2000-11-28
12555 					 */
12556 					$$ = (Node *) makeFuncCall(SystemFuncName("substring"), $3, @1);
12557 				}
12558 			| TREAT '(' a_expr AS Typename ')'
12559 				{
12560 					/* TREAT(expr AS target) converts expr of a particular type to target,
12561 					 * which is defined to be a subtype of the original expression.
12562 					 * In SQL99, this is intended for use with structured UDTs,
12563 					 * but let's make this a generally useful form allowing stronger
12564 					 * coercions than are handled by implicit casting.
12565 					 *
12566 					 * Convert SystemTypeName() to SystemFuncName() even though
12567 					 * at the moment they result in the same thing.
12568 					 */
12569 					$$ = (Node *) makeFuncCall(SystemFuncName(((Value *)llast($5->names))->val.str),
12570 												list_make1($3),
12571 												@1);
12572 				}
12573 			| TRIM '(' BOTH trim_list ')'
12574 				{
12575 					/* various trim expressions are defined in SQL
12576 					 * - thomas 1997-07-19
12577 					 */
12578 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $4, @1);
12579 				}
12580 			| TRIM '(' LEADING trim_list ')'
12581 				{
12582 					$$ = (Node *) makeFuncCall(SystemFuncName("ltrim"), $4, @1);
12583 				}
12584 			| TRIM '(' TRAILING trim_list ')'
12585 				{
12586 					$$ = (Node *) makeFuncCall(SystemFuncName("rtrim"), $4, @1);
12587 				}
12588 			| TRIM '(' trim_list ')'
12589 				{
12590 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $3, @1);
12591 				}
12592 			| NULLIF '(' a_expr ',' a_expr ')'
12593 				{
12594 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
12595 				}
12596 			| COALESCE '(' expr_list ')'
12597 				{
12598 					CoalesceExpr *c = makeNode(CoalesceExpr);
12599 					c->args = $3;
12600 					c->location = @1;
12601 					$$ = (Node *)c;
12602 				}
12603 			| GREATEST '(' expr_list ')'
12604 				{
12605 					MinMaxExpr *v = makeNode(MinMaxExpr);
12606 					v->args = $3;
12607 					v->op = IS_GREATEST;
12608 					v->location = @1;
12609 					$$ = (Node *)v;
12610 				}
12611 			| LEAST '(' expr_list ')'
12612 				{
12613 					MinMaxExpr *v = makeNode(MinMaxExpr);
12614 					v->args = $3;
12615 					v->op = IS_LEAST;
12616 					v->location = @1;
12617 					$$ = (Node *)v;
12618 				}
12619 			| XMLCONCAT '(' expr_list ')'
12620 				{
12621 					$$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
12622 				}
12623 			| XMLELEMENT '(' NAME_P ColLabel ')'
12624 				{
12625 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
12626 				}
12627 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
12628 				{
12629 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
12630 				}
12631 			| XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
12632 				{
12633 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
12634 				}
12635 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
12636 				{
12637 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
12638 				}
12639 			| XMLEXISTS '(' c_expr xmlexists_argument ')'
12640 				{
12641 					/* xmlexists(A PASSING [BY REF] B [BY REF]) is
12642 					 * converted to xmlexists(A, B)*/
12643 					$$ = (Node *) makeFuncCall(SystemFuncName("xmlexists"), list_make2($3, $4), @1);
12644 				}
12645 			| XMLFOREST '(' xml_attribute_list ')'
12646 				{
12647 					$$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
12648 				}
12649 			| XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
12650 				{
12651 					XmlExpr *x = (XmlExpr *)
12652 						makeXmlExpr(IS_XMLPARSE, NULL, NIL,
12653 									list_make2($4, makeBoolAConst($5, -1)),
12654 									@1);
12655 					x->xmloption = $3;
12656 					$$ = (Node *)x;
12657 				}
12658 			| XMLPI '(' NAME_P ColLabel ')'
12659 				{
12660 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
12661 				}
12662 			| XMLPI '(' NAME_P ColLabel ',' a_expr ')'
12663 				{
12664 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
12665 				}
12666 			| XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
12667 				{
12668 					$$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
12669 									 list_make3($3, $5, $6), @1);
12670 				}
12671 			| XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
12672 				{
12673 					XmlSerialize *n = makeNode(XmlSerialize);
12674 					n->xmloption = $3;
12675 					n->expr = $4;
12676 					n->typeName = $6;
12677 					n->location = @1;
12678 					$$ = (Node *)n;
12679 				}
12680 		;
12681 
12682 /*
12683  * SQL/XML support
12684  */
12685 xml_root_version: VERSION_P a_expr
12686 				{ $$ = $2; }
12687 			| VERSION_P NO VALUE_P
12688 				{ $$ = makeNullAConst(-1); }
12689 		;
12690 
12691 opt_xml_root_standalone: ',' STANDALONE_P YES_P
12692 				{ $$ = makeIntConst(XML_STANDALONE_YES, -1); }
12693 			| ',' STANDALONE_P NO
12694 				{ $$ = makeIntConst(XML_STANDALONE_NO, -1); }
12695 			| ',' STANDALONE_P NO VALUE_P
12696 				{ $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
12697 			| /*EMPTY*/
12698 				{ $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
12699 		;
12700 
12701 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'	{ $$ = $3; }
12702 		;
12703 
12704 xml_attribute_list:	xml_attribute_el					{ $$ = list_make1($1); }
12705 			| xml_attribute_list ',' xml_attribute_el	{ $$ = lappend($1, $3); }
12706 		;
12707 
12708 xml_attribute_el: a_expr AS ColLabel
12709 				{
12710 					$$ = makeNode(ResTarget);
12711 					$$->name = $3;
12712 					$$->indirection = NIL;
12713 					$$->val = (Node *) $1;
12714 					$$->location = @1;
12715 				}
12716 			| a_expr
12717 				{
12718 					$$ = makeNode(ResTarget);
12719 					$$->name = NULL;
12720 					$$->indirection = NIL;
12721 					$$->val = (Node *) $1;
12722 					$$->location = @1;
12723 				}
12724 		;
12725 
12726 document_or_content: DOCUMENT_P						{ $$ = XMLOPTION_DOCUMENT; }
12727 			| CONTENT_P								{ $$ = XMLOPTION_CONTENT; }
12728 		;
12729 
12730 xml_whitespace_option: PRESERVE WHITESPACE_P		{ $$ = TRUE; }
12731 			| STRIP_P WHITESPACE_P					{ $$ = FALSE; }
12732 			| /*EMPTY*/								{ $$ = FALSE; }
12733 		;
12734 
12735 /* We allow several variants for SQL and other compatibility. */
12736 xmlexists_argument:
12737 			PASSING c_expr
12738 				{
12739 					$$ = $2;
12740 				}
12741 			| PASSING c_expr BY REF
12742 				{
12743 					$$ = $2;
12744 				}
12745 			| PASSING BY REF c_expr
12746 				{
12747 					$$ = $4;
12748 				}
12749 			| PASSING BY REF c_expr BY REF
12750 				{
12751 					$$ = $4;
12752 				}
12753 		;
12754 
12755 
12756 /*
12757  * Aggregate decoration clauses
12758  */
12759 within_group_clause:
12760 			WITHIN GROUP_P '(' sort_clause ')'		{ $$ = $4; }
12761 			| /*EMPTY*/								{ $$ = NIL; }
12762 		;
12763 
12764 filter_clause:
12765 			FILTER '(' WHERE a_expr ')'				{ $$ = $4; }
12766 			| /*EMPTY*/								{ $$ = NULL; }
12767 		;
12768 
12769 
12770 /*
12771  * Window Definitions
12772  */
12773 window_clause:
12774 			WINDOW window_definition_list			{ $$ = $2; }
12775 			| /*EMPTY*/								{ $$ = NIL; }
12776 		;
12777 
12778 window_definition_list:
12779 			window_definition						{ $$ = list_make1($1); }
12780 			| window_definition_list ',' window_definition
12781 													{ $$ = lappend($1, $3); }
12782 		;
12783 
12784 window_definition:
12785 			ColId AS window_specification
12786 				{
12787 					WindowDef *n = $3;
12788 					n->name = $1;
12789 					$$ = n;
12790 				}
12791 		;
12792 
12793 over_clause: OVER window_specification
12794 				{ $$ = $2; }
12795 			| OVER ColId
12796 				{
12797 					WindowDef *n = makeNode(WindowDef);
12798 					n->name = $2;
12799 					n->refname = NULL;
12800 					n->partitionClause = NIL;
12801 					n->orderClause = NIL;
12802 					n->frameOptions = FRAMEOPTION_DEFAULTS;
12803 					n->startOffset = NULL;
12804 					n->endOffset = NULL;
12805 					n->location = @2;
12806 					$$ = n;
12807 				}
12808 			| /*EMPTY*/
12809 				{ $$ = NULL; }
12810 		;
12811 
12812 window_specification: '(' opt_existing_window_name opt_partition_clause
12813 						opt_sort_clause opt_frame_clause ')'
12814 				{
12815 					WindowDef *n = makeNode(WindowDef);
12816 					n->name = NULL;
12817 					n->refname = $2;
12818 					n->partitionClause = $3;
12819 					n->orderClause = $4;
12820 					/* copy relevant fields of opt_frame_clause */
12821 					n->frameOptions = $5->frameOptions;
12822 					n->startOffset = $5->startOffset;
12823 					n->endOffset = $5->endOffset;
12824 					n->location = @1;
12825 					$$ = n;
12826 				}
12827 		;
12828 
12829 /*
12830  * If we see PARTITION, RANGE, or ROWS as the first token after the '('
12831  * of a window_specification, we want the assumption to be that there is
12832  * no existing_window_name; but those keywords are unreserved and so could
12833  * be ColIds.  We fix this by making them have the same precedence as IDENT
12834  * and giving the empty production here a slightly higher precedence, so
12835  * that the shift/reduce conflict is resolved in favor of reducing the rule.
12836  * These keywords are thus precluded from being an existing_window_name but
12837  * are not reserved for any other purpose.
12838  */
12839 opt_existing_window_name: ColId						{ $$ = $1; }
12840 			| /*EMPTY*/				%prec Op		{ $$ = NULL; }
12841 		;
12842 
12843 opt_partition_clause: PARTITION BY expr_list		{ $$ = $3; }
12844 			| /*EMPTY*/								{ $$ = NIL; }
12845 		;
12846 
12847 /*
12848  * For frame clauses, we return a WindowDef, but only some fields are used:
12849  * frameOptions, startOffset, and endOffset.
12850  *
12851  * This is only a subset of the full SQL:2008 frame_clause grammar.
12852  * We don't support <window frame exclusion> yet.
12853  */
12854 opt_frame_clause:
12855 			RANGE frame_extent
12856 				{
12857 					WindowDef *n = $2;
12858 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
12859 					if (n->frameOptions & (FRAMEOPTION_START_VALUE_PRECEDING |
12860 										   FRAMEOPTION_END_VALUE_PRECEDING))
12861 						ereport(ERROR,
12862 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
12863 								 errmsg("RANGE PRECEDING is only supported with UNBOUNDED"),
12864 								 parser_errposition(@1)));
12865 					if (n->frameOptions & (FRAMEOPTION_START_VALUE_FOLLOWING |
12866 										   FRAMEOPTION_END_VALUE_FOLLOWING))
12867 						ereport(ERROR,
12868 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
12869 								 errmsg("RANGE FOLLOWING is only supported with UNBOUNDED"),
12870 								 parser_errposition(@1)));
12871 					$$ = n;
12872 				}
12873 			| ROWS frame_extent
12874 				{
12875 					WindowDef *n = $2;
12876 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
12877 					$$ = n;
12878 				}
12879 			| /*EMPTY*/
12880 				{
12881 					WindowDef *n = makeNode(WindowDef);
12882 					n->frameOptions = FRAMEOPTION_DEFAULTS;
12883 					n->startOffset = NULL;
12884 					n->endOffset = NULL;
12885 					$$ = n;
12886 				}
12887 		;
12888 
12889 frame_extent: frame_bound
12890 				{
12891 					WindowDef *n = $1;
12892 					/* reject invalid cases */
12893 					if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
12894 						ereport(ERROR,
12895 								(errcode(ERRCODE_WINDOWING_ERROR),
12896 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
12897 								 parser_errposition(@1)));
12898 					if (n->frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING)
12899 						ereport(ERROR,
12900 								(errcode(ERRCODE_WINDOWING_ERROR),
12901 								 errmsg("frame starting from following row cannot end with current row"),
12902 								 parser_errposition(@1)));
12903 					n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
12904 					$$ = n;
12905 				}
12906 			| BETWEEN frame_bound AND frame_bound
12907 				{
12908 					WindowDef *n1 = $2;
12909 					WindowDef *n2 = $4;
12910 					/* form merged options */
12911 					int		frameOptions = n1->frameOptions;
12912 					/* shift converts START_ options to END_ options */
12913 					frameOptions |= n2->frameOptions << 1;
12914 					frameOptions |= FRAMEOPTION_BETWEEN;
12915 					/* reject invalid cases */
12916 					if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
12917 						ereport(ERROR,
12918 								(errcode(ERRCODE_WINDOWING_ERROR),
12919 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
12920 								 parser_errposition(@2)));
12921 					if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
12922 						ereport(ERROR,
12923 								(errcode(ERRCODE_WINDOWING_ERROR),
12924 								 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
12925 								 parser_errposition(@4)));
12926 					if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
12927 						(frameOptions & FRAMEOPTION_END_VALUE_PRECEDING))
12928 						ereport(ERROR,
12929 								(errcode(ERRCODE_WINDOWING_ERROR),
12930 								 errmsg("frame starting from current row cannot have preceding rows"),
12931 								 parser_errposition(@4)));
12932 					if ((frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING) &&
12933 						(frameOptions & (FRAMEOPTION_END_VALUE_PRECEDING |
12934 										 FRAMEOPTION_END_CURRENT_ROW)))
12935 						ereport(ERROR,
12936 								(errcode(ERRCODE_WINDOWING_ERROR),
12937 								 errmsg("frame starting from following row cannot have preceding rows"),
12938 								 parser_errposition(@4)));
12939 					n1->frameOptions = frameOptions;
12940 					n1->endOffset = n2->startOffset;
12941 					$$ = n1;
12942 				}
12943 		;
12944 
12945 /*
12946  * This is used for both frame start and frame end, with output set up on
12947  * the assumption it's frame start; the frame_extent productions must reject
12948  * invalid cases.
12949  */
12950 frame_bound:
12951 			UNBOUNDED PRECEDING
12952 				{
12953 					WindowDef *n = makeNode(WindowDef);
12954 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
12955 					n->startOffset = NULL;
12956 					n->endOffset = NULL;
12957 					$$ = n;
12958 				}
12959 			| UNBOUNDED FOLLOWING
12960 				{
12961 					WindowDef *n = makeNode(WindowDef);
12962 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
12963 					n->startOffset = NULL;
12964 					n->endOffset = NULL;
12965 					$$ = n;
12966 				}
12967 			| CURRENT_P ROW
12968 				{
12969 					WindowDef *n = makeNode(WindowDef);
12970 					n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
12971 					n->startOffset = NULL;
12972 					n->endOffset = NULL;
12973 					$$ = n;
12974 				}
12975 			| a_expr PRECEDING
12976 				{
12977 					WindowDef *n = makeNode(WindowDef);
12978 					n->frameOptions = FRAMEOPTION_START_VALUE_PRECEDING;
12979 					n->startOffset = $1;
12980 					n->endOffset = NULL;
12981 					$$ = n;
12982 				}
12983 			| a_expr FOLLOWING
12984 				{
12985 					WindowDef *n = makeNode(WindowDef);
12986 					n->frameOptions = FRAMEOPTION_START_VALUE_FOLLOWING;
12987 					n->startOffset = $1;
12988 					n->endOffset = NULL;
12989 					$$ = n;
12990 				}
12991 		;
12992 
12993 
12994 /*
12995  * Supporting nonterminals for expressions.
12996  */
12997 
12998 /* Explicit row production.
12999  *
13000  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
13001  * without conflicting with the parenthesized a_expr production.  Without the
13002  * ROW keyword, there must be more than one a_expr inside the parens.
13003  */
13004 row:		ROW '(' expr_list ')'					{ $$ = $3; }
13005 			| ROW '(' ')'							{ $$ = NIL; }
13006 			| '(' expr_list ',' a_expr ')'			{ $$ = lappend($2, $4); }
13007 		;
13008 
13009 explicit_row:	ROW '(' expr_list ')'				{ $$ = $3; }
13010 			| ROW '(' ')'							{ $$ = NIL; }
13011 		;
13012 
13013 implicit_row:	'(' expr_list ',' a_expr ')'		{ $$ = lappend($2, $4); }
13014 		;
13015 
13016 sub_type:	ANY										{ $$ = ANY_SUBLINK; }
13017 			| SOME									{ $$ = ANY_SUBLINK; }
13018 			| ALL									{ $$ = ALL_SUBLINK; }
13019 		;
13020 
13021 all_Op:		Op										{ $$ = $1; }
13022 			| MathOp								{ $$ = $1; }
13023 		;
13024 
13025 MathOp:		 '+'									{ $$ = "+"; }
13026 			| '-'									{ $$ = "-"; }
13027 			| '*'									{ $$ = "*"; }
13028 			| '/'									{ $$ = "/"; }
13029 			| '%'									{ $$ = "%"; }
13030 			| '^'									{ $$ = "^"; }
13031 			| '<'									{ $$ = "<"; }
13032 			| '>'									{ $$ = ">"; }
13033 			| '='									{ $$ = "="; }
13034 			| LESS_EQUALS							{ $$ = "<="; }
13035 			| GREATER_EQUALS						{ $$ = ">="; }
13036 			| NOT_EQUALS							{ $$ = "<>"; }
13037 		;
13038 
13039 qual_Op:	Op
13040 					{ $$ = list_make1(makeString($1)); }
13041 			| OPERATOR '(' any_operator ')'
13042 					{ $$ = $3; }
13043 		;
13044 
13045 qual_all_Op:
13046 			all_Op
13047 					{ $$ = list_make1(makeString($1)); }
13048 			| OPERATOR '(' any_operator ')'
13049 					{ $$ = $3; }
13050 		;
13051 
13052 subquery_Op:
13053 			all_Op
13054 					{ $$ = list_make1(makeString($1)); }
13055 			| OPERATOR '(' any_operator ')'
13056 					{ $$ = $3; }
13057 			| LIKE
13058 					{ $$ = list_make1(makeString("~~")); }
13059 			| NOT_LA LIKE
13060 					{ $$ = list_make1(makeString("!~~")); }
13061 			| ILIKE
13062 					{ $$ = list_make1(makeString("~~*")); }
13063 			| NOT_LA ILIKE
13064 					{ $$ = list_make1(makeString("!~~*")); }
13065 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
13066  * the regular expression is preprocessed by a function (similar_escape),
13067  * and the ~ operator for posix regular expressions is used.
13068  *        x SIMILAR TO y     ->    x ~ similar_escape(y)
13069  * this transformation is made on the fly by the parser upwards.
13070  * however the SubLink structure which handles any/some/all stuff
13071  * is not ready for such a thing.
13072  */
13073 			;
13074 
13075 expr_list:	a_expr
13076 				{
13077 					$$ = list_make1($1);
13078 				}
13079 			| expr_list ',' a_expr
13080 				{
13081 					$$ = lappend($1, $3);
13082 				}
13083 		;
13084 
13085 /* function arguments can have names */
13086 func_arg_list:  func_arg_expr
13087 				{
13088 					$$ = list_make1($1);
13089 				}
13090 			| func_arg_list ',' func_arg_expr
13091 				{
13092 					$$ = lappend($1, $3);
13093 				}
13094 		;
13095 
13096 func_arg_expr:  a_expr
13097 				{
13098 					$$ = $1;
13099 				}
13100 			| param_name COLON_EQUALS a_expr
13101 				{
13102 					NamedArgExpr *na = makeNode(NamedArgExpr);
13103 					na->name = $1;
13104 					na->arg = (Expr *) $3;
13105 					na->argnumber = -1;		/* until determined */
13106 					na->location = @1;
13107 					$$ = (Node *) na;
13108 				}
13109 			| param_name EQUALS_GREATER a_expr
13110 				{
13111 					NamedArgExpr *na = makeNode(NamedArgExpr);
13112 					na->name = $1;
13113 					na->arg = (Expr *) $3;
13114 					na->argnumber = -1;		/* until determined */
13115 					na->location = @1;
13116 					$$ = (Node *) na;
13117 				}
13118 		;
13119 
13120 type_list:	Typename								{ $$ = list_make1($1); }
13121 			| type_list ',' Typename				{ $$ = lappend($1, $3); }
13122 		;
13123 
13124 array_expr: '[' expr_list ']'
13125 				{
13126 					$$ = makeAArrayExpr($2, @1);
13127 				}
13128 			| '[' array_expr_list ']'
13129 				{
13130 					$$ = makeAArrayExpr($2, @1);
13131 				}
13132 			| '[' ']'
13133 				{
13134 					$$ = makeAArrayExpr(NIL, @1);
13135 				}
13136 		;
13137 
13138 array_expr_list: array_expr							{ $$ = list_make1($1); }
13139 			| array_expr_list ',' array_expr		{ $$ = lappend($1, $3); }
13140 		;
13141 
13142 
13143 extract_list:
13144 			extract_arg FROM a_expr
13145 				{
13146 					$$ = list_make2(makeStringConst($1, @1), $3);
13147 				}
13148 			| /*EMPTY*/								{ $$ = NIL; }
13149 		;
13150 
13151 /* Allow delimited string Sconst in extract_arg as an SQL extension.
13152  * - thomas 2001-04-12
13153  */
13154 extract_arg:
13155 			IDENT									{ $$ = $1; }
13156 			| YEAR_P								{ $$ = "year"; }
13157 			| MONTH_P								{ $$ = "month"; }
13158 			| DAY_P									{ $$ = "day"; }
13159 			| HOUR_P								{ $$ = "hour"; }
13160 			| MINUTE_P								{ $$ = "minute"; }
13161 			| SECOND_P								{ $$ = "second"; }
13162 			| Sconst								{ $$ = $1; }
13163 		;
13164 
13165 /* OVERLAY() arguments
13166  * SQL99 defines the OVERLAY() function:
13167  * o overlay(text placing text from int for int)
13168  * o overlay(text placing text from int)
13169  * and similarly for binary strings
13170  */
13171 overlay_list:
13172 			a_expr overlay_placing substr_from substr_for
13173 				{
13174 					$$ = list_make4($1, $2, $3, $4);
13175 				}
13176 			| a_expr overlay_placing substr_from
13177 				{
13178 					$$ = list_make3($1, $2, $3);
13179 				}
13180 		;
13181 
13182 overlay_placing:
13183 			PLACING a_expr
13184 				{ $$ = $2; }
13185 		;
13186 
13187 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
13188 
13189 position_list:
13190 			b_expr IN_P b_expr						{ $$ = list_make2($3, $1); }
13191 			| /*EMPTY*/								{ $$ = NIL; }
13192 		;
13193 
13194 /* SUBSTRING() arguments
13195  * SQL9x defines a specific syntax for arguments to SUBSTRING():
13196  * o substring(text from int for int)
13197  * o substring(text from int) get entire string from starting point "int"
13198  * o substring(text for int) get first "int" characters of string
13199  * o substring(text from pattern) get entire string matching pattern
13200  * o substring(text from pattern for escape) same with specified escape char
13201  * We also want to support generic substring functions which accept
13202  * the usual generic list of arguments. So we will accept both styles
13203  * here, and convert the SQL9x style to the generic list for further
13204  * processing. - thomas 2000-11-28
13205  */
13206 substr_list:
13207 			a_expr substr_from substr_for
13208 				{
13209 					$$ = list_make3($1, $2, $3);
13210 				}
13211 			| a_expr substr_for substr_from
13212 				{
13213 					/* not legal per SQL99, but might as well allow it */
13214 					$$ = list_make3($1, $3, $2);
13215 				}
13216 			| a_expr substr_from
13217 				{
13218 					$$ = list_make2($1, $2);
13219 				}
13220 			| a_expr substr_for
13221 				{
13222 					/*
13223 					 * Since there are no cases where this syntax allows
13224 					 * a textual FOR value, we forcibly cast the argument
13225 					 * to int4.  The possible matches in pg_proc are
13226 					 * substring(text,int4) and substring(text,text),
13227 					 * and we don't want the parser to choose the latter,
13228 					 * which it is likely to do if the second argument
13229 					 * is unknown or doesn't have an implicit cast to int4.
13230 					 */
13231 					$$ = list_make3($1, makeIntConst(1, -1),
13232 									makeTypeCast($2,
13233 												 SystemTypeName("int4"), -1));
13234 				}
13235 			| expr_list
13236 				{
13237 					$$ = $1;
13238 				}
13239 			| /*EMPTY*/
13240 				{ $$ = NIL; }
13241 		;
13242 
13243 substr_from:
13244 			FROM a_expr								{ $$ = $2; }
13245 		;
13246 
13247 substr_for: FOR a_expr								{ $$ = $2; }
13248 		;
13249 
13250 trim_list:	a_expr FROM expr_list					{ $$ = lappend($3, $1); }
13251 			| FROM expr_list						{ $$ = $2; }
13252 			| expr_list								{ $$ = $1; }
13253 		;
13254 
13255 in_expr:	select_with_parens
13256 				{
13257 					SubLink *n = makeNode(SubLink);
13258 					n->subselect = $1;
13259 					/* other fields will be filled later */
13260 					$$ = (Node *)n;
13261 				}
13262 			| '(' expr_list ')'						{ $$ = (Node *)$2; }
13263 		;
13264 
13265 /*
13266  * Define SQL-style CASE clause.
13267  * - Full specification
13268  *	CASE WHEN a = b THEN c ... ELSE d END
13269  * - Implicit argument
13270  *	CASE a WHEN b THEN c ... ELSE d END
13271  */
13272 case_expr:	CASE case_arg when_clause_list case_default END_P
13273 				{
13274 					CaseExpr *c = makeNode(CaseExpr);
13275 					c->casetype = InvalidOid; /* not analyzed yet */
13276 					c->arg = (Expr *) $2;
13277 					c->args = $3;
13278 					c->defresult = (Expr *) $4;
13279 					c->location = @1;
13280 					$$ = (Node *)c;
13281 				}
13282 		;
13283 
13284 when_clause_list:
13285 			/* There must be at least one */
13286 			when_clause								{ $$ = list_make1($1); }
13287 			| when_clause_list when_clause			{ $$ = lappend($1, $2); }
13288 		;
13289 
13290 when_clause:
13291 			WHEN a_expr THEN a_expr
13292 				{
13293 					CaseWhen *w = makeNode(CaseWhen);
13294 					w->expr = (Expr *) $2;
13295 					w->result = (Expr *) $4;
13296 					w->location = @1;
13297 					$$ = (Node *)w;
13298 				}
13299 		;
13300 
13301 case_default:
13302 			ELSE a_expr								{ $$ = $2; }
13303 			| /*EMPTY*/								{ $$ = NULL; }
13304 		;
13305 
13306 case_arg:	a_expr									{ $$ = $1; }
13307 			| /*EMPTY*/								{ $$ = NULL; }
13308 		;
13309 
13310 columnref:	ColId
13311 				{
13312 					$$ = makeColumnRef($1, NIL, @1, yyscanner);
13313 				}
13314 			| ColId indirection
13315 				{
13316 					$$ = makeColumnRef($1, $2, @1, yyscanner);
13317 				}
13318 		;
13319 
13320 indirection_el:
13321 			'.' attr_name
13322 				{
13323 					$$ = (Node *) makeString($2);
13324 				}
13325 			| '.' '*'
13326 				{
13327 					$$ = (Node *) makeNode(A_Star);
13328 				}
13329 			| '[' a_expr ']'
13330 				{
13331 					A_Indices *ai = makeNode(A_Indices);
13332 					ai->is_slice = false;
13333 					ai->lidx = NULL;
13334 					ai->uidx = $2;
13335 					$$ = (Node *) ai;
13336 				}
13337 			| '[' opt_slice_bound ':' opt_slice_bound ']'
13338 				{
13339 					A_Indices *ai = makeNode(A_Indices);
13340 					ai->is_slice = true;
13341 					ai->lidx = $2;
13342 					ai->uidx = $4;
13343 					$$ = (Node *) ai;
13344 				}
13345 		;
13346 
13347 opt_slice_bound:
13348 			a_expr									{ $$ = $1; }
13349 			| /*EMPTY*/								{ $$ = NULL; }
13350 		;
13351 
13352 indirection:
13353 			indirection_el							{ $$ = list_make1($1); }
13354 			| indirection indirection_el			{ $$ = lappend($1, $2); }
13355 		;
13356 
13357 opt_indirection:
13358 			/*EMPTY*/								{ $$ = NIL; }
13359 			| opt_indirection indirection_el		{ $$ = lappend($1, $2); }
13360 		;
13361 
13362 opt_asymmetric: ASYMMETRIC
13363 			| /*EMPTY*/
13364 		;
13365 
13366 /*
13367  * The SQL spec defines "contextually typed value expressions" and
13368  * "contextually typed row value constructors", which for our purposes
13369  * are the same as "a_expr" and "row" except that DEFAULT can appear at
13370  * the top level.
13371  */
13372 
13373 ctext_expr:
13374 			a_expr					{ $$ = (Node *) $1; }
13375 			| DEFAULT
13376 				{
13377 					SetToDefault *n = makeNode(SetToDefault);
13378 					n->location = @1;
13379 					$$ = (Node *) n;
13380 				}
13381 		;
13382 
13383 ctext_expr_list:
13384 			ctext_expr								{ $$ = list_make1($1); }
13385 			| ctext_expr_list ',' ctext_expr		{ $$ = lappend($1, $3); }
13386 		;
13387 
13388 /*
13389  * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
13390  * making VALUES a fully reserved word, which will probably break more apps
13391  * than allowing the noise-word is worth.
13392  */
13393 ctext_row: '(' ctext_expr_list ')'					{ $$ = $2; }
13394 		;
13395 
13396 
13397 /*****************************************************************************
13398  *
13399  *	target list for SELECT
13400  *
13401  *****************************************************************************/
13402 
13403 opt_target_list: target_list						{ $$ = $1; }
13404 			| /* EMPTY */							{ $$ = NIL; }
13405 		;
13406 
13407 target_list:
13408 			target_el								{ $$ = list_make1($1); }
13409 			| target_list ',' target_el				{ $$ = lappend($1, $3); }
13410 		;
13411 
13412 target_el:	a_expr AS ColLabel
13413 				{
13414 					$$ = makeNode(ResTarget);
13415 					$$->name = $3;
13416 					$$->indirection = NIL;
13417 					$$->val = (Node *)$1;
13418 					$$->location = @1;
13419 				}
13420 			/*
13421 			 * We support omitting AS only for column labels that aren't
13422 			 * any known keyword.  There is an ambiguity against postfix
13423 			 * operators: is "a ! b" an infix expression, or a postfix
13424 			 * expression and a column label?  We prefer to resolve this
13425 			 * as an infix expression, which we accomplish by assigning
13426 			 * IDENT a precedence higher than POSTFIXOP.
13427 			 */
13428 			| a_expr IDENT
13429 				{
13430 					$$ = makeNode(ResTarget);
13431 					$$->name = $2;
13432 					$$->indirection = NIL;
13433 					$$->val = (Node *)$1;
13434 					$$->location = @1;
13435 				}
13436 			| a_expr
13437 				{
13438 					$$ = makeNode(ResTarget);
13439 					$$->name = NULL;
13440 					$$->indirection = NIL;
13441 					$$->val = (Node *)$1;
13442 					$$->location = @1;
13443 				}
13444 			| '*'
13445 				{
13446 					ColumnRef *n = makeNode(ColumnRef);
13447 					n->fields = list_make1(makeNode(A_Star));
13448 					n->location = @1;
13449 
13450 					$$ = makeNode(ResTarget);
13451 					$$->name = NULL;
13452 					$$->indirection = NIL;
13453 					$$->val = (Node *)n;
13454 					$$->location = @1;
13455 				}
13456 		;
13457 
13458 
13459 /*****************************************************************************
13460  *
13461  *	Names and constants
13462  *
13463  *****************************************************************************/
13464 
13465 qualified_name_list:
13466 			qualified_name							{ $$ = list_make1($1); }
13467 			| qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
13468 		;
13469 
13470 /*
13471  * The production for a qualified relation name has to exactly match the
13472  * production for a qualified func_name, because in a FROM clause we cannot
13473  * tell which we are parsing until we see what comes after it ('(' for a
13474  * func_name, something else for a relation). Therefore we allow 'indirection'
13475  * which may contain subscripts, and reject that case in the C code.
13476  */
13477 qualified_name:
13478 			ColId
13479 				{
13480 					$$ = makeRangeVar(NULL, $1, @1);
13481 				}
13482 			| ColId indirection
13483 				{
13484 					check_qualified_name($2, yyscanner);
13485 					$$ = makeRangeVar(NULL, NULL, @1);
13486 					switch (list_length($2))
13487 					{
13488 						case 1:
13489 							$$->catalogname = NULL;
13490 							$$->schemaname = $1;
13491 							$$->relname = strVal(linitial($2));
13492 							break;
13493 						case 2:
13494 							$$->catalogname = $1;
13495 							$$->schemaname = strVal(linitial($2));
13496 							$$->relname = strVal(lsecond($2));
13497 							break;
13498 						default:
13499 							ereport(ERROR,
13500 									(errcode(ERRCODE_SYNTAX_ERROR),
13501 									 errmsg("improper qualified name (too many dotted names): %s",
13502 											NameListToString(lcons(makeString($1), $2))),
13503 									 parser_errposition(@1)));
13504 							break;
13505 					}
13506 				}
13507 		;
13508 
13509 name_list:	name
13510 					{ $$ = list_make1(makeString($1)); }
13511 			| name_list ',' name
13512 					{ $$ = lappend($1, makeString($3)); }
13513 		;
13514 
13515 
13516 name:		ColId									{ $$ = $1; };
13517 
13518 database_name:
13519 			ColId									{ $$ = $1; };
13520 
13521 access_method:
13522 			ColId									{ $$ = $1; };
13523 
13524 attr_name:	ColLabel								{ $$ = $1; };
13525 
13526 index_name: ColId									{ $$ = $1; };
13527 
13528 file_name:	Sconst									{ $$ = $1; };
13529 
13530 /*
13531  * The production for a qualified func_name has to exactly match the
13532  * production for a qualified columnref, because we cannot tell which we
13533  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
13534  * anything else for a columnref).  Therefore we allow 'indirection' which
13535  * may contain subscripts, and reject that case in the C code.  (If we
13536  * ever implement SQL99-like methods, such syntax may actually become legal!)
13537  */
13538 func_name:	type_function_name
13539 					{ $$ = list_make1(makeString($1)); }
13540 			| ColId indirection
13541 					{
13542 						$$ = check_func_name(lcons(makeString($1), $2),
13543 											 yyscanner);
13544 					}
13545 		;
13546 
13547 
13548 /*
13549  * Constants
13550  */
13551 AexprConst: Iconst
13552 				{
13553 					$$ = makeIntConst($1, @1);
13554 				}
13555 			| FCONST
13556 				{
13557 					$$ = makeFloatConst($1, @1);
13558 				}
13559 			| Sconst
13560 				{
13561 					$$ = makeStringConst($1, @1);
13562 				}
13563 			| BCONST
13564 				{
13565 					$$ = makeBitStringConst($1, @1);
13566 				}
13567 			| XCONST
13568 				{
13569 					/* This is a bit constant per SQL99:
13570 					 * Without Feature F511, "BIT data type",
13571 					 * a <general literal> shall not be a
13572 					 * <bit string literal> or a <hex string literal>.
13573 					 */
13574 					$$ = makeBitStringConst($1, @1);
13575 				}
13576 			| func_name Sconst
13577 				{
13578 					/* generic type 'literal' syntax */
13579 					TypeName *t = makeTypeNameFromNameList($1);
13580 					t->location = @1;
13581 					$$ = makeStringConstCast($2, @2, t);
13582 				}
13583 			| func_name '(' func_arg_list opt_sort_clause ')' Sconst
13584 				{
13585 					/* generic syntax with a type modifier */
13586 					TypeName *t = makeTypeNameFromNameList($1);
13587 					ListCell *lc;
13588 
13589 					/*
13590 					 * We must use func_arg_list and opt_sort_clause in the
13591 					 * production to avoid reduce/reduce conflicts, but we
13592 					 * don't actually wish to allow NamedArgExpr in this
13593 					 * context, nor ORDER BY.
13594 					 */
foreach(lc,$3)13595 					foreach(lc, $3)
13596 					{
13597 						NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
13598 
13599 						if (IsA(arg, NamedArgExpr))
13600 							ereport(ERROR,
13601 									(errcode(ERRCODE_SYNTAX_ERROR),
13602 									 errmsg("type modifier cannot have parameter name"),
13603 									 parser_errposition(arg->location)));
13604 					}
13605 					if ($4 != NIL)
13606 							ereport(ERROR,
13607 									(errcode(ERRCODE_SYNTAX_ERROR),
13608 									 errmsg("type modifier cannot have ORDER BY"),
13609 									 parser_errposition(@4)));
13610 
13611 					t->typmods = $3;
13612 					t->location = @1;
13613 					$$ = makeStringConstCast($6, @6, t);
13614 				}
13615 			| ConstTypename Sconst
13616 				{
13617 					$$ = makeStringConstCast($2, @2, $1);
13618 				}
13619 			| ConstInterval Sconst opt_interval
13620 				{
13621 					TypeName *t = $1;
13622 					t->typmods = $3;
13623 					$$ = makeStringConstCast($2, @2, t);
13624 				}
13625 			| ConstInterval '(' Iconst ')' Sconst
13626 				{
13627 					TypeName *t = $1;
13628 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
13629 											makeIntConst($3, @3));
13630 					$$ = makeStringConstCast($5, @5, t);
13631 				}
13632 			| TRUE_P
13633 				{
13634 					$$ = makeBoolAConst(TRUE, @1);
13635 				}
13636 			| FALSE_P
13637 				{
13638 					$$ = makeBoolAConst(FALSE, @1);
13639 				}
13640 			| NULL_P
13641 				{
13642 					$$ = makeNullAConst(@1);
13643 				}
13644 		;
13645 
13646 Iconst:		ICONST									{ $$ = $1; };
13647 Sconst:		SCONST									{ $$ = $1; };
13648 
13649 SignedIconst: Iconst								{ $$ = $1; }
13650 			| '+' Iconst							{ $$ = + $2; }
13651 			| '-' Iconst							{ $$ = - $2; }
13652 		;
13653 
13654 /* Role specifications */
13655 RoleId:		RoleSpec
13656 				{
13657 					RoleSpec *spc = (RoleSpec *) $1;
13658 					switch (spc->roletype)
13659 					{
13660 						case ROLESPEC_CSTRING:
13661 							$$ = spc->rolename;
13662 							break;
13663 						case ROLESPEC_PUBLIC:
13664 							ereport(ERROR,
13665 									(errcode(ERRCODE_RESERVED_NAME),
13666 									 errmsg("role name \"%s\" is reserved",
13667 											"public"),
13668 									 parser_errposition(@1)));
13669 						case ROLESPEC_SESSION_USER:
13670 							ereport(ERROR,
13671 									(errcode(ERRCODE_RESERVED_NAME),
13672 									 errmsg("%s cannot be used as a role name here",
13673 											"SESSION_USER"),
13674 									 parser_errposition(@1)));
13675 						case ROLESPEC_CURRENT_USER:
13676 							ereport(ERROR,
13677 									(errcode(ERRCODE_RESERVED_NAME),
13678 									 errmsg("%s cannot be used as a role name here",
13679 											"CURRENT_USER"),
13680 									 parser_errposition(@1)));
13681 					}
13682 				}
13683 			;
13684 
13685 RoleSpec:	NonReservedWord
13686 					{
13687 						/*
13688 						 * "public" and "none" are not keywords, but they must
13689 						 * be treated specially here.
13690 						 */
13691 						RoleSpec *n;
13692 						if (strcmp($1, "public") == 0)
13693 						{
13694 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_PUBLIC, @1);
13695 							n->roletype = ROLESPEC_PUBLIC;
13696 						}
13697 						else if (strcmp($1, "none") == 0)
13698 						{
13699 							ereport(ERROR,
13700 									(errcode(ERRCODE_RESERVED_NAME),
13701 									 errmsg("role name \"%s\" is reserved",
13702 											"none"),
13703 									 parser_errposition(@1)));
13704 						}
13705 						else
13706 						{
13707 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_CSTRING, @1);
13708 							n->rolename = pstrdup($1);
13709 						}
13710 						$$ = (Node *) n;
13711 					}
13712 			| CURRENT_USER
13713 					{
13714 						$$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1);
13715 					}
13716 			| SESSION_USER
13717 					{
13718 						$$ = makeRoleSpec(ROLESPEC_SESSION_USER, @1);
13719 					}
13720 		;
13721 
13722 role_list:	RoleSpec
13723 					{ $$ = list_make1($1); }
13724 			| role_list ',' RoleSpec
13725 					{ $$ = lappend($1, $3); }
13726 		;
13727 
13728 /*
13729  * Name classification hierarchy.
13730  *
13731  * IDENT is the lexeme returned by the lexer for identifiers that match
13732  * no known keyword.  In most cases, we can accept certain keywords as
13733  * names, not only IDENTs.	We prefer to accept as many such keywords
13734  * as possible to minimize the impact of "reserved words" on programmers.
13735  * So, we divide names into several possible classes.  The classification
13736  * is chosen in part to make keywords acceptable as names wherever possible.
13737  */
13738 
13739 /* Column identifier --- names that can be column, table, etc names.
13740  */
13741 ColId:		IDENT									{ $$ = $1; }
13742 			| unreserved_keyword					{ $$ = pstrdup($1); }
13743 			| col_name_keyword						{ $$ = pstrdup($1); }
13744 		;
13745 
13746 /* Type/function identifier --- names that can be type or function names.
13747  */
13748 type_function_name:	IDENT							{ $$ = $1; }
13749 			| unreserved_keyword					{ $$ = pstrdup($1); }
13750 			| type_func_name_keyword				{ $$ = pstrdup($1); }
13751 		;
13752 
13753 /* Any not-fully-reserved word --- these names can be, eg, role names.
13754  */
13755 NonReservedWord:	IDENT							{ $$ = $1; }
13756 			| unreserved_keyword					{ $$ = pstrdup($1); }
13757 			| col_name_keyword						{ $$ = pstrdup($1); }
13758 			| type_func_name_keyword				{ $$ = pstrdup($1); }
13759 		;
13760 
13761 /* Column label --- allowed labels in "AS" clauses.
13762  * This presently includes *all* Postgres keywords.
13763  */
13764 ColLabel:	IDENT									{ $$ = $1; }
13765 			| unreserved_keyword					{ $$ = pstrdup($1); }
13766 			| col_name_keyword						{ $$ = pstrdup($1); }
13767 			| type_func_name_keyword				{ $$ = pstrdup($1); }
13768 			| reserved_keyword						{ $$ = pstrdup($1); }
13769 		;
13770 
13771 
13772 /*
13773  * Keyword category lists.  Generally, every keyword present in
13774  * the Postgres grammar should appear in exactly one of these lists.
13775  *
13776  * Put a new keyword into the first list that it can go into without causing
13777  * shift or reduce conflicts.  The earlier lists define "less reserved"
13778  * categories of keywords.
13779  *
13780  * Make sure that each keyword's category in kwlist.h matches where
13781  * it is listed here.  (Someday we may be able to generate these lists and
13782  * kwlist.h's table from a common master list.)
13783  */
13784 
13785 /* "Unreserved" keywords --- available for use as any kind of name.
13786  */
13787 unreserved_keyword:
13788 			  ABORT_P
13789 			| ABSOLUTE_P
13790 			| ACCESS
13791 			| ACTION
13792 			| ADD_P
13793 			| ADMIN
13794 			| AFTER
13795 			| AGGREGATE
13796 			| ALSO
13797 			| ALTER
13798 			| ALWAYS
13799 			| ASSERTION
13800 			| ASSIGNMENT
13801 			| AT
13802 			| ATTRIBUTE
13803 			| BACKWARD
13804 			| BEFORE
13805 			| BEGIN_P
13806 			| BY
13807 			| CACHE
13808 			| CALLED
13809 			| CASCADE
13810 			| CASCADED
13811 			| CATALOG_P
13812 			| CHAIN
13813 			| CHARACTERISTICS
13814 			| CHECKPOINT
13815 			| CLASS
13816 			| CLOSE
13817 			| CLUSTER
13818 			| COMMENT
13819 			| COMMENTS
13820 			| COMMIT
13821 			| COMMITTED
13822 			| CONFIGURATION
13823 			| CONFLICT
13824 			| CONNECTION
13825 			| CONSTRAINTS
13826 			| CONTENT_P
13827 			| CONTINUE_P
13828 			| CONVERSION_P
13829 			| COPY
13830 			| COST
13831 			| CSV
13832 			| CUBE
13833 			| CURRENT_P
13834 			| CURSOR
13835 			| CYCLE
13836 			| DATA_P
13837 			| DATABASE
13838 			| DAY_P
13839 			| DEALLOCATE
13840 			| DECLARE
13841 			| DEFAULTS
13842 			| DEFERRED
13843 			| DEFINER
13844 			| DELETE_P
13845 			| DELIMITER
13846 			| DELIMITERS
13847 			| DEPENDS
13848 			| DICTIONARY
13849 			| DISABLE_P
13850 			| DISCARD
13851 			| DOCUMENT_P
13852 			| DOMAIN_P
13853 			| DOUBLE_P
13854 			| DROP
13855 			| EACH
13856 			| ENABLE_P
13857 			| ENCODING
13858 			| ENCRYPTED
13859 			| ENUM_P
13860 			| ESCAPE
13861 			| EVENT
13862 			| EXCLUDE
13863 			| EXCLUDING
13864 			| EXCLUSIVE
13865 			| EXECUTE
13866 			| EXPLAIN
13867 			| EXTENSION
13868 			| EXTERNAL
13869 			| FAMILY
13870 			| FILTER
13871 			| FIRST_P
13872 			| FOLLOWING
13873 			| FORCE
13874 			| FORWARD
13875 			| FUNCTION
13876 			| FUNCTIONS
13877 			| GLOBAL
13878 			| GRANTED
13879 			| HANDLER
13880 			| HEADER_P
13881 			| HOLD
13882 			| HOUR_P
13883 			| IDENTITY_P
13884 			| IF_P
13885 			| IMMEDIATE
13886 			| IMMUTABLE
13887 			| IMPLICIT_P
13888 			| IMPORT_P
13889 			| INCLUDING
13890 			| INCREMENT
13891 			| INDEX
13892 			| INDEXES
13893 			| INHERIT
13894 			| INHERITS
13895 			| INLINE_P
13896 			| INPUT_P
13897 			| INSENSITIVE
13898 			| INSERT
13899 			| INSTEAD
13900 			| INVOKER
13901 			| ISOLATION
13902 			| KEY
13903 			| LABEL
13904 			| LANGUAGE
13905 			| LARGE_P
13906 			| LAST_P
13907 			| LEAKPROOF
13908 			| LEVEL
13909 			| LISTEN
13910 			| LOAD
13911 			| LOCAL
13912 			| LOCATION
13913 			| LOCK_P
13914 			| LOCKED
13915 			| LOGGED
13916 			| MAPPING
13917 			| MATCH
13918 			| MATERIALIZED
13919 			| MAXVALUE
13920 			| METHOD
13921 			| MINUTE_P
13922 			| MINVALUE
13923 			| MODE
13924 			| MONTH_P
13925 			| MOVE
13926 			| NAME_P
13927 			| NAMES
13928 			| NEXT
13929 			| NO
13930 			| NOTHING
13931 			| NOTIFY
13932 			| NOWAIT
13933 			| NULLS_P
13934 			| OBJECT_P
13935 			| OF
13936 			| OFF
13937 			| OIDS
13938 			| OPERATOR
13939 			| OPTION
13940 			| OPTIONS
13941 			| ORDINALITY
13942 			| OVER
13943 			| OWNED
13944 			| OWNER
13945 			| PARALLEL
13946 			| PARSER
13947 			| PARTIAL
13948 			| PARTITION
13949 			| PASSING
13950 			| PASSWORD
13951 			| PLANS
13952 			| POLICY
13953 			| PRECEDING
13954 			| PREPARE
13955 			| PREPARED
13956 			| PRESERVE
13957 			| PRIOR
13958 			| PRIVILEGES
13959 			| PROCEDURAL
13960 			| PROCEDURE
13961 			| PROGRAM
13962 			| QUOTE
13963 			| RANGE
13964 			| READ
13965 			| REASSIGN
13966 			| RECHECK
13967 			| RECURSIVE
13968 			| REF
13969 			| REFRESH
13970 			| REINDEX
13971 			| RELATIVE_P
13972 			| RELEASE
13973 			| RENAME
13974 			| REPEATABLE
13975 			| REPLACE
13976 			| REPLICA
13977 			| RESET
13978 			| RESTART
13979 			| RESTRICT
13980 			| RETURNS
13981 			| REVOKE
13982 			| ROLE
13983 			| ROLLBACK
13984 			| ROLLUP
13985 			| ROWS
13986 			| RULE
13987 			| SAVEPOINT
13988 			| SCHEMA
13989 			| SCROLL
13990 			| SEARCH
13991 			| SECOND_P
13992 			| SECURITY
13993 			| SEQUENCE
13994 			| SEQUENCES
13995 			| SERIALIZABLE
13996 			| SERVER
13997 			| SESSION
13998 			| SET
13999 			| SETS
14000 			| SHARE
14001 			| SHOW
14002 			| SIMPLE
14003 			| SKIP
14004 			| SNAPSHOT
14005 			| SQL_P
14006 			| STABLE
14007 			| STANDALONE_P
14008 			| START
14009 			| STATEMENT
14010 			| STATISTICS
14011 			| STDIN
14012 			| STDOUT
14013 			| STORAGE
14014 			| STRICT_P
14015 			| STRIP_P
14016 			| SYSID
14017 			| SYSTEM_P
14018 			| TABLES
14019 			| TABLESPACE
14020 			| TEMP
14021 			| TEMPLATE
14022 			| TEMPORARY
14023 			| TEXT_P
14024 			| TRANSACTION
14025 			| TRANSFORM
14026 			| TRIGGER
14027 			| TRUNCATE
14028 			| TRUSTED
14029 			| TYPE_P
14030 			| TYPES_P
14031 			| UNBOUNDED
14032 			| UNCOMMITTED
14033 			| UNENCRYPTED
14034 			| UNKNOWN
14035 			| UNLISTEN
14036 			| UNLOGGED
14037 			| UNTIL
14038 			| UPDATE
14039 			| VACUUM
14040 			| VALID
14041 			| VALIDATE
14042 			| VALIDATOR
14043 			| VALUE_P
14044 			| VARYING
14045 			| VERSION_P
14046 			| VIEW
14047 			| VIEWS
14048 			| VOLATILE
14049 			| WHITESPACE_P
14050 			| WITHIN
14051 			| WITHOUT
14052 			| WORK
14053 			| WRAPPER
14054 			| WRITE
14055 			| XML_P
14056 			| YEAR_P
14057 			| YES_P
14058 			| ZONE
14059 		;
14060 
14061 /* Column identifier --- keywords that can be column, table, etc names.
14062  *
14063  * Many of these keywords will in fact be recognized as type or function
14064  * names too; but they have special productions for the purpose, and so
14065  * can't be treated as "generic" type or function names.
14066  *
14067  * The type names appearing here are not usable as function names
14068  * because they can be followed by '(' in typename productions, which
14069  * looks too much like a function call for an LR(1) parser.
14070  */
14071 col_name_keyword:
14072 			  BETWEEN
14073 			| BIGINT
14074 			| BIT
14075 			| BOOLEAN_P
14076 			| CHAR_P
14077 			| CHARACTER
14078 			| COALESCE
14079 			| DEC
14080 			| DECIMAL_P
14081 			| EXISTS
14082 			| EXTRACT
14083 			| FLOAT_P
14084 			| GREATEST
14085 			| GROUPING
14086 			| INOUT
14087 			| INT_P
14088 			| INTEGER
14089 			| INTERVAL
14090 			| LEAST
14091 			| NATIONAL
14092 			| NCHAR
14093 			| NONE
14094 			| NULLIF
14095 			| NUMERIC
14096 			| OUT_P
14097 			| OVERLAY
14098 			| POSITION
14099 			| PRECISION
14100 			| REAL
14101 			| ROW
14102 			| SETOF
14103 			| SMALLINT
14104 			| SUBSTRING
14105 			| TIME
14106 			| TIMESTAMP
14107 			| TREAT
14108 			| TRIM
14109 			| VALUES
14110 			| VARCHAR
14111 			| XMLATTRIBUTES
14112 			| XMLCONCAT
14113 			| XMLELEMENT
14114 			| XMLEXISTS
14115 			| XMLFOREST
14116 			| XMLPARSE
14117 			| XMLPI
14118 			| XMLROOT
14119 			| XMLSERIALIZE
14120 		;
14121 
14122 /* Type/function identifier --- keywords that can be type or function names.
14123  *
14124  * Most of these are keywords that are used as operators in expressions;
14125  * in general such keywords can't be column names because they would be
14126  * ambiguous with variables, but they are unambiguous as function identifiers.
14127  *
14128  * Do not include POSITION, SUBSTRING, etc here since they have explicit
14129  * productions in a_expr to support the goofy SQL9x argument syntax.
14130  * - thomas 2000-11-28
14131  */
14132 type_func_name_keyword:
14133 			  AUTHORIZATION
14134 			| BINARY
14135 			| COLLATION
14136 			| CONCURRENTLY
14137 			| CROSS
14138 			| CURRENT_SCHEMA
14139 			| FREEZE
14140 			| FULL
14141 			| ILIKE
14142 			| INNER_P
14143 			| IS
14144 			| ISNULL
14145 			| JOIN
14146 			| LEFT
14147 			| LIKE
14148 			| NATURAL
14149 			| NOTNULL
14150 			| OUTER_P
14151 			| OVERLAPS
14152 			| RIGHT
14153 			| SIMILAR
14154 			| TABLESAMPLE
14155 			| VERBOSE
14156 		;
14157 
14158 /* Reserved keyword --- these keywords are usable only as a ColLabel.
14159  *
14160  * Keywords appear here if they could not be distinguished from variable,
14161  * type, or function names in some contexts.  Don't put things here unless
14162  * forced to.
14163  */
14164 reserved_keyword:
14165 			  ALL
14166 			| ANALYSE
14167 			| ANALYZE
14168 			| AND
14169 			| ANY
14170 			| ARRAY
14171 			| AS
14172 			| ASC
14173 			| ASYMMETRIC
14174 			| BOTH
14175 			| CASE
14176 			| CAST
14177 			| CHECK
14178 			| COLLATE
14179 			| COLUMN
14180 			| CONSTRAINT
14181 			| CREATE
14182 			| CURRENT_CATALOG
14183 			| CURRENT_DATE
14184 			| CURRENT_ROLE
14185 			| CURRENT_TIME
14186 			| CURRENT_TIMESTAMP
14187 			| CURRENT_USER
14188 			| DEFAULT
14189 			| DEFERRABLE
14190 			| DESC
14191 			| DISTINCT
14192 			| DO
14193 			| ELSE
14194 			| END_P
14195 			| EXCEPT
14196 			| FALSE_P
14197 			| FETCH
14198 			| FOR
14199 			| FOREIGN
14200 			| FROM
14201 			| GRANT
14202 			| GROUP_P
14203 			| HAVING
14204 			| IN_P
14205 			| INITIALLY
14206 			| INTERSECT
14207 			| INTO
14208 			| LATERAL_P
14209 			| LEADING
14210 			| LIMIT
14211 			| LOCALTIME
14212 			| LOCALTIMESTAMP
14213 			| NOT
14214 			| NULL_P
14215 			| OFFSET
14216 			| ON
14217 			| ONLY
14218 			| OR
14219 			| ORDER
14220 			| PLACING
14221 			| PRIMARY
14222 			| REFERENCES
14223 			| RETURNING
14224 			| SELECT
14225 			| SESSION_USER
14226 			| SOME
14227 			| SYMMETRIC
14228 			| TABLE
14229 			| THEN
14230 			| TO
14231 			| TRAILING
14232 			| TRUE_P
14233 			| UNION
14234 			| UNIQUE
14235 			| USER
14236 			| USING
14237 			| VARIADIC
14238 			| WHEN
14239 			| WHERE
14240 			| WINDOW
14241 			| WITH
14242 		;
14243 
14244 %%
14245 
14246 /*
14247  * The signature of this function is required by bison.  However, we
14248  * ignore the passed yylloc and instead use the last token position
14249  * available from the scanner.
14250  */
14251 static void
14252 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
14253 {
14254 	parser_yyerror(msg);
14255 }
14256 
14257 static Node *
makeColumnRef(char * colname,List * indirection,int location,core_yyscan_t yyscanner)14258 makeColumnRef(char *colname, List *indirection,
14259 			  int location, core_yyscan_t yyscanner)
14260 {
14261 	/*
14262 	 * Generate a ColumnRef node, with an A_Indirection node added if there
14263 	 * is any subscripting in the specified indirection list.  However,
14264 	 * any field selection at the start of the indirection list must be
14265 	 * transposed into the "fields" part of the ColumnRef node.
14266 	 */
14267 	ColumnRef  *c = makeNode(ColumnRef);
14268 	int		nfields = 0;
14269 	ListCell *l;
14270 
14271 	c->location = location;
14272 	foreach(l, indirection)
14273 	{
14274 		if (IsA(lfirst(l), A_Indices))
14275 		{
14276 			A_Indirection *i = makeNode(A_Indirection);
14277 
14278 			if (nfields == 0)
14279 			{
14280 				/* easy case - all indirection goes to A_Indirection */
14281 				c->fields = list_make1(makeString(colname));
14282 				i->indirection = check_indirection(indirection, yyscanner);
14283 			}
14284 			else
14285 			{
14286 				/* got to split the list in two */
14287 				i->indirection = check_indirection(list_copy_tail(indirection,
14288 																  nfields),
14289 												   yyscanner);
14290 				indirection = list_truncate(indirection, nfields);
14291 				c->fields = lcons(makeString(colname), indirection);
14292 			}
14293 			i->arg = (Node *) c;
14294 			return (Node *) i;
14295 		}
14296 		else if (IsA(lfirst(l), A_Star))
14297 		{
14298 			/* We only allow '*' at the end of a ColumnRef */
14299 			if (lnext(l) != NULL)
14300 				parser_yyerror("improper use of \"*\"");
14301 		}
14302 		nfields++;
14303 	}
14304 	/* No subscripting, so all indirection gets added to field list */
14305 	c->fields = lcons(makeString(colname), indirection);
14306 	return (Node *) c;
14307 }
14308 
14309 static Node *
makeTypeCast(Node * arg,TypeName * typename,int location)14310 makeTypeCast(Node *arg, TypeName *typename, int location)
14311 {
14312 	TypeCast *n = makeNode(TypeCast);
14313 	n->arg = arg;
14314 	n->typeName = typename;
14315 	n->location = location;
14316 	return (Node *) n;
14317 }
14318 
14319 static Node *
makeStringConst(char * str,int location)14320 makeStringConst(char *str, int location)
14321 {
14322 	A_Const *n = makeNode(A_Const);
14323 
14324 	n->val.type = T_String;
14325 	n->val.val.str = str;
14326 	n->location = location;
14327 
14328 	return (Node *)n;
14329 }
14330 
14331 static Node *
makeStringConstCast(char * str,int location,TypeName * typename)14332 makeStringConstCast(char *str, int location, TypeName *typename)
14333 {
14334 	Node *s = makeStringConst(str, location);
14335 
14336 	return makeTypeCast(s, typename, -1);
14337 }
14338 
14339 static Node *
makeIntConst(int val,int location)14340 makeIntConst(int val, int location)
14341 {
14342 	A_Const *n = makeNode(A_Const);
14343 
14344 	n->val.type = T_Integer;
14345 	n->val.val.ival = val;
14346 	n->location = location;
14347 
14348 	return (Node *)n;
14349 }
14350 
14351 static Node *
makeFloatConst(char * str,int location)14352 makeFloatConst(char *str, int location)
14353 {
14354 	A_Const *n = makeNode(A_Const);
14355 
14356 	n->val.type = T_Float;
14357 	n->val.val.str = str;
14358 	n->location = location;
14359 
14360 	return (Node *)n;
14361 }
14362 
14363 static Node *
makeBitStringConst(char * str,int location)14364 makeBitStringConst(char *str, int location)
14365 {
14366 	A_Const *n = makeNode(A_Const);
14367 
14368 	n->val.type = T_BitString;
14369 	n->val.val.str = str;
14370 	n->location = location;
14371 
14372 	return (Node *)n;
14373 }
14374 
14375 static Node *
makeNullAConst(int location)14376 makeNullAConst(int location)
14377 {
14378 	A_Const *n = makeNode(A_Const);
14379 
14380 	n->val.type = T_Null;
14381 	n->location = location;
14382 
14383 	return (Node *)n;
14384 }
14385 
14386 static Node *
makeAConst(Value * v,int location)14387 makeAConst(Value *v, int location)
14388 {
14389 	Node *n;
14390 
14391 	switch (v->type)
14392 	{
14393 		case T_Float:
14394 			n = makeFloatConst(v->val.str, location);
14395 			break;
14396 
14397 		case T_Integer:
14398 			n = makeIntConst(v->val.ival, location);
14399 			break;
14400 
14401 		case T_String:
14402 		default:
14403 			n = makeStringConst(v->val.str, location);
14404 			break;
14405 	}
14406 
14407 	return n;
14408 }
14409 
14410 /* makeBoolAConst()
14411  * Create an A_Const string node and put it inside a boolean cast.
14412  */
14413 static Node *
makeBoolAConst(bool state,int location)14414 makeBoolAConst(bool state, int location)
14415 {
14416 	A_Const *n = makeNode(A_Const);
14417 
14418 	n->val.type = T_String;
14419 	n->val.val.str = (state ? "t" : "f");
14420 	n->location = location;
14421 
14422 	return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
14423 }
14424 
14425 /* makeRoleSpec
14426  * Create a RoleSpec with the given type
14427  */
14428 static Node *
makeRoleSpec(RoleSpecType type,int location)14429 makeRoleSpec(RoleSpecType type, int location)
14430 {
14431 	RoleSpec *spec = makeNode(RoleSpec);
14432 
14433 	spec->roletype = type;
14434 	spec->location = location;
14435 
14436 	return (Node *) spec;
14437 }
14438 
14439 /* check_qualified_name --- check the result of qualified_name production
14440  *
14441  * It's easiest to let the grammar production for qualified_name allow
14442  * subscripts and '*', which we then must reject here.
14443  */
14444 static void
check_qualified_name(List * names,core_yyscan_t yyscanner)14445 check_qualified_name(List *names, core_yyscan_t yyscanner)
14446 {
14447 	ListCell   *i;
14448 
14449 	foreach(i, names)
14450 	{
14451 		if (!IsA(lfirst(i), String))
14452 			parser_yyerror("syntax error");
14453 	}
14454 }
14455 
14456 /* check_func_name --- check the result of func_name production
14457  *
14458  * It's easiest to let the grammar production for func_name allow subscripts
14459  * and '*', which we then must reject here.
14460  */
14461 static List *
check_func_name(List * names,core_yyscan_t yyscanner)14462 check_func_name(List *names, core_yyscan_t yyscanner)
14463 {
14464 	ListCell   *i;
14465 
14466 	foreach(i, names)
14467 	{
14468 		if (!IsA(lfirst(i), String))
14469 			parser_yyerror("syntax error");
14470 	}
14471 	return names;
14472 }
14473 
14474 /* check_indirection --- check the result of indirection production
14475  *
14476  * We only allow '*' at the end of the list, but it's hard to enforce that
14477  * in the grammar, so do it here.
14478  */
14479 static List *
check_indirection(List * indirection,core_yyscan_t yyscanner)14480 check_indirection(List *indirection, core_yyscan_t yyscanner)
14481 {
14482 	ListCell *l;
14483 
14484 	foreach(l, indirection)
14485 	{
14486 		if (IsA(lfirst(l), A_Star))
14487 		{
14488 			if (lnext(l) != NULL)
14489 				parser_yyerror("improper use of \"*\"");
14490 		}
14491 	}
14492 	return indirection;
14493 }
14494 
14495 /* extractArgTypes()
14496  * Given a list of FunctionParameter nodes, extract a list of just the
14497  * argument types (TypeNames) for input parameters only.  This is what
14498  * is needed to look up an existing function, which is what is wanted by
14499  * the productions that use this call.
14500  */
14501 static List *
extractArgTypes(List * parameters)14502 extractArgTypes(List *parameters)
14503 {
14504 	List	   *result = NIL;
14505 	ListCell   *i;
14506 
14507 	foreach(i, parameters)
14508 	{
14509 		FunctionParameter *p = (FunctionParameter *) lfirst(i);
14510 
14511 		if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
14512 			result = lappend(result, p->argType);
14513 	}
14514 	return result;
14515 }
14516 
14517 /* extractAggrArgTypes()
14518  * As above, but work from the output of the aggr_args production.
14519  */
14520 static List *
extractAggrArgTypes(List * aggrargs)14521 extractAggrArgTypes(List *aggrargs)
14522 {
14523 	Assert(list_length(aggrargs) == 2);
14524 	return extractArgTypes((List *) linitial(aggrargs));
14525 }
14526 
14527 /* makeOrderedSetArgs()
14528  * Build the result of the aggr_args production (which see the comments for).
14529  * This handles only the case where both given lists are nonempty, so that
14530  * we have to deal with multiple VARIADIC arguments.
14531  */
14532 static List *
makeOrderedSetArgs(List * directargs,List * orderedargs,core_yyscan_t yyscanner)14533 makeOrderedSetArgs(List *directargs, List *orderedargs,
14534 				   core_yyscan_t yyscanner)
14535 {
14536 	FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
14537 	Value	   *ndirectargs;
14538 
14539 	/* No restriction unless last direct arg is VARIADIC */
14540 	if (lastd->mode == FUNC_PARAM_VARIADIC)
14541 	{
14542 		FunctionParameter *firsto = (FunctionParameter *) linitial(orderedargs);
14543 
14544 		/*
14545 		 * We ignore the names, though the aggr_arg production allows them;
14546 		 * it doesn't allow default values, so those need not be checked.
14547 		 */
14548 		if (list_length(orderedargs) != 1 ||
14549 			firsto->mode != FUNC_PARAM_VARIADIC ||
14550 			!equal(lastd->argType, firsto->argType))
14551 			ereport(ERROR,
14552 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14553 					 errmsg("an ordered-set aggregate with a VARIADIC direct argument must have one VARIADIC aggregated argument of the same data type"),
14554 					 parser_errposition(exprLocation((Node *) firsto))));
14555 
14556 		/* OK, drop the duplicate VARIADIC argument from the internal form */
14557 		orderedargs = NIL;
14558 	}
14559 
14560 	/* don't merge into the next line, as list_concat changes directargs */
14561 	ndirectargs = makeInteger(list_length(directargs));
14562 
14563 	return list_make2(list_concat(directargs, orderedargs),
14564 					  ndirectargs);
14565 }
14566 
14567 /* insertSelectOptions()
14568  * Insert ORDER BY, etc into an already-constructed SelectStmt.
14569  *
14570  * This routine is just to avoid duplicating code in SelectStmt productions.
14571  */
14572 static void
insertSelectOptions(SelectStmt * stmt,List * sortClause,List * lockingClause,Node * limitOffset,Node * limitCount,WithClause * withClause,core_yyscan_t yyscanner)14573 insertSelectOptions(SelectStmt *stmt,
14574 					List *sortClause, List *lockingClause,
14575 					Node *limitOffset, Node *limitCount,
14576 					WithClause *withClause,
14577 					core_yyscan_t yyscanner)
14578 {
14579 	Assert(IsA(stmt, SelectStmt));
14580 
14581 	/*
14582 	 * Tests here are to reject constructs like
14583 	 *	(SELECT foo ORDER BY bar) ORDER BY baz
14584 	 */
14585 	if (sortClause)
14586 	{
14587 		if (stmt->sortClause)
14588 			ereport(ERROR,
14589 					(errcode(ERRCODE_SYNTAX_ERROR),
14590 					 errmsg("multiple ORDER BY clauses not allowed"),
14591 					 parser_errposition(exprLocation((Node *) sortClause))));
14592 		stmt->sortClause = sortClause;
14593 	}
14594 	/* We can handle multiple locking clauses, though */
14595 	stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
14596 	if (limitOffset)
14597 	{
14598 		if (stmt->limitOffset)
14599 			ereport(ERROR,
14600 					(errcode(ERRCODE_SYNTAX_ERROR),
14601 					 errmsg("multiple OFFSET clauses not allowed"),
14602 					 parser_errposition(exprLocation(limitOffset))));
14603 		stmt->limitOffset = limitOffset;
14604 	}
14605 	if (limitCount)
14606 	{
14607 		if (stmt->limitCount)
14608 			ereport(ERROR,
14609 					(errcode(ERRCODE_SYNTAX_ERROR),
14610 					 errmsg("multiple LIMIT clauses not allowed"),
14611 					 parser_errposition(exprLocation(limitCount))));
14612 		stmt->limitCount = limitCount;
14613 	}
14614 	if (withClause)
14615 	{
14616 		if (stmt->withClause)
14617 			ereport(ERROR,
14618 					(errcode(ERRCODE_SYNTAX_ERROR),
14619 					 errmsg("multiple WITH clauses not allowed"),
14620 					 parser_errposition(exprLocation((Node *) withClause))));
14621 		stmt->withClause = withClause;
14622 	}
14623 }
14624 
14625 static Node *
makeSetOp(SetOperation op,bool all,Node * larg,Node * rarg)14626 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
14627 {
14628 	SelectStmt *n = makeNode(SelectStmt);
14629 
14630 	n->op = op;
14631 	n->all = all;
14632 	n->larg = (SelectStmt *) larg;
14633 	n->rarg = (SelectStmt *) rarg;
14634 	return (Node *) n;
14635 }
14636 
14637 /* SystemFuncName()
14638  * Build a properly-qualified reference to a built-in function.
14639  */
14640 List *
SystemFuncName(char * name)14641 SystemFuncName(char *name)
14642 {
14643 	return list_make2(makeString("pg_catalog"), makeString(name));
14644 }
14645 
14646 /* SystemTypeName()
14647  * Build a properly-qualified reference to a built-in type.
14648  *
14649  * typmod is defaulted, but may be changed afterwards by caller.
14650  * Likewise for the location.
14651  */
14652 TypeName *
SystemTypeName(char * name)14653 SystemTypeName(char *name)
14654 {
14655 	return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
14656 											   makeString(name)));
14657 }
14658 
14659 /* doNegate()
14660  * Handle negation of a numeric constant.
14661  *
14662  * Formerly, we did this here because the optimizer couldn't cope with
14663  * indexquals that looked like "var = -4" --- it wants "var = const"
14664  * and a unary minus operator applied to a constant didn't qualify.
14665  * As of Postgres 7.0, that problem doesn't exist anymore because there
14666  * is a constant-subexpression simplifier in the optimizer.  However,
14667  * there's still a good reason for doing this here, which is that we can
14668  * postpone committing to a particular internal representation for simple
14669  * negative constants.	It's better to leave "-123.456" in string form
14670  * until we know what the desired type is.
14671  */
14672 static Node *
doNegate(Node * n,int location)14673 doNegate(Node *n, int location)
14674 {
14675 	if (IsA(n, A_Const))
14676 	{
14677 		A_Const *con = (A_Const *)n;
14678 
14679 		/* report the constant's location as that of the '-' sign */
14680 		con->location = location;
14681 
14682 		if (con->val.type == T_Integer)
14683 		{
14684 			con->val.val.ival = -con->val.val.ival;
14685 			return n;
14686 		}
14687 		if (con->val.type == T_Float)
14688 		{
14689 			doNegateFloat(&con->val);
14690 			return n;
14691 		}
14692 	}
14693 
14694 	return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
14695 }
14696 
14697 static void
doNegateFloat(Value * v)14698 doNegateFloat(Value *v)
14699 {
14700 	char   *oldval = v->val.str;
14701 
14702 	Assert(IsA(v, Float));
14703 	if (*oldval == '+')
14704 		oldval++;
14705 	if (*oldval == '-')
14706 		v->val.str = oldval+1;	/* just strip the '-' */
14707 	else
14708 		v->val.str = psprintf("-%s", oldval);
14709 }
14710 
14711 static Node *
makeAndExpr(Node * lexpr,Node * rexpr,int location)14712 makeAndExpr(Node *lexpr, Node *rexpr, int location)
14713 {
14714 	Node	   *lexp = lexpr;
14715 
14716 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
14717 	while (IsA(lexp, A_Expr) &&
14718 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
14719 		lexp = ((A_Expr *) lexp)->lexpr;
14720 	/* Flatten "a AND b AND c ..." to a single BoolExpr on sight */
14721 	if (IsA(lexp, BoolExpr))
14722 	{
14723 		BoolExpr *blexpr = (BoolExpr *) lexp;
14724 
14725 		if (blexpr->boolop == AND_EXPR)
14726 		{
14727 			blexpr->args = lappend(blexpr->args, rexpr);
14728 			return (Node *) blexpr;
14729 		}
14730 	}
14731 	return (Node *) makeBoolExpr(AND_EXPR, list_make2(lexpr, rexpr), location);
14732 }
14733 
14734 static Node *
makeOrExpr(Node * lexpr,Node * rexpr,int location)14735 makeOrExpr(Node *lexpr, Node *rexpr, int location)
14736 {
14737 	Node	   *lexp = lexpr;
14738 
14739 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
14740 	while (IsA(lexp, A_Expr) &&
14741 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
14742 		lexp = ((A_Expr *) lexp)->lexpr;
14743 	/* Flatten "a OR b OR c ..." to a single BoolExpr on sight */
14744 	if (IsA(lexp, BoolExpr))
14745 	{
14746 		BoolExpr *blexpr = (BoolExpr *) lexp;
14747 
14748 		if (blexpr->boolop == OR_EXPR)
14749 		{
14750 			blexpr->args = lappend(blexpr->args, rexpr);
14751 			return (Node *) blexpr;
14752 		}
14753 	}
14754 	return (Node *) makeBoolExpr(OR_EXPR, list_make2(lexpr, rexpr), location);
14755 }
14756 
14757 static Node *
makeNotExpr(Node * expr,int location)14758 makeNotExpr(Node *expr, int location)
14759 {
14760 	return (Node *) makeBoolExpr(NOT_EXPR, list_make1(expr), location);
14761 }
14762 
14763 static Node *
makeAArrayExpr(List * elements,int location)14764 makeAArrayExpr(List *elements, int location)
14765 {
14766 	A_ArrayExpr *n = makeNode(A_ArrayExpr);
14767 
14768 	n->elements = elements;
14769 	n->location = location;
14770 	return (Node *) n;
14771 }
14772 
14773 static Node *
makeXmlExpr(XmlExprOp op,char * name,List * named_args,List * args,int location)14774 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
14775 			int location)
14776 {
14777 	XmlExpr		*x = makeNode(XmlExpr);
14778 
14779 	x->op = op;
14780 	x->name = name;
14781 	/*
14782 	 * named_args is a list of ResTarget; it'll be split apart into separate
14783 	 * expression and name lists in transformXmlExpr().
14784 	 */
14785 	x->named_args = named_args;
14786 	x->arg_names = NIL;
14787 	x->args = args;
14788 	/* xmloption, if relevant, must be filled in by caller */
14789 	/* type and typmod will be filled in during parse analysis */
14790 	x->type = InvalidOid;			/* marks the node as not analyzed */
14791 	x->location = location;
14792 	return (Node *) x;
14793 }
14794 
14795 /*
14796  * Merge the input and output parameters of a table function.
14797  */
14798 static List *
mergeTableFuncParameters(List * func_args,List * columns)14799 mergeTableFuncParameters(List *func_args, List *columns)
14800 {
14801 	ListCell   *lc;
14802 
14803 	/* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
14804 	foreach(lc, func_args)
14805 	{
14806 		FunctionParameter *p = (FunctionParameter *) lfirst(lc);
14807 
14808 		if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
14809 			ereport(ERROR,
14810 					(errcode(ERRCODE_SYNTAX_ERROR),
14811 					 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
14812 	}
14813 
14814 	return list_concat(func_args, columns);
14815 }
14816 
14817 /*
14818  * Determine return type of a TABLE function.  A single result column
14819  * returns setof that column's type; otherwise return setof record.
14820  */
14821 static TypeName *
TableFuncTypeName(List * columns)14822 TableFuncTypeName(List *columns)
14823 {
14824 	TypeName *result;
14825 
14826 	if (list_length(columns) == 1)
14827 	{
14828 		FunctionParameter *p = (FunctionParameter *) linitial(columns);
14829 
14830 		result = (TypeName *) copyObject(p->argType);
14831 	}
14832 	else
14833 		result = SystemTypeName("record");
14834 
14835 	result->setof = true;
14836 
14837 	return result;
14838 }
14839 
14840 /*
14841  * Convert a list of (dotted) names to a RangeVar (like
14842  * makeRangeVarFromNameList, but with position support).  The
14843  * "AnyName" refers to the any_name production in the grammar.
14844  */
14845 static RangeVar *
makeRangeVarFromAnyName(List * names,int position,core_yyscan_t yyscanner)14846 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
14847 {
14848 	RangeVar *r = makeNode(RangeVar);
14849 
14850 	switch (list_length(names))
14851 	{
14852 		case 1:
14853 			r->catalogname = NULL;
14854 			r->schemaname = NULL;
14855 			r->relname = strVal(linitial(names));
14856 			break;
14857 		case 2:
14858 			r->catalogname = NULL;
14859 			r->schemaname = strVal(linitial(names));
14860 			r->relname = strVal(lsecond(names));
14861 			break;
14862 		case 3:
14863 			r->catalogname = strVal(linitial(names));
14864 			r->schemaname = strVal(lsecond(names));
14865 			r->relname = strVal(lthird(names));
14866 			break;
14867 		default:
14868 			ereport(ERROR,
14869 					(errcode(ERRCODE_SYNTAX_ERROR),
14870 					 errmsg("improper qualified name (too many dotted names): %s",
14871 							NameListToString(names)),
14872 					 parser_errposition(position)));
14873 			break;
14874 	}
14875 
14876 	r->relpersistence = RELPERSISTENCE_PERMANENT;
14877 	r->location = position;
14878 
14879 	return r;
14880 }
14881 
14882 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
14883 static void
SplitColQualList(List * qualList,List ** constraintList,CollateClause ** collClause,core_yyscan_t yyscanner)14884 SplitColQualList(List *qualList,
14885 				 List **constraintList, CollateClause **collClause,
14886 				 core_yyscan_t yyscanner)
14887 {
14888 	ListCell   *cell;
14889 	ListCell   *prev;
14890 	ListCell   *next;
14891 
14892 	*collClause = NULL;
14893 	prev = NULL;
14894 	for (cell = list_head(qualList); cell; cell = next)
14895 	{
14896 		Node   *n = (Node *) lfirst(cell);
14897 
14898 		next = lnext(cell);
14899 		if (IsA(n, Constraint))
14900 		{
14901 			/* keep it in list */
14902 			prev = cell;
14903 			continue;
14904 		}
14905 		if (IsA(n, CollateClause))
14906 		{
14907 			CollateClause *c = (CollateClause *) n;
14908 
14909 			if (*collClause)
14910 				ereport(ERROR,
14911 						(errcode(ERRCODE_SYNTAX_ERROR),
14912 						 errmsg("multiple COLLATE clauses not allowed"),
14913 						 parser_errposition(c->location)));
14914 			*collClause = c;
14915 		}
14916 		else
14917 			elog(ERROR, "unexpected node type %d", (int) n->type);
14918 		/* remove non-Constraint nodes from qualList */
14919 		qualList = list_delete_cell(qualList, cell, prev);
14920 	}
14921 	*constraintList = qualList;
14922 }
14923 
14924 /*
14925  * Process result of ConstraintAttributeSpec, and set appropriate bool flags
14926  * in the output command node.  Pass NULL for any flags the particular
14927  * command doesn't support.
14928  */
14929 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)14930 processCASbits(int cas_bits, int location, const char *constrType,
14931 			   bool *deferrable, bool *initdeferred, bool *not_valid,
14932 			   bool *no_inherit, core_yyscan_t yyscanner)
14933 {
14934 	/* defaults */
14935 	if (deferrable)
14936 		*deferrable = false;
14937 	if (initdeferred)
14938 		*initdeferred = false;
14939 	if (not_valid)
14940 		*not_valid = false;
14941 
14942 	if (cas_bits & (CAS_DEFERRABLE | CAS_INITIALLY_DEFERRED))
14943 	{
14944 		if (deferrable)
14945 			*deferrable = true;
14946 		else
14947 			ereport(ERROR,
14948 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14949 					 /* translator: %s is CHECK, UNIQUE, or similar */
14950 					 errmsg("%s constraints cannot be marked DEFERRABLE",
14951 							constrType),
14952 					 parser_errposition(location)));
14953 	}
14954 
14955 	if (cas_bits & CAS_INITIALLY_DEFERRED)
14956 	{
14957 		if (initdeferred)
14958 			*initdeferred = true;
14959 		else
14960 			ereport(ERROR,
14961 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14962 					 /* translator: %s is CHECK, UNIQUE, or similar */
14963 					 errmsg("%s constraints cannot be marked DEFERRABLE",
14964 							constrType),
14965 					 parser_errposition(location)));
14966 	}
14967 
14968 	if (cas_bits & CAS_NOT_VALID)
14969 	{
14970 		if (not_valid)
14971 			*not_valid = true;
14972 		else
14973 			ereport(ERROR,
14974 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14975 					 /* translator: %s is CHECK, UNIQUE, or similar */
14976 					 errmsg("%s constraints cannot be marked NOT VALID",
14977 							constrType),
14978 					 parser_errposition(location)));
14979 	}
14980 
14981 	if (cas_bits & CAS_NO_INHERIT)
14982 	{
14983 		if (no_inherit)
14984 			*no_inherit = true;
14985 		else
14986 			ereport(ERROR,
14987 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14988 					 /* translator: %s is CHECK, UNIQUE, or similar */
14989 					 errmsg("%s constraints cannot be marked NO INHERIT",
14990 							constrType),
14991 					 parser_errposition(location)));
14992 	}
14993 }
14994 
14995 /*----------
14996  * Recursive view transformation
14997  *
14998  * Convert
14999  *
15000  *     CREATE RECURSIVE VIEW relname (aliases) AS query
15001  *
15002  * to
15003  *
15004  *     CREATE VIEW relname (aliases) AS
15005  *         WITH RECURSIVE relname (aliases) AS (query)
15006  *         SELECT aliases FROM relname
15007  *
15008  * Actually, just the WITH ... part, which is then inserted into the original
15009  * view definition as the query.
15010  * ----------
15011  */
15012 static Node *
makeRecursiveViewSelect(char * relname,List * aliases,Node * query)15013 makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
15014 {
15015 	SelectStmt *s = makeNode(SelectStmt);
15016 	WithClause *w = makeNode(WithClause);
15017 	CommonTableExpr *cte = makeNode(CommonTableExpr);
15018 	List	   *tl = NIL;
15019 	ListCell   *lc;
15020 
15021 	/* create common table expression */
15022 	cte->ctename = relname;
15023 	cte->aliascolnames = aliases;
15024 	cte->ctequery = query;
15025 	cte->location = -1;
15026 
15027 	/* create WITH clause and attach CTE */
15028 	w->recursive = true;
15029 	w->ctes = list_make1(cte);
15030 	w->location = -1;
15031 
15032 	/* create target list for the new SELECT from the alias list of the
15033 	 * recursive view specification */
15034 	foreach (lc, aliases)
15035 	{
15036 		ResTarget *rt = makeNode(ResTarget);
15037 
15038 		rt->name = NULL;
15039 		rt->indirection = NIL;
15040 		rt->val = makeColumnRef(strVal(lfirst(lc)), NIL, -1, 0);
15041 		rt->location = -1;
15042 
15043 		tl = lappend(tl, rt);
15044 	}
15045 
15046 	/* create new SELECT combining WITH clause, target list, and fake FROM
15047 	 * clause */
15048 	s->withClause = w;
15049 	s->targetList = tl;
15050 	s->fromClause = list_make1(makeRangeVar(NULL, relname, -1));
15051 
15052 	return (Node *) s;
15053 }
15054 
15055 /* parser_init()
15056  * Initialize to parse one query string
15057  */
15058 void
parser_init(base_yy_extra_type * yyext)15059 parser_init(base_yy_extra_type *yyext)
15060 {
15061 	yyext->parsetree = NIL;		/* in case grammar forgets to set it */
15062 }
15063