1 %{
2 
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5  *
6  * gram.y
7  *	  POSTGRESQL BISON rules/actions
8  *
9  * Portions Copyright (c) 2003-2019, PgPool Global Development Group
10  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  *	  src/backend/parser/gram.y
16  *
17  * HISTORY
18  *	  AUTHOR			DATE			MAJOR EVENT
19  *	  Andrew Yu			Sept, 1994		POSTQUEL to SQL conversion
20  *	  Andrew Yu			Oct, 1994		lispy code conversion
21  *
22  * NOTES
23  *	  CAPITALS are used to represent terminal symbols.
24  *	  non-capitals are used to represent non-terminals.
25  *
26  *	  In general, nothing in this file should initiate database accesses
27  *	  nor depend on changeable state (such as SET variables).  If you do
28  *	  database accesses, your code will fail when we have aborted the
29  *	  current transaction and are just parsing commands to find the next
30  *	  ROLLBACK or COMMIT.  If you make use of SET variables, then you
31  *	  will do the wrong thing in multi-query strings like this:
32  *			SET constraint_exclusion TO off; SELECT * FROM foo;
33  *	  because the entire string is parsed by gram.y before the SET gets
34  *	  executed.  Anything that depends on the database or changeable state
35  *	  should be handled during parse analysis so that it happens at the
36  *	  right time not the wrong time.
37  *
38  * WARNINGS
39  *	  If you use a list, make sure the datum is a node so that the printing
40  *	  routines work.
41  *
42  *	  Sometimes we assign constants to makeStrings. Make sure we don't free
43  *	  those.
44  *
45  *-------------------------------------------------------------------------
46  */
47 
48 #include "pool_parser.h"
49 #include "utils/elog.h"
50 #include "utils/palloc.h"
51 #include <ctype.h>
52 #include <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include "nodes.h"
58 #include "keywords.h"
59 #include "gramparse.h"
60 #include "makefuncs.h"
61 #include "pool_string.h"
62 #include "parser.h"
63 #include "pg_class.h"
64 #include "pg_trigger.h"
65 
66 /* This is a configuration parameter since PostgreSQL 9.5.
67  * We set this false in pgpool-II. This is default in PostgreSQL.
68  */
69 static bool operator_precedence_warning = false;
70 
71 /*
72  * Definition taken from
73  * postgreSQL source code file: src/include/commands/trigger.h
74  */
75 #define TRIGGER_FIRES_ON_ORIGIN                         'O'
76 #define TRIGGER_FIRES_ALWAYS                            'A'
77 #define TRIGGER_FIRES_ON_REPLICA                        'R'
78 #define TRIGGER_DISABLED                                'D'
79 
80 /*
81  * Definition taken from
82  * postgreSQL source code file: src/include/catalog/pg_class.h
83  */
84 
85 #define           REPLICA_IDENTITY_DEFAULT      'd'
86 #define           REPLICA_IDENTITY_NOTHING      'n'
87 #define           REPLICA_IDENTITY_FULL         'f'
88 #define           REPLICA_IDENTITY_INDEX        'i'
89 
90 /*
91  * Definition taken from
92  * postgreSQL source code file: src/include/catalog/pg_attribute.h
93  */
94 #define		  ATTRIBUTE_IDENTITY_ALWAYS	'a'
95 #define		  ATTRIBUTE_IDENTITY_BY_DEFAULT 'd'
96 
97 /*
98  * Definition taken from
99  * postgreSQL source code file: src/include/utils/xml.h
100  */
101 typedef enum
102 {
103 	XML_STANDALONE_YES,
104 	XML_STANDALONE_NO,
105 	XML_STANDALONE_NO_VALUE,
106 	XML_STANDALONE_OMITTED
107 } XmlStandaloneType;
108 
109 /*
110  * Location tracking support --- simpler than bison's default, since we only
111  * want to track the start position not the end position of each nonterminal.
112  */
113 #define YYLLOC_DEFAULT(Current, Rhs, N) \
114 	do { \
115 		if ((N) > 0) \
116 			(Current) = (Rhs)[1]; \
117 		else \
118 			(Current) = (-1); \
119 	} while (0)
120 
121 /*
122  * The above macro assigns -1 (unknown) as the parse location of any
123  * nonterminal that was reduced from an empty rule, or whose leftmost
124  * component was reduced from an empty rule.  This is problematic
125  * for nonterminals defined like
126  *		OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ;
127  * because we'll set -1 as the location during the first reduction and then
128  * copy it during each subsequent reduction, leaving us with -1 for the
129  * location even when the list is not empty.  To fix that, do this in the
130  * action for the nonempty rule(s):
131  *		if (@$ < 0) @$ = @2;
132  * (Although we have many nonterminals that follow this pattern, we only
133  * bother with fixing @$ like this when the nonterminal's parse location
134  * is actually referenced in some rule.)
135  *
136  * A cleaner answer would be to make YYLLOC_DEFAULT scan all the Rhs
137  * locations until it's found one that's not -1.  Then we'd get a correct
138  * location for any nonterminal that isn't entirely empty.  But this way
139  * would add overhead to every rule reduction, and so far there's not been
140  * a compelling reason to pay that overhead.
141  */
142 
143 /*
144  * Bison doesn't allocate anything that needs to live across parser calls,
145  * so we can easily have it use palloc instead of malloc.  This prevents
146  * memory leaks if we error out during parsing.  Note this only works with
147  * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
148  * if possible, so there's not really much problem anyhow, at least if
149  * you're building with gcc.
150  */
151 #define YYMALLOC palloc
152 #define YYFREE   pfree
153 
154 /* Private struct for the result of privilege_target production */
155 typedef struct PrivTarget
156 {
157 	GrantTargetType targtype;
158 	ObjectType	objtype;
159 	List	   *objs;
160 } PrivTarget;
161 
162 /* Private struct for the result of import_qualification production */
163 typedef struct ImportQual
164 {
165 	ImportForeignSchemaType type;
166 	List	   *table_names;
167 } ImportQual;
168 
169 /* ConstraintAttributeSpec yields an integer bitmask of these flags: */
170 #define CAS_NOT_DEFERRABLE			0x01
171 #define CAS_DEFERRABLE				0x02
172 #define CAS_INITIALLY_IMMEDIATE		0x04
173 #define CAS_INITIALLY_DEFERRED		0x08
174 #define CAS_NOT_VALID				0x10
175 #define CAS_NO_INHERIT				0x20
176 
177 
178 #define parser_yyerror(msg)  scanner_yyerror(msg, yyscanner)
179 #define parser_errposition(pos)  scanner_errposition(pos, yyscanner)
180 
181 static void minimal_base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner,
182 						 const char *msg);
183 static RawStmt *makeRawStmt(Node *stmt, int stmt_location);
184 static void updateRawStmtEnd(RawStmt *rs, int end_location);
185 static Node *makeColumnRef(char *colname, List *indirection,
186 						   int location, core_yyscan_t yyscanner);
187 static Node *makeStringConst(char *str, int location);
188 static Node *makeFloatConst(char *str, int location);
189 static Node *makeBitStringConst(char *str, int location);
190 static Node *makeNullAConst(int location);
191 static Node *makeAConst(Value *v, int location);
192 static Node *makeBoolAConst(bool state, int location);
193 static RoleSpec *makeRoleSpec(RoleSpecType type, int location);
194 static void check_qualified_name(List *names, core_yyscan_t yyscanner);
195 static List *check_func_name(List *names, core_yyscan_t yyscanner);
196 static List *check_indirection(List *indirection, core_yyscan_t yyscanner);
197 static List *extractArgTypes(List *parameters);
198 static List *extractAggrArgTypes(List *aggrargs);
199 static List *makeOrderedSetArgs(List *directargs, List *orderedargs,
200 								core_yyscan_t yyscanner);
201 static void insertSelectOptions(SelectStmt *stmt,
202 								List *sortClause, List *lockingClause,
203 								Node *limitOffset, Node *limitCount,
204 								WithClause *withClause,
205 								core_yyscan_t yyscanner);
206 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
207 static Node *doNegate(Node *n, int location);
208 static void doNegateFloat(Value *v);
209 static Node *makeAndExpr(Node *lexpr, Node *rexpr, int location);
210 static Node *makeOrExpr(Node *lexpr, Node *rexpr, int location);
211 static Node *makeNotExpr(Node *expr, int location);
212 static Node *makeAArrayExpr(List *elements, int location);
213 static Node *makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod,
214 								  int location);
215 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
216 						 List *args, int location);
217 static List *mergeTableFuncParameters(List *func_args, List *columns);
218 static TypeName *TableFuncTypeName(List *columns);
219 static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner);
220 static void SplitColQualList(List *qualList,
221 							 List **constraintList, CollateClause **collClause,
222 							 core_yyscan_t yyscanner);
223 static void processCASbits(int cas_bits, int location, const char *constrType,
224 			   bool *deferrable, bool *initdeferred, bool *not_valid,
225 			   bool *no_inherit, core_yyscan_t yyscanner);
226 static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
227 
228 %}
229 
230 %pure-parser
231 %expect 0
232 %name-prefix="minimal_base_yy"
233 %locations
234 
235 %parse-param {core_yyscan_t yyscanner}
236 %lex-param   {core_yyscan_t yyscanner}
237 
238 %union
239 {
240 	core_YYSTYPE		core_yystype;
241 	/* these fields must match core_YYSTYPE: */
242 	int					ival;
243 	char				*str;
244 	const char			*keyword;
245 
246 	char				chr;
247 	bool				boolean;
248 	JoinType			jtype;
249 	DropBehavior		dbehavior;
250 	OnCommitAction		oncommit;
251 	List				*list;
252 	Node				*node;
253 	Value				*value;
254 	ObjectType			objtype;
255 	TypeName			*typnam;
256 	FunctionParameter   *fun_param;
257 	FunctionParameterMode fun_param_mode;
258 	ObjectWithArgs		*objwithargs;
259 	DefElem				*defelt;
260 	SortBy				*sortby;
261 	WindowDef			*windef;
262 	JoinExpr			*jexpr;
263 	IndexElem			*ielem;
264 	Alias				*alias;
265 	RangeVar			*range;
266 	IntoClause			*into;
267 	WithClause			*with;
268 	InferClause			*infer;
269 	OnConflictClause	*onconflict;
270 	A_Indices			*aind;
271 	ResTarget			*target;
272 	struct PrivTarget	*privtarget;
273 	AccessPriv			*accesspriv;
274 	struct ImportQual	*importqual;
275 	InsertStmt			*istmt;
276 	VariableSetStmt		*vsetstmt;
277 	PartitionElem		*partelem;
278 	PartitionSpec		*partspec;
279 	PartitionBoundSpec	*partboundspec;
280 	RoleSpec			*rolespec;
281 }
282 
283 %type <node>	stmt schema_stmt
284 		AlterEventTrigStmt AlterCollationStmt
285 		AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterEnumStmt
286 		AlterFdwStmt AlterForeignServerStmt AlterGroupStmt
287 		AlterObjectDependsStmt AlterObjectSchemaStmt AlterOwnerStmt
288 		AlterOperatorStmt AlterSeqStmt AlterSystemStmt AlterTableStmt
289 		AlterTblSpcStmt AlterExtensionStmt AlterExtensionContentsStmt AlterForeignTableStmt
290 		AlterCompositeTypeStmt AlterUserMappingStmt
291 		AlterRoleStmt AlterRoleSetStmt AlterPolicyStmt
292 		AlterDefaultPrivilegesStmt DefACLAction
293 		AnalyzeStmt CallStmt ClosePortalStmt ClusterStmt CommentStmt
294 		ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
295 		CreateDomainStmt CreateExtensionStmt CreateGroupStmt CreateOpClassStmt
296 		CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
297 		CreateSchemaStmt CreateSeqStmt CreateStmt CreateStatsStmt CreateTableSpaceStmt
298 		CreateFdwStmt CreateForeignServerStmt CreateForeignTableStmt
299 		CreateAssertionStmt CreateTransformStmt CreateTrigStmt CreateEventTrigStmt
300 		CreateUserStmt CreateUserMappingStmt CreateRoleStmt CreatePolicyStmt
301 		CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
302 		DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
303 		DropCastStmt DropRoleStmt
304 		DropdbStmt DropTableSpaceStmt
305 		DropTransformStmt
306 		DropUserMappingStmt ExplainStmt FetchStmt
307 		GrantStmt GrantRoleStmt ImportForeignSchemaStmt IndexStmt InsertStmt
308 InsertStmtShort
309 		ListenStmt LoadStmt LockStmt NotifyStmt ExplainableStmt PreparableStmt
310 		CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
311 		RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
312 		RuleActionStmt RuleActionStmtOrEmpty RuleStmt
313 		SecLabelStmt SelectStmt TransactionStmt TruncateStmt
314 		UnlistenStmt UpdateStmt
315 UpdateStmtShort
316 		VacuumStmt
317 		VariableResetStmt VariableSetStmt VariableShowStmt
318 		ViewStmt CheckPointStmt CreateConversionStmt
319 		DeallocateStmt PrepareStmt ExecuteStmt
320 		DropOwnedStmt ReassignOwnedStmt
321 		AlterTSConfigurationStmt AlterTSDictionaryStmt
322 		CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
323 		CreatePublicationStmt AlterPublicationStmt
324 		CreateSubscriptionStmt AlterSubscriptionStmt DropSubscriptionStmt
325 
326 %type <node>	select_no_parens select_with_parens select_clause
327 				simple_select values_clause
328 
329 %type <node>	alter_column_default opclass_item opclass_drop alter_using
330 %type <ival>	add_drop opt_asc_desc opt_nulls_order
331 
332 %type <node>	alter_table_cmd alter_type_cmd opt_collate_clause
333 	   replica_identity partition_cmd index_partition_cmd
334 %type <list>	alter_table_cmds alter_type_cmds
335 %type <list>    alter_identity_column_option_list
336 %type <defelt>  alter_identity_column_option
337 
338 %type <dbehavior>	opt_drop_behavior
339 
340 %type <list>	createdb_opt_list createdb_opt_items copy_opt_list
341 				transaction_mode_list
342 				create_extension_opt_list alter_extension_opt_list
343 %type <defelt>	createdb_opt_item copy_opt_item
344 				transaction_mode_item
345 				create_extension_opt_item alter_extension_opt_item
346 
347 %type <ival>	opt_lock lock_type cast_context
348 %type <str>		vac_analyze_option_name
349 %type <defelt>	vac_analyze_option_elem
350 %type <list>	vac_analyze_option_list
351 %type <node>	vac_analyze_option_arg
352 %type <boolean>	opt_or_replace
353 				opt_grant_grant_option opt_grant_admin_option
354 				opt_nowait opt_if_exists opt_with_data
355 				opt_transaction_chain
356 %type <ival>	opt_nowait_or_skip
357 
358 %type <list>	OptRoleList AlterOptRoleList
359 %type <defelt>	CreateOptRoleElem AlterOptRoleElem
360 
361 %type <str>		opt_type
362 %type <str>		foreign_server_version opt_foreign_server_version
363 %type <str>		opt_in_database
364 
365 %type <str>		OptSchemaName
366 %type <list>	OptSchemaEltList
367 
368 %type <chr>		am_type
369 
370 %type <boolean> TriggerForSpec TriggerForType
371 %type <ival>	TriggerActionTime
372 %type <list>	TriggerEvents TriggerOneEvent
373 %type <value>	TriggerFuncArg
374 %type <node>	TriggerWhen
375 %type <str>		TransitionRelName
376 %type <boolean>	TransitionRowOrTable TransitionOldOrNew
377 %type <node>	TriggerTransition
378 
379 %type <list>	event_trigger_when_list event_trigger_value_list
380 %type <defelt>	event_trigger_when_item
381 %type <chr>		enable_trigger
382 
383 %type <str>		copy_file_name
384 				database_name access_method_clause access_method attr_name
385 				table_access_method_clause name cursor_name file_name
386 				index_name opt_index_name cluster_index_specification
387 
388 %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
389 				opt_class opt_inline_handler opt_validator validator_clause
390 				opt_collate
391 
392 %type <range>	qualified_name insert_target OptConstrFromTable
393 
394 %type <str>		all_Op MathOp
395 
396 %type <str>		row_security_cmd RowSecurityDefaultForCmd
397 %type <boolean> RowSecurityDefaultPermissive
398 %type <node>	RowSecurityOptionalWithCheck RowSecurityOptionalExpr
399 %type <list>	RowSecurityDefaultToRole RowSecurityOptionalToRole
400 
401 %type <str>		iso_level opt_encoding
402 %type <rolespec> grantee
403 %type <list>	grantee_list
404 %type <accesspriv> privilege
405 %type <list>	privileges privilege_list
406 %type <privtarget> privilege_target
407 %type <objwithargs> function_with_argtypes aggregate_with_argtypes operator_with_argtypes
408 %type <list>	function_with_argtypes_list aggregate_with_argtypes_list operator_with_argtypes_list
409 %type <ival>	defacl_privilege_target
410 %type <defelt>	DefACLOption
411 %type <list>	DefACLOptionList
412 %type <ival>	import_qualification_type
413 %type <importqual> import_qualification
414 %type <node>	vacuum_relation
415 
416 %type <list>	stmtblock stmtmulti
417 				OptTableElementList TableElementList OptInherit definition
418 				OptTypedTableElementList TypedTableElementList
419 				reloptions opt_reloptions
420 				OptWith distinct_clause opt_all_clause opt_definition func_args func_args_list
421 				func_args_with_defaults func_args_with_defaults_list
422 				aggr_args aggr_args_list
423 				func_as createfunc_opt_list alterfunc_opt_list
424 				old_aggr_definition old_aggr_list
425 				oper_argtypes RuleActionList RuleActionMulti
426 				opt_column_list columnList opt_name_list
427 				sort_clause opt_sort_clause sortby_list index_params
428 				opt_include opt_c_include index_including_params
429 				name_list role_list from_clause from_list opt_array_bounds
430 				qualified_name_list any_name any_name_list type_name_list
431 				any_operator expr_list attrs
432 				target_list opt_target_list insert_column_list set_target_list
433 				set_clause_list set_clause
434 				def_list operator_def_list indirection opt_indirection
435 				reloption_list group_clause TriggerFuncArgs select_limit
436 				opt_select_limit opclass_item_list opclass_drop_list
437 				opclass_purpose opt_opfamily transaction_mode_list_or_empty
438 				OptTableFuncElementList TableFuncElementList opt_type_modifiers
439 				prep_type_clause
440 				execute_param_clause using_clause returning_clause
441 				opt_enum_val_list enum_val_list table_func_column_list
442 				create_generic_options alter_generic_options
443 				relation_expr_list dostmt_opt_list
444 				transform_element_list transform_type_list
445 				TriggerTransitions TriggerReferencing
446 				publication_name_list
447 				vacuum_relation_list opt_vacuum_relation_list
448 
449 %type <list>	group_by_list
450 %type <node>	group_by_item empty_grouping_set rollup_clause cube_clause
451 %type <node>	grouping_sets_clause
452 %type <node>	opt_publication_for_tables publication_for_tables
453 %type <value>	publication_name_item
454 
455 %type <list>	opt_fdw_options fdw_options
456 %type <defelt>	fdw_option
457 
458 %type <range>	OptTempTableName
459 %type <into>	into_clause create_as_target create_mv_target
460 
461 %type <defelt>	createfunc_opt_item common_func_opt_item dostmt_opt_item
462 %type <fun_param> func_arg func_arg_with_default table_func_column aggr_arg
463 %type <fun_param_mode> arg_class
464 %type <typnam>	func_return func_type
465 
466 %type <boolean>  opt_trusted opt_restart_seqs
467 %type <ival>	 OptTemp
468 %type <ival>	 OptNoLog
469 %type <oncommit> OnCommitOption
470 
471 %type <ival>	for_locking_strength
472 %type <node>	for_locking_item
473 %type <list>	for_locking_clause opt_for_locking_clause for_locking_items
474 %type <list>	locked_rels_list
475 %type <boolean>	all_or_distinct
476 
477 %type <node>	join_outer join_qual
478 %type <jtype>	join_type
479 
480 %type <list>	extract_list overlay_list position_list
481 %type <list>	substr_list trim_list
482 %type <list>	opt_interval interval_second
483 %type <node>	overlay_placing substr_from substr_for
484 
485 %type <boolean> opt_instead
486 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
487 %type <boolean> opt_freeze opt_analyze opt_default opt_recheck
488 %type <defelt>	opt_binary copy_delimiter
489 
490 %type <boolean> copy_from opt_program
491 
492 %type <ival>	opt_column event cursor_options opt_hold opt_set_data
493 %type <objtype>	drop_type_any_name drop_type_name drop_type_name_on_any_name
494 				comment_type_any_name comment_type_name
495 				security_label_type_any_name security_label_type_name
496 
497 %type <node>	fetch_args limit_clause select_limit_value
498 				offset_clause select_offset_value
499 				select_fetch_first_value I_or_F_const
500 %type <ival>	row_or_rows first_or_next
501 
502 %type <list>	OptSeqOptList SeqOptList OptParenthesizedSeqOptList
503 %type <defelt>	SeqOptElem
504 
505 %type <istmt>	insert_rest
506 %type <infer>	opt_conf_expr
507 %type <onconflict> opt_on_conflict
508 
509 %type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
510 				 SetResetClause FunctionSetResetClause
511 
512 %type <node>	TableElement TypedTableElement ConstraintElem TableFuncElement
513 %type <node>	columnDef columnOptions
514 %type <defelt>	def_elem reloption_elem old_aggr_elem operator_def_elem
515 %type <node>	def_arg columnElem where_clause where_or_current_clause
516 				a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
517 				columnref in_expr having_clause func_table xmltable array_expr
518 				ExclusionWhereClause operator_def_arg
519 %type <list>	rowsfrom_item rowsfrom_list opt_col_def_list
520 %type <boolean> opt_ordinality
521 %type <list>	ExclusionConstraintList ExclusionConstraintElem
522 %type <list>	func_arg_list
523 %type <node>	func_arg_expr
524 %type <list>	row explicit_row implicit_row type_list array_expr_list
525 %type <node>	case_expr case_arg when_clause case_default
526 %type <list>	when_clause_list
527 %type <ival>	sub_type opt_materialized
528 %type <value>	NumericOnly
529 %type <list>	NumericOnly_list
530 %type <alias>	alias_clause opt_alias_clause
531 %type <list>	func_alias_clause
532 %type <sortby>	sortby
533 %type <ielem>	index_elem
534 %type <node>	table_ref
535 %type <jexpr>	joined_table
536 %type <range>	relation_expr
537 %type <range>	relation_expr_opt_alias
538 %type <node>	tablesample_clause opt_repeatable_clause
539 %type <target>	target_el set_target insert_column_item
540 
541 %type <str>		generic_option_name
542 %type <node>	generic_option_arg
543 %type <defelt>	generic_option_elem alter_generic_option_elem
544 %type <list>	generic_option_list alter_generic_option_list
545 %type <str>		explain_option_name
546 %type <node>	explain_option_arg
547 %type <defelt>	explain_option_elem
548 %type <list>	explain_option_list
549 
550 %type <ival>	reindex_target_type reindex_target_multitable
551 %type <ival>	reindex_option_list reindex_option_elem
552 
553 %type <node>	copy_generic_opt_arg copy_generic_opt_arg_list_item
554 %type <defelt>	copy_generic_opt_elem
555 %type <list>	copy_generic_opt_list copy_generic_opt_arg_list
556 %type <list>	copy_options
557 
558 %type <typnam>	Typename SimpleTypename ConstTypename
559 				GenericType Numeric opt_float
560 				Character ConstCharacter
561 				CharacterWithLength CharacterWithoutLength
562 				ConstDatetime ConstInterval
563 				Bit ConstBit BitWithLength BitWithoutLength
564 %type <str>		character
565 %type <str>		extract_arg
566 %type <boolean> opt_varying opt_timezone opt_no_inherit
567 
568 %type <ival>	Iconst SignedIconst
569 %type <str>		Sconst comment_text notify_payload
570 %type <str>		RoleId opt_boolean_or_string
571 %type <list>	var_list
572 %type <str>		ColId ColLabel var_name type_function_name param_name
573 %type <str>		NonReservedWord NonReservedWord_or_Sconst
574 %type <str>		createdb_opt_name
575 %type <node>	var_value zone_value
576 %type <rolespec> auth_ident RoleSpec opt_granted_by
577 
578 %type <keyword> unreserved_keyword type_func_name_keyword
579 %type <keyword> col_name_keyword reserved_keyword
580 
581 %type <node>	TableConstraint TableLikeClause
582 %type <ival>	TableLikeOptionList TableLikeOption
583 %type <list>	ColQualList
584 %type <node>	ColConstraint ColConstraintElem ConstraintAttr
585 %type <ival>	key_actions key_delete key_match key_update key_action
586 %type <ival>	ConstraintAttributeSpec ConstraintAttributeElem
587 %type <str>		ExistingIndex
588 
589 %type <list>	constraints_set_list
590 %type <boolean> constraints_set_mode
591 %type <str>		OptTableSpace OptConsTableSpace
592 %type <rolespec> OptTableSpaceOwner
593 %type <ival>	opt_check_option
594 
595 %type <str>		opt_provider security_label
596 
597 %type <target>	xml_attribute_el
598 %type <list>	xml_attribute_list xml_attributes
599 %type <node>	xml_root_version opt_xml_root_standalone
600 %type <node>	xmlexists_argument
601 %type <ival>	document_or_content
602 %type <boolean> xml_whitespace_option
603 %type <list>	xmltable_column_list xmltable_column_option_list
604 %type <node>	xmltable_column_el
605 %type <defelt>	xmltable_column_option_el
606 %type <list>	xml_namespace_list
607 %type <target>	xml_namespace_el
608 
609 %type <node>	func_application func_expr_common_subexpr
610 %type <node>	func_expr func_expr_windowless
611 %type <node>	common_table_expr
612 %type <with>	with_clause opt_with_clause
613 %type <list>	cte_list
614 
615 %type <list>	within_group_clause
616 %type <node>	filter_clause
617 %type <list>	window_clause window_definition_list opt_partition_clause
618 %type <windef>	window_definition over_clause window_specification
619 				opt_frame_clause frame_extent frame_bound
620 %type <ival>	opt_window_exclusion_clause
621 %type <str>		opt_existing_window_name
622 %type <boolean> opt_if_not_exists
623 %type <ival>	generated_when override_kind
624 %type <partspec>	PartitionSpec OptPartitionSpec
625 %type <str>			part_strategy
626 %type <partelem>	part_elem
627 %type <list>		part_params
628 %type <partboundspec> PartitionBoundSpec
629 %type <list>		hash_partbound
630 %type <defelt>		hash_partbound_elem
631 
632 /*
633  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
634  * They must be listed first so that their numeric codes do not depend on
635  * the set of keywords.  PL/pgSQL depends on this so that it can share the
636  * same lexer.  If you add/change tokens here, fix PL/pgSQL to match!
637  *
638  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
639  * parse errors.  It is needed by PL/pgSQL.
640  */
641 %token <str>	IDENT FCONST SCONST BCONST XCONST Op
642 %token <ival>	ICONST PARAM
643 %token			TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
644 %token			LESS_EQUALS GREATER_EQUALS NOT_EQUALS
645 
646 /*
647  * If you want to make any keyword changes, update the keyword table in
648  * src/include/parser/kwlist.h and add new keywords to the appropriate one
649  * of the reserved-or-not-so-reserved keyword lists, below; search
650  * this file for "Keyword category lists".
651  */
652 
653 /* ordinary key words in alphabetical order */
654 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
655 	AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
656 	ASSERTION ASSIGNMENT ASYMMETRIC AT ATTACH ATTRIBUTE AUTHORIZATION
657 
658 	BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
659 	BOOLEAN_P BOTH BY
660 
661 	CACHE CALL CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
662 	CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
663 	CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMENT COMMENTS COMMIT
664 	COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION CONSTRAINT
665 	CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE
666 	CROSS CSV CUBE CURRENT_P
667 	CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
668 	CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
669 
670 	DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
671 	DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DESC
672 	DETACH DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P
673 	DOUBLE_P DROP
674 
675 	EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EVENT EXCEPT
676 	EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
677 	EXTENSION EXTERNAL EXTRACT
678 
679 	FALSE_P FAMILY FETCH FILTER FIRST_P FLOAT_P FOLLOWING FOR
680 	FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
681 
682 	GENERATED GLOBAL GRANT GRANTED GREATEST GROUP_P GROUPING GROUPS
683 
684 	HANDLER HAVING HEADER_P HOLD HOUR_P
685 
686 	IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P INCLUDE
687 	INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
688 	INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
689 	INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
690 
691 	JOIN
692 
693 	KEY
694 
695 	LABEL LANGUAGE LARGE_P LAST_P LATERAL_P
696 	LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL
697 	LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED
698 
699 	MAPPING MATCH MATERIALIZED MAXVALUE METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE
700 
701 	NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NONE
702 	NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
703 	NULLS_P NUMERIC
704 
705 	OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
706 	ORDER ORDINALITY OTHERS OUT_P OUTER_P
707 	OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
708 
709 	PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PGPOOL PLACING PLANS POLICY
710 	POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
711 	PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROCEDURES PROGRAM PUBLICATION
712 
713 	QUOTE
714 
715 	RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REFERENCING
716 	REFRESH REINDEX RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
717 	RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP
718 	ROUTINE ROUTINES ROW ROWS RULE
719 
720 	SAVEPOINT SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
721 	SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
722 	SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P
723 	START STATEMENT STATISTICS STDIN STDOUT STORAGE STORED STRICT_P STRIP_P
724 	SUBSCRIPTION SUBSTRING SUPPORT SYMMETRIC SYSID SYSTEM_P
725 
726 	TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
727 	TIES TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM
728 	TREAT TRIGGER TRIM TRUE_P
729 	TRUNCATE TRUSTED TYPE_P TYPES_P
730 
731 	UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNLOGGED
732 	UNTIL UPDATE USER USING
733 
734 	VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
735 	VERBOSE VERSION_P VIEW VIEWS VOLATILE
736 
737 	WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE
738 
739 	XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLNAMESPACES
740 	XMLPARSE XMLPI XMLROOT XMLSERIALIZE XMLTABLE
741 
742 	YEAR_P YES_P
743 
744 	ZONE
745 
746 /*
747  * The grammar thinks these are keywords, but they are not in the kwlist.h
748  * list and so can never be entered directly.  The filter in parser.c
749  * creates these tokens when required (based on looking one token ahead).
750  *
751  * NOT_LA exists so that productions such as NOT LIKE can be given the same
752  * precedence as LIKE; otherwise they'd effectively have the same precedence
753  * as NOT, at least with respect to their left-hand subexpression.
754  * NULLS_LA and WITH_LA are needed to make the grammar LALR(1).
755  */
756 %token		NOT_LA NULLS_LA WITH_LA
757 
758 
759 /* Precedence: lowest to highest */
760 %nonassoc	SET				/* see relation_expr_opt_alias */
761 %left		UNION EXCEPT
762 %left		INTERSECT
763 %left		OR
764 %left		AND
765 %right		NOT
766 %nonassoc	IS ISNULL NOTNULL	/* IS sets precedence for IS NULL, etc */
767 %nonassoc	'<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
768 %nonassoc	BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA
769 %nonassoc	ESCAPE			/* ESCAPE must be just above LIKE/ILIKE/SIMILAR */
770 %left		POSTFIXOP		/* dummy for postfix Op rules */
771 /*
772  * To support target_el without AS, we must give IDENT an explicit priority
773  * between POSTFIXOP and Op.  We can safely assign the same priority to
774  * various unreserved keywords as needed to resolve ambiguities (this can't
775  * have any bad effects since obviously the keywords will still behave the
776  * same as if they weren't keywords).  We need to do this:
777  * for PARTITION, RANGE, ROWS, GROUPS to support opt_existing_window_name;
778  * for RANGE, ROWS, GROUPS so that they can follow a_expr without creating
779  * postfix-operator problems;
780  * for GENERATED so that it can follow b_expr;
781  * and for NULL so that it can follow b_expr in ColQualList without creating
782  * postfix-operator problems.
783  *
784  * To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
785  * an explicit priority lower than '(', so that a rule with CUBE '(' will shift
786  * rather than reducing a conflicting rule that takes CUBE as a function name.
787  * Using the same precedence as IDENT seems right for the reasons given above.
788  *
789  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
790  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
791  * there is no principled way to distinguish these from the productions
792  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
793  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
794  * appear to cause UNBOUNDED to be treated differently from other unreserved
795  * keywords anywhere else in the grammar, but it's definitely risky.  We can
796  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
797  */
798 %nonassoc	UNBOUNDED		/* ideally should have same precedence as IDENT */
799 %nonassoc	IDENT GENERATED NULL_P PARTITION RANGE ROWS GROUPS PRECEDING FOLLOWING CUBE ROLLUP
800 %left		Op OPERATOR		/* multi-character ops and user-defined operators */
801 %left		'+' '-'
802 %left		'*' '/' '%'
803 %left		'^'
804 /* Unary Operators */
805 %left		AT				/* sets precedence for AT TIME ZONE */
806 %left		COLLATE
807 %right		UMINUS
808 %left		'[' ']'
809 %left		'(' ')'
810 %left		TYPECAST
811 %left		'.'
812 /*
813  * These might seem to be low-precedence, but actually they are not part
814  * of the arithmetic hierarchy at all in their use as JOIN operators.
815  * We make them high-precedence to support their use as function names.
816  * They wouldn't be given a precedence at all, were it not that we need
817  * left-associativity among the JOIN rules themselves.
818  */
819 %left		JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
820 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
821 %right		PRESERVE STRIP_P
822 
823 %%
824 
825 /*
826  *	The target production for the whole parse.
827  */
828 stmtblock:	stmtmulti
829 			{
830 				pg_yyget_extra(yyscanner)->parsetree = $1;
831 			}
832 		;
833 
834 /*
835  * At top level, we wrap each stmt with a RawStmt node carrying start location
836  * and length of the stmt's text.  Notice that the start loc/len are driven
837  * entirely from semicolon locations (@2).  It would seem natural to use
838  * @1 or @3 to get the true start location of a stmt, but that doesn't work
839  * for statements that can start with empty nonterminals (opt_with_clause is
840  * the main offender here); as noted in the comments for YYLLOC_DEFAULT,
841  * we'd get -1 for the location in such cases.
842  * We also take care to discard empty statements entirely.
843  */
844 stmtmulti:	stmtmulti ';' stmt
845 				{
846 					if ($1 != NIL)
847 					{
848 						/* update length of previous stmt */
849 						updateRawStmtEnd(llast_node(RawStmt, $1), @2);
850 					}
851 					if ($3 != NULL)
852 						$$ = lappend($1, makeRawStmt($3, @2 + 1));
853 					else
854 						$$ = $1;
855 				}
856 			| stmt
857 				{
858 					if ($1 != NULL)
859 						$$ = list_make1(makeRawStmt($1, 0));
860 					else
861 						$$ = NIL;
862 				}
863 		;
864 
865 stmt :
866 			AlterEventTrigStmt
867 			| AlterCollationStmt
868 			| AlterDatabaseStmt
869 			| AlterDatabaseSetStmt
870 			| AlterDefaultPrivilegesStmt
871 			| AlterDomainStmt
872 			| AlterEnumStmt
873 			| AlterExtensionStmt
874 			| AlterExtensionContentsStmt
875 			| AlterFdwStmt
876 			| AlterForeignServerStmt
877 			| AlterForeignTableStmt
878 			| AlterFunctionStmt
879 			| AlterGroupStmt
880 			| AlterObjectDependsStmt
881 			| AlterObjectSchemaStmt
882 			| AlterOwnerStmt
883 			| AlterOperatorStmt
884 			| AlterPolicyStmt
885 			| AlterSeqStmt
886 			| AlterSystemStmt
887 			| AlterTableStmt
888 			| AlterTblSpcStmt
889 			| AlterCompositeTypeStmt
890 			| AlterPublicationStmt
891 			| AlterRoleSetStmt
892 			| AlterRoleStmt
893 			| AlterSubscriptionStmt
894 			| AlterTSConfigurationStmt
895 			| AlterTSDictionaryStmt
896 			| AlterUserMappingStmt
897 			| AnalyzeStmt
898 			| CallStmt
899 			| CheckPointStmt
900 			| ClosePortalStmt
901 			| ClusterStmt
902 			| CommentStmt
903 			| ConstraintsSetStmt
904 			| CopyStmt
905 			| CreateAmStmt
906 			| CreateAsStmt
907 			| CreateAssertionStmt
908 			| CreateCastStmt
909 			| CreateConversionStmt
910 			| CreateDomainStmt
911 			| CreateExtensionStmt
912 			| CreateFdwStmt
913 			| CreateForeignServerStmt
914 			| CreateForeignTableStmt
915 			| CreateFunctionStmt
916 			| CreateGroupStmt
917 			| CreateMatViewStmt
918 			| CreateOpClassStmt
919 			| CreateOpFamilyStmt
920 			| CreatePublicationStmt
921 			| AlterOpFamilyStmt
922 			| CreatePolicyStmt
923 			| CreatePLangStmt
924 			| CreateSchemaStmt
925 			| CreateSeqStmt
926 			| CreateStmt
927 			| CreateSubscriptionStmt
928 			| CreateStatsStmt
929 			| CreateTableSpaceStmt
930 			| CreateTransformStmt
931 			| CreateTrigStmt
932 			| CreateEventTrigStmt
933 			| CreateRoleStmt
934 			| CreateUserStmt
935 			| CreateUserMappingStmt
936 			| CreatedbStmt
937 			| DeallocateStmt
938 			| DeclareCursorStmt
939 			| DefineStmt
940 			| DeleteStmt
941 			| DiscardStmt
942 			| DoStmt
943 			| DropCastStmt
944 			| DropOpClassStmt
945 			| DropOpFamilyStmt
946 			| DropOwnedStmt
947 			| DropPLangStmt
948 			| DropStmt
949 			| DropSubscriptionStmt
950 			| DropTableSpaceStmt
951 			| DropTransformStmt
952 			| DropRoleStmt
953 			| DropUserMappingStmt
954 			| DropdbStmt
955 			| ExecuteStmt
956 			| ExplainStmt
957 			| FetchStmt
958 			| GrantStmt
959 			| GrantRoleStmt
960 			| ImportForeignSchemaStmt
961 			| IndexStmt
962 			| InsertStmtShort
963 			| ListenStmt
964 			| RefreshMatViewStmt
965 			| LoadStmt
966 			| LockStmt
967 			| NotifyStmt
968 			| PrepareStmt
969 			| ReassignOwnedStmt
970 			| ReindexStmt
971 			| RemoveAggrStmt
972 			| RemoveFuncStmt
973 			| RemoveOperStmt
974 			| RenameStmt
975 			| RevokeStmt
976 			| RevokeRoleStmt
977 			| RuleStmt
978 			| SecLabelStmt
979 			| SelectStmt
980 			| TransactionStmt
981 			| TruncateStmt
982 			| UnlistenStmt
983 			| UpdateStmtShort
984 			| VacuumStmt
985 			| VariableResetStmt
986 			| VariableSetStmt
987 			| VariableShowStmt
988 			| ViewStmt
989 			| /*EMPTY*/
990 				{ $$ = NULL; }
991 		;
992 
993 /*****************************************************************************
994  *
995  * CALL statement
996  *
997  *****************************************************************************/
998 
999 CallStmt:	CALL func_application
1000 				{
1001 					CallStmt *n = makeNode(CallStmt);
1002 					n->funccall = castNode(FuncCall, $2);
1003 					$$ = (Node *)n;
1004 				}
1005 		;
1006 
1007 /*****************************************************************************
1008  *
1009  * Create a new Postgres DBMS role
1010  *
1011  *****************************************************************************/
1012 
1013 CreateRoleStmt:
1014 			CREATE ROLE RoleId opt_with OptRoleList
1015 				{
1016 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1017 					n->stmt_type = ROLESTMT_ROLE;
1018 					n->role = $3;
1019 					n->options = $5;
1020 					$$ = (Node *)n;
1021 				}
1022 		;
1023 
1024 
1025 opt_with:	WITH									{}
1026 			| WITH_LA								{}
1027 			| /*EMPTY*/								{}
1028 		;
1029 
1030 /*
1031  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
1032  * for backwards compatibility).  Note: the only option required by SQL99
1033  * is "WITH ADMIN name".
1034  */
1035 OptRoleList:
1036 			OptRoleList CreateOptRoleElem			{ $$ = lappend($1, $2); }
1037 			| /* EMPTY */							{ $$ = NIL; }
1038 		;
1039 
1040 AlterOptRoleList:
1041 			AlterOptRoleList AlterOptRoleElem		{ $$ = lappend($1, $2); }
1042 			| /* EMPTY */							{ $$ = NIL; }
1043 		;
1044 
1045 AlterOptRoleElem:
1046 			PASSWORD Sconst
1047 				{
1048 					$$ = makeDefElem("password",
1049 									 (Node *)makeString($2), @1);
1050 				}
1051 			| PASSWORD NULL_P
1052 				{
1053 					$$ = makeDefElem("password", NULL, @1);
1054 				}
1055 			| ENCRYPTED PASSWORD Sconst
1056 				{
1057 					/*
1058 					 * These days, passwords are always stored in encrypted
1059 					 * form, so there is no difference between PASSWORD and
1060 					 * ENCRYPTED PASSWORD.
1061 					 */
1062 					$$ = makeDefElem("password",
1063 									 (Node *)makeString($3), @1);
1064 				}
1065 			| UNENCRYPTED PASSWORD Sconst
1066 				{
1067 					ereport(ERROR,
1068 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1069 							 errmsg("UNENCRYPTED PASSWORD is no longer supported"),
1070 							 errhint("Remove UNENCRYPTED to store the password in encrypted form instead."),
1071 							 parser_errposition(@1)));
1072 				}
1073 			| INHERIT
1074 				{
1075 					$$ = makeDefElem("inherit", (Node *)makeInteger(true), @1);
1076 				}
1077 			| CONNECTION LIMIT SignedIconst
1078 				{
1079 					$$ = makeDefElem("connectionlimit", (Node *)makeInteger($3), @1);
1080 				}
1081 			| VALID UNTIL Sconst
1082 				{
1083 					$$ = makeDefElem("validUntil", (Node *)makeString($3), @1);
1084 				}
1085 		/*	Supported but not documented for roles, for use by ALTER GROUP. */
1086 			| USER role_list
1087 				{
1088 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1089 				}
1090 			| IDENT
1091 				{
1092 					/*
1093 					 * We handle identifiers that aren't parser keywords with
1094 					 * the following special-case codes, to avoid bloating the
1095 					 * size of the main parser.
1096 					 */
1097 					if (strcmp($1, "superuser") == 0)
1098 						$$ = makeDefElem("superuser", (Node *)makeInteger(true), @1);
1099 					else if (strcmp($1, "nosuperuser") == 0)
1100 						$$ = makeDefElem("superuser", (Node *)makeInteger(false), @1);
1101 					else if (strcmp($1, "createrole") == 0)
1102 						$$ = makeDefElem("createrole", (Node *)makeInteger(true), @1);
1103 					else if (strcmp($1, "nocreaterole") == 0)
1104 						$$ = makeDefElem("createrole", (Node *)makeInteger(false), @1);
1105 					else if (strcmp($1, "replication") == 0)
1106 						$$ = makeDefElem("isreplication", (Node *)makeInteger(true), @1);
1107 					else if (strcmp($1, "noreplication") == 0)
1108 						$$ = makeDefElem("isreplication", (Node *)makeInteger(false), @1);
1109 					else if (strcmp($1, "createdb") == 0)
1110 						$$ = makeDefElem("createdb", (Node *)makeInteger(true), @1);
1111 					else if (strcmp($1, "nocreatedb") == 0)
1112 						$$ = makeDefElem("createdb", (Node *)makeInteger(false), @1);
1113 					else if (strcmp($1, "login") == 0)
1114 						$$ = makeDefElem("canlogin", (Node *)makeInteger(true), @1);
1115 					else if (strcmp($1, "nologin") == 0)
1116 						$$ = makeDefElem("canlogin", (Node *)makeInteger(false), @1);
1117 					else if (strcmp($1, "bypassrls") == 0)
1118 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(true), @1);
1119 					else if (strcmp($1, "nobypassrls") == 0)
1120 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(false), @1);
1121 					else if (strcmp($1, "noinherit") == 0)
1122 					{
1123 						/*
1124 						 * Note that INHERIT is a keyword, so it's handled by main parser, but
1125 						 * NOINHERIT is handled here.
1126 						 */
1127 						$$ = makeDefElem("inherit", (Node *)makeInteger(false), @1);
1128 					}
1129 					else
1130 						ereport(ERROR,
1131 								(errcode(ERRCODE_SYNTAX_ERROR),
1132 								 errmsg("unrecognized role option \"%s\"", $1),
1133 									 parser_errposition(@1)));
1134 				}
1135 		;
1136 
1137 CreateOptRoleElem:
1138 			AlterOptRoleElem			{ $$ = $1; }
1139 			/* The following are not supported by ALTER ROLE/USER/GROUP */
1140 			| SYSID Iconst
1141 				{
1142 					$$ = makeDefElem("sysid", (Node *)makeInteger($2), @1);
1143 				}
1144 			| ADMIN role_list
1145 				{
1146 					$$ = makeDefElem("adminmembers", (Node *)$2, @1);
1147 				}
1148 			| ROLE role_list
1149 				{
1150 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1151 				}
1152 			| IN_P ROLE role_list
1153 				{
1154 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1155 				}
1156 			| IN_P GROUP_P role_list
1157 				{
1158 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1159 				}
1160 		;
1161 
1162 
1163 /*****************************************************************************
1164  *
1165  * Create a new Postgres DBMS user (role with implied login ability)
1166  *
1167  *****************************************************************************/
1168 
1169 CreateUserStmt:
1170 			CREATE USER RoleId opt_with OptRoleList
1171 				{
1172 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1173 					n->stmt_type = ROLESTMT_USER;
1174 					n->role = $3;
1175 					n->options = $5;
1176 					$$ = (Node *)n;
1177 				}
1178 		;
1179 
1180 
1181 /*****************************************************************************
1182  *
1183  * Alter a postgresql DBMS role
1184  *
1185  *****************************************************************************/
1186 
1187 AlterRoleStmt:
1188 			ALTER ROLE RoleSpec opt_with AlterOptRoleList
1189 				 {
1190 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1191 					n->role = $3;
1192 					n->action = +1;	/* add, if there are members */
1193 					n->options = $5;
1194 					$$ = (Node *)n;
1195 				 }
1196 			| ALTER USER RoleSpec opt_with AlterOptRoleList
1197 				 {
1198 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1199 					n->role = $3;
1200 					n->action = +1;	/* add, if there are members */
1201 					n->options = $5;
1202 					$$ = (Node *)n;
1203 				 }
1204 		;
1205 
1206 opt_in_database:
1207 			   /* EMPTY */					{ $$ = NULL; }
1208 			| IN_P DATABASE database_name	{ $$ = $3; }
1209 		;
1210 
1211 AlterRoleSetStmt:
1212 			ALTER ROLE RoleSpec opt_in_database SetResetClause
1213 				{
1214 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1215 					n->role = $3;
1216 					n->database = $4;
1217 					n->setstmt = $5;
1218 					$$ = (Node *)n;
1219 				}
1220 			| ALTER ROLE ALL opt_in_database SetResetClause
1221 				{
1222 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1223 					n->role = NULL;
1224 					n->database = $4;
1225 					n->setstmt = $5;
1226 					$$ = (Node *)n;
1227 				}
1228 			| ALTER USER RoleSpec opt_in_database SetResetClause
1229 				{
1230 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1231 					n->role = $3;
1232 					n->database = $4;
1233 					n->setstmt = $5;
1234 					$$ = (Node *)n;
1235 				}
1236 			| ALTER USER ALL opt_in_database SetResetClause
1237 				{
1238 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1239 					n->role = NULL;
1240 					n->database = $4;
1241 					n->setstmt = $5;
1242 					$$ = (Node *)n;
1243 				}
1244 		;
1245 
1246 
1247 /*****************************************************************************
1248  *
1249  * Drop a postgresql DBMS role
1250  *
1251  * XXX Ideally this would have CASCADE/RESTRICT options, but a role
1252  * might own objects in multiple databases, and there is presently no way to
1253  * implement cascading to other databases.  So we always behave as RESTRICT.
1254  *****************************************************************************/
1255 
1256 DropRoleStmt:
1257 			DROP ROLE role_list
1258 				{
1259 					DropRoleStmt *n = makeNode(DropRoleStmt);
1260 					n->missing_ok = false;
1261 					n->roles = $3;
1262 					$$ = (Node *)n;
1263 				}
1264 			| DROP ROLE IF_P EXISTS role_list
1265 				{
1266 					DropRoleStmt *n = makeNode(DropRoleStmt);
1267 					n->missing_ok = true;
1268 					n->roles = $5;
1269 					$$ = (Node *)n;
1270 				}
1271 			| DROP USER role_list
1272 				{
1273 					DropRoleStmt *n = makeNode(DropRoleStmt);
1274 					n->missing_ok = false;
1275 					n->roles = $3;
1276 					$$ = (Node *)n;
1277 				}
1278 			| DROP USER IF_P EXISTS role_list
1279 				{
1280 					DropRoleStmt *n = makeNode(DropRoleStmt);
1281 					n->roles = $5;
1282 					n->missing_ok = true;
1283 					$$ = (Node *)n;
1284 				}
1285 			| DROP GROUP_P role_list
1286 				{
1287 					DropRoleStmt *n = makeNode(DropRoleStmt);
1288 					n->missing_ok = false;
1289 					n->roles = $3;
1290 					$$ = (Node *)n;
1291 				}
1292 			| DROP GROUP_P IF_P EXISTS role_list
1293 				{
1294 					DropRoleStmt *n = makeNode(DropRoleStmt);
1295 					n->missing_ok = true;
1296 					n->roles = $5;
1297 					$$ = (Node *)n;
1298 				}
1299 			;
1300 
1301 
1302 /*****************************************************************************
1303  *
1304  * Create a postgresql group (role without login ability)
1305  *
1306  *****************************************************************************/
1307 
1308 CreateGroupStmt:
1309 			CREATE GROUP_P RoleId opt_with OptRoleList
1310 				{
1311 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1312 					n->stmt_type = ROLESTMT_GROUP;
1313 					n->role = $3;
1314 					n->options = $5;
1315 					$$ = (Node *)n;
1316 				}
1317 		;
1318 
1319 
1320 /*****************************************************************************
1321  *
1322  * Alter a postgresql group
1323  *
1324  *****************************************************************************/
1325 
1326 AlterGroupStmt:
1327 			ALTER GROUP_P RoleSpec add_drop USER role_list
1328 				{
1329 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1330 					n->role = $3;
1331 					n->action = $4;
1332 					n->options = list_make1(makeDefElem("rolemembers",
1333 														(Node *)$6, @6));
1334 					$$ = (Node *)n;
1335 				}
1336 		;
1337 
1338 add_drop:	ADD_P									{ $$ = +1; }
1339 			| DROP									{ $$ = -1; }
1340 		;
1341 
1342 
1343 /*****************************************************************************
1344  *
1345  * Manipulate a schema
1346  *
1347  *****************************************************************************/
1348 
1349 CreateSchemaStmt:
1350 			CREATE SCHEMA OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1351 				{
1352 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1353 					/* One can omit the schema name or the authorization id. */
1354 					n->schemaname = $3;
1355 					n->authrole = $5;
1356 					n->schemaElts = $6;
1357 					n->if_not_exists = false;
1358 					$$ = (Node *)n;
1359 				}
1360 			| CREATE SCHEMA ColId OptSchemaEltList
1361 				{
1362 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1363 					/* ...but not both */
1364 					n->schemaname = $3;
1365 					n->authrole = NULL;
1366 					n->schemaElts = $4;
1367 					n->if_not_exists = false;
1368 					$$ = (Node *)n;
1369 				}
1370 			| CREATE SCHEMA IF_P NOT EXISTS OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1371 				{
1372 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1373 					/* schema name can be omitted here, too */
1374 					n->schemaname = $6;
1375 					n->authrole = $8;
1376 					if ($9 != NIL)
1377 						ereport(ERROR,
1378 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1379 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1380 								 parser_errposition(@9)));
1381 					n->schemaElts = $9;
1382 					n->if_not_exists = true;
1383 					$$ = (Node *)n;
1384 				}
1385 			| CREATE SCHEMA IF_P NOT EXISTS ColId OptSchemaEltList
1386 				{
1387 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1388 					/* ...but not here */
1389 					n->schemaname = $6;
1390 					n->authrole = NULL;
1391 					if ($7 != NIL)
1392 						ereport(ERROR,
1393 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1394 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1395 								 parser_errposition(@7)));
1396 					n->schemaElts = $7;
1397 					n->if_not_exists = true;
1398 					$$ = (Node *)n;
1399 				}
1400 		;
1401 
1402 OptSchemaName:
1403 			ColId									{ $$ = $1; }
1404 			| /* EMPTY */							{ $$ = NULL; }
1405 		;
1406 
1407 OptSchemaEltList:
1408 			OptSchemaEltList schema_stmt
1409 				{
1410 					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
1411 						@$ = @2;
1412 					$$ = lappend($1, $2);
1413 				}
1414 			| /* EMPTY */
1415 				{ $$ = NIL; }
1416 		;
1417 
1418 /*
1419  *	schema_stmt are the ones that can show up inside a CREATE SCHEMA
1420  *	statement (in addition to by themselves).
1421  */
1422 schema_stmt:
1423 			CreateStmt
1424 			| IndexStmt
1425 			| CreateSeqStmt
1426 			| CreateTrigStmt
1427 			| GrantStmt
1428 			| ViewStmt
1429 		;
1430 
1431 
1432 /*****************************************************************************
1433  *
1434  * Set PG internal variable
1435  *	  SET name TO 'var_value'
1436  * Include SQL syntax (thomas 1997-10-22):
1437  *	  SET TIME ZONE 'var_value'
1438  *
1439  *****************************************************************************/
1440 
1441 VariableSetStmt:
1442 			PGPOOL SET generic_set
1443 				{
1444 					VariableSetStmt *n = $3;
1445 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep changes minumum */
1446 					n->is_local = false;
1447 					$$ = (Node *) n;
1448 				}
1449 			| SET set_rest
1450 				{
1451 					VariableSetStmt *n = $2;
1452 					n->is_local = false;
1453 					$$ = (Node *) n;
1454 				}
1455 			| SET LOCAL set_rest
1456 				{
1457 					VariableSetStmt *n = $3;
1458 					n->is_local = true;
1459 					$$ = (Node *) n;
1460 				}
1461 			| SET SESSION set_rest
1462 				{
1463 					VariableSetStmt *n = $3;
1464 					n->is_local = false;
1465 					$$ = (Node *) n;
1466 				}
1467 		;
1468 
1469 set_rest:
1470 			TRANSACTION transaction_mode_list
1471 				{
1472 					VariableSetStmt *n = makeNode(VariableSetStmt);
1473 					n->kind = VAR_SET_MULTI;
1474 					n->name = "TRANSACTION";
1475 					n->args = $2;
1476 					$$ = n;
1477 				}
1478 			| SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1479 				{
1480 					VariableSetStmt *n = makeNode(VariableSetStmt);
1481 					n->kind = VAR_SET_MULTI;
1482 					n->name = "SESSION CHARACTERISTICS";
1483 					n->args = $5;
1484 					$$ = n;
1485 				}
1486 			| set_rest_more
1487 			;
1488 
1489 generic_set:
1490 			var_name TO var_list
1491 				{
1492 					VariableSetStmt *n = makeNode(VariableSetStmt);
1493 					n->kind = VAR_SET_VALUE;
1494 					n->name = $1;
1495 					n->args = $3;
1496 					$$ = n;
1497 				}
1498 			| var_name '=' var_list
1499 				{
1500 					VariableSetStmt *n = makeNode(VariableSetStmt);
1501 					n->kind = VAR_SET_VALUE;
1502 					n->name = $1;
1503 					n->args = $3;
1504 					$$ = n;
1505 				}
1506 			| var_name TO DEFAULT
1507 				{
1508 					VariableSetStmt *n = makeNode(VariableSetStmt);
1509 					n->kind = VAR_SET_DEFAULT;
1510 					n->name = $1;
1511 					$$ = n;
1512 				}
1513 			| var_name '=' DEFAULT
1514 				{
1515 					VariableSetStmt *n = makeNode(VariableSetStmt);
1516 					n->kind = VAR_SET_DEFAULT;
1517 					n->name = $1;
1518 					$$ = n;
1519 				}
1520 		;
1521 
1522 set_rest_more:	/* Generic SET syntaxes: */
1523 			generic_set 						{$$ = $1;}
1524 			| var_name FROM CURRENT_P
1525 				{
1526 					VariableSetStmt *n = makeNode(VariableSetStmt);
1527 					n->kind = VAR_SET_CURRENT;
1528 					n->name = $1;
1529 					$$ = n;
1530 				}
1531 			/* Special syntaxes mandated by SQL standard: */
1532 			| TIME ZONE zone_value
1533 				{
1534 					VariableSetStmt *n = makeNode(VariableSetStmt);
1535 					n->kind = VAR_SET_VALUE;
1536 					n->name = "timezone";
1537 					if ($3 != NULL)
1538 						n->args = list_make1($3);
1539 					else
1540 						n->kind = VAR_SET_DEFAULT;
1541 					$$ = n;
1542 				}
1543 			| CATALOG_P Sconst
1544 				{
1545 					ereport(ERROR,
1546 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1547 							 errmsg("current database cannot be changed"),
1548 							 parser_errposition(@2)));
1549 					$$ = NULL; /*not reached*/
1550 				}
1551 			| SCHEMA Sconst
1552 				{
1553 					VariableSetStmt *n = makeNode(VariableSetStmt);
1554 					n->kind = VAR_SET_VALUE;
1555 					n->name = "search_path";
1556 					n->args = list_make1(makeStringConst($2, @2));
1557 					$$ = n;
1558 				}
1559 			| NAMES opt_encoding
1560 				{
1561 					VariableSetStmt *n = makeNode(VariableSetStmt);
1562 					n->kind = VAR_SET_VALUE;
1563 					n->name = "client_encoding";
1564 					if ($2 != NULL)
1565 						n->args = list_make1(makeStringConst($2, @2));
1566 					else
1567 						n->kind = VAR_SET_DEFAULT;
1568 					$$ = n;
1569 				}
1570 			| ROLE NonReservedWord_or_Sconst
1571 				{
1572 					VariableSetStmt *n = makeNode(VariableSetStmt);
1573 					n->kind = VAR_SET_VALUE;
1574 					n->name = "role";
1575 					n->args = list_make1(makeStringConst($2, @2));
1576 					$$ = n;
1577 				}
1578 			| SESSION AUTHORIZATION NonReservedWord_or_Sconst
1579 				{
1580 					VariableSetStmt *n = makeNode(VariableSetStmt);
1581 					n->kind = VAR_SET_VALUE;
1582 					n->name = "session_authorization";
1583 					n->args = list_make1(makeStringConst($3, @3));
1584 					$$ = n;
1585 				}
1586 			| SESSION AUTHORIZATION DEFAULT
1587 				{
1588 					VariableSetStmt *n = makeNode(VariableSetStmt);
1589 					n->kind = VAR_SET_DEFAULT;
1590 					n->name = "session_authorization";
1591 					$$ = n;
1592 				}
1593 			| XML_P OPTION document_or_content
1594 				{
1595 					VariableSetStmt *n = makeNode(VariableSetStmt);
1596 					n->kind = VAR_SET_VALUE;
1597 					n->name = "xmloption";
1598 					n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1599 					$$ = n;
1600 				}
1601 			/* Special syntaxes invented by PostgreSQL: */
1602 			| TRANSACTION SNAPSHOT Sconst
1603 				{
1604 					VariableSetStmt *n = makeNode(VariableSetStmt);
1605 					n->kind = VAR_SET_MULTI;
1606 					n->name = "TRANSACTION SNAPSHOT";
1607 					n->args = list_make1(makeStringConst($3, @3));
1608 					$$ = n;
1609 				}
1610 		;
1611 
1612 var_name:	ColId								{ $$ = $1; }
1613 			| var_name '.' ColId
1614 				{ $$ = psprintf("%s.%s", $1, $3); }
1615 		;
1616 
1617 var_list:	var_value								{ $$ = list_make1($1); }
1618 			| var_list ',' var_value				{ $$ = lappend($1, $3); }
1619 		;
1620 
1621 var_value:	opt_boolean_or_string
1622 				{ $$ = makeStringConst($1, @1); }
1623 			| NumericOnly
1624 				{ $$ = makeAConst($1, @1); }
1625 		;
1626 
1627 iso_level:	READ UNCOMMITTED						{ $$ = "read uncommitted"; }
1628 			| READ COMMITTED						{ $$ = "read committed"; }
1629 			| REPEATABLE READ						{ $$ = "repeatable read"; }
1630 			| SERIALIZABLE							{ $$ = "serializable"; }
1631 		;
1632 
1633 opt_boolean_or_string:
1634 			TRUE_P									{ $$ = "true"; }
1635 			| FALSE_P								{ $$ = "false"; }
1636 			| ON									{ $$ = "on"; }
1637 			/*
1638 			 * OFF is also accepted as a boolean value, but is handled by
1639 			 * the NonReservedWord rule.  The action for booleans and strings
1640 			 * is the same, so we don't need to distinguish them here.
1641 			 */
1642 			| NonReservedWord_or_Sconst				{ $$ = $1; }
1643 		;
1644 
1645 /* Timezone values can be:
1646  * - a string such as 'pst8pdt'
1647  * - an identifier such as "pst8pdt"
1648  * - an integer or floating point number
1649  * - a time interval per SQL99
1650  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1651  * so use IDENT (meaning we reject anything that is a key word).
1652  */
1653 zone_value:
1654 			Sconst
1655 				{
1656 					$$ = makeStringConst($1, @1);
1657 				}
1658 			| IDENT
1659 				{
1660 					$$ = makeStringConst($1, @1);
1661 				}
1662 			| ConstInterval Sconst opt_interval
1663 				{
1664 					TypeName *t = $1;
1665 					if ($3 != NIL)
1666 					{
1667 						A_Const *n = (A_Const *) linitial($3);
1668 						if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1669 							ereport(ERROR,
1670 									(errcode(ERRCODE_SYNTAX_ERROR),
1671 									 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1672 									 parser_errposition(@3)));
1673 					}
1674 					t->typmods = $3;
1675 					$$ = makeStringConstCast($2, @2, t);
1676 				}
1677 			| ConstInterval '(' Iconst ')' Sconst
1678 				{
1679 					TypeName *t = $1;
1680 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1681 											makeIntConst($3, @3));
1682 					$$ = makeStringConstCast($5, @5, t);
1683 				}
1684 			| NumericOnly							{ $$ = makeAConst($1, @1); }
1685 			| DEFAULT								{ $$ = NULL; }
1686 			| LOCAL									{ $$ = NULL; }
1687 		;
1688 
1689 opt_encoding:
1690 			Sconst									{ $$ = $1; }
1691 			| DEFAULT								{ $$ = NULL; }
1692 			| /*EMPTY*/								{ $$ = NULL; }
1693 		;
1694 
1695 NonReservedWord_or_Sconst:
1696 			NonReservedWord							{ $$ = $1; }
1697 			| Sconst								{ $$ = $1; }
1698 		;
1699 
1700 VariableResetStmt:
1701 			RESET reset_rest						{ $$ = (Node *) $2; }
1702 			| PGPOOL RESET generic_reset
1703 				{
1704 					VariableSetStmt *n = $3;
1705 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep the changes minumum */
1706 					$$ = (Node *) n;
1707 				}
1708 		;
1709 
1710 reset_rest:
1711 			generic_reset							{ $$ = $1; }
1712 			| TIME ZONE
1713 				{
1714 					VariableSetStmt *n = makeNode(VariableSetStmt);
1715 					n->kind = VAR_RESET;
1716 					n->name = "timezone";
1717 					$$ = n;
1718 				}
1719 			| TRANSACTION ISOLATION LEVEL
1720 				{
1721 					VariableSetStmt *n = makeNode(VariableSetStmt);
1722 					n->kind = VAR_RESET;
1723 					n->name = "transaction_isolation";
1724 					$$ = n;
1725 				}
1726 			| SESSION AUTHORIZATION
1727 				{
1728 					VariableSetStmt *n = makeNode(VariableSetStmt);
1729 					n->kind = VAR_RESET;
1730 					n->name = "session_authorization";
1731 					$$ = n;
1732 				}
1733 		;
1734 
1735 generic_reset:
1736 			var_name
1737 				{
1738 					VariableSetStmt *n = makeNode(VariableSetStmt);
1739 					n->kind = VAR_RESET;
1740 					n->name = $1;
1741 					$$ = n;
1742 				}
1743 			| ALL
1744 				{
1745 					VariableSetStmt *n = makeNode(VariableSetStmt);
1746 					n->kind = VAR_RESET_ALL;
1747 					$$ = n;
1748 				}
1749 		;
1750 
1751 /* SetResetClause allows SET or RESET without LOCAL */
1752 SetResetClause:
1753 			SET set_rest					{ $$ = $2; }
1754 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1755 		;
1756 
1757 /* SetResetClause allows SET or RESET without LOCAL */
1758 FunctionSetResetClause:
1759 			SET set_rest_more				{ $$ = $2; }
1760 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1761 		;
1762 
1763 
1764 VariableShowStmt:
1765 			/* pgpool extension */
1766 			PGPOOL SHOW var_name
1767 			{
1768 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1769 				n->name = $3;
1770 				$$ = (Node *) n;
1771 			}
1772 			| PGPOOL SHOW ALL
1773 			{
1774 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1775 				n->name = "all";
1776 				$$ = (Node *) n;
1777 			}
1778 			| SHOW var_name
1779 				{
1780 					VariableShowStmt *n = makeNode(VariableShowStmt);
1781 					n->name = $2;
1782 					$$ = (Node *) n;
1783 				}
1784 			| SHOW TIME ZONE
1785 				{
1786 					VariableShowStmt *n = makeNode(VariableShowStmt);
1787 					n->name = "timezone";
1788 					$$ = (Node *) n;
1789 				}
1790 			| SHOW TRANSACTION ISOLATION LEVEL
1791 				{
1792 					VariableShowStmt *n = makeNode(VariableShowStmt);
1793 					n->name = "transaction_isolation";
1794 					$$ = (Node *) n;
1795 				}
1796 			| SHOW SESSION AUTHORIZATION
1797 				{
1798 					VariableShowStmt *n = makeNode(VariableShowStmt);
1799 					n->name = "session_authorization";
1800 					$$ = (Node *) n;
1801 				}
1802 			| SHOW ALL
1803 				{
1804 					VariableShowStmt *n = makeNode(VariableShowStmt);
1805 					n->name = "all";
1806 					$$ = (Node *) n;
1807 				}
1808 		;
1809 
1810 
1811 ConstraintsSetStmt:
1812 			SET CONSTRAINTS constraints_set_list constraints_set_mode
1813 				{
1814 					ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1815 					n->constraints = $3;
1816 					n->deferred = $4;
1817 					$$ = (Node *) n;
1818 				}
1819 		;
1820 
1821 constraints_set_list:
1822 			ALL										{ $$ = NIL; }
1823 			| qualified_name_list					{ $$ = $1; }
1824 		;
1825 
1826 constraints_set_mode:
1827 			DEFERRED								{ $$ = true; }
1828 			| IMMEDIATE								{ $$ = false; }
1829 		;
1830 
1831 
1832 /*
1833  * Checkpoint statement
1834  */
1835 CheckPointStmt:
1836 			CHECKPOINT
1837 				{
1838 					CheckPointStmt *n = makeNode(CheckPointStmt);
1839 					$$ = (Node *)n;
1840 				}
1841 		;
1842 
1843 
1844 /*****************************************************************************
1845  *
1846  * DISCARD { ALL | TEMP | PLANS | SEQUENCES }
1847  *
1848  *****************************************************************************/
1849 
1850 DiscardStmt:
1851 			DISCARD ALL
1852 				{
1853 					DiscardStmt *n = makeNode(DiscardStmt);
1854 					n->target = DISCARD_ALL;
1855 					$$ = (Node *) n;
1856 				}
1857 			| DISCARD TEMP
1858 				{
1859 					DiscardStmt *n = makeNode(DiscardStmt);
1860 					n->target = DISCARD_TEMP;
1861 					$$ = (Node *) n;
1862 				}
1863 			| DISCARD TEMPORARY
1864 				{
1865 					DiscardStmt *n = makeNode(DiscardStmt);
1866 					n->target = DISCARD_TEMP;
1867 					$$ = (Node *) n;
1868 				}
1869 			| DISCARD PLANS
1870 				{
1871 					DiscardStmt *n = makeNode(DiscardStmt);
1872 					n->target = DISCARD_PLANS;
1873 					$$ = (Node *) n;
1874 				}
1875 			| DISCARD SEQUENCES
1876 				{
1877 					DiscardStmt *n = makeNode(DiscardStmt);
1878 					n->target = DISCARD_SEQUENCES;
1879 					$$ = (Node *) n;
1880 				}
1881 
1882 		;
1883 
1884 
1885 /*****************************************************************************
1886  *
1887  *	ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW ] variations
1888  *
1889  * Note: we accept all subcommands for each of the five variants, and sort
1890  * out what's really legal at execution time.
1891  *****************************************************************************/
1892 
1893 AlterTableStmt:
1894 			ALTER TABLE relation_expr alter_table_cmds
1895 				{
1896 					AlterTableStmt *n = makeNode(AlterTableStmt);
1897 					n->relation = $3;
1898 					n->cmds = $4;
1899 					n->relkind = OBJECT_TABLE;
1900 					n->missing_ok = false;
1901 					$$ = (Node *)n;
1902 				}
1903 		|	ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
1904 				{
1905 					AlterTableStmt *n = makeNode(AlterTableStmt);
1906 					n->relation = $5;
1907 					n->cmds = $6;
1908 					n->relkind = OBJECT_TABLE;
1909 					n->missing_ok = true;
1910 					$$ = (Node *)n;
1911 				}
1912 		|	ALTER TABLE relation_expr partition_cmd
1913 				{
1914 					AlterTableStmt *n = makeNode(AlterTableStmt);
1915 					n->relation = $3;
1916 					n->cmds = list_make1($4);
1917 					n->relkind = OBJECT_TABLE;
1918 					n->missing_ok = false;
1919 					$$ = (Node *)n;
1920 				}
1921 		|	ALTER TABLE IF_P EXISTS relation_expr partition_cmd
1922 				{
1923 					AlterTableStmt *n = makeNode(AlterTableStmt);
1924 					n->relation = $5;
1925 					n->cmds = list_make1($6);
1926 					n->relkind = OBJECT_TABLE;
1927 					n->missing_ok = true;
1928 					$$ = (Node *)n;
1929 				}
1930 		|	ALTER TABLE ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1931 				{
1932 					AlterTableMoveAllStmt *n =
1933 						makeNode(AlterTableMoveAllStmt);
1934 					n->orig_tablespacename = $6;
1935 					n->objtype = OBJECT_TABLE;
1936 					n->roles = NIL;
1937 					n->new_tablespacename = $9;
1938 					n->nowait = $10;
1939 					$$ = (Node *)n;
1940 				}
1941 		|	ALTER TABLE ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1942 				{
1943 					AlterTableMoveAllStmt *n =
1944 						makeNode(AlterTableMoveAllStmt);
1945 					n->orig_tablespacename = $6;
1946 					n->objtype = OBJECT_TABLE;
1947 					n->roles = $9;
1948 					n->new_tablespacename = $12;
1949 					n->nowait = $13;
1950 					$$ = (Node *)n;
1951 				}
1952 		|	ALTER INDEX qualified_name alter_table_cmds
1953 				{
1954 					AlterTableStmt *n = makeNode(AlterTableStmt);
1955 					n->relation = $3;
1956 					n->cmds = $4;
1957 					n->relkind = OBJECT_INDEX;
1958 					n->missing_ok = false;
1959 					$$ = (Node *)n;
1960 				}
1961 		|	ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
1962 				{
1963 					AlterTableStmt *n = makeNode(AlterTableStmt);
1964 					n->relation = $5;
1965 					n->cmds = $6;
1966 					n->relkind = OBJECT_INDEX;
1967 					n->missing_ok = true;
1968 					$$ = (Node *)n;
1969 				}
1970 		|	ALTER INDEX qualified_name index_partition_cmd
1971 				{
1972 					AlterTableStmt *n = makeNode(AlterTableStmt);
1973 					n->relation = $3;
1974 					n->cmds = list_make1($4);
1975 					n->relkind = OBJECT_INDEX;
1976 					n->missing_ok = false;
1977 					$$ = (Node *)n;
1978 				}
1979 		|	ALTER INDEX ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1980 				{
1981 					AlterTableMoveAllStmt *n =
1982 						makeNode(AlterTableMoveAllStmt);
1983 					n->orig_tablespacename = $6;
1984 					n->objtype = OBJECT_INDEX;
1985 					n->roles = NIL;
1986 					n->new_tablespacename = $9;
1987 					n->nowait = $10;
1988 					$$ = (Node *)n;
1989 				}
1990 		|	ALTER INDEX ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1991 				{
1992 					AlterTableMoveAllStmt *n =
1993 						makeNode(AlterTableMoveAllStmt);
1994 					n->orig_tablespacename = $6;
1995 					n->objtype = OBJECT_INDEX;
1996 					n->roles = $9;
1997 					n->new_tablespacename = $12;
1998 					n->nowait = $13;
1999 					$$ = (Node *)n;
2000 				}
2001 		|	ALTER SEQUENCE qualified_name alter_table_cmds
2002 				{
2003 					AlterTableStmt *n = makeNode(AlterTableStmt);
2004 					n->relation = $3;
2005 					n->cmds = $4;
2006 					n->relkind = OBJECT_SEQUENCE;
2007 					n->missing_ok = false;
2008 					$$ = (Node *)n;
2009 				}
2010 		|	ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
2011 				{
2012 					AlterTableStmt *n = makeNode(AlterTableStmt);
2013 					n->relation = $5;
2014 					n->cmds = $6;
2015 					n->relkind = OBJECT_SEQUENCE;
2016 					n->missing_ok = true;
2017 					$$ = (Node *)n;
2018 				}
2019 		|	ALTER VIEW qualified_name alter_table_cmds
2020 				{
2021 					AlterTableStmt *n = makeNode(AlterTableStmt);
2022 					n->relation = $3;
2023 					n->cmds = $4;
2024 					n->relkind = OBJECT_VIEW;
2025 					n->missing_ok = false;
2026 					$$ = (Node *)n;
2027 				}
2028 		|	ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
2029 				{
2030 					AlterTableStmt *n = makeNode(AlterTableStmt);
2031 					n->relation = $5;
2032 					n->cmds = $6;
2033 					n->relkind = OBJECT_VIEW;
2034 					n->missing_ok = true;
2035 					$$ = (Node *)n;
2036 				}
2037 		|	ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
2038 				{
2039 					AlterTableStmt *n = makeNode(AlterTableStmt);
2040 					n->relation = $4;
2041 					n->cmds = $5;
2042 					n->relkind = OBJECT_MATVIEW;
2043 					n->missing_ok = false;
2044 					$$ = (Node *)n;
2045 				}
2046 		|	ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
2047 				{
2048 					AlterTableStmt *n = makeNode(AlterTableStmt);
2049 					n->relation = $6;
2050 					n->cmds = $7;
2051 					n->relkind = OBJECT_MATVIEW;
2052 					n->missing_ok = true;
2053 					$$ = (Node *)n;
2054 				}
2055 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
2056 				{
2057 					AlterTableMoveAllStmt *n =
2058 						makeNode(AlterTableMoveAllStmt);
2059 					n->orig_tablespacename = $7;
2060 					n->objtype = OBJECT_MATVIEW;
2061 					n->roles = NIL;
2062 					n->new_tablespacename = $10;
2063 					n->nowait = $11;
2064 					$$ = (Node *)n;
2065 				}
2066 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
2067 				{
2068 					AlterTableMoveAllStmt *n =
2069 						makeNode(AlterTableMoveAllStmt);
2070 					n->orig_tablespacename = $7;
2071 					n->objtype = OBJECT_MATVIEW;
2072 					n->roles = $10;
2073 					n->new_tablespacename = $13;
2074 					n->nowait = $14;
2075 					$$ = (Node *)n;
2076 				}
2077 		;
2078 
2079 alter_table_cmds:
2080 			alter_table_cmd							{ $$ = list_make1($1); }
2081 			| alter_table_cmds ',' alter_table_cmd	{ $$ = lappend($1, $3); }
2082 		;
2083 
2084 partition_cmd:
2085 			/* ALTER TABLE <name> ATTACH PARTITION <table_name> FOR VALUES */
2086 			ATTACH PARTITION qualified_name PartitionBoundSpec
2087 				{
2088 					AlterTableCmd *n = makeNode(AlterTableCmd);
2089 					PartitionCmd *cmd = makeNode(PartitionCmd);
2090 
2091 					n->subtype = AT_AttachPartition;
2092 					cmd->name = $3;
2093 					cmd->bound = $4;
2094 					n->def = (Node *) cmd;
2095 
2096 					$$ = (Node *) n;
2097 				}
2098 			/* ALTER TABLE <name> DETACH PARTITION <partition_name> */
2099 			| DETACH PARTITION qualified_name
2100 				{
2101 					AlterTableCmd *n = makeNode(AlterTableCmd);
2102 					PartitionCmd *cmd = makeNode(PartitionCmd);
2103 
2104 					n->subtype = AT_DetachPartition;
2105 					cmd->name = $3;
2106 					cmd->bound = NULL;
2107 					n->def = (Node *) cmd;
2108 
2109 					$$ = (Node *) n;
2110 				}
2111 		;
2112 
2113 index_partition_cmd:
2114 			/* ALTER INDEX <name> ATTACH PARTITION <index_name> */
2115 			ATTACH PARTITION qualified_name
2116 				{
2117 					AlterTableCmd *n = makeNode(AlterTableCmd);
2118 					PartitionCmd *cmd = makeNode(PartitionCmd);
2119 
2120 					n->subtype = AT_AttachPartition;
2121 					cmd->name = $3;
2122 					cmd->bound = NULL;
2123 					n->def = (Node *) cmd;
2124 
2125 					$$ = (Node *) n;
2126 				}
2127 		;
2128 
2129 alter_table_cmd:
2130 			/* ALTER TABLE <name> ADD <coldef> */
2131 			ADD_P columnDef
2132 				{
2133 					AlterTableCmd *n = makeNode(AlterTableCmd);
2134 					n->subtype = AT_AddColumn;
2135 					n->def = $2;
2136 					n->missing_ok = false;
2137 					$$ = (Node *)n;
2138 				}
2139 			/* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
2140 			| ADD_P IF_P NOT EXISTS columnDef
2141 				{
2142 					AlterTableCmd *n = makeNode(AlterTableCmd);
2143 					n->subtype = AT_AddColumn;
2144 					n->def = $5;
2145 					n->missing_ok = true;
2146 					$$ = (Node *)n;
2147 				}
2148 			/* ALTER TABLE <name> ADD COLUMN <coldef> */
2149 			| ADD_P COLUMN columnDef
2150 				{
2151 					AlterTableCmd *n = makeNode(AlterTableCmd);
2152 					n->subtype = AT_AddColumn;
2153 					n->def = $3;
2154 					n->missing_ok = false;
2155 					$$ = (Node *)n;
2156 				}
2157 			/* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
2158 			| ADD_P COLUMN IF_P NOT EXISTS columnDef
2159 				{
2160 					AlterTableCmd *n = makeNode(AlterTableCmd);
2161 					n->subtype = AT_AddColumn;
2162 					n->def = $6;
2163 					n->missing_ok = true;
2164 					$$ = (Node *)n;
2165 				}
2166 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
2167 			| ALTER opt_column ColId alter_column_default
2168 				{
2169 					AlterTableCmd *n = makeNode(AlterTableCmd);
2170 					n->subtype = AT_ColumnDefault;
2171 					n->name = $3;
2172 					n->def = $4;
2173 					$$ = (Node *)n;
2174 				}
2175 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
2176 			| ALTER opt_column ColId DROP NOT NULL_P
2177 				{
2178 					AlterTableCmd *n = makeNode(AlterTableCmd);
2179 					n->subtype = AT_DropNotNull;
2180 					n->name = $3;
2181 					$$ = (Node *)n;
2182 				}
2183 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
2184 			| ALTER opt_column ColId SET NOT NULL_P
2185 				{
2186 					AlterTableCmd *n = makeNode(AlterTableCmd);
2187 					n->subtype = AT_SetNotNull;
2188 					n->name = $3;
2189 					$$ = (Node *)n;
2190 				}
2191 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
2192 			| ALTER opt_column ColId SET STATISTICS SignedIconst
2193 				{
2194 					AlterTableCmd *n = makeNode(AlterTableCmd);
2195 					n->subtype = AT_SetStatistics;
2196 					n->name = $3;
2197 					n->def = (Node *) makeInteger($6);
2198 					$$ = (Node *)n;
2199 				}
2200 			/* ALTER TABLE <name> ALTER [COLUMN] <colnum> SET STATISTICS <SignedIconst> */
2201 			| ALTER opt_column Iconst SET STATISTICS SignedIconst
2202 				{
2203 					AlterTableCmd *n = makeNode(AlterTableCmd);
2204 
2205 					if ($3 <= 0 || $3 > PG_INT16_MAX)
2206 						ereport(ERROR,
2207 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2208 								 errmsg("column number must be in range from 1 to %d", PG_INT16_MAX),
2209 								 parser_errposition(@3)));
2210 
2211 					n->subtype = AT_SetStatistics;
2212 					n->num = (int16) $3;
2213 					n->def = (Node *) makeInteger($6);
2214 					$$ = (Node *)n;
2215 				}
2216 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
2217 			| ALTER opt_column ColId SET reloptions
2218 				{
2219 					AlterTableCmd *n = makeNode(AlterTableCmd);
2220 					n->subtype = AT_SetOptions;
2221 					n->name = $3;
2222 					n->def = (Node *) $5;
2223 					$$ = (Node *)n;
2224 				}
2225 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
2226 			| ALTER opt_column ColId RESET reloptions
2227 				{
2228 					AlterTableCmd *n = makeNode(AlterTableCmd);
2229 					n->subtype = AT_ResetOptions;
2230 					n->name = $3;
2231 					n->def = (Node *) $5;
2232 					$$ = (Node *)n;
2233 				}
2234 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
2235 			| ALTER opt_column ColId SET STORAGE ColId
2236 				{
2237 					AlterTableCmd *n = makeNode(AlterTableCmd);
2238 					n->subtype = AT_SetStorage;
2239 					n->name = $3;
2240 					n->def = (Node *) makeString($6);
2241 					$$ = (Node *)n;
2242 				}
2243 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> ADD GENERATED ... AS IDENTITY ... */
2244 			| ALTER opt_column ColId ADD_P GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
2245 				{
2246 					AlterTableCmd *n = makeNode(AlterTableCmd);
2247 					Constraint *c = makeNode(Constraint);
2248 
2249 					c->contype = CONSTR_IDENTITY;
2250 					c->generated_when = $6;
2251 					c->options = $9;
2252 					c->location = @5;
2253 
2254 					n->subtype = AT_AddIdentity;
2255 					n->name = $3;
2256 					n->def = (Node *) c;
2257 
2258 					$$ = (Node *)n;
2259 				}
2260 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET <sequence options>/RESET */
2261 			| ALTER opt_column ColId alter_identity_column_option_list
2262 				{
2263 					AlterTableCmd *n = makeNode(AlterTableCmd);
2264 					n->subtype = AT_SetIdentity;
2265 					n->name = $3;
2266 					n->def = (Node *) $4;
2267 					$$ = (Node *)n;
2268 				}
2269 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY */
2270 			| ALTER opt_column ColId DROP IDENTITY_P
2271 				{
2272 					AlterTableCmd *n = makeNode(AlterTableCmd);
2273 					n->subtype = AT_DropIdentity;
2274 					n->name = $3;
2275 					n->missing_ok = false;
2276 					$$ = (Node *)n;
2277 				}
2278 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY IF EXISTS */
2279 			| ALTER opt_column ColId DROP IDENTITY_P IF_P EXISTS
2280 				{
2281 					AlterTableCmd *n = makeNode(AlterTableCmd);
2282 					n->subtype = AT_DropIdentity;
2283 					n->name = $3;
2284 					n->missing_ok = true;
2285 					$$ = (Node *)n;
2286 				}
2287 			/* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
2288 			| DROP opt_column IF_P EXISTS ColId opt_drop_behavior
2289 				{
2290 					AlterTableCmd *n = makeNode(AlterTableCmd);
2291 					n->subtype = AT_DropColumn;
2292 					n->name = $5;
2293 					n->behavior = $6;
2294 					n->missing_ok = true;
2295 					$$ = (Node *)n;
2296 				}
2297 			/* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
2298 			| DROP opt_column ColId opt_drop_behavior
2299 				{
2300 					AlterTableCmd *n = makeNode(AlterTableCmd);
2301 					n->subtype = AT_DropColumn;
2302 					n->name = $3;
2303 					n->behavior = $4;
2304 					n->missing_ok = false;
2305 					$$ = (Node *)n;
2306 				}
2307 			/*
2308 			 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
2309 			 *		[ USING <expression> ]
2310 			 */
2311 			| ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
2312 				{
2313 					AlterTableCmd *n = makeNode(AlterTableCmd);
2314 					ColumnDef *def = makeNode(ColumnDef);
2315 					n->subtype = AT_AlterColumnType;
2316 					n->name = $3;
2317 					n->def = (Node *) def;
2318 					/* We only use these fields of the ColumnDef node */
2319 					def->typeName = $6;
2320 					def->collClause = (CollateClause *) $7;
2321 					def->raw_default = $8;
2322 					def->location = @3;
2323 					$$ = (Node *)n;
2324 				}
2325 			/* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
2326 			| ALTER opt_column ColId alter_generic_options
2327 				{
2328 					AlterTableCmd *n = makeNode(AlterTableCmd);
2329 					n->subtype = AT_AlterColumnGenericOptions;
2330 					n->name = $3;
2331 					n->def = (Node *) $4;
2332 					$$ = (Node *)n;
2333 				}
2334 			/* ALTER TABLE <name> ADD CONSTRAINT ... */
2335 			| ADD_P TableConstraint
2336 				{
2337 					AlterTableCmd *n = makeNode(AlterTableCmd);
2338 					n->subtype = AT_AddConstraint;
2339 					n->def = $2;
2340 					n->missing_ok = false;
2341 					$$ = (Node *)n;
2342 				}
2343 			/* ALTER TABLE <name> ALTER CONSTRAINT ... */
2344 			| ALTER CONSTRAINT name ConstraintAttributeSpec
2345 				{
2346 					AlterTableCmd *n = makeNode(AlterTableCmd);
2347 					Constraint *c = makeNode(Constraint);
2348 					n->subtype = AT_AlterConstraint;
2349 					n->def = (Node *) c;
2350 					c->contype = CONSTR_FOREIGN; /* others not supported, yet */
2351 					c->conname = $3;
2352 					processCASbits($4, @4, "ALTER CONSTRAINT statement",
2353 									&c->deferrable,
2354 									&c->initdeferred,
2355 									NULL, NULL, yyscanner);
2356 					$$ = (Node *)n;
2357 				}
2358 			/* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
2359 			| VALIDATE CONSTRAINT name
2360 				{
2361 					AlterTableCmd *n = makeNode(AlterTableCmd);
2362 					n->subtype = AT_ValidateConstraint;
2363 					n->name = $3;
2364 					$$ = (Node *)n;
2365 				}
2366 			/* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
2367 			| DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
2368 				{
2369 					AlterTableCmd *n = makeNode(AlterTableCmd);
2370 					n->subtype = AT_DropConstraint;
2371 					n->name = $5;
2372 					n->behavior = $6;
2373 					n->missing_ok = true;
2374 					$$ = (Node *)n;
2375 				}
2376 			/* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
2377 			| DROP CONSTRAINT name opt_drop_behavior
2378 				{
2379 					AlterTableCmd *n = makeNode(AlterTableCmd);
2380 					n->subtype = AT_DropConstraint;
2381 					n->name = $3;
2382 					n->behavior = $4;
2383 					n->missing_ok = false;
2384 					$$ = (Node *)n;
2385 				}
2386 			/* ALTER TABLE <name> SET WITHOUT OIDS, for backward compat  */
2387 			| SET WITHOUT OIDS
2388 				{
2389 					AlterTableCmd *n = makeNode(AlterTableCmd);
2390 					n->subtype = AT_DropOids;
2391 					$$ = (Node *)n;
2392 				}
2393 			/* ALTER TABLE <name> CLUSTER ON <indexname> */
2394 			| CLUSTER ON name
2395 				{
2396 					AlterTableCmd *n = makeNode(AlterTableCmd);
2397 					n->subtype = AT_ClusterOn;
2398 					n->name = $3;
2399 					$$ = (Node *)n;
2400 				}
2401 			/* ALTER TABLE <name> SET WITHOUT CLUSTER */
2402 			| SET WITHOUT CLUSTER
2403 				{
2404 					AlterTableCmd *n = makeNode(AlterTableCmd);
2405 					n->subtype = AT_DropCluster;
2406 					n->name = NULL;
2407 					$$ = (Node *)n;
2408 				}
2409 			/* ALTER TABLE <name> SET LOGGED  */
2410 			| SET LOGGED
2411 				{
2412 					AlterTableCmd *n = makeNode(AlterTableCmd);
2413 					n->subtype = AT_SetLogged;
2414 					$$ = (Node *)n;
2415 				}
2416 			/* ALTER TABLE <name> SET UNLOGGED  */
2417 			| SET UNLOGGED
2418 				{
2419 					AlterTableCmd *n = makeNode(AlterTableCmd);
2420 					n->subtype = AT_SetUnLogged;
2421 					$$ = (Node *)n;
2422 				}
2423 			/* ALTER TABLE <name> ENABLE TRIGGER <trig> */
2424 			| ENABLE_P TRIGGER name
2425 				{
2426 					AlterTableCmd *n = makeNode(AlterTableCmd);
2427 					n->subtype = AT_EnableTrig;
2428 					n->name = $3;
2429 					$$ = (Node *)n;
2430 				}
2431 			/* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
2432 			| ENABLE_P ALWAYS TRIGGER name
2433 				{
2434 					AlterTableCmd *n = makeNode(AlterTableCmd);
2435 					n->subtype = AT_EnableAlwaysTrig;
2436 					n->name = $4;
2437 					$$ = (Node *)n;
2438 				}
2439 			/* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
2440 			| ENABLE_P REPLICA TRIGGER name
2441 				{
2442 					AlterTableCmd *n = makeNode(AlterTableCmd);
2443 					n->subtype = AT_EnableReplicaTrig;
2444 					n->name = $4;
2445 					$$ = (Node *)n;
2446 				}
2447 			/* ALTER TABLE <name> ENABLE TRIGGER ALL */
2448 			| ENABLE_P TRIGGER ALL
2449 				{
2450 					AlterTableCmd *n = makeNode(AlterTableCmd);
2451 					n->subtype = AT_EnableTrigAll;
2452 					$$ = (Node *)n;
2453 				}
2454 			/* ALTER TABLE <name> ENABLE TRIGGER USER */
2455 			| ENABLE_P TRIGGER USER
2456 				{
2457 					AlterTableCmd *n = makeNode(AlterTableCmd);
2458 					n->subtype = AT_EnableTrigUser;
2459 					$$ = (Node *)n;
2460 				}
2461 			/* ALTER TABLE <name> DISABLE TRIGGER <trig> */
2462 			| DISABLE_P TRIGGER name
2463 				{
2464 					AlterTableCmd *n = makeNode(AlterTableCmd);
2465 					n->subtype = AT_DisableTrig;
2466 					n->name = $3;
2467 					$$ = (Node *)n;
2468 				}
2469 			/* ALTER TABLE <name> DISABLE TRIGGER ALL */
2470 			| DISABLE_P TRIGGER ALL
2471 				{
2472 					AlterTableCmd *n = makeNode(AlterTableCmd);
2473 					n->subtype = AT_DisableTrigAll;
2474 					$$ = (Node *)n;
2475 				}
2476 			/* ALTER TABLE <name> DISABLE TRIGGER USER */
2477 			| DISABLE_P TRIGGER USER
2478 				{
2479 					AlterTableCmd *n = makeNode(AlterTableCmd);
2480 					n->subtype = AT_DisableTrigUser;
2481 					$$ = (Node *)n;
2482 				}
2483 			/* ALTER TABLE <name> ENABLE RULE <rule> */
2484 			| ENABLE_P RULE name
2485 				{
2486 					AlterTableCmd *n = makeNode(AlterTableCmd);
2487 					n->subtype = AT_EnableRule;
2488 					n->name = $3;
2489 					$$ = (Node *)n;
2490 				}
2491 			/* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
2492 			| ENABLE_P ALWAYS RULE name
2493 				{
2494 					AlterTableCmd *n = makeNode(AlterTableCmd);
2495 					n->subtype = AT_EnableAlwaysRule;
2496 					n->name = $4;
2497 					$$ = (Node *)n;
2498 				}
2499 			/* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
2500 			| ENABLE_P REPLICA RULE name
2501 				{
2502 					AlterTableCmd *n = makeNode(AlterTableCmd);
2503 					n->subtype = AT_EnableReplicaRule;
2504 					n->name = $4;
2505 					$$ = (Node *)n;
2506 				}
2507 			/* ALTER TABLE <name> DISABLE RULE <rule> */
2508 			| DISABLE_P RULE name
2509 				{
2510 					AlterTableCmd *n = makeNode(AlterTableCmd);
2511 					n->subtype = AT_DisableRule;
2512 					n->name = $3;
2513 					$$ = (Node *)n;
2514 				}
2515 			/* ALTER TABLE <name> INHERIT <parent> */
2516 			| INHERIT qualified_name
2517 				{
2518 					AlterTableCmd *n = makeNode(AlterTableCmd);
2519 					n->subtype = AT_AddInherit;
2520 					n->def = (Node *) $2;
2521 					$$ = (Node *)n;
2522 				}
2523 			/* ALTER TABLE <name> NO INHERIT <parent> */
2524 			| NO INHERIT qualified_name
2525 				{
2526 					AlterTableCmd *n = makeNode(AlterTableCmd);
2527 					n->subtype = AT_DropInherit;
2528 					n->def = (Node *) $3;
2529 					$$ = (Node *)n;
2530 				}
2531 			/* ALTER TABLE <name> OF <type_name> */
2532 			| OF any_name
2533 				{
2534 					AlterTableCmd *n = makeNode(AlterTableCmd);
2535 					TypeName *def = makeTypeNameFromNameList($2);
2536 					def->location = @2;
2537 					n->subtype = AT_AddOf;
2538 					n->def = (Node *) def;
2539 					$$ = (Node *)n;
2540 				}
2541 			/* ALTER TABLE <name> NOT OF */
2542 			| NOT OF
2543 				{
2544 					AlterTableCmd *n = makeNode(AlterTableCmd);
2545 					n->subtype = AT_DropOf;
2546 					$$ = (Node *)n;
2547 				}
2548 			/* ALTER TABLE <name> OWNER TO RoleSpec */
2549 			| OWNER TO RoleSpec
2550 				{
2551 					AlterTableCmd *n = makeNode(AlterTableCmd);
2552 					n->subtype = AT_ChangeOwner;
2553 					n->newowner = $3;
2554 					$$ = (Node *)n;
2555 				}
2556 			/* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
2557 			| SET TABLESPACE name
2558 				{
2559 					AlterTableCmd *n = makeNode(AlterTableCmd);
2560 					n->subtype = AT_SetTableSpace;
2561 					n->name = $3;
2562 					$$ = (Node *)n;
2563 				}
2564 			/* ALTER TABLE <name> SET (...) */
2565 			| SET reloptions
2566 				{
2567 					AlterTableCmd *n = makeNode(AlterTableCmd);
2568 					n->subtype = AT_SetRelOptions;
2569 					n->def = (Node *)$2;
2570 					$$ = (Node *)n;
2571 				}
2572 			/* ALTER TABLE <name> RESET (...) */
2573 			| RESET reloptions
2574 				{
2575 					AlterTableCmd *n = makeNode(AlterTableCmd);
2576 					n->subtype = AT_ResetRelOptions;
2577 					n->def = (Node *)$2;
2578 					$$ = (Node *)n;
2579 				}
2580 			/* ALTER TABLE <name> REPLICA IDENTITY  */
2581 			| REPLICA IDENTITY_P replica_identity
2582 				{
2583 					AlterTableCmd *n = makeNode(AlterTableCmd);
2584 					n->subtype = AT_ReplicaIdentity;
2585 					n->def = $3;
2586 					$$ = (Node *)n;
2587 				}
2588 			/* ALTER TABLE <name> ENABLE ROW LEVEL SECURITY */
2589 			| ENABLE_P ROW LEVEL SECURITY
2590 				{
2591 					AlterTableCmd *n = makeNode(AlterTableCmd);
2592 					n->subtype = AT_EnableRowSecurity;
2593 					$$ = (Node *)n;
2594 				}
2595 			/* ALTER TABLE <name> DISABLE ROW LEVEL SECURITY */
2596 			| DISABLE_P ROW LEVEL SECURITY
2597 				{
2598 					AlterTableCmd *n = makeNode(AlterTableCmd);
2599 					n->subtype = AT_DisableRowSecurity;
2600 					$$ = (Node *)n;
2601 				}
2602 			/* ALTER TABLE <name> FORCE ROW LEVEL SECURITY */
2603 			| FORCE ROW LEVEL SECURITY
2604 				{
2605 					AlterTableCmd *n = makeNode(AlterTableCmd);
2606 					n->subtype = AT_ForceRowSecurity;
2607 					$$ = (Node *)n;
2608 				}
2609 			/* ALTER TABLE <name> NO FORCE ROW LEVEL SECURITY */
2610 			| NO FORCE ROW LEVEL SECURITY
2611 				{
2612 					AlterTableCmd *n = makeNode(AlterTableCmd);
2613 					n->subtype = AT_NoForceRowSecurity;
2614 					$$ = (Node *)n;
2615 				}
2616 			| alter_generic_options
2617 				{
2618 					AlterTableCmd *n = makeNode(AlterTableCmd);
2619 					n->subtype = AT_GenericOptions;
2620 					n->def = (Node *)$1;
2621 					$$ = (Node *) n;
2622 				}
2623 		;
2624 
2625 alter_column_default:
2626 			SET DEFAULT a_expr			{ $$ = $3; }
2627 			| DROP DEFAULT				{ $$ = NULL; }
2628 		;
2629 
2630 opt_drop_behavior:
2631 			CASCADE						{ $$ = DROP_CASCADE; }
2632 			| RESTRICT					{ $$ = DROP_RESTRICT; }
2633 			| /* EMPTY */				{ $$ = DROP_RESTRICT; /* default */ }
2634 		;
2635 
2636 opt_collate_clause:
2637 			COLLATE any_name
2638 				{
2639 					CollateClause *n = makeNode(CollateClause);
2640 					n->arg = NULL;
2641 					n->collname = $2;
2642 					n->location = @1;
2643 					$$ = (Node *) n;
2644 				}
2645 			| /* EMPTY */				{ $$ = NULL; }
2646 		;
2647 
2648 alter_using:
2649 			USING a_expr				{ $$ = $2; }
2650 			| /* EMPTY */				{ $$ = NULL; }
2651 		;
2652 
2653 replica_identity:
2654 			NOTHING
2655 				{
2656 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2657 					n->identity_type = REPLICA_IDENTITY_NOTHING;
2658 					n->name = NULL;
2659 					$$ = (Node *) n;
2660 				}
2661 			| FULL
2662 				{
2663 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2664 					n->identity_type = REPLICA_IDENTITY_FULL;
2665 					n->name = NULL;
2666 					$$ = (Node *) n;
2667 				}
2668 			| DEFAULT
2669 				{
2670 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2671 					n->identity_type = REPLICA_IDENTITY_DEFAULT;
2672 					n->name = NULL;
2673 					$$ = (Node *) n;
2674 				}
2675 			| USING INDEX name
2676 				{
2677 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2678 					n->identity_type = REPLICA_IDENTITY_INDEX;
2679 					n->name = $3;
2680 					$$ = (Node *) n;
2681 				}
2682 ;
2683 
2684 reloptions:
2685 			'(' reloption_list ')'					{ $$ = $2; }
2686 		;
2687 
2688 opt_reloptions:		WITH reloptions					{ $$ = $2; }
2689 			 |		/* EMPTY */						{ $$ = NIL; }
2690 		;
2691 
2692 reloption_list:
2693 			reloption_elem							{ $$ = list_make1($1); }
2694 			| reloption_list ',' reloption_elem		{ $$ = lappend($1, $3); }
2695 		;
2696 
2697 /* This should match def_elem and also allow qualified names */
2698 reloption_elem:
2699 			ColLabel '=' def_arg
2700 				{
2701 					$$ = makeDefElem($1, (Node *) $3, @1);
2702 				}
2703 			| ColLabel
2704 				{
2705 					$$ = makeDefElem($1, NULL, @1);
2706 				}
2707 			| ColLabel '.' ColLabel '=' def_arg
2708 				{
2709 					$$ = makeDefElemExtended($1, $3, (Node *) $5,
2710 											 DEFELEM_UNSPEC, @1);
2711 				}
2712 			| ColLabel '.' ColLabel
2713 				{
2714 					$$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC, @1);
2715 				}
2716 		;
2717 
2718 alter_identity_column_option_list:
2719 			alter_identity_column_option
2720 				{ $$ = list_make1($1); }
2721 			| alter_identity_column_option_list alter_identity_column_option
2722 				{ $$ = lappend($1, $2); }
2723 		;
2724 
2725 alter_identity_column_option:
2726 			RESTART
2727 				{
2728 					$$ = makeDefElem("restart", NULL, @1);
2729 				}
2730 			| RESTART opt_with NumericOnly
2731 				{
2732 					$$ = makeDefElem("restart", (Node *)$3, @1);
2733 				}
2734 			| SET SeqOptElem
2735 				{
2736 					if (strcmp($2->defname, "as") == 0 ||
2737 						strcmp($2->defname, "restart") == 0 ||
2738 						strcmp($2->defname, "owned_by") == 0)
2739 						ereport(ERROR,
2740 								(errcode(ERRCODE_SYNTAX_ERROR),
2741 								 errmsg("sequence option \"%s\" not supported here", $2->defname),
2742 								 parser_errposition(@2)));
2743 					$$ = $2;
2744 				}
2745 			| SET GENERATED generated_when
2746 				{
2747 					$$ = makeDefElem("generated", (Node *) makeInteger($3), @1);
2748 				}
2749 		;
2750 
2751 PartitionBoundSpec:
2752 			/* a HASH partition */
2753 			FOR VALUES WITH '(' hash_partbound ')'
2754 				{
2755 					ListCell   *lc;
2756 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2757 
2758 					n->strategy = PARTITION_STRATEGY_HASH;
2759 					n->modulus = n->remainder = -1;
2760 
foreach(lc,$5)2761 					foreach (lc, $5)
2762 					{
2763 						DefElem    *opt = lfirst_node(DefElem, lc);
2764 
2765 						if (strcmp(opt->defname, "modulus") == 0)
2766 						{
2767 							if (n->modulus != -1)
2768 								ereport(ERROR,
2769 										(errcode(ERRCODE_DUPLICATE_OBJECT),
2770 										 errmsg("modulus for hash partition provided more than once"),
2771 										 parser_errposition(opt->location)));
2772 							n->modulus = defGetInt32(opt);
2773 						}
2774 						else if (strcmp(opt->defname, "remainder") == 0)
2775 						{
2776 							if (n->remainder != -1)
2777 								ereport(ERROR,
2778 										(errcode(ERRCODE_DUPLICATE_OBJECT),
2779 										 errmsg("remainder for hash partition provided more than once"),
2780 										 parser_errposition(opt->location)));
2781 							n->remainder = defGetInt32(opt);
2782 						}
2783 						else
2784 							ereport(ERROR,
2785 									(errcode(ERRCODE_SYNTAX_ERROR),
2786 									 errmsg("unrecognized hash partition bound specification \"%s\"",
2787 											opt->defname),
2788 									 parser_errposition(opt->location)));
2789 					}
2790 
2791 					if (n->modulus == -1)
2792 						ereport(ERROR,
2793 								(errcode(ERRCODE_SYNTAX_ERROR),
2794 								 errmsg("modulus for hash partition must be specified")));
2795 					if (n->remainder == -1)
2796 						ereport(ERROR,
2797 								(errcode(ERRCODE_SYNTAX_ERROR),
2798 								 errmsg("remainder for hash partition must be specified")));
2799 
2800 					n->location = @3;
2801 
2802 					$$ = n;
2803 				}
2804 
2805 			/* a LIST partition */
2806 			| FOR VALUES IN_P '(' expr_list ')'
2807 				{
2808 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2809 
2810 					n->strategy = PARTITION_STRATEGY_LIST;
2811 					n->is_default = false;
2812 					n->listdatums = $5;
2813 					n->location = @3;
2814 
2815 					$$ = n;
2816 				}
2817 
2818 			/* a RANGE partition */
2819 			| FOR VALUES FROM '(' expr_list ')' TO '(' expr_list ')'
2820 				{
2821 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2822 
2823 					n->strategy = PARTITION_STRATEGY_RANGE;
2824 					n->is_default = false;
2825 					n->lowerdatums = $5;
2826 					n->upperdatums = $9;
2827 					n->location = @3;
2828 
2829 					$$ = n;
2830 				}
2831 
2832 			/* a DEFAULT partition */
2833 			| DEFAULT
2834 				{
2835 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2836 
2837 					n->is_default = true;
2838 					n->location = @1;
2839 
2840 					$$ = n;
2841 				}
2842 		;
2843 
2844 hash_partbound_elem:
2845 		NonReservedWord Iconst
2846 			{
2847 				$$ = makeDefElem($1, (Node *)makeInteger($2), @1);
2848 			}
2849 		;
2850 
2851 hash_partbound:
2852 		hash_partbound_elem
2853 			{
2854 				$$ = list_make1($1);
2855 			}
2856 		| hash_partbound ',' hash_partbound_elem
2857 			{
2858 				$$ = lappend($1, $3);
2859 			}
2860 		;
2861 
2862 /*****************************************************************************
2863  *
2864  *	ALTER TYPE
2865  *
2866  * really variants of the ALTER TABLE subcommands with different spellings
2867  *****************************************************************************/
2868 
2869 AlterCompositeTypeStmt:
2870 			ALTER TYPE_P any_name alter_type_cmds
2871 				{
2872 					AlterTableStmt *n = makeNode(AlterTableStmt);
2873 
2874 					/* can't use qualified_name, sigh */
2875 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
2876 					n->cmds = $4;
2877 					n->relkind = OBJECT_TYPE;
2878 					$$ = (Node *)n;
2879 				}
2880 			;
2881 
2882 alter_type_cmds:
2883 			alter_type_cmd							{ $$ = list_make1($1); }
2884 			| alter_type_cmds ',' alter_type_cmd	{ $$ = lappend($1, $3); }
2885 		;
2886 
2887 alter_type_cmd:
2888 			/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
2889 			ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
2890 				{
2891 					AlterTableCmd *n = makeNode(AlterTableCmd);
2892 					n->subtype = AT_AddColumn;
2893 					n->def = $3;
2894 					n->behavior = $4;
2895 					$$ = (Node *)n;
2896 				}
2897 			/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
2898 			| DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
2899 				{
2900 					AlterTableCmd *n = makeNode(AlterTableCmd);
2901 					n->subtype = AT_DropColumn;
2902 					n->name = $5;
2903 					n->behavior = $6;
2904 					n->missing_ok = true;
2905 					$$ = (Node *)n;
2906 				}
2907 			/* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
2908 			| DROP ATTRIBUTE ColId opt_drop_behavior
2909 				{
2910 					AlterTableCmd *n = makeNode(AlterTableCmd);
2911 					n->subtype = AT_DropColumn;
2912 					n->name = $3;
2913 					n->behavior = $4;
2914 					n->missing_ok = false;
2915 					$$ = (Node *)n;
2916 				}
2917 			/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
2918 			| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
2919 				{
2920 					AlterTableCmd *n = makeNode(AlterTableCmd);
2921 					ColumnDef *def = makeNode(ColumnDef);
2922 					n->subtype = AT_AlterColumnType;
2923 					n->name = $3;
2924 					n->def = (Node *) def;
2925 					n->behavior = $8;
2926 					/* We only use these fields of the ColumnDef node */
2927 					def->typeName = $6;
2928 					def->collClause = (CollateClause *) $7;
2929 					def->raw_default = NULL;
2930 					def->location = @3;
2931 					$$ = (Node *)n;
2932 				}
2933 		;
2934 
2935 
2936 /*****************************************************************************
2937  *
2938  *		QUERY :
2939  *				close <portalname>
2940  *
2941  *****************************************************************************/
2942 
2943 ClosePortalStmt:
2944 			CLOSE cursor_name
2945 				{
2946 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2947 					n->portalname = $2;
2948 					$$ = (Node *)n;
2949 				}
2950 			| CLOSE ALL
2951 				{
2952 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2953 					n->portalname = NULL;
2954 					$$ = (Node *)n;
2955 				}
2956 		;
2957 
2958 
2959 /*****************************************************************************
2960  *
2961  *		QUERY :
2962  *				COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
2963  *				COPY ( query ) TO file	[WITH] [(options)]
2964  *
2965  *				where 'query' can be one of:
2966  *				{ SELECT | UPDATE | INSERT | DELETE }
2967  *
2968  *				and 'file' can be one of:
2969  *				{ PROGRAM 'command' | STDIN | STDOUT | 'filename' }
2970  *
2971  *				In the preferred syntax the options are comma-separated
2972  *				and use generic identifiers instead of keywords.  The pre-9.0
2973  *				syntax had a hard-wired, space-separated set of options.
2974  *
2975  *				Really old syntax, from versions 7.2 and prior:
2976  *				COPY [ BINARY ] table FROM/TO file
2977  *					[ [ USING ] DELIMITERS 'delimiter' ] ]
2978  *					[ WITH NULL AS 'null string' ]
2979  *				This option placement is not supported with COPY (query...).
2980  *
2981  *****************************************************************************/
2982 
2983 CopyStmt:	COPY opt_binary qualified_name opt_column_list
2984 			copy_from opt_program copy_file_name copy_delimiter opt_with
2985 			copy_options where_clause
2986 				{
2987 					CopyStmt *n = makeNode(CopyStmt);
2988 					n->relation = $3;
2989 					n->query = NULL;
2990 					n->attlist = $4;
2991 					n->is_from = $5;
2992 					n->is_program = $6;
2993 					n->filename = $7;
2994 					n->whereClause = $11;
2995 
2996 					if (n->is_program && n->filename == NULL)
2997 						ereport(ERROR,
2998 								(errcode(ERRCODE_SYNTAX_ERROR),
2999 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
3000 								 parser_errposition(@8)));
3001 
3002 					if (!n->is_from && n->whereClause != NULL)
3003 						ereport(ERROR,
3004 								(errcode(ERRCODE_SYNTAX_ERROR),
3005 								 errmsg("WHERE clause not allowed with COPY TO"),
3006 								 parser_errposition(@11)));
3007 
3008 					n->options = NIL;
3009 					/* Concatenate user-supplied flags */
3010 					if ($2)
3011 						n->options = lappend(n->options, $2);
3012 					if ($8)
3013 						n->options = lappend(n->options, $8);
3014 					if ($10)
3015 						n->options = list_concat(n->options, $10);
3016 					$$ = (Node *)n;
3017 				}
3018 			| COPY '(' PreparableStmt ')' TO opt_program copy_file_name opt_with copy_options
3019 				{
3020 					CopyStmt *n = makeNode(CopyStmt);
3021 					n->relation = NULL;
3022 					n->query = $3;
3023 					n->attlist = NIL;
3024 					n->is_from = false;
3025 					n->is_program = $6;
3026 					n->filename = $7;
3027 					n->options = $9;
3028 
3029 					if (n->is_program && n->filename == NULL)
3030 						ereport(ERROR,
3031 								(errcode(ERRCODE_SYNTAX_ERROR),
3032 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
3033 								 parser_errposition(@5)));
3034 
3035 					$$ = (Node *)n;
3036 				}
3037 		;
3038 
3039 copy_from:
3040 			FROM									{ $$ = true; }
3041 			| TO									{ $$ = false; }
3042 		;
3043 
3044 opt_program:
3045 			PROGRAM									{ $$ = true; }
3046 			| /* EMPTY */							{ $$ = false; }
3047 		;
3048 
3049 /*
3050  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
3051  * used depends on the direction. (It really doesn't make sense to copy from
3052  * stdout. We silently correct the "typo".)		 - AY 9/94
3053  */
3054 copy_file_name:
3055 			Sconst									{ $$ = $1; }
3056 			| STDIN									{ $$ = NULL; }
3057 			| STDOUT								{ $$ = NULL; }
3058 		;
3059 
3060 copy_options: copy_opt_list							{ $$ = $1; }
3061 			| '(' copy_generic_opt_list ')'			{ $$ = $2; }
3062 		;
3063 
3064 /* old COPY option syntax */
3065 copy_opt_list:
3066 			copy_opt_list copy_opt_item				{ $$ = lappend($1, $2); }
3067 			| /* EMPTY */							{ $$ = NIL; }
3068 		;
3069 
3070 copy_opt_item:
3071 			BINARY
3072 				{
3073 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3074 				}
3075 			| FREEZE
3076 				{
3077 					$$ = makeDefElem("freeze", (Node *)makeInteger(true), @1);
3078 				}
3079 			| DELIMITER opt_as Sconst
3080 				{
3081 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @1);
3082 				}
3083 			| NULL_P opt_as Sconst
3084 				{
3085 					$$ = makeDefElem("null", (Node *)makeString($3), @1);
3086 				}
3087 			| CSV
3088 				{
3089 					$$ = makeDefElem("format", (Node *)makeString("csv"), @1);
3090 				}
3091 			| HEADER_P
3092 				{
3093 					$$ = makeDefElem("header", (Node *)makeInteger(true), @1);
3094 				}
3095 			| QUOTE opt_as Sconst
3096 				{
3097 					$$ = makeDefElem("quote", (Node *)makeString($3), @1);
3098 				}
3099 			| ESCAPE opt_as Sconst
3100 				{
3101 					$$ = makeDefElem("escape", (Node *)makeString($3), @1);
3102 				}
3103 			| FORCE QUOTE columnList
3104 				{
3105 					$$ = makeDefElem("force_quote", (Node *)$3, @1);
3106 				}
3107 			| FORCE QUOTE '*'
3108 				{
3109 					$$ = makeDefElem("force_quote", (Node *)makeNode(A_Star), @1);
3110 				}
3111 			| FORCE NOT NULL_P columnList
3112 				{
3113 					$$ = makeDefElem("force_not_null", (Node *)$4, @1);
3114 				}
3115 			| FORCE NULL_P columnList
3116 				{
3117 					$$ = makeDefElem("force_null", (Node *)$3, @1);
3118 				}
3119 			| ENCODING Sconst
3120 				{
3121 					$$ = makeDefElem("encoding", (Node *)makeString($2), @1);
3122 				}
3123 		;
3124 
3125 /* The following exist for backward compatibility with very old versions */
3126 
3127 opt_binary:
3128 			BINARY
3129 				{
3130 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3131 				}
3132 			| /*EMPTY*/								{ $$ = NULL; }
3133 		;
3134 
3135 copy_delimiter:
3136 			opt_using DELIMITERS Sconst
3137 				{
3138 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @2);
3139 				}
3140 			| /*EMPTY*/								{ $$ = NULL; }
3141 		;
3142 
3143 opt_using:
3144 			USING									{}
3145 			| /*EMPTY*/								{}
3146 		;
3147 
3148 /* new COPY option syntax */
3149 copy_generic_opt_list:
3150 			copy_generic_opt_elem
3151 				{
3152 					$$ = list_make1($1);
3153 				}
3154 			| copy_generic_opt_list ',' copy_generic_opt_elem
3155 				{
3156 					$$ = lappend($1, $3);
3157 				}
3158 		;
3159 
3160 copy_generic_opt_elem:
3161 			ColLabel copy_generic_opt_arg
3162 				{
3163 					$$ = makeDefElem($1, $2, @1);
3164 				}
3165 		;
3166 
3167 copy_generic_opt_arg:
3168 			opt_boolean_or_string			{ $$ = (Node *) makeString($1); }
3169 			| NumericOnly					{ $$ = (Node *) $1; }
3170 			| '*'							{ $$ = (Node *) makeNode(A_Star); }
3171 			| '(' copy_generic_opt_arg_list ')'		{ $$ = (Node *) $2; }
3172 			| /* EMPTY */					{ $$ = NULL; }
3173 		;
3174 
3175 copy_generic_opt_arg_list:
3176 			  copy_generic_opt_arg_list_item
3177 				{
3178 					$$ = list_make1($1);
3179 				}
3180 			| copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
3181 				{
3182 					$$ = lappend($1, $3);
3183 				}
3184 		;
3185 
3186 /* beware of emitting non-string list elements here; see commands/define.c */
3187 copy_generic_opt_arg_list_item:
3188 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
3189 		;
3190 
3191 
3192 /*****************************************************************************
3193  *
3194  *		QUERY :
3195  *				CREATE TABLE relname
3196  *
3197  *****************************************************************************/
3198 
3199 CreateStmt:	CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
3200 			OptInherit OptPartitionSpec table_access_method_clause OptWith
3201 			OnCommitOption OptTableSpace
3202 				{
3203 					CreateStmt *n = makeNode(CreateStmt);
3204 					$4->relpersistence = $2;
3205 					n->relation = $4;
3206 					n->tableElts = $6;
3207 					n->inhRelations = $8;
3208 					n->partspec = $9;
3209 					n->ofTypename = NULL;
3210 					n->constraints = NIL;
3211 					n->accessMethod = $10;
3212 					n->options = $11;
3213 					n->oncommit = $12;
3214 					n->tablespacename = $13;
3215 					n->if_not_exists = false;
3216 					$$ = (Node *)n;
3217 				}
3218 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
3219 			OptTableElementList ')' OptInherit OptPartitionSpec table_access_method_clause
3220 			OptWith OnCommitOption OptTableSpace
3221 				{
3222 					CreateStmt *n = makeNode(CreateStmt);
3223 					$7->relpersistence = $2;
3224 					n->relation = $7;
3225 					n->tableElts = $9;
3226 					n->inhRelations = $11;
3227 					n->partspec = $12;
3228 					n->ofTypename = NULL;
3229 					n->constraints = NIL;
3230 					n->accessMethod = $13;
3231 					n->options = $14;
3232 					n->oncommit = $15;
3233 					n->tablespacename = $16;
3234 					n->if_not_exists = true;
3235 					$$ = (Node *)n;
3236 				}
3237 		| CREATE OptTemp TABLE qualified_name OF any_name
3238 			OptTypedTableElementList OptPartitionSpec table_access_method_clause
3239 			OptWith OnCommitOption OptTableSpace
3240 				{
3241 					CreateStmt *n = makeNode(CreateStmt);
3242 					$4->relpersistence = $2;
3243 					n->relation = $4;
3244 					n->tableElts = $7;
3245 					n->inhRelations = NIL;
3246 					n->partspec = $8;
3247 					n->ofTypename = makeTypeNameFromNameList($6);
3248 					n->ofTypename->location = @6;
3249 					n->constraints = NIL;
3250 					n->accessMethod = $9;
3251 					n->options = $10;
3252 					n->oncommit = $11;
3253 					n->tablespacename = $12;
3254 					n->if_not_exists = false;
3255 					$$ = (Node *)n;
3256 				}
3257 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
3258 			OptTypedTableElementList OptPartitionSpec table_access_method_clause
3259 			OptWith OnCommitOption OptTableSpace
3260 				{
3261 					CreateStmt *n = makeNode(CreateStmt);
3262 					$7->relpersistence = $2;
3263 					n->relation = $7;
3264 					n->tableElts = $10;
3265 					n->inhRelations = NIL;
3266 					n->partspec = $11;
3267 					n->ofTypename = makeTypeNameFromNameList($9);
3268 					n->ofTypename->location = @9;
3269 					n->constraints = NIL;
3270 					n->accessMethod = $12;
3271 					n->options = $13;
3272 					n->oncommit = $14;
3273 					n->tablespacename = $15;
3274 					n->if_not_exists = true;
3275 					$$ = (Node *)n;
3276 				}
3277 		| CREATE OptTemp TABLE qualified_name PARTITION OF qualified_name
3278 			OptTypedTableElementList PartitionBoundSpec OptPartitionSpec
3279 			table_access_method_clause OptWith OnCommitOption OptTableSpace
3280 				{
3281 					CreateStmt *n = makeNode(CreateStmt);
3282 					$4->relpersistence = $2;
3283 					n->relation = $4;
3284 					n->tableElts = $8;
3285 					n->inhRelations = list_make1($7);
3286 					n->partbound = $9;
3287 					n->partspec = $10;
3288 					n->ofTypename = NULL;
3289 					n->constraints = NIL;
3290 					n->accessMethod = $11;
3291 					n->options = $12;
3292 					n->oncommit = $13;
3293 					n->tablespacename = $14;
3294 					n->if_not_exists = false;
3295 					$$ = (Node *)n;
3296 				}
3297 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name PARTITION OF
3298 			qualified_name OptTypedTableElementList PartitionBoundSpec OptPartitionSpec
3299 			table_access_method_clause OptWith OnCommitOption OptTableSpace
3300 				{
3301 					CreateStmt *n = makeNode(CreateStmt);
3302 					$7->relpersistence = $2;
3303 					n->relation = $7;
3304 					n->tableElts = $11;
3305 					n->inhRelations = list_make1($10);
3306 					n->partbound = $12;
3307 					n->partspec = $13;
3308 					n->ofTypename = NULL;
3309 					n->constraints = NIL;
3310 					n->accessMethod = $14;
3311 					n->options = $15;
3312 					n->oncommit = $16;
3313 					n->tablespacename = $17;
3314 					n->if_not_exists = true;
3315 					$$ = (Node *)n;
3316 				}
3317 		;
3318 
3319 /*
3320  * Redundancy here is needed to avoid shift/reduce conflicts,
3321  * since TEMP is not a reserved word.  See also OptTempTableName.
3322  *
3323  * NOTE: we accept both GLOBAL and LOCAL options.  They currently do nothing,
3324  * but future versions might consider GLOBAL to request SQL-spec-compliant
3325  * temp table behavior, so warn about that.  Since we have no modules the
3326  * LOCAL keyword is really meaningless; furthermore, some other products
3327  * implement LOCAL as meaning the same as our default temp table behavior,
3328  * so we'll probably continue to treat LOCAL as a noise word.
3329  */
3330 OptTemp:	TEMPORARY					{ $$ = RELPERSISTENCE_TEMP; }
3331 			| TEMP						{ $$ = RELPERSISTENCE_TEMP; }
3332 			| LOCAL TEMPORARY			{ $$ = RELPERSISTENCE_TEMP; }
3333 			| LOCAL TEMP				{ $$ = RELPERSISTENCE_TEMP; }
3334 			| GLOBAL TEMPORARY
3335 				{
3336 					ereport(WARNING,
3337 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3338 							 parser_errposition(@1)));
3339 					$$ = RELPERSISTENCE_TEMP;
3340 				}
3341 			| GLOBAL TEMP
3342 				{
3343 					ereport(WARNING,
3344 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3345 							 parser_errposition(@1)));
3346 					$$ = RELPERSISTENCE_TEMP;
3347 				}
3348 			| UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
3349 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
3350 		;
3351 
3352 OptTableElementList:
3353 			TableElementList					{ $$ = $1; }
3354 			| /*EMPTY*/							{ $$ = NIL; }
3355 		;
3356 
3357 OptTypedTableElementList:
3358 			'(' TypedTableElementList ')'		{ $$ = $2; }
3359 			| /*EMPTY*/							{ $$ = NIL; }
3360 		;
3361 
3362 TableElementList:
3363 			TableElement
3364 				{
3365 					$$ = list_make1($1);
3366 				}
3367 			| TableElementList ',' TableElement
3368 				{
3369 					$$ = lappend($1, $3);
3370 				}
3371 		;
3372 
3373 TypedTableElementList:
3374 			TypedTableElement
3375 				{
3376 					$$ = list_make1($1);
3377 				}
3378 			| TypedTableElementList ',' TypedTableElement
3379 				{
3380 					$$ = lappend($1, $3);
3381 				}
3382 		;
3383 
3384 TableElement:
3385 			columnDef							{ $$ = $1; }
3386 			| TableLikeClause					{ $$ = $1; }
3387 			| TableConstraint					{ $$ = $1; }
3388 		;
3389 
3390 TypedTableElement:
3391 			columnOptions						{ $$ = $1; }
3392 			| TableConstraint					{ $$ = $1; }
3393 		;
3394 
3395 columnDef:	ColId Typename create_generic_options ColQualList
3396 				{
3397 					ColumnDef *n = makeNode(ColumnDef);
3398 					n->colname = $1;
3399 					n->typeName = $2;
3400 					n->inhcount = 0;
3401 					n->is_local = true;
3402 					n->is_not_null = false;
3403 					n->is_from_type = false;
3404 					n->storage = 0;
3405 					n->raw_default = NULL;
3406 					n->cooked_default = NULL;
3407 					n->collOid = InvalidOid;
3408 					n->fdwoptions = $3;
3409 					SplitColQualList($4, &n->constraints, &n->collClause,
3410 									 yyscanner);
3411 					n->location = @1;
3412 					$$ = (Node *)n;
3413 				}
3414 		;
3415 
3416 columnOptions:	ColId ColQualList
3417 				{
3418 					ColumnDef *n = makeNode(ColumnDef);
3419 					n->colname = $1;
3420 					n->typeName = NULL;
3421 					n->inhcount = 0;
3422 					n->is_local = true;
3423 					n->is_not_null = false;
3424 					n->is_from_type = false;
3425 					n->storage = 0;
3426 					n->raw_default = NULL;
3427 					n->cooked_default = NULL;
3428 					n->collOid = InvalidOid;
3429 					SplitColQualList($2, &n->constraints, &n->collClause,
3430 									 yyscanner);
3431 					n->location = @1;
3432 					$$ = (Node *)n;
3433 				}
3434 				| ColId WITH OPTIONS ColQualList
3435 				{
3436 					ColumnDef *n = makeNode(ColumnDef);
3437 					n->colname = $1;
3438 					n->typeName = NULL;
3439 					n->inhcount = 0;
3440 					n->is_local = true;
3441 					n->is_not_null = false;
3442 					n->is_from_type = false;
3443 					n->storage = 0;
3444 					n->raw_default = NULL;
3445 					n->cooked_default = NULL;
3446 					n->collOid = InvalidOid;
3447 					SplitColQualList($4, &n->constraints, &n->collClause,
3448 									 yyscanner);
3449 					n->location = @1;
3450 					$$ = (Node *)n;
3451 				}
3452 		;
3453 
3454 ColQualList:
3455 			ColQualList ColConstraint				{ $$ = lappend($1, $2); }
3456 			| /*EMPTY*/								{ $$ = NIL; }
3457 		;
3458 
3459 ColConstraint:
3460 			CONSTRAINT name ColConstraintElem
3461 				{
3462 					Constraint *n = castNode(Constraint, $3);
3463 					n->conname = $2;
3464 					n->location = @1;
3465 					$$ = (Node *) n;
3466 				}
3467 			| ColConstraintElem						{ $$ = $1; }
3468 			| ConstraintAttr						{ $$ = $1; }
3469 			| COLLATE any_name
3470 				{
3471 					/*
3472 					 * Note: the CollateClause is momentarily included in
3473 					 * the list built by ColQualList, but we split it out
3474 					 * again in SplitColQualList.
3475 					 */
3476 					CollateClause *n = makeNode(CollateClause);
3477 					n->arg = NULL;
3478 					n->collname = $2;
3479 					n->location = @1;
3480 					$$ = (Node *) n;
3481 				}
3482 		;
3483 
3484 /* DEFAULT NULL is already the default for Postgres.
3485  * But define it here and carry it forward into the system
3486  * to make it explicit.
3487  * - thomas 1998-09-13
3488  *
3489  * WITH NULL and NULL are not SQL-standard syntax elements,
3490  * so leave them out. Use DEFAULT NULL to explicitly indicate
3491  * that a column may have that value. WITH NULL leads to
3492  * shift/reduce conflicts with WITH TIME ZONE anyway.
3493  * - thomas 1999-01-08
3494  *
3495  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
3496  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
3497  * or be part of a_expr NOT LIKE or similar constructs).
3498  */
3499 ColConstraintElem:
3500 			NOT NULL_P
3501 				{
3502 					Constraint *n = makeNode(Constraint);
3503 					n->contype = CONSTR_NOTNULL;
3504 					n->location = @1;
3505 					$$ = (Node *)n;
3506 				}
3507 			| NULL_P
3508 				{
3509 					Constraint *n = makeNode(Constraint);
3510 					n->contype = CONSTR_NULL;
3511 					n->location = @1;
3512 					$$ = (Node *)n;
3513 				}
3514 			| UNIQUE opt_definition OptConsTableSpace
3515 				{
3516 					Constraint *n = makeNode(Constraint);
3517 					n->contype = CONSTR_UNIQUE;
3518 					n->location = @1;
3519 					n->keys = NULL;
3520 					n->options = $2;
3521 					n->indexname = NULL;
3522 					n->indexspace = $3;
3523 					$$ = (Node *)n;
3524 				}
3525 			| PRIMARY KEY opt_definition OptConsTableSpace
3526 				{
3527 					Constraint *n = makeNode(Constraint);
3528 					n->contype = CONSTR_PRIMARY;
3529 					n->location = @1;
3530 					n->keys = NULL;
3531 					n->options = $3;
3532 					n->indexname = NULL;
3533 					n->indexspace = $4;
3534 					$$ = (Node *)n;
3535 				}
3536 			| CHECK '(' a_expr ')' opt_no_inherit
3537 				{
3538 					Constraint *n = makeNode(Constraint);
3539 					n->contype = CONSTR_CHECK;
3540 					n->location = @1;
3541 					n->is_no_inherit = $5;
3542 					n->raw_expr = $3;
3543 					n->cooked_expr = NULL;
3544 					n->skip_validation = false;
3545 					n->initially_valid = true;
3546 					$$ = (Node *)n;
3547 				}
3548 			| DEFAULT b_expr
3549 				{
3550 					Constraint *n = makeNode(Constraint);
3551 					n->contype = CONSTR_DEFAULT;
3552 					n->location = @1;
3553 					n->raw_expr = $2;
3554 					n->cooked_expr = NULL;
3555 					n->skip_validation = false;
3556 					n->initially_valid = true;
3557 					$$ = (Node *)n;
3558 				}
3559 			| GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
3560 				{
3561 					Constraint *n = makeNode(Constraint);
3562 					n->contype = CONSTR_IDENTITY;
3563 					n->generated_when = $2;
3564 					n->options = $5;
3565 					n->location = @1;
3566 					$$ = (Node *)n;
3567 				}
3568 			| GENERATED generated_when AS '(' a_expr ')' STORED
3569 				{
3570 					Constraint *n = makeNode(Constraint);
3571 					n->contype = CONSTR_GENERATED;
3572 					n->generated_when = $2;
3573 					n->raw_expr = $5;
3574 					n->cooked_expr = NULL;
3575 					n->location = @1;
3576 
3577 					/*
3578 					 * Can't do this in the grammar because of shift/reduce
3579 					 * conflicts.  (IDENTITY allows both ALWAYS and BY
3580 					 * DEFAULT, but generated columns only allow ALWAYS.)  We
3581 					 * can also give a more useful error message and location.
3582 					 */
3583 					if ($2 != ATTRIBUTE_IDENTITY_ALWAYS)
3584 						ereport(ERROR,
3585 								(errcode(ERRCODE_SYNTAX_ERROR),
3586 								 errmsg("for a generated column, GENERATED ALWAYS must be specified"),
3587 								 parser_errposition(@2)));
3588 
3589 					$$ = (Node *)n;
3590 				}
3591 			| REFERENCES qualified_name opt_column_list key_match key_actions
3592 				{
3593 					Constraint *n = makeNode(Constraint);
3594 					n->contype = CONSTR_FOREIGN;
3595 					n->location = @1;
3596 					n->pktable			= $2;
3597 					n->fk_attrs			= NIL;
3598 					n->pk_attrs			= $3;
3599 					n->fk_matchtype		= $4;
3600 					n->fk_upd_action	= (char) ($5 >> 8);
3601 					n->fk_del_action	= (char) ($5 & 0xFF);
3602 					n->skip_validation  = false;
3603 					n->initially_valid  = true;
3604 					$$ = (Node *)n;
3605 				}
3606 		;
3607 
3608 generated_when:
3609 			ALWAYS			{ $$ = ATTRIBUTE_IDENTITY_ALWAYS; }
3610 			| BY DEFAULT	{ $$ = ATTRIBUTE_IDENTITY_BY_DEFAULT; }
3611 		;
3612 
3613 /*
3614  * ConstraintAttr represents constraint attributes, which we parse as if
3615  * they were independent constraint clauses, in order to avoid shift/reduce
3616  * conflicts (since NOT might start either an independent NOT NULL clause
3617  * or an attribute).  parse_utilcmd.c is responsible for attaching the
3618  * attribute information to the preceding "real" constraint node, and for
3619  * complaining if attribute clauses appear in the wrong place or wrong
3620  * combinations.
3621  *
3622  * See also ConstraintAttributeSpec, which can be used in places where
3623  * there is no parsing conflict.  (Note: currently, NOT VALID and NO INHERIT
3624  * are allowed clauses in ConstraintAttributeSpec, but not here.  Someday we
3625  * might need to allow them here too, but for the moment it doesn't seem
3626  * useful in the statements that use ConstraintAttr.)
3627  */
3628 ConstraintAttr:
3629 			DEFERRABLE
3630 				{
3631 					Constraint *n = makeNode(Constraint);
3632 					n->contype = CONSTR_ATTR_DEFERRABLE;
3633 					n->location = @1;
3634 					$$ = (Node *)n;
3635 				}
3636 			| NOT DEFERRABLE
3637 				{
3638 					Constraint *n = makeNode(Constraint);
3639 					n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
3640 					n->location = @1;
3641 					$$ = (Node *)n;
3642 				}
3643 			| INITIALLY DEFERRED
3644 				{
3645 					Constraint *n = makeNode(Constraint);
3646 					n->contype = CONSTR_ATTR_DEFERRED;
3647 					n->location = @1;
3648 					$$ = (Node *)n;
3649 				}
3650 			| INITIALLY IMMEDIATE
3651 				{
3652 					Constraint *n = makeNode(Constraint);
3653 					n->contype = CONSTR_ATTR_IMMEDIATE;
3654 					n->location = @1;
3655 					$$ = (Node *)n;
3656 				}
3657 		;
3658 
3659 
3660 TableLikeClause:
3661 			LIKE qualified_name TableLikeOptionList
3662 				{
3663 					TableLikeClause *n = makeNode(TableLikeClause);
3664 					n->relation = $2;
3665 					n->options = $3;
3666 					$$ = (Node *)n;
3667 				}
3668 		;
3669 
3670 TableLikeOptionList:
3671 				TableLikeOptionList INCLUDING TableLikeOption	{ $$ = $1 | $3; }
3672 				| TableLikeOptionList EXCLUDING TableLikeOption	{ $$ = $1 & ~$3; }
3673 				| /* EMPTY */						{ $$ = 0; }
3674 		;
3675 
3676 TableLikeOption:
3677 				COMMENTS			{ $$ = CREATE_TABLE_LIKE_COMMENTS; }
3678 				| CONSTRAINTS		{ $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
3679 				| DEFAULTS			{ $$ = CREATE_TABLE_LIKE_DEFAULTS; }
3680 				| IDENTITY_P		{ $$ = CREATE_TABLE_LIKE_IDENTITY; }
3681 				| GENERATED			{ $$ = CREATE_TABLE_LIKE_GENERATED; }
3682 				| INDEXES			{ $$ = CREATE_TABLE_LIKE_INDEXES; }
3683 				| STATISTICS		{ $$ = CREATE_TABLE_LIKE_STATISTICS; }
3684 				| STORAGE			{ $$ = CREATE_TABLE_LIKE_STORAGE; }
3685 				| ALL				{ $$ = CREATE_TABLE_LIKE_ALL; }
3686 		;
3687 
3688 
3689 /* ConstraintElem specifies constraint syntax which is not embedded into
3690  *	a column definition. ColConstraintElem specifies the embedded form.
3691  * - thomas 1997-12-03
3692  */
3693 TableConstraint:
3694 			CONSTRAINT name ConstraintElem
3695 				{
3696 					Constraint *n = castNode(Constraint, $3);
3697 					n->conname = $2;
3698 					n->location = @1;
3699 					$$ = (Node *) n;
3700 				}
3701 			| ConstraintElem						{ $$ = $1; }
3702 		;
3703 
3704 ConstraintElem:
3705 			CHECK '(' a_expr ')' ConstraintAttributeSpec
3706 				{
3707 					Constraint *n = makeNode(Constraint);
3708 					n->contype = CONSTR_CHECK;
3709 					n->location = @1;
3710 					n->raw_expr = $3;
3711 					n->cooked_expr = NULL;
3712 					processCASbits($5, @5, "CHECK",
3713 								   NULL, NULL, &n->skip_validation,
3714 								   &n->is_no_inherit, yyscanner);
3715 					n->initially_valid = !n->skip_validation;
3716 					$$ = (Node *)n;
3717 				}
3718 			| UNIQUE '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3719 				ConstraintAttributeSpec
3720 				{
3721 					Constraint *n = makeNode(Constraint);
3722 					n->contype = CONSTR_UNIQUE;
3723 					n->location = @1;
3724 					n->keys = $3;
3725 					n->including = $5;
3726 					n->options = $6;
3727 					n->indexname = NULL;
3728 					n->indexspace = $7;
3729 					processCASbits($8, @8, "UNIQUE",
3730 								   &n->deferrable, &n->initdeferred, NULL,
3731 								   NULL, yyscanner);
3732 					$$ = (Node *)n;
3733 				}
3734 			| UNIQUE ExistingIndex ConstraintAttributeSpec
3735 				{
3736 					Constraint *n = makeNode(Constraint);
3737 					n->contype = CONSTR_UNIQUE;
3738 					n->location = @1;
3739 					n->keys = NIL;
3740 					n->including = NIL;
3741 					n->options = NIL;
3742 					n->indexname = $2;
3743 					n->indexspace = NULL;
3744 					processCASbits($3, @3, "UNIQUE",
3745 								   &n->deferrable, &n->initdeferred, NULL,
3746 								   NULL, yyscanner);
3747 					$$ = (Node *)n;
3748 				}
3749 			| PRIMARY KEY '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3750 				ConstraintAttributeSpec
3751 				{
3752 					Constraint *n = makeNode(Constraint);
3753 					n->contype = CONSTR_PRIMARY;
3754 					n->location = @1;
3755 					n->keys = $4;
3756 					n->including = $6;
3757 					n->options = $7;
3758 					n->indexname = NULL;
3759 					n->indexspace = $8;
3760 					processCASbits($9, @9, "PRIMARY KEY",
3761 								   &n->deferrable, &n->initdeferred, NULL,
3762 								   NULL, yyscanner);
3763 					$$ = (Node *)n;
3764 				}
3765 			| PRIMARY KEY ExistingIndex ConstraintAttributeSpec
3766 				{
3767 					Constraint *n = makeNode(Constraint);
3768 					n->contype = CONSTR_PRIMARY;
3769 					n->location = @1;
3770 					n->keys = NIL;
3771 					n->including = NIL;
3772 					n->options = NIL;
3773 					n->indexname = $3;
3774 					n->indexspace = NULL;
3775 					processCASbits($4, @4, "PRIMARY KEY",
3776 								   &n->deferrable, &n->initdeferred, NULL,
3777 								   NULL, yyscanner);
3778 					$$ = (Node *)n;
3779 				}
3780 			| EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
3781 				opt_c_include opt_definition OptConsTableSpace  ExclusionWhereClause
3782 				ConstraintAttributeSpec
3783 				{
3784 					Constraint *n = makeNode(Constraint);
3785 					n->contype = CONSTR_EXCLUSION;
3786 					n->location = @1;
3787 					n->access_method	= $2;
3788 					n->exclusions		= $4;
3789 					n->including		= $6;
3790 					n->options			= $7;
3791 					n->indexname		= NULL;
3792 					n->indexspace		= $8;
3793 					n->where_clause		= $9;
3794 					processCASbits($10, @10, "EXCLUDE",
3795 								   &n->deferrable, &n->initdeferred, NULL,
3796 								   NULL, yyscanner);
3797 					$$ = (Node *)n;
3798 				}
3799 			| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
3800 				opt_column_list key_match key_actions ConstraintAttributeSpec
3801 				{
3802 					Constraint *n = makeNode(Constraint);
3803 					n->contype = CONSTR_FOREIGN;
3804 					n->location = @1;
3805 					n->pktable			= $7;
3806 					n->fk_attrs			= $4;
3807 					n->pk_attrs			= $8;
3808 					n->fk_matchtype		= $9;
3809 					n->fk_upd_action	= (char) ($10 >> 8);
3810 					n->fk_del_action	= (char) ($10 & 0xFF);
3811 					processCASbits($11, @11, "FOREIGN KEY",
3812 								   &n->deferrable, &n->initdeferred,
3813 								   &n->skip_validation, NULL,
3814 								   yyscanner);
3815 					n->initially_valid = !n->skip_validation;
3816 					$$ = (Node *)n;
3817 				}
3818 		;
3819 
3820 opt_no_inherit:	NO INHERIT							{  $$ = true; }
3821 			| /* EMPTY */							{  $$ = false; }
3822 		;
3823 
3824 opt_column_list:
3825 			'(' columnList ')'						{ $$ = $2; }
3826 			| /*EMPTY*/								{ $$ = NIL; }
3827 		;
3828 
3829 columnList:
3830 			columnElem								{ $$ = list_make1($1); }
3831 			| columnList ',' columnElem				{ $$ = lappend($1, $3); }
3832 		;
3833 
3834 columnElem: ColId
3835 				{
3836 					$$ = (Node *) makeString($1);
3837 				}
3838 		;
3839 
3840 opt_c_include:	INCLUDE '(' columnList ')'			{ $$ = $3; }
3841 			 |		/* EMPTY */						{ $$ = NIL; }
3842 		;
3843 
3844 key_match:  MATCH FULL
3845 			{
3846 				$$ = FKCONSTR_MATCH_FULL;
3847 			}
3848 		| MATCH PARTIAL
3849 			{
3850 				ereport(ERROR,
3851 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3852 						 errmsg("MATCH PARTIAL not yet implemented"),
3853 						 parser_errposition(@1)));
3854 				$$ = FKCONSTR_MATCH_PARTIAL;
3855 			}
3856 		| MATCH SIMPLE
3857 			{
3858 				$$ = FKCONSTR_MATCH_SIMPLE;
3859 			}
3860 		| /*EMPTY*/
3861 			{
3862 				$$ = FKCONSTR_MATCH_SIMPLE;
3863 			}
3864 		;
3865 
3866 ExclusionConstraintList:
3867 			ExclusionConstraintElem					{ $$ = list_make1($1); }
3868 			| ExclusionConstraintList ',' ExclusionConstraintElem
3869 													{ $$ = lappend($1, $3); }
3870 		;
3871 
3872 ExclusionConstraintElem: index_elem WITH any_operator
3873 			{
3874 				$$ = list_make2($1, $3);
3875 			}
3876 			/* allow OPERATOR() decoration for the benefit of ruleutils.c */
3877 			| index_elem WITH OPERATOR '(' any_operator ')'
3878 			{
3879 				$$ = list_make2($1, $5);
3880 			}
3881 		;
3882 
3883 ExclusionWhereClause:
3884 			WHERE '(' a_expr ')'					{ $$ = $3; }
3885 			| /*EMPTY*/								{ $$ = NULL; }
3886 		;
3887 
3888 /*
3889  * We combine the update and delete actions into one value temporarily
3890  * for simplicity of parsing, and then break them down again in the
3891  * calling production.  update is in the left 8 bits, delete in the right.
3892  * Note that NOACTION is the default.
3893  */
3894 key_actions:
3895 			key_update
3896 				{ $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3897 			| key_delete
3898 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
3899 			| key_update key_delete
3900 				{ $$ = ($1 << 8) | ($2 & 0xFF); }
3901 			| key_delete key_update
3902 				{ $$ = ($2 << 8) | ($1 & 0xFF); }
3903 			| /*EMPTY*/
3904 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3905 		;
3906 
3907 key_update: ON UPDATE key_action		{ $$ = $3; }
3908 		;
3909 
3910 key_delete: ON DELETE_P key_action		{ $$ = $3; }
3911 		;
3912 
3913 key_action:
3914 			NO ACTION					{ $$ = FKCONSTR_ACTION_NOACTION; }
3915 			| RESTRICT					{ $$ = FKCONSTR_ACTION_RESTRICT; }
3916 			| CASCADE					{ $$ = FKCONSTR_ACTION_CASCADE; }
3917 			| SET NULL_P				{ $$ = FKCONSTR_ACTION_SETNULL; }
3918 			| SET DEFAULT				{ $$ = FKCONSTR_ACTION_SETDEFAULT; }
3919 		;
3920 
3921 OptInherit: INHERITS '(' qualified_name_list ')'	{ $$ = $3; }
3922 			| /*EMPTY*/								{ $$ = NIL; }
3923 		;
3924 
3925 /* Optional partition key specification */
3926 OptPartitionSpec: PartitionSpec	{ $$ = $1; }
3927 			| /*EMPTY*/			{ $$ = NULL; }
3928 		;
3929 
3930 PartitionSpec: PARTITION BY part_strategy '(' part_params ')'
3931 				{
3932 					PartitionSpec *n = makeNode(PartitionSpec);
3933 
3934 					n->strategy = $3;
3935 					n->partParams = $5;
3936 					n->location = @1;
3937 
3938 					$$ = n;
3939 				}
3940 		;
3941 
3942 part_strategy:	IDENT					{ $$ = $1; }
3943 				| unreserved_keyword	{ $$ = pstrdup($1); }
3944 		;
3945 
3946 part_params:	part_elem						{ $$ = list_make1($1); }
3947 			| part_params ',' part_elem			{ $$ = lappend($1, $3); }
3948 		;
3949 
3950 part_elem: ColId opt_collate opt_class
3951 				{
3952 					PartitionElem *n = makeNode(PartitionElem);
3953 
3954 					n->name = $1;
3955 					n->expr = NULL;
3956 					n->collation = $2;
3957 					n->opclass = $3;
3958 					n->location = @1;
3959 					$$ = n;
3960 				}
3961 			| func_expr_windowless opt_collate opt_class
3962 				{
3963 					PartitionElem *n = makeNode(PartitionElem);
3964 
3965 					n->name = NULL;
3966 					n->expr = $1;
3967 					n->collation = $2;
3968 					n->opclass = $3;
3969 					n->location = @1;
3970 					$$ = n;
3971 				}
3972 			| '(' a_expr ')' opt_collate opt_class
3973 				{
3974 					PartitionElem *n = makeNode(PartitionElem);
3975 
3976 					n->name = NULL;
3977 					n->expr = $2;
3978 					n->collation = $4;
3979 					n->opclass = $5;
3980 					n->location = @1;
3981 					$$ = n;
3982 				}
3983 		;
3984 
3985 table_access_method_clause:
3986 			USING access_method					{ $$ = $2; }
3987 			| /*EMPTY*/							{ $$ = NULL; }
3988 		;
3989 
3990 /* WITHOUT OIDS is legacy only */
3991 OptWith:
3992 			WITH reloptions				{ $$ = $2; }
3993 			| WITHOUT OIDS				{ $$ = NIL; }
3994 			| /*EMPTY*/					{ $$ = NIL; }
3995 		;
3996 
3997 OnCommitOption:  ON COMMIT DROP				{ $$ = ONCOMMIT_DROP; }
3998 			| ON COMMIT DELETE_P ROWS		{ $$ = ONCOMMIT_DELETE_ROWS; }
3999 			| ON COMMIT PRESERVE ROWS		{ $$ = ONCOMMIT_PRESERVE_ROWS; }
4000 			| /*EMPTY*/						{ $$ = ONCOMMIT_NOOP; }
4001 		;
4002 
4003 OptTableSpace:   TABLESPACE name					{ $$ = $2; }
4004 			| /*EMPTY*/								{ $$ = NULL; }
4005 		;
4006 
4007 OptConsTableSpace:   USING INDEX TABLESPACE name	{ $$ = $4; }
4008 			| /*EMPTY*/								{ $$ = NULL; }
4009 		;
4010 
4011 ExistingIndex:   USING INDEX index_name				{ $$ = $3; }
4012 		;
4013 
4014 /*****************************************************************************
4015  *
4016  *		QUERY :
4017  *				CREATE STATISTICS [IF NOT EXISTS] stats_name [(stat types)]
4018  *					ON expression-list FROM from_list
4019  *
4020  * Note: the expectation here is that the clauses after ON are a subset of
4021  * SELECT syntax, allowing for expressions and joined tables, and probably
4022  * someday a WHERE clause.  Much less than that is currently implemented,
4023  * but the grammar accepts it and then we'll throw FEATURE_NOT_SUPPORTED
4024  * errors as necessary at execution.
4025  *
4026  *****************************************************************************/
4027 
4028 CreateStatsStmt:
4029 			CREATE STATISTICS any_name
4030 			opt_name_list ON expr_list FROM from_list
4031 				{
4032 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
4033 					n->defnames = $3;
4034 					n->stat_types = $4;
4035 					n->exprs = $6;
4036 					n->relations = $8;
4037 					n->stxcomment = NULL;
4038 					n->if_not_exists = false;
4039 					$$ = (Node *)n;
4040 				}
4041 			| CREATE STATISTICS IF_P NOT EXISTS any_name
4042 			opt_name_list ON expr_list FROM from_list
4043 				{
4044 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
4045 					n->defnames = $6;
4046 					n->stat_types = $7;
4047 					n->exprs = $9;
4048 					n->relations = $11;
4049 					n->stxcomment = NULL;
4050 					n->if_not_exists = true;
4051 					$$ = (Node *)n;
4052 				}
4053 			;
4054 
4055 /*****************************************************************************
4056  *
4057  *		QUERY :
4058  *				CREATE TABLE relname AS SelectStmt [ WITH [NO] DATA ]
4059  *
4060  *
4061  * Note: SELECT ... INTO is a now-deprecated alternative for this.
4062  *
4063  *****************************************************************************/
4064 
4065 CreateAsStmt:
4066 		CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
4067 				{
4068 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4069 					ctas->query = $6;
4070 					ctas->into = $4;
4071 					ctas->relkind = OBJECT_TABLE;
4072 					ctas->is_select_into = false;
4073 					ctas->if_not_exists = false;
4074 					/* cram additional flags into the IntoClause */
4075 					$4->rel->relpersistence = $2;
4076 					$4->skipData = !($7);
4077 					$$ = (Node *) ctas;
4078 				}
4079 		| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS SelectStmt opt_with_data
4080 				{
4081 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4082 					ctas->query = $9;
4083 					ctas->into = $7;
4084 					ctas->relkind = OBJECT_TABLE;
4085 					ctas->is_select_into = false;
4086 					ctas->if_not_exists = true;
4087 					/* cram additional flags into the IntoClause */
4088 					$7->rel->relpersistence = $2;
4089 					$7->skipData = !($10);
4090 					$$ = (Node *) ctas;
4091 				}
4092 		;
4093 
4094 create_as_target:
4095 			qualified_name opt_column_list table_access_method_clause
4096 			OptWith OnCommitOption OptTableSpace
4097 				{
4098 					$$ = makeNode(IntoClause);
4099 					$$->rel = $1;
4100 					$$->colNames = $2;
4101 					$$->accessMethod = $3;
4102 					$$->options = $4;
4103 					$$->onCommit = $5;
4104 					$$->tableSpaceName = $6;
4105 					$$->viewQuery = NULL;
4106 					$$->skipData = false;		/* might get changed later */
4107 				}
4108 		;
4109 
4110 opt_with_data:
4111 			WITH DATA_P								{ $$ = true; }
4112 			| WITH NO DATA_P						{ $$ = false; }
4113 			| /*EMPTY*/								{ $$ = true; }
4114 		;
4115 
4116 
4117 /*****************************************************************************
4118  *
4119  *		QUERY :
4120  *				CREATE MATERIALIZED VIEW relname AS SelectStmt
4121  *
4122  *****************************************************************************/
4123 
4124 CreateMatViewStmt:
4125 		CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data
4126 				{
4127 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4128 					ctas->query = $7;
4129 					ctas->into = $5;
4130 					ctas->relkind = OBJECT_MATVIEW;
4131 					ctas->is_select_into = false;
4132 					ctas->if_not_exists = false;
4133 					/* cram additional flags into the IntoClause */
4134 					$5->rel->relpersistence = $2;
4135 					$5->skipData = !($8);
4136 					$$ = (Node *) ctas;
4137 				}
4138 		| CREATE OptNoLog MATERIALIZED VIEW IF_P NOT EXISTS create_mv_target AS SelectStmt opt_with_data
4139 				{
4140 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4141 					ctas->query = $10;
4142 					ctas->into = $8;
4143 					ctas->relkind = OBJECT_MATVIEW;
4144 					ctas->is_select_into = false;
4145 					ctas->if_not_exists = true;
4146 					/* cram additional flags into the IntoClause */
4147 					$8->rel->relpersistence = $2;
4148 					$8->skipData = !($11);
4149 					$$ = (Node *) ctas;
4150 				}
4151 		;
4152 
4153 create_mv_target:
4154 			qualified_name opt_column_list table_access_method_clause opt_reloptions OptTableSpace
4155 				{
4156 					$$ = makeNode(IntoClause);
4157 					$$->rel = $1;
4158 					$$->colNames = $2;
4159 					$$->accessMethod = $3;
4160 					$$->options = $4;
4161 					$$->onCommit = ONCOMMIT_NOOP;
4162 					$$->tableSpaceName = $5;
4163 					$$->viewQuery = NULL;		/* filled at analysis time */
4164 					$$->skipData = false;		/* might get changed later */
4165 				}
4166 		;
4167 
4168 OptNoLog:	UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
4169 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
4170 		;
4171 
4172 
4173 /*****************************************************************************
4174  *
4175  *		QUERY :
4176  *				REFRESH MATERIALIZED VIEW qualified_name
4177  *
4178  *****************************************************************************/
4179 
4180 RefreshMatViewStmt:
4181 			REFRESH MATERIALIZED VIEW opt_concurrently qualified_name opt_with_data
4182 				{
4183 					RefreshMatViewStmt *n = makeNode(RefreshMatViewStmt);
4184 					n->concurrent = $4;
4185 					n->relation = $5;
4186 					n->skipData = !($6);
4187 					$$ = (Node *) n;
4188 				}
4189 		;
4190 
4191 
4192 /*****************************************************************************
4193  *
4194  *		QUERY :
4195  *				CREATE SEQUENCE seqname
4196  *				ALTER SEQUENCE seqname
4197  *
4198  *****************************************************************************/
4199 
4200 CreateSeqStmt:
4201 			CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
4202 				{
4203 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4204 					$4->relpersistence = $2;
4205 					n->sequence = $4;
4206 					n->options = $5;
4207 					n->ownerId = InvalidOid;
4208 					n->if_not_exists = false;
4209 					$$ = (Node *)n;
4210 				}
4211 			| CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
4212 				{
4213 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4214 					$7->relpersistence = $2;
4215 					n->sequence = $7;
4216 					n->options = $8;
4217 					n->ownerId = InvalidOid;
4218 					n->if_not_exists = true;
4219 					$$ = (Node *)n;
4220 				}
4221 		;
4222 
4223 AlterSeqStmt:
4224 			ALTER SEQUENCE qualified_name SeqOptList
4225 				{
4226 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4227 					n->sequence = $3;
4228 					n->options = $4;
4229 					n->missing_ok = false;
4230 					$$ = (Node *)n;
4231 				}
4232 			| ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
4233 				{
4234 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4235 					n->sequence = $5;
4236 					n->options = $6;
4237 					n->missing_ok = true;
4238 					$$ = (Node *)n;
4239 				}
4240 
4241 		;
4242 
4243 OptSeqOptList: SeqOptList							{ $$ = $1; }
4244 			| /*EMPTY*/								{ $$ = NIL; }
4245 		;
4246 
4247 OptParenthesizedSeqOptList: '(' SeqOptList ')'		{ $$ = $2; }
4248 			| /*EMPTY*/								{ $$ = NIL; }
4249 		;
4250 
4251 SeqOptList: SeqOptElem								{ $$ = list_make1($1); }
4252 			| SeqOptList SeqOptElem					{ $$ = lappend($1, $2); }
4253 		;
4254 
4255 SeqOptElem: AS SimpleTypename
4256 				{
4257 					$$ = makeDefElem("as", (Node *)$2, @1);
4258 				}
4259 			| CACHE NumericOnly
4260 				{
4261 					$$ = makeDefElem("cache", (Node *)$2, @1);
4262 				}
4263 			| CYCLE
4264 				{
4265 					$$ = makeDefElem("cycle", (Node *)makeInteger(true), @1);
4266 				}
4267 			| NO CYCLE
4268 				{
4269 					$$ = makeDefElem("cycle", (Node *)makeInteger(false), @1);
4270 				}
4271 			| INCREMENT opt_by NumericOnly
4272 				{
4273 					$$ = makeDefElem("increment", (Node *)$3, @1);
4274 				}
4275 			| MAXVALUE NumericOnly
4276 				{
4277 					$$ = makeDefElem("maxvalue", (Node *)$2, @1);
4278 				}
4279 			| MINVALUE NumericOnly
4280 				{
4281 					$$ = makeDefElem("minvalue", (Node *)$2, @1);
4282 				}
4283 			| NO MAXVALUE
4284 				{
4285 					$$ = makeDefElem("maxvalue", NULL, @1);
4286 				}
4287 			| NO MINVALUE
4288 				{
4289 					$$ = makeDefElem("minvalue", NULL, @1);
4290 				}
4291 			| OWNED BY any_name
4292 				{
4293 					$$ = makeDefElem("owned_by", (Node *)$3, @1);
4294 				}
4295 			| SEQUENCE NAME_P any_name
4296 				{
4297 					/* not documented, only used by pg_dump */
4298 					$$ = makeDefElem("sequence_name", (Node *)$3, @1);
4299 				}
4300 			| START opt_with NumericOnly
4301 				{
4302 					$$ = makeDefElem("start", (Node *)$3, @1);
4303 				}
4304 			| RESTART
4305 				{
4306 					$$ = makeDefElem("restart", NULL, @1);
4307 				}
4308 			| RESTART opt_with NumericOnly
4309 				{
4310 					$$ = makeDefElem("restart", (Node *)$3, @1);
4311 				}
4312 		;
4313 
4314 opt_by:		BY				{}
4315 			| /* empty */	{}
4316 	  ;
4317 
4318 NumericOnly:
4319 			FCONST								{ $$ = makeFloat($1); }
4320 			| '+' FCONST						{ $$ = makeFloat($2); }
4321 			| '-' FCONST
4322 				{
4323 					$$ = makeFloat($2);
4324 					doNegateFloat($$);
4325 				}
4326 			| SignedIconst						{ $$ = makeInteger($1); }
4327 		;
4328 
4329 NumericOnly_list:	NumericOnly						{ $$ = list_make1($1); }
4330 				| NumericOnly_list ',' NumericOnly	{ $$ = lappend($1, $3); }
4331 		;
4332 
4333 /*****************************************************************************
4334  *
4335  *		QUERIES :
4336  *				CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
4337  *				DROP [PROCEDURAL] LANGUAGE ...
4338  *
4339  *****************************************************************************/
4340 
4341 CreatePLangStmt:
4342 			CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4343 			{
4344 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4345 				n->replace = $2;
4346 				n->plname = $6;
4347 				/* parameters are all to be supplied by system */
4348 				n->plhandler = NIL;
4349 				n->plinline = NIL;
4350 				n->plvalidator = NIL;
4351 				n->pltrusted = false;
4352 				$$ = (Node *)n;
4353 			}
4354 			| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4355 			  HANDLER handler_name opt_inline_handler opt_validator
4356 			{
4357 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4358 				n->replace = $2;
4359 				n->plname = $6;
4360 				n->plhandler = $8;
4361 				n->plinline = $9;
4362 				n->plvalidator = $10;
4363 				n->pltrusted = $3;
4364 				$$ = (Node *)n;
4365 			}
4366 		;
4367 
4368 opt_trusted:
4369 			TRUSTED									{ $$ = true; }
4370 			| /*EMPTY*/								{ $$ = false; }
4371 		;
4372 
4373 /* This ought to be just func_name, but that causes reduce/reduce conflicts
4374  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
4375  * Work around by using simple names, instead.
4376  */
4377 handler_name:
4378 			name						{ $$ = list_make1(makeString($1)); }
4379 			| name attrs				{ $$ = lcons(makeString($1), $2); }
4380 		;
4381 
4382 opt_inline_handler:
4383 			INLINE_P handler_name					{ $$ = $2; }
4384 			| /*EMPTY*/								{ $$ = NIL; }
4385 		;
4386 
4387 validator_clause:
4388 			VALIDATOR handler_name					{ $$ = $2; }
4389 			| NO VALIDATOR							{ $$ = NIL; }
4390 		;
4391 
4392 opt_validator:
4393 			validator_clause						{ $$ = $1; }
4394 			| /*EMPTY*/								{ $$ = NIL; }
4395 		;
4396 
4397 DropPLangStmt:
4398 			DROP opt_procedural LANGUAGE NonReservedWord_or_Sconst opt_drop_behavior
4399 				{
4400 					DropStmt *n = makeNode(DropStmt);
4401 					n->removeType = OBJECT_LANGUAGE;
4402 					n->objects = list_make1(makeString($4));
4403 					n->behavior = $5;
4404 					n->missing_ok = false;
4405 					n->concurrent = false;
4406 					$$ = (Node *)n;
4407 				}
4408 			| DROP opt_procedural LANGUAGE IF_P EXISTS NonReservedWord_or_Sconst opt_drop_behavior
4409 				{
4410 					DropStmt *n = makeNode(DropStmt);
4411 					n->removeType = OBJECT_LANGUAGE;
4412 					n->objects = list_make1(makeString($6));
4413 					n->behavior = $7;
4414 					n->missing_ok = true;
4415 					n->concurrent = false;
4416 					$$ = (Node *)n;
4417 				}
4418 		;
4419 
4420 opt_procedural:
4421 			PROCEDURAL								{}
4422 			| /*EMPTY*/								{}
4423 		;
4424 
4425 /*****************************************************************************
4426  *
4427  *		QUERY:
4428  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
4429  *
4430  *****************************************************************************/
4431 
4432 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst opt_reloptions
4433 				{
4434 					CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
4435 					n->tablespacename = $3;
4436 					n->owner = $4;
4437 					n->location = $6;
4438 					n->options = $7;
4439 					$$ = (Node *) n;
4440 				}
4441 		;
4442 
4443 OptTableSpaceOwner: OWNER RoleSpec		{ $$ = $2; }
4444 			| /*EMPTY */				{ $$ = NULL; }
4445 		;
4446 
4447 /*****************************************************************************
4448  *
4449  *		QUERY :
4450  *				DROP TABLESPACE <tablespace>
4451  *
4452  *		No need for drop behaviour as we cannot implement dependencies for
4453  *		objects in other databases; we can only support RESTRICT.
4454  *
4455  ****************************************************************************/
4456 
4457 DropTableSpaceStmt: DROP TABLESPACE name
4458 				{
4459 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4460 					n->tablespacename = $3;
4461 					n->missing_ok = false;
4462 					$$ = (Node *) n;
4463 				}
4464 				|  DROP TABLESPACE IF_P EXISTS name
4465 				{
4466 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4467 					n->tablespacename = $5;
4468 					n->missing_ok = true;
4469 					$$ = (Node *) n;
4470 				}
4471 		;
4472 
4473 /*****************************************************************************
4474  *
4475  *		QUERY:
4476  *             CREATE EXTENSION extension
4477  *             [ WITH ] [ SCHEMA schema ] [ VERSION version ] [ FROM oldversion ]
4478  *
4479  *****************************************************************************/
4480 
4481 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
4482 				{
4483 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4484 					n->extname = $3;
4485 					n->if_not_exists = false;
4486 					n->options = $5;
4487 					$$ = (Node *) n;
4488 				}
4489 				| CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
4490 				{
4491 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4492 					n->extname = $6;
4493 					n->if_not_exists = true;
4494 					n->options = $8;
4495 					$$ = (Node *) n;
4496 				}
4497 		;
4498 
4499 create_extension_opt_list:
4500 			create_extension_opt_list create_extension_opt_item
4501 				{ $$ = lappend($1, $2); }
4502 			| /* EMPTY */
4503 				{ $$ = NIL; }
4504 		;
4505 
4506 create_extension_opt_item:
4507 			SCHEMA name
4508 				{
4509 					$$ = makeDefElem("schema", (Node *)makeString($2), @1);
4510 				}
4511 			| VERSION_P NonReservedWord_or_Sconst
4512 				{
4513 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4514 				}
4515 			| FROM NonReservedWord_or_Sconst
4516 				{
4517 					$$ = makeDefElem("old_version", (Node *)makeString($2), @1);
4518 				}
4519 			| CASCADE
4520 				{
4521 					$$ = makeDefElem("cascade", (Node *)makeInteger(true), @1);
4522 				}
4523 		;
4524 
4525 /*****************************************************************************
4526  *
4527  * ALTER EXTENSION name UPDATE [ TO version ]
4528  *
4529  *****************************************************************************/
4530 
4531 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
4532 				{
4533 					AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
4534 					n->extname = $3;
4535 					n->options = $5;
4536 					$$ = (Node *) n;
4537 				}
4538 		;
4539 
4540 alter_extension_opt_list:
4541 			alter_extension_opt_list alter_extension_opt_item
4542 				{ $$ = lappend($1, $2); }
4543 			| /* EMPTY */
4544 				{ $$ = NIL; }
4545 		;
4546 
4547 alter_extension_opt_item:
4548 			TO NonReservedWord_or_Sconst
4549 				{
4550 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4551 				}
4552 		;
4553 
4554 /*****************************************************************************
4555  *
4556  * ALTER EXTENSION name ADD/DROP object-identifier
4557  *
4558  *****************************************************************************/
4559 
4560 AlterExtensionContentsStmt:
4561 			ALTER EXTENSION name add_drop ACCESS METHOD name
4562 				{
4563 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4564 					n->extname = $3;
4565 					n->action = $4;
4566 					n->objtype = OBJECT_ACCESS_METHOD;
4567 					n->object = (Node *) makeString($7);
4568 					$$ = (Node *)n;
4569 				}
4570 			| ALTER EXTENSION name add_drop AGGREGATE aggregate_with_argtypes
4571 				{
4572 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4573 					n->extname = $3;
4574 					n->action = $4;
4575 					n->objtype = OBJECT_AGGREGATE;
4576 					n->object = (Node *) $6;
4577 					$$ = (Node *)n;
4578 				}
4579 			| ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
4580 				{
4581 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4582 					n->extname = $3;
4583 					n->action = $4;
4584 					n->objtype = OBJECT_CAST;
4585 					n->object = (Node *) list_make2($7, $9);
4586 					$$ = (Node *) n;
4587 				}
4588 			| ALTER EXTENSION name add_drop COLLATION any_name
4589 				{
4590 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4591 					n->extname = $3;
4592 					n->action = $4;
4593 					n->objtype = OBJECT_COLLATION;
4594 					n->object = (Node *) $6;
4595 					$$ = (Node *)n;
4596 				}
4597 			| ALTER EXTENSION name add_drop CONVERSION_P any_name
4598 				{
4599 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4600 					n->extname = $3;
4601 					n->action = $4;
4602 					n->objtype = OBJECT_CONVERSION;
4603 					n->object = (Node *) $6;
4604 					$$ = (Node *)n;
4605 				}
4606 			| ALTER EXTENSION name add_drop DOMAIN_P Typename
4607 				{
4608 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4609 					n->extname = $3;
4610 					n->action = $4;
4611 					n->objtype = OBJECT_DOMAIN;
4612 					n->object = (Node *) $6;
4613 					$$ = (Node *)n;
4614 				}
4615 			| ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
4616 				{
4617 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4618 					n->extname = $3;
4619 					n->action = $4;
4620 					n->objtype = OBJECT_FUNCTION;
4621 					n->object = (Node *) $6;
4622 					$$ = (Node *)n;
4623 				}
4624 			| ALTER EXTENSION name add_drop opt_procedural LANGUAGE name
4625 				{
4626 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4627 					n->extname = $3;
4628 					n->action = $4;
4629 					n->objtype = OBJECT_LANGUAGE;
4630 					n->object = (Node *) makeString($7);
4631 					$$ = (Node *)n;
4632 				}
4633 			| ALTER EXTENSION name add_drop OPERATOR operator_with_argtypes
4634 				{
4635 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4636 					n->extname = $3;
4637 					n->action = $4;
4638 					n->objtype = OBJECT_OPERATOR;
4639 					n->object = (Node *) $6;
4640 					$$ = (Node *)n;
4641 				}
4642 			| ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING access_method
4643 				{
4644 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4645 					n->extname = $3;
4646 					n->action = $4;
4647 					n->objtype = OBJECT_OPCLASS;
4648 					n->object = (Node *) lcons(makeString($9), $7);
4649 					$$ = (Node *)n;
4650 				}
4651 			| ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING access_method
4652 				{
4653 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4654 					n->extname = $3;
4655 					n->action = $4;
4656 					n->objtype = OBJECT_OPFAMILY;
4657 					n->object = (Node *) lcons(makeString($9), $7);
4658 					$$ = (Node *)n;
4659 				}
4660 			| ALTER EXTENSION name add_drop PROCEDURE function_with_argtypes
4661 				{
4662 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4663 					n->extname = $3;
4664 					n->action = $4;
4665 					n->objtype = OBJECT_PROCEDURE;
4666 					n->object = (Node *) $6;
4667 					$$ = (Node *)n;
4668 				}
4669 			| ALTER EXTENSION name add_drop ROUTINE function_with_argtypes
4670 				{
4671 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4672 					n->extname = $3;
4673 					n->action = $4;
4674 					n->objtype = OBJECT_ROUTINE;
4675 					n->object = (Node *) $6;
4676 					$$ = (Node *)n;
4677 				}
4678 			| ALTER EXTENSION name add_drop SCHEMA name
4679 				{
4680 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4681 					n->extname = $3;
4682 					n->action = $4;
4683 					n->objtype = OBJECT_SCHEMA;
4684 					n->object = (Node *) makeString($6);
4685 					$$ = (Node *)n;
4686 				}
4687 			| ALTER EXTENSION name add_drop EVENT TRIGGER name
4688 				{
4689 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4690 					n->extname = $3;
4691 					n->action = $4;
4692 					n->objtype = OBJECT_EVENT_TRIGGER;
4693 					n->object = (Node *) makeString($7);
4694 					$$ = (Node *)n;
4695 				}
4696 			| ALTER EXTENSION name add_drop TABLE any_name
4697 				{
4698 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4699 					n->extname = $3;
4700 					n->action = $4;
4701 					n->objtype = OBJECT_TABLE;
4702 					n->object = (Node *) $6;
4703 					$$ = (Node *)n;
4704 				}
4705 			| ALTER EXTENSION name add_drop TEXT_P SEARCH PARSER any_name
4706 				{
4707 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4708 					n->extname = $3;
4709 					n->action = $4;
4710 					n->objtype = OBJECT_TSPARSER;
4711 					n->object = (Node *) $8;
4712 					$$ = (Node *)n;
4713 				}
4714 			| ALTER EXTENSION name add_drop TEXT_P SEARCH DICTIONARY any_name
4715 				{
4716 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4717 					n->extname = $3;
4718 					n->action = $4;
4719 					n->objtype = OBJECT_TSDICTIONARY;
4720 					n->object = (Node *) $8;
4721 					$$ = (Node *)n;
4722 				}
4723 			| ALTER EXTENSION name add_drop TEXT_P SEARCH TEMPLATE any_name
4724 				{
4725 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4726 					n->extname = $3;
4727 					n->action = $4;
4728 					n->objtype = OBJECT_TSTEMPLATE;
4729 					n->object = (Node *) $8;
4730 					$$ = (Node *)n;
4731 				}
4732 			| ALTER EXTENSION name add_drop TEXT_P SEARCH CONFIGURATION any_name
4733 				{
4734 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4735 					n->extname = $3;
4736 					n->action = $4;
4737 					n->objtype = OBJECT_TSCONFIGURATION;
4738 					n->object = (Node *) $8;
4739 					$$ = (Node *)n;
4740 				}
4741 			| ALTER EXTENSION name add_drop SEQUENCE any_name
4742 				{
4743 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4744 					n->extname = $3;
4745 					n->action = $4;
4746 					n->objtype = OBJECT_SEQUENCE;
4747 					n->object = (Node *) $6;
4748 					$$ = (Node *)n;
4749 				}
4750 			| ALTER EXTENSION name add_drop VIEW any_name
4751 				{
4752 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4753 					n->extname = $3;
4754 					n->action = $4;
4755 					n->objtype = OBJECT_VIEW;
4756 					n->object = (Node *) $6;
4757 					$$ = (Node *)n;
4758 				}
4759 			| ALTER EXTENSION name add_drop MATERIALIZED VIEW any_name
4760 				{
4761 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4762 					n->extname = $3;
4763 					n->action = $4;
4764 					n->objtype = OBJECT_MATVIEW;
4765 					n->object = (Node *) $7;
4766 					$$ = (Node *)n;
4767 				}
4768 			| ALTER EXTENSION name add_drop FOREIGN TABLE any_name
4769 				{
4770 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4771 					n->extname = $3;
4772 					n->action = $4;
4773 					n->objtype = OBJECT_FOREIGN_TABLE;
4774 					n->object = (Node *) $7;
4775 					$$ = (Node *)n;
4776 				}
4777 			| ALTER EXTENSION name add_drop FOREIGN DATA_P WRAPPER name
4778 				{
4779 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4780 					n->extname = $3;
4781 					n->action = $4;
4782 					n->objtype = OBJECT_FDW;
4783 					n->object = (Node *) makeString($8);
4784 					$$ = (Node *)n;
4785 				}
4786 			| ALTER EXTENSION name add_drop SERVER name
4787 				{
4788 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4789 					n->extname = $3;
4790 					n->action = $4;
4791 					n->objtype = OBJECT_FOREIGN_SERVER;
4792 					n->object = (Node *) makeString($6);
4793 					$$ = (Node *)n;
4794 				}
4795 			| ALTER EXTENSION name add_drop TRANSFORM FOR Typename LANGUAGE name
4796 				{
4797 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4798 					n->extname = $3;
4799 					n->action = $4;
4800 					n->objtype = OBJECT_TRANSFORM;
4801 					n->object = (Node *) list_make2($7, makeString($9));
4802 					$$ = (Node *)n;
4803 				}
4804 			| ALTER EXTENSION name add_drop TYPE_P Typename
4805 				{
4806 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4807 					n->extname = $3;
4808 					n->action = $4;
4809 					n->objtype = OBJECT_TYPE;
4810 					n->object = (Node *) $6;
4811 					$$ = (Node *)n;
4812 				}
4813 		;
4814 
4815 /*****************************************************************************
4816  *
4817  *		QUERY:
4818  *             CREATE FOREIGN DATA WRAPPER name options
4819  *
4820  *****************************************************************************/
4821 
4822 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
4823 				{
4824 					CreateFdwStmt *n = makeNode(CreateFdwStmt);
4825 					n->fdwname = $5;
4826 					n->func_options = $6;
4827 					n->options = $7;
4828 					$$ = (Node *) n;
4829 				}
4830 		;
4831 
4832 fdw_option:
4833 			HANDLER handler_name				{ $$ = makeDefElem("handler", (Node *)$2, @1); }
4834 			| NO HANDLER						{ $$ = makeDefElem("handler", NULL, @1); }
4835 			| VALIDATOR handler_name			{ $$ = makeDefElem("validator", (Node *)$2, @1); }
4836 			| NO VALIDATOR						{ $$ = makeDefElem("validator", NULL, @1); }
4837 		;
4838 
4839 fdw_options:
4840 			fdw_option							{ $$ = list_make1($1); }
4841 			| fdw_options fdw_option			{ $$ = lappend($1, $2); }
4842 		;
4843 
4844 opt_fdw_options:
4845 			fdw_options							{ $$ = $1; }
4846 			| /*EMPTY*/							{ $$ = NIL; }
4847 		;
4848 
4849 /*****************************************************************************
4850  *
4851  *		QUERY :
4852  *				ALTER FOREIGN DATA WRAPPER name options
4853  *
4854  ****************************************************************************/
4855 
4856 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
4857 				{
4858 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4859 					n->fdwname = $5;
4860 					n->func_options = $6;
4861 					n->options = $7;
4862 					$$ = (Node *) n;
4863 				}
4864 			| ALTER FOREIGN DATA_P WRAPPER name fdw_options
4865 				{
4866 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4867 					n->fdwname = $5;
4868 					n->func_options = $6;
4869 					n->options = NIL;
4870 					$$ = (Node *) n;
4871 				}
4872 		;
4873 
4874 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
4875 create_generic_options:
4876 			OPTIONS '(' generic_option_list ')'			{ $$ = $3; }
4877 			| /*EMPTY*/									{ $$ = NIL; }
4878 		;
4879 
4880 generic_option_list:
4881 			generic_option_elem
4882 				{
4883 					$$ = list_make1($1);
4884 				}
4885 			| generic_option_list ',' generic_option_elem
4886 				{
4887 					$$ = lappend($1, $3);
4888 				}
4889 		;
4890 
4891 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
4892 alter_generic_options:
4893 			OPTIONS	'(' alter_generic_option_list ')'		{ $$ = $3; }
4894 		;
4895 
4896 alter_generic_option_list:
4897 			alter_generic_option_elem
4898 				{
4899 					$$ = list_make1($1);
4900 				}
4901 			| alter_generic_option_list ',' alter_generic_option_elem
4902 				{
4903 					$$ = lappend($1, $3);
4904 				}
4905 		;
4906 
4907 alter_generic_option_elem:
4908 			generic_option_elem
4909 				{
4910 					$$ = $1;
4911 				}
4912 			| SET generic_option_elem
4913 				{
4914 					$$ = $2;
4915 					$$->defaction = DEFELEM_SET;
4916 				}
4917 			| ADD_P generic_option_elem
4918 				{
4919 					$$ = $2;
4920 					$$->defaction = DEFELEM_ADD;
4921 				}
4922 			| DROP generic_option_name
4923 				{
4924 					$$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP, @2);
4925 				}
4926 		;
4927 
4928 generic_option_elem:
4929 			generic_option_name generic_option_arg
4930 				{
4931 					$$ = makeDefElem($1, $2, @1);
4932 				}
4933 		;
4934 
4935 generic_option_name:
4936 				ColLabel			{ $$ = $1; }
4937 		;
4938 
4939 /* We could use def_arg here, but the spec only requires string literals */
4940 generic_option_arg:
4941 				Sconst				{ $$ = (Node *) makeString($1); }
4942 		;
4943 
4944 /*****************************************************************************
4945  *
4946  *		QUERY:
4947  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
4948  *
4949  *****************************************************************************/
4950 
4951 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
4952 						 FOREIGN DATA_P WRAPPER name create_generic_options
4953 				{
4954 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4955 					n->servername = $3;
4956 					n->servertype = $4;
4957 					n->version = $5;
4958 					n->fdwname = $9;
4959 					n->options = $10;
4960 					n->if_not_exists = false;
4961 					$$ = (Node *) n;
4962 				}
4963 				| CREATE SERVER IF_P NOT EXISTS name opt_type opt_foreign_server_version
4964 						 FOREIGN DATA_P WRAPPER name create_generic_options
4965 				{
4966 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4967 					n->servername = $6;
4968 					n->servertype = $7;
4969 					n->version = $8;
4970 					n->fdwname = $12;
4971 					n->options = $13;
4972 					n->if_not_exists = true;
4973 					$$ = (Node *) n;
4974 				}
4975 		;
4976 
4977 opt_type:
4978 			TYPE_P Sconst			{ $$ = $2; }
4979 			| /*EMPTY*/				{ $$ = NULL; }
4980 		;
4981 
4982 
4983 foreign_server_version:
4984 			VERSION_P Sconst		{ $$ = $2; }
4985 		|	VERSION_P NULL_P		{ $$ = NULL; }
4986 		;
4987 
4988 opt_foreign_server_version:
4989 			foreign_server_version	{ $$ = $1; }
4990 			| /*EMPTY*/				{ $$ = NULL; }
4991 		;
4992 
4993 /*****************************************************************************
4994  *
4995  *		QUERY :
4996  *				ALTER SERVER name [VERSION] [OPTIONS]
4997  *
4998  ****************************************************************************/
4999 
5000 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
5001 				{
5002 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5003 					n->servername = $3;
5004 					n->version = $4;
5005 					n->options = $5;
5006 					n->has_version = true;
5007 					$$ = (Node *) n;
5008 				}
5009 			| ALTER SERVER name foreign_server_version
5010 				{
5011 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5012 					n->servername = $3;
5013 					n->version = $4;
5014 					n->has_version = true;
5015 					$$ = (Node *) n;
5016 				}
5017 			| ALTER SERVER name alter_generic_options
5018 				{
5019 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5020 					n->servername = $3;
5021 					n->options = $4;
5022 					$$ = (Node *) n;
5023 				}
5024 		;
5025 
5026 /*****************************************************************************
5027  *
5028  *		QUERY:
5029  *             CREATE FOREIGN TABLE relname (...) SERVER name (...)
5030  *
5031  *****************************************************************************/
5032 
5033 CreateForeignTableStmt:
5034 		CREATE FOREIGN TABLE qualified_name
5035 			'(' OptTableElementList ')'
5036 			OptInherit SERVER name create_generic_options
5037 				{
5038 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5039 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
5040 					n->base.relation = $4;
5041 					n->base.tableElts = $6;
5042 					n->base.inhRelations = $8;
5043 					n->base.ofTypename = NULL;
5044 					n->base.constraints = NIL;
5045 					n->base.options = NIL;
5046 					n->base.oncommit = ONCOMMIT_NOOP;
5047 					n->base.tablespacename = NULL;
5048 					n->base.if_not_exists = false;
5049 					/* FDW-specific data */
5050 					n->servername = $10;
5051 					n->options = $11;
5052 					$$ = (Node *) n;
5053 				}
5054 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5055 			'(' OptTableElementList ')'
5056 			OptInherit SERVER name create_generic_options
5057 				{
5058 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5059 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
5060 					n->base.relation = $7;
5061 					n->base.tableElts = $9;
5062 					n->base.inhRelations = $11;
5063 					n->base.ofTypename = NULL;
5064 					n->base.constraints = NIL;
5065 					n->base.options = NIL;
5066 					n->base.oncommit = ONCOMMIT_NOOP;
5067 					n->base.tablespacename = NULL;
5068 					n->base.if_not_exists = true;
5069 					/* FDW-specific data */
5070 					n->servername = $13;
5071 					n->options = $14;
5072 					$$ = (Node *) n;
5073 				}
5074 		| CREATE FOREIGN TABLE qualified_name
5075 			PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5076 			SERVER name create_generic_options
5077 				{
5078 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5079 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
5080 					n->base.relation = $4;
5081 					n->base.inhRelations = list_make1($7);
5082 					n->base.tableElts = $8;
5083 					n->base.partbound = $9;
5084 					n->base.ofTypename = NULL;
5085 					n->base.constraints = NIL;
5086 					n->base.options = NIL;
5087 					n->base.oncommit = ONCOMMIT_NOOP;
5088 					n->base.tablespacename = NULL;
5089 					n->base.if_not_exists = false;
5090 					/* FDW-specific data */
5091 					n->servername = $11;
5092 					n->options = $12;
5093 					$$ = (Node *) n;
5094 				}
5095 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5096 			PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5097 			SERVER name create_generic_options
5098 				{
5099 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5100 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
5101 					n->base.relation = $7;
5102 					n->base.inhRelations = list_make1($10);
5103 					n->base.tableElts = $11;
5104 					n->base.partbound = $12;
5105 					n->base.ofTypename = NULL;
5106 					n->base.constraints = NIL;
5107 					n->base.options = NIL;
5108 					n->base.oncommit = ONCOMMIT_NOOP;
5109 					n->base.tablespacename = NULL;
5110 					n->base.if_not_exists = true;
5111 					/* FDW-specific data */
5112 					n->servername = $14;
5113 					n->options = $15;
5114 					$$ = (Node *) n;
5115 				}
5116 		;
5117 
5118 /*****************************************************************************
5119  *
5120  *		QUERY:
5121  *             ALTER FOREIGN TABLE relname [...]
5122  *
5123  *****************************************************************************/
5124 
5125 AlterForeignTableStmt:
5126 			ALTER FOREIGN TABLE relation_expr alter_table_cmds
5127 				{
5128 					AlterTableStmt *n = makeNode(AlterTableStmt);
5129 					n->relation = $4;
5130 					n->cmds = $5;
5131 					n->relkind = OBJECT_FOREIGN_TABLE;
5132 					n->missing_ok = false;
5133 					$$ = (Node *)n;
5134 				}
5135 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
5136 				{
5137 					AlterTableStmt *n = makeNode(AlterTableStmt);
5138 					n->relation = $6;
5139 					n->cmds = $7;
5140 					n->relkind = OBJECT_FOREIGN_TABLE;
5141 					n->missing_ok = true;
5142 					$$ = (Node *)n;
5143 				}
5144 		;
5145 
5146 /*****************************************************************************
5147  *
5148  *		QUERY:
5149  *				IMPORT FOREIGN SCHEMA remote_schema
5150  *				[ { LIMIT TO | EXCEPT } ( table_list ) ]
5151  *				FROM SERVER server_name INTO local_schema [ OPTIONS (...) ]
5152  *
5153  ****************************************************************************/
5154 
5155 ImportForeignSchemaStmt:
5156 		IMPORT_P FOREIGN SCHEMA name import_qualification
5157 		  FROM SERVER name INTO name create_generic_options
5158 			{
5159 				ImportForeignSchemaStmt *n = makeNode(ImportForeignSchemaStmt);
5160 				n->server_name = $8;
5161 				n->remote_schema = $4;
5162 				n->local_schema = $10;
5163 				n->list_type = $5->type;
5164 				n->table_list = $5->table_names;
5165 				n->options = $11;
5166 				$$ = (Node *) n;
5167 			}
5168 		;
5169 
5170 import_qualification_type:
5171 		LIMIT TO 				{ $$ = FDW_IMPORT_SCHEMA_LIMIT_TO; }
5172 		| EXCEPT 				{ $$ = FDW_IMPORT_SCHEMA_EXCEPT; }
5173 		;
5174 
5175 import_qualification:
5176 		import_qualification_type '(' relation_expr_list ')'
5177 			{
5178 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5179 				n->type = $1;
5180 				n->table_names = $3;
5181 				$$ = n;
5182 			}
5183 		| /*EMPTY*/
5184 			{
5185 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5186 				n->type = FDW_IMPORT_SCHEMA_ALL;
5187 				n->table_names = NIL;
5188 				$$ = n;
5189 			}
5190 		;
5191 
5192 /*****************************************************************************
5193  *
5194  *		QUERY:
5195  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
5196  *
5197  *****************************************************************************/
5198 
5199 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
5200 				{
5201 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5202 					n->user = $5;
5203 					n->servername = $7;
5204 					n->options = $8;
5205 					n->if_not_exists = false;
5206 					$$ = (Node *) n;
5207 				}
5208 				| CREATE USER MAPPING IF_P NOT EXISTS FOR auth_ident SERVER name create_generic_options
5209 				{
5210 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5211 					n->user = $8;
5212 					n->servername = $10;
5213 					n->options = $11;
5214 					n->if_not_exists = true;
5215 					$$ = (Node *) n;
5216 				}
5217 		;
5218 
5219 /* User mapping authorization identifier */
5220 auth_ident: RoleSpec			{ $$ = $1; }
5221 			| USER				{ $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1); }
5222 		;
5223 
5224 /*****************************************************************************
5225  *
5226  *		QUERY :
5227  *				DROP USER MAPPING FOR auth_ident SERVER name
5228  *
5229  * XXX you'd think this should have a CASCADE/RESTRICT option, even if it's
5230  * only pro forma; but the SQL standard doesn't show one.
5231  ****************************************************************************/
5232 
5233 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
5234 				{
5235 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5236 					n->user = $5;
5237 					n->servername = $7;
5238 					n->missing_ok = false;
5239 					$$ = (Node *) n;
5240 				}
5241 				|  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
5242 				{
5243 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5244 					n->user = $7;
5245 					n->servername = $9;
5246 					n->missing_ok = true;
5247 					$$ = (Node *) n;
5248 				}
5249 		;
5250 
5251 /*****************************************************************************
5252  *
5253  *		QUERY :
5254  *				ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
5255  *
5256  ****************************************************************************/
5257 
5258 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
5259 				{
5260 					AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
5261 					n->user = $5;
5262 					n->servername = $7;
5263 					n->options = $8;
5264 					$$ = (Node *) n;
5265 				}
5266 		;
5267 
5268 /*****************************************************************************
5269  *
5270  *		QUERIES:
5271  *				CREATE POLICY name ON table
5272  *					[AS { PERMISSIVE | RESTRICTIVE } ]
5273  *					[FOR { SELECT | INSERT | UPDATE | DELETE } ]
5274  *					[TO role, ...]
5275  *					[USING (qual)] [WITH CHECK (with check qual)]
5276  *				ALTER POLICY name ON table [TO role, ...]
5277  *					[USING (qual)] [WITH CHECK (with check qual)]
5278  *
5279  *****************************************************************************/
5280 
5281 CreatePolicyStmt:
5282 			CREATE POLICY name ON qualified_name RowSecurityDefaultPermissive
5283 				RowSecurityDefaultForCmd RowSecurityDefaultToRole
5284 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5285 				{
5286 					CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
5287 					n->policy_name = $3;
5288 					n->table = $5;
5289 					n->permissive = $6;
5290 					n->cmd_name = $7;
5291 					n->roles = $8;
5292 					n->qual = $9;
5293 					n->with_check = $10;
5294 					$$ = (Node *) n;
5295 				}
5296 		;
5297 
5298 AlterPolicyStmt:
5299 			ALTER POLICY name ON qualified_name RowSecurityOptionalToRole
5300 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5301 				{
5302 					AlterPolicyStmt *n = makeNode(AlterPolicyStmt);
5303 					n->policy_name = $3;
5304 					n->table = $5;
5305 					n->roles = $6;
5306 					n->qual = $7;
5307 					n->with_check = $8;
5308 					$$ = (Node *) n;
5309 				}
5310 		;
5311 
5312 RowSecurityOptionalExpr:
5313 			USING '(' a_expr ')'	{ $$ = $3; }
5314 			| /* EMPTY */			{ $$ = NULL; }
5315 		;
5316 
5317 RowSecurityOptionalWithCheck:
5318 			WITH CHECK '(' a_expr ')'		{ $$ = $4; }
5319 			| /* EMPTY */					{ $$ = NULL; }
5320 		;
5321 
5322 RowSecurityDefaultToRole:
5323 			TO role_list			{ $$ = $2; }
5324 			| /* EMPTY */			{ $$ = list_make1(makeRoleSpec(ROLESPEC_PUBLIC, -1)); }
5325 		;
5326 
5327 RowSecurityOptionalToRole:
5328 			TO role_list			{ $$ = $2; }
5329 			| /* EMPTY */			{ $$ = NULL; }
5330 		;
5331 
5332 RowSecurityDefaultPermissive:
5333 			AS IDENT
5334 				{
5335 					if (strcmp($2, "permissive") == 0)
5336 						$$ = true;
5337 					else if (strcmp($2, "restrictive") == 0)
5338 						$$ = false;
5339 					else
5340 						ereport(ERROR,
5341 								(errcode(ERRCODE_SYNTAX_ERROR),
5342 							 errmsg("unrecognized row security option \"%s\"", $2),
5343 								 errhint("Only PERMISSIVE or RESTRICTIVE policies are supported currently."),
5344 									 parser_errposition(@2)));
5345 
5346 				}
5347 			| /* EMPTY */			{ $$ = true; }
5348 		;
5349 
5350 RowSecurityDefaultForCmd:
5351 			FOR row_security_cmd	{ $$ = $2; }
5352 			| /* EMPTY */			{ $$ = "all"; }
5353 		;
5354 
5355 row_security_cmd:
5356 			ALL				{ $$ = "all"; }
5357 		|	SELECT			{ $$ = "select"; }
5358 		|	INSERT			{ $$ = "insert"; }
5359 		|	UPDATE			{ $$ = "update"; }
5360 		|	DELETE_P		{ $$ = "delete"; }
5361 		;
5362 
5363 /*****************************************************************************
5364  *
5365  *		QUERY:
5366  *             CREATE ACCESS METHOD name HANDLER handler_name
5367  *
5368  *****************************************************************************/
5369 
5370 CreateAmStmt: CREATE ACCESS METHOD name TYPE_P am_type HANDLER handler_name
5371 				{
5372 					CreateAmStmt *n = makeNode(CreateAmStmt);
5373 					n->amname = $4;
5374 					n->handler_name = $8;
5375 					n->amtype = $6;
5376 					$$ = (Node *) n;
5377 				}
5378 		;
5379 
5380 am_type:
5381 			INDEX			{ $$ = AMTYPE_INDEX; }
5382 		|	TABLE			{ $$ = AMTYPE_TABLE; }
5383 		;
5384 
5385 /*****************************************************************************
5386  *
5387  *		QUERIES :
5388  *				CREATE TRIGGER ...
5389  *
5390  *****************************************************************************/
5391 
5392 CreateTrigStmt:
5393 			CREATE TRIGGER name TriggerActionTime TriggerEvents ON
5394 			qualified_name TriggerReferencing TriggerForSpec TriggerWhen
5395 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' TriggerFuncArgs ')'
5396 				{
5397 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5398 					n->trigname = $3;
5399 					n->relation = $7;
5400 					n->funcname = $13;
5401 					n->args = $15;
5402 					n->row = $9;
5403 					n->timing = $4;
5404 					n->events = intVal(linitial($5));
5405 					n->columns = (List *) lsecond($5);
5406 					n->whenClause = $10;
5407 					n->transitionRels = $8;
5408 					n->isconstraint  = false;
5409 					n->deferrable	 = false;
5410 					n->initdeferred  = false;
5411 					n->constrrel = NULL;
5412 					$$ = (Node *)n;
5413 				}
5414 			| CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
5415 			qualified_name OptConstrFromTable ConstraintAttributeSpec
5416 			FOR EACH ROW TriggerWhen
5417 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' TriggerFuncArgs ')'
5418 				{
5419 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5420 					n->trigname = $4;
5421 					n->relation = $8;
5422 					n->funcname = $17;
5423 					n->args = $19;
5424 					n->row = true;
5425 					n->timing = TRIGGER_TYPE_AFTER;
5426 					n->events = intVal(linitial($6));
5427 					n->columns = (List *) lsecond($6);
5428 					n->whenClause = $14;
5429 					n->transitionRels = NIL;
5430 					n->isconstraint  = true;
5431 					processCASbits($10, @10, "TRIGGER",
5432 								   &n->deferrable, &n->initdeferred, NULL,
5433 								   NULL, yyscanner);
5434 					n->constrrel = $9;
5435 					$$ = (Node *)n;
5436 				}
5437 		;
5438 
5439 TriggerActionTime:
5440 			BEFORE								{ $$ = TRIGGER_TYPE_BEFORE; }
5441 			| AFTER								{ $$ = TRIGGER_TYPE_AFTER; }
5442 			| INSTEAD OF						{ $$ = TRIGGER_TYPE_INSTEAD; }
5443 		;
5444 
5445 TriggerEvents:
5446 			TriggerOneEvent
5447 				{ $$ = $1; }
5448 			| TriggerEvents OR TriggerOneEvent
5449 				{
5450 					int		events1 = intVal(linitial($1));
5451 					int		events2 = intVal(linitial($3));
5452 					List   *columns1 = (List *) lsecond($1);
5453 					List   *columns2 = (List *) lsecond($3);
5454 
5455 					if (events1 & events2)
5456 						parser_yyerror("duplicate trigger events specified");
5457 					/*
5458 					 * concat'ing the columns lists loses information about
5459 					 * which columns went with which event, but so long as
5460 					 * only UPDATE carries columns and we disallow multiple
5461 					 * UPDATE items, it doesn't matter.  Command execution
5462 					 * should just ignore the columns for non-UPDATE events.
5463 					 */
5464 					$$ = list_make2(makeInteger(events1 | events2),
5465 									list_concat(columns1, columns2));
5466 				}
5467 		;
5468 
5469 TriggerOneEvent:
5470 			INSERT
5471 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
5472 			| DELETE_P
5473 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
5474 			| UPDATE
5475 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
5476 			| UPDATE OF columnList
5477 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
5478 			| TRUNCATE
5479 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
5480 		;
5481 
5482 TriggerReferencing:
5483 			REFERENCING TriggerTransitions			{ $$ = $2; }
5484 			| /*EMPTY*/								{ $$ = NIL; }
5485 		;
5486 
5487 TriggerTransitions:
5488 			TriggerTransition						{ $$ = list_make1($1); }
5489 			| TriggerTransitions TriggerTransition	{ $$ = lappend($1, $2); }
5490 		;
5491 
5492 TriggerTransition:
5493 			TransitionOldOrNew TransitionRowOrTable opt_as TransitionRelName
5494 				{
5495 					TriggerTransition *n = makeNode(TriggerTransition);
5496 					n->name = $4;
5497 					n->isNew = $1;
5498 					n->isTable = $2;
5499 					$$ = (Node *)n;
5500 				}
5501 		;
5502 
5503 TransitionOldOrNew:
5504 			NEW										{ $$ = true; }
5505 			| OLD									{ $$ = false; }
5506 		;
5507 
5508 TransitionRowOrTable:
5509 			TABLE									{ $$ = true; }
5510 			/*
5511 			 * According to the standard, lack of a keyword here implies ROW.
5512 			 * Support for that would require prohibiting ROW entirely here,
5513 			 * reserving the keyword ROW, and/or requiring AS (instead of
5514 			 * allowing it to be optional, as the standard specifies) as the
5515 			 * next token.  Requiring ROW seems cleanest and easiest to
5516 			 * explain.
5517 			 */
5518 			| ROW									{ $$ = false; }
5519 		;
5520 
5521 TransitionRelName:
5522 			ColId									{ $$ = $1; }
5523 		;
5524 
5525 TriggerForSpec:
5526 			FOR TriggerForOptEach TriggerForType
5527 				{
5528 					$$ = $3;
5529 				}
5530 			| /* EMPTY */
5531 				{
5532 					/*
5533 					 * If ROW/STATEMENT not specified, default to
5534 					 * STATEMENT, per SQL
5535 					 */
5536 					$$ = false;
5537 				}
5538 		;
5539 
5540 TriggerForOptEach:
5541 			EACH									{}
5542 			| /*EMPTY*/								{}
5543 		;
5544 
5545 TriggerForType:
5546 			ROW										{ $$ = true; }
5547 			| STATEMENT								{ $$ = false; }
5548 		;
5549 
5550 TriggerWhen:
5551 			WHEN '(' a_expr ')'						{ $$ = $3; }
5552 			| /*EMPTY*/								{ $$ = NULL; }
5553 		;
5554 
5555 FUNCTION_or_PROCEDURE:
5556 			FUNCTION
5557 		|	PROCEDURE
5558 		;
5559 
5560 TriggerFuncArgs:
5561 			TriggerFuncArg							{ $$ = list_make1($1); }
5562 			| TriggerFuncArgs ',' TriggerFuncArg	{ $$ = lappend($1, $3); }
5563 			| /*EMPTY*/								{ $$ = NIL; }
5564 		;
5565 
5566 TriggerFuncArg:
5567 			Iconst
5568 				{
5569 					$$ = makeString(psprintf("%d", $1));
5570 				}
5571 			| FCONST								{ $$ = makeString($1); }
5572 			| Sconst								{ $$ = makeString($1); }
5573 			| ColLabel								{ $$ = makeString($1); }
5574 		;
5575 
5576 OptConstrFromTable:
5577 			FROM qualified_name						{ $$ = $2; }
5578 			| /*EMPTY*/								{ $$ = NULL; }
5579 		;
5580 
5581 ConstraintAttributeSpec:
5582 			/*EMPTY*/
5583 				{ $$ = 0; }
5584 			| ConstraintAttributeSpec ConstraintAttributeElem
5585 				{
5586 					/*
5587 					 * We must complain about conflicting options.
5588 					 * We could, but choose not to, complain about redundant
5589 					 * options (ie, where $2's bit is already set in $1).
5590 					 */
5591 					int		newspec = $1 | $2;
5592 
5593 					/* special message for this case */
5594 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED)) == (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED))
5595 						ereport(ERROR,
5596 								(errcode(ERRCODE_SYNTAX_ERROR),
5597 								 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
5598 								 parser_errposition(@2)));
5599 					/* generic message for other conflicts */
5600 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE)) == (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE) ||
5601 						(newspec & (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED)) == (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED))
5602 						ereport(ERROR,
5603 								(errcode(ERRCODE_SYNTAX_ERROR),
5604 								 errmsg("conflicting constraint properties"),
5605 								 parser_errposition(@2)));
5606 					$$ = newspec;
5607 				}
5608 		;
5609 
5610 ConstraintAttributeElem:
5611 			NOT DEFERRABLE					{ $$ = CAS_NOT_DEFERRABLE; }
5612 			| DEFERRABLE					{ $$ = CAS_DEFERRABLE; }
5613 			| INITIALLY IMMEDIATE			{ $$ = CAS_INITIALLY_IMMEDIATE; }
5614 			| INITIALLY DEFERRED			{ $$ = CAS_INITIALLY_DEFERRED; }
5615 			| NOT VALID						{ $$ = CAS_NOT_VALID; }
5616 			| NO INHERIT					{ $$ = CAS_NO_INHERIT; }
5617 		;
5618 
5619 
5620 /*****************************************************************************
5621  *
5622  *		QUERIES :
5623  *				CREATE EVENT TRIGGER ...
5624  *				ALTER EVENT TRIGGER ...
5625  *
5626  *****************************************************************************/
5627 
5628 CreateEventTrigStmt:
5629 			CREATE EVENT TRIGGER name ON ColLabel
5630 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' ')'
5631 				{
5632 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5633 					n->trigname = $4;
5634 					n->eventname = $6;
5635 					n->whenclause = NULL;
5636 					n->funcname = $9;
5637 					$$ = (Node *)n;
5638 				}
5639 		  | CREATE EVENT TRIGGER name ON ColLabel
5640 			WHEN event_trigger_when_list
5641 			EXECUTE FUNCTION_or_PROCEDURE func_name '(' ')'
5642 				{
5643 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5644 					n->trigname = $4;
5645 					n->eventname = $6;
5646 					n->whenclause = $8;
5647 					n->funcname = $11;
5648 					$$ = (Node *)n;
5649 				}
5650 		;
5651 
5652 event_trigger_when_list:
5653 		  event_trigger_when_item
5654 			{ $$ = list_make1($1); }
5655 		| event_trigger_when_list AND event_trigger_when_item
5656 			{ $$ = lappend($1, $3); }
5657 		;
5658 
5659 event_trigger_when_item:
5660 		ColId IN_P '(' event_trigger_value_list ')'
5661 			{ $$ = makeDefElem($1, (Node *) $4, @1); }
5662 		;
5663 
5664 event_trigger_value_list:
5665 		  SCONST
5666 			{ $$ = list_make1(makeString($1)); }
5667 		| event_trigger_value_list ',' SCONST
5668 			{ $$ = lappend($1, makeString($3)); }
5669 		;
5670 
5671 AlterEventTrigStmt:
5672 			ALTER EVENT TRIGGER name enable_trigger
5673 				{
5674 					AlterEventTrigStmt *n = makeNode(AlterEventTrigStmt);
5675 					n->trigname = $4;
5676 					n->tgenabled = $5;
5677 					$$ = (Node *) n;
5678 				}
5679 		;
5680 
5681 enable_trigger:
5682 			ENABLE_P					{ $$ = TRIGGER_FIRES_ON_ORIGIN; }
5683 			| ENABLE_P REPLICA			{ $$ = TRIGGER_FIRES_ON_REPLICA; }
5684 			| ENABLE_P ALWAYS			{ $$ = TRIGGER_FIRES_ALWAYS; }
5685 			| DISABLE_P					{ $$ = TRIGGER_DISABLED; }
5686 		;
5687 
5688 /*****************************************************************************
5689  *
5690  *		QUERY :
5691  *				CREATE ASSERTION ...
5692  *
5693  *****************************************************************************/
5694 
5695 CreateAssertionStmt:
5696 			CREATE ASSERTION any_name CHECK '(' a_expr ')' ConstraintAttributeSpec
5697 				{
5698 					ereport(ERROR,
5699 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5700 							 errmsg("CREATE ASSERTION is not yet implemented")));
5701 
5702 					$$ = NULL;
5703 				}
5704 		;
5705 
5706 
5707 /*****************************************************************************
5708  *
5709  *		QUERY :
5710  *				define (aggregate,operator,type)
5711  *
5712  *****************************************************************************/
5713 
5714 DefineStmt:
5715 			CREATE opt_or_replace AGGREGATE func_name aggr_args definition
5716 				{
5717 					DefineStmt *n = makeNode(DefineStmt);
5718 					n->kind = OBJECT_AGGREGATE;
5719 					n->oldstyle = false;
5720 					n->replace = $2;
5721 					n->defnames = $4;
5722 					n->args = $5;
5723 					n->definition = $6;
5724 					$$ = (Node *)n;
5725 				}
5726 			| CREATE opt_or_replace AGGREGATE func_name old_aggr_definition
5727 				{
5728 					/* old-style (pre-8.2) syntax for CREATE AGGREGATE */
5729 					DefineStmt *n = makeNode(DefineStmt);
5730 					n->kind = OBJECT_AGGREGATE;
5731 					n->oldstyle = true;
5732 					n->replace = $2;
5733 					n->defnames = $4;
5734 					n->args = NIL;
5735 					n->definition = $5;
5736 					$$ = (Node *)n;
5737 				}
5738 			| CREATE OPERATOR any_operator definition
5739 				{
5740 					DefineStmt *n = makeNode(DefineStmt);
5741 					n->kind = OBJECT_OPERATOR;
5742 					n->oldstyle = false;
5743 					n->defnames = $3;
5744 					n->args = NIL;
5745 					n->definition = $4;
5746 					$$ = (Node *)n;
5747 				}
5748 			| CREATE TYPE_P any_name definition
5749 				{
5750 					DefineStmt *n = makeNode(DefineStmt);
5751 					n->kind = OBJECT_TYPE;
5752 					n->oldstyle = false;
5753 					n->defnames = $3;
5754 					n->args = NIL;
5755 					n->definition = $4;
5756 					$$ = (Node *)n;
5757 				}
5758 			| CREATE TYPE_P any_name
5759 				{
5760 					/* Shell type (identified by lack of definition) */
5761 					DefineStmt *n = makeNode(DefineStmt);
5762 					n->kind = OBJECT_TYPE;
5763 					n->oldstyle = false;
5764 					n->defnames = $3;
5765 					n->args = NIL;
5766 					n->definition = NIL;
5767 					$$ = (Node *)n;
5768 				}
5769 			| CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
5770 				{
5771 					CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
5772 
5773 					/* can't use qualified_name, sigh */
5774 					n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
5775 					n->coldeflist = $6;
5776 					$$ = (Node *)n;
5777 				}
5778 			| CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
5779 				{
5780 					CreateEnumStmt *n = makeNode(CreateEnumStmt);
5781 					n->typeName = $3;
5782 					n->vals = $7;
5783 					$$ = (Node *)n;
5784 				}
5785 			| CREATE TYPE_P any_name AS RANGE definition
5786 				{
5787 					CreateRangeStmt *n = makeNode(CreateRangeStmt);
5788 					n->typeName = $3;
5789 					n->params	= $6;
5790 					$$ = (Node *)n;
5791 				}
5792 			| CREATE TEXT_P SEARCH PARSER any_name definition
5793 				{
5794 					DefineStmt *n = makeNode(DefineStmt);
5795 					n->kind = OBJECT_TSPARSER;
5796 					n->args = NIL;
5797 					n->defnames = $5;
5798 					n->definition = $6;
5799 					$$ = (Node *)n;
5800 				}
5801 			| CREATE TEXT_P SEARCH DICTIONARY any_name definition
5802 				{
5803 					DefineStmt *n = makeNode(DefineStmt);
5804 					n->kind = OBJECT_TSDICTIONARY;
5805 					n->args = NIL;
5806 					n->defnames = $5;
5807 					n->definition = $6;
5808 					$$ = (Node *)n;
5809 				}
5810 			| CREATE TEXT_P SEARCH TEMPLATE any_name definition
5811 				{
5812 					DefineStmt *n = makeNode(DefineStmt);
5813 					n->kind = OBJECT_TSTEMPLATE;
5814 					n->args = NIL;
5815 					n->defnames = $5;
5816 					n->definition = $6;
5817 					$$ = (Node *)n;
5818 				}
5819 			| CREATE TEXT_P SEARCH CONFIGURATION any_name definition
5820 				{
5821 					DefineStmt *n = makeNode(DefineStmt);
5822 					n->kind = OBJECT_TSCONFIGURATION;
5823 					n->args = NIL;
5824 					n->defnames = $5;
5825 					n->definition = $6;
5826 					$$ = (Node *)n;
5827 				}
5828 			| CREATE COLLATION any_name definition
5829 				{
5830 					DefineStmt *n = makeNode(DefineStmt);
5831 					n->kind = OBJECT_COLLATION;
5832 					n->args = NIL;
5833 					n->defnames = $3;
5834 					n->definition = $4;
5835 					$$ = (Node *)n;
5836 				}
5837 			| CREATE COLLATION IF_P NOT EXISTS any_name definition
5838 				{
5839 					DefineStmt *n = makeNode(DefineStmt);
5840 					n->kind = OBJECT_COLLATION;
5841 					n->args = NIL;
5842 					n->defnames = $6;
5843 					n->definition = $7;
5844 					n->if_not_exists = true;
5845 					$$ = (Node *)n;
5846 				}
5847 			| CREATE COLLATION any_name FROM any_name
5848 				{
5849 					DefineStmt *n = makeNode(DefineStmt);
5850 					n->kind = OBJECT_COLLATION;
5851 					n->args = NIL;
5852 					n->defnames = $3;
5853 					n->definition = list_make1(makeDefElem("from", (Node *) $5, @5));
5854 					$$ = (Node *)n;
5855 				}
5856 			| CREATE COLLATION IF_P NOT EXISTS any_name FROM any_name
5857 				{
5858 					DefineStmt *n = makeNode(DefineStmt);
5859 					n->kind = OBJECT_COLLATION;
5860 					n->args = NIL;
5861 					n->defnames = $6;
5862 					n->definition = list_make1(makeDefElem("from", (Node *) $8, @8));
5863 					n->if_not_exists = true;
5864 					$$ = (Node *)n;
5865 				}
5866 		;
5867 
5868 definition: '(' def_list ')'						{ $$ = $2; }
5869 		;
5870 
5871 def_list:	def_elem								{ $$ = list_make1($1); }
5872 			| def_list ',' def_elem					{ $$ = lappend($1, $3); }
5873 		;
5874 
5875 def_elem:	ColLabel '=' def_arg
5876 				{
5877 					$$ = makeDefElem($1, (Node *) $3, @1);
5878 				}
5879 			| ColLabel
5880 				{
5881 					$$ = makeDefElem($1, NULL, @1);
5882 				}
5883 		;
5884 
5885 /* Note: any simple identifier will be returned as a type name! */
5886 def_arg:	func_type						{ $$ = (Node *)$1; }
5887 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
5888 			| qual_all_Op					{ $$ = (Node *)$1; }
5889 			| NumericOnly					{ $$ = (Node *)$1; }
5890 			| Sconst						{ $$ = (Node *)makeString($1); }
5891 			| NONE							{ $$ = (Node *)makeString(pstrdup($1)); }
5892 		;
5893 
5894 old_aggr_definition: '(' old_aggr_list ')'			{ $$ = $2; }
5895 		;
5896 
5897 old_aggr_list: old_aggr_elem						{ $$ = list_make1($1); }
5898 			| old_aggr_list ',' old_aggr_elem		{ $$ = lappend($1, $3); }
5899 		;
5900 
5901 /*
5902  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
5903  * the item names needed in old aggregate definitions are likely to become
5904  * SQL keywords.
5905  */
5906 old_aggr_elem:  IDENT '=' def_arg
5907 				{
5908 					$$ = makeDefElem($1, (Node *)$3, @1);
5909 				}
5910 		;
5911 
5912 opt_enum_val_list:
5913 		enum_val_list							{ $$ = $1; }
5914 		| /*EMPTY*/								{ $$ = NIL; }
5915 		;
5916 
5917 enum_val_list:	Sconst
5918 				{ $$ = list_make1(makeString($1)); }
5919 			| enum_val_list ',' Sconst
5920 				{ $$ = lappend($1, makeString($3)); }
5921 		;
5922 
5923 /*****************************************************************************
5924  *
5925  *	ALTER TYPE enumtype ADD ...
5926  *
5927  *****************************************************************************/
5928 
5929 AlterEnumStmt:
5930 		ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst
5931 			{
5932 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5933 				n->typeName = $3;
5934 				n->oldVal = NULL;
5935 				n->newVal = $7;
5936 				n->newValNeighbor = NULL;
5937 				n->newValIsAfter = true;
5938 				n->skipIfNewValExists = $6;
5939 				$$ = (Node *) n;
5940 			}
5941 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst BEFORE Sconst
5942 			{
5943 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5944 				n->typeName = $3;
5945 				n->oldVal = NULL;
5946 				n->newVal = $7;
5947 				n->newValNeighbor = $9;
5948 				n->newValIsAfter = false;
5949 				n->skipIfNewValExists = $6;
5950 				$$ = (Node *) n;
5951 			}
5952 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst AFTER Sconst
5953 			{
5954 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5955 				n->typeName = $3;
5956 				n->oldVal = NULL;
5957 				n->newVal = $7;
5958 				n->newValNeighbor = $9;
5959 				n->newValIsAfter = true;
5960 				n->skipIfNewValExists = $6;
5961 				$$ = (Node *) n;
5962 			}
5963 		 | ALTER TYPE_P any_name RENAME VALUE_P Sconst TO Sconst
5964 			{
5965 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5966 				n->typeName = $3;
5967 				n->oldVal = $6;
5968 				n->newVal = $8;
5969 				n->newValNeighbor = NULL;
5970 				n->newValIsAfter = false;
5971 				n->skipIfNewValExists = false;
5972 				$$ = (Node *) n;
5973 			}
5974 		 ;
5975 
5976 opt_if_not_exists: IF_P NOT EXISTS              { $$ = true; }
5977 		| /* empty */                          { $$ = false; }
5978 		;
5979 
5980 
5981 /*****************************************************************************
5982  *
5983  *		QUERIES :
5984  *				CREATE OPERATOR CLASS ...
5985  *				CREATE OPERATOR FAMILY ...
5986  *				ALTER OPERATOR FAMILY ...
5987  *				DROP OPERATOR CLASS ...
5988  *				DROP OPERATOR FAMILY ...
5989  *
5990  *****************************************************************************/
5991 
5992 CreateOpClassStmt:
5993 			CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
5994 			USING access_method opt_opfamily AS opclass_item_list
5995 				{
5996 					CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
5997 					n->opclassname = $4;
5998 					n->isDefault = $5;
5999 					n->datatype = $8;
6000 					n->amname = $10;
6001 					n->opfamilyname = $11;
6002 					n->items = $13;
6003 					$$ = (Node *) n;
6004 				}
6005 		;
6006 
6007 opclass_item_list:
6008 			opclass_item							{ $$ = list_make1($1); }
6009 			| opclass_item_list ',' opclass_item	{ $$ = lappend($1, $3); }
6010 		;
6011 
6012 opclass_item:
6013 			OPERATOR Iconst any_operator opclass_purpose opt_recheck
6014 				{
6015 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6016 					ObjectWithArgs *owa = makeNode(ObjectWithArgs);
6017 					owa->objname = $3;
6018 					owa->objargs = NIL;
6019 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6020 					n->name = owa;
6021 					n->number = $2;
6022 					n->order_family = $4;
6023 					$$ = (Node *) n;
6024 				}
6025 			| OPERATOR Iconst operator_with_argtypes opclass_purpose
6026 			  opt_recheck
6027 				{
6028 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6029 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6030 					n->name = $3;
6031 					n->number = $2;
6032 					n->order_family = $4;
6033 					$$ = (Node *) n;
6034 				}
6035 			| FUNCTION Iconst function_with_argtypes
6036 				{
6037 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6038 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6039 					n->name = $3;
6040 					n->number = $2;
6041 					$$ = (Node *) n;
6042 				}
6043 			| FUNCTION Iconst '(' type_list ')' function_with_argtypes
6044 				{
6045 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6046 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6047 					n->name = $6;
6048 					n->number = $2;
6049 					n->class_args = $4;
6050 					$$ = (Node *) n;
6051 				}
6052 			| STORAGE Typename
6053 				{
6054 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6055 					n->itemtype = OPCLASS_ITEM_STORAGETYPE;
6056 					n->storedtype = $2;
6057 					$$ = (Node *) n;
6058 				}
6059 		;
6060 
6061 opt_default:	DEFAULT						{ $$ = true; }
6062 			| /*EMPTY*/						{ $$ = false; }
6063 		;
6064 
6065 opt_opfamily:	FAMILY any_name				{ $$ = $2; }
6066 			| /*EMPTY*/						{ $$ = NIL; }
6067 		;
6068 
6069 opclass_purpose: FOR SEARCH					{ $$ = NIL; }
6070 			| FOR ORDER BY any_name			{ $$ = $4; }
6071 			| /*EMPTY*/						{ $$ = NIL; }
6072 		;
6073 
6074 opt_recheck:	RECHECK
6075 				{
6076 					/*
6077 					 * RECHECK no longer does anything in opclass definitions,
6078 					 * but we still accept it to ease porting of old database
6079 					 * dumps.
6080 					 */
6081 					ereport(NOTICE,
6082 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6083 							 errmsg("RECHECK is no longer required"),
6084 							 errhint("Update your data type."),
6085 							 parser_errposition(@1)));
6086 					$$ = true;
6087 				}
6088 			| /*EMPTY*/						{ $$ = false; }
6089 		;
6090 
6091 
6092 CreateOpFamilyStmt:
6093 			CREATE OPERATOR FAMILY any_name USING access_method
6094 				{
6095 					CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
6096 					n->opfamilyname = $4;
6097 					n->amname = $6;
6098 					$$ = (Node *) n;
6099 				}
6100 		;
6101 
6102 AlterOpFamilyStmt:
6103 			ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
6104 				{
6105 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6106 					n->opfamilyname = $4;
6107 					n->amname = $6;
6108 					n->isDrop = false;
6109 					n->items = $8;
6110 					$$ = (Node *) n;
6111 				}
6112 			| ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
6113 				{
6114 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6115 					n->opfamilyname = $4;
6116 					n->amname = $6;
6117 					n->isDrop = true;
6118 					n->items = $8;
6119 					$$ = (Node *) n;
6120 				}
6121 		;
6122 
6123 opclass_drop_list:
6124 			opclass_drop							{ $$ = list_make1($1); }
6125 			| opclass_drop_list ',' opclass_drop	{ $$ = lappend($1, $3); }
6126 		;
6127 
6128 opclass_drop:
6129 			OPERATOR Iconst '(' type_list ')'
6130 				{
6131 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6132 					n->itemtype = OPCLASS_ITEM_OPERATOR;
6133 					n->number = $2;
6134 					n->class_args = $4;
6135 					$$ = (Node *) n;
6136 				}
6137 			| FUNCTION Iconst '(' type_list ')'
6138 				{
6139 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
6140 					n->itemtype = OPCLASS_ITEM_FUNCTION;
6141 					n->number = $2;
6142 					n->class_args = $4;
6143 					$$ = (Node *) n;
6144 				}
6145 		;
6146 
6147 
6148 DropOpClassStmt:
6149 			DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
6150 				{
6151 					DropStmt *n = makeNode(DropStmt);
6152 					n->objects = list_make1(lcons(makeString($6), $4));
6153 					n->removeType = OBJECT_OPCLASS;
6154 					n->behavior = $7;
6155 					n->missing_ok = false;
6156 					n->concurrent = false;
6157 					$$ = (Node *) n;
6158 				}
6159 			| DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
6160 				{
6161 					DropStmt *n = makeNode(DropStmt);
6162 					n->objects = list_make1(lcons(makeString($8), $6));
6163 					n->removeType = OBJECT_OPCLASS;
6164 					n->behavior = $9;
6165 					n->missing_ok = true;
6166 					n->concurrent = false;
6167 					$$ = (Node *) n;
6168 				}
6169 		;
6170 
6171 DropOpFamilyStmt:
6172 			DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
6173 				{
6174 					DropStmt *n = makeNode(DropStmt);
6175 					n->objects = list_make1(lcons(makeString($6), $4));
6176 					n->removeType = OBJECT_OPFAMILY;
6177 					n->behavior = $7;
6178 					n->missing_ok = false;
6179 					n->concurrent = false;
6180 					$$ = (Node *) n;
6181 				}
6182 			| DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
6183 				{
6184 					DropStmt *n = makeNode(DropStmt);
6185 					n->objects = list_make1(lcons(makeString($8), $6));
6186 					n->removeType = OBJECT_OPFAMILY;
6187 					n->behavior = $9;
6188 					n->missing_ok = true;
6189 					n->concurrent = false;
6190 					$$ = (Node *) n;
6191 				}
6192 		;
6193 
6194 
6195 /*****************************************************************************
6196  *
6197  *		QUERY:
6198  *
6199  *		DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
6200  *		REASSIGN OWNED BY username [, username ...] TO username
6201  *
6202  *****************************************************************************/
6203 DropOwnedStmt:
6204 			DROP OWNED BY role_list opt_drop_behavior
6205 				{
6206 					DropOwnedStmt *n = makeNode(DropOwnedStmt);
6207 					n->roles = $4;
6208 					n->behavior = $5;
6209 					$$ = (Node *)n;
6210 				}
6211 		;
6212 
6213 ReassignOwnedStmt:
6214 			REASSIGN OWNED BY role_list TO RoleSpec
6215 				{
6216 					ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
6217 					n->roles = $4;
6218 					n->newrole = $6;
6219 					$$ = (Node *)n;
6220 				}
6221 		;
6222 
6223 /*****************************************************************************
6224  *
6225  *		QUERY:
6226  *
6227  *		DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
6228  *           [ RESTRICT | CASCADE ]
6229  *
6230  *****************************************************************************/
6231 
6232 DropStmt:	DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
6233 				{
6234 					DropStmt *n = makeNode(DropStmt);
6235 					n->removeType = $2;
6236 					n->missing_ok = true;
6237 					n->objects = $5;
6238 					n->behavior = $6;
6239 					n->concurrent = false;
6240 					$$ = (Node *)n;
6241 				}
6242 			| DROP drop_type_any_name any_name_list opt_drop_behavior
6243 				{
6244 					DropStmt *n = makeNode(DropStmt);
6245 					n->removeType = $2;
6246 					n->missing_ok = false;
6247 					n->objects = $3;
6248 					n->behavior = $4;
6249 					n->concurrent = false;
6250 					$$ = (Node *)n;
6251 				}
6252 			| DROP drop_type_name IF_P EXISTS name_list opt_drop_behavior
6253 				{
6254 					DropStmt *n = makeNode(DropStmt);
6255 					n->removeType = $2;
6256 					n->missing_ok = true;
6257 					n->objects = $5;
6258 					n->behavior = $6;
6259 					n->concurrent = false;
6260 					$$ = (Node *)n;
6261 				}
6262 			| DROP drop_type_name name_list opt_drop_behavior
6263 				{
6264 					DropStmt *n = makeNode(DropStmt);
6265 					n->removeType = $2;
6266 					n->missing_ok = false;
6267 					n->objects = $3;
6268 					n->behavior = $4;
6269 					n->concurrent = false;
6270 					$$ = (Node *)n;
6271 				}
6272 			| DROP drop_type_name_on_any_name name ON any_name opt_drop_behavior
6273 				{
6274 					DropStmt *n = makeNode(DropStmt);
6275 					n->removeType = $2;
6276 					n->objects = list_make1(lappend($5, makeString($3)));
6277 					n->behavior = $6;
6278 					n->missing_ok = false;
6279 					n->concurrent = false;
6280 					$$ = (Node *) n;
6281 				}
6282 			| DROP drop_type_name_on_any_name IF_P EXISTS name ON any_name opt_drop_behavior
6283 				{
6284 					DropStmt *n = makeNode(DropStmt);
6285 					n->removeType = $2;
6286 					n->objects = list_make1(lappend($7, makeString($5)));
6287 					n->behavior = $8;
6288 					n->missing_ok = true;
6289 					n->concurrent = false;
6290 					$$ = (Node *) n;
6291 				}
6292 			| DROP TYPE_P type_name_list opt_drop_behavior
6293 				{
6294 					DropStmt *n = makeNode(DropStmt);
6295 					n->removeType = OBJECT_TYPE;
6296 					n->missing_ok = false;
6297 					n->objects = $3;
6298 					n->behavior = $4;
6299 					n->concurrent = false;
6300 					$$ = (Node *) n;
6301 				}
6302 			| DROP TYPE_P IF_P EXISTS type_name_list opt_drop_behavior
6303 				{
6304 					DropStmt *n = makeNode(DropStmt);
6305 					n->removeType = OBJECT_TYPE;
6306 					n->missing_ok = true;
6307 					n->objects = $5;
6308 					n->behavior = $6;
6309 					n->concurrent = false;
6310 					$$ = (Node *) n;
6311 				}
6312 			| DROP DOMAIN_P type_name_list opt_drop_behavior
6313 				{
6314 					DropStmt *n = makeNode(DropStmt);
6315 					n->removeType = OBJECT_DOMAIN;
6316 					n->missing_ok = false;
6317 					n->objects = $3;
6318 					n->behavior = $4;
6319 					n->concurrent = false;
6320 					$$ = (Node *) n;
6321 				}
6322 			| DROP DOMAIN_P IF_P EXISTS type_name_list opt_drop_behavior
6323 				{
6324 					DropStmt *n = makeNode(DropStmt);
6325 					n->removeType = OBJECT_DOMAIN;
6326 					n->missing_ok = true;
6327 					n->objects = $5;
6328 					n->behavior = $6;
6329 					n->concurrent = false;
6330 					$$ = (Node *) n;
6331 				}
6332 			| DROP INDEX CONCURRENTLY any_name_list opt_drop_behavior
6333 				{
6334 					DropStmt *n = makeNode(DropStmt);
6335 					n->removeType = OBJECT_INDEX;
6336 					n->missing_ok = false;
6337 					n->objects = $4;
6338 					n->behavior = $5;
6339 					n->concurrent = true;
6340 					$$ = (Node *)n;
6341 				}
6342 			| DROP INDEX CONCURRENTLY IF_P EXISTS any_name_list opt_drop_behavior
6343 				{
6344 					DropStmt *n = makeNode(DropStmt);
6345 					n->removeType = OBJECT_INDEX;
6346 					n->missing_ok = true;
6347 					n->objects = $6;
6348 					n->behavior = $7;
6349 					n->concurrent = true;
6350 					$$ = (Node *)n;
6351 				}
6352 		;
6353 
6354 /* object types taking any_name_list */
6355 drop_type_any_name:
6356 			TABLE									{ $$ = OBJECT_TABLE; }
6357 			| SEQUENCE								{ $$ = OBJECT_SEQUENCE; }
6358 			| VIEW									{ $$ = OBJECT_VIEW; }
6359 			| MATERIALIZED VIEW						{ $$ = OBJECT_MATVIEW; }
6360 			| INDEX									{ $$ = OBJECT_INDEX; }
6361 			| FOREIGN TABLE							{ $$ = OBJECT_FOREIGN_TABLE; }
6362 			| COLLATION								{ $$ = OBJECT_COLLATION; }
6363 			| CONVERSION_P							{ $$ = OBJECT_CONVERSION; }
6364 			| STATISTICS							{ $$ = OBJECT_STATISTIC_EXT; }
6365 			| TEXT_P SEARCH PARSER					{ $$ = OBJECT_TSPARSER; }
6366 			| TEXT_P SEARCH DICTIONARY				{ $$ = OBJECT_TSDICTIONARY; }
6367 			| TEXT_P SEARCH TEMPLATE				{ $$ = OBJECT_TSTEMPLATE; }
6368 			| TEXT_P SEARCH CONFIGURATION			{ $$ = OBJECT_TSCONFIGURATION; }
6369 		;
6370 
6371 /* object types taking name_list */
6372 drop_type_name:
6373 			ACCESS METHOD							{ $$ = OBJECT_ACCESS_METHOD; }
6374 			| EVENT TRIGGER							{ $$ = OBJECT_EVENT_TRIGGER; }
6375 			| EXTENSION								{ $$ = OBJECT_EXTENSION; }
6376 			| FOREIGN DATA_P WRAPPER				{ $$ = OBJECT_FDW; }
6377 			| PUBLICATION							{ $$ = OBJECT_PUBLICATION; }
6378 			| SCHEMA								{ $$ = OBJECT_SCHEMA; }
6379 			| SERVER								{ $$ = OBJECT_FOREIGN_SERVER; }
6380 		;
6381 
6382 /* object types attached to a table */
6383 drop_type_name_on_any_name:
6384 			POLICY									{ $$ = OBJECT_POLICY; }
6385 			| RULE									{ $$ = OBJECT_RULE; }
6386 			| TRIGGER								{ $$ = OBJECT_TRIGGER; }
6387 		;
6388 
6389 any_name_list:
6390 			any_name								{ $$ = list_make1($1); }
6391 			| any_name_list ',' any_name			{ $$ = lappend($1, $3); }
6392 		;
6393 
6394 any_name:	ColId						{ $$ = list_make1(makeString($1)); }
6395 			| ColId attrs				{ $$ = lcons(makeString($1), $2); }
6396 		;
6397 
6398 attrs:		'.' attr_name
6399 					{ $$ = list_make1(makeString($2)); }
6400 			| attrs '.' attr_name
6401 					{ $$ = lappend($1, makeString($3)); }
6402 		;
6403 
6404 type_name_list:
6405 			Typename								{ $$ = list_make1($1); }
6406 			| type_name_list ',' Typename			{ $$ = lappend($1, $3); }
6407 		;
6408 
6409 /*****************************************************************************
6410  *
6411  *		QUERY:
6412  *				truncate table relname1, relname2, ...
6413  *
6414  *****************************************************************************/
6415 
6416 TruncateStmt:
6417 			TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
6418 				{
6419 					TruncateStmt *n = makeNode(TruncateStmt);
6420 					n->relations = $3;
6421 					n->restart_seqs = $4;
6422 					n->behavior = $5;
6423 					$$ = (Node *)n;
6424 				}
6425 		;
6426 
6427 opt_restart_seqs:
6428 			CONTINUE_P IDENTITY_P		{ $$ = false; }
6429 			| RESTART IDENTITY_P		{ $$ = true; }
6430 			| /* EMPTY */				{ $$ = false; }
6431 		;
6432 
6433 /*****************************************************************************
6434  *
6435  *	The COMMENT ON statement can take different forms based upon the type of
6436  *	the object associated with the comment. The form of the statement is:
6437  *
6438  *	COMMENT ON [ [ ACCESS METHOD | CONVERSION | COLLATION |
6439  *                 DATABASE | DOMAIN |
6440  *                 EXTENSION | EVENT TRIGGER | FOREIGN DATA WRAPPER |
6441  *                 FOREIGN TABLE | INDEX | [PROCEDURAL] LANGUAGE |
6442  *                 MATERIALIZED VIEW | POLICY | ROLE | SCHEMA | SEQUENCE |
6443  *                 SERVER | TABLE | TABLESPACE |
6444  *                 TEXT SEARCH CONFIGURATION | TEXT SEARCH DICTIONARY |
6445  *                 TEXT SEARCH PARSER | TEXT SEARCH TEMPLATE | TYPE |
6446  *                 VIEW] <objname> |
6447  *				 AGGREGATE <aggname> (arg1, ...) |
6448  *				 CAST (<src type> AS <dst type>) |
6449  *				 COLUMN <relname>.<colname> |
6450  *				 CONSTRAINT <constraintname> ON <relname> |
6451  *				 CONSTRAINT <constraintname> ON DOMAIN <domainname> |
6452  *				 FUNCTION <funcname> (arg1, arg2, ...) |
6453  *				 LARGE OBJECT <oid> |
6454  *				 OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
6455  *				 OPERATOR CLASS <name> USING <access-method> |
6456  *				 OPERATOR FAMILY <name> USING <access-method> |
6457  *				 RULE <rulename> ON <relname> |
6458  *				 TRIGGER <triggername> ON <relname> ]
6459  *			   IS { 'text' | NULL }
6460  *
6461  *****************************************************************************/
6462 
6463 CommentStmt:
6464 			COMMENT ON comment_type_any_name any_name IS comment_text
6465 				{
6466 					CommentStmt *n = makeNode(CommentStmt);
6467 					n->objtype = $3;
6468 					n->object = (Node *) $4;
6469 					n->comment = $6;
6470 					$$ = (Node *) n;
6471 				}
6472 			| COMMENT ON comment_type_name name IS comment_text
6473 				{
6474 					CommentStmt *n = makeNode(CommentStmt);
6475 					n->objtype = $3;
6476 					n->object = (Node *) makeString($4);
6477 					n->comment = $6;
6478 					$$ = (Node *) n;
6479 				}
6480 			| COMMENT ON TYPE_P Typename IS comment_text
6481 				{
6482 					CommentStmt *n = makeNode(CommentStmt);
6483 					n->objtype = OBJECT_TYPE;
6484 					n->object = (Node *) $4;
6485 					n->comment = $6;
6486 					$$ = (Node *) n;
6487 				}
6488 			| COMMENT ON DOMAIN_P Typename IS comment_text
6489 				{
6490 					CommentStmt *n = makeNode(CommentStmt);
6491 					n->objtype = OBJECT_DOMAIN;
6492 					n->object = (Node *) $4;
6493 					n->comment = $6;
6494 					$$ = (Node *) n;
6495 				}
6496 			| COMMENT ON AGGREGATE aggregate_with_argtypes IS comment_text
6497 				{
6498 					CommentStmt *n = makeNode(CommentStmt);
6499 					n->objtype = OBJECT_AGGREGATE;
6500 					n->object = (Node *) $4;
6501 					n->comment = $6;
6502 					$$ = (Node *) n;
6503 				}
6504 			| COMMENT ON FUNCTION function_with_argtypes IS comment_text
6505 				{
6506 					CommentStmt *n = makeNode(CommentStmt);
6507 					n->objtype = OBJECT_FUNCTION;
6508 					n->object = (Node *) $4;
6509 					n->comment = $6;
6510 					$$ = (Node *) n;
6511 				}
6512 			| COMMENT ON OPERATOR operator_with_argtypes IS comment_text
6513 				{
6514 					CommentStmt *n = makeNode(CommentStmt);
6515 					n->objtype = OBJECT_OPERATOR;
6516 					n->object = (Node *) $4;
6517 					n->comment = $6;
6518 					$$ = (Node *) n;
6519 				}
6520 			| COMMENT ON CONSTRAINT name ON any_name IS comment_text
6521 				{
6522 					CommentStmt *n = makeNode(CommentStmt);
6523 					n->objtype = OBJECT_TABCONSTRAINT;
6524 					n->object = (Node *) lappend($6, makeString($4));
6525 					n->comment = $8;
6526 					$$ = (Node *) n;
6527 				}
6528 			| COMMENT ON CONSTRAINT name ON DOMAIN_P any_name IS comment_text
6529 				{
6530 					CommentStmt *n = makeNode(CommentStmt);
6531 					n->objtype = OBJECT_DOMCONSTRAINT;
6532 					/*
6533 					 * should use Typename not any_name in the production, but
6534 					 * there's a shift/reduce conflict if we do that, so fix it
6535 					 * up here.
6536 					 */
6537 					n->object = (Node *) list_make2(makeTypeNameFromNameList($7), makeString($4));
6538 					n->comment = $9;
6539 					$$ = (Node *) n;
6540 				}
6541 			| COMMENT ON POLICY name ON any_name IS comment_text
6542 				{
6543 					CommentStmt *n = makeNode(CommentStmt);
6544 					n->objtype = OBJECT_POLICY;
6545 					n->object = (Node *) lappend($6, makeString($4));
6546 					n->comment = $8;
6547 					$$ = (Node *) n;
6548 				}
6549 			| COMMENT ON PROCEDURE function_with_argtypes IS comment_text
6550 				{
6551 					CommentStmt *n = makeNode(CommentStmt);
6552 					n->objtype = OBJECT_PROCEDURE;
6553 					n->object = (Node *) $4;
6554 					n->comment = $6;
6555 					$$ = (Node *) n;
6556 				}
6557 			| COMMENT ON ROUTINE function_with_argtypes IS comment_text
6558 				{
6559 					CommentStmt *n = makeNode(CommentStmt);
6560 					n->objtype = OBJECT_ROUTINE;
6561 					n->object = (Node *) $4;
6562 					n->comment = $6;
6563 					$$ = (Node *) n;
6564 				}
6565 			| COMMENT ON RULE name ON any_name IS comment_text
6566 				{
6567 					CommentStmt *n = makeNode(CommentStmt);
6568 					n->objtype = OBJECT_RULE;
6569 					n->object = (Node *) lappend($6, makeString($4));
6570 					n->comment = $8;
6571 					$$ = (Node *) n;
6572 				}
6573 			| COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
6574 				{
6575 					CommentStmt *n = makeNode(CommentStmt);
6576 					n->objtype = OBJECT_TRANSFORM;
6577 					n->object = (Node *) list_make2($5, makeString($7));
6578 					n->comment = $9;
6579 					$$ = (Node *) n;
6580 				}
6581 			| COMMENT ON TRIGGER name ON any_name IS comment_text
6582 				{
6583 					CommentStmt *n = makeNode(CommentStmt);
6584 					n->objtype = OBJECT_TRIGGER;
6585 					n->object = (Node *) lappend($6, makeString($4));
6586 					n->comment = $8;
6587 					$$ = (Node *) n;
6588 				}
6589 			| COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
6590 				{
6591 					CommentStmt *n = makeNode(CommentStmt);
6592 					n->objtype = OBJECT_OPCLASS;
6593 					n->object = (Node *) lcons(makeString($7), $5);
6594 					n->comment = $9;
6595 					$$ = (Node *) n;
6596 				}
6597 			| COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
6598 				{
6599 					CommentStmt *n = makeNode(CommentStmt);
6600 					n->objtype = OBJECT_OPFAMILY;
6601 					n->object = (Node *) lcons(makeString($7), $5);
6602 					n->comment = $9;
6603 					$$ = (Node *) n;
6604 				}
6605 			| COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
6606 				{
6607 					CommentStmt *n = makeNode(CommentStmt);
6608 					n->objtype = OBJECT_LARGEOBJECT;
6609 					n->object = (Node *) $5;
6610 					n->comment = $7;
6611 					$$ = (Node *) n;
6612 				}
6613 			| COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
6614 				{
6615 					CommentStmt *n = makeNode(CommentStmt);
6616 					n->objtype = OBJECT_CAST;
6617 					n->object = (Node *) list_make2($5, $7);
6618 					n->comment = $10;
6619 					$$ = (Node *) n;
6620 				}
6621 		;
6622 
6623 /* object types taking any_name */
6624 comment_type_any_name:
6625 			COLUMN								{ $$ = OBJECT_COLUMN; }
6626 			| INDEX								{ $$ = OBJECT_INDEX; }
6627 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6628 			| STATISTICS						{ $$ = OBJECT_STATISTIC_EXT; }
6629 			| TABLE								{ $$ = OBJECT_TABLE; }
6630 			| VIEW								{ $$ = OBJECT_VIEW; }
6631 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6632 			| COLLATION							{ $$ = OBJECT_COLLATION; }
6633 			| CONVERSION_P						{ $$ = OBJECT_CONVERSION; }
6634 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6635 			| TEXT_P SEARCH CONFIGURATION		{ $$ = OBJECT_TSCONFIGURATION; }
6636 			| TEXT_P SEARCH DICTIONARY			{ $$ = OBJECT_TSDICTIONARY; }
6637 			| TEXT_P SEARCH PARSER				{ $$ = OBJECT_TSPARSER; }
6638 			| TEXT_P SEARCH TEMPLATE			{ $$ = OBJECT_TSTEMPLATE; }
6639 		;
6640 
6641 /* object types taking name */
6642 comment_type_name:
6643 			ACCESS METHOD						{ $$ = OBJECT_ACCESS_METHOD; }
6644 			| DATABASE							{ $$ = OBJECT_DATABASE; }
6645 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6646 			| EXTENSION							{ $$ = OBJECT_EXTENSION; }
6647 			| FOREIGN DATA_P WRAPPER			{ $$ = OBJECT_FDW; }
6648 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6649 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6650 			| ROLE								{ $$ = OBJECT_ROLE; }
6651 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6652 			| SERVER							{ $$ = OBJECT_FOREIGN_SERVER; }
6653 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6654 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6655 		;
6656 
6657 comment_text:
6658 			Sconst								{ $$ = $1; }
6659 			| NULL_P							{ $$ = NULL; }
6660 		;
6661 
6662 
6663 /*****************************************************************************
6664  *
6665  *  SECURITY LABEL [FOR <provider>] ON <object> IS <label>
6666  *
6667  *  As with COMMENT ON, <object> can refer to various types of database
6668  *  objects (e.g. TABLE, COLUMN, etc.).
6669  *
6670  *****************************************************************************/
6671 
6672 SecLabelStmt:
6673 			SECURITY LABEL opt_provider ON security_label_type_any_name any_name
6674 			IS security_label
6675 				{
6676 					SecLabelStmt *n = makeNode(SecLabelStmt);
6677 					n->provider = $3;
6678 					n->objtype = $5;
6679 					n->object = (Node *) $6;
6680 					n->label = $8;
6681 					$$ = (Node *) n;
6682 				}
6683 			| SECURITY LABEL opt_provider ON security_label_type_name name
6684 			  IS security_label
6685 				{
6686 					SecLabelStmt *n = makeNode(SecLabelStmt);
6687 					n->provider = $3;
6688 					n->objtype = $5;
6689 					n->object = (Node *) makeString($6);
6690 					n->label = $8;
6691 					$$ = (Node *) n;
6692 				}
6693 			| SECURITY LABEL opt_provider ON TYPE_P Typename
6694 			  IS security_label
6695 				{
6696 					SecLabelStmt *n = makeNode(SecLabelStmt);
6697 					n->provider = $3;
6698 					n->objtype = OBJECT_TYPE;
6699 					n->object = (Node *) $6;
6700 					n->label = $8;
6701 					$$ = (Node *) n;
6702 				}
6703 			| SECURITY LABEL opt_provider ON DOMAIN_P Typename
6704 			  IS security_label
6705 				{
6706 					SecLabelStmt *n = makeNode(SecLabelStmt);
6707 					n->provider = $3;
6708 					n->objtype = OBJECT_DOMAIN;
6709 					n->object = (Node *) $6;
6710 					n->label = $8;
6711 					$$ = (Node *) n;
6712 				}
6713 			| SECURITY LABEL opt_provider ON AGGREGATE aggregate_with_argtypes
6714 			  IS security_label
6715 				{
6716 					SecLabelStmt *n = makeNode(SecLabelStmt);
6717 					n->provider = $3;
6718 					n->objtype = OBJECT_AGGREGATE;
6719 					n->object = (Node *) $6;
6720 					n->label = $8;
6721 					$$ = (Node *) n;
6722 				}
6723 			| SECURITY LABEL opt_provider ON FUNCTION function_with_argtypes
6724 			  IS security_label
6725 				{
6726 					SecLabelStmt *n = makeNode(SecLabelStmt);
6727 					n->provider = $3;
6728 					n->objtype = OBJECT_FUNCTION;
6729 					n->object = (Node *) $6;
6730 					n->label = $8;
6731 					$$ = (Node *) n;
6732 				}
6733 			| SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
6734 			  IS security_label
6735 				{
6736 					SecLabelStmt *n = makeNode(SecLabelStmt);
6737 					n->provider = $3;
6738 					n->objtype = OBJECT_LARGEOBJECT;
6739 					n->object = (Node *) $7;
6740 					n->label = $9;
6741 					$$ = (Node *) n;
6742 				}
6743 			| SECURITY LABEL opt_provider ON PROCEDURE function_with_argtypes
6744 			  IS security_label
6745 				{
6746 					SecLabelStmt *n = makeNode(SecLabelStmt);
6747 					n->provider = $3;
6748 					n->objtype = OBJECT_PROCEDURE;
6749 					n->object = (Node *) $6;
6750 					n->label = $8;
6751 					$$ = (Node *) n;
6752 				}
6753 			| SECURITY LABEL opt_provider ON ROUTINE function_with_argtypes
6754 			  IS security_label
6755 				{
6756 					SecLabelStmt *n = makeNode(SecLabelStmt);
6757 					n->provider = $3;
6758 					n->objtype = OBJECT_ROUTINE;
6759 					n->object = (Node *) $6;
6760 					n->label = $8;
6761 					$$ = (Node *) n;
6762 				}
6763 		;
6764 
6765 opt_provider:	FOR NonReservedWord_or_Sconst	{ $$ = $2; }
6766 				| /* empty */					{ $$ = NULL; }
6767 		;
6768 
6769 /* object types taking any_name */
6770 security_label_type_any_name:
6771 			COLUMN								{ $$ = OBJECT_COLUMN; }
6772 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6773 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6774 			| TABLE								{ $$ = OBJECT_TABLE; }
6775 			| VIEW								{ $$ = OBJECT_VIEW; }
6776 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6777 		;
6778 
6779 /* object types taking name */
6780 security_label_type_name:
6781 			DATABASE							{ $$ = OBJECT_DATABASE; }
6782 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6783 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6784 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6785 			| ROLE								{ $$ = OBJECT_ROLE; }
6786 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6787 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6788 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6789 		;
6790 
6791 security_label:	Sconst				{ $$ = $1; }
6792 				| NULL_P			{ $$ = NULL; }
6793 		;
6794 
6795 /*****************************************************************************
6796  *
6797  *		QUERY:
6798  *			fetch/move
6799  *
6800  *****************************************************************************/
6801 
6802 FetchStmt:	FETCH fetch_args
6803 				{
6804 					FetchStmt *n = (FetchStmt *) $2;
6805 					n->ismove = false;
6806 					$$ = (Node *)n;
6807 				}
6808 			| MOVE fetch_args
6809 				{
6810 					FetchStmt *n = (FetchStmt *) $2;
6811 					n->ismove = true;
6812 					$$ = (Node *)n;
6813 				}
6814 		;
6815 
6816 fetch_args:	cursor_name
6817 				{
6818 					FetchStmt *n = makeNode(FetchStmt);
6819 					n->portalname = $1;
6820 					n->direction = FETCH_FORWARD;
6821 					n->howMany = 1;
6822 					$$ = (Node *)n;
6823 				}
6824 			| from_in cursor_name
6825 				{
6826 					FetchStmt *n = makeNode(FetchStmt);
6827 					n->portalname = $2;
6828 					n->direction = FETCH_FORWARD;
6829 					n->howMany = 1;
6830 					$$ = (Node *)n;
6831 				}
6832 			| NEXT opt_from_in cursor_name
6833 				{
6834 					FetchStmt *n = makeNode(FetchStmt);
6835 					n->portalname = $3;
6836 					n->direction = FETCH_FORWARD;
6837 					n->howMany = 1;
6838 					$$ = (Node *)n;
6839 				}
6840 			| PRIOR opt_from_in cursor_name
6841 				{
6842 					FetchStmt *n = makeNode(FetchStmt);
6843 					n->portalname = $3;
6844 					n->direction = FETCH_BACKWARD;
6845 					n->howMany = 1;
6846 					$$ = (Node *)n;
6847 				}
6848 			| FIRST_P opt_from_in cursor_name
6849 				{
6850 					FetchStmt *n = makeNode(FetchStmt);
6851 					n->portalname = $3;
6852 					n->direction = FETCH_ABSOLUTE;
6853 					n->howMany = 1;
6854 					$$ = (Node *)n;
6855 				}
6856 			| LAST_P opt_from_in cursor_name
6857 				{
6858 					FetchStmt *n = makeNode(FetchStmt);
6859 					n->portalname = $3;
6860 					n->direction = FETCH_ABSOLUTE;
6861 					n->howMany = -1;
6862 					$$ = (Node *)n;
6863 				}
6864 			| ABSOLUTE_P SignedIconst opt_from_in cursor_name
6865 				{
6866 					FetchStmt *n = makeNode(FetchStmt);
6867 					n->portalname = $4;
6868 					n->direction = FETCH_ABSOLUTE;
6869 					n->howMany = $2;
6870 					$$ = (Node *)n;
6871 				}
6872 			| RELATIVE_P SignedIconst opt_from_in cursor_name
6873 				{
6874 					FetchStmt *n = makeNode(FetchStmt);
6875 					n->portalname = $4;
6876 					n->direction = FETCH_RELATIVE;
6877 					n->howMany = $2;
6878 					$$ = (Node *)n;
6879 				}
6880 			| SignedIconst opt_from_in cursor_name
6881 				{
6882 					FetchStmt *n = makeNode(FetchStmt);
6883 					n->portalname = $3;
6884 					n->direction = FETCH_FORWARD;
6885 					n->howMany = $1;
6886 					$$ = (Node *)n;
6887 				}
6888 			| ALL opt_from_in cursor_name
6889 				{
6890 					FetchStmt *n = makeNode(FetchStmt);
6891 					n->portalname = $3;
6892 					n->direction = FETCH_FORWARD;
6893 					n->howMany = FETCH_ALL;
6894 					$$ = (Node *)n;
6895 				}
6896 			| FORWARD opt_from_in cursor_name
6897 				{
6898 					FetchStmt *n = makeNode(FetchStmt);
6899 					n->portalname = $3;
6900 					n->direction = FETCH_FORWARD;
6901 					n->howMany = 1;
6902 					$$ = (Node *)n;
6903 				}
6904 			| FORWARD SignedIconst opt_from_in cursor_name
6905 				{
6906 					FetchStmt *n = makeNode(FetchStmt);
6907 					n->portalname = $4;
6908 					n->direction = FETCH_FORWARD;
6909 					n->howMany = $2;
6910 					$$ = (Node *)n;
6911 				}
6912 			| FORWARD ALL opt_from_in cursor_name
6913 				{
6914 					FetchStmt *n = makeNode(FetchStmt);
6915 					n->portalname = $4;
6916 					n->direction = FETCH_FORWARD;
6917 					n->howMany = FETCH_ALL;
6918 					$$ = (Node *)n;
6919 				}
6920 			| BACKWARD opt_from_in cursor_name
6921 				{
6922 					FetchStmt *n = makeNode(FetchStmt);
6923 					n->portalname = $3;
6924 					n->direction = FETCH_BACKWARD;
6925 					n->howMany = 1;
6926 					$$ = (Node *)n;
6927 				}
6928 			| BACKWARD SignedIconst opt_from_in cursor_name
6929 				{
6930 					FetchStmt *n = makeNode(FetchStmt);
6931 					n->portalname = $4;
6932 					n->direction = FETCH_BACKWARD;
6933 					n->howMany = $2;
6934 					$$ = (Node *)n;
6935 				}
6936 			| BACKWARD ALL opt_from_in cursor_name
6937 				{
6938 					FetchStmt *n = makeNode(FetchStmt);
6939 					n->portalname = $4;
6940 					n->direction = FETCH_BACKWARD;
6941 					n->howMany = FETCH_ALL;
6942 					$$ = (Node *)n;
6943 				}
6944 		;
6945 
6946 from_in:	FROM									{}
6947 			| IN_P									{}
6948 		;
6949 
6950 opt_from_in:	from_in								{}
6951 			| /* EMPTY */							{}
6952 		;
6953 
6954 
6955 /*****************************************************************************
6956  *
6957  * GRANT and REVOKE statements
6958  *
6959  *****************************************************************************/
6960 
6961 GrantStmt:	GRANT privileges ON privilege_target TO grantee_list
6962 			opt_grant_grant_option
6963 				{
6964 					GrantStmt *n = makeNode(GrantStmt);
6965 					n->is_grant = true;
6966 					n->privileges = $2;
6967 					n->targtype = ($4)->targtype;
6968 					n->objtype = ($4)->objtype;
6969 					n->objects = ($4)->objs;
6970 					n->grantees = $6;
6971 					n->grant_option = $7;
6972 					$$ = (Node*)n;
6973 				}
6974 		;
6975 
6976 RevokeStmt:
6977 			REVOKE privileges ON privilege_target
6978 			FROM grantee_list opt_drop_behavior
6979 				{
6980 					GrantStmt *n = makeNode(GrantStmt);
6981 					n->is_grant = false;
6982 					n->grant_option = false;
6983 					n->privileges = $2;
6984 					n->targtype = ($4)->targtype;
6985 					n->objtype = ($4)->objtype;
6986 					n->objects = ($4)->objs;
6987 					n->grantees = $6;
6988 					n->behavior = $7;
6989 					$$ = (Node *)n;
6990 				}
6991 			| REVOKE GRANT OPTION FOR privileges ON privilege_target
6992 			FROM grantee_list opt_drop_behavior
6993 				{
6994 					GrantStmt *n = makeNode(GrantStmt);
6995 					n->is_grant = false;
6996 					n->grant_option = true;
6997 					n->privileges = $5;
6998 					n->targtype = ($7)->targtype;
6999 					n->objtype = ($7)->objtype;
7000 					n->objects = ($7)->objs;
7001 					n->grantees = $9;
7002 					n->behavior = $10;
7003 					$$ = (Node *)n;
7004 				}
7005 		;
7006 
7007 
7008 /*
7009  * Privilege names are represented as strings; the validity of the privilege
7010  * names gets checked at execution.  This is a bit annoying but we have little
7011  * choice because of the syntactic conflict with lists of role names in
7012  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
7013  * production any reserved keywords that need to be usable as privilege names.
7014  */
7015 
7016 /* either ALL [PRIVILEGES] or a list of individual privileges */
7017 privileges: privilege_list
7018 				{ $$ = $1; }
7019 			| ALL
7020 				{ $$ = NIL; }
7021 			| ALL PRIVILEGES
7022 				{ $$ = NIL; }
7023 			| ALL '(' columnList ')'
7024 				{
7025 					AccessPriv *n = makeNode(AccessPriv);
7026 					n->priv_name = NULL;
7027 					n->cols = $3;
7028 					$$ = list_make1(n);
7029 				}
7030 			| ALL PRIVILEGES '(' columnList ')'
7031 				{
7032 					AccessPriv *n = makeNode(AccessPriv);
7033 					n->priv_name = NULL;
7034 					n->cols = $4;
7035 					$$ = list_make1(n);
7036 				}
7037 		;
7038 
7039 privilege_list:	privilege							{ $$ = list_make1($1); }
7040 			| privilege_list ',' privilege			{ $$ = lappend($1, $3); }
7041 		;
7042 
7043 privilege:	SELECT opt_column_list
7044 			{
7045 				AccessPriv *n = makeNode(AccessPriv);
7046 				n->priv_name = pstrdup($1);
7047 				n->cols = $2;
7048 				$$ = n;
7049 			}
7050 		| REFERENCES opt_column_list
7051 			{
7052 				AccessPriv *n = makeNode(AccessPriv);
7053 				n->priv_name = pstrdup($1);
7054 				n->cols = $2;
7055 				$$ = n;
7056 			}
7057 		| CREATE opt_column_list
7058 			{
7059 				AccessPriv *n = makeNode(AccessPriv);
7060 				n->priv_name = pstrdup($1);
7061 				n->cols = $2;
7062 				$$ = n;
7063 			}
7064 		| ColId opt_column_list
7065 			{
7066 				AccessPriv *n = makeNode(AccessPriv);
7067 				n->priv_name = $1;
7068 				n->cols = $2;
7069 				$$ = n;
7070 			}
7071 		;
7072 
7073 
7074 /* Don't bother trying to fold the first two rules into one using
7075  * opt_table.  You're going to get conflicts.
7076  */
7077 privilege_target:
7078 			qualified_name_list
7079 				{
7080 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7081 					n->targtype = ACL_TARGET_OBJECT;
7082 					n->objtype = OBJECT_TABLE;
7083 					n->objs = $1;
7084 					$$ = n;
7085 				}
7086 			| TABLE qualified_name_list
7087 				{
7088 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7089 					n->targtype = ACL_TARGET_OBJECT;
7090 					n->objtype = OBJECT_TABLE;
7091 					n->objs = $2;
7092 					$$ = n;
7093 				}
7094 			| SEQUENCE qualified_name_list
7095 				{
7096 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7097 					n->targtype = ACL_TARGET_OBJECT;
7098 					n->objtype = OBJECT_SEQUENCE;
7099 					n->objs = $2;
7100 					$$ = n;
7101 				}
7102 			| FOREIGN DATA_P WRAPPER name_list
7103 				{
7104 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7105 					n->targtype = ACL_TARGET_OBJECT;
7106 					n->objtype = OBJECT_FDW;
7107 					n->objs = $4;
7108 					$$ = n;
7109 				}
7110 			| FOREIGN SERVER name_list
7111 				{
7112 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7113 					n->targtype = ACL_TARGET_OBJECT;
7114 					n->objtype = OBJECT_FOREIGN_SERVER;
7115 					n->objs = $3;
7116 					$$ = n;
7117 				}
7118 			| FUNCTION function_with_argtypes_list
7119 				{
7120 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7121 					n->targtype = ACL_TARGET_OBJECT;
7122 					n->objtype = OBJECT_FUNCTION;
7123 					n->objs = $2;
7124 					$$ = n;
7125 				}
7126 			| PROCEDURE function_with_argtypes_list
7127 				{
7128 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7129 					n->targtype = ACL_TARGET_OBJECT;
7130 					n->objtype = OBJECT_PROCEDURE;
7131 					n->objs = $2;
7132 					$$ = n;
7133 				}
7134 			| ROUTINE function_with_argtypes_list
7135 				{
7136 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7137 					n->targtype = ACL_TARGET_OBJECT;
7138 					n->objtype = OBJECT_ROUTINE;
7139 					n->objs = $2;
7140 					$$ = n;
7141 				}
7142 			| DATABASE name_list
7143 				{
7144 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7145 					n->targtype = ACL_TARGET_OBJECT;
7146 					n->objtype = OBJECT_DATABASE;
7147 					n->objs = $2;
7148 					$$ = n;
7149 				}
7150 			| DOMAIN_P any_name_list
7151 				{
7152 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7153 					n->targtype = ACL_TARGET_OBJECT;
7154 					n->objtype = OBJECT_DOMAIN;
7155 					n->objs = $2;
7156 					$$ = n;
7157 				}
7158 			| LANGUAGE name_list
7159 				{
7160 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7161 					n->targtype = ACL_TARGET_OBJECT;
7162 					n->objtype = OBJECT_LANGUAGE;
7163 					n->objs = $2;
7164 					$$ = n;
7165 				}
7166 			| LARGE_P OBJECT_P NumericOnly_list
7167 				{
7168 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7169 					n->targtype = ACL_TARGET_OBJECT;
7170 					n->objtype = OBJECT_LARGEOBJECT;
7171 					n->objs = $3;
7172 					$$ = n;
7173 				}
7174 			| SCHEMA name_list
7175 				{
7176 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7177 					n->targtype = ACL_TARGET_OBJECT;
7178 					n->objtype = OBJECT_SCHEMA;
7179 					n->objs = $2;
7180 					$$ = n;
7181 				}
7182 			| TABLESPACE name_list
7183 				{
7184 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7185 					n->targtype = ACL_TARGET_OBJECT;
7186 					n->objtype = OBJECT_TABLESPACE;
7187 					n->objs = $2;
7188 					$$ = n;
7189 				}
7190 			| TYPE_P any_name_list
7191 				{
7192 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7193 					n->targtype = ACL_TARGET_OBJECT;
7194 					n->objtype = OBJECT_TYPE;
7195 					n->objs = $2;
7196 					$$ = n;
7197 				}
7198 			| ALL TABLES IN_P SCHEMA name_list
7199 				{
7200 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7201 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7202 					n->objtype = OBJECT_TABLE;
7203 					n->objs = $5;
7204 					$$ = n;
7205 				}
7206 			| ALL SEQUENCES IN_P SCHEMA name_list
7207 				{
7208 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7209 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7210 					n->objtype = OBJECT_SEQUENCE;
7211 					n->objs = $5;
7212 					$$ = n;
7213 				}
7214 			| ALL FUNCTIONS IN_P SCHEMA name_list
7215 				{
7216 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7217 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7218 					n->objtype = OBJECT_FUNCTION;
7219 					n->objs = $5;
7220 					$$ = n;
7221 				}
7222 			| ALL PROCEDURES IN_P SCHEMA name_list
7223 				{
7224 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7225 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7226 					n->objtype = OBJECT_PROCEDURE;
7227 					n->objs = $5;
7228 					$$ = n;
7229 				}
7230 			| ALL ROUTINES IN_P SCHEMA name_list
7231 				{
7232 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7233 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7234 					n->objtype = OBJECT_ROUTINE;
7235 					n->objs = $5;
7236 					$$ = n;
7237 				}
7238 		;
7239 
7240 
7241 grantee_list:
7242 			grantee									{ $$ = list_make1($1); }
7243 			| grantee_list ',' grantee				{ $$ = lappend($1, $3); }
7244 		;
7245 
7246 grantee:
7247 			RoleSpec								{ $$ = $1; }
7248 			| GROUP_P RoleSpec						{ $$ = $2; }
7249 		;
7250 
7251 
7252 opt_grant_grant_option:
7253 			WITH GRANT OPTION { $$ = true; }
7254 			| /*EMPTY*/ { $$ = false; }
7255 		;
7256 
7257 /*****************************************************************************
7258  *
7259  * GRANT and REVOKE ROLE statements
7260  *
7261  *****************************************************************************/
7262 
7263 GrantRoleStmt:
7264 			GRANT privilege_list TO role_list opt_grant_admin_option opt_granted_by
7265 				{
7266 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7267 					n->is_grant = true;
7268 					n->granted_roles = $2;
7269 					n->grantee_roles = $4;
7270 					n->admin_opt = $5;
7271 					n->grantor = $6;
7272 					$$ = (Node*)n;
7273 				}
7274 		;
7275 
7276 RevokeRoleStmt:
7277 			REVOKE privilege_list FROM role_list opt_granted_by opt_drop_behavior
7278 				{
7279 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7280 					n->is_grant = false;
7281 					n->admin_opt = false;
7282 					n->granted_roles = $2;
7283 					n->grantee_roles = $4;
7284 					n->behavior = $6;
7285 					$$ = (Node*)n;
7286 				}
7287 			| REVOKE ADMIN OPTION FOR privilege_list FROM role_list opt_granted_by opt_drop_behavior
7288 				{
7289 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7290 					n->is_grant = false;
7291 					n->admin_opt = true;
7292 					n->granted_roles = $5;
7293 					n->grantee_roles = $7;
7294 					n->behavior = $9;
7295 					$$ = (Node*)n;
7296 				}
7297 		;
7298 
7299 opt_grant_admin_option: WITH ADMIN OPTION				{ $$ = true; }
7300 			| /*EMPTY*/									{ $$ = false; }
7301 		;
7302 
7303 opt_granted_by: GRANTED BY RoleSpec						{ $$ = $3; }
7304 			| /*EMPTY*/									{ $$ = NULL; }
7305 		;
7306 
7307 /*****************************************************************************
7308  *
7309  * ALTER DEFAULT PRIVILEGES statement
7310  *
7311  *****************************************************************************/
7312 
7313 AlterDefaultPrivilegesStmt:
7314 			ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
7315 				{
7316 					AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
7317 					n->options = $4;
7318 					n->action = (GrantStmt *) $5;
7319 					$$ = (Node*)n;
7320 				}
7321 		;
7322 
7323 DefACLOptionList:
7324 			DefACLOptionList DefACLOption			{ $$ = lappend($1, $2); }
7325 			| /* EMPTY */							{ $$ = NIL; }
7326 		;
7327 
7328 DefACLOption:
7329 			IN_P SCHEMA name_list
7330 				{
7331 					$$ = makeDefElem("schemas", (Node *)$3, @1);
7332 				}
7333 			| FOR ROLE role_list
7334 				{
7335 					$$ = makeDefElem("roles", (Node *)$3, @1);
7336 				}
7337 			| FOR USER role_list
7338 				{
7339 					$$ = makeDefElem("roles", (Node *)$3, @1);
7340 				}
7341 		;
7342 
7343 /*
7344  * This should match GRANT/REVOKE, except that individual target objects
7345  * are not mentioned and we only allow a subset of object types.
7346  */
7347 DefACLAction:
7348 			GRANT privileges ON defacl_privilege_target TO grantee_list
7349 			opt_grant_grant_option
7350 				{
7351 					GrantStmt *n = makeNode(GrantStmt);
7352 					n->is_grant = true;
7353 					n->privileges = $2;
7354 					n->targtype = ACL_TARGET_DEFAULTS;
7355 					n->objtype = $4;
7356 					n->objects = NIL;
7357 					n->grantees = $6;
7358 					n->grant_option = $7;
7359 					$$ = (Node*)n;
7360 				}
7361 			| REVOKE privileges ON defacl_privilege_target
7362 			FROM grantee_list opt_drop_behavior
7363 				{
7364 					GrantStmt *n = makeNode(GrantStmt);
7365 					n->is_grant = false;
7366 					n->grant_option = false;
7367 					n->privileges = $2;
7368 					n->targtype = ACL_TARGET_DEFAULTS;
7369 					n->objtype = $4;
7370 					n->objects = NIL;
7371 					n->grantees = $6;
7372 					n->behavior = $7;
7373 					$$ = (Node *)n;
7374 				}
7375 			| REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
7376 			FROM grantee_list opt_drop_behavior
7377 				{
7378 					GrantStmt *n = makeNode(GrantStmt);
7379 					n->is_grant = false;
7380 					n->grant_option = true;
7381 					n->privileges = $5;
7382 					n->targtype = ACL_TARGET_DEFAULTS;
7383 					n->objtype = $7;
7384 					n->objects = NIL;
7385 					n->grantees = $9;
7386 					n->behavior = $10;
7387 					$$ = (Node *)n;
7388 				}
7389 		;
7390 
7391 defacl_privilege_target:
7392 			TABLES			{ $$ = OBJECT_TABLE; }
7393 			| FUNCTIONS		{ $$ = OBJECT_FUNCTION; }
7394 			| ROUTINES		{ $$ = OBJECT_FUNCTION; }
7395 			| SEQUENCES		{ $$ = OBJECT_SEQUENCE; }
7396 			| TYPES_P		{ $$ = OBJECT_TYPE; }
7397 			| SCHEMAS		{ $$ = OBJECT_SCHEMA; }
7398 		;
7399 
7400 
7401 /*****************************************************************************
7402  *
7403  *		QUERY: CREATE INDEX
7404  *
7405  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
7406  * willing to make TABLESPACE a fully reserved word.
7407  *****************************************************************************/
7408 
7409 IndexStmt:	CREATE opt_unique INDEX opt_concurrently opt_index_name
7410 			ON relation_expr access_method_clause '(' index_params ')'
7411 			opt_include opt_reloptions OptTableSpace where_clause
7412 				{
7413 					IndexStmt *n = makeNode(IndexStmt);
7414 					n->unique = $2;
7415 					n->concurrent = $4;
7416 					n->idxname = $5;
7417 					n->relation = $7;
7418 					n->accessMethod = $8;
7419 					n->indexParams = $10;
7420 					n->indexIncludingParams = $12;
7421 					n->options = $13;
7422 					n->tableSpace = $14;
7423 					n->whereClause = $15;
7424 					n->excludeOpNames = NIL;
7425 					n->idxcomment = NULL;
7426 					n->indexOid = InvalidOid;
7427 					n->oldNode = InvalidOid;
7428 					n->primary = false;
7429 					n->isconstraint = false;
7430 					n->deferrable = false;
7431 					n->initdeferred = false;
7432 					n->transformed = false;
7433 					n->if_not_exists = false;
7434 					n->reset_default_tblspc = false;
7435 					$$ = (Node *)n;
7436 				}
7437 			| CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS index_name
7438 			ON relation_expr access_method_clause '(' index_params ')'
7439 			opt_include opt_reloptions OptTableSpace where_clause
7440 				{
7441 					IndexStmt *n = makeNode(IndexStmt);
7442 					n->unique = $2;
7443 					n->concurrent = $4;
7444 					n->idxname = $8;
7445 					n->relation = $10;
7446 					n->accessMethod = $11;
7447 					n->indexParams = $13;
7448 					n->indexIncludingParams = $15;
7449 					n->options = $16;
7450 					n->tableSpace = $17;
7451 					n->whereClause = $18;
7452 					n->excludeOpNames = NIL;
7453 					n->idxcomment = NULL;
7454 					n->indexOid = InvalidOid;
7455 					n->oldNode = InvalidOid;
7456 					n->primary = false;
7457 					n->isconstraint = false;
7458 					n->deferrable = false;
7459 					n->initdeferred = false;
7460 					n->transformed = false;
7461 					n->if_not_exists = true;
7462 					n->reset_default_tblspc = false;
7463 					$$ = (Node *)n;
7464 				}
7465 		;
7466 
7467 opt_unique:
7468 			UNIQUE									{ $$ = true; }
7469 			| /*EMPTY*/								{ $$ = false; }
7470 		;
7471 
7472 opt_concurrently:
7473 			CONCURRENTLY							{ $$ = true; }
7474 			| /*EMPTY*/								{ $$ = false; }
7475 		;
7476 
7477 opt_index_name:
7478 			index_name								{ $$ = $1; }
7479 			| /*EMPTY*/								{ $$ = NULL; }
7480 		;
7481 
7482 access_method_clause:
7483 			USING access_method						{ $$ = $2; }
7484 			| /*EMPTY*/								{ $$ = DEFAULT_INDEX_TYPE; }
7485 		;
7486 
7487 index_params:	index_elem							{ $$ = list_make1($1); }
7488 			| index_params ',' index_elem			{ $$ = lappend($1, $3); }
7489 		;
7490 
7491 /*
7492  * Index attributes can be either simple column references, or arbitrary
7493  * expressions in parens.  For backwards-compatibility reasons, we allow
7494  * an expression that's just a function call to be written without parens.
7495  */
7496 index_elem:	ColId opt_collate opt_class opt_asc_desc opt_nulls_order
7497 				{
7498 					$$ = makeNode(IndexElem);
7499 					$$->name = $1;
7500 					$$->expr = NULL;
7501 					$$->indexcolname = NULL;
7502 					$$->collation = $2;
7503 					$$->opclass = $3;
7504 					$$->ordering = $4;
7505 					$$->nulls_ordering = $5;
7506 				}
7507 			| func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order
7508 				{
7509 					$$ = makeNode(IndexElem);
7510 					$$->name = NULL;
7511 					$$->expr = $1;
7512 					$$->indexcolname = NULL;
7513 					$$->collation = $2;
7514 					$$->opclass = $3;
7515 					$$->ordering = $4;
7516 					$$->nulls_ordering = $5;
7517 				}
7518 			| '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order
7519 				{
7520 					$$ = makeNode(IndexElem);
7521 					$$->name = NULL;
7522 					$$->expr = $2;
7523 					$$->indexcolname = NULL;
7524 					$$->collation = $4;
7525 					$$->opclass = $5;
7526 					$$->ordering = $6;
7527 					$$->nulls_ordering = $7;
7528 				}
7529 		;
7530 
7531 opt_include:		INCLUDE '(' index_including_params ')'			{ $$ = $3; }
7532 			 |		/* EMPTY */						{ $$ = NIL; }
7533 		;
7534 
7535 index_including_params:	index_elem						{ $$ = list_make1($1); }
7536 			| index_including_params ',' index_elem		{ $$ = lappend($1, $3); }
7537 		;
7538 
7539 opt_collate: COLLATE any_name						{ $$ = $2; }
7540 			| /*EMPTY*/								{ $$ = NIL; }
7541 		;
7542 
7543 opt_class:	any_name								{ $$ = $1; }
7544 			| /*EMPTY*/								{ $$ = NIL; }
7545 		;
7546 
7547 opt_asc_desc: ASC							{ $$ = SORTBY_ASC; }
7548 			| DESC							{ $$ = SORTBY_DESC; }
7549 			| /*EMPTY*/						{ $$ = SORTBY_DEFAULT; }
7550 		;
7551 
7552 opt_nulls_order: NULLS_LA FIRST_P			{ $$ = SORTBY_NULLS_FIRST; }
7553 			| NULLS_LA LAST_P				{ $$ = SORTBY_NULLS_LAST; }
7554 			| /*EMPTY*/						{ $$ = SORTBY_NULLS_DEFAULT; }
7555 		;
7556 
7557 
7558 /*****************************************************************************
7559  *
7560  *		QUERY:
7561  *				create [or replace] function <fname>
7562  *						[(<type-1> { , <type-n>})]
7563  *						returns <type-r>
7564  *						as <filename or code in language as appropriate>
7565  *						language <lang> [with parameters]
7566  *
7567  *****************************************************************************/
7568 
7569 CreateFunctionStmt:
7570 			CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7571 			RETURNS func_return createfunc_opt_list
7572 				{
7573 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7574 					n->is_procedure = false;
7575 					n->replace = $2;
7576 					n->funcname = $4;
7577 					n->parameters = $5;
7578 					n->returnType = $7;
7579 					n->options = $8;
7580 					$$ = (Node *)n;
7581 				}
7582 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7583 			  RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list
7584 				{
7585 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7586 					n->is_procedure = false;
7587 					n->replace = $2;
7588 					n->funcname = $4;
7589 					n->parameters = mergeTableFuncParameters($5, $9);
7590 					n->returnType = TableFuncTypeName($9);
7591 					n->returnType->location = @7;
7592 					n->options = $11;
7593 					$$ = (Node *)n;
7594 				}
7595 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7596 			  createfunc_opt_list
7597 				{
7598 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7599 					n->is_procedure = false;
7600 					n->replace = $2;
7601 					n->funcname = $4;
7602 					n->parameters = $5;
7603 					n->returnType = NULL;
7604 					n->options = $6;
7605 					$$ = (Node *)n;
7606 				}
7607 			| CREATE opt_or_replace PROCEDURE func_name func_args_with_defaults
7608 			  createfunc_opt_list
7609 				{
7610 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7611 					n->is_procedure = true;
7612 					n->replace = $2;
7613 					n->funcname = $4;
7614 					n->parameters = $5;
7615 					n->returnType = NULL;
7616 					n->options = $6;
7617 					$$ = (Node *)n;
7618 				}
7619 		;
7620 
7621 opt_or_replace:
7622 			OR REPLACE								{ $$ = true; }
7623 			| /*EMPTY*/								{ $$ = false; }
7624 		;
7625 
7626 func_args:	'(' func_args_list ')'					{ $$ = $2; }
7627 			| '(' ')'								{ $$ = NIL; }
7628 		;
7629 
7630 func_args_list:
7631 			func_arg								{ $$ = list_make1($1); }
7632 			| func_args_list ',' func_arg			{ $$ = lappend($1, $3); }
7633 		;
7634 
7635 function_with_argtypes_list:
7636 			function_with_argtypes					{ $$ = list_make1($1); }
7637 			| function_with_argtypes_list ',' function_with_argtypes
7638 													{ $$ = lappend($1, $3); }
7639 		;
7640 
7641 function_with_argtypes:
7642 			func_name func_args
7643 				{
7644 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7645 					n->objname = $1;
7646 					n->objargs = extractArgTypes($2);
7647 					$$ = n;
7648 				}
7649 			/*
7650 			 * Because of reduce/reduce conflicts, we can't use func_name
7651 			 * below, but we can write it out the long way, which actually
7652 			 * allows more cases.
7653 			 */
7654 			| type_func_name_keyword
7655 				{
7656 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7657 					n->objname = list_make1(makeString(pstrdup($1)));
7658 					n->args_unspecified = true;
7659 					$$ = n;
7660 				}
7661 			| ColId
7662 				{
7663 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7664 					n->objname = list_make1(makeString($1));
7665 					n->args_unspecified = true;
7666 					$$ = n;
7667 				}
7668 			| ColId indirection
7669 				{
7670 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7671 					n->objname = check_func_name(lcons(makeString($1), $2),
7672 												  yyscanner);
7673 					n->args_unspecified = true;
7674 					$$ = n;
7675 				}
7676 		;
7677 
7678 /*
7679  * func_args_with_defaults is separate because we only want to accept
7680  * defaults in CREATE FUNCTION, not in ALTER etc.
7681  */
7682 func_args_with_defaults:
7683 		'(' func_args_with_defaults_list ')'		{ $$ = $2; }
7684 		| '(' ')'									{ $$ = NIL; }
7685 		;
7686 
7687 func_args_with_defaults_list:
7688 		func_arg_with_default						{ $$ = list_make1($1); }
7689 		| func_args_with_defaults_list ',' func_arg_with_default
7690 													{ $$ = lappend($1, $3); }
7691 		;
7692 
7693 /*
7694  * The style with arg_class first is SQL99 standard, but Oracle puts
7695  * param_name first; accept both since it's likely people will try both
7696  * anyway.  Don't bother trying to save productions by letting arg_class
7697  * have an empty alternative ... you'll get shift/reduce conflicts.
7698  *
7699  * We can catch over-specified arguments here if we want to,
7700  * but for now better to silently swallow typmod, etc.
7701  * - thomas 2000-03-22
7702  */
7703 func_arg:
7704 			arg_class param_name func_type
7705 				{
7706 					FunctionParameter *n = makeNode(FunctionParameter);
7707 					n->name = $2;
7708 					n->argType = $3;
7709 					n->mode = $1;
7710 					n->defexpr = NULL;
7711 					$$ = n;
7712 				}
7713 			| param_name arg_class func_type
7714 				{
7715 					FunctionParameter *n = makeNode(FunctionParameter);
7716 					n->name = $1;
7717 					n->argType = $3;
7718 					n->mode = $2;
7719 					n->defexpr = NULL;
7720 					$$ = n;
7721 				}
7722 			| param_name func_type
7723 				{
7724 					FunctionParameter *n = makeNode(FunctionParameter);
7725 					n->name = $1;
7726 					n->argType = $2;
7727 					n->mode = FUNC_PARAM_IN;
7728 					n->defexpr = NULL;
7729 					$$ = n;
7730 				}
7731 			| arg_class func_type
7732 				{
7733 					FunctionParameter *n = makeNode(FunctionParameter);
7734 					n->name = NULL;
7735 					n->argType = $2;
7736 					n->mode = $1;
7737 					n->defexpr = NULL;
7738 					$$ = n;
7739 				}
7740 			| func_type
7741 				{
7742 					FunctionParameter *n = makeNode(FunctionParameter);
7743 					n->name = NULL;
7744 					n->argType = $1;
7745 					n->mode = FUNC_PARAM_IN;
7746 					n->defexpr = NULL;
7747 					$$ = n;
7748 				}
7749 		;
7750 
7751 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
7752 arg_class:	IN_P								{ $$ = FUNC_PARAM_IN; }
7753 			| OUT_P								{ $$ = FUNC_PARAM_OUT; }
7754 			| INOUT								{ $$ = FUNC_PARAM_INOUT; }
7755 			| IN_P OUT_P						{ $$ = FUNC_PARAM_INOUT; }
7756 			| VARIADIC							{ $$ = FUNC_PARAM_VARIADIC; }
7757 		;
7758 
7759 /*
7760  * Ideally param_name should be ColId, but that causes too many conflicts.
7761  */
7762 param_name:	type_function_name
7763 		;
7764 
7765 func_return:
7766 			func_type
7767 				{
7768 					/* We can catch over-specified results here if we want to,
7769 					 * but for now better to silently swallow typmod, etc.
7770 					 * - thomas 2000-03-22
7771 					 */
7772 					$$ = $1;
7773 				}
7774 		;
7775 
7776 /*
7777  * We would like to make the %TYPE productions here be ColId attrs etc,
7778  * but that causes reduce/reduce conflicts.  type_function_name
7779  * is next best choice.
7780  */
7781 func_type:	Typename								{ $$ = $1; }
7782 			| type_function_name attrs '%' TYPE_P
7783 				{
7784 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7785 					$$->pct_type = true;
7786 					$$->location = @1;
7787 				}
7788 			| SETOF type_function_name attrs '%' TYPE_P
7789 				{
7790 					$$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
7791 					$$->pct_type = true;
7792 					$$->setof = true;
7793 					$$->location = @2;
7794 				}
7795 		;
7796 
7797 func_arg_with_default:
7798 		func_arg
7799 				{
7800 					$$ = $1;
7801 				}
7802 		| func_arg DEFAULT a_expr
7803 				{
7804 					$$ = $1;
7805 					$$->defexpr = $3;
7806 				}
7807 		| func_arg '=' a_expr
7808 				{
7809 					$$ = $1;
7810 					$$->defexpr = $3;
7811 				}
7812 		;
7813 
7814 /* Aggregate args can be most things that function args can be */
7815 aggr_arg:	func_arg
7816 				{
7817 					if (!($1->mode == FUNC_PARAM_IN ||
7818 						  $1->mode == FUNC_PARAM_VARIADIC))
7819 						ereport(ERROR,
7820 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7821 								 errmsg("aggregates cannot have output arguments"),
7822 								 parser_errposition(@1)));
7823 					$$ = $1;
7824 				}
7825 		;
7826 
7827 /*
7828  * The SQL standard offers no guidance on how to declare aggregate argument
7829  * lists, since it doesn't have CREATE AGGREGATE etc.  We accept these cases:
7830  *
7831  * (*)									- normal agg with no args
7832  * (aggr_arg,...)						- normal agg with args
7833  * (ORDER BY aggr_arg,...)				- ordered-set agg with no direct args
7834  * (aggr_arg,... ORDER BY aggr_arg,...)	- ordered-set agg with direct args
7835  *
7836  * The zero-argument case is spelled with '*' for consistency with COUNT(*).
7837  *
7838  * An additional restriction is that if the direct-args list ends in a
7839  * VARIADIC item, the ordered-args list must contain exactly one item that
7840  * is also VARIADIC with the same type.  This allows us to collapse the two
7841  * VARIADIC items into one, which is necessary to represent the aggregate in
7842  * pg_proc.  We check this at the grammar stage so that we can return a list
7843  * in which the second VARIADIC item is already discarded, avoiding extra work
7844  * in cases such as DROP AGGREGATE.
7845  *
7846  * The return value of this production is a two-element list, in which the
7847  * first item is a sublist of FunctionParameter nodes (with any duplicate
7848  * VARIADIC item already dropped, as per above) and the second is an integer
7849  * Value node, containing -1 if there was no ORDER BY and otherwise the number
7850  * of argument declarations before the ORDER BY.  (If this number is equal
7851  * to the first sublist's length, then we dropped a duplicate VARIADIC item.)
7852  * This representation is passed as-is to CREATE AGGREGATE; for operations
7853  * on existing aggregates, we can just apply extractArgTypes to the first
7854  * sublist.
7855  */
7856 aggr_args:	'(' '*' ')'
7857 				{
7858 					$$ = list_make2(NIL, makeInteger(-1));
7859 				}
7860 			| '(' aggr_args_list ')'
7861 				{
7862 					$$ = list_make2($2, makeInteger(-1));
7863 				}
7864 			| '(' ORDER BY aggr_args_list ')'
7865 				{
7866 					$$ = list_make2($4, makeInteger(0));
7867 				}
7868 			| '(' aggr_args_list ORDER BY aggr_args_list ')'
7869 				{
7870 					/* this is the only case requiring consistency checking */
7871 					$$ = makeOrderedSetArgs($2, $5, yyscanner);
7872 				}
7873 		;
7874 
7875 aggr_args_list:
7876 			aggr_arg								{ $$ = list_make1($1); }
7877 			| aggr_args_list ',' aggr_arg			{ $$ = lappend($1, $3); }
7878 		;
7879 
7880 aggregate_with_argtypes:
7881 			func_name aggr_args
7882 				{
7883 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7884 					n->objname = $1;
7885 					n->objargs = extractAggrArgTypes($2);
7886 					$$ = n;
7887 				}
7888 		;
7889 
7890 aggregate_with_argtypes_list:
7891 			aggregate_with_argtypes					{ $$ = list_make1($1); }
7892 			| aggregate_with_argtypes_list ',' aggregate_with_argtypes
7893 													{ $$ = lappend($1, $3); }
7894 		;
7895 
7896 createfunc_opt_list:
7897 			/* Must be at least one to prevent conflict */
7898 			createfunc_opt_item						{ $$ = list_make1($1); }
7899 			| createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
7900 	;
7901 
7902 /*
7903  * Options common to both CREATE FUNCTION and ALTER FUNCTION
7904  */
7905 common_func_opt_item:
7906 			CALLED ON NULL_P INPUT_P
7907 				{
7908 					$$ = makeDefElem("strict", (Node *)makeInteger(false), @1);
7909 				}
7910 			| RETURNS NULL_P ON NULL_P INPUT_P
7911 				{
7912 					$$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7913 				}
7914 			| STRICT_P
7915 				{
7916 					$$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7917 				}
7918 			| IMMUTABLE
7919 				{
7920 					$$ = makeDefElem("volatility", (Node *)makeString("immutable"), @1);
7921 				}
7922 			| STABLE
7923 				{
7924 					$$ = makeDefElem("volatility", (Node *)makeString("stable"), @1);
7925 				}
7926 			| VOLATILE
7927 				{
7928 					$$ = makeDefElem("volatility", (Node *)makeString("volatile"), @1);
7929 				}
7930 			| EXTERNAL SECURITY DEFINER
7931 				{
7932 					$$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7933 				}
7934 			| EXTERNAL SECURITY INVOKER
7935 				{
7936 					$$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7937 				}
7938 			| SECURITY DEFINER
7939 				{
7940 					$$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7941 				}
7942 			| SECURITY INVOKER
7943 				{
7944 					$$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7945 				}
7946 			| LEAKPROOF
7947 				{
7948 					$$ = makeDefElem("leakproof", (Node *)makeInteger(true), @1);
7949 				}
7950 			| NOT LEAKPROOF
7951 				{
7952 					$$ = makeDefElem("leakproof", (Node *)makeInteger(false), @1);
7953 				}
7954 			| COST NumericOnly
7955 				{
7956 					$$ = makeDefElem("cost", (Node *)$2, @1);
7957 				}
7958 			| ROWS NumericOnly
7959 				{
7960 					$$ = makeDefElem("rows", (Node *)$2, @1);
7961 				}
7962 			| SUPPORT any_name
7963 				{
7964 					$$ = makeDefElem("support", (Node *)$2, @1);
7965 				}
7966 			| FunctionSetResetClause
7967 				{
7968 					/* we abuse the normal content of a DefElem here */
7969 					$$ = makeDefElem("set", (Node *)$1, @1);
7970 				}
7971 			| PARALLEL ColId
7972 				{
7973 					$$ = makeDefElem("parallel", (Node *)makeString($2), @1);
7974 				}
7975 		;
7976 
7977 createfunc_opt_item:
7978 			AS func_as
7979 				{
7980 					$$ = makeDefElem("as", (Node *)$2, @1);
7981 				}
7982 			| LANGUAGE NonReservedWord_or_Sconst
7983 				{
7984 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
7985 				}
7986 			| TRANSFORM transform_type_list
7987 				{
7988 					$$ = makeDefElem("transform", (Node *)$2, @1);
7989 				}
7990 			| WINDOW
7991 				{
7992 					$$ = makeDefElem("window", (Node *)makeInteger(true), @1);
7993 				}
7994 			| common_func_opt_item
7995 				{
7996 					$$ = $1;
7997 				}
7998 		;
7999 
8000 func_as:	Sconst						{ $$ = list_make1(makeString($1)); }
8001 			| Sconst ',' Sconst
8002 				{
8003 					$$ = list_make2(makeString($1), makeString($3));
8004 				}
8005 		;
8006 
8007 transform_type_list:
8008 			FOR TYPE_P Typename { $$ = list_make1($3); }
8009 			| transform_type_list ',' FOR TYPE_P Typename { $$ = lappend($1, $5); }
8010 		;
8011 
8012 opt_definition:
8013 			WITH definition							{ $$ = $2; }
8014 			| /*EMPTY*/								{ $$ = NIL; }
8015 		;
8016 
8017 table_func_column:	param_name func_type
8018 				{
8019 					FunctionParameter *n = makeNode(FunctionParameter);
8020 					n->name = $1;
8021 					n->argType = $2;
8022 					n->mode = FUNC_PARAM_TABLE;
8023 					n->defexpr = NULL;
8024 					$$ = n;
8025 				}
8026 		;
8027 
8028 table_func_column_list:
8029 			table_func_column
8030 				{
8031 					$$ = list_make1($1);
8032 				}
8033 			| table_func_column_list ',' table_func_column
8034 				{
8035 					$$ = lappend($1, $3);
8036 				}
8037 		;
8038 
8039 /*****************************************************************************
8040  * ALTER FUNCTION / ALTER PROCEDURE / ALTER ROUTINE
8041  *
8042  * RENAME and OWNER subcommands are already provided by the generic
8043  * ALTER infrastructure, here we just specify alterations that can
8044  * only be applied to functions.
8045  *
8046  *****************************************************************************/
8047 AlterFunctionStmt:
8048 			ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
8049 				{
8050 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8051 					n->objtype = OBJECT_FUNCTION;
8052 					n->func = $3;
8053 					n->actions = $4;
8054 					$$ = (Node *) n;
8055 				}
8056 			| ALTER PROCEDURE function_with_argtypes alterfunc_opt_list opt_restrict
8057 				{
8058 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8059 					n->objtype = OBJECT_PROCEDURE;
8060 					n->func = $3;
8061 					n->actions = $4;
8062 					$$ = (Node *) n;
8063 				}
8064 			| ALTER ROUTINE function_with_argtypes alterfunc_opt_list opt_restrict
8065 				{
8066 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8067 					n->objtype = OBJECT_ROUTINE;
8068 					n->func = $3;
8069 					n->actions = $4;
8070 					$$ = (Node *) n;
8071 				}
8072 		;
8073 
8074 alterfunc_opt_list:
8075 			/* At least one option must be specified */
8076 			common_func_opt_item					{ $$ = list_make1($1); }
8077 			| alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
8078 		;
8079 
8080 /* Ignored, merely for SQL compliance */
8081 opt_restrict:
8082 			RESTRICT
8083 			| /* EMPTY */
8084 		;
8085 
8086 
8087 /*****************************************************************************
8088  *
8089  *		QUERY:
8090  *
8091  *		DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8092  *		DROP PROCEDURE procname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8093  *		DROP ROUTINE routname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8094  *		DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
8095  *		DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
8096  *
8097  *****************************************************************************/
8098 
8099 RemoveFuncStmt:
8100 			DROP FUNCTION function_with_argtypes_list opt_drop_behavior
8101 				{
8102 					DropStmt *n = makeNode(DropStmt);
8103 					n->removeType = OBJECT_FUNCTION;
8104 					n->objects = $3;
8105 					n->behavior = $4;
8106 					n->missing_ok = false;
8107 					n->concurrent = false;
8108 					$$ = (Node *)n;
8109 				}
8110 			| DROP FUNCTION IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8111 				{
8112 					DropStmt *n = makeNode(DropStmt);
8113 					n->removeType = OBJECT_FUNCTION;
8114 					n->objects = $5;
8115 					n->behavior = $6;
8116 					n->missing_ok = true;
8117 					n->concurrent = false;
8118 					$$ = (Node *)n;
8119 				}
8120 			| DROP PROCEDURE function_with_argtypes_list opt_drop_behavior
8121 				{
8122 					DropStmt *n = makeNode(DropStmt);
8123 					n->removeType = OBJECT_PROCEDURE;
8124 					n->objects = $3;
8125 					n->behavior = $4;
8126 					n->missing_ok = false;
8127 					n->concurrent = false;
8128 					$$ = (Node *)n;
8129 				}
8130 			| DROP PROCEDURE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8131 				{
8132 					DropStmt *n = makeNode(DropStmt);
8133 					n->removeType = OBJECT_PROCEDURE;
8134 					n->objects = $5;
8135 					n->behavior = $6;
8136 					n->missing_ok = true;
8137 					n->concurrent = false;
8138 					$$ = (Node *)n;
8139 				}
8140 			| DROP ROUTINE function_with_argtypes_list opt_drop_behavior
8141 				{
8142 					DropStmt *n = makeNode(DropStmt);
8143 					n->removeType = OBJECT_ROUTINE;
8144 					n->objects = $3;
8145 					n->behavior = $4;
8146 					n->missing_ok = false;
8147 					n->concurrent = false;
8148 					$$ = (Node *)n;
8149 				}
8150 			| DROP ROUTINE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8151 				{
8152 					DropStmt *n = makeNode(DropStmt);
8153 					n->removeType = OBJECT_ROUTINE;
8154 					n->objects = $5;
8155 					n->behavior = $6;
8156 					n->missing_ok = true;
8157 					n->concurrent = false;
8158 					$$ = (Node *)n;
8159 				}
8160 		;
8161 
8162 RemoveAggrStmt:
8163 			DROP AGGREGATE aggregate_with_argtypes_list opt_drop_behavior
8164 				{
8165 					DropStmt *n = makeNode(DropStmt);
8166 					n->removeType = OBJECT_AGGREGATE;
8167 					n->objects = $3;
8168 					n->behavior = $4;
8169 					n->missing_ok = false;
8170 					n->concurrent = false;
8171 					$$ = (Node *)n;
8172 				}
8173 			| DROP AGGREGATE IF_P EXISTS aggregate_with_argtypes_list opt_drop_behavior
8174 				{
8175 					DropStmt *n = makeNode(DropStmt);
8176 					n->removeType = OBJECT_AGGREGATE;
8177 					n->objects = $5;
8178 					n->behavior = $6;
8179 					n->missing_ok = true;
8180 					n->concurrent = false;
8181 					$$ = (Node *)n;
8182 				}
8183 		;
8184 
8185 RemoveOperStmt:
8186 			DROP OPERATOR operator_with_argtypes_list opt_drop_behavior
8187 				{
8188 					DropStmt *n = makeNode(DropStmt);
8189 					n->removeType = OBJECT_OPERATOR;
8190 					n->objects = $3;
8191 					n->behavior = $4;
8192 					n->missing_ok = false;
8193 					n->concurrent = false;
8194 					$$ = (Node *)n;
8195 				}
8196 			| DROP OPERATOR IF_P EXISTS operator_with_argtypes_list opt_drop_behavior
8197 				{
8198 					DropStmt *n = makeNode(DropStmt);
8199 					n->removeType = OBJECT_OPERATOR;
8200 					n->objects = $5;
8201 					n->behavior = $6;
8202 					n->missing_ok = true;
8203 					n->concurrent = false;
8204 					$$ = (Node *)n;
8205 				}
8206 		;
8207 
8208 oper_argtypes:
8209 			'(' Typename ')'
8210 				{
8211 				   ereport(ERROR,
8212 						   (errcode(ERRCODE_SYNTAX_ERROR),
8213 							errmsg("missing argument"),
8214 							errhint("Use NONE to denote the missing argument of a unary operator."),
8215 							parser_errposition(@3)));
8216 				}
8217 			| '(' Typename ',' Typename ')'
8218 					{ $$ = list_make2($2, $4); }
8219 			| '(' NONE ',' Typename ')'					/* left unary */
8220 					{ $$ = list_make2(NULL, $4); }
8221 			| '(' Typename ',' NONE ')'					/* right unary */
8222 					{ $$ = list_make2($2, NULL); }
8223 		;
8224 
8225 any_operator:
8226 			all_Op
8227 					{ $$ = list_make1(makeString($1)); }
8228 			| ColId '.' any_operator
8229 					{ $$ = lcons(makeString($1), $3); }
8230 		;
8231 
8232 operator_with_argtypes_list:
8233 			operator_with_argtypes					{ $$ = list_make1($1); }
8234 			| operator_with_argtypes_list ',' operator_with_argtypes
8235 													{ $$ = lappend($1, $3); }
8236 		;
8237 
8238 operator_with_argtypes:
8239 			any_operator oper_argtypes
8240 				{
8241 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
8242 					n->objname = $1;
8243 					n->objargs = $2;
8244 					$$ = n;
8245 				}
8246 		;
8247 
8248 /*****************************************************************************
8249  *
8250  *		DO <anonymous code block> [ LANGUAGE language ]
8251  *
8252  * We use a DefElem list for future extensibility, and to allow flexibility
8253  * in the clause order.
8254  *
8255  *****************************************************************************/
8256 
8257 DoStmt: DO dostmt_opt_list
8258 				{
8259 					DoStmt *n = makeNode(DoStmt);
8260 					n->args = $2;
8261 					$$ = (Node *)n;
8262 				}
8263 		;
8264 
8265 dostmt_opt_list:
8266 			dostmt_opt_item						{ $$ = list_make1($1); }
8267 			| dostmt_opt_list dostmt_opt_item	{ $$ = lappend($1, $2); }
8268 		;
8269 
8270 dostmt_opt_item:
8271 			Sconst
8272 				{
8273 					$$ = makeDefElem("as", (Node *)makeString($1), @1);
8274 				}
8275 			| LANGUAGE NonReservedWord_or_Sconst
8276 				{
8277 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
8278 				}
8279 		;
8280 
8281 /*****************************************************************************
8282  *
8283  *		CREATE CAST / DROP CAST
8284  *
8285  *****************************************************************************/
8286 
8287 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
8288 					WITH FUNCTION function_with_argtypes cast_context
8289 				{
8290 					CreateCastStmt *n = makeNode(CreateCastStmt);
8291 					n->sourcetype = $4;
8292 					n->targettype = $6;
8293 					n->func = $10;
8294 					n->context = (CoercionContext) $11;
8295 					n->inout = false;
8296 					$$ = (Node *)n;
8297 				}
8298 			| CREATE CAST '(' Typename AS Typename ')'
8299 					WITHOUT FUNCTION cast_context
8300 				{
8301 					CreateCastStmt *n = makeNode(CreateCastStmt);
8302 					n->sourcetype = $4;
8303 					n->targettype = $6;
8304 					n->func = NULL;
8305 					n->context = (CoercionContext) $10;
8306 					n->inout = false;
8307 					$$ = (Node *)n;
8308 				}
8309 			| CREATE CAST '(' Typename AS Typename ')'
8310 					WITH INOUT cast_context
8311 				{
8312 					CreateCastStmt *n = makeNode(CreateCastStmt);
8313 					n->sourcetype = $4;
8314 					n->targettype = $6;
8315 					n->func = NULL;
8316 					n->context = (CoercionContext) $10;
8317 					n->inout = true;
8318 					$$ = (Node *)n;
8319 				}
8320 		;
8321 
8322 cast_context:  AS IMPLICIT_P					{ $$ = COERCION_IMPLICIT; }
8323 		| AS ASSIGNMENT							{ $$ = COERCION_ASSIGNMENT; }
8324 		| /*EMPTY*/								{ $$ = COERCION_EXPLICIT; }
8325 		;
8326 
8327 
8328 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
8329 				{
8330 					DropStmt *n = makeNode(DropStmt);
8331 					n->removeType = OBJECT_CAST;
8332 					n->objects = list_make1(list_make2($5, $7));
8333 					n->behavior = $9;
8334 					n->missing_ok = $3;
8335 					n->concurrent = false;
8336 					$$ = (Node *)n;
8337 				}
8338 		;
8339 
8340 opt_if_exists: IF_P EXISTS						{ $$ = true; }
8341 		| /*EMPTY*/								{ $$ = false; }
8342 		;
8343 
8344 
8345 /*****************************************************************************
8346  *
8347  *		CREATE TRANSFORM / DROP TRANSFORM
8348  *
8349  *****************************************************************************/
8350 
8351 CreateTransformStmt: CREATE opt_or_replace TRANSFORM FOR Typename LANGUAGE name '(' transform_element_list ')'
8352 				{
8353 					CreateTransformStmt *n = makeNode(CreateTransformStmt);
8354 					n->replace = $2;
8355 					n->type_name = $5;
8356 					n->lang = $7;
8357 					n->fromsql = linitial($9);
8358 					n->tosql = lsecond($9);
8359 					$$ = (Node *)n;
8360 				}
8361 		;
8362 
8363 transform_element_list: FROM SQL_P WITH FUNCTION function_with_argtypes ',' TO SQL_P WITH FUNCTION function_with_argtypes
8364 				{
8365 					$$ = list_make2($5, $11);
8366 				}
8367 				| TO SQL_P WITH FUNCTION function_with_argtypes ',' FROM SQL_P WITH FUNCTION function_with_argtypes
8368 				{
8369 					$$ = list_make2($11, $5);
8370 				}
8371 				| FROM SQL_P WITH FUNCTION function_with_argtypes
8372 				{
8373 					$$ = list_make2($5, NULL);
8374 				}
8375 				| TO SQL_P WITH FUNCTION function_with_argtypes
8376 				{
8377 					$$ = list_make2(NULL, $5);
8378 				}
8379 		;
8380 
8381 
8382 DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_drop_behavior
8383 				{
8384 					DropStmt *n = makeNode(DropStmt);
8385 					n->removeType = OBJECT_TRANSFORM;
8386 					n->objects = list_make1(list_make2($5, makeString($7)));
8387 					n->behavior = $8;
8388 					n->missing_ok = $3;
8389 					$$ = (Node *)n;
8390 				}
8391 		;
8392 
8393 
8394 /*****************************************************************************
8395  *
8396  *		QUERY:
8397  *
8398  *		REINDEX [ (options) ] type [CONCURRENTLY] <name>
8399  *****************************************************************************/
8400 
8401 ReindexStmt:
8402 			REINDEX reindex_target_type opt_concurrently qualified_name
8403 				{
8404 					ReindexStmt *n = makeNode(ReindexStmt);
8405 					n->kind = $2;
8406 					n->concurrent = $3;
8407 					n->relation = $4;
8408 					n->name = NULL;
8409 					n->options = 0;
8410 					$$ = (Node *)n;
8411 				}
8412 			| REINDEX reindex_target_multitable opt_concurrently name
8413 				{
8414 					ReindexStmt *n = makeNode(ReindexStmt);
8415 					n->kind = $2;
8416 					n->concurrent = $3;
8417 					n->name = $4;
8418 					n->relation = NULL;
8419 					n->options = 0;
8420 					$$ = (Node *)n;
8421 				}
8422 			| REINDEX '(' reindex_option_list ')' reindex_target_type opt_concurrently qualified_name
8423 				{
8424 					ReindexStmt *n = makeNode(ReindexStmt);
8425 					n->kind = $5;
8426 					n->concurrent = $6;
8427 					n->relation = $7;
8428 					n->name = NULL;
8429 					n->options = $3;
8430 					$$ = (Node *)n;
8431 				}
8432 			| REINDEX '(' reindex_option_list ')' reindex_target_multitable opt_concurrently name
8433 				{
8434 					ReindexStmt *n = makeNode(ReindexStmt);
8435 					n->kind = $5;
8436 					n->concurrent = $6;
8437 					n->name = $7;
8438 					n->relation = NULL;
8439 					n->options = $3;
8440 					$$ = (Node *)n;
8441 				}
8442 		;
8443 reindex_target_type:
8444 			INDEX					{ $$ = REINDEX_OBJECT_INDEX; }
8445 			| TABLE					{ $$ = REINDEX_OBJECT_TABLE; }
8446 		;
8447 reindex_target_multitable:
8448 			SCHEMA					{ $$ = REINDEX_OBJECT_SCHEMA; }
8449 			| SYSTEM_P				{ $$ = REINDEX_OBJECT_SYSTEM; }
8450 			| DATABASE				{ $$ = REINDEX_OBJECT_DATABASE; }
8451 		;
8452 reindex_option_list:
8453 			reindex_option_elem								{ $$ = $1; }
8454 			| reindex_option_list ',' reindex_option_elem	{ $$ = $1 | $3; }
8455 		;
8456 reindex_option_elem:
8457 			VERBOSE	{ $$ = REINDEXOPT_VERBOSE; }
8458 		;
8459 
8460 /*****************************************************************************
8461  *
8462  * ALTER TABLESPACE
8463  *
8464  *****************************************************************************/
8465 
8466 AlterTblSpcStmt:
8467 			ALTER TABLESPACE name SET reloptions
8468 				{
8469 					AlterTableSpaceOptionsStmt *n =
8470 						makeNode(AlterTableSpaceOptionsStmt);
8471 					n->tablespacename = $3;
8472 					n->options = $5;
8473 					n->isReset = false;
8474 					$$ = (Node *)n;
8475 				}
8476 			| ALTER TABLESPACE name RESET reloptions
8477 				{
8478 					AlterTableSpaceOptionsStmt *n =
8479 						makeNode(AlterTableSpaceOptionsStmt);
8480 					n->tablespacename = $3;
8481 					n->options = $5;
8482 					n->isReset = true;
8483 					$$ = (Node *)n;
8484 				}
8485 		;
8486 
8487 /*****************************************************************************
8488  *
8489  * ALTER THING name RENAME TO newname
8490  *
8491  *****************************************************************************/
8492 
8493 RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
8494 				{
8495 					RenameStmt *n = makeNode(RenameStmt);
8496 					n->renameType = OBJECT_AGGREGATE;
8497 					n->object = (Node *) $3;
8498 					n->newname = $6;
8499 					n->missing_ok = false;
8500 					$$ = (Node *)n;
8501 				}
8502 			| ALTER COLLATION any_name RENAME TO name
8503 				{
8504 					RenameStmt *n = makeNode(RenameStmt);
8505 					n->renameType = OBJECT_COLLATION;
8506 					n->object = (Node *) $3;
8507 					n->newname = $6;
8508 					n->missing_ok = false;
8509 					$$ = (Node *)n;
8510 				}
8511 			| ALTER CONVERSION_P any_name RENAME TO name
8512 				{
8513 					RenameStmt *n = makeNode(RenameStmt);
8514 					n->renameType = OBJECT_CONVERSION;
8515 					n->object = (Node *) $3;
8516 					n->newname = $6;
8517 					n->missing_ok = false;
8518 					$$ = (Node *)n;
8519 				}
8520 			| ALTER DATABASE database_name RENAME TO database_name
8521 				{
8522 					RenameStmt *n = makeNode(RenameStmt);
8523 					n->renameType = OBJECT_DATABASE;
8524 					n->subname = $3;
8525 					n->newname = $6;
8526 					n->missing_ok = false;
8527 					$$ = (Node *)n;
8528 				}
8529 			| ALTER DOMAIN_P any_name RENAME TO name
8530 				{
8531 					RenameStmt *n = makeNode(RenameStmt);
8532 					n->renameType = OBJECT_DOMAIN;
8533 					n->object = (Node *) $3;
8534 					n->newname = $6;
8535 					n->missing_ok = false;
8536 					$$ = (Node *)n;
8537 				}
8538 			| ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
8539 				{
8540 					RenameStmt *n = makeNode(RenameStmt);
8541 					n->renameType = OBJECT_DOMCONSTRAINT;
8542 					n->object = (Node *) $3;
8543 					n->subname = $6;
8544 					n->newname = $8;
8545 					$$ = (Node *)n;
8546 				}
8547 			| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
8548 				{
8549 					RenameStmt *n = makeNode(RenameStmt);
8550 					n->renameType = OBJECT_FDW;
8551 					n->object = (Node *) makeString($5);
8552 					n->newname = $8;
8553 					n->missing_ok = false;
8554 					$$ = (Node *)n;
8555 				}
8556 			| ALTER FUNCTION function_with_argtypes RENAME TO name
8557 				{
8558 					RenameStmt *n = makeNode(RenameStmt);
8559 					n->renameType = OBJECT_FUNCTION;
8560 					n->object = (Node *) $3;
8561 					n->newname = $6;
8562 					n->missing_ok = false;
8563 					$$ = (Node *)n;
8564 				}
8565 			| ALTER GROUP_P RoleId RENAME TO RoleId
8566 				{
8567 					RenameStmt *n = makeNode(RenameStmt);
8568 					n->renameType = OBJECT_ROLE;
8569 					n->subname = $3;
8570 					n->newname = $6;
8571 					n->missing_ok = false;
8572 					$$ = (Node *)n;
8573 				}
8574 			| ALTER opt_procedural LANGUAGE name RENAME TO name
8575 				{
8576 					RenameStmt *n = makeNode(RenameStmt);
8577 					n->renameType = OBJECT_LANGUAGE;
8578 					n->object = (Node *) makeString($4);
8579 					n->newname = $7;
8580 					n->missing_ok = false;
8581 					$$ = (Node *)n;
8582 				}
8583 			| ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
8584 				{
8585 					RenameStmt *n = makeNode(RenameStmt);
8586 					n->renameType = OBJECT_OPCLASS;
8587 					n->object = (Node *) lcons(makeString($6), $4);
8588 					n->newname = $9;
8589 					n->missing_ok = false;
8590 					$$ = (Node *)n;
8591 				}
8592 			| ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
8593 				{
8594 					RenameStmt *n = makeNode(RenameStmt);
8595 					n->renameType = OBJECT_OPFAMILY;
8596 					n->object = (Node *) lcons(makeString($6), $4);
8597 					n->newname = $9;
8598 					n->missing_ok = false;
8599 					$$ = (Node *)n;
8600 				}
8601 			| ALTER POLICY name ON qualified_name RENAME TO name
8602 				{
8603 					RenameStmt *n = makeNode(RenameStmt);
8604 					n->renameType = OBJECT_POLICY;
8605 					n->relation = $5;
8606 					n->subname = $3;
8607 					n->newname = $8;
8608 					n->missing_ok = false;
8609 					$$ = (Node *)n;
8610 				}
8611 			| ALTER POLICY IF_P EXISTS name ON qualified_name RENAME TO name
8612 				{
8613 					RenameStmt *n = makeNode(RenameStmt);
8614 					n->renameType = OBJECT_POLICY;
8615 					n->relation = $7;
8616 					n->subname = $5;
8617 					n->newname = $10;
8618 					n->missing_ok = true;
8619 					$$ = (Node *)n;
8620 				}
8621 			| ALTER PROCEDURE function_with_argtypes RENAME TO name
8622 				{
8623 					RenameStmt *n = makeNode(RenameStmt);
8624 					n->renameType = OBJECT_PROCEDURE;
8625 					n->object = (Node *) $3;
8626 					n->newname = $6;
8627 					n->missing_ok = false;
8628 					$$ = (Node *)n;
8629 				}
8630 			| ALTER PUBLICATION name RENAME TO name
8631 				{
8632 					RenameStmt *n = makeNode(RenameStmt);
8633 					n->renameType = OBJECT_PUBLICATION;
8634 					n->object = (Node *) makeString($3);
8635 					n->newname = $6;
8636 					n->missing_ok = false;
8637 					$$ = (Node *)n;
8638 				}
8639 			| ALTER ROUTINE function_with_argtypes RENAME TO name
8640 				{
8641 					RenameStmt *n = makeNode(RenameStmt);
8642 					n->renameType = OBJECT_ROUTINE;
8643 					n->object = (Node *) $3;
8644 					n->newname = $6;
8645 					n->missing_ok = false;
8646 					$$ = (Node *)n;
8647 				}
8648 			| ALTER SCHEMA name RENAME TO name
8649 				{
8650 					RenameStmt *n = makeNode(RenameStmt);
8651 					n->renameType = OBJECT_SCHEMA;
8652 					n->subname = $3;
8653 					n->newname = $6;
8654 					n->missing_ok = false;
8655 					$$ = (Node *)n;
8656 				}
8657 			| ALTER SERVER name RENAME TO name
8658 				{
8659 					RenameStmt *n = makeNode(RenameStmt);
8660 					n->renameType = OBJECT_FOREIGN_SERVER;
8661 					n->object = (Node *) makeString($3);
8662 					n->newname = $6;
8663 					n->missing_ok = false;
8664 					$$ = (Node *)n;
8665 				}
8666 			| ALTER SUBSCRIPTION name RENAME TO name
8667 				{
8668 					RenameStmt *n = makeNode(RenameStmt);
8669 					n->renameType = OBJECT_SUBSCRIPTION;
8670 					n->object = (Node *) makeString($3);
8671 					n->newname = $6;
8672 					n->missing_ok = false;
8673 					$$ = (Node *)n;
8674 				}
8675 			| ALTER TABLE relation_expr RENAME TO name
8676 				{
8677 					RenameStmt *n = makeNode(RenameStmt);
8678 					n->renameType = OBJECT_TABLE;
8679 					n->relation = $3;
8680 					n->subname = NULL;
8681 					n->newname = $6;
8682 					n->missing_ok = false;
8683 					$$ = (Node *)n;
8684 				}
8685 			| ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
8686 				{
8687 					RenameStmt *n = makeNode(RenameStmt);
8688 					n->renameType = OBJECT_TABLE;
8689 					n->relation = $5;
8690 					n->subname = NULL;
8691 					n->newname = $8;
8692 					n->missing_ok = true;
8693 					$$ = (Node *)n;
8694 				}
8695 			| ALTER SEQUENCE qualified_name RENAME TO name
8696 				{
8697 					RenameStmt *n = makeNode(RenameStmt);
8698 					n->renameType = OBJECT_SEQUENCE;
8699 					n->relation = $3;
8700 					n->subname = NULL;
8701 					n->newname = $6;
8702 					n->missing_ok = false;
8703 					$$ = (Node *)n;
8704 				}
8705 			| ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
8706 				{
8707 					RenameStmt *n = makeNode(RenameStmt);
8708 					n->renameType = OBJECT_SEQUENCE;
8709 					n->relation = $5;
8710 					n->subname = NULL;
8711 					n->newname = $8;
8712 					n->missing_ok = true;
8713 					$$ = (Node *)n;
8714 				}
8715 			| ALTER VIEW qualified_name RENAME TO name
8716 				{
8717 					RenameStmt *n = makeNode(RenameStmt);
8718 					n->renameType = OBJECT_VIEW;
8719 					n->relation = $3;
8720 					n->subname = NULL;
8721 					n->newname = $6;
8722 					n->missing_ok = false;
8723 					$$ = (Node *)n;
8724 				}
8725 			| ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
8726 				{
8727 					RenameStmt *n = makeNode(RenameStmt);
8728 					n->renameType = OBJECT_VIEW;
8729 					n->relation = $5;
8730 					n->subname = NULL;
8731 					n->newname = $8;
8732 					n->missing_ok = true;
8733 					$$ = (Node *)n;
8734 				}
8735 			| ALTER MATERIALIZED VIEW qualified_name RENAME TO name
8736 				{
8737 					RenameStmt *n = makeNode(RenameStmt);
8738 					n->renameType = OBJECT_MATVIEW;
8739 					n->relation = $4;
8740 					n->subname = NULL;
8741 					n->newname = $7;
8742 					n->missing_ok = false;
8743 					$$ = (Node *)n;
8744 				}
8745 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
8746 				{
8747 					RenameStmt *n = makeNode(RenameStmt);
8748 					n->renameType = OBJECT_MATVIEW;
8749 					n->relation = $6;
8750 					n->subname = NULL;
8751 					n->newname = $9;
8752 					n->missing_ok = true;
8753 					$$ = (Node *)n;
8754 				}
8755 			| ALTER INDEX qualified_name RENAME TO name
8756 				{
8757 					RenameStmt *n = makeNode(RenameStmt);
8758 					n->renameType = OBJECT_INDEX;
8759 					n->relation = $3;
8760 					n->subname = NULL;
8761 					n->newname = $6;
8762 					n->missing_ok = false;
8763 					$$ = (Node *)n;
8764 				}
8765 			| ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
8766 				{
8767 					RenameStmt *n = makeNode(RenameStmt);
8768 					n->renameType = OBJECT_INDEX;
8769 					n->relation = $5;
8770 					n->subname = NULL;
8771 					n->newname = $8;
8772 					n->missing_ok = true;
8773 					$$ = (Node *)n;
8774 				}
8775 			| ALTER FOREIGN TABLE relation_expr RENAME TO name
8776 				{
8777 					RenameStmt *n = makeNode(RenameStmt);
8778 					n->renameType = OBJECT_FOREIGN_TABLE;
8779 					n->relation = $4;
8780 					n->subname = NULL;
8781 					n->newname = $7;
8782 					n->missing_ok = false;
8783 					$$ = (Node *)n;
8784 				}
8785 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
8786 				{
8787 					RenameStmt *n = makeNode(RenameStmt);
8788 					n->renameType = OBJECT_FOREIGN_TABLE;
8789 					n->relation = $6;
8790 					n->subname = NULL;
8791 					n->newname = $9;
8792 					n->missing_ok = true;
8793 					$$ = (Node *)n;
8794 				}
8795 			| ALTER TABLE relation_expr RENAME opt_column name TO name
8796 				{
8797 					RenameStmt *n = makeNode(RenameStmt);
8798 					n->renameType = OBJECT_COLUMN;
8799 					n->relationType = OBJECT_TABLE;
8800 					n->relation = $3;
8801 					n->subname = $6;
8802 					n->newname = $8;
8803 					n->missing_ok = false;
8804 					$$ = (Node *)n;
8805 				}
8806 			| ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8807 				{
8808 					RenameStmt *n = makeNode(RenameStmt);
8809 					n->renameType = OBJECT_COLUMN;
8810 					n->relationType = OBJECT_TABLE;
8811 					n->relation = $5;
8812 					n->subname = $8;
8813 					n->newname = $10;
8814 					n->missing_ok = true;
8815 					$$ = (Node *)n;
8816 				}
8817 			| ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
8818 				{
8819 					RenameStmt *n = makeNode(RenameStmt);
8820 					n->renameType = OBJECT_COLUMN;
8821 					n->relationType = OBJECT_MATVIEW;
8822 					n->relation = $4;
8823 					n->subname = $7;
8824 					n->newname = $9;
8825 					n->missing_ok = false;
8826 					$$ = (Node *)n;
8827 				}
8828 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
8829 				{
8830 					RenameStmt *n = makeNode(RenameStmt);
8831 					n->renameType = OBJECT_COLUMN;
8832 					n->relationType = OBJECT_MATVIEW;
8833 					n->relation = $6;
8834 					n->subname = $9;
8835 					n->newname = $11;
8836 					n->missing_ok = true;
8837 					$$ = (Node *)n;
8838 				}
8839 			| ALTER TABLE relation_expr RENAME CONSTRAINT name TO name
8840 				{
8841 					RenameStmt *n = makeNode(RenameStmt);
8842 					n->renameType = OBJECT_TABCONSTRAINT;
8843 					n->relation = $3;
8844 					n->subname = $6;
8845 					n->newname = $8;
8846 					n->missing_ok = false;
8847 					$$ = (Node *)n;
8848 				}
8849 			| ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
8850 				{
8851 					RenameStmt *n = makeNode(RenameStmt);
8852 					n->renameType = OBJECT_TABCONSTRAINT;
8853 					n->relation = $5;
8854 					n->subname = $8;
8855 					n->newname = $10;
8856 					n->missing_ok = true;
8857 					$$ = (Node *)n;
8858 				}
8859 			| ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
8860 				{
8861 					RenameStmt *n = makeNode(RenameStmt);
8862 					n->renameType = OBJECT_COLUMN;
8863 					n->relationType = OBJECT_FOREIGN_TABLE;
8864 					n->relation = $4;
8865 					n->subname = $7;
8866 					n->newname = $9;
8867 					n->missing_ok = false;
8868 					$$ = (Node *)n;
8869 				}
8870 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8871 				{
8872 					RenameStmt *n = makeNode(RenameStmt);
8873 					n->renameType = OBJECT_COLUMN;
8874 					n->relationType = OBJECT_FOREIGN_TABLE;
8875 					n->relation = $6;
8876 					n->subname = $9;
8877 					n->newname = $11;
8878 					n->missing_ok = true;
8879 					$$ = (Node *)n;
8880 				}
8881 			| ALTER RULE name ON qualified_name RENAME TO name
8882 				{
8883 					RenameStmt *n = makeNode(RenameStmt);
8884 					n->renameType = OBJECT_RULE;
8885 					n->relation = $5;
8886 					n->subname = $3;
8887 					n->newname = $8;
8888 					n->missing_ok = false;
8889 					$$ = (Node *)n;
8890 				}
8891 			| ALTER TRIGGER name ON qualified_name RENAME TO name
8892 				{
8893 					RenameStmt *n = makeNode(RenameStmt);
8894 					n->renameType = OBJECT_TRIGGER;
8895 					n->relation = $5;
8896 					n->subname = $3;
8897 					n->newname = $8;
8898 					n->missing_ok = false;
8899 					$$ = (Node *)n;
8900 				}
8901 			| ALTER EVENT TRIGGER name RENAME TO name
8902 				{
8903 					RenameStmt *n = makeNode(RenameStmt);
8904 					n->renameType = OBJECT_EVENT_TRIGGER;
8905 					n->object = (Node *) makeString($4);
8906 					n->newname = $7;
8907 					$$ = (Node *)n;
8908 				}
8909 			| ALTER ROLE RoleId RENAME TO RoleId
8910 				{
8911 					RenameStmt *n = makeNode(RenameStmt);
8912 					n->renameType = OBJECT_ROLE;
8913 					n->subname = $3;
8914 					n->newname = $6;
8915 					n->missing_ok = false;
8916 					$$ = (Node *)n;
8917 				}
8918 			| ALTER USER RoleId RENAME TO RoleId
8919 				{
8920 					RenameStmt *n = makeNode(RenameStmt);
8921 					n->renameType = OBJECT_ROLE;
8922 					n->subname = $3;
8923 					n->newname = $6;
8924 					n->missing_ok = false;
8925 					$$ = (Node *)n;
8926 				}
8927 			| ALTER TABLESPACE name RENAME TO name
8928 				{
8929 					RenameStmt *n = makeNode(RenameStmt);
8930 					n->renameType = OBJECT_TABLESPACE;
8931 					n->subname = $3;
8932 					n->newname = $6;
8933 					n->missing_ok = false;
8934 					$$ = (Node *)n;
8935 				}
8936 			| ALTER STATISTICS any_name RENAME TO name
8937 				{
8938 					RenameStmt *n = makeNode(RenameStmt);
8939 					n->renameType = OBJECT_STATISTIC_EXT;
8940 					n->object = (Node *) $3;
8941 					n->newname = $6;
8942 					n->missing_ok = false;
8943 					$$ = (Node *)n;
8944 				}
8945 			| ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
8946 				{
8947 					RenameStmt *n = makeNode(RenameStmt);
8948 					n->renameType = OBJECT_TSPARSER;
8949 					n->object = (Node *) $5;
8950 					n->newname = $8;
8951 					n->missing_ok = false;
8952 					$$ = (Node *)n;
8953 				}
8954 			| ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
8955 				{
8956 					RenameStmt *n = makeNode(RenameStmt);
8957 					n->renameType = OBJECT_TSDICTIONARY;
8958 					n->object = (Node *) $5;
8959 					n->newname = $8;
8960 					n->missing_ok = false;
8961 					$$ = (Node *)n;
8962 				}
8963 			| ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
8964 				{
8965 					RenameStmt *n = makeNode(RenameStmt);
8966 					n->renameType = OBJECT_TSTEMPLATE;
8967 					n->object = (Node *) $5;
8968 					n->newname = $8;
8969 					n->missing_ok = false;
8970 					$$ = (Node *)n;
8971 				}
8972 			| ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
8973 				{
8974 					RenameStmt *n = makeNode(RenameStmt);
8975 					n->renameType = OBJECT_TSCONFIGURATION;
8976 					n->object = (Node *) $5;
8977 					n->newname = $8;
8978 					n->missing_ok = false;
8979 					$$ = (Node *)n;
8980 				}
8981 			| ALTER TYPE_P any_name RENAME TO name
8982 				{
8983 					RenameStmt *n = makeNode(RenameStmt);
8984 					n->renameType = OBJECT_TYPE;
8985 					n->object = (Node *) $3;
8986 					n->newname = $6;
8987 					n->missing_ok = false;
8988 					$$ = (Node *)n;
8989 				}
8990 			| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
8991 				{
8992 					RenameStmt *n = makeNode(RenameStmt);
8993 					n->renameType = OBJECT_ATTRIBUTE;
8994 					n->relationType = OBJECT_TYPE;
8995 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
8996 					n->subname = $6;
8997 					n->newname = $8;
8998 					n->behavior = $9;
8999 					n->missing_ok = false;
9000 					$$ = (Node *)n;
9001 				}
9002 		;
9003 
9004 opt_column: COLUMN									{ $$ = COLUMN; }
9005 			| /*EMPTY*/								{ $$ = 0; }
9006 		;
9007 
9008 opt_set_data: SET DATA_P							{ $$ = 1; }
9009 			| /*EMPTY*/								{ $$ = 0; }
9010 		;
9011 
9012 /*****************************************************************************
9013  *
9014  * ALTER THING name DEPENDS ON EXTENSION name
9015  *
9016  *****************************************************************************/
9017 
9018 AlterObjectDependsStmt:
9019 			ALTER FUNCTION function_with_argtypes DEPENDS ON EXTENSION name
9020 				{
9021 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9022 					n->objectType = OBJECT_FUNCTION;
9023 					n->object = (Node *) $3;
9024 					n->extname = makeString($7);
9025 					$$ = (Node *)n;
9026 				}
9027 			| ALTER PROCEDURE function_with_argtypes DEPENDS ON EXTENSION name
9028 				{
9029 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9030 					n->objectType = OBJECT_PROCEDURE;
9031 					n->object = (Node *) $3;
9032 					n->extname = makeString($7);
9033 					$$ = (Node *)n;
9034 				}
9035 			| ALTER ROUTINE function_with_argtypes DEPENDS ON EXTENSION name
9036 				{
9037 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9038 					n->objectType = OBJECT_ROUTINE;
9039 					n->object = (Node *) $3;
9040 					n->extname = makeString($7);
9041 					$$ = (Node *)n;
9042 				}
9043 			| ALTER TRIGGER name ON qualified_name DEPENDS ON EXTENSION name
9044 				{
9045 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9046 					n->objectType = OBJECT_TRIGGER;
9047 					n->relation = $5;
9048 					n->object = (Node *) list_make1(makeString($3));
9049 					n->extname = makeString($9);
9050 					$$ = (Node *)n;
9051 				}
9052 			| ALTER MATERIALIZED VIEW qualified_name DEPENDS ON EXTENSION name
9053 				{
9054 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9055 					n->objectType = OBJECT_MATVIEW;
9056 					n->relation = $4;
9057 					n->extname = makeString($8);
9058 					$$ = (Node *)n;
9059 				}
9060 			| ALTER INDEX qualified_name DEPENDS ON EXTENSION name
9061 				{
9062 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9063 					n->objectType = OBJECT_INDEX;
9064 					n->relation = $3;
9065 					n->extname = makeString($7);
9066 					$$ = (Node *)n;
9067 				}
9068 		;
9069 
9070 /*****************************************************************************
9071  *
9072  * ALTER THING name SET SCHEMA name
9073  *
9074  *****************************************************************************/
9075 
9076 AlterObjectSchemaStmt:
9077 			ALTER AGGREGATE aggregate_with_argtypes SET SCHEMA name
9078 				{
9079 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9080 					n->objectType = OBJECT_AGGREGATE;
9081 					n->object = (Node *) $3;
9082 					n->newschema = $6;
9083 					n->missing_ok = false;
9084 					$$ = (Node *)n;
9085 				}
9086 			| ALTER COLLATION any_name SET SCHEMA name
9087 				{
9088 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9089 					n->objectType = OBJECT_COLLATION;
9090 					n->object = (Node *) $3;
9091 					n->newschema = $6;
9092 					n->missing_ok = false;
9093 					$$ = (Node *)n;
9094 				}
9095 			| ALTER CONVERSION_P any_name SET SCHEMA name
9096 				{
9097 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9098 					n->objectType = OBJECT_CONVERSION;
9099 					n->object = (Node *) $3;
9100 					n->newschema = $6;
9101 					n->missing_ok = false;
9102 					$$ = (Node *)n;
9103 				}
9104 			| ALTER DOMAIN_P any_name SET SCHEMA name
9105 				{
9106 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9107 					n->objectType = OBJECT_DOMAIN;
9108 					n->object = (Node *) $3;
9109 					n->newschema = $6;
9110 					n->missing_ok = false;
9111 					$$ = (Node *)n;
9112 				}
9113 			| ALTER EXTENSION name SET SCHEMA name
9114 				{
9115 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9116 					n->objectType = OBJECT_EXTENSION;
9117 					n->object = (Node *) makeString($3);
9118 					n->newschema = $6;
9119 					n->missing_ok = false;
9120 					$$ = (Node *)n;
9121 				}
9122 			| ALTER FUNCTION function_with_argtypes SET SCHEMA name
9123 				{
9124 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9125 					n->objectType = OBJECT_FUNCTION;
9126 					n->object = (Node *) $3;
9127 					n->newschema = $6;
9128 					n->missing_ok = false;
9129 					$$ = (Node *)n;
9130 				}
9131 			| ALTER OPERATOR operator_with_argtypes SET SCHEMA name
9132 				{
9133 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9134 					n->objectType = OBJECT_OPERATOR;
9135 					n->object = (Node *) $3;
9136 					n->newschema = $6;
9137 					n->missing_ok = false;
9138 					$$ = (Node *)n;
9139 				}
9140 			| ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
9141 				{
9142 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9143 					n->objectType = OBJECT_OPCLASS;
9144 					n->object = (Node *) lcons(makeString($6), $4);
9145 					n->newschema = $9;
9146 					n->missing_ok = false;
9147 					$$ = (Node *)n;
9148 				}
9149 			| ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
9150 				{
9151 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9152 					n->objectType = OBJECT_OPFAMILY;
9153 					n->object = (Node *) lcons(makeString($6), $4);
9154 					n->newschema = $9;
9155 					n->missing_ok = false;
9156 					$$ = (Node *)n;
9157 				}
9158 			| ALTER PROCEDURE function_with_argtypes SET SCHEMA name
9159 				{
9160 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9161 					n->objectType = OBJECT_PROCEDURE;
9162 					n->object = (Node *) $3;
9163 					n->newschema = $6;
9164 					n->missing_ok = false;
9165 					$$ = (Node *)n;
9166 				}
9167 			| ALTER ROUTINE function_with_argtypes SET SCHEMA name
9168 				{
9169 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9170 					n->objectType = OBJECT_ROUTINE;
9171 					n->object = (Node *) $3;
9172 					n->newschema = $6;
9173 					n->missing_ok = false;
9174 					$$ = (Node *)n;
9175 				}
9176 			| ALTER TABLE relation_expr SET SCHEMA name
9177 				{
9178 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9179 					n->objectType = OBJECT_TABLE;
9180 					n->relation = $3;
9181 					n->newschema = $6;
9182 					n->missing_ok = false;
9183 					$$ = (Node *)n;
9184 				}
9185 			| ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
9186 				{
9187 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9188 					n->objectType = OBJECT_TABLE;
9189 					n->relation = $5;
9190 					n->newschema = $8;
9191 					n->missing_ok = true;
9192 					$$ = (Node *)n;
9193 				}
9194 			| ALTER STATISTICS any_name SET SCHEMA name
9195 				{
9196 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9197 					n->objectType = OBJECT_STATISTIC_EXT;
9198 					n->object = (Node *) $3;
9199 					n->newschema = $6;
9200 					n->missing_ok = false;
9201 					$$ = (Node *)n;
9202 				}
9203 			| ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
9204 				{
9205 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9206 					n->objectType = OBJECT_TSPARSER;
9207 					n->object = (Node *) $5;
9208 					n->newschema = $8;
9209 					n->missing_ok = false;
9210 					$$ = (Node *)n;
9211 				}
9212 			| ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
9213 				{
9214 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9215 					n->objectType = OBJECT_TSDICTIONARY;
9216 					n->object = (Node *) $5;
9217 					n->newschema = $8;
9218 					n->missing_ok = false;
9219 					$$ = (Node *)n;
9220 				}
9221 			| ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
9222 				{
9223 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9224 					n->objectType = OBJECT_TSTEMPLATE;
9225 					n->object = (Node *) $5;
9226 					n->newschema = $8;
9227 					n->missing_ok = false;
9228 					$$ = (Node *)n;
9229 				}
9230 			| ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
9231 				{
9232 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9233 					n->objectType = OBJECT_TSCONFIGURATION;
9234 					n->object = (Node *) $5;
9235 					n->newschema = $8;
9236 					n->missing_ok = false;
9237 					$$ = (Node *)n;
9238 				}
9239 			| ALTER SEQUENCE qualified_name SET SCHEMA name
9240 				{
9241 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9242 					n->objectType = OBJECT_SEQUENCE;
9243 					n->relation = $3;
9244 					n->newschema = $6;
9245 					n->missing_ok = false;
9246 					$$ = (Node *)n;
9247 				}
9248 			| ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
9249 				{
9250 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9251 					n->objectType = OBJECT_SEQUENCE;
9252 					n->relation = $5;
9253 					n->newschema = $8;
9254 					n->missing_ok = true;
9255 					$$ = (Node *)n;
9256 				}
9257 			| ALTER VIEW qualified_name SET SCHEMA name
9258 				{
9259 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9260 					n->objectType = OBJECT_VIEW;
9261 					n->relation = $3;
9262 					n->newschema = $6;
9263 					n->missing_ok = false;
9264 					$$ = (Node *)n;
9265 				}
9266 			| ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
9267 				{
9268 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9269 					n->objectType = OBJECT_VIEW;
9270 					n->relation = $5;
9271 					n->newschema = $8;
9272 					n->missing_ok = true;
9273 					$$ = (Node *)n;
9274 				}
9275 			| ALTER MATERIALIZED VIEW qualified_name SET SCHEMA name
9276 				{
9277 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9278 					n->objectType = OBJECT_MATVIEW;
9279 					n->relation = $4;
9280 					n->newschema = $7;
9281 					n->missing_ok = false;
9282 					$$ = (Node *)n;
9283 				}
9284 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name SET SCHEMA name
9285 				{
9286 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9287 					n->objectType = OBJECT_MATVIEW;
9288 					n->relation = $6;
9289 					n->newschema = $9;
9290 					n->missing_ok = true;
9291 					$$ = (Node *)n;
9292 				}
9293 			| ALTER FOREIGN TABLE relation_expr SET SCHEMA name
9294 				{
9295 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9296 					n->objectType = OBJECT_FOREIGN_TABLE;
9297 					n->relation = $4;
9298 					n->newschema = $7;
9299 					n->missing_ok = false;
9300 					$$ = (Node *)n;
9301 				}
9302 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
9303 				{
9304 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9305 					n->objectType = OBJECT_FOREIGN_TABLE;
9306 					n->relation = $6;
9307 					n->newschema = $9;
9308 					n->missing_ok = true;
9309 					$$ = (Node *)n;
9310 				}
9311 			| ALTER TYPE_P any_name SET SCHEMA name
9312 				{
9313 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9314 					n->objectType = OBJECT_TYPE;
9315 					n->object = (Node *) $3;
9316 					n->newschema = $6;
9317 					n->missing_ok = false;
9318 					$$ = (Node *)n;
9319 				}
9320 		;
9321 
9322 /*****************************************************************************
9323  *
9324  * ALTER OPERATOR name SET define
9325  *
9326  *****************************************************************************/
9327 
9328 AlterOperatorStmt:
9329 			ALTER OPERATOR operator_with_argtypes SET '(' operator_def_list ')'
9330 				{
9331 					AlterOperatorStmt *n = makeNode(AlterOperatorStmt);
9332 					n->opername = $3;
9333 					n->options = $6;
9334 					$$ = (Node *)n;
9335 				}
9336 		;
9337 
9338 operator_def_list:	operator_def_elem								{ $$ = list_make1($1); }
9339 			| operator_def_list ',' operator_def_elem				{ $$ = lappend($1, $3); }
9340 		;
9341 
9342 operator_def_elem: ColLabel '=' NONE
9343 						{ $$ = makeDefElem($1, NULL, @1); }
9344 				   | ColLabel '=' operator_def_arg
9345 						{ $$ = makeDefElem($1, (Node *) $3, @1); }
9346 		;
9347 
9348 /* must be similar enough to def_arg to avoid reduce/reduce conflicts */
9349 operator_def_arg:
9350 			func_type						{ $$ = (Node *)$1; }
9351 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
9352 			| qual_all_Op					{ $$ = (Node *)$1; }
9353 			| NumericOnly					{ $$ = (Node *)$1; }
9354 			| Sconst						{ $$ = (Node *)makeString($1); }
9355 		;
9356 
9357 /*****************************************************************************
9358  *
9359  * ALTER THING name OWNER TO newname
9360  *
9361  *****************************************************************************/
9362 
9363 AlterOwnerStmt: ALTER AGGREGATE aggregate_with_argtypes OWNER TO RoleSpec
9364 				{
9365 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9366 					n->objectType = OBJECT_AGGREGATE;
9367 					n->object = (Node *) $3;
9368 					n->newowner = $6;
9369 					$$ = (Node *)n;
9370 				}
9371 			| ALTER COLLATION any_name OWNER TO RoleSpec
9372 				{
9373 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9374 					n->objectType = OBJECT_COLLATION;
9375 					n->object = (Node *) $3;
9376 					n->newowner = $6;
9377 					$$ = (Node *)n;
9378 				}
9379 			| ALTER CONVERSION_P any_name OWNER TO RoleSpec
9380 				{
9381 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9382 					n->objectType = OBJECT_CONVERSION;
9383 					n->object = (Node *) $3;
9384 					n->newowner = $6;
9385 					$$ = (Node *)n;
9386 				}
9387 			| ALTER DATABASE database_name OWNER TO RoleSpec
9388 				{
9389 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9390 					n->objectType = OBJECT_DATABASE;
9391 					n->object = (Node *) makeString($3);
9392 					n->newowner = $6;
9393 					$$ = (Node *)n;
9394 				}
9395 			| ALTER DOMAIN_P any_name OWNER TO RoleSpec
9396 				{
9397 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9398 					n->objectType = OBJECT_DOMAIN;
9399 					n->object = (Node *) $3;
9400 					n->newowner = $6;
9401 					$$ = (Node *)n;
9402 				}
9403 			| ALTER FUNCTION function_with_argtypes OWNER TO RoleSpec
9404 				{
9405 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9406 					n->objectType = OBJECT_FUNCTION;
9407 					n->object = (Node *) $3;
9408 					n->newowner = $6;
9409 					$$ = (Node *)n;
9410 				}
9411 			| ALTER opt_procedural LANGUAGE name OWNER TO RoleSpec
9412 				{
9413 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9414 					n->objectType = OBJECT_LANGUAGE;
9415 					n->object = (Node *) makeString($4);
9416 					n->newowner = $7;
9417 					$$ = (Node *)n;
9418 				}
9419 			| ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleSpec
9420 				{
9421 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9422 					n->objectType = OBJECT_LARGEOBJECT;
9423 					n->object = (Node *) $4;
9424 					n->newowner = $7;
9425 					$$ = (Node *)n;
9426 				}
9427 			| ALTER OPERATOR operator_with_argtypes OWNER TO RoleSpec
9428 				{
9429 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9430 					n->objectType = OBJECT_OPERATOR;
9431 					n->object = (Node *) $3;
9432 					n->newowner = $6;
9433 					$$ = (Node *)n;
9434 				}
9435 			| ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleSpec
9436 				{
9437 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9438 					n->objectType = OBJECT_OPCLASS;
9439 					n->object = (Node *) lcons(makeString($6), $4);
9440 					n->newowner = $9;
9441 					$$ = (Node *)n;
9442 				}
9443 			| ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleSpec
9444 				{
9445 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9446 					n->objectType = OBJECT_OPFAMILY;
9447 					n->object = (Node *) lcons(makeString($6), $4);
9448 					n->newowner = $9;
9449 					$$ = (Node *)n;
9450 				}
9451 			| ALTER PROCEDURE function_with_argtypes OWNER TO RoleSpec
9452 				{
9453 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9454 					n->objectType = OBJECT_PROCEDURE;
9455 					n->object = (Node *) $3;
9456 					n->newowner = $6;
9457 					$$ = (Node *)n;
9458 				}
9459 			| ALTER ROUTINE function_with_argtypes OWNER TO RoleSpec
9460 				{
9461 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9462 					n->objectType = OBJECT_ROUTINE;
9463 					n->object = (Node *) $3;
9464 					n->newowner = $6;
9465 					$$ = (Node *)n;
9466 				}
9467 			| ALTER SCHEMA name OWNER TO RoleSpec
9468 				{
9469 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9470 					n->objectType = OBJECT_SCHEMA;
9471 					n->object = (Node *) makeString($3);
9472 					n->newowner = $6;
9473 					$$ = (Node *)n;
9474 				}
9475 			| ALTER TYPE_P any_name OWNER TO RoleSpec
9476 				{
9477 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9478 					n->objectType = OBJECT_TYPE;
9479 					n->object = (Node *) $3;
9480 					n->newowner = $6;
9481 					$$ = (Node *)n;
9482 				}
9483 			| ALTER TABLESPACE name OWNER TO RoleSpec
9484 				{
9485 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9486 					n->objectType = OBJECT_TABLESPACE;
9487 					n->object = (Node *) makeString($3);
9488 					n->newowner = $6;
9489 					$$ = (Node *)n;
9490 				}
9491 			| ALTER STATISTICS any_name OWNER TO RoleSpec
9492 				{
9493 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9494 					n->objectType = OBJECT_STATISTIC_EXT;
9495 					n->object = (Node *) $3;
9496 					n->newowner = $6;
9497 					$$ = (Node *)n;
9498 				}
9499 			| ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleSpec
9500 				{
9501 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9502 					n->objectType = OBJECT_TSDICTIONARY;
9503 					n->object = (Node *) $5;
9504 					n->newowner = $8;
9505 					$$ = (Node *)n;
9506 				}
9507 			| ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleSpec
9508 				{
9509 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9510 					n->objectType = OBJECT_TSCONFIGURATION;
9511 					n->object = (Node *) $5;
9512 					n->newowner = $8;
9513 					$$ = (Node *)n;
9514 				}
9515 			| ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleSpec
9516 				{
9517 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9518 					n->objectType = OBJECT_FDW;
9519 					n->object = (Node *) makeString($5);
9520 					n->newowner = $8;
9521 					$$ = (Node *)n;
9522 				}
9523 			| ALTER SERVER name OWNER TO RoleSpec
9524 				{
9525 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9526 					n->objectType = OBJECT_FOREIGN_SERVER;
9527 					n->object = (Node *) makeString($3);
9528 					n->newowner = $6;
9529 					$$ = (Node *)n;
9530 				}
9531 			| ALTER EVENT TRIGGER name OWNER TO RoleSpec
9532 				{
9533 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9534 					n->objectType = OBJECT_EVENT_TRIGGER;
9535 					n->object = (Node *) makeString($4);
9536 					n->newowner = $7;
9537 					$$ = (Node *)n;
9538 				}
9539 			| ALTER PUBLICATION name OWNER TO RoleSpec
9540 				{
9541 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9542 					n->objectType = OBJECT_PUBLICATION;
9543 					n->object = (Node *) makeString($3);
9544 					n->newowner = $6;
9545 					$$ = (Node *)n;
9546 				}
9547 			| ALTER SUBSCRIPTION name OWNER TO RoleSpec
9548 				{
9549 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9550 					n->objectType = OBJECT_SUBSCRIPTION;
9551 					n->object = (Node *) makeString($3);
9552 					n->newowner = $6;
9553 					$$ = (Node *)n;
9554 				}
9555 		;
9556 
9557 
9558 /*****************************************************************************
9559  *
9560  * CREATE PUBLICATION name [ FOR TABLE ] [ WITH options ]
9561  *
9562  *****************************************************************************/
9563 
9564 CreatePublicationStmt:
9565 			CREATE PUBLICATION name opt_publication_for_tables opt_definition
9566 				{
9567 					CreatePublicationStmt *n = makeNode(CreatePublicationStmt);
9568 					n->pubname = $3;
9569 					n->options = $5;
9570 					if ($4 != NULL)
9571 					{
9572 						/* FOR TABLE */
9573 						if (IsA($4, List))
9574 							n->tables = (List *)$4;
9575 						/* FOR ALL TABLES */
9576 						else
9577 							n->for_all_tables = true;
9578 					}
9579 					$$ = (Node *)n;
9580 				}
9581 		;
9582 
9583 opt_publication_for_tables:
9584 			publication_for_tables					{ $$ = $1; }
9585 			| /* EMPTY */							{ $$ = NULL; }
9586 		;
9587 
9588 publication_for_tables:
9589 			FOR TABLE relation_expr_list
9590 				{
9591 					$$ = (Node *) $3;
9592 				}
9593 			| FOR ALL TABLES
9594 				{
9595 					$$ = (Node *) makeInteger(true);
9596 				}
9597 		;
9598 
9599 
9600 /*****************************************************************************
9601  *
9602  * ALTER PUBLICATION name SET ( options )
9603  *
9604  * ALTER PUBLICATION name ADD TABLE table [, table2]
9605  *
9606  * ALTER PUBLICATION name DROP TABLE table [, table2]
9607  *
9608  * ALTER PUBLICATION name SET TABLE table [, table2]
9609  *
9610  *****************************************************************************/
9611 
9612 AlterPublicationStmt:
9613 			ALTER PUBLICATION name SET definition
9614 				{
9615 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9616 					n->pubname = $3;
9617 					n->options = $5;
9618 					$$ = (Node *)n;
9619 				}
9620 			| ALTER PUBLICATION name ADD_P TABLE relation_expr_list
9621 				{
9622 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9623 					n->pubname = $3;
9624 					n->tables = $6;
9625 					n->tableAction = DEFELEM_ADD;
9626 					$$ = (Node *)n;
9627 				}
9628 			| ALTER PUBLICATION name SET TABLE relation_expr_list
9629 				{
9630 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9631 					n->pubname = $3;
9632 					n->tables = $6;
9633 					n->tableAction = DEFELEM_SET;
9634 					$$ = (Node *)n;
9635 				}
9636 			| ALTER PUBLICATION name DROP TABLE relation_expr_list
9637 				{
9638 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9639 					n->pubname = $3;
9640 					n->tables = $6;
9641 					n->tableAction = DEFELEM_DROP;
9642 					$$ = (Node *)n;
9643 				}
9644 		;
9645 
9646 /*****************************************************************************
9647  *
9648  * CREATE SUBSCRIPTION name ...
9649  *
9650  *****************************************************************************/
9651 
9652 CreateSubscriptionStmt:
9653 			CREATE SUBSCRIPTION name CONNECTION Sconst PUBLICATION publication_name_list opt_definition
9654 				{
9655 					CreateSubscriptionStmt *n =
9656 						makeNode(CreateSubscriptionStmt);
9657 					n->subname = $3;
9658 					n->conninfo = $5;
9659 					n->publication = $7;
9660 					n->options = $8;
9661 					$$ = (Node *)n;
9662 				}
9663 		;
9664 
9665 publication_name_list:
9666 			publication_name_item
9667 				{
9668 					$$ = list_make1($1);
9669 				}
9670 			| publication_name_list ',' publication_name_item
9671 				{
9672 					$$ = lappend($1, $3);
9673 				}
9674 		;
9675 
9676 publication_name_item:
9677 			ColLabel			{ $$ = makeString($1); };
9678 
9679 /*****************************************************************************
9680  *
9681  * ALTER SUBSCRIPTION name ...
9682  *
9683  *****************************************************************************/
9684 
9685 AlterSubscriptionStmt:
9686 			ALTER SUBSCRIPTION name SET definition
9687 				{
9688 					AlterSubscriptionStmt *n =
9689 						makeNode(AlterSubscriptionStmt);
9690 					n->kind = ALTER_SUBSCRIPTION_OPTIONS;
9691 					n->subname = $3;
9692 					n->options = $5;
9693 					$$ = (Node *)n;
9694 				}
9695 			| ALTER SUBSCRIPTION name CONNECTION Sconst
9696 				{
9697 					AlterSubscriptionStmt *n =
9698 						makeNode(AlterSubscriptionStmt);
9699 					n->kind = ALTER_SUBSCRIPTION_CONNECTION;
9700 					n->subname = $3;
9701 					n->conninfo = $5;
9702 					$$ = (Node *)n;
9703 				}
9704 			| ALTER SUBSCRIPTION name REFRESH PUBLICATION opt_definition
9705 				{
9706 					AlterSubscriptionStmt *n =
9707 						makeNode(AlterSubscriptionStmt);
9708 					n->kind = ALTER_SUBSCRIPTION_REFRESH;
9709 					n->subname = $3;
9710 					n->options = $6;
9711 					$$ = (Node *)n;
9712 				}
9713 			| ALTER SUBSCRIPTION name SET PUBLICATION publication_name_list opt_definition
9714 				{
9715 					AlterSubscriptionStmt *n =
9716 						makeNode(AlterSubscriptionStmt);
9717 					n->kind = ALTER_SUBSCRIPTION_PUBLICATION;
9718 					n->subname = $3;
9719 					n->publication = $6;
9720 					n->options = $7;
9721 					$$ = (Node *)n;
9722 				}
9723 			| ALTER SUBSCRIPTION name ENABLE_P
9724 				{
9725 					AlterSubscriptionStmt *n =
9726 						makeNode(AlterSubscriptionStmt);
9727 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9728 					n->subname = $3;
9729 					n->options = list_make1(makeDefElem("enabled",
9730 											(Node *)makeInteger(true), @1));
9731 					$$ = (Node *)n;
9732 				}
9733 			| ALTER SUBSCRIPTION name DISABLE_P
9734 				{
9735 					AlterSubscriptionStmt *n =
9736 						makeNode(AlterSubscriptionStmt);
9737 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9738 					n->subname = $3;
9739 					n->options = list_make1(makeDefElem("enabled",
9740 											(Node *)makeInteger(false), @1));
9741 					$$ = (Node *)n;
9742 				}
9743 		;
9744 
9745 /*****************************************************************************
9746  *
9747  * DROP SUBSCRIPTION [ IF EXISTS ] name
9748  *
9749  *****************************************************************************/
9750 
9751 DropSubscriptionStmt: DROP SUBSCRIPTION name opt_drop_behavior
9752 				{
9753 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9754 					n->subname = $3;
9755 					n->missing_ok = false;
9756 					n->behavior = $4;
9757 					$$ = (Node *) n;
9758 				}
9759 				|  DROP SUBSCRIPTION IF_P EXISTS name opt_drop_behavior
9760 				{
9761 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9762 					n->subname = $5;
9763 					n->missing_ok = true;
9764 					n->behavior = $6;
9765 					$$ = (Node *) n;
9766 				}
9767 		;
9768 
9769 /*****************************************************************************
9770  *
9771  *		QUERY:	Define Rewrite Rule
9772  *
9773  *****************************************************************************/
9774 
9775 RuleStmt:	CREATE opt_or_replace RULE name AS
9776 			ON event TO qualified_name where_clause
9777 			DO opt_instead RuleActionList
9778 				{
9779 					RuleStmt *n = makeNode(RuleStmt);
9780 					n->replace = $2;
9781 					n->relation = $9;
9782 					n->rulename = $4;
9783 					n->whereClause = $10;
9784 					n->event = $7;
9785 					n->instead = $12;
9786 					n->actions = $13;
9787 					$$ = (Node *)n;
9788 				}
9789 		;
9790 
9791 RuleActionList:
9792 			NOTHING									{ $$ = NIL; }
9793 			| RuleActionStmt						{ $$ = list_make1($1); }
9794 			| '(' RuleActionMulti ')'				{ $$ = $2; }
9795 		;
9796 
9797 /* the thrashing around here is to discard "empty" statements... */
9798 RuleActionMulti:
9799 			RuleActionMulti ';' RuleActionStmtOrEmpty
9800 				{ if ($3 != NULL)
9801 					$$ = lappend($1, $3);
9802 				  else
9803 					$$ = $1;
9804 				}
9805 			| RuleActionStmtOrEmpty
9806 				{ if ($1 != NULL)
9807 					$$ = list_make1($1);
9808 				  else
9809 					$$ = NIL;
9810 				}
9811 		;
9812 
9813 RuleActionStmt:
9814 			SelectStmt
9815 			| InsertStmt
9816 			| UpdateStmt
9817 			| DeleteStmt
9818 			| NotifyStmt
9819 		;
9820 
9821 RuleActionStmtOrEmpty:
9822 			RuleActionStmt							{ $$ = $1; }
9823 			|	/*EMPTY*/							{ $$ = NULL; }
9824 		;
9825 
9826 event:		SELECT									{ $$ = CMD_SELECT; }
9827 			| UPDATE								{ $$ = CMD_UPDATE; }
9828 			| DELETE_P								{ $$ = CMD_DELETE; }
9829 			| INSERT								{ $$ = CMD_INSERT; }
9830 		 ;
9831 
9832 opt_instead:
9833 			INSTEAD									{ $$ = true; }
9834 			| ALSO									{ $$ = false; }
9835 			| /*EMPTY*/								{ $$ = false; }
9836 		;
9837 
9838 
9839 /*****************************************************************************
9840  *
9841  *		QUERY:
9842  *				NOTIFY <identifier> can appear both in rule bodies and
9843  *				as a query-level command
9844  *
9845  *****************************************************************************/
9846 
9847 NotifyStmt: NOTIFY ColId notify_payload
9848 				{
9849 					NotifyStmt *n = makeNode(NotifyStmt);
9850 					n->conditionname = $2;
9851 					n->payload = $3;
9852 					$$ = (Node *)n;
9853 				}
9854 		;
9855 
9856 notify_payload:
9857 			',' Sconst							{ $$ = $2; }
9858 			| /*EMPTY*/							{ $$ = NULL; }
9859 		;
9860 
9861 ListenStmt: LISTEN ColId
9862 				{
9863 					ListenStmt *n = makeNode(ListenStmt);
9864 					n->conditionname = $2;
9865 					$$ = (Node *)n;
9866 				}
9867 		;
9868 
9869 UnlistenStmt:
9870 			UNLISTEN ColId
9871 				{
9872 					UnlistenStmt *n = makeNode(UnlistenStmt);
9873 					n->conditionname = $2;
9874 					$$ = (Node *)n;
9875 				}
9876 			| UNLISTEN '*'
9877 				{
9878 					UnlistenStmt *n = makeNode(UnlistenStmt);
9879 					n->conditionname = NULL;
9880 					$$ = (Node *)n;
9881 				}
9882 		;
9883 
9884 
9885 /*****************************************************************************
9886  *
9887  *		Transactions:
9888  *
9889  *		BEGIN / COMMIT / ROLLBACK
9890  *		(also older versions END / ABORT)
9891  *
9892  *****************************************************************************/
9893 
9894 TransactionStmt:
9895 			ABORT_P opt_transaction opt_transaction_chain
9896 				{
9897 					TransactionStmt *n = makeNode(TransactionStmt);
9898 					n->kind = TRANS_STMT_ROLLBACK;
9899 					n->options = NIL;
9900 					n->chain = $3;
9901 					$$ = (Node *)n;
9902 				}
9903 			| BEGIN_P opt_transaction transaction_mode_list_or_empty
9904 				{
9905 					TransactionStmt *n = makeNode(TransactionStmt);
9906 					n->kind = TRANS_STMT_BEGIN;
9907 					n->options = $3;
9908 					$$ = (Node *)n;
9909 				}
9910 			| START TRANSACTION transaction_mode_list_or_empty
9911 				{
9912 					TransactionStmt *n = makeNode(TransactionStmt);
9913 					n->kind = TRANS_STMT_START;
9914 					n->options = $3;
9915 					$$ = (Node *)n;
9916 				}
9917 			| COMMIT opt_transaction opt_transaction_chain
9918 				{
9919 					TransactionStmt *n = makeNode(TransactionStmt);
9920 					n->kind = TRANS_STMT_COMMIT;
9921 					n->options = NIL;
9922 					n->chain = $3;
9923 					$$ = (Node *)n;
9924 				}
9925 			| END_P opt_transaction opt_transaction_chain
9926 				{
9927 					TransactionStmt *n = makeNode(TransactionStmt);
9928 					n->kind = TRANS_STMT_COMMIT;
9929 					n->options = NIL;
9930 					n->chain = $3;
9931 					$$ = (Node *)n;
9932 				}
9933 			| ROLLBACK opt_transaction opt_transaction_chain
9934 				{
9935 					TransactionStmt *n = makeNode(TransactionStmt);
9936 					n->kind = TRANS_STMT_ROLLBACK;
9937 					n->options = NIL;
9938 					n->chain = $3;
9939 					$$ = (Node *)n;
9940 				}
9941 			| SAVEPOINT ColId
9942 				{
9943 					TransactionStmt *n = makeNode(TransactionStmt);
9944 					n->kind = TRANS_STMT_SAVEPOINT;
9945 					n->savepoint_name = $2;
9946 					$$ = (Node *)n;
9947 				}
9948 			| RELEASE SAVEPOINT ColId
9949 				{
9950 					TransactionStmt *n = makeNode(TransactionStmt);
9951 					n->kind = TRANS_STMT_RELEASE;
9952 					n->savepoint_name = $3;
9953 					$$ = (Node *)n;
9954 				}
9955 			| RELEASE ColId
9956 				{
9957 					TransactionStmt *n = makeNode(TransactionStmt);
9958 					n->kind = TRANS_STMT_RELEASE;
9959 					n->savepoint_name = $2;
9960 					$$ = (Node *)n;
9961 				}
9962 			| ROLLBACK opt_transaction TO SAVEPOINT ColId
9963 				{
9964 					TransactionStmt *n = makeNode(TransactionStmt);
9965 					n->kind = TRANS_STMT_ROLLBACK_TO;
9966 					n->savepoint_name = $5;
9967 					$$ = (Node *)n;
9968 				}
9969 			| ROLLBACK opt_transaction TO ColId
9970 				{
9971 					TransactionStmt *n = makeNode(TransactionStmt);
9972 					n->kind = TRANS_STMT_ROLLBACK_TO;
9973 					n->savepoint_name = $4;
9974 					$$ = (Node *)n;
9975 				}
9976 			| PREPARE TRANSACTION Sconst
9977 				{
9978 					TransactionStmt *n = makeNode(TransactionStmt);
9979 					n->kind = TRANS_STMT_PREPARE;
9980 					n->gid = $3;
9981 					$$ = (Node *)n;
9982 				}
9983 			| COMMIT PREPARED Sconst
9984 				{
9985 					TransactionStmt *n = makeNode(TransactionStmt);
9986 					n->kind = TRANS_STMT_COMMIT_PREPARED;
9987 					n->gid = $3;
9988 					$$ = (Node *)n;
9989 				}
9990 			| ROLLBACK PREPARED Sconst
9991 				{
9992 					TransactionStmt *n = makeNode(TransactionStmt);
9993 					n->kind = TRANS_STMT_ROLLBACK_PREPARED;
9994 					n->gid = $3;
9995 					$$ = (Node *)n;
9996 				}
9997 		;
9998 
9999 opt_transaction:	WORK							{}
10000 			| TRANSACTION							{}
10001 			| /*EMPTY*/								{}
10002 		;
10003 
10004 transaction_mode_item:
10005 			ISOLATION LEVEL iso_level
10006 					{ $$ = makeDefElem("transaction_isolation",
10007 									   makeStringConst($3, @3), @1); }
10008 			| READ ONLY
10009 					{ $$ = makeDefElem("transaction_read_only",
10010 									   makeIntConst(true, @1), @1); }
10011 			| READ WRITE
10012 					{ $$ = makeDefElem("transaction_read_only",
10013 									   makeIntConst(false, @1), @1); }
10014 			| DEFERRABLE
10015 					{ $$ = makeDefElem("transaction_deferrable",
10016 									   makeIntConst(true, @1), @1); }
10017 			| NOT DEFERRABLE
10018 					{ $$ = makeDefElem("transaction_deferrable",
10019 									   makeIntConst(false, @1), @1); }
10020 		;
10021 
10022 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
10023 transaction_mode_list:
10024 			transaction_mode_item
10025 					{ $$ = list_make1($1); }
10026 			| transaction_mode_list ',' transaction_mode_item
10027 					{ $$ = lappend($1, $3); }
10028 			| transaction_mode_list transaction_mode_item
10029 					{ $$ = lappend($1, $2); }
10030 		;
10031 
10032 transaction_mode_list_or_empty:
10033 			transaction_mode_list
10034 			| /* EMPTY */
10035 					{ $$ = NIL; }
10036 		;
10037 
10038 opt_transaction_chain:
10039 			AND CHAIN		{ $$ = true; }
10040 			| AND NO CHAIN	{ $$ = false; }
10041 			| /* EMPTY */	{ $$ = false; }
10042 		;
10043 
10044 
10045 /*****************************************************************************
10046  *
10047  *	QUERY:
10048  *		CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
10049  *			AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
10050  *
10051  *****************************************************************************/
10052 
10053 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10054 				AS SelectStmt opt_check_option
10055 				{
10056 					ViewStmt *n = makeNode(ViewStmt);
10057 					n->view = $4;
10058 					n->view->relpersistence = $2;
10059 					n->aliases = $5;
10060 					n->query = $8;
10061 					n->replace = false;
10062 					n->options = $6;
10063 					n->withCheckOption = $9;
10064 					$$ = (Node *) n;
10065 				}
10066 		| CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10067 				AS SelectStmt opt_check_option
10068 				{
10069 					ViewStmt *n = makeNode(ViewStmt);
10070 					n->view = $6;
10071 					n->view->relpersistence = $4;
10072 					n->aliases = $7;
10073 					n->query = $10;
10074 					n->replace = true;
10075 					n->options = $8;
10076 					n->withCheckOption = $11;
10077 					$$ = (Node *) n;
10078 				}
10079 		| CREATE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10080 				AS SelectStmt opt_check_option
10081 				{
10082 					ViewStmt *n = makeNode(ViewStmt);
10083 					n->view = $5;
10084 					n->view->relpersistence = $2;
10085 					n->aliases = $7;
10086 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $11);
10087 					n->replace = false;
10088 					n->options = $9;
10089 					n->withCheckOption = $12;
10090 					if (n->withCheckOption != NO_CHECK_OPTION)
10091 						ereport(ERROR,
10092 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10093 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
10094 								 parser_errposition(@12)));
10095 					$$ = (Node *) n;
10096 				}
10097 		| CREATE OR REPLACE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10098 				AS SelectStmt opt_check_option
10099 				{
10100 					ViewStmt *n = makeNode(ViewStmt);
10101 					n->view = $7;
10102 					n->view->relpersistence = $4;
10103 					n->aliases = $9;
10104 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $13);
10105 					n->replace = true;
10106 					n->options = $11;
10107 					n->withCheckOption = $14;
10108 					if (n->withCheckOption != NO_CHECK_OPTION)
10109 						ereport(ERROR,
10110 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10111 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
10112 								 parser_errposition(@14)));
10113 					$$ = (Node *) n;
10114 				}
10115 		;
10116 
10117 opt_check_option:
10118 		WITH CHECK OPTION				{ $$ = CASCADED_CHECK_OPTION; }
10119 		| WITH CASCADED CHECK OPTION	{ $$ = CASCADED_CHECK_OPTION; }
10120 		| WITH LOCAL CHECK OPTION		{ $$ = LOCAL_CHECK_OPTION; }
10121 		| /* EMPTY */					{ $$ = NO_CHECK_OPTION; }
10122 		;
10123 
10124 /*****************************************************************************
10125  *
10126  *		QUERY:
10127  *				LOAD "filename"
10128  *
10129  *****************************************************************************/
10130 
10131 LoadStmt:	LOAD file_name
10132 				{
10133 					LoadStmt *n = makeNode(LoadStmt);
10134 					n->filename = $2;
10135 					$$ = (Node *)n;
10136 				}
10137 		;
10138 
10139 
10140 /*****************************************************************************
10141  *
10142  *		CREATE DATABASE
10143  *
10144  *****************************************************************************/
10145 
10146 CreatedbStmt:
10147 			CREATE DATABASE database_name opt_with createdb_opt_list
10148 				{
10149 					CreatedbStmt *n = makeNode(CreatedbStmt);
10150 					n->dbname = $3;
10151 					n->options = $5;
10152 					$$ = (Node *)n;
10153 				}
10154 		;
10155 
10156 createdb_opt_list:
10157 			createdb_opt_items						{ $$ = $1; }
10158 			| /* EMPTY */							{ $$ = NIL; }
10159 		;
10160 
10161 createdb_opt_items:
10162 			createdb_opt_item						{ $$ = list_make1($1); }
10163 			| createdb_opt_items createdb_opt_item	{ $$ = lappend($1, $2); }
10164 		;
10165 
10166 createdb_opt_item:
10167 			createdb_opt_name opt_equal SignedIconst
10168 				{
10169 					$$ = makeDefElem($1, (Node *)makeInteger($3), @1);
10170 				}
10171 			| createdb_opt_name opt_equal opt_boolean_or_string
10172 				{
10173 					$$ = makeDefElem($1, (Node *)makeString($3), @1);
10174 				}
10175 			| createdb_opt_name opt_equal DEFAULT
10176 				{
10177 					$$ = makeDefElem($1, NULL, @1);
10178 				}
10179 		;
10180 
10181 /*
10182  * Ideally we'd use ColId here, but that causes shift/reduce conflicts against
10183  * the ALTER DATABASE SET/RESET syntaxes.  Instead call out specific keywords
10184  * we need, and allow IDENT so that database option names don't have to be
10185  * parser keywords unless they are already keywords for other reasons.
10186  *
10187  * XXX this coding technique is fragile since if someone makes a formerly
10188  * non-keyword option name into a keyword and forgets to add it here, the
10189  * option will silently break.  Best defense is to provide a regression test
10190  * exercising every such option, at least at the syntax level.
10191  */
10192 createdb_opt_name:
10193 			IDENT							{ $$ = $1; }
10194 			| CONNECTION LIMIT				{ $$ = pstrdup("connection_limit"); }
10195 			| ENCODING						{ $$ = pstrdup($1); }
10196 			| LOCATION						{ $$ = pstrdup($1); }
10197 			| OWNER							{ $$ = pstrdup($1); }
10198 			| TABLESPACE					{ $$ = pstrdup($1); }
10199 			| TEMPLATE						{ $$ = pstrdup($1); }
10200 		;
10201 
10202 /*
10203  *	Though the equals sign doesn't match other WITH options, pg_dump uses
10204  *	equals for backward compatibility, and it doesn't seem worth removing it.
10205  */
10206 opt_equal:	'='										{}
10207 			| /*EMPTY*/								{}
10208 		;
10209 
10210 
10211 /*****************************************************************************
10212  *
10213  *		ALTER DATABASE
10214  *
10215  *****************************************************************************/
10216 
10217 AlterDatabaseStmt:
10218 			ALTER DATABASE database_name WITH createdb_opt_list
10219 				 {
10220 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10221 					n->dbname = $3;
10222 					n->options = $5;
10223 					$$ = (Node *)n;
10224 				 }
10225 			| ALTER DATABASE database_name createdb_opt_list
10226 				 {
10227 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10228 					n->dbname = $3;
10229 					n->options = $4;
10230 					$$ = (Node *)n;
10231 				 }
10232 			| ALTER DATABASE database_name SET TABLESPACE name
10233 				 {
10234 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10235 					n->dbname = $3;
10236 					n->options = list_make1(makeDefElem("tablespace",
10237 														(Node *)makeString($6), @6));
10238 					$$ = (Node *)n;
10239 				 }
10240 		;
10241 
10242 AlterDatabaseSetStmt:
10243 			ALTER DATABASE database_name SetResetClause
10244 				{
10245 					AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
10246 					n->dbname = $3;
10247 					n->setstmt = $4;
10248 					$$ = (Node *)n;
10249 				}
10250 		;
10251 
10252 
10253 /*****************************************************************************
10254  *
10255  *		DROP DATABASE [ IF EXISTS ]
10256  *
10257  * This is implicitly CASCADE, no need for drop behavior
10258  *****************************************************************************/
10259 
10260 DropdbStmt: DROP DATABASE database_name
10261 				{
10262 					DropdbStmt *n = makeNode(DropdbStmt);
10263 					n->dbname = $3;
10264 					n->missing_ok = false;
10265 					$$ = (Node *)n;
10266 				}
10267 			| DROP DATABASE IF_P EXISTS database_name
10268 				{
10269 					DropdbStmt *n = makeNode(DropdbStmt);
10270 					n->dbname = $5;
10271 					n->missing_ok = true;
10272 					$$ = (Node *)n;
10273 				}
10274 		;
10275 
10276 
10277 /*****************************************************************************
10278  *
10279  *		ALTER COLLATION
10280  *
10281  *****************************************************************************/
10282 
10283 AlterCollationStmt: ALTER COLLATION any_name REFRESH VERSION_P
10284 				{
10285 					AlterCollationStmt *n = makeNode(AlterCollationStmt);
10286 					n->collname = $3;
10287 					$$ = (Node *)n;
10288 				}
10289 		;
10290 
10291 
10292 /*****************************************************************************
10293  *
10294  *		ALTER SYSTEM
10295  *
10296  * This is used to change configuration parameters persistently.
10297  *****************************************************************************/
10298 
10299 AlterSystemStmt:
10300 			ALTER SYSTEM_P SET generic_set
10301 				{
10302 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
10303 					n->setstmt = $4;
10304 					$$ = (Node *)n;
10305 				}
10306 			| ALTER SYSTEM_P RESET generic_reset
10307 				{
10308 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
10309 					n->setstmt = $4;
10310 					$$ = (Node *)n;
10311 				}
10312 		;
10313 
10314 
10315 /*****************************************************************************
10316  *
10317  * Manipulate a domain
10318  *
10319  *****************************************************************************/
10320 
10321 CreateDomainStmt:
10322 			CREATE DOMAIN_P any_name opt_as Typename ColQualList
10323 				{
10324 					CreateDomainStmt *n = makeNode(CreateDomainStmt);
10325 					n->domainname = $3;
10326 					n->typeName = $5;
10327 					SplitColQualList($6, &n->constraints, &n->collClause,
10328 									 yyscanner);
10329 					$$ = (Node *)n;
10330 				}
10331 		;
10332 
10333 AlterDomainStmt:
10334 			/* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
10335 			ALTER DOMAIN_P any_name alter_column_default
10336 				{
10337 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10338 					n->subtype = 'T';
10339 					n->typeName = $3;
10340 					n->def = $4;
10341 					$$ = (Node *)n;
10342 				}
10343 			/* ALTER DOMAIN <domain> DROP NOT NULL */
10344 			| ALTER DOMAIN_P any_name DROP NOT NULL_P
10345 				{
10346 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10347 					n->subtype = 'N';
10348 					n->typeName = $3;
10349 					$$ = (Node *)n;
10350 				}
10351 			/* ALTER DOMAIN <domain> SET NOT NULL */
10352 			| ALTER DOMAIN_P any_name SET NOT NULL_P
10353 				{
10354 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10355 					n->subtype = 'O';
10356 					n->typeName = $3;
10357 					$$ = (Node *)n;
10358 				}
10359 			/* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
10360 			| ALTER DOMAIN_P any_name ADD_P TableConstraint
10361 				{
10362 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10363 					n->subtype = 'C';
10364 					n->typeName = $3;
10365 					n->def = $5;
10366 					$$ = (Node *)n;
10367 				}
10368 			/* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
10369 			| ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
10370 				{
10371 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10372 					n->subtype = 'X';
10373 					n->typeName = $3;
10374 					n->name = $6;
10375 					n->behavior = $7;
10376 					n->missing_ok = false;
10377 					$$ = (Node *)n;
10378 				}
10379 			/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
10380 			| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
10381 				{
10382 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10383 					n->subtype = 'X';
10384 					n->typeName = $3;
10385 					n->name = $8;
10386 					n->behavior = $9;
10387 					n->missing_ok = true;
10388 					$$ = (Node *)n;
10389 				}
10390 			/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
10391 			| ALTER DOMAIN_P any_name VALIDATE CONSTRAINT name
10392 				{
10393 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10394 					n->subtype = 'V';
10395 					n->typeName = $3;
10396 					n->name = $6;
10397 					$$ = (Node *)n;
10398 				}
10399 			;
10400 
10401 opt_as:		AS										{}
10402 			| /* EMPTY */							{}
10403 		;
10404 
10405 
10406 /*****************************************************************************
10407  *
10408  * Manipulate a text search dictionary or configuration
10409  *
10410  *****************************************************************************/
10411 
10412 AlterTSDictionaryStmt:
10413 			ALTER TEXT_P SEARCH DICTIONARY any_name definition
10414 				{
10415 					AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
10416 					n->dictname = $5;
10417 					n->options = $6;
10418 					$$ = (Node *)n;
10419 				}
10420 		;
10421 
10422 AlterTSConfigurationStmt:
10423 			ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list any_with any_name_list
10424 				{
10425 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10426 					n->kind = ALTER_TSCONFIG_ADD_MAPPING;
10427 					n->cfgname = $5;
10428 					n->tokentype = $9;
10429 					n->dicts = $11;
10430 					n->override = false;
10431 					n->replace = false;
10432 					$$ = (Node*)n;
10433 				}
10434 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list any_with any_name_list
10435 				{
10436 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10437 					n->kind = ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN;
10438 					n->cfgname = $5;
10439 					n->tokentype = $9;
10440 					n->dicts = $11;
10441 					n->override = true;
10442 					n->replace = false;
10443 					$$ = (Node*)n;
10444 				}
10445 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name any_with any_name
10446 				{
10447 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10448 					n->kind = ALTER_TSCONFIG_REPLACE_DICT;
10449 					n->cfgname = $5;
10450 					n->tokentype = NIL;
10451 					n->dicts = list_make2($9,$11);
10452 					n->override = false;
10453 					n->replace = true;
10454 					$$ = (Node*)n;
10455 				}
10456 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name any_with any_name
10457 				{
10458 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10459 					n->kind = ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN;
10460 					n->cfgname = $5;
10461 					n->tokentype = $9;
10462 					n->dicts = list_make2($11,$13);
10463 					n->override = false;
10464 					n->replace = true;
10465 					$$ = (Node*)n;
10466 				}
10467 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
10468 				{
10469 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10470 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10471 					n->cfgname = $5;
10472 					n->tokentype = $9;
10473 					n->missing_ok = false;
10474 					$$ = (Node*)n;
10475 				}
10476 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
10477 				{
10478 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10479 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10480 					n->cfgname = $5;
10481 					n->tokentype = $11;
10482 					n->missing_ok = true;
10483 					$$ = (Node*)n;
10484 				}
10485 		;
10486 
10487 /* Use this if TIME or ORDINALITY after WITH should be taken as an identifier */
10488 any_with:	WITH									{}
10489 			| WITH_LA								{}
10490 		;
10491 
10492 
10493 /*****************************************************************************
10494  *
10495  * Manipulate a conversion
10496  *
10497  *		CREATE [DEFAULT] CONVERSION <conversion_name>
10498  *		FOR <encoding_name> TO <encoding_name> FROM <func_name>
10499  *
10500  *****************************************************************************/
10501 
10502 CreateConversionStmt:
10503 			CREATE opt_default CONVERSION_P any_name FOR Sconst
10504 			TO Sconst FROM any_name
10505 			{
10506 				CreateConversionStmt *n = makeNode(CreateConversionStmt);
10507 				n->conversion_name = $4;
10508 				n->for_encoding_name = $6;
10509 				n->to_encoding_name = $8;
10510 				n->func_name = $10;
10511 				n->def = $2;
10512 				$$ = (Node *)n;
10513 			}
10514 		;
10515 
10516 /*****************************************************************************
10517  *
10518  *		QUERY:
10519  *				CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
10520  *				CLUSTER [VERBOSE]
10521  *				CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
10522  *
10523  *****************************************************************************/
10524 
10525 ClusterStmt:
10526 			CLUSTER opt_verbose qualified_name cluster_index_specification
10527 				{
10528 					ClusterStmt *n = makeNode(ClusterStmt);
10529 					n->relation = $3;
10530 					n->indexname = $4;
10531 					n->options = 0;
10532 					if ($2)
10533 						n->options |= CLUOPT_VERBOSE;
10534 					$$ = (Node*)n;
10535 				}
10536 			| CLUSTER opt_verbose
10537 				{
10538 					ClusterStmt *n = makeNode(ClusterStmt);
10539 					n->relation = NULL;
10540 					n->indexname = NULL;
10541 					n->options = 0;
10542 					if ($2)
10543 						n->options |= CLUOPT_VERBOSE;
10544 					$$ = (Node*)n;
10545 				}
10546 			/* kept for pre-8.3 compatibility */
10547 			| CLUSTER opt_verbose index_name ON qualified_name
10548 				{
10549 					ClusterStmt *n = makeNode(ClusterStmt);
10550 					n->relation = $5;
10551 					n->indexname = $3;
10552 					n->options = 0;
10553 					if ($2)
10554 						n->options |= CLUOPT_VERBOSE;
10555 					$$ = (Node*)n;
10556 				}
10557 		;
10558 
10559 cluster_index_specification:
10560 			USING index_name		{ $$ = $2; }
10561 			| /*EMPTY*/				{ $$ = NULL; }
10562 		;
10563 
10564 
10565 /*****************************************************************************
10566  *
10567  *		QUERY:
10568  *				VACUUM
10569  *				ANALYZE
10570  *
10571  *****************************************************************************/
10572 
10573 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose opt_analyze opt_vacuum_relation_list
10574 				{
10575 					VacuumStmt *n = makeNode(VacuumStmt);
10576 					n->options = NIL;
10577 					if ($2)
10578 						n->options = lappend(n->options,
10579 											 makeDefElem("full", NULL, @2));
10580 					if ($3)
10581 						n->options = lappend(n->options,
10582 											 makeDefElem("freeze", NULL, @3));
10583 					if ($4)
10584 						n->options = lappend(n->options,
10585 											 makeDefElem("verbose", NULL, @4));
10586 					if ($5)
10587 						n->options = lappend(n->options,
10588 											 makeDefElem("analyze", NULL, @5));
10589 					n->rels = $6;
10590 					n->is_vacuumcmd = true;
10591 					$$ = (Node *)n;
10592 				}
10593 			| VACUUM '(' vac_analyze_option_list ')' opt_vacuum_relation_list
10594 				{
10595 					VacuumStmt *n = makeNode(VacuumStmt);
10596 					n->options = $3;
10597 					n->rels = $5;
10598 					n->is_vacuumcmd = true;
10599 					$$ = (Node *) n;
10600 				}
10601 		;
10602 
10603 AnalyzeStmt: analyze_keyword opt_verbose opt_vacuum_relation_list
10604 				{
10605 					VacuumStmt *n = makeNode(VacuumStmt);
10606 					n->options = NIL;
10607 					if ($2)
10608 						n->options = lappend(n->options,
10609 											 makeDefElem("verbose", NULL, @2));
10610 					n->rels = $3;
10611 					n->is_vacuumcmd = false;
10612 					$$ = (Node *)n;
10613 				}
10614 			| analyze_keyword '(' vac_analyze_option_list ')' opt_vacuum_relation_list
10615 				{
10616 					VacuumStmt *n = makeNode(VacuumStmt);
10617 					n->options = $3;
10618 					n->rels = $5;
10619 					n->is_vacuumcmd = false;
10620 					$$ = (Node *) n;
10621 				}
10622 		;
10623 
10624 vac_analyze_option_list:
10625 			vac_analyze_option_elem
10626 				{
10627 					$$ = list_make1($1);
10628 				}
10629 			| vac_analyze_option_list ',' vac_analyze_option_elem
10630 				{
10631 					$$ = lappend($1, $3);
10632 				}
10633 		;
10634 
10635 analyze_keyword:
10636 			ANALYZE									{}
10637 			| ANALYSE /* British */					{}
10638 		;
10639 
10640 vac_analyze_option_elem:
10641 			vac_analyze_option_name vac_analyze_option_arg
10642 				{
10643 					$$ = makeDefElem($1, $2, @1);
10644 				}
10645 		;
10646 
10647 vac_analyze_option_name:
10648 			NonReservedWord							{ $$ = $1; }
10649 			| analyze_keyword						{ $$ = "analyze"; }
10650 		;
10651 
10652 vac_analyze_option_arg:
10653 			opt_boolean_or_string					{ $$ = (Node *) makeString($1); }
10654 			| NumericOnly			{ $$ = (Node *) $1; }
10655 			| /* EMPTY */		 					{ $$ = NULL; }
10656 		;
10657 
10658 opt_analyze:
10659 			analyze_keyword							{ $$ = true; }
10660 			| /*EMPTY*/								{ $$ = false; }
10661 		;
10662 
10663 opt_verbose:
10664 			VERBOSE									{ $$ = true; }
10665 			| /*EMPTY*/								{ $$ = false; }
10666 		;
10667 
10668 opt_full:	FULL									{ $$ = true; }
10669 			| /*EMPTY*/								{ $$ = false; }
10670 		;
10671 
10672 opt_freeze: FREEZE									{ $$ = true; }
10673 			| /*EMPTY*/								{ $$ = false; }
10674 		;
10675 
10676 opt_name_list:
10677 			'(' name_list ')'						{ $$ = $2; }
10678 			| /*EMPTY*/								{ $$ = NIL; }
10679 		;
10680 
10681 vacuum_relation:
10682 			qualified_name opt_name_list
10683 				{
10684 					$$ = (Node *) makeVacuumRelation($1, InvalidOid, $2);
10685 				}
10686 		;
10687 
10688 vacuum_relation_list:
10689 			vacuum_relation
10690 					{ $$ = list_make1($1); }
10691 			| vacuum_relation_list ',' vacuum_relation
10692 					{ $$ = lappend($1, $3); }
10693 		;
10694 
10695 opt_vacuum_relation_list:
10696 			vacuum_relation_list					{ $$ = $1; }
10697 			| /*EMPTY*/								{ $$ = NIL; }
10698 		;
10699 
10700 
10701 /*****************************************************************************
10702  *
10703  *		QUERY:
10704  *				EXPLAIN [ANALYZE] [VERBOSE] query
10705  *				EXPLAIN ( options ) query
10706  *
10707  *****************************************************************************/
10708 
10709 ExplainStmt:
10710 		EXPLAIN ExplainableStmt
10711 				{
10712 					ExplainStmt *n = makeNode(ExplainStmt);
10713 					n->query = $2;
10714 					n->options = NIL;
10715 					$$ = (Node *) n;
10716 				}
10717 		| EXPLAIN analyze_keyword opt_verbose ExplainableStmt
10718 				{
10719 					ExplainStmt *n = makeNode(ExplainStmt);
10720 					n->query = $4;
10721 					n->options = list_make1(makeDefElem("analyze", NULL, @2));
10722 					if ($3)
10723 						n->options = lappend(n->options,
10724 											 makeDefElem("verbose", NULL, @3));
10725 					$$ = (Node *) n;
10726 				}
10727 		| EXPLAIN VERBOSE ExplainableStmt
10728 				{
10729 					ExplainStmt *n = makeNode(ExplainStmt);
10730 					n->query = $3;
10731 					n->options = list_make1(makeDefElem("verbose", NULL, @2));
10732 					$$ = (Node *) n;
10733 				}
10734 		| EXPLAIN '(' explain_option_list ')' ExplainableStmt
10735 				{
10736 					ExplainStmt *n = makeNode(ExplainStmt);
10737 					n->query = $5;
10738 					n->options = $3;
10739 					$$ = (Node *) n;
10740 				}
10741 		;
10742 
10743 ExplainableStmt:
10744 			SelectStmt
10745 			| InsertStmt
10746 			| UpdateStmt
10747 			| DeleteStmt
10748 			| DeclareCursorStmt
10749 			| CreateAsStmt
10750 			| CreateMatViewStmt
10751 			| RefreshMatViewStmt
10752 			| ExecuteStmt					/* by default all are $$=$1 */
10753 		;
10754 
10755 explain_option_list:
10756 			explain_option_elem
10757 				{
10758 					$$ = list_make1($1);
10759 				}
10760 			| explain_option_list ',' explain_option_elem
10761 				{
10762 					$$ = lappend($1, $3);
10763 				}
10764 		;
10765 
10766 explain_option_elem:
10767 			explain_option_name explain_option_arg
10768 				{
10769 					$$ = makeDefElem($1, $2, @1);
10770 				}
10771 		;
10772 
10773 explain_option_name:
10774 			NonReservedWord			{ $$ = $1; }
10775 			| analyze_keyword		{ $$ = "analyze"; }
10776 		;
10777 
10778 explain_option_arg:
10779 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
10780 			| NumericOnly			{ $$ = (Node *) $1; }
10781 			| /* EMPTY */			{ $$ = NULL; }
10782 		;
10783 
10784 /*****************************************************************************
10785  *
10786  *		QUERY:
10787  *				PREPARE <plan_name> [(args, ...)] AS <query>
10788  *
10789  *****************************************************************************/
10790 
10791 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
10792 				{
10793 					PrepareStmt *n = makeNode(PrepareStmt);
10794 					n->name = $2;
10795 					n->argtypes = $3;
10796 					n->query = $5;
10797 					$$ = (Node *) n;
10798 				}
10799 		;
10800 
10801 prep_type_clause: '(' type_list ')'			{ $$ = $2; }
10802 				| /* EMPTY */				{ $$ = NIL; }
10803 		;
10804 
10805 PreparableStmt:
10806 			SelectStmt
10807 			| InsertStmt
10808 			| UpdateStmt
10809 			| DeleteStmt					/* by default all are $$=$1 */
10810 		;
10811 
10812 /*****************************************************************************
10813  *
10814  * EXECUTE <plan_name> [(params, ...)]
10815  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
10816  *
10817  *****************************************************************************/
10818 
10819 ExecuteStmt: EXECUTE name execute_param_clause
10820 				{
10821 					ExecuteStmt *n = makeNode(ExecuteStmt);
10822 					n->name = $2;
10823 					n->params = $3;
10824 					$$ = (Node *) n;
10825 				}
10826 			| CREATE OptTemp TABLE create_as_target AS
10827 				EXECUTE name execute_param_clause opt_with_data
10828 				{
10829 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10830 					ExecuteStmt *n = makeNode(ExecuteStmt);
10831 					n->name = $7;
10832 					n->params = $8;
10833 					ctas->query = (Node *) n;
10834 					ctas->into = $4;
10835 					ctas->relkind = OBJECT_TABLE;
10836 					ctas->is_select_into = false;
10837 					ctas->if_not_exists = false;
10838 					/* cram additional flags into the IntoClause */
10839 					$4->rel->relpersistence = $2;
10840 					$4->skipData = !($9);
10841 					$$ = (Node *) ctas;
10842 				}
10843 			| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS
10844 				EXECUTE name execute_param_clause opt_with_data
10845 				{
10846 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10847 					ExecuteStmt *n = makeNode(ExecuteStmt);
10848 					n->name = $10;
10849 					n->params = $11;
10850 					ctas->query = (Node *) n;
10851 					ctas->into = $7;
10852 					ctas->relkind = OBJECT_TABLE;
10853 					ctas->is_select_into = false;
10854 					ctas->if_not_exists = true;
10855 					/* cram additional flags into the IntoClause */
10856 					$7->rel->relpersistence = $2;
10857 					$7->skipData = !($12);
10858 					$$ = (Node *) ctas;
10859 				}
10860 		;
10861 
10862 execute_param_clause: '(' expr_list ')'				{ $$ = $2; }
10863 					| /* EMPTY */					{ $$ = NIL; }
10864 					;
10865 
10866 /*****************************************************************************
10867  *
10868  *		QUERY:
10869  *				DEALLOCATE [PREPARE] <plan_name>
10870  *
10871  *****************************************************************************/
10872 
10873 DeallocateStmt: DEALLOCATE name
10874 					{
10875 						DeallocateStmt *n = makeNode(DeallocateStmt);
10876 						n->name = $2;
10877 						$$ = (Node *) n;
10878 					}
10879 				| DEALLOCATE PREPARE name
10880 					{
10881 						DeallocateStmt *n = makeNode(DeallocateStmt);
10882 						n->name = $3;
10883 						$$ = (Node *) n;
10884 					}
10885 				| DEALLOCATE ALL
10886 					{
10887 						DeallocateStmt *n = makeNode(DeallocateStmt);
10888 						n->name = NULL;
10889 						$$ = (Node *) n;
10890 					}
10891 				| DEALLOCATE PREPARE ALL
10892 					{
10893 						DeallocateStmt *n = makeNode(DeallocateStmt);
10894 						n->name = NULL;
10895 						$$ = (Node *) n;
10896 					}
10897 		;
10898 
10899 /*****************************************************************************
10900  *
10901  *		QUERY:
10902  *				INSERT STATEMENTS
10903  *
10904  *****************************************************************************/
10905 
10906 InsertStmt:
10907 			opt_with_clause INSERT INTO insert_target insert_rest
10908 			opt_on_conflict returning_clause
10909 				{
10910 					$5->relation = $4;
10911 					$5->onConflictClause = $6;
10912 					$5->returningList = $7;
10913 					$5->withClause = $1;
10914 					$$ = (Node *) $5;
10915 				}
10916 		;
10917 InsertStmtShort:
10918 			opt_with_clause INSERT INTO insert_target
10919 			{
10920 				InsertStmt *insert = makeNode(InsertStmt);
10921 				insert->relation = $4;
10922 				insert->withClause= $1;
10923 				$$ = (Node *) insert;
10924 				/*
10925 				 * Assign the node directly to the parsetree and exit the scanner
10926 				 * we don't want to keep parsing for information we don't need
10927 				 */
10928 				pg_yyget_extra(yyscanner)->parsetree = list_make1(makeRawStmt($$, 0));
10929 				YYACCEPT;
10930 			}
10931 		;
10932 
10933 /*
10934  * Can't easily make AS optional here, because VALUES in insert_rest would
10935  * have a shift/reduce conflict with VALUES as an optional alias.  We could
10936  * easily allow unreserved_keywords as optional aliases, but that'd be an odd
10937  * divergence from other places.  So just require AS for now.
10938  */
10939 insert_target:
10940 			qualified_name
10941 				{
10942 					$$ = $1;
10943 				}
10944 			| qualified_name AS ColId
10945 				{
10946 					$1->alias = makeAlias($3, NIL);
10947 					$$ = $1;
10948 				}
10949 		;
10950 
10951 insert_rest:
10952 			SelectStmt
10953 				{
10954 					$$ = makeNode(InsertStmt);
10955 					$$->cols = NIL;
10956 					$$->selectStmt = $1;
10957 				}
10958 			| OVERRIDING override_kind VALUE_P SelectStmt
10959 				{
10960 					$$ = makeNode(InsertStmt);
10961 					$$->cols = NIL;
10962 					$$->override = $2;
10963 					$$->selectStmt = $4;
10964 				}
10965 			| '(' insert_column_list ')' SelectStmt
10966 				{
10967 					$$ = makeNode(InsertStmt);
10968 					$$->cols = $2;
10969 					$$->selectStmt = $4;
10970 				}
10971 			| '(' insert_column_list ')' OVERRIDING override_kind VALUE_P SelectStmt
10972 				{
10973 					$$ = makeNode(InsertStmt);
10974 					$$->cols = $2;
10975 					$$->override = $5;
10976 					$$->selectStmt = $7;
10977 				}
10978 			| DEFAULT VALUES
10979 				{
10980 					$$ = makeNode(InsertStmt);
10981 					$$->cols = NIL;
10982 					$$->selectStmt = NULL;
10983 				}
10984 		;
10985 
10986 override_kind:
10987 			USER		{ $$ = OVERRIDING_USER_VALUE; }
10988 			| SYSTEM_P	{ $$ = OVERRIDING_SYSTEM_VALUE; }
10989 		;
10990 
10991 insert_column_list:
10992 			insert_column_item
10993 					{ $$ = list_make1($1); }
10994 			| insert_column_list ',' insert_column_item
10995 					{ $$ = lappend($1, $3); }
10996 		;
10997 
10998 insert_column_item:
10999 			ColId opt_indirection
11000 				{
11001 					$$ = makeNode(ResTarget);
11002 					$$->name = $1;
11003 					$$->indirection = check_indirection($2, yyscanner);
11004 					$$->val = NULL;
11005 					$$->location = @1;
11006 				}
11007 		;
11008 
11009 opt_on_conflict:
11010 			ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list	where_clause
11011 				{
11012 					$$ = makeNode(OnConflictClause);
11013 					$$->action = ONCONFLICT_UPDATE;
11014 					$$->infer = $3;
11015 					$$->targetList = $7;
11016 					$$->whereClause = $8;
11017 					$$->location = @1;
11018 				}
11019 			|
11020 			ON CONFLICT opt_conf_expr DO NOTHING
11021 				{
11022 					$$ = makeNode(OnConflictClause);
11023 					$$->action = ONCONFLICT_NOTHING;
11024 					$$->infer = $3;
11025 					$$->targetList = NIL;
11026 					$$->whereClause = NULL;
11027 					$$->location = @1;
11028 				}
11029 			| /*EMPTY*/
11030 				{
11031 					$$ = NULL;
11032 				}
11033 		;
11034 
11035 opt_conf_expr:
11036 			'(' index_params ')' where_clause
11037 				{
11038 					$$ = makeNode(InferClause);
11039 					$$->indexElems = $2;
11040 					$$->whereClause = $4;
11041 					$$->conname = NULL;
11042 					$$->location = @1;
11043 				}
11044 			|
11045 			ON CONSTRAINT name
11046 				{
11047 					$$ = makeNode(InferClause);
11048 					$$->indexElems = NIL;
11049 					$$->whereClause = NULL;
11050 					$$->conname = $3;
11051 					$$->location = @1;
11052 				}
11053 			| /*EMPTY*/
11054 				{
11055 					$$ = NULL;
11056 				}
11057 		;
11058 
11059 returning_clause:
11060 			RETURNING target_list		{ $$ = $2; }
11061 			| /* EMPTY */				{ $$ = NIL; }
11062 		;
11063 
11064 
11065 /*****************************************************************************
11066  *
11067  *		QUERY:
11068  *				DELETE STATEMENTS
11069  *
11070  *****************************************************************************/
11071 
11072 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
11073 			using_clause where_or_current_clause returning_clause
11074 				{
11075 					DeleteStmt *n = makeNode(DeleteStmt);
11076 					n->relation = $4;
11077 					n->usingClause = $5;
11078 					n->whereClause = $6;
11079 					n->returningList = $7;
11080 					n->withClause = $1;
11081 					$$ = (Node *)n;
11082 				}
11083 		;
11084 
11085 using_clause:
11086 				USING from_list						{ $$ = $2; }
11087 			| /*EMPTY*/								{ $$ = NIL; }
11088 		;
11089 
11090 
11091 /*****************************************************************************
11092  *
11093  *		QUERY:
11094  *				LOCK TABLE
11095  *
11096  *****************************************************************************/
11097 
11098 LockStmt:	LOCK_P opt_table relation_expr_list opt_lock opt_nowait
11099 				{
11100 					LockStmt *n = makeNode(LockStmt);
11101 
11102 					n->relations = $3;
11103 					n->mode = $4;
11104 					n->nowait = $5;
11105 					$$ = (Node *)n;
11106 				}
11107 		;
11108 
11109 opt_lock:	IN_P lock_type MODE				{ $$ = $2; }
11110 			| /*EMPTY*/						{ $$ = AccessExclusiveLock; }
11111 		;
11112 
11113 lock_type:	ACCESS SHARE					{ $$ = AccessShareLock; }
11114 			| ROW SHARE						{ $$ = RowShareLock; }
11115 			| ROW EXCLUSIVE					{ $$ = RowExclusiveLock; }
11116 			| SHARE UPDATE EXCLUSIVE		{ $$ = ShareUpdateExclusiveLock; }
11117 			| SHARE							{ $$ = ShareLock; }
11118 			| SHARE ROW EXCLUSIVE			{ $$ = ShareRowExclusiveLock; }
11119 			| EXCLUSIVE						{ $$ = ExclusiveLock; }
11120 			| ACCESS EXCLUSIVE				{ $$ = AccessExclusiveLock; }
11121 		;
11122 
11123 opt_nowait:	NOWAIT							{ $$ = true; }
11124 			| /*EMPTY*/						{ $$ = false; }
11125 		;
11126 
11127 opt_nowait_or_skip:
11128 			NOWAIT							{ $$ = LockWaitError; }
11129 			| SKIP LOCKED					{ $$ = LockWaitSkip; }
11130 			| /*EMPTY*/						{ $$ = LockWaitBlock; }
11131 		;
11132 
11133 
11134 /*****************************************************************************
11135  *
11136  *		QUERY:
11137  *				UpdateStmt (UPDATE)
11138  *
11139  *****************************************************************************/
11140 
11141 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
11142 			SET set_clause_list
11143 			from_clause
11144 			where_or_current_clause
11145 			returning_clause
11146 				{
11147 					UpdateStmt *n = makeNode(UpdateStmt);
11148 					n->relation = $3;
11149 					n->targetList = $5;
11150 					n->fromClause = $6;
11151 					n->whereClause = $7;
11152 					n->returningList = $8;
11153 					n->withClause = $1;
11154 					$$ = (Node *)n;
11155 				}
11156 		;
11157 UpdateStmtShort: opt_with_clause UPDATE relation_expr_opt_alias
11158 				{
11159 					UpdateStmt *n = makeNode(UpdateStmt);
11160 					n->relation = $3;
11161 					n->targetList = NULL;
11162 					n->fromClause = NULL;
11163 					n->whereClause = NULL;
11164 					n->returningList = NULL;
11165 					n->withClause = $1;
11166 					$$ = (Node *)n;
11167 					pg_yyget_extra(yyscanner)->parsetree = list_make1(makeRawStmt($$, 0));
11168 					YYACCEPT;
11169 				}
11170 		;
11171 set_clause_list:
11172 			set_clause							{ $$ = $1; }
11173 			| set_clause_list ',' set_clause	{ $$ = list_concat($1,$3); }
11174 		;
11175 
11176 set_clause:
11177 			set_target '=' a_expr
11178 				{
11179 					$1->val = (Node *) $3;
11180 					$$ = list_make1($1);
11181 				}
11182 			| '(' set_target_list ')' '=' a_expr
11183 				{
11184 					int ncolumns = list_length($2);
11185 					int i = 1;
11186 					ListCell *col_cell;
11187 
11188 					/* Create a MultiAssignRef source for each target */
foreach(col_cell,$2)11189 					foreach(col_cell, $2)
11190 					{
11191 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
11192 						MultiAssignRef *r = makeNode(MultiAssignRef);
11193 
11194 						r->source = (Node *) $5;
11195 						r->colno = i;
11196 						r->ncolumns = ncolumns;
11197 						res_col->val = (Node *) r;
11198 						i++;
11199 					}
11200 
11201 					$$ = $2;
11202 				}
11203 		;
11204 
11205 set_target:
11206 			ColId opt_indirection
11207 				{
11208 					$$ = makeNode(ResTarget);
11209 					$$->name = $1;
11210 					$$->indirection = check_indirection($2, yyscanner);
11211 					$$->val = NULL;	/* upper production sets this */
11212 					$$->location = @1;
11213 				}
11214 		;
11215 
11216 set_target_list:
11217 			set_target								{ $$ = list_make1($1); }
11218 			| set_target_list ',' set_target		{ $$ = lappend($1,$3); }
11219 		;
11220 
11221 
11222 /*****************************************************************************
11223  *
11224  *		QUERY:
11225  *				CURSOR STATEMENTS
11226  *
11227  *****************************************************************************/
11228 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
11229 				{
11230 					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
11231 					n->portalname = $2;
11232 					/* currently we always set FAST_PLAN option */
11233 					n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
11234 					n->query = $7;
11235 					$$ = (Node *)n;
11236 				}
11237 		;
11238 
11239 cursor_name:	name						{ $$ = $1; }
11240 		;
11241 
11242 cursor_options: /*EMPTY*/					{ $$ = 0; }
11243 			| cursor_options NO SCROLL		{ $$ = $1 | CURSOR_OPT_NO_SCROLL; }
11244 			| cursor_options SCROLL			{ $$ = $1 | CURSOR_OPT_SCROLL; }
11245 			| cursor_options BINARY			{ $$ = $1 | CURSOR_OPT_BINARY; }
11246 			| cursor_options INSENSITIVE	{ $$ = $1 | CURSOR_OPT_INSENSITIVE; }
11247 		;
11248 
11249 opt_hold: /* EMPTY */						{ $$ = 0; }
11250 			| WITH HOLD						{ $$ = CURSOR_OPT_HOLD; }
11251 			| WITHOUT HOLD					{ $$ = 0; }
11252 		;
11253 
11254 /*****************************************************************************
11255  *
11256  *		QUERY:
11257  *				SELECT STATEMENTS
11258  *
11259  *****************************************************************************/
11260 
11261 /* A complete SELECT statement looks like this.
11262  *
11263  * The rule returns either a single SelectStmt node or a tree of them,
11264  * representing a set-operation tree.
11265  *
11266  * There is an ambiguity when a sub-SELECT is within an a_expr and there
11267  * are excess parentheses: do the parentheses belong to the sub-SELECT or
11268  * to the surrounding a_expr?  We don't really care, but bison wants to know.
11269  * To resolve the ambiguity, we are careful to define the grammar so that
11270  * the decision is staved off as long as possible: as long as we can keep
11271  * absorbing parentheses into the sub-SELECT, we will do so, and only when
11272  * it's no longer possible to do that will we decide that parens belong to
11273  * the expression.	For example, in "SELECT (((SELECT 2)) + 3)" the extra
11274  * parentheses are treated as part of the sub-select.  The necessity of doing
11275  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".	Had we
11276  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
11277  * SELECT viewpoint when we see the UNION.
11278  *
11279  * This approach is implemented by defining a nonterminal select_with_parens,
11280  * which represents a SELECT with at least one outer layer of parentheses,
11281  * and being careful to use select_with_parens, never '(' SelectStmt ')',
11282  * in the expression grammar.  We will then have shift-reduce conflicts
11283  * which we can resolve in favor of always treating '(' <select> ')' as
11284  * a select_with_parens.  To resolve the conflicts, the productions that
11285  * conflict with the select_with_parens productions are manually given
11286  * precedences lower than the precedence of ')', thereby ensuring that we
11287  * shift ')' (and then reduce to select_with_parens) rather than trying to
11288  * reduce the inner <select> nonterminal to something else.  We use UMINUS
11289  * precedence for this, which is a fairly arbitrary choice.
11290  *
11291  * To be able to define select_with_parens itself without ambiguity, we need
11292  * a nonterminal select_no_parens that represents a SELECT structure with no
11293  * outermost parentheses.  This is a little bit tedious, but it works.
11294  *
11295  * In non-expression contexts, we use SelectStmt which can represent a SELECT
11296  * with or without outer parentheses.
11297  */
11298 
11299 SelectStmt: select_no_parens			%prec UMINUS
11300 			| select_with_parens		%prec UMINUS
11301 		;
11302 
11303 select_with_parens:
11304 			'(' select_no_parens ')'				{ $$ = $2; }
11305 			| '(' select_with_parens ')'			{ $$ = $2; }
11306 		;
11307 
11308 /*
11309  * This rule parses the equivalent of the standard's <query expression>.
11310  * The duplicative productions are annoying, but hard to get rid of without
11311  * creating shift/reduce conflicts.
11312  *
11313  *	The locking clause (FOR UPDATE etc) may be before or after LIMIT/OFFSET.
11314  *	In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
11315  *	We now support both orderings, but prefer LIMIT/OFFSET before the locking
11316  * clause.
11317  *	2002-08-28 bjm
11318  */
11319 select_no_parens:
11320 			simple_select						{ $$ = $1; }
11321 			| select_clause sort_clause
11322 				{
11323 					insertSelectOptions((SelectStmt *) $1, $2, NIL,
11324 										NULL, NULL, NULL,
11325 										yyscanner);
11326 					$$ = $1;
11327 				}
11328 			| select_clause opt_sort_clause for_locking_clause opt_select_limit
11329 				{
11330 					insertSelectOptions((SelectStmt *) $1, $2, $3,
11331 										list_nth($4, 0), list_nth($4, 1),
11332 										NULL,
11333 										yyscanner);
11334 					$$ = $1;
11335 				}
11336 			| select_clause opt_sort_clause select_limit opt_for_locking_clause
11337 				{
11338 					insertSelectOptions((SelectStmt *) $1, $2, $4,
11339 										list_nth($3, 0), list_nth($3, 1),
11340 										NULL,
11341 										yyscanner);
11342 					$$ = $1;
11343 				}
11344 			| with_clause select_clause
11345 				{
11346 					insertSelectOptions((SelectStmt *) $2, NULL, NIL,
11347 										NULL, NULL,
11348 										$1,
11349 										yyscanner);
11350 					$$ = $2;
11351 				}
11352 			| with_clause select_clause sort_clause
11353 				{
11354 					insertSelectOptions((SelectStmt *) $2, $3, NIL,
11355 										NULL, NULL,
11356 										$1,
11357 										yyscanner);
11358 					$$ = $2;
11359 				}
11360 			| with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
11361 				{
11362 					insertSelectOptions((SelectStmt *) $2, $3, $4,
11363 										list_nth($5, 0), list_nth($5, 1),
11364 										$1,
11365 										yyscanner);
11366 					$$ = $2;
11367 				}
11368 			| with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
11369 				{
11370 					insertSelectOptions((SelectStmt *) $2, $3, $5,
11371 										list_nth($4, 0), list_nth($4, 1),
11372 										$1,
11373 										yyscanner);
11374 					$$ = $2;
11375 				}
11376 		;
11377 
11378 select_clause:
11379 			simple_select							{ $$ = $1; }
11380 			| select_with_parens					{ $$ = $1; }
11381 		;
11382 
11383 /*
11384  * This rule parses SELECT statements that can appear within set operations,
11385  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
11386  * the ordering of the set operations.	Without '(' and ')' we want the
11387  * operations to be ordered per the precedence specs at the head of this file.
11388  *
11389  * As with select_no_parens, simple_select cannot have outer parentheses,
11390  * but can have parenthesized subclauses.
11391  *
11392  * Note that sort clauses cannot be included at this level --- SQL requires
11393  *		SELECT foo UNION SELECT bar ORDER BY baz
11394  * to be parsed as
11395  *		(SELECT foo UNION SELECT bar) ORDER BY baz
11396  * not
11397  *		SELECT foo UNION (SELECT bar ORDER BY baz)
11398  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
11399  * described as part of the select_no_parens production, not simple_select.
11400  * This does not limit functionality, because you can reintroduce these
11401  * clauses inside parentheses.
11402  *
11403  * NOTE: only the leftmost component SelectStmt should have INTO.
11404  * However, this is not checked by the grammar; parse analysis must check it.
11405  */
11406 simple_select:
11407 			SELECT opt_all_clause opt_target_list
11408 			into_clause from_clause where_clause
11409 			group_clause having_clause window_clause
11410 				{
11411 					SelectStmt *n = makeNode(SelectStmt);
11412 					n->targetList = $3;
11413 					n->intoClause = $4;
11414 					n->fromClause = $5;
11415 					n->whereClause = $6;
11416 					n->groupClause = $7;
11417 					n->havingClause = $8;
11418 					n->windowClause = $9;
11419 					$$ = (Node *)n;
11420 				}
11421 			| SELECT distinct_clause target_list
11422 			into_clause from_clause where_clause
11423 			group_clause having_clause window_clause
11424 				{
11425 					SelectStmt *n = makeNode(SelectStmt);
11426 					n->distinctClause = $2;
11427 					n->targetList = $3;
11428 					n->intoClause = $4;
11429 					n->fromClause = $5;
11430 					n->whereClause = $6;
11431 					n->groupClause = $7;
11432 					n->havingClause = $8;
11433 					n->windowClause = $9;
11434 					$$ = (Node *)n;
11435 				}
11436 			| values_clause							{ $$ = $1; }
11437 			| TABLE relation_expr
11438 				{
11439 					/* same as SELECT * FROM relation_expr */
11440 					ColumnRef *cr = makeNode(ColumnRef);
11441 					ResTarget *rt = makeNode(ResTarget);
11442 					SelectStmt *n = makeNode(SelectStmt);
11443 
11444 					cr->fields = list_make1(makeNode(A_Star));
11445 					cr->location = -1;
11446 
11447 					rt->name = NULL;
11448 					rt->indirection = NIL;
11449 					rt->val = (Node *)cr;
11450 					rt->location = -1;
11451 
11452 					n->targetList = list_make1(rt);
11453 					n->fromClause = list_make1($2);
11454 					$$ = (Node *)n;
11455 				}
11456 			| select_clause UNION all_or_distinct select_clause
11457 				{
11458 					$$ = makeSetOp(SETOP_UNION, $3, $1, $4);
11459 				}
11460 			| select_clause INTERSECT all_or_distinct select_clause
11461 				{
11462 					$$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
11463 				}
11464 			| select_clause EXCEPT all_or_distinct select_clause
11465 				{
11466 					$$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
11467 				}
11468 		;
11469 
11470 /*
11471  * SQL standard WITH clause looks like:
11472  *
11473  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
11474  *		AS (query) [ SEARCH or CYCLE clause ]
11475  *
11476  * We don't currently support the SEARCH or CYCLE clause.
11477  *
11478  * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY.
11479  */
11480 with_clause:
11481 		WITH cte_list
11482 			{
11483 				$$ = makeNode(WithClause);
11484 				$$->ctes = $2;
11485 				$$->recursive = false;
11486 				$$->location = @1;
11487 			}
11488 		| WITH_LA cte_list
11489 			{
11490 				$$ = makeNode(WithClause);
11491 				$$->ctes = $2;
11492 				$$->recursive = false;
11493 				$$->location = @1;
11494 			}
11495 		| WITH RECURSIVE cte_list
11496 			{
11497 				$$ = makeNode(WithClause);
11498 				$$->ctes = $3;
11499 				$$->recursive = true;
11500 				$$->location = @1;
11501 			}
11502 		;
11503 
11504 cte_list:
11505 		common_table_expr						{ $$ = list_make1($1); }
11506 		| cte_list ',' common_table_expr		{ $$ = lappend($1, $3); }
11507 		;
11508 
11509 common_table_expr:  name opt_name_list AS opt_materialized '(' PreparableStmt ')'
11510 			{
11511 				CommonTableExpr *n = makeNode(CommonTableExpr);
11512 				n->ctename = $1;
11513 				n->aliascolnames = $2;
11514 				n->ctematerialized = $4;
11515 				n->ctequery = $6;
11516 				n->location = @1;
11517 				$$ = (Node *) n;
11518 			}
11519 		;
11520 
11521 opt_materialized:
11522 		MATERIALIZED							{ $$ = CTEMaterializeAlways; }
11523 		| NOT MATERIALIZED						{ $$ = CTEMaterializeNever; }
11524 		| /*EMPTY*/								{ $$ = CTEMaterializeDefault; }
11525 		;
11526 
11527 opt_with_clause:
11528 		with_clause								{ $$ = $1; }
11529 		| /*EMPTY*/								{ $$ = NULL; }
11530 		;
11531 
11532 into_clause:
11533 			INTO OptTempTableName
11534 				{
11535 					$$ = makeNode(IntoClause);
11536 					$$->rel = $2;
11537 					$$->colNames = NIL;
11538 					$$->options = NIL;
11539 					$$->onCommit = ONCOMMIT_NOOP;
11540 					$$->tableSpaceName = NULL;
11541 					$$->viewQuery = NULL;
11542 					$$->skipData = false;
11543 				}
11544 			| /*EMPTY*/
11545 				{ $$ = NULL; }
11546 		;
11547 
11548 /*
11549  * Redundancy here is needed to avoid shift/reduce conflicts,
11550  * since TEMP is not a reserved word.  See also OptTemp.
11551  */
11552 OptTempTableName:
11553 			TEMPORARY opt_table qualified_name
11554 				{
11555 					$$ = $3;
11556 					$$->relpersistence = RELPERSISTENCE_TEMP;
11557 				}
11558 			| TEMP opt_table qualified_name
11559 				{
11560 					$$ = $3;
11561 					$$->relpersistence = RELPERSISTENCE_TEMP;
11562 				}
11563 			| LOCAL TEMPORARY opt_table qualified_name
11564 				{
11565 					$$ = $4;
11566 					$$->relpersistence = RELPERSISTENCE_TEMP;
11567 				}
11568 			| LOCAL TEMP opt_table qualified_name
11569 				{
11570 					$$ = $4;
11571 					$$->relpersistence = RELPERSISTENCE_TEMP;
11572 				}
11573 			| GLOBAL TEMPORARY opt_table qualified_name
11574 				{
11575 					ereport(WARNING,
11576 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11577 							 parser_errposition(@1)));
11578 					$$ = $4;
11579 					$$->relpersistence = RELPERSISTENCE_TEMP;
11580 				}
11581 			| GLOBAL TEMP opt_table qualified_name
11582 				{
11583 					ereport(WARNING,
11584 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11585 							 parser_errposition(@1)));
11586 					$$ = $4;
11587 					$$->relpersistence = RELPERSISTENCE_TEMP;
11588 				}
11589 			| UNLOGGED opt_table qualified_name
11590 				{
11591 					$$ = $3;
11592 					$$->relpersistence = RELPERSISTENCE_UNLOGGED;
11593 				}
11594 			| TABLE qualified_name
11595 				{
11596 					$$ = $2;
11597 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11598 				}
11599 			| qualified_name
11600 				{
11601 					$$ = $1;
11602 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11603 				}
11604 		;
11605 
11606 opt_table:	TABLE									{}
11607 			| /*EMPTY*/								{}
11608 		;
11609 
11610 all_or_distinct:
11611 			ALL										{ $$ = true; }
11612 			| DISTINCT								{ $$ = false; }
11613 			| /*EMPTY*/								{ $$ = false; }
11614 		;
11615 
11616 /* We use (NIL) as a placeholder to indicate that all target expressions
11617  * should be placed in the DISTINCT list during parsetree analysis.
11618  */
11619 distinct_clause:
11620 			DISTINCT								{ $$ = list_make1(NIL); }
11621 			| DISTINCT ON '(' expr_list ')'			{ $$ = $4; }
11622 		;
11623 
11624 opt_all_clause:
11625 			ALL										{ $$ = NIL;}
11626 			| /*EMPTY*/								{ $$ = NIL; }
11627 		;
11628 
11629 opt_sort_clause:
11630 			sort_clause								{ $$ = $1;}
11631 			| /*EMPTY*/								{ $$ = NIL; }
11632 		;
11633 
11634 sort_clause:
11635 			ORDER BY sortby_list					{ $$ = $3; }
11636 		;
11637 
11638 sortby_list:
11639 			sortby									{ $$ = list_make1($1); }
11640 			| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
11641 		;
11642 
11643 sortby:		a_expr USING qual_all_Op opt_nulls_order
11644 				{
11645 					$$ = makeNode(SortBy);
11646 					$$->node = $1;
11647 					$$->sortby_dir = SORTBY_USING;
11648 					$$->sortby_nulls = $4;
11649 					$$->useOp = $3;
11650 					$$->location = @3;
11651 				}
11652 			| a_expr opt_asc_desc opt_nulls_order
11653 				{
11654 					$$ = makeNode(SortBy);
11655 					$$->node = $1;
11656 					$$->sortby_dir = $2;
11657 					$$->sortby_nulls = $3;
11658 					$$->useOp = NIL;
11659 					$$->location = -1;		/* no operator */
11660 				}
11661 		;
11662 
11663 
11664 select_limit:
11665 			limit_clause offset_clause			{ $$ = list_make2($2, $1); }
11666 			| offset_clause limit_clause		{ $$ = list_make2($1, $2); }
11667 			| limit_clause						{ $$ = list_make2(NULL, $1); }
11668 			| offset_clause						{ $$ = list_make2($1, NULL); }
11669 		;
11670 
11671 opt_select_limit:
11672 			select_limit						{ $$ = $1; }
11673 			| /* EMPTY */						{ $$ = list_make2(NULL,NULL); }
11674 		;
11675 
11676 limit_clause:
11677 			LIMIT select_limit_value
11678 				{ $$ = $2; }
11679 			| LIMIT select_limit_value ',' select_offset_value
11680 				{
11681 					/* Disabled because it was too confusing, bjm 2002-02-18 */
11682 					ereport(ERROR,
11683 							(errcode(ERRCODE_SYNTAX_ERROR),
11684 							 errmsg("LIMIT #,# syntax is not supported"),
11685 							 errhint("Use separate LIMIT and OFFSET clauses."),
11686 							 parser_errposition(@1)));
11687 				}
11688 			/* SQL:2008 syntax */
11689 			/* to avoid shift/reduce conflicts, handle the optional value with
11690 			 * a separate production rather than an opt_ expression.  The fact
11691 			 * that ONLY is fully reserved means that this way, we defer any
11692 			 * decision about what rule reduces ROW or ROWS to the point where
11693 			 * we can see the ONLY token in the lookahead slot.
11694 			 */
11695 			| FETCH first_or_next select_fetch_first_value row_or_rows ONLY
11696 				{ $$ = $3; }
11697 			| FETCH first_or_next row_or_rows ONLY
11698 				{ $$ = makeIntConst(1, -1); }
11699 		;
11700 
11701 offset_clause:
11702 			OFFSET select_offset_value
11703 				{ $$ = $2; }
11704 			/* SQL:2008 syntax */
11705 			| OFFSET select_fetch_first_value row_or_rows
11706 				{ $$ = $2; }
11707 		;
11708 
11709 select_limit_value:
11710 			a_expr									{ $$ = $1; }
11711 			| ALL
11712 				{
11713 					/* LIMIT ALL is represented as a NULL constant */
11714 					$$ = makeNullAConst(@1);
11715 				}
11716 		;
11717 
11718 select_offset_value:
11719 			a_expr									{ $$ = $1; }
11720 		;
11721 
11722 /*
11723  * Allowing full expressions without parentheses causes various parsing
11724  * problems with the trailing ROW/ROWS key words.  SQL spec only calls for
11725  * <simple value specification>, which is either a literal or a parameter (but
11726  * an <SQL parameter reference> could be an identifier, bringing up conflicts
11727  * with ROW/ROWS). We solve this by leveraging the presence of ONLY (see above)
11728  * to determine whether the expression is missing rather than trying to make it
11729  * optional in this rule.
11730  *
11731  * c_expr covers almost all the spec-required cases (and more), but it doesn't
11732  * cover signed numeric literals, which are allowed by the spec. So we include
11733  * those here explicitly. We need FCONST as well as ICONST because values that
11734  * don't fit in the platform's "long", but do fit in bigint, should still be
11735  * accepted here. (This is possible in 64-bit Windows as well as all 32-bit
11736  * builds.)
11737  */
11738 select_fetch_first_value:
11739 			c_expr									{ $$ = $1; }
11740 			| '+' I_or_F_const
11741 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
11742 			| '-' I_or_F_const
11743 				{ $$ = doNegate($2, @1); }
11744 		;
11745 
11746 I_or_F_const:
11747 			Iconst									{ $$ = makeIntConst($1,@1); }
11748 			| FCONST								{ $$ = makeFloatConst($1,@1); }
11749 		;
11750 
11751 /* noise words */
11752 row_or_rows: ROW									{ $$ = 0; }
11753 			| ROWS									{ $$ = 0; }
11754 		;
11755 
11756 first_or_next: FIRST_P								{ $$ = 0; }
11757 			| NEXT									{ $$ = 0; }
11758 		;
11759 
11760 
11761 /*
11762  * This syntax for group_clause tries to follow the spec quite closely.
11763  * However, the spec allows only column references, not expressions,
11764  * which introduces an ambiguity between implicit row constructors
11765  * (a,b) and lists of column references.
11766  *
11767  * We handle this by using the a_expr production for what the spec calls
11768  * <ordinary grouping set>, which in the spec represents either one column
11769  * reference or a parenthesized list of column references. Then, we check the
11770  * top node of the a_expr to see if it's an implicit RowExpr, and if so, just
11771  * grab and use the list, discarding the node. (this is done in parse analysis,
11772  * not here)
11773  *
11774  * (we abuse the row_format field of RowExpr to distinguish implicit and
11775  * explicit row constructors; it's debatable if anyone sanely wants to use them
11776  * in a group clause, but if they have a reason to, we make it possible.)
11777  *
11778  * Each item in the group_clause list is either an expression tree or a
11779  * GroupingSet node of some type.
11780  */
11781 group_clause:
11782 			GROUP_P BY group_by_list				{ $$ = $3; }
11783 			| /*EMPTY*/								{ $$ = NIL; }
11784 		;
11785 
11786 group_by_list:
11787 			group_by_item							{ $$ = list_make1($1); }
11788 			| group_by_list ',' group_by_item		{ $$ = lappend($1,$3); }
11789 		;
11790 
11791 group_by_item:
11792 			a_expr									{ $$ = $1; }
11793 			| empty_grouping_set					{ $$ = $1; }
11794 			| cube_clause							{ $$ = $1; }
11795 			| rollup_clause							{ $$ = $1; }
11796 			| grouping_sets_clause					{ $$ = $1; }
11797 		;
11798 
11799 empty_grouping_set:
11800 			'(' ')'
11801 				{
11802 					$$ = (Node *) makeGroupingSet(GROUPING_SET_EMPTY, NIL, @1);
11803 				}
11804 		;
11805 
11806 /*
11807  * These hacks rely on setting precedence of CUBE and ROLLUP below that of '(',
11808  * so that they shift in these rules rather than reducing the conflicting
11809  * unreserved_keyword rule.
11810  */
11811 
11812 rollup_clause:
11813 			ROLLUP '(' expr_list ')'
11814 				{
11815 					$$ = (Node *) makeGroupingSet(GROUPING_SET_ROLLUP, $3, @1);
11816 				}
11817 		;
11818 
11819 cube_clause:
11820 			CUBE '(' expr_list ')'
11821 				{
11822 					$$ = (Node *) makeGroupingSet(GROUPING_SET_CUBE, $3, @1);
11823 				}
11824 		;
11825 
11826 grouping_sets_clause:
11827 			GROUPING SETS '(' group_by_list ')'
11828 				{
11829 					$$ = (Node *) makeGroupingSet(GROUPING_SET_SETS, $4, @1);
11830 				}
11831 		;
11832 
11833 having_clause:
11834 			HAVING a_expr							{ $$ = $2; }
11835 			| /*EMPTY*/								{ $$ = NULL; }
11836 		;
11837 
11838 for_locking_clause:
11839 			for_locking_items						{ $$ = $1; }
11840 			| FOR READ ONLY							{ $$ = NIL; }
11841 		;
11842 
11843 opt_for_locking_clause:
11844 			for_locking_clause						{ $$ = $1; }
11845 			| /* EMPTY */							{ $$ = NIL; }
11846 		;
11847 
11848 for_locking_items:
11849 			for_locking_item						{ $$ = list_make1($1); }
11850 			| for_locking_items for_locking_item	{ $$ = lappend($1, $2); }
11851 		;
11852 
11853 for_locking_item:
11854 			for_locking_strength locked_rels_list opt_nowait_or_skip
11855 				{
11856 					LockingClause *n = makeNode(LockingClause);
11857 					n->lockedRels = $2;
11858 					n->strength = $1;
11859 					n->waitPolicy = $3;
11860 					$$ = (Node *) n;
11861 				}
11862 		;
11863 
11864 for_locking_strength:
11865 			FOR UPDATE 							{ $$ = LCS_FORUPDATE; }
11866 			| FOR NO KEY UPDATE 				{ $$ = LCS_FORNOKEYUPDATE; }
11867 			| FOR SHARE 						{ $$ = LCS_FORSHARE; }
11868 			| FOR KEY SHARE 					{ $$ = LCS_FORKEYSHARE; }
11869 		;
11870 
11871 locked_rels_list:
11872 			OF qualified_name_list					{ $$ = $2; }
11873 			| /* EMPTY */							{ $$ = NIL; }
11874 		;
11875 
11876 
11877 /*
11878  * We should allow ROW '(' expr_list ')' too, but that seems to require
11879  * making VALUES a fully reserved word, which will probably break more apps
11880  * than allowing the noise-word is worth.
11881  */
11882 values_clause:
11883 			VALUES '(' expr_list ')'
11884 				{
11885 					SelectStmt *n = makeNode(SelectStmt);
11886 					n->valuesLists = list_make1($3);
11887 					$$ = (Node *) n;
11888 				}
11889 			| values_clause ',' '(' expr_list ')'
11890 				{
11891 					SelectStmt *n = (SelectStmt *) $1;
11892 					n->valuesLists = lappend(n->valuesLists, $4);
11893 					$$ = (Node *) n;
11894 				}
11895 		;
11896 
11897 
11898 /*****************************************************************************
11899  *
11900  *	clauses common to all Optimizable Stmts:
11901  *		from_clause		- allow list of both JOIN expressions and table names
11902  *		where_clause	- qualifications for joins or restrictions
11903  *
11904  *****************************************************************************/
11905 
11906 from_clause:
11907 			FROM from_list							{ $$ = $2; }
11908 			| /*EMPTY*/								{ $$ = NIL; }
11909 		;
11910 
11911 from_list:
11912 			table_ref								{ $$ = list_make1($1); }
11913 			| from_list ',' table_ref				{ $$ = lappend($1, $3); }
11914 		;
11915 
11916 /*
11917  * table_ref is where an alias clause can be attached.
11918  */
11919 table_ref:	relation_expr opt_alias_clause
11920 				{
11921 					$1->alias = $2;
11922 					$$ = (Node *) $1;
11923 				}
11924 			| relation_expr opt_alias_clause tablesample_clause
11925 				{
11926 					RangeTableSample *n = (RangeTableSample *) $3;
11927 					$1->alias = $2;
11928 					/* relation_expr goes inside the RangeTableSample node */
11929 					n->relation = (Node *) $1;
11930 					$$ = (Node *) n;
11931 				}
11932 			| func_table func_alias_clause
11933 				{
11934 					RangeFunction *n = (RangeFunction *) $1;
11935 					n->alias = linitial($2);
11936 					n->coldeflist = lsecond($2);
11937 					$$ = (Node *) n;
11938 				}
11939 			| LATERAL_P func_table func_alias_clause
11940 				{
11941 					RangeFunction *n = (RangeFunction *) $2;
11942 					n->lateral = true;
11943 					n->alias = linitial($3);
11944 					n->coldeflist = lsecond($3);
11945 					$$ = (Node *) n;
11946 				}
11947 			| xmltable opt_alias_clause
11948 				{
11949 					RangeTableFunc *n = (RangeTableFunc *) $1;
11950 					n->alias = $2;
11951 					$$ = (Node *) n;
11952 				}
11953 			| LATERAL_P xmltable opt_alias_clause
11954 				{
11955 					RangeTableFunc *n = (RangeTableFunc *) $2;
11956 					n->lateral = true;
11957 					n->alias = $3;
11958 					$$ = (Node *) n;
11959 				}
11960 			| select_with_parens opt_alias_clause
11961 				{
11962 					RangeSubselect *n = makeNode(RangeSubselect);
11963 					n->lateral = false;
11964 					n->subquery = $1;
11965 					n->alias = $2;
11966 					/*
11967 					 * The SQL spec does not permit a subselect
11968 					 * (<derived_table>) without an alias clause,
11969 					 * so we don't either.  This avoids the problem
11970 					 * of needing to invent a unique refname for it.
11971 					 * That could be surmounted if there's sufficient
11972 					 * popular demand, but for now let's just implement
11973 					 * the spec and see if anyone complains.
11974 					 * However, it does seem like a good idea to emit
11975 					 * an error message that's better than "syntax error".
11976 					 */
11977 					if ($2 == NULL)
11978 					{
11979 						if (IsA($1, SelectStmt) &&
11980 							((SelectStmt *) $1)->valuesLists)
11981 							ereport(ERROR,
11982 									(errcode(ERRCODE_SYNTAX_ERROR),
11983 									 errmsg("VALUES in FROM must have an alias"),
11984 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
11985 									 parser_errposition(@1)));
11986 						else
11987 							ereport(ERROR,
11988 									(errcode(ERRCODE_SYNTAX_ERROR),
11989 									 errmsg("subquery in FROM must have an alias"),
11990 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
11991 									 parser_errposition(@1)));
11992 					}
11993 					$$ = (Node *) n;
11994 				}
11995 			| LATERAL_P select_with_parens opt_alias_clause
11996 				{
11997 					RangeSubselect *n = makeNode(RangeSubselect);
11998 					n->lateral = true;
11999 					n->subquery = $2;
12000 					n->alias = $3;
12001 					/* same comment as above */
12002 					if ($3 == NULL)
12003 					{
12004 						if (IsA($2, SelectStmt) &&
12005 							((SelectStmt *) $2)->valuesLists)
12006 							ereport(ERROR,
12007 									(errcode(ERRCODE_SYNTAX_ERROR),
12008 									 errmsg("VALUES in FROM must have an alias"),
12009 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
12010 									 parser_errposition(@2)));
12011 						else
12012 							ereport(ERROR,
12013 									(errcode(ERRCODE_SYNTAX_ERROR),
12014 									 errmsg("subquery in FROM must have an alias"),
12015 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
12016 									 parser_errposition(@2)));
12017 					}
12018 					$$ = (Node *) n;
12019 				}
12020 			| joined_table
12021 				{
12022 					$$ = (Node *) $1;
12023 				}
12024 			| '(' joined_table ')' alias_clause
12025 				{
12026 					$2->alias = $4;
12027 					$$ = (Node *) $2;
12028 				}
12029 		;
12030 
12031 
12032 /*
12033  * It may seem silly to separate joined_table from table_ref, but there is
12034  * method in SQL's madness: if you don't do it this way you get reduce-
12035  * reduce conflicts, because it's not clear to the parser generator whether
12036  * to expect alias_clause after ')' or not.  For the same reason we must
12037  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
12038  * join_type to expand to empty; if we try it, the parser generator can't
12039  * figure out when to reduce an empty join_type right after table_ref.
12040  *
12041  * Note that a CROSS JOIN is the same as an unqualified
12042  * INNER JOIN, and an INNER JOIN/ON has the same shape
12043  * but a qualification expression to limit membership.
12044  * A NATURAL JOIN implicitly matches column names between
12045  * tables and the shape is determined by which columns are
12046  * in common. We'll collect columns during the later transformations.
12047  */
12048 
12049 joined_table:
12050 			'(' joined_table ')'
12051 				{
12052 					$$ = $2;
12053 				}
12054 			| table_ref CROSS JOIN table_ref
12055 				{
12056 					/* CROSS JOIN is same as unqualified inner join */
12057 					JoinExpr *n = makeNode(JoinExpr);
12058 					n->jointype = JOIN_INNER;
12059 					n->isNatural = false;
12060 					n->larg = $1;
12061 					n->rarg = $4;
12062 					n->usingClause = NIL;
12063 					n->quals = NULL;
12064 					$$ = n;
12065 				}
12066 			| table_ref join_type JOIN table_ref join_qual
12067 				{
12068 					JoinExpr *n = makeNode(JoinExpr);
12069 					n->jointype = $2;
12070 					n->isNatural = false;
12071 					n->larg = $1;
12072 					n->rarg = $4;
12073 					if ($5 != NULL && IsA($5, List))
12074 						n->usingClause = (List *) $5; /* USING clause */
12075 					else
12076 						n->quals = $5; /* ON clause */
12077 					$$ = n;
12078 				}
12079 			| table_ref JOIN table_ref join_qual
12080 				{
12081 					/* letting join_type reduce to empty doesn't work */
12082 					JoinExpr *n = makeNode(JoinExpr);
12083 					n->jointype = JOIN_INNER;
12084 					n->isNatural = false;
12085 					n->larg = $1;
12086 					n->rarg = $3;
12087 					if ($4 != NULL && IsA($4, List))
12088 						n->usingClause = (List *) $4; /* USING clause */
12089 					else
12090 						n->quals = $4; /* ON clause */
12091 					$$ = n;
12092 				}
12093 			| table_ref NATURAL join_type JOIN table_ref
12094 				{
12095 					JoinExpr *n = makeNode(JoinExpr);
12096 					n->jointype = $3;
12097 					n->isNatural = true;
12098 					n->larg = $1;
12099 					n->rarg = $5;
12100 					n->usingClause = NIL; /* figure out which columns later... */
12101 					n->quals = NULL; /* fill later */
12102 					$$ = n;
12103 				}
12104 			| table_ref NATURAL JOIN table_ref
12105 				{
12106 					/* letting join_type reduce to empty doesn't work */
12107 					JoinExpr *n = makeNode(JoinExpr);
12108 					n->jointype = JOIN_INNER;
12109 					n->isNatural = true;
12110 					n->larg = $1;
12111 					n->rarg = $4;
12112 					n->usingClause = NIL; /* figure out which columns later... */
12113 					n->quals = NULL; /* fill later */
12114 					$$ = n;
12115 				}
12116 		;
12117 
12118 alias_clause:
12119 			AS ColId '(' name_list ')'
12120 				{
12121 					$$ = makeNode(Alias);
12122 					$$->aliasname = $2;
12123 					$$->colnames = $4;
12124 				}
12125 			| AS ColId
12126 				{
12127 					$$ = makeNode(Alias);
12128 					$$->aliasname = $2;
12129 				}
12130 			| ColId '(' name_list ')'
12131 				{
12132 					$$ = makeNode(Alias);
12133 					$$->aliasname = $1;
12134 					$$->colnames = $3;
12135 				}
12136 			| ColId
12137 				{
12138 					$$ = makeNode(Alias);
12139 					$$->aliasname = $1;
12140 				}
12141 		;
12142 
12143 opt_alias_clause: alias_clause						{ $$ = $1; }
12144 			| /*EMPTY*/								{ $$ = NULL; }
12145 		;
12146 
12147 /*
12148  * func_alias_clause can include both an Alias and a coldeflist, so we make it
12149  * return a 2-element list that gets disassembled by calling production.
12150  */
12151 func_alias_clause:
12152 			alias_clause
12153 				{
12154 					$$ = list_make2($1, NIL);
12155 				}
12156 			| AS '(' TableFuncElementList ')'
12157 				{
12158 					$$ = list_make2(NULL, $3);
12159 				}
12160 			| AS ColId '(' TableFuncElementList ')'
12161 				{
12162 					Alias *a = makeNode(Alias);
12163 					a->aliasname = $2;
12164 					$$ = list_make2(a, $4);
12165 				}
12166 			| ColId '(' TableFuncElementList ')'
12167 				{
12168 					Alias *a = makeNode(Alias);
12169 					a->aliasname = $1;
12170 					$$ = list_make2(a, $3);
12171 				}
12172 			| /*EMPTY*/
12173 				{
12174 					$$ = list_make2(NULL, NIL);
12175 				}
12176 		;
12177 
12178 join_type:	FULL join_outer							{ $$ = JOIN_FULL; }
12179 			| LEFT join_outer						{ $$ = JOIN_LEFT; }
12180 			| RIGHT join_outer						{ $$ = JOIN_RIGHT; }
12181 			| INNER_P								{ $$ = JOIN_INNER; }
12182 		;
12183 
12184 /* OUTER is just noise... */
12185 join_outer: OUTER_P									{ $$ = NULL; }
12186 			| /*EMPTY*/								{ $$ = NULL; }
12187 		;
12188 
12189 /* JOIN qualification clauses
12190  * Possibilities are:
12191  *	USING ( column list ) allows only unqualified column names,
12192  *						  which must match between tables.
12193  *	ON expr allows more general qualifications.
12194  *
12195  * We return USING as a List node, while an ON-expr will not be a List.
12196  */
12197 
12198 join_qual:	USING '(' name_list ')'					{ $$ = (Node *) $3; }
12199 			| ON a_expr								{ $$ = $2; }
12200 		;
12201 
12202 
12203 relation_expr:
12204 			qualified_name
12205 				{
12206 					/* inheritance query, implicitly */
12207 					$$ = $1;
12208 					$$->inh = true;
12209 					$$->alias = NULL;
12210 				}
12211 			| qualified_name '*'
12212 				{
12213 					/* inheritance query, explicitly */
12214 					$$ = $1;
12215 					$$->inh = true;
12216 					$$->alias = NULL;
12217 				}
12218 			| ONLY qualified_name
12219 				{
12220 					/* no inheritance */
12221 					$$ = $2;
12222 					$$->inh = false;
12223 					$$->alias = NULL;
12224 				}
12225 			| ONLY '(' qualified_name ')'
12226 				{
12227 					/* no inheritance, SQL99-style syntax */
12228 					$$ = $3;
12229 					$$->inh = false;
12230 					$$->alias = NULL;
12231 				}
12232 		;
12233 
12234 
12235 relation_expr_list:
12236 			relation_expr							{ $$ = list_make1($1); }
12237 			| relation_expr_list ',' relation_expr	{ $$ = lappend($1, $3); }
12238 		;
12239 
12240 
12241 /*
12242  * Given "UPDATE foo set set ...", we have to decide without looking any
12243  * further ahead whether the first "set" is an alias or the UPDATE's SET
12244  * keyword.  Since "set" is allowed as a column name both interpretations
12245  * are feasible.  We resolve the shift/reduce conflict by giving the first
12246  * relation_expr_opt_alias production a higher precedence than the SET token
12247  * has, causing the parser to prefer to reduce, in effect assuming that the
12248  * SET is not an alias.
12249  */
12250 relation_expr_opt_alias: relation_expr					%prec UMINUS
12251 				{
12252 					$$ = $1;
12253 				}
12254 			| relation_expr ColId
12255 				{
12256 					Alias *alias = makeNode(Alias);
12257 					alias->aliasname = $2;
12258 					$1->alias = alias;
12259 					$$ = $1;
12260 				}
12261 			| relation_expr AS ColId
12262 				{
12263 					Alias *alias = makeNode(Alias);
12264 					alias->aliasname = $3;
12265 					$1->alias = alias;
12266 					$$ = $1;
12267 				}
12268 		;
12269 
12270 /*
12271  * TABLESAMPLE decoration in a FROM item
12272  */
12273 tablesample_clause:
12274 			TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause
12275 				{
12276 					RangeTableSample *n = makeNode(RangeTableSample);
12277 					/* n->relation will be filled in later */
12278 					n->method = $2;
12279 					n->args = $4;
12280 					n->repeatable = $6;
12281 					n->location = @2;
12282 					$$ = (Node *) n;
12283 				}
12284 		;
12285 
12286 opt_repeatable_clause:
12287 			REPEATABLE '(' a_expr ')'	{ $$ = (Node *) $3; }
12288 			| /*EMPTY*/					{ $$ = NULL; }
12289 		;
12290 
12291 /*
12292  * func_table represents a function invocation in a FROM list. It can be
12293  * a plain function call, like "foo(...)", or a ROWS FROM expression with
12294  * one or more function calls, "ROWS FROM (foo(...), bar(...))",
12295  * optionally with WITH ORDINALITY attached.
12296  * In the ROWS FROM syntax, a column definition list can be given for each
12297  * function, for example:
12298  *     ROWS FROM (foo() AS (foo_res_a text, foo_res_b text),
12299  *                bar() AS (bar_res_a text, bar_res_b text))
12300  * It's also possible to attach a column definition list to the RangeFunction
12301  * as a whole, but that's handled by the table_ref production.
12302  */
12303 func_table: func_expr_windowless opt_ordinality
12304 				{
12305 					RangeFunction *n = makeNode(RangeFunction);
12306 					n->lateral = false;
12307 					n->ordinality = $2;
12308 					n->is_rowsfrom = false;
12309 					n->functions = list_make1(list_make2($1, NIL));
12310 					/* alias and coldeflist are set by table_ref production */
12311 					$$ = (Node *) n;
12312 				}
12313 			| ROWS FROM '(' rowsfrom_list ')' opt_ordinality
12314 				{
12315 					RangeFunction *n = makeNode(RangeFunction);
12316 					n->lateral = false;
12317 					n->ordinality = $6;
12318 					n->is_rowsfrom = true;
12319 					n->functions = $4;
12320 					/* alias and coldeflist are set by table_ref production */
12321 					$$ = (Node *) n;
12322 				}
12323 		;
12324 
12325 rowsfrom_item: func_expr_windowless opt_col_def_list
12326 				{ $$ = list_make2($1, $2); }
12327 		;
12328 
12329 rowsfrom_list:
12330 			rowsfrom_item						{ $$ = list_make1($1); }
12331 			| rowsfrom_list ',' rowsfrom_item	{ $$ = lappend($1, $3); }
12332 		;
12333 
12334 opt_col_def_list: AS '(' TableFuncElementList ')'	{ $$ = $3; }
12335 			| /*EMPTY*/								{ $$ = NIL; }
12336 		;
12337 
12338 opt_ordinality: WITH_LA ORDINALITY					{ $$ = true; }
12339 			| /*EMPTY*/								{ $$ = false; }
12340 		;
12341 
12342 
12343 where_clause:
12344 			WHERE a_expr							{ $$ = $2; }
12345 			| /*EMPTY*/								{ $$ = NULL; }
12346 		;
12347 
12348 /* variant for UPDATE and DELETE */
12349 where_or_current_clause:
12350 			WHERE a_expr							{ $$ = $2; }
12351 			| WHERE CURRENT_P OF cursor_name
12352 				{
12353 					CurrentOfExpr *n = makeNode(CurrentOfExpr);
12354 					/* cvarno is filled in by parse analysis */
12355 					n->cursor_name = $4;
12356 					n->cursor_param = 0;
12357 					$$ = (Node *) n;
12358 				}
12359 			| /*EMPTY*/								{ $$ = NULL; }
12360 		;
12361 
12362 
12363 OptTableFuncElementList:
12364 			TableFuncElementList				{ $$ = $1; }
12365 			| /*EMPTY*/							{ $$ = NIL; }
12366 		;
12367 
12368 TableFuncElementList:
12369 			TableFuncElement
12370 				{
12371 					$$ = list_make1($1);
12372 				}
12373 			| TableFuncElementList ',' TableFuncElement
12374 				{
12375 					$$ = lappend($1, $3);
12376 				}
12377 		;
12378 
12379 TableFuncElement:	ColId Typename opt_collate_clause
12380 				{
12381 					ColumnDef *n = makeNode(ColumnDef);
12382 					n->colname = $1;
12383 					n->typeName = $2;
12384 					n->inhcount = 0;
12385 					n->is_local = true;
12386 					n->is_not_null = false;
12387 					n->is_from_type = false;
12388 					n->storage = 0;
12389 					n->raw_default = NULL;
12390 					n->cooked_default = NULL;
12391 					n->collClause = (CollateClause *) $3;
12392 					n->collOid = InvalidOid;
12393 					n->constraints = NIL;
12394 					n->location = @1;
12395 					$$ = (Node *)n;
12396 				}
12397 		;
12398 
12399 /*
12400  * XMLTABLE
12401  */
12402 xmltable:
12403 			XMLTABLE '(' c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12404 				{
12405 					RangeTableFunc *n = makeNode(RangeTableFunc);
12406 					n->rowexpr = $3;
12407 					n->docexpr = $4;
12408 					n->columns = $6;
12409 					n->namespaces = NIL;
12410 					n->location = @1;
12411 					$$ = (Node *)n;
12412 				}
12413 			| XMLTABLE '(' XMLNAMESPACES '(' xml_namespace_list ')' ','
12414 				c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12415 				{
12416 					RangeTableFunc *n = makeNode(RangeTableFunc);
12417 					n->rowexpr = $8;
12418 					n->docexpr = $9;
12419 					n->columns = $11;
12420 					n->namespaces = $5;
12421 					n->location = @1;
12422 					$$ = (Node *)n;
12423 				}
12424 		;
12425 
12426 xmltable_column_list: xmltable_column_el					{ $$ = list_make1($1); }
12427 			| xmltable_column_list ',' xmltable_column_el	{ $$ = lappend($1, $3); }
12428 		;
12429 
12430 xmltable_column_el:
12431 			ColId Typename
12432 				{
12433 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12434 
12435 					fc->colname = $1;
12436 					fc->for_ordinality = false;
12437 					fc->typeName = $2;
12438 					fc->is_not_null = false;
12439 					fc->colexpr = NULL;
12440 					fc->coldefexpr = NULL;
12441 					fc->location = @1;
12442 
12443 					$$ = (Node *) fc;
12444 				}
12445 			| ColId Typename xmltable_column_option_list
12446 				{
12447 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12448 					ListCell		   *option;
12449 					bool				nullability_seen = false;
12450 
12451 					fc->colname = $1;
12452 					fc->typeName = $2;
12453 					fc->for_ordinality = false;
12454 					fc->is_not_null = false;
12455 					fc->colexpr = NULL;
12456 					fc->coldefexpr = NULL;
12457 					fc->location = @1;
12458 
foreach(option,$3)12459 					foreach(option, $3)
12460 					{
12461 						DefElem   *defel = (DefElem *) lfirst(option);
12462 
12463 						if (strcmp(defel->defname, "default") == 0)
12464 						{
12465 							if (fc->coldefexpr != NULL)
12466 								ereport(ERROR,
12467 										(errcode(ERRCODE_SYNTAX_ERROR),
12468 										 errmsg("only one DEFAULT value is allowed"),
12469 										 parser_errposition(defel->location)));
12470 							fc->coldefexpr = defel->arg;
12471 						}
12472 						else if (strcmp(defel->defname, "path") == 0)
12473 						{
12474 							if (fc->colexpr != NULL)
12475 								ereport(ERROR,
12476 										(errcode(ERRCODE_SYNTAX_ERROR),
12477 										 errmsg("only one PATH value per column is allowed"),
12478 										 parser_errposition(defel->location)));
12479 							fc->colexpr = defel->arg;
12480 						}
12481 						else if (strcmp(defel->defname, "is_not_null") == 0)
12482 						{
12483 							if (nullability_seen)
12484 								ereport(ERROR,
12485 										(errcode(ERRCODE_SYNTAX_ERROR),
12486 										 errmsg("conflicting or redundant NULL / NOT NULL declarations for column \"%s\"", fc->colname),
12487 										 parser_errposition(defel->location)));
12488 							fc->is_not_null = intVal(defel->arg);
12489 							nullability_seen = true;
12490 						}
12491 						else
12492 						{
12493 							ereport(ERROR,
12494 									(errcode(ERRCODE_SYNTAX_ERROR),
12495 									 errmsg("unrecognized column option \"%s\"",
12496 											defel->defname),
12497 									 parser_errposition(defel->location)));
12498 						}
12499 					}
12500 					$$ = (Node *) fc;
12501 				}
12502 			| ColId FOR ORDINALITY
12503 				{
12504 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12505 
12506 					fc->colname = $1;
12507 					fc->for_ordinality = true;
12508 					/* other fields are ignored, initialized by makeNode */
12509 					fc->location = @1;
12510 
12511 					$$ = (Node *) fc;
12512 				}
12513 		;
12514 
12515 xmltable_column_option_list:
12516 			xmltable_column_option_el
12517 				{ $$ = list_make1($1); }
12518 			| xmltable_column_option_list xmltable_column_option_el
12519 				{ $$ = lappend($1, $2); }
12520 		;
12521 
12522 xmltable_column_option_el:
12523 			IDENT b_expr
12524 				{ $$ = makeDefElem($1, $2, @1); }
12525 			| DEFAULT b_expr
12526 				{ $$ = makeDefElem("default", $2, @1); }
12527 			| NOT NULL_P
12528 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(true), @1); }
12529 			| NULL_P
12530 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(false), @1); }
12531 		;
12532 
12533 xml_namespace_list:
12534 			xml_namespace_el
12535 				{ $$ = list_make1($1); }
12536 			| xml_namespace_list ',' xml_namespace_el
12537 				{ $$ = lappend($1, $3); }
12538 		;
12539 
12540 xml_namespace_el:
12541 			b_expr AS ColLabel
12542 				{
12543 					$$ = makeNode(ResTarget);
12544 					$$->name = $3;
12545 					$$->indirection = NIL;
12546 					$$->val = $1;
12547 					$$->location = @1;
12548 				}
12549 			| DEFAULT b_expr
12550 				{
12551 					$$ = makeNode(ResTarget);
12552 					$$->name = NULL;
12553 					$$->indirection = NIL;
12554 					$$->val = $2;
12555 					$$->location = @1;
12556 				}
12557 		;
12558 
12559 /*****************************************************************************
12560  *
12561  *	Type syntax
12562  *		SQL introduces a large amount of type-specific syntax.
12563  *		Define individual clauses to handle these cases, and use
12564  *		 the generic case to handle regular type-extensible Postgres syntax.
12565  *		- thomas 1997-10-10
12566  *
12567  *****************************************************************************/
12568 
12569 Typename:	SimpleTypename opt_array_bounds
12570 				{
12571 					$$ = $1;
12572 					$$->arrayBounds = $2;
12573 				}
12574 			| SETOF SimpleTypename opt_array_bounds
12575 				{
12576 					$$ = $2;
12577 					$$->arrayBounds = $3;
12578 					$$->setof = true;
12579 				}
12580 			/* SQL standard syntax, currently only one-dimensional */
12581 			| SimpleTypename ARRAY '[' Iconst ']'
12582 				{
12583 					$$ = $1;
12584 					$$->arrayBounds = list_make1(makeInteger($4));
12585 				}
12586 			| SETOF SimpleTypename ARRAY '[' Iconst ']'
12587 				{
12588 					$$ = $2;
12589 					$$->arrayBounds = list_make1(makeInteger($5));
12590 					$$->setof = true;
12591 				}
12592 			| SimpleTypename ARRAY
12593 				{
12594 					$$ = $1;
12595 					$$->arrayBounds = list_make1(makeInteger(-1));
12596 				}
12597 			| SETOF SimpleTypename ARRAY
12598 				{
12599 					$$ = $2;
12600 					$$->arrayBounds = list_make1(makeInteger(-1));
12601 					$$->setof = true;
12602 				}
12603 		;
12604 
12605 opt_array_bounds:
12606 			opt_array_bounds '[' ']'
12607 					{  $$ = lappend($1, makeInteger(-1)); }
12608 			| opt_array_bounds '[' Iconst ']'
12609 					{  $$ = lappend($1, makeInteger($3)); }
12610 			| /*EMPTY*/
12611 					{  $$ = NIL; }
12612 		;
12613 
12614 SimpleTypename:
12615 			GenericType								{ $$ = $1; }
12616 			| Numeric								{ $$ = $1; }
12617 			| Bit									{ $$ = $1; }
12618 			| Character								{ $$ = $1; }
12619 			| ConstDatetime							{ $$ = $1; }
12620 			| ConstInterval opt_interval
12621 				{
12622 					$$ = $1;
12623 					$$->typmods = $2;
12624 				}
12625 			| ConstInterval '(' Iconst ')'
12626 				{
12627 					$$ = $1;
12628 					$$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
12629 											 makeIntConst($3, @3));
12630 				}
12631 		;
12632 
12633 /* We have a separate ConstTypename to allow defaulting fixed-length
12634  * types such as CHAR() and BIT() to an unspecified length.
12635  * SQL9x requires that these default to a length of one, but this
12636  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
12637  * where there is an obvious better choice to make.
12638  * Note that ConstInterval is not included here since it must
12639  * be pushed up higher in the rules to accommodate the postfix
12640  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
12641  * the generic-type-name case in AExprConst to avoid premature
12642  * reduce/reduce conflicts against function names.
12643  */
12644 ConstTypename:
12645 			Numeric									{ $$ = $1; }
12646 			| ConstBit								{ $$ = $1; }
12647 			| ConstCharacter						{ $$ = $1; }
12648 			| ConstDatetime							{ $$ = $1; }
12649 		;
12650 
12651 /*
12652  * GenericType covers all type names that don't have special syntax mandated
12653  * by the standard, including qualified names.  We also allow type modifiers.
12654  * To avoid parsing conflicts against function invocations, the modifiers
12655  * have to be shown as expr_list here, but parse analysis will only accept
12656  * constants for them.
12657  */
12658 GenericType:
12659 			type_function_name opt_type_modifiers
12660 				{
12661 					$$ = makeTypeName($1);
12662 					$$->typmods = $2;
12663 					$$->location = @1;
12664 				}
12665 			| type_function_name attrs opt_type_modifiers
12666 				{
12667 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
12668 					$$->typmods = $3;
12669 					$$->location = @1;
12670 				}
12671 		;
12672 
12673 opt_type_modifiers: '(' expr_list ')'				{ $$ = $2; }
12674 					| /* EMPTY */					{ $$ = NIL; }
12675 		;
12676 
12677 /*
12678  * SQL numeric data types
12679  */
12680 Numeric:	INT_P
12681 				{
12682 					$$ = SystemTypeName("int4");
12683 					$$->location = @1;
12684 				}
12685 			| INTEGER
12686 				{
12687 					$$ = SystemTypeName("int4");
12688 					$$->location = @1;
12689 				}
12690 			| SMALLINT
12691 				{
12692 					$$ = SystemTypeName("int2");
12693 					$$->location = @1;
12694 				}
12695 			| BIGINT
12696 				{
12697 					$$ = SystemTypeName("int8");
12698 					$$->location = @1;
12699 				}
12700 			| REAL
12701 				{
12702 					$$ = SystemTypeName("float4");
12703 					$$->location = @1;
12704 				}
12705 			| FLOAT_P opt_float
12706 				{
12707 					$$ = $2;
12708 					$$->location = @1;
12709 				}
12710 			| DOUBLE_P PRECISION
12711 				{
12712 					$$ = SystemTypeName("float8");
12713 					$$->location = @1;
12714 				}
12715 			| DECIMAL_P opt_type_modifiers
12716 				{
12717 					$$ = SystemTypeName("numeric");
12718 					$$->typmods = $2;
12719 					$$->location = @1;
12720 				}
12721 			| DEC opt_type_modifiers
12722 				{
12723 					$$ = SystemTypeName("numeric");
12724 					$$->typmods = $2;
12725 					$$->location = @1;
12726 				}
12727 			| NUMERIC opt_type_modifiers
12728 				{
12729 					$$ = SystemTypeName("numeric");
12730 					$$->typmods = $2;
12731 					$$->location = @1;
12732 				}
12733 			| BOOLEAN_P
12734 				{
12735 					$$ = SystemTypeName("bool");
12736 					$$->location = @1;
12737 				}
12738 		;
12739 
12740 opt_float:	'(' Iconst ')'
12741 				{
12742 					/*
12743 					 * Check FLOAT() precision limits assuming IEEE floating
12744 					 * types - thomas 1997-09-18
12745 					 */
12746 					if ($2 < 1)
12747 						ereport(ERROR,
12748 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12749 								 errmsg("precision for type float must be at least 1 bit"),
12750 								 parser_errposition(@2)));
12751 					else if ($2 <= 24)
12752 						$$ = SystemTypeName("float4");
12753 					else if ($2 <= 53)
12754 						$$ = SystemTypeName("float8");
12755 					else
12756 						ereport(ERROR,
12757 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12758 								 errmsg("precision for type float must be less than 54 bits"),
12759 								 parser_errposition(@2)));
12760 				}
12761 			| /*EMPTY*/
12762 				{
12763 					$$ = SystemTypeName("float8");
12764 				}
12765 		;
12766 
12767 /*
12768  * SQL bit-field data types
12769  * The following implements BIT() and BIT VARYING().
12770  */
12771 Bit:		BitWithLength
12772 				{
12773 					$$ = $1;
12774 				}
12775 			| BitWithoutLength
12776 				{
12777 					$$ = $1;
12778 				}
12779 		;
12780 
12781 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
12782 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
12783 ConstBit:	BitWithLength
12784 				{
12785 					$$ = $1;
12786 				}
12787 			| BitWithoutLength
12788 				{
12789 					$$ = $1;
12790 					$$->typmods = NIL;
12791 				}
12792 		;
12793 
12794 BitWithLength:
12795 			BIT opt_varying '(' expr_list ')'
12796 				{
12797 					char *typname;
12798 
12799 					typname = $2 ? "varbit" : "bit";
12800 					$$ = SystemTypeName(typname);
12801 					$$->typmods = $4;
12802 					$$->location = @1;
12803 				}
12804 		;
12805 
12806 BitWithoutLength:
12807 			BIT opt_varying
12808 				{
12809 					/* bit defaults to bit(1), varbit to no limit */
12810 					if ($2)
12811 					{
12812 						$$ = SystemTypeName("varbit");
12813 					}
12814 					else
12815 					{
12816 						$$ = SystemTypeName("bit");
12817 						$$->typmods = list_make1(makeIntConst(1, -1));
12818 					}
12819 					$$->location = @1;
12820 				}
12821 		;
12822 
12823 
12824 /*
12825  * SQL character data types
12826  * The following implements CHAR() and VARCHAR().
12827  */
12828 Character:  CharacterWithLength
12829 				{
12830 					$$ = $1;
12831 				}
12832 			| CharacterWithoutLength
12833 				{
12834 					$$ = $1;
12835 				}
12836 		;
12837 
12838 ConstCharacter:  CharacterWithLength
12839 				{
12840 					$$ = $1;
12841 				}
12842 			| CharacterWithoutLength
12843 				{
12844 					/* Length was not specified so allow to be unrestricted.
12845 					 * This handles problems with fixed-length (bpchar) strings
12846 					 * which in column definitions must default to a length
12847 					 * of one, but should not be constrained if the length
12848 					 * was not specified.
12849 					 */
12850 					$$ = $1;
12851 					$$->typmods = NIL;
12852 				}
12853 		;
12854 
12855 CharacterWithLength:  character '(' Iconst ')'
12856 				{
12857 					$$ = SystemTypeName($1);
12858 					$$->typmods = list_make1(makeIntConst($3, @3));
12859 					$$->location = @1;
12860 				}
12861 		;
12862 
12863 CharacterWithoutLength:	 character
12864 				{
12865 					$$ = SystemTypeName($1);
12866 					/* char defaults to char(1), varchar to no limit */
12867 					if (strcmp($1, "bpchar") == 0)
12868 						$$->typmods = list_make1(makeIntConst(1, -1));
12869 					$$->location = @1;
12870 				}
12871 		;
12872 
12873 character:	CHARACTER opt_varying
12874 										{ $$ = $2 ? "varchar": "bpchar"; }
12875 			| CHAR_P opt_varying
12876 										{ $$ = $2 ? "varchar": "bpchar"; }
12877 			| VARCHAR
12878 										{ $$ = "varchar"; }
12879 			| NATIONAL CHARACTER opt_varying
12880 										{ $$ = $3 ? "varchar": "bpchar"; }
12881 			| NATIONAL CHAR_P opt_varying
12882 										{ $$ = $3 ? "varchar": "bpchar"; }
12883 			| NCHAR opt_varying
12884 										{ $$ = $2 ? "varchar": "bpchar"; }
12885 		;
12886 
12887 opt_varying:
12888 			VARYING									{ $$ = true; }
12889 			| /*EMPTY*/								{ $$ = false; }
12890 		;
12891 
12892 /*
12893  * SQL date/time types
12894  */
12895 ConstDatetime:
12896 			TIMESTAMP '(' Iconst ')' opt_timezone
12897 				{
12898 					if ($5)
12899 						$$ = SystemTypeName("timestamptz");
12900 					else
12901 						$$ = SystemTypeName("timestamp");
12902 					$$->typmods = list_make1(makeIntConst($3, @3));
12903 					$$->location = @1;
12904 				}
12905 			| TIMESTAMP opt_timezone
12906 				{
12907 					if ($2)
12908 						$$ = SystemTypeName("timestamptz");
12909 					else
12910 						$$ = SystemTypeName("timestamp");
12911 					$$->location = @1;
12912 				}
12913 			| TIME '(' Iconst ')' opt_timezone
12914 				{
12915 					if ($5)
12916 						$$ = SystemTypeName("timetz");
12917 					else
12918 						$$ = SystemTypeName("time");
12919 					$$->typmods = list_make1(makeIntConst($3, @3));
12920 					$$->location = @1;
12921 				}
12922 			| TIME opt_timezone
12923 				{
12924 					if ($2)
12925 						$$ = SystemTypeName("timetz");
12926 					else
12927 						$$ = SystemTypeName("time");
12928 					$$->location = @1;
12929 				}
12930 		;
12931 
12932 ConstInterval:
12933 			INTERVAL
12934 				{
12935 					$$ = SystemTypeName("interval");
12936 					$$->location = @1;
12937 				}
12938 		;
12939 
12940 opt_timezone:
12941 			WITH_LA TIME ZONE						{ $$ = true; }
12942 			| WITHOUT TIME ZONE						{ $$ = false; }
12943 			| /*EMPTY*/								{ $$ = false; }
12944 		;
12945 
12946 opt_interval:
12947 			YEAR_P
12948 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
12949 			| MONTH_P
12950 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
12951 			| DAY_P
12952 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
12953 			| HOUR_P
12954 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
12955 			| MINUTE_P
12956 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
12957 			| interval_second
12958 				{ $$ = $1; }
12959 			| YEAR_P TO MONTH_P
12960 				{
12961 					$$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
12962 												 INTERVAL_MASK(MONTH), @1));
12963 				}
12964 			| DAY_P TO HOUR_P
12965 				{
12966 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12967 												 INTERVAL_MASK(HOUR), @1));
12968 				}
12969 			| DAY_P TO MINUTE_P
12970 				{
12971 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12972 												 INTERVAL_MASK(HOUR) |
12973 												 INTERVAL_MASK(MINUTE), @1));
12974 				}
12975 			| DAY_P TO interval_second
12976 				{
12977 					$$ = $3;
12978 					linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
12979 												INTERVAL_MASK(HOUR) |
12980 												INTERVAL_MASK(MINUTE) |
12981 												INTERVAL_MASK(SECOND), @1);
12982 				}
12983 			| HOUR_P TO MINUTE_P
12984 				{
12985 					$$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
12986 												 INTERVAL_MASK(MINUTE), @1));
12987 				}
12988 			| HOUR_P TO interval_second
12989 				{
12990 					$$ = $3;
12991 					linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
12992 												INTERVAL_MASK(MINUTE) |
12993 												INTERVAL_MASK(SECOND), @1);
12994 				}
12995 			| MINUTE_P TO interval_second
12996 				{
12997 					$$ = $3;
12998 					linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
12999 												INTERVAL_MASK(SECOND), @1);
13000 				}
13001 			| /*EMPTY*/
13002 				{ $$ = NIL; }
13003 		;
13004 
13005 interval_second:
13006 			SECOND_P
13007 				{
13008 					$$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
13009 				}
13010 			| SECOND_P '(' Iconst ')'
13011 				{
13012 					$$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
13013 									makeIntConst($3, @3));
13014 				}
13015 		;
13016 
13017 
13018 /*****************************************************************************
13019  *
13020  *	expression grammar
13021  *
13022  *****************************************************************************/
13023 
13024 /*
13025  * General expressions
13026  * This is the heart of the expression syntax.
13027  *
13028  * We have two expression types: a_expr is the unrestricted kind, and
13029  * b_expr is a subset that must be used in some places to avoid shift/reduce
13030  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
13031  * because that use of AND conflicts with AND as a boolean operator.  So,
13032  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
13033  *
13034  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
13035  * always be used by surrounding it with parens.
13036  *
13037  * c_expr is all the productions that are common to a_expr and b_expr;
13038  * it's factored out just to eliminate redundant coding.
13039  *
13040  * Be careful of productions involving more than one terminal token.
13041  * By default, bison will assign such productions the precedence of their
13042  * last terminal, but in nearly all cases you want it to be the precedence
13043  * of the first terminal instead; otherwise you will not get the behavior
13044  * you expect!  So we use %prec annotations freely to set precedences.
13045  */
13046 a_expr:		c_expr									{ $$ = $1; }
13047 			| a_expr TYPECAST Typename
13048 					{ $$ = makeTypeCast($1, $3, @2); }
13049 			| a_expr COLLATE any_name
13050 				{
13051 					CollateClause *n = makeNode(CollateClause);
13052 					n->arg = $1;
13053 					n->collname = $3;
13054 					n->location = @2;
13055 					$$ = (Node *) n;
13056 				}
13057 			| a_expr AT TIME ZONE a_expr			%prec AT
13058 				{
13059 					$$ = (Node *) makeFuncCall(SystemFuncName("timezone"),
13060 											   list_make2($5, $1),
13061 											   @2);
13062 				}
13063 		/*
13064 		 * These operators must be called out explicitly in order to make use
13065 		 * of bison's automatic operator-precedence handling.  All other
13066 		 * operator names are handled by the generic productions using "Op",
13067 		 * below; and all those operators will have the same precedence.
13068 		 *
13069 		 * If you add more explicitly-known operators, be sure to add them
13070 		 * also to b_expr and to the MathOp list below.
13071 		 */
13072 			| '+' a_expr					%prec UMINUS
13073 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13074 			| '-' a_expr					%prec UMINUS
13075 				{ $$ = doNegate($2, @1); }
13076 			| a_expr '+' a_expr
13077 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13078 			| a_expr '-' a_expr
13079 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13080 			| a_expr '*' a_expr
13081 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13082 			| a_expr '/' a_expr
13083 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13084 			| a_expr '%' a_expr
13085 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13086 			| a_expr '^' a_expr
13087 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13088 			| a_expr '<' a_expr
13089 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13090 			| a_expr '>' a_expr
13091 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13092 			| a_expr '=' a_expr
13093 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13094 			| a_expr LESS_EQUALS a_expr
13095 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13096 			| a_expr GREATER_EQUALS a_expr
13097 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13098 			| a_expr NOT_EQUALS a_expr
13099 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13100 
13101 			| a_expr qual_Op a_expr				%prec Op
13102 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13103 			| qual_Op a_expr					%prec Op
13104 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13105 			| a_expr qual_Op					%prec POSTFIXOP
13106 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13107 
13108 			| a_expr AND a_expr
13109 				{ $$ = makeAndExpr($1, $3, @2); }
13110 			| a_expr OR a_expr
13111 				{ $$ = makeOrExpr($1, $3, @2); }
13112 			| NOT a_expr
13113 				{ $$ = makeNotExpr($2, @1); }
13114 			| NOT_LA a_expr						%prec NOT
13115 				{ $$ = makeNotExpr($2, @1); }
13116 
13117 			| a_expr LIKE a_expr
13118 				{
13119 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13120 												   $1, $3, @2);
13121 				}
13122 			| a_expr LIKE a_expr ESCAPE a_expr					%prec LIKE
13123 				{
13124 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13125 											   list_make2($3, $5),
13126 											   @2);
13127 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13128 												   $1, (Node *) n, @2);
13129 				}
13130 			| a_expr NOT_LA LIKE a_expr							%prec NOT_LA
13131 				{
13132 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13133 												   $1, $4, @2);
13134 				}
13135 			| a_expr NOT_LA LIKE a_expr ESCAPE a_expr			%prec NOT_LA
13136 				{
13137 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13138 											   list_make2($4, $6),
13139 											   @2);
13140 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13141 												   $1, (Node *) n, @2);
13142 				}
13143 			| a_expr ILIKE a_expr
13144 				{
13145 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13146 												   $1, $3, @2);
13147 				}
13148 			| a_expr ILIKE a_expr ESCAPE a_expr					%prec ILIKE
13149 				{
13150 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13151 											   list_make2($3, $5),
13152 											   @2);
13153 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13154 												   $1, (Node *) n, @2);
13155 				}
13156 			| a_expr NOT_LA ILIKE a_expr						%prec NOT_LA
13157 				{
13158 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13159 												   $1, $4, @2);
13160 				}
13161 			| a_expr NOT_LA ILIKE a_expr ESCAPE a_expr			%prec NOT_LA
13162 				{
13163 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13164 											   list_make2($4, $6),
13165 											   @2);
13166 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13167 												   $1, (Node *) n, @2);
13168 				}
13169 
13170 			| a_expr SIMILAR TO a_expr							%prec SIMILAR
13171 				{
13172 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13173 											   list_make2($4, makeNullAConst(-1)),
13174 											   @2);
13175 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13176 												   $1, (Node *) n, @2);
13177 				}
13178 			| a_expr SIMILAR TO a_expr ESCAPE a_expr			%prec SIMILAR
13179 				{
13180 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13181 											   list_make2($4, $6),
13182 											   @2);
13183 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13184 												   $1, (Node *) n, @2);
13185 				}
13186 			| a_expr NOT_LA SIMILAR TO a_expr					%prec NOT_LA
13187 				{
13188 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13189 											   list_make2($5, makeNullAConst(-1)),
13190 											   @2);
13191 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13192 												   $1, (Node *) n, @2);
13193 				}
13194 			| a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr		%prec NOT_LA
13195 				{
13196 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13197 											   list_make2($5, $7),
13198 											   @2);
13199 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13200 												   $1, (Node *) n, @2);
13201 				}
13202 
13203 			/* NullTest clause
13204 			 * Define SQL-style Null test clause.
13205 			 * Allow two forms described in the standard:
13206 			 *	a IS NULL
13207 			 *	a IS NOT NULL
13208 			 * Allow two SQL extensions
13209 			 *	a ISNULL
13210 			 *	a NOTNULL
13211 			 */
13212 			| a_expr IS NULL_P							%prec IS
13213 				{
13214 					NullTest *n = makeNode(NullTest);
13215 					n->arg = (Expr *) $1;
13216 					n->nulltesttype = IS_NULL;
13217 					n->location = @2;
13218 					$$ = (Node *)n;
13219 				}
13220 			| a_expr ISNULL
13221 				{
13222 					NullTest *n = makeNode(NullTest);
13223 					n->arg = (Expr *) $1;
13224 					n->nulltesttype = IS_NULL;
13225 					n->location = @2;
13226 					$$ = (Node *)n;
13227 				}
13228 			| a_expr IS NOT NULL_P						%prec IS
13229 				{
13230 					NullTest *n = makeNode(NullTest);
13231 					n->arg = (Expr *) $1;
13232 					n->nulltesttype = IS_NOT_NULL;
13233 					n->location = @2;
13234 					$$ = (Node *)n;
13235 				}
13236 			| a_expr NOTNULL
13237 				{
13238 					NullTest *n = makeNode(NullTest);
13239 					n->arg = (Expr *) $1;
13240 					n->nulltesttype = IS_NOT_NULL;
13241 					n->location = @2;
13242 					$$ = (Node *)n;
13243 				}
13244 			| row OVERLAPS row
13245 				{
13246 					if (list_length($1) != 2)
13247 						ereport(ERROR,
13248 								(errcode(ERRCODE_SYNTAX_ERROR),
13249 								 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
13250 								 parser_errposition(@1)));
13251 					if (list_length($3) != 2)
13252 						ereport(ERROR,
13253 								(errcode(ERRCODE_SYNTAX_ERROR),
13254 								 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
13255 								 parser_errposition(@3)));
13256 					$$ = (Node *) makeFuncCall(SystemFuncName("overlaps"),
13257 											   list_concat($1, $3),
13258 											   @2);
13259 				}
13260 			| a_expr IS TRUE_P							%prec IS
13261 				{
13262 					BooleanTest *b = makeNode(BooleanTest);
13263 					b->arg = (Expr *) $1;
13264 					b->booltesttype = IS_TRUE;
13265 					b->location = @2;
13266 					$$ = (Node *)b;
13267 				}
13268 			| a_expr IS NOT TRUE_P						%prec IS
13269 				{
13270 					BooleanTest *b = makeNode(BooleanTest);
13271 					b->arg = (Expr *) $1;
13272 					b->booltesttype = IS_NOT_TRUE;
13273 					b->location = @2;
13274 					$$ = (Node *)b;
13275 				}
13276 			| a_expr IS FALSE_P							%prec IS
13277 				{
13278 					BooleanTest *b = makeNode(BooleanTest);
13279 					b->arg = (Expr *) $1;
13280 					b->booltesttype = IS_FALSE;
13281 					b->location = @2;
13282 					$$ = (Node *)b;
13283 				}
13284 			| a_expr IS NOT FALSE_P						%prec IS
13285 				{
13286 					BooleanTest *b = makeNode(BooleanTest);
13287 					b->arg = (Expr *) $1;
13288 					b->booltesttype = IS_NOT_FALSE;
13289 					b->location = @2;
13290 					$$ = (Node *)b;
13291 				}
13292 			| a_expr IS UNKNOWN							%prec IS
13293 				{
13294 					BooleanTest *b = makeNode(BooleanTest);
13295 					b->arg = (Expr *) $1;
13296 					b->booltesttype = IS_UNKNOWN;
13297 					b->location = @2;
13298 					$$ = (Node *)b;
13299 				}
13300 			| a_expr IS NOT UNKNOWN						%prec IS
13301 				{
13302 					BooleanTest *b = makeNode(BooleanTest);
13303 					b->arg = (Expr *) $1;
13304 					b->booltesttype = IS_NOT_UNKNOWN;
13305 					b->location = @2;
13306 					$$ = (Node *)b;
13307 				}
13308 			| a_expr IS DISTINCT FROM a_expr			%prec IS
13309 				{
13310 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13311 				}
13312 			| a_expr IS NOT DISTINCT FROM a_expr		%prec IS
13313 				{
13314                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13315 				}
13316 			| a_expr IS OF '(' type_list ')'			%prec IS
13317 				{
13318 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13319 				}
13320 			| a_expr IS NOT OF '(' type_list ')'		%prec IS
13321 				{
13322 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13323 				}
13324 			| a_expr BETWEEN opt_asymmetric b_expr AND a_expr		%prec BETWEEN
13325 				{
13326 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN,
13327 												   "BETWEEN",
13328 												   $1,
13329 												   (Node *) list_make2($4, $6),
13330 												   @2);
13331 				}
13332 			| a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr %prec NOT_LA
13333 				{
13334 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN,
13335 												   "NOT BETWEEN",
13336 												   $1,
13337 												   (Node *) list_make2($5, $7),
13338 												   @2);
13339 				}
13340 			| a_expr BETWEEN SYMMETRIC b_expr AND a_expr			%prec BETWEEN
13341 				{
13342 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN_SYM,
13343 												   "BETWEEN SYMMETRIC",
13344 												   $1,
13345 												   (Node *) list_make2($4, $6),
13346 												   @2);
13347 				}
13348 			| a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr		%prec NOT_LA
13349 				{
13350 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN_SYM,
13351 												   "NOT BETWEEN SYMMETRIC",
13352 												   $1,
13353 												   (Node *) list_make2($5, $7),
13354 												   @2);
13355 				}
13356 			| a_expr IN_P in_expr
13357 				{
13358 					/* in_expr returns a SubLink or a list of a_exprs */
13359 					if (IsA($3, SubLink))
13360 					{
13361 						/* generate foo = ANY (subquery) */
13362 						SubLink *n = (SubLink *) $3;
13363 						n->subLinkType = ANY_SUBLINK;
13364 						n->subLinkId = 0;
13365 						n->testexpr = $1;
13366 						n->operName = NIL;		/* show it's IN not = ANY */
13367 						n->location = @2;
13368 						$$ = (Node *)n;
13369 					}
13370 					else
13371 					{
13372 						/* generate scalar IN expression */
13373 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
13374 					}
13375 				}
13376 			| a_expr NOT_LA IN_P in_expr						%prec NOT_LA
13377 				{
13378 					/* in_expr returns a SubLink or a list of a_exprs */
13379 					if (IsA($4, SubLink))
13380 					{
13381 						/* generate NOT (foo = ANY (subquery)) */
13382 						/* Make an = ANY node */
13383 						SubLink *n = (SubLink *) $4;
13384 						n->subLinkType = ANY_SUBLINK;
13385 						n->subLinkId = 0;
13386 						n->testexpr = $1;
13387 						n->operName = NIL;		/* show it's IN not = ANY */
13388 						n->location = @2;
13389 						/* Stick a NOT on top; must have same parse location */
13390 						$$ = makeNotExpr((Node *) n, @2);
13391 					}
13392 					else
13393 					{
13394 						/* generate scalar NOT IN expression */
13395 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
13396 					}
13397 				}
13398 			| a_expr subquery_Op sub_type select_with_parens	%prec Op
13399 				{
13400 					SubLink *n = makeNode(SubLink);
13401 					n->subLinkType = $3;
13402 					n->subLinkId = 0;
13403 					n->testexpr = $1;
13404 					n->operName = $2;
13405 					n->subselect = $4;
13406 					n->location = @2;
13407 					$$ = (Node *)n;
13408 				}
13409 			| a_expr subquery_Op sub_type '(' a_expr ')'		%prec Op
13410 				{
13411 					if ($3 == ANY_SUBLINK)
13412 						$$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
13413 					else
13414 						$$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
13415 				}
13416 			| UNIQUE select_with_parens
13417 				{
13418 					/* Not sure how to get rid of the parentheses
13419 					 * but there are lots of shift/reduce errors without them.
13420 					 *
13421 					 * Should be able to implement this by plopping the entire
13422 					 * select into a node, then transforming the target expressions
13423 					 * from whatever they are into count(*), and testing the
13424 					 * entire result equal to one.
13425 					 * But, will probably implement a separate node in the executor.
13426 					 */
13427 					ereport(ERROR,
13428 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13429 							 errmsg("UNIQUE predicate is not yet implemented"),
13430 							 parser_errposition(@1)));
13431 				}
13432 			| a_expr IS DOCUMENT_P					%prec IS
13433 				{
13434 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13435 									 list_make1($1), @2);
13436 				}
13437 			| a_expr IS NOT DOCUMENT_P				%prec IS
13438 				{
13439 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13440 												 list_make1($1), @2),
13441 									 @2);
13442 				}
13443 			| DEFAULT
13444 				{
13445 					/*
13446 					 * The SQL spec only allows DEFAULT in "contextually typed
13447 					 * expressions", but for us, it's easier to allow it in
13448 					 * any a_expr and then throw error during parse analysis
13449 					 * if it's in an inappropriate context.  This way also
13450 					 * lets us say something smarter than "syntax error".
13451 					 */
13452 					SetToDefault *n = makeNode(SetToDefault);
13453 					/* parse analysis will fill in the rest */
13454 					n->location = @1;
13455 					$$ = (Node *)n;
13456 				}
13457 		;
13458 
13459 /*
13460  * Restricted expressions
13461  *
13462  * b_expr is a subset of the complete expression syntax defined by a_expr.
13463  *
13464  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
13465  * cause trouble in the places where b_expr is used.  For simplicity, we
13466  * just eliminate all the boolean-keyword-operator productions from b_expr.
13467  */
13468 b_expr:		c_expr
13469 				{ $$ = $1; }
13470 			| b_expr TYPECAST Typename
13471 				{ $$ = makeTypeCast($1, $3, @2); }
13472 			| '+' b_expr					%prec UMINUS
13473 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13474 			| '-' b_expr					%prec UMINUS
13475 				{ $$ = doNegate($2, @1); }
13476 			| b_expr '+' b_expr
13477 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13478 			| b_expr '-' b_expr
13479 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13480 			| b_expr '*' b_expr
13481 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13482 			| b_expr '/' b_expr
13483 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13484 			| b_expr '%' b_expr
13485 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13486 			| b_expr '^' b_expr
13487 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13488 			| b_expr '<' b_expr
13489 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13490 			| b_expr '>' b_expr
13491 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13492 			| b_expr '=' b_expr
13493 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13494 			| b_expr LESS_EQUALS b_expr
13495 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13496 			| b_expr GREATER_EQUALS b_expr
13497 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13498 			| b_expr NOT_EQUALS b_expr
13499 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13500 			| b_expr qual_Op b_expr				%prec Op
13501 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13502 			| qual_Op b_expr					%prec Op
13503 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13504 			| b_expr qual_Op					%prec POSTFIXOP
13505 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13506 			| b_expr IS DISTINCT FROM b_expr		%prec IS
13507 				{
13508 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13509 				}
13510 			| b_expr IS NOT DISTINCT FROM b_expr	%prec IS
13511 				{
13512                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13513 				}
13514 			| b_expr IS OF '(' type_list ')'		%prec IS
13515 				{
13516 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13517 				}
13518 			| b_expr IS NOT OF '(' type_list ')'	%prec IS
13519 				{
13520 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13521 				}
13522 			| b_expr IS DOCUMENT_P					%prec IS
13523 				{
13524 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13525 									 list_make1($1), @2);
13526 				}
13527 			| b_expr IS NOT DOCUMENT_P				%prec IS
13528 				{
13529 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13530 												 list_make1($1), @2),
13531 									 @2);
13532 				}
13533 		;
13534 
13535 /*
13536  * Productions that can be used in both a_expr and b_expr.
13537  *
13538  * Note: productions that refer recursively to a_expr or b_expr mostly
13539  * cannot appear here.	However, it's OK to refer to a_exprs that occur
13540  * inside parentheses, such as function arguments; that cannot introduce
13541  * ambiguity to the b_expr syntax.
13542  */
13543 c_expr:		columnref								{ $$ = $1; }
13544 			| AexprConst							{ $$ = $1; }
13545 			| PARAM opt_indirection
13546 				{
13547 					ParamRef *p = makeNode(ParamRef);
13548 					p->number = $1;
13549 					p->location = @1;
13550 					if ($2)
13551 					{
13552 						A_Indirection *n = makeNode(A_Indirection);
13553 						n->arg = (Node *) p;
13554 						n->indirection = check_indirection($2, yyscanner);
13555 						$$ = (Node *) n;
13556 					}
13557 					else
13558 						$$ = (Node *) p;
13559 				}
13560 			| '(' a_expr ')' opt_indirection
13561 				{
13562 					if ($4)
13563 					{
13564 						A_Indirection *n = makeNode(A_Indirection);
13565 						n->arg = $2;
13566 						n->indirection = check_indirection($4, yyscanner);
13567 						$$ = (Node *)n;
13568 					}
13569 					else if (operator_precedence_warning)
13570 					{
13571 						/*
13572 						 * If precedence warnings are enabled, insert
13573 						 * AEXPR_PAREN nodes wrapping all explicitly
13574 						 * parenthesized subexpressions; this prevents bogus
13575 						 * warnings from being issued when the ordering has
13576 						 * been forced by parentheses.  Take care that an
13577 						 * AEXPR_PAREN node has the same exprLocation as its
13578 						 * child, so as not to cause surprising changes in
13579 						 * error cursor positioning.
13580 						 *
13581 						 * In principle we should not be relying on a GUC to
13582 						 * decide whether to insert AEXPR_PAREN nodes.
13583 						 * However, since they have no effect except to
13584 						 * suppress warnings, it's probably safe enough; and
13585 						 * we'd just as soon not waste cycles on dummy parse
13586 						 * nodes if we don't have to.
13587 						 */
13588 						$$ = (Node *) makeA_Expr(AEXPR_PAREN, NIL, $2, NULL,
13589 												 exprLocation($2));
13590 					}
13591 					else
13592 						$$ = $2;
13593 				}
13594 			| case_expr
13595 				{ $$ = $1; }
13596 			| func_expr
13597 				{ $$ = $1; }
13598 			| select_with_parens			%prec UMINUS
13599 				{
13600 					SubLink *n = makeNode(SubLink);
13601 					n->subLinkType = EXPR_SUBLINK;
13602 					n->subLinkId = 0;
13603 					n->testexpr = NULL;
13604 					n->operName = NIL;
13605 					n->subselect = $1;
13606 					n->location = @1;
13607 					$$ = (Node *)n;
13608 				}
13609 			| select_with_parens indirection
13610 				{
13611 					/*
13612 					 * Because the select_with_parens nonterminal is designed
13613 					 * to "eat" as many levels of parens as possible, the
13614 					 * '(' a_expr ')' opt_indirection production above will
13615 					 * fail to match a sub-SELECT with indirection decoration;
13616 					 * the sub-SELECT won't be regarded as an a_expr as long
13617 					 * as there are parens around it.  To support applying
13618 					 * subscripting or field selection to a sub-SELECT result,
13619 					 * we need this redundant-looking production.
13620 					 */
13621 					SubLink *n = makeNode(SubLink);
13622 					A_Indirection *a = makeNode(A_Indirection);
13623 					n->subLinkType = EXPR_SUBLINK;
13624 					n->subLinkId = 0;
13625 					n->testexpr = NULL;
13626 					n->operName = NIL;
13627 					n->subselect = $1;
13628 					n->location = @1;
13629 					a->arg = (Node *)n;
13630 					a->indirection = check_indirection($2, yyscanner);
13631 					$$ = (Node *)a;
13632 				}
13633 			| EXISTS select_with_parens
13634 				{
13635 					SubLink *n = makeNode(SubLink);
13636 					n->subLinkType = EXISTS_SUBLINK;
13637 					n->subLinkId = 0;
13638 					n->testexpr = NULL;
13639 					n->operName = NIL;
13640 					n->subselect = $2;
13641 					n->location = @1;
13642 					$$ = (Node *)n;
13643 				}
13644 			| ARRAY select_with_parens
13645 				{
13646 					SubLink *n = makeNode(SubLink);
13647 					n->subLinkType = ARRAY_SUBLINK;
13648 					n->subLinkId = 0;
13649 					n->testexpr = NULL;
13650 					n->operName = NIL;
13651 					n->subselect = $2;
13652 					n->location = @1;
13653 					$$ = (Node *)n;
13654 				}
13655 			| ARRAY array_expr
13656 				{
13657 					A_ArrayExpr *n = castNode(A_ArrayExpr, $2);
13658 					/* point outermost A_ArrayExpr to the ARRAY keyword */
13659 					n->location = @1;
13660 					$$ = (Node *)n;
13661 				}
13662 			| explicit_row
13663 				{
13664 					RowExpr *r = makeNode(RowExpr);
13665 					r->args = $1;
13666 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13667 					r->colnames = NIL;	/* to be filled in during analysis */
13668 					r->row_format = COERCE_EXPLICIT_CALL; /* abuse */
13669 					r->location = @1;
13670 					$$ = (Node *)r;
13671 				}
13672 			| implicit_row
13673 				{
13674 					RowExpr *r = makeNode(RowExpr);
13675 					r->args = $1;
13676 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13677 					r->colnames = NIL;	/* to be filled in during analysis */
13678 					r->row_format = COERCE_IMPLICIT_CAST; /* abuse */
13679 					r->location = @1;
13680 					$$ = (Node *)r;
13681 				}
13682 			| GROUPING '(' expr_list ')'
13683 			  {
13684 				  GroupingFunc *g = makeNode(GroupingFunc);
13685 				  g->args = $3;
13686 				  g->location = @1;
13687 				  $$ = (Node *)g;
13688 			  }
13689 		;
13690 
13691 func_application: func_name '(' ')'
13692 				{
13693 					$$ = (Node *) makeFuncCall($1, NIL, @1);
13694 				}
13695 			| func_name '(' func_arg_list opt_sort_clause ')'
13696 				{
13697 					FuncCall *n = makeFuncCall($1, $3, @1);
13698 					n->agg_order = $4;
13699 					$$ = (Node *)n;
13700 				}
13701 			| func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'
13702 				{
13703 					FuncCall *n = makeFuncCall($1, list_make1($4), @1);
13704 					n->func_variadic = true;
13705 					n->agg_order = $5;
13706 					$$ = (Node *)n;
13707 				}
13708 			| func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'
13709 				{
13710 					FuncCall *n = makeFuncCall($1, lappend($3, $6), @1);
13711 					n->func_variadic = true;
13712 					n->agg_order = $7;
13713 					$$ = (Node *)n;
13714 				}
13715 			| func_name '(' ALL func_arg_list opt_sort_clause ')'
13716 				{
13717 					FuncCall *n = makeFuncCall($1, $4, @1);
13718 					n->agg_order = $5;
13719 					/* Ideally we'd mark the FuncCall node to indicate
13720 					 * "must be an aggregate", but there's no provision
13721 					 * for that in FuncCall at the moment.
13722 					 */
13723 					$$ = (Node *)n;
13724 				}
13725 			| func_name '(' DISTINCT func_arg_list opt_sort_clause ')'
13726 				{
13727 					FuncCall *n = makeFuncCall($1, $4, @1);
13728 					n->agg_order = $5;
13729 					n->agg_distinct = true;
13730 					$$ = (Node *)n;
13731 				}
13732 			| func_name '(' '*' ')'
13733 				{
13734 					/*
13735 					 * We consider AGGREGATE(*) to invoke a parameterless
13736 					 * aggregate.  This does the right thing for COUNT(*),
13737 					 * and there are no other aggregates in SQL that accept
13738 					 * '*' as parameter.
13739 					 *
13740 					 * The FuncCall node is also marked agg_star = true,
13741 					 * so that later processing can detect what the argument
13742 					 * really was.
13743 					 */
13744 					FuncCall *n = makeFuncCall($1, NIL, @1);
13745 					n->agg_star = true;
13746 					$$ = (Node *)n;
13747 				}
13748 		;
13749 
13750 
13751 /*
13752  * func_expr and its cousin func_expr_windowless are split out from c_expr just
13753  * so that we have classifications for "everything that is a function call or
13754  * looks like one".  This isn't very important, but it saves us having to
13755  * document which variants are legal in places like "FROM function()" or the
13756  * backwards-compatible functional-index syntax for CREATE INDEX.
13757  * (Note that many of the special SQL functions wouldn't actually make any
13758  * sense as functional index entries, but we ignore that consideration here.)
13759  */
13760 func_expr: func_application within_group_clause filter_clause over_clause
13761 				{
13762 					FuncCall *n = (FuncCall *) $1;
13763 					/*
13764 					 * The order clause for WITHIN GROUP and the one for
13765 					 * plain-aggregate ORDER BY share a field, so we have to
13766 					 * check here that at most one is present.  We also check
13767 					 * for DISTINCT and VARIADIC here to give a better error
13768 					 * location.  Other consistency checks are deferred to
13769 					 * parse analysis.
13770 					 */
13771 					if ($2 != NIL)
13772 					{
13773 						if (n->agg_order != NIL)
13774 							ereport(ERROR,
13775 									(errcode(ERRCODE_SYNTAX_ERROR),
13776 									 errmsg("cannot use multiple ORDER BY clauses with WITHIN GROUP"),
13777 									 parser_errposition(@2)));
13778 						if (n->agg_distinct)
13779 							ereport(ERROR,
13780 									(errcode(ERRCODE_SYNTAX_ERROR),
13781 									 errmsg("cannot use DISTINCT with WITHIN GROUP"),
13782 									 parser_errposition(@2)));
13783 						if (n->func_variadic)
13784 							ereport(ERROR,
13785 									(errcode(ERRCODE_SYNTAX_ERROR),
13786 									 errmsg("cannot use VARIADIC with WITHIN GROUP"),
13787 									 parser_errposition(@2)));
13788 						n->agg_order = $2;
13789 						n->agg_within_group = true;
13790 					}
13791 					n->agg_filter = $3;
13792 					n->over = $4;
13793 					$$ = (Node *) n;
13794 				}
13795 			| func_expr_common_subexpr
13796 				{ $$ = $1; }
13797 		;
13798 
13799 /*
13800  * As func_expr but does not accept WINDOW functions directly
13801  * (but they can still be contained in arguments for functions etc).
13802  * Use this when window expressions are not allowed, where needed to
13803  * disambiguate the grammar (e.g. in CREATE INDEX).
13804  */
13805 func_expr_windowless:
13806 			func_application						{ $$ = $1; }
13807 			| func_expr_common_subexpr				{ $$ = $1; }
13808 		;
13809 
13810 /*
13811  * Special expressions that are considered to be functions.
13812  */
13813 func_expr_common_subexpr:
13814 			COLLATION FOR '(' a_expr ')'
13815 				{
13816 					$$ = (Node *) makeFuncCall(SystemFuncName("pg_collation_for"),
13817 											   list_make1($4),
13818 											   @1);
13819 				}
13820 			| CURRENT_DATE
13821 				{
13822 					$$ = makeSQLValueFunction(SVFOP_CURRENT_DATE, -1, @1);
13823 				}
13824 			| CURRENT_TIME
13825 				{
13826 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME, -1, @1);
13827 				}
13828 			| CURRENT_TIME '(' Iconst ')'
13829 				{
13830 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME_N, $3, @1);
13831 				}
13832 			| CURRENT_TIMESTAMP
13833 				{
13834 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP, -1, @1);
13835 				}
13836 			| CURRENT_TIMESTAMP '(' Iconst ')'
13837 				{
13838 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP_N, $3, @1);
13839 				}
13840 			| LOCALTIME
13841 				{
13842 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME, -1, @1);
13843 				}
13844 			| LOCALTIME '(' Iconst ')'
13845 				{
13846 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME_N, $3, @1);
13847 				}
13848 			| LOCALTIMESTAMP
13849 				{
13850 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP, -1, @1);
13851 				}
13852 			| LOCALTIMESTAMP '(' Iconst ')'
13853 				{
13854 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP_N, $3, @1);
13855 				}
13856 			| CURRENT_ROLE
13857 				{
13858 					$$ = makeSQLValueFunction(SVFOP_CURRENT_ROLE, -1, @1);
13859 				}
13860 			| CURRENT_USER
13861 				{
13862 					$$ = makeSQLValueFunction(SVFOP_CURRENT_USER, -1, @1);
13863 				}
13864 			| SESSION_USER
13865 				{
13866 					$$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1);
13867 				}
13868 			| USER
13869 				{
13870 					$$ = makeSQLValueFunction(SVFOP_USER, -1, @1);
13871 				}
13872 			| CURRENT_CATALOG
13873 				{
13874 					$$ = makeSQLValueFunction(SVFOP_CURRENT_CATALOG, -1, @1);
13875 				}
13876 			| CURRENT_SCHEMA
13877 				{
13878 					$$ = makeSQLValueFunction(SVFOP_CURRENT_SCHEMA, -1, @1);
13879 				}
13880 			| CAST '(' a_expr AS Typename ')'
13881 				{ $$ = makeTypeCast($3, $5, @1); }
13882 			| EXTRACT '(' extract_list ')'
13883 				{
13884 					$$ = (Node *) makeFuncCall(SystemFuncName("date_part"), $3, @1);
13885 				}
13886 			| OVERLAY '(' overlay_list ')'
13887 				{
13888 					/* overlay(A PLACING B FROM C FOR D) is converted to
13889 					 * overlay(A, B, C, D)
13890 					 * overlay(A PLACING B FROM C) is converted to
13891 					 * overlay(A, B, C)
13892 					 */
13893 					$$ = (Node *) makeFuncCall(SystemFuncName("overlay"), $3, @1);
13894 				}
13895 			| POSITION '(' position_list ')'
13896 				{
13897 					/* position(A in B) is converted to position(B, A) */
13898 					$$ = (Node *) makeFuncCall(SystemFuncName("position"), $3, @1);
13899 				}
13900 			| SUBSTRING '(' substr_list ')'
13901 				{
13902 					/* substring(A from B for C) is converted to
13903 					 * substring(A, B, C) - thomas 2000-11-28
13904 					 */
13905 					$$ = (Node *) makeFuncCall(SystemFuncName("substring"), $3, @1);
13906 				}
13907 			| TREAT '(' a_expr AS Typename ')'
13908 				{
13909 					/* TREAT(expr AS target) converts expr of a particular type to target,
13910 					 * which is defined to be a subtype of the original expression.
13911 					 * In SQL99, this is intended for use with structured UDTs,
13912 					 * but let's make this a generally useful form allowing stronger
13913 					 * coercions than are handled by implicit casting.
13914 					 *
13915 					 * Convert SystemTypeName() to SystemFuncName() even though
13916 					 * at the moment they result in the same thing.
13917 					 */
13918 					$$ = (Node *) makeFuncCall(SystemFuncName(((Value *)llast($5->names))->val.str),
13919 												list_make1($3),
13920 												@1);
13921 				}
13922 			| TRIM '(' BOTH trim_list ')'
13923 				{
13924 					/* various trim expressions are defined in SQL
13925 					 * - thomas 1997-07-19
13926 					 */
13927 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $4, @1);
13928 				}
13929 			| TRIM '(' LEADING trim_list ')'
13930 				{
13931 					$$ = (Node *) makeFuncCall(SystemFuncName("ltrim"), $4, @1);
13932 				}
13933 			| TRIM '(' TRAILING trim_list ')'
13934 				{
13935 					$$ = (Node *) makeFuncCall(SystemFuncName("rtrim"), $4, @1);
13936 				}
13937 			| TRIM '(' trim_list ')'
13938 				{
13939 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $3, @1);
13940 				}
13941 			| NULLIF '(' a_expr ',' a_expr ')'
13942 				{
13943 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
13944 				}
13945 			| COALESCE '(' expr_list ')'
13946 				{
13947 					CoalesceExpr *c = makeNode(CoalesceExpr);
13948 					c->args = $3;
13949 					c->location = @1;
13950 					$$ = (Node *)c;
13951 				}
13952 			| GREATEST '(' expr_list ')'
13953 				{
13954 					MinMaxExpr *v = makeNode(MinMaxExpr);
13955 					v->args = $3;
13956 					v->op = IS_GREATEST;
13957 					v->location = @1;
13958 					$$ = (Node *)v;
13959 				}
13960 			| LEAST '(' expr_list ')'
13961 				{
13962 					MinMaxExpr *v = makeNode(MinMaxExpr);
13963 					v->args = $3;
13964 					v->op = IS_LEAST;
13965 					v->location = @1;
13966 					$$ = (Node *)v;
13967 				}
13968 			| XMLCONCAT '(' expr_list ')'
13969 				{
13970 					$$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
13971 				}
13972 			| XMLELEMENT '(' NAME_P ColLabel ')'
13973 				{
13974 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
13975 				}
13976 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
13977 				{
13978 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
13979 				}
13980 			| XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
13981 				{
13982 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
13983 				}
13984 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
13985 				{
13986 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
13987 				}
13988 			| XMLEXISTS '(' c_expr xmlexists_argument ')'
13989 				{
13990 					/* xmlexists(A PASSING [BY REF] B [BY REF]) is
13991 					 * converted to xmlexists(A, B)*/
13992 					$$ = (Node *) makeFuncCall(SystemFuncName("xmlexists"), list_make2($3, $4), @1);
13993 				}
13994 			| XMLFOREST '(' xml_attribute_list ')'
13995 				{
13996 					$$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
13997 				}
13998 			| XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
13999 				{
14000 					XmlExpr *x = (XmlExpr *)
14001 						makeXmlExpr(IS_XMLPARSE, NULL, NIL,
14002 									list_make2($4, makeBoolAConst($5, -1)),
14003 									@1);
14004 					x->xmloption = $3;
14005 					$$ = (Node *)x;
14006 				}
14007 			| XMLPI '(' NAME_P ColLabel ')'
14008 				{
14009 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
14010 				}
14011 			| XMLPI '(' NAME_P ColLabel ',' a_expr ')'
14012 				{
14013 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
14014 				}
14015 			| XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
14016 				{
14017 					$$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
14018 									 list_make3($3, $5, $6), @1);
14019 				}
14020 			| XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
14021 				{
14022 					XmlSerialize *n = makeNode(XmlSerialize);
14023 					n->xmloption = $3;
14024 					n->expr = $4;
14025 					n->typeName = $6;
14026 					n->location = @1;
14027 					$$ = (Node *)n;
14028 				}
14029 		;
14030 
14031 /*
14032  * SQL/XML support
14033  */
14034 xml_root_version: VERSION_P a_expr
14035 				{ $$ = $2; }
14036 			| VERSION_P NO VALUE_P
14037 				{ $$ = makeNullAConst(-1); }
14038 		;
14039 
14040 opt_xml_root_standalone: ',' STANDALONE_P YES_P
14041 				{ $$ = makeIntConst(XML_STANDALONE_YES, -1); }
14042 			| ',' STANDALONE_P NO
14043 				{ $$ = makeIntConst(XML_STANDALONE_NO, -1); }
14044 			| ',' STANDALONE_P NO VALUE_P
14045 				{ $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
14046 			| /*EMPTY*/
14047 				{ $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
14048 		;
14049 
14050 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'	{ $$ = $3; }
14051 		;
14052 
14053 xml_attribute_list:	xml_attribute_el					{ $$ = list_make1($1); }
14054 			| xml_attribute_list ',' xml_attribute_el	{ $$ = lappend($1, $3); }
14055 		;
14056 
14057 xml_attribute_el: a_expr AS ColLabel
14058 				{
14059 					$$ = makeNode(ResTarget);
14060 					$$->name = $3;
14061 					$$->indirection = NIL;
14062 					$$->val = (Node *) $1;
14063 					$$->location = @1;
14064 				}
14065 			| a_expr
14066 				{
14067 					$$ = makeNode(ResTarget);
14068 					$$->name = NULL;
14069 					$$->indirection = NIL;
14070 					$$->val = (Node *) $1;
14071 					$$->location = @1;
14072 				}
14073 		;
14074 
14075 document_or_content: DOCUMENT_P						{ $$ = XMLOPTION_DOCUMENT; }
14076 			| CONTENT_P								{ $$ = XMLOPTION_CONTENT; }
14077 		;
14078 
14079 xml_whitespace_option: PRESERVE WHITESPACE_P		{ $$ = true; }
14080 			| STRIP_P WHITESPACE_P					{ $$ = false; }
14081 			| /*EMPTY*/								{ $$ = false; }
14082 		;
14083 
14084 /* We allow several variants for SQL and other compatibility. */
14085 xmlexists_argument:
14086 			PASSING c_expr
14087 				{
14088 					$$ = $2;
14089 				}
14090 			| PASSING c_expr xml_passing_mech
14091 				{
14092 					$$ = $2;
14093 				}
14094 			| PASSING xml_passing_mech c_expr
14095 				{
14096 					$$ = $3;
14097 				}
14098 			| PASSING xml_passing_mech c_expr xml_passing_mech
14099 				{
14100 					$$ = $3;
14101 				}
14102 		;
14103 
14104 xml_passing_mech:
14105 			BY REF
14106 			| BY VALUE_P
14107 		;
14108 
14109 
14110 /*
14111  * Aggregate decoration clauses
14112  */
14113 within_group_clause:
14114 			WITHIN GROUP_P '(' sort_clause ')'		{ $$ = $4; }
14115 			| /*EMPTY*/								{ $$ = NIL; }
14116 		;
14117 
14118 filter_clause:
14119 			FILTER '(' WHERE a_expr ')'				{ $$ = $4; }
14120 			| /*EMPTY*/								{ $$ = NULL; }
14121 		;
14122 
14123 
14124 /*
14125  * Window Definitions
14126  */
14127 window_clause:
14128 			WINDOW window_definition_list			{ $$ = $2; }
14129 			| /*EMPTY*/								{ $$ = NIL; }
14130 		;
14131 
14132 window_definition_list:
14133 			window_definition						{ $$ = list_make1($1); }
14134 			| window_definition_list ',' window_definition
14135 													{ $$ = lappend($1, $3); }
14136 		;
14137 
14138 window_definition:
14139 			ColId AS window_specification
14140 				{
14141 					WindowDef *n = $3;
14142 					n->name = $1;
14143 					$$ = n;
14144 				}
14145 		;
14146 
14147 over_clause: OVER window_specification
14148 				{ $$ = $2; }
14149 			| OVER ColId
14150 				{
14151 					WindowDef *n = makeNode(WindowDef);
14152 					n->name = $2;
14153 					n->refname = NULL;
14154 					n->partitionClause = NIL;
14155 					n->orderClause = NIL;
14156 					n->frameOptions = FRAMEOPTION_DEFAULTS;
14157 					n->startOffset = NULL;
14158 					n->endOffset = NULL;
14159 					n->location = @2;
14160 					$$ = n;
14161 				}
14162 			| /*EMPTY*/
14163 				{ $$ = NULL; }
14164 		;
14165 
14166 window_specification: '(' opt_existing_window_name opt_partition_clause
14167 						opt_sort_clause opt_frame_clause ')'
14168 				{
14169 					WindowDef *n = makeNode(WindowDef);
14170 					n->name = NULL;
14171 					n->refname = $2;
14172 					n->partitionClause = $3;
14173 					n->orderClause = $4;
14174 					/* copy relevant fields of opt_frame_clause */
14175 					n->frameOptions = $5->frameOptions;
14176 					n->startOffset = $5->startOffset;
14177 					n->endOffset = $5->endOffset;
14178 					n->location = @1;
14179 					$$ = n;
14180 				}
14181 		;
14182 
14183 /*
14184  * If we see PARTITION, RANGE, ROWS or GROUPS as the first token after the '('
14185  * of a window_specification, we want the assumption to be that there is
14186  * no existing_window_name; but those keywords are unreserved and so could
14187  * be ColIds.  We fix this by making them have the same precedence as IDENT
14188  * and giving the empty production here a slightly higher precedence, so
14189  * that the shift/reduce conflict is resolved in favor of reducing the rule.
14190  * These keywords are thus precluded from being an existing_window_name but
14191  * are not reserved for any other purpose.
14192  */
14193 opt_existing_window_name: ColId						{ $$ = $1; }
14194 			| /*EMPTY*/				%prec Op		{ $$ = NULL; }
14195 		;
14196 
14197 opt_partition_clause: PARTITION BY expr_list		{ $$ = $3; }
14198 			| /*EMPTY*/								{ $$ = NIL; }
14199 		;
14200 
14201 /*
14202  * For frame clauses, we return a WindowDef, but only some fields are used:
14203  * frameOptions, startOffset, and endOffset.
14204  */
14205 opt_frame_clause:
14206 			RANGE frame_extent opt_window_exclusion_clause
14207 				{
14208 					WindowDef *n = $2;
14209 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
14210 					n->frameOptions |= $3;
14211 					$$ = n;
14212 				}
14213 			| ROWS frame_extent opt_window_exclusion_clause
14214 				{
14215 					WindowDef *n = $2;
14216 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
14217 					n->frameOptions |= $3;
14218 					$$ = n;
14219 				}
14220 			| GROUPS frame_extent opt_window_exclusion_clause
14221 				{
14222 					WindowDef *n = $2;
14223 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_GROUPS;
14224 					n->frameOptions |= $3;
14225 					$$ = n;
14226 				}
14227 			| /*EMPTY*/
14228 				{
14229 					WindowDef *n = makeNode(WindowDef);
14230 					n->frameOptions = FRAMEOPTION_DEFAULTS;
14231 					n->startOffset = NULL;
14232 					n->endOffset = NULL;
14233 					$$ = n;
14234 				}
14235 		;
14236 
14237 frame_extent: frame_bound
14238 				{
14239 					WindowDef *n = $1;
14240 					/* reject invalid cases */
14241 					if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14242 						ereport(ERROR,
14243 								(errcode(ERRCODE_WINDOWING_ERROR),
14244 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14245 								 parser_errposition(@1)));
14246 					if (n->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
14247 						ereport(ERROR,
14248 								(errcode(ERRCODE_WINDOWING_ERROR),
14249 								 errmsg("frame starting from following row cannot end with current row"),
14250 								 parser_errposition(@1)));
14251 					n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
14252 					$$ = n;
14253 				}
14254 			| BETWEEN frame_bound AND frame_bound
14255 				{
14256 					WindowDef *n1 = $2;
14257 					WindowDef *n2 = $4;
14258 					/* form merged options */
14259 					int		frameOptions = n1->frameOptions;
14260 					/* shift converts START_ options to END_ options */
14261 					frameOptions |= n2->frameOptions << 1;
14262 					frameOptions |= FRAMEOPTION_BETWEEN;
14263 					/* reject invalid cases */
14264 					if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14265 						ereport(ERROR,
14266 								(errcode(ERRCODE_WINDOWING_ERROR),
14267 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14268 								 parser_errposition(@2)));
14269 					if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
14270 						ereport(ERROR,
14271 								(errcode(ERRCODE_WINDOWING_ERROR),
14272 								 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
14273 								 parser_errposition(@4)));
14274 					if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
14275 						(frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING))
14276 						ereport(ERROR,
14277 								(errcode(ERRCODE_WINDOWING_ERROR),
14278 								 errmsg("frame starting from current row cannot have preceding rows"),
14279 								 parser_errposition(@4)));
14280 					if ((frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING) &&
14281 						(frameOptions & (FRAMEOPTION_END_OFFSET_PRECEDING |
14282 										 FRAMEOPTION_END_CURRENT_ROW)))
14283 						ereport(ERROR,
14284 								(errcode(ERRCODE_WINDOWING_ERROR),
14285 								 errmsg("frame starting from following row cannot have preceding rows"),
14286 								 parser_errposition(@4)));
14287 					n1->frameOptions = frameOptions;
14288 					n1->endOffset = n2->startOffset;
14289 					$$ = n1;
14290 				}
14291 		;
14292 
14293 /*
14294  * This is used for both frame start and frame end, with output set up on
14295  * the assumption it's frame start; the frame_extent productions must reject
14296  * invalid cases.
14297  */
14298 frame_bound:
14299 			UNBOUNDED PRECEDING
14300 				{
14301 					WindowDef *n = makeNode(WindowDef);
14302 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
14303 					n->startOffset = NULL;
14304 					n->endOffset = NULL;
14305 					$$ = n;
14306 				}
14307 			| UNBOUNDED FOLLOWING
14308 				{
14309 					WindowDef *n = makeNode(WindowDef);
14310 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
14311 					n->startOffset = NULL;
14312 					n->endOffset = NULL;
14313 					$$ = n;
14314 				}
14315 			| CURRENT_P ROW
14316 				{
14317 					WindowDef *n = makeNode(WindowDef);
14318 					n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
14319 					n->startOffset = NULL;
14320 					n->endOffset = NULL;
14321 					$$ = n;
14322 				}
14323 			| a_expr PRECEDING
14324 				{
14325 					WindowDef *n = makeNode(WindowDef);
14326 					n->frameOptions = FRAMEOPTION_START_OFFSET_PRECEDING;
14327 					n->startOffset = $1;
14328 					n->endOffset = NULL;
14329 					$$ = n;
14330 				}
14331 			| a_expr FOLLOWING
14332 				{
14333 					WindowDef *n = makeNode(WindowDef);
14334 					n->frameOptions = FRAMEOPTION_START_OFFSET_FOLLOWING;
14335 					n->startOffset = $1;
14336 					n->endOffset = NULL;
14337 					$$ = n;
14338 				}
14339 		;
14340 
14341 opt_window_exclusion_clause:
14342 			EXCLUDE CURRENT_P ROW	{ $$ = FRAMEOPTION_EXCLUDE_CURRENT_ROW; }
14343 			| EXCLUDE GROUP_P		{ $$ = FRAMEOPTION_EXCLUDE_GROUP; }
14344 			| EXCLUDE TIES			{ $$ = FRAMEOPTION_EXCLUDE_TIES; }
14345 			| EXCLUDE NO OTHERS		{ $$ = 0; }
14346 			| /*EMPTY*/				{ $$ = 0; }
14347 		;
14348 
14349 
14350 /*
14351  * Supporting nonterminals for expressions.
14352  */
14353 
14354 /* Explicit row production.
14355  *
14356  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
14357  * without conflicting with the parenthesized a_expr production.  Without the
14358  * ROW keyword, there must be more than one a_expr inside the parens.
14359  */
14360 row:		ROW '(' expr_list ')'					{ $$ = $3; }
14361 			| ROW '(' ')'							{ $$ = NIL; }
14362 			| '(' expr_list ',' a_expr ')'			{ $$ = lappend($2, $4); }
14363 		;
14364 
14365 explicit_row:	ROW '(' expr_list ')'				{ $$ = $3; }
14366 			| ROW '(' ')'							{ $$ = NIL; }
14367 		;
14368 
14369 implicit_row:	'(' expr_list ',' a_expr ')'		{ $$ = lappend($2, $4); }
14370 		;
14371 
14372 sub_type:	ANY										{ $$ = ANY_SUBLINK; }
14373 			| SOME									{ $$ = ANY_SUBLINK; }
14374 			| ALL									{ $$ = ALL_SUBLINK; }
14375 		;
14376 
14377 all_Op:		Op										{ $$ = $1; }
14378 			| MathOp								{ $$ = $1; }
14379 		;
14380 
14381 MathOp:		 '+'									{ $$ = "+"; }
14382 			| '-'									{ $$ = "-"; }
14383 			| '*'									{ $$ = "*"; }
14384 			| '/'									{ $$ = "/"; }
14385 			| '%'									{ $$ = "%"; }
14386 			| '^'									{ $$ = "^"; }
14387 			| '<'									{ $$ = "<"; }
14388 			| '>'									{ $$ = ">"; }
14389 			| '='									{ $$ = "="; }
14390 			| LESS_EQUALS							{ $$ = "<="; }
14391 			| GREATER_EQUALS						{ $$ = ">="; }
14392 			| NOT_EQUALS							{ $$ = "<>"; }
14393 		;
14394 
14395 qual_Op:	Op
14396 					{ $$ = list_make1(makeString($1)); }
14397 			| OPERATOR '(' any_operator ')'
14398 					{ $$ = $3; }
14399 		;
14400 
14401 qual_all_Op:
14402 			all_Op
14403 					{ $$ = list_make1(makeString($1)); }
14404 			| OPERATOR '(' any_operator ')'
14405 					{ $$ = $3; }
14406 		;
14407 
14408 subquery_Op:
14409 			all_Op
14410 					{ $$ = list_make1(makeString($1)); }
14411 			| OPERATOR '(' any_operator ')'
14412 					{ $$ = $3; }
14413 			| LIKE
14414 					{ $$ = list_make1(makeString("~~")); }
14415 			| NOT_LA LIKE
14416 					{ $$ = list_make1(makeString("!~~")); }
14417 			| ILIKE
14418 					{ $$ = list_make1(makeString("~~*")); }
14419 			| NOT_LA ILIKE
14420 					{ $$ = list_make1(makeString("!~~*")); }
14421 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
14422  * the regular expression is preprocessed by a function (similar_escape),
14423  * and the ~ operator for posix regular expressions is used.
14424  *        x SIMILAR TO y     ->    x ~ similar_escape(y)
14425  * this transformation is made on the fly by the parser upwards.
14426  * however the SubLink structure which handles any/some/all stuff
14427  * is not ready for such a thing.
14428  */
14429 			;
14430 
14431 expr_list:	a_expr
14432 				{
14433 					$$ = list_make1($1);
14434 				}
14435 			| expr_list ',' a_expr
14436 				{
14437 					$$ = lappend($1, $3);
14438 				}
14439 		;
14440 
14441 /* function arguments can have names */
14442 func_arg_list:  func_arg_expr
14443 				{
14444 					$$ = list_make1($1);
14445 				}
14446 			| func_arg_list ',' func_arg_expr
14447 				{
14448 					$$ = lappend($1, $3);
14449 				}
14450 		;
14451 
14452 func_arg_expr:  a_expr
14453 				{
14454 					$$ = $1;
14455 				}
14456 			| param_name COLON_EQUALS a_expr
14457 				{
14458 					NamedArgExpr *na = makeNode(NamedArgExpr);
14459 					na->name = $1;
14460 					na->arg = (Expr *) $3;
14461 					na->argnumber = -1;		/* until determined */
14462 					na->location = @1;
14463 					$$ = (Node *) na;
14464 				}
14465 			| param_name EQUALS_GREATER a_expr
14466 				{
14467 					NamedArgExpr *na = makeNode(NamedArgExpr);
14468 					na->name = $1;
14469 					na->arg = (Expr *) $3;
14470 					na->argnumber = -1;		/* until determined */
14471 					na->location = @1;
14472 					$$ = (Node *) na;
14473 				}
14474 		;
14475 
14476 type_list:	Typename								{ $$ = list_make1($1); }
14477 			| type_list ',' Typename				{ $$ = lappend($1, $3); }
14478 		;
14479 
14480 array_expr: '[' expr_list ']'
14481 				{
14482 					$$ = makeAArrayExpr($2, @1);
14483 				}
14484 			| '[' array_expr_list ']'
14485 				{
14486 					$$ = makeAArrayExpr($2, @1);
14487 				}
14488 			| '[' ']'
14489 				{
14490 					$$ = makeAArrayExpr(NIL, @1);
14491 				}
14492 		;
14493 
14494 array_expr_list: array_expr							{ $$ = list_make1($1); }
14495 			| array_expr_list ',' array_expr		{ $$ = lappend($1, $3); }
14496 		;
14497 
14498 
14499 extract_list:
14500 			extract_arg FROM a_expr
14501 				{
14502 					$$ = list_make2(makeStringConst($1, @1), $3);
14503 				}
14504 			| /*EMPTY*/								{ $$ = NIL; }
14505 		;
14506 
14507 /* Allow delimited string Sconst in extract_arg as an SQL extension.
14508  * - thomas 2001-04-12
14509  */
14510 extract_arg:
14511 			IDENT									{ $$ = $1; }
14512 			| YEAR_P								{ $$ = "year"; }
14513 			| MONTH_P								{ $$ = "month"; }
14514 			| DAY_P									{ $$ = "day"; }
14515 			| HOUR_P								{ $$ = "hour"; }
14516 			| MINUTE_P								{ $$ = "minute"; }
14517 			| SECOND_P								{ $$ = "second"; }
14518 			| Sconst								{ $$ = $1; }
14519 		;
14520 
14521 /* OVERLAY() arguments
14522  * SQL99 defines the OVERLAY() function:
14523  * o overlay(text placing text from int for int)
14524  * o overlay(text placing text from int)
14525  * and similarly for binary strings
14526  */
14527 overlay_list:
14528 			a_expr overlay_placing substr_from substr_for
14529 				{
14530 					$$ = list_make4($1, $2, $3, $4);
14531 				}
14532 			| a_expr overlay_placing substr_from
14533 				{
14534 					$$ = list_make3($1, $2, $3);
14535 				}
14536 		;
14537 
14538 overlay_placing:
14539 			PLACING a_expr
14540 				{ $$ = $2; }
14541 		;
14542 
14543 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
14544 
14545 position_list:
14546 			b_expr IN_P b_expr						{ $$ = list_make2($3, $1); }
14547 			| /*EMPTY*/								{ $$ = NIL; }
14548 		;
14549 
14550 /* SUBSTRING() arguments
14551  * SQL9x defines a specific syntax for arguments to SUBSTRING():
14552  * o substring(text from int for int)
14553  * o substring(text from int) get entire string from starting point "int"
14554  * o substring(text for int) get first "int" characters of string
14555  * o substring(text from pattern) get entire string matching pattern
14556  * o substring(text from pattern for escape) same with specified escape char
14557  * We also want to support generic substring functions which accept
14558  * the usual generic list of arguments. So we will accept both styles
14559  * here, and convert the SQL9x style to the generic list for further
14560  * processing. - thomas 2000-11-28
14561  */
14562 substr_list:
14563 			a_expr substr_from substr_for
14564 				{
14565 					$$ = list_make3($1, $2, $3);
14566 				}
14567 			| a_expr substr_for substr_from
14568 				{
14569 					/* not legal per SQL99, but might as well allow it */
14570 					$$ = list_make3($1, $3, $2);
14571 				}
14572 			| a_expr substr_from
14573 				{
14574 					$$ = list_make2($1, $2);
14575 				}
14576 			| a_expr substr_for
14577 				{
14578 					/*
14579 					 * Since there are no cases where this syntax allows
14580 					 * a textual FOR value, we forcibly cast the argument
14581 					 * to int4.  The possible matches in pg_proc are
14582 					 * substring(text,int4) and substring(text,text),
14583 					 * and we don't want the parser to choose the latter,
14584 					 * which it is likely to do if the second argument
14585 					 * is unknown or doesn't have an implicit cast to int4.
14586 					 */
14587 					$$ = list_make3($1, makeIntConst(1, -1),
14588 									makeTypeCast($2,
14589 												 SystemTypeName("int4"), -1));
14590 				}
14591 			| expr_list
14592 				{
14593 					$$ = $1;
14594 				}
14595 			| /*EMPTY*/
14596 				{ $$ = NIL; }
14597 		;
14598 
14599 substr_from:
14600 			FROM a_expr								{ $$ = $2; }
14601 		;
14602 
14603 substr_for: FOR a_expr								{ $$ = $2; }
14604 		;
14605 
14606 trim_list:	a_expr FROM expr_list					{ $$ = lappend($3, $1); }
14607 			| FROM expr_list						{ $$ = $2; }
14608 			| expr_list								{ $$ = $1; }
14609 		;
14610 
14611 in_expr:	select_with_parens
14612 				{
14613 					SubLink *n = makeNode(SubLink);
14614 					n->subselect = $1;
14615 					/* other fields will be filled later */
14616 					$$ = (Node *)n;
14617 				}
14618 			| '(' expr_list ')'						{ $$ = (Node *)$2; }
14619 		;
14620 
14621 /*
14622  * Define SQL-style CASE clause.
14623  * - Full specification
14624  *	CASE WHEN a = b THEN c ... ELSE d END
14625  * - Implicit argument
14626  *	CASE a WHEN b THEN c ... ELSE d END
14627  */
14628 case_expr:	CASE case_arg when_clause_list case_default END_P
14629 				{
14630 					CaseExpr *c = makeNode(CaseExpr);
14631 					c->casetype = InvalidOid; /* not analyzed yet */
14632 					c->arg = (Expr *) $2;
14633 					c->args = $3;
14634 					c->defresult = (Expr *) $4;
14635 					c->location = @1;
14636 					$$ = (Node *)c;
14637 				}
14638 		;
14639 
14640 when_clause_list:
14641 			/* There must be at least one */
14642 			when_clause								{ $$ = list_make1($1); }
14643 			| when_clause_list when_clause			{ $$ = lappend($1, $2); }
14644 		;
14645 
14646 when_clause:
14647 			WHEN a_expr THEN a_expr
14648 				{
14649 					CaseWhen *w = makeNode(CaseWhen);
14650 					w->expr = (Expr *) $2;
14651 					w->result = (Expr *) $4;
14652 					w->location = @1;
14653 					$$ = (Node *)w;
14654 				}
14655 		;
14656 
14657 case_default:
14658 			ELSE a_expr								{ $$ = $2; }
14659 			| /*EMPTY*/								{ $$ = NULL; }
14660 		;
14661 
14662 case_arg:	a_expr									{ $$ = $1; }
14663 			| /*EMPTY*/								{ $$ = NULL; }
14664 		;
14665 
14666 columnref:	ColId
14667 				{
14668 					$$ = makeColumnRef($1, NIL, @1, yyscanner);
14669 				}
14670 			| ColId indirection
14671 				{
14672 					$$ = makeColumnRef($1, $2, @1, yyscanner);
14673 				}
14674 		;
14675 
14676 indirection_el:
14677 			'.' attr_name
14678 				{
14679 					$$ = (Node *) makeString($2);
14680 				}
14681 			| '.' '*'
14682 				{
14683 					$$ = (Node *) makeNode(A_Star);
14684 				}
14685 			| '[' a_expr ']'
14686 				{
14687 					A_Indices *ai = makeNode(A_Indices);
14688 					ai->is_slice = false;
14689 					ai->lidx = NULL;
14690 					ai->uidx = $2;
14691 					$$ = (Node *) ai;
14692 				}
14693 			| '[' opt_slice_bound ':' opt_slice_bound ']'
14694 				{
14695 					A_Indices *ai = makeNode(A_Indices);
14696 					ai->is_slice = true;
14697 					ai->lidx = $2;
14698 					ai->uidx = $4;
14699 					$$ = (Node *) ai;
14700 				}
14701 		;
14702 
14703 opt_slice_bound:
14704 			a_expr									{ $$ = $1; }
14705 			| /*EMPTY*/								{ $$ = NULL; }
14706 		;
14707 
14708 indirection:
14709 			indirection_el							{ $$ = list_make1($1); }
14710 			| indirection indirection_el			{ $$ = lappend($1, $2); }
14711 		;
14712 
14713 opt_indirection:
14714 			/*EMPTY*/								{ $$ = NIL; }
14715 			| opt_indirection indirection_el		{ $$ = lappend($1, $2); }
14716 		;
14717 
14718 opt_asymmetric: ASYMMETRIC
14719 			| /*EMPTY*/
14720 		;
14721 
14722 
14723 /*****************************************************************************
14724  *
14725  *	target list for SELECT
14726  *
14727  *****************************************************************************/
14728 
14729 opt_target_list: target_list						{ $$ = $1; }
14730 			| /* EMPTY */							{ $$ = NIL; }
14731 		;
14732 
14733 target_list:
14734 			target_el								{ $$ = list_make1($1); }
14735 			| target_list ',' target_el				{ $$ = lappend($1, $3); }
14736 		;
14737 
14738 target_el:	a_expr AS ColLabel
14739 				{
14740 					$$ = makeNode(ResTarget);
14741 					$$->name = $3;
14742 					$$->indirection = NIL;
14743 					$$->val = (Node *)$1;
14744 					$$->location = @1;
14745 				}
14746 			/*
14747 			 * We support omitting AS only for column labels that aren't
14748 			 * any known keyword.  There is an ambiguity against postfix
14749 			 * operators: is "a ! b" an infix expression, or a postfix
14750 			 * expression and a column label?  We prefer to resolve this
14751 			 * as an infix expression, which we accomplish by assigning
14752 			 * IDENT a precedence higher than POSTFIXOP.
14753 			 */
14754 			| a_expr IDENT
14755 				{
14756 					$$ = makeNode(ResTarget);
14757 					$$->name = $2;
14758 					$$->indirection = NIL;
14759 					$$->val = (Node *)$1;
14760 					$$->location = @1;
14761 				}
14762 			| a_expr
14763 				{
14764 					$$ = makeNode(ResTarget);
14765 					$$->name = NULL;
14766 					$$->indirection = NIL;
14767 					$$->val = (Node *)$1;
14768 					$$->location = @1;
14769 				}
14770 			| '*'
14771 				{
14772 					ColumnRef *n = makeNode(ColumnRef);
14773 					n->fields = list_make1(makeNode(A_Star));
14774 					n->location = @1;
14775 
14776 					$$ = makeNode(ResTarget);
14777 					$$->name = NULL;
14778 					$$->indirection = NIL;
14779 					$$->val = (Node *)n;
14780 					$$->location = @1;
14781 				}
14782 		;
14783 
14784 
14785 /*****************************************************************************
14786  *
14787  *	Names and constants
14788  *
14789  *****************************************************************************/
14790 
14791 qualified_name_list:
14792 			qualified_name							{ $$ = list_make1($1); }
14793 			| qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
14794 		;
14795 
14796 /*
14797  * The production for a qualified relation name has to exactly match the
14798  * production for a qualified func_name, because in a FROM clause we cannot
14799  * tell which we are parsing until we see what comes after it ('(' for a
14800  * func_name, something else for a relation). Therefore we allow 'indirection'
14801  * which may contain subscripts, and reject that case in the C code.
14802  */
14803 qualified_name:
14804 			ColId
14805 				{
14806 					$$ = makeRangeVar(NULL, $1, @1);
14807 				}
14808 			| ColId indirection
14809 				{
14810 					check_qualified_name($2, yyscanner);
14811 					$$ = makeRangeVar(NULL, NULL, @1);
14812 					switch (list_length($2))
14813 					{
14814 						case 1:
14815 							$$->catalogname = NULL;
14816 							$$->schemaname = $1;
14817 							$$->relname = strVal(linitial($2));
14818 							break;
14819 						case 2:
14820 							$$->catalogname = $1;
14821 							$$->schemaname = strVal(linitial($2));
14822 							$$->relname = strVal(lsecond($2));
14823 							break;
14824 						default:
14825 							ereport(ERROR,
14826 									(errcode(ERRCODE_SYNTAX_ERROR),
14827 									 errmsg("improper qualified name (too many dotted names): %s",
14828 											NameListToString(lcons(makeString($1), $2))),
14829 									 parser_errposition(@1)));
14830 							break;
14831 					}
14832 				}
14833 		;
14834 
14835 name_list:	name
14836 					{ $$ = list_make1(makeString($1)); }
14837 			| name_list ',' name
14838 					{ $$ = lappend($1, makeString($3)); }
14839 		;
14840 
14841 
14842 name:		ColId									{ $$ = $1; };
14843 
14844 database_name:
14845 			ColId									{ $$ = $1; };
14846 
14847 access_method:
14848 			ColId									{ $$ = $1; };
14849 
14850 attr_name:	ColLabel								{ $$ = $1; };
14851 
14852 index_name: ColId									{ $$ = $1; };
14853 
14854 file_name:	Sconst									{ $$ = $1; };
14855 
14856 /*
14857  * The production for a qualified func_name has to exactly match the
14858  * production for a qualified columnref, because we cannot tell which we
14859  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
14860  * anything else for a columnref).  Therefore we allow 'indirection' which
14861  * may contain subscripts, and reject that case in the C code.  (If we
14862  * ever implement SQL99-like methods, such syntax may actually become legal!)
14863  */
14864 func_name:	type_function_name
14865 					{ $$ = list_make1(makeString($1)); }
14866 			| ColId indirection
14867 					{
14868 						$$ = check_func_name(lcons(makeString($1), $2),
14869 											 yyscanner);
14870 					}
14871 		;
14872 
14873 
14874 /*
14875  * Constants
14876  */
14877 AexprConst: Iconst
14878 				{
14879 					$$ = makeIntConst($1, @1);
14880 				}
14881 			| FCONST
14882 				{
14883 					$$ = makeFloatConst($1, @1);
14884 				}
14885 			| Sconst
14886 				{
14887 					$$ = makeStringConst($1, @1);
14888 				}
14889 			| BCONST
14890 				{
14891 					$$ = makeBitStringConst($1, @1);
14892 				}
14893 			| XCONST
14894 				{
14895 					/* This is a bit constant per SQL99:
14896 					 * Without Feature F511, "BIT data type",
14897 					 * a <general literal> shall not be a
14898 					 * <bit string literal> or a <hex string literal>.
14899 					 */
14900 					$$ = makeBitStringConst($1, @1);
14901 				}
14902 			| func_name Sconst
14903 				{
14904 					/* generic type 'literal' syntax */
14905 					TypeName *t = makeTypeNameFromNameList($1);
14906 					t->location = @1;
14907 					$$ = makeStringConstCast($2, @2, t);
14908 				}
14909 			| func_name '(' func_arg_list opt_sort_clause ')' Sconst
14910 				{
14911 					/* generic syntax with a type modifier */
14912 					TypeName *t = makeTypeNameFromNameList($1);
14913 					ListCell *lc;
14914 
14915 					/*
14916 					 * We must use func_arg_list and opt_sort_clause in the
14917 					 * production to avoid reduce/reduce conflicts, but we
14918 					 * don't actually wish to allow NamedArgExpr in this
14919 					 * context, nor ORDER BY.
14920 					 */
foreach(lc,$3)14921 					foreach(lc, $3)
14922 					{
14923 						NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
14924 
14925 						if (IsA(arg, NamedArgExpr))
14926 							ereport(ERROR,
14927 									(errcode(ERRCODE_SYNTAX_ERROR),
14928 									 errmsg("type modifier cannot have parameter name"),
14929 									 parser_errposition(arg->location)));
14930 					}
14931 					if ($4 != NIL)
14932 							ereport(ERROR,
14933 									(errcode(ERRCODE_SYNTAX_ERROR),
14934 									 errmsg("type modifier cannot have ORDER BY"),
14935 									 parser_errposition(@4)));
14936 
14937 					t->typmods = $3;
14938 					t->location = @1;
14939 					$$ = makeStringConstCast($6, @6, t);
14940 				}
14941 			| ConstTypename Sconst
14942 				{
14943 					$$ = makeStringConstCast($2, @2, $1);
14944 				}
14945 			| ConstInterval Sconst opt_interval
14946 				{
14947 					TypeName *t = $1;
14948 					t->typmods = $3;
14949 					$$ = makeStringConstCast($2, @2, t);
14950 				}
14951 			| ConstInterval '(' Iconst ')' Sconst
14952 				{
14953 					TypeName *t = $1;
14954 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
14955 											makeIntConst($3, @3));
14956 					$$ = makeStringConstCast($5, @5, t);
14957 				}
14958 			| TRUE_P
14959 				{
14960 					$$ = makeBoolAConst(true, @1);
14961 				}
14962 			| FALSE_P
14963 				{
14964 					$$ = makeBoolAConst(false, @1);
14965 				}
14966 			| NULL_P
14967 				{
14968 					$$ = makeNullAConst(@1);
14969 				}
14970 		;
14971 
14972 Iconst:		ICONST									{ $$ = $1; };
14973 Sconst:		SCONST									{ $$ = $1; };
14974 
14975 SignedIconst: Iconst								{ $$ = $1; }
14976 			| '+' Iconst							{ $$ = + $2; }
14977 			| '-' Iconst							{ $$ = - $2; }
14978 		;
14979 
14980 /* Role specifications */
14981 RoleId:		RoleSpec
14982 				{
14983 					RoleSpec *spc = (RoleSpec *) $1;
14984 					switch (spc->roletype)
14985 					{
14986 						case ROLESPEC_CSTRING:
14987 							$$ = spc->rolename;
14988 							break;
14989 						case ROLESPEC_PUBLIC:
14990 							ereport(ERROR,
14991 									(errcode(ERRCODE_RESERVED_NAME),
14992 									 errmsg("role name \"%s\" is reserved",
14993 											"public"),
14994 									 parser_errposition(@1)));
14995 							break;
14996 						case ROLESPEC_SESSION_USER:
14997 							ereport(ERROR,
14998 									(errcode(ERRCODE_RESERVED_NAME),
14999 									 errmsg("%s cannot be used as a role name here",
15000 											"SESSION_USER"),
15001 									 parser_errposition(@1)));
15002 							break;
15003 						case ROLESPEC_CURRENT_USER:
15004 							ereport(ERROR,
15005 									(errcode(ERRCODE_RESERVED_NAME),
15006 									 errmsg("%s cannot be used as a role name here",
15007 											"CURRENT_USER"),
15008 									 parser_errposition(@1)));
15009 							break;
15010 					}
15011 				}
15012 			;
15013 
15014 RoleSpec:	NonReservedWord
15015 					{
15016 						/*
15017 						 * "public" and "none" are not keywords, but they must
15018 						 * be treated specially here.
15019 						 */
15020 						RoleSpec *n;
15021 						if (strcmp($1, "public") == 0)
15022 						{
15023 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_PUBLIC, @1);
15024 							n->roletype = ROLESPEC_PUBLIC;
15025 						}
15026 						else if (strcmp($1, "none") == 0)
15027 						{
15028 							ereport(ERROR,
15029 									(errcode(ERRCODE_RESERVED_NAME),
15030 									 errmsg("role name \"%s\" is reserved",
15031 											"none"),
15032 									 parser_errposition(@1)));
15033 						}
15034 						else
15035 						{
15036 							n = makeRoleSpec(ROLESPEC_CSTRING, @1);
15037 							n->rolename = pstrdup($1);
15038 						}
15039 						$$ = n;
15040 					}
15041 			| CURRENT_USER
15042 					{
15043 						$$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1);
15044 					}
15045 			| SESSION_USER
15046 					{
15047 						$$ = makeRoleSpec(ROLESPEC_SESSION_USER, @1);
15048 					}
15049 		;
15050 
15051 role_list:	RoleSpec
15052 					{ $$ = list_make1($1); }
15053 			| role_list ',' RoleSpec
15054 					{ $$ = lappend($1, $3); }
15055 		;
15056 
15057 /*
15058  * Name classification hierarchy.
15059  *
15060  * IDENT is the lexeme returned by the lexer for identifiers that match
15061  * no known keyword.  In most cases, we can accept certain keywords as
15062  * names, not only IDENTs.	We prefer to accept as many such keywords
15063  * as possible to minimize the impact of "reserved words" on programmers.
15064  * So, we divide names into several possible classes.  The classification
15065  * is chosen in part to make keywords acceptable as names wherever possible.
15066  */
15067 
15068 /* Column identifier --- names that can be column, table, etc names.
15069  */
15070 ColId:		IDENT									{ $$ = $1; }
15071 			| unreserved_keyword					{ $$ = pstrdup($1); }
15072 			| col_name_keyword						{ $$ = pstrdup($1); }
15073 		;
15074 
15075 /* Type/function identifier --- names that can be type or function names.
15076  */
15077 type_function_name:	IDENT							{ $$ = $1; }
15078 			| unreserved_keyword					{ $$ = pstrdup($1); }
15079 			| type_func_name_keyword				{ $$ = pstrdup($1); }
15080 		;
15081 
15082 /* Any not-fully-reserved word --- these names can be, eg, role names.
15083  */
15084 NonReservedWord:	IDENT							{ $$ = $1; }
15085 			| unreserved_keyword					{ $$ = pstrdup($1); }
15086 			| col_name_keyword						{ $$ = pstrdup($1); }
15087 			| type_func_name_keyword				{ $$ = pstrdup($1); }
15088 		;
15089 
15090 /* Column label --- allowed labels in "AS" clauses.
15091  * This presently includes *all* Postgres keywords.
15092  */
15093 ColLabel:	IDENT									{ $$ = $1; }
15094 			| unreserved_keyword					{ $$ = pstrdup($1); }
15095 			| col_name_keyword						{ $$ = pstrdup($1); }
15096 			| type_func_name_keyword				{ $$ = pstrdup($1); }
15097 			| reserved_keyword						{ $$ = pstrdup($1); }
15098 		;
15099 
15100 
15101 /*
15102  * Keyword category lists.  Generally, every keyword present in
15103  * the Postgres grammar should appear in exactly one of these lists.
15104  *
15105  * Put a new keyword into the first list that it can go into without causing
15106  * shift or reduce conflicts.  The earlier lists define "less reserved"
15107  * categories of keywords.
15108  *
15109  * Make sure that each keyword's category in kwlist.h matches where
15110  * it is listed here.  (Someday we may be able to generate these lists and
15111  * kwlist.h's table from a common master list.)
15112  */
15113 
15114 /* "Unreserved" keywords --- available for use as any kind of name.
15115  */
15116 unreserved_keyword:
15117 			  ABORT_P
15118 			| ABSOLUTE_P
15119 			| ACCESS
15120 			| ACTION
15121 			| ADD_P
15122 			| ADMIN
15123 			| AFTER
15124 			| AGGREGATE
15125 			| ALSO
15126 			| ALTER
15127 			| ALWAYS
15128 			| ASSERTION
15129 			| ASSIGNMENT
15130 			| AT
15131 			| ATTACH
15132 			| ATTRIBUTE
15133 			| BACKWARD
15134 			| BEFORE
15135 			| BEGIN_P
15136 			| BY
15137 			| CACHE
15138 			| CALL
15139 			| CALLED
15140 			| CASCADE
15141 			| CASCADED
15142 			| CATALOG_P
15143 			| CHAIN
15144 			| CHARACTERISTICS
15145 			| CHECKPOINT
15146 			| CLASS
15147 			| CLOSE
15148 			| CLUSTER
15149 			| COLUMNS
15150 			| COMMENT
15151 			| COMMENTS
15152 			| COMMIT
15153 			| COMMITTED
15154 			| CONFIGURATION
15155 			| CONFLICT
15156 			| CONNECTION
15157 			| CONSTRAINTS
15158 			| CONTENT_P
15159 			| CONTINUE_P
15160 			| CONVERSION_P
15161 			| COPY
15162 			| COST
15163 			| CSV
15164 			| CUBE
15165 			| CURRENT_P
15166 			| CURSOR
15167 			| CYCLE
15168 			| DATA_P
15169 			| DATABASE
15170 			| DAY_P
15171 			| DEALLOCATE
15172 			| DECLARE
15173 			| DEFAULTS
15174 			| DEFERRED
15175 			| DEFINER
15176 			| DELETE_P
15177 			| DELIMITER
15178 			| DELIMITERS
15179 			| DEPENDS
15180 			| DETACH
15181 			| DICTIONARY
15182 			| DISABLE_P
15183 			| DISCARD
15184 			| DOCUMENT_P
15185 			| DOMAIN_P
15186 			| DOUBLE_P
15187 			| DROP
15188 			| EACH
15189 			| ENABLE_P
15190 			| ENCODING
15191 			| ENCRYPTED
15192 			| ENUM_P
15193 			| ESCAPE
15194 			| EVENT
15195 			| EXCLUDE
15196 			| EXCLUDING
15197 			| EXCLUSIVE
15198 			| EXECUTE
15199 			| EXPLAIN
15200 			| EXTENSION
15201 			| EXTERNAL
15202 			| FAMILY
15203 			| FILTER
15204 			| FIRST_P
15205 			| FOLLOWING
15206 			| FORCE
15207 			| FORWARD
15208 			| FUNCTION
15209 			| FUNCTIONS
15210 			| GENERATED
15211 			| GLOBAL
15212 			| GRANTED
15213 			| GROUPS
15214 			| HANDLER
15215 			| HEADER_P
15216 			| HOLD
15217 			| HOUR_P
15218 			| IDENTITY_P
15219 			| IF_P
15220 			| IMMEDIATE
15221 			| IMMUTABLE
15222 			| IMPLICIT_P
15223 			| IMPORT_P
15224 			| INCLUDE
15225 			| INCLUDING
15226 			| INCREMENT
15227 			| INDEX
15228 			| INDEXES
15229 			| INHERIT
15230 			| INHERITS
15231 			| INLINE_P
15232 			| INPUT_P
15233 			| INSENSITIVE
15234 			| INSERT
15235 			| INSTEAD
15236 			| INVOKER
15237 			| ISOLATION
15238 			| KEY
15239 			| LABEL
15240 			| LANGUAGE
15241 			| LARGE_P
15242 			| LAST_P
15243 			| LEAKPROOF
15244 			| LEVEL
15245 			| LISTEN
15246 			| LOAD
15247 			| LOCAL
15248 			| LOCATION
15249 			| LOCK_P
15250 			| LOCKED
15251 			| LOGGED
15252 			| MAPPING
15253 			| MATCH
15254 			| MATERIALIZED
15255 			| MAXVALUE
15256 			| METHOD
15257 			| MINUTE_P
15258 			| MINVALUE
15259 			| MODE
15260 			| MONTH_P
15261 			| MOVE
15262 			| NAME_P
15263 			| NAMES
15264 			| NEW
15265 			| NEXT
15266 			| NO
15267 			| NOTHING
15268 			| NOTIFY
15269 			| NOWAIT
15270 			| NULLS_P
15271 			| OBJECT_P
15272 			| OF
15273 			| OFF
15274 			| OIDS
15275 			| OLD
15276 			| OPERATOR
15277 			| OPTION
15278 			| OPTIONS
15279 			| ORDINALITY
15280 			| OTHERS
15281 			| OVER
15282 			| OVERRIDING
15283 			| OWNED
15284 			| OWNER
15285 			| PARALLEL
15286 			| PARSER
15287 			| PARTIAL
15288 			| PARTITION
15289 			| PASSING
15290 			| PASSWORD
15291 			| PLANS
15292 			| POLICY
15293 			| PRECEDING
15294 			| PREPARE
15295 			| PREPARED
15296 			| PRESERVE
15297 			| PRIOR
15298 			| PRIVILEGES
15299 			| PROCEDURAL
15300 			| PROCEDURE
15301 			| PROCEDURES
15302 			| PROGRAM
15303 			| PUBLICATION
15304 			| QUOTE
15305 			| RANGE
15306 			| READ
15307 			| REASSIGN
15308 			| RECHECK
15309 			| RECURSIVE
15310 			| REF
15311 			| REFERENCING
15312 			| REFRESH
15313 			| REINDEX
15314 			| RELATIVE_P
15315 			| RELEASE
15316 			| RENAME
15317 			| REPEATABLE
15318 			| REPLACE
15319 			| REPLICA
15320 			| RESET
15321 			| RESTART
15322 			| RESTRICT
15323 			| RETURNS
15324 			| REVOKE
15325 			| ROLE
15326 			| ROLLBACK
15327 			| ROLLUP
15328 			| ROUTINE
15329 			| ROUTINES
15330 			| ROWS
15331 			| RULE
15332 			| SAVEPOINT
15333 			| SCHEMA
15334 			| SCHEMAS
15335 			| SCROLL
15336 			| SEARCH
15337 			| SECOND_P
15338 			| SECURITY
15339 			| SEQUENCE
15340 			| SEQUENCES
15341 			| SERIALIZABLE
15342 			| SERVER
15343 			| SESSION
15344 			| SET
15345 			| SETS
15346 			| SHARE
15347 			| SHOW
15348 			| SIMPLE
15349 			| SKIP
15350 			| SNAPSHOT
15351 			| SQL_P
15352 			| STABLE
15353 			| STANDALONE_P
15354 			| START
15355 			| STATEMENT
15356 			| STATISTICS
15357 			| STDIN
15358 			| STDOUT
15359 			| STORAGE
15360 			| STORED
15361 			| STRICT_P
15362 			| STRIP_P
15363 			| SUBSCRIPTION
15364 			| SUPPORT
15365 			| SYSID
15366 			| SYSTEM_P
15367 			| TABLES
15368 			| TABLESPACE
15369 			| TEMP
15370 			| TEMPLATE
15371 			| TEMPORARY
15372 			| TEXT_P
15373 			| TIES
15374 			| TRANSACTION
15375 			| TRANSFORM
15376 			| TRIGGER
15377 			| TRUNCATE
15378 			| TRUSTED
15379 			| TYPE_P
15380 			| TYPES_P
15381 			| UNBOUNDED
15382 			| UNCOMMITTED
15383 			| UNENCRYPTED
15384 			| UNKNOWN
15385 			| UNLISTEN
15386 			| UNLOGGED
15387 			| UNTIL
15388 			| UPDATE
15389 			| VACUUM
15390 			| VALID
15391 			| VALIDATE
15392 			| VALIDATOR
15393 			| VALUE_P
15394 			| VARYING
15395 			| VERSION_P
15396 			| VIEW
15397 			| VIEWS
15398 			| VOLATILE
15399 			| WHITESPACE_P
15400 			| WITHIN
15401 			| WITHOUT
15402 			| WORK
15403 			| WRAPPER
15404 			| WRITE
15405 			| XML_P
15406 			| YEAR_P
15407 			| YES_P
15408 			| ZONE
15409 		;
15410 
15411 /* Column identifier --- keywords that can be column, table, etc names.
15412  *
15413  * Many of these keywords will in fact be recognized as type or function
15414  * names too; but they have special productions for the purpose, and so
15415  * can't be treated as "generic" type or function names.
15416  *
15417  * The type names appearing here are not usable as function names
15418  * because they can be followed by '(' in typename productions, which
15419  * looks too much like a function call for an LR(1) parser.
15420  */
15421 col_name_keyword:
15422 			  BETWEEN
15423 			| BIGINT
15424 			| BIT
15425 			| BOOLEAN_P
15426 			| CHAR_P
15427 			| CHARACTER
15428 			| COALESCE
15429 			| DEC
15430 			| DECIMAL_P
15431 			| EXISTS
15432 			| EXTRACT
15433 			| FLOAT_P
15434 			| GREATEST
15435 			| GROUPING
15436 			| INOUT
15437 			| INT_P
15438 			| INTEGER
15439 			| INTERVAL
15440 			| LEAST
15441 			| NATIONAL
15442 			| NCHAR
15443 			| NONE
15444 			| NULLIF
15445 			| NUMERIC
15446 			| OUT_P
15447 			| OVERLAY
15448 			| POSITION
15449 			| PRECISION
15450 			| REAL
15451 			| ROW
15452 			| SETOF
15453 			| SMALLINT
15454 			| SUBSTRING
15455 			| TIME
15456 			| TIMESTAMP
15457 			| TREAT
15458 			| TRIM
15459 			| VALUES
15460 			| VARCHAR
15461 			| XMLATTRIBUTES
15462 			| XMLCONCAT
15463 			| XMLELEMENT
15464 			| XMLEXISTS
15465 			| XMLFOREST
15466 			| XMLNAMESPACES
15467 			| XMLPARSE
15468 			| XMLPI
15469 			| XMLROOT
15470 			| XMLSERIALIZE
15471 			| XMLTABLE
15472 		;
15473 
15474 /* Type/function identifier --- keywords that can be type or function names.
15475  *
15476  * Most of these are keywords that are used as operators in expressions;
15477  * in general such keywords can't be column names because they would be
15478  * ambiguous with variables, but they are unambiguous as function identifiers.
15479  *
15480  * Do not include POSITION, SUBSTRING, etc here since they have explicit
15481  * productions in a_expr to support the goofy SQL9x argument syntax.
15482  * - thomas 2000-11-28
15483  */
15484 type_func_name_keyword:
15485 			  AUTHORIZATION
15486 			| BINARY
15487 			| COLLATION
15488 			| CONCURRENTLY
15489 			| CROSS
15490 			| CURRENT_SCHEMA
15491 			| FREEZE
15492 			| FULL
15493 			| ILIKE
15494 			| INNER_P
15495 			| IS
15496 			| ISNULL
15497 			| JOIN
15498 			| LEFT
15499 			| LIKE
15500 			| NATURAL
15501 			| NOTNULL
15502 			| OUTER_P
15503 			| OVERLAPS
15504 			| RIGHT
15505 			| SIMILAR
15506 			| TABLESAMPLE
15507 			| VERBOSE
15508 		;
15509 
15510 /* Reserved keyword --- these keywords are usable only as a ColLabel.
15511  *
15512  * Keywords appear here if they could not be distinguished from variable,
15513  * type, or function names in some contexts.  Don't put things here unless
15514  * forced to.
15515  */
15516 reserved_keyword:
15517 			  ALL
15518 			| ANALYSE
15519 			| ANALYZE
15520 			| AND
15521 			| ANY
15522 			| ARRAY
15523 			| AS
15524 			| ASC
15525 			| ASYMMETRIC
15526 			| BOTH
15527 			| CASE
15528 			| CAST
15529 			| CHECK
15530 			| COLLATE
15531 			| COLUMN
15532 			| CONSTRAINT
15533 			| CREATE
15534 			| CURRENT_CATALOG
15535 			| CURRENT_DATE
15536 			| CURRENT_ROLE
15537 			| CURRENT_TIME
15538 			| CURRENT_TIMESTAMP
15539 			| CURRENT_USER
15540 			| DEFAULT
15541 			| DEFERRABLE
15542 			| DESC
15543 			| DISTINCT
15544 			| DO
15545 			| ELSE
15546 			| END_P
15547 			| EXCEPT
15548 			| FALSE_P
15549 			| FETCH
15550 			| FOR
15551 			| FOREIGN
15552 			| FROM
15553 			| GRANT
15554 			| GROUP_P
15555 			| HAVING
15556 			| IN_P
15557 			| INITIALLY
15558 			| INTERSECT
15559 			| INTO
15560 			| LATERAL_P
15561 			| LEADING
15562 			| LIMIT
15563 			| LOCALTIME
15564 			| LOCALTIMESTAMP
15565 			| NOT
15566 			| NULL_P
15567 			| OFFSET
15568 			| ON
15569 			| ONLY
15570 			| OR
15571 			| ORDER
15572 			| PLACING
15573 			| PRIMARY
15574 			| REFERENCES
15575 			| RETURNING
15576 			| SELECT
15577 			| SESSION_USER
15578 			| SOME
15579 			| SYMMETRIC
15580 			| TABLE
15581 			| THEN
15582 			| TO
15583 			| TRAILING
15584 			| TRUE_P
15585 			| UNION
15586 			| UNIQUE
15587 			| USER
15588 			| USING
15589 			| VARIADIC
15590 			| WHEN
15591 			| WHERE
15592 			| WINDOW
15593 			| WITH
15594 		;
15595 
15596 %%
15597 
15598 /*
15599  * The signature of this function is required by bison.  However, we
15600  * ignore the passed yylloc and instead use the last token position
15601  * available from the scanner.
15602  */
15603 static void
15604 minimal_base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
15605 {
15606 	parser_yyerror(msg);
15607 }
15608 
15609 static RawStmt *
makeRawStmt(Node * stmt,int stmt_location)15610 makeRawStmt(Node *stmt, int stmt_location)
15611 {
15612 	RawStmt    *rs = makeNode(RawStmt);
15613 
15614 	rs->stmt = stmt;
15615 	rs->stmt_location = stmt_location;
15616 	rs->stmt_len = 0;			/* might get changed later */
15617 	return rs;
15618 }
15619 
15620 /* Adjust a RawStmt to reflect that it doesn't run to the end of the string */
15621 static void
updateRawStmtEnd(RawStmt * rs,int end_location)15622 updateRawStmtEnd(RawStmt *rs, int end_location)
15623 {
15624 	/*
15625 	 * If we already set the length, don't change it.  This is for situations
15626 	 * like "select foo ;; select bar" where the same statement will be last
15627 	 * in the string for more than one semicolon.
15628 	 */
15629 	if (rs->stmt_len > 0)
15630 		return;
15631 
15632 	/* OK, update length of RawStmt */
15633 	rs->stmt_len = end_location - rs->stmt_location;
15634 }
15635 
15636 static Node *
makeColumnRef(char * colname,List * indirection,int location,core_yyscan_t yyscanner)15637 makeColumnRef(char *colname, List *indirection,
15638 			  int location, core_yyscan_t yyscanner)
15639 {
15640 	/*
15641 	 * Generate a ColumnRef node, with an A_Indirection node added if there
15642 	 * is any subscripting in the specified indirection list.  However,
15643 	 * any field selection at the start of the indirection list must be
15644 	 * transposed into the "fields" part of the ColumnRef node.
15645 	 */
15646 	ColumnRef  *c = makeNode(ColumnRef);
15647 	int		nfields = 0;
15648 	ListCell *l;
15649 
15650 	c->location = location;
15651 	foreach(l, indirection)
15652 	{
15653 		if (IsA(lfirst(l), A_Indices))
15654 		{
15655 			A_Indirection *i = makeNode(A_Indirection);
15656 
15657 			if (nfields == 0)
15658 			{
15659 				/* easy case - all indirection goes to A_Indirection */
15660 				c->fields = list_make1(makeString(colname));
15661 				i->indirection = check_indirection(indirection, yyscanner);
15662 			}
15663 			else
15664 			{
15665 				/* got to split the list in two */
15666 				i->indirection = check_indirection(list_copy_tail(indirection,
15667 																  nfields),
15668 												   yyscanner);
15669 				indirection = list_truncate(indirection, nfields);
15670 				c->fields = lcons(makeString(colname), indirection);
15671 			}
15672 			i->arg = (Node *) c;
15673 			return (Node *) i;
15674 		}
15675 		else if (IsA(lfirst(l), A_Star))
15676 		{
15677 			/* We only allow '*' at the end of a ColumnRef */
15678 			if (lnext(l) != NULL)
15679 				parser_yyerror("improper use of \"*\"");
15680 		}
15681 		nfields++;
15682 	}
15683 	/* No subscripting, so all indirection gets added to field list */
15684 	c->fields = lcons(makeString(colname), indirection);
15685 	return (Node *) c;
15686 }
15687 
15688 
15689 static Node *
makeStringConst(char * str,int location)15690 makeStringConst(char *str, int location)
15691 {
15692 	A_Const *n = makeNode(A_Const);
15693 
15694 	n->val.type = T_String;
15695 	n->val.val.str = str;
15696 	n->location = location;
15697 
15698 	return (Node *)n;
15699 }
15700 
15701 
15702 static Node *
makeFloatConst(char * str,int location)15703 makeFloatConst(char *str, int location)
15704 {
15705 	A_Const *n = makeNode(A_Const);
15706 
15707 	n->val.type = T_Float;
15708 	n->val.val.str = str;
15709 	n->location = location;
15710 
15711 	return (Node *)n;
15712 }
15713 
15714 static Node *
makeBitStringConst(char * str,int location)15715 makeBitStringConst(char *str, int location)
15716 {
15717 	A_Const *n = makeNode(A_Const);
15718 
15719 	n->val.type = T_BitString;
15720 	n->val.val.str = str;
15721 	n->location = location;
15722 
15723 	return (Node *)n;
15724 }
15725 
15726 static Node *
makeNullAConst(int location)15727 makeNullAConst(int location)
15728 {
15729 	A_Const *n = makeNode(A_Const);
15730 
15731 	n->val.type = T_Null;
15732 	n->location = location;
15733 
15734 	return (Node *)n;
15735 }
15736 
15737 static Node *
makeAConst(Value * v,int location)15738 makeAConst(Value *v, int location)
15739 {
15740 	Node *n;
15741 
15742 	switch (v->type)
15743 	{
15744 		case T_Float:
15745 			n = makeFloatConst(v->val.str, location);
15746 			break;
15747 
15748 		case T_Integer:
15749 			n = makeIntConst(v->val.ival, location);
15750 			break;
15751 
15752 		case T_String:
15753 		default:
15754 			n = makeStringConst(v->val.str, location);
15755 			break;
15756 	}
15757 
15758 	return n;
15759 }
15760 
15761 /* makeBoolAConst()
15762  * Create an A_Const string node and put it inside a boolean cast.
15763  */
15764 static Node *
makeBoolAConst(bool state,int location)15765 makeBoolAConst(bool state, int location)
15766 {
15767 	A_Const *n = makeNode(A_Const);
15768 
15769 	n->val.type = T_String;
15770 	n->val.val.str = (state ? "t" : "f");
15771 	n->location = location;
15772 
15773 	return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
15774 }
15775 
15776 /* makeRoleSpec
15777  * Create a RoleSpec with the given type
15778  */
15779 static RoleSpec *
makeRoleSpec(RoleSpecType type,int location)15780 makeRoleSpec(RoleSpecType type, int location)
15781 {
15782 	RoleSpec *spec = makeNode(RoleSpec);
15783 
15784 	spec->roletype = type;
15785 	spec->location = location;
15786 
15787 	return spec;
15788 }
15789 
15790 /* check_qualified_name --- check the result of qualified_name production
15791  *
15792  * It's easiest to let the grammar production for qualified_name allow
15793  * subscripts and '*', which we then must reject here.
15794  */
15795 static void
check_qualified_name(List * names,core_yyscan_t yyscanner)15796 check_qualified_name(List *names, core_yyscan_t yyscanner)
15797 {
15798 	ListCell   *i;
15799 
15800 	foreach(i, names)
15801 	{
15802 		if (!IsA(lfirst(i), String))
15803 			parser_yyerror("syntax error");
15804 	}
15805 }
15806 
15807 /* check_func_name --- check the result of func_name production
15808  *
15809  * It's easiest to let the grammar production for func_name allow subscripts
15810  * and '*', which we then must reject here.
15811  */
15812 static List *
check_func_name(List * names,core_yyscan_t yyscanner)15813 check_func_name(List *names, core_yyscan_t yyscanner)
15814 {
15815 	ListCell   *i;
15816 
15817 	foreach(i, names)
15818 	{
15819 		if (!IsA(lfirst(i), String))
15820 			parser_yyerror("syntax error");
15821 	}
15822 	return names;
15823 }
15824 
15825 /* check_indirection --- check the result of indirection production
15826  *
15827  * We only allow '*' at the end of the list, but it's hard to enforce that
15828  * in the grammar, so do it here.
15829  */
15830 static List *
check_indirection(List * indirection,core_yyscan_t yyscanner)15831 check_indirection(List *indirection, core_yyscan_t yyscanner)
15832 {
15833 	ListCell *l;
15834 
15835 	foreach(l, indirection)
15836 	{
15837 		if (IsA(lfirst(l), A_Star))
15838 		{
15839 			if (lnext(l) != NULL)
15840 				parser_yyerror("improper use of \"*\"");
15841 		}
15842 	}
15843 	return indirection;
15844 }
15845 
15846 /* extractArgTypes()
15847  * Given a list of FunctionParameter nodes, extract a list of just the
15848  * argument types (TypeNames) for input parameters only.  This is what
15849  * is needed to look up an existing function, which is what is wanted by
15850  * the productions that use this call.
15851  */
15852 static List *
extractArgTypes(List * parameters)15853 extractArgTypes(List *parameters)
15854 {
15855 	List	   *result = NIL;
15856 	ListCell   *i;
15857 
15858 	foreach(i, parameters)
15859 	{
15860 		FunctionParameter *p = (FunctionParameter *) lfirst(i);
15861 
15862 		if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
15863 			result = lappend(result, p->argType);
15864 	}
15865 	return result;
15866 }
15867 
15868 /* extractAggrArgTypes()
15869  * As above, but work from the output of the aggr_args production.
15870  */
15871 static List *
extractAggrArgTypes(List * aggrargs)15872 extractAggrArgTypes(List *aggrargs)
15873 {
15874 	Assert(list_length(aggrargs) == 2);
15875 	return extractArgTypes((List *) linitial(aggrargs));
15876 }
15877 
15878 /* makeOrderedSetArgs()
15879  * Build the result of the aggr_args production (which see the comments for).
15880  * This handles only the case where both given lists are nonempty, so that
15881  * we have to deal with multiple VARIADIC arguments.
15882  */
15883 static List *
makeOrderedSetArgs(List * directargs,List * orderedargs,core_yyscan_t yyscanner)15884 makeOrderedSetArgs(List *directargs, List *orderedargs,
15885 				   core_yyscan_t yyscanner)
15886 {
15887 	FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
15888 	int			ndirectargs;
15889 
15890 	/* No restriction unless last direct arg is VARIADIC */
15891 	if (lastd->mode == FUNC_PARAM_VARIADIC)
15892 	{
15893 		FunctionParameter *firsto = (FunctionParameter *) linitial(orderedargs);
15894 
15895 		/*
15896 		 * We ignore the names, though the aggr_arg production allows them;
15897 		 * it doesn't allow default values, so those need not be checked.
15898 		 */
15899 		if (list_length(orderedargs) != 1 ||
15900 			firsto->mode != FUNC_PARAM_VARIADIC)
15901 			ereport(ERROR,
15902 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15903 					 errmsg("an ordered-set aggregate with a VARIADIC direct argument must have one VARIADIC aggregated argument of the same data type"),
15904 					 parser_errposition(exprLocation((Node *) firsto))));
15905 
15906 		/* OK, drop the duplicate VARIADIC argument from the internal form */
15907 		orderedargs = NIL;
15908 	}
15909 
15910 	/* don't merge into the next line, as list_concat changes directargs */
15911 	ndirectargs = list_length(directargs);
15912 
15913 	return list_make2(list_concat(directargs, orderedargs),
15914 					  makeInteger(ndirectargs));
15915 }
15916 
15917 /* insertSelectOptions()
15918  * Insert ORDER BY, etc into an already-constructed SelectStmt.
15919  *
15920  * This routine is just to avoid duplicating code in SelectStmt productions.
15921  */
15922 static void
insertSelectOptions(SelectStmt * stmt,List * sortClause,List * lockingClause,Node * limitOffset,Node * limitCount,WithClause * withClause,core_yyscan_t yyscanner)15923 insertSelectOptions(SelectStmt *stmt,
15924 					List *sortClause, List *lockingClause,
15925 					Node *limitOffset, Node *limitCount,
15926 					WithClause *withClause,
15927 					core_yyscan_t yyscanner)
15928 {
15929 	Assert(IsA(stmt, SelectStmt));
15930 
15931 	/*
15932 	 * Tests here are to reject constructs like
15933 	 *	(SELECT foo ORDER BY bar) ORDER BY baz
15934 	 */
15935 	if (sortClause)
15936 	{
15937 		if (stmt->sortClause)
15938 			ereport(ERROR,
15939 					(errcode(ERRCODE_SYNTAX_ERROR),
15940 					 errmsg("multiple ORDER BY clauses not allowed"),
15941 					 parser_errposition(exprLocation((Node *) sortClause))));
15942 		stmt->sortClause = sortClause;
15943 	}
15944 	/* We can handle multiple locking clauses, though */
15945 	stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
15946 	if (limitOffset)
15947 	{
15948 		if (stmt->limitOffset)
15949 			ereport(ERROR,
15950 					(errcode(ERRCODE_SYNTAX_ERROR),
15951 					 errmsg("multiple OFFSET clauses not allowed"),
15952 					 parser_errposition(exprLocation(limitOffset))));
15953 		stmt->limitOffset = limitOffset;
15954 	}
15955 	if (limitCount)
15956 	{
15957 		if (stmt->limitCount)
15958 			ereport(ERROR,
15959 					(errcode(ERRCODE_SYNTAX_ERROR),
15960 					 errmsg("multiple LIMIT clauses not allowed"),
15961 					 parser_errposition(exprLocation(limitCount))));
15962 		stmt->limitCount = limitCount;
15963 	}
15964 	if (withClause)
15965 	{
15966 		if (stmt->withClause)
15967 			ereport(ERROR,
15968 					(errcode(ERRCODE_SYNTAX_ERROR),
15969 					 errmsg("multiple WITH clauses not allowed"),
15970 					 parser_errposition(exprLocation((Node *) withClause))));
15971 		stmt->withClause = withClause;
15972 	}
15973 }
15974 
15975 static Node *
makeSetOp(SetOperation op,bool all,Node * larg,Node * rarg)15976 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
15977 {
15978 	SelectStmt *n = makeNode(SelectStmt);
15979 
15980 	n->op = op;
15981 	n->all = all;
15982 	n->larg = (SelectStmt *) larg;
15983 	n->rarg = (SelectStmt *) rarg;
15984 	return (Node *) n;
15985 }
15986 
15987 /* doNegate()
15988  * Handle negation of a numeric constant.
15989  *
15990  * Formerly, we did this here because the optimizer couldn't cope with
15991  * indexquals that looked like "var = -4" --- it wants "var = const"
15992  * and a unary minus operator applied to a constant didn't qualify.
15993  * As of Postgres 7.0, that problem doesn't exist anymore because there
15994  * is a constant-subexpression simplifier in the optimizer.  However,
15995  * there's still a good reason for doing this here, which is that we can
15996  * postpone committing to a particular internal representation for simple
15997  * negative constants.	It's better to leave "-123.456" in string form
15998  * until we know what the desired type is.
15999  */
16000 static Node *
doNegate(Node * n,int location)16001 doNegate(Node *n, int location)
16002 {
16003 	if (IsA(n, A_Const))
16004 	{
16005 		A_Const *con = (A_Const *)n;
16006 
16007 		/* report the constant's location as that of the '-' sign */
16008 		con->location = location;
16009 
16010 		if (con->val.type == T_Integer)
16011 		{
16012 			con->val.val.ival = -con->val.val.ival;
16013 			return n;
16014 		}
16015 		if (con->val.type == T_Float)
16016 		{
16017 			doNegateFloat(&con->val);
16018 			return n;
16019 		}
16020 	}
16021 
16022 	return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
16023 }
16024 
16025 static void
doNegateFloat(Value * v)16026 doNegateFloat(Value *v)
16027 {
16028 	char   *oldval = v->val.str;
16029 
16030 	Assert(IsA(v, Float));
16031 	if (*oldval == '+')
16032 		oldval++;
16033 	if (*oldval == '-')
16034 		v->val.str = oldval+1;	/* just strip the '-' */
16035 	else
16036 		v->val.str = psprintf("-%s", oldval);
16037 }
16038 
16039 static Node *
makeAndExpr(Node * lexpr,Node * rexpr,int location)16040 makeAndExpr(Node *lexpr, Node *rexpr, int location)
16041 {
16042 	Node	   *lexp = lexpr;
16043 
16044 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
16045 	while (IsA(lexp, A_Expr) &&
16046 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
16047 		lexp = ((A_Expr *) lexp)->lexpr;
16048 	/* Flatten "a AND b AND c ..." to a single BoolExpr on sight */
16049 	if (IsA(lexp, BoolExpr))
16050 	{
16051 		BoolExpr *blexpr = (BoolExpr *) lexp;
16052 
16053 		if (blexpr->boolop == AND_EXPR)
16054 		{
16055 			blexpr->args = lappend(blexpr->args, rexpr);
16056 			return (Node *) blexpr;
16057 		}
16058 	}
16059 	return (Node *) makeBoolExpr(AND_EXPR, list_make2(lexpr, rexpr), location);
16060 }
16061 
16062 static Node *
makeOrExpr(Node * lexpr,Node * rexpr,int location)16063 makeOrExpr(Node *lexpr, Node *rexpr, int location)
16064 {
16065 	Node	   *lexp = lexpr;
16066 
16067 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
16068 	while (IsA(lexp, A_Expr) &&
16069 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
16070 		lexp = ((A_Expr *) lexp)->lexpr;
16071 	/* Flatten "a OR b OR c ..." to a single BoolExpr on sight */
16072 	if (IsA(lexp, BoolExpr))
16073 	{
16074 		BoolExpr *blexpr = (BoolExpr *) lexp;
16075 
16076 		if (blexpr->boolop == OR_EXPR)
16077 		{
16078 			blexpr->args = lappend(blexpr->args, rexpr);
16079 			return (Node *) blexpr;
16080 		}
16081 	}
16082 	return (Node *) makeBoolExpr(OR_EXPR, list_make2(lexpr, rexpr), location);
16083 }
16084 
16085 static Node *
makeNotExpr(Node * expr,int location)16086 makeNotExpr(Node *expr, int location)
16087 {
16088 	return (Node *) makeBoolExpr(NOT_EXPR, list_make1(expr), location);
16089 }
16090 
16091 static Node *
makeAArrayExpr(List * elements,int location)16092 makeAArrayExpr(List *elements, int location)
16093 {
16094 	A_ArrayExpr *n = makeNode(A_ArrayExpr);
16095 
16096 	n->elements = elements;
16097 	n->location = location;
16098 	return (Node *) n;
16099 }
16100 
16101 static Node *
makeSQLValueFunction(SQLValueFunctionOp op,int32 typmod,int location)16102 makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location)
16103 {
16104 	SQLValueFunction *svf = makeNode(SQLValueFunction);
16105 
16106 	svf->op = op;
16107 	/* svf->type will be filled during parse analysis */
16108 	svf->typmod = typmod;
16109 	svf->location = location;
16110 	return (Node *) svf;
16111 }
16112 
16113 static Node *
makeXmlExpr(XmlExprOp op,char * name,List * named_args,List * args,int location)16114 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
16115 			int location)
16116 {
16117 	XmlExpr		*x = makeNode(XmlExpr);
16118 
16119 	x->op = op;
16120 	x->name = name;
16121 	/*
16122 	 * named_args is a list of ResTarget; it'll be split apart into separate
16123 	 * expression and name lists in transformXmlExpr().
16124 	 */
16125 	x->named_args = named_args;
16126 	x->arg_names = NIL;
16127 	x->args = args;
16128 	/* xmloption, if relevant, must be filled in by caller */
16129 	/* type and typmod will be filled in during parse analysis */
16130 	x->type = InvalidOid;			/* marks the node as not analyzed */
16131 	x->location = location;
16132 	return (Node *) x;
16133 }
16134 
16135 /*
16136  * Merge the input and output parameters of a table function.
16137  */
16138 static List *
mergeTableFuncParameters(List * func_args,List * columns)16139 mergeTableFuncParameters(List *func_args, List *columns)
16140 {
16141 	ListCell   *lc;
16142 
16143 	/* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
16144 	foreach(lc, func_args)
16145 	{
16146 		FunctionParameter *p = (FunctionParameter *) lfirst(lc);
16147 
16148 		if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
16149 			ereport(ERROR,
16150 					(errcode(ERRCODE_SYNTAX_ERROR),
16151 					 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
16152 	}
16153 
16154 	return list_concat(func_args, columns);
16155 }
16156 
16157 /*
16158  * Determine return type of a TABLE function.  A single result column
16159  * returns setof that column's type; otherwise return setof record.
16160  */
16161 static TypeName *
TableFuncTypeName(List * columns)16162 TableFuncTypeName(List *columns)
16163 {
16164 	TypeName *result;
16165 
16166 	if (list_length(columns) == 1)
16167 	{
16168 		FunctionParameter *p = (FunctionParameter *) linitial(columns);
16169 
16170 		result = copyObject(p->argType);
16171 	}
16172 	else
16173 		result = SystemTypeName("record");
16174 
16175 	result->setof = true;
16176 
16177 	return result;
16178 }
16179 
16180 /*
16181  * Convert a list of (dotted) names to a RangeVar (like
16182  * makeRangeVarFromNameList, but with position support).  The
16183  * "AnyName" refers to the any_name production in the grammar.
16184  */
16185 static RangeVar *
makeRangeVarFromAnyName(List * names,int position,core_yyscan_t yyscanner)16186 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
16187 {
16188 	RangeVar *r = makeNode(RangeVar);
16189 
16190 	switch (list_length(names))
16191 	{
16192 		case 1:
16193 			r->catalogname = NULL;
16194 			r->schemaname = NULL;
16195 			r->relname = strVal(linitial(names));
16196 			break;
16197 		case 2:
16198 			r->catalogname = NULL;
16199 			r->schemaname = strVal(linitial(names));
16200 			r->relname = strVal(lsecond(names));
16201 			break;
16202 		case 3:
16203 			r->catalogname = strVal(linitial(names));
16204 			r->schemaname = strVal(lsecond(names));
16205 			r->relname = strVal(lthird(names));
16206 			break;
16207 		default:
16208 			ereport(ERROR,
16209 					(errcode(ERRCODE_SYNTAX_ERROR),
16210 					 errmsg("improper qualified name (too many dotted names): %s",
16211 							NameListToString(names)),
16212 					 parser_errposition(position)));
16213 			break;
16214 	}
16215 
16216 	r->relpersistence = RELPERSISTENCE_PERMANENT;
16217 	r->location = position;
16218 
16219 	return r;
16220 }
16221 
16222 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
16223 static void
SplitColQualList(List * qualList,List ** constraintList,CollateClause ** collClause,core_yyscan_t yyscanner)16224 SplitColQualList(List *qualList,
16225 				 List **constraintList, CollateClause **collClause,
16226 				 core_yyscan_t yyscanner)
16227 {
16228 	ListCell   *cell;
16229 	ListCell   *prev;
16230 	ListCell   *next;
16231 
16232 	*collClause = NULL;
16233 	prev = NULL;
16234 	for (cell = list_head(qualList); cell; cell = next)
16235 	{
16236 		Node   *n = (Node *) lfirst(cell);
16237 
16238 		next = lnext(cell);
16239 		if (IsA(n, Constraint))
16240 		{
16241 			/* keep it in list */
16242 			prev = cell;
16243 			continue;
16244 		}
16245 		if (IsA(n, CollateClause))
16246 		{
16247 			CollateClause *c = (CollateClause *) n;
16248 
16249 			if (*collClause)
16250 				ereport(ERROR,
16251 						(errcode(ERRCODE_SYNTAX_ERROR),
16252 						 errmsg("multiple COLLATE clauses not allowed"),
16253 						 parser_errposition(c->location)));
16254 			*collClause = c;
16255 		}
16256 		else
16257 			elog(ERROR, "unexpected node type %d", (int) n->type);
16258 		/* remove non-Constraint nodes from qualList */
16259 		qualList = list_delete_cell(qualList, cell, prev);
16260 	}
16261 	*constraintList = qualList;
16262 }
16263 
16264 /*
16265  * Process result of ConstraintAttributeSpec, and set appropriate bool flags
16266  * in the output command node.  Pass NULL for any flags the particular
16267  * command doesn't support.
16268  */
16269 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)16270 processCASbits(int cas_bits, int location, const char *constrType,
16271 			   bool *deferrable, bool *initdeferred, bool *not_valid,
16272 			   bool *no_inherit, core_yyscan_t yyscanner)
16273 {
16274 	/* defaults */
16275 	if (deferrable)
16276 		*deferrable = false;
16277 	if (initdeferred)
16278 		*initdeferred = false;
16279 	if (not_valid)
16280 		*not_valid = false;
16281 
16282 	if (cas_bits & (CAS_DEFERRABLE | CAS_INITIALLY_DEFERRED))
16283 	{
16284 		if (deferrable)
16285 			*deferrable = true;
16286 		else
16287 			ereport(ERROR,
16288 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16289 					 /* translator: %s is CHECK, UNIQUE, or similar */
16290 					 errmsg("%s constraints cannot be marked DEFERRABLE",
16291 							constrType),
16292 					 parser_errposition(location)));
16293 	}
16294 
16295 	if (cas_bits & CAS_INITIALLY_DEFERRED)
16296 	{
16297 		if (initdeferred)
16298 			*initdeferred = true;
16299 		else
16300 			ereport(ERROR,
16301 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16302 					 /* translator: %s is CHECK, UNIQUE, or similar */
16303 					 errmsg("%s constraints cannot be marked DEFERRABLE",
16304 							constrType),
16305 					 parser_errposition(location)));
16306 	}
16307 
16308 	if (cas_bits & CAS_NOT_VALID)
16309 	{
16310 		if (not_valid)
16311 			*not_valid = true;
16312 		else
16313 			ereport(ERROR,
16314 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16315 					 /* translator: %s is CHECK, UNIQUE, or similar */
16316 					 errmsg("%s constraints cannot be marked NOT VALID",
16317 							constrType),
16318 					 parser_errposition(location)));
16319 	}
16320 
16321 	if (cas_bits & CAS_NO_INHERIT)
16322 	{
16323 		if (no_inherit)
16324 			*no_inherit = true;
16325 		else
16326 			ereport(ERROR,
16327 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16328 					 /* translator: %s is CHECK, UNIQUE, or similar */
16329 					 errmsg("%s constraints cannot be marked NO INHERIT",
16330 							constrType),
16331 					 parser_errposition(location)));
16332 	}
16333 }
16334 
16335 /*----------
16336  * Recursive view transformation
16337  *
16338  * Convert
16339  *
16340  *     CREATE RECURSIVE VIEW relname (aliases) AS query
16341  *
16342  * to
16343  *
16344  *     CREATE VIEW relname (aliases) AS
16345  *         WITH RECURSIVE relname (aliases) AS (query)
16346  *         SELECT aliases FROM relname
16347  *
16348  * Actually, just the WITH ... part, which is then inserted into the original
16349  * view definition as the query.
16350  * ----------
16351  */
16352 static Node *
makeRecursiveViewSelect(char * relname,List * aliases,Node * query)16353 makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
16354 {
16355 	SelectStmt *s = makeNode(SelectStmt);
16356 	WithClause *w = makeNode(WithClause);
16357 	CommonTableExpr *cte = makeNode(CommonTableExpr);
16358 	List	   *tl = NIL;
16359 	ListCell   *lc;
16360 
16361 	/* create common table expression */
16362 	cte->ctename = relname;
16363 	cte->aliascolnames = aliases;
16364 	cte->ctematerialized = CTEMaterializeDefault;
16365 	cte->ctequery = query;
16366 	cte->location = -1;
16367 
16368 	/* create WITH clause and attach CTE */
16369 	w->recursive = true;
16370 	w->ctes = list_make1(cte);
16371 	w->location = -1;
16372 
16373 	/* create target list for the new SELECT from the alias list of the
16374 	 * recursive view specification */
16375 	foreach (lc, aliases)
16376 	{
16377 		ResTarget *rt = makeNode(ResTarget);
16378 
16379 		rt->name = NULL;
16380 		rt->indirection = NIL;
16381 		rt->val = makeColumnRef(strVal(lfirst(lc)), NIL, -1, 0);
16382 		rt->location = -1;
16383 
16384 		tl = lappend(tl, rt);
16385 	}
16386 
16387 	/* create new SELECT combining WITH clause, target list, and fake FROM
16388 	 * clause */
16389 	s->withClause = w;
16390 	s->targetList = tl;
16391 	s->fromClause = list_make1(makeRangeVar(NULL, relname, -1));
16392 
16393 	return (Node *) s;
16394 }
16395 
16396 /* parser_init()
16397  * Initialize to parse one query string
16398  */
16399 void
minimal_parser_init(base_yy_extra_type * yyext)16400 minimal_parser_init(base_yy_extra_type *yyext)
16401 {
16402 	yyext->parsetree = NIL;		/* in case grammar forgets to set it */
16403 }
16404 
16405