1 %{
2 
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5  *
6  * gram.y
7  *	  POSTGRESQL BISON rules/actions
8  *
9  * Portions Copyright (c) 2003-2016, PgPool Global Development Group
10  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  *	  src/backend/parser/gram.y
16  *
17  * HISTORY
18  *	  AUTHOR			DATE			MAJOR EVENT
19  *	  Andrew Yu			Sept, 1994		POSTQUEL to SQL conversion
20  *	  Andrew Yu			Oct, 1994		lispy code conversion
21  *
22  * NOTES
23  *	  CAPITALS are used to represent terminal symbols.
24  *	  non-capitals are used to represent non-terminals.
25  *
26  *	  In general, nothing in this file should initiate database accesses
27  *	  nor depend on changeable state (such as SET variables).  If you do
28  *	  database accesses, your code will fail when we have aborted the
29  *	  current transaction and are just parsing commands to find the next
30  *	  ROLLBACK or COMMIT.  If you make use of SET variables, then you
31  *	  will do the wrong thing in multi-query strings like this:
32  *			SET SQL_inheritance TO off; SELECT * FROM foo;
33  *	  because the entire string is parsed by gram.y before the SET gets
34  *	  executed.  Anything that depends on the database or changeable state
35  *	  should be handled during parse analysis so that it happens at the
36  *	  right time not the wrong time.  The handling of SQL_inheritance is
37  *	  a good example.
38  *
39  * WARNINGS
40  *	  If you use a list, make sure the datum is a node so that the printing
41  *	  routines work.
42  *
43  *	  Sometimes we assign constants to makeStrings. Make sure we don't free
44  *	  those.
45  *
46  *-------------------------------------------------------------------------
47  */
48 
49 #include "pool_parser.h"
50 #include "utils/elog.h"
51 #include "utils/palloc.h"
52 #include <ctype.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 
58 #include "nodes.h"
59 #include "keywords.h"
60 #include "gramparse.h"
61 #include "makefuncs.h"
62 #include "pool_string.h"
63 #include "parser.h"
64 #include "pg_class.h"
65 #include "pg_trigger.h"
66 
67 /* This is a configuration parameter since PostgreSQL 9.5.
68  * We set this false in pgpool-II. This is default in PostgreSQL.
69  */
70 bool operator_precedence_warning = false;
71 
72 /*
73  * Definition taken from
74  * postgreSQL source code file: src/include/commands/trigger.h
75  */
76 #define TRIGGER_FIRES_ON_ORIGIN                         'O'
77 #define TRIGGER_FIRES_ALWAYS                            'A'
78 #define TRIGGER_FIRES_ON_REPLICA                        'R'
79 #define TRIGGER_DISABLED                                'D'
80 
81 /*
82  * Definition taken from
83  * postgreSQL source code file: src/include/catalog/pg_class.h
84  */
85 
86 #define           REPLICA_IDENTITY_DEFAULT      'd'
87 #define           REPLICA_IDENTITY_NOTHING      'n'
88 #define           REPLICA_IDENTITY_FULL         'f'
89 #define           REPLICA_IDENTITY_INDEX        'i'
90 
91 /*
92  * Definition taken from
93  * postgreSQL source code file: src/include/utils/xml.h
94  */
95 typedef enum
96 {
97 	XML_STANDALONE_YES,
98 	XML_STANDALONE_NO,
99 	XML_STANDALONE_NO_VALUE,
100 	XML_STANDALONE_OMITTED
101 } XmlStandaloneType;
102 
103 static DefElem *defWithOids(bool value);
104 /*
105  * Location tracking support --- simpler than bison's default, since we only
106  * want to track the start position not the end position of each nonterminal.
107  */
108 #define YYLLOC_DEFAULT(Current, Rhs, N) \
109 	do { \
110 		if ((N) > 0) \
111 			(Current) = (Rhs)[1]; \
112 		else \
113 			(Current) = (-1); \
114 	} while (0)
115 
116 /*
117  * The above macro assigns -1 (unknown) as the parse location of any
118  * nonterminal that was reduced from an empty rule.  This is problematic
119  * for nonterminals defined like
120  *		OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ;
121  * because we'll set -1 as the location during the first reduction and then
122  * copy it during each subsequent reduction, leaving us with -1 for the
123  * location even when the list is not empty.  To fix that, do this in the
124  * action for the nonempty rule(s):
125  *		if (@$ < 0) @$ = @2;
126  * (Although we have many nonterminals that follow this pattern, we only
127  * bother with fixing @$ like this when the nonterminal's parse location
128  * is actually referenced in some rule.)
129  */
130 
131 /*
132  * Bison doesn't allocate anything that needs to live across parser calls,
133  * so we can easily have it use palloc instead of malloc.  This prevents
134  * memory leaks if we error out during parsing.  Note this only works with
135  * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
136  * if possible, so there's not really much problem anyhow, at least if
137  * you're building with gcc.
138  */
139 #define YYMALLOC palloc
140 #define YYFREE   pfree
141 
142 /* Private struct for the result of privilege_target production */
143 typedef struct PrivTarget
144 {
145 	GrantTargetType targtype;
146 	GrantObjectType objtype;
147 	List	   *objs;
148 } PrivTarget;
149 
150 /* Private struct for the result of import_qualification production */
151 typedef struct ImportQual
152 {
153 	ImportForeignSchemaType type;
154 	List	   *table_names;
155 } ImportQual;
156 
157 /* ConstraintAttributeSpec yields an integer bitmask of these flags: */
158 #define CAS_NOT_DEFERRABLE			0x01
159 #define CAS_DEFERRABLE				0x02
160 #define CAS_INITIALLY_IMMEDIATE		0x04
161 #define CAS_INITIALLY_DEFERRED		0x08
162 #define CAS_NOT_VALID				0x10
163 #define CAS_NO_INHERIT				0x20
164 
165 
166 #define parser_yyerror(msg)  scanner_yyerror(msg, yyscanner)
167 #define parser_errposition(pos)  scanner_errposition(pos, yyscanner)
168 
169 static void base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner,
170 						 const char *msg);
171 static Node *makeColumnRef(char *colname, List *indirection,
172 						   int location, core_yyscan_t yyscanner);
173 static Node *makeTypeCast(Node *arg, TypeName *typename, int location);
174 static Node *makeStringConst(char *str, int location);
175 static Node *makeStringConstCast(char *str, int location, TypeName *typename);
176 static Node *makeIntConst(int val, int location);
177 static Node *makeFloatConst(char *str, int location);
178 static Node *makeBitStringConst(char *str, int location);
179 static Node *makeNullAConst(int location);
180 static Node *makeAConst(Value *v, int location);
181 static Node *makeBoolAConst(bool state, int location);
182 static Node *makeRoleSpec(RoleSpecType type, int location);
183 static void check_qualified_name(List *names, core_yyscan_t yyscanner);
184 static List *check_func_name(List *names, core_yyscan_t yyscanner);
185 static List *check_indirection(List *indirection, core_yyscan_t yyscanner);
186 static List *extractArgTypes(List *parameters);
187 static List *extractAggrArgTypes(List *aggrargs);
188 static List *makeOrderedSetArgs(List *directargs, List *orderedargs,
189 								core_yyscan_t yyscanner);
190 static void insertSelectOptions(SelectStmt *stmt,
191 								List *sortClause, List *lockingClause,
192 								Node *limitOffset, Node *limitCount,
193 								WithClause *withClause,
194 								core_yyscan_t yyscanner);
195 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
196 static Node *doNegate(Node *n, int location);
197 static void doNegateFloat(Value *v);
198 static Node *makeAndExpr(Node *lexpr, Node *rexpr, int location);
199 static Node *makeOrExpr(Node *lexpr, Node *rexpr, int location);
200 static Node *makeNotExpr(Node *expr, int location);
201 static Node *makeAArrayExpr(List *elements, int location);
202 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
203 						 List *args, int location);
204 static List *mergeTableFuncParameters(List *func_args, List *columns);
205 static TypeName *TableFuncTypeName(List *columns);
206 static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner);
207 static void SplitColQualList(List *qualList,
208 							 List **constraintList, CollateClause **collClause,
209 							 core_yyscan_t yyscanner);
210 static void processCASbits(int cas_bits, int location, const char *constrType,
211 			   bool *deferrable, bool *initdeferred, bool *not_valid,
212 			   bool *no_inherit, core_yyscan_t yyscanner);
213 static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
214 
215 %}
216 
217 %pure-parser
218 %expect 0
219 %name-prefix="base_yy"
220 %locations
221 
222 %parse-param {core_yyscan_t yyscanner}
223 %lex-param   {core_yyscan_t yyscanner}
224 
225 %union
226 {
227 	core_YYSTYPE		core_yystype;
228 	/* these fields must match core_YYSTYPE: */
229 	int					ival;
230 	char				*str;
231 	const char			*keyword;
232 
233 	char				chr;
234 	bool				boolean;
235 	JoinType			jtype;
236 	DropBehavior		dbehavior;
237 	OnCommitAction		oncommit;
238 	List				*list;
239 	Node				*node;
240 	Value				*value;
241 	ObjectType			objtype;
242 	TypeName			*typnam;
243 	FunctionParameter   *fun_param;
244 	FunctionParameterMode fun_param_mode;
245 	FuncWithArgs		*funwithargs;
246 	DefElem				*defelt;
247 	SortBy				*sortby;
248 	WindowDef			*windef;
249 	JoinExpr			*jexpr;
250 	IndexElem			*ielem;
251 	Alias				*alias;
252 	RangeVar			*range;
253 	IntoClause			*into;
254 	WithClause			*with;
255 	InferClause			*infer;
256 	OnConflictClause	*onconflict;
257 	A_Indices			*aind;
258 	ResTarget			*target;
259 	struct PrivTarget	*privtarget;
260 	AccessPriv			*accesspriv;
261 	struct ImportQual	*importqual;
262 	InsertStmt			*istmt;
263 	VariableSetStmt		*vsetstmt;
264 }
265 
266 %type <node>	stmt schema_stmt
267 		AlterEventTrigStmt
268 		AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterEnumStmt
269 		AlterFdwStmt AlterForeignServerStmt AlterGroupStmt
270 		AlterObjectDependsStmt AlterObjectSchemaStmt AlterOwnerStmt
271 		AlterOperatorStmt AlterSeqStmt AlterSystemStmt AlterTableStmt
272 		AlterTblSpcStmt AlterExtensionStmt AlterExtensionContentsStmt AlterForeignTableStmt
273 		AlterCompositeTypeStmt AlterUserStmt AlterUserMappingStmt AlterUserSetStmt
274 		AlterRoleStmt AlterRoleSetStmt AlterPolicyStmt
275 		AlterDefaultPrivilegesStmt DefACLAction
276 		AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
277 		ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
278 		CreateDomainStmt CreateExtensionStmt CreateGroupStmt CreateOpClassStmt
279 		CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
280 		CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
281 		CreateFdwStmt CreateForeignServerStmt CreateForeignTableStmt
282 		CreateAssertStmt CreateTransformStmt CreateTrigStmt CreateEventTrigStmt
283 		CreateUserStmt CreateUserMappingStmt CreateRoleStmt CreatePolicyStmt
284 		CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
285 		DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
286 		DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
287 		DropPolicyStmt DropUserStmt DropdbStmt DropTableSpaceStmt DropFdwStmt
288 		DropTransformStmt
289 		DropForeignServerStmt DropUserMappingStmt ExplainStmt FetchStmt
290 		GrantStmt GrantRoleStmt ImportForeignSchemaStmt IndexStmt InsertStmt
291 		ListenStmt LoadStmt LockStmt NotifyStmt ExplainableStmt PreparableStmt
292 		CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
293 		RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
294 		RuleActionStmt RuleActionStmtOrEmpty RuleStmt
295 		SecLabelStmt SelectStmt TransactionStmt TruncateStmt
296 		UnlistenStmt UpdateStmt VacuumStmt
297 		VariableResetStmt VariableSetStmt VariableShowStmt
298 		ViewStmt CheckPointStmt CreateConversionStmt
299 		DeallocateStmt PrepareStmt ExecuteStmt
300 		DropOwnedStmt ReassignOwnedStmt
301 		AlterTSConfigurationStmt AlterTSDictionaryStmt
302 		CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
303 
304 %type <node>	select_no_parens select_with_parens select_clause
305 				simple_select values_clause
306 
307 %type <node>	alter_column_default opclass_item opclass_drop alter_using
308 %type <ival>	add_drop opt_asc_desc opt_nulls_order
309 
310 %type <node>	alter_table_cmd alter_type_cmd opt_collate_clause
311 	   replica_identity
312 %type <list>	alter_table_cmds alter_type_cmds
313 
314 %type <dbehavior>	opt_drop_behavior
315 
316 %type <list>	createdb_opt_list createdb_opt_items copy_opt_list
317 				transaction_mode_list
318 				create_extension_opt_list alter_extension_opt_list
319 %type <defelt>	createdb_opt_item copy_opt_item
320 				transaction_mode_item
321 				create_extension_opt_item alter_extension_opt_item
322 
323 %type <ival>	opt_lock lock_type cast_context
324 %type <ival>	vacuum_option_list vacuum_option_elem
325 %type <boolean>	opt_or_replace
326 				opt_grant_grant_option opt_grant_admin_option
327 				opt_nowait opt_if_exists opt_with_data
328 %type <ival>	opt_nowait_or_skip
329 
330 %type <list>	OptRoleList AlterOptRoleList
331 %type <defelt>	CreateOptRoleElem AlterOptRoleElem
332 
333 %type <str>		opt_type
334 %type <str>		foreign_server_version opt_foreign_server_version
335 %type <str>		opt_in_database
336 
337 %type <str>		OptSchemaName
338 %type <list>	OptSchemaEltList
339 
340 %type <boolean> TriggerForSpec TriggerForType
341 %type <ival>	TriggerActionTime
342 %type <list>	TriggerEvents TriggerOneEvent
343 %type <value>	TriggerFuncArg
344 %type <node>	TriggerWhen
345 
346 %type <list>	event_trigger_when_list event_trigger_value_list
347 %type <defelt>	event_trigger_when_item
348 %type <chr>		enable_trigger
349 
350 %type <str>		copy_file_name
351 				database_name access_method_clause access_method attr_name
352 				name cursor_name file_name
353 				index_name opt_index_name cluster_index_specification
354 
355 %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
356 				opt_class opt_inline_handler opt_validator validator_clause
357 				opt_collate
358 
359 %type <range>	qualified_name insert_target OptConstrFromTable
360 
361 %type <str>		all_Op MathOp
362 
363 %type <str>		row_security_cmd RowSecurityDefaultForCmd
364 %type <node>	RowSecurityOptionalWithCheck RowSecurityOptionalExpr
365 %type <list>	RowSecurityDefaultToRole RowSecurityOptionalToRole
366 
367 %type <str>		iso_level opt_encoding
368 %type <node>	grantee
369 %type <list>	grantee_list
370 %type <accesspriv> privilege
371 %type <list>	privileges privilege_list
372 %type <privtarget> privilege_target
373 %type <funwithargs> function_with_argtypes
374 %type <list>	function_with_argtypes_list
375 %type <ival>	defacl_privilege_target
376 %type <defelt>	DefACLOption
377 %type <list>	DefACLOptionList
378 %type <ival>	import_qualification_type
379 %type <importqual> import_qualification
380 
381 %type <list>	stmtblock stmtmulti
382 				OptTableElementList TableElementList OptInherit definition
383 				OptTypedTableElementList TypedTableElementList
384 				reloptions opt_reloptions
385 				OptWith distinct_clause opt_all_clause opt_definition func_args func_args_list
386 				func_args_with_defaults func_args_with_defaults_list
387 				aggr_args aggr_args_list
388 				func_as createfunc_opt_list alterfunc_opt_list
389 				old_aggr_definition old_aggr_list
390 				oper_argtypes RuleActionList RuleActionMulti
391 				opt_column_list columnList opt_name_list
392 				sort_clause opt_sort_clause sortby_list index_params
393 				name_list role_list from_clause from_list opt_array_bounds
394 				qualified_name_list any_name any_name_list type_name_list
395 				any_operator expr_list attrs
396 				target_list opt_target_list insert_column_list set_target_list
397 				set_clause_list set_clause multiple_set_clause
398 				ctext_expr_list ctext_row def_list operator_def_list indirection opt_indirection
399 				reloption_list group_clause TriggerFuncArgs select_limit
400 				opt_select_limit opclass_item_list opclass_drop_list
401 				opclass_purpose opt_opfamily transaction_mode_list_or_empty
402 				OptTableFuncElementList TableFuncElementList opt_type_modifiers
403 				prep_type_clause
404 				execute_param_clause using_clause returning_clause
405 				opt_enum_val_list enum_val_list table_func_column_list
406 				create_generic_options alter_generic_options
407 				relation_expr_list dostmt_opt_list
408 				transform_element_list transform_type_list
409 
410 %type <list>	group_by_list
411 %type <node>	group_by_item empty_grouping_set rollup_clause cube_clause
412 %type <node>	grouping_sets_clause
413 
414 %type <list>	opt_fdw_options fdw_options
415 %type <defelt>	fdw_option
416 
417 %type <range>	OptTempTableName
418 %type <into>	into_clause create_as_target create_mv_target
419 
420 %type <defelt>	createfunc_opt_item common_func_opt_item dostmt_opt_item
421 %type <fun_param> func_arg func_arg_with_default table_func_column aggr_arg
422 %type <fun_param_mode> arg_class
423 %type <typnam>	func_return func_type
424 
425 %type <boolean>  opt_trusted opt_restart_seqs
426 %type <ival>	 OptTemp
427 %type <ival>	 OptNoLog
428 %type <oncommit> OnCommitOption
429 
430 %type <ival>	for_locking_strength
431 %type <node>	for_locking_item
432 %type <list>	for_locking_clause opt_for_locking_clause for_locking_items
433 %type <list>	locked_rels_list
434 %type <boolean>	all_or_distinct
435 
436 %type <node>	join_outer join_qual
437 %type <jtype>	join_type
438 
439 %type <list>	extract_list overlay_list position_list
440 %type <list>	substr_list trim_list
441 %type <list>	opt_interval interval_second
442 %type <node>	overlay_placing substr_from substr_for
443 
444 %type <boolean> opt_instead
445 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
446 %type <boolean> opt_freeze opt_default opt_recheck
447 %type <defelt>	opt_binary opt_oids copy_delimiter
448 
449 %type <boolean> copy_from opt_program
450 
451 %type <ival>	opt_column event cursor_options opt_hold opt_set_data
452 %type <objtype>	drop_type comment_type security_label_type
453 
454 %type <node>	fetch_args limit_clause select_limit_value
455 				offset_clause select_offset_value
456 				select_offset_value2 opt_select_fetch_first_value
457 %type <ival>	row_or_rows first_or_next
458 
459 %type <list>	OptSeqOptList SeqOptList
460 %type <defelt>	SeqOptElem
461 
462 %type <istmt>	insert_rest
463 %type <infer>	opt_conf_expr
464 %type <onconflict> opt_on_conflict
465 
466 %type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
467 				 SetResetClause FunctionSetResetClause
468 
469 %type <node>	TableElement TypedTableElement ConstraintElem TableFuncElement
470 %type <node>	columnDef columnOptions
471 %type <defelt>	def_elem reloption_elem old_aggr_elem operator_def_elem
472 %type <node>	def_arg columnElem where_clause where_or_current_clause
473 				a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
474 				columnref in_expr having_clause func_table array_expr
475 				ExclusionWhereClause
476 %type <list>	rowsfrom_item rowsfrom_list opt_col_def_list
477 %type <boolean> opt_ordinality
478 %type <list>	ExclusionConstraintList ExclusionConstraintElem
479 %type <list>	func_arg_list
480 %type <node>	func_arg_expr
481 %type <list>	row explicit_row implicit_row type_list array_expr_list
482 %type <node>	case_expr case_arg when_clause case_default
483 %type <list>	when_clause_list
484 %type <ival>	sub_type
485 %type <node>	ctext_expr
486 %type <value>	NumericOnly
487 %type <list>	NumericOnly_list
488 %type <alias>	alias_clause opt_alias_clause
489 %type <list>	func_alias_clause
490 %type <sortby>	sortby
491 %type <ielem>	index_elem
492 %type <node>	table_ref
493 %type <jexpr>	joined_table
494 %type <range>	relation_expr
495 %type <range>	relation_expr_opt_alias
496 %type <node>	tablesample_clause opt_repeatable_clause
497 %type <target>	target_el single_set_clause set_target insert_column_item
498 
499 %type <str>		generic_option_name
500 %type <node>	generic_option_arg
501 %type <defelt>	generic_option_elem alter_generic_option_elem
502 %type <list>	generic_option_list alter_generic_option_list
503 %type <str>		explain_option_name
504 %type <node>	explain_option_arg
505 %type <defelt>	explain_option_elem
506 %type <list>	explain_option_list
507 
508 %type <ival>	reindex_target_type reindex_target_multitable
509 %type <ival>	reindex_option_list reindex_option_elem
510 
511 %type <node>	copy_generic_opt_arg copy_generic_opt_arg_list_item
512 %type <defelt>	copy_generic_opt_elem
513 %type <list>	copy_generic_opt_list copy_generic_opt_arg_list
514 %type <list>	copy_options
515 
516 %type <typnam>	Typename SimpleTypename ConstTypename
517 				GenericType Numeric opt_float
518 				Character ConstCharacter
519 				CharacterWithLength CharacterWithoutLength
520 				ConstDatetime ConstInterval
521 				Bit ConstBit BitWithLength BitWithoutLength
522 %type <str>		character
523 %type <str>		extract_arg
524 %type <str>		opt_charset
525 %type <boolean> opt_varying opt_timezone opt_no_inherit
526 
527 %type <ival>	Iconst SignedIconst
528 %type <str>		Sconst comment_text notify_payload
529 %type <str>		RoleId opt_boolean_or_string
530 %type <list>	var_list
531 %type <str>		ColId ColLabel var_name type_function_name param_name
532 %type <str>		NonReservedWord NonReservedWord_or_Sconst
533 %type <str>		createdb_opt_name
534 %type <node>	var_value zone_value
535 %type <node>	auth_ident RoleSpec opt_granted_by
536 
537 %type <keyword> unreserved_keyword type_func_name_keyword
538 %type <keyword> col_name_keyword reserved_keyword
539 
540 %type <node>	TableConstraint TableLikeClause
541 %type <ival>	TableLikeOptionList TableLikeOption
542 %type <list>	ColQualList
543 %type <node>	ColConstraint ColConstraintElem ConstraintAttr
544 %type <ival>	key_actions key_delete key_match key_update key_action
545 %type <ival>	ConstraintAttributeSpec ConstraintAttributeElem
546 %type <str>		ExistingIndex
547 
548 %type <list>	constraints_set_list
549 %type <boolean> constraints_set_mode
550 %type <str>		OptTableSpace OptConsTableSpace
551 %type <node>	OptTableSpaceOwner
552 %type <ival>	opt_check_option
553 
554 %type <str>		opt_provider security_label
555 
556 %type <target>	xml_attribute_el
557 %type <list>	xml_attribute_list xml_attributes
558 %type <node>	xml_root_version opt_xml_root_standalone
559 %type <node>	xmlexists_argument
560 %type <ival>	document_or_content
561 %type <boolean> xml_whitespace_option
562 
563 %type <node>	func_application func_expr_common_subexpr
564 %type <node>	func_expr func_expr_windowless
565 %type <node>	common_table_expr
566 %type <with>	with_clause opt_with_clause
567 %type <list>	cte_list
568 
569 %type <list>	within_group_clause
570 %type <node>	filter_clause
571 %type <list>	window_clause window_definition_list opt_partition_clause
572 %type <windef>	window_definition over_clause window_specification
573 				opt_frame_clause frame_extent frame_bound
574 %type <str>		opt_existing_window_name
575 %type <boolean> opt_if_not_exists
576 
577 /*
578  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
579  * They must be listed first so that their numeric codes do not depend on
580  * the set of keywords.  PL/pgsql depends on this so that it can share the
581  * same lexer.  If you add/change tokens here, fix PL/pgsql to match!
582  *
583  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
584  * parse errors.  It is needed by PL/pgsql.
585  */
586 %token <str>	IDENT FCONST SCONST BCONST XCONST Op
587 %token <ival>	ICONST PARAM
588 %token			TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
589 %token			LESS_EQUALS GREATER_EQUALS NOT_EQUALS
590 
591 /*
592  * If you want to make any keyword changes, update the keyword table in
593  * src/include/parser/kwlist.h and add new keywords to the appropriate one
594  * of the reserved-or-not-so-reserved keyword lists, below; search
595  * this file for "Keyword category lists".
596  */
597 
598 /* ordinary key words in alphabetical order */
599 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
600 	AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
601 	ASSERTION ASSIGNMENT ASYMMETRIC AT ATTRIBUTE AUTHORIZATION
602 
603 	BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
604 	BOOLEAN_P BOTH BY
605 
606 	CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
607 	CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
608 	CLUSTER COALESCE COLLATE COLLATION COLUMN COMMENT COMMENTS COMMIT
609 	COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION CONSTRAINT
610 	CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE
611 	CROSS CSV CUBE CURRENT_P
612 	CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
613 	CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
614 
615 	DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
616 	DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DESC
617 	DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
618 
619 	EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EVENT EXCEPT
620 	EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
621 	EXTENSION EXTERNAL EXTRACT
622 
623 	FALSE_P FAMILY FETCH FILTER FIRST_P FLOAT_P FOLLOWING FOR
624 	FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
625 
626 	GLOBAL GRANT GRANTED GREATEST GROUP_P GROUPING
627 
628 	HANDLER HAVING HEADER_P HOLD HOUR_P
629 
630 	IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P
631 	INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
632 	INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
633 	INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
634 
635 	JOIN
636 
637 	KEY
638 
639 	LABEL LANGUAGE LARGE_P LAST_P LATERAL_P
640 	LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL
641 	LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED
642 
643 	MAPPING MATCH MATERIALIZED MAXVALUE METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE
644 
645 	NAME_P NAMES NATIONAL NATURAL NCHAR NEXT NO NONE
646 	NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
647 	NULLS_P NUMERIC
648 
649 	OBJECT_P OF OFF OFFSET OIDS ON ONLY OPERATOR OPTION OPTIONS OR
650 	ORDER ORDINALITY OUT_P OUTER_P OVER OVERLAPS OVERLAY OWNED OWNER
651 
652 	PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PGPOOL PLACING PLANS POLICY
653 	POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
654 	PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROGRAM
655 
656 	QUOTE
657 
658 	RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REFRESH REINDEX
659 	RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
660 	RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP
661 	ROW ROWS RULE
662 
663 	SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
664 	SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
665 	SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P START
666 	STATEMENT STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING
667 	SYMMETRIC SYSID SYSTEM_P
668 
669 	TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
670 	TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM TREAT TRIGGER TRIM TRUE_P
671 	TRUNCATE TRUSTED TYPE_P TYPES_P
672 
673 	UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNLOGGED
674 	UNTIL UPDATE USER USING
675 
676 	VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
677 	VERBOSE VERSION_P VIEW VIEWS VOLATILE
678 
679 	WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE
680 
681 	XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLPARSE
682 	XMLPI XMLROOT XMLSERIALIZE
683 
684 	YEAR_P YES_P
685 
686 	ZONE
687 
688 /*
689  * The grammar thinks these are keywords, but they are not in the kwlist.h
690  * list and so can never be entered directly.  The filter in parser.c
691  * creates these tokens when required (based on looking one token ahead).
692  *
693  * NOT_LA exists so that productions such as NOT LIKE can be given the same
694  * precedence as LIKE; otherwise they'd effectively have the same precedence
695  * as NOT, at least with respect to their left-hand subexpression.
696  * NULLS_LA and WITH_LA are needed to make the grammar LALR(1).
697  */
698 %token		NOT_LA NULLS_LA WITH_LA
699 
700 
701 /* Precedence: lowest to highest */
702 %nonassoc	SET				/* see relation_expr_opt_alias */
703 %left		UNION EXCEPT
704 %left		INTERSECT
705 %left		OR
706 %left		AND
707 %right		NOT
708 %nonassoc	IS ISNULL NOTNULL	/* IS sets precedence for IS NULL, etc */
709 %nonassoc	'<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
710 %nonassoc	BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA
711 %nonassoc	ESCAPE			/* ESCAPE must be just above LIKE/ILIKE/SIMILAR */
712 %left		POSTFIXOP		/* dummy for postfix Op rules */
713 /*
714  * To support target_el without AS, we must give IDENT an explicit priority
715  * between POSTFIXOP and Op.  We can safely assign the same priority to
716  * various unreserved keywords as needed to resolve ambiguities (this can't
717  * have any bad effects since obviously the keywords will still behave the
718  * same as if they weren't keywords).  We need to do this for PARTITION,
719  * RANGE, ROWS to support opt_existing_window_name; and for RANGE, ROWS
720  * so that they can follow a_expr without creating postfix-operator problems;
721  * and for NULL so that it can follow b_expr in ColQualList without creating
722  * postfix-operator problems.
723  *
724  * To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
725  * an explicit priority lower than '(', so that a rule with CUBE '(' will shift
726  * rather than reducing a conflicting rule that takes CUBE as a function name.
727  * Using the same precedence as IDENT seems right for the reasons given above.
728  *
729  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
730  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
731  * there is no principled way to distinguish these from the productions
732  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
733  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
734  * appear to cause UNBOUNDED to be treated differently from other unreserved
735  * keywords anywhere else in the grammar, but it's definitely risky.  We can
736  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
737  */
738 %nonassoc	UNBOUNDED		/* ideally should have same precedence as IDENT */
739 %nonassoc	IDENT NULL_P PARTITION RANGE ROWS PRECEDING FOLLOWING CUBE ROLLUP
740 %left		Op OPERATOR		/* multi-character ops and user-defined operators */
741 %left		'+' '-'
742 %left		'*' '/' '%'
743 %left		'^'
744 /* Unary Operators */
745 %left		AT				/* sets precedence for AT TIME ZONE */
746 %left		COLLATE
747 %right		UMINUS
748 %left		'[' ']'
749 %left		'(' ')'
750 %left		TYPECAST
751 %left		'.'
752 /*
753  * These might seem to be low-precedence, but actually they are not part
754  * of the arithmetic hierarchy at all in their use as JOIN operators.
755  * We make them high-precedence to support their use as function names.
756  * They wouldn't be given a precedence at all, were it not that we need
757  * left-associativity among the JOIN rules themselves.
758  */
759 %left		JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
760 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
761 %right		PRESERVE STRIP_P
762 
763 %%
764 
765 /*
766  *	The target production for the whole parse.
767  */
768 stmtblock:	stmtmulti
769 			{
770 				pg_yyget_extra(yyscanner)->parsetree = $1;
771 			}
772 		;
773 
774 /* the thrashing around here is to discard "empty" statements... */
775 stmtmulti:	stmtmulti ';' stmt
776 				{
777 					if ($3 != NULL)
778 						$$ = lappend($1, $3);
779 					else
780 						$$ = $1;
781 				}
782 			| stmt
783 				{
784 					if ($1 != NULL)
785 						$$ = list_make1($1);
786 					else
787 						$$ = NIL;
788 				}
789 		;
790 
791 stmt :
792 			AlterEventTrigStmt
793 			| AlterDatabaseStmt
794 			| AlterDatabaseSetStmt
795 			| AlterDefaultPrivilegesStmt
796 			| AlterDomainStmt
797 			| AlterEnumStmt
798 			| AlterExtensionStmt
799 			| AlterExtensionContentsStmt
800 			| AlterFdwStmt
801 			| AlterForeignServerStmt
802 			| AlterForeignTableStmt
803 			| AlterFunctionStmt
804 			| AlterGroupStmt
805 			| AlterObjectDependsStmt
806 			| AlterObjectSchemaStmt
807 			| AlterOwnerStmt
808 			| AlterOperatorStmt
809 			| AlterPolicyStmt
810 			| AlterSeqStmt
811 			| AlterSystemStmt
812 			| AlterTableStmt
813 			| AlterTblSpcStmt
814 			| AlterCompositeTypeStmt
815 			| AlterRoleSetStmt
816 			| AlterRoleStmt
817 			| AlterTSConfigurationStmt
818 			| AlterTSDictionaryStmt
819 			| AlterUserMappingStmt
820 			| AlterUserSetStmt
821 			| AlterUserStmt
822 			| AnalyzeStmt
823 			| CheckPointStmt
824 			| ClosePortalStmt
825 			| ClusterStmt
826 			| CommentStmt
827 			| ConstraintsSetStmt
828 			| CopyStmt
829 			| CreateAmStmt
830 			| CreateAsStmt
831 			| CreateAssertStmt
832 			| CreateCastStmt
833 			| CreateConversionStmt
834 			| CreateDomainStmt
835 			| CreateExtensionStmt
836 			| CreateFdwStmt
837 			| CreateForeignServerStmt
838 			| CreateForeignTableStmt
839 			| CreateFunctionStmt
840 			| CreateGroupStmt
841 			| CreateMatViewStmt
842 			| CreateOpClassStmt
843 			| CreateOpFamilyStmt
844 			| AlterOpFamilyStmt
845 			| CreatePolicyStmt
846 			| CreatePLangStmt
847 			| CreateSchemaStmt
848 			| CreateSeqStmt
849 			| CreateStmt
850 			| CreateTableSpaceStmt
851 			| CreateTransformStmt
852 			| CreateTrigStmt
853 			| CreateEventTrigStmt
854 			| CreateRoleStmt
855 			| CreateUserStmt
856 			| CreateUserMappingStmt
857 			| CreatedbStmt
858 			| DeallocateStmt
859 			| DeclareCursorStmt
860 			| DefineStmt
861 			| DeleteStmt
862 			| DiscardStmt
863 			| DoStmt
864 			| DropAssertStmt
865 			| DropCastStmt
866 			| DropFdwStmt
867 			| DropForeignServerStmt
868 			| DropGroupStmt
869 			| DropOpClassStmt
870 			| DropOpFamilyStmt
871 			| DropOwnedStmt
872 			| DropPolicyStmt
873 			| DropPLangStmt
874 			| DropRuleStmt
875 			| DropStmt
876 			| DropTableSpaceStmt
877 			| DropTransformStmt
878 			| DropTrigStmt
879 			| DropRoleStmt
880 			| DropUserStmt
881 			| DropUserMappingStmt
882 			| DropdbStmt
883 			| ExecuteStmt
884 			| ExplainStmt
885 			| FetchStmt
886 			| GrantStmt
887 			| GrantRoleStmt
888 			| ImportForeignSchemaStmt
889 			| IndexStmt
890 			| InsertStmt
891 			| ListenStmt
892 			| RefreshMatViewStmt
893 			| LoadStmt
894 			| LockStmt
895 			| NotifyStmt
896 			| PrepareStmt
897 			| ReassignOwnedStmt
898 			| ReindexStmt
899 			| RemoveAggrStmt
900 			| RemoveFuncStmt
901 			| RemoveOperStmt
902 			| RenameStmt
903 			| RevokeStmt
904 			| RevokeRoleStmt
905 			| RuleStmt
906 			| SecLabelStmt
907 			| SelectStmt
908 			| TransactionStmt
909 			| TruncateStmt
910 			| UnlistenStmt
911 			| UpdateStmt
912 			| VacuumStmt
913 			| VariableResetStmt
914 			| VariableSetStmt
915 			| VariableShowStmt
916 			| ViewStmt
917 			| /*EMPTY*/
918 				{ $$ = NULL; }
919 		;
920 
921 /*****************************************************************************
922  *
923  * Create a new Postgres DBMS role
924  *
925  *****************************************************************************/
926 
927 CreateRoleStmt:
928 			CREATE ROLE RoleId opt_with OptRoleList
929 				{
930 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
931 					n->stmt_type = ROLESTMT_ROLE;
932 					n->role = $3;
933 					n->options = $5;
934 					$$ = (Node *)n;
935 				}
936 		;
937 
938 
939 opt_with:	WITH									{}
940 			| WITH_LA								{}
941 			| /*EMPTY*/								{}
942 		;
943 
944 /*
945  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
946  * for backwards compatibility).  Note: the only option required by SQL99
947  * is "WITH ADMIN name".
948  */
949 OptRoleList:
950 			OptRoleList CreateOptRoleElem			{ $$ = lappend($1, $2); }
951 			| /* EMPTY */							{ $$ = NIL; }
952 		;
953 
954 AlterOptRoleList:
955 			AlterOptRoleList AlterOptRoleElem		{ $$ = lappend($1, $2); }
956 			| /* EMPTY */							{ $$ = NIL; }
957 		;
958 
959 AlterOptRoleElem:
960 			PASSWORD Sconst
961 				{
962 					$$ = makeDefElem("password",
963 									 (Node *)makeString($2));
964 				}
965 			| PASSWORD NULL_P
966 				{
967 					$$ = makeDefElem("password", NULL);
968 				}
969 			| ENCRYPTED PASSWORD Sconst
970 				{
971 					$$ = makeDefElem("encryptedPassword",
972 									 (Node *)makeString($3));
973 				}
974 			| UNENCRYPTED PASSWORD Sconst
975 				{
976 					$$ = makeDefElem("unencryptedPassword",
977 									 (Node *)makeString($3));
978 				}
979 			| INHERIT
980 				{
981 					$$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
982 				}
983 			| CONNECTION LIMIT SignedIconst
984 				{
985 					$$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
986 				}
987 			| VALID UNTIL Sconst
988 				{
989 					$$ = makeDefElem("validUntil", (Node *)makeString($3));
990 				}
991 		/*	Supported but not documented for roles, for use by ALTER GROUP. */
992 			| USER role_list
993 				{
994 					$$ = makeDefElem("rolemembers", (Node *)$2);
995 				}
996 			| IDENT
997 				{
998 					/*
999 					 * We handle identifiers that aren't parser keywords with
1000 					 * the following special-case codes, to avoid bloating the
1001 					 * size of the main parser.
1002 					 */
1003 					if (strcmp($1, "superuser") == 0)
1004 						$$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
1005 					else if (strcmp($1, "nosuperuser") == 0)
1006 						$$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
1007 					else if (strcmp($1, "createrole") == 0)
1008 						$$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
1009 					else if (strcmp($1, "nocreaterole") == 0)
1010 						$$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
1011 					else if (strcmp($1, "replication") == 0)
1012 						$$ = makeDefElem("isreplication", (Node *)makeInteger(TRUE));
1013 					else if (strcmp($1, "noreplication") == 0)
1014 						$$ = makeDefElem("isreplication", (Node *)makeInteger(FALSE));
1015 					else if (strcmp($1, "createdb") == 0)
1016 						$$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
1017 					else if (strcmp($1, "nocreatedb") == 0)
1018 						$$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
1019 					else if (strcmp($1, "login") == 0)
1020 						$$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
1021 					else if (strcmp($1, "nologin") == 0)
1022 						$$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
1023 					else if (strcmp($1, "bypassrls") == 0)
1024 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(TRUE));
1025 					else if (strcmp($1, "nobypassrls") == 0)
1026 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(FALSE));
1027 					else if (strcmp($1, "noinherit") == 0)
1028 					{
1029 						/*
1030 						 * Note that INHERIT is a keyword, so it's handled by main parser, but
1031 						 * NOINHERIT is handled here.
1032 						 */
1033 						$$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
1034 					}
1035 					else
1036 						ereport(ERROR,
1037 								(errcode(ERRCODE_SYNTAX_ERROR),
1038 								 errmsg("unrecognized role option \"%s\"", $1),
1039 									 parser_errposition(@1)));
1040 				}
1041 		;
1042 
1043 CreateOptRoleElem:
1044 			AlterOptRoleElem			{ $$ = $1; }
1045 			/* The following are not supported by ALTER ROLE/USER/GROUP */
1046 			| SYSID Iconst
1047 				{
1048 					$$ = makeDefElem("sysid", (Node *)makeInteger($2));
1049 				}
1050 			| ADMIN role_list
1051 				{
1052 					$$ = makeDefElem("adminmembers", (Node *)$2);
1053 				}
1054 			| ROLE role_list
1055 				{
1056 					$$ = makeDefElem("rolemembers", (Node *)$2);
1057 				}
1058 			| IN_P ROLE role_list
1059 				{
1060 					$$ = makeDefElem("addroleto", (Node *)$3);
1061 				}
1062 			| IN_P GROUP_P role_list
1063 				{
1064 					$$ = makeDefElem("addroleto", (Node *)$3);
1065 				}
1066 		;
1067 
1068 
1069 /*****************************************************************************
1070  *
1071  * Create a new Postgres DBMS user (role with implied login ability)
1072  *
1073  *****************************************************************************/
1074 
1075 CreateUserStmt:
1076 			CREATE USER RoleId opt_with OptRoleList
1077 				{
1078 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1079 					n->stmt_type = ROLESTMT_USER;
1080 					n->role = $3;
1081 					n->options = $5;
1082 					$$ = (Node *)n;
1083 				}
1084 		;
1085 
1086 
1087 /*****************************************************************************
1088  *
1089  * Alter a postgresql DBMS role
1090  *
1091  *****************************************************************************/
1092 
1093 AlterRoleStmt:
1094 			ALTER ROLE RoleSpec opt_with AlterOptRoleList
1095 				 {
1096 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1097 					n->role = $3;
1098 					n->action = +1;	/* add, if there are members */
1099 					n->options = $5;
1100 					$$ = (Node *)n;
1101 				 }
1102 		;
1103 
1104 opt_in_database:
1105 			   /* EMPTY */					{ $$ = NULL; }
1106 			| IN_P DATABASE database_name	{ $$ = $3; }
1107 		;
1108 
1109 AlterRoleSetStmt:
1110 			ALTER ROLE RoleSpec opt_in_database SetResetClause
1111 				{
1112 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1113 					n->role = $3;
1114 					n->database = $4;
1115 					n->setstmt = $5;
1116 					$$ = (Node *)n;
1117 				}
1118 			| ALTER ROLE ALL opt_in_database SetResetClause
1119 				{
1120 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1121 					n->role = NULL;
1122 					n->database = $4;
1123 					n->setstmt = $5;
1124 					$$ = (Node *)n;
1125 				}
1126 		;
1127 
1128 
1129 /*****************************************************************************
1130  *
1131  * Alter a postgresql DBMS user
1132  *
1133  *****************************************************************************/
1134 
1135 AlterUserStmt:
1136 			ALTER USER RoleSpec opt_with AlterOptRoleList
1137 				 {
1138 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1139 					n->role = $3;
1140 					n->action = +1;	/* add, if there are members */
1141 					n->options = $5;
1142 					$$ = (Node *)n;
1143 				 }
1144 		;
1145 
1146 
1147 AlterUserSetStmt:
1148 			ALTER USER RoleSpec SetResetClause
1149 				{
1150 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1151 					n->role = $3;
1152 					n->database = NULL;
1153 					n->setstmt = $4;
1154 					$$ = (Node *)n;
1155 				}
1156 			;
1157 
1158 
1159 /*****************************************************************************
1160  *
1161  * Drop a postgresql DBMS role
1162  *
1163  * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
1164  * might own objects in multiple databases, there is presently no way to
1165  * implement either cascading or restricting.  Caveat DBA.
1166  *****************************************************************************/
1167 
1168 DropRoleStmt:
1169 			DROP ROLE role_list
1170 				{
1171 					DropRoleStmt *n = makeNode(DropRoleStmt);
1172 					n->missing_ok = FALSE;
1173 					n->roles = $3;
1174 					$$ = (Node *)n;
1175 				}
1176 			| DROP ROLE IF_P EXISTS role_list
1177 				{
1178 					DropRoleStmt *n = makeNode(DropRoleStmt);
1179 					n->missing_ok = TRUE;
1180 					n->roles = $5;
1181 					$$ = (Node *)n;
1182 				}
1183 			;
1184 
1185 /*****************************************************************************
1186  *
1187  * Drop a postgresql DBMS user
1188  *
1189  * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
1190  * might own objects in multiple databases, there is presently no way to
1191  * implement either cascading or restricting.  Caveat DBA.
1192  *****************************************************************************/
1193 
1194 DropUserStmt:
1195 			DROP USER role_list
1196 				{
1197 					DropRoleStmt *n = makeNode(DropRoleStmt);
1198 					n->missing_ok = FALSE;
1199 					n->roles = $3;
1200 					$$ = (Node *)n;
1201 				}
1202 			| DROP USER IF_P EXISTS role_list
1203 				{
1204 					DropRoleStmt *n = makeNode(DropRoleStmt);
1205 					n->roles = $5;
1206 					n->missing_ok = TRUE;
1207 					$$ = (Node *)n;
1208 				}
1209 			;
1210 
1211 
1212 /*****************************************************************************
1213  *
1214  * Create a postgresql group (role without login ability)
1215  *
1216  *****************************************************************************/
1217 
1218 CreateGroupStmt:
1219 			CREATE GROUP_P RoleId opt_with OptRoleList
1220 				{
1221 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1222 					n->stmt_type = ROLESTMT_GROUP;
1223 					n->role = $3;
1224 					n->options = $5;
1225 					$$ = (Node *)n;
1226 				}
1227 		;
1228 
1229 
1230 /*****************************************************************************
1231  *
1232  * Alter a postgresql group
1233  *
1234  *****************************************************************************/
1235 
1236 AlterGroupStmt:
1237 			ALTER GROUP_P RoleSpec add_drop USER role_list
1238 				{
1239 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1240 					n->role = $3;
1241 					n->action = $4;
1242 					n->options = list_make1(makeDefElem("rolemembers",
1243 														(Node *)$6));
1244 					$$ = (Node *)n;
1245 				}
1246 		;
1247 
1248 add_drop:	ADD_P									{ $$ = +1; }
1249 			| DROP									{ $$ = -1; }
1250 		;
1251 
1252 
1253 /*****************************************************************************
1254  *
1255  * Drop a postgresql group
1256  *
1257  * XXX see above notes about cascading DROP USER; groups have same problem.
1258  *****************************************************************************/
1259 
1260 DropGroupStmt:
1261 			DROP GROUP_P role_list
1262 				{
1263 					DropRoleStmt *n = makeNode(DropRoleStmt);
1264 					n->missing_ok = FALSE;
1265 					n->roles = $3;
1266 					$$ = (Node *)n;
1267 				}
1268 			| DROP GROUP_P IF_P EXISTS role_list
1269 				{
1270 					DropRoleStmt *n = makeNode(DropRoleStmt);
1271 					n->missing_ok = TRUE;
1272 					n->roles = $5;
1273 					$$ = (Node *)n;
1274 				}
1275 		;
1276 
1277 
1278 /*****************************************************************************
1279  *
1280  * Manipulate a schema
1281  *
1282  *****************************************************************************/
1283 
1284 CreateSchemaStmt:
1285 			CREATE SCHEMA OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1286 				{
1287 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1288 					/* One can omit the schema name or the authorization id. */
1289 					n->schemaname = $3;
1290 					n->authrole = $5;
1291 					n->schemaElts = $6;
1292 					n->if_not_exists = false;
1293 					$$ = (Node *)n;
1294 				}
1295 			| CREATE SCHEMA ColId OptSchemaEltList
1296 				{
1297 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1298 					/* ...but not both */
1299 					n->schemaname = $3;
1300 					n->authrole = NULL;
1301 					n->schemaElts = $4;
1302 					n->if_not_exists = false;
1303 					$$ = (Node *)n;
1304 				}
1305 			| CREATE SCHEMA IF_P NOT EXISTS OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1306 				{
1307 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1308 					/* schema name can be omitted here, too */
1309 					n->schemaname = $6;
1310 					n->authrole = $8;
1311 					if ($9 != NIL)
1312 						ereport(ERROR,
1313 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1314 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1315 								 parser_errposition(@9)));
1316 					n->schemaElts = $9;
1317 					n->if_not_exists = true;
1318 					$$ = (Node *)n;
1319 				}
1320 			| CREATE SCHEMA IF_P NOT EXISTS ColId OptSchemaEltList
1321 				{
1322 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1323 					/* ...but not here */
1324 					n->schemaname = $6;
1325 					n->authrole = NULL;
1326 					if ($7 != NIL)
1327 						ereport(ERROR,
1328 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1329 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1330 								 parser_errposition(@7)));
1331 					n->schemaElts = $7;
1332 					n->if_not_exists = true;
1333 					$$ = (Node *)n;
1334 				}
1335 		;
1336 
1337 OptSchemaName:
1338 			ColId									{ $$ = $1; }
1339 			| /* EMPTY */							{ $$ = NULL; }
1340 		;
1341 
1342 OptSchemaEltList:
1343 			OptSchemaEltList schema_stmt
1344 				{
1345 					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
1346 						@$ = @2;
1347 					$$ = lappend($1, $2);
1348 				}
1349 			| /* EMPTY */
1350 				{ $$ = NIL; }
1351 		;
1352 
1353 /*
1354  *	schema_stmt are the ones that can show up inside a CREATE SCHEMA
1355  *	statement (in addition to by themselves).
1356  */
1357 schema_stmt:
1358 			CreateStmt
1359 			| IndexStmt
1360 			| CreateSeqStmt
1361 			| CreateTrigStmt
1362 			| GrantStmt
1363 			| ViewStmt
1364 		;
1365 
1366 
1367 /*****************************************************************************
1368  *
1369  * Set PG internal variable
1370  *	  SET name TO 'var_value'
1371  * Include SQL syntax (thomas 1997-10-22):
1372  *	  SET TIME ZONE 'var_value'
1373  *
1374  *****************************************************************************/
1375 
1376 VariableSetStmt:
1377 			PGPOOL SET generic_set
1378 				{
1379 					VariableSetStmt *n = $3;
1380 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep changes minumum */
1381 					n->is_local = false;
1382 					$$ = (Node *) n;
1383 				}
1384 			| SET set_rest
1385 				{
1386 					VariableSetStmt *n = $2;
1387 					n->is_local = false;
1388 					$$ = (Node *) n;
1389 				}
1390 			| SET LOCAL set_rest
1391 				{
1392 					VariableSetStmt *n = $3;
1393 					n->is_local = true;
1394 					$$ = (Node *) n;
1395 				}
1396 			| SET SESSION set_rest
1397 				{
1398 					VariableSetStmt *n = $3;
1399 					n->is_local = false;
1400 					$$ = (Node *) n;
1401 				}
1402 		;
1403 
1404 set_rest:
1405 			TRANSACTION transaction_mode_list
1406 				{
1407 					VariableSetStmt *n = makeNode(VariableSetStmt);
1408 					n->kind = VAR_SET_MULTI;
1409 					n->name = "TRANSACTION";
1410 					n->args = $2;
1411 					$$ = n;
1412 				}
1413 			| SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1414 				{
1415 					VariableSetStmt *n = makeNode(VariableSetStmt);
1416 					n->kind = VAR_SET_MULTI;
1417 					n->name = "SESSION CHARACTERISTICS";
1418 					n->args = $5;
1419 					$$ = n;
1420 				}
1421 			| set_rest_more
1422 			;
1423 
1424 generic_set:
1425 			var_name TO var_list
1426 				{
1427 					VariableSetStmt *n = makeNode(VariableSetStmt);
1428 					n->kind = VAR_SET_VALUE;
1429 					n->name = $1;
1430 					n->args = $3;
1431 					$$ = n;
1432 				}
1433 			| var_name '=' var_list
1434 				{
1435 					VariableSetStmt *n = makeNode(VariableSetStmt);
1436 					n->kind = VAR_SET_VALUE;
1437 					n->name = $1;
1438 					n->args = $3;
1439 					$$ = n;
1440 				}
1441 			| var_name TO DEFAULT
1442 				{
1443 					VariableSetStmt *n = makeNode(VariableSetStmt);
1444 					n->kind = VAR_SET_DEFAULT;
1445 					n->name = $1;
1446 					$$ = n;
1447 				}
1448 			| var_name '=' DEFAULT
1449 				{
1450 					VariableSetStmt *n = makeNode(VariableSetStmt);
1451 					n->kind = VAR_SET_DEFAULT;
1452 					n->name = $1;
1453 					$$ = n;
1454 				}
1455 
1456 set_rest_more:	/* Generic SET syntaxes: */
1457 			generic_set 						{$$ = $1;}
1458 			| var_name FROM CURRENT_P
1459 				{
1460 					VariableSetStmt *n = makeNode(VariableSetStmt);
1461 					n->kind = VAR_SET_CURRENT;
1462 					n->name = $1;
1463 					$$ = n;
1464 				}
1465 			/* Special syntaxes mandated by SQL standard: */
1466 			| TIME ZONE zone_value
1467 				{
1468 					VariableSetStmt *n = makeNode(VariableSetStmt);
1469 					n->kind = VAR_SET_VALUE;
1470 					n->name = "timezone";
1471 					if ($3 != NULL)
1472 						n->args = list_make1($3);
1473 					else
1474 						n->kind = VAR_SET_DEFAULT;
1475 					$$ = n;
1476 				}
1477 			| CATALOG_P Sconst
1478 				{
1479 					ereport(ERROR,
1480 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1481 							 errmsg("current database cannot be changed"),
1482 							 parser_errposition(@2)));
1483 					$$ = NULL; /*not reached*/
1484 				}
1485 			| SCHEMA Sconst
1486 				{
1487 					VariableSetStmt *n = makeNode(VariableSetStmt);
1488 					n->kind = VAR_SET_VALUE;
1489 					n->name = "search_path";
1490 					n->args = list_make1(makeStringConst($2, @2));
1491 					$$ = n;
1492 				}
1493 			| NAMES opt_encoding
1494 				{
1495 					VariableSetStmt *n = makeNode(VariableSetStmt);
1496 					n->kind = VAR_SET_VALUE;
1497 					n->name = "client_encoding";
1498 					if ($2 != NULL)
1499 						n->args = list_make1(makeStringConst($2, @2));
1500 					else
1501 						n->kind = VAR_SET_DEFAULT;
1502 					$$ = n;
1503 				}
1504 			| ROLE NonReservedWord_or_Sconst
1505 				{
1506 					VariableSetStmt *n = makeNode(VariableSetStmt);
1507 					n->kind = VAR_SET_VALUE;
1508 					n->name = "role";
1509 					n->args = list_make1(makeStringConst($2, @2));
1510 					$$ = n;
1511 				}
1512 			| SESSION AUTHORIZATION NonReservedWord_or_Sconst
1513 				{
1514 					VariableSetStmt *n = makeNode(VariableSetStmt);
1515 					n->kind = VAR_SET_VALUE;
1516 					n->name = "session_authorization";
1517 					n->args = list_make1(makeStringConst($3, @3));
1518 					$$ = n;
1519 				}
1520 			| SESSION AUTHORIZATION DEFAULT
1521 				{
1522 					VariableSetStmt *n = makeNode(VariableSetStmt);
1523 					n->kind = VAR_SET_DEFAULT;
1524 					n->name = "session_authorization";
1525 					$$ = n;
1526 				}
1527 			| XML_P OPTION document_or_content
1528 				{
1529 					VariableSetStmt *n = makeNode(VariableSetStmt);
1530 					n->kind = VAR_SET_VALUE;
1531 					n->name = "xmloption";
1532 					n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1533 					$$ = n;
1534 				}
1535 			/* Special syntaxes invented by PostgreSQL: */
1536 			| TRANSACTION SNAPSHOT Sconst
1537 				{
1538 					VariableSetStmt *n = makeNode(VariableSetStmt);
1539 					n->kind = VAR_SET_MULTI;
1540 					n->name = "TRANSACTION SNAPSHOT";
1541 					n->args = list_make1(makeStringConst($3, @3));
1542 					$$ = n;
1543 				}
1544 		;
1545 
1546 var_name:	ColId								{ $$ = $1; }
1547 			| var_name '.' ColId
1548 				{ $$ = psprintf("%s.%s", $1, $3); }
1549 		;
1550 
1551 var_list:	var_value								{ $$ = list_make1($1); }
1552 			| var_list ',' var_value				{ $$ = lappend($1, $3); }
1553 		;
1554 
1555 var_value:	opt_boolean_or_string
1556 				{ $$ = makeStringConst($1, @1); }
1557 			| NumericOnly
1558 				{ $$ = makeAConst($1, @1); }
1559 		;
1560 
1561 iso_level:	READ UNCOMMITTED						{ $$ = "read uncommitted"; }
1562 			| READ COMMITTED						{ $$ = "read committed"; }
1563 			| REPEATABLE READ						{ $$ = "repeatable read"; }
1564 			| SERIALIZABLE							{ $$ = "serializable"; }
1565 		;
1566 
1567 opt_boolean_or_string:
1568 			TRUE_P									{ $$ = "true"; }
1569 			| FALSE_P								{ $$ = "false"; }
1570 			| ON									{ $$ = "on"; }
1571 			/*
1572 			 * OFF is also accepted as a boolean value, but is handled by
1573 			 * the NonReservedWord rule.  The action for booleans and strings
1574 			 * is the same, so we don't need to distinguish them here.
1575 			 */
1576 			| NonReservedWord_or_Sconst				{ $$ = $1; }
1577 		;
1578 
1579 /* Timezone values can be:
1580  * - a string such as 'pst8pdt'
1581  * - an identifier such as "pst8pdt"
1582  * - an integer or floating point number
1583  * - a time interval per SQL99
1584  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1585  * so use IDENT (meaning we reject anything that is a key word).
1586  */
1587 zone_value:
1588 			Sconst
1589 				{
1590 					$$ = makeStringConst($1, @1);
1591 				}
1592 			| IDENT
1593 				{
1594 					$$ = makeStringConst($1, @1);
1595 				}
1596 			| ConstInterval Sconst opt_interval
1597 				{
1598 					TypeName *t = $1;
1599 					if ($3 != NIL)
1600 					{
1601 						A_Const *n = (A_Const *) linitial($3);
1602 						if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1603 							ereport(ERROR,
1604 									(errcode(ERRCODE_SYNTAX_ERROR),
1605 									 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1606 									 parser_errposition(@3)));
1607 					}
1608 					t->typmods = $3;
1609 					$$ = makeStringConstCast($2, @2, t);
1610 				}
1611 			| ConstInterval '(' Iconst ')' Sconst
1612 				{
1613 					TypeName *t = $1;
1614 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1615 											makeIntConst($3, @3));
1616 					$$ = makeStringConstCast($5, @5, t);
1617 				}
1618 			| NumericOnly							{ $$ = makeAConst($1, @1); }
1619 			| DEFAULT								{ $$ = NULL; }
1620 			| LOCAL									{ $$ = NULL; }
1621 		;
1622 
1623 opt_encoding:
1624 			Sconst									{ $$ = $1; }
1625 			| DEFAULT								{ $$ = NULL; }
1626 			| /*EMPTY*/								{ $$ = NULL; }
1627 		;
1628 
1629 NonReservedWord_or_Sconst:
1630 			NonReservedWord							{ $$ = $1; }
1631 			| Sconst								{ $$ = $1; }
1632 		;
1633 
1634 VariableResetStmt:
1635 			RESET reset_rest						{ $$ = (Node *) $2; }
1636 			| PGPOOL RESET generic_reset
1637 				{
1638 					VariableSetStmt *n = $3;
1639 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep the changes minumum */
1640 					$$ = (Node *) n;
1641 				}
1642 		;
1643 
1644 reset_rest:
1645 			generic_reset							{ $$ = $1; }
1646 			| TIME ZONE
1647 				{
1648 					VariableSetStmt *n = makeNode(VariableSetStmt);
1649 					n->kind = VAR_RESET;
1650 					n->name = "timezone";
1651 					$$ = n;
1652 				}
1653 			| TRANSACTION ISOLATION LEVEL
1654 				{
1655 					VariableSetStmt *n = makeNode(VariableSetStmt);
1656 					n->kind = VAR_RESET;
1657 					n->name = "transaction_isolation";
1658 					$$ = n;
1659 				}
1660 			| SESSION AUTHORIZATION
1661 				{
1662 					VariableSetStmt *n = makeNode(VariableSetStmt);
1663 					n->kind = VAR_RESET;
1664 					n->name = "session_authorization";
1665 					$$ = n;
1666 				}
1667 		;
1668 
1669 generic_reset:
1670 			var_name
1671 				{
1672 					VariableSetStmt *n = makeNode(VariableSetStmt);
1673 					n->kind = VAR_RESET;
1674 					n->name = $1;
1675 					$$ = n;
1676 				}
1677 			| ALL
1678 				{
1679 					VariableSetStmt *n = makeNode(VariableSetStmt);
1680 					n->kind = VAR_RESET_ALL;
1681 					$$ = n;
1682 				}
1683 		;
1684 
1685 /* SetResetClause allows SET or RESET without LOCAL */
1686 SetResetClause:
1687 			SET set_rest					{ $$ = $2; }
1688 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1689 		;
1690 
1691 /* SetResetClause allows SET or RESET without LOCAL */
1692 FunctionSetResetClause:
1693 			SET set_rest_more				{ $$ = $2; }
1694 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1695 		;
1696 
1697 
1698 VariableShowStmt:
1699 			/* pgpool extension */
1700 			PGPOOL SHOW var_name
1701 			{
1702 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1703 				n->name = $3;
1704 				$$ = (Node *) n;
1705 			}
1706 			| PGPOOL SHOW ALL
1707 			{
1708 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1709 				n->name = "all";
1710 				$$ = (Node *) n;
1711 			}
1712 			| SHOW var_name
1713 				{
1714 					VariableShowStmt *n = makeNode(VariableShowStmt);
1715 					n->name = $2;
1716 					$$ = (Node *) n;
1717 				}
1718 			| SHOW TIME ZONE
1719 				{
1720 					VariableShowStmt *n = makeNode(VariableShowStmt);
1721 					n->name = "timezone";
1722 					$$ = (Node *) n;
1723 				}
1724 			| SHOW TRANSACTION ISOLATION LEVEL
1725 				{
1726 					VariableShowStmt *n = makeNode(VariableShowStmt);
1727 					n->name = "transaction_isolation";
1728 					$$ = (Node *) n;
1729 				}
1730 			| SHOW SESSION AUTHORIZATION
1731 				{
1732 					VariableShowStmt *n = makeNode(VariableShowStmt);
1733 					n->name = "session_authorization";
1734 					$$ = (Node *) n;
1735 				}
1736 			| SHOW ALL
1737 				{
1738 					VariableShowStmt *n = makeNode(VariableShowStmt);
1739 					n->name = "all";
1740 					$$ = (Node *) n;
1741 				}
1742 		;
1743 
1744 
1745 ConstraintsSetStmt:
1746 			SET CONSTRAINTS constraints_set_list constraints_set_mode
1747 				{
1748 					ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1749 					n->constraints = $3;
1750 					n->deferred = $4;
1751 					$$ = (Node *) n;
1752 				}
1753 		;
1754 
1755 constraints_set_list:
1756 			ALL										{ $$ = NIL; }
1757 			| qualified_name_list					{ $$ = $1; }
1758 		;
1759 
1760 constraints_set_mode:
1761 			DEFERRED								{ $$ = TRUE; }
1762 			| IMMEDIATE								{ $$ = FALSE; }
1763 		;
1764 
1765 
1766 /*
1767  * Checkpoint statement
1768  */
1769 CheckPointStmt:
1770 			CHECKPOINT
1771 				{
1772 					CheckPointStmt *n = makeNode(CheckPointStmt);
1773 					$$ = (Node *)n;
1774 				}
1775 		;
1776 
1777 
1778 /*****************************************************************************
1779  *
1780  * DISCARD { ALL | TEMP | PLANS | SEQUENCES }
1781  *
1782  *****************************************************************************/
1783 
1784 DiscardStmt:
1785 			DISCARD ALL
1786 				{
1787 					DiscardStmt *n = makeNode(DiscardStmt);
1788 					n->target = DISCARD_ALL;
1789 					$$ = (Node *) n;
1790 				}
1791 			| DISCARD TEMP
1792 				{
1793 					DiscardStmt *n = makeNode(DiscardStmt);
1794 					n->target = DISCARD_TEMP;
1795 					$$ = (Node *) n;
1796 				}
1797 			| DISCARD TEMPORARY
1798 				{
1799 					DiscardStmt *n = makeNode(DiscardStmt);
1800 					n->target = DISCARD_TEMP;
1801 					$$ = (Node *) n;
1802 				}
1803 			| DISCARD PLANS
1804 				{
1805 					DiscardStmt *n = makeNode(DiscardStmt);
1806 					n->target = DISCARD_PLANS;
1807 					$$ = (Node *) n;
1808 				}
1809 			| DISCARD SEQUENCES
1810 				{
1811 					DiscardStmt *n = makeNode(DiscardStmt);
1812 					n->target = DISCARD_SEQUENCES;
1813 					$$ = (Node *) n;
1814 				}
1815 
1816 		;
1817 
1818 
1819 /*****************************************************************************
1820  *
1821  *	ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW ] variations
1822  *
1823  * Note: we accept all subcommands for each of the five variants, and sort
1824  * out what's really legal at execution time.
1825  *****************************************************************************/
1826 
1827 AlterTableStmt:
1828 			ALTER TABLE relation_expr alter_table_cmds
1829 				{
1830 					AlterTableStmt *n = makeNode(AlterTableStmt);
1831 					n->relation = $3;
1832 					n->cmds = $4;
1833 					n->relkind = OBJECT_TABLE;
1834 					n->missing_ok = false;
1835 					$$ = (Node *)n;
1836 				}
1837 		|	ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
1838 				{
1839 					AlterTableStmt *n = makeNode(AlterTableStmt);
1840 					n->relation = $5;
1841 					n->cmds = $6;
1842 					n->relkind = OBJECT_TABLE;
1843 					n->missing_ok = true;
1844 					$$ = (Node *)n;
1845 				}
1846 		|	ALTER TABLE ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1847 				{
1848 					AlterTableMoveAllStmt *n =
1849 						makeNode(AlterTableMoveAllStmt);
1850 					n->orig_tablespacename = $6;
1851 					n->objtype = OBJECT_TABLE;
1852 					n->roles = NIL;
1853 					n->new_tablespacename = $9;
1854 					n->nowait = $10;
1855 					$$ = (Node *)n;
1856 				}
1857 		|	ALTER TABLE ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1858 				{
1859 					AlterTableMoveAllStmt *n =
1860 						makeNode(AlterTableMoveAllStmt);
1861 					n->orig_tablespacename = $6;
1862 					n->objtype = OBJECT_TABLE;
1863 					n->roles = $9;
1864 					n->new_tablespacename = $12;
1865 					n->nowait = $13;
1866 					$$ = (Node *)n;
1867 				}
1868 		|	ALTER INDEX qualified_name alter_table_cmds
1869 				{
1870 					AlterTableStmt *n = makeNode(AlterTableStmt);
1871 					n->relation = $3;
1872 					n->cmds = $4;
1873 					n->relkind = OBJECT_INDEX;
1874 					n->missing_ok = false;
1875 					$$ = (Node *)n;
1876 				}
1877 		|	ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
1878 				{
1879 					AlterTableStmt *n = makeNode(AlterTableStmt);
1880 					n->relation = $5;
1881 					n->cmds = $6;
1882 					n->relkind = OBJECT_INDEX;
1883 					n->missing_ok = true;
1884 					$$ = (Node *)n;
1885 				}
1886 		|	ALTER INDEX ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1887 				{
1888 					AlterTableMoveAllStmt *n =
1889 						makeNode(AlterTableMoveAllStmt);
1890 					n->orig_tablespacename = $6;
1891 					n->objtype = OBJECT_INDEX;
1892 					n->roles = NIL;
1893 					n->new_tablespacename = $9;
1894 					n->nowait = $10;
1895 					$$ = (Node *)n;
1896 				}
1897 		|	ALTER INDEX ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1898 				{
1899 					AlterTableMoveAllStmt *n =
1900 						makeNode(AlterTableMoveAllStmt);
1901 					n->orig_tablespacename = $6;
1902 					n->objtype = OBJECT_INDEX;
1903 					n->roles = $9;
1904 					n->new_tablespacename = $12;
1905 					n->nowait = $13;
1906 					$$ = (Node *)n;
1907 				}
1908 		|	ALTER SEQUENCE qualified_name alter_table_cmds
1909 				{
1910 					AlterTableStmt *n = makeNode(AlterTableStmt);
1911 					n->relation = $3;
1912 					n->cmds = $4;
1913 					n->relkind = OBJECT_SEQUENCE;
1914 					n->missing_ok = false;
1915 					$$ = (Node *)n;
1916 				}
1917 		|	ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
1918 				{
1919 					AlterTableStmt *n = makeNode(AlterTableStmt);
1920 					n->relation = $5;
1921 					n->cmds = $6;
1922 					n->relkind = OBJECT_SEQUENCE;
1923 					n->missing_ok = true;
1924 					$$ = (Node *)n;
1925 				}
1926 		|	ALTER VIEW qualified_name alter_table_cmds
1927 				{
1928 					AlterTableStmt *n = makeNode(AlterTableStmt);
1929 					n->relation = $3;
1930 					n->cmds = $4;
1931 					n->relkind = OBJECT_VIEW;
1932 					n->missing_ok = false;
1933 					$$ = (Node *)n;
1934 				}
1935 		|	ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
1936 				{
1937 					AlterTableStmt *n = makeNode(AlterTableStmt);
1938 					n->relation = $5;
1939 					n->cmds = $6;
1940 					n->relkind = OBJECT_VIEW;
1941 					n->missing_ok = true;
1942 					$$ = (Node *)n;
1943 				}
1944 		|	ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
1945 				{
1946 					AlterTableStmt *n = makeNode(AlterTableStmt);
1947 					n->relation = $4;
1948 					n->cmds = $5;
1949 					n->relkind = OBJECT_MATVIEW;
1950 					n->missing_ok = false;
1951 					$$ = (Node *)n;
1952 				}
1953 		|	ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
1954 				{
1955 					AlterTableStmt *n = makeNode(AlterTableStmt);
1956 					n->relation = $6;
1957 					n->cmds = $7;
1958 					n->relkind = OBJECT_MATVIEW;
1959 					n->missing_ok = true;
1960 					$$ = (Node *)n;
1961 				}
1962 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1963 				{
1964 					AlterTableMoveAllStmt *n =
1965 						makeNode(AlterTableMoveAllStmt);
1966 					n->orig_tablespacename = $7;
1967 					n->objtype = OBJECT_MATVIEW;
1968 					n->roles = NIL;
1969 					n->new_tablespacename = $10;
1970 					n->nowait = $11;
1971 					$$ = (Node *)n;
1972 				}
1973 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1974 				{
1975 					AlterTableMoveAllStmt *n =
1976 						makeNode(AlterTableMoveAllStmt);
1977 					n->orig_tablespacename = $7;
1978 					n->objtype = OBJECT_MATVIEW;
1979 					n->roles = $10;
1980 					n->new_tablespacename = $13;
1981 					n->nowait = $14;
1982 					$$ = (Node *)n;
1983 				}
1984 		;
1985 
1986 alter_table_cmds:
1987 			alter_table_cmd							{ $$ = list_make1($1); }
1988 			| alter_table_cmds ',' alter_table_cmd	{ $$ = lappend($1, $3); }
1989 		;
1990 
1991 alter_table_cmd:
1992 			/* ALTER TABLE <name> ADD <coldef> */
1993 			ADD_P columnDef
1994 				{
1995 					AlterTableCmd *n = makeNode(AlterTableCmd);
1996 					n->subtype = AT_AddColumn;
1997 					n->def = $2;
1998 					n->missing_ok = false;
1999 					$$ = (Node *)n;
2000 				}
2001 			/* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
2002 			| ADD_P IF_P NOT EXISTS columnDef
2003 				{
2004 					AlterTableCmd *n = makeNode(AlterTableCmd);
2005 					n->subtype = AT_AddColumn;
2006 					n->def = $5;
2007 					n->missing_ok = true;
2008 					$$ = (Node *)n;
2009 				}
2010 			/* ALTER TABLE <name> ADD COLUMN <coldef> */
2011 			| ADD_P COLUMN columnDef
2012 				{
2013 					AlterTableCmd *n = makeNode(AlterTableCmd);
2014 					n->subtype = AT_AddColumn;
2015 					n->def = $3;
2016 					n->missing_ok = false;
2017 					$$ = (Node *)n;
2018 				}
2019 			/* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
2020 			| ADD_P COLUMN IF_P NOT EXISTS columnDef
2021 				{
2022 					AlterTableCmd *n = makeNode(AlterTableCmd);
2023 					n->subtype = AT_AddColumn;
2024 					n->def = $6;
2025 					n->missing_ok = true;
2026 					$$ = (Node *)n;
2027 				}
2028 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
2029 			| ALTER opt_column ColId alter_column_default
2030 				{
2031 					AlterTableCmd *n = makeNode(AlterTableCmd);
2032 					n->subtype = AT_ColumnDefault;
2033 					n->name = $3;
2034 					n->def = $4;
2035 					$$ = (Node *)n;
2036 				}
2037 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
2038 			| ALTER opt_column ColId DROP NOT NULL_P
2039 				{
2040 					AlterTableCmd *n = makeNode(AlterTableCmd);
2041 					n->subtype = AT_DropNotNull;
2042 					n->name = $3;
2043 					$$ = (Node *)n;
2044 				}
2045 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
2046 			| ALTER opt_column ColId SET NOT NULL_P
2047 				{
2048 					AlterTableCmd *n = makeNode(AlterTableCmd);
2049 					n->subtype = AT_SetNotNull;
2050 					n->name = $3;
2051 					$$ = (Node *)n;
2052 				}
2053 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
2054 			| ALTER opt_column ColId SET STATISTICS SignedIconst
2055 				{
2056 					AlterTableCmd *n = makeNode(AlterTableCmd);
2057 					n->subtype = AT_SetStatistics;
2058 					n->name = $3;
2059 					n->def = (Node *) makeInteger($6);
2060 					$$ = (Node *)n;
2061 				}
2062 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
2063 			| ALTER opt_column ColId SET reloptions
2064 				{
2065 					AlterTableCmd *n = makeNode(AlterTableCmd);
2066 					n->subtype = AT_SetOptions;
2067 					n->name = $3;
2068 					n->def = (Node *) $5;
2069 					$$ = (Node *)n;
2070 				}
2071 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
2072 			| ALTER opt_column ColId RESET reloptions
2073 				{
2074 					AlterTableCmd *n = makeNode(AlterTableCmd);
2075 					n->subtype = AT_ResetOptions;
2076 					n->name = $3;
2077 					n->def = (Node *) $5;
2078 					$$ = (Node *)n;
2079 				}
2080 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
2081 			| ALTER opt_column ColId SET STORAGE ColId
2082 				{
2083 					AlterTableCmd *n = makeNode(AlterTableCmd);
2084 					n->subtype = AT_SetStorage;
2085 					n->name = $3;
2086 					n->def = (Node *) makeString($6);
2087 					$$ = (Node *)n;
2088 				}
2089 			/* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
2090 			| DROP opt_column IF_P EXISTS ColId opt_drop_behavior
2091 				{
2092 					AlterTableCmd *n = makeNode(AlterTableCmd);
2093 					n->subtype = AT_DropColumn;
2094 					n->name = $5;
2095 					n->behavior = $6;
2096 					n->missing_ok = TRUE;
2097 					$$ = (Node *)n;
2098 				}
2099 			/* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
2100 			| DROP opt_column ColId opt_drop_behavior
2101 				{
2102 					AlterTableCmd *n = makeNode(AlterTableCmd);
2103 					n->subtype = AT_DropColumn;
2104 					n->name = $3;
2105 					n->behavior = $4;
2106 					n->missing_ok = FALSE;
2107 					$$ = (Node *)n;
2108 				}
2109 			/*
2110 			 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
2111 			 *		[ USING <expression> ]
2112 			 */
2113 			| ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
2114 				{
2115 					AlterTableCmd *n = makeNode(AlterTableCmd);
2116 					ColumnDef *def = makeNode(ColumnDef);
2117 					n->subtype = AT_AlterColumnType;
2118 					n->name = $3;
2119 					n->def = (Node *) def;
2120 					/* We only use these fields of the ColumnDef node */
2121 					def->typeName = $6;
2122 					def->collClause = (CollateClause *) $7;
2123 					def->raw_default = $8;
2124 					def->location = @3;
2125 					$$ = (Node *)n;
2126 				}
2127 			/* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
2128 			| ALTER opt_column ColId alter_generic_options
2129 				{
2130 					AlterTableCmd *n = makeNode(AlterTableCmd);
2131 					n->subtype = AT_AlterColumnGenericOptions;
2132 					n->name = $3;
2133 					n->def = (Node *) $4;
2134 					$$ = (Node *)n;
2135 				}
2136 			/* ALTER TABLE <name> ADD CONSTRAINT ... */
2137 			| ADD_P TableConstraint
2138 				{
2139 					AlterTableCmd *n = makeNode(AlterTableCmd);
2140 					n->subtype = AT_AddConstraint;
2141 					n->def = $2;
2142 					n->missing_ok = false;
2143 					$$ = (Node *)n;
2144 				}
2145 			/* ALTER TABLE <name> ALTER CONSTRAINT ... */
2146 			| ALTER CONSTRAINT name ConstraintAttributeSpec
2147 				{
2148 					AlterTableCmd *n = makeNode(AlterTableCmd);
2149 					Constraint *c = makeNode(Constraint);
2150 					n->subtype = AT_AlterConstraint;
2151 					n->def = (Node *) c;
2152 					c->contype = CONSTR_FOREIGN; /* others not supported, yet */
2153 					c->conname = $3;
2154 					processCASbits($4, @4, "ALTER CONSTRAINT statement",
2155 									&c->deferrable,
2156 									&c->initdeferred,
2157 									NULL, NULL, yyscanner);
2158 					$$ = (Node *)n;
2159 				}
2160 			/* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
2161 			| VALIDATE CONSTRAINT name
2162 				{
2163 					AlterTableCmd *n = makeNode(AlterTableCmd);
2164 					n->subtype = AT_ValidateConstraint;
2165 					n->name = $3;
2166 					$$ = (Node *)n;
2167 				}
2168 			/* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
2169 			| DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
2170 				{
2171 					AlterTableCmd *n = makeNode(AlterTableCmd);
2172 					n->subtype = AT_DropConstraint;
2173 					n->name = $5;
2174 					n->behavior = $6;
2175 					n->missing_ok = TRUE;
2176 					$$ = (Node *)n;
2177 				}
2178 			/* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
2179 			| DROP CONSTRAINT name opt_drop_behavior
2180 				{
2181 					AlterTableCmd *n = makeNode(AlterTableCmd);
2182 					n->subtype = AT_DropConstraint;
2183 					n->name = $3;
2184 					n->behavior = $4;
2185 					n->missing_ok = FALSE;
2186 					$$ = (Node *)n;
2187 				}
2188 			/* ALTER TABLE <name> SET WITH OIDS  */
2189 			| SET WITH OIDS
2190 				{
2191 					AlterTableCmd *n = makeNode(AlterTableCmd);
2192 					n->subtype = AT_AddOids;
2193 					$$ = (Node *)n;
2194 				}
2195 			/* ALTER TABLE <name> SET WITHOUT OIDS  */
2196 			| SET WITHOUT OIDS
2197 				{
2198 					AlterTableCmd *n = makeNode(AlterTableCmd);
2199 					n->subtype = AT_DropOids;
2200 					$$ = (Node *)n;
2201 				}
2202 			/* ALTER TABLE <name> CLUSTER ON <indexname> */
2203 			| CLUSTER ON name
2204 				{
2205 					AlterTableCmd *n = makeNode(AlterTableCmd);
2206 					n->subtype = AT_ClusterOn;
2207 					n->name = $3;
2208 					$$ = (Node *)n;
2209 				}
2210 			/* ALTER TABLE <name> SET WITHOUT CLUSTER */
2211 			| SET WITHOUT CLUSTER
2212 				{
2213 					AlterTableCmd *n = makeNode(AlterTableCmd);
2214 					n->subtype = AT_DropCluster;
2215 					n->name = NULL;
2216 					$$ = (Node *)n;
2217 				}
2218 			/* ALTER TABLE <name> SET LOGGED  */
2219 			| SET LOGGED
2220 				{
2221 					AlterTableCmd *n = makeNode(AlterTableCmd);
2222 					n->subtype = AT_SetLogged;
2223 					$$ = (Node *)n;
2224 				}
2225 			/* ALTER TABLE <name> SET UNLOGGED  */
2226 			| SET UNLOGGED
2227 				{
2228 					AlterTableCmd *n = makeNode(AlterTableCmd);
2229 					n->subtype = AT_SetUnLogged;
2230 					$$ = (Node *)n;
2231 				}
2232 			/* ALTER TABLE <name> ENABLE TRIGGER <trig> */
2233 			| ENABLE_P TRIGGER name
2234 				{
2235 					AlterTableCmd *n = makeNode(AlterTableCmd);
2236 					n->subtype = AT_EnableTrig;
2237 					n->name = $3;
2238 					$$ = (Node *)n;
2239 				}
2240 			/* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
2241 			| ENABLE_P ALWAYS TRIGGER name
2242 				{
2243 					AlterTableCmd *n = makeNode(AlterTableCmd);
2244 					n->subtype = AT_EnableAlwaysTrig;
2245 					n->name = $4;
2246 					$$ = (Node *)n;
2247 				}
2248 			/* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
2249 			| ENABLE_P REPLICA TRIGGER name
2250 				{
2251 					AlterTableCmd *n = makeNode(AlterTableCmd);
2252 					n->subtype = AT_EnableReplicaTrig;
2253 					n->name = $4;
2254 					$$ = (Node *)n;
2255 				}
2256 			/* ALTER TABLE <name> ENABLE TRIGGER ALL */
2257 			| ENABLE_P TRIGGER ALL
2258 				{
2259 					AlterTableCmd *n = makeNode(AlterTableCmd);
2260 					n->subtype = AT_EnableTrigAll;
2261 					$$ = (Node *)n;
2262 				}
2263 			/* ALTER TABLE <name> ENABLE TRIGGER USER */
2264 			| ENABLE_P TRIGGER USER
2265 				{
2266 					AlterTableCmd *n = makeNode(AlterTableCmd);
2267 					n->subtype = AT_EnableTrigUser;
2268 					$$ = (Node *)n;
2269 				}
2270 			/* ALTER TABLE <name> DISABLE TRIGGER <trig> */
2271 			| DISABLE_P TRIGGER name
2272 				{
2273 					AlterTableCmd *n = makeNode(AlterTableCmd);
2274 					n->subtype = AT_DisableTrig;
2275 					n->name = $3;
2276 					$$ = (Node *)n;
2277 				}
2278 			/* ALTER TABLE <name> DISABLE TRIGGER ALL */
2279 			| DISABLE_P TRIGGER ALL
2280 				{
2281 					AlterTableCmd *n = makeNode(AlterTableCmd);
2282 					n->subtype = AT_DisableTrigAll;
2283 					$$ = (Node *)n;
2284 				}
2285 			/* ALTER TABLE <name> DISABLE TRIGGER USER */
2286 			| DISABLE_P TRIGGER USER
2287 				{
2288 					AlterTableCmd *n = makeNode(AlterTableCmd);
2289 					n->subtype = AT_DisableTrigUser;
2290 					$$ = (Node *)n;
2291 				}
2292 			/* ALTER TABLE <name> ENABLE RULE <rule> */
2293 			| ENABLE_P RULE name
2294 				{
2295 					AlterTableCmd *n = makeNode(AlterTableCmd);
2296 					n->subtype = AT_EnableRule;
2297 					n->name = $3;
2298 					$$ = (Node *)n;
2299 				}
2300 			/* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
2301 			| ENABLE_P ALWAYS RULE name
2302 				{
2303 					AlterTableCmd *n = makeNode(AlterTableCmd);
2304 					n->subtype = AT_EnableAlwaysRule;
2305 					n->name = $4;
2306 					$$ = (Node *)n;
2307 				}
2308 			/* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
2309 			| ENABLE_P REPLICA RULE name
2310 				{
2311 					AlterTableCmd *n = makeNode(AlterTableCmd);
2312 					n->subtype = AT_EnableReplicaRule;
2313 					n->name = $4;
2314 					$$ = (Node *)n;
2315 				}
2316 			/* ALTER TABLE <name> DISABLE RULE <rule> */
2317 			| DISABLE_P RULE name
2318 				{
2319 					AlterTableCmd *n = makeNode(AlterTableCmd);
2320 					n->subtype = AT_DisableRule;
2321 					n->name = $3;
2322 					$$ = (Node *)n;
2323 				}
2324 			/* ALTER TABLE <name> INHERIT <parent> */
2325 			| INHERIT qualified_name
2326 				{
2327 					AlterTableCmd *n = makeNode(AlterTableCmd);
2328 					n->subtype = AT_AddInherit;
2329 					n->def = (Node *) $2;
2330 					$$ = (Node *)n;
2331 				}
2332 			/* ALTER TABLE <name> NO INHERIT <parent> */
2333 			| NO INHERIT qualified_name
2334 				{
2335 					AlterTableCmd *n = makeNode(AlterTableCmd);
2336 					n->subtype = AT_DropInherit;
2337 					n->def = (Node *) $3;
2338 					$$ = (Node *)n;
2339 				}
2340 			/* ALTER TABLE <name> OF <type_name> */
2341 			| OF any_name
2342 				{
2343 					AlterTableCmd *n = makeNode(AlterTableCmd);
2344 					TypeName *def = makeTypeNameFromNameList($2);
2345 					def->location = @2;
2346 					n->subtype = AT_AddOf;
2347 					n->def = (Node *) def;
2348 					$$ = (Node *)n;
2349 				}
2350 			/* ALTER TABLE <name> NOT OF */
2351 			| NOT OF
2352 				{
2353 					AlterTableCmd *n = makeNode(AlterTableCmd);
2354 					n->subtype = AT_DropOf;
2355 					$$ = (Node *)n;
2356 				}
2357 			/* ALTER TABLE <name> OWNER TO RoleSpec */
2358 			| OWNER TO RoleSpec
2359 				{
2360 					AlterTableCmd *n = makeNode(AlterTableCmd);
2361 					n->subtype = AT_ChangeOwner;
2362 					n->newowner = $3;
2363 					$$ = (Node *)n;
2364 				}
2365 			/* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
2366 			| SET TABLESPACE name
2367 				{
2368 					AlterTableCmd *n = makeNode(AlterTableCmd);
2369 					n->subtype = AT_SetTableSpace;
2370 					n->name = $3;
2371 					$$ = (Node *)n;
2372 				}
2373 			/* ALTER TABLE <name> SET (...) */
2374 			| SET reloptions
2375 				{
2376 					AlterTableCmd *n = makeNode(AlterTableCmd);
2377 					n->subtype = AT_SetRelOptions;
2378 					n->def = (Node *)$2;
2379 					$$ = (Node *)n;
2380 				}
2381 			/* ALTER TABLE <name> RESET (...) */
2382 			| RESET reloptions
2383 				{
2384 					AlterTableCmd *n = makeNode(AlterTableCmd);
2385 					n->subtype = AT_ResetRelOptions;
2386 					n->def = (Node *)$2;
2387 					$$ = (Node *)n;
2388 				}
2389 			/* ALTER TABLE <name> REPLICA IDENTITY  */
2390 			| REPLICA IDENTITY_P replica_identity
2391 				{
2392 					AlterTableCmd *n = makeNode(AlterTableCmd);
2393 					n->subtype = AT_ReplicaIdentity;
2394 					n->def = $3;
2395 					$$ = (Node *)n;
2396 				}
2397 			/* ALTER TABLE <name> ENABLE ROW LEVEL SECURITY */
2398 			| ENABLE_P ROW LEVEL SECURITY
2399 				{
2400 					AlterTableCmd *n = makeNode(AlterTableCmd);
2401 					n->subtype = AT_EnableRowSecurity;
2402 					$$ = (Node *)n;
2403 				}
2404 			/* ALTER TABLE <name> DISABLE ROW LEVEL SECURITY */
2405 			| DISABLE_P ROW LEVEL SECURITY
2406 				{
2407 					AlterTableCmd *n = makeNode(AlterTableCmd);
2408 					n->subtype = AT_DisableRowSecurity;
2409 					$$ = (Node *)n;
2410 				}
2411 			/* ALTER TABLE <name> FORCE ROW LEVEL SECURITY */
2412 			| FORCE ROW LEVEL SECURITY
2413 				{
2414 					AlterTableCmd *n = makeNode(AlterTableCmd);
2415 					n->subtype = AT_ForceRowSecurity;
2416 					$$ = (Node *)n;
2417 				}
2418 			/* ALTER TABLE <name> NO FORCE ROW LEVEL SECURITY */
2419 			| NO FORCE ROW LEVEL SECURITY
2420 				{
2421 					AlterTableCmd *n = makeNode(AlterTableCmd);
2422 					n->subtype = AT_NoForceRowSecurity;
2423 					$$ = (Node *)n;
2424 				}
2425 			| alter_generic_options
2426 				{
2427 					AlterTableCmd *n = makeNode(AlterTableCmd);
2428 					n->subtype = AT_GenericOptions;
2429 					n->def = (Node *)$1;
2430 					$$ = (Node *) n;
2431 				}
2432 		;
2433 
2434 alter_column_default:
2435 			SET DEFAULT a_expr			{ $$ = $3; }
2436 			| DROP DEFAULT				{ $$ = NULL; }
2437 		;
2438 
2439 opt_drop_behavior:
2440 			CASCADE						{ $$ = DROP_CASCADE; }
2441 			| RESTRICT					{ $$ = DROP_RESTRICT; }
2442 			| /* EMPTY */				{ $$ = DROP_RESTRICT; /* default */ }
2443 		;
2444 
2445 opt_collate_clause:
2446 			COLLATE any_name
2447 				{
2448 					CollateClause *n = makeNode(CollateClause);
2449 					n->arg = NULL;
2450 					n->collname = $2;
2451 					n->location = @1;
2452 					$$ = (Node *) n;
2453 				}
2454 			| /* EMPTY */				{ $$ = NULL; }
2455 		;
2456 
2457 alter_using:
2458 			USING a_expr				{ $$ = $2; }
2459 			| /* EMPTY */				{ $$ = NULL; }
2460 		;
2461 
2462 replica_identity:
2463 			NOTHING
2464 				{
2465 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2466 					n->identity_type = REPLICA_IDENTITY_NOTHING;
2467 					n->name = NULL;
2468 					$$ = (Node *) n;
2469 				}
2470 			| FULL
2471 				{
2472 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2473 					n->identity_type = REPLICA_IDENTITY_FULL;
2474 					n->name = NULL;
2475 					$$ = (Node *) n;
2476 				}
2477 			| DEFAULT
2478 				{
2479 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2480 					n->identity_type = REPLICA_IDENTITY_DEFAULT;
2481 					n->name = NULL;
2482 					$$ = (Node *) n;
2483 				}
2484 			| USING INDEX name
2485 				{
2486 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2487 					n->identity_type = REPLICA_IDENTITY_INDEX;
2488 					n->name = $3;
2489 					$$ = (Node *) n;
2490 				}
2491 ;
2492 
2493 reloptions:
2494 			'(' reloption_list ')'					{ $$ = $2; }
2495 		;
2496 
2497 opt_reloptions:		WITH reloptions					{ $$ = $2; }
2498 			 |		/* EMPTY */						{ $$ = NIL; }
2499 		;
2500 
2501 reloption_list:
2502 			reloption_elem							{ $$ = list_make1($1); }
2503 			| reloption_list ',' reloption_elem		{ $$ = lappend($1, $3); }
2504 		;
2505 
2506 /* This should match def_elem and also allow qualified names */
2507 reloption_elem:
2508 			ColLabel '=' def_arg
2509 				{
2510 					$$ = makeDefElem($1, (Node *) $3);
2511 				}
2512 			| ColLabel
2513 				{
2514 					$$ = makeDefElem($1, NULL);
2515 				}
2516 			| ColLabel '.' ColLabel '=' def_arg
2517 				{
2518 					$$ = makeDefElemExtended($1, $3, (Node *) $5,
2519 											 DEFELEM_UNSPEC);
2520 				}
2521 			| ColLabel '.' ColLabel
2522 				{
2523 					$$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC);
2524 				}
2525 		;
2526 
2527 
2528 /*****************************************************************************
2529  *
2530  *	ALTER TYPE
2531  *
2532  * really variants of the ALTER TABLE subcommands with different spellings
2533  *****************************************************************************/
2534 
2535 AlterCompositeTypeStmt:
2536 			ALTER TYPE_P any_name alter_type_cmds
2537 				{
2538 					AlterTableStmt *n = makeNode(AlterTableStmt);
2539 
2540 					/* can't use qualified_name, sigh */
2541 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
2542 					n->cmds = $4;
2543 					n->relkind = OBJECT_TYPE;
2544 					$$ = (Node *)n;
2545 				}
2546 			;
2547 
2548 alter_type_cmds:
2549 			alter_type_cmd							{ $$ = list_make1($1); }
2550 			| alter_type_cmds ',' alter_type_cmd	{ $$ = lappend($1, $3); }
2551 		;
2552 
2553 alter_type_cmd:
2554 			/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
2555 			ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
2556 				{
2557 					AlterTableCmd *n = makeNode(AlterTableCmd);
2558 					n->subtype = AT_AddColumn;
2559 					n->def = $3;
2560 					n->behavior = $4;
2561 					$$ = (Node *)n;
2562 				}
2563 			/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
2564 			| DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
2565 				{
2566 					AlterTableCmd *n = makeNode(AlterTableCmd);
2567 					n->subtype = AT_DropColumn;
2568 					n->name = $5;
2569 					n->behavior = $6;
2570 					n->missing_ok = TRUE;
2571 					$$ = (Node *)n;
2572 				}
2573 			/* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
2574 			| DROP ATTRIBUTE ColId opt_drop_behavior
2575 				{
2576 					AlterTableCmd *n = makeNode(AlterTableCmd);
2577 					n->subtype = AT_DropColumn;
2578 					n->name = $3;
2579 					n->behavior = $4;
2580 					n->missing_ok = FALSE;
2581 					$$ = (Node *)n;
2582 				}
2583 			/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
2584 			| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
2585 				{
2586 					AlterTableCmd *n = makeNode(AlterTableCmd);
2587 					ColumnDef *def = makeNode(ColumnDef);
2588 					n->subtype = AT_AlterColumnType;
2589 					n->name = $3;
2590 					n->def = (Node *) def;
2591 					n->behavior = $8;
2592 					/* We only use these fields of the ColumnDef node */
2593 					def->typeName = $6;
2594 					def->collClause = (CollateClause *) $7;
2595 					def->raw_default = NULL;
2596 					def->location = @3;
2597 					$$ = (Node *)n;
2598 				}
2599 		;
2600 
2601 
2602 /*****************************************************************************
2603  *
2604  *		QUERY :
2605  *				close <portalname>
2606  *
2607  *****************************************************************************/
2608 
2609 ClosePortalStmt:
2610 			CLOSE cursor_name
2611 				{
2612 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2613 					n->portalname = $2;
2614 					$$ = (Node *)n;
2615 				}
2616 			| CLOSE ALL
2617 				{
2618 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2619 					n->portalname = NULL;
2620 					$$ = (Node *)n;
2621 				}
2622 		;
2623 
2624 
2625 /*****************************************************************************
2626  *
2627  *		QUERY :
2628  *				COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
2629  *				COPY ( query ) TO file	[WITH] [(options)]
2630  *
2631  *				where 'query' can be one of:
2632  *				{ SELECT | UPDATE | INSERT | DELETE }
2633  *
2634  *				and 'file' can be one of:
2635  *				{ PROGRAM 'command' | STDIN | STDOUT | 'filename' }
2636  *
2637  *				In the preferred syntax the options are comma-separated
2638  *				and use generic identifiers instead of keywords.  The pre-9.0
2639  *				syntax had a hard-wired, space-separated set of options.
2640  *
2641  *				Really old syntax, from versions 7.2 and prior:
2642  *				COPY [ BINARY ] table [ WITH OIDS ] FROM/TO file
2643  *					[ [ USING ] DELIMITERS 'delimiter' ] ]
2644  *					[ WITH NULL AS 'null string' ]
2645  *				This option placement is not supported with COPY (query...).
2646  *
2647  *****************************************************************************/
2648 
2649 CopyStmt:	COPY opt_binary qualified_name opt_column_list opt_oids
2650 			copy_from opt_program copy_file_name copy_delimiter opt_with copy_options
2651 				{
2652 					CopyStmt *n = makeNode(CopyStmt);
2653 					n->relation = $3;
2654 					n->query = NULL;
2655 					n->attlist = $4;
2656 					n->is_from = $6;
2657 					n->is_program = $7;
2658 					n->filename = $8;
2659 
2660 					if (n->is_program && n->filename == NULL)
2661 						ereport(ERROR,
2662 								(errcode(ERRCODE_SYNTAX_ERROR),
2663 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2664 								 parser_errposition(@8)));
2665 
2666 					n->options = NIL;
2667 					/* Concatenate user-supplied flags */
2668 					if ($2)
2669 						n->options = lappend(n->options, $2);
2670 					if ($5)
2671 						n->options = lappend(n->options, $5);
2672 					if ($9)
2673 						n->options = lappend(n->options, $9);
2674 					if ($11)
2675 						n->options = list_concat(n->options, $11);
2676 					$$ = (Node *)n;
2677 				}
2678 			| COPY '(' PreparableStmt ')' TO opt_program copy_file_name opt_with copy_options
2679 				{
2680 					CopyStmt *n = makeNode(CopyStmt);
2681 					n->relation = NULL;
2682 					n->query = $3;
2683 					n->attlist = NIL;
2684 					n->is_from = false;
2685 					n->is_program = $6;
2686 					n->filename = $7;
2687 					n->options = $9;
2688 
2689 					if (n->is_program && n->filename == NULL)
2690 						ereport(ERROR,
2691 								(errcode(ERRCODE_SYNTAX_ERROR),
2692 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2693 								 parser_errposition(@5)));
2694 
2695 					$$ = (Node *)n;
2696 				}
2697 		;
2698 
2699 copy_from:
2700 			FROM									{ $$ = TRUE; }
2701 			| TO									{ $$ = FALSE; }
2702 		;
2703 
2704 opt_program:
2705 			PROGRAM									{ $$ = TRUE; }
2706 			| /* EMPTY */							{ $$ = FALSE; }
2707 		;
2708 
2709 /*
2710  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
2711  * used depends on the direction. (It really doesn't make sense to copy from
2712  * stdout. We silently correct the "typo".)		 - AY 9/94
2713  */
2714 copy_file_name:
2715 			Sconst									{ $$ = $1; }
2716 			| STDIN									{ $$ = NULL; }
2717 			| STDOUT								{ $$ = NULL; }
2718 		;
2719 
2720 copy_options: copy_opt_list							{ $$ = $1; }
2721 			| '(' copy_generic_opt_list ')'			{ $$ = $2; }
2722 		;
2723 
2724 /* old COPY option syntax */
2725 copy_opt_list:
2726 			copy_opt_list copy_opt_item				{ $$ = lappend($1, $2); }
2727 			| /* EMPTY */							{ $$ = NIL; }
2728 		;
2729 
2730 copy_opt_item:
2731 			BINARY
2732 				{
2733 					$$ = makeDefElem("format", (Node *)makeString("binary"));
2734 				}
2735 			| OIDS
2736 				{
2737 					$$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2738 				}
2739 			| FREEZE
2740 				{
2741 					$$ = makeDefElem("freeze", (Node *)makeInteger(TRUE));
2742 				}
2743 			| DELIMITER opt_as Sconst
2744 				{
2745 					$$ = makeDefElem("delimiter", (Node *)makeString($3));
2746 				}
2747 			| NULL_P opt_as Sconst
2748 				{
2749 					$$ = makeDefElem("null", (Node *)makeString($3));
2750 				}
2751 			| CSV
2752 				{
2753 					$$ = makeDefElem("format", (Node *)makeString("csv"));
2754 				}
2755 			| HEADER_P
2756 				{
2757 					$$ = makeDefElem("header", (Node *)makeInteger(TRUE));
2758 				}
2759 			| QUOTE opt_as Sconst
2760 				{
2761 					$$ = makeDefElem("quote", (Node *)makeString($3));
2762 				}
2763 			| ESCAPE opt_as Sconst
2764 				{
2765 					$$ = makeDefElem("escape", (Node *)makeString($3));
2766 				}
2767 			| FORCE QUOTE columnList
2768 				{
2769 					$$ = makeDefElem("force_quote", (Node *)$3);
2770 				}
2771 			| FORCE QUOTE '*'
2772 				{
2773 					$$ = makeDefElem("force_quote", (Node *)makeNode(A_Star));
2774 				}
2775 			| FORCE NOT NULL_P columnList
2776 				{
2777 					$$ = makeDefElem("force_not_null", (Node *)$4);
2778 				}
2779 			| FORCE NULL_P columnList
2780 				{
2781 					$$ = makeDefElem("force_null", (Node *)$3);
2782 				}
2783 			| ENCODING Sconst
2784 				{
2785 					$$ = makeDefElem("encoding", (Node *)makeString($2));
2786 				}
2787 		;
2788 
2789 /* The following exist for backward compatibility with very old versions */
2790 
2791 opt_binary:
2792 			BINARY
2793 				{
2794 					$$ = makeDefElem("format", (Node *)makeString("binary"));
2795 				}
2796 			| /*EMPTY*/								{ $$ = NULL; }
2797 		;
2798 
2799 opt_oids:
2800 			WITH OIDS
2801 				{
2802 					$$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2803 				}
2804 			| /*EMPTY*/								{ $$ = NULL; }
2805 		;
2806 
2807 copy_delimiter:
2808 			opt_using DELIMITERS Sconst
2809 				{
2810 					$$ = makeDefElem("delimiter", (Node *)makeString($3));
2811 				}
2812 			| /*EMPTY*/								{ $$ = NULL; }
2813 		;
2814 
2815 opt_using:
2816 			USING									{}
2817 			| /*EMPTY*/								{}
2818 		;
2819 
2820 /* new COPY option syntax */
2821 copy_generic_opt_list:
2822 			copy_generic_opt_elem
2823 				{
2824 					$$ = list_make1($1);
2825 				}
2826 			| copy_generic_opt_list ',' copy_generic_opt_elem
2827 				{
2828 					$$ = lappend($1, $3);
2829 				}
2830 		;
2831 
2832 copy_generic_opt_elem:
2833 			ColLabel copy_generic_opt_arg
2834 				{
2835 					$$ = makeDefElem($1, $2);
2836 				}
2837 		;
2838 
2839 copy_generic_opt_arg:
2840 			opt_boolean_or_string			{ $$ = (Node *) makeString($1); }
2841 			| NumericOnly					{ $$ = (Node *) $1; }
2842 			| '*'							{ $$ = (Node *) makeNode(A_Star); }
2843 			| '(' copy_generic_opt_arg_list ')'		{ $$ = (Node *) $2; }
2844 			| /* EMPTY */					{ $$ = NULL; }
2845 		;
2846 
2847 copy_generic_opt_arg_list:
2848 			  copy_generic_opt_arg_list_item
2849 				{
2850 					$$ = list_make1($1);
2851 				}
2852 			| copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
2853 				{
2854 					$$ = lappend($1, $3);
2855 				}
2856 		;
2857 
2858 /* beware of emitting non-string list elements here; see commands/define.c */
2859 copy_generic_opt_arg_list_item:
2860 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
2861 		;
2862 
2863 
2864 /*****************************************************************************
2865  *
2866  *		QUERY :
2867  *				CREATE TABLE relname
2868  *
2869  *****************************************************************************/
2870 
2871 CreateStmt:	CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
2872 			OptInherit OptWith OnCommitOption OptTableSpace
2873 				{
2874 					CreateStmt *n = makeNode(CreateStmt);
2875 					$4->relpersistence = $2;
2876 					n->relation = $4;
2877 					n->tableElts = $6;
2878 					n->inhRelations = $8;
2879 					n->ofTypename = NULL;
2880 					n->constraints = NIL;
2881 					n->options = $9;
2882 					n->oncommit = $10;
2883 					n->tablespacename = $11;
2884 					n->if_not_exists = false;
2885 					$$ = (Node *)n;
2886 				}
2887 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
2888 			OptTableElementList ')' OptInherit OptWith OnCommitOption
2889 			OptTableSpace
2890 				{
2891 					CreateStmt *n = makeNode(CreateStmt);
2892 					$7->relpersistence = $2;
2893 					n->relation = $7;
2894 					n->tableElts = $9;
2895 					n->inhRelations = $11;
2896 					n->ofTypename = NULL;
2897 					n->constraints = NIL;
2898 					n->options = $12;
2899 					n->oncommit = $13;
2900 					n->tablespacename = $14;
2901 					n->if_not_exists = true;
2902 					$$ = (Node *)n;
2903 				}
2904 		| CREATE OptTemp TABLE qualified_name OF any_name
2905 			OptTypedTableElementList OptWith OnCommitOption OptTableSpace
2906 				{
2907 					CreateStmt *n = makeNode(CreateStmt);
2908 					$4->relpersistence = $2;
2909 					n->relation = $4;
2910 					n->tableElts = $7;
2911 					n->inhRelations = NIL;
2912 					n->ofTypename = makeTypeNameFromNameList($6);
2913 					n->ofTypename->location = @6;
2914 					n->constraints = NIL;
2915 					n->options = $8;
2916 					n->oncommit = $9;
2917 					n->tablespacename = $10;
2918 					n->if_not_exists = false;
2919 					$$ = (Node *)n;
2920 				}
2921 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
2922 			OptTypedTableElementList OptWith OnCommitOption OptTableSpace
2923 				{
2924 					CreateStmt *n = makeNode(CreateStmt);
2925 					$7->relpersistence = $2;
2926 					n->relation = $7;
2927 					n->tableElts = $10;
2928 					n->inhRelations = NIL;
2929 					n->ofTypename = makeTypeNameFromNameList($9);
2930 					n->ofTypename->location = @9;
2931 					n->constraints = NIL;
2932 					n->options = $11;
2933 					n->oncommit = $12;
2934 					n->tablespacename = $13;
2935 					n->if_not_exists = true;
2936 					$$ = (Node *)n;
2937 				}
2938 		;
2939 
2940 /*
2941  * Redundancy here is needed to avoid shift/reduce conflicts,
2942  * since TEMP is not a reserved word.  See also OptTempTableName.
2943  *
2944  * NOTE: we accept both GLOBAL and LOCAL options.  They currently do nothing,
2945  * but future versions might consider GLOBAL to request SQL-spec-compliant
2946  * temp table behavior, so warn about that.  Since we have no modules the
2947  * LOCAL keyword is really meaningless; furthermore, some other products
2948  * implement LOCAL as meaning the same as our default temp table behavior,
2949  * so we'll probably continue to treat LOCAL as a noise word.
2950  */
2951 OptTemp:	TEMPORARY					{ $$ = RELPERSISTENCE_TEMP; }
2952 			| TEMP						{ $$ = RELPERSISTENCE_TEMP; }
2953 			| LOCAL TEMPORARY			{ $$ = RELPERSISTENCE_TEMP; }
2954 			| LOCAL TEMP				{ $$ = RELPERSISTENCE_TEMP; }
2955 			| GLOBAL TEMPORARY
2956 				{
2957 					ereport(WARNING,
2958 							(errmsg("GLOBAL is deprecated in temporary table creation"),
2959 							 parser_errposition(@1)));
2960 					$$ = RELPERSISTENCE_TEMP;
2961 				}
2962 			| GLOBAL TEMP
2963 				{
2964 					ereport(WARNING,
2965 							(errmsg("GLOBAL is deprecated in temporary table creation"),
2966 							 parser_errposition(@1)));
2967 					$$ = RELPERSISTENCE_TEMP;
2968 				}
2969 			| UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
2970 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
2971 		;
2972 
2973 OptTableElementList:
2974 			TableElementList					{ $$ = $1; }
2975 			| /*EMPTY*/							{ $$ = NIL; }
2976 		;
2977 
2978 OptTypedTableElementList:
2979 			'(' TypedTableElementList ')'		{ $$ = $2; }
2980 			| /*EMPTY*/							{ $$ = NIL; }
2981 		;
2982 
2983 TableElementList:
2984 			TableElement
2985 				{
2986 					$$ = list_make1($1);
2987 				}
2988 			| TableElementList ',' TableElement
2989 				{
2990 					$$ = lappend($1, $3);
2991 				}
2992 		;
2993 
2994 TypedTableElementList:
2995 			TypedTableElement
2996 				{
2997 					$$ = list_make1($1);
2998 				}
2999 			| TypedTableElementList ',' TypedTableElement
3000 				{
3001 					$$ = lappend($1, $3);
3002 				}
3003 		;
3004 
3005 TableElement:
3006 			columnDef							{ $$ = $1; }
3007 			| TableLikeClause					{ $$ = $1; }
3008 			| TableConstraint					{ $$ = $1; }
3009 		;
3010 
3011 TypedTableElement:
3012 			columnOptions						{ $$ = $1; }
3013 			| TableConstraint					{ $$ = $1; }
3014 		;
3015 
3016 columnDef:	ColId Typename create_generic_options ColQualList
3017 				{
3018 					ColumnDef *n = makeNode(ColumnDef);
3019 					n->colname = $1;
3020 					n->typeName = $2;
3021 					n->inhcount = 0;
3022 					n->is_local = true;
3023 					n->is_not_null = false;
3024 					n->is_from_type = false;
3025 					n->storage = 0;
3026 					n->raw_default = NULL;
3027 					n->cooked_default = NULL;
3028 					n->collOid = InvalidOid;
3029 					n->fdwoptions = $3;
3030 					SplitColQualList($4, &n->constraints, &n->collClause,
3031 									 yyscanner);
3032 					n->location = @1;
3033 					$$ = (Node *)n;
3034 				}
3035 		;
3036 
3037 columnOptions:	ColId WITH OPTIONS ColQualList
3038 				{
3039 					ColumnDef *n = makeNode(ColumnDef);
3040 					n->colname = $1;
3041 					n->typeName = NULL;
3042 					n->inhcount = 0;
3043 					n->is_local = true;
3044 					n->is_not_null = false;
3045 					n->is_from_type = false;
3046 					n->storage = 0;
3047 					n->raw_default = NULL;
3048 					n->cooked_default = NULL;
3049 					n->collOid = InvalidOid;
3050 					SplitColQualList($4, &n->constraints, &n->collClause,
3051 									 yyscanner);
3052 					n->location = @1;
3053 					$$ = (Node *)n;
3054 				}
3055 		;
3056 
3057 ColQualList:
3058 			ColQualList ColConstraint				{ $$ = lappend($1, $2); }
3059 			| /*EMPTY*/								{ $$ = NIL; }
3060 		;
3061 
3062 ColConstraint:
3063 			CONSTRAINT name ColConstraintElem
3064 				{
3065 					Constraint *n = (Constraint *) $3;
3066 					Assert(IsA(n, Constraint));
3067 					n->conname = $2;
3068 					n->location = @1;
3069 					$$ = (Node *) n;
3070 				}
3071 			| ColConstraintElem						{ $$ = $1; }
3072 			| ConstraintAttr						{ $$ = $1; }
3073 			| COLLATE any_name
3074 				{
3075 					/*
3076 					 * Note: the CollateClause is momentarily included in
3077 					 * the list built by ColQualList, but we split it out
3078 					 * again in SplitColQualList.
3079 					 */
3080 					CollateClause *n = makeNode(CollateClause);
3081 					n->arg = NULL;
3082 					n->collname = $2;
3083 					n->location = @1;
3084 					$$ = (Node *) n;
3085 				}
3086 		;
3087 
3088 /* DEFAULT NULL is already the default for Postgres.
3089  * But define it here and carry it forward into the system
3090  * to make it explicit.
3091  * - thomas 1998-09-13
3092  *
3093  * WITH NULL and NULL are not SQL-standard syntax elements,
3094  * so leave them out. Use DEFAULT NULL to explicitly indicate
3095  * that a column may have that value. WITH NULL leads to
3096  * shift/reduce conflicts with WITH TIME ZONE anyway.
3097  * - thomas 1999-01-08
3098  *
3099  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
3100  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
3101  * or be part of a_expr NOT LIKE or similar constructs).
3102  */
3103 ColConstraintElem:
3104 			NOT NULL_P
3105 				{
3106 					Constraint *n = makeNode(Constraint);
3107 					n->contype = CONSTR_NOTNULL;
3108 					n->location = @1;
3109 					$$ = (Node *)n;
3110 				}
3111 			| NULL_P
3112 				{
3113 					Constraint *n = makeNode(Constraint);
3114 					n->contype = CONSTR_NULL;
3115 					n->location = @1;
3116 					$$ = (Node *)n;
3117 				}
3118 			| UNIQUE opt_definition OptConsTableSpace
3119 				{
3120 					Constraint *n = makeNode(Constraint);
3121 					n->contype = CONSTR_UNIQUE;
3122 					n->location = @1;
3123 					n->keys = NULL;
3124 					n->options = $2;
3125 					n->indexname = NULL;
3126 					n->indexspace = $3;
3127 					$$ = (Node *)n;
3128 				}
3129 			| PRIMARY KEY opt_definition OptConsTableSpace
3130 				{
3131 					Constraint *n = makeNode(Constraint);
3132 					n->contype = CONSTR_PRIMARY;
3133 					n->location = @1;
3134 					n->keys = NULL;
3135 					n->options = $3;
3136 					n->indexname = NULL;
3137 					n->indexspace = $4;
3138 					$$ = (Node *)n;
3139 				}
3140 			| CHECK '(' a_expr ')' opt_no_inherit
3141 				{
3142 					Constraint *n = makeNode(Constraint);
3143 					n->contype = CONSTR_CHECK;
3144 					n->location = @1;
3145 					n->is_no_inherit = $5;
3146 					n->raw_expr = $3;
3147 					n->cooked_expr = NULL;
3148 					n->skip_validation = false;
3149 					n->initially_valid = true;
3150 					$$ = (Node *)n;
3151 				}
3152 			| DEFAULT b_expr
3153 				{
3154 					Constraint *n = makeNode(Constraint);
3155 					n->contype = CONSTR_DEFAULT;
3156 					n->location = @1;
3157 					n->raw_expr = $2;
3158 					n->cooked_expr = NULL;
3159 					n->skip_validation = false;
3160 					n->initially_valid = true;
3161 					$$ = (Node *)n;
3162 				}
3163 			| REFERENCES qualified_name opt_column_list key_match key_actions
3164 				{
3165 					Constraint *n = makeNode(Constraint);
3166 					n->contype = CONSTR_FOREIGN;
3167 					n->location = @1;
3168 					n->pktable			= $2;
3169 					n->fk_attrs			= NIL;
3170 					n->pk_attrs			= $3;
3171 					n->fk_matchtype		= $4;
3172 					n->fk_upd_action	= (char) ($5 >> 8);
3173 					n->fk_del_action	= (char) ($5 & 0xFF);
3174 					n->skip_validation  = false;
3175 					n->initially_valid  = true;
3176 					$$ = (Node *)n;
3177 				}
3178 		;
3179 
3180 /*
3181  * ConstraintAttr represents constraint attributes, which we parse as if
3182  * they were independent constraint clauses, in order to avoid shift/reduce
3183  * conflicts (since NOT might start either an independent NOT NULL clause
3184  * or an attribute).  parse_utilcmd.c is responsible for attaching the
3185  * attribute information to the preceding "real" constraint node, and for
3186  * complaining if attribute clauses appear in the wrong place or wrong
3187  * combinations.
3188  *
3189  * See also ConstraintAttributeSpec, which can be used in places where
3190  * there is no parsing conflict.  (Note: currently, NOT VALID and NO INHERIT
3191  * are allowed clauses in ConstraintAttributeSpec, but not here.  Someday we
3192  * might need to allow them here too, but for the moment it doesn't seem
3193  * useful in the statements that use ConstraintAttr.)
3194  */
3195 ConstraintAttr:
3196 			DEFERRABLE
3197 				{
3198 					Constraint *n = makeNode(Constraint);
3199 					n->contype = CONSTR_ATTR_DEFERRABLE;
3200 					n->location = @1;
3201 					$$ = (Node *)n;
3202 				}
3203 			| NOT DEFERRABLE
3204 				{
3205 					Constraint *n = makeNode(Constraint);
3206 					n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
3207 					n->location = @1;
3208 					$$ = (Node *)n;
3209 				}
3210 			| INITIALLY DEFERRED
3211 				{
3212 					Constraint *n = makeNode(Constraint);
3213 					n->contype = CONSTR_ATTR_DEFERRED;
3214 					n->location = @1;
3215 					$$ = (Node *)n;
3216 				}
3217 			| INITIALLY IMMEDIATE
3218 				{
3219 					Constraint *n = makeNode(Constraint);
3220 					n->contype = CONSTR_ATTR_IMMEDIATE;
3221 					n->location = @1;
3222 					$$ = (Node *)n;
3223 				}
3224 		;
3225 
3226 
3227 TableLikeClause:
3228 			LIKE qualified_name TableLikeOptionList
3229 				{
3230 					TableLikeClause *n = makeNode(TableLikeClause);
3231 					n->relation = $2;
3232 					n->options = $3;
3233 					$$ = (Node *)n;
3234 				}
3235 		;
3236 
3237 TableLikeOptionList:
3238 				TableLikeOptionList INCLUDING TableLikeOption	{ $$ = $1 | $3; }
3239 				| TableLikeOptionList EXCLUDING TableLikeOption	{ $$ = $1 & ~$3; }
3240 				| /* EMPTY */						{ $$ = 0; }
3241 		;
3242 
3243 TableLikeOption:
3244 				DEFAULTS			{ $$ = CREATE_TABLE_LIKE_DEFAULTS; }
3245 				| CONSTRAINTS		{ $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
3246 				| INDEXES			{ $$ = CREATE_TABLE_LIKE_INDEXES; }
3247 				| STORAGE			{ $$ = CREATE_TABLE_LIKE_STORAGE; }
3248 				| COMMENTS			{ $$ = CREATE_TABLE_LIKE_COMMENTS; }
3249 				| ALL				{ $$ = CREATE_TABLE_LIKE_ALL; }
3250 		;
3251 
3252 
3253 /* ConstraintElem specifies constraint syntax which is not embedded into
3254  *	a column definition. ColConstraintElem specifies the embedded form.
3255  * - thomas 1997-12-03
3256  */
3257 TableConstraint:
3258 			CONSTRAINT name ConstraintElem
3259 				{
3260 					Constraint *n = (Constraint *) $3;
3261 					Assert(IsA(n, Constraint));
3262 					n->conname = $2;
3263 					n->location = @1;
3264 					$$ = (Node *) n;
3265 				}
3266 			| ConstraintElem						{ $$ = $1; }
3267 		;
3268 
3269 ConstraintElem:
3270 			CHECK '(' a_expr ')' ConstraintAttributeSpec
3271 				{
3272 					Constraint *n = makeNode(Constraint);
3273 					n->contype = CONSTR_CHECK;
3274 					n->location = @1;
3275 					n->raw_expr = $3;
3276 					n->cooked_expr = NULL;
3277 					processCASbits($5, @5, "CHECK",
3278 								   NULL, NULL, &n->skip_validation,
3279 								   &n->is_no_inherit, yyscanner);
3280 					n->initially_valid = !n->skip_validation;
3281 					$$ = (Node *)n;
3282 				}
3283 			| UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
3284 				ConstraintAttributeSpec
3285 				{
3286 					Constraint *n = makeNode(Constraint);
3287 					n->contype = CONSTR_UNIQUE;
3288 					n->location = @1;
3289 					n->keys = $3;
3290 					n->options = $5;
3291 					n->indexname = NULL;
3292 					n->indexspace = $6;
3293 					processCASbits($7, @7, "UNIQUE",
3294 								   &n->deferrable, &n->initdeferred, NULL,
3295 								   NULL, yyscanner);
3296 					$$ = (Node *)n;
3297 				}
3298 			| UNIQUE ExistingIndex ConstraintAttributeSpec
3299 				{
3300 					Constraint *n = makeNode(Constraint);
3301 					n->contype = CONSTR_UNIQUE;
3302 					n->location = @1;
3303 					n->keys = NIL;
3304 					n->options = NIL;
3305 					n->indexname = $2;
3306 					n->indexspace = NULL;
3307 					processCASbits($3, @3, "UNIQUE",
3308 								   &n->deferrable, &n->initdeferred, NULL,
3309 								   NULL, yyscanner);
3310 					$$ = (Node *)n;
3311 				}
3312 			| PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
3313 				ConstraintAttributeSpec
3314 				{
3315 					Constraint *n = makeNode(Constraint);
3316 					n->contype = CONSTR_PRIMARY;
3317 					n->location = @1;
3318 					n->keys = $4;
3319 					n->options = $6;
3320 					n->indexname = NULL;
3321 					n->indexspace = $7;
3322 					processCASbits($8, @8, "PRIMARY KEY",
3323 								   &n->deferrable, &n->initdeferred, NULL,
3324 								   NULL, yyscanner);
3325 					$$ = (Node *)n;
3326 				}
3327 			| PRIMARY KEY ExistingIndex ConstraintAttributeSpec
3328 				{
3329 					Constraint *n = makeNode(Constraint);
3330 					n->contype = CONSTR_PRIMARY;
3331 					n->location = @1;
3332 					n->keys = NIL;
3333 					n->options = NIL;
3334 					n->indexname = $3;
3335 					n->indexspace = NULL;
3336 					processCASbits($4, @4, "PRIMARY KEY",
3337 								   &n->deferrable, &n->initdeferred, NULL,
3338 								   NULL, yyscanner);
3339 					$$ = (Node *)n;
3340 				}
3341 			| EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
3342 				opt_definition OptConsTableSpace ExclusionWhereClause
3343 				ConstraintAttributeSpec
3344 				{
3345 					Constraint *n = makeNode(Constraint);
3346 					n->contype = CONSTR_EXCLUSION;
3347 					n->location = @1;
3348 					n->access_method	= $2;
3349 					n->exclusions		= $4;
3350 					n->options			= $6;
3351 					n->indexname		= NULL;
3352 					n->indexspace		= $7;
3353 					n->where_clause		= $8;
3354 					processCASbits($9, @9, "EXCLUDE",
3355 								   &n->deferrable, &n->initdeferred, NULL,
3356 								   NULL, yyscanner);
3357 					$$ = (Node *)n;
3358 				}
3359 			| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
3360 				opt_column_list key_match key_actions ConstraintAttributeSpec
3361 				{
3362 					Constraint *n = makeNode(Constraint);
3363 					n->contype = CONSTR_FOREIGN;
3364 					n->location = @1;
3365 					n->pktable			= $7;
3366 					n->fk_attrs			= $4;
3367 					n->pk_attrs			= $8;
3368 					n->fk_matchtype		= $9;
3369 					n->fk_upd_action	= (char) ($10 >> 8);
3370 					n->fk_del_action	= (char) ($10 & 0xFF);
3371 					processCASbits($11, @11, "FOREIGN KEY",
3372 								   &n->deferrable, &n->initdeferred,
3373 								   &n->skip_validation, NULL,
3374 								   yyscanner);
3375 					n->initially_valid = !n->skip_validation;
3376 					$$ = (Node *)n;
3377 				}
3378 		;
3379 
3380 opt_no_inherit:	NO INHERIT							{  $$ = TRUE; }
3381 			| /* EMPTY */							{  $$ = FALSE; }
3382 		;
3383 
3384 opt_column_list:
3385 			'(' columnList ')'						{ $$ = $2; }
3386 			| /*EMPTY*/								{ $$ = NIL; }
3387 		;
3388 
3389 columnList:
3390 			columnElem								{ $$ = list_make1($1); }
3391 			| columnList ',' columnElem				{ $$ = lappend($1, $3); }
3392 		;
3393 
3394 columnElem: ColId
3395 				{
3396 					$$ = (Node *) makeString($1);
3397 				}
3398 		;
3399 
3400 key_match:  MATCH FULL
3401 			{
3402 				$$ = FKCONSTR_MATCH_FULL;
3403 			}
3404 		| MATCH PARTIAL
3405 			{
3406 				ereport(ERROR,
3407 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3408 						 errmsg("MATCH PARTIAL not yet implemented"),
3409 						 parser_errposition(@1)));
3410 				$$ = FKCONSTR_MATCH_PARTIAL;
3411 			}
3412 		| MATCH SIMPLE
3413 			{
3414 				$$ = FKCONSTR_MATCH_SIMPLE;
3415 			}
3416 		| /*EMPTY*/
3417 			{
3418 				$$ = FKCONSTR_MATCH_SIMPLE;
3419 			}
3420 		;
3421 
3422 ExclusionConstraintList:
3423 			ExclusionConstraintElem					{ $$ = list_make1($1); }
3424 			| ExclusionConstraintList ',' ExclusionConstraintElem
3425 													{ $$ = lappend($1, $3); }
3426 		;
3427 
3428 ExclusionConstraintElem: index_elem WITH any_operator
3429 			{
3430 				$$ = list_make2($1, $3);
3431 			}
3432 			/* allow OPERATOR() decoration for the benefit of ruleutils.c */
3433 			| index_elem WITH OPERATOR '(' any_operator ')'
3434 			{
3435 				$$ = list_make2($1, $5);
3436 			}
3437 		;
3438 
3439 ExclusionWhereClause:
3440 			WHERE '(' a_expr ')'					{ $$ = $3; }
3441 			| /*EMPTY*/								{ $$ = NULL; }
3442 		;
3443 
3444 /*
3445  * We combine the update and delete actions into one value temporarily
3446  * for simplicity of parsing, and then break them down again in the
3447  * calling production.  update is in the left 8 bits, delete in the right.
3448  * Note that NOACTION is the default.
3449  */
3450 key_actions:
3451 			key_update
3452 				{ $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3453 			| key_delete
3454 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
3455 			| key_update key_delete
3456 				{ $$ = ($1 << 8) | ($2 & 0xFF); }
3457 			| key_delete key_update
3458 				{ $$ = ($2 << 8) | ($1 & 0xFF); }
3459 			| /*EMPTY*/
3460 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3461 		;
3462 
3463 key_update: ON UPDATE key_action		{ $$ = $3; }
3464 		;
3465 
3466 key_delete: ON DELETE_P key_action		{ $$ = $3; }
3467 		;
3468 
3469 key_action:
3470 			NO ACTION					{ $$ = FKCONSTR_ACTION_NOACTION; }
3471 			| RESTRICT					{ $$ = FKCONSTR_ACTION_RESTRICT; }
3472 			| CASCADE					{ $$ = FKCONSTR_ACTION_CASCADE; }
3473 			| SET NULL_P				{ $$ = FKCONSTR_ACTION_SETNULL; }
3474 			| SET DEFAULT				{ $$ = FKCONSTR_ACTION_SETDEFAULT; }
3475 		;
3476 
3477 OptInherit: INHERITS '(' qualified_name_list ')'	{ $$ = $3; }
3478 			| /*EMPTY*/								{ $$ = NIL; }
3479 		;
3480 
3481 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
3482 OptWith:
3483 			WITH reloptions				{ $$ = $2; }
3484 			| WITH OIDS					{ $$ = list_make1(defWithOids(true)); }
3485 			| WITHOUT OIDS				{ $$ = list_make1(defWithOids(false)); }
3486 			| /*EMPTY*/					{ $$ = NIL; }
3487 		;
3488 
3489 OnCommitOption:  ON COMMIT DROP				{ $$ = ONCOMMIT_DROP; }
3490 			| ON COMMIT DELETE_P ROWS		{ $$ = ONCOMMIT_DELETE_ROWS; }
3491 			| ON COMMIT PRESERVE ROWS		{ $$ = ONCOMMIT_PRESERVE_ROWS; }
3492 			| /*EMPTY*/						{ $$ = ONCOMMIT_NOOP; }
3493 		;
3494 
3495 OptTableSpace:   TABLESPACE name					{ $$ = $2; }
3496 			| /*EMPTY*/								{ $$ = NULL; }
3497 		;
3498 
3499 OptConsTableSpace:   USING INDEX TABLESPACE name	{ $$ = $4; }
3500 			| /*EMPTY*/								{ $$ = NULL; }
3501 		;
3502 
3503 ExistingIndex:   USING INDEX index_name				{ $$ = $3; }
3504 		;
3505 
3506 
3507 /*****************************************************************************
3508  *
3509  *		QUERY :
3510  *				CREATE TABLE relname AS SelectStmt [ WITH [NO] DATA ]
3511  *
3512  *
3513  * Note: SELECT ... INTO is a now-deprecated alternative for this.
3514  *
3515  *****************************************************************************/
3516 
3517 CreateAsStmt:
3518 		CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
3519 				{
3520 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3521 					ctas->query = $6;
3522 					ctas->into = $4;
3523 					ctas->relkind = OBJECT_TABLE;
3524 					ctas->is_select_into = false;
3525 					ctas->if_not_exists = false;
3526 					/* cram additional flags into the IntoClause */
3527 					$4->rel->relpersistence = $2;
3528 					$4->skipData = !($7);
3529 					$$ = (Node *) ctas;
3530 				}
3531 		| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS SelectStmt opt_with_data
3532 				{
3533 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3534 					ctas->query = $9;
3535 					ctas->into = $7;
3536 					ctas->relkind = OBJECT_TABLE;
3537 					ctas->is_select_into = false;
3538 					ctas->if_not_exists = true;
3539 					/* cram additional flags into the IntoClause */
3540 					$7->rel->relpersistence = $2;
3541 					$7->skipData = !($10);
3542 					$$ = (Node *) ctas;
3543 				}
3544 		;
3545 
3546 create_as_target:
3547 			qualified_name opt_column_list OptWith OnCommitOption OptTableSpace
3548 				{
3549 					$$ = makeNode(IntoClause);
3550 					$$->rel = $1;
3551 					$$->colNames = $2;
3552 					$$->options = $3;
3553 					$$->onCommit = $4;
3554 					$$->tableSpaceName = $5;
3555 					$$->viewQuery = NULL;
3556 					$$->skipData = false;		/* might get changed later */
3557 				}
3558 		;
3559 
3560 opt_with_data:
3561 			WITH DATA_P								{ $$ = TRUE; }
3562 			| WITH NO DATA_P						{ $$ = FALSE; }
3563 			| /*EMPTY*/								{ $$ = TRUE; }
3564 		;
3565 
3566 
3567 /*****************************************************************************
3568  *
3569  *		QUERY :
3570  *				CREATE MATERIALIZED VIEW relname AS SelectStmt
3571  *
3572  *****************************************************************************/
3573 
3574 CreateMatViewStmt:
3575 		CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data
3576 				{
3577 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3578 					ctas->query = $7;
3579 					ctas->into = $5;
3580 					ctas->relkind = OBJECT_MATVIEW;
3581 					ctas->is_select_into = false;
3582 					ctas->if_not_exists = false;
3583 					/* cram additional flags into the IntoClause */
3584 					$5->rel->relpersistence = $2;
3585 					$5->skipData = !($8);
3586 					$$ = (Node *) ctas;
3587 				}
3588 		| CREATE OptNoLog MATERIALIZED VIEW IF_P NOT EXISTS create_mv_target AS SelectStmt opt_with_data
3589 				{
3590 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3591 					ctas->query = $10;
3592 					ctas->into = $8;
3593 					ctas->relkind = OBJECT_MATVIEW;
3594 					ctas->is_select_into = false;
3595 					ctas->if_not_exists = true;
3596 					/* cram additional flags into the IntoClause */
3597 					$8->rel->relpersistence = $2;
3598 					$8->skipData = !($11);
3599 					$$ = (Node *) ctas;
3600 				}
3601 		;
3602 
3603 create_mv_target:
3604 			qualified_name opt_column_list opt_reloptions OptTableSpace
3605 				{
3606 					$$ = makeNode(IntoClause);
3607 					$$->rel = $1;
3608 					$$->colNames = $2;
3609 					$$->options = $3;
3610 					$$->onCommit = ONCOMMIT_NOOP;
3611 					$$->tableSpaceName = $4;
3612 					$$->viewQuery = NULL;		/* filled at analysis time */
3613 					$$->skipData = false;		/* might get changed later */
3614 				}
3615 		;
3616 
3617 OptNoLog:	UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
3618 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
3619 		;
3620 
3621 
3622 /*****************************************************************************
3623  *
3624  *		QUERY :
3625  *				REFRESH MATERIALIZED VIEW qualified_name
3626  *
3627  *****************************************************************************/
3628 
3629 RefreshMatViewStmt:
3630 			REFRESH MATERIALIZED VIEW opt_concurrently qualified_name opt_with_data
3631 				{
3632 					RefreshMatViewStmt *n = makeNode(RefreshMatViewStmt);
3633 					n->concurrent = $4;
3634 					n->relation = $5;
3635 					n->skipData = !($6);
3636 					$$ = (Node *) n;
3637 				}
3638 		;
3639 
3640 
3641 /*****************************************************************************
3642  *
3643  *		QUERY :
3644  *				CREATE SEQUENCE seqname
3645  *				ALTER SEQUENCE seqname
3646  *
3647  *****************************************************************************/
3648 
3649 CreateSeqStmt:
3650 			CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
3651 				{
3652 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
3653 					$4->relpersistence = $2;
3654 					n->sequence = $4;
3655 					n->options = $5;
3656 					n->ownerId = InvalidOid;
3657 					n->if_not_exists = false;
3658 					$$ = (Node *)n;
3659 				}
3660 			| CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
3661 				{
3662 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
3663 					$7->relpersistence = $2;
3664 					n->sequence = $7;
3665 					n->options = $8;
3666 					n->ownerId = InvalidOid;
3667 					n->if_not_exists = true;
3668 					$$ = (Node *)n;
3669 				}
3670 		;
3671 
3672 AlterSeqStmt:
3673 			ALTER SEQUENCE qualified_name SeqOptList
3674 				{
3675 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
3676 					n->sequence = $3;
3677 					n->options = $4;
3678 					n->missing_ok = false;
3679 					$$ = (Node *)n;
3680 				}
3681 			| ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
3682 				{
3683 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
3684 					n->sequence = $5;
3685 					n->options = $6;
3686 					n->missing_ok = true;
3687 					$$ = (Node *)n;
3688 				}
3689 
3690 		;
3691 
3692 OptSeqOptList: SeqOptList							{ $$ = $1; }
3693 			| /*EMPTY*/								{ $$ = NIL; }
3694 		;
3695 
3696 SeqOptList: SeqOptElem								{ $$ = list_make1($1); }
3697 			| SeqOptList SeqOptElem					{ $$ = lappend($1, $2); }
3698 		;
3699 
3700 SeqOptElem: CACHE NumericOnly
3701 				{
3702 					$$ = makeDefElem("cache", (Node *)$2);
3703 				}
3704 			| CYCLE
3705 				{
3706 					$$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
3707 				}
3708 			| NO CYCLE
3709 				{
3710 					$$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
3711 				}
3712 			| INCREMENT opt_by NumericOnly
3713 				{
3714 					$$ = makeDefElem("increment", (Node *)$3);
3715 				}
3716 			| MAXVALUE NumericOnly
3717 				{
3718 					$$ = makeDefElem("maxvalue", (Node *)$2);
3719 				}
3720 			| MINVALUE NumericOnly
3721 				{
3722 					$$ = makeDefElem("minvalue", (Node *)$2);
3723 				}
3724 			| NO MAXVALUE
3725 				{
3726 					$$ = makeDefElem("maxvalue", NULL);
3727 				}
3728 			| NO MINVALUE
3729 				{
3730 					$$ = makeDefElem("minvalue", NULL);
3731 				}
3732 			| OWNED BY any_name
3733 				{
3734 					$$ = makeDefElem("owned_by", (Node *)$3);
3735 				}
3736 			| START opt_with NumericOnly
3737 				{
3738 					$$ = makeDefElem("start", (Node *)$3);
3739 				}
3740 			| RESTART
3741 				{
3742 					$$ = makeDefElem("restart", NULL);
3743 				}
3744 			| RESTART opt_with NumericOnly
3745 				{
3746 					$$ = makeDefElem("restart", (Node *)$3);
3747 				}
3748 		;
3749 
3750 opt_by:		BY				{}
3751 			| /* empty */	{}
3752 	  ;
3753 
3754 NumericOnly:
3755 			FCONST								{ $$ = makeFloat($1); }
3756 			| '-' FCONST
3757 				{
3758 					$$ = makeFloat($2);
3759 					doNegateFloat($$);
3760 				}
3761 			| SignedIconst						{ $$ = makeInteger($1); }
3762 		;
3763 
3764 NumericOnly_list:	NumericOnly						{ $$ = list_make1($1); }
3765 				| NumericOnly_list ',' NumericOnly	{ $$ = lappend($1, $3); }
3766 		;
3767 
3768 /*****************************************************************************
3769  *
3770  *		QUERIES :
3771  *				CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
3772  *				DROP [PROCEDURAL] LANGUAGE ...
3773  *
3774  *****************************************************************************/
3775 
3776 CreatePLangStmt:
3777 			CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
3778 			{
3779 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
3780 				n->replace = $2;
3781 				n->plname = $6;
3782 				/* parameters are all to be supplied by system */
3783 				n->plhandler = NIL;
3784 				n->plinline = NIL;
3785 				n->plvalidator = NIL;
3786 				n->pltrusted = false;
3787 				$$ = (Node *)n;
3788 			}
3789 			| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
3790 			  HANDLER handler_name opt_inline_handler opt_validator
3791 			{
3792 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
3793 				n->replace = $2;
3794 				n->plname = $6;
3795 				n->plhandler = $8;
3796 				n->plinline = $9;
3797 				n->plvalidator = $10;
3798 				n->pltrusted = $3;
3799 				$$ = (Node *)n;
3800 			}
3801 		;
3802 
3803 opt_trusted:
3804 			TRUSTED									{ $$ = TRUE; }
3805 			| /*EMPTY*/								{ $$ = FALSE; }
3806 		;
3807 
3808 /* This ought to be just func_name, but that causes reduce/reduce conflicts
3809  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
3810  * Work around by using simple names, instead.
3811  */
3812 handler_name:
3813 			name						{ $$ = list_make1(makeString($1)); }
3814 			| name attrs				{ $$ = lcons(makeString($1), $2); }
3815 		;
3816 
3817 opt_inline_handler:
3818 			INLINE_P handler_name					{ $$ = $2; }
3819 			| /*EMPTY*/								{ $$ = NIL; }
3820 		;
3821 
3822 validator_clause:
3823 			VALIDATOR handler_name					{ $$ = $2; }
3824 			| NO VALIDATOR							{ $$ = NIL; }
3825 		;
3826 
3827 opt_validator:
3828 			validator_clause						{ $$ = $1; }
3829 			| /*EMPTY*/								{ $$ = NIL; }
3830 		;
3831 
3832 DropPLangStmt:
3833 			DROP opt_procedural LANGUAGE NonReservedWord_or_Sconst opt_drop_behavior
3834 				{
3835 					DropStmt *n = makeNode(DropStmt);
3836 					n->removeType = OBJECT_LANGUAGE;
3837 					n->objects = list_make1(list_make1(makeString($4)));
3838 					n->arguments = NIL;
3839 					n->behavior = $5;
3840 					n->missing_ok = false;
3841 					n->concurrent = false;
3842 					$$ = (Node *)n;
3843 				}
3844 			| DROP opt_procedural LANGUAGE IF_P EXISTS NonReservedWord_or_Sconst opt_drop_behavior
3845 				{
3846 					DropStmt *n = makeNode(DropStmt);
3847 					n->removeType = OBJECT_LANGUAGE;
3848 					n->objects = list_make1(list_make1(makeString($6)));
3849 					n->behavior = $7;
3850 					n->missing_ok = true;
3851 					n->concurrent = false;
3852 					$$ = (Node *)n;
3853 				}
3854 		;
3855 
3856 opt_procedural:
3857 			PROCEDURAL								{}
3858 			| /*EMPTY*/								{}
3859 		;
3860 
3861 /*****************************************************************************
3862  *
3863  *		QUERY:
3864  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
3865  *
3866  *****************************************************************************/
3867 
3868 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst opt_reloptions
3869 				{
3870 					CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
3871 					n->tablespacename = $3;
3872 					n->owner = $4;
3873 					n->location = $6;
3874 					n->options = $7;
3875 					$$ = (Node *) n;
3876 				}
3877 		;
3878 
3879 OptTableSpaceOwner: OWNER RoleSpec		{ $$ = $2; }
3880 			| /*EMPTY */				{ $$ = NULL; }
3881 		;
3882 
3883 /*****************************************************************************
3884  *
3885  *		QUERY :
3886  *				DROP TABLESPACE <tablespace>
3887  *
3888  *		No need for drop behaviour as we cannot implement dependencies for
3889  *		objects in other databases; we can only support RESTRICT.
3890  *
3891  ****************************************************************************/
3892 
3893 DropTableSpaceStmt: DROP TABLESPACE name
3894 				{
3895 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
3896 					n->tablespacename = $3;
3897 					n->missing_ok = false;
3898 					$$ = (Node *) n;
3899 				}
3900 				|  DROP TABLESPACE IF_P EXISTS name
3901 				{
3902 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
3903 					n->tablespacename = $5;
3904 					n->missing_ok = true;
3905 					$$ = (Node *) n;
3906 				}
3907 		;
3908 
3909 /*****************************************************************************
3910  *
3911  *		QUERY:
3912  *             CREATE EXTENSION extension
3913  *             [ WITH ] [ SCHEMA schema ] [ VERSION version ] [ FROM oldversion ]
3914  *
3915  *****************************************************************************/
3916 
3917 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
3918 				{
3919 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
3920 					n->extname = $3;
3921 					n->if_not_exists = false;
3922 					n->options = $5;
3923 					$$ = (Node *) n;
3924 				}
3925 				| CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
3926 				{
3927 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
3928 					n->extname = $6;
3929 					n->if_not_exists = true;
3930 					n->options = $8;
3931 					$$ = (Node *) n;
3932 				}
3933 		;
3934 
3935 create_extension_opt_list:
3936 			create_extension_opt_list create_extension_opt_item
3937 				{ $$ = lappend($1, $2); }
3938 			| /* EMPTY */
3939 				{ $$ = NIL; }
3940 		;
3941 
3942 create_extension_opt_item:
3943 			SCHEMA name
3944 				{
3945 					$$ = makeDefElem("schema", (Node *)makeString($2));
3946 				}
3947 			| VERSION_P NonReservedWord_or_Sconst
3948 				{
3949 					$$ = makeDefElem("new_version", (Node *)makeString($2));
3950 				}
3951 			| FROM NonReservedWord_or_Sconst
3952 				{
3953 					$$ = makeDefElem("old_version", (Node *)makeString($2));
3954 				}
3955 			| CASCADE
3956 				{
3957 					$$ = makeDefElem("cascade", (Node *)makeInteger(TRUE));
3958 				}
3959 		;
3960 
3961 /*****************************************************************************
3962  *
3963  * ALTER EXTENSION name UPDATE [ TO version ]
3964  *
3965  *****************************************************************************/
3966 
3967 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
3968 				{
3969 					AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
3970 					n->extname = $3;
3971 					n->options = $5;
3972 					$$ = (Node *) n;
3973 				}
3974 		;
3975 
3976 alter_extension_opt_list:
3977 			alter_extension_opt_list alter_extension_opt_item
3978 				{ $$ = lappend($1, $2); }
3979 			| /* EMPTY */
3980 				{ $$ = NIL; }
3981 		;
3982 
3983 alter_extension_opt_item:
3984 			TO NonReservedWord_or_Sconst
3985 				{
3986 					$$ = makeDefElem("new_version", (Node *)makeString($2));
3987 				}
3988 		;
3989 
3990 /*****************************************************************************
3991  *
3992  * ALTER EXTENSION name ADD/DROP object-identifier
3993  *
3994  *****************************************************************************/
3995 
3996 AlterExtensionContentsStmt:
3997 			ALTER EXTENSION name add_drop AGGREGATE func_name aggr_args
3998 				{
3999 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4000 					n->extname = $3;
4001 					n->action = $4;
4002 					n->objtype = OBJECT_AGGREGATE;
4003 					n->objname = $6;
4004 					n->objargs = extractAggrArgTypes($7);
4005 					$$ = (Node *)n;
4006 				}
4007 			| ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
4008 				{
4009 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4010 					n->extname = $3;
4011 					n->action = $4;
4012 					n->objtype = OBJECT_CAST;
4013 					n->objname = list_make1($7);
4014 					n->objargs = list_make1($9);
4015 					$$ = (Node *) n;
4016 				}
4017 			| ALTER EXTENSION name add_drop COLLATION any_name
4018 				{
4019 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4020 					n->extname = $3;
4021 					n->action = $4;
4022 					n->objtype = OBJECT_COLLATION;
4023 					n->objname = $6;
4024 					$$ = (Node *)n;
4025 				}
4026 			| ALTER EXTENSION name add_drop CONVERSION_P any_name
4027 				{
4028 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4029 					n->extname = $3;
4030 					n->action = $4;
4031 					n->objtype = OBJECT_CONVERSION;
4032 					n->objname = $6;
4033 					$$ = (Node *)n;
4034 				}
4035 			| ALTER EXTENSION name add_drop DOMAIN_P Typename
4036 				{
4037 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4038 					n->extname = $3;
4039 					n->action = $4;
4040 					n->objtype = OBJECT_DOMAIN;
4041 					n->objname = list_make1($6);
4042 					$$ = (Node *)n;
4043 				}
4044 			| ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
4045 				{
4046 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4047 					n->extname = $3;
4048 					n->action = $4;
4049 					n->objtype = OBJECT_FUNCTION;
4050 					n->objname = $6->funcname;
4051 					n->objargs = $6->funcargs;
4052 					$$ = (Node *)n;
4053 				}
4054 			| ALTER EXTENSION name add_drop opt_procedural LANGUAGE name
4055 				{
4056 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4057 					n->extname = $3;
4058 					n->action = $4;
4059 					n->objtype = OBJECT_LANGUAGE;
4060 					n->objname = list_make1(makeString($7));
4061 					$$ = (Node *)n;
4062 				}
4063 			| ALTER EXTENSION name add_drop OPERATOR any_operator oper_argtypes
4064 				{
4065 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4066 					n->extname = $3;
4067 					n->action = $4;
4068 					n->objtype = OBJECT_OPERATOR;
4069 					n->objname = $6;
4070 					n->objargs = $7;
4071 					$$ = (Node *)n;
4072 				}
4073 			| ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING access_method
4074 				{
4075 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4076 					n->extname = $3;
4077 					n->action = $4;
4078 					n->objtype = OBJECT_OPCLASS;
4079 					n->objname = lcons(makeString($9), $7);
4080 					$$ = (Node *)n;
4081 				}
4082 			| ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING access_method
4083 				{
4084 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4085 					n->extname = $3;
4086 					n->action = $4;
4087 					n->objtype = OBJECT_OPFAMILY;
4088 					n->objname = lcons(makeString($9), $7);
4089 					$$ = (Node *)n;
4090 				}
4091 			| ALTER EXTENSION name add_drop SCHEMA name
4092 				{
4093 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4094 					n->extname = $3;
4095 					n->action = $4;
4096 					n->objtype = OBJECT_SCHEMA;
4097 					n->objname = list_make1(makeString($6));
4098 					$$ = (Node *)n;
4099 				}
4100 			| ALTER EXTENSION name add_drop EVENT TRIGGER name
4101 				{
4102 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4103 					n->extname = $3;
4104 					n->action = $4;
4105 					n->objtype = OBJECT_EVENT_TRIGGER;
4106 					n->objname = list_make1(makeString($7));
4107 					$$ = (Node *)n;
4108 				}
4109 			| ALTER EXTENSION name add_drop TABLE any_name
4110 				{
4111 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4112 					n->extname = $3;
4113 					n->action = $4;
4114 					n->objtype = OBJECT_TABLE;
4115 					n->objname = $6;
4116 					$$ = (Node *)n;
4117 				}
4118 			| ALTER EXTENSION name add_drop TEXT_P SEARCH PARSER any_name
4119 				{
4120 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4121 					n->extname = $3;
4122 					n->action = $4;
4123 					n->objtype = OBJECT_TSPARSER;
4124 					n->objname = $8;
4125 					$$ = (Node *)n;
4126 				}
4127 			| ALTER EXTENSION name add_drop TEXT_P SEARCH DICTIONARY any_name
4128 				{
4129 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4130 					n->extname = $3;
4131 					n->action = $4;
4132 					n->objtype = OBJECT_TSDICTIONARY;
4133 					n->objname = $8;
4134 					$$ = (Node *)n;
4135 				}
4136 			| ALTER EXTENSION name add_drop TEXT_P SEARCH TEMPLATE any_name
4137 				{
4138 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4139 					n->extname = $3;
4140 					n->action = $4;
4141 					n->objtype = OBJECT_TSTEMPLATE;
4142 					n->objname = $8;
4143 					$$ = (Node *)n;
4144 				}
4145 			| ALTER EXTENSION name add_drop TEXT_P SEARCH CONFIGURATION any_name
4146 				{
4147 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4148 					n->extname = $3;
4149 					n->action = $4;
4150 					n->objtype = OBJECT_TSCONFIGURATION;
4151 					n->objname = $8;
4152 					$$ = (Node *)n;
4153 				}
4154 			| ALTER EXTENSION name add_drop SEQUENCE any_name
4155 				{
4156 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4157 					n->extname = $3;
4158 					n->action = $4;
4159 					n->objtype = OBJECT_SEQUENCE;
4160 					n->objname = $6;
4161 					$$ = (Node *)n;
4162 				}
4163 			| ALTER EXTENSION name add_drop VIEW any_name
4164 				{
4165 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4166 					n->extname = $3;
4167 					n->action = $4;
4168 					n->objtype = OBJECT_VIEW;
4169 					n->objname = $6;
4170 					$$ = (Node *)n;
4171 				}
4172 			| ALTER EXTENSION name add_drop MATERIALIZED VIEW any_name
4173 				{
4174 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4175 					n->extname = $3;
4176 					n->action = $4;
4177 					n->objtype = OBJECT_MATVIEW;
4178 					n->objname = $7;
4179 					$$ = (Node *)n;
4180 				}
4181 			| ALTER EXTENSION name add_drop FOREIGN TABLE any_name
4182 				{
4183 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4184 					n->extname = $3;
4185 					n->action = $4;
4186 					n->objtype = OBJECT_FOREIGN_TABLE;
4187 					n->objname = $7;
4188 					$$ = (Node *)n;
4189 				}
4190 			| ALTER EXTENSION name add_drop FOREIGN DATA_P WRAPPER name
4191 				{
4192 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4193 					n->extname = $3;
4194 					n->action = $4;
4195 					n->objtype = OBJECT_FDW;
4196 					n->objname = list_make1(makeString($8));
4197 					$$ = (Node *)n;
4198 				}
4199 			| ALTER EXTENSION name add_drop SERVER name
4200 				{
4201 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4202 					n->extname = $3;
4203 					n->action = $4;
4204 					n->objtype = OBJECT_FOREIGN_SERVER;
4205 					n->objname = list_make1(makeString($6));
4206 					$$ = (Node *)n;
4207 				}
4208 			| ALTER EXTENSION name add_drop TRANSFORM FOR Typename LANGUAGE name
4209 				{
4210 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4211 					n->extname = $3;
4212 					n->action = $4;
4213 					n->objtype = OBJECT_TRANSFORM;
4214 					n->objname = list_make1($7);
4215 					n->objargs = list_make1(makeString($9));
4216 					$$ = (Node *)n;
4217 				}
4218 			| ALTER EXTENSION name add_drop TYPE_P Typename
4219 				{
4220 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4221 					n->extname = $3;
4222 					n->action = $4;
4223 					n->objtype = OBJECT_TYPE;
4224 					n->objname = list_make1($6);
4225 					$$ = (Node *)n;
4226 				}
4227 		;
4228 
4229 /*****************************************************************************
4230  *
4231  *		QUERY:
4232  *             CREATE FOREIGN DATA WRAPPER name options
4233  *
4234  *****************************************************************************/
4235 
4236 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
4237 				{
4238 					CreateFdwStmt *n = makeNode(CreateFdwStmt);
4239 					n->fdwname = $5;
4240 					n->func_options = $6;
4241 					n->options = $7;
4242 					$$ = (Node *) n;
4243 				}
4244 		;
4245 
4246 fdw_option:
4247 			HANDLER handler_name				{ $$ = makeDefElem("handler", (Node *)$2); }
4248 			| NO HANDLER						{ $$ = makeDefElem("handler", NULL); }
4249 			| VALIDATOR handler_name			{ $$ = makeDefElem("validator", (Node *)$2); }
4250 			| NO VALIDATOR						{ $$ = makeDefElem("validator", NULL); }
4251 		;
4252 
4253 fdw_options:
4254 			fdw_option							{ $$ = list_make1($1); }
4255 			| fdw_options fdw_option			{ $$ = lappend($1, $2); }
4256 		;
4257 
4258 opt_fdw_options:
4259 			fdw_options							{ $$ = $1; }
4260 			| /*EMPTY*/							{ $$ = NIL; }
4261 		;
4262 
4263 /*****************************************************************************
4264  *
4265  *		QUERY :
4266  *				DROP FOREIGN DATA WRAPPER name
4267  *
4268  ****************************************************************************/
4269 
4270 DropFdwStmt: DROP FOREIGN DATA_P WRAPPER name opt_drop_behavior
4271 				{
4272 					DropStmt *n = makeNode(DropStmt);
4273 					n->removeType = OBJECT_FDW;
4274 					n->objects = list_make1(list_make1(makeString($5)));
4275 					n->arguments = NIL;
4276 					n->missing_ok = false;
4277 					n->behavior = $6;
4278 					n->concurrent = false;
4279 					$$ = (Node *) n;
4280 				}
4281 				|  DROP FOREIGN DATA_P WRAPPER IF_P EXISTS name opt_drop_behavior
4282 				{
4283 					DropStmt *n = makeNode(DropStmt);
4284 					n->removeType = OBJECT_FDW;
4285 					n->objects = list_make1(list_make1(makeString($7)));
4286 					n->arguments = NIL;
4287 					n->missing_ok = true;
4288 					n->behavior = $8;
4289 					n->concurrent = false;
4290 					$$ = (Node *) n;
4291 				}
4292 		;
4293 
4294 /*****************************************************************************
4295  *
4296  *		QUERY :
4297  *				ALTER FOREIGN DATA WRAPPER name options
4298  *
4299  ****************************************************************************/
4300 
4301 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
4302 				{
4303 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4304 					n->fdwname = $5;
4305 					n->func_options = $6;
4306 					n->options = $7;
4307 					$$ = (Node *) n;
4308 				}
4309 			| ALTER FOREIGN DATA_P WRAPPER name fdw_options
4310 				{
4311 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4312 					n->fdwname = $5;
4313 					n->func_options = $6;
4314 					n->options = NIL;
4315 					$$ = (Node *) n;
4316 				}
4317 		;
4318 
4319 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
4320 create_generic_options:
4321 			OPTIONS '(' generic_option_list ')'			{ $$ = $3; }
4322 			| /*EMPTY*/									{ $$ = NIL; }
4323 		;
4324 
4325 generic_option_list:
4326 			generic_option_elem
4327 				{
4328 					$$ = list_make1($1);
4329 				}
4330 			| generic_option_list ',' generic_option_elem
4331 				{
4332 					$$ = lappend($1, $3);
4333 				}
4334 		;
4335 
4336 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
4337 alter_generic_options:
4338 			OPTIONS	'(' alter_generic_option_list ')'		{ $$ = $3; }
4339 		;
4340 
4341 alter_generic_option_list:
4342 			alter_generic_option_elem
4343 				{
4344 					$$ = list_make1($1);
4345 				}
4346 			| alter_generic_option_list ',' alter_generic_option_elem
4347 				{
4348 					$$ = lappend($1, $3);
4349 				}
4350 		;
4351 
4352 alter_generic_option_elem:
4353 			generic_option_elem
4354 				{
4355 					$$ = $1;
4356 				}
4357 			| SET generic_option_elem
4358 				{
4359 					$$ = $2;
4360 					$$->defaction = DEFELEM_SET;
4361 				}
4362 			| ADD_P generic_option_elem
4363 				{
4364 					$$ = $2;
4365 					$$->defaction = DEFELEM_ADD;
4366 				}
4367 			| DROP generic_option_name
4368 				{
4369 					$$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP);
4370 				}
4371 		;
4372 
4373 generic_option_elem:
4374 			generic_option_name generic_option_arg
4375 				{
4376 					$$ = makeDefElem($1, $2);
4377 				}
4378 		;
4379 
4380 generic_option_name:
4381 				ColLabel			{ $$ = $1; }
4382 		;
4383 
4384 /* We could use def_arg here, but the spec only requires string literals */
4385 generic_option_arg:
4386 				Sconst				{ $$ = (Node *) makeString($1); }
4387 		;
4388 
4389 /*****************************************************************************
4390  *
4391  *		QUERY:
4392  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
4393  *
4394  *****************************************************************************/
4395 
4396 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
4397 						 FOREIGN DATA_P WRAPPER name create_generic_options
4398 				{
4399 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4400 					n->servername = $3;
4401 					n->servertype = $4;
4402 					n->version = $5;
4403 					n->fdwname = $9;
4404 					n->options = $10;
4405 					$$ = (Node *) n;
4406 				}
4407 		;
4408 
4409 opt_type:
4410 			TYPE_P Sconst			{ $$ = $2; }
4411 			| /*EMPTY*/				{ $$ = NULL; }
4412 		;
4413 
4414 
4415 foreign_server_version:
4416 			VERSION_P Sconst		{ $$ = $2; }
4417 		|	VERSION_P NULL_P		{ $$ = NULL; }
4418 		;
4419 
4420 opt_foreign_server_version:
4421 			foreign_server_version	{ $$ = $1; }
4422 			| /*EMPTY*/				{ $$ = NULL; }
4423 		;
4424 
4425 /*****************************************************************************
4426  *
4427  *		QUERY :
4428  *				DROP SERVER name
4429  *
4430  ****************************************************************************/
4431 
4432 DropForeignServerStmt: DROP SERVER name opt_drop_behavior
4433 				{
4434 					DropStmt *n = makeNode(DropStmt);
4435 					n->removeType = OBJECT_FOREIGN_SERVER;
4436 					n->objects = list_make1(list_make1(makeString($3)));
4437 					n->arguments = NIL;
4438 					n->missing_ok = false;
4439 					n->behavior = $4;
4440 					n->concurrent = false;
4441 					$$ = (Node *) n;
4442 				}
4443 				|  DROP SERVER IF_P EXISTS name opt_drop_behavior
4444 				{
4445 					DropStmt *n = makeNode(DropStmt);
4446 					n->removeType = OBJECT_FOREIGN_SERVER;
4447 					n->objects = list_make1(list_make1(makeString($5)));
4448 					n->arguments = NIL;
4449 					n->missing_ok = true;
4450 					n->behavior = $6;
4451 					n->concurrent = false;
4452 					$$ = (Node *) n;
4453 				}
4454 		;
4455 
4456 /*****************************************************************************
4457  *
4458  *		QUERY :
4459  *				ALTER SERVER name [VERSION] [OPTIONS]
4460  *
4461  ****************************************************************************/
4462 
4463 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
4464 				{
4465 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4466 					n->servername = $3;
4467 					n->version = $4;
4468 					n->options = $5;
4469 					n->has_version = true;
4470 					$$ = (Node *) n;
4471 				}
4472 			| ALTER SERVER name foreign_server_version
4473 				{
4474 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4475 					n->servername = $3;
4476 					n->version = $4;
4477 					n->has_version = true;
4478 					$$ = (Node *) n;
4479 				}
4480 			| ALTER SERVER name alter_generic_options
4481 				{
4482 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4483 					n->servername = $3;
4484 					n->options = $4;
4485 					$$ = (Node *) n;
4486 				}
4487 		;
4488 
4489 /*****************************************************************************
4490  *
4491  *		QUERY:
4492  *             CREATE FOREIGN TABLE relname (...) SERVER name (...)
4493  *
4494  *****************************************************************************/
4495 
4496 CreateForeignTableStmt:
4497 		CREATE FOREIGN TABLE qualified_name
4498 			'(' OptTableElementList ')'
4499 			OptInherit SERVER name create_generic_options
4500 				{
4501 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4502 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
4503 					n->base.relation = $4;
4504 					n->base.tableElts = $6;
4505 					n->base.inhRelations = $8;
4506 					n->base.ofTypename = NULL;
4507 					n->base.constraints = NIL;
4508 					n->base.options = NIL;
4509 					n->base.oncommit = ONCOMMIT_NOOP;
4510 					n->base.tablespacename = NULL;
4511 					n->base.if_not_exists = false;
4512 					/* FDW-specific data */
4513 					n->servername = $10;
4514 					n->options = $11;
4515 					$$ = (Node *) n;
4516 				}
4517 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
4518 			'(' OptTableElementList ')'
4519 			OptInherit SERVER name create_generic_options
4520 				{
4521 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4522 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
4523 					n->base.relation = $7;
4524 					n->base.tableElts = $9;
4525 					n->base.inhRelations = $11;
4526 					n->base.ofTypename = NULL;
4527 					n->base.constraints = NIL;
4528 					n->base.options = NIL;
4529 					n->base.oncommit = ONCOMMIT_NOOP;
4530 					n->base.tablespacename = NULL;
4531 					n->base.if_not_exists = true;
4532 					/* FDW-specific data */
4533 					n->servername = $13;
4534 					n->options = $14;
4535 					$$ = (Node *) n;
4536 				}
4537 		;
4538 
4539 /*****************************************************************************
4540  *
4541  *		QUERY:
4542  *             ALTER FOREIGN TABLE relname [...]
4543  *
4544  *****************************************************************************/
4545 
4546 AlterForeignTableStmt:
4547 			ALTER FOREIGN TABLE relation_expr alter_table_cmds
4548 				{
4549 					AlterTableStmt *n = makeNode(AlterTableStmt);
4550 					n->relation = $4;
4551 					n->cmds = $5;
4552 					n->relkind = OBJECT_FOREIGN_TABLE;
4553 					n->missing_ok = false;
4554 					$$ = (Node *)n;
4555 				}
4556 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
4557 				{
4558 					AlterTableStmt *n = makeNode(AlterTableStmt);
4559 					n->relation = $6;
4560 					n->cmds = $7;
4561 					n->relkind = OBJECT_FOREIGN_TABLE;
4562 					n->missing_ok = true;
4563 					$$ = (Node *)n;
4564 				}
4565 		;
4566 
4567 /*****************************************************************************
4568  *
4569  *		QUERY:
4570  *				IMPORT FOREIGN SCHEMA remote_schema
4571  *				[ { LIMIT TO | EXCEPT } ( table_list ) ]
4572  *				FROM SERVER server_name INTO local_schema [ OPTIONS (...) ]
4573  *
4574  ****************************************************************************/
4575 
4576 ImportForeignSchemaStmt:
4577 		IMPORT_P FOREIGN SCHEMA name import_qualification
4578 		  FROM SERVER name INTO name create_generic_options
4579 			{
4580 				ImportForeignSchemaStmt *n = makeNode(ImportForeignSchemaStmt);
4581 				n->server_name = $8;
4582 				n->remote_schema = $4;
4583 				n->local_schema = $10;
4584 				n->list_type = $5->type;
4585 				n->table_list = $5->table_names;
4586 				n->options = $11;
4587 				$$ = (Node *) n;
4588 			}
4589 		;
4590 
4591 import_qualification_type:
4592 		LIMIT TO 				{ $$ = FDW_IMPORT_SCHEMA_LIMIT_TO; }
4593 		| EXCEPT 				{ $$ = FDW_IMPORT_SCHEMA_EXCEPT; }
4594 		;
4595 
4596 import_qualification:
4597 		import_qualification_type '(' relation_expr_list ')'
4598 			{
4599 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
4600 				n->type = $1;
4601 				n->table_names = $3;
4602 				$$ = n;
4603 			}
4604 		| /*EMPTY*/
4605 			{
4606 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
4607 				n->type = FDW_IMPORT_SCHEMA_ALL;
4608 				n->table_names = NIL;
4609 				$$ = n;
4610 			}
4611 		;
4612 
4613 /*****************************************************************************
4614  *
4615  *		QUERY:
4616  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
4617  *
4618  *****************************************************************************/
4619 
4620 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
4621 				{
4622 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
4623 					n->user = $5;
4624 					n->servername = $7;
4625 					n->options = $8;
4626 					$$ = (Node *) n;
4627 				}
4628 		;
4629 
4630 /* User mapping authorization identifier */
4631 auth_ident: RoleSpec			{ $$ = $1; }
4632 			| USER				{ $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1); }
4633 		;
4634 
4635 /*****************************************************************************
4636  *
4637  *		QUERY :
4638  *				DROP USER MAPPING FOR auth_ident SERVER name
4639  *
4640  ****************************************************************************/
4641 
4642 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
4643 				{
4644 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
4645 					n->user = $5;
4646 					n->servername = $7;
4647 					n->missing_ok = false;
4648 					$$ = (Node *) n;
4649 				}
4650 				|  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
4651 				{
4652 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
4653 					n->user = $7;
4654 					n->servername = $9;
4655 					n->missing_ok = true;
4656 					$$ = (Node *) n;
4657 				}
4658 		;
4659 
4660 /*****************************************************************************
4661  *
4662  *		QUERY :
4663  *				ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
4664  *
4665  ****************************************************************************/
4666 
4667 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
4668 				{
4669 					AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
4670 					n->user = $5;
4671 					n->servername = $7;
4672 					n->options = $8;
4673 					$$ = (Node *) n;
4674 				}
4675 		;
4676 
4677 /*****************************************************************************
4678  *
4679  *		QUERIES:
4680  *				CREATE POLICY name ON table [FOR cmd] [TO role, ...]
4681  *					[USING (qual)] [WITH CHECK (with_check)]
4682  *				ALTER POLICY name ON table [TO role, ...]
4683  *					[USING (qual)] [WITH CHECK (with_check)]
4684  *				DROP POLICY name ON table
4685  *
4686  *****************************************************************************/
4687 
4688 CreatePolicyStmt:
4689 			CREATE POLICY name ON qualified_name RowSecurityDefaultForCmd
4690 				RowSecurityDefaultToRole RowSecurityOptionalExpr
4691 				RowSecurityOptionalWithCheck
4692 				{
4693 					CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
4694 					n->policy_name = $3;
4695 					n->table = $5;
4696 					n->cmd_name = $6;
4697 					n->roles = $7;
4698 					n->qual = $8;
4699 					n->with_check = $9;
4700 					$$ = (Node *) n;
4701 				}
4702 		;
4703 
4704 AlterPolicyStmt:
4705 			ALTER POLICY name ON qualified_name RowSecurityOptionalToRole
4706 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
4707 				{
4708 					AlterPolicyStmt *n = makeNode(AlterPolicyStmt);
4709 					n->policy_name = $3;
4710 					n->table = $5;
4711 					n->roles = $6;
4712 					n->qual = $7;
4713 					n->with_check = $8;
4714 					$$ = (Node *) n;
4715 				}
4716 		;
4717 
4718 DropPolicyStmt:
4719 			DROP POLICY name ON any_name opt_drop_behavior
4720 				{
4721 					DropStmt *n = makeNode(DropStmt);
4722 					n->removeType = OBJECT_POLICY;
4723 					n->objects = list_make1(lappend($5, makeString($3)));
4724 					n->arguments = NIL;
4725 					n->behavior = $6;
4726 					n->missing_ok = false;
4727 					n->concurrent = false;
4728 					$$ = (Node *) n;
4729 				}
4730 			| DROP POLICY IF_P EXISTS name ON any_name opt_drop_behavior
4731 				{
4732 					DropStmt *n = makeNode(DropStmt);
4733 					n->removeType = OBJECT_POLICY;
4734 					n->objects = list_make1(lappend($7, makeString($5)));
4735 					n->arguments = NIL;
4736 					n->behavior = $8;
4737 					n->missing_ok = true;
4738 					n->concurrent = false;
4739 					$$ = (Node *) n;
4740 				}
4741 		;
4742 
4743 RowSecurityOptionalExpr:
4744 			USING '(' a_expr ')'	{ $$ = $3; }
4745 			| /* EMPTY */			{ $$ = NULL; }
4746 		;
4747 
4748 RowSecurityOptionalWithCheck:
4749 			WITH CHECK '(' a_expr ')'		{ $$ = $4; }
4750 			| /* EMPTY */					{ $$ = NULL; }
4751 		;
4752 
4753 RowSecurityDefaultToRole:
4754 			TO role_list			{ $$ = $2; }
4755 			| /* EMPTY */			{ $$ = list_make1(makeRoleSpec(ROLESPEC_PUBLIC, -1)); }
4756 		;
4757 
4758 RowSecurityOptionalToRole:
4759 			TO role_list			{ $$ = $2; }
4760 			| /* EMPTY */			{ $$ = NULL; }
4761 		;
4762 
4763 RowSecurityDefaultForCmd:
4764 			FOR row_security_cmd	{ $$ = $2; }
4765 			| /* EMPTY */			{ $$ = "all"; }
4766 		;
4767 
4768 row_security_cmd:
4769 			ALL				{ $$ = "all"; }
4770 		|	SELECT			{ $$ = "select"; }
4771 		|	INSERT			{ $$ = "insert"; }
4772 		|	UPDATE			{ $$ = "update"; }
4773 		|	DELETE_P		{ $$ = "delete"; }
4774 		;
4775 
4776 /*****************************************************************************
4777  *
4778  *		QUERY:
4779  *             CREATE ACCESS METHOD name HANDLER handler_name
4780  *
4781  *****************************************************************************/
4782 
4783 CreateAmStmt: CREATE ACCESS METHOD name TYPE_P INDEX HANDLER handler_name
4784 				{
4785 					CreateAmStmt *n = makeNode(CreateAmStmt);
4786 					n->amname = $4;
4787 					n->handler_name = $8;
4788 					n->amtype = AMTYPE_INDEX;
4789 					$$ = (Node *) n;
4790 				}
4791 		;
4792 
4793 /*****************************************************************************
4794  *
4795  *		QUERIES :
4796  *				CREATE TRIGGER ...
4797  *				DROP TRIGGER ...
4798  *
4799  *****************************************************************************/
4800 
4801 CreateTrigStmt:
4802 			CREATE TRIGGER name TriggerActionTime TriggerEvents ON
4803 			qualified_name TriggerForSpec TriggerWhen
4804 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
4805 				{
4806 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
4807 					n->trigname = $3;
4808 					n->relation = $7;
4809 					n->funcname = $12;
4810 					n->args = $14;
4811 					n->row = $8;
4812 					n->timing = $4;
4813 					n->events = intVal(linitial($5));
4814 					n->columns = (List *) lsecond($5);
4815 					n->whenClause = $9;
4816 					n->isconstraint  = FALSE;
4817 					n->deferrable	 = FALSE;
4818 					n->initdeferred  = FALSE;
4819 					n->constrrel = NULL;
4820 					$$ = (Node *)n;
4821 				}
4822 			| CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
4823 			qualified_name OptConstrFromTable ConstraintAttributeSpec
4824 			FOR EACH ROW TriggerWhen
4825 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
4826 				{
4827 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
4828 					n->trigname = $4;
4829 					n->relation = $8;
4830 					n->funcname = $17;
4831 					n->args = $19;
4832 					n->row = TRUE;
4833 					n->timing = TRIGGER_TYPE_AFTER;
4834 					n->events = intVal(linitial($6));
4835 					n->columns = (List *) lsecond($6);
4836 					n->whenClause = $14;
4837 					n->isconstraint  = TRUE;
4838 					processCASbits($10, @10, "TRIGGER",
4839 								   &n->deferrable, &n->initdeferred, NULL,
4840 								   NULL, yyscanner);
4841 					n->constrrel = $9;
4842 					$$ = (Node *)n;
4843 				}
4844 		;
4845 
4846 TriggerActionTime:
4847 			BEFORE								{ $$ = TRIGGER_TYPE_BEFORE; }
4848 			| AFTER								{ $$ = TRIGGER_TYPE_AFTER; }
4849 			| INSTEAD OF						{ $$ = TRIGGER_TYPE_INSTEAD; }
4850 		;
4851 
4852 TriggerEvents:
4853 			TriggerOneEvent
4854 				{ $$ = $1; }
4855 			| TriggerEvents OR TriggerOneEvent
4856 				{
4857 					int		events1 = intVal(linitial($1));
4858 					int		events2 = intVal(linitial($3));
4859 					List   *columns1 = (List *) lsecond($1);
4860 					List   *columns2 = (List *) lsecond($3);
4861 
4862 					if (events1 & events2)
4863 						parser_yyerror("duplicate trigger events specified");
4864 					/*
4865 					 * concat'ing the columns lists loses information about
4866 					 * which columns went with which event, but so long as
4867 					 * only UPDATE carries columns and we disallow multiple
4868 					 * UPDATE items, it doesn't matter.  Command execution
4869 					 * should just ignore the columns for non-UPDATE events.
4870 					 */
4871 					$$ = list_make2(makeInteger(events1 | events2),
4872 									list_concat(columns1, columns2));
4873 				}
4874 		;
4875 
4876 TriggerOneEvent:
4877 			INSERT
4878 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
4879 			| DELETE_P
4880 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
4881 			| UPDATE
4882 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
4883 			| UPDATE OF columnList
4884 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
4885 			| TRUNCATE
4886 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
4887 		;
4888 
4889 TriggerForSpec:
4890 			FOR TriggerForOptEach TriggerForType
4891 				{
4892 					$$ = $3;
4893 				}
4894 			| /* EMPTY */
4895 				{
4896 					/*
4897 					 * If ROW/STATEMENT not specified, default to
4898 					 * STATEMENT, per SQL
4899 					 */
4900 					$$ = FALSE;
4901 				}
4902 		;
4903 
4904 TriggerForOptEach:
4905 			EACH									{}
4906 			| /*EMPTY*/								{}
4907 		;
4908 
4909 TriggerForType:
4910 			ROW										{ $$ = TRUE; }
4911 			| STATEMENT								{ $$ = FALSE; }
4912 		;
4913 
4914 TriggerWhen:
4915 			WHEN '(' a_expr ')'						{ $$ = $3; }
4916 			| /*EMPTY*/								{ $$ = NULL; }
4917 		;
4918 
4919 TriggerFuncArgs:
4920 			TriggerFuncArg							{ $$ = list_make1($1); }
4921 			| TriggerFuncArgs ',' TriggerFuncArg	{ $$ = lappend($1, $3); }
4922 			| /*EMPTY*/								{ $$ = NIL; }
4923 		;
4924 
4925 TriggerFuncArg:
4926 			Iconst
4927 				{
4928 					$$ = makeString(psprintf("%d", $1));
4929 				}
4930 			| FCONST								{ $$ = makeString($1); }
4931 			| Sconst								{ $$ = makeString($1); }
4932 			| ColLabel								{ $$ = makeString($1); }
4933 		;
4934 
4935 OptConstrFromTable:
4936 			FROM qualified_name						{ $$ = $2; }
4937 			| /*EMPTY*/								{ $$ = NULL; }
4938 		;
4939 
4940 ConstraintAttributeSpec:
4941 			/*EMPTY*/
4942 				{ $$ = 0; }
4943 			| ConstraintAttributeSpec ConstraintAttributeElem
4944 				{
4945 					/*
4946 					 * We must complain about conflicting options.
4947 					 * We could, but choose not to, complain about redundant
4948 					 * options (ie, where $2's bit is already set in $1).
4949 					 */
4950 					int		newspec = $1 | $2;
4951 
4952 					/* special message for this case */
4953 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED)) == (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED))
4954 						ereport(ERROR,
4955 								(errcode(ERRCODE_SYNTAX_ERROR),
4956 								 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
4957 								 parser_errposition(@2)));
4958 					/* generic message for other conflicts */
4959 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE)) == (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE) ||
4960 						(newspec & (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED)) == (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED))
4961 						ereport(ERROR,
4962 								(errcode(ERRCODE_SYNTAX_ERROR),
4963 								 errmsg("conflicting constraint properties"),
4964 								 parser_errposition(@2)));
4965 					$$ = newspec;
4966 				}
4967 		;
4968 
4969 ConstraintAttributeElem:
4970 			NOT DEFERRABLE					{ $$ = CAS_NOT_DEFERRABLE; }
4971 			| DEFERRABLE					{ $$ = CAS_DEFERRABLE; }
4972 			| INITIALLY IMMEDIATE			{ $$ = CAS_INITIALLY_IMMEDIATE; }
4973 			| INITIALLY DEFERRED			{ $$ = CAS_INITIALLY_DEFERRED; }
4974 			| NOT VALID						{ $$ = CAS_NOT_VALID; }
4975 			| NO INHERIT					{ $$ = CAS_NO_INHERIT; }
4976 		;
4977 
4978 
4979 DropTrigStmt:
4980 			DROP TRIGGER name ON any_name opt_drop_behavior
4981 				{
4982 					DropStmt *n = makeNode(DropStmt);
4983 					n->removeType = OBJECT_TRIGGER;
4984 					n->objects = list_make1(lappend($5, makeString($3)));
4985 					n->arguments = NIL;
4986 					n->behavior = $6;
4987 					n->missing_ok = false;
4988 					n->concurrent = false;
4989 					$$ = (Node *) n;
4990 				}
4991 			| DROP TRIGGER IF_P EXISTS name ON any_name opt_drop_behavior
4992 				{
4993 					DropStmt *n = makeNode(DropStmt);
4994 					n->removeType = OBJECT_TRIGGER;
4995 					n->objects = list_make1(lappend($7, makeString($5)));
4996 					n->arguments = NIL;
4997 					n->behavior = $8;
4998 					n->missing_ok = true;
4999 					n->concurrent = false;
5000 					$$ = (Node *) n;
5001 				}
5002 		;
5003 
5004 
5005 /*****************************************************************************
5006  *
5007  *		QUERIES :
5008  *				CREATE EVENT TRIGGER ...
5009  *				ALTER EVENT TRIGGER ...
5010  *
5011  *****************************************************************************/
5012 
5013 CreateEventTrigStmt:
5014 			CREATE EVENT TRIGGER name ON ColLabel
5015 			EXECUTE PROCEDURE func_name '(' ')'
5016 				{
5017 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5018 					n->trigname = $4;
5019 					n->eventname = $6;
5020 					n->whenclause = NULL;
5021 					n->funcname = $9;
5022 					$$ = (Node *)n;
5023 				}
5024 		  | CREATE EVENT TRIGGER name ON ColLabel
5025 			WHEN event_trigger_when_list
5026 			EXECUTE PROCEDURE func_name '(' ')'
5027 				{
5028 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5029 					n->trigname = $4;
5030 					n->eventname = $6;
5031 					n->whenclause = $8;
5032 					n->funcname = $11;
5033 					$$ = (Node *)n;
5034 				}
5035 		;
5036 
5037 event_trigger_when_list:
5038 		  event_trigger_when_item
5039 			{ $$ = list_make1($1); }
5040 		| event_trigger_when_list AND event_trigger_when_item
5041 			{ $$ = lappend($1, $3); }
5042 		;
5043 
5044 event_trigger_when_item:
5045 		ColId IN_P '(' event_trigger_value_list ')'
5046 			{ $$ = makeDefElem($1, (Node *) $4); }
5047 		;
5048 
5049 event_trigger_value_list:
5050 		  SCONST
5051 			{ $$ = list_make1(makeString($1)); }
5052 		| event_trigger_value_list ',' SCONST
5053 			{ $$ = lappend($1, makeString($3)); }
5054 		;
5055 
5056 AlterEventTrigStmt:
5057 			ALTER EVENT TRIGGER name enable_trigger
5058 				{
5059 					AlterEventTrigStmt *n = makeNode(AlterEventTrigStmt);
5060 					n->trigname = $4;
5061 					n->tgenabled = $5;
5062 					$$ = (Node *) n;
5063 				}
5064 		;
5065 
5066 enable_trigger:
5067 			ENABLE_P					{ $$ = TRIGGER_FIRES_ON_ORIGIN; }
5068 			| ENABLE_P REPLICA			{ $$ = TRIGGER_FIRES_ON_REPLICA; }
5069 			| ENABLE_P ALWAYS			{ $$ = TRIGGER_FIRES_ALWAYS; }
5070 			| DISABLE_P					{ $$ = TRIGGER_DISABLED; }
5071 		;
5072 
5073 /*****************************************************************************
5074  *
5075  *		QUERIES :
5076  *				CREATE ASSERTION ...
5077  *				DROP ASSERTION ...
5078  *
5079  *****************************************************************************/
5080 
5081 CreateAssertStmt:
5082 			CREATE ASSERTION name CHECK '(' a_expr ')'
5083 			ConstraintAttributeSpec
5084 				{
5085 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5086 					n->trigname = $3;
5087 					n->args = list_make1($6);
5088 					n->isconstraint  = TRUE;
5089 					processCASbits($8, @8, "ASSERTION",
5090 								   &n->deferrable, &n->initdeferred, NULL,
5091 								   NULL, yyscanner);
5092 
5093 					ereport(ERROR,
5094 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5095 							 errmsg("CREATE ASSERTION is not yet implemented")));
5096 
5097 					$$ = (Node *)n;
5098 				}
5099 		;
5100 
5101 DropAssertStmt:
5102 			DROP ASSERTION name opt_drop_behavior
5103 				{
5104 					DropStmt *n = makeNode(DropStmt);
5105 					n->objects = NIL;
5106 					n->arguments = NIL;
5107 					n->behavior = $4;
5108 					n->removeType = OBJECT_TRIGGER; /* XXX */
5109 					ereport(ERROR,
5110 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5111 							 errmsg("DROP ASSERTION is not yet implemented")));
5112 					$$ = (Node *) n;
5113 				}
5114 		;
5115 
5116 
5117 /*****************************************************************************
5118  *
5119  *		QUERY :
5120  *				define (aggregate,operator,type)
5121  *
5122  *****************************************************************************/
5123 
5124 DefineStmt:
5125 			CREATE AGGREGATE func_name aggr_args definition
5126 				{
5127 					DefineStmt *n = makeNode(DefineStmt);
5128 					n->kind = OBJECT_AGGREGATE;
5129 					n->oldstyle = false;
5130 					n->defnames = $3;
5131 					n->args = $4;
5132 					n->definition = $5;
5133 					$$ = (Node *)n;
5134 				}
5135 			| CREATE AGGREGATE func_name old_aggr_definition
5136 				{
5137 					/* old-style (pre-8.2) syntax for CREATE AGGREGATE */
5138 					DefineStmt *n = makeNode(DefineStmt);
5139 					n->kind = OBJECT_AGGREGATE;
5140 					n->oldstyle = true;
5141 					n->defnames = $3;
5142 					n->args = NIL;
5143 					n->definition = $4;
5144 					$$ = (Node *)n;
5145 				}
5146 			| CREATE OPERATOR any_operator definition
5147 				{
5148 					DefineStmt *n = makeNode(DefineStmt);
5149 					n->kind = OBJECT_OPERATOR;
5150 					n->oldstyle = false;
5151 					n->defnames = $3;
5152 					n->args = NIL;
5153 					n->definition = $4;
5154 					$$ = (Node *)n;
5155 				}
5156 			| CREATE TYPE_P any_name definition
5157 				{
5158 					DefineStmt *n = makeNode(DefineStmt);
5159 					n->kind = OBJECT_TYPE;
5160 					n->oldstyle = false;
5161 					n->defnames = $3;
5162 					n->args = NIL;
5163 					n->definition = $4;
5164 					$$ = (Node *)n;
5165 				}
5166 			| CREATE TYPE_P any_name
5167 				{
5168 					/* Shell type (identified by lack of definition) */
5169 					DefineStmt *n = makeNode(DefineStmt);
5170 					n->kind = OBJECT_TYPE;
5171 					n->oldstyle = false;
5172 					n->defnames = $3;
5173 					n->args = NIL;
5174 					n->definition = NIL;
5175 					$$ = (Node *)n;
5176 				}
5177 			| CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
5178 				{
5179 					CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
5180 
5181 					/* can't use qualified_name, sigh */
5182 					n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
5183 					n->coldeflist = $6;
5184 					$$ = (Node *)n;
5185 				}
5186 			| CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
5187 				{
5188 					CreateEnumStmt *n = makeNode(CreateEnumStmt);
5189 					n->typeName = $3;
5190 					n->vals = $7;
5191 					$$ = (Node *)n;
5192 				}
5193 			| CREATE TYPE_P any_name AS RANGE definition
5194 				{
5195 					CreateRangeStmt *n = makeNode(CreateRangeStmt);
5196 					n->typeName = $3;
5197 					n->params	= $6;
5198 					$$ = (Node *)n;
5199 				}
5200 			| CREATE TEXT_P SEARCH PARSER any_name definition
5201 				{
5202 					DefineStmt *n = makeNode(DefineStmt);
5203 					n->kind = OBJECT_TSPARSER;
5204 					n->args = NIL;
5205 					n->defnames = $5;
5206 					n->definition = $6;
5207 					$$ = (Node *)n;
5208 				}
5209 			| CREATE TEXT_P SEARCH DICTIONARY any_name definition
5210 				{
5211 					DefineStmt *n = makeNode(DefineStmt);
5212 					n->kind = OBJECT_TSDICTIONARY;
5213 					n->args = NIL;
5214 					n->defnames = $5;
5215 					n->definition = $6;
5216 					$$ = (Node *)n;
5217 				}
5218 			| CREATE TEXT_P SEARCH TEMPLATE any_name definition
5219 				{
5220 					DefineStmt *n = makeNode(DefineStmt);
5221 					n->kind = OBJECT_TSTEMPLATE;
5222 					n->args = NIL;
5223 					n->defnames = $5;
5224 					n->definition = $6;
5225 					$$ = (Node *)n;
5226 				}
5227 			| CREATE TEXT_P SEARCH CONFIGURATION any_name definition
5228 				{
5229 					DefineStmt *n = makeNode(DefineStmt);
5230 					n->kind = OBJECT_TSCONFIGURATION;
5231 					n->args = NIL;
5232 					n->defnames = $5;
5233 					n->definition = $6;
5234 					$$ = (Node *)n;
5235 				}
5236 			| CREATE COLLATION any_name definition
5237 				{
5238 					DefineStmt *n = makeNode(DefineStmt);
5239 					n->kind = OBJECT_COLLATION;
5240 					n->args = NIL;
5241 					n->defnames = $3;
5242 					n->definition = $4;
5243 					$$ = (Node *)n;
5244 				}
5245 			| CREATE COLLATION any_name FROM any_name
5246 				{
5247 					DefineStmt *n = makeNode(DefineStmt);
5248 					n->kind = OBJECT_COLLATION;
5249 					n->args = NIL;
5250 					n->defnames = $3;
5251 					n->definition = list_make1(makeDefElem("from", (Node *) $5));
5252 					$$ = (Node *)n;
5253 				}
5254 		;
5255 
5256 definition: '(' def_list ')'						{ $$ = $2; }
5257 		;
5258 
5259 def_list:	def_elem								{ $$ = list_make1($1); }
5260 			| def_list ',' def_elem					{ $$ = lappend($1, $3); }
5261 		;
5262 
5263 def_elem:	ColLabel '=' def_arg
5264 				{
5265 					$$ = makeDefElem($1, (Node *) $3);
5266 				}
5267 			| ColLabel
5268 				{
5269 					$$ = makeDefElem($1, NULL);
5270 				}
5271 		;
5272 
5273 /* Note: any simple identifier will be returned as a type name! */
5274 def_arg:	func_type						{ $$ = (Node *)$1; }
5275 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
5276 			| qual_all_Op					{ $$ = (Node *)$1; }
5277 			| NumericOnly					{ $$ = (Node *)$1; }
5278 			| Sconst						{ $$ = (Node *)makeString($1); }
5279 		;
5280 
5281 old_aggr_definition: '(' old_aggr_list ')'			{ $$ = $2; }
5282 		;
5283 
5284 old_aggr_list: old_aggr_elem						{ $$ = list_make1($1); }
5285 			| old_aggr_list ',' old_aggr_elem		{ $$ = lappend($1, $3); }
5286 		;
5287 
5288 /*
5289  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
5290  * the item names needed in old aggregate definitions are likely to become
5291  * SQL keywords.
5292  */
5293 old_aggr_elem:  IDENT '=' def_arg
5294 				{
5295 					$$ = makeDefElem($1, (Node *)$3);
5296 				}
5297 		;
5298 
5299 opt_enum_val_list:
5300 		enum_val_list							{ $$ = $1; }
5301 		| /*EMPTY*/								{ $$ = NIL; }
5302 		;
5303 
5304 enum_val_list:	Sconst
5305 				{ $$ = list_make1(makeString($1)); }
5306 			| enum_val_list ',' Sconst
5307 				{ $$ = lappend($1, makeString($3)); }
5308 		;
5309 
5310 /*****************************************************************************
5311  *
5312  *	ALTER TYPE enumtype ADD ...
5313  *
5314  *****************************************************************************/
5315 
5316 AlterEnumStmt:
5317 		ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst
5318 			{
5319 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5320 				n->typeName = $3;
5321 				n->newVal = $7;
5322 				n->newValNeighbor = NULL;
5323 				n->newValIsAfter = true;
5324 				n->skipIfExists = $6;
5325 				$$ = (Node *) n;
5326 			}
5327 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst BEFORE Sconst
5328 			{
5329 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5330 				n->typeName = $3;
5331 				n->newVal = $7;
5332 				n->newValNeighbor = $9;
5333 				n->newValIsAfter = false;
5334 				n->skipIfExists = $6;
5335 				$$ = (Node *) n;
5336 			}
5337 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst AFTER Sconst
5338 			{
5339 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5340 				n->typeName = $3;
5341 				n->newVal = $7;
5342 				n->newValNeighbor = $9;
5343 				n->newValIsAfter = true;
5344 				n->skipIfExists = $6;
5345 				$$ = (Node *) n;
5346 			}
5347 		 ;
5348 
5349 opt_if_not_exists: IF_P NOT EXISTS              { $$ = true; }
5350 		| /* empty */                          { $$ = false; }
5351 		;
5352 
5353 
5354 /*****************************************************************************
5355  *
5356  *		QUERIES :
5357  *				CREATE OPERATOR CLASS ...
5358  *				CREATE OPERATOR FAMILY ...
5359  *				ALTER OPERATOR FAMILY ...
5360  *				DROP OPERATOR CLASS ...
5361  *				DROP OPERATOR FAMILY ...
5362  *
5363  *****************************************************************************/
5364 
5365 CreateOpClassStmt:
5366 			CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
5367 			USING access_method opt_opfamily AS opclass_item_list
5368 				{
5369 					CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
5370 					n->opclassname = $4;
5371 					n->isDefault = $5;
5372 					n->datatype = $8;
5373 					n->amname = $10;
5374 					n->opfamilyname = $11;
5375 					n->items = $13;
5376 					$$ = (Node *) n;
5377 				}
5378 		;
5379 
5380 opclass_item_list:
5381 			opclass_item							{ $$ = list_make1($1); }
5382 			| opclass_item_list ',' opclass_item	{ $$ = lappend($1, $3); }
5383 		;
5384 
5385 opclass_item:
5386 			OPERATOR Iconst any_operator opclass_purpose opt_recheck
5387 				{
5388 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5389 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5390 					n->name = $3;
5391 					n->args = NIL;
5392 					n->number = $2;
5393 					n->order_family = $4;
5394 					$$ = (Node *) n;
5395 				}
5396 			| OPERATOR Iconst any_operator oper_argtypes opclass_purpose
5397 			  opt_recheck
5398 				{
5399 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5400 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5401 					n->name = $3;
5402 					n->args = $4;
5403 					n->number = $2;
5404 					n->order_family = $5;
5405 					$$ = (Node *) n;
5406 				}
5407 			| FUNCTION Iconst func_name func_args
5408 				{
5409 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5410 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5411 					n->name = $3;
5412 					n->args = extractArgTypes($4);
5413 					n->number = $2;
5414 					$$ = (Node *) n;
5415 				}
5416 			| FUNCTION Iconst '(' type_list ')' func_name func_args
5417 				{
5418 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5419 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5420 					n->name = $6;
5421 					n->args = extractArgTypes($7);
5422 					n->number = $2;
5423 					n->class_args = $4;
5424 					$$ = (Node *) n;
5425 				}
5426 			| STORAGE Typename
5427 				{
5428 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5429 					n->itemtype = OPCLASS_ITEM_STORAGETYPE;
5430 					n->storedtype = $2;
5431 					$$ = (Node *) n;
5432 				}
5433 		;
5434 
5435 opt_default:	DEFAULT						{ $$ = TRUE; }
5436 			| /*EMPTY*/						{ $$ = FALSE; }
5437 		;
5438 
5439 opt_opfamily:	FAMILY any_name				{ $$ = $2; }
5440 			| /*EMPTY*/						{ $$ = NIL; }
5441 		;
5442 
5443 opclass_purpose: FOR SEARCH					{ $$ = NIL; }
5444 			| FOR ORDER BY any_name			{ $$ = $4; }
5445 			| /*EMPTY*/						{ $$ = NIL; }
5446 		;
5447 
5448 opt_recheck:	RECHECK
5449 				{
5450 					/*
5451 					 * RECHECK no longer does anything in opclass definitions,
5452 					 * but we still accept it to ease porting of old database
5453 					 * dumps.
5454 					 */
5455 					ereport(NOTICE,
5456 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5457 							 errmsg("RECHECK is no longer required"),
5458 							 errhint("Update your data type."),
5459 							 parser_errposition(@1)));
5460 					$$ = TRUE;
5461 				}
5462 			| /*EMPTY*/						{ $$ = FALSE; }
5463 		;
5464 
5465 
5466 CreateOpFamilyStmt:
5467 			CREATE OPERATOR FAMILY any_name USING access_method
5468 				{
5469 					CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
5470 					n->opfamilyname = $4;
5471 					n->amname = $6;
5472 					$$ = (Node *) n;
5473 				}
5474 		;
5475 
5476 AlterOpFamilyStmt:
5477 			ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
5478 				{
5479 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
5480 					n->opfamilyname = $4;
5481 					n->amname = $6;
5482 					n->isDrop = false;
5483 					n->items = $8;
5484 					$$ = (Node *) n;
5485 				}
5486 			| ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
5487 				{
5488 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
5489 					n->opfamilyname = $4;
5490 					n->amname = $6;
5491 					n->isDrop = true;
5492 					n->items = $8;
5493 					$$ = (Node *) n;
5494 				}
5495 		;
5496 
5497 opclass_drop_list:
5498 			opclass_drop							{ $$ = list_make1($1); }
5499 			| opclass_drop_list ',' opclass_drop	{ $$ = lappend($1, $3); }
5500 		;
5501 
5502 opclass_drop:
5503 			OPERATOR Iconst '(' type_list ')'
5504 				{
5505 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5506 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5507 					n->number = $2;
5508 					n->args = $4;
5509 					$$ = (Node *) n;
5510 				}
5511 			| FUNCTION Iconst '(' type_list ')'
5512 				{
5513 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5514 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5515 					n->number = $2;
5516 					n->args = $4;
5517 					$$ = (Node *) n;
5518 				}
5519 		;
5520 
5521 
5522 DropOpClassStmt:
5523 			DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
5524 				{
5525 					DropStmt *n = makeNode(DropStmt);
5526 					n->objects = list_make1(lcons(makeString($6), $4));
5527 					n->removeType = OBJECT_OPCLASS;
5528 					n->behavior = $7;
5529 					n->missing_ok = false;
5530 					n->concurrent = false;
5531 					$$ = (Node *) n;
5532 				}
5533 			| DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
5534 				{
5535 					DropStmt *n = makeNode(DropStmt);
5536 					n->objects = list_make1(lcons(makeString($8), $6));
5537 					n->removeType = OBJECT_OPCLASS;
5538 					n->behavior = $9;
5539 					n->missing_ok = true;
5540 					n->concurrent = false;
5541 					$$ = (Node *) n;
5542 				}
5543 		;
5544 
5545 DropOpFamilyStmt:
5546 			DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
5547 				{
5548 					DropStmt *n = makeNode(DropStmt);
5549 					n->objects = list_make1(lcons(makeString($6), $4));
5550 					n->removeType = OBJECT_OPFAMILY;
5551 					n->behavior = $7;
5552 					n->missing_ok = false;
5553 					n->concurrent = false;
5554 					$$ = (Node *) n;
5555 				}
5556 			| DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
5557 				{
5558 					DropStmt *n = makeNode(DropStmt);
5559 					n->objects = list_make1(lcons(makeString($8), $6));
5560 					n->removeType = OBJECT_OPFAMILY;
5561 					n->behavior = $9;
5562 					n->missing_ok = true;
5563 					n->concurrent = false;
5564 					$$ = (Node *) n;
5565 				}
5566 		;
5567 
5568 
5569 /*****************************************************************************
5570  *
5571  *		QUERY:
5572  *
5573  *		DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
5574  *		REASSIGN OWNED BY username [, username ...] TO username
5575  *
5576  *****************************************************************************/
5577 DropOwnedStmt:
5578 			DROP OWNED BY role_list opt_drop_behavior
5579 				{
5580 					DropOwnedStmt *n = makeNode(DropOwnedStmt);
5581 					n->roles = $4;
5582 					n->behavior = $5;
5583 					$$ = (Node *)n;
5584 				}
5585 		;
5586 
5587 ReassignOwnedStmt:
5588 			REASSIGN OWNED BY role_list TO RoleSpec
5589 				{
5590 					ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
5591 					n->roles = $4;
5592 					n->newrole = $6;
5593 					$$ = (Node *)n;
5594 				}
5595 		;
5596 
5597 /*****************************************************************************
5598  *
5599  *		QUERY:
5600  *
5601  *		DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
5602  *           [ RESTRICT | CASCADE ]
5603  *
5604  *****************************************************************************/
5605 
5606 DropStmt:	DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
5607 				{
5608 					DropStmt *n = makeNode(DropStmt);
5609 					n->removeType = $2;
5610 					n->missing_ok = TRUE;
5611 					n->objects = $5;
5612 					n->arguments = NIL;
5613 					n->behavior = $6;
5614 					n->concurrent = false;
5615 					$$ = (Node *)n;
5616 				}
5617 			| DROP drop_type any_name_list opt_drop_behavior
5618 				{
5619 					DropStmt *n = makeNode(DropStmt);
5620 					n->removeType = $2;
5621 					n->missing_ok = FALSE;
5622 					n->objects = $3;
5623 					n->arguments = NIL;
5624 					n->behavior = $4;
5625 					n->concurrent = false;
5626 					$$ = (Node *)n;
5627 				}
5628 			| DROP TYPE_P type_name_list opt_drop_behavior
5629 				{
5630 					DropStmt *n = makeNode(DropStmt);
5631 					n->removeType = OBJECT_TYPE;
5632 					n->missing_ok = FALSE;
5633 					n->objects = $3;
5634 					n->behavior = $4;
5635 					n->concurrent = false;
5636 					$$ = (Node *) n;
5637 				}
5638 			| DROP TYPE_P IF_P EXISTS type_name_list opt_drop_behavior
5639 				{
5640 					DropStmt *n = makeNode(DropStmt);
5641 					n->removeType = OBJECT_TYPE;
5642 					n->missing_ok = TRUE;
5643 					n->objects = $5;
5644 					n->behavior = $6;
5645 					n->concurrent = false;
5646 					$$ = (Node *) n;
5647 				}
5648 			| DROP DOMAIN_P type_name_list opt_drop_behavior
5649 				{
5650 					DropStmt *n = makeNode(DropStmt);
5651 					n->removeType = OBJECT_DOMAIN;
5652 					n->missing_ok = FALSE;
5653 					n->objects = $3;
5654 					n->behavior = $4;
5655 					n->concurrent = false;
5656 					$$ = (Node *) n;
5657 				}
5658 			| DROP DOMAIN_P IF_P EXISTS type_name_list opt_drop_behavior
5659 				{
5660 					DropStmt *n = makeNode(DropStmt);
5661 					n->removeType = OBJECT_DOMAIN;
5662 					n->missing_ok = TRUE;
5663 					n->objects = $5;
5664 					n->behavior = $6;
5665 					n->concurrent = false;
5666 					$$ = (Node *) n;
5667 				}
5668 			| DROP INDEX CONCURRENTLY any_name_list opt_drop_behavior
5669 				{
5670 					DropStmt *n = makeNode(DropStmt);
5671 					n->removeType = OBJECT_INDEX;
5672 					n->missing_ok = FALSE;
5673 					n->objects = $4;
5674 					n->arguments = NIL;
5675 					n->behavior = $5;
5676 					n->concurrent = true;
5677 					$$ = (Node *)n;
5678 				}
5679 			| DROP INDEX CONCURRENTLY IF_P EXISTS any_name_list opt_drop_behavior
5680 				{
5681 					DropStmt *n = makeNode(DropStmt);
5682 					n->removeType = OBJECT_INDEX;
5683 					n->missing_ok = TRUE;
5684 					n->objects = $6;
5685 					n->arguments = NIL;
5686 					n->behavior = $7;
5687 					n->concurrent = true;
5688 					$$ = (Node *)n;
5689 				}
5690 		;
5691 
5692 
5693 drop_type:	TABLE									{ $$ = OBJECT_TABLE; }
5694 			| SEQUENCE								{ $$ = OBJECT_SEQUENCE; }
5695 			| VIEW									{ $$ = OBJECT_VIEW; }
5696 			| MATERIALIZED VIEW						{ $$ = OBJECT_MATVIEW; }
5697 			| INDEX									{ $$ = OBJECT_INDEX; }
5698 			| FOREIGN TABLE							{ $$ = OBJECT_FOREIGN_TABLE; }
5699 			| ACCESS METHOD							{ $$ = OBJECT_ACCESS_METHOD; }
5700 			| EVENT TRIGGER 						{ $$ = OBJECT_EVENT_TRIGGER; }
5701 			| COLLATION								{ $$ = OBJECT_COLLATION; }
5702 			| CONVERSION_P							{ $$ = OBJECT_CONVERSION; }
5703 			| SCHEMA								{ $$ = OBJECT_SCHEMA; }
5704 			| EXTENSION								{ $$ = OBJECT_EXTENSION; }
5705 			| TEXT_P SEARCH PARSER					{ $$ = OBJECT_TSPARSER; }
5706 			| TEXT_P SEARCH DICTIONARY				{ $$ = OBJECT_TSDICTIONARY; }
5707 			| TEXT_P SEARCH TEMPLATE				{ $$ = OBJECT_TSTEMPLATE; }
5708 			| TEXT_P SEARCH CONFIGURATION			{ $$ = OBJECT_TSCONFIGURATION; }
5709 		;
5710 
5711 any_name_list:
5712 			any_name								{ $$ = list_make1($1); }
5713 			| any_name_list ',' any_name			{ $$ = lappend($1, $3); }
5714 		;
5715 
5716 any_name:	ColId						{ $$ = list_make1(makeString($1)); }
5717 			| ColId attrs				{ $$ = lcons(makeString($1), $2); }
5718 		;
5719 
5720 attrs:		'.' attr_name
5721 					{ $$ = list_make1(makeString($2)); }
5722 			| attrs '.' attr_name
5723 					{ $$ = lappend($1, makeString($3)); }
5724 		;
5725 
5726 type_name_list:
5727 			Typename								{ $$ = list_make1(list_make1($1)); }
5728 			| type_name_list ',' Typename			{ $$ = lappend($1, list_make1($3)); }
5729 
5730 /*****************************************************************************
5731  *
5732  *		QUERY:
5733  *				truncate table relname1, relname2, ...
5734  *
5735  *****************************************************************************/
5736 
5737 TruncateStmt:
5738 			TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
5739 				{
5740 					TruncateStmt *n = makeNode(TruncateStmt);
5741 					n->relations = $3;
5742 					n->restart_seqs = $4;
5743 					n->behavior = $5;
5744 					$$ = (Node *)n;
5745 				}
5746 		;
5747 
5748 opt_restart_seqs:
5749 			CONTINUE_P IDENTITY_P		{ $$ = false; }
5750 			| RESTART IDENTITY_P		{ $$ = true; }
5751 			| /* EMPTY */				{ $$ = false; }
5752 		;
5753 
5754 /*****************************************************************************
5755  *
5756  *	The COMMENT ON statement can take different forms based upon the type of
5757  *	the object associated with the comment. The form of the statement is:
5758  *
5759  *	COMMENT ON [ [ ACCESS METHOD | CONVERSION | COLLATION |
5760  *                 DATABASE | DOMAIN |
5761  *                 EXTENSION | EVENT TRIGGER | FOREIGN DATA WRAPPER |
5762  *                 FOREIGN TABLE | INDEX | [PROCEDURAL] LANGUAGE |
5763  *                 MATERIALIZED VIEW | POLICY | ROLE | SCHEMA | SEQUENCE |
5764  *                 SERVER | TABLE | TABLESPACE |
5765  *                 TEXT SEARCH CONFIGURATION | TEXT SEARCH DICTIONARY |
5766  *                 TEXT SEARCH PARSER | TEXT SEARCH TEMPLATE | TYPE |
5767  *                 VIEW] <objname> |
5768  *				 AGGREGATE <aggname> (arg1, ...) |
5769  *				 CAST (<src type> AS <dst type>) |
5770  *				 COLUMN <relname>.<colname> |
5771  *				 CONSTRAINT <constraintname> ON <relname> |
5772  *				 CONSTRAINT <constraintname> ON DOMAIN <domainname> |
5773  *				 FUNCTION <funcname> (arg1, arg2, ...) |
5774  *				 LARGE OBJECT <oid> |
5775  *				 OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
5776  *				 OPERATOR CLASS <name> USING <access-method> |
5777  *				 OPERATOR FAMILY <name> USING <access-method> |
5778  *				 RULE <rulename> ON <relname> |
5779  *				 TRIGGER <triggername> ON <relname> ]
5780  *			   IS { 'text' | NULL }
5781  *
5782  *****************************************************************************/
5783 
5784 CommentStmt:
5785 			COMMENT ON comment_type any_name IS comment_text
5786 				{
5787 					CommentStmt *n = makeNode(CommentStmt);
5788 					n->objtype = $3;
5789 					n->objname = $4;
5790 					n->objargs = NIL;
5791 					n->comment = $6;
5792 					$$ = (Node *) n;
5793 				}
5794 			| COMMENT ON TYPE_P Typename IS comment_text
5795 				{
5796 					CommentStmt *n = makeNode(CommentStmt);
5797 					n->objtype = OBJECT_TYPE;
5798 					n->objname = list_make1($4);
5799 					n->objargs = NIL;
5800 					n->comment = $6;
5801 					$$ = (Node *) n;
5802 				}
5803 			| COMMENT ON DOMAIN_P Typename IS comment_text
5804 				{
5805 					CommentStmt *n = makeNode(CommentStmt);
5806 					n->objtype = OBJECT_DOMAIN;
5807 					n->objname = list_make1($4);
5808 					n->objargs = NIL;
5809 					n->comment = $6;
5810 					$$ = (Node *) n;
5811 				}
5812 			| COMMENT ON AGGREGATE func_name aggr_args IS comment_text
5813 				{
5814 					CommentStmt *n = makeNode(CommentStmt);
5815 					n->objtype = OBJECT_AGGREGATE;
5816 					n->objname = $4;
5817 					n->objargs = extractAggrArgTypes($5);
5818 					n->comment = $7;
5819 					$$ = (Node *) n;
5820 				}
5821 			| COMMENT ON FUNCTION func_name func_args IS comment_text
5822 				{
5823 					CommentStmt *n = makeNode(CommentStmt);
5824 					n->objtype = OBJECT_FUNCTION;
5825 					n->objname = $4;
5826 					n->objargs = extractArgTypes($5);
5827 					n->comment = $7;
5828 					$$ = (Node *) n;
5829 				}
5830 			| COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
5831 				{
5832 					CommentStmt *n = makeNode(CommentStmt);
5833 					n->objtype = OBJECT_OPERATOR;
5834 					n->objname = $4;
5835 					n->objargs = $5;
5836 					n->comment = $7;
5837 					$$ = (Node *) n;
5838 				}
5839 			| COMMENT ON CONSTRAINT name ON any_name IS comment_text
5840 				{
5841 					CommentStmt *n = makeNode(CommentStmt);
5842 					n->objtype = OBJECT_TABCONSTRAINT;
5843 					n->objname = lappend($6, makeString($4));
5844 					n->objargs = NIL;
5845 					n->comment = $8;
5846 					$$ = (Node *) n;
5847 				}
5848 			| COMMENT ON CONSTRAINT name ON DOMAIN_P any_name IS comment_text
5849 				{
5850 					CommentStmt *n = makeNode(CommentStmt);
5851 					n->objtype = OBJECT_DOMCONSTRAINT;
5852 					/*
5853 					 * should use Typename not any_name in the production, but
5854 					 * there's a shift/reduce conflict if we do that, so fix it
5855 					 * up here.
5856 					 */
5857 					n->objname = list_make1(makeTypeNameFromNameList($7));
5858 					n->objargs = list_make1(makeString($4));
5859 					n->comment = $9;
5860 					$$ = (Node *) n;
5861 				}
5862 			| COMMENT ON POLICY name ON any_name IS comment_text
5863 				{
5864 					CommentStmt *n = makeNode(CommentStmt);
5865 					n->objtype = OBJECT_POLICY;
5866 					n->objname = lappend($6, makeString($4));
5867 					n->objargs = NIL;
5868 					n->comment = $8;
5869 					$$ = (Node *) n;
5870 				}
5871 			| COMMENT ON RULE name ON any_name IS comment_text
5872 				{
5873 					CommentStmt *n = makeNode(CommentStmt);
5874 					n->objtype = OBJECT_RULE;
5875 					n->objname = lappend($6, makeString($4));
5876 					n->objargs = NIL;
5877 					n->comment = $8;
5878 					$$ = (Node *) n;
5879 				}
5880 			| COMMENT ON RULE name IS comment_text
5881 				{
5882 					/* Obsolete syntax supported for awhile for compatibility */
5883 					CommentStmt *n = makeNode(CommentStmt);
5884 					n->objtype = OBJECT_RULE;
5885 					n->objname = list_make1(makeString($4));
5886 					n->objargs = NIL;
5887 					n->comment = $6;
5888 					$$ = (Node *) n;
5889 				}
5890 			| COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
5891 				{
5892 					CommentStmt *n = makeNode(CommentStmt);
5893 					n->objtype = OBJECT_TRANSFORM;
5894 					n->objname = list_make1($5);
5895 					n->objargs = list_make1(makeString($7));
5896 					n->comment = $9;
5897 					$$ = (Node *) n;
5898 				}
5899 			| COMMENT ON TRIGGER name ON any_name IS comment_text
5900 				{
5901 					CommentStmt *n = makeNode(CommentStmt);
5902 					n->objtype = OBJECT_TRIGGER;
5903 					n->objname = lappend($6, makeString($4));
5904 					n->objargs = NIL;
5905 					n->comment = $8;
5906 					$$ = (Node *) n;
5907 				}
5908 			| COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
5909 				{
5910 					CommentStmt *n = makeNode(CommentStmt);
5911 					n->objtype = OBJECT_OPCLASS;
5912 					n->objname = lcons(makeString($7), $5);
5913 					n->comment = $9;
5914 					$$ = (Node *) n;
5915 				}
5916 			| COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
5917 				{
5918 					CommentStmt *n = makeNode(CommentStmt);
5919 					n->objtype = OBJECT_OPFAMILY;
5920 					n->objname = lcons(makeString($7), $5);
5921 					n->objargs = NIL;
5922 					n->comment = $9;
5923 					$$ = (Node *) n;
5924 				}
5925 			| COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
5926 				{
5927 					CommentStmt *n = makeNode(CommentStmt);
5928 					n->objtype = OBJECT_LARGEOBJECT;
5929 					n->objname = list_make1($5);
5930 					n->objargs = NIL;
5931 					n->comment = $7;
5932 					$$ = (Node *) n;
5933 				}
5934 			| COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
5935 				{
5936 					CommentStmt *n = makeNode(CommentStmt);
5937 					n->objtype = OBJECT_CAST;
5938 					n->objname = list_make1($5);
5939 					n->objargs = list_make1($7);
5940 					n->comment = $10;
5941 					$$ = (Node *) n;
5942 				}
5943 			| COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
5944 				{
5945 					CommentStmt *n = makeNode(CommentStmt);
5946 					n->objtype = OBJECT_LANGUAGE;
5947 					n->objname = $5;
5948 					n->objargs = NIL;
5949 					n->comment = $7;
5950 					$$ = (Node *) n;
5951 				}
5952 		;
5953 
5954 comment_type:
5955 			ACCESS METHOD						{ $$ = OBJECT_ACCESS_METHOD; }
5956 			| COLUMN							{ $$ = OBJECT_COLUMN; }
5957 			| DATABASE							{ $$ = OBJECT_DATABASE; }
5958 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
5959 			| INDEX								{ $$ = OBJECT_INDEX; }
5960 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
5961 			| TABLE								{ $$ = OBJECT_TABLE; }
5962 			| VIEW								{ $$ = OBJECT_VIEW; }
5963 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
5964 			| COLLATION							{ $$ = OBJECT_COLLATION; }
5965 			| CONVERSION_P						{ $$ = OBJECT_CONVERSION; }
5966 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
5967 			| EXTENSION							{ $$ = OBJECT_EXTENSION; }
5968 			| ROLE								{ $$ = OBJECT_ROLE; }
5969 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
5970 			| SERVER							{ $$ = OBJECT_FOREIGN_SERVER; }
5971 			| FOREIGN DATA_P WRAPPER			{ $$ = OBJECT_FDW; }
5972 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
5973 			| TEXT_P SEARCH CONFIGURATION		{ $$ = OBJECT_TSCONFIGURATION; }
5974 			| TEXT_P SEARCH DICTIONARY			{ $$ = OBJECT_TSDICTIONARY; }
5975 			| TEXT_P SEARCH PARSER				{ $$ = OBJECT_TSPARSER; }
5976 			| TEXT_P SEARCH TEMPLATE			{ $$ = OBJECT_TSTEMPLATE; }
5977 		;
5978 
5979 comment_text:
5980 			Sconst								{ $$ = $1; }
5981 			| NULL_P							{ $$ = NULL; }
5982 		;
5983 
5984 
5985 /*****************************************************************************
5986  *
5987  *  SECURITY LABEL [FOR <provider>] ON <object> IS <label>
5988  *
5989  *  As with COMMENT ON, <object> can refer to various types of database
5990  *  objects (e.g. TABLE, COLUMN, etc.).
5991  *
5992  *****************************************************************************/
5993 
5994 SecLabelStmt:
5995 			SECURITY LABEL opt_provider ON security_label_type any_name
5996 			IS security_label
5997 				{
5998 					SecLabelStmt *n = makeNode(SecLabelStmt);
5999 					n->provider = $3;
6000 					n->objtype = $5;
6001 					n->objname = $6;
6002 					n->objargs = NIL;
6003 					n->label = $8;
6004 					$$ = (Node *) n;
6005 				}
6006 			| SECURITY LABEL opt_provider ON TYPE_P Typename
6007 			  IS security_label
6008 				{
6009 					SecLabelStmt *n = makeNode(SecLabelStmt);
6010 					n->provider = $3;
6011 					n->objtype = OBJECT_TYPE;
6012 					n->objname = list_make1($6);
6013 					n->objargs = NIL;
6014 					n->label = $8;
6015 					$$ = (Node *) n;
6016 				}
6017 			| SECURITY LABEL opt_provider ON DOMAIN_P Typename
6018 			  IS security_label
6019 				{
6020 					SecLabelStmt *n = makeNode(SecLabelStmt);
6021 					n->provider = $3;
6022 					n->objtype = OBJECT_TYPE;
6023 					n->objname = list_make1($6);
6024 					n->objargs = NIL;
6025 					n->label = $8;
6026 					$$ = (Node *) n;
6027 				}
6028 			| SECURITY LABEL opt_provider ON AGGREGATE func_name aggr_args
6029 			  IS security_label
6030 				{
6031 					SecLabelStmt *n = makeNode(SecLabelStmt);
6032 					n->provider = $3;
6033 					n->objtype = OBJECT_AGGREGATE;
6034 					n->objname = $6;
6035 					n->objargs = extractAggrArgTypes($7);
6036 					n->label = $9;
6037 					$$ = (Node *) n;
6038 				}
6039 			| SECURITY LABEL opt_provider ON FUNCTION func_name func_args
6040 			  IS security_label
6041 				{
6042 					SecLabelStmt *n = makeNode(SecLabelStmt);
6043 					n->provider = $3;
6044 					n->objtype = OBJECT_FUNCTION;
6045 					n->objname = $6;
6046 					n->objargs = extractArgTypes($7);
6047 					n->label = $9;
6048 					$$ = (Node *) n;
6049 				}
6050 			| SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
6051 			  IS security_label
6052 				{
6053 					SecLabelStmt *n = makeNode(SecLabelStmt);
6054 					n->provider = $3;
6055 					n->objtype = OBJECT_LARGEOBJECT;
6056 					n->objname = list_make1($7);
6057 					n->objargs = NIL;
6058 					n->label = $9;
6059 					$$ = (Node *) n;
6060 				}
6061 			| SECURITY LABEL opt_provider ON opt_procedural LANGUAGE any_name
6062 			  IS security_label
6063 				{
6064 					SecLabelStmt *n = makeNode(SecLabelStmt);
6065 					n->provider = $3;
6066 					n->objtype = OBJECT_LANGUAGE;
6067 					n->objname = $7;
6068 					n->objargs = NIL;
6069 					n->label = $9;
6070 					$$ = (Node *) n;
6071 				}
6072 		;
6073 
6074 opt_provider:	FOR NonReservedWord_or_Sconst	{ $$ = $2; }
6075 				| /* empty */					{ $$ = NULL; }
6076 		;
6077 
6078 security_label_type:
6079 			COLUMN								{ $$ = OBJECT_COLUMN; }
6080 			| DATABASE							{ $$ = OBJECT_DATABASE; }
6081 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6082 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6083 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6084 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6085 			| TABLE								{ $$ = OBJECT_TABLE; }
6086 			| ROLE								{ $$ = OBJECT_ROLE; }
6087 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6088 			| VIEW								{ $$ = OBJECT_VIEW; }
6089 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6090 		;
6091 
6092 security_label:	Sconst				{ $$ = $1; }
6093 				| NULL_P			{ $$ = NULL; }
6094 		;
6095 
6096 /*****************************************************************************
6097  *
6098  *		QUERY:
6099  *			fetch/move
6100  *
6101  *****************************************************************************/
6102 
6103 FetchStmt:	FETCH fetch_args
6104 				{
6105 					FetchStmt *n = (FetchStmt *) $2;
6106 					n->ismove = FALSE;
6107 					$$ = (Node *)n;
6108 				}
6109 			| MOVE fetch_args
6110 				{
6111 					FetchStmt *n = (FetchStmt *) $2;
6112 					n->ismove = TRUE;
6113 					$$ = (Node *)n;
6114 				}
6115 		;
6116 
6117 fetch_args:	cursor_name
6118 				{
6119 					FetchStmt *n = makeNode(FetchStmt);
6120 					n->portalname = $1;
6121 					n->direction = FETCH_FORWARD;
6122 					n->howMany = 1;
6123 					$$ = (Node *)n;
6124 				}
6125 			| from_in cursor_name
6126 				{
6127 					FetchStmt *n = makeNode(FetchStmt);
6128 					n->portalname = $2;
6129 					n->direction = FETCH_FORWARD;
6130 					n->howMany = 1;
6131 					$$ = (Node *)n;
6132 				}
6133 			| NEXT opt_from_in cursor_name
6134 				{
6135 					FetchStmt *n = makeNode(FetchStmt);
6136 					n->portalname = $3;
6137 					n->direction = FETCH_FORWARD;
6138 					n->howMany = 1;
6139 					$$ = (Node *)n;
6140 				}
6141 			| PRIOR opt_from_in cursor_name
6142 				{
6143 					FetchStmt *n = makeNode(FetchStmt);
6144 					n->portalname = $3;
6145 					n->direction = FETCH_BACKWARD;
6146 					n->howMany = 1;
6147 					$$ = (Node *)n;
6148 				}
6149 			| FIRST_P opt_from_in cursor_name
6150 				{
6151 					FetchStmt *n = makeNode(FetchStmt);
6152 					n->portalname = $3;
6153 					n->direction = FETCH_ABSOLUTE;
6154 					n->howMany = 1;
6155 					$$ = (Node *)n;
6156 				}
6157 			| LAST_P opt_from_in cursor_name
6158 				{
6159 					FetchStmt *n = makeNode(FetchStmt);
6160 					n->portalname = $3;
6161 					n->direction = FETCH_ABSOLUTE;
6162 					n->howMany = -1;
6163 					$$ = (Node *)n;
6164 				}
6165 			| ABSOLUTE_P SignedIconst opt_from_in cursor_name
6166 				{
6167 					FetchStmt *n = makeNode(FetchStmt);
6168 					n->portalname = $4;
6169 					n->direction = FETCH_ABSOLUTE;
6170 					n->howMany = $2;
6171 					$$ = (Node *)n;
6172 				}
6173 			| RELATIVE_P SignedIconst opt_from_in cursor_name
6174 				{
6175 					FetchStmt *n = makeNode(FetchStmt);
6176 					n->portalname = $4;
6177 					n->direction = FETCH_RELATIVE;
6178 					n->howMany = $2;
6179 					$$ = (Node *)n;
6180 				}
6181 			| SignedIconst opt_from_in cursor_name
6182 				{
6183 					FetchStmt *n = makeNode(FetchStmt);
6184 					n->portalname = $3;
6185 					n->direction = FETCH_FORWARD;
6186 					n->howMany = $1;
6187 					$$ = (Node *)n;
6188 				}
6189 			| ALL opt_from_in cursor_name
6190 				{
6191 					FetchStmt *n = makeNode(FetchStmt);
6192 					n->portalname = $3;
6193 					n->direction = FETCH_FORWARD;
6194 					n->howMany = FETCH_ALL;
6195 					$$ = (Node *)n;
6196 				}
6197 			| FORWARD opt_from_in cursor_name
6198 				{
6199 					FetchStmt *n = makeNode(FetchStmt);
6200 					n->portalname = $3;
6201 					n->direction = FETCH_FORWARD;
6202 					n->howMany = 1;
6203 					$$ = (Node *)n;
6204 				}
6205 			| FORWARD SignedIconst opt_from_in cursor_name
6206 				{
6207 					FetchStmt *n = makeNode(FetchStmt);
6208 					n->portalname = $4;
6209 					n->direction = FETCH_FORWARD;
6210 					n->howMany = $2;
6211 					$$ = (Node *)n;
6212 				}
6213 			| FORWARD ALL opt_from_in cursor_name
6214 				{
6215 					FetchStmt *n = makeNode(FetchStmt);
6216 					n->portalname = $4;
6217 					n->direction = FETCH_FORWARD;
6218 					n->howMany = FETCH_ALL;
6219 					$$ = (Node *)n;
6220 				}
6221 			| BACKWARD opt_from_in cursor_name
6222 				{
6223 					FetchStmt *n = makeNode(FetchStmt);
6224 					n->portalname = $3;
6225 					n->direction = FETCH_BACKWARD;
6226 					n->howMany = 1;
6227 					$$ = (Node *)n;
6228 				}
6229 			| BACKWARD SignedIconst opt_from_in cursor_name
6230 				{
6231 					FetchStmt *n = makeNode(FetchStmt);
6232 					n->portalname = $4;
6233 					n->direction = FETCH_BACKWARD;
6234 					n->howMany = $2;
6235 					$$ = (Node *)n;
6236 				}
6237 			| BACKWARD ALL opt_from_in cursor_name
6238 				{
6239 					FetchStmt *n = makeNode(FetchStmt);
6240 					n->portalname = $4;
6241 					n->direction = FETCH_BACKWARD;
6242 					n->howMany = FETCH_ALL;
6243 					$$ = (Node *)n;
6244 				}
6245 		;
6246 
6247 from_in:	FROM									{}
6248 			| IN_P									{}
6249 		;
6250 
6251 opt_from_in:	from_in								{}
6252 			| /* EMPTY */							{}
6253 		;
6254 
6255 
6256 /*****************************************************************************
6257  *
6258  * GRANT and REVOKE statements
6259  *
6260  *****************************************************************************/
6261 
6262 GrantStmt:	GRANT privileges ON privilege_target TO grantee_list
6263 			opt_grant_grant_option
6264 				{
6265 					GrantStmt *n = makeNode(GrantStmt);
6266 					n->is_grant = true;
6267 					n->privileges = $2;
6268 					n->targtype = ($4)->targtype;
6269 					n->objtype = ($4)->objtype;
6270 					n->objects = ($4)->objs;
6271 					n->grantees = $6;
6272 					n->grant_option = $7;
6273 					$$ = (Node*)n;
6274 				}
6275 		;
6276 
6277 RevokeStmt:
6278 			REVOKE privileges ON privilege_target
6279 			FROM grantee_list opt_drop_behavior
6280 				{
6281 					GrantStmt *n = makeNode(GrantStmt);
6282 					n->is_grant = false;
6283 					n->grant_option = false;
6284 					n->privileges = $2;
6285 					n->targtype = ($4)->targtype;
6286 					n->objtype = ($4)->objtype;
6287 					n->objects = ($4)->objs;
6288 					n->grantees = $6;
6289 					n->behavior = $7;
6290 					$$ = (Node *)n;
6291 				}
6292 			| REVOKE GRANT OPTION FOR privileges ON privilege_target
6293 			FROM grantee_list opt_drop_behavior
6294 				{
6295 					GrantStmt *n = makeNode(GrantStmt);
6296 					n->is_grant = false;
6297 					n->grant_option = true;
6298 					n->privileges = $5;
6299 					n->targtype = ($7)->targtype;
6300 					n->objtype = ($7)->objtype;
6301 					n->objects = ($7)->objs;
6302 					n->grantees = $9;
6303 					n->behavior = $10;
6304 					$$ = (Node *)n;
6305 				}
6306 		;
6307 
6308 
6309 /*
6310  * Privilege names are represented as strings; the validity of the privilege
6311  * names gets checked at execution.  This is a bit annoying but we have little
6312  * choice because of the syntactic conflict with lists of role names in
6313  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
6314  * production any reserved keywords that need to be usable as privilege names.
6315  */
6316 
6317 /* either ALL [PRIVILEGES] or a list of individual privileges */
6318 privileges: privilege_list
6319 				{ $$ = $1; }
6320 			| ALL
6321 				{ $$ = NIL; }
6322 			| ALL PRIVILEGES
6323 				{ $$ = NIL; }
6324 			| ALL '(' columnList ')'
6325 				{
6326 					AccessPriv *n = makeNode(AccessPriv);
6327 					n->priv_name = NULL;
6328 					n->cols = $3;
6329 					$$ = list_make1(n);
6330 				}
6331 			| ALL PRIVILEGES '(' columnList ')'
6332 				{
6333 					AccessPriv *n = makeNode(AccessPriv);
6334 					n->priv_name = NULL;
6335 					n->cols = $4;
6336 					$$ = list_make1(n);
6337 				}
6338 		;
6339 
6340 privilege_list:	privilege							{ $$ = list_make1($1); }
6341 			| privilege_list ',' privilege			{ $$ = lappend($1, $3); }
6342 		;
6343 
6344 privilege:	SELECT opt_column_list
6345 			{
6346 				AccessPriv *n = makeNode(AccessPriv);
6347 				n->priv_name = pstrdup($1);
6348 				n->cols = $2;
6349 				$$ = n;
6350 			}
6351 		| REFERENCES opt_column_list
6352 			{
6353 				AccessPriv *n = makeNode(AccessPriv);
6354 				n->priv_name = pstrdup($1);
6355 				n->cols = $2;
6356 				$$ = n;
6357 			}
6358 		| CREATE opt_column_list
6359 			{
6360 				AccessPriv *n = makeNode(AccessPriv);
6361 				n->priv_name = pstrdup($1);
6362 				n->cols = $2;
6363 				$$ = n;
6364 			}
6365 		| ColId opt_column_list
6366 			{
6367 				AccessPriv *n = makeNode(AccessPriv);
6368 				n->priv_name = $1;
6369 				n->cols = $2;
6370 				$$ = n;
6371 			}
6372 		;
6373 
6374 
6375 /* Don't bother trying to fold the first two rules into one using
6376  * opt_table.  You're going to get conflicts.
6377  */
6378 privilege_target:
6379 			qualified_name_list
6380 				{
6381 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6382 					n->targtype = ACL_TARGET_OBJECT;
6383 					n->objtype = ACL_OBJECT_RELATION;
6384 					n->objs = $1;
6385 					$$ = n;
6386 				}
6387 			| TABLE qualified_name_list
6388 				{
6389 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6390 					n->targtype = ACL_TARGET_OBJECT;
6391 					n->objtype = ACL_OBJECT_RELATION;
6392 					n->objs = $2;
6393 					$$ = n;
6394 				}
6395 			| SEQUENCE qualified_name_list
6396 				{
6397 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6398 					n->targtype = ACL_TARGET_OBJECT;
6399 					n->objtype = ACL_OBJECT_SEQUENCE;
6400 					n->objs = $2;
6401 					$$ = n;
6402 				}
6403 			| FOREIGN DATA_P WRAPPER name_list
6404 				{
6405 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6406 					n->targtype = ACL_TARGET_OBJECT;
6407 					n->objtype = ACL_OBJECT_FDW;
6408 					n->objs = $4;
6409 					$$ = n;
6410 				}
6411 			| FOREIGN SERVER name_list
6412 				{
6413 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6414 					n->targtype = ACL_TARGET_OBJECT;
6415 					n->objtype = ACL_OBJECT_FOREIGN_SERVER;
6416 					n->objs = $3;
6417 					$$ = n;
6418 				}
6419 			| FUNCTION function_with_argtypes_list
6420 				{
6421 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6422 					n->targtype = ACL_TARGET_OBJECT;
6423 					n->objtype = ACL_OBJECT_FUNCTION;
6424 					n->objs = $2;
6425 					$$ = n;
6426 				}
6427 			| DATABASE name_list
6428 				{
6429 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6430 					n->targtype = ACL_TARGET_OBJECT;
6431 					n->objtype = ACL_OBJECT_DATABASE;
6432 					n->objs = $2;
6433 					$$ = n;
6434 				}
6435 			| DOMAIN_P any_name_list
6436 				{
6437 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6438 					n->targtype = ACL_TARGET_OBJECT;
6439 					n->objtype = ACL_OBJECT_DOMAIN;
6440 					n->objs = $2;
6441 					$$ = n;
6442 				}
6443 			| LANGUAGE name_list
6444 				{
6445 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6446 					n->targtype = ACL_TARGET_OBJECT;
6447 					n->objtype = ACL_OBJECT_LANGUAGE;
6448 					n->objs = $2;
6449 					$$ = n;
6450 				}
6451 			| LARGE_P OBJECT_P NumericOnly_list
6452 				{
6453 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6454 					n->targtype = ACL_TARGET_OBJECT;
6455 					n->objtype = ACL_OBJECT_LARGEOBJECT;
6456 					n->objs = $3;
6457 					$$ = n;
6458 				}
6459 			| SCHEMA name_list
6460 				{
6461 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6462 					n->targtype = ACL_TARGET_OBJECT;
6463 					n->objtype = ACL_OBJECT_NAMESPACE;
6464 					n->objs = $2;
6465 					$$ = n;
6466 				}
6467 			| TABLESPACE name_list
6468 				{
6469 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6470 					n->targtype = ACL_TARGET_OBJECT;
6471 					n->objtype = ACL_OBJECT_TABLESPACE;
6472 					n->objs = $2;
6473 					$$ = n;
6474 				}
6475 			| TYPE_P any_name_list
6476 				{
6477 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6478 					n->targtype = ACL_TARGET_OBJECT;
6479 					n->objtype = ACL_OBJECT_TYPE;
6480 					n->objs = $2;
6481 					$$ = n;
6482 				}
6483 			| ALL TABLES IN_P SCHEMA name_list
6484 				{
6485 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6486 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
6487 					n->objtype = ACL_OBJECT_RELATION;
6488 					n->objs = $5;
6489 					$$ = n;
6490 				}
6491 			| ALL SEQUENCES IN_P SCHEMA name_list
6492 				{
6493 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6494 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
6495 					n->objtype = ACL_OBJECT_SEQUENCE;
6496 					n->objs = $5;
6497 					$$ = n;
6498 				}
6499 			| ALL FUNCTIONS IN_P SCHEMA name_list
6500 				{
6501 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6502 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
6503 					n->objtype = ACL_OBJECT_FUNCTION;
6504 					n->objs = $5;
6505 					$$ = n;
6506 				}
6507 		;
6508 
6509 
6510 grantee_list:
6511 			grantee									{ $$ = list_make1($1); }
6512 			| grantee_list ',' grantee				{ $$ = lappend($1, $3); }
6513 		;
6514 
6515 grantee:
6516 			RoleSpec								{ $$ = $1; }
6517 			| GROUP_P RoleSpec						{ $$ = $2; }
6518 		;
6519 
6520 
6521 opt_grant_grant_option:
6522 			WITH GRANT OPTION { $$ = TRUE; }
6523 			| /*EMPTY*/ { $$ = FALSE; }
6524 		;
6525 
6526 function_with_argtypes_list:
6527 			function_with_argtypes					{ $$ = list_make1($1); }
6528 			| function_with_argtypes_list ',' function_with_argtypes
6529 													{ $$ = lappend($1, $3); }
6530 		;
6531 
6532 function_with_argtypes:
6533 			func_name func_args
6534 				{
6535 					FuncWithArgs *n = makeNode(FuncWithArgs);
6536 					n->funcname = $1;
6537 					n->funcargs = extractArgTypes($2);
6538 					$$ = n;
6539 				}
6540 		;
6541 
6542 /*****************************************************************************
6543  *
6544  * GRANT and REVOKE ROLE statements
6545  *
6546  *****************************************************************************/
6547 
6548 GrantRoleStmt:
6549 			GRANT privilege_list TO role_list opt_grant_admin_option opt_granted_by
6550 				{
6551 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
6552 					n->is_grant = true;
6553 					n->granted_roles = $2;
6554 					n->grantee_roles = $4;
6555 					n->admin_opt = $5;
6556 					n->grantor = $6;
6557 					$$ = (Node*)n;
6558 				}
6559 		;
6560 
6561 RevokeRoleStmt:
6562 			REVOKE privilege_list FROM role_list opt_granted_by opt_drop_behavior
6563 				{
6564 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
6565 					n->is_grant = false;
6566 					n->admin_opt = false;
6567 					n->granted_roles = $2;
6568 					n->grantee_roles = $4;
6569 					n->behavior = $6;
6570 					$$ = (Node*)n;
6571 				}
6572 			| REVOKE ADMIN OPTION FOR privilege_list FROM role_list opt_granted_by opt_drop_behavior
6573 				{
6574 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
6575 					n->is_grant = false;
6576 					n->admin_opt = true;
6577 					n->granted_roles = $5;
6578 					n->grantee_roles = $7;
6579 					n->behavior = $9;
6580 					$$ = (Node*)n;
6581 				}
6582 		;
6583 
6584 opt_grant_admin_option: WITH ADMIN OPTION				{ $$ = TRUE; }
6585 			| /*EMPTY*/									{ $$ = FALSE; }
6586 		;
6587 
6588 opt_granted_by: GRANTED BY RoleSpec						{ $$ = $3; }
6589 			| /*EMPTY*/									{ $$ = NULL; }
6590 		;
6591 
6592 /*****************************************************************************
6593  *
6594  * ALTER DEFAULT PRIVILEGES statement
6595  *
6596  *****************************************************************************/
6597 
6598 AlterDefaultPrivilegesStmt:
6599 			ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
6600 				{
6601 					AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
6602 					n->options = $4;
6603 					n->action = (GrantStmt *) $5;
6604 					$$ = (Node*)n;
6605 				}
6606 		;
6607 
6608 DefACLOptionList:
6609 			DefACLOptionList DefACLOption			{ $$ = lappend($1, $2); }
6610 			| /* EMPTY */							{ $$ = NIL; }
6611 		;
6612 
6613 DefACLOption:
6614 			IN_P SCHEMA name_list
6615 				{
6616 					$$ = makeDefElem("schemas", (Node *)$3);
6617 				}
6618 			| FOR ROLE role_list
6619 				{
6620 					$$ = makeDefElem("roles", (Node *)$3);
6621 				}
6622 			| FOR USER role_list
6623 				{
6624 					$$ = makeDefElem("roles", (Node *)$3);
6625 				}
6626 		;
6627 
6628 /*
6629  * This should match GRANT/REVOKE, except that individual target objects
6630  * are not mentioned and we only allow a subset of object types.
6631  */
6632 DefACLAction:
6633 			GRANT privileges ON defacl_privilege_target TO grantee_list
6634 			opt_grant_grant_option
6635 				{
6636 					GrantStmt *n = makeNode(GrantStmt);
6637 					n->is_grant = true;
6638 					n->privileges = $2;
6639 					n->targtype = ACL_TARGET_DEFAULTS;
6640 					n->objtype = $4;
6641 					n->objects = NIL;
6642 					n->grantees = $6;
6643 					n->grant_option = $7;
6644 					$$ = (Node*)n;
6645 				}
6646 			| REVOKE privileges ON defacl_privilege_target
6647 			FROM grantee_list opt_drop_behavior
6648 				{
6649 					GrantStmt *n = makeNode(GrantStmt);
6650 					n->is_grant = false;
6651 					n->grant_option = false;
6652 					n->privileges = $2;
6653 					n->targtype = ACL_TARGET_DEFAULTS;
6654 					n->objtype = $4;
6655 					n->objects = NIL;
6656 					n->grantees = $6;
6657 					n->behavior = $7;
6658 					$$ = (Node *)n;
6659 				}
6660 			| REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
6661 			FROM grantee_list opt_drop_behavior
6662 				{
6663 					GrantStmt *n = makeNode(GrantStmt);
6664 					n->is_grant = false;
6665 					n->grant_option = true;
6666 					n->privileges = $5;
6667 					n->targtype = ACL_TARGET_DEFAULTS;
6668 					n->objtype = $7;
6669 					n->objects = NIL;
6670 					n->grantees = $9;
6671 					n->behavior = $10;
6672 					$$ = (Node *)n;
6673 				}
6674 		;
6675 
6676 defacl_privilege_target:
6677 			TABLES			{ $$ = ACL_OBJECT_RELATION; }
6678 			| FUNCTIONS		{ $$ = ACL_OBJECT_FUNCTION; }
6679 			| SEQUENCES		{ $$ = ACL_OBJECT_SEQUENCE; }
6680 			| TYPES_P		{ $$ = ACL_OBJECT_TYPE; }
6681 		;
6682 
6683 
6684 /*****************************************************************************
6685  *
6686  *		QUERY: CREATE INDEX
6687  *
6688  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
6689  * willing to make TABLESPACE a fully reserved word.
6690  *****************************************************************************/
6691 
6692 IndexStmt:	CREATE opt_unique INDEX opt_concurrently opt_index_name
6693 			ON qualified_name access_method_clause '(' index_params ')'
6694 			opt_reloptions OptTableSpace where_clause
6695 				{
6696 					IndexStmt *n = makeNode(IndexStmt);
6697 					n->unique = $2;
6698 					n->concurrent = $4;
6699 					n->idxname = $5;
6700 					n->relation = $7;
6701 					n->accessMethod = $8;
6702 					n->indexParams = $10;
6703 					n->options = $12;
6704 					n->tableSpace = $13;
6705 					n->whereClause = $14;
6706 					n->excludeOpNames = NIL;
6707 					n->idxcomment = NULL;
6708 					n->indexOid = InvalidOid;
6709 					n->oldNode = InvalidOid;
6710 					n->primary = false;
6711 					n->isconstraint = false;
6712 					n->deferrable = false;
6713 					n->initdeferred = false;
6714 					n->transformed = false;
6715 					n->if_not_exists = false;
6716 					$$ = (Node *)n;
6717 				}
6718 			| CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS index_name
6719 			ON qualified_name access_method_clause '(' index_params ')'
6720 			opt_reloptions OptTableSpace where_clause
6721 				{
6722 					IndexStmt *n = makeNode(IndexStmt);
6723 					n->unique = $2;
6724 					n->concurrent = $4;
6725 					n->idxname = $8;
6726 					n->relation = $10;
6727 					n->accessMethod = $11;
6728 					n->indexParams = $13;
6729 					n->options = $15;
6730 					n->tableSpace = $16;
6731 					n->whereClause = $17;
6732 					n->excludeOpNames = NIL;
6733 					n->idxcomment = NULL;
6734 					n->indexOid = InvalidOid;
6735 					n->oldNode = InvalidOid;
6736 					n->primary = false;
6737 					n->isconstraint = false;
6738 					n->deferrable = false;
6739 					n->initdeferred = false;
6740 					n->transformed = false;
6741 					n->if_not_exists = true;
6742 					$$ = (Node *)n;
6743 				}
6744 		;
6745 
6746 opt_unique:
6747 			UNIQUE									{ $$ = TRUE; }
6748 			| /*EMPTY*/								{ $$ = FALSE; }
6749 		;
6750 
6751 opt_concurrently:
6752 			CONCURRENTLY							{ $$ = TRUE; }
6753 			| /*EMPTY*/								{ $$ = FALSE; }
6754 		;
6755 
6756 opt_index_name:
6757 			index_name								{ $$ = $1; }
6758 			| /*EMPTY*/								{ $$ = NULL; }
6759 		;
6760 
6761 access_method_clause:
6762 			USING access_method						{ $$ = $2; }
6763 			| /*EMPTY*/								{ $$ = DEFAULT_INDEX_TYPE; }
6764 		;
6765 
6766 index_params:	index_elem							{ $$ = list_make1($1); }
6767 			| index_params ',' index_elem			{ $$ = lappend($1, $3); }
6768 		;
6769 
6770 /*
6771  * Index attributes can be either simple column references, or arbitrary
6772  * expressions in parens.  For backwards-compatibility reasons, we allow
6773  * an expression that's just a function call to be written without parens.
6774  */
6775 index_elem:	ColId opt_collate opt_class opt_asc_desc opt_nulls_order
6776 				{
6777 					$$ = makeNode(IndexElem);
6778 					$$->name = $1;
6779 					$$->expr = NULL;
6780 					$$->indexcolname = NULL;
6781 					$$->collation = $2;
6782 					$$->opclass = $3;
6783 					$$->ordering = $4;
6784 					$$->nulls_ordering = $5;
6785 				}
6786 			| func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order
6787 				{
6788 					$$ = makeNode(IndexElem);
6789 					$$->name = NULL;
6790 					$$->expr = $1;
6791 					$$->indexcolname = NULL;
6792 					$$->collation = $2;
6793 					$$->opclass = $3;
6794 					$$->ordering = $4;
6795 					$$->nulls_ordering = $5;
6796 				}
6797 			| '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order
6798 				{
6799 					$$ = makeNode(IndexElem);
6800 					$$->name = NULL;
6801 					$$->expr = $2;
6802 					$$->indexcolname = NULL;
6803 					$$->collation = $4;
6804 					$$->opclass = $5;
6805 					$$->ordering = $6;
6806 					$$->nulls_ordering = $7;
6807 				}
6808 		;
6809 
6810 opt_collate: COLLATE any_name						{ $$ = $2; }
6811 			| /*EMPTY*/								{ $$ = NIL; }
6812 		;
6813 
6814 opt_class:	any_name								{ $$ = $1; }
6815 			| /*EMPTY*/								{ $$ = NIL; }
6816 		;
6817 
6818 opt_asc_desc: ASC							{ $$ = SORTBY_ASC; }
6819 			| DESC							{ $$ = SORTBY_DESC; }
6820 			| /*EMPTY*/						{ $$ = SORTBY_DEFAULT; }
6821 		;
6822 
6823 opt_nulls_order: NULLS_LA FIRST_P			{ $$ = SORTBY_NULLS_FIRST; }
6824 			| NULLS_LA LAST_P				{ $$ = SORTBY_NULLS_LAST; }
6825 			| /*EMPTY*/						{ $$ = SORTBY_NULLS_DEFAULT; }
6826 		;
6827 
6828 
6829 /*****************************************************************************
6830  *
6831  *		QUERY:
6832  *				create [or replace] function <fname>
6833  *						[(<type-1> { , <type-n>})]
6834  *						returns <type-r>
6835  *						as <filename or code in language as appropriate>
6836  *						language <lang> [with parameters]
6837  *
6838  *****************************************************************************/
6839 
6840 CreateFunctionStmt:
6841 			CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
6842 			RETURNS func_return createfunc_opt_list opt_definition
6843 				{
6844 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
6845 					n->replace = $2;
6846 					n->funcname = $4;
6847 					n->parameters = $5;
6848 					n->returnType = $7;
6849 					n->options = $8;
6850 					n->withClause = $9;
6851 					$$ = (Node *)n;
6852 				}
6853 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
6854 			  RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
6855 				{
6856 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
6857 					n->replace = $2;
6858 					n->funcname = $4;
6859 					n->parameters = mergeTableFuncParameters($5, $9);
6860 					n->returnType = TableFuncTypeName($9);
6861 					n->returnType->location = @7;
6862 					n->options = $11;
6863 					n->withClause = $12;
6864 					$$ = (Node *)n;
6865 				}
6866 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
6867 			  createfunc_opt_list opt_definition
6868 				{
6869 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
6870 					n->replace = $2;
6871 					n->funcname = $4;
6872 					n->parameters = $5;
6873 					n->returnType = NULL;
6874 					n->options = $6;
6875 					n->withClause = $7;
6876 					$$ = (Node *)n;
6877 				}
6878 		;
6879 
6880 opt_or_replace:
6881 			OR REPLACE								{ $$ = TRUE; }
6882 			| /*EMPTY*/								{ $$ = FALSE; }
6883 		;
6884 
6885 func_args:	'(' func_args_list ')'					{ $$ = $2; }
6886 			| '(' ')'								{ $$ = NIL; }
6887 		;
6888 
6889 func_args_list:
6890 			func_arg								{ $$ = list_make1($1); }
6891 			| func_args_list ',' func_arg			{ $$ = lappend($1, $3); }
6892 		;
6893 
6894 /*
6895  * func_args_with_defaults is separate because we only want to accept
6896  * defaults in CREATE FUNCTION, not in ALTER etc.
6897  */
6898 func_args_with_defaults:
6899 		'(' func_args_with_defaults_list ')'		{ $$ = $2; }
6900 		| '(' ')'									{ $$ = NIL; }
6901 		;
6902 
6903 func_args_with_defaults_list:
6904 		func_arg_with_default						{ $$ = list_make1($1); }
6905 		| func_args_with_defaults_list ',' func_arg_with_default
6906 													{ $$ = lappend($1, $3); }
6907 		;
6908 
6909 /*
6910  * The style with arg_class first is SQL99 standard, but Oracle puts
6911  * param_name first; accept both since it's likely people will try both
6912  * anyway.  Don't bother trying to save productions by letting arg_class
6913  * have an empty alternative ... you'll get shift/reduce conflicts.
6914  *
6915  * We can catch over-specified arguments here if we want to,
6916  * but for now better to silently swallow typmod, etc.
6917  * - thomas 2000-03-22
6918  */
6919 func_arg:
6920 			arg_class param_name func_type
6921 				{
6922 					FunctionParameter *n = makeNode(FunctionParameter);
6923 					n->name = $2;
6924 					n->argType = $3;
6925 					n->mode = $1;
6926 					n->defexpr = NULL;
6927 					$$ = n;
6928 				}
6929 			| param_name arg_class func_type
6930 				{
6931 					FunctionParameter *n = makeNode(FunctionParameter);
6932 					n->name = $1;
6933 					n->argType = $3;
6934 					n->mode = $2;
6935 					n->defexpr = NULL;
6936 					$$ = n;
6937 				}
6938 			| param_name func_type
6939 				{
6940 					FunctionParameter *n = makeNode(FunctionParameter);
6941 					n->name = $1;
6942 					n->argType = $2;
6943 					n->mode = FUNC_PARAM_IN;
6944 					n->defexpr = NULL;
6945 					$$ = n;
6946 				}
6947 			| arg_class func_type
6948 				{
6949 					FunctionParameter *n = makeNode(FunctionParameter);
6950 					n->name = NULL;
6951 					n->argType = $2;
6952 					n->mode = $1;
6953 					n->defexpr = NULL;
6954 					$$ = n;
6955 				}
6956 			| func_type
6957 				{
6958 					FunctionParameter *n = makeNode(FunctionParameter);
6959 					n->name = NULL;
6960 					n->argType = $1;
6961 					n->mode = FUNC_PARAM_IN;
6962 					n->defexpr = NULL;
6963 					$$ = n;
6964 				}
6965 		;
6966 
6967 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
6968 arg_class:	IN_P								{ $$ = FUNC_PARAM_IN; }
6969 			| OUT_P								{ $$ = FUNC_PARAM_OUT; }
6970 			| INOUT								{ $$ = FUNC_PARAM_INOUT; }
6971 			| IN_P OUT_P						{ $$ = FUNC_PARAM_INOUT; }
6972 			| VARIADIC							{ $$ = FUNC_PARAM_VARIADIC; }
6973 		;
6974 
6975 /*
6976  * Ideally param_name should be ColId, but that causes too many conflicts.
6977  */
6978 param_name:	type_function_name
6979 		;
6980 
6981 func_return:
6982 			func_type
6983 				{
6984 					/* We can catch over-specified results here if we want to,
6985 					 * but for now better to silently swallow typmod, etc.
6986 					 * - thomas 2000-03-22
6987 					 */
6988 					$$ = $1;
6989 				}
6990 		;
6991 
6992 /*
6993  * We would like to make the %TYPE productions here be ColId attrs etc,
6994  * but that causes reduce/reduce conflicts.  type_function_name
6995  * is next best choice.
6996  */
6997 func_type:	Typename								{ $$ = $1; }
6998 			| type_function_name attrs '%' TYPE_P
6999 				{
7000 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7001 					$$->pct_type = true;
7002 					$$->location = @1;
7003 				}
7004 			| SETOF type_function_name attrs '%' TYPE_P
7005 				{
7006 					$$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
7007 					$$->pct_type = true;
7008 					$$->setof = TRUE;
7009 					$$->location = @2;
7010 				}
7011 		;
7012 
7013 func_arg_with_default:
7014 		func_arg
7015 				{
7016 					$$ = $1;
7017 				}
7018 		| func_arg DEFAULT a_expr
7019 				{
7020 					$$ = $1;
7021 					$$->defexpr = $3;
7022 				}
7023 		| func_arg '=' a_expr
7024 				{
7025 					$$ = $1;
7026 					$$->defexpr = $3;
7027 				}
7028 		;
7029 
7030 /* Aggregate args can be most things that function args can be */
7031 aggr_arg:	func_arg
7032 				{
7033 					if (!($1->mode == FUNC_PARAM_IN ||
7034 						  $1->mode == FUNC_PARAM_VARIADIC))
7035 						ereport(ERROR,
7036 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7037 								 errmsg("aggregates cannot have output arguments"),
7038 								 parser_errposition(@1)));
7039 					$$ = $1;
7040 				}
7041 		;
7042 
7043 /*
7044  * The SQL standard offers no guidance on how to declare aggregate argument
7045  * lists, since it doesn't have CREATE AGGREGATE etc.  We accept these cases:
7046  *
7047  * (*)									- normal agg with no args
7048  * (aggr_arg,...)						- normal agg with args
7049  * (ORDER BY aggr_arg,...)				- ordered-set agg with no direct args
7050  * (aggr_arg,... ORDER BY aggr_arg,...)	- ordered-set agg with direct args
7051  *
7052  * The zero-argument case is spelled with '*' for consistency with COUNT(*).
7053  *
7054  * An additional restriction is that if the direct-args list ends in a
7055  * VARIADIC item, the ordered-args list must contain exactly one item that
7056  * is also VARIADIC with the same type.  This allows us to collapse the two
7057  * VARIADIC items into one, which is necessary to represent the aggregate in
7058  * pg_proc.  We check this at the grammar stage so that we can return a list
7059  * in which the second VARIADIC item is already discarded, avoiding extra work
7060  * in cases such as DROP AGGREGATE.
7061  *
7062  * The return value of this production is a two-element list, in which the
7063  * first item is a sublist of FunctionParameter nodes (with any duplicate
7064  * VARIADIC item already dropped, as per above) and the second is an integer
7065  * Value node, containing -1 if there was no ORDER BY and otherwise the number
7066  * of argument declarations before the ORDER BY.  (If this number is equal
7067  * to the first sublist's length, then we dropped a duplicate VARIADIC item.)
7068  * This representation is passed as-is to CREATE AGGREGATE; for operations
7069  * on existing aggregates, we can just apply extractArgTypes to the first
7070  * sublist.
7071  */
7072 aggr_args:	'(' '*' ')'
7073 				{
7074 					$$ = list_make2(NIL, makeInteger(-1));
7075 				}
7076 			| '(' aggr_args_list ')'
7077 				{
7078 					$$ = list_make2($2, makeInteger(-1));
7079 				}
7080 			| '(' ORDER BY aggr_args_list ')'
7081 				{
7082 					$$ = list_make2($4, makeInteger(0));
7083 				}
7084 			| '(' aggr_args_list ORDER BY aggr_args_list ')'
7085 				{
7086 					/* this is the only case requiring consistency checking */
7087 					$$ = makeOrderedSetArgs($2, $5, yyscanner);
7088 				}
7089 		;
7090 
7091 aggr_args_list:
7092 			aggr_arg								{ $$ = list_make1($1); }
7093 			| aggr_args_list ',' aggr_arg			{ $$ = lappend($1, $3); }
7094 		;
7095 
7096 createfunc_opt_list:
7097 			/* Must be at least one to prevent conflict */
7098 			createfunc_opt_item						{ $$ = list_make1($1); }
7099 			| createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
7100 	;
7101 
7102 /*
7103  * Options common to both CREATE FUNCTION and ALTER FUNCTION
7104  */
7105 common_func_opt_item:
7106 			CALLED ON NULL_P INPUT_P
7107 				{
7108 					$$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
7109 				}
7110 			| RETURNS NULL_P ON NULL_P INPUT_P
7111 				{
7112 					$$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
7113 				}
7114 			| STRICT_P
7115 				{
7116 					$$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
7117 				}
7118 			| IMMUTABLE
7119 				{
7120 					$$ = makeDefElem("volatility", (Node *)makeString("immutable"));
7121 				}
7122 			| STABLE
7123 				{
7124 					$$ = makeDefElem("volatility", (Node *)makeString("stable"));
7125 				}
7126 			| VOLATILE
7127 				{
7128 					$$ = makeDefElem("volatility", (Node *)makeString("volatile"));
7129 				}
7130 			| EXTERNAL SECURITY DEFINER
7131 				{
7132 					$$ = makeDefElem("security", (Node *)makeInteger(TRUE));
7133 				}
7134 			| EXTERNAL SECURITY INVOKER
7135 				{
7136 					$$ = makeDefElem("security", (Node *)makeInteger(FALSE));
7137 				}
7138 			| SECURITY DEFINER
7139 				{
7140 					$$ = makeDefElem("security", (Node *)makeInteger(TRUE));
7141 				}
7142 			| SECURITY INVOKER
7143 				{
7144 					$$ = makeDefElem("security", (Node *)makeInteger(FALSE));
7145 				}
7146 			| LEAKPROOF
7147 				{
7148 					$$ = makeDefElem("leakproof", (Node *)makeInteger(TRUE));
7149 				}
7150 			| NOT LEAKPROOF
7151 				{
7152 					$$ = makeDefElem("leakproof", (Node *)makeInteger(FALSE));
7153 				}
7154 			| COST NumericOnly
7155 				{
7156 					$$ = makeDefElem("cost", (Node *)$2);
7157 				}
7158 			| ROWS NumericOnly
7159 				{
7160 					$$ = makeDefElem("rows", (Node *)$2);
7161 				}
7162 			| FunctionSetResetClause
7163 				{
7164 					/* we abuse the normal content of a DefElem here */
7165 					$$ = makeDefElem("set", (Node *)$1);
7166 				}
7167 			| PARALLEL ColId
7168 				{
7169 					$$ = makeDefElem("parallel", (Node *)makeString($2));
7170 				}
7171 		;
7172 
7173 createfunc_opt_item:
7174 			AS func_as
7175 				{
7176 					$$ = makeDefElem("as", (Node *)$2);
7177 				}
7178 			| LANGUAGE NonReservedWord_or_Sconst
7179 				{
7180 					$$ = makeDefElem("language", (Node *)makeString($2));
7181 				}
7182 			| TRANSFORM transform_type_list
7183 				{
7184 					$$ = makeDefElem("transform", (Node *)$2);
7185 				}
7186 			| WINDOW
7187 				{
7188 					$$ = makeDefElem("window", (Node *)makeInteger(TRUE));
7189 				}
7190 			| common_func_opt_item
7191 				{
7192 					$$ = $1;
7193 				}
7194 		;
7195 
7196 func_as:	Sconst						{ $$ = list_make1(makeString($1)); }
7197 			| Sconst ',' Sconst
7198 				{
7199 					$$ = list_make2(makeString($1), makeString($3));
7200 				}
7201 		;
7202 
7203 transform_type_list:
7204 			FOR TYPE_P Typename { $$ = list_make1($3); }
7205 			| transform_type_list ',' FOR TYPE_P Typename { $$ = lappend($1, $5); }
7206 		;
7207 
7208 opt_definition:
7209 			WITH definition							{ $$ = $2; }
7210 			| /*EMPTY*/								{ $$ = NIL; }
7211 		;
7212 
7213 table_func_column:	param_name func_type
7214 				{
7215 					FunctionParameter *n = makeNode(FunctionParameter);
7216 					n->name = $1;
7217 					n->argType = $2;
7218 					n->mode = FUNC_PARAM_TABLE;
7219 					n->defexpr = NULL;
7220 					$$ = n;
7221 				}
7222 		;
7223 
7224 table_func_column_list:
7225 			table_func_column
7226 				{
7227 					$$ = list_make1($1);
7228 				}
7229 			| table_func_column_list ',' table_func_column
7230 				{
7231 					$$ = lappend($1, $3);
7232 				}
7233 		;
7234 
7235 /*****************************************************************************
7236  * ALTER FUNCTION
7237  *
7238  * RENAME and OWNER subcommands are already provided by the generic
7239  * ALTER infrastructure, here we just specify alterations that can
7240  * only be applied to functions.
7241  *
7242  *****************************************************************************/
7243 AlterFunctionStmt:
7244 			ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
7245 				{
7246 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
7247 					n->func = $3;
7248 					n->actions = $4;
7249 					$$ = (Node *) n;
7250 				}
7251 		;
7252 
7253 alterfunc_opt_list:
7254 			/* At least one option must be specified */
7255 			common_func_opt_item					{ $$ = list_make1($1); }
7256 			| alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
7257 		;
7258 
7259 /* Ignored, merely for SQL compliance */
7260 opt_restrict:
7261 			RESTRICT
7262 			| /* EMPTY */
7263 		;
7264 
7265 
7266 /*****************************************************************************
7267  *
7268  *		QUERY:
7269  *
7270  *		DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
7271  *		DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
7272  *		DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
7273  *
7274  *****************************************************************************/
7275 
7276 RemoveFuncStmt:
7277 			DROP FUNCTION func_name func_args opt_drop_behavior
7278 				{
7279 					DropStmt *n = makeNode(DropStmt);
7280 					n->removeType = OBJECT_FUNCTION;
7281 					n->objects = list_make1($3);
7282 					n->arguments = list_make1(extractArgTypes($4));
7283 					n->behavior = $5;
7284 					n->missing_ok = false;
7285 					n->concurrent = false;
7286 					$$ = (Node *)n;
7287 				}
7288 			| DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
7289 				{
7290 					DropStmt *n = makeNode(DropStmt);
7291 					n->removeType = OBJECT_FUNCTION;
7292 					n->objects = list_make1($5);
7293 					n->arguments = list_make1(extractArgTypes($6));
7294 					n->behavior = $7;
7295 					n->missing_ok = true;
7296 					n->concurrent = false;
7297 					$$ = (Node *)n;
7298 				}
7299 		;
7300 
7301 RemoveAggrStmt:
7302 			DROP AGGREGATE func_name aggr_args opt_drop_behavior
7303 				{
7304 					DropStmt *n = makeNode(DropStmt);
7305 					n->removeType = OBJECT_AGGREGATE;
7306 					n->objects = list_make1($3);
7307 					n->arguments = list_make1(extractAggrArgTypes($4));
7308 					n->behavior = $5;
7309 					n->missing_ok = false;
7310 					n->concurrent = false;
7311 					$$ = (Node *)n;
7312 				}
7313 			| DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
7314 				{
7315 					DropStmt *n = makeNode(DropStmt);
7316 					n->removeType = OBJECT_AGGREGATE;
7317 					n->objects = list_make1($5);
7318 					n->arguments = list_make1(extractAggrArgTypes($6));
7319 					n->behavior = $7;
7320 					n->missing_ok = true;
7321 					n->concurrent = false;
7322 					$$ = (Node *)n;
7323 				}
7324 		;
7325 
7326 RemoveOperStmt:
7327 			DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
7328 				{
7329 					DropStmt *n = makeNode(DropStmt);
7330 					n->removeType = OBJECT_OPERATOR;
7331 					n->objects = list_make1($3);
7332 					n->arguments = list_make1($4);
7333 					n->behavior = $5;
7334 					n->missing_ok = false;
7335 					n->concurrent = false;
7336 					$$ = (Node *)n;
7337 				}
7338 			| DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
7339 				{
7340 					DropStmt *n = makeNode(DropStmt);
7341 					n->removeType = OBJECT_OPERATOR;
7342 					n->objects = list_make1($5);
7343 					n->arguments = list_make1($6);
7344 					n->behavior = $7;
7345 					n->missing_ok = true;
7346 					n->concurrent = false;
7347 					$$ = (Node *)n;
7348 				}
7349 		;
7350 
7351 oper_argtypes:
7352 			'(' Typename ')'
7353 				{
7354 				   ereport(ERROR,
7355 						   (errcode(ERRCODE_SYNTAX_ERROR),
7356 							errmsg("missing argument"),
7357 							errhint("Use NONE to denote the missing argument of a unary operator."),
7358 							parser_errposition(@3)));
7359 				}
7360 			| '(' Typename ',' Typename ')'
7361 					{ $$ = list_make2($2, $4); }
7362 			| '(' NONE ',' Typename ')'					/* left unary */
7363 					{ $$ = list_make2(NULL, $4); }
7364 			| '(' Typename ',' NONE ')'					/* right unary */
7365 					{ $$ = list_make2($2, NULL); }
7366 		;
7367 
7368 any_operator:
7369 			all_Op
7370 					{ $$ = list_make1(makeString($1)); }
7371 			| ColId '.' any_operator
7372 					{ $$ = lcons(makeString($1), $3); }
7373 		;
7374 
7375 /*****************************************************************************
7376  *
7377  *		DO <anonymous code block> [ LANGUAGE language ]
7378  *
7379  * We use a DefElem list for future extensibility, and to allow flexibility
7380  * in the clause order.
7381  *
7382  *****************************************************************************/
7383 
7384 DoStmt: DO dostmt_opt_list
7385 				{
7386 					DoStmt *n = makeNode(DoStmt);
7387 					n->args = $2;
7388 					$$ = (Node *)n;
7389 				}
7390 		;
7391 
7392 dostmt_opt_list:
7393 			dostmt_opt_item						{ $$ = list_make1($1); }
7394 			| dostmt_opt_list dostmt_opt_item	{ $$ = lappend($1, $2); }
7395 		;
7396 
7397 dostmt_opt_item:
7398 			Sconst
7399 				{
7400 					$$ = makeDefElem("as", (Node *)makeString($1));
7401 				}
7402 			| LANGUAGE NonReservedWord_or_Sconst
7403 				{
7404 					$$ = makeDefElem("language", (Node *)makeString($2));
7405 				}
7406 		;
7407 
7408 /*****************************************************************************
7409  *
7410  *		CREATE CAST / DROP CAST
7411  *
7412  *****************************************************************************/
7413 
7414 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
7415 					WITH FUNCTION function_with_argtypes cast_context
7416 				{
7417 					CreateCastStmt *n = makeNode(CreateCastStmt);
7418 					n->sourcetype = $4;
7419 					n->targettype = $6;
7420 					n->func = $10;
7421 					n->context = (CoercionContext) $11;
7422 					n->inout = false;
7423 					$$ = (Node *)n;
7424 				}
7425 			| CREATE CAST '(' Typename AS Typename ')'
7426 					WITHOUT FUNCTION cast_context
7427 				{
7428 					CreateCastStmt *n = makeNode(CreateCastStmt);
7429 					n->sourcetype = $4;
7430 					n->targettype = $6;
7431 					n->func = NULL;
7432 					n->context = (CoercionContext) $10;
7433 					n->inout = false;
7434 					$$ = (Node *)n;
7435 				}
7436 			| CREATE CAST '(' Typename AS Typename ')'
7437 					WITH INOUT cast_context
7438 				{
7439 					CreateCastStmt *n = makeNode(CreateCastStmt);
7440 					n->sourcetype = $4;
7441 					n->targettype = $6;
7442 					n->func = NULL;
7443 					n->context = (CoercionContext) $10;
7444 					n->inout = true;
7445 					$$ = (Node *)n;
7446 				}
7447 		;
7448 
7449 cast_context:  AS IMPLICIT_P					{ $$ = COERCION_IMPLICIT; }
7450 		| AS ASSIGNMENT							{ $$ = COERCION_ASSIGNMENT; }
7451 		| /*EMPTY*/								{ $$ = COERCION_EXPLICIT; }
7452 		;
7453 
7454 
7455 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
7456 				{
7457 					DropStmt *n = makeNode(DropStmt);
7458 					n->removeType = OBJECT_CAST;
7459 					n->objects = list_make1(list_make1($5));
7460 					n->arguments = list_make1(list_make1($7));
7461 					n->behavior = $9;
7462 					n->missing_ok = $3;
7463 					n->concurrent = false;
7464 					$$ = (Node *)n;
7465 				}
7466 		;
7467 
7468 opt_if_exists: IF_P EXISTS						{ $$ = TRUE; }
7469 		| /*EMPTY*/								{ $$ = FALSE; }
7470 		;
7471 
7472 
7473 /*****************************************************************************
7474  *
7475  *		CREATE TRANSFORM / DROP TRANSFORM
7476  *
7477  *****************************************************************************/
7478 
7479 CreateTransformStmt: CREATE opt_or_replace TRANSFORM FOR Typename LANGUAGE name '(' transform_element_list ')'
7480 				{
7481 					CreateTransformStmt *n = makeNode(CreateTransformStmt);
7482 					n->replace = $2;
7483 					n->type_name = $5;
7484 					n->lang = $7;
7485 					n->fromsql = linitial($9);
7486 					n->tosql = lsecond($9);
7487 					$$ = (Node *)n;
7488 				}
7489 		;
7490 
7491 transform_element_list: FROM SQL_P WITH FUNCTION function_with_argtypes ',' TO SQL_P WITH FUNCTION function_with_argtypes
7492 				{
7493 					$$ = list_make2($5, $11);
7494 				}
7495 				| TO SQL_P WITH FUNCTION function_with_argtypes ',' FROM SQL_P WITH FUNCTION function_with_argtypes
7496 				{
7497 					$$ = list_make2($11, $5);
7498 				}
7499 				| FROM SQL_P WITH FUNCTION function_with_argtypes
7500 				{
7501 					$$ = list_make2($5, NULL);
7502 				}
7503 				| TO SQL_P WITH FUNCTION function_with_argtypes
7504 				{
7505 					$$ = list_make2(NULL, $5);
7506 				}
7507 		;
7508 
7509 
7510 DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_drop_behavior
7511 				{
7512 					DropStmt *n = makeNode(DropStmt);
7513 					n->removeType = OBJECT_TRANSFORM;
7514 					n->objects = list_make1(list_make1($5));
7515 					n->arguments = list_make1(list_make1(makeString($7)));
7516 					n->behavior = $8;
7517 					n->missing_ok = $3;
7518 					$$ = (Node *)n;
7519 				}
7520 		;
7521 
7522 
7523 /*****************************************************************************
7524  *
7525  *		QUERY:
7526  *
7527  *		REINDEX [ (options) ] type <name>
7528  *****************************************************************************/
7529 
7530 ReindexStmt:
7531 			REINDEX reindex_target_type qualified_name
7532 				{
7533 					ReindexStmt *n = makeNode(ReindexStmt);
7534 					n->kind = $2;
7535 					n->relation = $3;
7536 					n->name = NULL;
7537 					n->options = 0;
7538 					$$ = (Node *)n;
7539 				}
7540 			| REINDEX reindex_target_multitable name
7541 				{
7542 					ReindexStmt *n = makeNode(ReindexStmt);
7543 					n->kind = $2;
7544 					n->name = $3;
7545 					n->relation = NULL;
7546 					n->options = 0;
7547 					$$ = (Node *)n;
7548 				}
7549 			| REINDEX '(' reindex_option_list ')' reindex_target_type qualified_name
7550 				{
7551 					ReindexStmt *n = makeNode(ReindexStmt);
7552 					n->kind = $5;
7553 					n->relation = $6;
7554 					n->name = NULL;
7555 					n->options = $3;
7556 					$$ = (Node *)n;
7557 				}
7558 			| REINDEX '(' reindex_option_list ')' reindex_target_multitable name
7559 				{
7560 					ReindexStmt *n = makeNode(ReindexStmt);
7561 					n->kind = $5;
7562 					n->name = $6;
7563 					n->relation = NULL;
7564 					n->options = $3;
7565 					$$ = (Node *)n;
7566 				}
7567 		;
7568 reindex_target_type:
7569 			INDEX					{ $$ = REINDEX_OBJECT_INDEX; }
7570 			| TABLE					{ $$ = REINDEX_OBJECT_TABLE; }
7571 		;
7572 reindex_target_multitable:
7573 			SCHEMA					{ $$ = REINDEX_OBJECT_SCHEMA; }
7574 			| SYSTEM_P				{ $$ = REINDEX_OBJECT_SYSTEM; }
7575 			| DATABASE				{ $$ = REINDEX_OBJECT_DATABASE; }
7576 		;
7577 reindex_option_list:
7578 			reindex_option_elem								{ $$ = $1; }
7579 			| reindex_option_list ',' reindex_option_elem	{ $$ = $1 | $3; }
7580 		;
7581 reindex_option_elem:
7582 			VERBOSE	{ $$ = REINDEXOPT_VERBOSE; }
7583 		;
7584 
7585 /*****************************************************************************
7586  *
7587  * ALTER TABLESPACE
7588  *
7589  *****************************************************************************/
7590 
7591 AlterTblSpcStmt:
7592 			ALTER TABLESPACE name SET reloptions
7593 				{
7594 					AlterTableSpaceOptionsStmt *n =
7595 						makeNode(AlterTableSpaceOptionsStmt);
7596 					n->tablespacename = $3;
7597 					n->options = $5;
7598 					n->isReset = FALSE;
7599 					$$ = (Node *)n;
7600 				}
7601 			| ALTER TABLESPACE name RESET reloptions
7602 				{
7603 					AlterTableSpaceOptionsStmt *n =
7604 						makeNode(AlterTableSpaceOptionsStmt);
7605 					n->tablespacename = $3;
7606 					n->options = $5;
7607 					n->isReset = TRUE;
7608 					$$ = (Node *)n;
7609 				}
7610 		;
7611 
7612 /*****************************************************************************
7613  *
7614  * ALTER THING name RENAME TO newname
7615  *
7616  *****************************************************************************/
7617 
7618 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
7619 				{
7620 					RenameStmt *n = makeNode(RenameStmt);
7621 					n->renameType = OBJECT_AGGREGATE;
7622 					n->object = $3;
7623 					n->objarg = extractAggrArgTypes($4);
7624 					n->newname = $7;
7625 					n->missing_ok = false;
7626 					$$ = (Node *)n;
7627 				}
7628 			| ALTER COLLATION any_name RENAME TO name
7629 				{
7630 					RenameStmt *n = makeNode(RenameStmt);
7631 					n->renameType = OBJECT_COLLATION;
7632 					n->object = $3;
7633 					n->newname = $6;
7634 					n->missing_ok = false;
7635 					$$ = (Node *)n;
7636 				}
7637 			| ALTER CONVERSION_P any_name RENAME TO name
7638 				{
7639 					RenameStmt *n = makeNode(RenameStmt);
7640 					n->renameType = OBJECT_CONVERSION;
7641 					n->object = $3;
7642 					n->newname = $6;
7643 					n->missing_ok = false;
7644 					$$ = (Node *)n;
7645 				}
7646 			| ALTER DATABASE database_name RENAME TO database_name
7647 				{
7648 					RenameStmt *n = makeNode(RenameStmt);
7649 					n->renameType = OBJECT_DATABASE;
7650 					n->subname = $3;
7651 					n->newname = $6;
7652 					n->missing_ok = false;
7653 					$$ = (Node *)n;
7654 				}
7655 			| ALTER DOMAIN_P any_name RENAME TO name
7656 				{
7657 					RenameStmt *n = makeNode(RenameStmt);
7658 					n->renameType = OBJECT_DOMAIN;
7659 					n->object = $3;
7660 					n->newname = $6;
7661 					n->missing_ok = false;
7662 					$$ = (Node *)n;
7663 				}
7664 			| ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
7665 				{
7666 					RenameStmt *n = makeNode(RenameStmt);
7667 					n->renameType = OBJECT_DOMCONSTRAINT;
7668 					n->object = $3;
7669 					n->subname = $6;
7670 					n->newname = $8;
7671 					$$ = (Node *)n;
7672 				}
7673 			| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
7674 				{
7675 					RenameStmt *n = makeNode(RenameStmt);
7676 					n->renameType = OBJECT_FDW;
7677 					n->object = list_make1(makeString($5));
7678 					n->newname = $8;
7679 					n->missing_ok = false;
7680 					$$ = (Node *)n;
7681 				}
7682 			| ALTER FUNCTION function_with_argtypes RENAME TO name
7683 				{
7684 					RenameStmt *n = makeNode(RenameStmt);
7685 					n->renameType = OBJECT_FUNCTION;
7686 					n->object = $3->funcname;
7687 					n->objarg = $3->funcargs;
7688 					n->newname = $6;
7689 					n->missing_ok = false;
7690 					$$ = (Node *)n;
7691 				}
7692 			| ALTER GROUP_P RoleId RENAME TO RoleId
7693 				{
7694 					RenameStmt *n = makeNode(RenameStmt);
7695 					n->renameType = OBJECT_ROLE;
7696 					n->subname = $3;
7697 					n->newname = $6;
7698 					n->missing_ok = false;
7699 					$$ = (Node *)n;
7700 				}
7701 			| ALTER opt_procedural LANGUAGE name RENAME TO name
7702 				{
7703 					RenameStmt *n = makeNode(RenameStmt);
7704 					n->renameType = OBJECT_LANGUAGE;
7705 					n->object = list_make1(makeString($4));
7706 					n->newname = $7;
7707 					n->missing_ok = false;
7708 					$$ = (Node *)n;
7709 				}
7710 			| ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
7711 				{
7712 					RenameStmt *n = makeNode(RenameStmt);
7713 					n->renameType = OBJECT_OPCLASS;
7714 					n->object = lcons(makeString($6), $4);
7715 					n->newname = $9;
7716 					n->missing_ok = false;
7717 					$$ = (Node *)n;
7718 				}
7719 			| ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
7720 				{
7721 					RenameStmt *n = makeNode(RenameStmt);
7722 					n->renameType = OBJECT_OPFAMILY;
7723 					n->object = lcons(makeString($6), $4);
7724 					n->newname = $9;
7725 					n->missing_ok = false;
7726 					$$ = (Node *)n;
7727 				}
7728 			| ALTER POLICY name ON qualified_name RENAME TO name
7729 				{
7730 					RenameStmt *n = makeNode(RenameStmt);
7731 					n->renameType = OBJECT_POLICY;
7732 					n->relation = $5;
7733 					n->subname = $3;
7734 					n->newname = $8;
7735 					n->missing_ok = false;
7736 					$$ = (Node *)n;
7737 				}
7738 			| ALTER POLICY IF_P EXISTS name ON qualified_name RENAME TO name
7739 				{
7740 					RenameStmt *n = makeNode(RenameStmt);
7741 					n->renameType = OBJECT_POLICY;
7742 					n->relation = $7;
7743 					n->subname = $5;
7744 					n->newname = $10;
7745 					n->missing_ok = true;
7746 					$$ = (Node *)n;
7747 				}
7748 			| ALTER SCHEMA name RENAME TO name
7749 				{
7750 					RenameStmt *n = makeNode(RenameStmt);
7751 					n->renameType = OBJECT_SCHEMA;
7752 					n->subname = $3;
7753 					n->newname = $6;
7754 					n->missing_ok = false;
7755 					$$ = (Node *)n;
7756 				}
7757 			| ALTER SERVER name RENAME TO name
7758 				{
7759 					RenameStmt *n = makeNode(RenameStmt);
7760 					n->renameType = OBJECT_FOREIGN_SERVER;
7761 					n->object = list_make1(makeString($3));
7762 					n->newname = $6;
7763 					n->missing_ok = false;
7764 					$$ = (Node *)n;
7765 				}
7766 			| ALTER TABLE relation_expr RENAME TO name
7767 				{
7768 					RenameStmt *n = makeNode(RenameStmt);
7769 					n->renameType = OBJECT_TABLE;
7770 					n->relation = $3;
7771 					n->subname = NULL;
7772 					n->newname = $6;
7773 					n->missing_ok = false;
7774 					$$ = (Node *)n;
7775 				}
7776 			| ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
7777 				{
7778 					RenameStmt *n = makeNode(RenameStmt);
7779 					n->renameType = OBJECT_TABLE;
7780 					n->relation = $5;
7781 					n->subname = NULL;
7782 					n->newname = $8;
7783 					n->missing_ok = true;
7784 					$$ = (Node *)n;
7785 				}
7786 			| ALTER SEQUENCE qualified_name RENAME TO name
7787 				{
7788 					RenameStmt *n = makeNode(RenameStmt);
7789 					n->renameType = OBJECT_SEQUENCE;
7790 					n->relation = $3;
7791 					n->subname = NULL;
7792 					n->newname = $6;
7793 					n->missing_ok = false;
7794 					$$ = (Node *)n;
7795 				}
7796 			| ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
7797 				{
7798 					RenameStmt *n = makeNode(RenameStmt);
7799 					n->renameType = OBJECT_SEQUENCE;
7800 					n->relation = $5;
7801 					n->subname = NULL;
7802 					n->newname = $8;
7803 					n->missing_ok = true;
7804 					$$ = (Node *)n;
7805 				}
7806 			| ALTER VIEW qualified_name RENAME TO name
7807 				{
7808 					RenameStmt *n = makeNode(RenameStmt);
7809 					n->renameType = OBJECT_VIEW;
7810 					n->relation = $3;
7811 					n->subname = NULL;
7812 					n->newname = $6;
7813 					n->missing_ok = false;
7814 					$$ = (Node *)n;
7815 				}
7816 			| ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
7817 				{
7818 					RenameStmt *n = makeNode(RenameStmt);
7819 					n->renameType = OBJECT_VIEW;
7820 					n->relation = $5;
7821 					n->subname = NULL;
7822 					n->newname = $8;
7823 					n->missing_ok = true;
7824 					$$ = (Node *)n;
7825 				}
7826 			| ALTER MATERIALIZED VIEW qualified_name RENAME TO name
7827 				{
7828 					RenameStmt *n = makeNode(RenameStmt);
7829 					n->renameType = OBJECT_MATVIEW;
7830 					n->relation = $4;
7831 					n->subname = NULL;
7832 					n->newname = $7;
7833 					n->missing_ok = false;
7834 					$$ = (Node *)n;
7835 				}
7836 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
7837 				{
7838 					RenameStmt *n = makeNode(RenameStmt);
7839 					n->renameType = OBJECT_MATVIEW;
7840 					n->relation = $6;
7841 					n->subname = NULL;
7842 					n->newname = $9;
7843 					n->missing_ok = true;
7844 					$$ = (Node *)n;
7845 				}
7846 			| ALTER INDEX qualified_name RENAME TO name
7847 				{
7848 					RenameStmt *n = makeNode(RenameStmt);
7849 					n->renameType = OBJECT_INDEX;
7850 					n->relation = $3;
7851 					n->subname = NULL;
7852 					n->newname = $6;
7853 					n->missing_ok = false;
7854 					$$ = (Node *)n;
7855 				}
7856 			| ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
7857 				{
7858 					RenameStmt *n = makeNode(RenameStmt);
7859 					n->renameType = OBJECT_INDEX;
7860 					n->relation = $5;
7861 					n->subname = NULL;
7862 					n->newname = $8;
7863 					n->missing_ok = true;
7864 					$$ = (Node *)n;
7865 				}
7866 			| ALTER FOREIGN TABLE relation_expr RENAME TO name
7867 				{
7868 					RenameStmt *n = makeNode(RenameStmt);
7869 					n->renameType = OBJECT_FOREIGN_TABLE;
7870 					n->relation = $4;
7871 					n->subname = NULL;
7872 					n->newname = $7;
7873 					n->missing_ok = false;
7874 					$$ = (Node *)n;
7875 				}
7876 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
7877 				{
7878 					RenameStmt *n = makeNode(RenameStmt);
7879 					n->renameType = OBJECT_FOREIGN_TABLE;
7880 					n->relation = $6;
7881 					n->subname = NULL;
7882 					n->newname = $9;
7883 					n->missing_ok = true;
7884 					$$ = (Node *)n;
7885 				}
7886 			| ALTER TABLE relation_expr RENAME opt_column name TO name
7887 				{
7888 					RenameStmt *n = makeNode(RenameStmt);
7889 					n->renameType = OBJECT_COLUMN;
7890 					n->relationType = OBJECT_TABLE;
7891 					n->relation = $3;
7892 					n->subname = $6;
7893 					n->newname = $8;
7894 					n->missing_ok = false;
7895 					$$ = (Node *)n;
7896 				}
7897 			| ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
7898 				{
7899 					RenameStmt *n = makeNode(RenameStmt);
7900 					n->renameType = OBJECT_COLUMN;
7901 					n->relationType = OBJECT_TABLE;
7902 					n->relation = $5;
7903 					n->subname = $8;
7904 					n->newname = $10;
7905 					n->missing_ok = true;
7906 					$$ = (Node *)n;
7907 				}
7908 			| ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
7909 				{
7910 					RenameStmt *n = makeNode(RenameStmt);
7911 					n->renameType = OBJECT_COLUMN;
7912 					n->relationType = OBJECT_MATVIEW;
7913 					n->relation = $4;
7914 					n->subname = $7;
7915 					n->newname = $9;
7916 					n->missing_ok = false;
7917 					$$ = (Node *)n;
7918 				}
7919 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
7920 				{
7921 					RenameStmt *n = makeNode(RenameStmt);
7922 					n->renameType = OBJECT_COLUMN;
7923 					n->relationType = OBJECT_MATVIEW;
7924 					n->relation = $6;
7925 					n->subname = $9;
7926 					n->newname = $11;
7927 					n->missing_ok = true;
7928 					$$ = (Node *)n;
7929 				}
7930 			| ALTER TABLE relation_expr RENAME CONSTRAINT name TO name
7931 				{
7932 					RenameStmt *n = makeNode(RenameStmt);
7933 					n->renameType = OBJECT_TABCONSTRAINT;
7934 					n->relation = $3;
7935 					n->subname = $6;
7936 					n->newname = $8;
7937 					n->missing_ok = false;
7938 					$$ = (Node *)n;
7939 				}
7940 			| ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
7941 				{
7942 					RenameStmt *n = makeNode(RenameStmt);
7943 					n->renameType = OBJECT_TABCONSTRAINT;
7944 					n->relation = $5;
7945 					n->subname = $8;
7946 					n->newname = $10;
7947 					n->missing_ok = true;
7948 					$$ = (Node *)n;
7949 				}
7950 			| ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
7951 				{
7952 					RenameStmt *n = makeNode(RenameStmt);
7953 					n->renameType = OBJECT_COLUMN;
7954 					n->relationType = OBJECT_FOREIGN_TABLE;
7955 					n->relation = $4;
7956 					n->subname = $7;
7957 					n->newname = $9;
7958 					n->missing_ok = false;
7959 					$$ = (Node *)n;
7960 				}
7961 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
7962 				{
7963 					RenameStmt *n = makeNode(RenameStmt);
7964 					n->renameType = OBJECT_COLUMN;
7965 					n->relationType = OBJECT_FOREIGN_TABLE;
7966 					n->relation = $6;
7967 					n->subname = $9;
7968 					n->newname = $11;
7969 					n->missing_ok = true;
7970 					$$ = (Node *)n;
7971 				}
7972 			| ALTER RULE name ON qualified_name RENAME TO name
7973 				{
7974 					RenameStmt *n = makeNode(RenameStmt);
7975 					n->renameType = OBJECT_RULE;
7976 					n->relation = $5;
7977 					n->subname = $3;
7978 					n->newname = $8;
7979 					n->missing_ok = false;
7980 					$$ = (Node *)n;
7981 				}
7982 			| ALTER TRIGGER name ON qualified_name RENAME TO name
7983 				{
7984 					RenameStmt *n = makeNode(RenameStmt);
7985 					n->renameType = OBJECT_TRIGGER;
7986 					n->relation = $5;
7987 					n->subname = $3;
7988 					n->newname = $8;
7989 					n->missing_ok = false;
7990 					$$ = (Node *)n;
7991 				}
7992 			| ALTER EVENT TRIGGER name RENAME TO name
7993 				{
7994 					RenameStmt *n = makeNode(RenameStmt);
7995 					n->renameType = OBJECT_EVENT_TRIGGER;
7996 					n->object = list_make1(makeString($4));
7997 					n->newname = $7;
7998 					$$ = (Node *)n;
7999 				}
8000 			| ALTER ROLE RoleId RENAME TO RoleId
8001 				{
8002 					RenameStmt *n = makeNode(RenameStmt);
8003 					n->renameType = OBJECT_ROLE;
8004 					n->subname = $3;
8005 					n->newname = $6;
8006 					n->missing_ok = false;
8007 					$$ = (Node *)n;
8008 				}
8009 			| ALTER USER RoleId RENAME TO RoleId
8010 				{
8011 					RenameStmt *n = makeNode(RenameStmt);
8012 					n->renameType = OBJECT_ROLE;
8013 					n->subname = $3;
8014 					n->newname = $6;
8015 					n->missing_ok = false;
8016 					$$ = (Node *)n;
8017 				}
8018 			| ALTER TABLESPACE name RENAME TO name
8019 				{
8020 					RenameStmt *n = makeNode(RenameStmt);
8021 					n->renameType = OBJECT_TABLESPACE;
8022 					n->subname = $3;
8023 					n->newname = $6;
8024 					n->missing_ok = false;
8025 					$$ = (Node *)n;
8026 				}
8027 			| ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
8028 				{
8029 					RenameStmt *n = makeNode(RenameStmt);
8030 					n->renameType = OBJECT_TSPARSER;
8031 					n->object = $5;
8032 					n->newname = $8;
8033 					n->missing_ok = false;
8034 					$$ = (Node *)n;
8035 				}
8036 			| ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
8037 				{
8038 					RenameStmt *n = makeNode(RenameStmt);
8039 					n->renameType = OBJECT_TSDICTIONARY;
8040 					n->object = $5;
8041 					n->newname = $8;
8042 					n->missing_ok = false;
8043 					$$ = (Node *)n;
8044 				}
8045 			| ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
8046 				{
8047 					RenameStmt *n = makeNode(RenameStmt);
8048 					n->renameType = OBJECT_TSTEMPLATE;
8049 					n->object = $5;
8050 					n->newname = $8;
8051 					n->missing_ok = false;
8052 					$$ = (Node *)n;
8053 				}
8054 			| ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
8055 				{
8056 					RenameStmt *n = makeNode(RenameStmt);
8057 					n->renameType = OBJECT_TSCONFIGURATION;
8058 					n->object = $5;
8059 					n->newname = $8;
8060 					n->missing_ok = false;
8061 					$$ = (Node *)n;
8062 				}
8063 			| ALTER TYPE_P any_name RENAME TO name
8064 				{
8065 					RenameStmt *n = makeNode(RenameStmt);
8066 					n->renameType = OBJECT_TYPE;
8067 					n->object = $3;
8068 					n->newname = $6;
8069 					n->missing_ok = false;
8070 					$$ = (Node *)n;
8071 				}
8072 			| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
8073 				{
8074 					RenameStmt *n = makeNode(RenameStmt);
8075 					n->renameType = OBJECT_ATTRIBUTE;
8076 					n->relationType = OBJECT_TYPE;
8077 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
8078 					n->subname = $6;
8079 					n->newname = $8;
8080 					n->behavior = $9;
8081 					n->missing_ok = false;
8082 					$$ = (Node *)n;
8083 				}
8084 		;
8085 
8086 opt_column: COLUMN									{ $$ = COLUMN; }
8087 			| /*EMPTY*/								{ $$ = 0; }
8088 		;
8089 
8090 opt_set_data: SET DATA_P							{ $$ = 1; }
8091 			| /*EMPTY*/								{ $$ = 0; }
8092 		;
8093 
8094 /*****************************************************************************
8095  *
8096  * ALTER THING name DEPENDS ON EXTENSION name
8097  *
8098  *****************************************************************************/
8099 
8100 AlterObjectDependsStmt:
8101 			ALTER FUNCTION function_with_argtypes DEPENDS ON EXTENSION name
8102 				{
8103 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8104 					n->objectType = OBJECT_FUNCTION;
8105 					n->relation = NULL;
8106 					n->objname = $3->funcname;
8107 					n->objargs = $3->funcargs;
8108 					n->extname = makeString($7);
8109 					$$ = (Node *)n;
8110 				}
8111 			| ALTER TRIGGER name ON qualified_name DEPENDS ON EXTENSION name
8112 				{
8113 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8114 					n->objectType = OBJECT_TRIGGER;
8115 					n->relation = $5;
8116 					n->objname = list_make1(makeString($3));
8117 					n->objargs = NIL;
8118 					n->extname = makeString($9);
8119 					$$ = (Node *)n;
8120 				}
8121 			| ALTER MATERIALIZED VIEW qualified_name DEPENDS ON EXTENSION name
8122 				{
8123 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8124 					n->objectType = OBJECT_MATVIEW;
8125 					n->relation = $4;
8126 					n->objname = NIL;
8127 					n->objargs = NIL;
8128 					n->extname = makeString($8);
8129 					$$ = (Node *)n;
8130 				}
8131 			| ALTER INDEX qualified_name DEPENDS ON EXTENSION name
8132 				{
8133 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8134 					n->objectType = OBJECT_INDEX;
8135 					n->relation = $3;
8136 					n->objname = NIL;
8137 					n->objargs = NIL;
8138 					n->extname = makeString($7);
8139 					$$ = (Node *)n;
8140 				}
8141 		;
8142 
8143 /*****************************************************************************
8144  *
8145  * ALTER THING name SET SCHEMA name
8146  *
8147  *****************************************************************************/
8148 
8149 AlterObjectSchemaStmt:
8150 			ALTER AGGREGATE func_name aggr_args SET SCHEMA name
8151 				{
8152 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8153 					n->objectType = OBJECT_AGGREGATE;
8154 					n->object = $3;
8155 					n->objarg = extractAggrArgTypes($4);
8156 					n->newschema = $7;
8157 					n->missing_ok = false;
8158 					$$ = (Node *)n;
8159 				}
8160 			| ALTER COLLATION any_name SET SCHEMA name
8161 				{
8162 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8163 					n->objectType = OBJECT_COLLATION;
8164 					n->object = $3;
8165 					n->newschema = $6;
8166 					n->missing_ok = false;
8167 					$$ = (Node *)n;
8168 				}
8169 			| ALTER CONVERSION_P any_name SET SCHEMA name
8170 				{
8171 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8172 					n->objectType = OBJECT_CONVERSION;
8173 					n->object = $3;
8174 					n->newschema = $6;
8175 					n->missing_ok = false;
8176 					$$ = (Node *)n;
8177 				}
8178 			| ALTER DOMAIN_P any_name SET SCHEMA name
8179 				{
8180 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8181 					n->objectType = OBJECT_DOMAIN;
8182 					n->object = $3;
8183 					n->newschema = $6;
8184 					n->missing_ok = false;
8185 					$$ = (Node *)n;
8186 				}
8187 			| ALTER EXTENSION any_name SET SCHEMA name
8188 				{
8189 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8190 					n->objectType = OBJECT_EXTENSION;
8191 					n->object = $3;
8192 					n->newschema = $6;
8193 					n->missing_ok = false;
8194 					$$ = (Node *)n;
8195 				}
8196 			| ALTER FUNCTION function_with_argtypes SET SCHEMA name
8197 				{
8198 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8199 					n->objectType = OBJECT_FUNCTION;
8200 					n->object = $3->funcname;
8201 					n->objarg = $3->funcargs;
8202 					n->newschema = $6;
8203 					n->missing_ok = false;
8204 					$$ = (Node *)n;
8205 				}
8206 			| ALTER OPERATOR any_operator oper_argtypes SET SCHEMA name
8207 				{
8208 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8209 					n->objectType = OBJECT_OPERATOR;
8210 					n->object = $3;
8211 					n->objarg = $4;
8212 					n->newschema = $7;
8213 					n->missing_ok = false;
8214 					$$ = (Node *)n;
8215 				}
8216 			| ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
8217 				{
8218 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8219 					n->objectType = OBJECT_OPCLASS;
8220 					n->object = lcons(makeString($6), $4);
8221 					n->newschema = $9;
8222 					n->missing_ok = false;
8223 					$$ = (Node *)n;
8224 				}
8225 			| ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
8226 				{
8227 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8228 					n->objectType = OBJECT_OPFAMILY;
8229 					n->object = lcons(makeString($6), $4);
8230 					n->newschema = $9;
8231 					n->missing_ok = false;
8232 					$$ = (Node *)n;
8233 				}
8234 			| ALTER TABLE relation_expr SET SCHEMA name
8235 				{
8236 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8237 					n->objectType = OBJECT_TABLE;
8238 					n->relation = $3;
8239 					n->newschema = $6;
8240 					n->missing_ok = false;
8241 					$$ = (Node *)n;
8242 				}
8243 			| ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
8244 				{
8245 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8246 					n->objectType = OBJECT_TABLE;
8247 					n->relation = $5;
8248 					n->newschema = $8;
8249 					n->missing_ok = true;
8250 					$$ = (Node *)n;
8251 				}
8252 			| ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
8253 				{
8254 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8255 					n->objectType = OBJECT_TSPARSER;
8256 					n->object = $5;
8257 					n->newschema = $8;
8258 					n->missing_ok = false;
8259 					$$ = (Node *)n;
8260 				}
8261 			| ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
8262 				{
8263 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8264 					n->objectType = OBJECT_TSDICTIONARY;
8265 					n->object = $5;
8266 					n->newschema = $8;
8267 					n->missing_ok = false;
8268 					$$ = (Node *)n;
8269 				}
8270 			| ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
8271 				{
8272 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8273 					n->objectType = OBJECT_TSTEMPLATE;
8274 					n->object = $5;
8275 					n->newschema = $8;
8276 					n->missing_ok = false;
8277 					$$ = (Node *)n;
8278 				}
8279 			| ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
8280 				{
8281 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8282 					n->objectType = OBJECT_TSCONFIGURATION;
8283 					n->object = $5;
8284 					n->newschema = $8;
8285 					n->missing_ok = false;
8286 					$$ = (Node *)n;
8287 				}
8288 			| ALTER SEQUENCE qualified_name SET SCHEMA name
8289 				{
8290 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8291 					n->objectType = OBJECT_SEQUENCE;
8292 					n->relation = $3;
8293 					n->newschema = $6;
8294 					n->missing_ok = false;
8295 					$$ = (Node *)n;
8296 				}
8297 			| ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
8298 				{
8299 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8300 					n->objectType = OBJECT_SEQUENCE;
8301 					n->relation = $5;
8302 					n->newschema = $8;
8303 					n->missing_ok = true;
8304 					$$ = (Node *)n;
8305 				}
8306 			| ALTER VIEW qualified_name SET SCHEMA name
8307 				{
8308 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8309 					n->objectType = OBJECT_VIEW;
8310 					n->relation = $3;
8311 					n->newschema = $6;
8312 					n->missing_ok = false;
8313 					$$ = (Node *)n;
8314 				}
8315 			| ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
8316 				{
8317 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8318 					n->objectType = OBJECT_VIEW;
8319 					n->relation = $5;
8320 					n->newschema = $8;
8321 					n->missing_ok = true;
8322 					$$ = (Node *)n;
8323 				}
8324 			| ALTER MATERIALIZED VIEW qualified_name SET SCHEMA name
8325 				{
8326 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8327 					n->objectType = OBJECT_MATVIEW;
8328 					n->relation = $4;
8329 					n->newschema = $7;
8330 					n->missing_ok = false;
8331 					$$ = (Node *)n;
8332 				}
8333 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name SET SCHEMA name
8334 				{
8335 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8336 					n->objectType = OBJECT_MATVIEW;
8337 					n->relation = $6;
8338 					n->newschema = $9;
8339 					n->missing_ok = true;
8340 					$$ = (Node *)n;
8341 				}
8342 			| ALTER FOREIGN TABLE relation_expr SET SCHEMA name
8343 				{
8344 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8345 					n->objectType = OBJECT_FOREIGN_TABLE;
8346 					n->relation = $4;
8347 					n->newschema = $7;
8348 					n->missing_ok = false;
8349 					$$ = (Node *)n;
8350 				}
8351 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
8352 				{
8353 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8354 					n->objectType = OBJECT_FOREIGN_TABLE;
8355 					n->relation = $6;
8356 					n->newschema = $9;
8357 					n->missing_ok = true;
8358 					$$ = (Node *)n;
8359 				}
8360 			| ALTER TYPE_P any_name SET SCHEMA name
8361 				{
8362 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8363 					n->objectType = OBJECT_TYPE;
8364 					n->object = $3;
8365 					n->newschema = $6;
8366 					n->missing_ok = false;
8367 					$$ = (Node *)n;
8368 				}
8369 		;
8370 
8371 /*****************************************************************************
8372  *
8373  * ALTER OPERATOR name SET define
8374  *
8375  *****************************************************************************/
8376 
8377 AlterOperatorStmt:
8378 			ALTER OPERATOR any_operator oper_argtypes SET '(' operator_def_list ')'
8379 				{
8380 					AlterOperatorStmt *n = makeNode(AlterOperatorStmt);
8381 					n->opername = $3;
8382 					n->operargs = $4;
8383 					n->options = $7;
8384 					$$ = (Node *)n;
8385 				}
8386 		;
8387 
8388 operator_def_list:	operator_def_elem								{ $$ = list_make1($1); }
8389 			| operator_def_list ',' operator_def_elem				{ $$ = lappend($1, $3); }
8390 		;
8391 
8392 operator_def_elem: ColLabel '=' NONE
8393 						{ $$ = makeDefElem($1, NULL); }
8394 				   | ColLabel '=' def_arg
8395 						{ $$ = makeDefElem($1, (Node *) $3); }
8396 		;
8397 
8398 /*****************************************************************************
8399  *
8400  * ALTER THING name OWNER TO newname
8401  *
8402  *****************************************************************************/
8403 
8404 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleSpec
8405 				{
8406 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8407 					n->objectType = OBJECT_AGGREGATE;
8408 					n->object = $3;
8409 					n->objarg = extractAggrArgTypes($4);
8410 					n->newowner = $7;
8411 					$$ = (Node *)n;
8412 				}
8413 			| ALTER COLLATION any_name OWNER TO RoleSpec
8414 				{
8415 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8416 					n->objectType = OBJECT_COLLATION;
8417 					n->object = $3;
8418 					n->newowner = $6;
8419 					$$ = (Node *)n;
8420 				}
8421 			| ALTER CONVERSION_P any_name OWNER TO RoleSpec
8422 				{
8423 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8424 					n->objectType = OBJECT_CONVERSION;
8425 					n->object = $3;
8426 					n->newowner = $6;
8427 					$$ = (Node *)n;
8428 				}
8429 			| ALTER DATABASE database_name OWNER TO RoleSpec
8430 				{
8431 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8432 					n->objectType = OBJECT_DATABASE;
8433 					n->object = list_make1(makeString($3));
8434 					n->newowner = $6;
8435 					$$ = (Node *)n;
8436 				}
8437 			| ALTER DOMAIN_P any_name OWNER TO RoleSpec
8438 				{
8439 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8440 					n->objectType = OBJECT_DOMAIN;
8441 					n->object = $3;
8442 					n->newowner = $6;
8443 					$$ = (Node *)n;
8444 				}
8445 			| ALTER FUNCTION function_with_argtypes OWNER TO RoleSpec
8446 				{
8447 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8448 					n->objectType = OBJECT_FUNCTION;
8449 					n->object = $3->funcname;
8450 					n->objarg = $3->funcargs;
8451 					n->newowner = $6;
8452 					$$ = (Node *)n;
8453 				}
8454 			| ALTER opt_procedural LANGUAGE name OWNER TO RoleSpec
8455 				{
8456 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8457 					n->objectType = OBJECT_LANGUAGE;
8458 					n->object = list_make1(makeString($4));
8459 					n->newowner = $7;
8460 					$$ = (Node *)n;
8461 				}
8462 			| ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleSpec
8463 				{
8464 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8465 					n->objectType = OBJECT_LARGEOBJECT;
8466 					n->object = list_make1($4);
8467 					n->newowner = $7;
8468 					$$ = (Node *)n;
8469 				}
8470 			| ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleSpec
8471 				{
8472 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8473 					n->objectType = OBJECT_OPERATOR;
8474 					n->object = $3;
8475 					n->objarg = $4;
8476 					n->newowner = $7;
8477 					$$ = (Node *)n;
8478 				}
8479 			| ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleSpec
8480 				{
8481 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8482 					n->objectType = OBJECT_OPCLASS;
8483 					n->object = lcons(makeString($6), $4);
8484 					n->newowner = $9;
8485 					$$ = (Node *)n;
8486 				}
8487 			| ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleSpec
8488 				{
8489 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8490 					n->objectType = OBJECT_OPFAMILY;
8491 					n->object = lcons(makeString($6), $4);
8492 					n->newowner = $9;
8493 					$$ = (Node *)n;
8494 				}
8495 			| ALTER SCHEMA name OWNER TO RoleSpec
8496 				{
8497 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8498 					n->objectType = OBJECT_SCHEMA;
8499 					n->object = list_make1(makeString($3));
8500 					n->newowner = $6;
8501 					$$ = (Node *)n;
8502 				}
8503 			| ALTER TYPE_P any_name OWNER TO RoleSpec
8504 				{
8505 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8506 					n->objectType = OBJECT_TYPE;
8507 					n->object = $3;
8508 					n->newowner = $6;
8509 					$$ = (Node *)n;
8510 				}
8511 			| ALTER TABLESPACE name OWNER TO RoleSpec
8512 				{
8513 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8514 					n->objectType = OBJECT_TABLESPACE;
8515 					n->object = list_make1(makeString($3));
8516 					n->newowner = $6;
8517 					$$ = (Node *)n;
8518 				}
8519 			| ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleSpec
8520 				{
8521 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8522 					n->objectType = OBJECT_TSDICTIONARY;
8523 					n->object = $5;
8524 					n->newowner = $8;
8525 					$$ = (Node *)n;
8526 				}
8527 			| ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleSpec
8528 				{
8529 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8530 					n->objectType = OBJECT_TSCONFIGURATION;
8531 					n->object = $5;
8532 					n->newowner = $8;
8533 					$$ = (Node *)n;
8534 				}
8535 			| ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleSpec
8536 				{
8537 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8538 					n->objectType = OBJECT_FDW;
8539 					n->object = list_make1(makeString($5));
8540 					n->newowner = $8;
8541 					$$ = (Node *)n;
8542 				}
8543 			| ALTER SERVER name OWNER TO RoleSpec
8544 				{
8545 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8546 					n->objectType = OBJECT_FOREIGN_SERVER;
8547 					n->object = list_make1(makeString($3));
8548 					n->newowner = $6;
8549 					$$ = (Node *)n;
8550 				}
8551 			| ALTER EVENT TRIGGER name OWNER TO RoleSpec
8552 				{
8553 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
8554 					n->objectType = OBJECT_EVENT_TRIGGER;
8555 					n->object = list_make1(makeString($4));
8556 					n->newowner = $7;
8557 					$$ = (Node *)n;
8558 				}
8559 		;
8560 
8561 
8562 /*****************************************************************************
8563  *
8564  *		QUERY:	Define Rewrite Rule
8565  *
8566  *****************************************************************************/
8567 
8568 RuleStmt:	CREATE opt_or_replace RULE name AS
8569 			ON event TO qualified_name where_clause
8570 			DO opt_instead RuleActionList
8571 				{
8572 					RuleStmt *n = makeNode(RuleStmt);
8573 					n->replace = $2;
8574 					n->relation = $9;
8575 					n->rulename = $4;
8576 					n->whereClause = $10;
8577 					n->event = $7;
8578 					n->instead = $12;
8579 					n->actions = $13;
8580 					$$ = (Node *)n;
8581 				}
8582 		;
8583 
8584 RuleActionList:
8585 			NOTHING									{ $$ = NIL; }
8586 			| RuleActionStmt						{ $$ = list_make1($1); }
8587 			| '(' RuleActionMulti ')'				{ $$ = $2; }
8588 		;
8589 
8590 /* the thrashing around here is to discard "empty" statements... */
8591 RuleActionMulti:
8592 			RuleActionMulti ';' RuleActionStmtOrEmpty
8593 				{ if ($3 != NULL)
8594 					$$ = lappend($1, $3);
8595 				  else
8596 					$$ = $1;
8597 				}
8598 			| RuleActionStmtOrEmpty
8599 				{ if ($1 != NULL)
8600 					$$ = list_make1($1);
8601 				  else
8602 					$$ = NIL;
8603 				}
8604 		;
8605 
8606 RuleActionStmt:
8607 			SelectStmt
8608 			| InsertStmt
8609 			| UpdateStmt
8610 			| DeleteStmt
8611 			| NotifyStmt
8612 		;
8613 
8614 RuleActionStmtOrEmpty:
8615 			RuleActionStmt							{ $$ = $1; }
8616 			|	/*EMPTY*/							{ $$ = NULL; }
8617 		;
8618 
8619 event:		SELECT									{ $$ = CMD_SELECT; }
8620 			| UPDATE								{ $$ = CMD_UPDATE; }
8621 			| DELETE_P								{ $$ = CMD_DELETE; }
8622 			| INSERT								{ $$ = CMD_INSERT; }
8623 		 ;
8624 
8625 opt_instead:
8626 			INSTEAD									{ $$ = TRUE; }
8627 			| ALSO									{ $$ = FALSE; }
8628 			| /*EMPTY*/								{ $$ = FALSE; }
8629 		;
8630 
8631 
8632 DropRuleStmt:
8633 			DROP RULE name ON any_name opt_drop_behavior
8634 				{
8635 					DropStmt *n = makeNode(DropStmt);
8636 					n->removeType = OBJECT_RULE;
8637 					n->objects = list_make1(lappend($5, makeString($3)));
8638 					n->arguments = NIL;
8639 					n->behavior = $6;
8640 					n->missing_ok = false;
8641 					n->concurrent = false;
8642 					$$ = (Node *) n;
8643 				}
8644 			| DROP RULE IF_P EXISTS name ON any_name opt_drop_behavior
8645 				{
8646 					DropStmt *n = makeNode(DropStmt);
8647 					n->removeType = OBJECT_RULE;
8648 					n->objects = list_make1(lappend($7, makeString($5)));
8649 					n->arguments = NIL;
8650 					n->behavior = $8;
8651 					n->missing_ok = true;
8652 					n->concurrent = false;
8653 					$$ = (Node *) n;
8654 				}
8655 		;
8656 
8657 
8658 /*****************************************************************************
8659  *
8660  *		QUERY:
8661  *				NOTIFY <identifier> can appear both in rule bodies and
8662  *				as a query-level command
8663  *
8664  *****************************************************************************/
8665 
8666 NotifyStmt: NOTIFY ColId notify_payload
8667 				{
8668 					NotifyStmt *n = makeNode(NotifyStmt);
8669 					n->conditionname = $2;
8670 					n->payload = $3;
8671 					$$ = (Node *)n;
8672 				}
8673 		;
8674 
8675 notify_payload:
8676 			',' Sconst							{ $$ = $2; }
8677 			| /*EMPTY*/							{ $$ = NULL; }
8678 		;
8679 
8680 ListenStmt: LISTEN ColId
8681 				{
8682 					ListenStmt *n = makeNode(ListenStmt);
8683 					n->conditionname = $2;
8684 					$$ = (Node *)n;
8685 				}
8686 		;
8687 
8688 UnlistenStmt:
8689 			UNLISTEN ColId
8690 				{
8691 					UnlistenStmt *n = makeNode(UnlistenStmt);
8692 					n->conditionname = $2;
8693 					$$ = (Node *)n;
8694 				}
8695 			| UNLISTEN '*'
8696 				{
8697 					UnlistenStmt *n = makeNode(UnlistenStmt);
8698 					n->conditionname = NULL;
8699 					$$ = (Node *)n;
8700 				}
8701 		;
8702 
8703 
8704 /*****************************************************************************
8705  *
8706  *		Transactions:
8707  *
8708  *		BEGIN / COMMIT / ROLLBACK
8709  *		(also older versions END / ABORT)
8710  *
8711  *****************************************************************************/
8712 
8713 TransactionStmt:
8714 			ABORT_P opt_transaction
8715 				{
8716 					TransactionStmt *n = makeNode(TransactionStmt);
8717 					n->kind = TRANS_STMT_ROLLBACK;
8718 					n->options = NIL;
8719 					$$ = (Node *)n;
8720 				}
8721 			| BEGIN_P opt_transaction transaction_mode_list_or_empty
8722 				{
8723 					TransactionStmt *n = makeNode(TransactionStmt);
8724 					n->kind = TRANS_STMT_BEGIN;
8725 					n->options = $3;
8726 					$$ = (Node *)n;
8727 				}
8728 			| START TRANSACTION transaction_mode_list_or_empty
8729 				{
8730 					TransactionStmt *n = makeNode(TransactionStmt);
8731 					n->kind = TRANS_STMT_START;
8732 					n->options = $3;
8733 					$$ = (Node *)n;
8734 				}
8735 			| COMMIT opt_transaction
8736 				{
8737 					TransactionStmt *n = makeNode(TransactionStmt);
8738 					n->kind = TRANS_STMT_COMMIT;
8739 					n->options = NIL;
8740 					$$ = (Node *)n;
8741 				}
8742 			| END_P opt_transaction
8743 				{
8744 					TransactionStmt *n = makeNode(TransactionStmt);
8745 					n->kind = TRANS_STMT_COMMIT;
8746 					n->options = NIL;
8747 					$$ = (Node *)n;
8748 				}
8749 			| ROLLBACK opt_transaction
8750 				{
8751 					TransactionStmt *n = makeNode(TransactionStmt);
8752 					n->kind = TRANS_STMT_ROLLBACK;
8753 					n->options = NIL;
8754 					$$ = (Node *)n;
8755 				}
8756 			| SAVEPOINT ColId
8757 				{
8758 					TransactionStmt *n = makeNode(TransactionStmt);
8759 					n->kind = TRANS_STMT_SAVEPOINT;
8760 					n->options = list_make1(makeDefElem("savepoint_name",
8761 														(Node *)makeString($2)));
8762 					$$ = (Node *)n;
8763 				}
8764 			| RELEASE SAVEPOINT ColId
8765 				{
8766 					TransactionStmt *n = makeNode(TransactionStmt);
8767 					n->kind = TRANS_STMT_RELEASE;
8768 					n->options = list_make1(makeDefElem("savepoint_name",
8769 														(Node *)makeString($3)));
8770 					$$ = (Node *)n;
8771 				}
8772 			| RELEASE ColId
8773 				{
8774 					TransactionStmt *n = makeNode(TransactionStmt);
8775 					n->kind = TRANS_STMT_RELEASE;
8776 					n->options = list_make1(makeDefElem("savepoint_name",
8777 														(Node *)makeString($2)));
8778 					$$ = (Node *)n;
8779 				}
8780 			| ROLLBACK opt_transaction TO SAVEPOINT ColId
8781 				{
8782 					TransactionStmt *n = makeNode(TransactionStmt);
8783 					n->kind = TRANS_STMT_ROLLBACK_TO;
8784 					n->options = list_make1(makeDefElem("savepoint_name",
8785 														(Node *)makeString($5)));
8786 					$$ = (Node *)n;
8787 				}
8788 			| ROLLBACK opt_transaction TO ColId
8789 				{
8790 					TransactionStmt *n = makeNode(TransactionStmt);
8791 					n->kind = TRANS_STMT_ROLLBACK_TO;
8792 					n->options = list_make1(makeDefElem("savepoint_name",
8793 														(Node *)makeString($4)));
8794 					$$ = (Node *)n;
8795 				}
8796 			| PREPARE TRANSACTION Sconst
8797 				{
8798 					TransactionStmt *n = makeNode(TransactionStmt);
8799 					n->kind = TRANS_STMT_PREPARE;
8800 					n->gid = $3;
8801 					$$ = (Node *)n;
8802 				}
8803 			| COMMIT PREPARED Sconst
8804 				{
8805 					TransactionStmt *n = makeNode(TransactionStmt);
8806 					n->kind = TRANS_STMT_COMMIT_PREPARED;
8807 					n->gid = $3;
8808 					$$ = (Node *)n;
8809 				}
8810 			| ROLLBACK PREPARED Sconst
8811 				{
8812 					TransactionStmt *n = makeNode(TransactionStmt);
8813 					n->kind = TRANS_STMT_ROLLBACK_PREPARED;
8814 					n->gid = $3;
8815 					$$ = (Node *)n;
8816 				}
8817 		;
8818 
8819 opt_transaction:	WORK							{}
8820 			| TRANSACTION							{}
8821 			| /*EMPTY*/								{}
8822 		;
8823 
8824 transaction_mode_item:
8825 			ISOLATION LEVEL iso_level
8826 					{ $$ = makeDefElem("transaction_isolation",
8827 									   makeStringConst($3, @3)); }
8828 			| READ ONLY
8829 					{ $$ = makeDefElem("transaction_read_only",
8830 									   makeIntConst(TRUE, @1)); }
8831 			| READ WRITE
8832 					{ $$ = makeDefElem("transaction_read_only",
8833 									   makeIntConst(FALSE, @1)); }
8834 			| DEFERRABLE
8835 					{ $$ = makeDefElem("transaction_deferrable",
8836 									   makeIntConst(TRUE, @1)); }
8837 			| NOT DEFERRABLE
8838 					{ $$ = makeDefElem("transaction_deferrable",
8839 									   makeIntConst(FALSE, @1)); }
8840 		;
8841 
8842 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
8843 transaction_mode_list:
8844 			transaction_mode_item
8845 					{ $$ = list_make1($1); }
8846 			| transaction_mode_list ',' transaction_mode_item
8847 					{ $$ = lappend($1, $3); }
8848 			| transaction_mode_list transaction_mode_item
8849 					{ $$ = lappend($1, $2); }
8850 		;
8851 
8852 transaction_mode_list_or_empty:
8853 			transaction_mode_list
8854 			| /* EMPTY */
8855 					{ $$ = NIL; }
8856 		;
8857 
8858 
8859 /*****************************************************************************
8860  *
8861  *	QUERY:
8862  *		CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
8863  *			AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
8864  *
8865  *****************************************************************************/
8866 
8867 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list opt_reloptions
8868 				AS SelectStmt opt_check_option
8869 				{
8870 					ViewStmt *n = makeNode(ViewStmt);
8871 					n->view = $4;
8872 					n->view->relpersistence = $2;
8873 					n->aliases = $5;
8874 					n->query = $8;
8875 					n->replace = false;
8876 					n->options = $6;
8877 					n->withCheckOption = $9;
8878 					$$ = (Node *) n;
8879 				}
8880 		| CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list opt_reloptions
8881 				AS SelectStmt opt_check_option
8882 				{
8883 					ViewStmt *n = makeNode(ViewStmt);
8884 					n->view = $6;
8885 					n->view->relpersistence = $4;
8886 					n->aliases = $7;
8887 					n->query = $10;
8888 					n->replace = true;
8889 					n->options = $8;
8890 					n->withCheckOption = $11;
8891 					$$ = (Node *) n;
8892 				}
8893 		| CREATE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
8894 				AS SelectStmt opt_check_option
8895 				{
8896 					ViewStmt *n = makeNode(ViewStmt);
8897 					n->view = $5;
8898 					n->view->relpersistence = $2;
8899 					n->aliases = $7;
8900 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $11);
8901 					n->replace = false;
8902 					n->options = $9;
8903 					n->withCheckOption = $12;
8904 					if (n->withCheckOption != NO_CHECK_OPTION)
8905 						ereport(ERROR,
8906 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8907 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
8908 								 parser_errposition(@12)));
8909 					$$ = (Node *) n;
8910 				}
8911 		| CREATE OR REPLACE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
8912 				AS SelectStmt opt_check_option
8913 				{
8914 					ViewStmt *n = makeNode(ViewStmt);
8915 					n->view = $7;
8916 					n->view->relpersistence = $4;
8917 					n->aliases = $9;
8918 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $13);
8919 					n->replace = true;
8920 					n->options = $11;
8921 					n->withCheckOption = $14;
8922 					if (n->withCheckOption != NO_CHECK_OPTION)
8923 						ereport(ERROR,
8924 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8925 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
8926 								 parser_errposition(@14)));
8927 					$$ = (Node *) n;
8928 				}
8929 		;
8930 
8931 opt_check_option:
8932 		WITH CHECK OPTION				{ $$ = CASCADED_CHECK_OPTION; }
8933 		| WITH CASCADED CHECK OPTION	{ $$ = CASCADED_CHECK_OPTION; }
8934 		| WITH LOCAL CHECK OPTION		{ $$ = LOCAL_CHECK_OPTION; }
8935 		| /* EMPTY */					{ $$ = NO_CHECK_OPTION; }
8936 		;
8937 
8938 /*****************************************************************************
8939  *
8940  *		QUERY:
8941  *				LOAD "filename"
8942  *
8943  *****************************************************************************/
8944 
8945 LoadStmt:	LOAD file_name
8946 				{
8947 					LoadStmt *n = makeNode(LoadStmt);
8948 					n->filename = $2;
8949 					$$ = (Node *)n;
8950 				}
8951 		;
8952 
8953 
8954 /*****************************************************************************
8955  *
8956  *		CREATE DATABASE
8957  *
8958  *****************************************************************************/
8959 
8960 CreatedbStmt:
8961 			CREATE DATABASE database_name opt_with createdb_opt_list
8962 				{
8963 					CreatedbStmt *n = makeNode(CreatedbStmt);
8964 					n->dbname = $3;
8965 					n->options = $5;
8966 					$$ = (Node *)n;
8967 				}
8968 		;
8969 
8970 createdb_opt_list:
8971 			createdb_opt_items						{ $$ = $1; }
8972 			| /* EMPTY */							{ $$ = NIL; }
8973 		;
8974 
8975 createdb_opt_items:
8976 			createdb_opt_item						{ $$ = list_make1($1); }
8977 			| createdb_opt_items createdb_opt_item	{ $$ = lappend($1, $2); }
8978 		;
8979 
8980 createdb_opt_item:
8981 			createdb_opt_name opt_equal SignedIconst
8982 				{
8983 					$$ = makeDefElem($1, (Node *)makeInteger($3));
8984 				}
8985 			| createdb_opt_name opt_equal opt_boolean_or_string
8986 				{
8987 					$$ = makeDefElem($1, (Node *)makeString($3));
8988 				}
8989 			| createdb_opt_name opt_equal DEFAULT
8990 				{
8991 					$$ = makeDefElem($1, NULL);
8992 				}
8993 		;
8994 
8995 /*
8996  * Ideally we'd use ColId here, but that causes shift/reduce conflicts against
8997  * the ALTER DATABASE SET/RESET syntaxes.  Instead call out specific keywords
8998  * we need, and allow IDENT so that database option names don't have to be
8999  * parser keywords unless they are already keywords for other reasons.
9000  *
9001  * XXX this coding technique is fragile since if someone makes a formerly
9002  * non-keyword option name into a keyword and forgets to add it here, the
9003  * option will silently break.  Best defense is to provide a regression test
9004  * exercising every such option, at least at the syntax level.
9005  */
9006 createdb_opt_name:
9007 			IDENT							{ $$ = $1; }
9008 			| CONNECTION LIMIT				{ $$ = pstrdup("connection_limit"); }
9009 			| ENCODING						{ $$ = pstrdup($1); }
9010 			| LOCATION						{ $$ = pstrdup($1); }
9011 			| OWNER							{ $$ = pstrdup($1); }
9012 			| TABLESPACE					{ $$ = pstrdup($1); }
9013 			| TEMPLATE						{ $$ = pstrdup($1); }
9014 		;
9015 
9016 /*
9017  *	Though the equals sign doesn't match other WITH options, pg_dump uses
9018  *	equals for backward compatibility, and it doesn't seem worth removing it.
9019  */
9020 opt_equal:	'='										{}
9021 			| /*EMPTY*/								{}
9022 		;
9023 
9024 
9025 /*****************************************************************************
9026  *
9027  *		ALTER DATABASE
9028  *
9029  *****************************************************************************/
9030 
9031 AlterDatabaseStmt:
9032 			ALTER DATABASE database_name WITH createdb_opt_list
9033 				 {
9034 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9035 					n->dbname = $3;
9036 					n->options = $5;
9037 					$$ = (Node *)n;
9038 				 }
9039 			| ALTER DATABASE database_name createdb_opt_list
9040 				 {
9041 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9042 					n->dbname = $3;
9043 					n->options = $4;
9044 					$$ = (Node *)n;
9045 				 }
9046 			| ALTER DATABASE database_name SET TABLESPACE name
9047 				 {
9048 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9049 					n->dbname = $3;
9050 					n->options = list_make1(makeDefElem("tablespace",
9051 													(Node *)makeString($6)));
9052 					$$ = (Node *)n;
9053 				 }
9054 		;
9055 
9056 AlterDatabaseSetStmt:
9057 			ALTER DATABASE database_name SetResetClause
9058 				{
9059 					AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
9060 					n->dbname = $3;
9061 					n->setstmt = $4;
9062 					$$ = (Node *)n;
9063 				}
9064 		;
9065 
9066 
9067 /*****************************************************************************
9068  *
9069  *		DROP DATABASE [ IF EXISTS ]
9070  *
9071  * This is implicitly CASCADE, no need for drop behavior
9072  *****************************************************************************/
9073 
9074 DropdbStmt: DROP DATABASE database_name
9075 				{
9076 					DropdbStmt *n = makeNode(DropdbStmt);
9077 					n->dbname = $3;
9078 					n->missing_ok = FALSE;
9079 					$$ = (Node *)n;
9080 				}
9081 			| DROP DATABASE IF_P EXISTS database_name
9082 				{
9083 					DropdbStmt *n = makeNode(DropdbStmt);
9084 					n->dbname = $5;
9085 					n->missing_ok = TRUE;
9086 					$$ = (Node *)n;
9087 				}
9088 		;
9089 
9090 
9091 /*****************************************************************************
9092  *
9093  *		ALTER SYSTEM
9094  *
9095  * This is used to change configuration parameters persistently.
9096  *****************************************************************************/
9097 
9098 AlterSystemStmt:
9099 			ALTER SYSTEM_P SET generic_set
9100 				{
9101 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
9102 					n->setstmt = $4;
9103 					$$ = (Node *)n;
9104 				}
9105 			| ALTER SYSTEM_P RESET generic_reset
9106 				{
9107 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
9108 					n->setstmt = $4;
9109 					$$ = (Node *)n;
9110 				}
9111 		;
9112 
9113 
9114 /*****************************************************************************
9115  *
9116  * Manipulate a domain
9117  *
9118  *****************************************************************************/
9119 
9120 CreateDomainStmt:
9121 			CREATE DOMAIN_P any_name opt_as Typename ColQualList
9122 				{
9123 					CreateDomainStmt *n = makeNode(CreateDomainStmt);
9124 					n->domainname = $3;
9125 					n->typeName = $5;
9126 					SplitColQualList($6, &n->constraints, &n->collClause,
9127 									 yyscanner);
9128 					$$ = (Node *)n;
9129 				}
9130 		;
9131 
9132 AlterDomainStmt:
9133 			/* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
9134 			ALTER DOMAIN_P any_name alter_column_default
9135 				{
9136 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9137 					n->subtype = 'T';
9138 					n->typeName = $3;
9139 					n->def = $4;
9140 					$$ = (Node *)n;
9141 				}
9142 			/* ALTER DOMAIN <domain> DROP NOT NULL */
9143 			| ALTER DOMAIN_P any_name DROP NOT NULL_P
9144 				{
9145 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9146 					n->subtype = 'N';
9147 					n->typeName = $3;
9148 					$$ = (Node *)n;
9149 				}
9150 			/* ALTER DOMAIN <domain> SET NOT NULL */
9151 			| ALTER DOMAIN_P any_name SET NOT NULL_P
9152 				{
9153 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9154 					n->subtype = 'O';
9155 					n->typeName = $3;
9156 					$$ = (Node *)n;
9157 				}
9158 			/* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
9159 			| ALTER DOMAIN_P any_name ADD_P TableConstraint
9160 				{
9161 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9162 					n->subtype = 'C';
9163 					n->typeName = $3;
9164 					n->def = $5;
9165 					$$ = (Node *)n;
9166 				}
9167 			/* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
9168 			| ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
9169 				{
9170 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9171 					n->subtype = 'X';
9172 					n->typeName = $3;
9173 					n->name = $6;
9174 					n->behavior = $7;
9175 					n->missing_ok = false;
9176 					$$ = (Node *)n;
9177 				}
9178 			/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
9179 			| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
9180 				{
9181 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9182 					n->subtype = 'X';
9183 					n->typeName = $3;
9184 					n->name = $8;
9185 					n->behavior = $9;
9186 					n->missing_ok = true;
9187 					$$ = (Node *)n;
9188 				}
9189 			/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
9190 			| ALTER DOMAIN_P any_name VALIDATE CONSTRAINT name
9191 				{
9192 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9193 					n->subtype = 'V';
9194 					n->typeName = $3;
9195 					n->name = $6;
9196 					$$ = (Node *)n;
9197 				}
9198 			;
9199 
9200 opt_as:		AS										{}
9201 			| /* EMPTY */							{}
9202 		;
9203 
9204 
9205 /*****************************************************************************
9206  *
9207  * Manipulate a text search dictionary or configuration
9208  *
9209  *****************************************************************************/
9210 
9211 AlterTSDictionaryStmt:
9212 			ALTER TEXT_P SEARCH DICTIONARY any_name definition
9213 				{
9214 					AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
9215 					n->dictname = $5;
9216 					n->options = $6;
9217 					$$ = (Node *)n;
9218 				}
9219 		;
9220 
9221 AlterTSConfigurationStmt:
9222 			ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list any_with any_name_list
9223 				{
9224 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9225 					n->kind = ALTER_TSCONFIG_ADD_MAPPING;
9226 					n->cfgname = $5;
9227 					n->tokentype = $9;
9228 					n->dicts = $11;
9229 					n->override = false;
9230 					n->replace = false;
9231 					$$ = (Node*)n;
9232 				}
9233 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list any_with any_name_list
9234 				{
9235 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9236 					n->kind = ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN;
9237 					n->cfgname = $5;
9238 					n->tokentype = $9;
9239 					n->dicts = $11;
9240 					n->override = true;
9241 					n->replace = false;
9242 					$$ = (Node*)n;
9243 				}
9244 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name any_with any_name
9245 				{
9246 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9247 					n->kind = ALTER_TSCONFIG_REPLACE_DICT;
9248 					n->cfgname = $5;
9249 					n->tokentype = NIL;
9250 					n->dicts = list_make2($9,$11);
9251 					n->override = false;
9252 					n->replace = true;
9253 					$$ = (Node*)n;
9254 				}
9255 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name any_with any_name
9256 				{
9257 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9258 					n->kind = ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN;
9259 					n->cfgname = $5;
9260 					n->tokentype = $9;
9261 					n->dicts = list_make2($11,$13);
9262 					n->override = false;
9263 					n->replace = true;
9264 					$$ = (Node*)n;
9265 				}
9266 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
9267 				{
9268 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9269 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
9270 					n->cfgname = $5;
9271 					n->tokentype = $9;
9272 					n->missing_ok = false;
9273 					$$ = (Node*)n;
9274 				}
9275 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
9276 				{
9277 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
9278 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
9279 					n->cfgname = $5;
9280 					n->tokentype = $11;
9281 					n->missing_ok = true;
9282 					$$ = (Node*)n;
9283 				}
9284 		;
9285 
9286 /* Use this if TIME or ORDINALITY after WITH should be taken as an identifier */
9287 any_with:	WITH									{}
9288 			| WITH_LA								{}
9289 		;
9290 
9291 
9292 /*****************************************************************************
9293  *
9294  * Manipulate a conversion
9295  *
9296  *		CREATE [DEFAULT] CONVERSION <conversion_name>
9297  *		FOR <encoding_name> TO <encoding_name> FROM <func_name>
9298  *
9299  *****************************************************************************/
9300 
9301 CreateConversionStmt:
9302 			CREATE opt_default CONVERSION_P any_name FOR Sconst
9303 			TO Sconst FROM any_name
9304 			{
9305 				CreateConversionStmt *n = makeNode(CreateConversionStmt);
9306 				n->conversion_name = $4;
9307 				n->for_encoding_name = $6;
9308 				n->to_encoding_name = $8;
9309 				n->func_name = $10;
9310 				n->def = $2;
9311 				$$ = (Node *)n;
9312 			}
9313 		;
9314 
9315 /*****************************************************************************
9316  *
9317  *		QUERY:
9318  *				CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
9319  *				CLUSTER [VERBOSE]
9320  *				CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
9321  *
9322  *****************************************************************************/
9323 
9324 ClusterStmt:
9325 			CLUSTER opt_verbose qualified_name cluster_index_specification
9326 				{
9327 					ClusterStmt *n = makeNode(ClusterStmt);
9328 					n->relation = $3;
9329 					n->indexname = $4;
9330 					n->verbose = $2;
9331 					$$ = (Node*)n;
9332 				}
9333 			| CLUSTER opt_verbose
9334 				{
9335 					ClusterStmt *n = makeNode(ClusterStmt);
9336 					n->relation = NULL;
9337 					n->indexname = NULL;
9338 					n->verbose = $2;
9339 					$$ = (Node*)n;
9340 				}
9341 			/* kept for pre-8.3 compatibility */
9342 			| CLUSTER opt_verbose index_name ON qualified_name
9343 				{
9344 					ClusterStmt *n = makeNode(ClusterStmt);
9345 					n->relation = $5;
9346 					n->indexname = $3;
9347 					n->verbose = $2;
9348 					$$ = (Node*)n;
9349 				}
9350 		;
9351 
9352 cluster_index_specification:
9353 			USING index_name		{ $$ = $2; }
9354 			| /*EMPTY*/				{ $$ = NULL; }
9355 		;
9356 
9357 
9358 /*****************************************************************************
9359  *
9360  *		QUERY:
9361  *				VACUUM
9362  *				ANALYZE
9363  *
9364  *****************************************************************************/
9365 
9366 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
9367 				{
9368 					VacuumStmt *n = makeNode(VacuumStmt);
9369 					n->options = VACOPT_VACUUM;
9370 					if ($2)
9371 						n->options |= VACOPT_FULL;
9372 					if ($3)
9373 						n->options |= VACOPT_FREEZE;
9374 					if ($4)
9375 						n->options |= VACOPT_VERBOSE;
9376 					n->relation = NULL;
9377 					n->va_cols = NIL;
9378 					$$ = (Node *)n;
9379 				}
9380 			| VACUUM opt_full opt_freeze opt_verbose qualified_name
9381 				{
9382 					VacuumStmt *n = makeNode(VacuumStmt);
9383 					n->options = VACOPT_VACUUM;
9384 					if ($2)
9385 						n->options |= VACOPT_FULL;
9386 					if ($3)
9387 						n->options |= VACOPT_FREEZE;
9388 					if ($4)
9389 						n->options |= VACOPT_VERBOSE;
9390 					n->relation = $5;
9391 					n->va_cols = NIL;
9392 					$$ = (Node *)n;
9393 				}
9394 			| VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
9395 				{
9396 					VacuumStmt *n = (VacuumStmt *) $5;
9397 					n->options |= VACOPT_VACUUM;
9398 					if ($2)
9399 						n->options |= VACOPT_FULL;
9400 					if ($3)
9401 						n->options |= VACOPT_FREEZE;
9402 					if ($4)
9403 						n->options |= VACOPT_VERBOSE;
9404 					$$ = (Node *)n;
9405 				}
9406 			| VACUUM '(' vacuum_option_list ')'
9407 				{
9408 					VacuumStmt *n = makeNode(VacuumStmt);
9409 					n->options = VACOPT_VACUUM | $3;
9410 					n->relation = NULL;
9411 					n->va_cols = NIL;
9412 					$$ = (Node *) n;
9413 				}
9414 			| VACUUM '(' vacuum_option_list ')' qualified_name opt_name_list
9415 				{
9416 					VacuumStmt *n = makeNode(VacuumStmt);
9417 					n->options = VACOPT_VACUUM | $3;
9418 					n->relation = $5;
9419 					n->va_cols = $6;
9420 					if (n->va_cols != NIL)	/* implies analyze */
9421 						n->options |= VACOPT_ANALYZE;
9422 					$$ = (Node *) n;
9423 				}
9424 		;
9425 
9426 vacuum_option_list:
9427 			vacuum_option_elem								{ $$ = $1; }
9428 			| vacuum_option_list ',' vacuum_option_elem		{ $$ = $1 | $3; }
9429 		;
9430 
9431 vacuum_option_elem:
9432 			analyze_keyword		{ $$ = VACOPT_ANALYZE; }
9433 			| VERBOSE			{ $$ = VACOPT_VERBOSE; }
9434 			| FREEZE			{ $$ = VACOPT_FREEZE; }
9435 			| FULL				{ $$ = VACOPT_FULL; }
9436 			| IDENT
9437 				{
9438 					if (strcmp($1, "disable_page_skipping") == 0)
9439 						$$ = VACOPT_DISABLE_PAGE_SKIPPING;
9440 					else
9441 						ereport(ERROR,
9442 								(errcode(ERRCODE_SYNTAX_ERROR),
9443 							 errmsg("unrecognized VACUUM option \"%s\"", $1),
9444 									 parser_errposition(@1)));
9445 				}
9446 		;
9447 
9448 AnalyzeStmt:
9449 			analyze_keyword opt_verbose
9450 				{
9451 					VacuumStmt *n = makeNode(VacuumStmt);
9452 					n->options = VACOPT_ANALYZE;
9453 					if ($2)
9454 						n->options |= VACOPT_VERBOSE;
9455 					n->relation = NULL;
9456 					n->va_cols = NIL;
9457 					$$ = (Node *)n;
9458 				}
9459 			| analyze_keyword opt_verbose qualified_name opt_name_list
9460 				{
9461 					VacuumStmt *n = makeNode(VacuumStmt);
9462 					n->options = VACOPT_ANALYZE;
9463 					if ($2)
9464 						n->options |= VACOPT_VERBOSE;
9465 					n->relation = $3;
9466 					n->va_cols = $4;
9467 					$$ = (Node *)n;
9468 				}
9469 		;
9470 
9471 analyze_keyword:
9472 			ANALYZE									{}
9473 			| ANALYSE /* British */					{}
9474 		;
9475 
9476 opt_verbose:
9477 			VERBOSE									{ $$ = TRUE; }
9478 			| /*EMPTY*/								{ $$ = FALSE; }
9479 		;
9480 
9481 opt_full:	FULL									{ $$ = TRUE; }
9482 			| /*EMPTY*/								{ $$ = FALSE; }
9483 		;
9484 
9485 opt_freeze: FREEZE									{ $$ = TRUE; }
9486 			| /*EMPTY*/								{ $$ = FALSE; }
9487 		;
9488 
9489 opt_name_list:
9490 			'(' name_list ')'						{ $$ = $2; }
9491 			| /*EMPTY*/								{ $$ = NIL; }
9492 		;
9493 
9494 
9495 /*****************************************************************************
9496  *
9497  *		QUERY:
9498  *				EXPLAIN [ANALYZE] [VERBOSE] query
9499  *				EXPLAIN ( options ) query
9500  *
9501  *****************************************************************************/
9502 
9503 ExplainStmt:
9504 		EXPLAIN ExplainableStmt
9505 				{
9506 					ExplainStmt *n = makeNode(ExplainStmt);
9507 					n->query = $2;
9508 					n->options = NIL;
9509 					$$ = (Node *) n;
9510 				}
9511 		| EXPLAIN analyze_keyword opt_verbose ExplainableStmt
9512 				{
9513 					ExplainStmt *n = makeNode(ExplainStmt);
9514 					n->query = $4;
9515 					n->options = list_make1(makeDefElem("analyze", NULL));
9516 					if ($3)
9517 						n->options = lappend(n->options,
9518 											 makeDefElem("verbose", NULL));
9519 					$$ = (Node *) n;
9520 				}
9521 		| EXPLAIN VERBOSE ExplainableStmt
9522 				{
9523 					ExplainStmt *n = makeNode(ExplainStmt);
9524 					n->query = $3;
9525 					n->options = list_make1(makeDefElem("verbose", NULL));
9526 					$$ = (Node *) n;
9527 				}
9528 		| EXPLAIN '(' explain_option_list ')' ExplainableStmt
9529 				{
9530 					ExplainStmt *n = makeNode(ExplainStmt);
9531 					n->query = $5;
9532 					n->options = $3;
9533 					$$ = (Node *) n;
9534 				}
9535 		;
9536 
9537 ExplainableStmt:
9538 			SelectStmt
9539 			| InsertStmt
9540 			| UpdateStmt
9541 			| DeleteStmt
9542 			| DeclareCursorStmt
9543 			| CreateAsStmt
9544 			| CreateMatViewStmt
9545 			| RefreshMatViewStmt
9546 			| ExecuteStmt					/* by default all are $$=$1 */
9547 		;
9548 
9549 explain_option_list:
9550 			explain_option_elem
9551 				{
9552 					$$ = list_make1($1);
9553 				}
9554 			| explain_option_list ',' explain_option_elem
9555 				{
9556 					$$ = lappend($1, $3);
9557 				}
9558 		;
9559 
9560 explain_option_elem:
9561 			explain_option_name explain_option_arg
9562 				{
9563 					$$ = makeDefElem($1, $2);
9564 				}
9565 		;
9566 
9567 explain_option_name:
9568 			NonReservedWord			{ $$ = $1; }
9569 			| analyze_keyword		{ $$ = "analyze"; }
9570 		;
9571 
9572 explain_option_arg:
9573 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
9574 			| NumericOnly			{ $$ = (Node *) $1; }
9575 			| /* EMPTY */			{ $$ = NULL; }
9576 		;
9577 
9578 /*****************************************************************************
9579  *
9580  *		QUERY:
9581  *				PREPARE <plan_name> [(args, ...)] AS <query>
9582  *
9583  *****************************************************************************/
9584 
9585 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
9586 				{
9587 					PrepareStmt *n = makeNode(PrepareStmt);
9588 					n->name = $2;
9589 					n->argtypes = $3;
9590 					n->query = $5;
9591 					$$ = (Node *) n;
9592 				}
9593 		;
9594 
9595 prep_type_clause: '(' type_list ')'			{ $$ = $2; }
9596 				| /* EMPTY */				{ $$ = NIL; }
9597 		;
9598 
9599 PreparableStmt:
9600 			SelectStmt
9601 			| InsertStmt
9602 			| UpdateStmt
9603 			| DeleteStmt					/* by default all are $$=$1 */
9604 		;
9605 
9606 /*****************************************************************************
9607  *
9608  * EXECUTE <plan_name> [(params, ...)]
9609  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
9610  *
9611  *****************************************************************************/
9612 
9613 ExecuteStmt: EXECUTE name execute_param_clause
9614 				{
9615 					ExecuteStmt *n = makeNode(ExecuteStmt);
9616 					n->name = $2;
9617 					n->params = $3;
9618 					$$ = (Node *) n;
9619 				}
9620 			| CREATE OptTemp TABLE create_as_target AS
9621 				EXECUTE name execute_param_clause opt_with_data
9622 				{
9623 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
9624 					ExecuteStmt *n = makeNode(ExecuteStmt);
9625 					n->name = $7;
9626 					n->params = $8;
9627 					ctas->query = (Node *) n;
9628 					ctas->into = $4;
9629 					ctas->relkind = OBJECT_TABLE;
9630 					ctas->is_select_into = false;
9631 					/* cram additional flags into the IntoClause */
9632 					$4->rel->relpersistence = $2;
9633 					$4->skipData = !($9);
9634 					$$ = (Node *) ctas;
9635 				}
9636 		;
9637 
9638 execute_param_clause: '(' expr_list ')'				{ $$ = $2; }
9639 					| /* EMPTY */					{ $$ = NIL; }
9640 					;
9641 
9642 /*****************************************************************************
9643  *
9644  *		QUERY:
9645  *				DEALLOCATE [PREPARE] <plan_name>
9646  *
9647  *****************************************************************************/
9648 
9649 DeallocateStmt: DEALLOCATE name
9650 					{
9651 						DeallocateStmt *n = makeNode(DeallocateStmt);
9652 						n->name = $2;
9653 						$$ = (Node *) n;
9654 					}
9655 				| DEALLOCATE PREPARE name
9656 					{
9657 						DeallocateStmt *n = makeNode(DeallocateStmt);
9658 						n->name = $3;
9659 						$$ = (Node *) n;
9660 					}
9661 				| DEALLOCATE ALL
9662 					{
9663 						DeallocateStmt *n = makeNode(DeallocateStmt);
9664 						n->name = NULL;
9665 						$$ = (Node *) n;
9666 					}
9667 				| DEALLOCATE PREPARE ALL
9668 					{
9669 						DeallocateStmt *n = makeNode(DeallocateStmt);
9670 						n->name = NULL;
9671 						$$ = (Node *) n;
9672 					}
9673 		;
9674 
9675 /*****************************************************************************
9676  *
9677  *		QUERY:
9678  *				INSERT STATEMENTS
9679  *
9680  *****************************************************************************/
9681 
9682 InsertStmt:
9683 			opt_with_clause INSERT INTO insert_target insert_rest
9684 			opt_on_conflict returning_clause
9685 				{
9686 					$5->relation = $4;
9687 					$5->onConflictClause = $6;
9688 					$5->returningList = $7;
9689 					$5->withClause = $1;
9690 					$$ = (Node *) $5;
9691 				}
9692 		;
9693 
9694 /*
9695  * Can't easily make AS optional here, because VALUES in insert_rest would
9696  * have a shift/reduce conflict with VALUES as an optional alias.  We could
9697  * easily allow unreserved_keywords as optional aliases, but that'd be an odd
9698  * divergence from other places.  So just require AS for now.
9699  */
9700 insert_target:
9701 			qualified_name
9702 				{
9703 					$$ = $1;
9704 				}
9705 			| qualified_name AS ColId
9706 				{
9707 					$1->alias = makeAlias($3, NIL);
9708 					$$ = $1;
9709 				}
9710 		;
9711 
9712 insert_rest:
9713 			SelectStmt
9714 				{
9715 					$$ = makeNode(InsertStmt);
9716 					$$->cols = NIL;
9717 					$$->selectStmt = $1;
9718 				}
9719 			| '(' insert_column_list ')' SelectStmt
9720 				{
9721 					$$ = makeNode(InsertStmt);
9722 					$$->cols = $2;
9723 					$$->selectStmt = $4;
9724 				}
9725 			| DEFAULT VALUES
9726 				{
9727 					$$ = makeNode(InsertStmt);
9728 					$$->cols = NIL;
9729 					$$->selectStmt = NULL;
9730 				}
9731 		;
9732 
9733 insert_column_list:
9734 			insert_column_item
9735 					{ $$ = list_make1($1); }
9736 			| insert_column_list ',' insert_column_item
9737 					{ $$ = lappend($1, $3); }
9738 		;
9739 
9740 insert_column_item:
9741 			ColId opt_indirection
9742 				{
9743 					$$ = makeNode(ResTarget);
9744 					$$->name = $1;
9745 					$$->indirection = check_indirection($2, yyscanner);
9746 					$$->val = NULL;
9747 					$$->location = @1;
9748 				}
9749 		;
9750 
9751 opt_on_conflict:
9752 			ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list	where_clause
9753 				{
9754 					$$ = makeNode(OnConflictClause);
9755 					$$->action = ONCONFLICT_UPDATE;
9756 					$$->infer = $3;
9757 					$$->targetList = $7;
9758 					$$->whereClause = $8;
9759 					$$->location = @1;
9760 				}
9761 			|
9762 			ON CONFLICT opt_conf_expr DO NOTHING
9763 				{
9764 					$$ = makeNode(OnConflictClause);
9765 					$$->action = ONCONFLICT_NOTHING;
9766 					$$->infer = $3;
9767 					$$->targetList = NIL;
9768 					$$->whereClause = NULL;
9769 					$$->location = @1;
9770 				}
9771 			| /*EMPTY*/
9772 				{
9773 					$$ = NULL;
9774 				}
9775 		;
9776 
9777 opt_conf_expr:
9778 			'(' index_params ')' where_clause
9779 				{
9780 					$$ = makeNode(InferClause);
9781 					$$->indexElems = $2;
9782 					$$->whereClause = $4;
9783 					$$->conname = NULL;
9784 					$$->location = @1;
9785 				}
9786 			|
9787 			ON CONSTRAINT name
9788 				{
9789 					$$ = makeNode(InferClause);
9790 					$$->indexElems = NIL;
9791 					$$->whereClause = NULL;
9792 					$$->conname = $3;
9793 					$$->location = @1;
9794 				}
9795 			| /*EMPTY*/
9796 				{
9797 					$$ = NULL;
9798 				}
9799 		;
9800 
9801 returning_clause:
9802 			RETURNING target_list		{ $$ = $2; }
9803 			| /* EMPTY */				{ $$ = NIL; }
9804 		;
9805 
9806 
9807 /*****************************************************************************
9808  *
9809  *		QUERY:
9810  *				DELETE STATEMENTS
9811  *
9812  *****************************************************************************/
9813 
9814 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
9815 			using_clause where_or_current_clause returning_clause
9816 				{
9817 					DeleteStmt *n = makeNode(DeleteStmt);
9818 					n->relation = $4;
9819 					n->usingClause = $5;
9820 					n->whereClause = $6;
9821 					n->returningList = $7;
9822 					n->withClause = $1;
9823 					$$ = (Node *)n;
9824 				}
9825 		;
9826 
9827 using_clause:
9828 				USING from_list						{ $$ = $2; }
9829 			| /*EMPTY*/								{ $$ = NIL; }
9830 		;
9831 
9832 
9833 /*****************************************************************************
9834  *
9835  *		QUERY:
9836  *				LOCK TABLE
9837  *
9838  *****************************************************************************/
9839 
9840 LockStmt:	LOCK_P opt_table relation_expr_list opt_lock opt_nowait
9841 				{
9842 					LockStmt *n = makeNode(LockStmt);
9843 
9844 					n->relations = $3;
9845 					n->mode = $4;
9846 					n->nowait = $5;
9847 					$$ = (Node *)n;
9848 				}
9849 		;
9850 
9851 opt_lock:	IN_P lock_type MODE				{ $$ = $2; }
9852 			| /*EMPTY*/						{ $$ = AccessExclusiveLock; }
9853 		;
9854 
9855 lock_type:	ACCESS SHARE					{ $$ = AccessShareLock; }
9856 			| ROW SHARE						{ $$ = RowShareLock; }
9857 			| ROW EXCLUSIVE					{ $$ = RowExclusiveLock; }
9858 			| SHARE UPDATE EXCLUSIVE		{ $$ = ShareUpdateExclusiveLock; }
9859 			| SHARE							{ $$ = ShareLock; }
9860 			| SHARE ROW EXCLUSIVE			{ $$ = ShareRowExclusiveLock; }
9861 			| EXCLUSIVE						{ $$ = ExclusiveLock; }
9862 			| ACCESS EXCLUSIVE				{ $$ = AccessExclusiveLock; }
9863 		;
9864 
9865 opt_nowait:	NOWAIT							{ $$ = TRUE; }
9866 			| /*EMPTY*/						{ $$ = FALSE; }
9867 		;
9868 
9869 opt_nowait_or_skip:
9870 			NOWAIT							{ $$ = LockWaitError; }
9871 			| SKIP LOCKED					{ $$ = LockWaitSkip; }
9872 			| /*EMPTY*/						{ $$ = LockWaitBlock; }
9873 		;
9874 
9875 
9876 /*****************************************************************************
9877  *
9878  *		QUERY:
9879  *				UpdateStmt (UPDATE)
9880  *
9881  *****************************************************************************/
9882 
9883 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
9884 			SET set_clause_list
9885 			from_clause
9886 			where_or_current_clause
9887 			returning_clause
9888 				{
9889 					UpdateStmt *n = makeNode(UpdateStmt);
9890 					n->relation = $3;
9891 					n->targetList = $5;
9892 					n->fromClause = $6;
9893 					n->whereClause = $7;
9894 					n->returningList = $8;
9895 					n->withClause = $1;
9896 					$$ = (Node *)n;
9897 				}
9898 		;
9899 
9900 set_clause_list:
9901 			set_clause							{ $$ = $1; }
9902 			| set_clause_list ',' set_clause	{ $$ = list_concat($1,$3); }
9903 		;
9904 
9905 set_clause:
9906 			single_set_clause						{ $$ = list_make1($1); }
9907 			| multiple_set_clause					{ $$ = $1; }
9908 		;
9909 
9910 single_set_clause:
9911 			set_target '=' ctext_expr
9912 				{
9913 					$$ = $1;
9914 					$$->val = (Node *) $3;
9915 				}
9916 		;
9917 
9918 /*
9919  * Ideally, we'd accept any row-valued a_expr as RHS of a multiple_set_clause.
9920  * However, per SQL spec the row-constructor case must allow DEFAULT as a row
9921  * member, and it's pretty unclear how to do that (unless perhaps we allow
9922  * DEFAULT in any a_expr and let parse analysis sort it out later?).  For the
9923  * moment, the planner/executor only support a subquery as a multiassignment
9924  * source anyhow, so we need only accept ctext_row and subqueries here.
9925  */
9926 multiple_set_clause:
9927 			'(' set_target_list ')' '=' ctext_row
9928 				{
9929 					ListCell *col_cell;
9930 					ListCell *val_cell;
9931 
9932 					/*
9933 					 * Break the ctext_row apart, merge individual expressions
9934 					 * into the destination ResTargets.  This is semantically
9935 					 * equivalent to, and much cheaper to process than, the
9936 					 * general case.
9937 					 */
9938 					if (list_length($2) != list_length($5))
9939 						ereport(ERROR,
9940 								(errcode(ERRCODE_SYNTAX_ERROR),
9941 								 errmsg("number of columns does not match number of values"),
9942 								 parser_errposition(@5)));
forboth(col_cell,$2,val_cell,$5)9943 					forboth(col_cell, $2, val_cell, $5)
9944 					{
9945 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
9946 						Node *res_val = (Node *) lfirst(val_cell);
9947 
9948 						res_col->val = res_val;
9949 					}
9950 
9951 					$$ = $2;
9952 				}
9953 			| '(' set_target_list ')' '=' select_with_parens
9954 				{
9955 					SubLink *sl = makeNode(SubLink);
9956 					int ncolumns = list_length($2);
9957 					int i = 1;
9958 					ListCell *col_cell;
9959 
9960 					/* First, convert bare SelectStmt into a SubLink */
9961 					sl->subLinkType = MULTIEXPR_SUBLINK;
9962 					sl->subLinkId = 0;		/* will be assigned later */
9963 					sl->testexpr = NULL;
9964 					sl->operName = NIL;
9965 					sl->subselect = $5;
9966 					sl->location = @5;
9967 
9968 					/* Create a MultiAssignRef source for each target */
foreach(col_cell,$2)9969 					foreach(col_cell, $2)
9970 					{
9971 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
9972 						MultiAssignRef *r = makeNode(MultiAssignRef);
9973 
9974 						r->source = (Node *) sl;
9975 						r->colno = i;
9976 						r->ncolumns = ncolumns;
9977 						res_col->val = (Node *) r;
9978 						i++;
9979 					}
9980 
9981 					$$ = $2;
9982 				}
9983 		;
9984 
9985 set_target:
9986 			ColId opt_indirection
9987 				{
9988 					$$ = makeNode(ResTarget);
9989 					$$->name = $1;
9990 					$$->indirection = check_indirection($2, yyscanner);
9991 					$$->val = NULL;	/* upper production sets this */
9992 					$$->location = @1;
9993 				}
9994 		;
9995 
9996 set_target_list:
9997 			set_target								{ $$ = list_make1($1); }
9998 			| set_target_list ',' set_target		{ $$ = lappend($1,$3); }
9999 		;
10000 
10001 
10002 /*****************************************************************************
10003  *
10004  *		QUERY:
10005  *				CURSOR STATEMENTS
10006  *
10007  *****************************************************************************/
10008 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
10009 				{
10010 					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
10011 					n->portalname = $2;
10012 					/* currently we always set FAST_PLAN option */
10013 					n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
10014 					n->query = $7;
10015 					$$ = (Node *)n;
10016 				}
10017 		;
10018 
10019 cursor_name:	name						{ $$ = $1; }
10020 		;
10021 
10022 cursor_options: /*EMPTY*/					{ $$ = 0; }
10023 			| cursor_options NO SCROLL		{ $$ = $1 | CURSOR_OPT_NO_SCROLL; }
10024 			| cursor_options SCROLL			{ $$ = $1 | CURSOR_OPT_SCROLL; }
10025 			| cursor_options BINARY			{ $$ = $1 | CURSOR_OPT_BINARY; }
10026 			| cursor_options INSENSITIVE	{ $$ = $1 | CURSOR_OPT_INSENSITIVE; }
10027 		;
10028 
10029 opt_hold: /* EMPTY */						{ $$ = 0; }
10030 			| WITH HOLD						{ $$ = CURSOR_OPT_HOLD; }
10031 			| WITHOUT HOLD					{ $$ = 0; }
10032 		;
10033 
10034 /*****************************************************************************
10035  *
10036  *		QUERY:
10037  *				SELECT STATEMENTS
10038  *
10039  *****************************************************************************/
10040 
10041 /* A complete SELECT statement looks like this.
10042  *
10043  * The rule returns either a single SelectStmt node or a tree of them,
10044  * representing a set-operation tree.
10045  *
10046  * There is an ambiguity when a sub-SELECT is within an a_expr and there
10047  * are excess parentheses: do the parentheses belong to the sub-SELECT or
10048  * to the surrounding a_expr?  We don't really care, but bison wants to know.
10049  * To resolve the ambiguity, we are careful to define the grammar so that
10050  * the decision is staved off as long as possible: as long as we can keep
10051  * absorbing parentheses into the sub-SELECT, we will do so, and only when
10052  * it's no longer possible to do that will we decide that parens belong to
10053  * the expression.	For example, in "SELECT (((SELECT 2)) + 3)" the extra
10054  * parentheses are treated as part of the sub-select.  The necessity of doing
10055  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".	Had we
10056  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
10057  * SELECT viewpoint when we see the UNION.
10058  *
10059  * This approach is implemented by defining a nonterminal select_with_parens,
10060  * which represents a SELECT with at least one outer layer of parentheses,
10061  * and being careful to use select_with_parens, never '(' SelectStmt ')',
10062  * in the expression grammar.  We will then have shift-reduce conflicts
10063  * which we can resolve in favor of always treating '(' <select> ')' as
10064  * a select_with_parens.  To resolve the conflicts, the productions that
10065  * conflict with the select_with_parens productions are manually given
10066  * precedences lower than the precedence of ')', thereby ensuring that we
10067  * shift ')' (and then reduce to select_with_parens) rather than trying to
10068  * reduce the inner <select> nonterminal to something else.  We use UMINUS
10069  * precedence for this, which is a fairly arbitrary choice.
10070  *
10071  * To be able to define select_with_parens itself without ambiguity, we need
10072  * a nonterminal select_no_parens that represents a SELECT structure with no
10073  * outermost parentheses.  This is a little bit tedious, but it works.
10074  *
10075  * In non-expression contexts, we use SelectStmt which can represent a SELECT
10076  * with or without outer parentheses.
10077  */
10078 
10079 SelectStmt: select_no_parens			%prec UMINUS
10080 			| select_with_parens		%prec UMINUS
10081 		;
10082 
10083 select_with_parens:
10084 			'(' select_no_parens ')'				{ $$ = $2; }
10085 			| '(' select_with_parens ')'			{ $$ = $2; }
10086 		;
10087 
10088 /*
10089  * This rule parses the equivalent of the standard's <query expression>.
10090  * The duplicative productions are annoying, but hard to get rid of without
10091  * creating shift/reduce conflicts.
10092  *
10093  *	The locking clause (FOR UPDATE etc) may be before or after LIMIT/OFFSET.
10094  *	In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
10095  *	We now support both orderings, but prefer LIMIT/OFFSET before the locking
10096  * clause.
10097  *	2002-08-28 bjm
10098  */
10099 select_no_parens:
10100 			simple_select						{ $$ = $1; }
10101 			| select_clause sort_clause
10102 				{
10103 					insertSelectOptions((SelectStmt *) $1, $2, NIL,
10104 										NULL, NULL, NULL,
10105 										yyscanner);
10106 					$$ = $1;
10107 				}
10108 			| select_clause opt_sort_clause for_locking_clause opt_select_limit
10109 				{
10110 					insertSelectOptions((SelectStmt *) $1, $2, $3,
10111 										list_nth($4, 0), list_nth($4, 1),
10112 										NULL,
10113 										yyscanner);
10114 					$$ = $1;
10115 				}
10116 			| select_clause opt_sort_clause select_limit opt_for_locking_clause
10117 				{
10118 					insertSelectOptions((SelectStmt *) $1, $2, $4,
10119 										list_nth($3, 0), list_nth($3, 1),
10120 										NULL,
10121 										yyscanner);
10122 					$$ = $1;
10123 				}
10124 			| with_clause select_clause
10125 				{
10126 					insertSelectOptions((SelectStmt *) $2, NULL, NIL,
10127 										NULL, NULL,
10128 										$1,
10129 										yyscanner);
10130 					$$ = $2;
10131 				}
10132 			| with_clause select_clause sort_clause
10133 				{
10134 					insertSelectOptions((SelectStmt *) $2, $3, NIL,
10135 										NULL, NULL,
10136 										$1,
10137 										yyscanner);
10138 					$$ = $2;
10139 				}
10140 			| with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
10141 				{
10142 					insertSelectOptions((SelectStmt *) $2, $3, $4,
10143 										list_nth($5, 0), list_nth($5, 1),
10144 										$1,
10145 										yyscanner);
10146 					$$ = $2;
10147 				}
10148 			| with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
10149 				{
10150 					insertSelectOptions((SelectStmt *) $2, $3, $5,
10151 										list_nth($4, 0), list_nth($4, 1),
10152 										$1,
10153 										yyscanner);
10154 					$$ = $2;
10155 				}
10156 		;
10157 
10158 select_clause:
10159 			simple_select							{ $$ = $1; }
10160 			| select_with_parens					{ $$ = $1; }
10161 		;
10162 
10163 /*
10164  * This rule parses SELECT statements that can appear within set operations,
10165  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
10166  * the ordering of the set operations.	Without '(' and ')' we want the
10167  * operations to be ordered per the precedence specs at the head of this file.
10168  *
10169  * As with select_no_parens, simple_select cannot have outer parentheses,
10170  * but can have parenthesized subclauses.
10171  *
10172  * Note that sort clauses cannot be included at this level --- SQL requires
10173  *		SELECT foo UNION SELECT bar ORDER BY baz
10174  * to be parsed as
10175  *		(SELECT foo UNION SELECT bar) ORDER BY baz
10176  * not
10177  *		SELECT foo UNION (SELECT bar ORDER BY baz)
10178  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
10179  * described as part of the select_no_parens production, not simple_select.
10180  * This does not limit functionality, because you can reintroduce these
10181  * clauses inside parentheses.
10182  *
10183  * NOTE: only the leftmost component SelectStmt should have INTO.
10184  * However, this is not checked by the grammar; parse analysis must check it.
10185  */
10186 simple_select:
10187 			SELECT opt_all_clause opt_target_list
10188 			into_clause from_clause where_clause
10189 			group_clause having_clause window_clause
10190 				{
10191 					SelectStmt *n = makeNode(SelectStmt);
10192 					n->targetList = $3;
10193 					n->intoClause = $4;
10194 					n->fromClause = $5;
10195 					n->whereClause = $6;
10196 					n->groupClause = $7;
10197 					n->havingClause = $8;
10198 					n->windowClause = $9;
10199 					$$ = (Node *)n;
10200 				}
10201 			| SELECT distinct_clause target_list
10202 			into_clause from_clause where_clause
10203 			group_clause having_clause window_clause
10204 				{
10205 					SelectStmt *n = makeNode(SelectStmt);
10206 					n->distinctClause = $2;
10207 					n->targetList = $3;
10208 					n->intoClause = $4;
10209 					n->fromClause = $5;
10210 					n->whereClause = $6;
10211 					n->groupClause = $7;
10212 					n->havingClause = $8;
10213 					n->windowClause = $9;
10214 					$$ = (Node *)n;
10215 				}
10216 			| values_clause							{ $$ = $1; }
10217 			| TABLE relation_expr
10218 				{
10219 					/* same as SELECT * FROM relation_expr */
10220 					ColumnRef *cr = makeNode(ColumnRef);
10221 					ResTarget *rt = makeNode(ResTarget);
10222 					SelectStmt *n = makeNode(SelectStmt);
10223 
10224 					cr->fields = list_make1(makeNode(A_Star));
10225 					cr->location = -1;
10226 
10227 					rt->name = NULL;
10228 					rt->indirection = NIL;
10229 					rt->val = (Node *)cr;
10230 					rt->location = -1;
10231 
10232 					n->targetList = list_make1(rt);
10233 					n->fromClause = list_make1($2);
10234 					$$ = (Node *)n;
10235 				}
10236 			| select_clause UNION all_or_distinct select_clause
10237 				{
10238 					$$ = makeSetOp(SETOP_UNION, $3, $1, $4);
10239 				}
10240 			| select_clause INTERSECT all_or_distinct select_clause
10241 				{
10242 					$$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
10243 				}
10244 			| select_clause EXCEPT all_or_distinct select_clause
10245 				{
10246 					$$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
10247 				}
10248 		;
10249 
10250 /*
10251  * SQL standard WITH clause looks like:
10252  *
10253  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
10254  *		AS (query) [ SEARCH or CYCLE clause ]
10255  *
10256  * We don't currently support the SEARCH or CYCLE clause.
10257  *
10258  * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY.
10259  */
10260 with_clause:
10261 		WITH cte_list
10262 			{
10263 				$$ = makeNode(WithClause);
10264 				$$->ctes = $2;
10265 				$$->recursive = false;
10266 				$$->location = @1;
10267 			}
10268 		| WITH_LA cte_list
10269 			{
10270 				$$ = makeNode(WithClause);
10271 				$$->ctes = $2;
10272 				$$->recursive = false;
10273 				$$->location = @1;
10274 			}
10275 		| WITH RECURSIVE cte_list
10276 			{
10277 				$$ = makeNode(WithClause);
10278 				$$->ctes = $3;
10279 				$$->recursive = true;
10280 				$$->location = @1;
10281 			}
10282 		;
10283 
10284 cte_list:
10285 		common_table_expr						{ $$ = list_make1($1); }
10286 		| cte_list ',' common_table_expr		{ $$ = lappend($1, $3); }
10287 		;
10288 
10289 common_table_expr:  name opt_name_list AS '(' PreparableStmt ')'
10290 			{
10291 				CommonTableExpr *n = makeNode(CommonTableExpr);
10292 				n->ctename = $1;
10293 				n->aliascolnames = $2;
10294 				n->ctequery = $5;
10295 				n->location = @1;
10296 				$$ = (Node *) n;
10297 			}
10298 		;
10299 
10300 opt_with_clause:
10301 		with_clause								{ $$ = $1; }
10302 		| /*EMPTY*/								{ $$ = NULL; }
10303 		;
10304 
10305 into_clause:
10306 			INTO OptTempTableName
10307 				{
10308 					$$ = makeNode(IntoClause);
10309 					$$->rel = $2;
10310 					$$->colNames = NIL;
10311 					$$->options = NIL;
10312 					$$->onCommit = ONCOMMIT_NOOP;
10313 					$$->tableSpaceName = NULL;
10314 					$$->viewQuery = NULL;
10315 					$$->skipData = false;
10316 				}
10317 			| /*EMPTY*/
10318 				{ $$ = NULL; }
10319 		;
10320 
10321 /*
10322  * Redundancy here is needed to avoid shift/reduce conflicts,
10323  * since TEMP is not a reserved word.  See also OptTemp.
10324  */
10325 OptTempTableName:
10326 			TEMPORARY opt_table qualified_name
10327 				{
10328 					$$ = $3;
10329 					$$->relpersistence = RELPERSISTENCE_TEMP;
10330 				}
10331 			| TEMP opt_table qualified_name
10332 				{
10333 					$$ = $3;
10334 					$$->relpersistence = RELPERSISTENCE_TEMP;
10335 				}
10336 			| LOCAL TEMPORARY opt_table qualified_name
10337 				{
10338 					$$ = $4;
10339 					$$->relpersistence = RELPERSISTENCE_TEMP;
10340 				}
10341 			| LOCAL TEMP opt_table qualified_name
10342 				{
10343 					$$ = $4;
10344 					$$->relpersistence = RELPERSISTENCE_TEMP;
10345 				}
10346 			| GLOBAL TEMPORARY opt_table qualified_name
10347 				{
10348 					ereport(WARNING,
10349 							(errmsg("GLOBAL is deprecated in temporary table creation"),
10350 							 parser_errposition(@1)));
10351 					$$ = $4;
10352 					$$->relpersistence = RELPERSISTENCE_TEMP;
10353 				}
10354 			| GLOBAL TEMP opt_table qualified_name
10355 				{
10356 					ereport(WARNING,
10357 							(errmsg("GLOBAL is deprecated in temporary table creation"),
10358 							 parser_errposition(@1)));
10359 					$$ = $4;
10360 					$$->relpersistence = RELPERSISTENCE_TEMP;
10361 				}
10362 			| UNLOGGED opt_table qualified_name
10363 				{
10364 					$$ = $3;
10365 					$$->relpersistence = RELPERSISTENCE_UNLOGGED;
10366 				}
10367 			| TABLE qualified_name
10368 				{
10369 					$$ = $2;
10370 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
10371 				}
10372 			| qualified_name
10373 				{
10374 					$$ = $1;
10375 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
10376 				}
10377 		;
10378 
10379 opt_table:	TABLE									{}
10380 			| /*EMPTY*/								{}
10381 		;
10382 
10383 all_or_distinct:
10384 			ALL										{ $$ = TRUE; }
10385 			| DISTINCT								{ $$ = FALSE; }
10386 			| /*EMPTY*/								{ $$ = FALSE; }
10387 		;
10388 
10389 /* We use (NIL) as a placeholder to indicate that all target expressions
10390  * should be placed in the DISTINCT list during parsetree analysis.
10391  */
10392 distinct_clause:
10393 			DISTINCT								{ $$ = list_make1(NIL); }
10394 			| DISTINCT ON '(' expr_list ')'			{ $$ = $4; }
10395 		;
10396 
10397 opt_all_clause:
10398 			ALL										{ $$ = NIL;}
10399 			| /*EMPTY*/								{ $$ = NIL; }
10400 		;
10401 
10402 opt_sort_clause:
10403 			sort_clause								{ $$ = $1;}
10404 			| /*EMPTY*/								{ $$ = NIL; }
10405 		;
10406 
10407 sort_clause:
10408 			ORDER BY sortby_list					{ $$ = $3; }
10409 		;
10410 
10411 sortby_list:
10412 			sortby									{ $$ = list_make1($1); }
10413 			| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
10414 		;
10415 
10416 sortby:		a_expr USING qual_all_Op opt_nulls_order
10417 				{
10418 					$$ = makeNode(SortBy);
10419 					$$->node = $1;
10420 					$$->sortby_dir = SORTBY_USING;
10421 					$$->sortby_nulls = $4;
10422 					$$->useOp = $3;
10423 					$$->location = @3;
10424 				}
10425 			| a_expr opt_asc_desc opt_nulls_order
10426 				{
10427 					$$ = makeNode(SortBy);
10428 					$$->node = $1;
10429 					$$->sortby_dir = $2;
10430 					$$->sortby_nulls = $3;
10431 					$$->useOp = NIL;
10432 					$$->location = -1;		/* no operator */
10433 				}
10434 		;
10435 
10436 
10437 select_limit:
10438 			limit_clause offset_clause			{ $$ = list_make2($2, $1); }
10439 			| offset_clause limit_clause		{ $$ = list_make2($1, $2); }
10440 			| limit_clause						{ $$ = list_make2(NULL, $1); }
10441 			| offset_clause						{ $$ = list_make2($1, NULL); }
10442 		;
10443 
10444 opt_select_limit:
10445 			select_limit						{ $$ = $1; }
10446 			| /* EMPTY */						{ $$ = list_make2(NULL,NULL); }
10447 		;
10448 
10449 limit_clause:
10450 			LIMIT select_limit_value
10451 				{ $$ = $2; }
10452 			| LIMIT select_limit_value ',' select_offset_value
10453 				{
10454 					/* Disabled because it was too confusing, bjm 2002-02-18 */
10455 					ereport(ERROR,
10456 							(errcode(ERRCODE_SYNTAX_ERROR),
10457 							 errmsg("LIMIT #,# syntax is not supported"),
10458 							 errhint("Use separate LIMIT and OFFSET clauses."),
10459 							 parser_errposition(@1)));
10460 				}
10461 			/* SQL:2008 syntax */
10462 			| FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
10463 				{ $$ = $3; }
10464 		;
10465 
10466 offset_clause:
10467 			OFFSET select_offset_value
10468 				{ $$ = $2; }
10469 			/* SQL:2008 syntax */
10470 			| OFFSET select_offset_value2 row_or_rows
10471 				{ $$ = $2; }
10472 		;
10473 
10474 select_limit_value:
10475 			a_expr									{ $$ = $1; }
10476 			| ALL
10477 				{
10478 					/* LIMIT ALL is represented as a NULL constant */
10479 					$$ = makeNullAConst(@1);
10480 				}
10481 		;
10482 
10483 select_offset_value:
10484 			a_expr									{ $$ = $1; }
10485 		;
10486 
10487 /*
10488  * Allowing full expressions without parentheses causes various parsing
10489  * problems with the trailing ROW/ROWS key words.  SQL only calls for
10490  * constants, so we allow the rest only with parentheses.  If omitted,
10491  * default to 1.
10492  */
10493 opt_select_fetch_first_value:
10494 			SignedIconst						{ $$ = makeIntConst($1, @1); }
10495 			| '(' a_expr ')'					{ $$ = $2; }
10496 			| /*EMPTY*/							{ $$ = makeIntConst(1, -1); }
10497 		;
10498 
10499 /*
10500  * Again, the trailing ROW/ROWS in this case prevent the full expression
10501  * syntax.  c_expr is the best we can do.
10502  */
10503 select_offset_value2:
10504 			c_expr									{ $$ = $1; }
10505 		;
10506 
10507 /* noise words */
10508 row_or_rows: ROW									{ $$ = 0; }
10509 			| ROWS									{ $$ = 0; }
10510 		;
10511 
10512 first_or_next: FIRST_P								{ $$ = 0; }
10513 			| NEXT									{ $$ = 0; }
10514 		;
10515 
10516 
10517 /*
10518  * This syntax for group_clause tries to follow the spec quite closely.
10519  * However, the spec allows only column references, not expressions,
10520  * which introduces an ambiguity between implicit row constructors
10521  * (a,b) and lists of column references.
10522  *
10523  * We handle this by using the a_expr production for what the spec calls
10524  * <ordinary grouping set>, which in the spec represents either one column
10525  * reference or a parenthesized list of column references. Then, we check the
10526  * top node of the a_expr to see if it's an implicit RowExpr, and if so, just
10527  * grab and use the list, discarding the node. (this is done in parse analysis,
10528  * not here)
10529  *
10530  * (we abuse the row_format field of RowExpr to distinguish implicit and
10531  * explicit row constructors; it's debatable if anyone sanely wants to use them
10532  * in a group clause, but if they have a reason to, we make it possible.)
10533  *
10534  * Each item in the group_clause list is either an expression tree or a
10535  * GroupingSet node of some type.
10536  */
10537 group_clause:
10538 			GROUP_P BY group_by_list				{ $$ = $3; }
10539 			| /*EMPTY*/								{ $$ = NIL; }
10540 		;
10541 
10542 group_by_list:
10543 			group_by_item							{ $$ = list_make1($1); }
10544 			| group_by_list ',' group_by_item		{ $$ = lappend($1,$3); }
10545 		;
10546 
10547 group_by_item:
10548 			a_expr									{ $$ = $1; }
10549 			| empty_grouping_set					{ $$ = $1; }
10550 			| cube_clause							{ $$ = $1; }
10551 			| rollup_clause							{ $$ = $1; }
10552 			| grouping_sets_clause					{ $$ = $1; }
10553 		;
10554 
10555 empty_grouping_set:
10556 			'(' ')'
10557 				{
10558 					$$ = (Node *) makeGroupingSet(GROUPING_SET_EMPTY, NIL, @1);
10559 				}
10560 		;
10561 
10562 /*
10563  * These hacks rely on setting precedence of CUBE and ROLLUP below that of '(',
10564  * so that they shift in these rules rather than reducing the conflicting
10565  * unreserved_keyword rule.
10566  */
10567 
10568 rollup_clause:
10569 			ROLLUP '(' expr_list ')'
10570 				{
10571 					$$ = (Node *) makeGroupingSet(GROUPING_SET_ROLLUP, $3, @1);
10572 				}
10573 		;
10574 
10575 cube_clause:
10576 			CUBE '(' expr_list ')'
10577 				{
10578 					$$ = (Node *) makeGroupingSet(GROUPING_SET_CUBE, $3, @1);
10579 				}
10580 		;
10581 
10582 grouping_sets_clause:
10583 			GROUPING SETS '(' group_by_list ')'
10584 				{
10585 					$$ = (Node *) makeGroupingSet(GROUPING_SET_SETS, $4, @1);
10586 				}
10587 		;
10588 
10589 having_clause:
10590 			HAVING a_expr							{ $$ = $2; }
10591 			| /*EMPTY*/								{ $$ = NULL; }
10592 		;
10593 
10594 for_locking_clause:
10595 			for_locking_items						{ $$ = $1; }
10596 			| FOR READ ONLY							{ $$ = NIL; }
10597 		;
10598 
10599 opt_for_locking_clause:
10600 			for_locking_clause						{ $$ = $1; }
10601 			| /* EMPTY */							{ $$ = NIL; }
10602 		;
10603 
10604 for_locking_items:
10605 			for_locking_item						{ $$ = list_make1($1); }
10606 			| for_locking_items for_locking_item	{ $$ = lappend($1, $2); }
10607 		;
10608 
10609 for_locking_item:
10610 			for_locking_strength locked_rels_list opt_nowait_or_skip
10611 				{
10612 					LockingClause *n = makeNode(LockingClause);
10613 					n->lockedRels = $2;
10614 					n->strength = $1;
10615 					n->waitPolicy = $3;
10616 					$$ = (Node *) n;
10617 				}
10618 		;
10619 
10620 for_locking_strength:
10621 			FOR UPDATE 							{ $$ = LCS_FORUPDATE; }
10622 			| FOR NO KEY UPDATE 				{ $$ = LCS_FORNOKEYUPDATE; }
10623 			| FOR SHARE 						{ $$ = LCS_FORSHARE; }
10624 			| FOR KEY SHARE 					{ $$ = LCS_FORKEYSHARE; }
10625 		;
10626 
10627 locked_rels_list:
10628 			OF qualified_name_list					{ $$ = $2; }
10629 			| /* EMPTY */							{ $$ = NIL; }
10630 		;
10631 
10632 
10633 values_clause:
10634 			VALUES ctext_row
10635 				{
10636 					SelectStmt *n = makeNode(SelectStmt);
10637 					n->valuesLists = list_make1($2);
10638 					$$ = (Node *) n;
10639 				}
10640 			| values_clause ',' ctext_row
10641 				{
10642 					SelectStmt *n = (SelectStmt *) $1;
10643 					n->valuesLists = lappend(n->valuesLists, $3);
10644 					$$ = (Node *) n;
10645 				}
10646 		;
10647 
10648 
10649 /*****************************************************************************
10650  *
10651  *	clauses common to all Optimizable Stmts:
10652  *		from_clause		- allow list of both JOIN expressions and table names
10653  *		where_clause	- qualifications for joins or restrictions
10654  *
10655  *****************************************************************************/
10656 
10657 from_clause:
10658 			FROM from_list							{ $$ = $2; }
10659 			| /*EMPTY*/								{ $$ = NIL; }
10660 		;
10661 
10662 from_list:
10663 			table_ref								{ $$ = list_make1($1); }
10664 			| from_list ',' table_ref				{ $$ = lappend($1, $3); }
10665 		;
10666 
10667 /*
10668  * table_ref is where an alias clause can be attached.
10669  */
10670 table_ref:	relation_expr opt_alias_clause
10671 				{
10672 					$1->alias = $2;
10673 					$$ = (Node *) $1;
10674 				}
10675 			| relation_expr opt_alias_clause tablesample_clause
10676 				{
10677 					RangeTableSample *n = (RangeTableSample *) $3;
10678 					$1->alias = $2;
10679 					/* relation_expr goes inside the RangeTableSample node */
10680 					n->relation = (Node *) $1;
10681 					$$ = (Node *) n;
10682 				}
10683 			| func_table func_alias_clause
10684 				{
10685 					RangeFunction *n = (RangeFunction *) $1;
10686 					n->alias = linitial($2);
10687 					n->coldeflist = lsecond($2);
10688 					$$ = (Node *) n;
10689 				}
10690 			| LATERAL_P func_table func_alias_clause
10691 				{
10692 					RangeFunction *n = (RangeFunction *) $2;
10693 					n->lateral = true;
10694 					n->alias = linitial($3);
10695 					n->coldeflist = lsecond($3);
10696 					$$ = (Node *) n;
10697 				}
10698 			| select_with_parens opt_alias_clause
10699 				{
10700 					RangeSubselect *n = makeNode(RangeSubselect);
10701 					n->lateral = false;
10702 					n->subquery = $1;
10703 					n->alias = $2;
10704 					/*
10705 					 * The SQL spec does not permit a subselect
10706 					 * (<derived_table>) without an alias clause,
10707 					 * so we don't either.  This avoids the problem
10708 					 * of needing to invent a unique refname for it.
10709 					 * That could be surmounted if there's sufficient
10710 					 * popular demand, but for now let's just implement
10711 					 * the spec and see if anyone complains.
10712 					 * However, it does seem like a good idea to emit
10713 					 * an error message that's better than "syntax error".
10714 					 */
10715 					if ($2 == NULL)
10716 					{
10717 						if (IsA($1, SelectStmt) &&
10718 							((SelectStmt *) $1)->valuesLists)
10719 							ereport(ERROR,
10720 									(errcode(ERRCODE_SYNTAX_ERROR),
10721 									 errmsg("VALUES in FROM must have an alias"),
10722 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
10723 									 parser_errposition(@1)));
10724 						else
10725 							ereport(ERROR,
10726 									(errcode(ERRCODE_SYNTAX_ERROR),
10727 									 errmsg("subquery in FROM must have an alias"),
10728 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
10729 									 parser_errposition(@1)));
10730 					}
10731 					$$ = (Node *) n;
10732 				}
10733 			| LATERAL_P select_with_parens opt_alias_clause
10734 				{
10735 					RangeSubselect *n = makeNode(RangeSubselect);
10736 					n->lateral = true;
10737 					n->subquery = $2;
10738 					n->alias = $3;
10739 					/* same coment as above */
10740 					if ($3 == NULL)
10741 					{
10742 						if (IsA($2, SelectStmt) &&
10743 							((SelectStmt *) $2)->valuesLists)
10744 							ereport(ERROR,
10745 									(errcode(ERRCODE_SYNTAX_ERROR),
10746 									 errmsg("VALUES in FROM must have an alias"),
10747 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
10748 									 parser_errposition(@2)));
10749 						else
10750 							ereport(ERROR,
10751 									(errcode(ERRCODE_SYNTAX_ERROR),
10752 									 errmsg("subquery in FROM must have an alias"),
10753 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
10754 									 parser_errposition(@2)));
10755 					}
10756 					$$ = (Node *) n;
10757 				}
10758 			| joined_table
10759 				{
10760 					$$ = (Node *) $1;
10761 				}
10762 			| '(' joined_table ')' alias_clause
10763 				{
10764 					$2->alias = $4;
10765 					$$ = (Node *) $2;
10766 				}
10767 		;
10768 
10769 
10770 /*
10771  * It may seem silly to separate joined_table from table_ref, but there is
10772  * method in SQL's madness: if you don't do it this way you get reduce-
10773  * reduce conflicts, because it's not clear to the parser generator whether
10774  * to expect alias_clause after ')' or not.  For the same reason we must
10775  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
10776  * join_type to expand to empty; if we try it, the parser generator can't
10777  * figure out when to reduce an empty join_type right after table_ref.
10778  *
10779  * Note that a CROSS JOIN is the same as an unqualified
10780  * INNER JOIN, and an INNER JOIN/ON has the same shape
10781  * but a qualification expression to limit membership.
10782  * A NATURAL JOIN implicitly matches column names between
10783  * tables and the shape is determined by which columns are
10784  * in common. We'll collect columns during the later transformations.
10785  */
10786 
10787 joined_table:
10788 			'(' joined_table ')'
10789 				{
10790 					$$ = $2;
10791 				}
10792 			| table_ref CROSS JOIN table_ref
10793 				{
10794 					/* CROSS JOIN is same as unqualified inner join */
10795 					JoinExpr *n = makeNode(JoinExpr);
10796 					n->jointype = JOIN_INNER;
10797 					n->isNatural = FALSE;
10798 					n->larg = $1;
10799 					n->rarg = $4;
10800 					n->usingClause = NIL;
10801 					n->quals = NULL;
10802 					$$ = n;
10803 				}
10804 			| table_ref join_type JOIN table_ref join_qual
10805 				{
10806 					JoinExpr *n = makeNode(JoinExpr);
10807 					n->jointype = $2;
10808 					n->isNatural = FALSE;
10809 					n->larg = $1;
10810 					n->rarg = $4;
10811 					if ($5 != NULL && IsA($5, List))
10812 						n->usingClause = (List *) $5; /* USING clause */
10813 					else
10814 						n->quals = $5; /* ON clause */
10815 					$$ = n;
10816 				}
10817 			| table_ref JOIN table_ref join_qual
10818 				{
10819 					/* letting join_type reduce to empty doesn't work */
10820 					JoinExpr *n = makeNode(JoinExpr);
10821 					n->jointype = JOIN_INNER;
10822 					n->isNatural = FALSE;
10823 					n->larg = $1;
10824 					n->rarg = $3;
10825 					if ($4 != NULL && IsA($4, List))
10826 						n->usingClause = (List *) $4; /* USING clause */
10827 					else
10828 						n->quals = $4; /* ON clause */
10829 					$$ = n;
10830 				}
10831 			| table_ref NATURAL join_type JOIN table_ref
10832 				{
10833 					JoinExpr *n = makeNode(JoinExpr);
10834 					n->jointype = $3;
10835 					n->isNatural = TRUE;
10836 					n->larg = $1;
10837 					n->rarg = $5;
10838 					n->usingClause = NIL; /* figure out which columns later... */
10839 					n->quals = NULL; /* fill later */
10840 					$$ = n;
10841 				}
10842 			| table_ref NATURAL JOIN table_ref
10843 				{
10844 					/* letting join_type reduce to empty doesn't work */
10845 					JoinExpr *n = makeNode(JoinExpr);
10846 					n->jointype = JOIN_INNER;
10847 					n->isNatural = TRUE;
10848 					n->larg = $1;
10849 					n->rarg = $4;
10850 					n->usingClause = NIL; /* figure out which columns later... */
10851 					n->quals = NULL; /* fill later */
10852 					$$ = n;
10853 				}
10854 		;
10855 
10856 alias_clause:
10857 			AS ColId '(' name_list ')'
10858 				{
10859 					$$ = makeNode(Alias);
10860 					$$->aliasname = $2;
10861 					$$->colnames = $4;
10862 				}
10863 			| AS ColId
10864 				{
10865 					$$ = makeNode(Alias);
10866 					$$->aliasname = $2;
10867 				}
10868 			| ColId '(' name_list ')'
10869 				{
10870 					$$ = makeNode(Alias);
10871 					$$->aliasname = $1;
10872 					$$->colnames = $3;
10873 				}
10874 			| ColId
10875 				{
10876 					$$ = makeNode(Alias);
10877 					$$->aliasname = $1;
10878 				}
10879 		;
10880 
10881 opt_alias_clause: alias_clause						{ $$ = $1; }
10882 			| /*EMPTY*/								{ $$ = NULL; }
10883 		;
10884 
10885 /*
10886  * func_alias_clause can include both an Alias and a coldeflist, so we make it
10887  * return a 2-element list that gets disassembled by calling production.
10888  */
10889 func_alias_clause:
10890 			alias_clause
10891 				{
10892 					$$ = list_make2($1, NIL);
10893 				}
10894 			| AS '(' TableFuncElementList ')'
10895 				{
10896 					$$ = list_make2(NULL, $3);
10897 				}
10898 			| AS ColId '(' TableFuncElementList ')'
10899 				{
10900 					Alias *a = makeNode(Alias);
10901 					a->aliasname = $2;
10902 					$$ = list_make2(a, $4);
10903 				}
10904 			| ColId '(' TableFuncElementList ')'
10905 				{
10906 					Alias *a = makeNode(Alias);
10907 					a->aliasname = $1;
10908 					$$ = list_make2(a, $3);
10909 				}
10910 			| /*EMPTY*/
10911 				{
10912 					$$ = list_make2(NULL, NIL);
10913 				}
10914 		;
10915 
10916 join_type:	FULL join_outer							{ $$ = JOIN_FULL; }
10917 			| LEFT join_outer						{ $$ = JOIN_LEFT; }
10918 			| RIGHT join_outer						{ $$ = JOIN_RIGHT; }
10919 			| INNER_P								{ $$ = JOIN_INNER; }
10920 		;
10921 
10922 /* OUTER is just noise... */
10923 join_outer: OUTER_P									{ $$ = NULL; }
10924 			| /*EMPTY*/								{ $$ = NULL; }
10925 		;
10926 
10927 /* JOIN qualification clauses
10928  * Possibilities are:
10929  *	USING ( column list ) allows only unqualified column names,
10930  *						  which must match between tables.
10931  *	ON expr allows more general qualifications.
10932  *
10933  * We return USING as a List node, while an ON-expr will not be a List.
10934  */
10935 
10936 join_qual:	USING '(' name_list ')'					{ $$ = (Node *) $3; }
10937 			| ON a_expr								{ $$ = $2; }
10938 		;
10939 
10940 
10941 relation_expr:
10942 			qualified_name
10943 				{
10944 					/* default inheritance */
10945 					$$ = $1;
10946 					$$->inhOpt = INH_DEFAULT;
10947 					$$->alias = NULL;
10948 				}
10949 			| qualified_name '*'
10950 				{
10951 					/* inheritance query */
10952 					$$ = $1;
10953 					$$->inhOpt = INH_YES;
10954 					$$->alias = NULL;
10955 				}
10956 			| ONLY qualified_name
10957 				{
10958 					/* no inheritance */
10959 					$$ = $2;
10960 					$$->inhOpt = INH_NO;
10961 					$$->alias = NULL;
10962 				}
10963 			| ONLY '(' qualified_name ')'
10964 				{
10965 					/* no inheritance, SQL99-style syntax */
10966 					$$ = $3;
10967 					$$->inhOpt = INH_NO;
10968 					$$->alias = NULL;
10969 				}
10970 		;
10971 
10972 
10973 relation_expr_list:
10974 			relation_expr							{ $$ = list_make1($1); }
10975 			| relation_expr_list ',' relation_expr	{ $$ = lappend($1, $3); }
10976 		;
10977 
10978 
10979 /*
10980  * Given "UPDATE foo set set ...", we have to decide without looking any
10981  * further ahead whether the first "set" is an alias or the UPDATE's SET
10982  * keyword.  Since "set" is allowed as a column name both interpretations
10983  * are feasible.  We resolve the shift/reduce conflict by giving the first
10984  * relation_expr_opt_alias production a higher precedence than the SET token
10985  * has, causing the parser to prefer to reduce, in effect assuming that the
10986  * SET is not an alias.
10987  */
10988 relation_expr_opt_alias: relation_expr					%prec UMINUS
10989 				{
10990 					$$ = $1;
10991 				}
10992 			| relation_expr ColId
10993 				{
10994 					Alias *alias = makeNode(Alias);
10995 					alias->aliasname = $2;
10996 					$1->alias = alias;
10997 					$$ = $1;
10998 				}
10999 			| relation_expr AS ColId
11000 				{
11001 					Alias *alias = makeNode(Alias);
11002 					alias->aliasname = $3;
11003 					$1->alias = alias;
11004 					$$ = $1;
11005 				}
11006 		;
11007 
11008 /*
11009  * TABLESAMPLE decoration in a FROM item
11010  */
11011 tablesample_clause:
11012 			TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause
11013 				{
11014 					RangeTableSample *n = makeNode(RangeTableSample);
11015 					/* n->relation will be filled in later */
11016 					n->method = $2;
11017 					n->args = $4;
11018 					n->repeatable = $6;
11019 					n->location = @2;
11020 					$$ = (Node *) n;
11021 				}
11022 		;
11023 
11024 opt_repeatable_clause:
11025 			REPEATABLE '(' a_expr ')'	{ $$ = (Node *) $3; }
11026 			| /*EMPTY*/					{ $$ = NULL; }
11027 		;
11028 
11029 /*
11030  * func_table represents a function invocation in a FROM list. It can be
11031  * a plain function call, like "foo(...)", or a ROWS FROM expression with
11032  * one or more function calls, "ROWS FROM (foo(...), bar(...))",
11033  * optionally with WITH ORDINALITY attached.
11034  * In the ROWS FROM syntax, a column definition list can be given for each
11035  * function, for example:
11036  *     ROWS FROM (foo() AS (foo_res_a text, foo_res_b text),
11037  *                bar() AS (bar_res_a text, bar_res_b text))
11038  * It's also possible to attach a column definition list to the RangeFunction
11039  * as a whole, but that's handled by the table_ref production.
11040  */
11041 func_table: func_expr_windowless opt_ordinality
11042 				{
11043 					RangeFunction *n = makeNode(RangeFunction);
11044 					n->lateral = false;
11045 					n->ordinality = $2;
11046 					n->is_rowsfrom = false;
11047 					n->functions = list_make1(list_make2($1, NIL));
11048 					/* alias and coldeflist are set by table_ref production */
11049 					$$ = (Node *) n;
11050 				}
11051 			| ROWS FROM '(' rowsfrom_list ')' opt_ordinality
11052 				{
11053 					RangeFunction *n = makeNode(RangeFunction);
11054 					n->lateral = false;
11055 					n->ordinality = $6;
11056 					n->is_rowsfrom = true;
11057 					n->functions = $4;
11058 					/* alias and coldeflist are set by table_ref production */
11059 					$$ = (Node *) n;
11060 				}
11061 		;
11062 
11063 rowsfrom_item: func_expr_windowless opt_col_def_list
11064 				{ $$ = list_make2($1, $2); }
11065 		;
11066 
11067 rowsfrom_list:
11068 			rowsfrom_item						{ $$ = list_make1($1); }
11069 			| rowsfrom_list ',' rowsfrom_item	{ $$ = lappend($1, $3); }
11070 		;
11071 
11072 opt_col_def_list: AS '(' TableFuncElementList ')'	{ $$ = $3; }
11073 			| /*EMPTY*/								{ $$ = NIL; }
11074 		;
11075 
11076 opt_ordinality: WITH_LA ORDINALITY					{ $$ = true; }
11077 			| /*EMPTY*/								{ $$ = false; }
11078 		;
11079 
11080 
11081 where_clause:
11082 			WHERE a_expr							{ $$ = $2; }
11083 			| /*EMPTY*/								{ $$ = NULL; }
11084 		;
11085 
11086 /* variant for UPDATE and DELETE */
11087 where_or_current_clause:
11088 			WHERE a_expr							{ $$ = $2; }
11089 			| WHERE CURRENT_P OF cursor_name
11090 				{
11091 					CurrentOfExpr *n = makeNode(CurrentOfExpr);
11092 					/* cvarno is filled in by parse analysis */
11093 					n->cursor_name = $4;
11094 					n->cursor_param = 0;
11095 					$$ = (Node *) n;
11096 				}
11097 			| /*EMPTY*/								{ $$ = NULL; }
11098 		;
11099 
11100 
11101 OptTableFuncElementList:
11102 			TableFuncElementList				{ $$ = $1; }
11103 			| /*EMPTY*/							{ $$ = NIL; }
11104 		;
11105 
11106 TableFuncElementList:
11107 			TableFuncElement
11108 				{
11109 					$$ = list_make1($1);
11110 				}
11111 			| TableFuncElementList ',' TableFuncElement
11112 				{
11113 					$$ = lappend($1, $3);
11114 				}
11115 		;
11116 
11117 TableFuncElement:	ColId Typename opt_collate_clause
11118 				{
11119 					ColumnDef *n = makeNode(ColumnDef);
11120 					n->colname = $1;
11121 					n->typeName = $2;
11122 					n->inhcount = 0;
11123 					n->is_local = true;
11124 					n->is_not_null = false;
11125 					n->is_from_type = false;
11126 					n->storage = 0;
11127 					n->raw_default = NULL;
11128 					n->cooked_default = NULL;
11129 					n->collClause = (CollateClause *) $3;
11130 					n->collOid = InvalidOid;
11131 					n->constraints = NIL;
11132 					n->location = @1;
11133 					$$ = (Node *)n;
11134 				}
11135 		;
11136 
11137 /*****************************************************************************
11138  *
11139  *	Type syntax
11140  *		SQL introduces a large amount of type-specific syntax.
11141  *		Define individual clauses to handle these cases, and use
11142  *		 the generic case to handle regular type-extensible Postgres syntax.
11143  *		- thomas 1997-10-10
11144  *
11145  *****************************************************************************/
11146 
11147 Typename:	SimpleTypename opt_array_bounds
11148 				{
11149 					$$ = $1;
11150 					$$->arrayBounds = $2;
11151 				}
11152 			| SETOF SimpleTypename opt_array_bounds
11153 				{
11154 					$$ = $2;
11155 					$$->arrayBounds = $3;
11156 					$$->setof = TRUE;
11157 				}
11158 			/* SQL standard syntax, currently only one-dimensional */
11159 			| SimpleTypename ARRAY '[' Iconst ']'
11160 				{
11161 					$$ = $1;
11162 					$$->arrayBounds = list_make1(makeInteger($4));
11163 				}
11164 			| SETOF SimpleTypename ARRAY '[' Iconst ']'
11165 				{
11166 					$$ = $2;
11167 					$$->arrayBounds = list_make1(makeInteger($5));
11168 					$$->setof = TRUE;
11169 				}
11170 			| SimpleTypename ARRAY
11171 				{
11172 					$$ = $1;
11173 					$$->arrayBounds = list_make1(makeInteger(-1));
11174 				}
11175 			| SETOF SimpleTypename ARRAY
11176 				{
11177 					$$ = $2;
11178 					$$->arrayBounds = list_make1(makeInteger(-1));
11179 					$$->setof = TRUE;
11180 				}
11181 		;
11182 
11183 opt_array_bounds:
11184 			opt_array_bounds '[' ']'
11185 					{  $$ = lappend($1, makeInteger(-1)); }
11186 			| opt_array_bounds '[' Iconst ']'
11187 					{  $$ = lappend($1, makeInteger($3)); }
11188 			| /*EMPTY*/
11189 					{  $$ = NIL; }
11190 		;
11191 
11192 SimpleTypename:
11193 			GenericType								{ $$ = $1; }
11194 			| Numeric								{ $$ = $1; }
11195 			| Bit									{ $$ = $1; }
11196 			| Character								{ $$ = $1; }
11197 			| ConstDatetime							{ $$ = $1; }
11198 			| ConstInterval opt_interval
11199 				{
11200 					$$ = $1;
11201 					$$->typmods = $2;
11202 				}
11203 			| ConstInterval '(' Iconst ')'
11204 				{
11205 					$$ = $1;
11206 					$$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
11207 											 makeIntConst($3, @3));
11208 				}
11209 		;
11210 
11211 /* We have a separate ConstTypename to allow defaulting fixed-length
11212  * types such as CHAR() and BIT() to an unspecified length.
11213  * SQL9x requires that these default to a length of one, but this
11214  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
11215  * where there is an obvious better choice to make.
11216  * Note that ConstInterval is not included here since it must
11217  * be pushed up higher in the rules to accommodate the postfix
11218  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
11219  * the generic-type-name case in AExprConst to avoid premature
11220  * reduce/reduce conflicts against function names.
11221  */
11222 ConstTypename:
11223 			Numeric									{ $$ = $1; }
11224 			| ConstBit								{ $$ = $1; }
11225 			| ConstCharacter						{ $$ = $1; }
11226 			| ConstDatetime							{ $$ = $1; }
11227 		;
11228 
11229 /*
11230  * GenericType covers all type names that don't have special syntax mandated
11231  * by the standard, including qualified names.  We also allow type modifiers.
11232  * To avoid parsing conflicts against function invocations, the modifiers
11233  * have to be shown as expr_list here, but parse analysis will only accept
11234  * constants for them.
11235  */
11236 GenericType:
11237 			type_function_name opt_type_modifiers
11238 				{
11239 					$$ = makeTypeName($1);
11240 					$$->typmods = $2;
11241 					$$->location = @1;
11242 				}
11243 			| type_function_name attrs opt_type_modifiers
11244 				{
11245 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
11246 					$$->typmods = $3;
11247 					$$->location = @1;
11248 				}
11249 		;
11250 
11251 opt_type_modifiers: '(' expr_list ')'				{ $$ = $2; }
11252 					| /* EMPTY */					{ $$ = NIL; }
11253 		;
11254 
11255 /*
11256  * SQL numeric data types
11257  */
11258 Numeric:	INT_P
11259 				{
11260 					$$ = SystemTypeName("int4");
11261 					$$->location = @1;
11262 				}
11263 			| INTEGER
11264 				{
11265 					$$ = SystemTypeName("int4");
11266 					$$->location = @1;
11267 				}
11268 			| SMALLINT
11269 				{
11270 					$$ = SystemTypeName("int2");
11271 					$$->location = @1;
11272 				}
11273 			| BIGINT
11274 				{
11275 					$$ = SystemTypeName("int8");
11276 					$$->location = @1;
11277 				}
11278 			| REAL
11279 				{
11280 					$$ = SystemTypeName("float4");
11281 					$$->location = @1;
11282 				}
11283 			| FLOAT_P opt_float
11284 				{
11285 					$$ = $2;
11286 					$$->location = @1;
11287 				}
11288 			| DOUBLE_P PRECISION
11289 				{
11290 					$$ = SystemTypeName("float8");
11291 					$$->location = @1;
11292 				}
11293 			| DECIMAL_P opt_type_modifiers
11294 				{
11295 					$$ = SystemTypeName("numeric");
11296 					$$->typmods = $2;
11297 					$$->location = @1;
11298 				}
11299 			| DEC opt_type_modifiers
11300 				{
11301 					$$ = SystemTypeName("numeric");
11302 					$$->typmods = $2;
11303 					$$->location = @1;
11304 				}
11305 			| NUMERIC opt_type_modifiers
11306 				{
11307 					$$ = SystemTypeName("numeric");
11308 					$$->typmods = $2;
11309 					$$->location = @1;
11310 				}
11311 			| BOOLEAN_P
11312 				{
11313 					$$ = SystemTypeName("bool");
11314 					$$->location = @1;
11315 				}
11316 		;
11317 
11318 opt_float:	'(' Iconst ')'
11319 				{
11320 					/*
11321 					 * Check FLOAT() precision limits assuming IEEE floating
11322 					 * types - thomas 1997-09-18
11323 					 */
11324 					if ($2 < 1)
11325 						ereport(ERROR,
11326 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
11327 								 errmsg("precision for type float must be at least 1 bit"),
11328 								 parser_errposition(@2)));
11329 					else if ($2 <= 24)
11330 						$$ = SystemTypeName("float4");
11331 					else if ($2 <= 53)
11332 						$$ = SystemTypeName("float8");
11333 					else
11334 						ereport(ERROR,
11335 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
11336 								 errmsg("precision for type float must be less than 54 bits"),
11337 								 parser_errposition(@2)));
11338 				}
11339 			| /*EMPTY*/
11340 				{
11341 					$$ = SystemTypeName("float8");
11342 				}
11343 		;
11344 
11345 /*
11346  * SQL bit-field data types
11347  * The following implements BIT() and BIT VARYING().
11348  */
11349 Bit:		BitWithLength
11350 				{
11351 					$$ = $1;
11352 				}
11353 			| BitWithoutLength
11354 				{
11355 					$$ = $1;
11356 				}
11357 		;
11358 
11359 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
11360 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
11361 ConstBit:	BitWithLength
11362 				{
11363 					$$ = $1;
11364 				}
11365 			| BitWithoutLength
11366 				{
11367 					$$ = $1;
11368 					$$->typmods = NIL;
11369 				}
11370 		;
11371 
11372 BitWithLength:
11373 			BIT opt_varying '(' expr_list ')'
11374 				{
11375 					char *typname;
11376 
11377 					typname = $2 ? "varbit" : "bit";
11378 					$$ = SystemTypeName(typname);
11379 					$$->typmods = $4;
11380 					$$->location = @1;
11381 				}
11382 		;
11383 
11384 BitWithoutLength:
11385 			BIT opt_varying
11386 				{
11387 					/* bit defaults to bit(1), varbit to no limit */
11388 					if ($2)
11389 					{
11390 						$$ = SystemTypeName("varbit");
11391 					}
11392 					else
11393 					{
11394 						$$ = SystemTypeName("bit");
11395 						$$->typmods = list_make1(makeIntConst(1, -1));
11396 					}
11397 					$$->location = @1;
11398 				}
11399 		;
11400 
11401 
11402 /*
11403  * SQL character data types
11404  * The following implements CHAR() and VARCHAR().
11405  */
11406 Character:  CharacterWithLength
11407 				{
11408 					$$ = $1;
11409 				}
11410 			| CharacterWithoutLength
11411 				{
11412 					$$ = $1;
11413 				}
11414 		;
11415 
11416 ConstCharacter:  CharacterWithLength
11417 				{
11418 					$$ = $1;
11419 				}
11420 			| CharacterWithoutLength
11421 				{
11422 					/* Length was not specified so allow to be unrestricted.
11423 					 * This handles problems with fixed-length (bpchar) strings
11424 					 * which in column definitions must default to a length
11425 					 * of one, but should not be constrained if the length
11426 					 * was not specified.
11427 					 */
11428 					$$ = $1;
11429 					$$->typmods = NIL;
11430 				}
11431 		;
11432 
11433 CharacterWithLength:  character '(' Iconst ')' opt_charset
11434 				{
11435 					if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
11436 						$1 = psprintf("%s_%s", $1, $5);
11437 
11438 					$$ = SystemTypeName($1);
11439 					$$->typmods = list_make1(makeIntConst($3, @3));
11440 					$$->location = @1;
11441 				}
11442 		;
11443 
11444 CharacterWithoutLength:	 character opt_charset
11445 				{
11446 					if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
11447 						$1 = psprintf("%s_%s", $1, $2);
11448 
11449 					$$ = SystemTypeName($1);
11450 
11451 					/* char defaults to char(1), varchar to no limit */
11452 					if (strcmp($1, "bpchar") == 0)
11453 						$$->typmods = list_make1(makeIntConst(1, -1));
11454 
11455 					$$->location = @1;
11456 				}
11457 		;
11458 
11459 character:	CHARACTER opt_varying
11460 										{ $$ = $2 ? "varchar": "bpchar"; }
11461 			| CHAR_P opt_varying
11462 										{ $$ = $2 ? "varchar": "bpchar"; }
11463 			| VARCHAR
11464 										{ $$ = "varchar"; }
11465 			| NATIONAL CHARACTER opt_varying
11466 										{ $$ = $3 ? "varchar": "bpchar"; }
11467 			| NATIONAL CHAR_P opt_varying
11468 										{ $$ = $3 ? "varchar": "bpchar"; }
11469 			| NCHAR opt_varying
11470 										{ $$ = $2 ? "varchar": "bpchar"; }
11471 		;
11472 
11473 opt_varying:
11474 			VARYING									{ $$ = TRUE; }
11475 			| /*EMPTY*/								{ $$ = FALSE; }
11476 		;
11477 
11478 opt_charset:
11479 			CHARACTER SET ColId						{ $$ = $3; }
11480 			| /*EMPTY*/								{ $$ = NULL; }
11481 		;
11482 
11483 /*
11484  * SQL date/time types
11485  */
11486 ConstDatetime:
11487 			TIMESTAMP '(' Iconst ')' opt_timezone
11488 				{
11489 					if ($5)
11490 						$$ = SystemTypeName("timestamptz");
11491 					else
11492 						$$ = SystemTypeName("timestamp");
11493 					$$->typmods = list_make1(makeIntConst($3, @3));
11494 					$$->location = @1;
11495 				}
11496 			| TIMESTAMP opt_timezone
11497 				{
11498 					if ($2)
11499 						$$ = SystemTypeName("timestamptz");
11500 					else
11501 						$$ = SystemTypeName("timestamp");
11502 					$$->location = @1;
11503 				}
11504 			| TIME '(' Iconst ')' opt_timezone
11505 				{
11506 					if ($5)
11507 						$$ = SystemTypeName("timetz");
11508 					else
11509 						$$ = SystemTypeName("time");
11510 					$$->typmods = list_make1(makeIntConst($3, @3));
11511 					$$->location = @1;
11512 				}
11513 			| TIME opt_timezone
11514 				{
11515 					if ($2)
11516 						$$ = SystemTypeName("timetz");
11517 					else
11518 						$$ = SystemTypeName("time");
11519 					$$->location = @1;
11520 				}
11521 		;
11522 
11523 ConstInterval:
11524 			INTERVAL
11525 				{
11526 					$$ = SystemTypeName("interval");
11527 					$$->location = @1;
11528 				}
11529 		;
11530 
11531 opt_timezone:
11532 			WITH_LA TIME ZONE						{ $$ = TRUE; }
11533 			| WITHOUT TIME ZONE						{ $$ = FALSE; }
11534 			| /*EMPTY*/								{ $$ = FALSE; }
11535 		;
11536 
11537 opt_interval:
11538 			YEAR_P
11539 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
11540 			| MONTH_P
11541 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
11542 			| DAY_P
11543 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
11544 			| HOUR_P
11545 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
11546 			| MINUTE_P
11547 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
11548 			| interval_second
11549 				{ $$ = $1; }
11550 			| YEAR_P TO MONTH_P
11551 				{
11552 					$$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
11553 												 INTERVAL_MASK(MONTH), @1));
11554 				}
11555 			| DAY_P TO HOUR_P
11556 				{
11557 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
11558 												 INTERVAL_MASK(HOUR), @1));
11559 				}
11560 			| DAY_P TO MINUTE_P
11561 				{
11562 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
11563 												 INTERVAL_MASK(HOUR) |
11564 												 INTERVAL_MASK(MINUTE), @1));
11565 				}
11566 			| DAY_P TO interval_second
11567 				{
11568 					$$ = $3;
11569 					linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
11570 												INTERVAL_MASK(HOUR) |
11571 												INTERVAL_MASK(MINUTE) |
11572 												INTERVAL_MASK(SECOND), @1);
11573 				}
11574 			| HOUR_P TO MINUTE_P
11575 				{
11576 					$$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
11577 												 INTERVAL_MASK(MINUTE), @1));
11578 				}
11579 			| HOUR_P TO interval_second
11580 				{
11581 					$$ = $3;
11582 					linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
11583 												INTERVAL_MASK(MINUTE) |
11584 												INTERVAL_MASK(SECOND), @1);
11585 				}
11586 			| MINUTE_P TO interval_second
11587 				{
11588 					$$ = $3;
11589 					linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
11590 												INTERVAL_MASK(SECOND), @1);
11591 				}
11592 			| /*EMPTY*/
11593 				{ $$ = NIL; }
11594 		;
11595 
11596 interval_second:
11597 			SECOND_P
11598 				{
11599 					$$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
11600 				}
11601 			| SECOND_P '(' Iconst ')'
11602 				{
11603 					$$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
11604 									makeIntConst($3, @3));
11605 				}
11606 		;
11607 
11608 
11609 /*****************************************************************************
11610  *
11611  *	expression grammar
11612  *
11613  *****************************************************************************/
11614 
11615 /*
11616  * General expressions
11617  * This is the heart of the expression syntax.
11618  *
11619  * We have two expression types: a_expr is the unrestricted kind, and
11620  * b_expr is a subset that must be used in some places to avoid shift/reduce
11621  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
11622  * because that use of AND conflicts with AND as a boolean operator.  So,
11623  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
11624  *
11625  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
11626  * always be used by surrounding it with parens.
11627  *
11628  * c_expr is all the productions that are common to a_expr and b_expr;
11629  * it's factored out just to eliminate redundant coding.
11630  *
11631  * Be careful of productions involving more than one terminal token.
11632  * By default, bison will assign such productions the precedence of their
11633  * last terminal, but in nearly all cases you want it to be the precedence
11634  * of the first terminal instead; otherwise you will not get the behavior
11635  * you expect!  So we use %prec annotations freely to set precedences.
11636  */
11637 a_expr:		c_expr									{ $$ = $1; }
11638 			| a_expr TYPECAST Typename
11639 					{ $$ = makeTypeCast($1, $3, @2); }
11640 			| a_expr COLLATE any_name
11641 				{
11642 					CollateClause *n = makeNode(CollateClause);
11643 					n->arg = $1;
11644 					n->collname = $3;
11645 					n->location = @2;
11646 					$$ = (Node *) n;
11647 				}
11648 			| a_expr AT TIME ZONE a_expr			%prec AT
11649 				{
11650 					$$ = (Node *) makeFuncCall(SystemFuncName("timezone"),
11651 											   list_make2($5, $1),
11652 											   @2);
11653 				}
11654 		/*
11655 		 * These operators must be called out explicitly in order to make use
11656 		 * of bison's automatic operator-precedence handling.  All other
11657 		 * operator names are handled by the generic productions using "Op",
11658 		 * below; and all those operators will have the same precedence.
11659 		 *
11660 		 * If you add more explicitly-known operators, be sure to add them
11661 		 * also to b_expr and to the MathOp list below.
11662 		 */
11663 			| '+' a_expr					%prec UMINUS
11664 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
11665 			| '-' a_expr					%prec UMINUS
11666 				{ $$ = doNegate($2, @1); }
11667 			| a_expr '+' a_expr
11668 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
11669 			| a_expr '-' a_expr
11670 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
11671 			| a_expr '*' a_expr
11672 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
11673 			| a_expr '/' a_expr
11674 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
11675 			| a_expr '%' a_expr
11676 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
11677 			| a_expr '^' a_expr
11678 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
11679 			| a_expr '<' a_expr
11680 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
11681 			| a_expr '>' a_expr
11682 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
11683 			| a_expr '=' a_expr
11684 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
11685 			| a_expr LESS_EQUALS a_expr
11686 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
11687 			| a_expr GREATER_EQUALS a_expr
11688 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
11689 			| a_expr NOT_EQUALS a_expr
11690 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
11691 
11692 			| a_expr qual_Op a_expr				%prec Op
11693 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
11694 			| qual_Op a_expr					%prec Op
11695 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
11696 			| a_expr qual_Op					%prec POSTFIXOP
11697 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
11698 
11699 			| a_expr AND a_expr
11700 				{ $$ = makeAndExpr($1, $3, @2); }
11701 			| a_expr OR a_expr
11702 				{ $$ = makeOrExpr($1, $3, @2); }
11703 			| NOT a_expr
11704 				{ $$ = makeNotExpr($2, @1); }
11705 			| NOT_LA a_expr						%prec NOT
11706 				{ $$ = makeNotExpr($2, @1); }
11707 
11708 			| a_expr LIKE a_expr
11709 				{
11710 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
11711 												   $1, $3, @2);
11712 				}
11713 			| a_expr LIKE a_expr ESCAPE a_expr					%prec LIKE
11714 				{
11715 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
11716 											   list_make2($3, $5),
11717 											   @2);
11718 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
11719 												   $1, (Node *) n, @2);
11720 				}
11721 			| a_expr NOT_LA LIKE a_expr							%prec NOT_LA
11722 				{
11723 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
11724 												   $1, $4, @2);
11725 				}
11726 			| a_expr NOT_LA LIKE a_expr ESCAPE a_expr			%prec NOT_LA
11727 				{
11728 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
11729 											   list_make2($4, $6),
11730 											   @2);
11731 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
11732 												   $1, (Node *) n, @2);
11733 				}
11734 			| a_expr ILIKE a_expr
11735 				{
11736 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
11737 												   $1, $3, @2);
11738 				}
11739 			| a_expr ILIKE a_expr ESCAPE a_expr					%prec ILIKE
11740 				{
11741 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
11742 											   list_make2($3, $5),
11743 											   @2);
11744 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
11745 												   $1, (Node *) n, @2);
11746 				}
11747 			| a_expr NOT_LA ILIKE a_expr						%prec NOT_LA
11748 				{
11749 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
11750 												   $1, $4, @2);
11751 				}
11752 			| a_expr NOT_LA ILIKE a_expr ESCAPE a_expr			%prec NOT_LA
11753 				{
11754 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
11755 											   list_make2($4, $6),
11756 											   @2);
11757 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
11758 												   $1, (Node *) n, @2);
11759 				}
11760 
11761 			| a_expr SIMILAR TO a_expr							%prec SIMILAR
11762 				{
11763 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
11764 											   list_make2($4, makeNullAConst(-1)),
11765 											   @2);
11766 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
11767 												   $1, (Node *) n, @2);
11768 				}
11769 			| a_expr SIMILAR TO a_expr ESCAPE a_expr			%prec SIMILAR
11770 				{
11771 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
11772 											   list_make2($4, $6),
11773 											   @2);
11774 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
11775 												   $1, (Node *) n, @2);
11776 				}
11777 			| a_expr NOT_LA SIMILAR TO a_expr					%prec NOT_LA
11778 				{
11779 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
11780 											   list_make2($5, makeNullAConst(-1)),
11781 											   @2);
11782 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
11783 												   $1, (Node *) n, @2);
11784 				}
11785 			| a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr		%prec NOT_LA
11786 				{
11787 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
11788 											   list_make2($5, $7),
11789 											   @2);
11790 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
11791 												   $1, (Node *) n, @2);
11792 				}
11793 
11794 			/* NullTest clause
11795 			 * Define SQL-style Null test clause.
11796 			 * Allow two forms described in the standard:
11797 			 *	a IS NULL
11798 			 *	a IS NOT NULL
11799 			 * Allow two SQL extensions
11800 			 *	a ISNULL
11801 			 *	a NOTNULL
11802 			 */
11803 			| a_expr IS NULL_P							%prec IS
11804 				{
11805 					NullTest *n = makeNode(NullTest);
11806 					n->arg = (Expr *) $1;
11807 					n->nulltesttype = IS_NULL;
11808 					n->location = @2;
11809 					$$ = (Node *)n;
11810 				}
11811 			| a_expr ISNULL
11812 				{
11813 					NullTest *n = makeNode(NullTest);
11814 					n->arg = (Expr *) $1;
11815 					n->nulltesttype = IS_NULL;
11816 					n->location = @2;
11817 					$$ = (Node *)n;
11818 				}
11819 			| a_expr IS NOT NULL_P						%prec IS
11820 				{
11821 					NullTest *n = makeNode(NullTest);
11822 					n->arg = (Expr *) $1;
11823 					n->nulltesttype = IS_NOT_NULL;
11824 					n->location = @2;
11825 					$$ = (Node *)n;
11826 				}
11827 			| a_expr NOTNULL
11828 				{
11829 					NullTest *n = makeNode(NullTest);
11830 					n->arg = (Expr *) $1;
11831 					n->nulltesttype = IS_NOT_NULL;
11832 					n->location = @2;
11833 					$$ = (Node *)n;
11834 				}
11835 			| row OVERLAPS row
11836 				{
11837 					if (list_length($1) != 2)
11838 						ereport(ERROR,
11839 								(errcode(ERRCODE_SYNTAX_ERROR),
11840 								 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
11841 								 parser_errposition(@1)));
11842 					if (list_length($3) != 2)
11843 						ereport(ERROR,
11844 								(errcode(ERRCODE_SYNTAX_ERROR),
11845 								 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
11846 								 parser_errposition(@3)));
11847 					$$ = (Node *) makeFuncCall(SystemFuncName("overlaps"),
11848 											   list_concat($1, $3),
11849 											   @2);
11850 				}
11851 			| a_expr IS TRUE_P							%prec IS
11852 				{
11853 					BooleanTest *b = makeNode(BooleanTest);
11854 					b->arg = (Expr *) $1;
11855 					b->booltesttype = IS_TRUE;
11856 					b->location = @2;
11857 					$$ = (Node *)b;
11858 				}
11859 			| a_expr IS NOT TRUE_P						%prec IS
11860 				{
11861 					BooleanTest *b = makeNode(BooleanTest);
11862 					b->arg = (Expr *) $1;
11863 					b->booltesttype = IS_NOT_TRUE;
11864 					b->location = @2;
11865 					$$ = (Node *)b;
11866 				}
11867 			| a_expr IS FALSE_P							%prec IS
11868 				{
11869 					BooleanTest *b = makeNode(BooleanTest);
11870 					b->arg = (Expr *) $1;
11871 					b->booltesttype = IS_FALSE;
11872 					b->location = @2;
11873 					$$ = (Node *)b;
11874 				}
11875 			| a_expr IS NOT FALSE_P						%prec IS
11876 				{
11877 					BooleanTest *b = makeNode(BooleanTest);
11878 					b->arg = (Expr *) $1;
11879 					b->booltesttype = IS_NOT_FALSE;
11880 					b->location = @2;
11881 					$$ = (Node *)b;
11882 				}
11883 			| a_expr IS UNKNOWN							%prec IS
11884 				{
11885 					BooleanTest *b = makeNode(BooleanTest);
11886 					b->arg = (Expr *) $1;
11887 					b->booltesttype = IS_UNKNOWN;
11888 					b->location = @2;
11889 					$$ = (Node *)b;
11890 				}
11891 			| a_expr IS NOT UNKNOWN						%prec IS
11892 				{
11893 					BooleanTest *b = makeNode(BooleanTest);
11894 					b->arg = (Expr *) $1;
11895 					b->booltesttype = IS_NOT_UNKNOWN;
11896 					b->location = @2;
11897 					$$ = (Node *)b;
11898 				}
11899 			| a_expr IS DISTINCT FROM a_expr			%prec IS
11900 				{
11901 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
11902 				}
11903 			| a_expr IS NOT DISTINCT FROM a_expr		%prec IS
11904 				{
11905                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
11906 				}
11907 			| a_expr IS OF '(' type_list ')'			%prec IS
11908 				{
11909 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
11910 				}
11911 			| a_expr IS NOT OF '(' type_list ')'		%prec IS
11912 				{
11913 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
11914 				}
11915 			| a_expr BETWEEN opt_asymmetric b_expr AND a_expr		%prec BETWEEN
11916 				{
11917 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN,
11918 												   "BETWEEN",
11919 												   $1,
11920 												   (Node *) list_make2($4, $6),
11921 												   @2);
11922 				}
11923 			| a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr %prec NOT_LA
11924 				{
11925 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN,
11926 												   "NOT BETWEEN",
11927 												   $1,
11928 												   (Node *) list_make2($5, $7),
11929 												   @2);
11930 				}
11931 			| a_expr BETWEEN SYMMETRIC b_expr AND a_expr			%prec BETWEEN
11932 				{
11933 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN_SYM,
11934 												   "BETWEEN SYMMETRIC",
11935 												   $1,
11936 												   (Node *) list_make2($4, $6),
11937 												   @2);
11938 				}
11939 			| a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr		%prec NOT_LA
11940 				{
11941 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN_SYM,
11942 												   "NOT BETWEEN SYMMETRIC",
11943 												   $1,
11944 												   (Node *) list_make2($5, $7),
11945 												   @2);
11946 				}
11947 			| a_expr IN_P in_expr
11948 				{
11949 					/* in_expr returns a SubLink or a list of a_exprs */
11950 					if (IsA($3, SubLink))
11951 					{
11952 						/* generate foo = ANY (subquery) */
11953 						SubLink *n = (SubLink *) $3;
11954 						n->subLinkType = ANY_SUBLINK;
11955 						n->subLinkId = 0;
11956 						n->testexpr = $1;
11957 						n->operName = NIL;		/* show it's IN not = ANY */
11958 						n->location = @2;
11959 						$$ = (Node *)n;
11960 					}
11961 					else
11962 					{
11963 						/* generate scalar IN expression */
11964 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
11965 					}
11966 				}
11967 			| a_expr NOT_LA IN_P in_expr						%prec NOT_LA
11968 				{
11969 					/* in_expr returns a SubLink or a list of a_exprs */
11970 					if (IsA($4, SubLink))
11971 					{
11972 						/* generate NOT (foo = ANY (subquery)) */
11973 						/* Make an = ANY node */
11974 						SubLink *n = (SubLink *) $4;
11975 						n->subLinkType = ANY_SUBLINK;
11976 						n->subLinkId = 0;
11977 						n->testexpr = $1;
11978 						n->operName = NIL;		/* show it's IN not = ANY */
11979 						n->location = @2;
11980 						/* Stick a NOT on top; must have same parse location */
11981 						$$ = makeNotExpr((Node *) n, @2);
11982 					}
11983 					else
11984 					{
11985 						/* generate scalar NOT IN expression */
11986 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
11987 					}
11988 				}
11989 			| a_expr subquery_Op sub_type select_with_parens	%prec Op
11990 				{
11991 					SubLink *n = makeNode(SubLink);
11992 					n->subLinkType = $3;
11993 					n->subLinkId = 0;
11994 					n->testexpr = $1;
11995 					n->operName = $2;
11996 					n->subselect = $4;
11997 					n->location = @2;
11998 					$$ = (Node *)n;
11999 				}
12000 			| a_expr subquery_Op sub_type '(' a_expr ')'		%prec Op
12001 				{
12002 					if ($3 == ANY_SUBLINK)
12003 						$$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
12004 					else
12005 						$$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
12006 				}
12007 			| UNIQUE select_with_parens
12008 				{
12009 					/* Not sure how to get rid of the parentheses
12010 					 * but there are lots of shift/reduce errors without them.
12011 					 *
12012 					 * Should be able to implement this by plopping the entire
12013 					 * select into a node, then transforming the target expressions
12014 					 * from whatever they are into count(*), and testing the
12015 					 * entire result equal to one.
12016 					 * But, will probably implement a separate node in the executor.
12017 					 */
12018 					ereport(ERROR,
12019 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
12020 							 errmsg("UNIQUE predicate is not yet implemented"),
12021 							 parser_errposition(@1)));
12022 				}
12023 			| a_expr IS DOCUMENT_P					%prec IS
12024 				{
12025 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12026 									 list_make1($1), @2);
12027 				}
12028 			| a_expr IS NOT DOCUMENT_P				%prec IS
12029 				{
12030 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12031 												 list_make1($1), @2),
12032 									 @2);
12033 				}
12034 		;
12035 
12036 /*
12037  * Restricted expressions
12038  *
12039  * b_expr is a subset of the complete expression syntax defined by a_expr.
12040  *
12041  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
12042  * cause trouble in the places where b_expr is used.  For simplicity, we
12043  * just eliminate all the boolean-keyword-operator productions from b_expr.
12044  */
12045 b_expr:		c_expr
12046 				{ $$ = $1; }
12047 			| b_expr TYPECAST Typename
12048 				{ $$ = makeTypeCast($1, $3, @2); }
12049 			| '+' b_expr					%prec UMINUS
12050 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
12051 			| '-' b_expr					%prec UMINUS
12052 				{ $$ = doNegate($2, @1); }
12053 			| b_expr '+' b_expr
12054 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
12055 			| b_expr '-' b_expr
12056 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
12057 			| b_expr '*' b_expr
12058 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
12059 			| b_expr '/' b_expr
12060 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
12061 			| b_expr '%' b_expr
12062 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
12063 			| b_expr '^' b_expr
12064 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
12065 			| b_expr '<' b_expr
12066 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
12067 			| b_expr '>' b_expr
12068 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
12069 			| b_expr '=' b_expr
12070 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
12071 			| b_expr LESS_EQUALS b_expr
12072 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
12073 			| b_expr GREATER_EQUALS b_expr
12074 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
12075 			| b_expr NOT_EQUALS b_expr
12076 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
12077 			| b_expr qual_Op b_expr				%prec Op
12078 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
12079 			| qual_Op b_expr					%prec Op
12080 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
12081 			| b_expr qual_Op					%prec POSTFIXOP
12082 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
12083 			| b_expr IS DISTINCT FROM b_expr		%prec IS
12084 				{
12085 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
12086 				}
12087 			| b_expr IS NOT DISTINCT FROM b_expr	%prec IS
12088 				{
12089                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
12090 				}
12091 			| b_expr IS OF '(' type_list ')'		%prec IS
12092 				{
12093 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
12094 				}
12095 			| b_expr IS NOT OF '(' type_list ')'	%prec IS
12096 				{
12097 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
12098 				}
12099 			| b_expr IS DOCUMENT_P					%prec IS
12100 				{
12101 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12102 									 list_make1($1), @2);
12103 				}
12104 			| b_expr IS NOT DOCUMENT_P				%prec IS
12105 				{
12106 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12107 												 list_make1($1), @2),
12108 									 @2);
12109 				}
12110 		;
12111 
12112 /*
12113  * Productions that can be used in both a_expr and b_expr.
12114  *
12115  * Note: productions that refer recursively to a_expr or b_expr mostly
12116  * cannot appear here.	However, it's OK to refer to a_exprs that occur
12117  * inside parentheses, such as function arguments; that cannot introduce
12118  * ambiguity to the b_expr syntax.
12119  */
12120 c_expr:		columnref								{ $$ = $1; }
12121 			| AexprConst							{ $$ = $1; }
12122 			| PARAM opt_indirection
12123 				{
12124 					ParamRef *p = makeNode(ParamRef);
12125 					p->number = $1;
12126 					p->location = @1;
12127 					if ($2)
12128 					{
12129 						A_Indirection *n = makeNode(A_Indirection);
12130 						n->arg = (Node *) p;
12131 						n->indirection = check_indirection($2, yyscanner);
12132 						$$ = (Node *) n;
12133 					}
12134 					else
12135 						$$ = (Node *) p;
12136 				}
12137 			| '(' a_expr ')' opt_indirection
12138 				{
12139 					if ($4)
12140 					{
12141 						A_Indirection *n = makeNode(A_Indirection);
12142 						n->arg = $2;
12143 						n->indirection = check_indirection($4, yyscanner);
12144 						$$ = (Node *)n;
12145 					}
12146 					else if (operator_precedence_warning)
12147 					{
12148 						/*
12149 						 * If precedence warnings are enabled, insert
12150 						 * AEXPR_PAREN nodes wrapping all explicitly
12151 						 * parenthesized subexpressions; this prevents bogus
12152 						 * warnings from being issued when the ordering has
12153 						 * been forced by parentheses.
12154 						 *
12155 						 * In principle we should not be relying on a GUC to
12156 						 * decide whether to insert AEXPR_PAREN nodes.
12157 						 * However, since they have no effect except to
12158 						 * suppress warnings, it's probably safe enough; and
12159 						 * we'd just as soon not waste cycles on dummy parse
12160 						 * nodes if we don't have to.
12161 						 */
12162 						$$ = (Node *) makeA_Expr(AEXPR_PAREN, NIL, $2, NULL, @1);
12163 					}
12164 					else
12165 						$$ = $2;
12166 				}
12167 			| case_expr
12168 				{ $$ = $1; }
12169 			| func_expr
12170 				{ $$ = $1; }
12171 			| select_with_parens			%prec UMINUS
12172 				{
12173 					SubLink *n = makeNode(SubLink);
12174 					n->subLinkType = EXPR_SUBLINK;
12175 					n->subLinkId = 0;
12176 					n->testexpr = NULL;
12177 					n->operName = NIL;
12178 					n->subselect = $1;
12179 					n->location = @1;
12180 					$$ = (Node *)n;
12181 				}
12182 			| select_with_parens indirection
12183 				{
12184 					/*
12185 					 * Because the select_with_parens nonterminal is designed
12186 					 * to "eat" as many levels of parens as possible, the
12187 					 * '(' a_expr ')' opt_indirection production above will
12188 					 * fail to match a sub-SELECT with indirection decoration;
12189 					 * the sub-SELECT won't be regarded as an a_expr as long
12190 					 * as there are parens around it.  To support applying
12191 					 * subscripting or field selection to a sub-SELECT result,
12192 					 * we need this redundant-looking production.
12193 					 */
12194 					SubLink *n = makeNode(SubLink);
12195 					A_Indirection *a = makeNode(A_Indirection);
12196 					n->subLinkType = EXPR_SUBLINK;
12197 					n->subLinkId = 0;
12198 					n->testexpr = NULL;
12199 					n->operName = NIL;
12200 					n->subselect = $1;
12201 					n->location = @1;
12202 					a->arg = (Node *)n;
12203 					a->indirection = check_indirection($2, yyscanner);
12204 					$$ = (Node *)a;
12205 				}
12206 			| EXISTS select_with_parens
12207 				{
12208 					SubLink *n = makeNode(SubLink);
12209 					n->subLinkType = EXISTS_SUBLINK;
12210 					n->subLinkId = 0;
12211 					n->testexpr = NULL;
12212 					n->operName = NIL;
12213 					n->subselect = $2;
12214 					n->location = @1;
12215 					$$ = (Node *)n;
12216 				}
12217 			| ARRAY select_with_parens
12218 				{
12219 					SubLink *n = makeNode(SubLink);
12220 					n->subLinkType = ARRAY_SUBLINK;
12221 					n->subLinkId = 0;
12222 					n->testexpr = NULL;
12223 					n->operName = NIL;
12224 					n->subselect = $2;
12225 					n->location = @1;
12226 					$$ = (Node *)n;
12227 				}
12228 			| ARRAY array_expr
12229 				{
12230 					A_ArrayExpr *n = (A_ArrayExpr *) $2;
12231 					Assert(IsA(n, A_ArrayExpr));
12232 					/* point outermost A_ArrayExpr to the ARRAY keyword */
12233 					n->location = @1;
12234 					$$ = (Node *)n;
12235 				}
12236 			| explicit_row
12237 				{
12238 					RowExpr *r = makeNode(RowExpr);
12239 					r->args = $1;
12240 					r->row_typeid = InvalidOid;	/* not analyzed yet */
12241 					r->colnames = NIL;	/* to be filled in during analysis */
12242 					r->row_format = COERCE_EXPLICIT_CALL; /* abuse */
12243 					r->location = @1;
12244 					$$ = (Node *)r;
12245 				}
12246 			| implicit_row
12247 				{
12248 					RowExpr *r = makeNode(RowExpr);
12249 					r->args = $1;
12250 					r->row_typeid = InvalidOid;	/* not analyzed yet */
12251 					r->colnames = NIL;	/* to be filled in during analysis */
12252 					r->row_format = COERCE_IMPLICIT_CAST; /* abuse */
12253 					r->location = @1;
12254 					$$ = (Node *)r;
12255 				}
12256 			| GROUPING '(' expr_list ')'
12257 			  {
12258 				  GroupingFunc *g = makeNode(GroupingFunc);
12259 				  g->args = $3;
12260 				  g->location = @1;
12261 				  $$ = (Node *)g;
12262 			  }
12263 		;
12264 
12265 func_application: func_name '(' ')'
12266 				{
12267 					$$ = (Node *) makeFuncCall($1, NIL, @1);
12268 				}
12269 			| func_name '(' func_arg_list opt_sort_clause ')'
12270 				{
12271 					FuncCall *n = makeFuncCall($1, $3, @1);
12272 					n->agg_order = $4;
12273 					$$ = (Node *)n;
12274 				}
12275 			| func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'
12276 				{
12277 					FuncCall *n = makeFuncCall($1, list_make1($4), @1);
12278 					n->func_variadic = TRUE;
12279 					n->agg_order = $5;
12280 					$$ = (Node *)n;
12281 				}
12282 			| func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'
12283 				{
12284 					FuncCall *n = makeFuncCall($1, lappend($3, $6), @1);
12285 					n->func_variadic = TRUE;
12286 					n->agg_order = $7;
12287 					$$ = (Node *)n;
12288 				}
12289 			| func_name '(' ALL func_arg_list opt_sort_clause ')'
12290 				{
12291 					FuncCall *n = makeFuncCall($1, $4, @1);
12292 					n->agg_order = $5;
12293 					/* Ideally we'd mark the FuncCall node to indicate
12294 					 * "must be an aggregate", but there's no provision
12295 					 * for that in FuncCall at the moment.
12296 					 */
12297 					$$ = (Node *)n;
12298 				}
12299 			| func_name '(' DISTINCT func_arg_list opt_sort_clause ')'
12300 				{
12301 					FuncCall *n = makeFuncCall($1, $4, @1);
12302 					n->agg_order = $5;
12303 					n->agg_distinct = TRUE;
12304 					$$ = (Node *)n;
12305 				}
12306 			| func_name '(' '*' ')'
12307 				{
12308 					/*
12309 					 * We consider AGGREGATE(*) to invoke a parameterless
12310 					 * aggregate.  This does the right thing for COUNT(*),
12311 					 * and there are no other aggregates in SQL that accept
12312 					 * '*' as parameter.
12313 					 *
12314 					 * The FuncCall node is also marked agg_star = true,
12315 					 * so that later processing can detect what the argument
12316 					 * really was.
12317 					 */
12318 					FuncCall *n = makeFuncCall($1, NIL, @1);
12319 					n->agg_star = TRUE;
12320 					$$ = (Node *)n;
12321 				}
12322 		;
12323 
12324 
12325 /*
12326  * func_expr and its cousin func_expr_windowless are split out from c_expr just
12327  * so that we have classifications for "everything that is a function call or
12328  * looks like one".  This isn't very important, but it saves us having to
12329  * document which variants are legal in places like "FROM function()" or the
12330  * backwards-compatible functional-index syntax for CREATE INDEX.
12331  * (Note that many of the special SQL functions wouldn't actually make any
12332  * sense as functional index entries, but we ignore that consideration here.)
12333  */
12334 func_expr: func_application within_group_clause filter_clause over_clause
12335 				{
12336 					FuncCall *n = (FuncCall *) $1;
12337 					/*
12338 					 * The order clause for WITHIN GROUP and the one for
12339 					 * plain-aggregate ORDER BY share a field, so we have to
12340 					 * check here that at most one is present.  We also check
12341 					 * for DISTINCT and VARIADIC here to give a better error
12342 					 * location.  Other consistency checks are deferred to
12343 					 * parse analysis.
12344 					 */
12345 					if ($2 != NIL)
12346 					{
12347 						if (n->agg_order != NIL)
12348 							ereport(ERROR,
12349 									(errcode(ERRCODE_SYNTAX_ERROR),
12350 									 errmsg("cannot use multiple ORDER BY clauses with WITHIN GROUP"),
12351 									 parser_errposition(@2)));
12352 						if (n->agg_distinct)
12353 							ereport(ERROR,
12354 									(errcode(ERRCODE_SYNTAX_ERROR),
12355 									 errmsg("cannot use DISTINCT with WITHIN GROUP"),
12356 									 parser_errposition(@2)));
12357 						if (n->func_variadic)
12358 							ereport(ERROR,
12359 									(errcode(ERRCODE_SYNTAX_ERROR),
12360 									 errmsg("cannot use VARIADIC with WITHIN GROUP"),
12361 									 parser_errposition(@2)));
12362 						n->agg_order = $2;
12363 						n->agg_within_group = TRUE;
12364 					}
12365 					n->agg_filter = $3;
12366 					n->over = $4;
12367 					$$ = (Node *) n;
12368 				}
12369 			| func_expr_common_subexpr
12370 				{ $$ = $1; }
12371 		;
12372 
12373 /*
12374  * As func_expr but does not accept WINDOW functions directly
12375  * (but they can still be contained in arguments for functions etc).
12376  * Use this when window expressions are not allowed, where needed to
12377  * disambiguate the grammar (e.g. in CREATE INDEX).
12378  */
12379 func_expr_windowless:
12380 			func_application						{ $$ = $1; }
12381 			| func_expr_common_subexpr				{ $$ = $1; }
12382 		;
12383 
12384 /*
12385  * Special expressions that are considered to be functions.
12386  */
12387 func_expr_common_subexpr:
12388 			COLLATION FOR '(' a_expr ')'
12389 				{
12390 					$$ = (Node *) makeFuncCall(SystemFuncName("pg_collation_for"),
12391 											   list_make1($4),
12392 											   @1);
12393 				}
12394 			| CURRENT_DATE
12395 				{
12396 					/*
12397 					 * Translate as "'now'::text::date".
12398 					 *
12399 					 * We cannot use "'now'::date" because coerce_type() will
12400 					 * immediately reduce that to a constant representing
12401 					 * today's date.  We need to delay the conversion until
12402 					 * runtime, else the wrong things will happen when
12403 					 * CURRENT_DATE is used in a column default value or rule.
12404 					 *
12405 					 * This could be simplified if we had a way to generate
12406 					 * an expression tree representing runtime application
12407 					 * of type-input conversion functions.  (As of PG 7.3
12408 					 * that is actually possible, but not clear that we want
12409 					 * to rely on it.)
12410 					 *
12411 					 * The token location is attached to the run-time
12412 					 * typecast, not to the Const, for the convenience of
12413 					 * pg_stat_statements (which doesn't want these constructs
12414 					 * to appear to be replaceable constants).
12415 					 */
12416 					Node *n;
12417 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12418 					$$ = makeTypeCast(n, SystemTypeName("date"), @1);
12419 				}
12420 			| CURRENT_TIME
12421 				{
12422 					/*
12423 					 * Translate as "'now'::text::timetz".
12424 					 * See comments for CURRENT_DATE.
12425 					 */
12426 					Node *n;
12427 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12428 					$$ = makeTypeCast(n, SystemTypeName("timetz"), @1);
12429 				}
12430 			| CURRENT_TIME '(' Iconst ')'
12431 				{
12432 					/*
12433 					 * Translate as "'now'::text::timetz(n)".
12434 					 * See comments for CURRENT_DATE.
12435 					 */
12436 					Node *n;
12437 					TypeName *d;
12438 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12439 					d = SystemTypeName("timetz");
12440 					d->typmods = list_make1(makeIntConst($3, @3));
12441 					$$ = makeTypeCast(n, d, @1);
12442 				}
12443 			| CURRENT_TIMESTAMP
12444 				{
12445 					/*
12446 					 * Translate as "now()", since we have a function that
12447 					 * does exactly what is needed.
12448 					 */
12449 					$$ = (Node *) makeFuncCall(SystemFuncName("now"), NIL, @1);
12450 				}
12451 			| CURRENT_TIMESTAMP '(' Iconst ')'
12452 				{
12453 					/*
12454 					 * Translate as "'now'::text::timestamptz(n)".
12455 					 * See comments for CURRENT_DATE.
12456 					 */
12457 					Node *n;
12458 					TypeName *d;
12459 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12460 					d = SystemTypeName("timestamptz");
12461 					d->typmods = list_make1(makeIntConst($3, @3));
12462 					$$ = makeTypeCast(n, d, @1);
12463 				}
12464 			| LOCALTIME
12465 				{
12466 					/*
12467 					 * Translate as "'now'::text::time".
12468 					 * See comments for CURRENT_DATE.
12469 					 */
12470 					Node *n;
12471 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12472 					$$ = makeTypeCast((Node *)n, SystemTypeName("time"), @1);
12473 				}
12474 			| LOCALTIME '(' Iconst ')'
12475 				{
12476 					/*
12477 					 * Translate as "'now'::text::time(n)".
12478 					 * See comments for CURRENT_DATE.
12479 					 */
12480 					Node *n;
12481 					TypeName *d;
12482 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12483 					d = SystemTypeName("time");
12484 					d->typmods = list_make1(makeIntConst($3, @3));
12485 					$$ = makeTypeCast((Node *)n, d, @1);
12486 				}
12487 			| LOCALTIMESTAMP
12488 				{
12489 					/*
12490 					 * Translate as "'now'::text::timestamp".
12491 					 * See comments for CURRENT_DATE.
12492 					 */
12493 					Node *n;
12494 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12495 					$$ = makeTypeCast(n, SystemTypeName("timestamp"), @1);
12496 				}
12497 			| LOCALTIMESTAMP '(' Iconst ')'
12498 				{
12499 					/*
12500 					 * Translate as "'now'::text::timestamp(n)".
12501 					 * See comments for CURRENT_DATE.
12502 					 */
12503 					Node *n;
12504 					TypeName *d;
12505 					n = makeStringConstCast("now", -1, SystemTypeName("text"));
12506 					d = SystemTypeName("timestamp");
12507 					d->typmods = list_make1(makeIntConst($3, @3));
12508 					$$ = makeTypeCast(n, d, @1);
12509 				}
12510 			| CURRENT_ROLE
12511 				{
12512 					$$ = (Node *) makeFuncCall(SystemFuncName("current_user"), NIL, @1);
12513 				}
12514 			| CURRENT_USER
12515 				{
12516 					$$ = (Node *) makeFuncCall(SystemFuncName("current_user"), NIL, @1);
12517 				}
12518 			| SESSION_USER
12519 				{
12520 					$$ = (Node *) makeFuncCall(SystemFuncName("session_user"), NIL, @1);
12521 				}
12522 			| USER
12523 				{
12524 					$$ = (Node *) makeFuncCall(SystemFuncName("current_user"), NIL, @1);
12525 				}
12526 			| CURRENT_CATALOG
12527 				{
12528 					$$ = (Node *) makeFuncCall(SystemFuncName("current_database"), NIL, @1);
12529 				}
12530 			| CURRENT_SCHEMA
12531 				{
12532 					$$ = (Node *) makeFuncCall(SystemFuncName("current_schema"), NIL, @1);
12533 				}
12534 			| CAST '(' a_expr AS Typename ')'
12535 				{ $$ = makeTypeCast($3, $5, @1); }
12536 			| EXTRACT '(' extract_list ')'
12537 				{
12538 					$$ = (Node *) makeFuncCall(SystemFuncName("date_part"), $3, @1);
12539 				}
12540 			| OVERLAY '(' overlay_list ')'
12541 				{
12542 					/* overlay(A PLACING B FROM C FOR D) is converted to
12543 					 * overlay(A, B, C, D)
12544 					 * overlay(A PLACING B FROM C) is converted to
12545 					 * overlay(A, B, C)
12546 					 */
12547 					$$ = (Node *) makeFuncCall(SystemFuncName("overlay"), $3, @1);
12548 				}
12549 			| POSITION '(' position_list ')'
12550 				{
12551 					/* position(A in B) is converted to position(B, A) */
12552 					$$ = (Node *) makeFuncCall(SystemFuncName("position"), $3, @1);
12553 				}
12554 			| SUBSTRING '(' substr_list ')'
12555 				{
12556 					/* substring(A from B for C) is converted to
12557 					 * substring(A, B, C) - thomas 2000-11-28
12558 					 */
12559 					$$ = (Node *) makeFuncCall(SystemFuncName("substring"), $3, @1);
12560 				}
12561 			| TREAT '(' a_expr AS Typename ')'
12562 				{
12563 					/* TREAT(expr AS target) converts expr of a particular type to target,
12564 					 * which is defined to be a subtype of the original expression.
12565 					 * In SQL99, this is intended for use with structured UDTs,
12566 					 * but let's make this a generally useful form allowing stronger
12567 					 * coercions than are handled by implicit casting.
12568 					 *
12569 					 * Convert SystemTypeName() to SystemFuncName() even though
12570 					 * at the moment they result in the same thing.
12571 					 */
12572 					$$ = (Node *) makeFuncCall(SystemFuncName(((Value *)llast($5->names))->val.str),
12573 												list_make1($3),
12574 												@1);
12575 				}
12576 			| TRIM '(' BOTH trim_list ')'
12577 				{
12578 					/* various trim expressions are defined in SQL
12579 					 * - thomas 1997-07-19
12580 					 */
12581 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $4, @1);
12582 				}
12583 			| TRIM '(' LEADING trim_list ')'
12584 				{
12585 					$$ = (Node *) makeFuncCall(SystemFuncName("ltrim"), $4, @1);
12586 				}
12587 			| TRIM '(' TRAILING trim_list ')'
12588 				{
12589 					$$ = (Node *) makeFuncCall(SystemFuncName("rtrim"), $4, @1);
12590 				}
12591 			| TRIM '(' trim_list ')'
12592 				{
12593 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $3, @1);
12594 				}
12595 			| NULLIF '(' a_expr ',' a_expr ')'
12596 				{
12597 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
12598 				}
12599 			| COALESCE '(' expr_list ')'
12600 				{
12601 					CoalesceExpr *c = makeNode(CoalesceExpr);
12602 					c->args = $3;
12603 					c->location = @1;
12604 					$$ = (Node *)c;
12605 				}
12606 			| GREATEST '(' expr_list ')'
12607 				{
12608 					MinMaxExpr *v = makeNode(MinMaxExpr);
12609 					v->args = $3;
12610 					v->op = IS_GREATEST;
12611 					v->location = @1;
12612 					$$ = (Node *)v;
12613 				}
12614 			| LEAST '(' expr_list ')'
12615 				{
12616 					MinMaxExpr *v = makeNode(MinMaxExpr);
12617 					v->args = $3;
12618 					v->op = IS_LEAST;
12619 					v->location = @1;
12620 					$$ = (Node *)v;
12621 				}
12622 			| XMLCONCAT '(' expr_list ')'
12623 				{
12624 					$$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
12625 				}
12626 			| XMLELEMENT '(' NAME_P ColLabel ')'
12627 				{
12628 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
12629 				}
12630 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
12631 				{
12632 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
12633 				}
12634 			| XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
12635 				{
12636 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
12637 				}
12638 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
12639 				{
12640 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
12641 				}
12642 			| XMLEXISTS '(' c_expr xmlexists_argument ')'
12643 				{
12644 					/* xmlexists(A PASSING [BY REF] B [BY REF]) is
12645 					 * converted to xmlexists(A, B)*/
12646 					$$ = (Node *) makeFuncCall(SystemFuncName("xmlexists"), list_make2($3, $4), @1);
12647 				}
12648 			| XMLFOREST '(' xml_attribute_list ')'
12649 				{
12650 					$$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
12651 				}
12652 			| XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
12653 				{
12654 					XmlExpr *x = (XmlExpr *)
12655 						makeXmlExpr(IS_XMLPARSE, NULL, NIL,
12656 									list_make2($4, makeBoolAConst($5, -1)),
12657 									@1);
12658 					x->xmloption = $3;
12659 					$$ = (Node *)x;
12660 				}
12661 			| XMLPI '(' NAME_P ColLabel ')'
12662 				{
12663 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
12664 				}
12665 			| XMLPI '(' NAME_P ColLabel ',' a_expr ')'
12666 				{
12667 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
12668 				}
12669 			| XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
12670 				{
12671 					$$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
12672 									 list_make3($3, $5, $6), @1);
12673 				}
12674 			| XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
12675 				{
12676 					XmlSerialize *n = makeNode(XmlSerialize);
12677 					n->xmloption = $3;
12678 					n->expr = $4;
12679 					n->typeName = $6;
12680 					n->location = @1;
12681 					$$ = (Node *)n;
12682 				}
12683 		;
12684 
12685 /*
12686  * SQL/XML support
12687  */
12688 xml_root_version: VERSION_P a_expr
12689 				{ $$ = $2; }
12690 			| VERSION_P NO VALUE_P
12691 				{ $$ = makeNullAConst(-1); }
12692 		;
12693 
12694 opt_xml_root_standalone: ',' STANDALONE_P YES_P
12695 				{ $$ = makeIntConst(XML_STANDALONE_YES, -1); }
12696 			| ',' STANDALONE_P NO
12697 				{ $$ = makeIntConst(XML_STANDALONE_NO, -1); }
12698 			| ',' STANDALONE_P NO VALUE_P
12699 				{ $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
12700 			| /*EMPTY*/
12701 				{ $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
12702 		;
12703 
12704 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'	{ $$ = $3; }
12705 		;
12706 
12707 xml_attribute_list:	xml_attribute_el					{ $$ = list_make1($1); }
12708 			| xml_attribute_list ',' xml_attribute_el	{ $$ = lappend($1, $3); }
12709 		;
12710 
12711 xml_attribute_el: a_expr AS ColLabel
12712 				{
12713 					$$ = makeNode(ResTarget);
12714 					$$->name = $3;
12715 					$$->indirection = NIL;
12716 					$$->val = (Node *) $1;
12717 					$$->location = @1;
12718 				}
12719 			| a_expr
12720 				{
12721 					$$ = makeNode(ResTarget);
12722 					$$->name = NULL;
12723 					$$->indirection = NIL;
12724 					$$->val = (Node *) $1;
12725 					$$->location = @1;
12726 				}
12727 		;
12728 
12729 document_or_content: DOCUMENT_P						{ $$ = XMLOPTION_DOCUMENT; }
12730 			| CONTENT_P								{ $$ = XMLOPTION_CONTENT; }
12731 		;
12732 
12733 xml_whitespace_option: PRESERVE WHITESPACE_P		{ $$ = TRUE; }
12734 			| STRIP_P WHITESPACE_P					{ $$ = FALSE; }
12735 			| /*EMPTY*/								{ $$ = FALSE; }
12736 		;
12737 
12738 /* We allow several variants for SQL and other compatibility. */
12739 xmlexists_argument:
12740 			PASSING c_expr
12741 				{
12742 					$$ = $2;
12743 				}
12744 			| PASSING c_expr BY REF
12745 				{
12746 					$$ = $2;
12747 				}
12748 			| PASSING BY REF c_expr
12749 				{
12750 					$$ = $4;
12751 				}
12752 			| PASSING BY REF c_expr BY REF
12753 				{
12754 					$$ = $4;
12755 				}
12756 		;
12757 
12758 
12759 /*
12760  * Aggregate decoration clauses
12761  */
12762 within_group_clause:
12763 			WITHIN GROUP_P '(' sort_clause ')'		{ $$ = $4; }
12764 			| /*EMPTY*/								{ $$ = NIL; }
12765 		;
12766 
12767 filter_clause:
12768 			FILTER '(' WHERE a_expr ')'				{ $$ = $4; }
12769 			| /*EMPTY*/								{ $$ = NULL; }
12770 		;
12771 
12772 
12773 /*
12774  * Window Definitions
12775  */
12776 window_clause:
12777 			WINDOW window_definition_list			{ $$ = $2; }
12778 			| /*EMPTY*/								{ $$ = NIL; }
12779 		;
12780 
12781 window_definition_list:
12782 			window_definition						{ $$ = list_make1($1); }
12783 			| window_definition_list ',' window_definition
12784 													{ $$ = lappend($1, $3); }
12785 		;
12786 
12787 window_definition:
12788 			ColId AS window_specification
12789 				{
12790 					WindowDef *n = $3;
12791 					n->name = $1;
12792 					$$ = n;
12793 				}
12794 		;
12795 
12796 over_clause: OVER window_specification
12797 				{ $$ = $2; }
12798 			| OVER ColId
12799 				{
12800 					WindowDef *n = makeNode(WindowDef);
12801 					n->name = $2;
12802 					n->refname = NULL;
12803 					n->partitionClause = NIL;
12804 					n->orderClause = NIL;
12805 					n->frameOptions = FRAMEOPTION_DEFAULTS;
12806 					n->startOffset = NULL;
12807 					n->endOffset = NULL;
12808 					n->location = @2;
12809 					$$ = n;
12810 				}
12811 			| /*EMPTY*/
12812 				{ $$ = NULL; }
12813 		;
12814 
12815 window_specification: '(' opt_existing_window_name opt_partition_clause
12816 						opt_sort_clause opt_frame_clause ')'
12817 				{
12818 					WindowDef *n = makeNode(WindowDef);
12819 					n->name = NULL;
12820 					n->refname = $2;
12821 					n->partitionClause = $3;
12822 					n->orderClause = $4;
12823 					/* copy relevant fields of opt_frame_clause */
12824 					n->frameOptions = $5->frameOptions;
12825 					n->startOffset = $5->startOffset;
12826 					n->endOffset = $5->endOffset;
12827 					n->location = @1;
12828 					$$ = n;
12829 				}
12830 		;
12831 
12832 /*
12833  * If we see PARTITION, RANGE, or ROWS as the first token after the '('
12834  * of a window_specification, we want the assumption to be that there is
12835  * no existing_window_name; but those keywords are unreserved and so could
12836  * be ColIds.  We fix this by making them have the same precedence as IDENT
12837  * and giving the empty production here a slightly higher precedence, so
12838  * that the shift/reduce conflict is resolved in favor of reducing the rule.
12839  * These keywords are thus precluded from being an existing_window_name but
12840  * are not reserved for any other purpose.
12841  */
12842 opt_existing_window_name: ColId						{ $$ = $1; }
12843 			| /*EMPTY*/				%prec Op		{ $$ = NULL; }
12844 		;
12845 
12846 opt_partition_clause: PARTITION BY expr_list		{ $$ = $3; }
12847 			| /*EMPTY*/								{ $$ = NIL; }
12848 		;
12849 
12850 /*
12851  * For frame clauses, we return a WindowDef, but only some fields are used:
12852  * frameOptions, startOffset, and endOffset.
12853  *
12854  * This is only a subset of the full SQL:2008 frame_clause grammar.
12855  * We don't support <window frame exclusion> yet.
12856  */
12857 opt_frame_clause:
12858 			RANGE frame_extent
12859 				{
12860 					WindowDef *n = $2;
12861 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
12862 					if (n->frameOptions & (FRAMEOPTION_START_VALUE_PRECEDING |
12863 										   FRAMEOPTION_END_VALUE_PRECEDING))
12864 						ereport(ERROR,
12865 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
12866 								 errmsg("RANGE PRECEDING is only supported with UNBOUNDED"),
12867 								 parser_errposition(@1)));
12868 					if (n->frameOptions & (FRAMEOPTION_START_VALUE_FOLLOWING |
12869 										   FRAMEOPTION_END_VALUE_FOLLOWING))
12870 						ereport(ERROR,
12871 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
12872 								 errmsg("RANGE FOLLOWING is only supported with UNBOUNDED"),
12873 								 parser_errposition(@1)));
12874 					$$ = n;
12875 				}
12876 			| ROWS frame_extent
12877 				{
12878 					WindowDef *n = $2;
12879 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
12880 					$$ = n;
12881 				}
12882 			| /*EMPTY*/
12883 				{
12884 					WindowDef *n = makeNode(WindowDef);
12885 					n->frameOptions = FRAMEOPTION_DEFAULTS;
12886 					n->startOffset = NULL;
12887 					n->endOffset = NULL;
12888 					$$ = n;
12889 				}
12890 		;
12891 
12892 frame_extent: frame_bound
12893 				{
12894 					WindowDef *n = $1;
12895 					/* reject invalid cases */
12896 					if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
12897 						ereport(ERROR,
12898 								(errcode(ERRCODE_WINDOWING_ERROR),
12899 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
12900 								 parser_errposition(@1)));
12901 					if (n->frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING)
12902 						ereport(ERROR,
12903 								(errcode(ERRCODE_WINDOWING_ERROR),
12904 								 errmsg("frame starting from following row cannot end with current row"),
12905 								 parser_errposition(@1)));
12906 					n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
12907 					$$ = n;
12908 				}
12909 			| BETWEEN frame_bound AND frame_bound
12910 				{
12911 					WindowDef *n1 = $2;
12912 					WindowDef *n2 = $4;
12913 					/* form merged options */
12914 					int		frameOptions = n1->frameOptions;
12915 					/* shift converts START_ options to END_ options */
12916 					frameOptions |= n2->frameOptions << 1;
12917 					frameOptions |= FRAMEOPTION_BETWEEN;
12918 					/* reject invalid cases */
12919 					if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
12920 						ereport(ERROR,
12921 								(errcode(ERRCODE_WINDOWING_ERROR),
12922 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
12923 								 parser_errposition(@2)));
12924 					if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
12925 						ereport(ERROR,
12926 								(errcode(ERRCODE_WINDOWING_ERROR),
12927 								 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
12928 								 parser_errposition(@4)));
12929 					if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
12930 						(frameOptions & FRAMEOPTION_END_VALUE_PRECEDING))
12931 						ereport(ERROR,
12932 								(errcode(ERRCODE_WINDOWING_ERROR),
12933 								 errmsg("frame starting from current row cannot have preceding rows"),
12934 								 parser_errposition(@4)));
12935 					if ((frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING) &&
12936 						(frameOptions & (FRAMEOPTION_END_VALUE_PRECEDING |
12937 										 FRAMEOPTION_END_CURRENT_ROW)))
12938 						ereport(ERROR,
12939 								(errcode(ERRCODE_WINDOWING_ERROR),
12940 								 errmsg("frame starting from following row cannot have preceding rows"),
12941 								 parser_errposition(@4)));
12942 					n1->frameOptions = frameOptions;
12943 					n1->endOffset = n2->startOffset;
12944 					$$ = n1;
12945 				}
12946 		;
12947 
12948 /*
12949  * This is used for both frame start and frame end, with output set up on
12950  * the assumption it's frame start; the frame_extent productions must reject
12951  * invalid cases.
12952  */
12953 frame_bound:
12954 			UNBOUNDED PRECEDING
12955 				{
12956 					WindowDef *n = makeNode(WindowDef);
12957 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
12958 					n->startOffset = NULL;
12959 					n->endOffset = NULL;
12960 					$$ = n;
12961 				}
12962 			| UNBOUNDED FOLLOWING
12963 				{
12964 					WindowDef *n = makeNode(WindowDef);
12965 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
12966 					n->startOffset = NULL;
12967 					n->endOffset = NULL;
12968 					$$ = n;
12969 				}
12970 			| CURRENT_P ROW
12971 				{
12972 					WindowDef *n = makeNode(WindowDef);
12973 					n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
12974 					n->startOffset = NULL;
12975 					n->endOffset = NULL;
12976 					$$ = n;
12977 				}
12978 			| a_expr PRECEDING
12979 				{
12980 					WindowDef *n = makeNode(WindowDef);
12981 					n->frameOptions = FRAMEOPTION_START_VALUE_PRECEDING;
12982 					n->startOffset = $1;
12983 					n->endOffset = NULL;
12984 					$$ = n;
12985 				}
12986 			| a_expr FOLLOWING
12987 				{
12988 					WindowDef *n = makeNode(WindowDef);
12989 					n->frameOptions = FRAMEOPTION_START_VALUE_FOLLOWING;
12990 					n->startOffset = $1;
12991 					n->endOffset = NULL;
12992 					$$ = n;
12993 				}
12994 		;
12995 
12996 
12997 /*
12998  * Supporting nonterminals for expressions.
12999  */
13000 
13001 /* Explicit row production.
13002  *
13003  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
13004  * without conflicting with the parenthesized a_expr production.  Without the
13005  * ROW keyword, there must be more than one a_expr inside the parens.
13006  */
13007 row:		ROW '(' expr_list ')'					{ $$ = $3; }
13008 			| ROW '(' ')'							{ $$ = NIL; }
13009 			| '(' expr_list ',' a_expr ')'			{ $$ = lappend($2, $4); }
13010 		;
13011 
13012 explicit_row:	ROW '(' expr_list ')'				{ $$ = $3; }
13013 			| ROW '(' ')'							{ $$ = NIL; }
13014 		;
13015 
13016 implicit_row:	'(' expr_list ',' a_expr ')'		{ $$ = lappend($2, $4); }
13017 		;
13018 
13019 sub_type:	ANY										{ $$ = ANY_SUBLINK; }
13020 			| SOME									{ $$ = ANY_SUBLINK; }
13021 			| ALL									{ $$ = ALL_SUBLINK; }
13022 		;
13023 
13024 all_Op:		Op										{ $$ = $1; }
13025 			| MathOp								{ $$ = $1; }
13026 		;
13027 
13028 MathOp:		 '+'									{ $$ = "+"; }
13029 			| '-'									{ $$ = "-"; }
13030 			| '*'									{ $$ = "*"; }
13031 			| '/'									{ $$ = "/"; }
13032 			| '%'									{ $$ = "%"; }
13033 			| '^'									{ $$ = "^"; }
13034 			| '<'									{ $$ = "<"; }
13035 			| '>'									{ $$ = ">"; }
13036 			| '='									{ $$ = "="; }
13037 			| LESS_EQUALS							{ $$ = "<="; }
13038 			| GREATER_EQUALS						{ $$ = ">="; }
13039 			| NOT_EQUALS							{ $$ = "<>"; }
13040 		;
13041 
13042 qual_Op:	Op
13043 					{ $$ = list_make1(makeString($1)); }
13044 			| OPERATOR '(' any_operator ')'
13045 					{ $$ = $3; }
13046 		;
13047 
13048 qual_all_Op:
13049 			all_Op
13050 					{ $$ = list_make1(makeString($1)); }
13051 			| OPERATOR '(' any_operator ')'
13052 					{ $$ = $3; }
13053 		;
13054 
13055 subquery_Op:
13056 			all_Op
13057 					{ $$ = list_make1(makeString($1)); }
13058 			| OPERATOR '(' any_operator ')'
13059 					{ $$ = $3; }
13060 			| LIKE
13061 					{ $$ = list_make1(makeString("~~")); }
13062 			| NOT_LA LIKE
13063 					{ $$ = list_make1(makeString("!~~")); }
13064 			| ILIKE
13065 					{ $$ = list_make1(makeString("~~*")); }
13066 			| NOT_LA ILIKE
13067 					{ $$ = list_make1(makeString("!~~*")); }
13068 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
13069  * the regular expression is preprocessed by a function (similar_escape),
13070  * and the ~ operator for posix regular expressions is used.
13071  *        x SIMILAR TO y     ->    x ~ similar_escape(y)
13072  * this transformation is made on the fly by the parser upwards.
13073  * however the SubLink structure which handles any/some/all stuff
13074  * is not ready for such a thing.
13075  */
13076 			;
13077 
13078 expr_list:	a_expr
13079 				{
13080 					$$ = list_make1($1);
13081 				}
13082 			| expr_list ',' a_expr
13083 				{
13084 					$$ = lappend($1, $3);
13085 				}
13086 		;
13087 
13088 /* function arguments can have names */
13089 func_arg_list:  func_arg_expr
13090 				{
13091 					$$ = list_make1($1);
13092 				}
13093 			| func_arg_list ',' func_arg_expr
13094 				{
13095 					$$ = lappend($1, $3);
13096 				}
13097 		;
13098 
13099 func_arg_expr:  a_expr
13100 				{
13101 					$$ = $1;
13102 				}
13103 			| param_name COLON_EQUALS a_expr
13104 				{
13105 					NamedArgExpr *na = makeNode(NamedArgExpr);
13106 					na->name = $1;
13107 					na->arg = (Expr *) $3;
13108 					na->argnumber = -1;		/* until determined */
13109 					na->location = @1;
13110 					$$ = (Node *) na;
13111 				}
13112 			| param_name EQUALS_GREATER a_expr
13113 				{
13114 					NamedArgExpr *na = makeNode(NamedArgExpr);
13115 					na->name = $1;
13116 					na->arg = (Expr *) $3;
13117 					na->argnumber = -1;		/* until determined */
13118 					na->location = @1;
13119 					$$ = (Node *) na;
13120 				}
13121 		;
13122 
13123 type_list:	Typename								{ $$ = list_make1($1); }
13124 			| type_list ',' Typename				{ $$ = lappend($1, $3); }
13125 		;
13126 
13127 array_expr: '[' expr_list ']'
13128 				{
13129 					$$ = makeAArrayExpr($2, @1);
13130 				}
13131 			| '[' array_expr_list ']'
13132 				{
13133 					$$ = makeAArrayExpr($2, @1);
13134 				}
13135 			| '[' ']'
13136 				{
13137 					$$ = makeAArrayExpr(NIL, @1);
13138 				}
13139 		;
13140 
13141 array_expr_list: array_expr							{ $$ = list_make1($1); }
13142 			| array_expr_list ',' array_expr		{ $$ = lappend($1, $3); }
13143 		;
13144 
13145 
13146 extract_list:
13147 			extract_arg FROM a_expr
13148 				{
13149 					$$ = list_make2(makeStringConst($1, @1), $3);
13150 				}
13151 			| /*EMPTY*/								{ $$ = NIL; }
13152 		;
13153 
13154 /* Allow delimited string Sconst in extract_arg as an SQL extension.
13155  * - thomas 2001-04-12
13156  */
13157 extract_arg:
13158 			IDENT									{ $$ = $1; }
13159 			| YEAR_P								{ $$ = "year"; }
13160 			| MONTH_P								{ $$ = "month"; }
13161 			| DAY_P									{ $$ = "day"; }
13162 			| HOUR_P								{ $$ = "hour"; }
13163 			| MINUTE_P								{ $$ = "minute"; }
13164 			| SECOND_P								{ $$ = "second"; }
13165 			| Sconst								{ $$ = $1; }
13166 		;
13167 
13168 /* OVERLAY() arguments
13169  * SQL99 defines the OVERLAY() function:
13170  * o overlay(text placing text from int for int)
13171  * o overlay(text placing text from int)
13172  * and similarly for binary strings
13173  */
13174 overlay_list:
13175 			a_expr overlay_placing substr_from substr_for
13176 				{
13177 					$$ = list_make4($1, $2, $3, $4);
13178 				}
13179 			| a_expr overlay_placing substr_from
13180 				{
13181 					$$ = list_make3($1, $2, $3);
13182 				}
13183 		;
13184 
13185 overlay_placing:
13186 			PLACING a_expr
13187 				{ $$ = $2; }
13188 		;
13189 
13190 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
13191 
13192 position_list:
13193 			b_expr IN_P b_expr						{ $$ = list_make2($3, $1); }
13194 			| /*EMPTY*/								{ $$ = NIL; }
13195 		;
13196 
13197 /* SUBSTRING() arguments
13198  * SQL9x defines a specific syntax for arguments to SUBSTRING():
13199  * o substring(text from int for int)
13200  * o substring(text from int) get entire string from starting point "int"
13201  * o substring(text for int) get first "int" characters of string
13202  * o substring(text from pattern) get entire string matching pattern
13203  * o substring(text from pattern for escape) same with specified escape char
13204  * We also want to support generic substring functions which accept
13205  * the usual generic list of arguments. So we will accept both styles
13206  * here, and convert the SQL9x style to the generic list for further
13207  * processing. - thomas 2000-11-28
13208  */
13209 substr_list:
13210 			a_expr substr_from substr_for
13211 				{
13212 					$$ = list_make3($1, $2, $3);
13213 				}
13214 			| a_expr substr_for substr_from
13215 				{
13216 					/* not legal per SQL99, but might as well allow it */
13217 					$$ = list_make3($1, $3, $2);
13218 				}
13219 			| a_expr substr_from
13220 				{
13221 					$$ = list_make2($1, $2);
13222 				}
13223 			| a_expr substr_for
13224 				{
13225 					/*
13226 					 * Since there are no cases where this syntax allows
13227 					 * a textual FOR value, we forcibly cast the argument
13228 					 * to int4.  The possible matches in pg_proc are
13229 					 * substring(text,int4) and substring(text,text),
13230 					 * and we don't want the parser to choose the latter,
13231 					 * which it is likely to do if the second argument
13232 					 * is unknown or doesn't have an implicit cast to int4.
13233 					 */
13234 					$$ = list_make3($1, makeIntConst(1, -1),
13235 									makeTypeCast($2,
13236 												 SystemTypeName("int4"), -1));
13237 				}
13238 			| expr_list
13239 				{
13240 					$$ = $1;
13241 				}
13242 			| /*EMPTY*/
13243 				{ $$ = NIL; }
13244 		;
13245 
13246 substr_from:
13247 			FROM a_expr								{ $$ = $2; }
13248 		;
13249 
13250 substr_for: FOR a_expr								{ $$ = $2; }
13251 		;
13252 
13253 trim_list:	a_expr FROM expr_list					{ $$ = lappend($3, $1); }
13254 			| FROM expr_list						{ $$ = $2; }
13255 			| expr_list								{ $$ = $1; }
13256 		;
13257 
13258 in_expr:	select_with_parens
13259 				{
13260 					SubLink *n = makeNode(SubLink);
13261 					n->subselect = $1;
13262 					/* other fields will be filled later */
13263 					$$ = (Node *)n;
13264 				}
13265 			| '(' expr_list ')'						{ $$ = (Node *)$2; }
13266 		;
13267 
13268 /*
13269  * Define SQL-style CASE clause.
13270  * - Full specification
13271  *	CASE WHEN a = b THEN c ... ELSE d END
13272  * - Implicit argument
13273  *	CASE a WHEN b THEN c ... ELSE d END
13274  */
13275 case_expr:	CASE case_arg when_clause_list case_default END_P
13276 				{
13277 					CaseExpr *c = makeNode(CaseExpr);
13278 					c->casetype = InvalidOid; /* not analyzed yet */
13279 					c->arg = (Expr *) $2;
13280 					c->args = $3;
13281 					c->defresult = (Expr *) $4;
13282 					c->location = @1;
13283 					$$ = (Node *)c;
13284 				}
13285 		;
13286 
13287 when_clause_list:
13288 			/* There must be at least one */
13289 			when_clause								{ $$ = list_make1($1); }
13290 			| when_clause_list when_clause			{ $$ = lappend($1, $2); }
13291 		;
13292 
13293 when_clause:
13294 			WHEN a_expr THEN a_expr
13295 				{
13296 					CaseWhen *w = makeNode(CaseWhen);
13297 					w->expr = (Expr *) $2;
13298 					w->result = (Expr *) $4;
13299 					w->location = @1;
13300 					$$ = (Node *)w;
13301 				}
13302 		;
13303 
13304 case_default:
13305 			ELSE a_expr								{ $$ = $2; }
13306 			| /*EMPTY*/								{ $$ = NULL; }
13307 		;
13308 
13309 case_arg:	a_expr									{ $$ = $1; }
13310 			| /*EMPTY*/								{ $$ = NULL; }
13311 		;
13312 
13313 columnref:	ColId
13314 				{
13315 					$$ = makeColumnRef($1, NIL, @1, yyscanner);
13316 				}
13317 			| ColId indirection
13318 				{
13319 					$$ = makeColumnRef($1, $2, @1, yyscanner);
13320 				}
13321 		;
13322 
13323 indirection_el:
13324 			'.' attr_name
13325 				{
13326 					$$ = (Node *) makeString($2);
13327 				}
13328 			| '.' '*'
13329 				{
13330 					$$ = (Node *) makeNode(A_Star);
13331 				}
13332 			| '[' a_expr ']'
13333 				{
13334 					A_Indices *ai = makeNode(A_Indices);
13335 					ai->is_slice = false;
13336 					ai->lidx = NULL;
13337 					ai->uidx = $2;
13338 					$$ = (Node *) ai;
13339 				}
13340 			| '[' opt_slice_bound ':' opt_slice_bound ']'
13341 				{
13342 					A_Indices *ai = makeNode(A_Indices);
13343 					ai->is_slice = true;
13344 					ai->lidx = $2;
13345 					ai->uidx = $4;
13346 					$$ = (Node *) ai;
13347 				}
13348 		;
13349 
13350 opt_slice_bound:
13351 			a_expr									{ $$ = $1; }
13352 			| /*EMPTY*/								{ $$ = NULL; }
13353 		;
13354 
13355 indirection:
13356 			indirection_el							{ $$ = list_make1($1); }
13357 			| indirection indirection_el			{ $$ = lappend($1, $2); }
13358 		;
13359 
13360 opt_indirection:
13361 			/*EMPTY*/								{ $$ = NIL; }
13362 			| opt_indirection indirection_el		{ $$ = lappend($1, $2); }
13363 		;
13364 
13365 opt_asymmetric: ASYMMETRIC
13366 			| /*EMPTY*/
13367 		;
13368 
13369 /*
13370  * The SQL spec defines "contextually typed value expressions" and
13371  * "contextually typed row value constructors", which for our purposes
13372  * are the same as "a_expr" and "row" except that DEFAULT can appear at
13373  * the top level.
13374  */
13375 
13376 ctext_expr:
13377 			a_expr					{ $$ = (Node *) $1; }
13378 			| DEFAULT
13379 				{
13380 					SetToDefault *n = makeNode(SetToDefault);
13381 					n->location = @1;
13382 					$$ = (Node *) n;
13383 				}
13384 		;
13385 
13386 ctext_expr_list:
13387 			ctext_expr								{ $$ = list_make1($1); }
13388 			| ctext_expr_list ',' ctext_expr		{ $$ = lappend($1, $3); }
13389 		;
13390 
13391 /*
13392  * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
13393  * making VALUES a fully reserved word, which will probably break more apps
13394  * than allowing the noise-word is worth.
13395  */
13396 ctext_row: '(' ctext_expr_list ')'					{ $$ = $2; }
13397 		;
13398 
13399 
13400 /*****************************************************************************
13401  *
13402  *	target list for SELECT
13403  *
13404  *****************************************************************************/
13405 
13406 opt_target_list: target_list						{ $$ = $1; }
13407 			| /* EMPTY */							{ $$ = NIL; }
13408 		;
13409 
13410 target_list:
13411 			target_el								{ $$ = list_make1($1); }
13412 			| target_list ',' target_el				{ $$ = lappend($1, $3); }
13413 		;
13414 
13415 target_el:	a_expr AS ColLabel
13416 				{
13417 					$$ = makeNode(ResTarget);
13418 					$$->name = $3;
13419 					$$->indirection = NIL;
13420 					$$->val = (Node *)$1;
13421 					$$->location = @1;
13422 				}
13423 			/*
13424 			 * We support omitting AS only for column labels that aren't
13425 			 * any known keyword.  There is an ambiguity against postfix
13426 			 * operators: is "a ! b" an infix expression, or a postfix
13427 			 * expression and a column label?  We prefer to resolve this
13428 			 * as an infix expression, which we accomplish by assigning
13429 			 * IDENT a precedence higher than POSTFIXOP.
13430 			 */
13431 			| a_expr IDENT
13432 				{
13433 					$$ = makeNode(ResTarget);
13434 					$$->name = $2;
13435 					$$->indirection = NIL;
13436 					$$->val = (Node *)$1;
13437 					$$->location = @1;
13438 				}
13439 			| a_expr
13440 				{
13441 					$$ = makeNode(ResTarget);
13442 					$$->name = NULL;
13443 					$$->indirection = NIL;
13444 					$$->val = (Node *)$1;
13445 					$$->location = @1;
13446 				}
13447 			| '*'
13448 				{
13449 					ColumnRef *n = makeNode(ColumnRef);
13450 					n->fields = list_make1(makeNode(A_Star));
13451 					n->location = @1;
13452 
13453 					$$ = makeNode(ResTarget);
13454 					$$->name = NULL;
13455 					$$->indirection = NIL;
13456 					$$->val = (Node *)n;
13457 					$$->location = @1;
13458 				}
13459 		;
13460 
13461 
13462 /*****************************************************************************
13463  *
13464  *	Names and constants
13465  *
13466  *****************************************************************************/
13467 
13468 qualified_name_list:
13469 			qualified_name							{ $$ = list_make1($1); }
13470 			| qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
13471 		;
13472 
13473 /*
13474  * The production for a qualified relation name has to exactly match the
13475  * production for a qualified func_name, because in a FROM clause we cannot
13476  * tell which we are parsing until we see what comes after it ('(' for a
13477  * func_name, something else for a relation). Therefore we allow 'indirection'
13478  * which may contain subscripts, and reject that case in the C code.
13479  */
13480 qualified_name:
13481 			ColId
13482 				{
13483 					$$ = makeRangeVar(NULL, $1, @1);
13484 				}
13485 			| ColId indirection
13486 				{
13487 					check_qualified_name($2, yyscanner);
13488 					$$ = makeRangeVar(NULL, NULL, @1);
13489 					switch (list_length($2))
13490 					{
13491 						case 1:
13492 							$$->catalogname = NULL;
13493 							$$->schemaname = $1;
13494 							$$->relname = strVal(linitial($2));
13495 							break;
13496 						case 2:
13497 							$$->catalogname = $1;
13498 							$$->schemaname = strVal(linitial($2));
13499 							$$->relname = strVal(lsecond($2));
13500 							break;
13501 						default:
13502 							ereport(ERROR,
13503 									(errcode(ERRCODE_SYNTAX_ERROR),
13504 									 errmsg("improper qualified name (too many dotted names): %s",
13505 											NameListToString(lcons(makeString($1), $2))),
13506 									 parser_errposition(@1)));
13507 							break;
13508 					}
13509 				}
13510 		;
13511 
13512 name_list:	name
13513 					{ $$ = list_make1(makeString($1)); }
13514 			| name_list ',' name
13515 					{ $$ = lappend($1, makeString($3)); }
13516 		;
13517 
13518 
13519 name:		ColId									{ $$ = $1; };
13520 
13521 database_name:
13522 			ColId									{ $$ = $1; };
13523 
13524 access_method:
13525 			ColId									{ $$ = $1; };
13526 
13527 attr_name:	ColLabel								{ $$ = $1; };
13528 
13529 index_name: ColId									{ $$ = $1; };
13530 
13531 file_name:	Sconst									{ $$ = $1; };
13532 
13533 /*
13534  * The production for a qualified func_name has to exactly match the
13535  * production for a qualified columnref, because we cannot tell which we
13536  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
13537  * anything else for a columnref).  Therefore we allow 'indirection' which
13538  * may contain subscripts, and reject that case in the C code.  (If we
13539  * ever implement SQL99-like methods, such syntax may actually become legal!)
13540  */
13541 func_name:	type_function_name
13542 					{ $$ = list_make1(makeString($1)); }
13543 			| ColId indirection
13544 					{
13545 						$$ = check_func_name(lcons(makeString($1), $2),
13546 											 yyscanner);
13547 					}
13548 		;
13549 
13550 
13551 /*
13552  * Constants
13553  */
13554 AexprConst: Iconst
13555 				{
13556 					$$ = makeIntConst($1, @1);
13557 				}
13558 			| FCONST
13559 				{
13560 					$$ = makeFloatConst($1, @1);
13561 				}
13562 			| Sconst
13563 				{
13564 					$$ = makeStringConst($1, @1);
13565 				}
13566 			| BCONST
13567 				{
13568 					$$ = makeBitStringConst($1, @1);
13569 				}
13570 			| XCONST
13571 				{
13572 					/* This is a bit constant per SQL99:
13573 					 * Without Feature F511, "BIT data type",
13574 					 * a <general literal> shall not be a
13575 					 * <bit string literal> or a <hex string literal>.
13576 					 */
13577 					$$ = makeBitStringConst($1, @1);
13578 				}
13579 			| func_name Sconst
13580 				{
13581 					/* generic type 'literal' syntax */
13582 					TypeName *t = makeTypeNameFromNameList($1);
13583 					t->location = @1;
13584 					$$ = makeStringConstCast($2, @2, t);
13585 				}
13586 			| func_name '(' func_arg_list opt_sort_clause ')' Sconst
13587 				{
13588 					/* generic syntax with a type modifier */
13589 					TypeName *t = makeTypeNameFromNameList($1);
13590 					ListCell *lc;
13591 
13592 					/*
13593 					 * We must use func_arg_list and opt_sort_clause in the
13594 					 * production to avoid reduce/reduce conflicts, but we
13595 					 * don't actually wish to allow NamedArgExpr in this
13596 					 * context, nor ORDER BY.
13597 					 */
foreach(lc,$3)13598 					foreach(lc, $3)
13599 					{
13600 						NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
13601 
13602 						if (IsA(arg, NamedArgExpr))
13603 							ereport(ERROR,
13604 									(errcode(ERRCODE_SYNTAX_ERROR),
13605 									 errmsg("type modifier cannot have parameter name"),
13606 									 parser_errposition(arg->location)));
13607 					}
13608 					if ($4 != NIL)
13609 							ereport(ERROR,
13610 									(errcode(ERRCODE_SYNTAX_ERROR),
13611 									 errmsg("type modifier cannot have ORDER BY"),
13612 									 parser_errposition(@4)));
13613 
13614 					t->typmods = $3;
13615 					t->location = @1;
13616 					$$ = makeStringConstCast($6, @6, t);
13617 				}
13618 			| ConstTypename Sconst
13619 				{
13620 					$$ = makeStringConstCast($2, @2, $1);
13621 				}
13622 			| ConstInterval Sconst opt_interval
13623 				{
13624 					TypeName *t = $1;
13625 					t->typmods = $3;
13626 					$$ = makeStringConstCast($2, @2, t);
13627 				}
13628 			| ConstInterval '(' Iconst ')' Sconst
13629 				{
13630 					TypeName *t = $1;
13631 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
13632 											makeIntConst($3, @3));
13633 					$$ = makeStringConstCast($5, @5, t);
13634 				}
13635 			| TRUE_P
13636 				{
13637 					$$ = makeBoolAConst(TRUE, @1);
13638 				}
13639 			| FALSE_P
13640 				{
13641 					$$ = makeBoolAConst(FALSE, @1);
13642 				}
13643 			| NULL_P
13644 				{
13645 					$$ = makeNullAConst(@1);
13646 				}
13647 		;
13648 
13649 Iconst:		ICONST									{ $$ = $1; };
13650 Sconst:		SCONST									{ $$ = $1; };
13651 
13652 SignedIconst: Iconst								{ $$ = $1; }
13653 			| '+' Iconst							{ $$ = + $2; }
13654 			| '-' Iconst							{ $$ = - $2; }
13655 		;
13656 
13657 /* Role specifications */
13658 RoleId:		RoleSpec
13659 				{
13660 					RoleSpec *spc = (RoleSpec *) $1;
13661 					switch (spc->roletype)
13662 					{
13663 						case ROLESPEC_CSTRING:
13664 							$$ = spc->rolename;
13665 							break;
13666 						case ROLESPEC_PUBLIC:
13667 							ereport(ERROR,
13668 									(errcode(ERRCODE_RESERVED_NAME),
13669 									 errmsg("role name \"%s\" is reserved",
13670 											"public"),
13671 									 parser_errposition(@1)));
13672 						case ROLESPEC_SESSION_USER:
13673 							ereport(ERROR,
13674 									(errcode(ERRCODE_RESERVED_NAME),
13675 									 errmsg("%s cannot be used as a role name here",
13676 											"SESSION_USER"),
13677 									 parser_errposition(@1)));
13678 						case ROLESPEC_CURRENT_USER:
13679 							ereport(ERROR,
13680 									(errcode(ERRCODE_RESERVED_NAME),
13681 									 errmsg("%s cannot be used as a role name here",
13682 											"CURRENT_USER"),
13683 									 parser_errposition(@1)));
13684 					}
13685 				}
13686 			;
13687 
13688 RoleSpec:	NonReservedWord
13689 					{
13690 						/*
13691 						 * "public" and "none" are not keywords, but they must
13692 						 * be treated specially here.
13693 						 */
13694 						RoleSpec *n;
13695 						if (strcmp($1, "public") == 0)
13696 						{
13697 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_PUBLIC, @1);
13698 							n->roletype = ROLESPEC_PUBLIC;
13699 						}
13700 						else if (strcmp($1, "none") == 0)
13701 						{
13702 							ereport(ERROR,
13703 									(errcode(ERRCODE_RESERVED_NAME),
13704 									 errmsg("role name \"%s\" is reserved",
13705 											"none"),
13706 									 parser_errposition(@1)));
13707 						}
13708 						else
13709 						{
13710 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_CSTRING, @1);
13711 							n->rolename = pstrdup($1);
13712 						}
13713 						$$ = (Node *) n;
13714 					}
13715 			| CURRENT_USER
13716 					{
13717 						$$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1);
13718 					}
13719 			| SESSION_USER
13720 					{
13721 						$$ = makeRoleSpec(ROLESPEC_SESSION_USER, @1);
13722 					}
13723 		;
13724 
13725 role_list:	RoleSpec
13726 					{ $$ = list_make1($1); }
13727 			| role_list ',' RoleSpec
13728 					{ $$ = lappend($1, $3); }
13729 		;
13730 
13731 /*
13732  * Name classification hierarchy.
13733  *
13734  * IDENT is the lexeme returned by the lexer for identifiers that match
13735  * no known keyword.  In most cases, we can accept certain keywords as
13736  * names, not only IDENTs.	We prefer to accept as many such keywords
13737  * as possible to minimize the impact of "reserved words" on programmers.
13738  * So, we divide names into several possible classes.  The classification
13739  * is chosen in part to make keywords acceptable as names wherever possible.
13740  */
13741 
13742 /* Column identifier --- names that can be column, table, etc names.
13743  */
13744 ColId:		IDENT									{ $$ = $1; }
13745 			| unreserved_keyword					{ $$ = pstrdup($1); }
13746 			| col_name_keyword						{ $$ = pstrdup($1); }
13747 		;
13748 
13749 /* Type/function identifier --- names that can be type or function names.
13750  */
13751 type_function_name:	IDENT							{ $$ = $1; }
13752 			| unreserved_keyword					{ $$ = pstrdup($1); }
13753 			| type_func_name_keyword				{ $$ = pstrdup($1); }
13754 		;
13755 
13756 /* Any not-fully-reserved word --- these names can be, eg, role names.
13757  */
13758 NonReservedWord:	IDENT							{ $$ = $1; }
13759 			| unreserved_keyword					{ $$ = pstrdup($1); }
13760 			| col_name_keyword						{ $$ = pstrdup($1); }
13761 			| type_func_name_keyword				{ $$ = pstrdup($1); }
13762 		;
13763 
13764 /* Column label --- allowed labels in "AS" clauses.
13765  * This presently includes *all* Postgres keywords.
13766  */
13767 ColLabel:	IDENT									{ $$ = $1; }
13768 			| unreserved_keyword					{ $$ = pstrdup($1); }
13769 			| col_name_keyword						{ $$ = pstrdup($1); }
13770 			| type_func_name_keyword				{ $$ = pstrdup($1); }
13771 			| reserved_keyword						{ $$ = pstrdup($1); }
13772 		;
13773 
13774 
13775 /*
13776  * Keyword category lists.  Generally, every keyword present in
13777  * the Postgres grammar should appear in exactly one of these lists.
13778  *
13779  * Put a new keyword into the first list that it can go into without causing
13780  * shift or reduce conflicts.  The earlier lists define "less reserved"
13781  * categories of keywords.
13782  *
13783  * Make sure that each keyword's category in kwlist.h matches where
13784  * it is listed here.  (Someday we may be able to generate these lists and
13785  * kwlist.h's table from a common master list.)
13786  */
13787 
13788 /* "Unreserved" keywords --- available for use as any kind of name.
13789  */
13790 unreserved_keyword:
13791 			  ABORT_P
13792 			| ABSOLUTE_P
13793 			| ACCESS
13794 			| ACTION
13795 			| ADD_P
13796 			| ADMIN
13797 			| AFTER
13798 			| AGGREGATE
13799 			| ALSO
13800 			| ALTER
13801 			| ALWAYS
13802 			| ASSERTION
13803 			| ASSIGNMENT
13804 			| AT
13805 			| ATTRIBUTE
13806 			| BACKWARD
13807 			| BEFORE
13808 			| BEGIN_P
13809 			| BY
13810 			| CACHE
13811 			| CALLED
13812 			| CASCADE
13813 			| CASCADED
13814 			| CATALOG_P
13815 			| CHAIN
13816 			| CHARACTERISTICS
13817 			| CHECKPOINT
13818 			| CLASS
13819 			| CLOSE
13820 			| CLUSTER
13821 			| COMMENT
13822 			| COMMENTS
13823 			| COMMIT
13824 			| COMMITTED
13825 			| CONFIGURATION
13826 			| CONFLICT
13827 			| CONNECTION
13828 			| CONSTRAINTS
13829 			| CONTENT_P
13830 			| CONTINUE_P
13831 			| CONVERSION_P
13832 			| COPY
13833 			| COST
13834 			| CSV
13835 			| CUBE
13836 			| CURRENT_P
13837 			| CURSOR
13838 			| CYCLE
13839 			| DATA_P
13840 			| DATABASE
13841 			| DAY_P
13842 			| DEALLOCATE
13843 			| DECLARE
13844 			| DEFAULTS
13845 			| DEFERRED
13846 			| DEFINER
13847 			| DELETE_P
13848 			| DELIMITER
13849 			| DELIMITERS
13850 			| DEPENDS
13851 			| DICTIONARY
13852 			| DISABLE_P
13853 			| DISCARD
13854 			| DOCUMENT_P
13855 			| DOMAIN_P
13856 			| DOUBLE_P
13857 			| DROP
13858 			| EACH
13859 			| ENABLE_P
13860 			| ENCODING
13861 			| ENCRYPTED
13862 			| ENUM_P
13863 			| ESCAPE
13864 			| EVENT
13865 			| EXCLUDE
13866 			| EXCLUDING
13867 			| EXCLUSIVE
13868 			| EXECUTE
13869 			| EXPLAIN
13870 			| EXTENSION
13871 			| EXTERNAL
13872 			| FAMILY
13873 			| FILTER
13874 			| FIRST_P
13875 			| FOLLOWING
13876 			| FORCE
13877 			| FORWARD
13878 			| FUNCTION
13879 			| FUNCTIONS
13880 			| GLOBAL
13881 			| GRANTED
13882 			| HANDLER
13883 			| HEADER_P
13884 			| HOLD
13885 			| HOUR_P
13886 			| IDENTITY_P
13887 			| IF_P
13888 			| IMMEDIATE
13889 			| IMMUTABLE
13890 			| IMPLICIT_P
13891 			| IMPORT_P
13892 			| INCLUDING
13893 			| INCREMENT
13894 			| INDEX
13895 			| INDEXES
13896 			| INHERIT
13897 			| INHERITS
13898 			| INLINE_P
13899 			| INPUT_P
13900 			| INSENSITIVE
13901 			| INSERT
13902 			| INSTEAD
13903 			| INVOKER
13904 			| ISOLATION
13905 			| KEY
13906 			| LABEL
13907 			| LANGUAGE
13908 			| LARGE_P
13909 			| LAST_P
13910 			| LEAKPROOF
13911 			| LEVEL
13912 			| LISTEN
13913 			| LOAD
13914 			| LOCAL
13915 			| LOCATION
13916 			| LOCK_P
13917 			| LOCKED
13918 			| LOGGED
13919 			| MAPPING
13920 			| MATCH
13921 			| MATERIALIZED
13922 			| MAXVALUE
13923 			| METHOD
13924 			| MINUTE_P
13925 			| MINVALUE
13926 			| MODE
13927 			| MONTH_P
13928 			| MOVE
13929 			| NAME_P
13930 			| NAMES
13931 			| NEXT
13932 			| NO
13933 			| NOTHING
13934 			| NOTIFY
13935 			| NOWAIT
13936 			| NULLS_P
13937 			| OBJECT_P
13938 			| OF
13939 			| OFF
13940 			| OIDS
13941 			| OPERATOR
13942 			| OPTION
13943 			| OPTIONS
13944 			| ORDINALITY
13945 			| OVER
13946 			| OWNED
13947 			| OWNER
13948 			| PARALLEL
13949 			| PARSER
13950 			| PARTIAL
13951 			| PARTITION
13952 			| PASSING
13953 			| PASSWORD
13954 			| PLANS
13955 			| POLICY
13956 			| PRECEDING
13957 			| PREPARE
13958 			| PREPARED
13959 			| PRESERVE
13960 			| PRIOR
13961 			| PRIVILEGES
13962 			| PROCEDURAL
13963 			| PROCEDURE
13964 			| PROGRAM
13965 			| QUOTE
13966 			| RANGE
13967 			| READ
13968 			| REASSIGN
13969 			| RECHECK
13970 			| RECURSIVE
13971 			| REF
13972 			| REFRESH
13973 			| REINDEX
13974 			| RELATIVE_P
13975 			| RELEASE
13976 			| RENAME
13977 			| REPEATABLE
13978 			| REPLACE
13979 			| REPLICA
13980 			| RESET
13981 			| RESTART
13982 			| RESTRICT
13983 			| RETURNS
13984 			| REVOKE
13985 			| ROLE
13986 			| ROLLBACK
13987 			| ROLLUP
13988 			| ROWS
13989 			| RULE
13990 			| SAVEPOINT
13991 			| SCHEMA
13992 			| SCROLL
13993 			| SEARCH
13994 			| SECOND_P
13995 			| SECURITY
13996 			| SEQUENCE
13997 			| SEQUENCES
13998 			| SERIALIZABLE
13999 			| SERVER
14000 			| SESSION
14001 			| SET
14002 			| SETS
14003 			| SHARE
14004 			| SHOW
14005 			| SIMPLE
14006 			| SKIP
14007 			| SNAPSHOT
14008 			| SQL_P
14009 			| STABLE
14010 			| STANDALONE_P
14011 			| START
14012 			| STATEMENT
14013 			| STATISTICS
14014 			| STDIN
14015 			| STDOUT
14016 			| STORAGE
14017 			| STRICT_P
14018 			| STRIP_P
14019 			| SYSID
14020 			| SYSTEM_P
14021 			| TABLES
14022 			| TABLESPACE
14023 			| TEMP
14024 			| TEMPLATE
14025 			| TEMPORARY
14026 			| TEXT_P
14027 			| TRANSACTION
14028 			| TRANSFORM
14029 			| TRIGGER
14030 			| TRUNCATE
14031 			| TRUSTED
14032 			| TYPE_P
14033 			| TYPES_P
14034 			| UNBOUNDED
14035 			| UNCOMMITTED
14036 			| UNENCRYPTED
14037 			| UNKNOWN
14038 			| UNLISTEN
14039 			| UNLOGGED
14040 			| UNTIL
14041 			| UPDATE
14042 			| VACUUM
14043 			| VALID
14044 			| VALIDATE
14045 			| VALIDATOR
14046 			| VALUE_P
14047 			| VARYING
14048 			| VERSION_P
14049 			| VIEW
14050 			| VIEWS
14051 			| VOLATILE
14052 			| WHITESPACE_P
14053 			| WITHIN
14054 			| WITHOUT
14055 			| WORK
14056 			| WRAPPER
14057 			| WRITE
14058 			| XML_P
14059 			| YEAR_P
14060 			| YES_P
14061 			| ZONE
14062 		;
14063 
14064 /* Column identifier --- keywords that can be column, table, etc names.
14065  *
14066  * Many of these keywords will in fact be recognized as type or function
14067  * names too; but they have special productions for the purpose, and so
14068  * can't be treated as "generic" type or function names.
14069  *
14070  * The type names appearing here are not usable as function names
14071  * because they can be followed by '(' in typename productions, which
14072  * looks too much like a function call for an LR(1) parser.
14073  */
14074 col_name_keyword:
14075 			  BETWEEN
14076 			| BIGINT
14077 			| BIT
14078 			| BOOLEAN_P
14079 			| CHAR_P
14080 			| CHARACTER
14081 			| COALESCE
14082 			| DEC
14083 			| DECIMAL_P
14084 			| EXISTS
14085 			| EXTRACT
14086 			| FLOAT_P
14087 			| GREATEST
14088 			| GROUPING
14089 			| INOUT
14090 			| INT_P
14091 			| INTEGER
14092 			| INTERVAL
14093 			| LEAST
14094 			| NATIONAL
14095 			| NCHAR
14096 			| NONE
14097 			| NULLIF
14098 			| NUMERIC
14099 			| OUT_P
14100 			| OVERLAY
14101 			| POSITION
14102 			| PRECISION
14103 			| REAL
14104 			| ROW
14105 			| SETOF
14106 			| SMALLINT
14107 			| SUBSTRING
14108 			| TIME
14109 			| TIMESTAMP
14110 			| TREAT
14111 			| TRIM
14112 			| VALUES
14113 			| VARCHAR
14114 			| XMLATTRIBUTES
14115 			| XMLCONCAT
14116 			| XMLELEMENT
14117 			| XMLEXISTS
14118 			| XMLFOREST
14119 			| XMLPARSE
14120 			| XMLPI
14121 			| XMLROOT
14122 			| XMLSERIALIZE
14123 		;
14124 
14125 /* Type/function identifier --- keywords that can be type or function names.
14126  *
14127  * Most of these are keywords that are used as operators in expressions;
14128  * in general such keywords can't be column names because they would be
14129  * ambiguous with variables, but they are unambiguous as function identifiers.
14130  *
14131  * Do not include POSITION, SUBSTRING, etc here since they have explicit
14132  * productions in a_expr to support the goofy SQL9x argument syntax.
14133  * - thomas 2000-11-28
14134  */
14135 type_func_name_keyword:
14136 			  AUTHORIZATION
14137 			| BINARY
14138 			| COLLATION
14139 			| CONCURRENTLY
14140 			| CROSS
14141 			| CURRENT_SCHEMA
14142 			| FREEZE
14143 			| FULL
14144 			| ILIKE
14145 			| INNER_P
14146 			| IS
14147 			| ISNULL
14148 			| JOIN
14149 			| LEFT
14150 			| LIKE
14151 			| NATURAL
14152 			| NOTNULL
14153 			| OUTER_P
14154 			| OVERLAPS
14155 			| RIGHT
14156 			| SIMILAR
14157 			| TABLESAMPLE
14158 			| VERBOSE
14159 		;
14160 
14161 /* Reserved keyword --- these keywords are usable only as a ColLabel.
14162  *
14163  * Keywords appear here if they could not be distinguished from variable,
14164  * type, or function names in some contexts.  Don't put things here unless
14165  * forced to.
14166  */
14167 reserved_keyword:
14168 			  ALL
14169 			| ANALYSE
14170 			| ANALYZE
14171 			| AND
14172 			| ANY
14173 			| ARRAY
14174 			| AS
14175 			| ASC
14176 			| ASYMMETRIC
14177 			| BOTH
14178 			| CASE
14179 			| CAST
14180 			| CHECK
14181 			| COLLATE
14182 			| COLUMN
14183 			| CONSTRAINT
14184 			| CREATE
14185 			| CURRENT_CATALOG
14186 			| CURRENT_DATE
14187 			| CURRENT_ROLE
14188 			| CURRENT_TIME
14189 			| CURRENT_TIMESTAMP
14190 			| CURRENT_USER
14191 			| DEFAULT
14192 			| DEFERRABLE
14193 			| DESC
14194 			| DISTINCT
14195 			| DO
14196 			| ELSE
14197 			| END_P
14198 			| EXCEPT
14199 			| FALSE_P
14200 			| FETCH
14201 			| FOR
14202 			| FOREIGN
14203 			| FROM
14204 			| GRANT
14205 			| GROUP_P
14206 			| HAVING
14207 			| IN_P
14208 			| INITIALLY
14209 			| INTERSECT
14210 			| INTO
14211 			| LATERAL_P
14212 			| LEADING
14213 			| LIMIT
14214 			| LOCALTIME
14215 			| LOCALTIMESTAMP
14216 			| NOT
14217 			| NULL_P
14218 			| OFFSET
14219 			| ON
14220 			| ONLY
14221 			| OR
14222 			| ORDER
14223 			| PLACING
14224 			| PRIMARY
14225 			| REFERENCES
14226 			| RETURNING
14227 			| SELECT
14228 			| SESSION_USER
14229 			| SOME
14230 			| SYMMETRIC
14231 			| TABLE
14232 			| THEN
14233 			| TO
14234 			| TRAILING
14235 			| TRUE_P
14236 			| UNION
14237 			| UNIQUE
14238 			| USER
14239 			| USING
14240 			| VARIADIC
14241 			| WHEN
14242 			| WHERE
14243 			| WINDOW
14244 			| WITH
14245 		;
14246 
14247 %%
14248 
14249 /*
14250  * The signature of this function is required by bison.  However, we
14251  * ignore the passed yylloc and instead use the last token position
14252  * available from the scanner.
14253  */
14254 static void
14255 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
14256 {
14257 	parser_yyerror(msg);
14258 }
14259 
14260 static Node *
makeColumnRef(char * colname,List * indirection,int location,core_yyscan_t yyscanner)14261 makeColumnRef(char *colname, List *indirection,
14262 			  int location, core_yyscan_t yyscanner)
14263 {
14264 	/*
14265 	 * Generate a ColumnRef node, with an A_Indirection node added if there
14266 	 * is any subscripting in the specified indirection list.  However,
14267 	 * any field selection at the start of the indirection list must be
14268 	 * transposed into the "fields" part of the ColumnRef node.
14269 	 */
14270 	ColumnRef  *c = makeNode(ColumnRef);
14271 	int		nfields = 0;
14272 	ListCell *l;
14273 
14274 	c->location = location;
14275 	foreach(l, indirection)
14276 	{
14277 		if (IsA(lfirst(l), A_Indices))
14278 		{
14279 			A_Indirection *i = makeNode(A_Indirection);
14280 
14281 			if (nfields == 0)
14282 			{
14283 				/* easy case - all indirection goes to A_Indirection */
14284 				c->fields = list_make1(makeString(colname));
14285 				i->indirection = check_indirection(indirection, yyscanner);
14286 			}
14287 			else
14288 			{
14289 				/* got to split the list in two */
14290 				i->indirection = check_indirection(list_copy_tail(indirection,
14291 																  nfields),
14292 												   yyscanner);
14293 				indirection = list_truncate(indirection, nfields);
14294 				c->fields = lcons(makeString(colname), indirection);
14295 			}
14296 			i->arg = (Node *) c;
14297 			return (Node *) i;
14298 		}
14299 		else if (IsA(lfirst(l), A_Star))
14300 		{
14301 			/* We only allow '*' at the end of a ColumnRef */
14302 			if (lnext(l) != NULL)
14303 				parser_yyerror("improper use of \"*\"");
14304 		}
14305 		nfields++;
14306 	}
14307 	/* No subscripting, so all indirection gets added to field list */
14308 	c->fields = lcons(makeString(colname), indirection);
14309 	return (Node *) c;
14310 }
14311 
14312 static Node *
makeTypeCast(Node * arg,TypeName * typename,int location)14313 makeTypeCast(Node *arg, TypeName *typename, int location)
14314 {
14315 	TypeCast *n = makeNode(TypeCast);
14316 	n->arg = arg;
14317 	n->typeName = typename;
14318 	n->location = location;
14319 	return (Node *) n;
14320 }
14321 
14322 static Node *
makeStringConst(char * str,int location)14323 makeStringConst(char *str, int location)
14324 {
14325 	A_Const *n = makeNode(A_Const);
14326 
14327 	n->val.type = T_String;
14328 	n->val.val.str = str;
14329 	n->location = location;
14330 
14331 	return (Node *)n;
14332 }
14333 
14334 static Node *
makeStringConstCast(char * str,int location,TypeName * typename)14335 makeStringConstCast(char *str, int location, TypeName *typename)
14336 {
14337 	Node *s = makeStringConst(str, location);
14338 
14339 	return makeTypeCast(s, typename, -1);
14340 }
14341 
14342 static Node *
makeIntConst(int val,int location)14343 makeIntConst(int val, int location)
14344 {
14345 	A_Const *n = makeNode(A_Const);
14346 
14347 	n->val.type = T_Integer;
14348 	n->val.val.ival = val;
14349 	n->location = location;
14350 
14351 	return (Node *)n;
14352 }
14353 
14354 static Node *
makeFloatConst(char * str,int location)14355 makeFloatConst(char *str, int location)
14356 {
14357 	A_Const *n = makeNode(A_Const);
14358 
14359 	n->val.type = T_Float;
14360 	n->val.val.str = str;
14361 	n->location = location;
14362 
14363 	return (Node *)n;
14364 }
14365 
14366 static Node *
makeBitStringConst(char * str,int location)14367 makeBitStringConst(char *str, int location)
14368 {
14369 	A_Const *n = makeNode(A_Const);
14370 
14371 	n->val.type = T_BitString;
14372 	n->val.val.str = str;
14373 	n->location = location;
14374 
14375 	return (Node *)n;
14376 }
14377 
14378 static Node *
makeNullAConst(int location)14379 makeNullAConst(int location)
14380 {
14381 	A_Const *n = makeNode(A_Const);
14382 
14383 	n->val.type = T_Null;
14384 	n->location = location;
14385 
14386 	return (Node *)n;
14387 }
14388 
14389 static Node *
makeAConst(Value * v,int location)14390 makeAConst(Value *v, int location)
14391 {
14392 	Node *n;
14393 
14394 	switch (v->type)
14395 	{
14396 		case T_Float:
14397 			n = makeFloatConst(v->val.str, location);
14398 			break;
14399 
14400 		case T_Integer:
14401 			n = makeIntConst(v->val.ival, location);
14402 			break;
14403 
14404 		case T_String:
14405 		default:
14406 			n = makeStringConst(v->val.str, location);
14407 			break;
14408 	}
14409 
14410 	return n;
14411 }
14412 
14413 /* makeBoolAConst()
14414  * Create an A_Const string node and put it inside a boolean cast.
14415  */
14416 static Node *
makeBoolAConst(bool state,int location)14417 makeBoolAConst(bool state, int location)
14418 {
14419 	A_Const *n = makeNode(A_Const);
14420 
14421 	n->val.type = T_String;
14422 	n->val.val.str = (state ? "t" : "f");
14423 	n->location = location;
14424 
14425 	return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
14426 }
14427 
14428 /* makeRoleSpec
14429  * Create a RoleSpec with the given type
14430  */
14431 static Node *
makeRoleSpec(RoleSpecType type,int location)14432 makeRoleSpec(RoleSpecType type, int location)
14433 {
14434 	RoleSpec *spec = makeNode(RoleSpec);
14435 
14436 	spec->roletype = type;
14437 	spec->location = location;
14438 
14439 	return (Node *) spec;
14440 }
14441 
14442 /* check_qualified_name --- check the result of qualified_name production
14443  *
14444  * It's easiest to let the grammar production for qualified_name allow
14445  * subscripts and '*', which we then must reject here.
14446  */
14447 static void
check_qualified_name(List * names,core_yyscan_t yyscanner)14448 check_qualified_name(List *names, core_yyscan_t yyscanner)
14449 {
14450 	ListCell   *i;
14451 
14452 	foreach(i, names)
14453 	{
14454 		if (!IsA(lfirst(i), String))
14455 			parser_yyerror("syntax error");
14456 	}
14457 }
14458 
14459 /* check_func_name --- check the result of func_name production
14460  *
14461  * It's easiest to let the grammar production for func_name allow subscripts
14462  * and '*', which we then must reject here.
14463  */
14464 static List *
check_func_name(List * names,core_yyscan_t yyscanner)14465 check_func_name(List *names, core_yyscan_t yyscanner)
14466 {
14467 	ListCell   *i;
14468 
14469 	foreach(i, names)
14470 	{
14471 		if (!IsA(lfirst(i), String))
14472 			parser_yyerror("syntax error");
14473 	}
14474 	return names;
14475 }
14476 
14477 /* check_indirection --- check the result of indirection production
14478  *
14479  * We only allow '*' at the end of the list, but it's hard to enforce that
14480  * in the grammar, so do it here.
14481  */
14482 static List *
check_indirection(List * indirection,core_yyscan_t yyscanner)14483 check_indirection(List *indirection, core_yyscan_t yyscanner)
14484 {
14485 	ListCell *l;
14486 
14487 	foreach(l, indirection)
14488 	{
14489 		if (IsA(lfirst(l), A_Star))
14490 		{
14491 			if (lnext(l) != NULL)
14492 				parser_yyerror("improper use of \"*\"");
14493 		}
14494 	}
14495 	return indirection;
14496 }
14497 
14498 /* extractArgTypes()
14499  * Given a list of FunctionParameter nodes, extract a list of just the
14500  * argument types (TypeNames) for input parameters only.  This is what
14501  * is needed to look up an existing function, which is what is wanted by
14502  * the productions that use this call.
14503  */
14504 static List *
extractArgTypes(List * parameters)14505 extractArgTypes(List *parameters)
14506 {
14507 	List	   *result = NIL;
14508 	ListCell   *i;
14509 
14510 	foreach(i, parameters)
14511 	{
14512 		FunctionParameter *p = (FunctionParameter *) lfirst(i);
14513 
14514 		if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
14515 			result = lappend(result, p->argType);
14516 	}
14517 	return result;
14518 }
14519 
14520 /* extractAggrArgTypes()
14521  * As above, but work from the output of the aggr_args production.
14522  */
14523 static List *
extractAggrArgTypes(List * aggrargs)14524 extractAggrArgTypes(List *aggrargs)
14525 {
14526 	Assert(list_length(aggrargs) == 2);
14527 	return extractArgTypes((List *) linitial(aggrargs));
14528 }
14529 
14530 /* makeOrderedSetArgs()
14531  * Build the result of the aggr_args production (which see the comments for).
14532  * This handles only the case where both given lists are nonempty, so that
14533  * we have to deal with multiple VARIADIC arguments.
14534  */
14535 static List *
makeOrderedSetArgs(List * directargs,List * orderedargs,core_yyscan_t yyscanner)14536 makeOrderedSetArgs(List *directargs, List *orderedargs,
14537 				   core_yyscan_t yyscanner)
14538 {
14539 	FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
14540 	int			ndirectargs;
14541 
14542 	/* No restriction unless last direct arg is VARIADIC */
14543 	if (lastd->mode == FUNC_PARAM_VARIADIC)
14544 	{
14545 		FunctionParameter *firsto = (FunctionParameter *) linitial(orderedargs);
14546 
14547 		/*
14548 		 * We ignore the names, though the aggr_arg production allows them;
14549 		 * it doesn't allow default values, so those need not be checked.
14550 		 */
14551 		if (list_length(orderedargs) != 1 ||
14552 			firsto->mode != FUNC_PARAM_VARIADIC)
14553 			ereport(ERROR,
14554 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14555 					 errmsg("an ordered-set aggregate with a VARIADIC direct argument must have one VARIADIC aggregated argument of the same data type"),
14556 					 parser_errposition(exprLocation((Node *) firsto))));
14557 
14558 		/* OK, drop the duplicate VARIADIC argument from the internal form */
14559 		orderedargs = NIL;
14560 	}
14561 
14562 	/* don't merge into the next line, as list_concat changes directargs */
14563 	ndirectargs = list_length(directargs);
14564 
14565 	return list_make2(list_concat(directargs, orderedargs),
14566 					  makeInteger(ndirectargs));
14567 }
14568 
14569 /* insertSelectOptions()
14570  * Insert ORDER BY, etc into an already-constructed SelectStmt.
14571  *
14572  * This routine is just to avoid duplicating code in SelectStmt productions.
14573  */
14574 static void
insertSelectOptions(SelectStmt * stmt,List * sortClause,List * lockingClause,Node * limitOffset,Node * limitCount,WithClause * withClause,core_yyscan_t yyscanner)14575 insertSelectOptions(SelectStmt *stmt,
14576 					List *sortClause, List *lockingClause,
14577 					Node *limitOffset, Node *limitCount,
14578 					WithClause *withClause,
14579 					core_yyscan_t yyscanner)
14580 {
14581 	Assert(IsA(stmt, SelectStmt));
14582 
14583 	/*
14584 	 * Tests here are to reject constructs like
14585 	 *	(SELECT foo ORDER BY bar) ORDER BY baz
14586 	 */
14587 	if (sortClause)
14588 	{
14589 		if (stmt->sortClause)
14590 			ereport(ERROR,
14591 					(errcode(ERRCODE_SYNTAX_ERROR),
14592 					 errmsg("multiple ORDER BY clauses not allowed"),
14593 					 parser_errposition(exprLocation((Node *) sortClause))));
14594 		stmt->sortClause = sortClause;
14595 	}
14596 	/* We can handle multiple locking clauses, though */
14597 	stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
14598 	if (limitOffset)
14599 	{
14600 		if (stmt->limitOffset)
14601 			ereport(ERROR,
14602 					(errcode(ERRCODE_SYNTAX_ERROR),
14603 					 errmsg("multiple OFFSET clauses not allowed"),
14604 					 parser_errposition(exprLocation(limitOffset))));
14605 		stmt->limitOffset = limitOffset;
14606 	}
14607 	if (limitCount)
14608 	{
14609 		if (stmt->limitCount)
14610 			ereport(ERROR,
14611 					(errcode(ERRCODE_SYNTAX_ERROR),
14612 					 errmsg("multiple LIMIT clauses not allowed"),
14613 					 parser_errposition(exprLocation(limitCount))));
14614 		stmt->limitCount = limitCount;
14615 	}
14616 	if (withClause)
14617 	{
14618 		if (stmt->withClause)
14619 			ereport(ERROR,
14620 					(errcode(ERRCODE_SYNTAX_ERROR),
14621 					 errmsg("multiple WITH clauses not allowed"),
14622 					 parser_errposition(exprLocation((Node *) withClause))));
14623 		stmt->withClause = withClause;
14624 	}
14625 }
14626 
14627 static Node *
makeSetOp(SetOperation op,bool all,Node * larg,Node * rarg)14628 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
14629 {
14630 	SelectStmt *n = makeNode(SelectStmt);
14631 
14632 	n->op = op;
14633 	n->all = all;
14634 	n->larg = (SelectStmt *) larg;
14635 	n->rarg = (SelectStmt *) rarg;
14636 	return (Node *) n;
14637 }
14638 
14639 /* SystemFuncName()
14640  * Build a properly-qualified reference to a built-in function.
14641  */
14642 List *
SystemFuncName(char * name)14643 SystemFuncName(char *name)
14644 {
14645 	return list_make2(makeString("pg_catalog"), makeString(name));
14646 }
14647 
14648 /* SystemTypeName()
14649  * Build a properly-qualified reference to a built-in type.
14650  *
14651  * typmod is defaulted, but may be changed afterwards by caller.
14652  * Likewise for the location.
14653  */
14654 TypeName *
SystemTypeName(char * name)14655 SystemTypeName(char *name)
14656 {
14657 	return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
14658 											   makeString(name)));
14659 }
14660 
14661 /* doNegate()
14662  * Handle negation of a numeric constant.
14663  *
14664  * Formerly, we did this here because the optimizer couldn't cope with
14665  * indexquals that looked like "var = -4" --- it wants "var = const"
14666  * and a unary minus operator applied to a constant didn't qualify.
14667  * As of Postgres 7.0, that problem doesn't exist anymore because there
14668  * is a constant-subexpression simplifier in the optimizer.  However,
14669  * there's still a good reason for doing this here, which is that we can
14670  * postpone committing to a particular internal representation for simple
14671  * negative constants.	It's better to leave "-123.456" in string form
14672  * until we know what the desired type is.
14673  */
14674 static Node *
doNegate(Node * n,int location)14675 doNegate(Node *n, int location)
14676 {
14677 	if (IsA(n, A_Const))
14678 	{
14679 		A_Const *con = (A_Const *)n;
14680 
14681 		/* report the constant's location as that of the '-' sign */
14682 		con->location = location;
14683 
14684 		if (con->val.type == T_Integer)
14685 		{
14686 			con->val.val.ival = -con->val.val.ival;
14687 			return n;
14688 		}
14689 		if (con->val.type == T_Float)
14690 		{
14691 			doNegateFloat(&con->val);
14692 			return n;
14693 		}
14694 	}
14695 
14696 	return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
14697 }
14698 
14699 static void
doNegateFloat(Value * v)14700 doNegateFloat(Value *v)
14701 {
14702 	char   *oldval = v->val.str;
14703 
14704 	Assert(IsA(v, Float));
14705 	if (*oldval == '+')
14706 		oldval++;
14707 	if (*oldval == '-')
14708 		v->val.str = oldval+1;	/* just strip the '-' */
14709 	else
14710 		v->val.str = psprintf("-%s", oldval);
14711 }
14712 
14713 static Node *
makeAndExpr(Node * lexpr,Node * rexpr,int location)14714 makeAndExpr(Node *lexpr, Node *rexpr, int location)
14715 {
14716 	Node	   *lexp = lexpr;
14717 
14718 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
14719 	while (IsA(lexp, A_Expr) &&
14720 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
14721 		lexp = ((A_Expr *) lexp)->lexpr;
14722 	/* Flatten "a AND b AND c ..." to a single BoolExpr on sight */
14723 	if (IsA(lexp, BoolExpr))
14724 	{
14725 		BoolExpr *blexpr = (BoolExpr *) lexp;
14726 
14727 		if (blexpr->boolop == AND_EXPR)
14728 		{
14729 			blexpr->args = lappend(blexpr->args, rexpr);
14730 			return (Node *) blexpr;
14731 		}
14732 	}
14733 	return (Node *) makeBoolExpr(AND_EXPR, list_make2(lexpr, rexpr), location);
14734 }
14735 
14736 static Node *
makeOrExpr(Node * lexpr,Node * rexpr,int location)14737 makeOrExpr(Node *lexpr, Node *rexpr, int location)
14738 {
14739 	Node	   *lexp = lexpr;
14740 
14741 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
14742 	while (IsA(lexp, A_Expr) &&
14743 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
14744 		lexp = ((A_Expr *) lexp)->lexpr;
14745 	/* Flatten "a OR b OR c ..." to a single BoolExpr on sight */
14746 	if (IsA(lexp, BoolExpr))
14747 	{
14748 		BoolExpr *blexpr = (BoolExpr *) lexp;
14749 
14750 		if (blexpr->boolop == OR_EXPR)
14751 		{
14752 			blexpr->args = lappend(blexpr->args, rexpr);
14753 			return (Node *) blexpr;
14754 		}
14755 	}
14756 	return (Node *) makeBoolExpr(OR_EXPR, list_make2(lexpr, rexpr), location);
14757 }
14758 
14759 static Node *
makeNotExpr(Node * expr,int location)14760 makeNotExpr(Node *expr, int location)
14761 {
14762 	return (Node *) makeBoolExpr(NOT_EXPR, list_make1(expr), location);
14763 }
14764 
14765 static Node *
makeAArrayExpr(List * elements,int location)14766 makeAArrayExpr(List *elements, int location)
14767 {
14768 	A_ArrayExpr *n = makeNode(A_ArrayExpr);
14769 
14770 	n->elements = elements;
14771 	n->location = location;
14772 	return (Node *) n;
14773 }
14774 
14775 static Node *
makeXmlExpr(XmlExprOp op,char * name,List * named_args,List * args,int location)14776 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
14777 			int location)
14778 {
14779 	XmlExpr		*x = makeNode(XmlExpr);
14780 
14781 	x->op = op;
14782 	x->name = name;
14783 	/*
14784 	 * named_args is a list of ResTarget; it'll be split apart into separate
14785 	 * expression and name lists in transformXmlExpr().
14786 	 */
14787 	x->named_args = named_args;
14788 	x->arg_names = NIL;
14789 	x->args = args;
14790 	/* xmloption, if relevant, must be filled in by caller */
14791 	/* type and typmod will be filled in during parse analysis */
14792 	x->type = InvalidOid;			/* marks the node as not analyzed */
14793 	x->location = location;
14794 	return (Node *) x;
14795 }
14796 
14797 /*
14798  * Merge the input and output parameters of a table function.
14799  */
14800 static List *
mergeTableFuncParameters(List * func_args,List * columns)14801 mergeTableFuncParameters(List *func_args, List *columns)
14802 {
14803 	ListCell   *lc;
14804 
14805 	/* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
14806 	foreach(lc, func_args)
14807 	{
14808 		FunctionParameter *p = (FunctionParameter *) lfirst(lc);
14809 
14810 		if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
14811 			ereport(ERROR,
14812 					(errcode(ERRCODE_SYNTAX_ERROR),
14813 					 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
14814 	}
14815 
14816 	return list_concat(func_args, columns);
14817 }
14818 
14819 /*
14820  * Determine return type of a TABLE function.  A single result column
14821  * returns setof that column's type; otherwise return setof record.
14822  */
14823 static TypeName *
TableFuncTypeName(List * columns)14824 TableFuncTypeName(List *columns)
14825 {
14826 	TypeName *result;
14827 
14828 	if (list_length(columns) == 1)
14829 	{
14830 		FunctionParameter *p = (FunctionParameter *) linitial(columns);
14831 
14832 		result = (TypeName *) copyObject(p->argType);
14833 	}
14834 	else
14835 		result = SystemTypeName("record");
14836 
14837 	result->setof = true;
14838 
14839 	return result;
14840 }
14841 
14842 /*
14843  * Convert a list of (dotted) names to a RangeVar (like
14844  * makeRangeVarFromNameList, but with position support).  The
14845  * "AnyName" refers to the any_name production in the grammar.
14846  */
14847 static RangeVar *
makeRangeVarFromAnyName(List * names,int position,core_yyscan_t yyscanner)14848 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
14849 {
14850 	RangeVar *r = makeNode(RangeVar);
14851 
14852 	switch (list_length(names))
14853 	{
14854 		case 1:
14855 			r->catalogname = NULL;
14856 			r->schemaname = NULL;
14857 			r->relname = strVal(linitial(names));
14858 			break;
14859 		case 2:
14860 			r->catalogname = NULL;
14861 			r->schemaname = strVal(linitial(names));
14862 			r->relname = strVal(lsecond(names));
14863 			break;
14864 		case 3:
14865 			r->catalogname = strVal(linitial(names));
14866 			r->schemaname = strVal(lsecond(names));
14867 			r->relname = strVal(lthird(names));
14868 			break;
14869 		default:
14870 			ereport(ERROR,
14871 					(errcode(ERRCODE_SYNTAX_ERROR),
14872 					 errmsg("improper qualified name (too many dotted names): %s",
14873 							NameListToString(names)),
14874 					 parser_errposition(position)));
14875 			break;
14876 	}
14877 
14878 	r->relpersistence = RELPERSISTENCE_PERMANENT;
14879 	r->location = position;
14880 
14881 	return r;
14882 }
14883 
14884 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
14885 static void
SplitColQualList(List * qualList,List ** constraintList,CollateClause ** collClause,core_yyscan_t yyscanner)14886 SplitColQualList(List *qualList,
14887 				 List **constraintList, CollateClause **collClause,
14888 				 core_yyscan_t yyscanner)
14889 {
14890 	ListCell   *cell;
14891 	ListCell   *prev;
14892 	ListCell   *next;
14893 
14894 	*collClause = NULL;
14895 	prev = NULL;
14896 	for (cell = list_head(qualList); cell; cell = next)
14897 	{
14898 		Node   *n = (Node *) lfirst(cell);
14899 
14900 		next = lnext(cell);
14901 		if (IsA(n, Constraint))
14902 		{
14903 			/* keep it in list */
14904 			prev = cell;
14905 			continue;
14906 		}
14907 		if (IsA(n, CollateClause))
14908 		{
14909 			CollateClause *c = (CollateClause *) n;
14910 
14911 			if (*collClause)
14912 				ereport(ERROR,
14913 						(errcode(ERRCODE_SYNTAX_ERROR),
14914 						 errmsg("multiple COLLATE clauses not allowed"),
14915 						 parser_errposition(c->location)));
14916 			*collClause = c;
14917 		}
14918 		else
14919 			elog(ERROR, "unexpected node type %d", (int) n->type);
14920 		/* remove non-Constraint nodes from qualList */
14921 		qualList = list_delete_cell(qualList, cell, prev);
14922 	}
14923 	*constraintList = qualList;
14924 }
14925 
14926 /*
14927  * Process result of ConstraintAttributeSpec, and set appropriate bool flags
14928  * in the output command node.  Pass NULL for any flags the particular
14929  * command doesn't support.
14930  */
14931 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)14932 processCASbits(int cas_bits, int location, const char *constrType,
14933 			   bool *deferrable, bool *initdeferred, bool *not_valid,
14934 			   bool *no_inherit, core_yyscan_t yyscanner)
14935 {
14936 	/* defaults */
14937 	if (deferrable)
14938 		*deferrable = false;
14939 	if (initdeferred)
14940 		*initdeferred = false;
14941 	if (not_valid)
14942 		*not_valid = false;
14943 
14944 	if (cas_bits & (CAS_DEFERRABLE | CAS_INITIALLY_DEFERRED))
14945 	{
14946 		if (deferrable)
14947 			*deferrable = true;
14948 		else
14949 			ereport(ERROR,
14950 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14951 					 /* translator: %s is CHECK, UNIQUE, or similar */
14952 					 errmsg("%s constraints cannot be marked DEFERRABLE",
14953 							constrType),
14954 					 parser_errposition(location)));
14955 	}
14956 
14957 	if (cas_bits & CAS_INITIALLY_DEFERRED)
14958 	{
14959 		if (initdeferred)
14960 			*initdeferred = true;
14961 		else
14962 			ereport(ERROR,
14963 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14964 					 /* translator: %s is CHECK, UNIQUE, or similar */
14965 					 errmsg("%s constraints cannot be marked DEFERRABLE",
14966 							constrType),
14967 					 parser_errposition(location)));
14968 	}
14969 
14970 	if (cas_bits & CAS_NOT_VALID)
14971 	{
14972 		if (not_valid)
14973 			*not_valid = true;
14974 		else
14975 			ereport(ERROR,
14976 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14977 					 /* translator: %s is CHECK, UNIQUE, or similar */
14978 					 errmsg("%s constraints cannot be marked NOT VALID",
14979 							constrType),
14980 					 parser_errposition(location)));
14981 	}
14982 
14983 	if (cas_bits & CAS_NO_INHERIT)
14984 	{
14985 		if (no_inherit)
14986 			*no_inherit = true;
14987 		else
14988 			ereport(ERROR,
14989 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14990 					 /* translator: %s is CHECK, UNIQUE, or similar */
14991 					 errmsg("%s constraints cannot be marked NO INHERIT",
14992 							constrType),
14993 					 parser_errposition(location)));
14994 	}
14995 }
14996 
14997 /*----------
14998  * Recursive view transformation
14999  *
15000  * Convert
15001  *
15002  *     CREATE RECURSIVE VIEW relname (aliases) AS query
15003  *
15004  * to
15005  *
15006  *     CREATE VIEW relname (aliases) AS
15007  *         WITH RECURSIVE relname (aliases) AS (query)
15008  *         SELECT aliases FROM relname
15009  *
15010  * Actually, just the WITH ... part, which is then inserted into the original
15011  * view definition as the query.
15012  * ----------
15013  */
15014 static Node *
makeRecursiveViewSelect(char * relname,List * aliases,Node * query)15015 makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
15016 {
15017 	SelectStmt *s = makeNode(SelectStmt);
15018 	WithClause *w = makeNode(WithClause);
15019 	CommonTableExpr *cte = makeNode(CommonTableExpr);
15020 	List	   *tl = NIL;
15021 	ListCell   *lc;
15022 
15023 	/* create common table expression */
15024 	cte->ctename = relname;
15025 	cte->aliascolnames = aliases;
15026 	cte->ctequery = query;
15027 	cte->location = -1;
15028 
15029 	/* create WITH clause and attach CTE */
15030 	w->recursive = true;
15031 	w->ctes = list_make1(cte);
15032 	w->location = -1;
15033 
15034 	/* create target list for the new SELECT from the alias list of the
15035 	 * recursive view specification */
15036 	foreach (lc, aliases)
15037 	{
15038 		ResTarget *rt = makeNode(ResTarget);
15039 
15040 		rt->name = NULL;
15041 		rt->indirection = NIL;
15042 		rt->val = makeColumnRef(strVal(lfirst(lc)), NIL, -1, 0);
15043 		rt->location = -1;
15044 
15045 		tl = lappend(tl, rt);
15046 	}
15047 
15048 	/* create new SELECT combining WITH clause, target list, and fake FROM
15049 	 * clause */
15050 	s->withClause = w;
15051 	s->targetList = tl;
15052 	s->fromClause = list_make1(makeRangeVar(NULL, relname, -1));
15053 
15054 	return (Node *) s;
15055 }
15056 
15057 /* parser_init()
15058  * Initialize to parse one query string
15059  */
15060 void
parser_init(base_yy_extra_type * yyext)15061 parser_init(base_yy_extra_type *yyext)
15062 {
15063 	yyext->parsetree = NIL;		/* in case grammar forgets to set it */
15064 }
15065 
15066 /*
15067  * Create a DefElem setting "oids" to the specified value.
15068  */
15069 static DefElem *
defWithOids(bool value)15070 defWithOids(bool value)
15071 {
15072 	DefElem    *f = makeNode(DefElem);
15073 
15074 	f->defname = "oids";
15075 	f->arg = (Node *) makeInteger(value);
15076 	return f;
15077 }
15078