1 %{
2 
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5  *
6  * gram.y
7  *	  POSTGRESQL BISON rules/actions
8  *
9  * Portions Copyright (c) 2003-2017, PgPool Global Development Group
10  * Portions Copyright (c) 1996-2017, 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 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 	GrantObjectType 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 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="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 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 		CreateAssertStmt CreateTransformStmt CreateTrigStmt CreateEventTrigStmt
300 		CreateUserStmt CreateUserMappingStmt CreateRoleStmt CreatePolicyStmt
301 		CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
302 		DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
303 		DropAssertStmt DropCastStmt DropRoleStmt
304 		DropdbStmt DropTableSpaceStmt
305 		DropTransformStmt
306 		DropUserMappingStmt ExplainStmt FetchStmt
307 		GrantStmt GrantRoleStmt ImportForeignSchemaStmt IndexStmt InsertStmt
308 		ListenStmt LoadStmt LockStmt NotifyStmt ExplainableStmt PreparableStmt
309 		CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
310 		RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
311 		RuleActionStmt RuleActionStmtOrEmpty RuleStmt
312 		SecLabelStmt SelectStmt TransactionStmt TruncateStmt
313 		UnlistenStmt UpdateStmt VacuumStmt
314 		VariableResetStmt VariableSetStmt VariableShowStmt
315 		ViewStmt CheckPointStmt CreateConversionStmt
316 		DeallocateStmt PrepareStmt ExecuteStmt
317 		DropOwnedStmt ReassignOwnedStmt
318 		AlterTSConfigurationStmt AlterTSDictionaryStmt
319 		CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
320 		CreatePublicationStmt AlterPublicationStmt
321 		CreateSubscriptionStmt AlterSubscriptionStmt DropSubscriptionStmt
322 
323 %type <node>	select_no_parens select_with_parens select_clause
324 				simple_select values_clause
325 
326 %type <node>	alter_column_default opclass_item opclass_drop alter_using
327 %type <ival>	add_drop opt_asc_desc opt_nulls_order
328 
329 %type <node>	alter_table_cmd alter_type_cmd opt_collate_clause
330 	   replica_identity partition_cmd
331 %type <list>	alter_table_cmds alter_type_cmds
332 %type <list>    alter_identity_column_option_list
333 %type <defelt>  alter_identity_column_option
334 
335 %type <dbehavior>	opt_drop_behavior
336 
337 %type <list>	createdb_opt_list createdb_opt_items copy_opt_list
338 				transaction_mode_list
339 				create_extension_opt_list alter_extension_opt_list
340 %type <defelt>	createdb_opt_item copy_opt_item
341 				transaction_mode_item
342 				create_extension_opt_item alter_extension_opt_item
343 
344 %type <ival>	opt_lock lock_type cast_context
345 %type <ival>	vacuum_option_list vacuum_option_elem
346 %type <boolean>	opt_or_replace
347 				opt_grant_grant_option opt_grant_admin_option
348 				opt_nowait opt_if_exists opt_with_data
349 %type <ival>	opt_nowait_or_skip
350 
351 %type <list>	OptRoleList AlterOptRoleList
352 %type <defelt>	CreateOptRoleElem AlterOptRoleElem
353 
354 %type <str>		opt_type
355 %type <str>		foreign_server_version opt_foreign_server_version
356 %type <str>		opt_in_database
357 
358 %type <str>		OptSchemaName
359 %type <list>	OptSchemaEltList
360 
361 %type <boolean> TriggerForSpec TriggerForType
362 %type <ival>	TriggerActionTime
363 %type <list>	TriggerEvents TriggerOneEvent
364 %type <value>	TriggerFuncArg
365 %type <node>	TriggerWhen
366 %type <str>		TransitionRelName
367 %type <boolean>	TransitionRowOrTable TransitionOldOrNew
368 %type <node>	TriggerTransition
369 
370 %type <list>	event_trigger_when_list event_trigger_value_list
371 %type <defelt>	event_trigger_when_item
372 %type <chr>		enable_trigger
373 
374 %type <str>		copy_file_name
375 				database_name access_method_clause access_method attr_name
376 				name cursor_name file_name
377 				index_name opt_index_name cluster_index_specification
378 
379 %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
380 				opt_class opt_inline_handler opt_validator validator_clause
381 				opt_collate
382 
383 %type <range>	qualified_name insert_target OptConstrFromTable
384 
385 %type <str>		all_Op MathOp
386 
387 %type <str>		row_security_cmd RowSecurityDefaultForCmd
388 %type <boolean> RowSecurityDefaultPermissive
389 %type <node>	RowSecurityOptionalWithCheck RowSecurityOptionalExpr
390 %type <list>	RowSecurityDefaultToRole RowSecurityOptionalToRole
391 
392 %type <str>		iso_level opt_encoding
393 %type <rolespec> grantee
394 %type <list>	grantee_list
395 %type <accesspriv> privilege
396 %type <list>	privileges privilege_list
397 %type <privtarget> privilege_target
398 %type <objwithargs> function_with_argtypes aggregate_with_argtypes operator_with_argtypes
399 %type <list>	function_with_argtypes_list aggregate_with_argtypes_list operator_with_argtypes_list
400 %type <ival>	defacl_privilege_target
401 %type <defelt>	DefACLOption
402 %type <list>	DefACLOptionList
403 %type <ival>	import_qualification_type
404 %type <importqual> import_qualification
405 
406 %type <list>	stmtblock stmtmulti
407 				OptTableElementList TableElementList OptInherit definition
408 				OptTypedTableElementList TypedTableElementList
409 				reloptions opt_reloptions
410 				OptWith distinct_clause opt_all_clause opt_definition func_args func_args_list
411 				func_args_with_defaults func_args_with_defaults_list
412 				aggr_args aggr_args_list
413 				func_as createfunc_opt_list alterfunc_opt_list
414 				old_aggr_definition old_aggr_list
415 				oper_argtypes RuleActionList RuleActionMulti
416 				opt_column_list columnList opt_name_list
417 				sort_clause opt_sort_clause sortby_list index_params
418 				name_list role_list from_clause from_list opt_array_bounds
419 				qualified_name_list any_name any_name_list type_name_list
420 				any_operator expr_list attrs
421 				target_list opt_target_list insert_column_list set_target_list
422 				set_clause_list set_clause
423 				def_list operator_def_list indirection opt_indirection
424 				reloption_list group_clause TriggerFuncArgs select_limit
425 				opt_select_limit opclass_item_list opclass_drop_list
426 				opclass_purpose opt_opfamily transaction_mode_list_or_empty
427 				OptTableFuncElementList TableFuncElementList opt_type_modifiers
428 				prep_type_clause
429 				execute_param_clause using_clause returning_clause
430 				opt_enum_val_list enum_val_list table_func_column_list
431 				create_generic_options alter_generic_options
432 				relation_expr_list dostmt_opt_list
433 				transform_element_list transform_type_list
434 				TriggerTransitions TriggerReferencing
435 				publication_name_list
436 
437 %type <list>	group_by_list
438 %type <node>	group_by_item empty_grouping_set rollup_clause cube_clause
439 %type <node>	grouping_sets_clause
440 %type <node>	opt_publication_for_tables publication_for_tables
441 %type <value>	publication_name_item
442 
443 %type <list>	opt_fdw_options fdw_options
444 %type <defelt>	fdw_option
445 
446 %type <range>	OptTempTableName
447 %type <into>	into_clause create_as_target create_mv_target
448 
449 %type <defelt>	createfunc_opt_item common_func_opt_item dostmt_opt_item
450 %type <fun_param> func_arg func_arg_with_default table_func_column aggr_arg
451 %type <fun_param_mode> arg_class
452 %type <typnam>	func_return func_type
453 
454 %type <boolean>  opt_trusted opt_restart_seqs
455 %type <ival>	 OptTemp
456 %type <ival>	 OptNoLog
457 %type <oncommit> OnCommitOption
458 
459 %type <ival>	for_locking_strength
460 %type <node>	for_locking_item
461 %type <list>	for_locking_clause opt_for_locking_clause for_locking_items
462 %type <list>	locked_rels_list
463 %type <boolean>	all_or_distinct
464 
465 %type <node>	join_outer join_qual
466 %type <jtype>	join_type
467 
468 %type <list>	extract_list overlay_list position_list
469 %type <list>	substr_list trim_list
470 %type <list>	opt_interval interval_second
471 %type <node>	overlay_placing substr_from substr_for
472 
473 %type <boolean> opt_instead
474 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
475 %type <boolean> opt_freeze opt_default opt_recheck
476 %type <defelt>	opt_binary opt_oids copy_delimiter
477 
478 %type <boolean> copy_from opt_program
479 
480 %type <ival>	opt_column event cursor_options opt_hold opt_set_data
481 %type <objtype>	drop_type_any_name drop_type_name drop_type_name_on_any_name
482 				comment_type_any_name comment_type_name
483 				security_label_type_any_name security_label_type_name
484 
485 %type <node>	fetch_args limit_clause select_limit_value
486 				offset_clause select_offset_value
487 				select_offset_value2 opt_select_fetch_first_value
488 %type <ival>	row_or_rows first_or_next
489 
490 %type <list>	OptSeqOptList SeqOptList OptParenthesizedSeqOptList
491 %type <defelt>	SeqOptElem
492 
493 %type <istmt>	insert_rest
494 %type <infer>	opt_conf_expr
495 %type <onconflict> opt_on_conflict
496 
497 %type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
498 				 SetResetClause FunctionSetResetClause
499 
500 %type <node>	TableElement TypedTableElement ConstraintElem TableFuncElement
501 %type <node>	columnDef columnOptions
502 %type <defelt>	def_elem reloption_elem old_aggr_elem operator_def_elem
503 %type <node>	def_arg columnElem where_clause where_or_current_clause
504 				a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
505 				columnref in_expr having_clause func_table xmltable array_expr
506 				ExclusionWhereClause operator_def_arg
507 %type <list>	rowsfrom_item rowsfrom_list opt_col_def_list
508 %type <boolean> opt_ordinality
509 %type <list>	ExclusionConstraintList ExclusionConstraintElem
510 %type <list>	func_arg_list
511 %type <node>	func_arg_expr
512 %type <list>	row explicit_row implicit_row type_list array_expr_list
513 %type <node>	case_expr case_arg when_clause case_default
514 %type <list>	when_clause_list
515 %type <ival>	sub_type
516 %type <value>	NumericOnly
517 %type <list>	NumericOnly_list
518 %type <alias>	alias_clause opt_alias_clause
519 %type <list>	func_alias_clause
520 %type <sortby>	sortby
521 %type <ielem>	index_elem
522 %type <node>	table_ref
523 %type <jexpr>	joined_table
524 %type <range>	relation_expr
525 %type <range>	relation_expr_opt_alias
526 %type <node>	tablesample_clause opt_repeatable_clause
527 %type <target>	target_el set_target insert_column_item
528 
529 %type <str>		generic_option_name
530 %type <node>	generic_option_arg
531 %type <defelt>	generic_option_elem alter_generic_option_elem
532 %type <list>	generic_option_list alter_generic_option_list
533 %type <str>		explain_option_name
534 %type <node>	explain_option_arg
535 %type <defelt>	explain_option_elem
536 %type <list>	explain_option_list
537 
538 %type <ival>	reindex_target_type reindex_target_multitable
539 %type <ival>	reindex_option_list reindex_option_elem
540 
541 %type <node>	copy_generic_opt_arg copy_generic_opt_arg_list_item
542 %type <defelt>	copy_generic_opt_elem
543 %type <list>	copy_generic_opt_list copy_generic_opt_arg_list
544 %type <list>	copy_options
545 
546 %type <typnam>	Typename SimpleTypename ConstTypename
547 				GenericType Numeric opt_float
548 				Character ConstCharacter
549 				CharacterWithLength CharacterWithoutLength
550 				ConstDatetime ConstInterval
551 				Bit ConstBit BitWithLength BitWithoutLength
552 %type <str>		character
553 %type <str>		extract_arg
554 %type <boolean> opt_varying opt_timezone opt_no_inherit
555 
556 %type <ival>	Iconst SignedIconst
557 %type <str>		Sconst comment_text notify_payload
558 %type <str>		RoleId opt_boolean_or_string
559 %type <list>	var_list
560 %type <str>		ColId ColLabel var_name type_function_name param_name
561 %type <str>		NonReservedWord NonReservedWord_or_Sconst
562 %type <str>		createdb_opt_name
563 %type <node>	var_value zone_value
564 %type <rolespec> auth_ident RoleSpec opt_granted_by
565 
566 %type <keyword> unreserved_keyword type_func_name_keyword
567 %type <keyword> col_name_keyword reserved_keyword
568 
569 %type <node>	TableConstraint TableLikeClause
570 %type <ival>	TableLikeOptionList TableLikeOption
571 %type <list>	ColQualList
572 %type <node>	ColConstraint ColConstraintElem ConstraintAttr
573 %type <ival>	key_actions key_delete key_match key_update key_action
574 %type <ival>	ConstraintAttributeSpec ConstraintAttributeElem
575 %type <str>		ExistingIndex
576 
577 %type <list>	constraints_set_list
578 %type <boolean> constraints_set_mode
579 %type <str>		OptTableSpace OptConsTableSpace
580 %type <rolespec> OptTableSpaceOwner
581 %type <ival>	opt_check_option
582 
583 %type <str>		opt_provider security_label
584 
585 %type <target>	xml_attribute_el
586 %type <list>	xml_attribute_list xml_attributes
587 %type <node>	xml_root_version opt_xml_root_standalone
588 %type <node>	xmlexists_argument
589 %type <ival>	document_or_content
590 %type <boolean> xml_whitespace_option
591 %type <list>	xmltable_column_list xmltable_column_option_list
592 %type <node>	xmltable_column_el
593 %type <defelt>	xmltable_column_option_el
594 %type <list>	xml_namespace_list
595 %type <target>	xml_namespace_el
596 
597 %type <node>	func_application func_expr_common_subexpr
598 %type <node>	func_expr func_expr_windowless
599 %type <node>	common_table_expr
600 %type <with>	with_clause opt_with_clause
601 %type <list>	cte_list
602 
603 %type <list>	within_group_clause
604 %type <node>	filter_clause
605 %type <list>	window_clause window_definition_list opt_partition_clause
606 %type <windef>	window_definition over_clause window_specification
607 				opt_frame_clause frame_extent frame_bound
608 %type <str>		opt_existing_window_name
609 %type <boolean> opt_if_not_exists
610 %type <ival>	generated_when override_kind
611 %type <partspec>	PartitionSpec OptPartitionSpec
612 %type <str>			part_strategy
613 %type <partelem>	part_elem
614 %type <list>		part_params
615 %type <partboundspec> ForValues
616 %type <node>		partbound_datum PartitionRangeDatum
617 %type <list>		partbound_datum_list range_datum_list
618 
619 /*
620  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
621  * They must be listed first so that their numeric codes do not depend on
622  * the set of keywords.  PL/pgSQL depends on this so that it can share the
623  * same lexer.  If you add/change tokens here, fix PL/pgSQL to match!
624  *
625  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
626  * parse errors.  It is needed by PL/pgSQL.
627  */
628 %token <str>	IDENT FCONST SCONST BCONST XCONST Op
629 %token <ival>	ICONST PARAM
630 %token			TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
631 %token			LESS_EQUALS GREATER_EQUALS NOT_EQUALS
632 
633 /*
634  * If you want to make any keyword changes, update the keyword table in
635  * src/include/parser/kwlist.h and add new keywords to the appropriate one
636  * of the reserved-or-not-so-reserved keyword lists, below; search
637  * this file for "Keyword category lists".
638  */
639 
640 /* ordinary key words in alphabetical order */
641 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
642 	AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
643 	ASSERTION ASSIGNMENT ASYMMETRIC AT ATTACH ATTRIBUTE AUTHORIZATION
644 
645 	BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
646 	BOOLEAN_P BOTH BY
647 
648 	CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
649 	CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
650 	CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMENT COMMENTS COMMIT
651 	COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION CONSTRAINT
652 	CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE
653 	CROSS CSV CUBE CURRENT_P
654 	CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
655 	CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
656 
657 	DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
658 	DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DESC
659 	DETACH DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P
660 	DOUBLE_P DROP
661 
662 	EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EVENT EXCEPT
663 	EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
664 	EXTENSION EXTERNAL EXTRACT
665 
666 	FALSE_P FAMILY FETCH FILTER FIRST_P FLOAT_P FOLLOWING FOR
667 	FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
668 
669 	GENERATED GLOBAL GRANT GRANTED GREATEST GROUP_P GROUPING
670 
671 	HANDLER HAVING HEADER_P HOLD HOUR_P
672 
673 	IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P
674 	INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
675 	INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
676 	INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
677 
678 	JOIN
679 
680 	KEY
681 
682 	LABEL LANGUAGE LARGE_P LAST_P LATERAL_P
683 	LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL
684 	LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED
685 
686 	MAPPING MATCH MATERIALIZED MAXVALUE METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE
687 
688 	NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NONE
689 	NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
690 	NULLS_P NUMERIC
691 
692 	OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
693 	ORDER ORDINALITY OUT_P OUTER_P OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
694 
695 	PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PGPOOL PLACING PLANS POLICY
696 	POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
697 	PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROGRAM PUBLICATION
698 
699 	QUOTE
700 
701 	RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REFERENCING
702 	REFRESH REINDEX RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
703 	RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP
704 	ROW ROWS RULE
705 
706 	SAVEPOINT SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
707 	SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
708 	SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P
709 	START STATEMENT STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P
710 	SUBSCRIPTION SUBSTRING SYMMETRIC SYSID SYSTEM_P
711 
712 	TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
713 	TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM TREAT TRIGGER TRIM TRUE_P
714 	TRUNCATE TRUSTED TYPE_P TYPES_P
715 
716 	UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNLOGGED
717 	UNTIL UPDATE USER USING
718 
719 	VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
720 	VERBOSE VERSION_P VIEW VIEWS VOLATILE
721 
722 	WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE
723 
724 	XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLNAMESPACES
725 	XMLPARSE XMLPI XMLROOT XMLSERIALIZE XMLTABLE
726 
727 	YEAR_P YES_P
728 
729 	ZONE
730 
731 /*
732  * The grammar thinks these are keywords, but they are not in the kwlist.h
733  * list and so can never be entered directly.  The filter in parser.c
734  * creates these tokens when required (based on looking one token ahead).
735  *
736  * NOT_LA exists so that productions such as NOT LIKE can be given the same
737  * precedence as LIKE; otherwise they'd effectively have the same precedence
738  * as NOT, at least with respect to their left-hand subexpression.
739  * NULLS_LA and WITH_LA are needed to make the grammar LALR(1).
740  */
741 %token		NOT_LA NULLS_LA WITH_LA
742 
743 
744 /* Precedence: lowest to highest */
745 %nonassoc	SET				/* see relation_expr_opt_alias */
746 %left		UNION EXCEPT
747 %left		INTERSECT
748 %left		OR
749 %left		AND
750 %right		NOT
751 %nonassoc	IS ISNULL NOTNULL	/* IS sets precedence for IS NULL, etc */
752 %nonassoc	'<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
753 %nonassoc	BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA
754 %nonassoc	ESCAPE			/* ESCAPE must be just above LIKE/ILIKE/SIMILAR */
755 %left		POSTFIXOP		/* dummy for postfix Op rules */
756 /*
757  * To support target_el without AS, we must give IDENT an explicit priority
758  * between POSTFIXOP and Op.  We can safely assign the same priority to
759  * various unreserved keywords as needed to resolve ambiguities (this can't
760  * have any bad effects since obviously the keywords will still behave the
761  * same as if they weren't keywords).  We need to do this for PARTITION,
762  * RANGE, ROWS to support opt_existing_window_name; and for RANGE, ROWS
763  * so that they can follow a_expr without creating postfix-operator problems;
764  * for GENERATED so that it can follow b_expr;
765  * and for NULL so that it can follow b_expr in ColQualList without creating
766  * postfix-operator problems.
767  *
768  * To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
769  * an explicit priority lower than '(', so that a rule with CUBE '(' will shift
770  * rather than reducing a conflicting rule that takes CUBE as a function name.
771  * Using the same precedence as IDENT seems right for the reasons given above.
772  *
773  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
774  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
775  * there is no principled way to distinguish these from the productions
776  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
777  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
778  * appear to cause UNBOUNDED to be treated differently from other unreserved
779  * keywords anywhere else in the grammar, but it's definitely risky.  We can
780  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
781  */
782 %nonassoc	UNBOUNDED		/* ideally should have same precedence as IDENT */
783 %nonassoc	IDENT GENERATED NULL_P PARTITION RANGE ROWS PRECEDING FOLLOWING CUBE ROLLUP
784 %left		Op OPERATOR		/* multi-character ops and user-defined operators */
785 %left		'+' '-'
786 %left		'*' '/' '%'
787 %left		'^'
788 /* Unary Operators */
789 %left		AT				/* sets precedence for AT TIME ZONE */
790 %left		COLLATE
791 %right		UMINUS
792 %left		'[' ']'
793 %left		'(' ')'
794 %left		TYPECAST
795 %left		'.'
796 /*
797  * These might seem to be low-precedence, but actually they are not part
798  * of the arithmetic hierarchy at all in their use as JOIN operators.
799  * We make them high-precedence to support their use as function names.
800  * They wouldn't be given a precedence at all, were it not that we need
801  * left-associativity among the JOIN rules themselves.
802  */
803 %left		JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
804 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
805 %right		PRESERVE STRIP_P
806 
807 %%
808 
809 /*
810  *	The target production for the whole parse.
811  */
812 stmtblock:	stmtmulti
813 			{
814 				pg_yyget_extra(yyscanner)->parsetree = $1;
815 			}
816 		;
817 
818 /*
819  * At top level, we wrap each stmt with a RawStmt node carrying start location
820  * and length of the stmt's text.  Notice that the start loc/len are driven
821  * entirely from semicolon locations (@2).  It would seem natural to use
822  * @1 or @3 to get the true start location of a stmt, but that doesn't work
823  * for statements that can start with empty nonterminals (opt_with_clause is
824  * the main offender here); as noted in the comments for YYLLOC_DEFAULT,
825  * we'd get -1 for the location in such cases.
826  * We also take care to discard empty statements entirely.
827  */
828 stmtmulti:	stmtmulti ';' stmt
829 				{
830 					if ($1 != NIL)
831 					{
832 						/* update length of previous stmt */
833 						updateRawStmtEnd(llast_node(RawStmt, $1), @2);
834 					}
835 					if ($3 != NULL)
836 						$$ = lappend($1, makeRawStmt($3, @2 + 1));
837 					else
838 						$$ = $1;
839 				}
840 			| stmt
841 				{
842 					if ($1 != NULL)
843 						$$ = list_make1(makeRawStmt($1, 0));
844 					else
845 						$$ = NIL;
846 				}
847 		;
848 
849 stmt :
850 			AlterEventTrigStmt
851 			| AlterCollationStmt
852 			| AlterDatabaseStmt
853 			| AlterDatabaseSetStmt
854 			| AlterDefaultPrivilegesStmt
855 			| AlterDomainStmt
856 			| AlterEnumStmt
857 			| AlterExtensionStmt
858 			| AlterExtensionContentsStmt
859 			| AlterFdwStmt
860 			| AlterForeignServerStmt
861 			| AlterForeignTableStmt
862 			| AlterFunctionStmt
863 			| AlterGroupStmt
864 			| AlterObjectDependsStmt
865 			| AlterObjectSchemaStmt
866 			| AlterOwnerStmt
867 			| AlterOperatorStmt
868 			| AlterPolicyStmt
869 			| AlterSeqStmt
870 			| AlterSystemStmt
871 			| AlterTableStmt
872 			| AlterTblSpcStmt
873 			| AlterCompositeTypeStmt
874 			| AlterPublicationStmt
875 			| AlterRoleSetStmt
876 			| AlterRoleStmt
877 			| AlterSubscriptionStmt
878 			| AlterTSConfigurationStmt
879 			| AlterTSDictionaryStmt
880 			| AlterUserMappingStmt
881 			| AnalyzeStmt
882 			| CheckPointStmt
883 			| ClosePortalStmt
884 			| ClusterStmt
885 			| CommentStmt
886 			| ConstraintsSetStmt
887 			| CopyStmt
888 			| CreateAmStmt
889 			| CreateAsStmt
890 			| CreateAssertStmt
891 			| CreateCastStmt
892 			| CreateConversionStmt
893 			| CreateDomainStmt
894 			| CreateExtensionStmt
895 			| CreateFdwStmt
896 			| CreateForeignServerStmt
897 			| CreateForeignTableStmt
898 			| CreateFunctionStmt
899 			| CreateGroupStmt
900 			| CreateMatViewStmt
901 			| CreateOpClassStmt
902 			| CreateOpFamilyStmt
903 			| CreatePublicationStmt
904 			| AlterOpFamilyStmt
905 			| CreatePolicyStmt
906 			| CreatePLangStmt
907 			| CreateSchemaStmt
908 			| CreateSeqStmt
909 			| CreateStmt
910 			| CreateSubscriptionStmt
911 			| CreateStatsStmt
912 			| CreateTableSpaceStmt
913 			| CreateTransformStmt
914 			| CreateTrigStmt
915 			| CreateEventTrigStmt
916 			| CreateRoleStmt
917 			| CreateUserStmt
918 			| CreateUserMappingStmt
919 			| CreatedbStmt
920 			| DeallocateStmt
921 			| DeclareCursorStmt
922 			| DefineStmt
923 			| DeleteStmt
924 			| DiscardStmt
925 			| DoStmt
926 			| DropAssertStmt
927 			| DropCastStmt
928 			| DropOpClassStmt
929 			| DropOpFamilyStmt
930 			| DropOwnedStmt
931 			| DropPLangStmt
932 			| DropStmt
933 			| DropSubscriptionStmt
934 			| DropTableSpaceStmt
935 			| DropTransformStmt
936 			| DropRoleStmt
937 			| DropUserMappingStmt
938 			| DropdbStmt
939 			| ExecuteStmt
940 			| ExplainStmt
941 			| FetchStmt
942 			| GrantStmt
943 			| GrantRoleStmt
944 			| ImportForeignSchemaStmt
945 			| IndexStmt
946 			| InsertStmt
947 			| ListenStmt
948 			| RefreshMatViewStmt
949 			| LoadStmt
950 			| LockStmt
951 			| NotifyStmt
952 			| PrepareStmt
953 			| ReassignOwnedStmt
954 			| ReindexStmt
955 			| RemoveAggrStmt
956 			| RemoveFuncStmt
957 			| RemoveOperStmt
958 			| RenameStmt
959 			| RevokeStmt
960 			| RevokeRoleStmt
961 			| RuleStmt
962 			| SecLabelStmt
963 			| SelectStmt
964 			| TransactionStmt
965 			| TruncateStmt
966 			| UnlistenStmt
967 			| UpdateStmt
968 			| VacuumStmt
969 			| VariableResetStmt
970 			| VariableSetStmt
971 			| VariableShowStmt
972 			| ViewStmt
973 			| /*EMPTY*/
974 				{ $$ = NULL; }
975 		;
976 
977 /*****************************************************************************
978  *
979  * Create a new Postgres DBMS role
980  *
981  *****************************************************************************/
982 
983 CreateRoleStmt:
984 			CREATE ROLE RoleId opt_with OptRoleList
985 				{
986 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
987 					n->stmt_type = ROLESTMT_ROLE;
988 					n->role = $3;
989 					n->options = $5;
990 					$$ = (Node *)n;
991 				}
992 		;
993 
994 
995 opt_with:	WITH									{}
996 			| WITH_LA								{}
997 			| /*EMPTY*/								{}
998 		;
999 
1000 /*
1001  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
1002  * for backwards compatibility).  Note: the only option required by SQL99
1003  * is "WITH ADMIN name".
1004  */
1005 OptRoleList:
1006 			OptRoleList CreateOptRoleElem			{ $$ = lappend($1, $2); }
1007 			| /* EMPTY */							{ $$ = NIL; }
1008 		;
1009 
1010 AlterOptRoleList:
1011 			AlterOptRoleList AlterOptRoleElem		{ $$ = lappend($1, $2); }
1012 			| /* EMPTY */							{ $$ = NIL; }
1013 		;
1014 
1015 AlterOptRoleElem:
1016 			PASSWORD Sconst
1017 				{
1018 					$$ = makeDefElem("password",
1019 									 (Node *)makeString($2), @1);
1020 				}
1021 			| PASSWORD NULL_P
1022 				{
1023 					$$ = makeDefElem("password", NULL, @1);
1024 				}
1025 			| ENCRYPTED PASSWORD Sconst
1026 				{
1027 					/*
1028 					 * These days, passwords are always stored in encrypted
1029 					 * form, so there is no difference between PASSWORD and
1030 					 * ENCRYPTED PASSWORD.
1031 					 */
1032 					$$ = makeDefElem("password",
1033 									 (Node *)makeString($3), @1);
1034 				}
1035 			| UNENCRYPTED PASSWORD Sconst
1036 				{
1037 					ereport(ERROR,
1038 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1039 							 errmsg("UNENCRYPTED PASSWORD is no longer supported"),
1040 							 errhint("Remove UNENCRYPTED to store the password in encrypted form instead."),
1041 							 parser_errposition(@1)));
1042 				}
1043 			| INHERIT
1044 				{
1045 					$$ = makeDefElem("inherit", (Node *)makeInteger(TRUE), @1);
1046 				}
1047 			| CONNECTION LIMIT SignedIconst
1048 				{
1049 					$$ = makeDefElem("connectionlimit", (Node *)makeInteger($3), @1);
1050 				}
1051 			| VALID UNTIL Sconst
1052 				{
1053 					$$ = makeDefElem("validUntil", (Node *)makeString($3), @1);
1054 				}
1055 		/*	Supported but not documented for roles, for use by ALTER GROUP. */
1056 			| USER role_list
1057 				{
1058 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1059 				}
1060 			| IDENT
1061 				{
1062 					/*
1063 					 * We handle identifiers that aren't parser keywords with
1064 					 * the following special-case codes, to avoid bloating the
1065 					 * size of the main parser.
1066 					 */
1067 					if (strcmp($1, "superuser") == 0)
1068 						$$ = makeDefElem("superuser", (Node *)makeInteger(TRUE), @1);
1069 					else if (strcmp($1, "nosuperuser") == 0)
1070 						$$ = makeDefElem("superuser", (Node *)makeInteger(FALSE), @1);
1071 					else if (strcmp($1, "createrole") == 0)
1072 						$$ = makeDefElem("createrole", (Node *)makeInteger(TRUE), @1);
1073 					else if (strcmp($1, "nocreaterole") == 0)
1074 						$$ = makeDefElem("createrole", (Node *)makeInteger(FALSE), @1);
1075 					else if (strcmp($1, "replication") == 0)
1076 						$$ = makeDefElem("isreplication", (Node *)makeInteger(TRUE), @1);
1077 					else if (strcmp($1, "noreplication") == 0)
1078 						$$ = makeDefElem("isreplication", (Node *)makeInteger(FALSE), @1);
1079 					else if (strcmp($1, "createdb") == 0)
1080 						$$ = makeDefElem("createdb", (Node *)makeInteger(TRUE), @1);
1081 					else if (strcmp($1, "nocreatedb") == 0)
1082 						$$ = makeDefElem("createdb", (Node *)makeInteger(FALSE), @1);
1083 					else if (strcmp($1, "login") == 0)
1084 						$$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE), @1);
1085 					else if (strcmp($1, "nologin") == 0)
1086 						$$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE), @1);
1087 					else if (strcmp($1, "bypassrls") == 0)
1088 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(TRUE), @1);
1089 					else if (strcmp($1, "nobypassrls") == 0)
1090 						$$ = makeDefElem("bypassrls", (Node *)makeInteger(FALSE), @1);
1091 					else if (strcmp($1, "noinherit") == 0)
1092 					{
1093 						/*
1094 						 * Note that INHERIT is a keyword, so it's handled by main parser, but
1095 						 * NOINHERIT is handled here.
1096 						 */
1097 						$$ = makeDefElem("inherit", (Node *)makeInteger(FALSE), @1);
1098 					}
1099 					else
1100 						ereport(ERROR,
1101 								(errcode(ERRCODE_SYNTAX_ERROR),
1102 								 errmsg("unrecognized role option \"%s\"", $1),
1103 									 parser_errposition(@1)));
1104 				}
1105 		;
1106 
1107 CreateOptRoleElem:
1108 			AlterOptRoleElem			{ $$ = $1; }
1109 			/* The following are not supported by ALTER ROLE/USER/GROUP */
1110 			| SYSID Iconst
1111 				{
1112 					$$ = makeDefElem("sysid", (Node *)makeInteger($2), @1);
1113 				}
1114 			| ADMIN role_list
1115 				{
1116 					$$ = makeDefElem("adminmembers", (Node *)$2, @1);
1117 				}
1118 			| ROLE role_list
1119 				{
1120 					$$ = makeDefElem("rolemembers", (Node *)$2, @1);
1121 				}
1122 			| IN_P ROLE role_list
1123 				{
1124 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1125 				}
1126 			| IN_P GROUP_P role_list
1127 				{
1128 					$$ = makeDefElem("addroleto", (Node *)$3, @1);
1129 				}
1130 		;
1131 
1132 
1133 /*****************************************************************************
1134  *
1135  * Create a new Postgres DBMS user (role with implied login ability)
1136  *
1137  *****************************************************************************/
1138 
1139 CreateUserStmt:
1140 			CREATE USER RoleId opt_with OptRoleList
1141 				{
1142 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1143 					n->stmt_type = ROLESTMT_USER;
1144 					n->role = $3;
1145 					n->options = $5;
1146 					$$ = (Node *)n;
1147 				}
1148 		;
1149 
1150 
1151 /*****************************************************************************
1152  *
1153  * Alter a postgresql DBMS role
1154  *
1155  *****************************************************************************/
1156 
1157 AlterRoleStmt:
1158 			ALTER ROLE RoleSpec opt_with AlterOptRoleList
1159 				 {
1160 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1161 					n->role = $3;
1162 					n->action = +1;	/* add, if there are members */
1163 					n->options = $5;
1164 					$$ = (Node *)n;
1165 				 }
1166 			| ALTER USER RoleSpec opt_with AlterOptRoleList
1167 				 {
1168 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1169 					n->role = $3;
1170 					n->action = +1;	/* add, if there are members */
1171 					n->options = $5;
1172 					$$ = (Node *)n;
1173 				 }
1174 		;
1175 
1176 opt_in_database:
1177 			   /* EMPTY */					{ $$ = NULL; }
1178 			| IN_P DATABASE database_name	{ $$ = $3; }
1179 		;
1180 
1181 AlterRoleSetStmt:
1182 			ALTER ROLE RoleSpec opt_in_database SetResetClause
1183 				{
1184 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1185 					n->role = $3;
1186 					n->database = $4;
1187 					n->setstmt = $5;
1188 					$$ = (Node *)n;
1189 				}
1190 			| ALTER ROLE ALL opt_in_database SetResetClause
1191 				{
1192 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1193 					n->role = NULL;
1194 					n->database = $4;
1195 					n->setstmt = $5;
1196 					$$ = (Node *)n;
1197 				}
1198 			| ALTER USER RoleSpec opt_in_database SetResetClause
1199 				{
1200 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1201 					n->role = $3;
1202 					n->database = $4;
1203 					n->setstmt = $5;
1204 					$$ = (Node *)n;
1205 				}
1206 			| ALTER USER ALL opt_in_database SetResetClause
1207 				{
1208 					AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1209 					n->role = NULL;
1210 					n->database = $4;
1211 					n->setstmt = $5;
1212 					$$ = (Node *)n;
1213 				}
1214 		;
1215 
1216 
1217 /*****************************************************************************
1218  *
1219  * Drop a postgresql DBMS role
1220  *
1221  * XXX Ideally this would have CASCADE/RESTRICT options, but a role
1222  * might own objects in multiple databases, and there is presently no way to
1223  * implement cascading to other databases.  So we always behave as RESTRICT.
1224  *****************************************************************************/
1225 
1226 DropRoleStmt:
1227 			DROP ROLE role_list
1228 				{
1229 					DropRoleStmt *n = makeNode(DropRoleStmt);
1230 					n->missing_ok = FALSE;
1231 					n->roles = $3;
1232 					$$ = (Node *)n;
1233 				}
1234 			| DROP ROLE IF_P EXISTS role_list
1235 				{
1236 					DropRoleStmt *n = makeNode(DropRoleStmt);
1237 					n->missing_ok = TRUE;
1238 					n->roles = $5;
1239 					$$ = (Node *)n;
1240 				}
1241 			| DROP USER role_list
1242 				{
1243 					DropRoleStmt *n = makeNode(DropRoleStmt);
1244 					n->missing_ok = FALSE;
1245 					n->roles = $3;
1246 					$$ = (Node *)n;
1247 				}
1248 			| DROP USER IF_P EXISTS role_list
1249 				{
1250 					DropRoleStmt *n = makeNode(DropRoleStmt);
1251 					n->roles = $5;
1252 					n->missing_ok = TRUE;
1253 					$$ = (Node *)n;
1254 				}
1255 			| DROP GROUP_P role_list
1256 				{
1257 					DropRoleStmt *n = makeNode(DropRoleStmt);
1258 					n->missing_ok = FALSE;
1259 					n->roles = $3;
1260 					$$ = (Node *)n;
1261 				}
1262 			| DROP GROUP_P IF_P EXISTS role_list
1263 				{
1264 					DropRoleStmt *n = makeNode(DropRoleStmt);
1265 					n->missing_ok = TRUE;
1266 					n->roles = $5;
1267 					$$ = (Node *)n;
1268 				}
1269 			;
1270 
1271 
1272 /*****************************************************************************
1273  *
1274  * Create a postgresql group (role without login ability)
1275  *
1276  *****************************************************************************/
1277 
1278 CreateGroupStmt:
1279 			CREATE GROUP_P RoleId opt_with OptRoleList
1280 				{
1281 					CreateRoleStmt *n = makeNode(CreateRoleStmt);
1282 					n->stmt_type = ROLESTMT_GROUP;
1283 					n->role = $3;
1284 					n->options = $5;
1285 					$$ = (Node *)n;
1286 				}
1287 		;
1288 
1289 
1290 /*****************************************************************************
1291  *
1292  * Alter a postgresql group
1293  *
1294  *****************************************************************************/
1295 
1296 AlterGroupStmt:
1297 			ALTER GROUP_P RoleSpec add_drop USER role_list
1298 				{
1299 					AlterRoleStmt *n = makeNode(AlterRoleStmt);
1300 					n->role = $3;
1301 					n->action = $4;
1302 					n->options = list_make1(makeDefElem("rolemembers",
1303 														(Node *)$6, @6));
1304 					$$ = (Node *)n;
1305 				}
1306 		;
1307 
1308 add_drop:	ADD_P									{ $$ = +1; }
1309 			| DROP									{ $$ = -1; }
1310 		;
1311 
1312 
1313 /*****************************************************************************
1314  *
1315  * Manipulate a schema
1316  *
1317  *****************************************************************************/
1318 
1319 CreateSchemaStmt:
1320 			CREATE SCHEMA OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1321 				{
1322 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1323 					/* One can omit the schema name or the authorization id. */
1324 					n->schemaname = $3;
1325 					n->authrole = $5;
1326 					n->schemaElts = $6;
1327 					n->if_not_exists = false;
1328 					$$ = (Node *)n;
1329 				}
1330 			| CREATE SCHEMA ColId OptSchemaEltList
1331 				{
1332 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1333 					/* ...but not both */
1334 					n->schemaname = $3;
1335 					n->authrole = NULL;
1336 					n->schemaElts = $4;
1337 					n->if_not_exists = false;
1338 					$$ = (Node *)n;
1339 				}
1340 			| CREATE SCHEMA IF_P NOT EXISTS OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1341 				{
1342 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1343 					/* schema name can be omitted here, too */
1344 					n->schemaname = $6;
1345 					n->authrole = $8;
1346 					if ($9 != NIL)
1347 						ereport(ERROR,
1348 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1349 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1350 								 parser_errposition(@9)));
1351 					n->schemaElts = $9;
1352 					n->if_not_exists = true;
1353 					$$ = (Node *)n;
1354 				}
1355 			| CREATE SCHEMA IF_P NOT EXISTS ColId OptSchemaEltList
1356 				{
1357 					CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1358 					/* ...but not here */
1359 					n->schemaname = $6;
1360 					n->authrole = NULL;
1361 					if ($7 != NIL)
1362 						ereport(ERROR,
1363 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1364 								 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1365 								 parser_errposition(@7)));
1366 					n->schemaElts = $7;
1367 					n->if_not_exists = true;
1368 					$$ = (Node *)n;
1369 				}
1370 		;
1371 
1372 OptSchemaName:
1373 			ColId									{ $$ = $1; }
1374 			| /* EMPTY */							{ $$ = NULL; }
1375 		;
1376 
1377 OptSchemaEltList:
1378 			OptSchemaEltList schema_stmt
1379 				{
1380 					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
1381 						@$ = @2;
1382 					$$ = lappend($1, $2);
1383 				}
1384 			| /* EMPTY */
1385 				{ $$ = NIL; }
1386 		;
1387 
1388 /*
1389  *	schema_stmt are the ones that can show up inside a CREATE SCHEMA
1390  *	statement (in addition to by themselves).
1391  */
1392 schema_stmt:
1393 			CreateStmt
1394 			| IndexStmt
1395 			| CreateSeqStmt
1396 			| CreateTrigStmt
1397 			| GrantStmt
1398 			| ViewStmt
1399 		;
1400 
1401 
1402 /*****************************************************************************
1403  *
1404  * Set PG internal variable
1405  *	  SET name TO 'var_value'
1406  * Include SQL syntax (thomas 1997-10-22):
1407  *	  SET TIME ZONE 'var_value'
1408  *
1409  *****************************************************************************/
1410 
1411 VariableSetStmt:
1412 			PGPOOL SET generic_set
1413 				{
1414 					VariableSetStmt *n = $3;
1415 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep changes minumum */
1416 					n->is_local = false;
1417 					$$ = (Node *) n;
1418 				}
1419 			| SET set_rest
1420 				{
1421 					VariableSetStmt *n = $2;
1422 					n->is_local = false;
1423 					$$ = (Node *) n;
1424 				}
1425 			| SET LOCAL set_rest
1426 				{
1427 					VariableSetStmt *n = $3;
1428 					n->is_local = true;
1429 					$$ = (Node *) n;
1430 				}
1431 			| SET SESSION set_rest
1432 				{
1433 					VariableSetStmt *n = $3;
1434 					n->is_local = false;
1435 					$$ = (Node *) n;
1436 				}
1437 		;
1438 
1439 set_rest:
1440 			TRANSACTION transaction_mode_list
1441 				{
1442 					VariableSetStmt *n = makeNode(VariableSetStmt);
1443 					n->kind = VAR_SET_MULTI;
1444 					n->name = "TRANSACTION";
1445 					n->args = $2;
1446 					$$ = n;
1447 				}
1448 			| SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1449 				{
1450 					VariableSetStmt *n = makeNode(VariableSetStmt);
1451 					n->kind = VAR_SET_MULTI;
1452 					n->name = "SESSION CHARACTERISTICS";
1453 					n->args = $5;
1454 					$$ = n;
1455 				}
1456 			| set_rest_more
1457 			;
1458 
1459 generic_set:
1460 			var_name TO var_list
1461 				{
1462 					VariableSetStmt *n = makeNode(VariableSetStmt);
1463 					n->kind = VAR_SET_VALUE;
1464 					n->name = $1;
1465 					n->args = $3;
1466 					$$ = n;
1467 				}
1468 			| var_name '=' var_list
1469 				{
1470 					VariableSetStmt *n = makeNode(VariableSetStmt);
1471 					n->kind = VAR_SET_VALUE;
1472 					n->name = $1;
1473 					n->args = $3;
1474 					$$ = n;
1475 				}
1476 			| var_name TO DEFAULT
1477 				{
1478 					VariableSetStmt *n = makeNode(VariableSetStmt);
1479 					n->kind = VAR_SET_DEFAULT;
1480 					n->name = $1;
1481 					$$ = n;
1482 				}
1483 			| var_name '=' DEFAULT
1484 				{
1485 					VariableSetStmt *n = makeNode(VariableSetStmt);
1486 					n->kind = VAR_SET_DEFAULT;
1487 					n->name = $1;
1488 					$$ = n;
1489 				}
1490 
1491 set_rest_more:	/* Generic SET syntaxes: */
1492 			generic_set 						{$$ = $1;}
1493 			| var_name FROM CURRENT_P
1494 				{
1495 					VariableSetStmt *n = makeNode(VariableSetStmt);
1496 					n->kind = VAR_SET_CURRENT;
1497 					n->name = $1;
1498 					$$ = n;
1499 				}
1500 			/* Special syntaxes mandated by SQL standard: */
1501 			| TIME ZONE zone_value
1502 				{
1503 					VariableSetStmt *n = makeNode(VariableSetStmt);
1504 					n->kind = VAR_SET_VALUE;
1505 					n->name = "timezone";
1506 					if ($3 != NULL)
1507 						n->args = list_make1($3);
1508 					else
1509 						n->kind = VAR_SET_DEFAULT;
1510 					$$ = n;
1511 				}
1512 			| CATALOG_P Sconst
1513 				{
1514 					ereport(ERROR,
1515 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1516 							 errmsg("current database cannot be changed"),
1517 							 parser_errposition(@2)));
1518 					$$ = NULL; /*not reached*/
1519 				}
1520 			| SCHEMA Sconst
1521 				{
1522 					VariableSetStmt *n = makeNode(VariableSetStmt);
1523 					n->kind = VAR_SET_VALUE;
1524 					n->name = "search_path";
1525 					n->args = list_make1(makeStringConst($2, @2));
1526 					$$ = n;
1527 				}
1528 			| NAMES opt_encoding
1529 				{
1530 					VariableSetStmt *n = makeNode(VariableSetStmt);
1531 					n->kind = VAR_SET_VALUE;
1532 					n->name = "client_encoding";
1533 					if ($2 != NULL)
1534 						n->args = list_make1(makeStringConst($2, @2));
1535 					else
1536 						n->kind = VAR_SET_DEFAULT;
1537 					$$ = n;
1538 				}
1539 			| ROLE NonReservedWord_or_Sconst
1540 				{
1541 					VariableSetStmt *n = makeNode(VariableSetStmt);
1542 					n->kind = VAR_SET_VALUE;
1543 					n->name = "role";
1544 					n->args = list_make1(makeStringConst($2, @2));
1545 					$$ = n;
1546 				}
1547 			| SESSION AUTHORIZATION NonReservedWord_or_Sconst
1548 				{
1549 					VariableSetStmt *n = makeNode(VariableSetStmt);
1550 					n->kind = VAR_SET_VALUE;
1551 					n->name = "session_authorization";
1552 					n->args = list_make1(makeStringConst($3, @3));
1553 					$$ = n;
1554 				}
1555 			| SESSION AUTHORIZATION DEFAULT
1556 				{
1557 					VariableSetStmt *n = makeNode(VariableSetStmt);
1558 					n->kind = VAR_SET_DEFAULT;
1559 					n->name = "session_authorization";
1560 					$$ = n;
1561 				}
1562 			| XML_P OPTION document_or_content
1563 				{
1564 					VariableSetStmt *n = makeNode(VariableSetStmt);
1565 					n->kind = VAR_SET_VALUE;
1566 					n->name = "xmloption";
1567 					n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1568 					$$ = n;
1569 				}
1570 			/* Special syntaxes invented by PostgreSQL: */
1571 			| TRANSACTION SNAPSHOT Sconst
1572 				{
1573 					VariableSetStmt *n = makeNode(VariableSetStmt);
1574 					n->kind = VAR_SET_MULTI;
1575 					n->name = "TRANSACTION SNAPSHOT";
1576 					n->args = list_make1(makeStringConst($3, @3));
1577 					$$ = n;
1578 				}
1579 		;
1580 
1581 var_name:	ColId								{ $$ = $1; }
1582 			| var_name '.' ColId
1583 				{ $$ = psprintf("%s.%s", $1, $3); }
1584 		;
1585 
1586 var_list:	var_value								{ $$ = list_make1($1); }
1587 			| var_list ',' var_value				{ $$ = lappend($1, $3); }
1588 		;
1589 
1590 var_value:	opt_boolean_or_string
1591 				{ $$ = makeStringConst($1, @1); }
1592 			| NumericOnly
1593 				{ $$ = makeAConst($1, @1); }
1594 		;
1595 
1596 iso_level:	READ UNCOMMITTED						{ $$ = "read uncommitted"; }
1597 			| READ COMMITTED						{ $$ = "read committed"; }
1598 			| REPEATABLE READ						{ $$ = "repeatable read"; }
1599 			| SERIALIZABLE							{ $$ = "serializable"; }
1600 		;
1601 
1602 opt_boolean_or_string:
1603 			TRUE_P									{ $$ = "true"; }
1604 			| FALSE_P								{ $$ = "false"; }
1605 			| ON									{ $$ = "on"; }
1606 			/*
1607 			 * OFF is also accepted as a boolean value, but is handled by
1608 			 * the NonReservedWord rule.  The action for booleans and strings
1609 			 * is the same, so we don't need to distinguish them here.
1610 			 */
1611 			| NonReservedWord_or_Sconst				{ $$ = $1; }
1612 		;
1613 
1614 /* Timezone values can be:
1615  * - a string such as 'pst8pdt'
1616  * - an identifier such as "pst8pdt"
1617  * - an integer or floating point number
1618  * - a time interval per SQL99
1619  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1620  * so use IDENT (meaning we reject anything that is a key word).
1621  */
1622 zone_value:
1623 			Sconst
1624 				{
1625 					$$ = makeStringConst($1, @1);
1626 				}
1627 			| IDENT
1628 				{
1629 					$$ = makeStringConst($1, @1);
1630 				}
1631 			| ConstInterval Sconst opt_interval
1632 				{
1633 					TypeName *t = $1;
1634 					if ($3 != NIL)
1635 					{
1636 						A_Const *n = (A_Const *) linitial($3);
1637 						if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1638 							ereport(ERROR,
1639 									(errcode(ERRCODE_SYNTAX_ERROR),
1640 									 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1641 									 parser_errposition(@3)));
1642 					}
1643 					t->typmods = $3;
1644 					$$ = makeStringConstCast($2, @2, t);
1645 				}
1646 			| ConstInterval '(' Iconst ')' Sconst
1647 				{
1648 					TypeName *t = $1;
1649 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1650 											makeIntConst($3, @3));
1651 					$$ = makeStringConstCast($5, @5, t);
1652 				}
1653 			| NumericOnly							{ $$ = makeAConst($1, @1); }
1654 			| DEFAULT								{ $$ = NULL; }
1655 			| LOCAL									{ $$ = NULL; }
1656 		;
1657 
1658 opt_encoding:
1659 			Sconst									{ $$ = $1; }
1660 			| DEFAULT								{ $$ = NULL; }
1661 			| /*EMPTY*/								{ $$ = NULL; }
1662 		;
1663 
1664 NonReservedWord_or_Sconst:
1665 			NonReservedWord							{ $$ = $1; }
1666 			| Sconst								{ $$ = $1; }
1667 		;
1668 
1669 VariableResetStmt:
1670 			RESET reset_rest						{ $$ = (Node *) $2; }
1671 			| PGPOOL RESET generic_reset
1672 				{
1673 					VariableSetStmt *n = $3;
1674 					n->type = T_PgpoolVariableSetStmt; /* Hack to keep the changes minumum */
1675 					$$ = (Node *) n;
1676 				}
1677 		;
1678 
1679 reset_rest:
1680 			generic_reset							{ $$ = $1; }
1681 			| TIME ZONE
1682 				{
1683 					VariableSetStmt *n = makeNode(VariableSetStmt);
1684 					n->kind = VAR_RESET;
1685 					n->name = "timezone";
1686 					$$ = n;
1687 				}
1688 			| TRANSACTION ISOLATION LEVEL
1689 				{
1690 					VariableSetStmt *n = makeNode(VariableSetStmt);
1691 					n->kind = VAR_RESET;
1692 					n->name = "transaction_isolation";
1693 					$$ = n;
1694 				}
1695 			| SESSION AUTHORIZATION
1696 				{
1697 					VariableSetStmt *n = makeNode(VariableSetStmt);
1698 					n->kind = VAR_RESET;
1699 					n->name = "session_authorization";
1700 					$$ = n;
1701 				}
1702 		;
1703 
1704 generic_reset:
1705 			var_name
1706 				{
1707 					VariableSetStmt *n = makeNode(VariableSetStmt);
1708 					n->kind = VAR_RESET;
1709 					n->name = $1;
1710 					$$ = n;
1711 				}
1712 			| ALL
1713 				{
1714 					VariableSetStmt *n = makeNode(VariableSetStmt);
1715 					n->kind = VAR_RESET_ALL;
1716 					$$ = n;
1717 				}
1718 		;
1719 
1720 /* SetResetClause allows SET or RESET without LOCAL */
1721 SetResetClause:
1722 			SET set_rest					{ $$ = $2; }
1723 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1724 		;
1725 
1726 /* SetResetClause allows SET or RESET without LOCAL */
1727 FunctionSetResetClause:
1728 			SET set_rest_more				{ $$ = $2; }
1729 			| VariableResetStmt				{ $$ = (VariableSetStmt *) $1; }
1730 		;
1731 
1732 
1733 VariableShowStmt:
1734 			/* pgpool extension */
1735 			PGPOOL SHOW var_name
1736 			{
1737 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1738 				n->name = $3;
1739 				$$ = (Node *) n;
1740 			}
1741 			| PGPOOL SHOW ALL
1742 			{
1743 				VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1744 				n->name = "all";
1745 				$$ = (Node *) n;
1746 			}
1747 			| SHOW var_name
1748 				{
1749 					VariableShowStmt *n = makeNode(VariableShowStmt);
1750 					n->name = $2;
1751 					$$ = (Node *) n;
1752 				}
1753 			| SHOW TIME ZONE
1754 				{
1755 					VariableShowStmt *n = makeNode(VariableShowStmt);
1756 					n->name = "timezone";
1757 					$$ = (Node *) n;
1758 				}
1759 			| SHOW TRANSACTION ISOLATION LEVEL
1760 				{
1761 					VariableShowStmt *n = makeNode(VariableShowStmt);
1762 					n->name = "transaction_isolation";
1763 					$$ = (Node *) n;
1764 				}
1765 			| SHOW SESSION AUTHORIZATION
1766 				{
1767 					VariableShowStmt *n = makeNode(VariableShowStmt);
1768 					n->name = "session_authorization";
1769 					$$ = (Node *) n;
1770 				}
1771 			| SHOW ALL
1772 				{
1773 					VariableShowStmt *n = makeNode(VariableShowStmt);
1774 					n->name = "all";
1775 					$$ = (Node *) n;
1776 				}
1777 		;
1778 
1779 
1780 ConstraintsSetStmt:
1781 			SET CONSTRAINTS constraints_set_list constraints_set_mode
1782 				{
1783 					ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1784 					n->constraints = $3;
1785 					n->deferred = $4;
1786 					$$ = (Node *) n;
1787 				}
1788 		;
1789 
1790 constraints_set_list:
1791 			ALL										{ $$ = NIL; }
1792 			| qualified_name_list					{ $$ = $1; }
1793 		;
1794 
1795 constraints_set_mode:
1796 			DEFERRED								{ $$ = TRUE; }
1797 			| IMMEDIATE								{ $$ = FALSE; }
1798 		;
1799 
1800 
1801 /*
1802  * Checkpoint statement
1803  */
1804 CheckPointStmt:
1805 			CHECKPOINT
1806 				{
1807 					CheckPointStmt *n = makeNode(CheckPointStmt);
1808 					$$ = (Node *)n;
1809 				}
1810 		;
1811 
1812 
1813 /*****************************************************************************
1814  *
1815  * DISCARD { ALL | TEMP | PLANS | SEQUENCES }
1816  *
1817  *****************************************************************************/
1818 
1819 DiscardStmt:
1820 			DISCARD ALL
1821 				{
1822 					DiscardStmt *n = makeNode(DiscardStmt);
1823 					n->target = DISCARD_ALL;
1824 					$$ = (Node *) n;
1825 				}
1826 			| DISCARD TEMP
1827 				{
1828 					DiscardStmt *n = makeNode(DiscardStmt);
1829 					n->target = DISCARD_TEMP;
1830 					$$ = (Node *) n;
1831 				}
1832 			| DISCARD TEMPORARY
1833 				{
1834 					DiscardStmt *n = makeNode(DiscardStmt);
1835 					n->target = DISCARD_TEMP;
1836 					$$ = (Node *) n;
1837 				}
1838 			| DISCARD PLANS
1839 				{
1840 					DiscardStmt *n = makeNode(DiscardStmt);
1841 					n->target = DISCARD_PLANS;
1842 					$$ = (Node *) n;
1843 				}
1844 			| DISCARD SEQUENCES
1845 				{
1846 					DiscardStmt *n = makeNode(DiscardStmt);
1847 					n->target = DISCARD_SEQUENCES;
1848 					$$ = (Node *) n;
1849 				}
1850 
1851 		;
1852 
1853 
1854 /*****************************************************************************
1855  *
1856  *	ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW ] variations
1857  *
1858  * Note: we accept all subcommands for each of the five variants, and sort
1859  * out what's really legal at execution time.
1860  *****************************************************************************/
1861 
1862 AlterTableStmt:
1863 			ALTER TABLE relation_expr alter_table_cmds
1864 				{
1865 					AlterTableStmt *n = makeNode(AlterTableStmt);
1866 					n->relation = $3;
1867 					n->cmds = $4;
1868 					n->relkind = OBJECT_TABLE;
1869 					n->missing_ok = false;
1870 					$$ = (Node *)n;
1871 				}
1872 		|	ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
1873 				{
1874 					AlterTableStmt *n = makeNode(AlterTableStmt);
1875 					n->relation = $5;
1876 					n->cmds = $6;
1877 					n->relkind = OBJECT_TABLE;
1878 					n->missing_ok = true;
1879 					$$ = (Node *)n;
1880 				}
1881 		|	ALTER TABLE relation_expr partition_cmd
1882 				{
1883 					AlterTableStmt *n = makeNode(AlterTableStmt);
1884 					n->relation = $3;
1885 					n->cmds = list_make1($4);
1886 					n->relkind = OBJECT_TABLE;
1887 					n->missing_ok = false;
1888 					$$ = (Node *)n;
1889 				}
1890 		|	ALTER TABLE IF_P EXISTS relation_expr partition_cmd
1891 				{
1892 					AlterTableStmt *n = makeNode(AlterTableStmt);
1893 					n->relation = $5;
1894 					n->cmds = list_make1($6);
1895 					n->relkind = OBJECT_TABLE;
1896 					n->missing_ok = true;
1897 					$$ = (Node *)n;
1898 				}
1899 		|	ALTER TABLE ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1900 				{
1901 					AlterTableMoveAllStmt *n =
1902 						makeNode(AlterTableMoveAllStmt);
1903 					n->orig_tablespacename = $6;
1904 					n->objtype = OBJECT_TABLE;
1905 					n->roles = NIL;
1906 					n->new_tablespacename = $9;
1907 					n->nowait = $10;
1908 					$$ = (Node *)n;
1909 				}
1910 		|	ALTER TABLE ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1911 				{
1912 					AlterTableMoveAllStmt *n =
1913 						makeNode(AlterTableMoveAllStmt);
1914 					n->orig_tablespacename = $6;
1915 					n->objtype = OBJECT_TABLE;
1916 					n->roles = $9;
1917 					n->new_tablespacename = $12;
1918 					n->nowait = $13;
1919 					$$ = (Node *)n;
1920 				}
1921 		|	ALTER INDEX qualified_name alter_table_cmds
1922 				{
1923 					AlterTableStmt *n = makeNode(AlterTableStmt);
1924 					n->relation = $3;
1925 					n->cmds = $4;
1926 					n->relkind = OBJECT_INDEX;
1927 					n->missing_ok = false;
1928 					$$ = (Node *)n;
1929 				}
1930 		|	ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
1931 				{
1932 					AlterTableStmt *n = makeNode(AlterTableStmt);
1933 					n->relation = $5;
1934 					n->cmds = $6;
1935 					n->relkind = OBJECT_INDEX;
1936 					n->missing_ok = true;
1937 					$$ = (Node *)n;
1938 				}
1939 		|	ALTER INDEX ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1940 				{
1941 					AlterTableMoveAllStmt *n =
1942 						makeNode(AlterTableMoveAllStmt);
1943 					n->orig_tablespacename = $6;
1944 					n->objtype = OBJECT_INDEX;
1945 					n->roles = NIL;
1946 					n->new_tablespacename = $9;
1947 					n->nowait = $10;
1948 					$$ = (Node *)n;
1949 				}
1950 		|	ALTER INDEX ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1951 				{
1952 					AlterTableMoveAllStmt *n =
1953 						makeNode(AlterTableMoveAllStmt);
1954 					n->orig_tablespacename = $6;
1955 					n->objtype = OBJECT_INDEX;
1956 					n->roles = $9;
1957 					n->new_tablespacename = $12;
1958 					n->nowait = $13;
1959 					$$ = (Node *)n;
1960 				}
1961 		|	ALTER SEQUENCE qualified_name alter_table_cmds
1962 				{
1963 					AlterTableStmt *n = makeNode(AlterTableStmt);
1964 					n->relation = $3;
1965 					n->cmds = $4;
1966 					n->relkind = OBJECT_SEQUENCE;
1967 					n->missing_ok = false;
1968 					$$ = (Node *)n;
1969 				}
1970 		|	ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
1971 				{
1972 					AlterTableStmt *n = makeNode(AlterTableStmt);
1973 					n->relation = $5;
1974 					n->cmds = $6;
1975 					n->relkind = OBJECT_SEQUENCE;
1976 					n->missing_ok = true;
1977 					$$ = (Node *)n;
1978 				}
1979 		|	ALTER VIEW qualified_name alter_table_cmds
1980 				{
1981 					AlterTableStmt *n = makeNode(AlterTableStmt);
1982 					n->relation = $3;
1983 					n->cmds = $4;
1984 					n->relkind = OBJECT_VIEW;
1985 					n->missing_ok = false;
1986 					$$ = (Node *)n;
1987 				}
1988 		|	ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
1989 				{
1990 					AlterTableStmt *n = makeNode(AlterTableStmt);
1991 					n->relation = $5;
1992 					n->cmds = $6;
1993 					n->relkind = OBJECT_VIEW;
1994 					n->missing_ok = true;
1995 					$$ = (Node *)n;
1996 				}
1997 		|	ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
1998 				{
1999 					AlterTableStmt *n = makeNode(AlterTableStmt);
2000 					n->relation = $4;
2001 					n->cmds = $5;
2002 					n->relkind = OBJECT_MATVIEW;
2003 					n->missing_ok = false;
2004 					$$ = (Node *)n;
2005 				}
2006 		|	ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
2007 				{
2008 					AlterTableStmt *n = makeNode(AlterTableStmt);
2009 					n->relation = $6;
2010 					n->cmds = $7;
2011 					n->relkind = OBJECT_MATVIEW;
2012 					n->missing_ok = true;
2013 					$$ = (Node *)n;
2014 				}
2015 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
2016 				{
2017 					AlterTableMoveAllStmt *n =
2018 						makeNode(AlterTableMoveAllStmt);
2019 					n->orig_tablespacename = $7;
2020 					n->objtype = OBJECT_MATVIEW;
2021 					n->roles = NIL;
2022 					n->new_tablespacename = $10;
2023 					n->nowait = $11;
2024 					$$ = (Node *)n;
2025 				}
2026 		|	ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
2027 				{
2028 					AlterTableMoveAllStmt *n =
2029 						makeNode(AlterTableMoveAllStmt);
2030 					n->orig_tablespacename = $7;
2031 					n->objtype = OBJECT_MATVIEW;
2032 					n->roles = $10;
2033 					n->new_tablespacename = $13;
2034 					n->nowait = $14;
2035 					$$ = (Node *)n;
2036 				}
2037 		;
2038 
2039 alter_table_cmds:
2040 			alter_table_cmd							{ $$ = list_make1($1); }
2041 			| alter_table_cmds ',' alter_table_cmd	{ $$ = lappend($1, $3); }
2042 		;
2043 
2044 partition_cmd:
2045 			/* ALTER TABLE <name> ATTACH PARTITION <table_name> FOR VALUES */
2046 			ATTACH PARTITION qualified_name ForValues
2047 				{
2048 					AlterTableCmd *n = makeNode(AlterTableCmd);
2049 					PartitionCmd *cmd = makeNode(PartitionCmd);
2050 
2051 					n->subtype = AT_AttachPartition;
2052 					cmd->name = $3;
2053 					cmd->bound = $4;
2054 					n->def = (Node *) cmd;
2055 
2056 					$$ = (Node *) n;
2057 				}
2058 			/* ALTER TABLE <name> DETACH PARTITION <partition_name> */
2059 			| DETACH PARTITION qualified_name
2060 				{
2061 					AlterTableCmd *n = makeNode(AlterTableCmd);
2062 					PartitionCmd *cmd = makeNode(PartitionCmd);
2063 
2064 					n->subtype = AT_DetachPartition;
2065 					cmd->name = $3;
2066 					cmd->bound = NULL;
2067 					n->def = (Node *) cmd;
2068 
2069 					$$ = (Node *) n;
2070 				}
2071 		;
2072 
2073 alter_table_cmd:
2074 			/* ALTER TABLE <name> ADD <coldef> */
2075 			ADD_P columnDef
2076 				{
2077 					AlterTableCmd *n = makeNode(AlterTableCmd);
2078 					n->subtype = AT_AddColumn;
2079 					n->def = $2;
2080 					n->missing_ok = false;
2081 					$$ = (Node *)n;
2082 				}
2083 			/* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
2084 			| ADD_P IF_P NOT EXISTS columnDef
2085 				{
2086 					AlterTableCmd *n = makeNode(AlterTableCmd);
2087 					n->subtype = AT_AddColumn;
2088 					n->def = $5;
2089 					n->missing_ok = true;
2090 					$$ = (Node *)n;
2091 				}
2092 			/* ALTER TABLE <name> ADD COLUMN <coldef> */
2093 			| ADD_P COLUMN columnDef
2094 				{
2095 					AlterTableCmd *n = makeNode(AlterTableCmd);
2096 					n->subtype = AT_AddColumn;
2097 					n->def = $3;
2098 					n->missing_ok = false;
2099 					$$ = (Node *)n;
2100 				}
2101 			/* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
2102 			| ADD_P COLUMN IF_P NOT EXISTS columnDef
2103 				{
2104 					AlterTableCmd *n = makeNode(AlterTableCmd);
2105 					n->subtype = AT_AddColumn;
2106 					n->def = $6;
2107 					n->missing_ok = true;
2108 					$$ = (Node *)n;
2109 				}
2110 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
2111 			| ALTER opt_column ColId alter_column_default
2112 				{
2113 					AlterTableCmd *n = makeNode(AlterTableCmd);
2114 					n->subtype = AT_ColumnDefault;
2115 					n->name = $3;
2116 					n->def = $4;
2117 					$$ = (Node *)n;
2118 				}
2119 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
2120 			| ALTER opt_column ColId DROP NOT NULL_P
2121 				{
2122 					AlterTableCmd *n = makeNode(AlterTableCmd);
2123 					n->subtype = AT_DropNotNull;
2124 					n->name = $3;
2125 					$$ = (Node *)n;
2126 				}
2127 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
2128 			| ALTER opt_column ColId SET NOT NULL_P
2129 				{
2130 					AlterTableCmd *n = makeNode(AlterTableCmd);
2131 					n->subtype = AT_SetNotNull;
2132 					n->name = $3;
2133 					$$ = (Node *)n;
2134 				}
2135 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
2136 			| ALTER opt_column ColId SET STATISTICS SignedIconst
2137 				{
2138 					AlterTableCmd *n = makeNode(AlterTableCmd);
2139 					n->subtype = AT_SetStatistics;
2140 					n->name = $3;
2141 					n->def = (Node *) makeInteger($6);
2142 					$$ = (Node *)n;
2143 				}
2144 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
2145 			| ALTER opt_column ColId SET reloptions
2146 				{
2147 					AlterTableCmd *n = makeNode(AlterTableCmd);
2148 					n->subtype = AT_SetOptions;
2149 					n->name = $3;
2150 					n->def = (Node *) $5;
2151 					$$ = (Node *)n;
2152 				}
2153 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
2154 			| ALTER opt_column ColId RESET reloptions
2155 				{
2156 					AlterTableCmd *n = makeNode(AlterTableCmd);
2157 					n->subtype = AT_ResetOptions;
2158 					n->name = $3;
2159 					n->def = (Node *) $5;
2160 					$$ = (Node *)n;
2161 				}
2162 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
2163 			| ALTER opt_column ColId SET STORAGE ColId
2164 				{
2165 					AlterTableCmd *n = makeNode(AlterTableCmd);
2166 					n->subtype = AT_SetStorage;
2167 					n->name = $3;
2168 					n->def = (Node *) makeString($6);
2169 					$$ = (Node *)n;
2170 				}
2171 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> ADD GENERATED ... AS IDENTITY ... */
2172 			| ALTER opt_column ColId ADD_P GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
2173 				{
2174 					AlterTableCmd *n = makeNode(AlterTableCmd);
2175 					Constraint *c = makeNode(Constraint);
2176 
2177 					c->contype = CONSTR_IDENTITY;
2178 					c->generated_when = $6;
2179 					c->options = $9;
2180 					c->location = @5;
2181 
2182 					n->subtype = AT_AddIdentity;
2183 					n->name = $3;
2184 					n->def = (Node *) c;
2185 
2186 					$$ = (Node *)n;
2187 				}
2188 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> SET <sequence options>/RESET */
2189 			| ALTER opt_column ColId alter_identity_column_option_list
2190 				{
2191 					AlterTableCmd *n = makeNode(AlterTableCmd);
2192 					n->subtype = AT_SetIdentity;
2193 					n->name = $3;
2194 					n->def = (Node *) $4;
2195 					$$ = (Node *)n;
2196 				}
2197 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY */
2198 			| ALTER opt_column ColId DROP IDENTITY_P
2199 				{
2200 					AlterTableCmd *n = makeNode(AlterTableCmd);
2201 					n->subtype = AT_DropIdentity;
2202 					n->name = $3;
2203 					n->missing_ok = false;
2204 					$$ = (Node *)n;
2205 				}
2206 			/* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY IF EXISTS */
2207 			| ALTER opt_column ColId DROP IDENTITY_P IF_P EXISTS
2208 				{
2209 					AlterTableCmd *n = makeNode(AlterTableCmd);
2210 					n->subtype = AT_DropIdentity;
2211 					n->name = $3;
2212 					n->missing_ok = true;
2213 					$$ = (Node *)n;
2214 				}
2215 			/* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
2216 			| DROP opt_column IF_P EXISTS ColId opt_drop_behavior
2217 				{
2218 					AlterTableCmd *n = makeNode(AlterTableCmd);
2219 					n->subtype = AT_DropColumn;
2220 					n->name = $5;
2221 					n->behavior = $6;
2222 					n->missing_ok = TRUE;
2223 					$$ = (Node *)n;
2224 				}
2225 			/* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
2226 			| DROP opt_column ColId opt_drop_behavior
2227 				{
2228 					AlterTableCmd *n = makeNode(AlterTableCmd);
2229 					n->subtype = AT_DropColumn;
2230 					n->name = $3;
2231 					n->behavior = $4;
2232 					n->missing_ok = FALSE;
2233 					$$ = (Node *)n;
2234 				}
2235 			/*
2236 			 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
2237 			 *		[ USING <expression> ]
2238 			 */
2239 			| ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
2240 				{
2241 					AlterTableCmd *n = makeNode(AlterTableCmd);
2242 					ColumnDef *def = makeNode(ColumnDef);
2243 					n->subtype = AT_AlterColumnType;
2244 					n->name = $3;
2245 					n->def = (Node *) def;
2246 					/* We only use these fields of the ColumnDef node */
2247 					def->typeName = $6;
2248 					def->collClause = (CollateClause *) $7;
2249 					def->raw_default = $8;
2250 					def->location = @3;
2251 					$$ = (Node *)n;
2252 				}
2253 			/* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
2254 			| ALTER opt_column ColId alter_generic_options
2255 				{
2256 					AlterTableCmd *n = makeNode(AlterTableCmd);
2257 					n->subtype = AT_AlterColumnGenericOptions;
2258 					n->name = $3;
2259 					n->def = (Node *) $4;
2260 					$$ = (Node *)n;
2261 				}
2262 			/* ALTER TABLE <name> ADD CONSTRAINT ... */
2263 			| ADD_P TableConstraint
2264 				{
2265 					AlterTableCmd *n = makeNode(AlterTableCmd);
2266 					n->subtype = AT_AddConstraint;
2267 					n->def = $2;
2268 					n->missing_ok = false;
2269 					$$ = (Node *)n;
2270 				}
2271 			/* ALTER TABLE <name> ALTER CONSTRAINT ... */
2272 			| ALTER CONSTRAINT name ConstraintAttributeSpec
2273 				{
2274 					AlterTableCmd *n = makeNode(AlterTableCmd);
2275 					Constraint *c = makeNode(Constraint);
2276 					n->subtype = AT_AlterConstraint;
2277 					n->def = (Node *) c;
2278 					c->contype = CONSTR_FOREIGN; /* others not supported, yet */
2279 					c->conname = $3;
2280 					processCASbits($4, @4, "ALTER CONSTRAINT statement",
2281 									&c->deferrable,
2282 									&c->initdeferred,
2283 									NULL, NULL, yyscanner);
2284 					$$ = (Node *)n;
2285 				}
2286 			/* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
2287 			| VALIDATE CONSTRAINT name
2288 				{
2289 					AlterTableCmd *n = makeNode(AlterTableCmd);
2290 					n->subtype = AT_ValidateConstraint;
2291 					n->name = $3;
2292 					$$ = (Node *)n;
2293 				}
2294 			/* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
2295 			| DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
2296 				{
2297 					AlterTableCmd *n = makeNode(AlterTableCmd);
2298 					n->subtype = AT_DropConstraint;
2299 					n->name = $5;
2300 					n->behavior = $6;
2301 					n->missing_ok = TRUE;
2302 					$$ = (Node *)n;
2303 				}
2304 			/* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
2305 			| DROP CONSTRAINT name opt_drop_behavior
2306 				{
2307 					AlterTableCmd *n = makeNode(AlterTableCmd);
2308 					n->subtype = AT_DropConstraint;
2309 					n->name = $3;
2310 					n->behavior = $4;
2311 					n->missing_ok = FALSE;
2312 					$$ = (Node *)n;
2313 				}
2314 			/* ALTER TABLE <name> SET WITH OIDS  */
2315 			| SET WITH OIDS
2316 				{
2317 					AlterTableCmd *n = makeNode(AlterTableCmd);
2318 					n->subtype = AT_AddOids;
2319 					$$ = (Node *)n;
2320 				}
2321 			/* ALTER TABLE <name> SET WITHOUT OIDS  */
2322 			| SET WITHOUT OIDS
2323 				{
2324 					AlterTableCmd *n = makeNode(AlterTableCmd);
2325 					n->subtype = AT_DropOids;
2326 					$$ = (Node *)n;
2327 				}
2328 			/* ALTER TABLE <name> CLUSTER ON <indexname> */
2329 			| CLUSTER ON name
2330 				{
2331 					AlterTableCmd *n = makeNode(AlterTableCmd);
2332 					n->subtype = AT_ClusterOn;
2333 					n->name = $3;
2334 					$$ = (Node *)n;
2335 				}
2336 			/* ALTER TABLE <name> SET WITHOUT CLUSTER */
2337 			| SET WITHOUT CLUSTER
2338 				{
2339 					AlterTableCmd *n = makeNode(AlterTableCmd);
2340 					n->subtype = AT_DropCluster;
2341 					n->name = NULL;
2342 					$$ = (Node *)n;
2343 				}
2344 			/* ALTER TABLE <name> SET LOGGED  */
2345 			| SET LOGGED
2346 				{
2347 					AlterTableCmd *n = makeNode(AlterTableCmd);
2348 					n->subtype = AT_SetLogged;
2349 					$$ = (Node *)n;
2350 				}
2351 			/* ALTER TABLE <name> SET UNLOGGED  */
2352 			| SET UNLOGGED
2353 				{
2354 					AlterTableCmd *n = makeNode(AlterTableCmd);
2355 					n->subtype = AT_SetUnLogged;
2356 					$$ = (Node *)n;
2357 				}
2358 			/* ALTER TABLE <name> ENABLE TRIGGER <trig> */
2359 			| ENABLE_P TRIGGER name
2360 				{
2361 					AlterTableCmd *n = makeNode(AlterTableCmd);
2362 					n->subtype = AT_EnableTrig;
2363 					n->name = $3;
2364 					$$ = (Node *)n;
2365 				}
2366 			/* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
2367 			| ENABLE_P ALWAYS TRIGGER name
2368 				{
2369 					AlterTableCmd *n = makeNode(AlterTableCmd);
2370 					n->subtype = AT_EnableAlwaysTrig;
2371 					n->name = $4;
2372 					$$ = (Node *)n;
2373 				}
2374 			/* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
2375 			| ENABLE_P REPLICA TRIGGER name
2376 				{
2377 					AlterTableCmd *n = makeNode(AlterTableCmd);
2378 					n->subtype = AT_EnableReplicaTrig;
2379 					n->name = $4;
2380 					$$ = (Node *)n;
2381 				}
2382 			/* ALTER TABLE <name> ENABLE TRIGGER ALL */
2383 			| ENABLE_P TRIGGER ALL
2384 				{
2385 					AlterTableCmd *n = makeNode(AlterTableCmd);
2386 					n->subtype = AT_EnableTrigAll;
2387 					$$ = (Node *)n;
2388 				}
2389 			/* ALTER TABLE <name> ENABLE TRIGGER USER */
2390 			| ENABLE_P TRIGGER USER
2391 				{
2392 					AlterTableCmd *n = makeNode(AlterTableCmd);
2393 					n->subtype = AT_EnableTrigUser;
2394 					$$ = (Node *)n;
2395 				}
2396 			/* ALTER TABLE <name> DISABLE TRIGGER <trig> */
2397 			| DISABLE_P TRIGGER name
2398 				{
2399 					AlterTableCmd *n = makeNode(AlterTableCmd);
2400 					n->subtype = AT_DisableTrig;
2401 					n->name = $3;
2402 					$$ = (Node *)n;
2403 				}
2404 			/* ALTER TABLE <name> DISABLE TRIGGER ALL */
2405 			| DISABLE_P TRIGGER ALL
2406 				{
2407 					AlterTableCmd *n = makeNode(AlterTableCmd);
2408 					n->subtype = AT_DisableTrigAll;
2409 					$$ = (Node *)n;
2410 				}
2411 			/* ALTER TABLE <name> DISABLE TRIGGER USER */
2412 			| DISABLE_P TRIGGER USER
2413 				{
2414 					AlterTableCmd *n = makeNode(AlterTableCmd);
2415 					n->subtype = AT_DisableTrigUser;
2416 					$$ = (Node *)n;
2417 				}
2418 			/* ALTER TABLE <name> ENABLE RULE <rule> */
2419 			| ENABLE_P RULE name
2420 				{
2421 					AlterTableCmd *n = makeNode(AlterTableCmd);
2422 					n->subtype = AT_EnableRule;
2423 					n->name = $3;
2424 					$$ = (Node *)n;
2425 				}
2426 			/* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
2427 			| ENABLE_P ALWAYS RULE name
2428 				{
2429 					AlterTableCmd *n = makeNode(AlterTableCmd);
2430 					n->subtype = AT_EnableAlwaysRule;
2431 					n->name = $4;
2432 					$$ = (Node *)n;
2433 				}
2434 			/* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
2435 			| ENABLE_P REPLICA RULE name
2436 				{
2437 					AlterTableCmd *n = makeNode(AlterTableCmd);
2438 					n->subtype = AT_EnableReplicaRule;
2439 					n->name = $4;
2440 					$$ = (Node *)n;
2441 				}
2442 			/* ALTER TABLE <name> DISABLE RULE <rule> */
2443 			| DISABLE_P RULE name
2444 				{
2445 					AlterTableCmd *n = makeNode(AlterTableCmd);
2446 					n->subtype = AT_DisableRule;
2447 					n->name = $3;
2448 					$$ = (Node *)n;
2449 				}
2450 			/* ALTER TABLE <name> INHERIT <parent> */
2451 			| INHERIT qualified_name
2452 				{
2453 					AlterTableCmd *n = makeNode(AlterTableCmd);
2454 					n->subtype = AT_AddInherit;
2455 					n->def = (Node *) $2;
2456 					$$ = (Node *)n;
2457 				}
2458 			/* ALTER TABLE <name> NO INHERIT <parent> */
2459 			| NO INHERIT qualified_name
2460 				{
2461 					AlterTableCmd *n = makeNode(AlterTableCmd);
2462 					n->subtype = AT_DropInherit;
2463 					n->def = (Node *) $3;
2464 					$$ = (Node *)n;
2465 				}
2466 			/* ALTER TABLE <name> OF <type_name> */
2467 			| OF any_name
2468 				{
2469 					AlterTableCmd *n = makeNode(AlterTableCmd);
2470 					TypeName *def = makeTypeNameFromNameList($2);
2471 					def->location = @2;
2472 					n->subtype = AT_AddOf;
2473 					n->def = (Node *) def;
2474 					$$ = (Node *)n;
2475 				}
2476 			/* ALTER TABLE <name> NOT OF */
2477 			| NOT OF
2478 				{
2479 					AlterTableCmd *n = makeNode(AlterTableCmd);
2480 					n->subtype = AT_DropOf;
2481 					$$ = (Node *)n;
2482 				}
2483 			/* ALTER TABLE <name> OWNER TO RoleSpec */
2484 			| OWNER TO RoleSpec
2485 				{
2486 					AlterTableCmd *n = makeNode(AlterTableCmd);
2487 					n->subtype = AT_ChangeOwner;
2488 					n->newowner = $3;
2489 					$$ = (Node *)n;
2490 				}
2491 			/* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
2492 			| SET TABLESPACE name
2493 				{
2494 					AlterTableCmd *n = makeNode(AlterTableCmd);
2495 					n->subtype = AT_SetTableSpace;
2496 					n->name = $3;
2497 					$$ = (Node *)n;
2498 				}
2499 			/* ALTER TABLE <name> SET (...) */
2500 			| SET reloptions
2501 				{
2502 					AlterTableCmd *n = makeNode(AlterTableCmd);
2503 					n->subtype = AT_SetRelOptions;
2504 					n->def = (Node *)$2;
2505 					$$ = (Node *)n;
2506 				}
2507 			/* ALTER TABLE <name> RESET (...) */
2508 			| RESET reloptions
2509 				{
2510 					AlterTableCmd *n = makeNode(AlterTableCmd);
2511 					n->subtype = AT_ResetRelOptions;
2512 					n->def = (Node *)$2;
2513 					$$ = (Node *)n;
2514 				}
2515 			/* ALTER TABLE <name> REPLICA IDENTITY  */
2516 			| REPLICA IDENTITY_P replica_identity
2517 				{
2518 					AlterTableCmd *n = makeNode(AlterTableCmd);
2519 					n->subtype = AT_ReplicaIdentity;
2520 					n->def = $3;
2521 					$$ = (Node *)n;
2522 				}
2523 			/* ALTER TABLE <name> ENABLE ROW LEVEL SECURITY */
2524 			| ENABLE_P ROW LEVEL SECURITY
2525 				{
2526 					AlterTableCmd *n = makeNode(AlterTableCmd);
2527 					n->subtype = AT_EnableRowSecurity;
2528 					$$ = (Node *)n;
2529 				}
2530 			/* ALTER TABLE <name> DISABLE ROW LEVEL SECURITY */
2531 			| DISABLE_P ROW LEVEL SECURITY
2532 				{
2533 					AlterTableCmd *n = makeNode(AlterTableCmd);
2534 					n->subtype = AT_DisableRowSecurity;
2535 					$$ = (Node *)n;
2536 				}
2537 			/* ALTER TABLE <name> FORCE ROW LEVEL SECURITY */
2538 			| FORCE ROW LEVEL SECURITY
2539 				{
2540 					AlterTableCmd *n = makeNode(AlterTableCmd);
2541 					n->subtype = AT_ForceRowSecurity;
2542 					$$ = (Node *)n;
2543 				}
2544 			/* ALTER TABLE <name> NO FORCE ROW LEVEL SECURITY */
2545 			| NO FORCE ROW LEVEL SECURITY
2546 				{
2547 					AlterTableCmd *n = makeNode(AlterTableCmd);
2548 					n->subtype = AT_NoForceRowSecurity;
2549 					$$ = (Node *)n;
2550 				}
2551 			| alter_generic_options
2552 				{
2553 					AlterTableCmd *n = makeNode(AlterTableCmd);
2554 					n->subtype = AT_GenericOptions;
2555 					n->def = (Node *)$1;
2556 					$$ = (Node *) n;
2557 				}
2558 		;
2559 
2560 alter_column_default:
2561 			SET DEFAULT a_expr			{ $$ = $3; }
2562 			| DROP DEFAULT				{ $$ = NULL; }
2563 		;
2564 
2565 opt_drop_behavior:
2566 			CASCADE						{ $$ = DROP_CASCADE; }
2567 			| RESTRICT					{ $$ = DROP_RESTRICT; }
2568 			| /* EMPTY */				{ $$ = DROP_RESTRICT; /* default */ }
2569 		;
2570 
2571 opt_collate_clause:
2572 			COLLATE any_name
2573 				{
2574 					CollateClause *n = makeNode(CollateClause);
2575 					n->arg = NULL;
2576 					n->collname = $2;
2577 					n->location = @1;
2578 					$$ = (Node *) n;
2579 				}
2580 			| /* EMPTY */				{ $$ = NULL; }
2581 		;
2582 
2583 alter_using:
2584 			USING a_expr				{ $$ = $2; }
2585 			| /* EMPTY */				{ $$ = NULL; }
2586 		;
2587 
2588 replica_identity:
2589 			NOTHING
2590 				{
2591 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2592 					n->identity_type = REPLICA_IDENTITY_NOTHING;
2593 					n->name = NULL;
2594 					$$ = (Node *) n;
2595 				}
2596 			| FULL
2597 				{
2598 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2599 					n->identity_type = REPLICA_IDENTITY_FULL;
2600 					n->name = NULL;
2601 					$$ = (Node *) n;
2602 				}
2603 			| DEFAULT
2604 				{
2605 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2606 					n->identity_type = REPLICA_IDENTITY_DEFAULT;
2607 					n->name = NULL;
2608 					$$ = (Node *) n;
2609 				}
2610 			| USING INDEX name
2611 				{
2612 					ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2613 					n->identity_type = REPLICA_IDENTITY_INDEX;
2614 					n->name = $3;
2615 					$$ = (Node *) n;
2616 				}
2617 ;
2618 
2619 reloptions:
2620 			'(' reloption_list ')'					{ $$ = $2; }
2621 		;
2622 
2623 opt_reloptions:		WITH reloptions					{ $$ = $2; }
2624 			 |		/* EMPTY */						{ $$ = NIL; }
2625 		;
2626 
2627 reloption_list:
2628 			reloption_elem							{ $$ = list_make1($1); }
2629 			| reloption_list ',' reloption_elem		{ $$ = lappend($1, $3); }
2630 		;
2631 
2632 /* This should match def_elem and also allow qualified names */
2633 reloption_elem:
2634 			ColLabel '=' def_arg
2635 				{
2636 					$$ = makeDefElem($1, (Node *) $3, @1);
2637 				}
2638 			| ColLabel
2639 				{
2640 					$$ = makeDefElem($1, NULL, @1);
2641 				}
2642 			| ColLabel '.' ColLabel '=' def_arg
2643 				{
2644 					$$ = makeDefElemExtended($1, $3, (Node *) $5,
2645 											 DEFELEM_UNSPEC, @1);
2646 				}
2647 			| ColLabel '.' ColLabel
2648 				{
2649 					$$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC, @1);
2650 				}
2651 		;
2652 
2653 alter_identity_column_option_list:
2654 			alter_identity_column_option
2655 				{ $$ = list_make1($1); }
2656 			| alter_identity_column_option_list alter_identity_column_option
2657 				{ $$ = lappend($1, $2); }
2658 		;
2659 
2660 alter_identity_column_option:
2661 			RESTART
2662 				{
2663 					$$ = makeDefElem("restart", NULL, @1);
2664 				}
2665 			| RESTART opt_with NumericOnly
2666 				{
2667 					$$ = makeDefElem("restart", (Node *)$3, @1);
2668 				}
2669 			| SET SeqOptElem
2670 				{
2671 					if (strcmp($2->defname, "as") == 0 ||
2672 						strcmp($2->defname, "restart") == 0 ||
2673 						strcmp($2->defname, "owned_by") == 0)
2674 						ereport(ERROR,
2675 								(errcode(ERRCODE_SYNTAX_ERROR),
2676 								 errmsg("sequence option \"%s\" not supported here", $2->defname),
2677 								 parser_errposition(@2)));
2678 					$$ = $2;
2679 				}
2680 			| SET GENERATED generated_when
2681 				{
2682 					$$ = makeDefElem("generated", (Node *) makeInteger($3), @1);
2683 				}
2684 		;
2685 
2686 ForValues:
2687 			/* a LIST partition */
2688 			FOR VALUES IN_P '(' partbound_datum_list ')'
2689 				{
2690 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2691 
2692 					n->strategy = PARTITION_STRATEGY_LIST;
2693 					n->listdatums = $5;
2694 					n->location = @3;
2695 
2696 					$$ = n;
2697 				}
2698 
2699 			/* a RANGE partition */
2700 			| FOR VALUES FROM '(' range_datum_list ')' TO '(' range_datum_list ')'
2701 				{
2702 					PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2703 
2704 					n->strategy = PARTITION_STRATEGY_RANGE;
2705 					n->lowerdatums = $5;
2706 					n->upperdatums = $9;
2707 					n->location = @3;
2708 
2709 					$$ = n;
2710 				}
2711 		;
2712 
2713 partbound_datum:
2714 			Sconst			{ $$ = makeStringConst($1, @1); }
2715 			| NumericOnly	{ $$ = makeAConst($1, @1); }
2716 			| NULL_P		{ $$ = makeNullAConst(@1); }
2717 		;
2718 
2719 partbound_datum_list:
2720 			partbound_datum						{ $$ = list_make1($1); }
2721 			| partbound_datum_list ',' partbound_datum
2722 												{ $$ = lappend($1, $3); }
2723 		;
2724 
2725 range_datum_list:
2726 			PartitionRangeDatum					{ $$ = list_make1($1); }
2727 			| range_datum_list ',' PartitionRangeDatum
2728 												{ $$ = lappend($1, $3); }
2729 		;
2730 
2731 PartitionRangeDatum:
2732 			MINVALUE
2733 				{
2734 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2735 
2736 					n->kind = PARTITION_RANGE_DATUM_MINVALUE;
2737 					n->value = NULL;
2738 					n->location = @1;
2739 
2740 					$$ = (Node *) n;
2741 				}
2742 			| MAXVALUE
2743 				{
2744 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2745 
2746 					n->kind = PARTITION_RANGE_DATUM_MAXVALUE;
2747 					n->value = NULL;
2748 					n->location = @1;
2749 
2750 					$$ = (Node *) n;
2751 				}
2752 			| partbound_datum
2753 				{
2754 					PartitionRangeDatum *n = makeNode(PartitionRangeDatum);
2755 
2756 					n->kind = PARTITION_RANGE_DATUM_VALUE;
2757 					n->value = $1;
2758 					n->location = @1;
2759 
2760 					$$ = (Node *) n;
2761 				}
2762 		;
2763 
2764 /*****************************************************************************
2765  *
2766  *	ALTER TYPE
2767  *
2768  * really variants of the ALTER TABLE subcommands with different spellings
2769  *****************************************************************************/
2770 
2771 AlterCompositeTypeStmt:
2772 			ALTER TYPE_P any_name alter_type_cmds
2773 				{
2774 					AlterTableStmt *n = makeNode(AlterTableStmt);
2775 
2776 					/* can't use qualified_name, sigh */
2777 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
2778 					n->cmds = $4;
2779 					n->relkind = OBJECT_TYPE;
2780 					$$ = (Node *)n;
2781 				}
2782 			;
2783 
2784 alter_type_cmds:
2785 			alter_type_cmd							{ $$ = list_make1($1); }
2786 			| alter_type_cmds ',' alter_type_cmd	{ $$ = lappend($1, $3); }
2787 		;
2788 
2789 alter_type_cmd:
2790 			/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
2791 			ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
2792 				{
2793 					AlterTableCmd *n = makeNode(AlterTableCmd);
2794 					n->subtype = AT_AddColumn;
2795 					n->def = $3;
2796 					n->behavior = $4;
2797 					$$ = (Node *)n;
2798 				}
2799 			/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
2800 			| DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
2801 				{
2802 					AlterTableCmd *n = makeNode(AlterTableCmd);
2803 					n->subtype = AT_DropColumn;
2804 					n->name = $5;
2805 					n->behavior = $6;
2806 					n->missing_ok = TRUE;
2807 					$$ = (Node *)n;
2808 				}
2809 			/* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
2810 			| DROP ATTRIBUTE ColId opt_drop_behavior
2811 				{
2812 					AlterTableCmd *n = makeNode(AlterTableCmd);
2813 					n->subtype = AT_DropColumn;
2814 					n->name = $3;
2815 					n->behavior = $4;
2816 					n->missing_ok = FALSE;
2817 					$$ = (Node *)n;
2818 				}
2819 			/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
2820 			| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
2821 				{
2822 					AlterTableCmd *n = makeNode(AlterTableCmd);
2823 					ColumnDef *def = makeNode(ColumnDef);
2824 					n->subtype = AT_AlterColumnType;
2825 					n->name = $3;
2826 					n->def = (Node *) def;
2827 					n->behavior = $8;
2828 					/* We only use these fields of the ColumnDef node */
2829 					def->typeName = $6;
2830 					def->collClause = (CollateClause *) $7;
2831 					def->raw_default = NULL;
2832 					def->location = @3;
2833 					$$ = (Node *)n;
2834 				}
2835 		;
2836 
2837 
2838 /*****************************************************************************
2839  *
2840  *		QUERY :
2841  *				close <portalname>
2842  *
2843  *****************************************************************************/
2844 
2845 ClosePortalStmt:
2846 			CLOSE cursor_name
2847 				{
2848 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2849 					n->portalname = $2;
2850 					$$ = (Node *)n;
2851 				}
2852 			| CLOSE ALL
2853 				{
2854 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
2855 					n->portalname = NULL;
2856 					$$ = (Node *)n;
2857 				}
2858 		;
2859 
2860 
2861 /*****************************************************************************
2862  *
2863  *		QUERY :
2864  *				COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
2865  *				COPY ( query ) TO file	[WITH] [(options)]
2866  *
2867  *				where 'query' can be one of:
2868  *				{ SELECT | UPDATE | INSERT | DELETE }
2869  *
2870  *				and 'file' can be one of:
2871  *				{ PROGRAM 'command' | STDIN | STDOUT | 'filename' }
2872  *
2873  *				In the preferred syntax the options are comma-separated
2874  *				and use generic identifiers instead of keywords.  The pre-9.0
2875  *				syntax had a hard-wired, space-separated set of options.
2876  *
2877  *				Really old syntax, from versions 7.2 and prior:
2878  *				COPY [ BINARY ] table [ WITH OIDS ] FROM/TO file
2879  *					[ [ USING ] DELIMITERS 'delimiter' ] ]
2880  *					[ WITH NULL AS 'null string' ]
2881  *				This option placement is not supported with COPY (query...).
2882  *
2883  *****************************************************************************/
2884 
2885 CopyStmt:	COPY opt_binary qualified_name opt_column_list opt_oids
2886 			copy_from opt_program copy_file_name copy_delimiter opt_with copy_options
2887 				{
2888 					CopyStmt *n = makeNode(CopyStmt);
2889 					n->relation = $3;
2890 					n->query = NULL;
2891 					n->attlist = $4;
2892 					n->is_from = $6;
2893 					n->is_program = $7;
2894 					n->filename = $8;
2895 
2896 					if (n->is_program && n->filename == NULL)
2897 						ereport(ERROR,
2898 								(errcode(ERRCODE_SYNTAX_ERROR),
2899 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2900 								 parser_errposition(@8)));
2901 
2902 					n->options = NIL;
2903 					/* Concatenate user-supplied flags */
2904 					if ($2)
2905 						n->options = lappend(n->options, $2);
2906 					if ($5)
2907 						n->options = lappend(n->options, $5);
2908 					if ($9)
2909 						n->options = lappend(n->options, $9);
2910 					if ($11)
2911 						n->options = list_concat(n->options, $11);
2912 					$$ = (Node *)n;
2913 				}
2914 			| COPY '(' PreparableStmt ')' TO opt_program copy_file_name opt_with copy_options
2915 				{
2916 					CopyStmt *n = makeNode(CopyStmt);
2917 					n->relation = NULL;
2918 					n->query = $3;
2919 					n->attlist = NIL;
2920 					n->is_from = false;
2921 					n->is_program = $6;
2922 					n->filename = $7;
2923 					n->options = $9;
2924 
2925 					if (n->is_program && n->filename == NULL)
2926 						ereport(ERROR,
2927 								(errcode(ERRCODE_SYNTAX_ERROR),
2928 								 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2929 								 parser_errposition(@5)));
2930 
2931 					$$ = (Node *)n;
2932 				}
2933 		;
2934 
2935 copy_from:
2936 			FROM									{ $$ = TRUE; }
2937 			| TO									{ $$ = FALSE; }
2938 		;
2939 
2940 opt_program:
2941 			PROGRAM									{ $$ = TRUE; }
2942 			| /* EMPTY */							{ $$ = FALSE; }
2943 		;
2944 
2945 /*
2946  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
2947  * used depends on the direction. (It really doesn't make sense to copy from
2948  * stdout. We silently correct the "typo".)		 - AY 9/94
2949  */
2950 copy_file_name:
2951 			Sconst									{ $$ = $1; }
2952 			| STDIN									{ $$ = NULL; }
2953 			| STDOUT								{ $$ = NULL; }
2954 		;
2955 
2956 copy_options: copy_opt_list							{ $$ = $1; }
2957 			| '(' copy_generic_opt_list ')'			{ $$ = $2; }
2958 		;
2959 
2960 /* old COPY option syntax */
2961 copy_opt_list:
2962 			copy_opt_list copy_opt_item				{ $$ = lappend($1, $2); }
2963 			| /* EMPTY */							{ $$ = NIL; }
2964 		;
2965 
2966 copy_opt_item:
2967 			BINARY
2968 				{
2969 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
2970 				}
2971 			| OIDS
2972 				{
2973 					$$ = makeDefElem("oids", (Node *)makeInteger(TRUE), @1);
2974 				}
2975 			| FREEZE
2976 				{
2977 					$$ = makeDefElem("freeze", (Node *)makeInteger(TRUE), @1);
2978 				}
2979 			| DELIMITER opt_as Sconst
2980 				{
2981 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @1);
2982 				}
2983 			| NULL_P opt_as Sconst
2984 				{
2985 					$$ = makeDefElem("null", (Node *)makeString($3), @1);
2986 				}
2987 			| CSV
2988 				{
2989 					$$ = makeDefElem("format", (Node *)makeString("csv"), @1);
2990 				}
2991 			| HEADER_P
2992 				{
2993 					$$ = makeDefElem("header", (Node *)makeInteger(TRUE), @1);
2994 				}
2995 			| QUOTE opt_as Sconst
2996 				{
2997 					$$ = makeDefElem("quote", (Node *)makeString($3), @1);
2998 				}
2999 			| ESCAPE opt_as Sconst
3000 				{
3001 					$$ = makeDefElem("escape", (Node *)makeString($3), @1);
3002 				}
3003 			| FORCE QUOTE columnList
3004 				{
3005 					$$ = makeDefElem("force_quote", (Node *)$3, @1);
3006 				}
3007 			| FORCE QUOTE '*'
3008 				{
3009 					$$ = makeDefElem("force_quote", (Node *)makeNode(A_Star), @1);
3010 				}
3011 			| FORCE NOT NULL_P columnList
3012 				{
3013 					$$ = makeDefElem("force_not_null", (Node *)$4, @1);
3014 				}
3015 			| FORCE NULL_P columnList
3016 				{
3017 					$$ = makeDefElem("force_null", (Node *)$3, @1);
3018 				}
3019 			| ENCODING Sconst
3020 				{
3021 					$$ = makeDefElem("encoding", (Node *)makeString($2), @1);
3022 				}
3023 		;
3024 
3025 /* The following exist for backward compatibility with very old versions */
3026 
3027 opt_binary:
3028 			BINARY
3029 				{
3030 					$$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3031 				}
3032 			| /*EMPTY*/								{ $$ = NULL; }
3033 		;
3034 
3035 opt_oids:
3036 			WITH OIDS
3037 				{
3038 					$$ = makeDefElem("oids", (Node *)makeInteger(TRUE), @1);
3039 				}
3040 			| /*EMPTY*/								{ $$ = NULL; }
3041 		;
3042 
3043 copy_delimiter:
3044 			opt_using DELIMITERS Sconst
3045 				{
3046 					$$ = makeDefElem("delimiter", (Node *)makeString($3), @2);
3047 				}
3048 			| /*EMPTY*/								{ $$ = NULL; }
3049 		;
3050 
3051 opt_using:
3052 			USING									{}
3053 			| /*EMPTY*/								{}
3054 		;
3055 
3056 /* new COPY option syntax */
3057 copy_generic_opt_list:
3058 			copy_generic_opt_elem
3059 				{
3060 					$$ = list_make1($1);
3061 				}
3062 			| copy_generic_opt_list ',' copy_generic_opt_elem
3063 				{
3064 					$$ = lappend($1, $3);
3065 				}
3066 		;
3067 
3068 copy_generic_opt_elem:
3069 			ColLabel copy_generic_opt_arg
3070 				{
3071 					$$ = makeDefElem($1, $2, @1);
3072 				}
3073 		;
3074 
3075 copy_generic_opt_arg:
3076 			opt_boolean_or_string			{ $$ = (Node *) makeString($1); }
3077 			| NumericOnly					{ $$ = (Node *) $1; }
3078 			| '*'							{ $$ = (Node *) makeNode(A_Star); }
3079 			| '(' copy_generic_opt_arg_list ')'		{ $$ = (Node *) $2; }
3080 			| /* EMPTY */					{ $$ = NULL; }
3081 		;
3082 
3083 copy_generic_opt_arg_list:
3084 			  copy_generic_opt_arg_list_item
3085 				{
3086 					$$ = list_make1($1);
3087 				}
3088 			| copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
3089 				{
3090 					$$ = lappend($1, $3);
3091 				}
3092 		;
3093 
3094 /* beware of emitting non-string list elements here; see commands/define.c */
3095 copy_generic_opt_arg_list_item:
3096 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
3097 		;
3098 
3099 
3100 /*****************************************************************************
3101  *
3102  *		QUERY :
3103  *				CREATE TABLE relname
3104  *
3105  *****************************************************************************/
3106 
3107 CreateStmt:	CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
3108 			OptInherit OptPartitionSpec OptWith OnCommitOption OptTableSpace
3109 				{
3110 					CreateStmt *n = makeNode(CreateStmt);
3111 					$4->relpersistence = $2;
3112 					n->relation = $4;
3113 					n->tableElts = $6;
3114 					n->inhRelations = $8;
3115 					n->partspec = $9;
3116 					n->ofTypename = NULL;
3117 					n->constraints = NIL;
3118 					n->options = $10;
3119 					n->oncommit = $11;
3120 					n->tablespacename = $12;
3121 					n->if_not_exists = false;
3122 					$$ = (Node *)n;
3123 				}
3124 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
3125 			OptTableElementList ')' OptInherit OptPartitionSpec OptWith
3126 			OnCommitOption OptTableSpace
3127 				{
3128 					CreateStmt *n = makeNode(CreateStmt);
3129 					$7->relpersistence = $2;
3130 					n->relation = $7;
3131 					n->tableElts = $9;
3132 					n->inhRelations = $11;
3133 					n->partspec = $12;
3134 					n->ofTypename = NULL;
3135 					n->constraints = NIL;
3136 					n->options = $13;
3137 					n->oncommit = $14;
3138 					n->tablespacename = $15;
3139 					n->if_not_exists = true;
3140 					$$ = (Node *)n;
3141 				}
3142 		| CREATE OptTemp TABLE qualified_name OF any_name
3143 			OptTypedTableElementList OptPartitionSpec OptWith OnCommitOption
3144 			OptTableSpace
3145 				{
3146 					CreateStmt *n = makeNode(CreateStmt);
3147 					$4->relpersistence = $2;
3148 					n->relation = $4;
3149 					n->tableElts = $7;
3150 					n->inhRelations = NIL;
3151 					n->partspec = $8;
3152 					n->ofTypename = makeTypeNameFromNameList($6);
3153 					n->ofTypename->location = @6;
3154 					n->constraints = NIL;
3155 					n->options = $9;
3156 					n->oncommit = $10;
3157 					n->tablespacename = $11;
3158 					n->if_not_exists = false;
3159 					$$ = (Node *)n;
3160 				}
3161 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
3162 			OptTypedTableElementList OptPartitionSpec OptWith OnCommitOption
3163 			OptTableSpace
3164 				{
3165 					CreateStmt *n = makeNode(CreateStmt);
3166 					$7->relpersistence = $2;
3167 					n->relation = $7;
3168 					n->tableElts = $10;
3169 					n->inhRelations = NIL;
3170 					n->partspec = $11;
3171 					n->ofTypename = makeTypeNameFromNameList($9);
3172 					n->ofTypename->location = @9;
3173 					n->constraints = NIL;
3174 					n->options = $12;
3175 					n->oncommit = $13;
3176 					n->tablespacename = $14;
3177 					n->if_not_exists = true;
3178 					$$ = (Node *)n;
3179 				}
3180 		| CREATE OptTemp TABLE qualified_name PARTITION OF qualified_name
3181 			OptTypedTableElementList ForValues OptPartitionSpec OptWith
3182 			OnCommitOption OptTableSpace
3183 				{
3184 					CreateStmt *n = makeNode(CreateStmt);
3185 					$4->relpersistence = $2;
3186 					n->relation = $4;
3187 					n->tableElts = $8;
3188 					n->inhRelations = list_make1($7);
3189 					n->partbound = $9;
3190 					n->partspec = $10;
3191 					n->ofTypename = NULL;
3192 					n->constraints = NIL;
3193 					n->options = $11;
3194 					n->oncommit = $12;
3195 					n->tablespacename = $13;
3196 					n->if_not_exists = false;
3197 					$$ = (Node *)n;
3198 				}
3199 		| CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name PARTITION OF
3200 			qualified_name OptTypedTableElementList ForValues OptPartitionSpec
3201 			OptWith OnCommitOption OptTableSpace
3202 				{
3203 					CreateStmt *n = makeNode(CreateStmt);
3204 					$7->relpersistence = $2;
3205 					n->relation = $7;
3206 					n->tableElts = $11;
3207 					n->inhRelations = list_make1($10);
3208 					n->partbound = $12;
3209 					n->partspec = $13;
3210 					n->ofTypename = NULL;
3211 					n->constraints = NIL;
3212 					n->options = $14;
3213 					n->oncommit = $15;
3214 					n->tablespacename = $16;
3215 					n->if_not_exists = true;
3216 					$$ = (Node *)n;
3217 				}
3218 		;
3219 
3220 /*
3221  * Redundancy here is needed to avoid shift/reduce conflicts,
3222  * since TEMP is not a reserved word.  See also OptTempTableName.
3223  *
3224  * NOTE: we accept both GLOBAL and LOCAL options.  They currently do nothing,
3225  * but future versions might consider GLOBAL to request SQL-spec-compliant
3226  * temp table behavior, so warn about that.  Since we have no modules the
3227  * LOCAL keyword is really meaningless; furthermore, some other products
3228  * implement LOCAL as meaning the same as our default temp table behavior,
3229  * so we'll probably continue to treat LOCAL as a noise word.
3230  */
3231 OptTemp:	TEMPORARY					{ $$ = RELPERSISTENCE_TEMP; }
3232 			| TEMP						{ $$ = RELPERSISTENCE_TEMP; }
3233 			| LOCAL TEMPORARY			{ $$ = RELPERSISTENCE_TEMP; }
3234 			| LOCAL TEMP				{ $$ = RELPERSISTENCE_TEMP; }
3235 			| GLOBAL TEMPORARY
3236 				{
3237 					ereport(WARNING,
3238 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3239 							 parser_errposition(@1)));
3240 					$$ = RELPERSISTENCE_TEMP;
3241 				}
3242 			| GLOBAL TEMP
3243 				{
3244 					ereport(WARNING,
3245 							(errmsg("GLOBAL is deprecated in temporary table creation"),
3246 							 parser_errposition(@1)));
3247 					$$ = RELPERSISTENCE_TEMP;
3248 				}
3249 			| UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
3250 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
3251 		;
3252 
3253 OptTableElementList:
3254 			TableElementList					{ $$ = $1; }
3255 			| /*EMPTY*/							{ $$ = NIL; }
3256 		;
3257 
3258 OptTypedTableElementList:
3259 			'(' TypedTableElementList ')'		{ $$ = $2; }
3260 			| /*EMPTY*/							{ $$ = NIL; }
3261 		;
3262 
3263 TableElementList:
3264 			TableElement
3265 				{
3266 					$$ = list_make1($1);
3267 				}
3268 			| TableElementList ',' TableElement
3269 				{
3270 					$$ = lappend($1, $3);
3271 				}
3272 		;
3273 
3274 TypedTableElementList:
3275 			TypedTableElement
3276 				{
3277 					$$ = list_make1($1);
3278 				}
3279 			| TypedTableElementList ',' TypedTableElement
3280 				{
3281 					$$ = lappend($1, $3);
3282 				}
3283 		;
3284 
3285 TableElement:
3286 			columnDef							{ $$ = $1; }
3287 			| TableLikeClause					{ $$ = $1; }
3288 			| TableConstraint					{ $$ = $1; }
3289 		;
3290 
3291 TypedTableElement:
3292 			columnOptions						{ $$ = $1; }
3293 			| TableConstraint					{ $$ = $1; }
3294 		;
3295 
3296 columnDef:	ColId Typename create_generic_options ColQualList
3297 				{
3298 					ColumnDef *n = makeNode(ColumnDef);
3299 					n->colname = $1;
3300 					n->typeName = $2;
3301 					n->inhcount = 0;
3302 					n->is_local = true;
3303 					n->is_not_null = false;
3304 					n->is_from_type = false;
3305 					n->is_from_parent = false;
3306 					n->storage = 0;
3307 					n->raw_default = NULL;
3308 					n->cooked_default = NULL;
3309 					n->collOid = InvalidOid;
3310 					n->fdwoptions = $3;
3311 					SplitColQualList($4, &n->constraints, &n->collClause,
3312 									 yyscanner);
3313 					n->location = @1;
3314 					$$ = (Node *)n;
3315 				}
3316 		;
3317 
3318 columnOptions:	ColId ColQualList
3319 				{
3320 					ColumnDef *n = makeNode(ColumnDef);
3321 					n->colname = $1;
3322 					n->typeName = NULL;
3323 					n->inhcount = 0;
3324 					n->is_local = true;
3325 					n->is_not_null = false;
3326 					n->is_from_type = false;
3327 					n->is_from_parent = false;
3328 					n->storage = 0;
3329 					n->raw_default = NULL;
3330 					n->cooked_default = NULL;
3331 					n->collOid = InvalidOid;
3332 					SplitColQualList($2, &n->constraints, &n->collClause,
3333 									 yyscanner);
3334 					n->location = @1;
3335 					$$ = (Node *)n;
3336 				}
3337 				| ColId WITH OPTIONS ColQualList
3338 				{
3339 					ColumnDef *n = makeNode(ColumnDef);
3340 					n->colname = $1;
3341 					n->typeName = NULL;
3342 					n->inhcount = 0;
3343 					n->is_local = true;
3344 					n->is_not_null = false;
3345 					n->is_from_type = false;
3346 					n->is_from_parent = false;
3347 					n->storage = 0;
3348 					n->raw_default = NULL;
3349 					n->cooked_default = NULL;
3350 					n->collOid = InvalidOid;
3351 					SplitColQualList($4, &n->constraints, &n->collClause,
3352 									 yyscanner);
3353 					n->location = @1;
3354 					$$ = (Node *)n;
3355 				}
3356 		;
3357 
3358 ColQualList:
3359 			ColQualList ColConstraint				{ $$ = lappend($1, $2); }
3360 			| /*EMPTY*/								{ $$ = NIL; }
3361 		;
3362 
3363 ColConstraint:
3364 			CONSTRAINT name ColConstraintElem
3365 				{
3366 					Constraint *n = castNode(Constraint, $3);
3367 					n->conname = $2;
3368 					n->location = @1;
3369 					$$ = (Node *) n;
3370 				}
3371 			| ColConstraintElem						{ $$ = $1; }
3372 			| ConstraintAttr						{ $$ = $1; }
3373 			| COLLATE any_name
3374 				{
3375 					/*
3376 					 * Note: the CollateClause is momentarily included in
3377 					 * the list built by ColQualList, but we split it out
3378 					 * again in SplitColQualList.
3379 					 */
3380 					CollateClause *n = makeNode(CollateClause);
3381 					n->arg = NULL;
3382 					n->collname = $2;
3383 					n->location = @1;
3384 					$$ = (Node *) n;
3385 				}
3386 		;
3387 
3388 /* DEFAULT NULL is already the default for Postgres.
3389  * But define it here and carry it forward into the system
3390  * to make it explicit.
3391  * - thomas 1998-09-13
3392  *
3393  * WITH NULL and NULL are not SQL-standard syntax elements,
3394  * so leave them out. Use DEFAULT NULL to explicitly indicate
3395  * that a column may have that value. WITH NULL leads to
3396  * shift/reduce conflicts with WITH TIME ZONE anyway.
3397  * - thomas 1999-01-08
3398  *
3399  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
3400  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
3401  * or be part of a_expr NOT LIKE or similar constructs).
3402  */
3403 ColConstraintElem:
3404 			NOT NULL_P
3405 				{
3406 					Constraint *n = makeNode(Constraint);
3407 					n->contype = CONSTR_NOTNULL;
3408 					n->location = @1;
3409 					$$ = (Node *)n;
3410 				}
3411 			| NULL_P
3412 				{
3413 					Constraint *n = makeNode(Constraint);
3414 					n->contype = CONSTR_NULL;
3415 					n->location = @1;
3416 					$$ = (Node *)n;
3417 				}
3418 			| UNIQUE opt_definition OptConsTableSpace
3419 				{
3420 					Constraint *n = makeNode(Constraint);
3421 					n->contype = CONSTR_UNIQUE;
3422 					n->location = @1;
3423 					n->keys = NULL;
3424 					n->options = $2;
3425 					n->indexname = NULL;
3426 					n->indexspace = $3;
3427 					$$ = (Node *)n;
3428 				}
3429 			| PRIMARY KEY opt_definition OptConsTableSpace
3430 				{
3431 					Constraint *n = makeNode(Constraint);
3432 					n->contype = CONSTR_PRIMARY;
3433 					n->location = @1;
3434 					n->keys = NULL;
3435 					n->options = $3;
3436 					n->indexname = NULL;
3437 					n->indexspace = $4;
3438 					$$ = (Node *)n;
3439 				}
3440 			| CHECK '(' a_expr ')' opt_no_inherit
3441 				{
3442 					Constraint *n = makeNode(Constraint);
3443 					n->contype = CONSTR_CHECK;
3444 					n->location = @1;
3445 					n->is_no_inherit = $5;
3446 					n->raw_expr = $3;
3447 					n->cooked_expr = NULL;
3448 					n->skip_validation = false;
3449 					n->initially_valid = true;
3450 					$$ = (Node *)n;
3451 				}
3452 			| DEFAULT b_expr
3453 				{
3454 					Constraint *n = makeNode(Constraint);
3455 					n->contype = CONSTR_DEFAULT;
3456 					n->location = @1;
3457 					n->raw_expr = $2;
3458 					n->cooked_expr = NULL;
3459 					n->skip_validation = false;
3460 					n->initially_valid = true;
3461 					$$ = (Node *)n;
3462 				}
3463 			| GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
3464 				{
3465 					Constraint *n = makeNode(Constraint);
3466 					n->contype = CONSTR_IDENTITY;
3467 					n->generated_when = $2;
3468 					n->options = $5;
3469 					n->location = @1;
3470 					$$ = (Node *)n;
3471 				}
3472 			| REFERENCES qualified_name opt_column_list key_match key_actions
3473 				{
3474 					Constraint *n = makeNode(Constraint);
3475 					n->contype = CONSTR_FOREIGN;
3476 					n->location = @1;
3477 					n->pktable			= $2;
3478 					n->fk_attrs			= NIL;
3479 					n->pk_attrs			= $3;
3480 					n->fk_matchtype		= $4;
3481 					n->fk_upd_action	= (char) ($5 >> 8);
3482 					n->fk_del_action	= (char) ($5 & 0xFF);
3483 					n->skip_validation  = false;
3484 					n->initially_valid  = true;
3485 					$$ = (Node *)n;
3486 				}
3487 		;
3488 
3489 generated_when:
3490 			ALWAYS			{ $$ = ATTRIBUTE_IDENTITY_ALWAYS; }
3491 			| BY DEFAULT	{ $$ = ATTRIBUTE_IDENTITY_BY_DEFAULT; }
3492 		;
3493 
3494 /*
3495  * ConstraintAttr represents constraint attributes, which we parse as if
3496  * they were independent constraint clauses, in order to avoid shift/reduce
3497  * conflicts (since NOT might start either an independent NOT NULL clause
3498  * or an attribute).  parse_utilcmd.c is responsible for attaching the
3499  * attribute information to the preceding "real" constraint node, and for
3500  * complaining if attribute clauses appear in the wrong place or wrong
3501  * combinations.
3502  *
3503  * See also ConstraintAttributeSpec, which can be used in places where
3504  * there is no parsing conflict.  (Note: currently, NOT VALID and NO INHERIT
3505  * are allowed clauses in ConstraintAttributeSpec, but not here.  Someday we
3506  * might need to allow them here too, but for the moment it doesn't seem
3507  * useful in the statements that use ConstraintAttr.)
3508  */
3509 ConstraintAttr:
3510 			DEFERRABLE
3511 				{
3512 					Constraint *n = makeNode(Constraint);
3513 					n->contype = CONSTR_ATTR_DEFERRABLE;
3514 					n->location = @1;
3515 					$$ = (Node *)n;
3516 				}
3517 			| NOT DEFERRABLE
3518 				{
3519 					Constraint *n = makeNode(Constraint);
3520 					n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
3521 					n->location = @1;
3522 					$$ = (Node *)n;
3523 				}
3524 			| INITIALLY DEFERRED
3525 				{
3526 					Constraint *n = makeNode(Constraint);
3527 					n->contype = CONSTR_ATTR_DEFERRED;
3528 					n->location = @1;
3529 					$$ = (Node *)n;
3530 				}
3531 			| INITIALLY IMMEDIATE
3532 				{
3533 					Constraint *n = makeNode(Constraint);
3534 					n->contype = CONSTR_ATTR_IMMEDIATE;
3535 					n->location = @1;
3536 					$$ = (Node *)n;
3537 				}
3538 		;
3539 
3540 
3541 TableLikeClause:
3542 			LIKE qualified_name TableLikeOptionList
3543 				{
3544 					TableLikeClause *n = makeNode(TableLikeClause);
3545 					n->relation = $2;
3546 					n->options = $3;
3547 					$$ = (Node *)n;
3548 				}
3549 		;
3550 
3551 TableLikeOptionList:
3552 				TableLikeOptionList INCLUDING TableLikeOption	{ $$ = $1 | $3; }
3553 				| TableLikeOptionList EXCLUDING TableLikeOption	{ $$ = $1 & ~$3; }
3554 				| /* EMPTY */						{ $$ = 0; }
3555 		;
3556 
3557 TableLikeOption:
3558 				DEFAULTS			{ $$ = CREATE_TABLE_LIKE_DEFAULTS; }
3559 				| CONSTRAINTS		{ $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
3560 				| IDENTITY_P		{ $$ = CREATE_TABLE_LIKE_IDENTITY; }
3561 				| INDEXES			{ $$ = CREATE_TABLE_LIKE_INDEXES; }
3562 				| STORAGE			{ $$ = CREATE_TABLE_LIKE_STORAGE; }
3563 				| COMMENTS			{ $$ = CREATE_TABLE_LIKE_COMMENTS; }
3564 				| ALL				{ $$ = CREATE_TABLE_LIKE_ALL; }
3565 		;
3566 
3567 
3568 /* ConstraintElem specifies constraint syntax which is not embedded into
3569  *	a column definition. ColConstraintElem specifies the embedded form.
3570  * - thomas 1997-12-03
3571  */
3572 TableConstraint:
3573 			CONSTRAINT name ConstraintElem
3574 				{
3575 					Constraint *n = castNode(Constraint, $3);
3576 					n->conname = $2;
3577 					n->location = @1;
3578 					$$ = (Node *) n;
3579 				}
3580 			| ConstraintElem						{ $$ = $1; }
3581 		;
3582 
3583 ConstraintElem:
3584 			CHECK '(' a_expr ')' ConstraintAttributeSpec
3585 				{
3586 					Constraint *n = makeNode(Constraint);
3587 					n->contype = CONSTR_CHECK;
3588 					n->location = @1;
3589 					n->raw_expr = $3;
3590 					n->cooked_expr = NULL;
3591 					processCASbits($5, @5, "CHECK",
3592 								   NULL, NULL, &n->skip_validation,
3593 								   &n->is_no_inherit, yyscanner);
3594 					n->initially_valid = !n->skip_validation;
3595 					$$ = (Node *)n;
3596 				}
3597 			| UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
3598 				ConstraintAttributeSpec
3599 				{
3600 					Constraint *n = makeNode(Constraint);
3601 					n->contype = CONSTR_UNIQUE;
3602 					n->location = @1;
3603 					n->keys = $3;
3604 					n->options = $5;
3605 					n->indexname = NULL;
3606 					n->indexspace = $6;
3607 					processCASbits($7, @7, "UNIQUE",
3608 								   &n->deferrable, &n->initdeferred, NULL,
3609 								   NULL, yyscanner);
3610 					$$ = (Node *)n;
3611 				}
3612 			| UNIQUE ExistingIndex ConstraintAttributeSpec
3613 				{
3614 					Constraint *n = makeNode(Constraint);
3615 					n->contype = CONSTR_UNIQUE;
3616 					n->location = @1;
3617 					n->keys = NIL;
3618 					n->options = NIL;
3619 					n->indexname = $2;
3620 					n->indexspace = NULL;
3621 					processCASbits($3, @3, "UNIQUE",
3622 								   &n->deferrable, &n->initdeferred, NULL,
3623 								   NULL, yyscanner);
3624 					$$ = (Node *)n;
3625 				}
3626 			| PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
3627 				ConstraintAttributeSpec
3628 				{
3629 					Constraint *n = makeNode(Constraint);
3630 					n->contype = CONSTR_PRIMARY;
3631 					n->location = @1;
3632 					n->keys = $4;
3633 					n->options = $6;
3634 					n->indexname = NULL;
3635 					n->indexspace = $7;
3636 					processCASbits($8, @8, "PRIMARY KEY",
3637 								   &n->deferrable, &n->initdeferred, NULL,
3638 								   NULL, yyscanner);
3639 					$$ = (Node *)n;
3640 				}
3641 			| PRIMARY KEY ExistingIndex ConstraintAttributeSpec
3642 				{
3643 					Constraint *n = makeNode(Constraint);
3644 					n->contype = CONSTR_PRIMARY;
3645 					n->location = @1;
3646 					n->keys = NIL;
3647 					n->options = NIL;
3648 					n->indexname = $3;
3649 					n->indexspace = NULL;
3650 					processCASbits($4, @4, "PRIMARY KEY",
3651 								   &n->deferrable, &n->initdeferred, NULL,
3652 								   NULL, yyscanner);
3653 					$$ = (Node *)n;
3654 				}
3655 			| EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
3656 				opt_definition OptConsTableSpace ExclusionWhereClause
3657 				ConstraintAttributeSpec
3658 				{
3659 					Constraint *n = makeNode(Constraint);
3660 					n->contype = CONSTR_EXCLUSION;
3661 					n->location = @1;
3662 					n->access_method	= $2;
3663 					n->exclusions		= $4;
3664 					n->options			= $6;
3665 					n->indexname		= NULL;
3666 					n->indexspace		= $7;
3667 					n->where_clause		= $8;
3668 					processCASbits($9, @9, "EXCLUDE",
3669 								   &n->deferrable, &n->initdeferred, NULL,
3670 								   NULL, yyscanner);
3671 					$$ = (Node *)n;
3672 				}
3673 			| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
3674 				opt_column_list key_match key_actions ConstraintAttributeSpec
3675 				{
3676 					Constraint *n = makeNode(Constraint);
3677 					n->contype = CONSTR_FOREIGN;
3678 					n->location = @1;
3679 					n->pktable			= $7;
3680 					n->fk_attrs			= $4;
3681 					n->pk_attrs			= $8;
3682 					n->fk_matchtype		= $9;
3683 					n->fk_upd_action	= (char) ($10 >> 8);
3684 					n->fk_del_action	= (char) ($10 & 0xFF);
3685 					processCASbits($11, @11, "FOREIGN KEY",
3686 								   &n->deferrable, &n->initdeferred,
3687 								   &n->skip_validation, NULL,
3688 								   yyscanner);
3689 					n->initially_valid = !n->skip_validation;
3690 					$$ = (Node *)n;
3691 				}
3692 		;
3693 
3694 opt_no_inherit:	NO INHERIT							{  $$ = TRUE; }
3695 			| /* EMPTY */							{  $$ = FALSE; }
3696 		;
3697 
3698 opt_column_list:
3699 			'(' columnList ')'						{ $$ = $2; }
3700 			| /*EMPTY*/								{ $$ = NIL; }
3701 		;
3702 
3703 columnList:
3704 			columnElem								{ $$ = list_make1($1); }
3705 			| columnList ',' columnElem				{ $$ = lappend($1, $3); }
3706 		;
3707 
3708 columnElem: ColId
3709 				{
3710 					$$ = (Node *) makeString($1);
3711 				}
3712 		;
3713 
3714 key_match:  MATCH FULL
3715 			{
3716 				$$ = FKCONSTR_MATCH_FULL;
3717 			}
3718 		| MATCH PARTIAL
3719 			{
3720 				ereport(ERROR,
3721 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3722 						 errmsg("MATCH PARTIAL not yet implemented"),
3723 						 parser_errposition(@1)));
3724 				$$ = FKCONSTR_MATCH_PARTIAL;
3725 			}
3726 		| MATCH SIMPLE
3727 			{
3728 				$$ = FKCONSTR_MATCH_SIMPLE;
3729 			}
3730 		| /*EMPTY*/
3731 			{
3732 				$$ = FKCONSTR_MATCH_SIMPLE;
3733 			}
3734 		;
3735 
3736 ExclusionConstraintList:
3737 			ExclusionConstraintElem					{ $$ = list_make1($1); }
3738 			| ExclusionConstraintList ',' ExclusionConstraintElem
3739 													{ $$ = lappend($1, $3); }
3740 		;
3741 
3742 ExclusionConstraintElem: index_elem WITH any_operator
3743 			{
3744 				$$ = list_make2($1, $3);
3745 			}
3746 			/* allow OPERATOR() decoration for the benefit of ruleutils.c */
3747 			| index_elem WITH OPERATOR '(' any_operator ')'
3748 			{
3749 				$$ = list_make2($1, $5);
3750 			}
3751 		;
3752 
3753 ExclusionWhereClause:
3754 			WHERE '(' a_expr ')'					{ $$ = $3; }
3755 			| /*EMPTY*/								{ $$ = NULL; }
3756 		;
3757 
3758 /*
3759  * We combine the update and delete actions into one value temporarily
3760  * for simplicity of parsing, and then break them down again in the
3761  * calling production.  update is in the left 8 bits, delete in the right.
3762  * Note that NOACTION is the default.
3763  */
3764 key_actions:
3765 			key_update
3766 				{ $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3767 			| key_delete
3768 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
3769 			| key_update key_delete
3770 				{ $$ = ($1 << 8) | ($2 & 0xFF); }
3771 			| key_delete key_update
3772 				{ $$ = ($2 << 8) | ($1 & 0xFF); }
3773 			| /*EMPTY*/
3774 				{ $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3775 		;
3776 
3777 key_update: ON UPDATE key_action		{ $$ = $3; }
3778 		;
3779 
3780 key_delete: ON DELETE_P key_action		{ $$ = $3; }
3781 		;
3782 
3783 key_action:
3784 			NO ACTION					{ $$ = FKCONSTR_ACTION_NOACTION; }
3785 			| RESTRICT					{ $$ = FKCONSTR_ACTION_RESTRICT; }
3786 			| CASCADE					{ $$ = FKCONSTR_ACTION_CASCADE; }
3787 			| SET NULL_P				{ $$ = FKCONSTR_ACTION_SETNULL; }
3788 			| SET DEFAULT				{ $$ = FKCONSTR_ACTION_SETDEFAULT; }
3789 		;
3790 
3791 OptInherit: INHERITS '(' qualified_name_list ')'	{ $$ = $3; }
3792 			| /*EMPTY*/								{ $$ = NIL; }
3793 		;
3794 
3795 /* Optional partition key specification */
3796 OptPartitionSpec: PartitionSpec	{ $$ = $1; }
3797 			| /*EMPTY*/			{ $$ = NULL; }
3798 		;
3799 
3800 PartitionSpec: PARTITION BY part_strategy '(' part_params ')'
3801 				{
3802 					PartitionSpec *n = makeNode(PartitionSpec);
3803 
3804 					n->strategy = $3;
3805 					n->partParams = $5;
3806 					n->location = @1;
3807 
3808 					$$ = n;
3809 				}
3810 		;
3811 
3812 part_strategy:	IDENT					{ $$ = $1; }
3813 				| unreserved_keyword	{ $$ = pstrdup($1); }
3814 		;
3815 
3816 part_params:	part_elem						{ $$ = list_make1($1); }
3817 			| part_params ',' part_elem			{ $$ = lappend($1, $3); }
3818 		;
3819 
3820 part_elem: ColId opt_collate opt_class
3821 				{
3822 					PartitionElem *n = makeNode(PartitionElem);
3823 
3824 					n->name = $1;
3825 					n->expr = NULL;
3826 					n->collation = $2;
3827 					n->opclass = $3;
3828 					n->location = @1;
3829 					$$ = n;
3830 				}
3831 			| func_expr_windowless opt_collate opt_class
3832 				{
3833 					PartitionElem *n = makeNode(PartitionElem);
3834 
3835 					n->name = NULL;
3836 					n->expr = $1;
3837 					n->collation = $2;
3838 					n->opclass = $3;
3839 					n->location = @1;
3840 					$$ = n;
3841 				}
3842 			| '(' a_expr ')' opt_collate opt_class
3843 				{
3844 					PartitionElem *n = makeNode(PartitionElem);
3845 
3846 					n->name = NULL;
3847 					n->expr = $2;
3848 					n->collation = $4;
3849 					n->opclass = $5;
3850 					n->location = @1;
3851 					$$ = n;
3852 				}
3853 		;
3854 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
3855 OptWith:
3856 			WITH reloptions				{ $$ = $2; }
3857 			| WITH OIDS					{ $$ = list_make1(makeDefElem("oids", (Node *) makeInteger(true), @1)); }
3858 			| WITHOUT OIDS				{ $$ = list_make1(makeDefElem("oids", (Node *) makeInteger(false), @1)); }
3859 			| /*EMPTY*/					{ $$ = NIL; }
3860 		;
3861 
3862 OnCommitOption:  ON COMMIT DROP				{ $$ = ONCOMMIT_DROP; }
3863 			| ON COMMIT DELETE_P ROWS		{ $$ = ONCOMMIT_DELETE_ROWS; }
3864 			| ON COMMIT PRESERVE ROWS		{ $$ = ONCOMMIT_PRESERVE_ROWS; }
3865 			| /*EMPTY*/						{ $$ = ONCOMMIT_NOOP; }
3866 		;
3867 
3868 OptTableSpace:   TABLESPACE name					{ $$ = $2; }
3869 			| /*EMPTY*/								{ $$ = NULL; }
3870 		;
3871 
3872 OptConsTableSpace:   USING INDEX TABLESPACE name	{ $$ = $4; }
3873 			| /*EMPTY*/								{ $$ = NULL; }
3874 		;
3875 
3876 ExistingIndex:   USING INDEX index_name				{ $$ = $3; }
3877 		;
3878 
3879 /*****************************************************************************
3880  *
3881  *		QUERY :
3882  *				CREATE STATISTICS [IF NOT EXISTS] stats_name [(stat types)]
3883  *					ON expression-list FROM from_list
3884  *
3885  * Note: the expectation here is that the clauses after ON are a subset of
3886  * SELECT syntax, allowing for expressions and joined tables, and probably
3887  * someday a WHERE clause.  Much less than that is currently implemented,
3888  * but the grammar accepts it and then we'll throw FEATURE_NOT_SUPPORTED
3889  * errors as necessary at execution.
3890  *
3891  *****************************************************************************/
3892 
3893 CreateStatsStmt:
3894 			CREATE STATISTICS any_name
3895 			opt_name_list ON expr_list FROM from_list
3896 				{
3897 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
3898 					n->defnames = $3;
3899 					n->stat_types = $4;
3900 					n->exprs = $6;
3901 					n->relations = $8;
3902 					n->if_not_exists = false;
3903 					$$ = (Node *)n;
3904 				}
3905 			| CREATE STATISTICS IF_P NOT EXISTS any_name
3906 			opt_name_list ON expr_list FROM from_list
3907 				{
3908 					CreateStatsStmt *n = makeNode(CreateStatsStmt);
3909 					n->defnames = $6;
3910 					n->stat_types = $7;
3911 					n->exprs = $9;
3912 					n->relations = $11;
3913 					n->if_not_exists = true;
3914 					$$ = (Node *)n;
3915 				}
3916 			;
3917 
3918 /*****************************************************************************
3919  *
3920  *		QUERY :
3921  *				CREATE TABLE relname AS SelectStmt [ WITH [NO] DATA ]
3922  *
3923  *
3924  * Note: SELECT ... INTO is a now-deprecated alternative for this.
3925  *
3926  *****************************************************************************/
3927 
3928 CreateAsStmt:
3929 		CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
3930 				{
3931 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3932 					ctas->query = $6;
3933 					ctas->into = $4;
3934 					ctas->relkind = OBJECT_TABLE;
3935 					ctas->is_select_into = false;
3936 					ctas->if_not_exists = false;
3937 					/* cram additional flags into the IntoClause */
3938 					$4->rel->relpersistence = $2;
3939 					$4->skipData = !($7);
3940 					$$ = (Node *) ctas;
3941 				}
3942 		| CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS SelectStmt opt_with_data
3943 				{
3944 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3945 					ctas->query = $9;
3946 					ctas->into = $7;
3947 					ctas->relkind = OBJECT_TABLE;
3948 					ctas->is_select_into = false;
3949 					ctas->if_not_exists = true;
3950 					/* cram additional flags into the IntoClause */
3951 					$7->rel->relpersistence = $2;
3952 					$7->skipData = !($10);
3953 					$$ = (Node *) ctas;
3954 				}
3955 		;
3956 
3957 create_as_target:
3958 			qualified_name opt_column_list OptWith OnCommitOption OptTableSpace
3959 				{
3960 					$$ = makeNode(IntoClause);
3961 					$$->rel = $1;
3962 					$$->colNames = $2;
3963 					$$->options = $3;
3964 					$$->onCommit = $4;
3965 					$$->tableSpaceName = $5;
3966 					$$->viewQuery = NULL;
3967 					$$->skipData = false;		/* might get changed later */
3968 				}
3969 		;
3970 
3971 opt_with_data:
3972 			WITH DATA_P								{ $$ = TRUE; }
3973 			| WITH NO DATA_P						{ $$ = FALSE; }
3974 			| /*EMPTY*/								{ $$ = TRUE; }
3975 		;
3976 
3977 
3978 /*****************************************************************************
3979  *
3980  *		QUERY :
3981  *				CREATE MATERIALIZED VIEW relname AS SelectStmt
3982  *
3983  *****************************************************************************/
3984 
3985 CreateMatViewStmt:
3986 		CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data
3987 				{
3988 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
3989 					ctas->query = $7;
3990 					ctas->into = $5;
3991 					ctas->relkind = OBJECT_MATVIEW;
3992 					ctas->is_select_into = false;
3993 					ctas->if_not_exists = false;
3994 					/* cram additional flags into the IntoClause */
3995 					$5->rel->relpersistence = $2;
3996 					$5->skipData = !($8);
3997 					$$ = (Node *) ctas;
3998 				}
3999 		| CREATE OptNoLog MATERIALIZED VIEW IF_P NOT EXISTS create_mv_target AS SelectStmt opt_with_data
4000 				{
4001 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4002 					ctas->query = $10;
4003 					ctas->into = $8;
4004 					ctas->relkind = OBJECT_MATVIEW;
4005 					ctas->is_select_into = false;
4006 					ctas->if_not_exists = true;
4007 					/* cram additional flags into the IntoClause */
4008 					$8->rel->relpersistence = $2;
4009 					$8->skipData = !($11);
4010 					$$ = (Node *) ctas;
4011 				}
4012 		;
4013 
4014 create_mv_target:
4015 			qualified_name opt_column_list opt_reloptions OptTableSpace
4016 				{
4017 					$$ = makeNode(IntoClause);
4018 					$$->rel = $1;
4019 					$$->colNames = $2;
4020 					$$->options = $3;
4021 					$$->onCommit = ONCOMMIT_NOOP;
4022 					$$->tableSpaceName = $4;
4023 					$$->viewQuery = NULL;		/* filled at analysis time */
4024 					$$->skipData = false;		/* might get changed later */
4025 				}
4026 		;
4027 
4028 OptNoLog:	UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
4029 			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
4030 		;
4031 
4032 
4033 /*****************************************************************************
4034  *
4035  *		QUERY :
4036  *				REFRESH MATERIALIZED VIEW qualified_name
4037  *
4038  *****************************************************************************/
4039 
4040 RefreshMatViewStmt:
4041 			REFRESH MATERIALIZED VIEW opt_concurrently qualified_name opt_with_data
4042 				{
4043 					RefreshMatViewStmt *n = makeNode(RefreshMatViewStmt);
4044 					n->concurrent = $4;
4045 					n->relation = $5;
4046 					n->skipData = !($6);
4047 					$$ = (Node *) n;
4048 				}
4049 		;
4050 
4051 
4052 /*****************************************************************************
4053  *
4054  *		QUERY :
4055  *				CREATE SEQUENCE seqname
4056  *				ALTER SEQUENCE seqname
4057  *
4058  *****************************************************************************/
4059 
4060 CreateSeqStmt:
4061 			CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
4062 				{
4063 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4064 					$4->relpersistence = $2;
4065 					n->sequence = $4;
4066 					n->options = $5;
4067 					n->ownerId = InvalidOid;
4068 					n->if_not_exists = false;
4069 					$$ = (Node *)n;
4070 				}
4071 			| CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
4072 				{
4073 					CreateSeqStmt *n = makeNode(CreateSeqStmt);
4074 					$7->relpersistence = $2;
4075 					n->sequence = $7;
4076 					n->options = $8;
4077 					n->ownerId = InvalidOid;
4078 					n->if_not_exists = true;
4079 					$$ = (Node *)n;
4080 				}
4081 		;
4082 
4083 AlterSeqStmt:
4084 			ALTER SEQUENCE qualified_name SeqOptList
4085 				{
4086 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4087 					n->sequence = $3;
4088 					n->options = $4;
4089 					n->missing_ok = false;
4090 					$$ = (Node *)n;
4091 				}
4092 			| ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
4093 				{
4094 					AlterSeqStmt *n = makeNode(AlterSeqStmt);
4095 					n->sequence = $5;
4096 					n->options = $6;
4097 					n->missing_ok = true;
4098 					$$ = (Node *)n;
4099 				}
4100 
4101 		;
4102 
4103 OptSeqOptList: SeqOptList							{ $$ = $1; }
4104 			| /*EMPTY*/								{ $$ = NIL; }
4105 		;
4106 
4107 OptParenthesizedSeqOptList: '(' SeqOptList ')'		{ $$ = $2; }
4108 			| /*EMPTY*/								{ $$ = NIL; }
4109 		;
4110 
4111 SeqOptList: SeqOptElem								{ $$ = list_make1($1); }
4112 			| SeqOptList SeqOptElem					{ $$ = lappend($1, $2); }
4113 		;
4114 
4115 SeqOptElem: AS SimpleTypename
4116 				{
4117 					$$ = makeDefElem("as", (Node *)$2, @1);
4118 				}
4119 			| CACHE NumericOnly
4120 				{
4121 					$$ = makeDefElem("cache", (Node *)$2, @1);
4122 				}
4123 			| CYCLE
4124 				{
4125 					$$ = makeDefElem("cycle", (Node *)makeInteger(TRUE), @1);
4126 				}
4127 			| NO CYCLE
4128 				{
4129 					$$ = makeDefElem("cycle", (Node *)makeInteger(FALSE), @1);
4130 				}
4131 			| INCREMENT opt_by NumericOnly
4132 				{
4133 					$$ = makeDefElem("increment", (Node *)$3, @1);
4134 				}
4135 			| MAXVALUE NumericOnly
4136 				{
4137 					$$ = makeDefElem("maxvalue", (Node *)$2, @1);
4138 				}
4139 			| MINVALUE NumericOnly
4140 				{
4141 					$$ = makeDefElem("minvalue", (Node *)$2, @1);
4142 				}
4143 			| NO MAXVALUE
4144 				{
4145 					$$ = makeDefElem("maxvalue", NULL, @1);
4146 				}
4147 			| NO MINVALUE
4148 				{
4149 					$$ = makeDefElem("minvalue", NULL, @1);
4150 				}
4151 			| OWNED BY any_name
4152 				{
4153 					$$ = makeDefElem("owned_by", (Node *)$3, @1);
4154 				}
4155 			| SEQUENCE NAME_P any_name
4156 				{
4157 					/* not documented, only used by pg_dump */
4158 					$$ = makeDefElem("sequence_name", (Node *)$3, @1);
4159 				}
4160 			| START opt_with NumericOnly
4161 				{
4162 					$$ = makeDefElem("start", (Node *)$3, @1);
4163 				}
4164 			| RESTART
4165 				{
4166 					$$ = makeDefElem("restart", NULL, @1);
4167 				}
4168 			| RESTART opt_with NumericOnly
4169 				{
4170 					$$ = makeDefElem("restart", (Node *)$3, @1);
4171 				}
4172 		;
4173 
4174 opt_by:		BY				{}
4175 			| /* empty */	{}
4176 	  ;
4177 
4178 NumericOnly:
4179 			FCONST								{ $$ = makeFloat($1); }
4180 			| '+' FCONST						{ $$ = makeFloat($2); }
4181 			| '-' FCONST
4182 				{
4183 					$$ = makeFloat($2);
4184 					doNegateFloat($$);
4185 				}
4186 			| SignedIconst						{ $$ = makeInteger($1); }
4187 		;
4188 
4189 NumericOnly_list:	NumericOnly						{ $$ = list_make1($1); }
4190 				| NumericOnly_list ',' NumericOnly	{ $$ = lappend($1, $3); }
4191 		;
4192 
4193 /*****************************************************************************
4194  *
4195  *		QUERIES :
4196  *				CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
4197  *				DROP [PROCEDURAL] LANGUAGE ...
4198  *
4199  *****************************************************************************/
4200 
4201 CreatePLangStmt:
4202 			CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4203 			{
4204 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4205 				n->replace = $2;
4206 				n->plname = $6;
4207 				/* parameters are all to be supplied by system */
4208 				n->plhandler = NIL;
4209 				n->plinline = NIL;
4210 				n->plvalidator = NIL;
4211 				n->pltrusted = false;
4212 				$$ = (Node *)n;
4213 			}
4214 			| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4215 			  HANDLER handler_name opt_inline_handler opt_validator
4216 			{
4217 				CreatePLangStmt *n = makeNode(CreatePLangStmt);
4218 				n->replace = $2;
4219 				n->plname = $6;
4220 				n->plhandler = $8;
4221 				n->plinline = $9;
4222 				n->plvalidator = $10;
4223 				n->pltrusted = $3;
4224 				$$ = (Node *)n;
4225 			}
4226 		;
4227 
4228 opt_trusted:
4229 			TRUSTED									{ $$ = TRUE; }
4230 			| /*EMPTY*/								{ $$ = FALSE; }
4231 		;
4232 
4233 /* This ought to be just func_name, but that causes reduce/reduce conflicts
4234  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
4235  * Work around by using simple names, instead.
4236  */
4237 handler_name:
4238 			name						{ $$ = list_make1(makeString($1)); }
4239 			| name attrs				{ $$ = lcons(makeString($1), $2); }
4240 		;
4241 
4242 opt_inline_handler:
4243 			INLINE_P handler_name					{ $$ = $2; }
4244 			| /*EMPTY*/								{ $$ = NIL; }
4245 		;
4246 
4247 validator_clause:
4248 			VALIDATOR handler_name					{ $$ = $2; }
4249 			| NO VALIDATOR							{ $$ = NIL; }
4250 		;
4251 
4252 opt_validator:
4253 			validator_clause						{ $$ = $1; }
4254 			| /*EMPTY*/								{ $$ = NIL; }
4255 		;
4256 
4257 DropPLangStmt:
4258 			DROP opt_procedural LANGUAGE NonReservedWord_or_Sconst opt_drop_behavior
4259 				{
4260 					DropStmt *n = makeNode(DropStmt);
4261 					n->removeType = OBJECT_LANGUAGE;
4262 					n->objects = list_make1(makeString($4));
4263 					n->behavior = $5;
4264 					n->missing_ok = false;
4265 					n->concurrent = false;
4266 					$$ = (Node *)n;
4267 				}
4268 			| DROP opt_procedural LANGUAGE IF_P EXISTS NonReservedWord_or_Sconst opt_drop_behavior
4269 				{
4270 					DropStmt *n = makeNode(DropStmt);
4271 					n->removeType = OBJECT_LANGUAGE;
4272 					n->objects = list_make1(makeString($6));
4273 					n->behavior = $7;
4274 					n->missing_ok = true;
4275 					n->concurrent = false;
4276 					$$ = (Node *)n;
4277 				}
4278 		;
4279 
4280 opt_procedural:
4281 			PROCEDURAL								{}
4282 			| /*EMPTY*/								{}
4283 		;
4284 
4285 /*****************************************************************************
4286  *
4287  *		QUERY:
4288  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
4289  *
4290  *****************************************************************************/
4291 
4292 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst opt_reloptions
4293 				{
4294 					CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
4295 					n->tablespacename = $3;
4296 					n->owner = $4;
4297 					n->location = $6;
4298 					n->options = $7;
4299 					$$ = (Node *) n;
4300 				}
4301 		;
4302 
4303 OptTableSpaceOwner: OWNER RoleSpec		{ $$ = $2; }
4304 			| /*EMPTY */				{ $$ = NULL; }
4305 		;
4306 
4307 /*****************************************************************************
4308  *
4309  *		QUERY :
4310  *				DROP TABLESPACE <tablespace>
4311  *
4312  *		No need for drop behaviour as we cannot implement dependencies for
4313  *		objects in other databases; we can only support RESTRICT.
4314  *
4315  ****************************************************************************/
4316 
4317 DropTableSpaceStmt: DROP TABLESPACE name
4318 				{
4319 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4320 					n->tablespacename = $3;
4321 					n->missing_ok = false;
4322 					$$ = (Node *) n;
4323 				}
4324 				|  DROP TABLESPACE IF_P EXISTS name
4325 				{
4326 					DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4327 					n->tablespacename = $5;
4328 					n->missing_ok = true;
4329 					$$ = (Node *) n;
4330 				}
4331 		;
4332 
4333 /*****************************************************************************
4334  *
4335  *		QUERY:
4336  *             CREATE EXTENSION extension
4337  *             [ WITH ] [ SCHEMA schema ] [ VERSION version ] [ FROM oldversion ]
4338  *
4339  *****************************************************************************/
4340 
4341 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
4342 				{
4343 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4344 					n->extname = $3;
4345 					n->if_not_exists = false;
4346 					n->options = $5;
4347 					$$ = (Node *) n;
4348 				}
4349 				| CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
4350 				{
4351 					CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4352 					n->extname = $6;
4353 					n->if_not_exists = true;
4354 					n->options = $8;
4355 					$$ = (Node *) n;
4356 				}
4357 		;
4358 
4359 create_extension_opt_list:
4360 			create_extension_opt_list create_extension_opt_item
4361 				{ $$ = lappend($1, $2); }
4362 			| /* EMPTY */
4363 				{ $$ = NIL; }
4364 		;
4365 
4366 create_extension_opt_item:
4367 			SCHEMA name
4368 				{
4369 					$$ = makeDefElem("schema", (Node *)makeString($2), @1);
4370 				}
4371 			| VERSION_P NonReservedWord_or_Sconst
4372 				{
4373 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4374 				}
4375 			| FROM NonReservedWord_or_Sconst
4376 				{
4377 					$$ = makeDefElem("old_version", (Node *)makeString($2), @1);
4378 				}
4379 			| CASCADE
4380 				{
4381 					$$ = makeDefElem("cascade", (Node *)makeInteger(TRUE), @1);
4382 				}
4383 		;
4384 
4385 /*****************************************************************************
4386  *
4387  * ALTER EXTENSION name UPDATE [ TO version ]
4388  *
4389  *****************************************************************************/
4390 
4391 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
4392 				{
4393 					AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
4394 					n->extname = $3;
4395 					n->options = $5;
4396 					$$ = (Node *) n;
4397 				}
4398 		;
4399 
4400 alter_extension_opt_list:
4401 			alter_extension_opt_list alter_extension_opt_item
4402 				{ $$ = lappend($1, $2); }
4403 			| /* EMPTY */
4404 				{ $$ = NIL; }
4405 		;
4406 
4407 alter_extension_opt_item:
4408 			TO NonReservedWord_or_Sconst
4409 				{
4410 					$$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4411 				}
4412 		;
4413 
4414 /*****************************************************************************
4415  *
4416  * ALTER EXTENSION name ADD/DROP object-identifier
4417  *
4418  *****************************************************************************/
4419 
4420 AlterExtensionContentsStmt:
4421 			ALTER EXTENSION name add_drop ACCESS METHOD name
4422 				{
4423 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4424 					n->extname = $3;
4425 					n->action = $4;
4426 					n->objtype = OBJECT_ACCESS_METHOD;
4427 					n->object = (Node *) makeString($7);
4428 					$$ = (Node *)n;
4429 				}
4430 			| ALTER EXTENSION name add_drop AGGREGATE aggregate_with_argtypes
4431 				{
4432 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4433 					n->extname = $3;
4434 					n->action = $4;
4435 					n->objtype = OBJECT_AGGREGATE;
4436 					n->object = (Node *) $6;
4437 					$$ = (Node *)n;
4438 				}
4439 			| ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
4440 				{
4441 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4442 					n->extname = $3;
4443 					n->action = $4;
4444 					n->objtype = OBJECT_CAST;
4445 					n->object = (Node *) list_make2($7, $9);
4446 					$$ = (Node *) n;
4447 				}
4448 			| ALTER EXTENSION name add_drop COLLATION any_name
4449 				{
4450 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4451 					n->extname = $3;
4452 					n->action = $4;
4453 					n->objtype = OBJECT_COLLATION;
4454 					n->object = (Node *) $6;
4455 					$$ = (Node *)n;
4456 				}
4457 			| ALTER EXTENSION name add_drop CONVERSION_P any_name
4458 				{
4459 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4460 					n->extname = $3;
4461 					n->action = $4;
4462 					n->objtype = OBJECT_CONVERSION;
4463 					n->object = (Node *) $6;
4464 					$$ = (Node *)n;
4465 				}
4466 			| ALTER EXTENSION name add_drop DOMAIN_P Typename
4467 				{
4468 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4469 					n->extname = $3;
4470 					n->action = $4;
4471 					n->objtype = OBJECT_DOMAIN;
4472 					n->object = (Node *) $6;
4473 					$$ = (Node *)n;
4474 				}
4475 			| ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
4476 				{
4477 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4478 					n->extname = $3;
4479 					n->action = $4;
4480 					n->objtype = OBJECT_FUNCTION;
4481 					n->object = (Node *) $6;
4482 					$$ = (Node *)n;
4483 				}
4484 			| ALTER EXTENSION name add_drop opt_procedural LANGUAGE name
4485 				{
4486 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4487 					n->extname = $3;
4488 					n->action = $4;
4489 					n->objtype = OBJECT_LANGUAGE;
4490 					n->object = (Node *) makeString($7);
4491 					$$ = (Node *)n;
4492 				}
4493 			| ALTER EXTENSION name add_drop OPERATOR operator_with_argtypes
4494 				{
4495 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4496 					n->extname = $3;
4497 					n->action = $4;
4498 					n->objtype = OBJECT_OPERATOR;
4499 					n->object = (Node *) $6;
4500 					$$ = (Node *)n;
4501 				}
4502 			| ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING access_method
4503 				{
4504 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4505 					n->extname = $3;
4506 					n->action = $4;
4507 					n->objtype = OBJECT_OPCLASS;
4508 					n->object = (Node *) lcons(makeString($9), $7);
4509 					$$ = (Node *)n;
4510 				}
4511 			| ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING access_method
4512 				{
4513 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4514 					n->extname = $3;
4515 					n->action = $4;
4516 					n->objtype = OBJECT_OPFAMILY;
4517 					n->object = (Node *) lcons(makeString($9), $7);
4518 					$$ = (Node *)n;
4519 				}
4520 			| ALTER EXTENSION name add_drop SCHEMA name
4521 				{
4522 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4523 					n->extname = $3;
4524 					n->action = $4;
4525 					n->objtype = OBJECT_SCHEMA;
4526 					n->object = (Node *) makeString($6);
4527 					$$ = (Node *)n;
4528 				}
4529 			| ALTER EXTENSION name add_drop EVENT TRIGGER name
4530 				{
4531 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4532 					n->extname = $3;
4533 					n->action = $4;
4534 					n->objtype = OBJECT_EVENT_TRIGGER;
4535 					n->object = (Node *) makeString($7);
4536 					$$ = (Node *)n;
4537 				}
4538 			| ALTER EXTENSION name add_drop TABLE any_name
4539 				{
4540 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4541 					n->extname = $3;
4542 					n->action = $4;
4543 					n->objtype = OBJECT_TABLE;
4544 					n->object = (Node *) $6;
4545 					$$ = (Node *)n;
4546 				}
4547 			| ALTER EXTENSION name add_drop TEXT_P SEARCH PARSER any_name
4548 				{
4549 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4550 					n->extname = $3;
4551 					n->action = $4;
4552 					n->objtype = OBJECT_TSPARSER;
4553 					n->object = (Node *) $8;
4554 					$$ = (Node *)n;
4555 				}
4556 			| ALTER EXTENSION name add_drop TEXT_P SEARCH DICTIONARY any_name
4557 				{
4558 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4559 					n->extname = $3;
4560 					n->action = $4;
4561 					n->objtype = OBJECT_TSDICTIONARY;
4562 					n->object = (Node *) $8;
4563 					$$ = (Node *)n;
4564 				}
4565 			| ALTER EXTENSION name add_drop TEXT_P SEARCH TEMPLATE any_name
4566 				{
4567 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4568 					n->extname = $3;
4569 					n->action = $4;
4570 					n->objtype = OBJECT_TSTEMPLATE;
4571 					n->object = (Node *) $8;
4572 					$$ = (Node *)n;
4573 				}
4574 			| ALTER EXTENSION name add_drop TEXT_P SEARCH CONFIGURATION any_name
4575 				{
4576 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4577 					n->extname = $3;
4578 					n->action = $4;
4579 					n->objtype = OBJECT_TSCONFIGURATION;
4580 					n->object = (Node *) $8;
4581 					$$ = (Node *)n;
4582 				}
4583 			| ALTER EXTENSION name add_drop SEQUENCE any_name
4584 				{
4585 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4586 					n->extname = $3;
4587 					n->action = $4;
4588 					n->objtype = OBJECT_SEQUENCE;
4589 					n->object = (Node *) $6;
4590 					$$ = (Node *)n;
4591 				}
4592 			| ALTER EXTENSION name add_drop VIEW any_name
4593 				{
4594 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4595 					n->extname = $3;
4596 					n->action = $4;
4597 					n->objtype = OBJECT_VIEW;
4598 					n->object = (Node *) $6;
4599 					$$ = (Node *)n;
4600 				}
4601 			| ALTER EXTENSION name add_drop MATERIALIZED VIEW any_name
4602 				{
4603 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4604 					n->extname = $3;
4605 					n->action = $4;
4606 					n->objtype = OBJECT_MATVIEW;
4607 					n->object = (Node *) $7;
4608 					$$ = (Node *)n;
4609 				}
4610 			| ALTER EXTENSION name add_drop FOREIGN TABLE any_name
4611 				{
4612 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4613 					n->extname = $3;
4614 					n->action = $4;
4615 					n->objtype = OBJECT_FOREIGN_TABLE;
4616 					n->object = (Node *) $7;
4617 					$$ = (Node *)n;
4618 				}
4619 			| ALTER EXTENSION name add_drop FOREIGN DATA_P WRAPPER name
4620 				{
4621 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4622 					n->extname = $3;
4623 					n->action = $4;
4624 					n->objtype = OBJECT_FDW;
4625 					n->object = (Node *) makeString($8);
4626 					$$ = (Node *)n;
4627 				}
4628 			| ALTER EXTENSION name add_drop SERVER name
4629 				{
4630 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4631 					n->extname = $3;
4632 					n->action = $4;
4633 					n->objtype = OBJECT_FOREIGN_SERVER;
4634 					n->object = (Node *) makeString($6);
4635 					$$ = (Node *)n;
4636 				}
4637 			| ALTER EXTENSION name add_drop TRANSFORM FOR Typename LANGUAGE name
4638 				{
4639 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4640 					n->extname = $3;
4641 					n->action = $4;
4642 					n->objtype = OBJECT_TRANSFORM;
4643 					n->object = (Node *) list_make2($7, makeString($9));
4644 					$$ = (Node *)n;
4645 				}
4646 			| ALTER EXTENSION name add_drop TYPE_P Typename
4647 				{
4648 					AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4649 					n->extname = $3;
4650 					n->action = $4;
4651 					n->objtype = OBJECT_TYPE;
4652 					n->object = (Node *) $6;
4653 					$$ = (Node *)n;
4654 				}
4655 		;
4656 
4657 /*****************************************************************************
4658  *
4659  *		QUERY:
4660  *             CREATE FOREIGN DATA WRAPPER name options
4661  *
4662  *****************************************************************************/
4663 
4664 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
4665 				{
4666 					CreateFdwStmt *n = makeNode(CreateFdwStmt);
4667 					n->fdwname = $5;
4668 					n->func_options = $6;
4669 					n->options = $7;
4670 					$$ = (Node *) n;
4671 				}
4672 		;
4673 
4674 fdw_option:
4675 			HANDLER handler_name				{ $$ = makeDefElem("handler", (Node *)$2, @1); }
4676 			| NO HANDLER						{ $$ = makeDefElem("handler", NULL, @1); }
4677 			| VALIDATOR handler_name			{ $$ = makeDefElem("validator", (Node *)$2, @1); }
4678 			| NO VALIDATOR						{ $$ = makeDefElem("validator", NULL, @1); }
4679 		;
4680 
4681 fdw_options:
4682 			fdw_option							{ $$ = list_make1($1); }
4683 			| fdw_options fdw_option			{ $$ = lappend($1, $2); }
4684 		;
4685 
4686 opt_fdw_options:
4687 			fdw_options							{ $$ = $1; }
4688 			| /*EMPTY*/							{ $$ = NIL; }
4689 		;
4690 
4691 /*****************************************************************************
4692  *
4693  *		QUERY :
4694  *				ALTER FOREIGN DATA WRAPPER name options
4695  *
4696  ****************************************************************************/
4697 
4698 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
4699 				{
4700 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4701 					n->fdwname = $5;
4702 					n->func_options = $6;
4703 					n->options = $7;
4704 					$$ = (Node *) n;
4705 				}
4706 			| ALTER FOREIGN DATA_P WRAPPER name fdw_options
4707 				{
4708 					AlterFdwStmt *n = makeNode(AlterFdwStmt);
4709 					n->fdwname = $5;
4710 					n->func_options = $6;
4711 					n->options = NIL;
4712 					$$ = (Node *) n;
4713 				}
4714 		;
4715 
4716 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
4717 create_generic_options:
4718 			OPTIONS '(' generic_option_list ')'			{ $$ = $3; }
4719 			| /*EMPTY*/									{ $$ = NIL; }
4720 		;
4721 
4722 generic_option_list:
4723 			generic_option_elem
4724 				{
4725 					$$ = list_make1($1);
4726 				}
4727 			| generic_option_list ',' generic_option_elem
4728 				{
4729 					$$ = lappend($1, $3);
4730 				}
4731 		;
4732 
4733 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
4734 alter_generic_options:
4735 			OPTIONS	'(' alter_generic_option_list ')'		{ $$ = $3; }
4736 		;
4737 
4738 alter_generic_option_list:
4739 			alter_generic_option_elem
4740 				{
4741 					$$ = list_make1($1);
4742 				}
4743 			| alter_generic_option_list ',' alter_generic_option_elem
4744 				{
4745 					$$ = lappend($1, $3);
4746 				}
4747 		;
4748 
4749 alter_generic_option_elem:
4750 			generic_option_elem
4751 				{
4752 					$$ = $1;
4753 				}
4754 			| SET generic_option_elem
4755 				{
4756 					$$ = $2;
4757 					$$->defaction = DEFELEM_SET;
4758 				}
4759 			| ADD_P generic_option_elem
4760 				{
4761 					$$ = $2;
4762 					$$->defaction = DEFELEM_ADD;
4763 				}
4764 			| DROP generic_option_name
4765 				{
4766 					$$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP, @2);
4767 				}
4768 		;
4769 
4770 generic_option_elem:
4771 			generic_option_name generic_option_arg
4772 				{
4773 					$$ = makeDefElem($1, $2, @1);
4774 				}
4775 		;
4776 
4777 generic_option_name:
4778 				ColLabel			{ $$ = $1; }
4779 		;
4780 
4781 /* We could use def_arg here, but the spec only requires string literals */
4782 generic_option_arg:
4783 				Sconst				{ $$ = (Node *) makeString($1); }
4784 		;
4785 
4786 /*****************************************************************************
4787  *
4788  *		QUERY:
4789  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
4790  *
4791  *****************************************************************************/
4792 
4793 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
4794 						 FOREIGN DATA_P WRAPPER name create_generic_options
4795 				{
4796 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4797 					n->servername = $3;
4798 					n->servertype = $4;
4799 					n->version = $5;
4800 					n->fdwname = $9;
4801 					n->options = $10;
4802 					n->if_not_exists = false;
4803 					$$ = (Node *) n;
4804 				}
4805 				| CREATE SERVER IF_P NOT EXISTS name opt_type opt_foreign_server_version
4806 						 FOREIGN DATA_P WRAPPER name create_generic_options
4807 				{
4808 					CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4809 					n->servername = $6;
4810 					n->servertype = $7;
4811 					n->version = $8;
4812 					n->fdwname = $12;
4813 					n->options = $13;
4814 					n->if_not_exists = true;
4815 					$$ = (Node *) n;
4816 				}
4817 		;
4818 
4819 opt_type:
4820 			TYPE_P Sconst			{ $$ = $2; }
4821 			| /*EMPTY*/				{ $$ = NULL; }
4822 		;
4823 
4824 
4825 foreign_server_version:
4826 			VERSION_P Sconst		{ $$ = $2; }
4827 		|	VERSION_P NULL_P		{ $$ = NULL; }
4828 		;
4829 
4830 opt_foreign_server_version:
4831 			foreign_server_version	{ $$ = $1; }
4832 			| /*EMPTY*/				{ $$ = NULL; }
4833 		;
4834 
4835 /*****************************************************************************
4836  *
4837  *		QUERY :
4838  *				ALTER SERVER name [VERSION] [OPTIONS]
4839  *
4840  ****************************************************************************/
4841 
4842 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
4843 				{
4844 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4845 					n->servername = $3;
4846 					n->version = $4;
4847 					n->options = $5;
4848 					n->has_version = true;
4849 					$$ = (Node *) n;
4850 				}
4851 			| ALTER SERVER name foreign_server_version
4852 				{
4853 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4854 					n->servername = $3;
4855 					n->version = $4;
4856 					n->has_version = true;
4857 					$$ = (Node *) n;
4858 				}
4859 			| ALTER SERVER name alter_generic_options
4860 				{
4861 					AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
4862 					n->servername = $3;
4863 					n->options = $4;
4864 					$$ = (Node *) n;
4865 				}
4866 		;
4867 
4868 /*****************************************************************************
4869  *
4870  *		QUERY:
4871  *             CREATE FOREIGN TABLE relname (...) SERVER name (...)
4872  *
4873  *****************************************************************************/
4874 
4875 CreateForeignTableStmt:
4876 		CREATE FOREIGN TABLE qualified_name
4877 			'(' OptTableElementList ')'
4878 			OptInherit SERVER name create_generic_options
4879 				{
4880 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4881 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
4882 					n->base.relation = $4;
4883 					n->base.tableElts = $6;
4884 					n->base.inhRelations = $8;
4885 					n->base.ofTypename = NULL;
4886 					n->base.constraints = NIL;
4887 					n->base.options = NIL;
4888 					n->base.oncommit = ONCOMMIT_NOOP;
4889 					n->base.tablespacename = NULL;
4890 					n->base.if_not_exists = false;
4891 					/* FDW-specific data */
4892 					n->servername = $10;
4893 					n->options = $11;
4894 					$$ = (Node *) n;
4895 				}
4896 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
4897 			'(' OptTableElementList ')'
4898 			OptInherit SERVER name create_generic_options
4899 				{
4900 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4901 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
4902 					n->base.relation = $7;
4903 					n->base.tableElts = $9;
4904 					n->base.inhRelations = $11;
4905 					n->base.ofTypename = NULL;
4906 					n->base.constraints = NIL;
4907 					n->base.options = NIL;
4908 					n->base.oncommit = ONCOMMIT_NOOP;
4909 					n->base.tablespacename = NULL;
4910 					n->base.if_not_exists = true;
4911 					/* FDW-specific data */
4912 					n->servername = $13;
4913 					n->options = $14;
4914 					$$ = (Node *) n;
4915 				}
4916 		| CREATE FOREIGN TABLE qualified_name
4917 			PARTITION OF qualified_name OptTypedTableElementList ForValues
4918 			SERVER name create_generic_options
4919 				{
4920 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4921 					$4->relpersistence = RELPERSISTENCE_PERMANENT;
4922 					n->base.relation = $4;
4923 					n->base.inhRelations = list_make1($7);
4924 					n->base.tableElts = $8;
4925 					n->base.partbound = $9;
4926 					n->base.ofTypename = NULL;
4927 					n->base.constraints = NIL;
4928 					n->base.options = NIL;
4929 					n->base.oncommit = ONCOMMIT_NOOP;
4930 					n->base.tablespacename = NULL;
4931 					n->base.if_not_exists = false;
4932 					/* FDW-specific data */
4933 					n->servername = $11;
4934 					n->options = $12;
4935 					$$ = (Node *) n;
4936 				}
4937 		| CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
4938 			PARTITION OF qualified_name OptTypedTableElementList ForValues
4939 			SERVER name create_generic_options
4940 				{
4941 					CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
4942 					$7->relpersistence = RELPERSISTENCE_PERMANENT;
4943 					n->base.relation = $7;
4944 					n->base.inhRelations = list_make1($10);
4945 					n->base.tableElts = $11;
4946 					n->base.partbound = $12;
4947 					n->base.ofTypename = NULL;
4948 					n->base.constraints = NIL;
4949 					n->base.options = NIL;
4950 					n->base.oncommit = ONCOMMIT_NOOP;
4951 					n->base.tablespacename = NULL;
4952 					n->base.if_not_exists = true;
4953 					/* FDW-specific data */
4954 					n->servername = $14;
4955 					n->options = $15;
4956 					$$ = (Node *) n;
4957 				}
4958 		;
4959 
4960 /*****************************************************************************
4961  *
4962  *		QUERY:
4963  *             ALTER FOREIGN TABLE relname [...]
4964  *
4965  *****************************************************************************/
4966 
4967 AlterForeignTableStmt:
4968 			ALTER FOREIGN TABLE relation_expr alter_table_cmds
4969 				{
4970 					AlterTableStmt *n = makeNode(AlterTableStmt);
4971 					n->relation = $4;
4972 					n->cmds = $5;
4973 					n->relkind = OBJECT_FOREIGN_TABLE;
4974 					n->missing_ok = false;
4975 					$$ = (Node *)n;
4976 				}
4977 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
4978 				{
4979 					AlterTableStmt *n = makeNode(AlterTableStmt);
4980 					n->relation = $6;
4981 					n->cmds = $7;
4982 					n->relkind = OBJECT_FOREIGN_TABLE;
4983 					n->missing_ok = true;
4984 					$$ = (Node *)n;
4985 				}
4986 		;
4987 
4988 /*****************************************************************************
4989  *
4990  *		QUERY:
4991  *				IMPORT FOREIGN SCHEMA remote_schema
4992  *				[ { LIMIT TO | EXCEPT } ( table_list ) ]
4993  *				FROM SERVER server_name INTO local_schema [ OPTIONS (...) ]
4994  *
4995  ****************************************************************************/
4996 
4997 ImportForeignSchemaStmt:
4998 		IMPORT_P FOREIGN SCHEMA name import_qualification
4999 		  FROM SERVER name INTO name create_generic_options
5000 			{
5001 				ImportForeignSchemaStmt *n = makeNode(ImportForeignSchemaStmt);
5002 				n->server_name = $8;
5003 				n->remote_schema = $4;
5004 				n->local_schema = $10;
5005 				n->list_type = $5->type;
5006 				n->table_list = $5->table_names;
5007 				n->options = $11;
5008 				$$ = (Node *) n;
5009 			}
5010 		;
5011 
5012 import_qualification_type:
5013 		LIMIT TO 				{ $$ = FDW_IMPORT_SCHEMA_LIMIT_TO; }
5014 		| EXCEPT 				{ $$ = FDW_IMPORT_SCHEMA_EXCEPT; }
5015 		;
5016 
5017 import_qualification:
5018 		import_qualification_type '(' relation_expr_list ')'
5019 			{
5020 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5021 				n->type = $1;
5022 				n->table_names = $3;
5023 				$$ = n;
5024 			}
5025 		| /*EMPTY*/
5026 			{
5027 				ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5028 				n->type = FDW_IMPORT_SCHEMA_ALL;
5029 				n->table_names = NIL;
5030 				$$ = n;
5031 			}
5032 		;
5033 
5034 /*****************************************************************************
5035  *
5036  *		QUERY:
5037  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
5038  *
5039  *****************************************************************************/
5040 
5041 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
5042 				{
5043 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5044 					n->user = $5;
5045 					n->servername = $7;
5046 					n->options = $8;
5047 					n->if_not_exists = false;
5048 					$$ = (Node *) n;
5049 				}
5050 				| CREATE USER MAPPING IF_P NOT EXISTS FOR auth_ident SERVER name create_generic_options
5051 				{
5052 					CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5053 					n->user = $8;
5054 					n->servername = $10;
5055 					n->options = $11;
5056 					n->if_not_exists = true;
5057 					$$ = (Node *) n;
5058 				}
5059 		;
5060 
5061 /* User mapping authorization identifier */
5062 auth_ident: RoleSpec			{ $$ = $1; }
5063 			| USER				{ $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1); }
5064 		;
5065 
5066 /*****************************************************************************
5067  *
5068  *		QUERY :
5069  *				DROP USER MAPPING FOR auth_ident SERVER name
5070  *
5071  * XXX you'd think this should have a CASCADE/RESTRICT option, even if it's
5072  * only pro forma; but the SQL standard doesn't show one.
5073  ****************************************************************************/
5074 
5075 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
5076 				{
5077 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5078 					n->user = $5;
5079 					n->servername = $7;
5080 					n->missing_ok = false;
5081 					$$ = (Node *) n;
5082 				}
5083 				|  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
5084 				{
5085 					DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5086 					n->user = $7;
5087 					n->servername = $9;
5088 					n->missing_ok = true;
5089 					$$ = (Node *) n;
5090 				}
5091 		;
5092 
5093 /*****************************************************************************
5094  *
5095  *		QUERY :
5096  *				ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
5097  *
5098  ****************************************************************************/
5099 
5100 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
5101 				{
5102 					AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
5103 					n->user = $5;
5104 					n->servername = $7;
5105 					n->options = $8;
5106 					$$ = (Node *) n;
5107 				}
5108 		;
5109 
5110 /*****************************************************************************
5111  *
5112  *		QUERIES:
5113  *				CREATE POLICY name ON table
5114  *					[AS { PERMISSIVE | RESTRICTIVE } ]
5115  *					[FOR { SELECT | INSERT | UPDATE | DELETE } ]
5116  *					[TO role, ...]
5117  *					[USING (qual)] [WITH CHECK (with check qual)]
5118  *				ALTER POLICY name ON table [TO role, ...]
5119  *					[USING (qual)] [WITH CHECK (with check qual)]
5120  *
5121  *****************************************************************************/
5122 
5123 CreatePolicyStmt:
5124 			CREATE POLICY name ON qualified_name RowSecurityDefaultPermissive
5125 				RowSecurityDefaultForCmd RowSecurityDefaultToRole
5126 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5127 				{
5128 					CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
5129 					n->policy_name = $3;
5130 					n->table = $5;
5131 					n->permissive = $6;
5132 					n->cmd_name = $7;
5133 					n->roles = $8;
5134 					n->qual = $9;
5135 					n->with_check = $10;
5136 					$$ = (Node *) n;
5137 				}
5138 		;
5139 
5140 AlterPolicyStmt:
5141 			ALTER POLICY name ON qualified_name RowSecurityOptionalToRole
5142 				RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5143 				{
5144 					AlterPolicyStmt *n = makeNode(AlterPolicyStmt);
5145 					n->policy_name = $3;
5146 					n->table = $5;
5147 					n->roles = $6;
5148 					n->qual = $7;
5149 					n->with_check = $8;
5150 					$$ = (Node *) n;
5151 				}
5152 		;
5153 
5154 RowSecurityOptionalExpr:
5155 			USING '(' a_expr ')'	{ $$ = $3; }
5156 			| /* EMPTY */			{ $$ = NULL; }
5157 		;
5158 
5159 RowSecurityOptionalWithCheck:
5160 			WITH CHECK '(' a_expr ')'		{ $$ = $4; }
5161 			| /* EMPTY */					{ $$ = NULL; }
5162 		;
5163 
5164 RowSecurityDefaultToRole:
5165 			TO role_list			{ $$ = $2; }
5166 			| /* EMPTY */			{ $$ = list_make1(makeRoleSpec(ROLESPEC_PUBLIC, -1)); }
5167 		;
5168 
5169 RowSecurityOptionalToRole:
5170 			TO role_list			{ $$ = $2; }
5171 			| /* EMPTY */			{ $$ = NULL; }
5172 		;
5173 
5174 RowSecurityDefaultPermissive:
5175 			AS IDENT
5176 				{
5177 					if (strcmp($2, "permissive") == 0)
5178 						$$ = true;
5179 					else if (strcmp($2, "restrictive") == 0)
5180 						$$ = false;
5181 					else
5182 						ereport(ERROR,
5183 								(errcode(ERRCODE_SYNTAX_ERROR),
5184 							 errmsg("unrecognized row security option \"%s\"", $2),
5185 								 errhint("Only PERMISSIVE or RESTRICTIVE policies are supported currently."),
5186 									 parser_errposition(@2)));
5187 
5188 				}
5189 			| /* EMPTY */			{ $$ = true; }
5190 		;
5191 
5192 RowSecurityDefaultForCmd:
5193 			FOR row_security_cmd	{ $$ = $2; }
5194 			| /* EMPTY */			{ $$ = "all"; }
5195 		;
5196 
5197 row_security_cmd:
5198 			ALL				{ $$ = "all"; }
5199 		|	SELECT			{ $$ = "select"; }
5200 		|	INSERT			{ $$ = "insert"; }
5201 		|	UPDATE			{ $$ = "update"; }
5202 		|	DELETE_P		{ $$ = "delete"; }
5203 		;
5204 
5205 /*****************************************************************************
5206  *
5207  *		QUERY:
5208  *             CREATE ACCESS METHOD name HANDLER handler_name
5209  *
5210  *****************************************************************************/
5211 
5212 CreateAmStmt: CREATE ACCESS METHOD name TYPE_P INDEX HANDLER handler_name
5213 				{
5214 					CreateAmStmt *n = makeNode(CreateAmStmt);
5215 					n->amname = $4;
5216 					n->handler_name = $8;
5217 					n->amtype = AMTYPE_INDEX;
5218 					$$ = (Node *) n;
5219 				}
5220 		;
5221 
5222 /*****************************************************************************
5223  *
5224  *		QUERIES :
5225  *				CREATE TRIGGER ...
5226  *
5227  *****************************************************************************/
5228 
5229 CreateTrigStmt:
5230 			CREATE TRIGGER name TriggerActionTime TriggerEvents ON
5231 			qualified_name TriggerReferencing TriggerForSpec TriggerWhen
5232 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
5233 				{
5234 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5235 					n->trigname = $3;
5236 					n->relation = $7;
5237 					n->funcname = $13;
5238 					n->args = $15;
5239 					n->row = $9;
5240 					n->timing = $4;
5241 					n->events = intVal(linitial($5));
5242 					n->columns = (List *) lsecond($5);
5243 					n->whenClause = $10;
5244 					n->transitionRels = $8;
5245 					n->isconstraint  = FALSE;
5246 					n->deferrable	 = FALSE;
5247 					n->initdeferred  = FALSE;
5248 					n->constrrel = NULL;
5249 					$$ = (Node *)n;
5250 				}
5251 			| CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
5252 			qualified_name OptConstrFromTable ConstraintAttributeSpec
5253 			FOR EACH ROW TriggerWhen
5254 			EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
5255 				{
5256 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5257 					n->trigname = $4;
5258 					n->relation = $8;
5259 					n->funcname = $17;
5260 					n->args = $19;
5261 					n->row = TRUE;
5262 					n->timing = TRIGGER_TYPE_AFTER;
5263 					n->events = intVal(linitial($6));
5264 					n->columns = (List *) lsecond($6);
5265 					n->whenClause = $14;
5266 					n->transitionRels = NIL;
5267 					n->isconstraint  = TRUE;
5268 					processCASbits($10, @10, "TRIGGER",
5269 								   &n->deferrable, &n->initdeferred, NULL,
5270 								   NULL, yyscanner);
5271 					n->constrrel = $9;
5272 					$$ = (Node *)n;
5273 				}
5274 		;
5275 
5276 TriggerActionTime:
5277 			BEFORE								{ $$ = TRIGGER_TYPE_BEFORE; }
5278 			| AFTER								{ $$ = TRIGGER_TYPE_AFTER; }
5279 			| INSTEAD OF						{ $$ = TRIGGER_TYPE_INSTEAD; }
5280 		;
5281 
5282 TriggerEvents:
5283 			TriggerOneEvent
5284 				{ $$ = $1; }
5285 			| TriggerEvents OR TriggerOneEvent
5286 				{
5287 					int		events1 = intVal(linitial($1));
5288 					int		events2 = intVal(linitial($3));
5289 					List   *columns1 = (List *) lsecond($1);
5290 					List   *columns2 = (List *) lsecond($3);
5291 
5292 					if (events1 & events2)
5293 						parser_yyerror("duplicate trigger events specified");
5294 					/*
5295 					 * concat'ing the columns lists loses information about
5296 					 * which columns went with which event, but so long as
5297 					 * only UPDATE carries columns and we disallow multiple
5298 					 * UPDATE items, it doesn't matter.  Command execution
5299 					 * should just ignore the columns for non-UPDATE events.
5300 					 */
5301 					$$ = list_make2(makeInteger(events1 | events2),
5302 									list_concat(columns1, columns2));
5303 				}
5304 		;
5305 
5306 TriggerOneEvent:
5307 			INSERT
5308 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
5309 			| DELETE_P
5310 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
5311 			| UPDATE
5312 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
5313 			| UPDATE OF columnList
5314 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
5315 			| TRUNCATE
5316 				{ $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
5317 		;
5318 
5319 TriggerReferencing:
5320 			REFERENCING TriggerTransitions			{ $$ = $2; }
5321 			| /*EMPTY*/								{ $$ = NIL; }
5322 		;
5323 
5324 TriggerTransitions:
5325 			TriggerTransition						{ $$ = list_make1($1); }
5326 			| TriggerTransitions TriggerTransition	{ $$ = lappend($1, $2); }
5327 		;
5328 
5329 TriggerTransition:
5330 			TransitionOldOrNew TransitionRowOrTable opt_as TransitionRelName
5331 				{
5332 					TriggerTransition *n = makeNode(TriggerTransition);
5333 					n->name = $4;
5334 					n->isNew = $1;
5335 					n->isTable = $2;
5336 					$$ = (Node *)n;
5337 				}
5338 		;
5339 
5340 TransitionOldOrNew:
5341 			NEW										{ $$ = TRUE; }
5342 			| OLD									{ $$ = FALSE; }
5343 		;
5344 
5345 TransitionRowOrTable:
5346 			TABLE									{ $$ = TRUE; }
5347 			/*
5348 			 * According to the standard, lack of a keyword here implies ROW.
5349 			 * Support for that would require prohibiting ROW entirely here,
5350 			 * reserving the keyword ROW, and/or requiring AS (instead of
5351 			 * allowing it to be optional, as the standard specifies) as the
5352 			 * next token.  Requiring ROW seems cleanest and easiest to
5353 			 * explain.
5354 			 */
5355 			| ROW									{ $$ = FALSE; }
5356 		;
5357 
5358 TransitionRelName:
5359 			ColId									{ $$ = $1; }
5360 		;
5361 
5362 TriggerForSpec:
5363 			FOR TriggerForOptEach TriggerForType
5364 				{
5365 					$$ = $3;
5366 				}
5367 			| /* EMPTY */
5368 				{
5369 					/*
5370 					 * If ROW/STATEMENT not specified, default to
5371 					 * STATEMENT, per SQL
5372 					 */
5373 					$$ = FALSE;
5374 				}
5375 		;
5376 
5377 TriggerForOptEach:
5378 			EACH									{}
5379 			| /*EMPTY*/								{}
5380 		;
5381 
5382 TriggerForType:
5383 			ROW										{ $$ = TRUE; }
5384 			| STATEMENT								{ $$ = FALSE; }
5385 		;
5386 
5387 TriggerWhen:
5388 			WHEN '(' a_expr ')'						{ $$ = $3; }
5389 			| /*EMPTY*/								{ $$ = NULL; }
5390 		;
5391 
5392 TriggerFuncArgs:
5393 			TriggerFuncArg							{ $$ = list_make1($1); }
5394 			| TriggerFuncArgs ',' TriggerFuncArg	{ $$ = lappend($1, $3); }
5395 			| /*EMPTY*/								{ $$ = NIL; }
5396 		;
5397 
5398 TriggerFuncArg:
5399 			Iconst
5400 				{
5401 					$$ = makeString(psprintf("%d", $1));
5402 				}
5403 			| FCONST								{ $$ = makeString($1); }
5404 			| Sconst								{ $$ = makeString($1); }
5405 			| ColLabel								{ $$ = makeString($1); }
5406 		;
5407 
5408 OptConstrFromTable:
5409 			FROM qualified_name						{ $$ = $2; }
5410 			| /*EMPTY*/								{ $$ = NULL; }
5411 		;
5412 
5413 ConstraintAttributeSpec:
5414 			/*EMPTY*/
5415 				{ $$ = 0; }
5416 			| ConstraintAttributeSpec ConstraintAttributeElem
5417 				{
5418 					/*
5419 					 * We must complain about conflicting options.
5420 					 * We could, but choose not to, complain about redundant
5421 					 * options (ie, where $2's bit is already set in $1).
5422 					 */
5423 					int		newspec = $1 | $2;
5424 
5425 					/* special message for this case */
5426 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED)) == (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED))
5427 						ereport(ERROR,
5428 								(errcode(ERRCODE_SYNTAX_ERROR),
5429 								 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
5430 								 parser_errposition(@2)));
5431 					/* generic message for other conflicts */
5432 					if ((newspec & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE)) == (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE) ||
5433 						(newspec & (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED)) == (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED))
5434 						ereport(ERROR,
5435 								(errcode(ERRCODE_SYNTAX_ERROR),
5436 								 errmsg("conflicting constraint properties"),
5437 								 parser_errposition(@2)));
5438 					$$ = newspec;
5439 				}
5440 		;
5441 
5442 ConstraintAttributeElem:
5443 			NOT DEFERRABLE					{ $$ = CAS_NOT_DEFERRABLE; }
5444 			| DEFERRABLE					{ $$ = CAS_DEFERRABLE; }
5445 			| INITIALLY IMMEDIATE			{ $$ = CAS_INITIALLY_IMMEDIATE; }
5446 			| INITIALLY DEFERRED			{ $$ = CAS_INITIALLY_DEFERRED; }
5447 			| NOT VALID						{ $$ = CAS_NOT_VALID; }
5448 			| NO INHERIT					{ $$ = CAS_NO_INHERIT; }
5449 		;
5450 
5451 
5452 /*****************************************************************************
5453  *
5454  *		QUERIES :
5455  *				CREATE EVENT TRIGGER ...
5456  *				ALTER EVENT TRIGGER ...
5457  *
5458  *****************************************************************************/
5459 
5460 CreateEventTrigStmt:
5461 			CREATE EVENT TRIGGER name ON ColLabel
5462 			EXECUTE PROCEDURE func_name '(' ')'
5463 				{
5464 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5465 					n->trigname = $4;
5466 					n->eventname = $6;
5467 					n->whenclause = NULL;
5468 					n->funcname = $9;
5469 					$$ = (Node *)n;
5470 				}
5471 		  | CREATE EVENT TRIGGER name ON ColLabel
5472 			WHEN event_trigger_when_list
5473 			EXECUTE PROCEDURE func_name '(' ')'
5474 				{
5475 					CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5476 					n->trigname = $4;
5477 					n->eventname = $6;
5478 					n->whenclause = $8;
5479 					n->funcname = $11;
5480 					$$ = (Node *)n;
5481 				}
5482 		;
5483 
5484 event_trigger_when_list:
5485 		  event_trigger_when_item
5486 			{ $$ = list_make1($1); }
5487 		| event_trigger_when_list AND event_trigger_when_item
5488 			{ $$ = lappend($1, $3); }
5489 		;
5490 
5491 event_trigger_when_item:
5492 		ColId IN_P '(' event_trigger_value_list ')'
5493 			{ $$ = makeDefElem($1, (Node *) $4, @1); }
5494 		;
5495 
5496 event_trigger_value_list:
5497 		  SCONST
5498 			{ $$ = list_make1(makeString($1)); }
5499 		| event_trigger_value_list ',' SCONST
5500 			{ $$ = lappend($1, makeString($3)); }
5501 		;
5502 
5503 AlterEventTrigStmt:
5504 			ALTER EVENT TRIGGER name enable_trigger
5505 				{
5506 					AlterEventTrigStmt *n = makeNode(AlterEventTrigStmt);
5507 					n->trigname = $4;
5508 					n->tgenabled = $5;
5509 					$$ = (Node *) n;
5510 				}
5511 		;
5512 
5513 enable_trigger:
5514 			ENABLE_P					{ $$ = TRIGGER_FIRES_ON_ORIGIN; }
5515 			| ENABLE_P REPLICA			{ $$ = TRIGGER_FIRES_ON_REPLICA; }
5516 			| ENABLE_P ALWAYS			{ $$ = TRIGGER_FIRES_ALWAYS; }
5517 			| DISABLE_P					{ $$ = TRIGGER_DISABLED; }
5518 		;
5519 
5520 /*****************************************************************************
5521  *
5522  *		QUERIES :
5523  *				CREATE ASSERTION ...
5524  *				DROP ASSERTION ...
5525  *
5526  *****************************************************************************/
5527 
5528 CreateAssertStmt:
5529 			CREATE ASSERTION name CHECK '(' a_expr ')'
5530 			ConstraintAttributeSpec
5531 				{
5532 					CreateTrigStmt *n = makeNode(CreateTrigStmt);
5533 					n->trigname = $3;
5534 					n->args = list_make1($6);
5535 					n->isconstraint  = TRUE;
5536 					processCASbits($8, @8, "ASSERTION",
5537 								   &n->deferrable, &n->initdeferred, NULL,
5538 								   NULL, yyscanner);
5539 
5540 					ereport(ERROR,
5541 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5542 							 errmsg("CREATE ASSERTION is not yet implemented")));
5543 
5544 					$$ = (Node *)n;
5545 				}
5546 		;
5547 
5548 DropAssertStmt:
5549 			DROP ASSERTION name opt_drop_behavior
5550 				{
5551 					DropStmt *n = makeNode(DropStmt);
5552 					n->objects = NIL;
5553 					n->behavior = $4;
5554 					n->removeType = OBJECT_TRIGGER; /* XXX */
5555 					ereport(ERROR,
5556 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5557 							 errmsg("DROP ASSERTION is not yet implemented")));
5558 					$$ = (Node *) n;
5559 				}
5560 		;
5561 
5562 
5563 /*****************************************************************************
5564  *
5565  *		QUERY :
5566  *				define (aggregate,operator,type)
5567  *
5568  *****************************************************************************/
5569 
5570 DefineStmt:
5571 			CREATE AGGREGATE func_name aggr_args definition
5572 				{
5573 					DefineStmt *n = makeNode(DefineStmt);
5574 					n->kind = OBJECT_AGGREGATE;
5575 					n->oldstyle = false;
5576 					n->defnames = $3;
5577 					n->args = $4;
5578 					n->definition = $5;
5579 					$$ = (Node *)n;
5580 				}
5581 			| CREATE AGGREGATE func_name old_aggr_definition
5582 				{
5583 					/* old-style (pre-8.2) syntax for CREATE AGGREGATE */
5584 					DefineStmt *n = makeNode(DefineStmt);
5585 					n->kind = OBJECT_AGGREGATE;
5586 					n->oldstyle = true;
5587 					n->defnames = $3;
5588 					n->args = NIL;
5589 					n->definition = $4;
5590 					$$ = (Node *)n;
5591 				}
5592 			| CREATE OPERATOR any_operator definition
5593 				{
5594 					DefineStmt *n = makeNode(DefineStmt);
5595 					n->kind = OBJECT_OPERATOR;
5596 					n->oldstyle = false;
5597 					n->defnames = $3;
5598 					n->args = NIL;
5599 					n->definition = $4;
5600 					$$ = (Node *)n;
5601 				}
5602 			| CREATE TYPE_P any_name definition
5603 				{
5604 					DefineStmt *n = makeNode(DefineStmt);
5605 					n->kind = OBJECT_TYPE;
5606 					n->oldstyle = false;
5607 					n->defnames = $3;
5608 					n->args = NIL;
5609 					n->definition = $4;
5610 					$$ = (Node *)n;
5611 				}
5612 			| CREATE TYPE_P any_name
5613 				{
5614 					/* Shell type (identified by lack of definition) */
5615 					DefineStmt *n = makeNode(DefineStmt);
5616 					n->kind = OBJECT_TYPE;
5617 					n->oldstyle = false;
5618 					n->defnames = $3;
5619 					n->args = NIL;
5620 					n->definition = NIL;
5621 					$$ = (Node *)n;
5622 				}
5623 			| CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
5624 				{
5625 					CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
5626 
5627 					/* can't use qualified_name, sigh */
5628 					n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
5629 					n->coldeflist = $6;
5630 					$$ = (Node *)n;
5631 				}
5632 			| CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
5633 				{
5634 					CreateEnumStmt *n = makeNode(CreateEnumStmt);
5635 					n->typeName = $3;
5636 					n->vals = $7;
5637 					$$ = (Node *)n;
5638 				}
5639 			| CREATE TYPE_P any_name AS RANGE definition
5640 				{
5641 					CreateRangeStmt *n = makeNode(CreateRangeStmt);
5642 					n->typeName = $3;
5643 					n->params	= $6;
5644 					$$ = (Node *)n;
5645 				}
5646 			| CREATE TEXT_P SEARCH PARSER any_name definition
5647 				{
5648 					DefineStmt *n = makeNode(DefineStmt);
5649 					n->kind = OBJECT_TSPARSER;
5650 					n->args = NIL;
5651 					n->defnames = $5;
5652 					n->definition = $6;
5653 					$$ = (Node *)n;
5654 				}
5655 			| CREATE TEXT_P SEARCH DICTIONARY any_name definition
5656 				{
5657 					DefineStmt *n = makeNode(DefineStmt);
5658 					n->kind = OBJECT_TSDICTIONARY;
5659 					n->args = NIL;
5660 					n->defnames = $5;
5661 					n->definition = $6;
5662 					$$ = (Node *)n;
5663 				}
5664 			| CREATE TEXT_P SEARCH TEMPLATE any_name definition
5665 				{
5666 					DefineStmt *n = makeNode(DefineStmt);
5667 					n->kind = OBJECT_TSTEMPLATE;
5668 					n->args = NIL;
5669 					n->defnames = $5;
5670 					n->definition = $6;
5671 					$$ = (Node *)n;
5672 				}
5673 			| CREATE TEXT_P SEARCH CONFIGURATION any_name definition
5674 				{
5675 					DefineStmt *n = makeNode(DefineStmt);
5676 					n->kind = OBJECT_TSCONFIGURATION;
5677 					n->args = NIL;
5678 					n->defnames = $5;
5679 					n->definition = $6;
5680 					$$ = (Node *)n;
5681 				}
5682 			| CREATE COLLATION any_name definition
5683 				{
5684 					DefineStmt *n = makeNode(DefineStmt);
5685 					n->kind = OBJECT_COLLATION;
5686 					n->args = NIL;
5687 					n->defnames = $3;
5688 					n->definition = $4;
5689 					$$ = (Node *)n;
5690 				}
5691 			| CREATE COLLATION IF_P NOT EXISTS any_name definition
5692 				{
5693 					DefineStmt *n = makeNode(DefineStmt);
5694 					n->kind = OBJECT_COLLATION;
5695 					n->args = NIL;
5696 					n->defnames = $6;
5697 					n->definition = $7;
5698 					n->if_not_exists = true;
5699 					$$ = (Node *)n;
5700 				}
5701 			| CREATE COLLATION any_name FROM any_name
5702 				{
5703 					DefineStmt *n = makeNode(DefineStmt);
5704 					n->kind = OBJECT_COLLATION;
5705 					n->args = NIL;
5706 					n->defnames = $3;
5707 					n->definition = list_make1(makeDefElem("from", (Node *) $5, @5));
5708 					$$ = (Node *)n;
5709 				}
5710 			| CREATE COLLATION IF_P NOT EXISTS any_name FROM any_name
5711 				{
5712 					DefineStmt *n = makeNode(DefineStmt);
5713 					n->kind = OBJECT_COLLATION;
5714 					n->args = NIL;
5715 					n->defnames = $6;
5716 					n->definition = list_make1(makeDefElem("from", (Node *) $8, @8));
5717 					n->if_not_exists = true;
5718 					$$ = (Node *)n;
5719 				}
5720 		;
5721 
5722 definition: '(' def_list ')'						{ $$ = $2; }
5723 		;
5724 
5725 def_list:	def_elem								{ $$ = list_make1($1); }
5726 			| def_list ',' def_elem					{ $$ = lappend($1, $3); }
5727 		;
5728 
5729 def_elem:	ColLabel '=' def_arg
5730 				{
5731 					$$ = makeDefElem($1, (Node *) $3, @1);
5732 				}
5733 			| ColLabel
5734 				{
5735 					$$ = makeDefElem($1, NULL, @1);
5736 				}
5737 		;
5738 
5739 /* Note: any simple identifier will be returned as a type name! */
5740 def_arg:	func_type						{ $$ = (Node *)$1; }
5741 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
5742 			| qual_all_Op					{ $$ = (Node *)$1; }
5743 			| NumericOnly					{ $$ = (Node *)$1; }
5744 			| Sconst						{ $$ = (Node *)makeString($1); }
5745 			| NONE							{ $$ = (Node *)makeString(pstrdup($1)); }
5746 		;
5747 
5748 old_aggr_definition: '(' old_aggr_list ')'			{ $$ = $2; }
5749 		;
5750 
5751 old_aggr_list: old_aggr_elem						{ $$ = list_make1($1); }
5752 			| old_aggr_list ',' old_aggr_elem		{ $$ = lappend($1, $3); }
5753 		;
5754 
5755 /*
5756  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
5757  * the item names needed in old aggregate definitions are likely to become
5758  * SQL keywords.
5759  */
5760 old_aggr_elem:  IDENT '=' def_arg
5761 				{
5762 					$$ = makeDefElem($1, (Node *)$3, @1);
5763 				}
5764 		;
5765 
5766 opt_enum_val_list:
5767 		enum_val_list							{ $$ = $1; }
5768 		| /*EMPTY*/								{ $$ = NIL; }
5769 		;
5770 
5771 enum_val_list:	Sconst
5772 				{ $$ = list_make1(makeString($1)); }
5773 			| enum_val_list ',' Sconst
5774 				{ $$ = lappend($1, makeString($3)); }
5775 		;
5776 
5777 /*****************************************************************************
5778  *
5779  *	ALTER TYPE enumtype ADD ...
5780  *
5781  *****************************************************************************/
5782 
5783 AlterEnumStmt:
5784 		ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst
5785 			{
5786 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5787 				n->typeName = $3;
5788 				n->oldVal = NULL;
5789 				n->newVal = $7;
5790 				n->newValNeighbor = NULL;
5791 				n->newValIsAfter = true;
5792 				n->skipIfNewValExists = $6;
5793 				$$ = (Node *) n;
5794 			}
5795 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst BEFORE Sconst
5796 			{
5797 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5798 				n->typeName = $3;
5799 				n->oldVal = NULL;
5800 				n->newVal = $7;
5801 				n->newValNeighbor = $9;
5802 				n->newValIsAfter = false;
5803 				n->skipIfNewValExists = $6;
5804 				$$ = (Node *) n;
5805 			}
5806 		 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst AFTER Sconst
5807 			{
5808 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5809 				n->typeName = $3;
5810 				n->oldVal = NULL;
5811 				n->newVal = $7;
5812 				n->newValNeighbor = $9;
5813 				n->newValIsAfter = true;
5814 				n->skipIfNewValExists = $6;
5815 				$$ = (Node *) n;
5816 			}
5817 		 | ALTER TYPE_P any_name RENAME VALUE_P Sconst TO Sconst
5818 			{
5819 				AlterEnumStmt *n = makeNode(AlterEnumStmt);
5820 				n->typeName = $3;
5821 				n->oldVal = $6;
5822 				n->newVal = $8;
5823 				n->newValNeighbor = NULL;
5824 				n->newValIsAfter = false;
5825 				n->skipIfNewValExists = false;
5826 				$$ = (Node *) n;
5827 			}
5828 		 ;
5829 
5830 opt_if_not_exists: IF_P NOT EXISTS              { $$ = true; }
5831 		| /* empty */                          { $$ = false; }
5832 		;
5833 
5834 
5835 /*****************************************************************************
5836  *
5837  *		QUERIES :
5838  *				CREATE OPERATOR CLASS ...
5839  *				CREATE OPERATOR FAMILY ...
5840  *				ALTER OPERATOR FAMILY ...
5841  *				DROP OPERATOR CLASS ...
5842  *				DROP OPERATOR FAMILY ...
5843  *
5844  *****************************************************************************/
5845 
5846 CreateOpClassStmt:
5847 			CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
5848 			USING access_method opt_opfamily AS opclass_item_list
5849 				{
5850 					CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
5851 					n->opclassname = $4;
5852 					n->isDefault = $5;
5853 					n->datatype = $8;
5854 					n->amname = $10;
5855 					n->opfamilyname = $11;
5856 					n->items = $13;
5857 					$$ = (Node *) n;
5858 				}
5859 		;
5860 
5861 opclass_item_list:
5862 			opclass_item							{ $$ = list_make1($1); }
5863 			| opclass_item_list ',' opclass_item	{ $$ = lappend($1, $3); }
5864 		;
5865 
5866 opclass_item:
5867 			OPERATOR Iconst any_operator opclass_purpose opt_recheck
5868 				{
5869 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5870 					ObjectWithArgs *owa = makeNode(ObjectWithArgs);
5871 					owa->objname = $3;
5872 					owa->objargs = NIL;
5873 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5874 					n->name = owa;
5875 					n->number = $2;
5876 					n->order_family = $4;
5877 					$$ = (Node *) n;
5878 				}
5879 			| OPERATOR Iconst operator_with_argtypes opclass_purpose
5880 			  opt_recheck
5881 				{
5882 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5883 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5884 					n->name = $3;
5885 					n->number = $2;
5886 					n->order_family = $4;
5887 					$$ = (Node *) n;
5888 				}
5889 			| FUNCTION Iconst function_with_argtypes
5890 				{
5891 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5892 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5893 					n->name = $3;
5894 					n->number = $2;
5895 					$$ = (Node *) n;
5896 				}
5897 			| FUNCTION Iconst '(' type_list ')' function_with_argtypes
5898 				{
5899 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5900 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5901 					n->name = $6;
5902 					n->number = $2;
5903 					n->class_args = $4;
5904 					$$ = (Node *) n;
5905 				}
5906 			| STORAGE Typename
5907 				{
5908 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5909 					n->itemtype = OPCLASS_ITEM_STORAGETYPE;
5910 					n->storedtype = $2;
5911 					$$ = (Node *) n;
5912 				}
5913 		;
5914 
5915 opt_default:	DEFAULT						{ $$ = TRUE; }
5916 			| /*EMPTY*/						{ $$ = FALSE; }
5917 		;
5918 
5919 opt_opfamily:	FAMILY any_name				{ $$ = $2; }
5920 			| /*EMPTY*/						{ $$ = NIL; }
5921 		;
5922 
5923 opclass_purpose: FOR SEARCH					{ $$ = NIL; }
5924 			| FOR ORDER BY any_name			{ $$ = $4; }
5925 			| /*EMPTY*/						{ $$ = NIL; }
5926 		;
5927 
5928 opt_recheck:	RECHECK
5929 				{
5930 					/*
5931 					 * RECHECK no longer does anything in opclass definitions,
5932 					 * but we still accept it to ease porting of old database
5933 					 * dumps.
5934 					 */
5935 					ereport(NOTICE,
5936 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5937 							 errmsg("RECHECK is no longer required"),
5938 							 errhint("Update your data type."),
5939 							 parser_errposition(@1)));
5940 					$$ = TRUE;
5941 				}
5942 			| /*EMPTY*/						{ $$ = FALSE; }
5943 		;
5944 
5945 
5946 CreateOpFamilyStmt:
5947 			CREATE OPERATOR FAMILY any_name USING access_method
5948 				{
5949 					CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
5950 					n->opfamilyname = $4;
5951 					n->amname = $6;
5952 					$$ = (Node *) n;
5953 				}
5954 		;
5955 
5956 AlterOpFamilyStmt:
5957 			ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
5958 				{
5959 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
5960 					n->opfamilyname = $4;
5961 					n->amname = $6;
5962 					n->isDrop = false;
5963 					n->items = $8;
5964 					$$ = (Node *) n;
5965 				}
5966 			| ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
5967 				{
5968 					AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
5969 					n->opfamilyname = $4;
5970 					n->amname = $6;
5971 					n->isDrop = true;
5972 					n->items = $8;
5973 					$$ = (Node *) n;
5974 				}
5975 		;
5976 
5977 opclass_drop_list:
5978 			opclass_drop							{ $$ = list_make1($1); }
5979 			| opclass_drop_list ',' opclass_drop	{ $$ = lappend($1, $3); }
5980 		;
5981 
5982 opclass_drop:
5983 			OPERATOR Iconst '(' type_list ')'
5984 				{
5985 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5986 					n->itemtype = OPCLASS_ITEM_OPERATOR;
5987 					n->number = $2;
5988 					n->class_args = $4;
5989 					$$ = (Node *) n;
5990 				}
5991 			| FUNCTION Iconst '(' type_list ')'
5992 				{
5993 					CreateOpClassItem *n = makeNode(CreateOpClassItem);
5994 					n->itemtype = OPCLASS_ITEM_FUNCTION;
5995 					n->number = $2;
5996 					n->class_args = $4;
5997 					$$ = (Node *) n;
5998 				}
5999 		;
6000 
6001 
6002 DropOpClassStmt:
6003 			DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
6004 				{
6005 					DropStmt *n = makeNode(DropStmt);
6006 					n->objects = list_make1(lcons(makeString($6), $4));
6007 					n->removeType = OBJECT_OPCLASS;
6008 					n->behavior = $7;
6009 					n->missing_ok = false;
6010 					n->concurrent = false;
6011 					$$ = (Node *) n;
6012 				}
6013 			| DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
6014 				{
6015 					DropStmt *n = makeNode(DropStmt);
6016 					n->objects = list_make1(lcons(makeString($8), $6));
6017 					n->removeType = OBJECT_OPCLASS;
6018 					n->behavior = $9;
6019 					n->missing_ok = true;
6020 					n->concurrent = false;
6021 					$$ = (Node *) n;
6022 				}
6023 		;
6024 
6025 DropOpFamilyStmt:
6026 			DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
6027 				{
6028 					DropStmt *n = makeNode(DropStmt);
6029 					n->objects = list_make1(lcons(makeString($6), $4));
6030 					n->removeType = OBJECT_OPFAMILY;
6031 					n->behavior = $7;
6032 					n->missing_ok = false;
6033 					n->concurrent = false;
6034 					$$ = (Node *) n;
6035 				}
6036 			| DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
6037 				{
6038 					DropStmt *n = makeNode(DropStmt);
6039 					n->objects = list_make1(lcons(makeString($8), $6));
6040 					n->removeType = OBJECT_OPFAMILY;
6041 					n->behavior = $9;
6042 					n->missing_ok = true;
6043 					n->concurrent = false;
6044 					$$ = (Node *) n;
6045 				}
6046 		;
6047 
6048 
6049 /*****************************************************************************
6050  *
6051  *		QUERY:
6052  *
6053  *		DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
6054  *		REASSIGN OWNED BY username [, username ...] TO username
6055  *
6056  *****************************************************************************/
6057 DropOwnedStmt:
6058 			DROP OWNED BY role_list opt_drop_behavior
6059 				{
6060 					DropOwnedStmt *n = makeNode(DropOwnedStmt);
6061 					n->roles = $4;
6062 					n->behavior = $5;
6063 					$$ = (Node *)n;
6064 				}
6065 		;
6066 
6067 ReassignOwnedStmt:
6068 			REASSIGN OWNED BY role_list TO RoleSpec
6069 				{
6070 					ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
6071 					n->roles = $4;
6072 					n->newrole = $6;
6073 					$$ = (Node *)n;
6074 				}
6075 		;
6076 
6077 /*****************************************************************************
6078  *
6079  *		QUERY:
6080  *
6081  *		DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
6082  *           [ RESTRICT | CASCADE ]
6083  *
6084  *****************************************************************************/
6085 
6086 DropStmt:	DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
6087 				{
6088 					DropStmt *n = makeNode(DropStmt);
6089 					n->removeType = $2;
6090 					n->missing_ok = TRUE;
6091 					n->objects = $5;
6092 					n->behavior = $6;
6093 					n->concurrent = false;
6094 					$$ = (Node *)n;
6095 				}
6096 			| DROP drop_type_any_name any_name_list opt_drop_behavior
6097 				{
6098 					DropStmt *n = makeNode(DropStmt);
6099 					n->removeType = $2;
6100 					n->missing_ok = FALSE;
6101 					n->objects = $3;
6102 					n->behavior = $4;
6103 					n->concurrent = false;
6104 					$$ = (Node *)n;
6105 				}
6106 			| DROP drop_type_name IF_P EXISTS name_list opt_drop_behavior
6107 				{
6108 					DropStmt *n = makeNode(DropStmt);
6109 					n->removeType = $2;
6110 					n->missing_ok = TRUE;
6111 					n->objects = $5;
6112 					n->behavior = $6;
6113 					n->concurrent = false;
6114 					$$ = (Node *)n;
6115 				}
6116 			| DROP drop_type_name name_list opt_drop_behavior
6117 				{
6118 					DropStmt *n = makeNode(DropStmt);
6119 					n->removeType = $2;
6120 					n->missing_ok = FALSE;
6121 					n->objects = $3;
6122 					n->behavior = $4;
6123 					n->concurrent = false;
6124 					$$ = (Node *)n;
6125 				}
6126 			| DROP drop_type_name_on_any_name name ON any_name opt_drop_behavior
6127 				{
6128 					DropStmt *n = makeNode(DropStmt);
6129 					n->removeType = $2;
6130 					n->objects = list_make1(lappend($5, makeString($3)));
6131 					n->behavior = $6;
6132 					n->missing_ok = false;
6133 					n->concurrent = false;
6134 					$$ = (Node *) n;
6135 				}
6136 			| DROP drop_type_name_on_any_name IF_P EXISTS name ON any_name opt_drop_behavior
6137 				{
6138 					DropStmt *n = makeNode(DropStmt);
6139 					n->removeType = $2;
6140 					n->objects = list_make1(lappend($7, makeString($5)));
6141 					n->behavior = $8;
6142 					n->missing_ok = true;
6143 					n->concurrent = false;
6144 					$$ = (Node *) n;
6145 				}
6146 			| DROP TYPE_P type_name_list opt_drop_behavior
6147 				{
6148 					DropStmt *n = makeNode(DropStmt);
6149 					n->removeType = OBJECT_TYPE;
6150 					n->missing_ok = FALSE;
6151 					n->objects = $3;
6152 					n->behavior = $4;
6153 					n->concurrent = false;
6154 					$$ = (Node *) n;
6155 				}
6156 			| DROP TYPE_P IF_P EXISTS type_name_list opt_drop_behavior
6157 				{
6158 					DropStmt *n = makeNode(DropStmt);
6159 					n->removeType = OBJECT_TYPE;
6160 					n->missing_ok = TRUE;
6161 					n->objects = $5;
6162 					n->behavior = $6;
6163 					n->concurrent = false;
6164 					$$ = (Node *) n;
6165 				}
6166 			| DROP DOMAIN_P type_name_list opt_drop_behavior
6167 				{
6168 					DropStmt *n = makeNode(DropStmt);
6169 					n->removeType = OBJECT_DOMAIN;
6170 					n->missing_ok = FALSE;
6171 					n->objects = $3;
6172 					n->behavior = $4;
6173 					n->concurrent = false;
6174 					$$ = (Node *) n;
6175 				}
6176 			| DROP DOMAIN_P IF_P EXISTS type_name_list opt_drop_behavior
6177 				{
6178 					DropStmt *n = makeNode(DropStmt);
6179 					n->removeType = OBJECT_DOMAIN;
6180 					n->missing_ok = TRUE;
6181 					n->objects = $5;
6182 					n->behavior = $6;
6183 					n->concurrent = false;
6184 					$$ = (Node *) n;
6185 				}
6186 			| DROP INDEX CONCURRENTLY any_name_list opt_drop_behavior
6187 				{
6188 					DropStmt *n = makeNode(DropStmt);
6189 					n->removeType = OBJECT_INDEX;
6190 					n->missing_ok = FALSE;
6191 					n->objects = $4;
6192 					n->behavior = $5;
6193 					n->concurrent = true;
6194 					$$ = (Node *)n;
6195 				}
6196 			| DROP INDEX CONCURRENTLY IF_P EXISTS any_name_list opt_drop_behavior
6197 				{
6198 					DropStmt *n = makeNode(DropStmt);
6199 					n->removeType = OBJECT_INDEX;
6200 					n->missing_ok = TRUE;
6201 					n->objects = $6;
6202 					n->behavior = $7;
6203 					n->concurrent = true;
6204 					$$ = (Node *)n;
6205 				}
6206 		;
6207 
6208 /* object types taking any_name_list */
6209 drop_type_any_name:
6210 			TABLE									{ $$ = OBJECT_TABLE; }
6211 			| SEQUENCE								{ $$ = OBJECT_SEQUENCE; }
6212 			| VIEW									{ $$ = OBJECT_VIEW; }
6213 			| MATERIALIZED VIEW						{ $$ = OBJECT_MATVIEW; }
6214 			| INDEX									{ $$ = OBJECT_INDEX; }
6215 			| FOREIGN TABLE							{ $$ = OBJECT_FOREIGN_TABLE; }
6216 			| COLLATION								{ $$ = OBJECT_COLLATION; }
6217 			| CONVERSION_P							{ $$ = OBJECT_CONVERSION; }
6218 			| STATISTICS							{ $$ = OBJECT_STATISTIC_EXT; }
6219 			| TEXT_P SEARCH PARSER					{ $$ = OBJECT_TSPARSER; }
6220 			| TEXT_P SEARCH DICTIONARY				{ $$ = OBJECT_TSDICTIONARY; }
6221 			| TEXT_P SEARCH TEMPLATE				{ $$ = OBJECT_TSTEMPLATE; }
6222 			| TEXT_P SEARCH CONFIGURATION			{ $$ = OBJECT_TSCONFIGURATION; }
6223 		;
6224 
6225 /* object types taking name_list */
6226 drop_type_name:
6227 			ACCESS METHOD							{ $$ = OBJECT_ACCESS_METHOD; }
6228 			| EVENT TRIGGER							{ $$ = OBJECT_EVENT_TRIGGER; }
6229 			| EXTENSION								{ $$ = OBJECT_EXTENSION; }
6230 			| FOREIGN DATA_P WRAPPER				{ $$ = OBJECT_FDW; }
6231 			| PUBLICATION							{ $$ = OBJECT_PUBLICATION; }
6232 			| SCHEMA								{ $$ = OBJECT_SCHEMA; }
6233 			| SERVER								{ $$ = OBJECT_FOREIGN_SERVER; }
6234 		;
6235 
6236 /* object types attached to a table */
6237 drop_type_name_on_any_name:
6238 			POLICY									{ $$ = OBJECT_POLICY; }
6239 			| RULE									{ $$ = OBJECT_RULE; }
6240 			| TRIGGER								{ $$ = OBJECT_TRIGGER; }
6241 		;
6242 
6243 any_name_list:
6244 			any_name								{ $$ = list_make1($1); }
6245 			| any_name_list ',' any_name			{ $$ = lappend($1, $3); }
6246 		;
6247 
6248 any_name:	ColId						{ $$ = list_make1(makeString($1)); }
6249 			| ColId attrs				{ $$ = lcons(makeString($1), $2); }
6250 		;
6251 
6252 attrs:		'.' attr_name
6253 					{ $$ = list_make1(makeString($2)); }
6254 			| attrs '.' attr_name
6255 					{ $$ = lappend($1, makeString($3)); }
6256 		;
6257 
6258 type_name_list:
6259 			Typename								{ $$ = list_make1($1); }
6260 			| type_name_list ',' Typename			{ $$ = lappend($1, $3); }
6261 
6262 /*****************************************************************************
6263  *
6264  *		QUERY:
6265  *				truncate table relname1, relname2, ...
6266  *
6267  *****************************************************************************/
6268 
6269 TruncateStmt:
6270 			TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
6271 				{
6272 					TruncateStmt *n = makeNode(TruncateStmt);
6273 					n->relations = $3;
6274 					n->restart_seqs = $4;
6275 					n->behavior = $5;
6276 					$$ = (Node *)n;
6277 				}
6278 		;
6279 
6280 opt_restart_seqs:
6281 			CONTINUE_P IDENTITY_P		{ $$ = false; }
6282 			| RESTART IDENTITY_P		{ $$ = true; }
6283 			| /* EMPTY */				{ $$ = false; }
6284 		;
6285 
6286 /*****************************************************************************
6287  *
6288  *	The COMMENT ON statement can take different forms based upon the type of
6289  *	the object associated with the comment. The form of the statement is:
6290  *
6291  *	COMMENT ON [ [ ACCESS METHOD | CONVERSION | COLLATION |
6292  *                 DATABASE | DOMAIN |
6293  *                 EXTENSION | EVENT TRIGGER | FOREIGN DATA WRAPPER |
6294  *                 FOREIGN TABLE | INDEX | [PROCEDURAL] LANGUAGE |
6295  *                 MATERIALIZED VIEW | POLICY | ROLE | SCHEMA | SEQUENCE |
6296  *                 SERVER | TABLE | TABLESPACE |
6297  *                 TEXT SEARCH CONFIGURATION | TEXT SEARCH DICTIONARY |
6298  *                 TEXT SEARCH PARSER | TEXT SEARCH TEMPLATE | TYPE |
6299  *                 VIEW] <objname> |
6300  *				 AGGREGATE <aggname> (arg1, ...) |
6301  *				 CAST (<src type> AS <dst type>) |
6302  *				 COLUMN <relname>.<colname> |
6303  *				 CONSTRAINT <constraintname> ON <relname> |
6304  *				 CONSTRAINT <constraintname> ON DOMAIN <domainname> |
6305  *				 FUNCTION <funcname> (arg1, arg2, ...) |
6306  *				 LARGE OBJECT <oid> |
6307  *				 OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
6308  *				 OPERATOR CLASS <name> USING <access-method> |
6309  *				 OPERATOR FAMILY <name> USING <access-method> |
6310  *				 RULE <rulename> ON <relname> |
6311  *				 TRIGGER <triggername> ON <relname> ]
6312  *			   IS { 'text' | NULL }
6313  *
6314  *****************************************************************************/
6315 
6316 CommentStmt:
6317 			COMMENT ON comment_type_any_name any_name IS comment_text
6318 				{
6319 					CommentStmt *n = makeNode(CommentStmt);
6320 					n->objtype = $3;
6321 					n->object = (Node *) $4;
6322 					n->comment = $6;
6323 					$$ = (Node *) n;
6324 				}
6325 			| COMMENT ON comment_type_name name IS comment_text
6326 				{
6327 					CommentStmt *n = makeNode(CommentStmt);
6328 					n->objtype = $3;
6329 					n->object = (Node *) makeString($4);
6330 					n->comment = $6;
6331 					$$ = (Node *) n;
6332 				}
6333 			| COMMENT ON TYPE_P Typename IS comment_text
6334 				{
6335 					CommentStmt *n = makeNode(CommentStmt);
6336 					n->objtype = OBJECT_TYPE;
6337 					n->object = (Node *) $4;
6338 					n->comment = $6;
6339 					$$ = (Node *) n;
6340 				}
6341 			| COMMENT ON DOMAIN_P Typename IS comment_text
6342 				{
6343 					CommentStmt *n = makeNode(CommentStmt);
6344 					n->objtype = OBJECT_DOMAIN;
6345 					n->object = (Node *) $4;
6346 					n->comment = $6;
6347 					$$ = (Node *) n;
6348 				}
6349 			| COMMENT ON AGGREGATE aggregate_with_argtypes IS comment_text
6350 				{
6351 					CommentStmt *n = makeNode(CommentStmt);
6352 					n->objtype = OBJECT_AGGREGATE;
6353 					n->object = (Node *) $4;
6354 					n->comment = $6;
6355 					$$ = (Node *) n;
6356 				}
6357 			| COMMENT ON FUNCTION function_with_argtypes IS comment_text
6358 				{
6359 					CommentStmt *n = makeNode(CommentStmt);
6360 					n->objtype = OBJECT_FUNCTION;
6361 					n->object = (Node *) $4;
6362 					n->comment = $6;
6363 					$$ = (Node *) n;
6364 				}
6365 			| COMMENT ON OPERATOR operator_with_argtypes IS comment_text
6366 				{
6367 					CommentStmt *n = makeNode(CommentStmt);
6368 					n->objtype = OBJECT_OPERATOR;
6369 					n->object = (Node *) $4;
6370 					n->comment = $6;
6371 					$$ = (Node *) n;
6372 				}
6373 			| COMMENT ON CONSTRAINT name ON any_name IS comment_text
6374 				{
6375 					CommentStmt *n = makeNode(CommentStmt);
6376 					n->objtype = OBJECT_TABCONSTRAINT;
6377 					n->object = (Node *) lappend($6, makeString($4));
6378 					n->comment = $8;
6379 					$$ = (Node *) n;
6380 				}
6381 			| COMMENT ON CONSTRAINT name ON DOMAIN_P any_name IS comment_text
6382 				{
6383 					CommentStmt *n = makeNode(CommentStmt);
6384 					n->objtype = OBJECT_DOMCONSTRAINT;
6385 					/*
6386 					 * should use Typename not any_name in the production, but
6387 					 * there's a shift/reduce conflict if we do that, so fix it
6388 					 * up here.
6389 					 */
6390 					n->object = (Node *) list_make2(makeTypeNameFromNameList($7), makeString($4));
6391 					n->comment = $9;
6392 					$$ = (Node *) n;
6393 				}
6394 			| COMMENT ON POLICY name ON any_name IS comment_text
6395 				{
6396 					CommentStmt *n = makeNode(CommentStmt);
6397 					n->objtype = OBJECT_POLICY;
6398 					n->object = (Node *) lappend($6, makeString($4));
6399 					n->comment = $8;
6400 					$$ = (Node *) n;
6401 				}
6402 			| COMMENT ON RULE name ON any_name IS comment_text
6403 				{
6404 					CommentStmt *n = makeNode(CommentStmt);
6405 					n->objtype = OBJECT_RULE;
6406 					n->object = (Node *) lappend($6, makeString($4));
6407 					n->comment = $8;
6408 					$$ = (Node *) n;
6409 				}
6410 			| COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
6411 				{
6412 					CommentStmt *n = makeNode(CommentStmt);
6413 					n->objtype = OBJECT_TRANSFORM;
6414 					n->object = (Node *) list_make2($5, makeString($7));
6415 					n->comment = $9;
6416 					$$ = (Node *) n;
6417 				}
6418 			| COMMENT ON TRIGGER name ON any_name IS comment_text
6419 				{
6420 					CommentStmt *n = makeNode(CommentStmt);
6421 					n->objtype = OBJECT_TRIGGER;
6422 					n->object = (Node *) lappend($6, makeString($4));
6423 					n->comment = $8;
6424 					$$ = (Node *) n;
6425 				}
6426 			| COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
6427 				{
6428 					CommentStmt *n = makeNode(CommentStmt);
6429 					n->objtype = OBJECT_OPCLASS;
6430 					n->object = (Node *) lcons(makeString($7), $5);
6431 					n->comment = $9;
6432 					$$ = (Node *) n;
6433 				}
6434 			| COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
6435 				{
6436 					CommentStmt *n = makeNode(CommentStmt);
6437 					n->objtype = OBJECT_OPFAMILY;
6438 					n->object = (Node *) lcons(makeString($7), $5);
6439 					n->comment = $9;
6440 					$$ = (Node *) n;
6441 				}
6442 			| COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
6443 				{
6444 					CommentStmt *n = makeNode(CommentStmt);
6445 					n->objtype = OBJECT_LARGEOBJECT;
6446 					n->object = (Node *) $5;
6447 					n->comment = $7;
6448 					$$ = (Node *) n;
6449 				}
6450 			| COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
6451 				{
6452 					CommentStmt *n = makeNode(CommentStmt);
6453 					n->objtype = OBJECT_CAST;
6454 					n->object = (Node *) list_make2($5, $7);
6455 					n->comment = $10;
6456 					$$ = (Node *) n;
6457 				}
6458 		;
6459 
6460 /* object types taking any_name */
6461 comment_type_any_name:
6462 			COLUMN								{ $$ = OBJECT_COLUMN; }
6463 			| INDEX								{ $$ = OBJECT_INDEX; }
6464 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6465 			| STATISTICS						{ $$ = OBJECT_STATISTIC_EXT; }
6466 			| TABLE								{ $$ = OBJECT_TABLE; }
6467 			| VIEW								{ $$ = OBJECT_VIEW; }
6468 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6469 			| COLLATION							{ $$ = OBJECT_COLLATION; }
6470 			| CONVERSION_P						{ $$ = OBJECT_CONVERSION; }
6471 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6472 			| TEXT_P SEARCH CONFIGURATION		{ $$ = OBJECT_TSCONFIGURATION; }
6473 			| TEXT_P SEARCH DICTIONARY			{ $$ = OBJECT_TSDICTIONARY; }
6474 			| TEXT_P SEARCH PARSER				{ $$ = OBJECT_TSPARSER; }
6475 			| TEXT_P SEARCH TEMPLATE			{ $$ = OBJECT_TSTEMPLATE; }
6476 		;
6477 
6478 /* object types taking name */
6479 comment_type_name:
6480 			ACCESS METHOD						{ $$ = OBJECT_ACCESS_METHOD; }
6481 			| DATABASE							{ $$ = OBJECT_DATABASE; }
6482 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6483 			| EXTENSION							{ $$ = OBJECT_EXTENSION; }
6484 			| FOREIGN DATA_P WRAPPER			{ $$ = OBJECT_FDW; }
6485 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6486 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6487 			| ROLE								{ $$ = OBJECT_ROLE; }
6488 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6489 			| SERVER							{ $$ = OBJECT_FOREIGN_SERVER; }
6490 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6491 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6492 		;
6493 
6494 comment_text:
6495 			Sconst								{ $$ = $1; }
6496 			| NULL_P							{ $$ = NULL; }
6497 		;
6498 
6499 
6500 /*****************************************************************************
6501  *
6502  *  SECURITY LABEL [FOR <provider>] ON <object> IS <label>
6503  *
6504  *  As with COMMENT ON, <object> can refer to various types of database
6505  *  objects (e.g. TABLE, COLUMN, etc.).
6506  *
6507  *****************************************************************************/
6508 
6509 SecLabelStmt:
6510 			SECURITY LABEL opt_provider ON security_label_type_any_name any_name
6511 			IS security_label
6512 				{
6513 					SecLabelStmt *n = makeNode(SecLabelStmt);
6514 					n->provider = $3;
6515 					n->objtype = $5;
6516 					n->object = (Node *) $6;
6517 					n->label = $8;
6518 					$$ = (Node *) n;
6519 				}
6520 			| SECURITY LABEL opt_provider ON security_label_type_name name
6521 			  IS security_label
6522 				{
6523 					SecLabelStmt *n = makeNode(SecLabelStmt);
6524 					n->provider = $3;
6525 					n->objtype = $5;
6526 					n->object = (Node *) makeString($6);
6527 					n->label = $8;
6528 					$$ = (Node *) n;
6529 				}
6530 			| SECURITY LABEL opt_provider ON TYPE_P Typename
6531 			  IS security_label
6532 				{
6533 					SecLabelStmt *n = makeNode(SecLabelStmt);
6534 					n->provider = $3;
6535 					n->objtype = OBJECT_TYPE;
6536 					n->object = (Node *) $6;
6537 					n->label = $8;
6538 					$$ = (Node *) n;
6539 				}
6540 			| SECURITY LABEL opt_provider ON DOMAIN_P Typename
6541 			  IS security_label
6542 				{
6543 					SecLabelStmt *n = makeNode(SecLabelStmt);
6544 					n->provider = $3;
6545 					n->objtype = OBJECT_DOMAIN;
6546 					n->object = (Node *) $6;
6547 					n->label = $8;
6548 					$$ = (Node *) n;
6549 				}
6550 			| SECURITY LABEL opt_provider ON AGGREGATE aggregate_with_argtypes
6551 			  IS security_label
6552 				{
6553 					SecLabelStmt *n = makeNode(SecLabelStmt);
6554 					n->provider = $3;
6555 					n->objtype = OBJECT_AGGREGATE;
6556 					n->object = (Node *) $6;
6557 					n->label = $8;
6558 					$$ = (Node *) n;
6559 				}
6560 			| SECURITY LABEL opt_provider ON FUNCTION function_with_argtypes
6561 			  IS security_label
6562 				{
6563 					SecLabelStmt *n = makeNode(SecLabelStmt);
6564 					n->provider = $3;
6565 					n->objtype = OBJECT_FUNCTION;
6566 					n->object = (Node *) $6;
6567 					n->label = $8;
6568 					$$ = (Node *) n;
6569 				}
6570 			| SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
6571 			  IS security_label
6572 				{
6573 					SecLabelStmt *n = makeNode(SecLabelStmt);
6574 					n->provider = $3;
6575 					n->objtype = OBJECT_LARGEOBJECT;
6576 					n->object = (Node *) $7;
6577 					n->label = $9;
6578 					$$ = (Node *) n;
6579 				}
6580 		;
6581 
6582 opt_provider:	FOR NonReservedWord_or_Sconst	{ $$ = $2; }
6583 				| /* empty */					{ $$ = NULL; }
6584 		;
6585 
6586 /* object types taking any_name */
6587 security_label_type_any_name:
6588 			COLUMN								{ $$ = OBJECT_COLUMN; }
6589 			| FOREIGN TABLE						{ $$ = OBJECT_FOREIGN_TABLE; }
6590 			| SEQUENCE							{ $$ = OBJECT_SEQUENCE; }
6591 			| TABLE								{ $$ = OBJECT_TABLE; }
6592 			| VIEW								{ $$ = OBJECT_VIEW; }
6593 			| MATERIALIZED VIEW					{ $$ = OBJECT_MATVIEW; }
6594 		;
6595 
6596 /* object types taking name */
6597 security_label_type_name:
6598 			DATABASE							{ $$ = OBJECT_DATABASE; }
6599 			| EVENT TRIGGER						{ $$ = OBJECT_EVENT_TRIGGER; }
6600 			| opt_procedural LANGUAGE			{ $$ = OBJECT_LANGUAGE; }
6601 			| PUBLICATION						{ $$ = OBJECT_PUBLICATION; }
6602 			| ROLE								{ $$ = OBJECT_ROLE; }
6603 			| SCHEMA							{ $$ = OBJECT_SCHEMA; }
6604 			| SUBSCRIPTION						{ $$ = OBJECT_SUBSCRIPTION; }
6605 			| TABLESPACE						{ $$ = OBJECT_TABLESPACE; }
6606 		;
6607 
6608 security_label:	Sconst				{ $$ = $1; }
6609 				| NULL_P			{ $$ = NULL; }
6610 		;
6611 
6612 /*****************************************************************************
6613  *
6614  *		QUERY:
6615  *			fetch/move
6616  *
6617  *****************************************************************************/
6618 
6619 FetchStmt:	FETCH fetch_args
6620 				{
6621 					FetchStmt *n = (FetchStmt *) $2;
6622 					n->ismove = FALSE;
6623 					$$ = (Node *)n;
6624 				}
6625 			| MOVE fetch_args
6626 				{
6627 					FetchStmt *n = (FetchStmt *) $2;
6628 					n->ismove = TRUE;
6629 					$$ = (Node *)n;
6630 				}
6631 		;
6632 
6633 fetch_args:	cursor_name
6634 				{
6635 					FetchStmt *n = makeNode(FetchStmt);
6636 					n->portalname = $1;
6637 					n->direction = FETCH_FORWARD;
6638 					n->howMany = 1;
6639 					$$ = (Node *)n;
6640 				}
6641 			| from_in cursor_name
6642 				{
6643 					FetchStmt *n = makeNode(FetchStmt);
6644 					n->portalname = $2;
6645 					n->direction = FETCH_FORWARD;
6646 					n->howMany = 1;
6647 					$$ = (Node *)n;
6648 				}
6649 			| NEXT opt_from_in cursor_name
6650 				{
6651 					FetchStmt *n = makeNode(FetchStmt);
6652 					n->portalname = $3;
6653 					n->direction = FETCH_FORWARD;
6654 					n->howMany = 1;
6655 					$$ = (Node *)n;
6656 				}
6657 			| PRIOR opt_from_in cursor_name
6658 				{
6659 					FetchStmt *n = makeNode(FetchStmt);
6660 					n->portalname = $3;
6661 					n->direction = FETCH_BACKWARD;
6662 					n->howMany = 1;
6663 					$$ = (Node *)n;
6664 				}
6665 			| FIRST_P opt_from_in cursor_name
6666 				{
6667 					FetchStmt *n = makeNode(FetchStmt);
6668 					n->portalname = $3;
6669 					n->direction = FETCH_ABSOLUTE;
6670 					n->howMany = 1;
6671 					$$ = (Node *)n;
6672 				}
6673 			| LAST_P opt_from_in cursor_name
6674 				{
6675 					FetchStmt *n = makeNode(FetchStmt);
6676 					n->portalname = $3;
6677 					n->direction = FETCH_ABSOLUTE;
6678 					n->howMany = -1;
6679 					$$ = (Node *)n;
6680 				}
6681 			| ABSOLUTE_P SignedIconst opt_from_in cursor_name
6682 				{
6683 					FetchStmt *n = makeNode(FetchStmt);
6684 					n->portalname = $4;
6685 					n->direction = FETCH_ABSOLUTE;
6686 					n->howMany = $2;
6687 					$$ = (Node *)n;
6688 				}
6689 			| RELATIVE_P SignedIconst opt_from_in cursor_name
6690 				{
6691 					FetchStmt *n = makeNode(FetchStmt);
6692 					n->portalname = $4;
6693 					n->direction = FETCH_RELATIVE;
6694 					n->howMany = $2;
6695 					$$ = (Node *)n;
6696 				}
6697 			| SignedIconst opt_from_in cursor_name
6698 				{
6699 					FetchStmt *n = makeNode(FetchStmt);
6700 					n->portalname = $3;
6701 					n->direction = FETCH_FORWARD;
6702 					n->howMany = $1;
6703 					$$ = (Node *)n;
6704 				}
6705 			| ALL opt_from_in cursor_name
6706 				{
6707 					FetchStmt *n = makeNode(FetchStmt);
6708 					n->portalname = $3;
6709 					n->direction = FETCH_FORWARD;
6710 					n->howMany = FETCH_ALL;
6711 					$$ = (Node *)n;
6712 				}
6713 			| FORWARD opt_from_in cursor_name
6714 				{
6715 					FetchStmt *n = makeNode(FetchStmt);
6716 					n->portalname = $3;
6717 					n->direction = FETCH_FORWARD;
6718 					n->howMany = 1;
6719 					$$ = (Node *)n;
6720 				}
6721 			| FORWARD SignedIconst opt_from_in cursor_name
6722 				{
6723 					FetchStmt *n = makeNode(FetchStmt);
6724 					n->portalname = $4;
6725 					n->direction = FETCH_FORWARD;
6726 					n->howMany = $2;
6727 					$$ = (Node *)n;
6728 				}
6729 			| FORWARD ALL opt_from_in cursor_name
6730 				{
6731 					FetchStmt *n = makeNode(FetchStmt);
6732 					n->portalname = $4;
6733 					n->direction = FETCH_FORWARD;
6734 					n->howMany = FETCH_ALL;
6735 					$$ = (Node *)n;
6736 				}
6737 			| BACKWARD opt_from_in cursor_name
6738 				{
6739 					FetchStmt *n = makeNode(FetchStmt);
6740 					n->portalname = $3;
6741 					n->direction = FETCH_BACKWARD;
6742 					n->howMany = 1;
6743 					$$ = (Node *)n;
6744 				}
6745 			| BACKWARD SignedIconst opt_from_in cursor_name
6746 				{
6747 					FetchStmt *n = makeNode(FetchStmt);
6748 					n->portalname = $4;
6749 					n->direction = FETCH_BACKWARD;
6750 					n->howMany = $2;
6751 					$$ = (Node *)n;
6752 				}
6753 			| BACKWARD ALL opt_from_in cursor_name
6754 				{
6755 					FetchStmt *n = makeNode(FetchStmt);
6756 					n->portalname = $4;
6757 					n->direction = FETCH_BACKWARD;
6758 					n->howMany = FETCH_ALL;
6759 					$$ = (Node *)n;
6760 				}
6761 		;
6762 
6763 from_in:	FROM									{}
6764 			| IN_P									{}
6765 		;
6766 
6767 opt_from_in:	from_in								{}
6768 			| /* EMPTY */							{}
6769 		;
6770 
6771 
6772 /*****************************************************************************
6773  *
6774  * GRANT and REVOKE statements
6775  *
6776  *****************************************************************************/
6777 
6778 GrantStmt:	GRANT privileges ON privilege_target TO grantee_list
6779 			opt_grant_grant_option
6780 				{
6781 					GrantStmt *n = makeNode(GrantStmt);
6782 					n->is_grant = true;
6783 					n->privileges = $2;
6784 					n->targtype = ($4)->targtype;
6785 					n->objtype = ($4)->objtype;
6786 					n->objects = ($4)->objs;
6787 					n->grantees = $6;
6788 					n->grant_option = $7;
6789 					$$ = (Node*)n;
6790 				}
6791 		;
6792 
6793 RevokeStmt:
6794 			REVOKE privileges ON privilege_target
6795 			FROM grantee_list opt_drop_behavior
6796 				{
6797 					GrantStmt *n = makeNode(GrantStmt);
6798 					n->is_grant = false;
6799 					n->grant_option = false;
6800 					n->privileges = $2;
6801 					n->targtype = ($4)->targtype;
6802 					n->objtype = ($4)->objtype;
6803 					n->objects = ($4)->objs;
6804 					n->grantees = $6;
6805 					n->behavior = $7;
6806 					$$ = (Node *)n;
6807 				}
6808 			| REVOKE GRANT OPTION FOR privileges ON privilege_target
6809 			FROM grantee_list opt_drop_behavior
6810 				{
6811 					GrantStmt *n = makeNode(GrantStmt);
6812 					n->is_grant = false;
6813 					n->grant_option = true;
6814 					n->privileges = $5;
6815 					n->targtype = ($7)->targtype;
6816 					n->objtype = ($7)->objtype;
6817 					n->objects = ($7)->objs;
6818 					n->grantees = $9;
6819 					n->behavior = $10;
6820 					$$ = (Node *)n;
6821 				}
6822 		;
6823 
6824 
6825 /*
6826  * Privilege names are represented as strings; the validity of the privilege
6827  * names gets checked at execution.  This is a bit annoying but we have little
6828  * choice because of the syntactic conflict with lists of role names in
6829  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
6830  * production any reserved keywords that need to be usable as privilege names.
6831  */
6832 
6833 /* either ALL [PRIVILEGES] or a list of individual privileges */
6834 privileges: privilege_list
6835 				{ $$ = $1; }
6836 			| ALL
6837 				{ $$ = NIL; }
6838 			| ALL PRIVILEGES
6839 				{ $$ = NIL; }
6840 			| ALL '(' columnList ')'
6841 				{
6842 					AccessPriv *n = makeNode(AccessPriv);
6843 					n->priv_name = NULL;
6844 					n->cols = $3;
6845 					$$ = list_make1(n);
6846 				}
6847 			| ALL PRIVILEGES '(' columnList ')'
6848 				{
6849 					AccessPriv *n = makeNode(AccessPriv);
6850 					n->priv_name = NULL;
6851 					n->cols = $4;
6852 					$$ = list_make1(n);
6853 				}
6854 		;
6855 
6856 privilege_list:	privilege							{ $$ = list_make1($1); }
6857 			| privilege_list ',' privilege			{ $$ = lappend($1, $3); }
6858 		;
6859 
6860 privilege:	SELECT opt_column_list
6861 			{
6862 				AccessPriv *n = makeNode(AccessPriv);
6863 				n->priv_name = pstrdup($1);
6864 				n->cols = $2;
6865 				$$ = n;
6866 			}
6867 		| REFERENCES opt_column_list
6868 			{
6869 				AccessPriv *n = makeNode(AccessPriv);
6870 				n->priv_name = pstrdup($1);
6871 				n->cols = $2;
6872 				$$ = n;
6873 			}
6874 		| CREATE opt_column_list
6875 			{
6876 				AccessPriv *n = makeNode(AccessPriv);
6877 				n->priv_name = pstrdup($1);
6878 				n->cols = $2;
6879 				$$ = n;
6880 			}
6881 		| ColId opt_column_list
6882 			{
6883 				AccessPriv *n = makeNode(AccessPriv);
6884 				n->priv_name = $1;
6885 				n->cols = $2;
6886 				$$ = n;
6887 			}
6888 		;
6889 
6890 
6891 /* Don't bother trying to fold the first two rules into one using
6892  * opt_table.  You're going to get conflicts.
6893  */
6894 privilege_target:
6895 			qualified_name_list
6896 				{
6897 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6898 					n->targtype = ACL_TARGET_OBJECT;
6899 					n->objtype = ACL_OBJECT_RELATION;
6900 					n->objs = $1;
6901 					$$ = n;
6902 				}
6903 			| TABLE qualified_name_list
6904 				{
6905 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6906 					n->targtype = ACL_TARGET_OBJECT;
6907 					n->objtype = ACL_OBJECT_RELATION;
6908 					n->objs = $2;
6909 					$$ = n;
6910 				}
6911 			| SEQUENCE qualified_name_list
6912 				{
6913 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6914 					n->targtype = ACL_TARGET_OBJECT;
6915 					n->objtype = ACL_OBJECT_SEQUENCE;
6916 					n->objs = $2;
6917 					$$ = n;
6918 				}
6919 			| FOREIGN DATA_P WRAPPER name_list
6920 				{
6921 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6922 					n->targtype = ACL_TARGET_OBJECT;
6923 					n->objtype = ACL_OBJECT_FDW;
6924 					n->objs = $4;
6925 					$$ = n;
6926 				}
6927 			| FOREIGN SERVER name_list
6928 				{
6929 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6930 					n->targtype = ACL_TARGET_OBJECT;
6931 					n->objtype = ACL_OBJECT_FOREIGN_SERVER;
6932 					n->objs = $3;
6933 					$$ = n;
6934 				}
6935 			| FUNCTION function_with_argtypes_list
6936 				{
6937 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6938 					n->targtype = ACL_TARGET_OBJECT;
6939 					n->objtype = ACL_OBJECT_FUNCTION;
6940 					n->objs = $2;
6941 					$$ = n;
6942 				}
6943 			| DATABASE name_list
6944 				{
6945 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6946 					n->targtype = ACL_TARGET_OBJECT;
6947 					n->objtype = ACL_OBJECT_DATABASE;
6948 					n->objs = $2;
6949 					$$ = n;
6950 				}
6951 			| DOMAIN_P any_name_list
6952 				{
6953 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6954 					n->targtype = ACL_TARGET_OBJECT;
6955 					n->objtype = ACL_OBJECT_DOMAIN;
6956 					n->objs = $2;
6957 					$$ = n;
6958 				}
6959 			| LANGUAGE name_list
6960 				{
6961 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6962 					n->targtype = ACL_TARGET_OBJECT;
6963 					n->objtype = ACL_OBJECT_LANGUAGE;
6964 					n->objs = $2;
6965 					$$ = n;
6966 				}
6967 			| LARGE_P OBJECT_P NumericOnly_list
6968 				{
6969 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6970 					n->targtype = ACL_TARGET_OBJECT;
6971 					n->objtype = ACL_OBJECT_LARGEOBJECT;
6972 					n->objs = $3;
6973 					$$ = n;
6974 				}
6975 			| SCHEMA name_list
6976 				{
6977 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6978 					n->targtype = ACL_TARGET_OBJECT;
6979 					n->objtype = ACL_OBJECT_NAMESPACE;
6980 					n->objs = $2;
6981 					$$ = n;
6982 				}
6983 			| TABLESPACE name_list
6984 				{
6985 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6986 					n->targtype = ACL_TARGET_OBJECT;
6987 					n->objtype = ACL_OBJECT_TABLESPACE;
6988 					n->objs = $2;
6989 					$$ = n;
6990 				}
6991 			| TYPE_P any_name_list
6992 				{
6993 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
6994 					n->targtype = ACL_TARGET_OBJECT;
6995 					n->objtype = ACL_OBJECT_TYPE;
6996 					n->objs = $2;
6997 					$$ = n;
6998 				}
6999 			| ALL TABLES IN_P SCHEMA name_list
7000 				{
7001 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7002 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7003 					n->objtype = ACL_OBJECT_RELATION;
7004 					n->objs = $5;
7005 					$$ = n;
7006 				}
7007 			| ALL SEQUENCES IN_P SCHEMA name_list
7008 				{
7009 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7010 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7011 					n->objtype = ACL_OBJECT_SEQUENCE;
7012 					n->objs = $5;
7013 					$$ = n;
7014 				}
7015 			| ALL FUNCTIONS IN_P SCHEMA name_list
7016 				{
7017 					PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7018 					n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7019 					n->objtype = ACL_OBJECT_FUNCTION;
7020 					n->objs = $5;
7021 					$$ = n;
7022 				}
7023 		;
7024 
7025 
7026 grantee_list:
7027 			grantee									{ $$ = list_make1($1); }
7028 			| grantee_list ',' grantee				{ $$ = lappend($1, $3); }
7029 		;
7030 
7031 grantee:
7032 			RoleSpec								{ $$ = $1; }
7033 			| GROUP_P RoleSpec						{ $$ = $2; }
7034 		;
7035 
7036 
7037 opt_grant_grant_option:
7038 			WITH GRANT OPTION { $$ = TRUE; }
7039 			| /*EMPTY*/ { $$ = FALSE; }
7040 		;
7041 
7042 /*****************************************************************************
7043  *
7044  * GRANT and REVOKE ROLE statements
7045  *
7046  *****************************************************************************/
7047 
7048 GrantRoleStmt:
7049 			GRANT privilege_list TO role_list opt_grant_admin_option opt_granted_by
7050 				{
7051 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7052 					n->is_grant = true;
7053 					n->granted_roles = $2;
7054 					n->grantee_roles = $4;
7055 					n->admin_opt = $5;
7056 					n->grantor = $6;
7057 					$$ = (Node*)n;
7058 				}
7059 		;
7060 
7061 RevokeRoleStmt:
7062 			REVOKE privilege_list FROM role_list opt_granted_by opt_drop_behavior
7063 				{
7064 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7065 					n->is_grant = false;
7066 					n->admin_opt = false;
7067 					n->granted_roles = $2;
7068 					n->grantee_roles = $4;
7069 					n->behavior = $6;
7070 					$$ = (Node*)n;
7071 				}
7072 			| REVOKE ADMIN OPTION FOR privilege_list FROM role_list opt_granted_by opt_drop_behavior
7073 				{
7074 					GrantRoleStmt *n = makeNode(GrantRoleStmt);
7075 					n->is_grant = false;
7076 					n->admin_opt = true;
7077 					n->granted_roles = $5;
7078 					n->grantee_roles = $7;
7079 					n->behavior = $9;
7080 					$$ = (Node*)n;
7081 				}
7082 		;
7083 
7084 opt_grant_admin_option: WITH ADMIN OPTION				{ $$ = TRUE; }
7085 			| /*EMPTY*/									{ $$ = FALSE; }
7086 		;
7087 
7088 opt_granted_by: GRANTED BY RoleSpec						{ $$ = $3; }
7089 			| /*EMPTY*/									{ $$ = NULL; }
7090 		;
7091 
7092 /*****************************************************************************
7093  *
7094  * ALTER DEFAULT PRIVILEGES statement
7095  *
7096  *****************************************************************************/
7097 
7098 AlterDefaultPrivilegesStmt:
7099 			ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
7100 				{
7101 					AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
7102 					n->options = $4;
7103 					n->action = (GrantStmt *) $5;
7104 					$$ = (Node*)n;
7105 				}
7106 		;
7107 
7108 DefACLOptionList:
7109 			DefACLOptionList DefACLOption			{ $$ = lappend($1, $2); }
7110 			| /* EMPTY */							{ $$ = NIL; }
7111 		;
7112 
7113 DefACLOption:
7114 			IN_P SCHEMA name_list
7115 				{
7116 					$$ = makeDefElem("schemas", (Node *)$3, @1);
7117 				}
7118 			| FOR ROLE role_list
7119 				{
7120 					$$ = makeDefElem("roles", (Node *)$3, @1);
7121 				}
7122 			| FOR USER role_list
7123 				{
7124 					$$ = makeDefElem("roles", (Node *)$3, @1);
7125 				}
7126 		;
7127 
7128 /*
7129  * This should match GRANT/REVOKE, except that individual target objects
7130  * are not mentioned and we only allow a subset of object types.
7131  */
7132 DefACLAction:
7133 			GRANT privileges ON defacl_privilege_target TO grantee_list
7134 			opt_grant_grant_option
7135 				{
7136 					GrantStmt *n = makeNode(GrantStmt);
7137 					n->is_grant = true;
7138 					n->privileges = $2;
7139 					n->targtype = ACL_TARGET_DEFAULTS;
7140 					n->objtype = $4;
7141 					n->objects = NIL;
7142 					n->grantees = $6;
7143 					n->grant_option = $7;
7144 					$$ = (Node*)n;
7145 				}
7146 			| REVOKE privileges ON defacl_privilege_target
7147 			FROM grantee_list opt_drop_behavior
7148 				{
7149 					GrantStmt *n = makeNode(GrantStmt);
7150 					n->is_grant = false;
7151 					n->grant_option = false;
7152 					n->privileges = $2;
7153 					n->targtype = ACL_TARGET_DEFAULTS;
7154 					n->objtype = $4;
7155 					n->objects = NIL;
7156 					n->grantees = $6;
7157 					n->behavior = $7;
7158 					$$ = (Node *)n;
7159 				}
7160 			| REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
7161 			FROM grantee_list opt_drop_behavior
7162 				{
7163 					GrantStmt *n = makeNode(GrantStmt);
7164 					n->is_grant = false;
7165 					n->grant_option = true;
7166 					n->privileges = $5;
7167 					n->targtype = ACL_TARGET_DEFAULTS;
7168 					n->objtype = $7;
7169 					n->objects = NIL;
7170 					n->grantees = $9;
7171 					n->behavior = $10;
7172 					$$ = (Node *)n;
7173 				}
7174 		;
7175 
7176 defacl_privilege_target:
7177 			TABLES			{ $$ = ACL_OBJECT_RELATION; }
7178 			| FUNCTIONS		{ $$ = ACL_OBJECT_FUNCTION; }
7179 			| SEQUENCES		{ $$ = ACL_OBJECT_SEQUENCE; }
7180 			| TYPES_P		{ $$ = ACL_OBJECT_TYPE; }
7181 			| SCHEMAS		{ $$ = ACL_OBJECT_NAMESPACE; }
7182 		;
7183 
7184 
7185 /*****************************************************************************
7186  *
7187  *		QUERY: CREATE INDEX
7188  *
7189  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
7190  * willing to make TABLESPACE a fully reserved word.
7191  *****************************************************************************/
7192 
7193 IndexStmt:	CREATE opt_unique INDEX opt_concurrently opt_index_name
7194 			ON qualified_name access_method_clause '(' index_params ')'
7195 			opt_reloptions OptTableSpace where_clause
7196 				{
7197 					IndexStmt *n = makeNode(IndexStmt);
7198 					n->unique = $2;
7199 					n->concurrent = $4;
7200 					n->idxname = $5;
7201 					n->relation = $7;
7202 					n->accessMethod = $8;
7203 					n->indexParams = $10;
7204 					n->options = $12;
7205 					n->tableSpace = $13;
7206 					n->whereClause = $14;
7207 					n->excludeOpNames = NIL;
7208 					n->idxcomment = NULL;
7209 					n->indexOid = InvalidOid;
7210 					n->oldNode = InvalidOid;
7211 					n->primary = false;
7212 					n->isconstraint = false;
7213 					n->deferrable = false;
7214 					n->initdeferred = false;
7215 					n->transformed = false;
7216 					n->if_not_exists = false;
7217 					$$ = (Node *)n;
7218 				}
7219 			| CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS index_name
7220 			ON qualified_name access_method_clause '(' index_params ')'
7221 			opt_reloptions OptTableSpace where_clause
7222 				{
7223 					IndexStmt *n = makeNode(IndexStmt);
7224 					n->unique = $2;
7225 					n->concurrent = $4;
7226 					n->idxname = $8;
7227 					n->relation = $10;
7228 					n->accessMethod = $11;
7229 					n->indexParams = $13;
7230 					n->options = $15;
7231 					n->tableSpace = $16;
7232 					n->whereClause = $17;
7233 					n->excludeOpNames = NIL;
7234 					n->idxcomment = NULL;
7235 					n->indexOid = InvalidOid;
7236 					n->oldNode = InvalidOid;
7237 					n->primary = false;
7238 					n->isconstraint = false;
7239 					n->deferrable = false;
7240 					n->initdeferred = false;
7241 					n->transformed = false;
7242 					n->if_not_exists = true;
7243 					$$ = (Node *)n;
7244 				}
7245 		;
7246 
7247 opt_unique:
7248 			UNIQUE									{ $$ = TRUE; }
7249 			| /*EMPTY*/								{ $$ = FALSE; }
7250 		;
7251 
7252 opt_concurrently:
7253 			CONCURRENTLY							{ $$ = TRUE; }
7254 			| /*EMPTY*/								{ $$ = FALSE; }
7255 		;
7256 
7257 opt_index_name:
7258 			index_name								{ $$ = $1; }
7259 			| /*EMPTY*/								{ $$ = NULL; }
7260 		;
7261 
7262 access_method_clause:
7263 			USING access_method						{ $$ = $2; }
7264 			| /*EMPTY*/								{ $$ = DEFAULT_INDEX_TYPE; }
7265 		;
7266 
7267 index_params:	index_elem							{ $$ = list_make1($1); }
7268 			| index_params ',' index_elem			{ $$ = lappend($1, $3); }
7269 		;
7270 
7271 /*
7272  * Index attributes can be either simple column references, or arbitrary
7273  * expressions in parens.  For backwards-compatibility reasons, we allow
7274  * an expression that's just a function call to be written without parens.
7275  */
7276 index_elem:	ColId opt_collate opt_class opt_asc_desc opt_nulls_order
7277 				{
7278 					$$ = makeNode(IndexElem);
7279 					$$->name = $1;
7280 					$$->expr = NULL;
7281 					$$->indexcolname = NULL;
7282 					$$->collation = $2;
7283 					$$->opclass = $3;
7284 					$$->ordering = $4;
7285 					$$->nulls_ordering = $5;
7286 				}
7287 			| func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order
7288 				{
7289 					$$ = makeNode(IndexElem);
7290 					$$->name = NULL;
7291 					$$->expr = $1;
7292 					$$->indexcolname = NULL;
7293 					$$->collation = $2;
7294 					$$->opclass = $3;
7295 					$$->ordering = $4;
7296 					$$->nulls_ordering = $5;
7297 				}
7298 			| '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order
7299 				{
7300 					$$ = makeNode(IndexElem);
7301 					$$->name = NULL;
7302 					$$->expr = $2;
7303 					$$->indexcolname = NULL;
7304 					$$->collation = $4;
7305 					$$->opclass = $5;
7306 					$$->ordering = $6;
7307 					$$->nulls_ordering = $7;
7308 				}
7309 		;
7310 
7311 opt_collate: COLLATE any_name						{ $$ = $2; }
7312 			| /*EMPTY*/								{ $$ = NIL; }
7313 		;
7314 
7315 opt_class:	any_name								{ $$ = $1; }
7316 			| /*EMPTY*/								{ $$ = NIL; }
7317 		;
7318 
7319 opt_asc_desc: ASC							{ $$ = SORTBY_ASC; }
7320 			| DESC							{ $$ = SORTBY_DESC; }
7321 			| /*EMPTY*/						{ $$ = SORTBY_DEFAULT; }
7322 		;
7323 
7324 opt_nulls_order: NULLS_LA FIRST_P			{ $$ = SORTBY_NULLS_FIRST; }
7325 			| NULLS_LA LAST_P				{ $$ = SORTBY_NULLS_LAST; }
7326 			| /*EMPTY*/						{ $$ = SORTBY_NULLS_DEFAULT; }
7327 		;
7328 
7329 
7330 /*****************************************************************************
7331  *
7332  *		QUERY:
7333  *				create [or replace] function <fname>
7334  *						[(<type-1> { , <type-n>})]
7335  *						returns <type-r>
7336  *						as <filename or code in language as appropriate>
7337  *						language <lang> [with parameters]
7338  *
7339  *****************************************************************************/
7340 
7341 CreateFunctionStmt:
7342 			CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7343 			RETURNS func_return createfunc_opt_list opt_definition
7344 				{
7345 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7346 					n->replace = $2;
7347 					n->funcname = $4;
7348 					n->parameters = $5;
7349 					n->returnType = $7;
7350 					n->options = $8;
7351 					n->withClause = $9;
7352 					$$ = (Node *)n;
7353 				}
7354 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7355 			  RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
7356 				{
7357 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7358 					n->replace = $2;
7359 					n->funcname = $4;
7360 					n->parameters = mergeTableFuncParameters($5, $9);
7361 					n->returnType = TableFuncTypeName($9);
7362 					n->returnType->location = @7;
7363 					n->options = $11;
7364 					n->withClause = $12;
7365 					$$ = (Node *)n;
7366 				}
7367 			| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7368 			  createfunc_opt_list opt_definition
7369 				{
7370 					CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7371 					n->replace = $2;
7372 					n->funcname = $4;
7373 					n->parameters = $5;
7374 					n->returnType = NULL;
7375 					n->options = $6;
7376 					n->withClause = $7;
7377 					$$ = (Node *)n;
7378 				}
7379 		;
7380 
7381 opt_or_replace:
7382 			OR REPLACE								{ $$ = TRUE; }
7383 			| /*EMPTY*/								{ $$ = FALSE; }
7384 		;
7385 
7386 func_args:	'(' func_args_list ')'					{ $$ = $2; }
7387 			| '(' ')'								{ $$ = NIL; }
7388 		;
7389 
7390 func_args_list:
7391 			func_arg								{ $$ = list_make1($1); }
7392 			| func_args_list ',' func_arg			{ $$ = lappend($1, $3); }
7393 		;
7394 
7395 function_with_argtypes_list:
7396 			function_with_argtypes					{ $$ = list_make1($1); }
7397 			| function_with_argtypes_list ',' function_with_argtypes
7398 													{ $$ = lappend($1, $3); }
7399 		;
7400 
7401 function_with_argtypes:
7402 			func_name func_args
7403 				{
7404 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7405 					n->objname = $1;
7406 					n->objargs = extractArgTypes($2);
7407 					$$ = n;
7408 				}
7409 			/*
7410 			 * Because of reduce/reduce conflicts, we can't use func_name
7411 			 * below, but we can write it out the long way, which actually
7412 			 * allows more cases.
7413 			 */
7414 			| type_func_name_keyword
7415 				{
7416 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7417 					n->objname = list_make1(makeString(pstrdup($1)));
7418 					n->args_unspecified = true;
7419 					$$ = n;
7420 				}
7421 			| ColId
7422 				{
7423 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7424 					n->objname = list_make1(makeString($1));
7425 					n->args_unspecified = true;
7426 					$$ = n;
7427 				}
7428 			| ColId indirection
7429 				{
7430 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7431 					n->objname = check_func_name(lcons(makeString($1), $2),
7432 												  yyscanner);
7433 					n->args_unspecified = true;
7434 					$$ = n;
7435 				}
7436 		;
7437 
7438 /*
7439  * func_args_with_defaults is separate because we only want to accept
7440  * defaults in CREATE FUNCTION, not in ALTER etc.
7441  */
7442 func_args_with_defaults:
7443 		'(' func_args_with_defaults_list ')'		{ $$ = $2; }
7444 		| '(' ')'									{ $$ = NIL; }
7445 		;
7446 
7447 func_args_with_defaults_list:
7448 		func_arg_with_default						{ $$ = list_make1($1); }
7449 		| func_args_with_defaults_list ',' func_arg_with_default
7450 													{ $$ = lappend($1, $3); }
7451 		;
7452 
7453 /*
7454  * The style with arg_class first is SQL99 standard, but Oracle puts
7455  * param_name first; accept both since it's likely people will try both
7456  * anyway.  Don't bother trying to save productions by letting arg_class
7457  * have an empty alternative ... you'll get shift/reduce conflicts.
7458  *
7459  * We can catch over-specified arguments here if we want to,
7460  * but for now better to silently swallow typmod, etc.
7461  * - thomas 2000-03-22
7462  */
7463 func_arg:
7464 			arg_class param_name func_type
7465 				{
7466 					FunctionParameter *n = makeNode(FunctionParameter);
7467 					n->name = $2;
7468 					n->argType = $3;
7469 					n->mode = $1;
7470 					n->defexpr = NULL;
7471 					$$ = n;
7472 				}
7473 			| param_name arg_class func_type
7474 				{
7475 					FunctionParameter *n = makeNode(FunctionParameter);
7476 					n->name = $1;
7477 					n->argType = $3;
7478 					n->mode = $2;
7479 					n->defexpr = NULL;
7480 					$$ = n;
7481 				}
7482 			| param_name func_type
7483 				{
7484 					FunctionParameter *n = makeNode(FunctionParameter);
7485 					n->name = $1;
7486 					n->argType = $2;
7487 					n->mode = FUNC_PARAM_IN;
7488 					n->defexpr = NULL;
7489 					$$ = n;
7490 				}
7491 			| arg_class func_type
7492 				{
7493 					FunctionParameter *n = makeNode(FunctionParameter);
7494 					n->name = NULL;
7495 					n->argType = $2;
7496 					n->mode = $1;
7497 					n->defexpr = NULL;
7498 					$$ = n;
7499 				}
7500 			| func_type
7501 				{
7502 					FunctionParameter *n = makeNode(FunctionParameter);
7503 					n->name = NULL;
7504 					n->argType = $1;
7505 					n->mode = FUNC_PARAM_IN;
7506 					n->defexpr = NULL;
7507 					$$ = n;
7508 				}
7509 		;
7510 
7511 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
7512 arg_class:	IN_P								{ $$ = FUNC_PARAM_IN; }
7513 			| OUT_P								{ $$ = FUNC_PARAM_OUT; }
7514 			| INOUT								{ $$ = FUNC_PARAM_INOUT; }
7515 			| IN_P OUT_P						{ $$ = FUNC_PARAM_INOUT; }
7516 			| VARIADIC							{ $$ = FUNC_PARAM_VARIADIC; }
7517 		;
7518 
7519 /*
7520  * Ideally param_name should be ColId, but that causes too many conflicts.
7521  */
7522 param_name:	type_function_name
7523 		;
7524 
7525 func_return:
7526 			func_type
7527 				{
7528 					/* We can catch over-specified results here if we want to,
7529 					 * but for now better to silently swallow typmod, etc.
7530 					 * - thomas 2000-03-22
7531 					 */
7532 					$$ = $1;
7533 				}
7534 		;
7535 
7536 /*
7537  * We would like to make the %TYPE productions here be ColId attrs etc,
7538  * but that causes reduce/reduce conflicts.  type_function_name
7539  * is next best choice.
7540  */
7541 func_type:	Typename								{ $$ = $1; }
7542 			| type_function_name attrs '%' TYPE_P
7543 				{
7544 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7545 					$$->pct_type = true;
7546 					$$->location = @1;
7547 				}
7548 			| SETOF type_function_name attrs '%' TYPE_P
7549 				{
7550 					$$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
7551 					$$->pct_type = true;
7552 					$$->setof = TRUE;
7553 					$$->location = @2;
7554 				}
7555 		;
7556 
7557 func_arg_with_default:
7558 		func_arg
7559 				{
7560 					$$ = $1;
7561 				}
7562 		| func_arg DEFAULT a_expr
7563 				{
7564 					$$ = $1;
7565 					$$->defexpr = $3;
7566 				}
7567 		| func_arg '=' a_expr
7568 				{
7569 					$$ = $1;
7570 					$$->defexpr = $3;
7571 				}
7572 		;
7573 
7574 /* Aggregate args can be most things that function args can be */
7575 aggr_arg:	func_arg
7576 				{
7577 					if (!($1->mode == FUNC_PARAM_IN ||
7578 						  $1->mode == FUNC_PARAM_VARIADIC))
7579 						ereport(ERROR,
7580 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7581 								 errmsg("aggregates cannot have output arguments"),
7582 								 parser_errposition(@1)));
7583 					$$ = $1;
7584 				}
7585 		;
7586 
7587 /*
7588  * The SQL standard offers no guidance on how to declare aggregate argument
7589  * lists, since it doesn't have CREATE AGGREGATE etc.  We accept these cases:
7590  *
7591  * (*)									- normal agg with no args
7592  * (aggr_arg,...)						- normal agg with args
7593  * (ORDER BY aggr_arg,...)				- ordered-set agg with no direct args
7594  * (aggr_arg,... ORDER BY aggr_arg,...)	- ordered-set agg with direct args
7595  *
7596  * The zero-argument case is spelled with '*' for consistency with COUNT(*).
7597  *
7598  * An additional restriction is that if the direct-args list ends in a
7599  * VARIADIC item, the ordered-args list must contain exactly one item that
7600  * is also VARIADIC with the same type.  This allows us to collapse the two
7601  * VARIADIC items into one, which is necessary to represent the aggregate in
7602  * pg_proc.  We check this at the grammar stage so that we can return a list
7603  * in which the second VARIADIC item is already discarded, avoiding extra work
7604  * in cases such as DROP AGGREGATE.
7605  *
7606  * The return value of this production is a two-element list, in which the
7607  * first item is a sublist of FunctionParameter nodes (with any duplicate
7608  * VARIADIC item already dropped, as per above) and the second is an integer
7609  * Value node, containing -1 if there was no ORDER BY and otherwise the number
7610  * of argument declarations before the ORDER BY.  (If this number is equal
7611  * to the first sublist's length, then we dropped a duplicate VARIADIC item.)
7612  * This representation is passed as-is to CREATE AGGREGATE; for operations
7613  * on existing aggregates, we can just apply extractArgTypes to the first
7614  * sublist.
7615  */
7616 aggr_args:	'(' '*' ')'
7617 				{
7618 					$$ = list_make2(NIL, makeInteger(-1));
7619 				}
7620 			| '(' aggr_args_list ')'
7621 				{
7622 					$$ = list_make2($2, makeInteger(-1));
7623 				}
7624 			| '(' ORDER BY aggr_args_list ')'
7625 				{
7626 					$$ = list_make2($4, makeInteger(0));
7627 				}
7628 			| '(' aggr_args_list ORDER BY aggr_args_list ')'
7629 				{
7630 					/* this is the only case requiring consistency checking */
7631 					$$ = makeOrderedSetArgs($2, $5, yyscanner);
7632 				}
7633 		;
7634 
7635 aggr_args_list:
7636 			aggr_arg								{ $$ = list_make1($1); }
7637 			| aggr_args_list ',' aggr_arg			{ $$ = lappend($1, $3); }
7638 		;
7639 
7640 aggregate_with_argtypes:
7641 			func_name aggr_args
7642 				{
7643 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7644 					n->objname = $1;
7645 					n->objargs = extractAggrArgTypes($2);
7646 					$$ = n;
7647 				}
7648 		;
7649 
7650 aggregate_with_argtypes_list:
7651 			aggregate_with_argtypes					{ $$ = list_make1($1); }
7652 			| aggregate_with_argtypes_list ',' aggregate_with_argtypes
7653 													{ $$ = lappend($1, $3); }
7654 		;
7655 
7656 createfunc_opt_list:
7657 			/* Must be at least one to prevent conflict */
7658 			createfunc_opt_item						{ $$ = list_make1($1); }
7659 			| createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
7660 	;
7661 
7662 /*
7663  * Options common to both CREATE FUNCTION and ALTER FUNCTION
7664  */
7665 common_func_opt_item:
7666 			CALLED ON NULL_P INPUT_P
7667 				{
7668 					$$ = makeDefElem("strict", (Node *)makeInteger(FALSE), @1);
7669 				}
7670 			| RETURNS NULL_P ON NULL_P INPUT_P
7671 				{
7672 					$$ = makeDefElem("strict", (Node *)makeInteger(TRUE), @1);
7673 				}
7674 			| STRICT_P
7675 				{
7676 					$$ = makeDefElem("strict", (Node *)makeInteger(TRUE), @1);
7677 				}
7678 			| IMMUTABLE
7679 				{
7680 					$$ = makeDefElem("volatility", (Node *)makeString("immutable"), @1);
7681 				}
7682 			| STABLE
7683 				{
7684 					$$ = makeDefElem("volatility", (Node *)makeString("stable"), @1);
7685 				}
7686 			| VOLATILE
7687 				{
7688 					$$ = makeDefElem("volatility", (Node *)makeString("volatile"), @1);
7689 				}
7690 			| EXTERNAL SECURITY DEFINER
7691 				{
7692 					$$ = makeDefElem("security", (Node *)makeInteger(TRUE), @1);
7693 				}
7694 			| EXTERNAL SECURITY INVOKER
7695 				{
7696 					$$ = makeDefElem("security", (Node *)makeInteger(FALSE), @1);
7697 				}
7698 			| SECURITY DEFINER
7699 				{
7700 					$$ = makeDefElem("security", (Node *)makeInteger(TRUE), @1);
7701 				}
7702 			| SECURITY INVOKER
7703 				{
7704 					$$ = makeDefElem("security", (Node *)makeInteger(FALSE), @1);
7705 				}
7706 			| LEAKPROOF
7707 				{
7708 					$$ = makeDefElem("leakproof", (Node *)makeInteger(TRUE), @1);
7709 				}
7710 			| NOT LEAKPROOF
7711 				{
7712 					$$ = makeDefElem("leakproof", (Node *)makeInteger(FALSE), @1);
7713 				}
7714 			| COST NumericOnly
7715 				{
7716 					$$ = makeDefElem("cost", (Node *)$2, @1);
7717 				}
7718 			| ROWS NumericOnly
7719 				{
7720 					$$ = makeDefElem("rows", (Node *)$2, @1);
7721 				}
7722 			| FunctionSetResetClause
7723 				{
7724 					/* we abuse the normal content of a DefElem here */
7725 					$$ = makeDefElem("set", (Node *)$1, @1);
7726 				}
7727 			| PARALLEL ColId
7728 				{
7729 					$$ = makeDefElem("parallel", (Node *)makeString($2), @1);
7730 				}
7731 		;
7732 
7733 createfunc_opt_item:
7734 			AS func_as
7735 				{
7736 					$$ = makeDefElem("as", (Node *)$2, @1);
7737 				}
7738 			| LANGUAGE NonReservedWord_or_Sconst
7739 				{
7740 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
7741 				}
7742 			| TRANSFORM transform_type_list
7743 				{
7744 					$$ = makeDefElem("transform", (Node *)$2, @1);
7745 				}
7746 			| WINDOW
7747 				{
7748 					$$ = makeDefElem("window", (Node *)makeInteger(TRUE), @1);
7749 				}
7750 			| common_func_opt_item
7751 				{
7752 					$$ = $1;
7753 				}
7754 		;
7755 
7756 func_as:	Sconst						{ $$ = list_make1(makeString($1)); }
7757 			| Sconst ',' Sconst
7758 				{
7759 					$$ = list_make2(makeString($1), makeString($3));
7760 				}
7761 		;
7762 
7763 transform_type_list:
7764 			FOR TYPE_P Typename { $$ = list_make1($3); }
7765 			| transform_type_list ',' FOR TYPE_P Typename { $$ = lappend($1, $5); }
7766 		;
7767 
7768 opt_definition:
7769 			WITH definition							{ $$ = $2; }
7770 			| /*EMPTY*/								{ $$ = NIL; }
7771 		;
7772 
7773 table_func_column:	param_name func_type
7774 				{
7775 					FunctionParameter *n = makeNode(FunctionParameter);
7776 					n->name = $1;
7777 					n->argType = $2;
7778 					n->mode = FUNC_PARAM_TABLE;
7779 					n->defexpr = NULL;
7780 					$$ = n;
7781 				}
7782 		;
7783 
7784 table_func_column_list:
7785 			table_func_column
7786 				{
7787 					$$ = list_make1($1);
7788 				}
7789 			| table_func_column_list ',' table_func_column
7790 				{
7791 					$$ = lappend($1, $3);
7792 				}
7793 		;
7794 
7795 /*****************************************************************************
7796  * ALTER FUNCTION
7797  *
7798  * RENAME and OWNER subcommands are already provided by the generic
7799  * ALTER infrastructure, here we just specify alterations that can
7800  * only be applied to functions.
7801  *
7802  *****************************************************************************/
7803 AlterFunctionStmt:
7804 			ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
7805 				{
7806 					AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
7807 					n->func = $3;
7808 					n->actions = $4;
7809 					$$ = (Node *) n;
7810 				}
7811 		;
7812 
7813 alterfunc_opt_list:
7814 			/* At least one option must be specified */
7815 			common_func_opt_item					{ $$ = list_make1($1); }
7816 			| alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
7817 		;
7818 
7819 /* Ignored, merely for SQL compliance */
7820 opt_restrict:
7821 			RESTRICT
7822 			| /* EMPTY */
7823 		;
7824 
7825 
7826 /*****************************************************************************
7827  *
7828  *		QUERY:
7829  *
7830  *		DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
7831  *		DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
7832  *		DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
7833  *
7834  *****************************************************************************/
7835 
7836 RemoveFuncStmt:
7837 			DROP FUNCTION function_with_argtypes_list opt_drop_behavior
7838 				{
7839 					DropStmt *n = makeNode(DropStmt);
7840 					n->removeType = OBJECT_FUNCTION;
7841 					n->objects = $3;
7842 					n->behavior = $4;
7843 					n->missing_ok = false;
7844 					n->concurrent = false;
7845 					$$ = (Node *)n;
7846 				}
7847 			| DROP FUNCTION IF_P EXISTS function_with_argtypes_list opt_drop_behavior
7848 				{
7849 					DropStmt *n = makeNode(DropStmt);
7850 					n->removeType = OBJECT_FUNCTION;
7851 					n->objects = $5;
7852 					n->behavior = $6;
7853 					n->missing_ok = true;
7854 					n->concurrent = false;
7855 					$$ = (Node *)n;
7856 				}
7857 		;
7858 
7859 RemoveAggrStmt:
7860 			DROP AGGREGATE aggregate_with_argtypes_list opt_drop_behavior
7861 				{
7862 					DropStmt *n = makeNode(DropStmt);
7863 					n->removeType = OBJECT_AGGREGATE;
7864 					n->objects = $3;
7865 					n->behavior = $4;
7866 					n->missing_ok = false;
7867 					n->concurrent = false;
7868 					$$ = (Node *)n;
7869 				}
7870 			| DROP AGGREGATE IF_P EXISTS aggregate_with_argtypes_list opt_drop_behavior
7871 				{
7872 					DropStmt *n = makeNode(DropStmt);
7873 					n->removeType = OBJECT_AGGREGATE;
7874 					n->objects = $5;
7875 					n->behavior = $6;
7876 					n->missing_ok = true;
7877 					n->concurrent = false;
7878 					$$ = (Node *)n;
7879 				}
7880 		;
7881 
7882 RemoveOperStmt:
7883 			DROP OPERATOR operator_with_argtypes_list opt_drop_behavior
7884 				{
7885 					DropStmt *n = makeNode(DropStmt);
7886 					n->removeType = OBJECT_OPERATOR;
7887 					n->objects = $3;
7888 					n->behavior = $4;
7889 					n->missing_ok = false;
7890 					n->concurrent = false;
7891 					$$ = (Node *)n;
7892 				}
7893 			| DROP OPERATOR IF_P EXISTS operator_with_argtypes_list opt_drop_behavior
7894 				{
7895 					DropStmt *n = makeNode(DropStmt);
7896 					n->removeType = OBJECT_OPERATOR;
7897 					n->objects = $5;
7898 					n->behavior = $6;
7899 					n->missing_ok = true;
7900 					n->concurrent = false;
7901 					$$ = (Node *)n;
7902 				}
7903 		;
7904 
7905 oper_argtypes:
7906 			'(' Typename ')'
7907 				{
7908 				   ereport(ERROR,
7909 						   (errcode(ERRCODE_SYNTAX_ERROR),
7910 							errmsg("missing argument"),
7911 							errhint("Use NONE to denote the missing argument of a unary operator."),
7912 							parser_errposition(@3)));
7913 				}
7914 			| '(' Typename ',' Typename ')'
7915 					{ $$ = list_make2($2, $4); }
7916 			| '(' NONE ',' Typename ')'					/* left unary */
7917 					{ $$ = list_make2(NULL, $4); }
7918 			| '(' Typename ',' NONE ')'					/* right unary */
7919 					{ $$ = list_make2($2, NULL); }
7920 		;
7921 
7922 any_operator:
7923 			all_Op
7924 					{ $$ = list_make1(makeString($1)); }
7925 			| ColId '.' any_operator
7926 					{ $$ = lcons(makeString($1), $3); }
7927 		;
7928 
7929 operator_with_argtypes_list:
7930 			operator_with_argtypes					{ $$ = list_make1($1); }
7931 			| operator_with_argtypes_list ',' operator_with_argtypes
7932 													{ $$ = lappend($1, $3); }
7933 		;
7934 
7935 operator_with_argtypes:
7936 			any_operator oper_argtypes
7937 				{
7938 					ObjectWithArgs *n = makeNode(ObjectWithArgs);
7939 					n->objname = $1;
7940 					n->objargs = $2;
7941 					$$ = n;
7942 				}
7943 		;
7944 
7945 /*****************************************************************************
7946  *
7947  *		DO <anonymous code block> [ LANGUAGE language ]
7948  *
7949  * We use a DefElem list for future extensibility, and to allow flexibility
7950  * in the clause order.
7951  *
7952  *****************************************************************************/
7953 
7954 DoStmt: DO dostmt_opt_list
7955 				{
7956 					DoStmt *n = makeNode(DoStmt);
7957 					n->args = $2;
7958 					$$ = (Node *)n;
7959 				}
7960 		;
7961 
7962 dostmt_opt_list:
7963 			dostmt_opt_item						{ $$ = list_make1($1); }
7964 			| dostmt_opt_list dostmt_opt_item	{ $$ = lappend($1, $2); }
7965 		;
7966 
7967 dostmt_opt_item:
7968 			Sconst
7969 				{
7970 					$$ = makeDefElem("as", (Node *)makeString($1), @1);
7971 				}
7972 			| LANGUAGE NonReservedWord_or_Sconst
7973 				{
7974 					$$ = makeDefElem("language", (Node *)makeString($2), @1);
7975 				}
7976 		;
7977 
7978 /*****************************************************************************
7979  *
7980  *		CREATE CAST / DROP CAST
7981  *
7982  *****************************************************************************/
7983 
7984 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
7985 					WITH FUNCTION function_with_argtypes cast_context
7986 				{
7987 					CreateCastStmt *n = makeNode(CreateCastStmt);
7988 					n->sourcetype = $4;
7989 					n->targettype = $6;
7990 					n->func = $10;
7991 					n->context = (CoercionContext) $11;
7992 					n->inout = false;
7993 					$$ = (Node *)n;
7994 				}
7995 			| CREATE CAST '(' Typename AS Typename ')'
7996 					WITHOUT FUNCTION cast_context
7997 				{
7998 					CreateCastStmt *n = makeNode(CreateCastStmt);
7999 					n->sourcetype = $4;
8000 					n->targettype = $6;
8001 					n->func = NULL;
8002 					n->context = (CoercionContext) $10;
8003 					n->inout = false;
8004 					$$ = (Node *)n;
8005 				}
8006 			| CREATE CAST '(' Typename AS Typename ')'
8007 					WITH INOUT cast_context
8008 				{
8009 					CreateCastStmt *n = makeNode(CreateCastStmt);
8010 					n->sourcetype = $4;
8011 					n->targettype = $6;
8012 					n->func = NULL;
8013 					n->context = (CoercionContext) $10;
8014 					n->inout = true;
8015 					$$ = (Node *)n;
8016 				}
8017 		;
8018 
8019 cast_context:  AS IMPLICIT_P					{ $$ = COERCION_IMPLICIT; }
8020 		| AS ASSIGNMENT							{ $$ = COERCION_ASSIGNMENT; }
8021 		| /*EMPTY*/								{ $$ = COERCION_EXPLICIT; }
8022 		;
8023 
8024 
8025 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
8026 				{
8027 					DropStmt *n = makeNode(DropStmt);
8028 					n->removeType = OBJECT_CAST;
8029 					n->objects = list_make1(list_make2($5, $7));
8030 					n->behavior = $9;
8031 					n->missing_ok = $3;
8032 					n->concurrent = false;
8033 					$$ = (Node *)n;
8034 				}
8035 		;
8036 
8037 opt_if_exists: IF_P EXISTS						{ $$ = TRUE; }
8038 		| /*EMPTY*/								{ $$ = FALSE; }
8039 		;
8040 
8041 
8042 /*****************************************************************************
8043  *
8044  *		CREATE TRANSFORM / DROP TRANSFORM
8045  *
8046  *****************************************************************************/
8047 
8048 CreateTransformStmt: CREATE opt_or_replace TRANSFORM FOR Typename LANGUAGE name '(' transform_element_list ')'
8049 				{
8050 					CreateTransformStmt *n = makeNode(CreateTransformStmt);
8051 					n->replace = $2;
8052 					n->type_name = $5;
8053 					n->lang = $7;
8054 					n->fromsql = linitial($9);
8055 					n->tosql = lsecond($9);
8056 					$$ = (Node *)n;
8057 				}
8058 		;
8059 
8060 transform_element_list: FROM SQL_P WITH FUNCTION function_with_argtypes ',' TO SQL_P WITH FUNCTION function_with_argtypes
8061 				{
8062 					$$ = list_make2($5, $11);
8063 				}
8064 				| TO SQL_P WITH FUNCTION function_with_argtypes ',' FROM SQL_P WITH FUNCTION function_with_argtypes
8065 				{
8066 					$$ = list_make2($11, $5);
8067 				}
8068 				| FROM SQL_P WITH FUNCTION function_with_argtypes
8069 				{
8070 					$$ = list_make2($5, NULL);
8071 				}
8072 				| TO SQL_P WITH FUNCTION function_with_argtypes
8073 				{
8074 					$$ = list_make2(NULL, $5);
8075 				}
8076 		;
8077 
8078 
8079 DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_drop_behavior
8080 				{
8081 					DropStmt *n = makeNode(DropStmt);
8082 					n->removeType = OBJECT_TRANSFORM;
8083 					n->objects = list_make1(list_make2($5, makeString($7)));
8084 					n->behavior = $8;
8085 					n->missing_ok = $3;
8086 					$$ = (Node *)n;
8087 				}
8088 		;
8089 
8090 
8091 /*****************************************************************************
8092  *
8093  *		QUERY:
8094  *
8095  *		REINDEX [ (options) ] type <name>
8096  *****************************************************************************/
8097 
8098 ReindexStmt:
8099 			REINDEX reindex_target_type qualified_name
8100 				{
8101 					ReindexStmt *n = makeNode(ReindexStmt);
8102 					n->kind = $2;
8103 					n->relation = $3;
8104 					n->name = NULL;
8105 					n->options = 0;
8106 					$$ = (Node *)n;
8107 				}
8108 			| REINDEX reindex_target_multitable name
8109 				{
8110 					ReindexStmt *n = makeNode(ReindexStmt);
8111 					n->kind = $2;
8112 					n->name = $3;
8113 					n->relation = NULL;
8114 					n->options = 0;
8115 					$$ = (Node *)n;
8116 				}
8117 			| REINDEX '(' reindex_option_list ')' reindex_target_type qualified_name
8118 				{
8119 					ReindexStmt *n = makeNode(ReindexStmt);
8120 					n->kind = $5;
8121 					n->relation = $6;
8122 					n->name = NULL;
8123 					n->options = $3;
8124 					$$ = (Node *)n;
8125 				}
8126 			| REINDEX '(' reindex_option_list ')' reindex_target_multitable name
8127 				{
8128 					ReindexStmt *n = makeNode(ReindexStmt);
8129 					n->kind = $5;
8130 					n->name = $6;
8131 					n->relation = NULL;
8132 					n->options = $3;
8133 					$$ = (Node *)n;
8134 				}
8135 		;
8136 reindex_target_type:
8137 			INDEX					{ $$ = REINDEX_OBJECT_INDEX; }
8138 			| TABLE					{ $$ = REINDEX_OBJECT_TABLE; }
8139 		;
8140 reindex_target_multitable:
8141 			SCHEMA					{ $$ = REINDEX_OBJECT_SCHEMA; }
8142 			| SYSTEM_P				{ $$ = REINDEX_OBJECT_SYSTEM; }
8143 			| DATABASE				{ $$ = REINDEX_OBJECT_DATABASE; }
8144 		;
8145 reindex_option_list:
8146 			reindex_option_elem								{ $$ = $1; }
8147 			| reindex_option_list ',' reindex_option_elem	{ $$ = $1 | $3; }
8148 		;
8149 reindex_option_elem:
8150 			VERBOSE	{ $$ = REINDEXOPT_VERBOSE; }
8151 		;
8152 
8153 /*****************************************************************************
8154  *
8155  * ALTER TABLESPACE
8156  *
8157  *****************************************************************************/
8158 
8159 AlterTblSpcStmt:
8160 			ALTER TABLESPACE name SET reloptions
8161 				{
8162 					AlterTableSpaceOptionsStmt *n =
8163 						makeNode(AlterTableSpaceOptionsStmt);
8164 					n->tablespacename = $3;
8165 					n->options = $5;
8166 					n->isReset = FALSE;
8167 					$$ = (Node *)n;
8168 				}
8169 			| ALTER TABLESPACE name RESET reloptions
8170 				{
8171 					AlterTableSpaceOptionsStmt *n =
8172 						makeNode(AlterTableSpaceOptionsStmt);
8173 					n->tablespacename = $3;
8174 					n->options = $5;
8175 					n->isReset = TRUE;
8176 					$$ = (Node *)n;
8177 				}
8178 		;
8179 
8180 /*****************************************************************************
8181  *
8182  * ALTER THING name RENAME TO newname
8183  *
8184  *****************************************************************************/
8185 
8186 RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
8187 				{
8188 					RenameStmt *n = makeNode(RenameStmt);
8189 					n->renameType = OBJECT_AGGREGATE;
8190 					n->object = (Node *) $3;
8191 					n->newname = $6;
8192 					n->missing_ok = false;
8193 					$$ = (Node *)n;
8194 				}
8195 			| ALTER COLLATION any_name RENAME TO name
8196 				{
8197 					RenameStmt *n = makeNode(RenameStmt);
8198 					n->renameType = OBJECT_COLLATION;
8199 					n->object = (Node *) $3;
8200 					n->newname = $6;
8201 					n->missing_ok = false;
8202 					$$ = (Node *)n;
8203 				}
8204 			| ALTER CONVERSION_P any_name RENAME TO name
8205 				{
8206 					RenameStmt *n = makeNode(RenameStmt);
8207 					n->renameType = OBJECT_CONVERSION;
8208 					n->object = (Node *) $3;
8209 					n->newname = $6;
8210 					n->missing_ok = false;
8211 					$$ = (Node *)n;
8212 				}
8213 			| ALTER DATABASE database_name RENAME TO database_name
8214 				{
8215 					RenameStmt *n = makeNode(RenameStmt);
8216 					n->renameType = OBJECT_DATABASE;
8217 					n->subname = $3;
8218 					n->newname = $6;
8219 					n->missing_ok = false;
8220 					$$ = (Node *)n;
8221 				}
8222 			| ALTER DOMAIN_P any_name RENAME TO name
8223 				{
8224 					RenameStmt *n = makeNode(RenameStmt);
8225 					n->renameType = OBJECT_DOMAIN;
8226 					n->object = (Node *) $3;
8227 					n->newname = $6;
8228 					n->missing_ok = false;
8229 					$$ = (Node *)n;
8230 				}
8231 			| ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
8232 				{
8233 					RenameStmt *n = makeNode(RenameStmt);
8234 					n->renameType = OBJECT_DOMCONSTRAINT;
8235 					n->object = (Node *) $3;
8236 					n->subname = $6;
8237 					n->newname = $8;
8238 					$$ = (Node *)n;
8239 				}
8240 			| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
8241 				{
8242 					RenameStmt *n = makeNode(RenameStmt);
8243 					n->renameType = OBJECT_FDW;
8244 					n->object = (Node *) makeString($5);
8245 					n->newname = $8;
8246 					n->missing_ok = false;
8247 					$$ = (Node *)n;
8248 				}
8249 			| ALTER FUNCTION function_with_argtypes RENAME TO name
8250 				{
8251 					RenameStmt *n = makeNode(RenameStmt);
8252 					n->renameType = OBJECT_FUNCTION;
8253 					n->object = (Node *) $3;
8254 					n->newname = $6;
8255 					n->missing_ok = false;
8256 					$$ = (Node *)n;
8257 				}
8258 			| ALTER GROUP_P RoleId RENAME TO RoleId
8259 				{
8260 					RenameStmt *n = makeNode(RenameStmt);
8261 					n->renameType = OBJECT_ROLE;
8262 					n->subname = $3;
8263 					n->newname = $6;
8264 					n->missing_ok = false;
8265 					$$ = (Node *)n;
8266 				}
8267 			| ALTER opt_procedural LANGUAGE name RENAME TO name
8268 				{
8269 					RenameStmt *n = makeNode(RenameStmt);
8270 					n->renameType = OBJECT_LANGUAGE;
8271 					n->object = (Node *) makeString($4);
8272 					n->newname = $7;
8273 					n->missing_ok = false;
8274 					$$ = (Node *)n;
8275 				}
8276 			| ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
8277 				{
8278 					RenameStmt *n = makeNode(RenameStmt);
8279 					n->renameType = OBJECT_OPCLASS;
8280 					n->object = (Node *) lcons(makeString($6), $4);
8281 					n->newname = $9;
8282 					n->missing_ok = false;
8283 					$$ = (Node *)n;
8284 				}
8285 			| ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
8286 				{
8287 					RenameStmt *n = makeNode(RenameStmt);
8288 					n->renameType = OBJECT_OPFAMILY;
8289 					n->object = (Node *) lcons(makeString($6), $4);
8290 					n->newname = $9;
8291 					n->missing_ok = false;
8292 					$$ = (Node *)n;
8293 				}
8294 			| ALTER POLICY name ON qualified_name RENAME TO name
8295 				{
8296 					RenameStmt *n = makeNode(RenameStmt);
8297 					n->renameType = OBJECT_POLICY;
8298 					n->relation = $5;
8299 					n->subname = $3;
8300 					n->newname = $8;
8301 					n->missing_ok = false;
8302 					$$ = (Node *)n;
8303 				}
8304 			| ALTER POLICY IF_P EXISTS name ON qualified_name RENAME TO name
8305 				{
8306 					RenameStmt *n = makeNode(RenameStmt);
8307 					n->renameType = OBJECT_POLICY;
8308 					n->relation = $7;
8309 					n->subname = $5;
8310 					n->newname = $10;
8311 					n->missing_ok = true;
8312 					$$ = (Node *)n;
8313 				}
8314 			| ALTER PUBLICATION name RENAME TO name
8315 				{
8316 					RenameStmt *n = makeNode(RenameStmt);
8317 					n->renameType = OBJECT_PUBLICATION;
8318 					n->object = (Node *) makeString($3);
8319 					n->newname = $6;
8320 					n->missing_ok = false;
8321 					$$ = (Node *)n;
8322 				}
8323 			| ALTER SCHEMA name RENAME TO name
8324 				{
8325 					RenameStmt *n = makeNode(RenameStmt);
8326 					n->renameType = OBJECT_SCHEMA;
8327 					n->subname = $3;
8328 					n->newname = $6;
8329 					n->missing_ok = false;
8330 					$$ = (Node *)n;
8331 				}
8332 			| ALTER SERVER name RENAME TO name
8333 				{
8334 					RenameStmt *n = makeNode(RenameStmt);
8335 					n->renameType = OBJECT_FOREIGN_SERVER;
8336 					n->object = (Node *) makeString($3);
8337 					n->newname = $6;
8338 					n->missing_ok = false;
8339 					$$ = (Node *)n;
8340 				}
8341 			| ALTER SUBSCRIPTION name RENAME TO name
8342 				{
8343 					RenameStmt *n = makeNode(RenameStmt);
8344 					n->renameType = OBJECT_SUBSCRIPTION;
8345 					n->object = (Node *) makeString($3);
8346 					n->newname = $6;
8347 					n->missing_ok = false;
8348 					$$ = (Node *)n;
8349 				}
8350 			| ALTER TABLE relation_expr RENAME TO name
8351 				{
8352 					RenameStmt *n = makeNode(RenameStmt);
8353 					n->renameType = OBJECT_TABLE;
8354 					n->relation = $3;
8355 					n->subname = NULL;
8356 					n->newname = $6;
8357 					n->missing_ok = false;
8358 					$$ = (Node *)n;
8359 				}
8360 			| ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
8361 				{
8362 					RenameStmt *n = makeNode(RenameStmt);
8363 					n->renameType = OBJECT_TABLE;
8364 					n->relation = $5;
8365 					n->subname = NULL;
8366 					n->newname = $8;
8367 					n->missing_ok = true;
8368 					$$ = (Node *)n;
8369 				}
8370 			| ALTER SEQUENCE qualified_name RENAME TO name
8371 				{
8372 					RenameStmt *n = makeNode(RenameStmt);
8373 					n->renameType = OBJECT_SEQUENCE;
8374 					n->relation = $3;
8375 					n->subname = NULL;
8376 					n->newname = $6;
8377 					n->missing_ok = false;
8378 					$$ = (Node *)n;
8379 				}
8380 			| ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
8381 				{
8382 					RenameStmt *n = makeNode(RenameStmt);
8383 					n->renameType = OBJECT_SEQUENCE;
8384 					n->relation = $5;
8385 					n->subname = NULL;
8386 					n->newname = $8;
8387 					n->missing_ok = true;
8388 					$$ = (Node *)n;
8389 				}
8390 			| ALTER VIEW qualified_name RENAME TO name
8391 				{
8392 					RenameStmt *n = makeNode(RenameStmt);
8393 					n->renameType = OBJECT_VIEW;
8394 					n->relation = $3;
8395 					n->subname = NULL;
8396 					n->newname = $6;
8397 					n->missing_ok = false;
8398 					$$ = (Node *)n;
8399 				}
8400 			| ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
8401 				{
8402 					RenameStmt *n = makeNode(RenameStmt);
8403 					n->renameType = OBJECT_VIEW;
8404 					n->relation = $5;
8405 					n->subname = NULL;
8406 					n->newname = $8;
8407 					n->missing_ok = true;
8408 					$$ = (Node *)n;
8409 				}
8410 			| ALTER MATERIALIZED VIEW qualified_name RENAME TO name
8411 				{
8412 					RenameStmt *n = makeNode(RenameStmt);
8413 					n->renameType = OBJECT_MATVIEW;
8414 					n->relation = $4;
8415 					n->subname = NULL;
8416 					n->newname = $7;
8417 					n->missing_ok = false;
8418 					$$ = (Node *)n;
8419 				}
8420 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
8421 				{
8422 					RenameStmt *n = makeNode(RenameStmt);
8423 					n->renameType = OBJECT_MATVIEW;
8424 					n->relation = $6;
8425 					n->subname = NULL;
8426 					n->newname = $9;
8427 					n->missing_ok = true;
8428 					$$ = (Node *)n;
8429 				}
8430 			| ALTER INDEX qualified_name RENAME TO name
8431 				{
8432 					RenameStmt *n = makeNode(RenameStmt);
8433 					n->renameType = OBJECT_INDEX;
8434 					n->relation = $3;
8435 					n->subname = NULL;
8436 					n->newname = $6;
8437 					n->missing_ok = false;
8438 					$$ = (Node *)n;
8439 				}
8440 			| ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
8441 				{
8442 					RenameStmt *n = makeNode(RenameStmt);
8443 					n->renameType = OBJECT_INDEX;
8444 					n->relation = $5;
8445 					n->subname = NULL;
8446 					n->newname = $8;
8447 					n->missing_ok = true;
8448 					$$ = (Node *)n;
8449 				}
8450 			| ALTER FOREIGN TABLE relation_expr RENAME TO name
8451 				{
8452 					RenameStmt *n = makeNode(RenameStmt);
8453 					n->renameType = OBJECT_FOREIGN_TABLE;
8454 					n->relation = $4;
8455 					n->subname = NULL;
8456 					n->newname = $7;
8457 					n->missing_ok = false;
8458 					$$ = (Node *)n;
8459 				}
8460 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
8461 				{
8462 					RenameStmt *n = makeNode(RenameStmt);
8463 					n->renameType = OBJECT_FOREIGN_TABLE;
8464 					n->relation = $6;
8465 					n->subname = NULL;
8466 					n->newname = $9;
8467 					n->missing_ok = true;
8468 					$$ = (Node *)n;
8469 				}
8470 			| ALTER TABLE relation_expr RENAME opt_column name TO name
8471 				{
8472 					RenameStmt *n = makeNode(RenameStmt);
8473 					n->renameType = OBJECT_COLUMN;
8474 					n->relationType = OBJECT_TABLE;
8475 					n->relation = $3;
8476 					n->subname = $6;
8477 					n->newname = $8;
8478 					n->missing_ok = false;
8479 					$$ = (Node *)n;
8480 				}
8481 			| ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8482 				{
8483 					RenameStmt *n = makeNode(RenameStmt);
8484 					n->renameType = OBJECT_COLUMN;
8485 					n->relationType = OBJECT_TABLE;
8486 					n->relation = $5;
8487 					n->subname = $8;
8488 					n->newname = $10;
8489 					n->missing_ok = true;
8490 					$$ = (Node *)n;
8491 				}
8492 			| ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
8493 				{
8494 					RenameStmt *n = makeNode(RenameStmt);
8495 					n->renameType = OBJECT_COLUMN;
8496 					n->relationType = OBJECT_MATVIEW;
8497 					n->relation = $4;
8498 					n->subname = $7;
8499 					n->newname = $9;
8500 					n->missing_ok = false;
8501 					$$ = (Node *)n;
8502 				}
8503 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
8504 				{
8505 					RenameStmt *n = makeNode(RenameStmt);
8506 					n->renameType = OBJECT_COLUMN;
8507 					n->relationType = OBJECT_MATVIEW;
8508 					n->relation = $6;
8509 					n->subname = $9;
8510 					n->newname = $11;
8511 					n->missing_ok = true;
8512 					$$ = (Node *)n;
8513 				}
8514 			| ALTER TABLE relation_expr RENAME CONSTRAINT name TO name
8515 				{
8516 					RenameStmt *n = makeNode(RenameStmt);
8517 					n->renameType = OBJECT_TABCONSTRAINT;
8518 					n->relation = $3;
8519 					n->subname = $6;
8520 					n->newname = $8;
8521 					n->missing_ok = false;
8522 					$$ = (Node *)n;
8523 				}
8524 			| ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
8525 				{
8526 					RenameStmt *n = makeNode(RenameStmt);
8527 					n->renameType = OBJECT_TABCONSTRAINT;
8528 					n->relation = $5;
8529 					n->subname = $8;
8530 					n->newname = $10;
8531 					n->missing_ok = true;
8532 					$$ = (Node *)n;
8533 				}
8534 			| ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
8535 				{
8536 					RenameStmt *n = makeNode(RenameStmt);
8537 					n->renameType = OBJECT_COLUMN;
8538 					n->relationType = OBJECT_FOREIGN_TABLE;
8539 					n->relation = $4;
8540 					n->subname = $7;
8541 					n->newname = $9;
8542 					n->missing_ok = false;
8543 					$$ = (Node *)n;
8544 				}
8545 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8546 				{
8547 					RenameStmt *n = makeNode(RenameStmt);
8548 					n->renameType = OBJECT_COLUMN;
8549 					n->relationType = OBJECT_FOREIGN_TABLE;
8550 					n->relation = $6;
8551 					n->subname = $9;
8552 					n->newname = $11;
8553 					n->missing_ok = true;
8554 					$$ = (Node *)n;
8555 				}
8556 			| ALTER RULE name ON qualified_name RENAME TO name
8557 				{
8558 					RenameStmt *n = makeNode(RenameStmt);
8559 					n->renameType = OBJECT_RULE;
8560 					n->relation = $5;
8561 					n->subname = $3;
8562 					n->newname = $8;
8563 					n->missing_ok = false;
8564 					$$ = (Node *)n;
8565 				}
8566 			| ALTER TRIGGER name ON qualified_name RENAME TO name
8567 				{
8568 					RenameStmt *n = makeNode(RenameStmt);
8569 					n->renameType = OBJECT_TRIGGER;
8570 					n->relation = $5;
8571 					n->subname = $3;
8572 					n->newname = $8;
8573 					n->missing_ok = false;
8574 					$$ = (Node *)n;
8575 				}
8576 			| ALTER EVENT TRIGGER name RENAME TO name
8577 				{
8578 					RenameStmt *n = makeNode(RenameStmt);
8579 					n->renameType = OBJECT_EVENT_TRIGGER;
8580 					n->object = (Node *) makeString($4);
8581 					n->newname = $7;
8582 					$$ = (Node *)n;
8583 				}
8584 			| ALTER ROLE RoleId RENAME TO RoleId
8585 				{
8586 					RenameStmt *n = makeNode(RenameStmt);
8587 					n->renameType = OBJECT_ROLE;
8588 					n->subname = $3;
8589 					n->newname = $6;
8590 					n->missing_ok = false;
8591 					$$ = (Node *)n;
8592 				}
8593 			| ALTER USER RoleId RENAME TO RoleId
8594 				{
8595 					RenameStmt *n = makeNode(RenameStmt);
8596 					n->renameType = OBJECT_ROLE;
8597 					n->subname = $3;
8598 					n->newname = $6;
8599 					n->missing_ok = false;
8600 					$$ = (Node *)n;
8601 				}
8602 			| ALTER TABLESPACE name RENAME TO name
8603 				{
8604 					RenameStmt *n = makeNode(RenameStmt);
8605 					n->renameType = OBJECT_TABLESPACE;
8606 					n->subname = $3;
8607 					n->newname = $6;
8608 					n->missing_ok = false;
8609 					$$ = (Node *)n;
8610 				}
8611 			| ALTER STATISTICS any_name RENAME TO name
8612 				{
8613 					RenameStmt *n = makeNode(RenameStmt);
8614 					n->renameType = OBJECT_STATISTIC_EXT;
8615 					n->object = (Node *) $3;
8616 					n->newname = $6;
8617 					n->missing_ok = false;
8618 					$$ = (Node *)n;
8619 				}
8620 			| ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
8621 				{
8622 					RenameStmt *n = makeNode(RenameStmt);
8623 					n->renameType = OBJECT_TSPARSER;
8624 					n->object = (Node *) $5;
8625 					n->newname = $8;
8626 					n->missing_ok = false;
8627 					$$ = (Node *)n;
8628 				}
8629 			| ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
8630 				{
8631 					RenameStmt *n = makeNode(RenameStmt);
8632 					n->renameType = OBJECT_TSDICTIONARY;
8633 					n->object = (Node *) $5;
8634 					n->newname = $8;
8635 					n->missing_ok = false;
8636 					$$ = (Node *)n;
8637 				}
8638 			| ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
8639 				{
8640 					RenameStmt *n = makeNode(RenameStmt);
8641 					n->renameType = OBJECT_TSTEMPLATE;
8642 					n->object = (Node *) $5;
8643 					n->newname = $8;
8644 					n->missing_ok = false;
8645 					$$ = (Node *)n;
8646 				}
8647 			| ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
8648 				{
8649 					RenameStmt *n = makeNode(RenameStmt);
8650 					n->renameType = OBJECT_TSCONFIGURATION;
8651 					n->object = (Node *) $5;
8652 					n->newname = $8;
8653 					n->missing_ok = false;
8654 					$$ = (Node *)n;
8655 				}
8656 			| ALTER TYPE_P any_name RENAME TO name
8657 				{
8658 					RenameStmt *n = makeNode(RenameStmt);
8659 					n->renameType = OBJECT_TYPE;
8660 					n->object = (Node *) $3;
8661 					n->newname = $6;
8662 					n->missing_ok = false;
8663 					$$ = (Node *)n;
8664 				}
8665 			| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
8666 				{
8667 					RenameStmt *n = makeNode(RenameStmt);
8668 					n->renameType = OBJECT_ATTRIBUTE;
8669 					n->relationType = OBJECT_TYPE;
8670 					n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
8671 					n->subname = $6;
8672 					n->newname = $8;
8673 					n->behavior = $9;
8674 					n->missing_ok = false;
8675 					$$ = (Node *)n;
8676 				}
8677 		;
8678 
8679 opt_column: COLUMN									{ $$ = COLUMN; }
8680 			| /*EMPTY*/								{ $$ = 0; }
8681 		;
8682 
8683 opt_set_data: SET DATA_P							{ $$ = 1; }
8684 			| /*EMPTY*/								{ $$ = 0; }
8685 		;
8686 
8687 /*****************************************************************************
8688  *
8689  * ALTER THING name DEPENDS ON EXTENSION name
8690  *
8691  *****************************************************************************/
8692 
8693 AlterObjectDependsStmt:
8694 			ALTER FUNCTION function_with_argtypes DEPENDS ON EXTENSION name
8695 				{
8696 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8697 					n->objectType = OBJECT_FUNCTION;
8698 					n->object = (Node *) $3;
8699 					n->extname = makeString($7);
8700 					$$ = (Node *)n;
8701 				}
8702 			| ALTER TRIGGER name ON qualified_name DEPENDS ON EXTENSION name
8703 				{
8704 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8705 					n->objectType = OBJECT_TRIGGER;
8706 					n->relation = $5;
8707 					n->object = (Node *) list_make1(makeString($3));
8708 					n->extname = makeString($9);
8709 					$$ = (Node *)n;
8710 				}
8711 			| ALTER MATERIALIZED VIEW qualified_name DEPENDS ON EXTENSION name
8712 				{
8713 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8714 					n->objectType = OBJECT_MATVIEW;
8715 					n->relation = $4;
8716 					n->extname = makeString($8);
8717 					$$ = (Node *)n;
8718 				}
8719 			| ALTER INDEX qualified_name DEPENDS ON EXTENSION name
8720 				{
8721 					AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
8722 					n->objectType = OBJECT_INDEX;
8723 					n->relation = $3;
8724 					n->extname = makeString($7);
8725 					$$ = (Node *)n;
8726 				}
8727 		;
8728 
8729 /*****************************************************************************
8730  *
8731  * ALTER THING name SET SCHEMA name
8732  *
8733  *****************************************************************************/
8734 
8735 AlterObjectSchemaStmt:
8736 			ALTER AGGREGATE aggregate_with_argtypes SET SCHEMA name
8737 				{
8738 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8739 					n->objectType = OBJECT_AGGREGATE;
8740 					n->object = (Node *) $3;
8741 					n->newschema = $6;
8742 					n->missing_ok = false;
8743 					$$ = (Node *)n;
8744 				}
8745 			| ALTER COLLATION any_name SET SCHEMA name
8746 				{
8747 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8748 					n->objectType = OBJECT_COLLATION;
8749 					n->object = (Node *) $3;
8750 					n->newschema = $6;
8751 					n->missing_ok = false;
8752 					$$ = (Node *)n;
8753 				}
8754 			| ALTER CONVERSION_P any_name SET SCHEMA name
8755 				{
8756 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8757 					n->objectType = OBJECT_CONVERSION;
8758 					n->object = (Node *) $3;
8759 					n->newschema = $6;
8760 					n->missing_ok = false;
8761 					$$ = (Node *)n;
8762 				}
8763 			| ALTER DOMAIN_P any_name SET SCHEMA name
8764 				{
8765 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8766 					n->objectType = OBJECT_DOMAIN;
8767 					n->object = (Node *) $3;
8768 					n->newschema = $6;
8769 					n->missing_ok = false;
8770 					$$ = (Node *)n;
8771 				}
8772 			| ALTER EXTENSION name SET SCHEMA name
8773 				{
8774 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8775 					n->objectType = OBJECT_EXTENSION;
8776 					n->object = (Node *) makeString($3);
8777 					n->newschema = $6;
8778 					n->missing_ok = false;
8779 					$$ = (Node *)n;
8780 				}
8781 			| ALTER FUNCTION function_with_argtypes SET SCHEMA name
8782 				{
8783 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8784 					n->objectType = OBJECT_FUNCTION;
8785 					n->object = (Node *) $3;
8786 					n->newschema = $6;
8787 					n->missing_ok = false;
8788 					$$ = (Node *)n;
8789 				}
8790 			| ALTER OPERATOR operator_with_argtypes SET SCHEMA name
8791 				{
8792 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8793 					n->objectType = OBJECT_OPERATOR;
8794 					n->object = (Node *) $3;
8795 					n->newschema = $6;
8796 					n->missing_ok = false;
8797 					$$ = (Node *)n;
8798 				}
8799 			| ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
8800 				{
8801 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8802 					n->objectType = OBJECT_OPCLASS;
8803 					n->object = (Node *) lcons(makeString($6), $4);
8804 					n->newschema = $9;
8805 					n->missing_ok = false;
8806 					$$ = (Node *)n;
8807 				}
8808 			| ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
8809 				{
8810 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8811 					n->objectType = OBJECT_OPFAMILY;
8812 					n->object = (Node *) lcons(makeString($6), $4);
8813 					n->newschema = $9;
8814 					n->missing_ok = false;
8815 					$$ = (Node *)n;
8816 				}
8817 			| ALTER TABLE relation_expr SET SCHEMA name
8818 				{
8819 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8820 					n->objectType = OBJECT_TABLE;
8821 					n->relation = $3;
8822 					n->newschema = $6;
8823 					n->missing_ok = false;
8824 					$$ = (Node *)n;
8825 				}
8826 			| ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
8827 				{
8828 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8829 					n->objectType = OBJECT_TABLE;
8830 					n->relation = $5;
8831 					n->newschema = $8;
8832 					n->missing_ok = true;
8833 					$$ = (Node *)n;
8834 				}
8835 			| ALTER STATISTICS any_name SET SCHEMA name
8836 				{
8837 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8838 					n->objectType = OBJECT_STATISTIC_EXT;
8839 					n->object = (Node *) $3;
8840 					n->newschema = $6;
8841 					n->missing_ok = false;
8842 					$$ = (Node *)n;
8843 				}
8844 			| ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
8845 				{
8846 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8847 					n->objectType = OBJECT_TSPARSER;
8848 					n->object = (Node *) $5;
8849 					n->newschema = $8;
8850 					n->missing_ok = false;
8851 					$$ = (Node *)n;
8852 				}
8853 			| ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
8854 				{
8855 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8856 					n->objectType = OBJECT_TSDICTIONARY;
8857 					n->object = (Node *) $5;
8858 					n->newschema = $8;
8859 					n->missing_ok = false;
8860 					$$ = (Node *)n;
8861 				}
8862 			| ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
8863 				{
8864 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8865 					n->objectType = OBJECT_TSTEMPLATE;
8866 					n->object = (Node *) $5;
8867 					n->newschema = $8;
8868 					n->missing_ok = false;
8869 					$$ = (Node *)n;
8870 				}
8871 			| ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
8872 				{
8873 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8874 					n->objectType = OBJECT_TSCONFIGURATION;
8875 					n->object = (Node *) $5;
8876 					n->newschema = $8;
8877 					n->missing_ok = false;
8878 					$$ = (Node *)n;
8879 				}
8880 			| ALTER SEQUENCE qualified_name SET SCHEMA name
8881 				{
8882 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8883 					n->objectType = OBJECT_SEQUENCE;
8884 					n->relation = $3;
8885 					n->newschema = $6;
8886 					n->missing_ok = false;
8887 					$$ = (Node *)n;
8888 				}
8889 			| ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
8890 				{
8891 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8892 					n->objectType = OBJECT_SEQUENCE;
8893 					n->relation = $5;
8894 					n->newschema = $8;
8895 					n->missing_ok = true;
8896 					$$ = (Node *)n;
8897 				}
8898 			| ALTER VIEW qualified_name SET SCHEMA name
8899 				{
8900 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8901 					n->objectType = OBJECT_VIEW;
8902 					n->relation = $3;
8903 					n->newschema = $6;
8904 					n->missing_ok = false;
8905 					$$ = (Node *)n;
8906 				}
8907 			| ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
8908 				{
8909 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8910 					n->objectType = OBJECT_VIEW;
8911 					n->relation = $5;
8912 					n->newschema = $8;
8913 					n->missing_ok = true;
8914 					$$ = (Node *)n;
8915 				}
8916 			| ALTER MATERIALIZED VIEW qualified_name SET SCHEMA name
8917 				{
8918 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8919 					n->objectType = OBJECT_MATVIEW;
8920 					n->relation = $4;
8921 					n->newschema = $7;
8922 					n->missing_ok = false;
8923 					$$ = (Node *)n;
8924 				}
8925 			| ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name SET SCHEMA name
8926 				{
8927 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8928 					n->objectType = OBJECT_MATVIEW;
8929 					n->relation = $6;
8930 					n->newschema = $9;
8931 					n->missing_ok = true;
8932 					$$ = (Node *)n;
8933 				}
8934 			| ALTER FOREIGN TABLE relation_expr SET SCHEMA name
8935 				{
8936 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8937 					n->objectType = OBJECT_FOREIGN_TABLE;
8938 					n->relation = $4;
8939 					n->newschema = $7;
8940 					n->missing_ok = false;
8941 					$$ = (Node *)n;
8942 				}
8943 			| ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
8944 				{
8945 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8946 					n->objectType = OBJECT_FOREIGN_TABLE;
8947 					n->relation = $6;
8948 					n->newschema = $9;
8949 					n->missing_ok = true;
8950 					$$ = (Node *)n;
8951 				}
8952 			| ALTER TYPE_P any_name SET SCHEMA name
8953 				{
8954 					AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
8955 					n->objectType = OBJECT_TYPE;
8956 					n->object = (Node *) $3;
8957 					n->newschema = $6;
8958 					n->missing_ok = false;
8959 					$$ = (Node *)n;
8960 				}
8961 		;
8962 
8963 /*****************************************************************************
8964  *
8965  * ALTER OPERATOR name SET define
8966  *
8967  *****************************************************************************/
8968 
8969 AlterOperatorStmt:
8970 			ALTER OPERATOR operator_with_argtypes SET '(' operator_def_list ')'
8971 				{
8972 					AlterOperatorStmt *n = makeNode(AlterOperatorStmt);
8973 					n->opername = $3;
8974 					n->options = $6;
8975 					$$ = (Node *)n;
8976 				}
8977 		;
8978 
8979 operator_def_list:	operator_def_elem								{ $$ = list_make1($1); }
8980 			| operator_def_list ',' operator_def_elem				{ $$ = lappend($1, $3); }
8981 		;
8982 
8983 operator_def_elem: ColLabel '=' NONE
8984 						{ $$ = makeDefElem($1, NULL, @1); }
8985 				   | ColLabel '=' operator_def_arg
8986 						{ $$ = makeDefElem($1, (Node *) $3, @1); }
8987 		;
8988 
8989 /* must be similar enough to def_arg to avoid reduce/reduce conflicts */
8990 operator_def_arg:
8991 			func_type						{ $$ = (Node *)$1; }
8992 			| reserved_keyword				{ $$ = (Node *)makeString(pstrdup($1)); }
8993 			| qual_all_Op					{ $$ = (Node *)$1; }
8994 			| NumericOnly					{ $$ = (Node *)$1; }
8995 			| Sconst						{ $$ = (Node *)makeString($1); }
8996 		;
8997 
8998 /*****************************************************************************
8999  *
9000  * ALTER THING name OWNER TO newname
9001  *
9002  *****************************************************************************/
9003 
9004 AlterOwnerStmt: ALTER AGGREGATE aggregate_with_argtypes OWNER TO RoleSpec
9005 				{
9006 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9007 					n->objectType = OBJECT_AGGREGATE;
9008 					n->object = (Node *) $3;
9009 					n->newowner = $6;
9010 					$$ = (Node *)n;
9011 				}
9012 			| ALTER COLLATION any_name OWNER TO RoleSpec
9013 				{
9014 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9015 					n->objectType = OBJECT_COLLATION;
9016 					n->object = (Node *) $3;
9017 					n->newowner = $6;
9018 					$$ = (Node *)n;
9019 				}
9020 			| ALTER CONVERSION_P any_name OWNER TO RoleSpec
9021 				{
9022 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9023 					n->objectType = OBJECT_CONVERSION;
9024 					n->object = (Node *) $3;
9025 					n->newowner = $6;
9026 					$$ = (Node *)n;
9027 				}
9028 			| ALTER DATABASE database_name OWNER TO RoleSpec
9029 				{
9030 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9031 					n->objectType = OBJECT_DATABASE;
9032 					n->object = (Node *) makeString($3);
9033 					n->newowner = $6;
9034 					$$ = (Node *)n;
9035 				}
9036 			| ALTER DOMAIN_P any_name OWNER TO RoleSpec
9037 				{
9038 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9039 					n->objectType = OBJECT_DOMAIN;
9040 					n->object = (Node *) $3;
9041 					n->newowner = $6;
9042 					$$ = (Node *)n;
9043 				}
9044 			| ALTER FUNCTION function_with_argtypes OWNER TO RoleSpec
9045 				{
9046 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9047 					n->objectType = OBJECT_FUNCTION;
9048 					n->object = (Node *) $3;
9049 					n->newowner = $6;
9050 					$$ = (Node *)n;
9051 				}
9052 			| ALTER opt_procedural LANGUAGE name OWNER TO RoleSpec
9053 				{
9054 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9055 					n->objectType = OBJECT_LANGUAGE;
9056 					n->object = (Node *) makeString($4);
9057 					n->newowner = $7;
9058 					$$ = (Node *)n;
9059 				}
9060 			| ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleSpec
9061 				{
9062 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9063 					n->objectType = OBJECT_LARGEOBJECT;
9064 					n->object = (Node *) $4;
9065 					n->newowner = $7;
9066 					$$ = (Node *)n;
9067 				}
9068 			| ALTER OPERATOR operator_with_argtypes OWNER TO RoleSpec
9069 				{
9070 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9071 					n->objectType = OBJECT_OPERATOR;
9072 					n->object = (Node *) $3;
9073 					n->newowner = $6;
9074 					$$ = (Node *)n;
9075 				}
9076 			| ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleSpec
9077 				{
9078 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9079 					n->objectType = OBJECT_OPCLASS;
9080 					n->object = (Node *) lcons(makeString($6), $4);
9081 					n->newowner = $9;
9082 					$$ = (Node *)n;
9083 				}
9084 			| ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleSpec
9085 				{
9086 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9087 					n->objectType = OBJECT_OPFAMILY;
9088 					n->object = (Node *) lcons(makeString($6), $4);
9089 					n->newowner = $9;
9090 					$$ = (Node *)n;
9091 				}
9092 			| ALTER SCHEMA name OWNER TO RoleSpec
9093 				{
9094 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9095 					n->objectType = OBJECT_SCHEMA;
9096 					n->object = (Node *) makeString($3);
9097 					n->newowner = $6;
9098 					$$ = (Node *)n;
9099 				}
9100 			| ALTER TYPE_P any_name OWNER TO RoleSpec
9101 				{
9102 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9103 					n->objectType = OBJECT_TYPE;
9104 					n->object = (Node *) $3;
9105 					n->newowner = $6;
9106 					$$ = (Node *)n;
9107 				}
9108 			| ALTER TABLESPACE name OWNER TO RoleSpec
9109 				{
9110 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9111 					n->objectType = OBJECT_TABLESPACE;
9112 					n->object = (Node *) makeString($3);
9113 					n->newowner = $6;
9114 					$$ = (Node *)n;
9115 				}
9116 			| ALTER STATISTICS any_name OWNER TO RoleSpec
9117 				{
9118 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9119 					n->objectType = OBJECT_STATISTIC_EXT;
9120 					n->object = (Node *) $3;
9121 					n->newowner = $6;
9122 					$$ = (Node *)n;
9123 				}
9124 			| ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleSpec
9125 				{
9126 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9127 					n->objectType = OBJECT_TSDICTIONARY;
9128 					n->object = (Node *) $5;
9129 					n->newowner = $8;
9130 					$$ = (Node *)n;
9131 				}
9132 			| ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleSpec
9133 				{
9134 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9135 					n->objectType = OBJECT_TSCONFIGURATION;
9136 					n->object = (Node *) $5;
9137 					n->newowner = $8;
9138 					$$ = (Node *)n;
9139 				}
9140 			| ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleSpec
9141 				{
9142 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9143 					n->objectType = OBJECT_FDW;
9144 					n->object = (Node *) makeString($5);
9145 					n->newowner = $8;
9146 					$$ = (Node *)n;
9147 				}
9148 			| ALTER SERVER name OWNER TO RoleSpec
9149 				{
9150 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9151 					n->objectType = OBJECT_FOREIGN_SERVER;
9152 					n->object = (Node *) makeString($3);
9153 					n->newowner = $6;
9154 					$$ = (Node *)n;
9155 				}
9156 			| ALTER EVENT TRIGGER name OWNER TO RoleSpec
9157 				{
9158 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9159 					n->objectType = OBJECT_EVENT_TRIGGER;
9160 					n->object = (Node *) makeString($4);
9161 					n->newowner = $7;
9162 					$$ = (Node *)n;
9163 				}
9164 			| ALTER PUBLICATION name OWNER TO RoleSpec
9165 				{
9166 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9167 					n->objectType = OBJECT_PUBLICATION;
9168 					n->object = (Node *) makeString($3);
9169 					n->newowner = $6;
9170 					$$ = (Node *)n;
9171 				}
9172 			| ALTER SUBSCRIPTION name OWNER TO RoleSpec
9173 				{
9174 					AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9175 					n->objectType = OBJECT_SUBSCRIPTION;
9176 					n->object = (Node *) makeString($3);
9177 					n->newowner = $6;
9178 					$$ = (Node *)n;
9179 				}
9180 		;
9181 
9182 
9183 /*****************************************************************************
9184  *
9185  * CREATE PUBLICATION name [ FOR TABLE ] [ WITH options ]
9186  *
9187  *****************************************************************************/
9188 
9189 CreatePublicationStmt:
9190 			CREATE PUBLICATION name opt_publication_for_tables opt_definition
9191 				{
9192 					CreatePublicationStmt *n = makeNode(CreatePublicationStmt);
9193 					n->pubname = $3;
9194 					n->options = $5;
9195 					if ($4 != NULL)
9196 					{
9197 						/* FOR TABLE */
9198 						if (IsA($4, List))
9199 							n->tables = (List *)$4;
9200 						/* FOR ALL TABLES */
9201 						else
9202 							n->for_all_tables = TRUE;
9203 					}
9204 					$$ = (Node *)n;
9205 				}
9206 		;
9207 
9208 opt_publication_for_tables:
9209 			publication_for_tables					{ $$ = $1; }
9210 			| /* EMPTY */							{ $$ = NULL; }
9211 		;
9212 
9213 publication_for_tables:
9214 			FOR TABLE relation_expr_list
9215 				{
9216 					$$ = (Node *) $3;
9217 				}
9218 			| FOR ALL TABLES
9219 				{
9220 					$$ = (Node *) makeInteger(TRUE);
9221 				}
9222 		;
9223 
9224 
9225 /*****************************************************************************
9226  *
9227  * ALTER PUBLICATION name SET ( options )
9228  *
9229  * ALTER PUBLICATION name ADD TABLE table [, table2]
9230  *
9231  * ALTER PUBLICATION name DROP TABLE table [, table2]
9232  *
9233  * ALTER PUBLICATION name SET TABLE table [, table2]
9234  *
9235  *****************************************************************************/
9236 
9237 AlterPublicationStmt:
9238 			ALTER PUBLICATION name SET definition
9239 				{
9240 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9241 					n->pubname = $3;
9242 					n->options = $5;
9243 					$$ = (Node *)n;
9244 				}
9245 			| ALTER PUBLICATION name ADD_P TABLE relation_expr_list
9246 				{
9247 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9248 					n->pubname = $3;
9249 					n->tables = $6;
9250 					n->tableAction = DEFELEM_ADD;
9251 					$$ = (Node *)n;
9252 				}
9253 			| ALTER PUBLICATION name SET TABLE relation_expr_list
9254 				{
9255 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9256 					n->pubname = $3;
9257 					n->tables = $6;
9258 					n->tableAction = DEFELEM_SET;
9259 					$$ = (Node *)n;
9260 				}
9261 			| ALTER PUBLICATION name DROP TABLE relation_expr_list
9262 				{
9263 					AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9264 					n->pubname = $3;
9265 					n->tables = $6;
9266 					n->tableAction = DEFELEM_DROP;
9267 					$$ = (Node *)n;
9268 				}
9269 		;
9270 
9271 /*****************************************************************************
9272  *
9273  * CREATE SUBSCRIPTION name ...
9274  *
9275  *****************************************************************************/
9276 
9277 CreateSubscriptionStmt:
9278 			CREATE SUBSCRIPTION name CONNECTION Sconst PUBLICATION publication_name_list opt_definition
9279 				{
9280 					CreateSubscriptionStmt *n =
9281 						makeNode(CreateSubscriptionStmt);
9282 					n->subname = $3;
9283 					n->conninfo = $5;
9284 					n->publication = $7;
9285 					n->options = $8;
9286 					$$ = (Node *)n;
9287 				}
9288 		;
9289 
9290 publication_name_list:
9291 			publication_name_item
9292 				{
9293 					$$ = list_make1($1);
9294 				}
9295 			| publication_name_list ',' publication_name_item
9296 				{
9297 					$$ = lappend($1, $3);
9298 				}
9299 		;
9300 
9301 publication_name_item:
9302 			ColLabel			{ $$ = makeString($1); };
9303 
9304 /*****************************************************************************
9305  *
9306  * ALTER SUBSCRIPTION name ...
9307  *
9308  *****************************************************************************/
9309 
9310 AlterSubscriptionStmt:
9311 			ALTER SUBSCRIPTION name SET definition
9312 				{
9313 					AlterSubscriptionStmt *n =
9314 						makeNode(AlterSubscriptionStmt);
9315 					n->kind = ALTER_SUBSCRIPTION_OPTIONS;
9316 					n->subname = $3;
9317 					n->options = $5;
9318 					$$ = (Node *)n;
9319 				}
9320 			| ALTER SUBSCRIPTION name CONNECTION Sconst
9321 				{
9322 					AlterSubscriptionStmt *n =
9323 						makeNode(AlterSubscriptionStmt);
9324 					n->kind = ALTER_SUBSCRIPTION_CONNECTION;
9325 					n->subname = $3;
9326 					n->conninfo = $5;
9327 					$$ = (Node *)n;
9328 				}
9329 			| ALTER SUBSCRIPTION name REFRESH PUBLICATION opt_definition
9330 				{
9331 					AlterSubscriptionStmt *n =
9332 						makeNode(AlterSubscriptionStmt);
9333 					n->kind = ALTER_SUBSCRIPTION_REFRESH;
9334 					n->subname = $3;
9335 					n->options = $6;
9336 					$$ = (Node *)n;
9337 				}
9338 			| ALTER SUBSCRIPTION name SET PUBLICATION publication_name_list opt_definition
9339 				{
9340 					AlterSubscriptionStmt *n =
9341 						makeNode(AlterSubscriptionStmt);
9342 					n->kind = ALTER_SUBSCRIPTION_PUBLICATION;
9343 					n->subname = $3;
9344 					n->publication = $6;
9345 					n->options = $7;
9346 					$$ = (Node *)n;
9347 				}
9348 			| ALTER SUBSCRIPTION name ENABLE_P
9349 				{
9350 					AlterSubscriptionStmt *n =
9351 						makeNode(AlterSubscriptionStmt);
9352 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9353 					n->subname = $3;
9354 					n->options = list_make1(makeDefElem("enabled",
9355 											(Node *)makeInteger(TRUE), @1));
9356 					$$ = (Node *)n;
9357 				}
9358 			| ALTER SUBSCRIPTION name DISABLE_P
9359 				{
9360 					AlterSubscriptionStmt *n =
9361 						makeNode(AlterSubscriptionStmt);
9362 					n->kind = ALTER_SUBSCRIPTION_ENABLED;
9363 					n->subname = $3;
9364 					n->options = list_make1(makeDefElem("enabled",
9365 											(Node *)makeInteger(FALSE), @1));
9366 					$$ = (Node *)n;
9367 				}
9368 		;
9369 
9370 /*****************************************************************************
9371  *
9372  * DROP SUBSCRIPTION [ IF EXISTS ] name
9373  *
9374  *****************************************************************************/
9375 
9376 DropSubscriptionStmt: DROP SUBSCRIPTION name opt_drop_behavior
9377 				{
9378 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9379 					n->subname = $3;
9380 					n->missing_ok = false;
9381 					n->behavior = $4;
9382 					$$ = (Node *) n;
9383 				}
9384 				|  DROP SUBSCRIPTION IF_P EXISTS name opt_drop_behavior
9385 				{
9386 					DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9387 					n->subname = $5;
9388 					n->missing_ok = true;
9389 					n->behavior = $6;
9390 					$$ = (Node *) n;
9391 				}
9392 		;
9393 
9394 /*****************************************************************************
9395  *
9396  *		QUERY:	Define Rewrite Rule
9397  *
9398  *****************************************************************************/
9399 
9400 RuleStmt:	CREATE opt_or_replace RULE name AS
9401 			ON event TO qualified_name where_clause
9402 			DO opt_instead RuleActionList
9403 				{
9404 					RuleStmt *n = makeNode(RuleStmt);
9405 					n->replace = $2;
9406 					n->relation = $9;
9407 					n->rulename = $4;
9408 					n->whereClause = $10;
9409 					n->event = $7;
9410 					n->instead = $12;
9411 					n->actions = $13;
9412 					$$ = (Node *)n;
9413 				}
9414 		;
9415 
9416 RuleActionList:
9417 			NOTHING									{ $$ = NIL; }
9418 			| RuleActionStmt						{ $$ = list_make1($1); }
9419 			| '(' RuleActionMulti ')'				{ $$ = $2; }
9420 		;
9421 
9422 /* the thrashing around here is to discard "empty" statements... */
9423 RuleActionMulti:
9424 			RuleActionMulti ';' RuleActionStmtOrEmpty
9425 				{ if ($3 != NULL)
9426 					$$ = lappend($1, $3);
9427 				  else
9428 					$$ = $1;
9429 				}
9430 			| RuleActionStmtOrEmpty
9431 				{ if ($1 != NULL)
9432 					$$ = list_make1($1);
9433 				  else
9434 					$$ = NIL;
9435 				}
9436 		;
9437 
9438 RuleActionStmt:
9439 			SelectStmt
9440 			| InsertStmt
9441 			| UpdateStmt
9442 			| DeleteStmt
9443 			| NotifyStmt
9444 		;
9445 
9446 RuleActionStmtOrEmpty:
9447 			RuleActionStmt							{ $$ = $1; }
9448 			|	/*EMPTY*/							{ $$ = NULL; }
9449 		;
9450 
9451 event:		SELECT									{ $$ = CMD_SELECT; }
9452 			| UPDATE								{ $$ = CMD_UPDATE; }
9453 			| DELETE_P								{ $$ = CMD_DELETE; }
9454 			| INSERT								{ $$ = CMD_INSERT; }
9455 		 ;
9456 
9457 opt_instead:
9458 			INSTEAD									{ $$ = TRUE; }
9459 			| ALSO									{ $$ = FALSE; }
9460 			| /*EMPTY*/								{ $$ = FALSE; }
9461 		;
9462 
9463 
9464 /*****************************************************************************
9465  *
9466  *		QUERY:
9467  *				NOTIFY <identifier> can appear both in rule bodies and
9468  *				as a query-level command
9469  *
9470  *****************************************************************************/
9471 
9472 NotifyStmt: NOTIFY ColId notify_payload
9473 				{
9474 					NotifyStmt *n = makeNode(NotifyStmt);
9475 					n->conditionname = $2;
9476 					n->payload = $3;
9477 					$$ = (Node *)n;
9478 				}
9479 		;
9480 
9481 notify_payload:
9482 			',' Sconst							{ $$ = $2; }
9483 			| /*EMPTY*/							{ $$ = NULL; }
9484 		;
9485 
9486 ListenStmt: LISTEN ColId
9487 				{
9488 					ListenStmt *n = makeNode(ListenStmt);
9489 					n->conditionname = $2;
9490 					$$ = (Node *)n;
9491 				}
9492 		;
9493 
9494 UnlistenStmt:
9495 			UNLISTEN ColId
9496 				{
9497 					UnlistenStmt *n = makeNode(UnlistenStmt);
9498 					n->conditionname = $2;
9499 					$$ = (Node *)n;
9500 				}
9501 			| UNLISTEN '*'
9502 				{
9503 					UnlistenStmt *n = makeNode(UnlistenStmt);
9504 					n->conditionname = NULL;
9505 					$$ = (Node *)n;
9506 				}
9507 		;
9508 
9509 
9510 /*****************************************************************************
9511  *
9512  *		Transactions:
9513  *
9514  *		BEGIN / COMMIT / ROLLBACK
9515  *		(also older versions END / ABORT)
9516  *
9517  *****************************************************************************/
9518 
9519 TransactionStmt:
9520 			ABORT_P opt_transaction
9521 				{
9522 					TransactionStmt *n = makeNode(TransactionStmt);
9523 					n->kind = TRANS_STMT_ROLLBACK;
9524 					n->options = NIL;
9525 					$$ = (Node *)n;
9526 				}
9527 			| BEGIN_P opt_transaction transaction_mode_list_or_empty
9528 				{
9529 					TransactionStmt *n = makeNode(TransactionStmt);
9530 					n->kind = TRANS_STMT_BEGIN;
9531 					n->options = $3;
9532 					$$ = (Node *)n;
9533 				}
9534 			| START TRANSACTION transaction_mode_list_or_empty
9535 				{
9536 					TransactionStmt *n = makeNode(TransactionStmt);
9537 					n->kind = TRANS_STMT_START;
9538 					n->options = $3;
9539 					$$ = (Node *)n;
9540 				}
9541 			| COMMIT opt_transaction
9542 				{
9543 					TransactionStmt *n = makeNode(TransactionStmt);
9544 					n->kind = TRANS_STMT_COMMIT;
9545 					n->options = NIL;
9546 					$$ = (Node *)n;
9547 				}
9548 			| END_P opt_transaction
9549 				{
9550 					TransactionStmt *n = makeNode(TransactionStmt);
9551 					n->kind = TRANS_STMT_COMMIT;
9552 					n->options = NIL;
9553 					$$ = (Node *)n;
9554 				}
9555 			| ROLLBACK opt_transaction
9556 				{
9557 					TransactionStmt *n = makeNode(TransactionStmt);
9558 					n->kind = TRANS_STMT_ROLLBACK;
9559 					n->options = NIL;
9560 					$$ = (Node *)n;
9561 				}
9562 			| SAVEPOINT ColId
9563 				{
9564 					TransactionStmt *n = makeNode(TransactionStmt);
9565 					n->kind = TRANS_STMT_SAVEPOINT;
9566 					n->options = list_make1(makeDefElem("savepoint_name",
9567 														(Node *)makeString($2), @1));
9568 					$$ = (Node *)n;
9569 				}
9570 			| RELEASE SAVEPOINT ColId
9571 				{
9572 					TransactionStmt *n = makeNode(TransactionStmt);
9573 					n->kind = TRANS_STMT_RELEASE;
9574 					n->options = list_make1(makeDefElem("savepoint_name",
9575 														(Node *)makeString($3), @1));
9576 					$$ = (Node *)n;
9577 				}
9578 			| RELEASE ColId
9579 				{
9580 					TransactionStmt *n = makeNode(TransactionStmt);
9581 					n->kind = TRANS_STMT_RELEASE;
9582 					n->options = list_make1(makeDefElem("savepoint_name",
9583 														(Node *)makeString($2), @1));
9584 					$$ = (Node *)n;
9585 				}
9586 			| ROLLBACK opt_transaction TO SAVEPOINT ColId
9587 				{
9588 					TransactionStmt *n = makeNode(TransactionStmt);
9589 					n->kind = TRANS_STMT_ROLLBACK_TO;
9590 					n->options = list_make1(makeDefElem("savepoint_name",
9591 														(Node *)makeString($5), @1));
9592 					$$ = (Node *)n;
9593 				}
9594 			| ROLLBACK opt_transaction TO ColId
9595 				{
9596 					TransactionStmt *n = makeNode(TransactionStmt);
9597 					n->kind = TRANS_STMT_ROLLBACK_TO;
9598 					n->options = list_make1(makeDefElem("savepoint_name",
9599 														(Node *)makeString($4), @1));
9600 					$$ = (Node *)n;
9601 				}
9602 			| PREPARE TRANSACTION Sconst
9603 				{
9604 					TransactionStmt *n = makeNode(TransactionStmt);
9605 					n->kind = TRANS_STMT_PREPARE;
9606 					n->gid = $3;
9607 					$$ = (Node *)n;
9608 				}
9609 			| COMMIT PREPARED Sconst
9610 				{
9611 					TransactionStmt *n = makeNode(TransactionStmt);
9612 					n->kind = TRANS_STMT_COMMIT_PREPARED;
9613 					n->gid = $3;
9614 					$$ = (Node *)n;
9615 				}
9616 			| ROLLBACK PREPARED Sconst
9617 				{
9618 					TransactionStmt *n = makeNode(TransactionStmt);
9619 					n->kind = TRANS_STMT_ROLLBACK_PREPARED;
9620 					n->gid = $3;
9621 					$$ = (Node *)n;
9622 				}
9623 		;
9624 
9625 opt_transaction:	WORK							{}
9626 			| TRANSACTION							{}
9627 			| /*EMPTY*/								{}
9628 		;
9629 
9630 transaction_mode_item:
9631 			ISOLATION LEVEL iso_level
9632 					{ $$ = makeDefElem("transaction_isolation",
9633 									   makeStringConst($3, @3), @1); }
9634 			| READ ONLY
9635 					{ $$ = makeDefElem("transaction_read_only",
9636 									   makeIntConst(TRUE, @1), @1); }
9637 			| READ WRITE
9638 					{ $$ = makeDefElem("transaction_read_only",
9639 									   makeIntConst(FALSE, @1), @1); }
9640 			| DEFERRABLE
9641 					{ $$ = makeDefElem("transaction_deferrable",
9642 									   makeIntConst(TRUE, @1), @1); }
9643 			| NOT DEFERRABLE
9644 					{ $$ = makeDefElem("transaction_deferrable",
9645 									   makeIntConst(FALSE, @1), @1); }
9646 		;
9647 
9648 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
9649 transaction_mode_list:
9650 			transaction_mode_item
9651 					{ $$ = list_make1($1); }
9652 			| transaction_mode_list ',' transaction_mode_item
9653 					{ $$ = lappend($1, $3); }
9654 			| transaction_mode_list transaction_mode_item
9655 					{ $$ = lappend($1, $2); }
9656 		;
9657 
9658 transaction_mode_list_or_empty:
9659 			transaction_mode_list
9660 			| /* EMPTY */
9661 					{ $$ = NIL; }
9662 		;
9663 
9664 
9665 /*****************************************************************************
9666  *
9667  *	QUERY:
9668  *		CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
9669  *			AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
9670  *
9671  *****************************************************************************/
9672 
9673 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list opt_reloptions
9674 				AS SelectStmt opt_check_option
9675 				{
9676 					ViewStmt *n = makeNode(ViewStmt);
9677 					n->view = $4;
9678 					n->view->relpersistence = $2;
9679 					n->aliases = $5;
9680 					n->query = $8;
9681 					n->replace = false;
9682 					n->options = $6;
9683 					n->withCheckOption = $9;
9684 					$$ = (Node *) n;
9685 				}
9686 		| CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list opt_reloptions
9687 				AS SelectStmt opt_check_option
9688 				{
9689 					ViewStmt *n = makeNode(ViewStmt);
9690 					n->view = $6;
9691 					n->view->relpersistence = $4;
9692 					n->aliases = $7;
9693 					n->query = $10;
9694 					n->replace = true;
9695 					n->options = $8;
9696 					n->withCheckOption = $11;
9697 					$$ = (Node *) n;
9698 				}
9699 		| CREATE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
9700 				AS SelectStmt opt_check_option
9701 				{
9702 					ViewStmt *n = makeNode(ViewStmt);
9703 					n->view = $5;
9704 					n->view->relpersistence = $2;
9705 					n->aliases = $7;
9706 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $11);
9707 					n->replace = false;
9708 					n->options = $9;
9709 					n->withCheckOption = $12;
9710 					if (n->withCheckOption != NO_CHECK_OPTION)
9711 						ereport(ERROR,
9712 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9713 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
9714 								 parser_errposition(@12)));
9715 					$$ = (Node *) n;
9716 				}
9717 		| CREATE OR REPLACE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
9718 				AS SelectStmt opt_check_option
9719 				{
9720 					ViewStmt *n = makeNode(ViewStmt);
9721 					n->view = $7;
9722 					n->view->relpersistence = $4;
9723 					n->aliases = $9;
9724 					n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $13);
9725 					n->replace = true;
9726 					n->options = $11;
9727 					n->withCheckOption = $14;
9728 					if (n->withCheckOption != NO_CHECK_OPTION)
9729 						ereport(ERROR,
9730 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9731 								 errmsg("WITH CHECK OPTION not supported on recursive views"),
9732 								 parser_errposition(@14)));
9733 					$$ = (Node *) n;
9734 				}
9735 		;
9736 
9737 opt_check_option:
9738 		WITH CHECK OPTION				{ $$ = CASCADED_CHECK_OPTION; }
9739 		| WITH CASCADED CHECK OPTION	{ $$ = CASCADED_CHECK_OPTION; }
9740 		| WITH LOCAL CHECK OPTION		{ $$ = LOCAL_CHECK_OPTION; }
9741 		| /* EMPTY */					{ $$ = NO_CHECK_OPTION; }
9742 		;
9743 
9744 /*****************************************************************************
9745  *
9746  *		QUERY:
9747  *				LOAD "filename"
9748  *
9749  *****************************************************************************/
9750 
9751 LoadStmt:	LOAD file_name
9752 				{
9753 					LoadStmt *n = makeNode(LoadStmt);
9754 					n->filename = $2;
9755 					$$ = (Node *)n;
9756 				}
9757 		;
9758 
9759 
9760 /*****************************************************************************
9761  *
9762  *		CREATE DATABASE
9763  *
9764  *****************************************************************************/
9765 
9766 CreatedbStmt:
9767 			CREATE DATABASE database_name opt_with createdb_opt_list
9768 				{
9769 					CreatedbStmt *n = makeNode(CreatedbStmt);
9770 					n->dbname = $3;
9771 					n->options = $5;
9772 					$$ = (Node *)n;
9773 				}
9774 		;
9775 
9776 createdb_opt_list:
9777 			createdb_opt_items						{ $$ = $1; }
9778 			| /* EMPTY */							{ $$ = NIL; }
9779 		;
9780 
9781 createdb_opt_items:
9782 			createdb_opt_item						{ $$ = list_make1($1); }
9783 			| createdb_opt_items createdb_opt_item	{ $$ = lappend($1, $2); }
9784 		;
9785 
9786 createdb_opt_item:
9787 			createdb_opt_name opt_equal SignedIconst
9788 				{
9789 					$$ = makeDefElem($1, (Node *)makeInteger($3), @1);
9790 				}
9791 			| createdb_opt_name opt_equal opt_boolean_or_string
9792 				{
9793 					$$ = makeDefElem($1, (Node *)makeString($3), @1);
9794 				}
9795 			| createdb_opt_name opt_equal DEFAULT
9796 				{
9797 					$$ = makeDefElem($1, NULL, @1);
9798 				}
9799 		;
9800 
9801 /*
9802  * Ideally we'd use ColId here, but that causes shift/reduce conflicts against
9803  * the ALTER DATABASE SET/RESET syntaxes.  Instead call out specific keywords
9804  * we need, and allow IDENT so that database option names don't have to be
9805  * parser keywords unless they are already keywords for other reasons.
9806  *
9807  * XXX this coding technique is fragile since if someone makes a formerly
9808  * non-keyword option name into a keyword and forgets to add it here, the
9809  * option will silently break.  Best defense is to provide a regression test
9810  * exercising every such option, at least at the syntax level.
9811  */
9812 createdb_opt_name:
9813 			IDENT							{ $$ = $1; }
9814 			| CONNECTION LIMIT				{ $$ = pstrdup("connection_limit"); }
9815 			| ENCODING						{ $$ = pstrdup($1); }
9816 			| LOCATION						{ $$ = pstrdup($1); }
9817 			| OWNER							{ $$ = pstrdup($1); }
9818 			| TABLESPACE					{ $$ = pstrdup($1); }
9819 			| TEMPLATE						{ $$ = pstrdup($1); }
9820 		;
9821 
9822 /*
9823  *	Though the equals sign doesn't match other WITH options, pg_dump uses
9824  *	equals for backward compatibility, and it doesn't seem worth removing it.
9825  */
9826 opt_equal:	'='										{}
9827 			| /*EMPTY*/								{}
9828 		;
9829 
9830 
9831 /*****************************************************************************
9832  *
9833  *		ALTER DATABASE
9834  *
9835  *****************************************************************************/
9836 
9837 AlterDatabaseStmt:
9838 			ALTER DATABASE database_name WITH createdb_opt_list
9839 				 {
9840 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9841 					n->dbname = $3;
9842 					n->options = $5;
9843 					$$ = (Node *)n;
9844 				 }
9845 			| ALTER DATABASE database_name createdb_opt_list
9846 				 {
9847 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9848 					n->dbname = $3;
9849 					n->options = $4;
9850 					$$ = (Node *)n;
9851 				 }
9852 			| ALTER DATABASE database_name SET TABLESPACE name
9853 				 {
9854 					AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
9855 					n->dbname = $3;
9856 					n->options = list_make1(makeDefElem("tablespace",
9857 														(Node *)makeString($6), @6));
9858 					$$ = (Node *)n;
9859 				 }
9860 		;
9861 
9862 AlterDatabaseSetStmt:
9863 			ALTER DATABASE database_name SetResetClause
9864 				{
9865 					AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
9866 					n->dbname = $3;
9867 					n->setstmt = $4;
9868 					$$ = (Node *)n;
9869 				}
9870 		;
9871 
9872 
9873 /*****************************************************************************
9874  *
9875  *		DROP DATABASE [ IF EXISTS ]
9876  *
9877  * This is implicitly CASCADE, no need for drop behavior
9878  *****************************************************************************/
9879 
9880 DropdbStmt: DROP DATABASE database_name
9881 				{
9882 					DropdbStmt *n = makeNode(DropdbStmt);
9883 					n->dbname = $3;
9884 					n->missing_ok = FALSE;
9885 					$$ = (Node *)n;
9886 				}
9887 			| DROP DATABASE IF_P EXISTS database_name
9888 				{
9889 					DropdbStmt *n = makeNode(DropdbStmt);
9890 					n->dbname = $5;
9891 					n->missing_ok = TRUE;
9892 					$$ = (Node *)n;
9893 				}
9894 		;
9895 
9896 
9897 /*****************************************************************************
9898  *
9899  *		ALTER COLLATION
9900  *
9901  *****************************************************************************/
9902 
9903 AlterCollationStmt: ALTER COLLATION any_name REFRESH VERSION_P
9904 				{
9905 					AlterCollationStmt *n = makeNode(AlterCollationStmt);
9906 					n->collname = $3;
9907 					$$ = (Node *)n;
9908 				}
9909 		;
9910 
9911 
9912 /*****************************************************************************
9913  *
9914  *		ALTER SYSTEM
9915  *
9916  * This is used to change configuration parameters persistently.
9917  *****************************************************************************/
9918 
9919 AlterSystemStmt:
9920 			ALTER SYSTEM_P SET generic_set
9921 				{
9922 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
9923 					n->setstmt = $4;
9924 					$$ = (Node *)n;
9925 				}
9926 			| ALTER SYSTEM_P RESET generic_reset
9927 				{
9928 					AlterSystemStmt *n = makeNode(AlterSystemStmt);
9929 					n->setstmt = $4;
9930 					$$ = (Node *)n;
9931 				}
9932 		;
9933 
9934 
9935 /*****************************************************************************
9936  *
9937  * Manipulate a domain
9938  *
9939  *****************************************************************************/
9940 
9941 CreateDomainStmt:
9942 			CREATE DOMAIN_P any_name opt_as Typename ColQualList
9943 				{
9944 					CreateDomainStmt *n = makeNode(CreateDomainStmt);
9945 					n->domainname = $3;
9946 					n->typeName = $5;
9947 					SplitColQualList($6, &n->constraints, &n->collClause,
9948 									 yyscanner);
9949 					$$ = (Node *)n;
9950 				}
9951 		;
9952 
9953 AlterDomainStmt:
9954 			/* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
9955 			ALTER DOMAIN_P any_name alter_column_default
9956 				{
9957 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9958 					n->subtype = 'T';
9959 					n->typeName = $3;
9960 					n->def = $4;
9961 					$$ = (Node *)n;
9962 				}
9963 			/* ALTER DOMAIN <domain> DROP NOT NULL */
9964 			| ALTER DOMAIN_P any_name DROP NOT NULL_P
9965 				{
9966 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9967 					n->subtype = 'N';
9968 					n->typeName = $3;
9969 					$$ = (Node *)n;
9970 				}
9971 			/* ALTER DOMAIN <domain> SET NOT NULL */
9972 			| ALTER DOMAIN_P any_name SET NOT NULL_P
9973 				{
9974 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9975 					n->subtype = 'O';
9976 					n->typeName = $3;
9977 					$$ = (Node *)n;
9978 				}
9979 			/* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
9980 			| ALTER DOMAIN_P any_name ADD_P TableConstraint
9981 				{
9982 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9983 					n->subtype = 'C';
9984 					n->typeName = $3;
9985 					n->def = $5;
9986 					$$ = (Node *)n;
9987 				}
9988 			/* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
9989 			| ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
9990 				{
9991 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
9992 					n->subtype = 'X';
9993 					n->typeName = $3;
9994 					n->name = $6;
9995 					n->behavior = $7;
9996 					n->missing_ok = false;
9997 					$$ = (Node *)n;
9998 				}
9999 			/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
10000 			| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
10001 				{
10002 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10003 					n->subtype = 'X';
10004 					n->typeName = $3;
10005 					n->name = $8;
10006 					n->behavior = $9;
10007 					n->missing_ok = true;
10008 					$$ = (Node *)n;
10009 				}
10010 			/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
10011 			| ALTER DOMAIN_P any_name VALIDATE CONSTRAINT name
10012 				{
10013 					AlterDomainStmt *n = makeNode(AlterDomainStmt);
10014 					n->subtype = 'V';
10015 					n->typeName = $3;
10016 					n->name = $6;
10017 					$$ = (Node *)n;
10018 				}
10019 			;
10020 
10021 opt_as:		AS										{}
10022 			| /* EMPTY */							{}
10023 		;
10024 
10025 
10026 /*****************************************************************************
10027  *
10028  * Manipulate a text search dictionary or configuration
10029  *
10030  *****************************************************************************/
10031 
10032 AlterTSDictionaryStmt:
10033 			ALTER TEXT_P SEARCH DICTIONARY any_name definition
10034 				{
10035 					AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
10036 					n->dictname = $5;
10037 					n->options = $6;
10038 					$$ = (Node *)n;
10039 				}
10040 		;
10041 
10042 AlterTSConfigurationStmt:
10043 			ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list any_with any_name_list
10044 				{
10045 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10046 					n->kind = ALTER_TSCONFIG_ADD_MAPPING;
10047 					n->cfgname = $5;
10048 					n->tokentype = $9;
10049 					n->dicts = $11;
10050 					n->override = false;
10051 					n->replace = false;
10052 					$$ = (Node*)n;
10053 				}
10054 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list any_with any_name_list
10055 				{
10056 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10057 					n->kind = ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN;
10058 					n->cfgname = $5;
10059 					n->tokentype = $9;
10060 					n->dicts = $11;
10061 					n->override = true;
10062 					n->replace = false;
10063 					$$ = (Node*)n;
10064 				}
10065 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name any_with any_name
10066 				{
10067 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10068 					n->kind = ALTER_TSCONFIG_REPLACE_DICT;
10069 					n->cfgname = $5;
10070 					n->tokentype = NIL;
10071 					n->dicts = list_make2($9,$11);
10072 					n->override = false;
10073 					n->replace = true;
10074 					$$ = (Node*)n;
10075 				}
10076 			| ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name any_with any_name
10077 				{
10078 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10079 					n->kind = ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN;
10080 					n->cfgname = $5;
10081 					n->tokentype = $9;
10082 					n->dicts = list_make2($11,$13);
10083 					n->override = false;
10084 					n->replace = true;
10085 					$$ = (Node*)n;
10086 				}
10087 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
10088 				{
10089 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10090 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10091 					n->cfgname = $5;
10092 					n->tokentype = $9;
10093 					n->missing_ok = false;
10094 					$$ = (Node*)n;
10095 				}
10096 			| ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
10097 				{
10098 					AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10099 					n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10100 					n->cfgname = $5;
10101 					n->tokentype = $11;
10102 					n->missing_ok = true;
10103 					$$ = (Node*)n;
10104 				}
10105 		;
10106 
10107 /* Use this if TIME or ORDINALITY after WITH should be taken as an identifier */
10108 any_with:	WITH									{}
10109 			| WITH_LA								{}
10110 		;
10111 
10112 
10113 /*****************************************************************************
10114  *
10115  * Manipulate a conversion
10116  *
10117  *		CREATE [DEFAULT] CONVERSION <conversion_name>
10118  *		FOR <encoding_name> TO <encoding_name> FROM <func_name>
10119  *
10120  *****************************************************************************/
10121 
10122 CreateConversionStmt:
10123 			CREATE opt_default CONVERSION_P any_name FOR Sconst
10124 			TO Sconst FROM any_name
10125 			{
10126 				CreateConversionStmt *n = makeNode(CreateConversionStmt);
10127 				n->conversion_name = $4;
10128 				n->for_encoding_name = $6;
10129 				n->to_encoding_name = $8;
10130 				n->func_name = $10;
10131 				n->def = $2;
10132 				$$ = (Node *)n;
10133 			}
10134 		;
10135 
10136 /*****************************************************************************
10137  *
10138  *		QUERY:
10139  *				CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
10140  *				CLUSTER [VERBOSE]
10141  *				CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
10142  *
10143  *****************************************************************************/
10144 
10145 ClusterStmt:
10146 			CLUSTER opt_verbose qualified_name cluster_index_specification
10147 				{
10148 					ClusterStmt *n = makeNode(ClusterStmt);
10149 					n->relation = $3;
10150 					n->indexname = $4;
10151 					n->verbose = $2;
10152 					$$ = (Node*)n;
10153 				}
10154 			| CLUSTER opt_verbose
10155 				{
10156 					ClusterStmt *n = makeNode(ClusterStmt);
10157 					n->relation = NULL;
10158 					n->indexname = NULL;
10159 					n->verbose = $2;
10160 					$$ = (Node*)n;
10161 				}
10162 			/* kept for pre-8.3 compatibility */
10163 			| CLUSTER opt_verbose index_name ON qualified_name
10164 				{
10165 					ClusterStmt *n = makeNode(ClusterStmt);
10166 					n->relation = $5;
10167 					n->indexname = $3;
10168 					n->verbose = $2;
10169 					$$ = (Node*)n;
10170 				}
10171 		;
10172 
10173 cluster_index_specification:
10174 			USING index_name		{ $$ = $2; }
10175 			| /*EMPTY*/				{ $$ = NULL; }
10176 		;
10177 
10178 
10179 /*****************************************************************************
10180  *
10181  *		QUERY:
10182  *				VACUUM
10183  *				ANALYZE
10184  *
10185  *****************************************************************************/
10186 
10187 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
10188 				{
10189 					VacuumStmt *n = makeNode(VacuumStmt);
10190 					n->options = VACOPT_VACUUM;
10191 					if ($2)
10192 						n->options |= VACOPT_FULL;
10193 					if ($3)
10194 						n->options |= VACOPT_FREEZE;
10195 					if ($4)
10196 						n->options |= VACOPT_VERBOSE;
10197 					n->relation = NULL;
10198 					n->va_cols = NIL;
10199 					$$ = (Node *)n;
10200 				}
10201 			| VACUUM opt_full opt_freeze opt_verbose qualified_name
10202 				{
10203 					VacuumStmt *n = makeNode(VacuumStmt);
10204 					n->options = VACOPT_VACUUM;
10205 					if ($2)
10206 						n->options |= VACOPT_FULL;
10207 					if ($3)
10208 						n->options |= VACOPT_FREEZE;
10209 					if ($4)
10210 						n->options |= VACOPT_VERBOSE;
10211 					n->relation = $5;
10212 					n->va_cols = NIL;
10213 					$$ = (Node *)n;
10214 				}
10215 			| VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
10216 				{
10217 					VacuumStmt *n = (VacuumStmt *) $5;
10218 					n->options |= VACOPT_VACUUM;
10219 					if ($2)
10220 						n->options |= VACOPT_FULL;
10221 					if ($3)
10222 						n->options |= VACOPT_FREEZE;
10223 					if ($4)
10224 						n->options |= VACOPT_VERBOSE;
10225 					$$ = (Node *)n;
10226 				}
10227 			| VACUUM '(' vacuum_option_list ')'
10228 				{
10229 					VacuumStmt *n = makeNode(VacuumStmt);
10230 					n->options = VACOPT_VACUUM | $3;
10231 					n->relation = NULL;
10232 					n->va_cols = NIL;
10233 					$$ = (Node *) n;
10234 				}
10235 			| VACUUM '(' vacuum_option_list ')' qualified_name opt_name_list
10236 				{
10237 					VacuumStmt *n = makeNode(VacuumStmt);
10238 					n->options = VACOPT_VACUUM | $3;
10239 					n->relation = $5;
10240 					n->va_cols = $6;
10241 					if (n->va_cols != NIL)	/* implies analyze */
10242 						n->options |= VACOPT_ANALYZE;
10243 					$$ = (Node *) n;
10244 				}
10245 		;
10246 
10247 vacuum_option_list:
10248 			vacuum_option_elem								{ $$ = $1; }
10249 			| vacuum_option_list ',' vacuum_option_elem		{ $$ = $1 | $3; }
10250 		;
10251 
10252 vacuum_option_elem:
10253 			analyze_keyword		{ $$ = VACOPT_ANALYZE; }
10254 			| VERBOSE			{ $$ = VACOPT_VERBOSE; }
10255 			| FREEZE			{ $$ = VACOPT_FREEZE; }
10256 			| FULL				{ $$ = VACOPT_FULL; }
10257 			| IDENT
10258 				{
10259 					if (strcmp($1, "disable_page_skipping") == 0)
10260 						$$ = VACOPT_DISABLE_PAGE_SKIPPING;
10261 					else
10262 						ereport(ERROR,
10263 								(errcode(ERRCODE_SYNTAX_ERROR),
10264 							 errmsg("unrecognized VACUUM option \"%s\"", $1),
10265 									 parser_errposition(@1)));
10266 				}
10267 		;
10268 
10269 AnalyzeStmt:
10270 			analyze_keyword opt_verbose
10271 				{
10272 					VacuumStmt *n = makeNode(VacuumStmt);
10273 					n->options = VACOPT_ANALYZE;
10274 					if ($2)
10275 						n->options |= VACOPT_VERBOSE;
10276 					n->relation = NULL;
10277 					n->va_cols = NIL;
10278 					$$ = (Node *)n;
10279 				}
10280 			| analyze_keyword opt_verbose qualified_name opt_name_list
10281 				{
10282 					VacuumStmt *n = makeNode(VacuumStmt);
10283 					n->options = VACOPT_ANALYZE;
10284 					if ($2)
10285 						n->options |= VACOPT_VERBOSE;
10286 					n->relation = $3;
10287 					n->va_cols = $4;
10288 					$$ = (Node *)n;
10289 				}
10290 		;
10291 
10292 analyze_keyword:
10293 			ANALYZE									{}
10294 			| ANALYSE /* British */					{}
10295 		;
10296 
10297 opt_verbose:
10298 			VERBOSE									{ $$ = TRUE; }
10299 			| /*EMPTY*/								{ $$ = FALSE; }
10300 		;
10301 
10302 opt_full:	FULL									{ $$ = TRUE; }
10303 			| /*EMPTY*/								{ $$ = FALSE; }
10304 		;
10305 
10306 opt_freeze: FREEZE									{ $$ = TRUE; }
10307 			| /*EMPTY*/								{ $$ = FALSE; }
10308 		;
10309 
10310 opt_name_list:
10311 			'(' name_list ')'						{ $$ = $2; }
10312 			| /*EMPTY*/								{ $$ = NIL; }
10313 		;
10314 
10315 
10316 /*****************************************************************************
10317  *
10318  *		QUERY:
10319  *				EXPLAIN [ANALYZE] [VERBOSE] query
10320  *				EXPLAIN ( options ) query
10321  *
10322  *****************************************************************************/
10323 
10324 ExplainStmt:
10325 		EXPLAIN ExplainableStmt
10326 				{
10327 					ExplainStmt *n = makeNode(ExplainStmt);
10328 					n->query = $2;
10329 					n->options = NIL;
10330 					$$ = (Node *) n;
10331 				}
10332 		| EXPLAIN analyze_keyword opt_verbose ExplainableStmt
10333 				{
10334 					ExplainStmt *n = makeNode(ExplainStmt);
10335 					n->query = $4;
10336 					n->options = list_make1(makeDefElem("analyze", NULL, @2));
10337 					if ($3)
10338 						n->options = lappend(n->options,
10339 											 makeDefElem("verbose", NULL, @3));
10340 					$$ = (Node *) n;
10341 				}
10342 		| EXPLAIN VERBOSE ExplainableStmt
10343 				{
10344 					ExplainStmt *n = makeNode(ExplainStmt);
10345 					n->query = $3;
10346 					n->options = list_make1(makeDefElem("verbose", NULL, @2));
10347 					$$ = (Node *) n;
10348 				}
10349 		| EXPLAIN '(' explain_option_list ')' ExplainableStmt
10350 				{
10351 					ExplainStmt *n = makeNode(ExplainStmt);
10352 					n->query = $5;
10353 					n->options = $3;
10354 					$$ = (Node *) n;
10355 				}
10356 		;
10357 
10358 ExplainableStmt:
10359 			SelectStmt
10360 			| InsertStmt
10361 			| UpdateStmt
10362 			| DeleteStmt
10363 			| DeclareCursorStmt
10364 			| CreateAsStmt
10365 			| CreateMatViewStmt
10366 			| RefreshMatViewStmt
10367 			| ExecuteStmt					/* by default all are $$=$1 */
10368 		;
10369 
10370 explain_option_list:
10371 			explain_option_elem
10372 				{
10373 					$$ = list_make1($1);
10374 				}
10375 			| explain_option_list ',' explain_option_elem
10376 				{
10377 					$$ = lappend($1, $3);
10378 				}
10379 		;
10380 
10381 explain_option_elem:
10382 			explain_option_name explain_option_arg
10383 				{
10384 					$$ = makeDefElem($1, $2, @1);
10385 				}
10386 		;
10387 
10388 explain_option_name:
10389 			NonReservedWord			{ $$ = $1; }
10390 			| analyze_keyword		{ $$ = "analyze"; }
10391 		;
10392 
10393 explain_option_arg:
10394 			opt_boolean_or_string	{ $$ = (Node *) makeString($1); }
10395 			| NumericOnly			{ $$ = (Node *) $1; }
10396 			| /* EMPTY */			{ $$ = NULL; }
10397 		;
10398 
10399 /*****************************************************************************
10400  *
10401  *		QUERY:
10402  *				PREPARE <plan_name> [(args, ...)] AS <query>
10403  *
10404  *****************************************************************************/
10405 
10406 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
10407 				{
10408 					PrepareStmt *n = makeNode(PrepareStmt);
10409 					n->name = $2;
10410 					n->argtypes = $3;
10411 					n->query = $5;
10412 					$$ = (Node *) n;
10413 				}
10414 		;
10415 
10416 prep_type_clause: '(' type_list ')'			{ $$ = $2; }
10417 				| /* EMPTY */				{ $$ = NIL; }
10418 		;
10419 
10420 PreparableStmt:
10421 			SelectStmt
10422 			| InsertStmt
10423 			| UpdateStmt
10424 			| DeleteStmt					/* by default all are $$=$1 */
10425 		;
10426 
10427 /*****************************************************************************
10428  *
10429  * EXECUTE <plan_name> [(params, ...)]
10430  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
10431  *
10432  *****************************************************************************/
10433 
10434 ExecuteStmt: EXECUTE name execute_param_clause
10435 				{
10436 					ExecuteStmt *n = makeNode(ExecuteStmt);
10437 					n->name = $2;
10438 					n->params = $3;
10439 					$$ = (Node *) n;
10440 				}
10441 			| CREATE OptTemp TABLE create_as_target AS
10442 				EXECUTE name execute_param_clause opt_with_data
10443 				{
10444 					CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10445 					ExecuteStmt *n = makeNode(ExecuteStmt);
10446 					n->name = $7;
10447 					n->params = $8;
10448 					ctas->query = (Node *) n;
10449 					ctas->into = $4;
10450 					ctas->relkind = OBJECT_TABLE;
10451 					ctas->is_select_into = false;
10452 					/* cram additional flags into the IntoClause */
10453 					$4->rel->relpersistence = $2;
10454 					$4->skipData = !($9);
10455 					$$ = (Node *) ctas;
10456 				}
10457 		;
10458 
10459 execute_param_clause: '(' expr_list ')'				{ $$ = $2; }
10460 					| /* EMPTY */					{ $$ = NIL; }
10461 					;
10462 
10463 /*****************************************************************************
10464  *
10465  *		QUERY:
10466  *				DEALLOCATE [PREPARE] <plan_name>
10467  *
10468  *****************************************************************************/
10469 
10470 DeallocateStmt: DEALLOCATE name
10471 					{
10472 						DeallocateStmt *n = makeNode(DeallocateStmt);
10473 						n->name = $2;
10474 						$$ = (Node *) n;
10475 					}
10476 				| DEALLOCATE PREPARE name
10477 					{
10478 						DeallocateStmt *n = makeNode(DeallocateStmt);
10479 						n->name = $3;
10480 						$$ = (Node *) n;
10481 					}
10482 				| DEALLOCATE ALL
10483 					{
10484 						DeallocateStmt *n = makeNode(DeallocateStmt);
10485 						n->name = NULL;
10486 						$$ = (Node *) n;
10487 					}
10488 				| DEALLOCATE PREPARE ALL
10489 					{
10490 						DeallocateStmt *n = makeNode(DeallocateStmt);
10491 						n->name = NULL;
10492 						$$ = (Node *) n;
10493 					}
10494 		;
10495 
10496 /*****************************************************************************
10497  *
10498  *		QUERY:
10499  *				INSERT STATEMENTS
10500  *
10501  *****************************************************************************/
10502 
10503 InsertStmt:
10504 			opt_with_clause INSERT INTO insert_target insert_rest
10505 			opt_on_conflict returning_clause
10506 				{
10507 					$5->relation = $4;
10508 					$5->onConflictClause = $6;
10509 					$5->returningList = $7;
10510 					$5->withClause = $1;
10511 					$$ = (Node *) $5;
10512 				}
10513 		;
10514 
10515 /*
10516  * Can't easily make AS optional here, because VALUES in insert_rest would
10517  * have a shift/reduce conflict with VALUES as an optional alias.  We could
10518  * easily allow unreserved_keywords as optional aliases, but that'd be an odd
10519  * divergence from other places.  So just require AS for now.
10520  */
10521 insert_target:
10522 			qualified_name
10523 				{
10524 					$$ = $1;
10525 				}
10526 			| qualified_name AS ColId
10527 				{
10528 					$1->alias = makeAlias($3, NIL);
10529 					$$ = $1;
10530 				}
10531 		;
10532 
10533 insert_rest:
10534 			SelectStmt
10535 				{
10536 					$$ = makeNode(InsertStmt);
10537 					$$->cols = NIL;
10538 					$$->selectStmt = $1;
10539 				}
10540 			| OVERRIDING override_kind VALUE_P SelectStmt
10541 				{
10542 					$$ = makeNode(InsertStmt);
10543 					$$->cols = NIL;
10544 					$$->override = $2;
10545 					$$->selectStmt = $4;
10546 				}
10547 			| '(' insert_column_list ')' SelectStmt
10548 				{
10549 					$$ = makeNode(InsertStmt);
10550 					$$->cols = $2;
10551 					$$->selectStmt = $4;
10552 				}
10553 			| '(' insert_column_list ')' OVERRIDING override_kind VALUE_P SelectStmt
10554 				{
10555 					$$ = makeNode(InsertStmt);
10556 					$$->cols = $2;
10557 					$$->override = $5;
10558 					$$->selectStmt = $7;
10559 				}
10560 			| DEFAULT VALUES
10561 				{
10562 					$$ = makeNode(InsertStmt);
10563 					$$->cols = NIL;
10564 					$$->selectStmt = NULL;
10565 				}
10566 		;
10567 
10568 override_kind:
10569 			USER		{ $$ = OVERRIDING_USER_VALUE; }
10570 			| SYSTEM_P	{ $$ = OVERRIDING_SYSTEM_VALUE; }
10571 		;
10572 
10573 insert_column_list:
10574 			insert_column_item
10575 					{ $$ = list_make1($1); }
10576 			| insert_column_list ',' insert_column_item
10577 					{ $$ = lappend($1, $3); }
10578 		;
10579 
10580 insert_column_item:
10581 			ColId opt_indirection
10582 				{
10583 					$$ = makeNode(ResTarget);
10584 					$$->name = $1;
10585 					$$->indirection = check_indirection($2, yyscanner);
10586 					$$->val = NULL;
10587 					$$->location = @1;
10588 				}
10589 		;
10590 
10591 opt_on_conflict:
10592 			ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list	where_clause
10593 				{
10594 					$$ = makeNode(OnConflictClause);
10595 					$$->action = ONCONFLICT_UPDATE;
10596 					$$->infer = $3;
10597 					$$->targetList = $7;
10598 					$$->whereClause = $8;
10599 					$$->location = @1;
10600 				}
10601 			|
10602 			ON CONFLICT opt_conf_expr DO NOTHING
10603 				{
10604 					$$ = makeNode(OnConflictClause);
10605 					$$->action = ONCONFLICT_NOTHING;
10606 					$$->infer = $3;
10607 					$$->targetList = NIL;
10608 					$$->whereClause = NULL;
10609 					$$->location = @1;
10610 				}
10611 			| /*EMPTY*/
10612 				{
10613 					$$ = NULL;
10614 				}
10615 		;
10616 
10617 opt_conf_expr:
10618 			'(' index_params ')' where_clause
10619 				{
10620 					$$ = makeNode(InferClause);
10621 					$$->indexElems = $2;
10622 					$$->whereClause = $4;
10623 					$$->conname = NULL;
10624 					$$->location = @1;
10625 				}
10626 			|
10627 			ON CONSTRAINT name
10628 				{
10629 					$$ = makeNode(InferClause);
10630 					$$->indexElems = NIL;
10631 					$$->whereClause = NULL;
10632 					$$->conname = $3;
10633 					$$->location = @1;
10634 				}
10635 			| /*EMPTY*/
10636 				{
10637 					$$ = NULL;
10638 				}
10639 		;
10640 
10641 returning_clause:
10642 			RETURNING target_list		{ $$ = $2; }
10643 			| /* EMPTY */				{ $$ = NIL; }
10644 		;
10645 
10646 
10647 /*****************************************************************************
10648  *
10649  *		QUERY:
10650  *				DELETE STATEMENTS
10651  *
10652  *****************************************************************************/
10653 
10654 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
10655 			using_clause where_or_current_clause returning_clause
10656 				{
10657 					DeleteStmt *n = makeNode(DeleteStmt);
10658 					n->relation = $4;
10659 					n->usingClause = $5;
10660 					n->whereClause = $6;
10661 					n->returningList = $7;
10662 					n->withClause = $1;
10663 					$$ = (Node *)n;
10664 				}
10665 		;
10666 
10667 using_clause:
10668 				USING from_list						{ $$ = $2; }
10669 			| /*EMPTY*/								{ $$ = NIL; }
10670 		;
10671 
10672 
10673 /*****************************************************************************
10674  *
10675  *		QUERY:
10676  *				LOCK TABLE
10677  *
10678  *****************************************************************************/
10679 
10680 LockStmt:	LOCK_P opt_table relation_expr_list opt_lock opt_nowait
10681 				{
10682 					LockStmt *n = makeNode(LockStmt);
10683 
10684 					n->relations = $3;
10685 					n->mode = $4;
10686 					n->nowait = $5;
10687 					$$ = (Node *)n;
10688 				}
10689 		;
10690 
10691 opt_lock:	IN_P lock_type MODE				{ $$ = $2; }
10692 			| /*EMPTY*/						{ $$ = AccessExclusiveLock; }
10693 		;
10694 
10695 lock_type:	ACCESS SHARE					{ $$ = AccessShareLock; }
10696 			| ROW SHARE						{ $$ = RowShareLock; }
10697 			| ROW EXCLUSIVE					{ $$ = RowExclusiveLock; }
10698 			| SHARE UPDATE EXCLUSIVE		{ $$ = ShareUpdateExclusiveLock; }
10699 			| SHARE							{ $$ = ShareLock; }
10700 			| SHARE ROW EXCLUSIVE			{ $$ = ShareRowExclusiveLock; }
10701 			| EXCLUSIVE						{ $$ = ExclusiveLock; }
10702 			| ACCESS EXCLUSIVE				{ $$ = AccessExclusiveLock; }
10703 		;
10704 
10705 opt_nowait:	NOWAIT							{ $$ = TRUE; }
10706 			| /*EMPTY*/						{ $$ = FALSE; }
10707 		;
10708 
10709 opt_nowait_or_skip:
10710 			NOWAIT							{ $$ = LockWaitError; }
10711 			| SKIP LOCKED					{ $$ = LockWaitSkip; }
10712 			| /*EMPTY*/						{ $$ = LockWaitBlock; }
10713 		;
10714 
10715 
10716 /*****************************************************************************
10717  *
10718  *		QUERY:
10719  *				UpdateStmt (UPDATE)
10720  *
10721  *****************************************************************************/
10722 
10723 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
10724 			SET set_clause_list
10725 			from_clause
10726 			where_or_current_clause
10727 			returning_clause
10728 				{
10729 					UpdateStmt *n = makeNode(UpdateStmt);
10730 					n->relation = $3;
10731 					n->targetList = $5;
10732 					n->fromClause = $6;
10733 					n->whereClause = $7;
10734 					n->returningList = $8;
10735 					n->withClause = $1;
10736 					$$ = (Node *)n;
10737 				}
10738 		;
10739 
10740 set_clause_list:
10741 			set_clause							{ $$ = $1; }
10742 			| set_clause_list ',' set_clause	{ $$ = list_concat($1,$3); }
10743 		;
10744 
10745 set_clause:
10746 			set_target '=' a_expr
10747 				{
10748 					$1->val = (Node *) $3;
10749 					$$ = list_make1($1);
10750 				}
10751 			| '(' set_target_list ')' '=' a_expr
10752 				{
10753 					int ncolumns = list_length($2);
10754 					int i = 1;
10755 					ListCell *col_cell;
10756 
10757 					/* Create a MultiAssignRef source for each target */
foreach(col_cell,$2)10758 					foreach(col_cell, $2)
10759 					{
10760 						ResTarget *res_col = (ResTarget *) lfirst(col_cell);
10761 						MultiAssignRef *r = makeNode(MultiAssignRef);
10762 
10763 						r->source = (Node *) $5;
10764 						r->colno = i;
10765 						r->ncolumns = ncolumns;
10766 						res_col->val = (Node *) r;
10767 						i++;
10768 					}
10769 
10770 					$$ = $2;
10771 				}
10772 		;
10773 
10774 set_target:
10775 			ColId opt_indirection
10776 				{
10777 					$$ = makeNode(ResTarget);
10778 					$$->name = $1;
10779 					$$->indirection = check_indirection($2, yyscanner);
10780 					$$->val = NULL;	/* upper production sets this */
10781 					$$->location = @1;
10782 				}
10783 		;
10784 
10785 set_target_list:
10786 			set_target								{ $$ = list_make1($1); }
10787 			| set_target_list ',' set_target		{ $$ = lappend($1,$3); }
10788 		;
10789 
10790 
10791 /*****************************************************************************
10792  *
10793  *		QUERY:
10794  *				CURSOR STATEMENTS
10795  *
10796  *****************************************************************************/
10797 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
10798 				{
10799 					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
10800 					n->portalname = $2;
10801 					/* currently we always set FAST_PLAN option */
10802 					n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
10803 					n->query = $7;
10804 					$$ = (Node *)n;
10805 				}
10806 		;
10807 
10808 cursor_name:	name						{ $$ = $1; }
10809 		;
10810 
10811 cursor_options: /*EMPTY*/					{ $$ = 0; }
10812 			| cursor_options NO SCROLL		{ $$ = $1 | CURSOR_OPT_NO_SCROLL; }
10813 			| cursor_options SCROLL			{ $$ = $1 | CURSOR_OPT_SCROLL; }
10814 			| cursor_options BINARY			{ $$ = $1 | CURSOR_OPT_BINARY; }
10815 			| cursor_options INSENSITIVE	{ $$ = $1 | CURSOR_OPT_INSENSITIVE; }
10816 		;
10817 
10818 opt_hold: /* EMPTY */						{ $$ = 0; }
10819 			| WITH HOLD						{ $$ = CURSOR_OPT_HOLD; }
10820 			| WITHOUT HOLD					{ $$ = 0; }
10821 		;
10822 
10823 /*****************************************************************************
10824  *
10825  *		QUERY:
10826  *				SELECT STATEMENTS
10827  *
10828  *****************************************************************************/
10829 
10830 /* A complete SELECT statement looks like this.
10831  *
10832  * The rule returns either a single SelectStmt node or a tree of them,
10833  * representing a set-operation tree.
10834  *
10835  * There is an ambiguity when a sub-SELECT is within an a_expr and there
10836  * are excess parentheses: do the parentheses belong to the sub-SELECT or
10837  * to the surrounding a_expr?  We don't really care, but bison wants to know.
10838  * To resolve the ambiguity, we are careful to define the grammar so that
10839  * the decision is staved off as long as possible: as long as we can keep
10840  * absorbing parentheses into the sub-SELECT, we will do so, and only when
10841  * it's no longer possible to do that will we decide that parens belong to
10842  * the expression.	For example, in "SELECT (((SELECT 2)) + 3)" the extra
10843  * parentheses are treated as part of the sub-select.  The necessity of doing
10844  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".	Had we
10845  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
10846  * SELECT viewpoint when we see the UNION.
10847  *
10848  * This approach is implemented by defining a nonterminal select_with_parens,
10849  * which represents a SELECT with at least one outer layer of parentheses,
10850  * and being careful to use select_with_parens, never '(' SelectStmt ')',
10851  * in the expression grammar.  We will then have shift-reduce conflicts
10852  * which we can resolve in favor of always treating '(' <select> ')' as
10853  * a select_with_parens.  To resolve the conflicts, the productions that
10854  * conflict with the select_with_parens productions are manually given
10855  * precedences lower than the precedence of ')', thereby ensuring that we
10856  * shift ')' (and then reduce to select_with_parens) rather than trying to
10857  * reduce the inner <select> nonterminal to something else.  We use UMINUS
10858  * precedence for this, which is a fairly arbitrary choice.
10859  *
10860  * To be able to define select_with_parens itself without ambiguity, we need
10861  * a nonterminal select_no_parens that represents a SELECT structure with no
10862  * outermost parentheses.  This is a little bit tedious, but it works.
10863  *
10864  * In non-expression contexts, we use SelectStmt which can represent a SELECT
10865  * with or without outer parentheses.
10866  */
10867 
10868 SelectStmt: select_no_parens			%prec UMINUS
10869 			| select_with_parens		%prec UMINUS
10870 		;
10871 
10872 select_with_parens:
10873 			'(' select_no_parens ')'				{ $$ = $2; }
10874 			| '(' select_with_parens ')'			{ $$ = $2; }
10875 		;
10876 
10877 /*
10878  * This rule parses the equivalent of the standard's <query expression>.
10879  * The duplicative productions are annoying, but hard to get rid of without
10880  * creating shift/reduce conflicts.
10881  *
10882  *	The locking clause (FOR UPDATE etc) may be before or after LIMIT/OFFSET.
10883  *	In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
10884  *	We now support both orderings, but prefer LIMIT/OFFSET before the locking
10885  * clause.
10886  *	2002-08-28 bjm
10887  */
10888 select_no_parens:
10889 			simple_select						{ $$ = $1; }
10890 			| select_clause sort_clause
10891 				{
10892 					insertSelectOptions((SelectStmt *) $1, $2, NIL,
10893 										NULL, NULL, NULL,
10894 										yyscanner);
10895 					$$ = $1;
10896 				}
10897 			| select_clause opt_sort_clause for_locking_clause opt_select_limit
10898 				{
10899 					insertSelectOptions((SelectStmt *) $1, $2, $3,
10900 										list_nth($4, 0), list_nth($4, 1),
10901 										NULL,
10902 										yyscanner);
10903 					$$ = $1;
10904 				}
10905 			| select_clause opt_sort_clause select_limit opt_for_locking_clause
10906 				{
10907 					insertSelectOptions((SelectStmt *) $1, $2, $4,
10908 										list_nth($3, 0), list_nth($3, 1),
10909 										NULL,
10910 										yyscanner);
10911 					$$ = $1;
10912 				}
10913 			| with_clause select_clause
10914 				{
10915 					insertSelectOptions((SelectStmt *) $2, NULL, NIL,
10916 										NULL, NULL,
10917 										$1,
10918 										yyscanner);
10919 					$$ = $2;
10920 				}
10921 			| with_clause select_clause sort_clause
10922 				{
10923 					insertSelectOptions((SelectStmt *) $2, $3, NIL,
10924 										NULL, NULL,
10925 										$1,
10926 										yyscanner);
10927 					$$ = $2;
10928 				}
10929 			| with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
10930 				{
10931 					insertSelectOptions((SelectStmt *) $2, $3, $4,
10932 										list_nth($5, 0), list_nth($5, 1),
10933 										$1,
10934 										yyscanner);
10935 					$$ = $2;
10936 				}
10937 			| with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
10938 				{
10939 					insertSelectOptions((SelectStmt *) $2, $3, $5,
10940 										list_nth($4, 0), list_nth($4, 1),
10941 										$1,
10942 										yyscanner);
10943 					$$ = $2;
10944 				}
10945 		;
10946 
10947 select_clause:
10948 			simple_select							{ $$ = $1; }
10949 			| select_with_parens					{ $$ = $1; }
10950 		;
10951 
10952 /*
10953  * This rule parses SELECT statements that can appear within set operations,
10954  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
10955  * the ordering of the set operations.	Without '(' and ')' we want the
10956  * operations to be ordered per the precedence specs at the head of this file.
10957  *
10958  * As with select_no_parens, simple_select cannot have outer parentheses,
10959  * but can have parenthesized subclauses.
10960  *
10961  * Note that sort clauses cannot be included at this level --- SQL requires
10962  *		SELECT foo UNION SELECT bar ORDER BY baz
10963  * to be parsed as
10964  *		(SELECT foo UNION SELECT bar) ORDER BY baz
10965  * not
10966  *		SELECT foo UNION (SELECT bar ORDER BY baz)
10967  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
10968  * described as part of the select_no_parens production, not simple_select.
10969  * This does not limit functionality, because you can reintroduce these
10970  * clauses inside parentheses.
10971  *
10972  * NOTE: only the leftmost component SelectStmt should have INTO.
10973  * However, this is not checked by the grammar; parse analysis must check it.
10974  */
10975 simple_select:
10976 			SELECT opt_all_clause opt_target_list
10977 			into_clause from_clause where_clause
10978 			group_clause having_clause window_clause
10979 				{
10980 					SelectStmt *n = makeNode(SelectStmt);
10981 					n->targetList = $3;
10982 					n->intoClause = $4;
10983 					n->fromClause = $5;
10984 					n->whereClause = $6;
10985 					n->groupClause = $7;
10986 					n->havingClause = $8;
10987 					n->windowClause = $9;
10988 					$$ = (Node *)n;
10989 				}
10990 			| SELECT distinct_clause target_list
10991 			into_clause from_clause where_clause
10992 			group_clause having_clause window_clause
10993 				{
10994 					SelectStmt *n = makeNode(SelectStmt);
10995 					n->distinctClause = $2;
10996 					n->targetList = $3;
10997 					n->intoClause = $4;
10998 					n->fromClause = $5;
10999 					n->whereClause = $6;
11000 					n->groupClause = $7;
11001 					n->havingClause = $8;
11002 					n->windowClause = $9;
11003 					$$ = (Node *)n;
11004 				}
11005 			| values_clause							{ $$ = $1; }
11006 			| TABLE relation_expr
11007 				{
11008 					/* same as SELECT * FROM relation_expr */
11009 					ColumnRef *cr = makeNode(ColumnRef);
11010 					ResTarget *rt = makeNode(ResTarget);
11011 					SelectStmt *n = makeNode(SelectStmt);
11012 
11013 					cr->fields = list_make1(makeNode(A_Star));
11014 					cr->location = -1;
11015 
11016 					rt->name = NULL;
11017 					rt->indirection = NIL;
11018 					rt->val = (Node *)cr;
11019 					rt->location = -1;
11020 
11021 					n->targetList = list_make1(rt);
11022 					n->fromClause = list_make1($2);
11023 					$$ = (Node *)n;
11024 				}
11025 			| select_clause UNION all_or_distinct select_clause
11026 				{
11027 					$$ = makeSetOp(SETOP_UNION, $3, $1, $4);
11028 				}
11029 			| select_clause INTERSECT all_or_distinct select_clause
11030 				{
11031 					$$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
11032 				}
11033 			| select_clause EXCEPT all_or_distinct select_clause
11034 				{
11035 					$$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
11036 				}
11037 		;
11038 
11039 /*
11040  * SQL standard WITH clause looks like:
11041  *
11042  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
11043  *		AS (query) [ SEARCH or CYCLE clause ]
11044  *
11045  * We don't currently support the SEARCH or CYCLE clause.
11046  *
11047  * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY.
11048  */
11049 with_clause:
11050 		WITH cte_list
11051 			{
11052 				$$ = makeNode(WithClause);
11053 				$$->ctes = $2;
11054 				$$->recursive = false;
11055 				$$->location = @1;
11056 			}
11057 		| WITH_LA cte_list
11058 			{
11059 				$$ = makeNode(WithClause);
11060 				$$->ctes = $2;
11061 				$$->recursive = false;
11062 				$$->location = @1;
11063 			}
11064 		| WITH RECURSIVE cte_list
11065 			{
11066 				$$ = makeNode(WithClause);
11067 				$$->ctes = $3;
11068 				$$->recursive = true;
11069 				$$->location = @1;
11070 			}
11071 		;
11072 
11073 cte_list:
11074 		common_table_expr						{ $$ = list_make1($1); }
11075 		| cte_list ',' common_table_expr		{ $$ = lappend($1, $3); }
11076 		;
11077 
11078 common_table_expr:  name opt_name_list AS '(' PreparableStmt ')'
11079 			{
11080 				CommonTableExpr *n = makeNode(CommonTableExpr);
11081 				n->ctename = $1;
11082 				n->aliascolnames = $2;
11083 				n->ctequery = $5;
11084 				n->location = @1;
11085 				$$ = (Node *) n;
11086 			}
11087 		;
11088 
11089 opt_with_clause:
11090 		with_clause								{ $$ = $1; }
11091 		| /*EMPTY*/								{ $$ = NULL; }
11092 		;
11093 
11094 into_clause:
11095 			INTO OptTempTableName
11096 				{
11097 					$$ = makeNode(IntoClause);
11098 					$$->rel = $2;
11099 					$$->colNames = NIL;
11100 					$$->options = NIL;
11101 					$$->onCommit = ONCOMMIT_NOOP;
11102 					$$->tableSpaceName = NULL;
11103 					$$->viewQuery = NULL;
11104 					$$->skipData = false;
11105 				}
11106 			| /*EMPTY*/
11107 				{ $$ = NULL; }
11108 		;
11109 
11110 /*
11111  * Redundancy here is needed to avoid shift/reduce conflicts,
11112  * since TEMP is not a reserved word.  See also OptTemp.
11113  */
11114 OptTempTableName:
11115 			TEMPORARY opt_table qualified_name
11116 				{
11117 					$$ = $3;
11118 					$$->relpersistence = RELPERSISTENCE_TEMP;
11119 				}
11120 			| TEMP opt_table qualified_name
11121 				{
11122 					$$ = $3;
11123 					$$->relpersistence = RELPERSISTENCE_TEMP;
11124 				}
11125 			| LOCAL TEMPORARY opt_table qualified_name
11126 				{
11127 					$$ = $4;
11128 					$$->relpersistence = RELPERSISTENCE_TEMP;
11129 				}
11130 			| LOCAL TEMP opt_table qualified_name
11131 				{
11132 					$$ = $4;
11133 					$$->relpersistence = RELPERSISTENCE_TEMP;
11134 				}
11135 			| GLOBAL TEMPORARY opt_table qualified_name
11136 				{
11137 					ereport(WARNING,
11138 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11139 							 parser_errposition(@1)));
11140 					$$ = $4;
11141 					$$->relpersistence = RELPERSISTENCE_TEMP;
11142 				}
11143 			| GLOBAL TEMP opt_table qualified_name
11144 				{
11145 					ereport(WARNING,
11146 							(errmsg("GLOBAL is deprecated in temporary table creation"),
11147 							 parser_errposition(@1)));
11148 					$$ = $4;
11149 					$$->relpersistence = RELPERSISTENCE_TEMP;
11150 				}
11151 			| UNLOGGED opt_table qualified_name
11152 				{
11153 					$$ = $3;
11154 					$$->relpersistence = RELPERSISTENCE_UNLOGGED;
11155 				}
11156 			| TABLE qualified_name
11157 				{
11158 					$$ = $2;
11159 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11160 				}
11161 			| qualified_name
11162 				{
11163 					$$ = $1;
11164 					$$->relpersistence = RELPERSISTENCE_PERMANENT;
11165 				}
11166 		;
11167 
11168 opt_table:	TABLE									{}
11169 			| /*EMPTY*/								{}
11170 		;
11171 
11172 all_or_distinct:
11173 			ALL										{ $$ = TRUE; }
11174 			| DISTINCT								{ $$ = FALSE; }
11175 			| /*EMPTY*/								{ $$ = FALSE; }
11176 		;
11177 
11178 /* We use (NIL) as a placeholder to indicate that all target expressions
11179  * should be placed in the DISTINCT list during parsetree analysis.
11180  */
11181 distinct_clause:
11182 			DISTINCT								{ $$ = list_make1(NIL); }
11183 			| DISTINCT ON '(' expr_list ')'			{ $$ = $4; }
11184 		;
11185 
11186 opt_all_clause:
11187 			ALL										{ $$ = NIL;}
11188 			| /*EMPTY*/								{ $$ = NIL; }
11189 		;
11190 
11191 opt_sort_clause:
11192 			sort_clause								{ $$ = $1;}
11193 			| /*EMPTY*/								{ $$ = NIL; }
11194 		;
11195 
11196 sort_clause:
11197 			ORDER BY sortby_list					{ $$ = $3; }
11198 		;
11199 
11200 sortby_list:
11201 			sortby									{ $$ = list_make1($1); }
11202 			| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
11203 		;
11204 
11205 sortby:		a_expr USING qual_all_Op opt_nulls_order
11206 				{
11207 					$$ = makeNode(SortBy);
11208 					$$->node = $1;
11209 					$$->sortby_dir = SORTBY_USING;
11210 					$$->sortby_nulls = $4;
11211 					$$->useOp = $3;
11212 					$$->location = @3;
11213 				}
11214 			| a_expr opt_asc_desc opt_nulls_order
11215 				{
11216 					$$ = makeNode(SortBy);
11217 					$$->node = $1;
11218 					$$->sortby_dir = $2;
11219 					$$->sortby_nulls = $3;
11220 					$$->useOp = NIL;
11221 					$$->location = -1;		/* no operator */
11222 				}
11223 		;
11224 
11225 
11226 select_limit:
11227 			limit_clause offset_clause			{ $$ = list_make2($2, $1); }
11228 			| offset_clause limit_clause		{ $$ = list_make2($1, $2); }
11229 			| limit_clause						{ $$ = list_make2(NULL, $1); }
11230 			| offset_clause						{ $$ = list_make2($1, NULL); }
11231 		;
11232 
11233 opt_select_limit:
11234 			select_limit						{ $$ = $1; }
11235 			| /* EMPTY */						{ $$ = list_make2(NULL,NULL); }
11236 		;
11237 
11238 limit_clause:
11239 			LIMIT select_limit_value
11240 				{ $$ = $2; }
11241 			| LIMIT select_limit_value ',' select_offset_value
11242 				{
11243 					/* Disabled because it was too confusing, bjm 2002-02-18 */
11244 					ereport(ERROR,
11245 							(errcode(ERRCODE_SYNTAX_ERROR),
11246 							 errmsg("LIMIT #,# syntax is not supported"),
11247 							 errhint("Use separate LIMIT and OFFSET clauses."),
11248 							 parser_errposition(@1)));
11249 				}
11250 			/* SQL:2008 syntax */
11251 			| FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
11252 				{ $$ = $3; }
11253 		;
11254 
11255 offset_clause:
11256 			OFFSET select_offset_value
11257 				{ $$ = $2; }
11258 			/* SQL:2008 syntax */
11259 			| OFFSET select_offset_value2 row_or_rows
11260 				{ $$ = $2; }
11261 		;
11262 
11263 select_limit_value:
11264 			a_expr									{ $$ = $1; }
11265 			| ALL
11266 				{
11267 					/* LIMIT ALL is represented as a NULL constant */
11268 					$$ = makeNullAConst(@1);
11269 				}
11270 		;
11271 
11272 select_offset_value:
11273 			a_expr									{ $$ = $1; }
11274 		;
11275 
11276 /*
11277  * Allowing full expressions without parentheses causes various parsing
11278  * problems with the trailing ROW/ROWS key words.  SQL only calls for
11279  * constants, so we allow the rest only with parentheses.  If omitted,
11280  * default to 1.
11281  */
11282 opt_select_fetch_first_value:
11283 			SignedIconst						{ $$ = makeIntConst($1, @1); }
11284 			| '(' a_expr ')'					{ $$ = $2; }
11285 			| /*EMPTY*/							{ $$ = makeIntConst(1, -1); }
11286 		;
11287 
11288 /*
11289  * Again, the trailing ROW/ROWS in this case prevent the full expression
11290  * syntax.  c_expr is the best we can do.
11291  */
11292 select_offset_value2:
11293 			c_expr									{ $$ = $1; }
11294 		;
11295 
11296 /* noise words */
11297 row_or_rows: ROW									{ $$ = 0; }
11298 			| ROWS									{ $$ = 0; }
11299 		;
11300 
11301 first_or_next: FIRST_P								{ $$ = 0; }
11302 			| NEXT									{ $$ = 0; }
11303 		;
11304 
11305 
11306 /*
11307  * This syntax for group_clause tries to follow the spec quite closely.
11308  * However, the spec allows only column references, not expressions,
11309  * which introduces an ambiguity between implicit row constructors
11310  * (a,b) and lists of column references.
11311  *
11312  * We handle this by using the a_expr production for what the spec calls
11313  * <ordinary grouping set>, which in the spec represents either one column
11314  * reference or a parenthesized list of column references. Then, we check the
11315  * top node of the a_expr to see if it's an implicit RowExpr, and if so, just
11316  * grab and use the list, discarding the node. (this is done in parse analysis,
11317  * not here)
11318  *
11319  * (we abuse the row_format field of RowExpr to distinguish implicit and
11320  * explicit row constructors; it's debatable if anyone sanely wants to use them
11321  * in a group clause, but if they have a reason to, we make it possible.)
11322  *
11323  * Each item in the group_clause list is either an expression tree or a
11324  * GroupingSet node of some type.
11325  */
11326 group_clause:
11327 			GROUP_P BY group_by_list				{ $$ = $3; }
11328 			| /*EMPTY*/								{ $$ = NIL; }
11329 		;
11330 
11331 group_by_list:
11332 			group_by_item							{ $$ = list_make1($1); }
11333 			| group_by_list ',' group_by_item		{ $$ = lappend($1,$3); }
11334 		;
11335 
11336 group_by_item:
11337 			a_expr									{ $$ = $1; }
11338 			| empty_grouping_set					{ $$ = $1; }
11339 			| cube_clause							{ $$ = $1; }
11340 			| rollup_clause							{ $$ = $1; }
11341 			| grouping_sets_clause					{ $$ = $1; }
11342 		;
11343 
11344 empty_grouping_set:
11345 			'(' ')'
11346 				{
11347 					$$ = (Node *) makeGroupingSet(GROUPING_SET_EMPTY, NIL, @1);
11348 				}
11349 		;
11350 
11351 /*
11352  * These hacks rely on setting precedence of CUBE and ROLLUP below that of '(',
11353  * so that they shift in these rules rather than reducing the conflicting
11354  * unreserved_keyword rule.
11355  */
11356 
11357 rollup_clause:
11358 			ROLLUP '(' expr_list ')'
11359 				{
11360 					$$ = (Node *) makeGroupingSet(GROUPING_SET_ROLLUP, $3, @1);
11361 				}
11362 		;
11363 
11364 cube_clause:
11365 			CUBE '(' expr_list ')'
11366 				{
11367 					$$ = (Node *) makeGroupingSet(GROUPING_SET_CUBE, $3, @1);
11368 				}
11369 		;
11370 
11371 grouping_sets_clause:
11372 			GROUPING SETS '(' group_by_list ')'
11373 				{
11374 					$$ = (Node *) makeGroupingSet(GROUPING_SET_SETS, $4, @1);
11375 				}
11376 		;
11377 
11378 having_clause:
11379 			HAVING a_expr							{ $$ = $2; }
11380 			| /*EMPTY*/								{ $$ = NULL; }
11381 		;
11382 
11383 for_locking_clause:
11384 			for_locking_items						{ $$ = $1; }
11385 			| FOR READ ONLY							{ $$ = NIL; }
11386 		;
11387 
11388 opt_for_locking_clause:
11389 			for_locking_clause						{ $$ = $1; }
11390 			| /* EMPTY */							{ $$ = NIL; }
11391 		;
11392 
11393 for_locking_items:
11394 			for_locking_item						{ $$ = list_make1($1); }
11395 			| for_locking_items for_locking_item	{ $$ = lappend($1, $2); }
11396 		;
11397 
11398 for_locking_item:
11399 			for_locking_strength locked_rels_list opt_nowait_or_skip
11400 				{
11401 					LockingClause *n = makeNode(LockingClause);
11402 					n->lockedRels = $2;
11403 					n->strength = $1;
11404 					n->waitPolicy = $3;
11405 					$$ = (Node *) n;
11406 				}
11407 		;
11408 
11409 for_locking_strength:
11410 			FOR UPDATE 							{ $$ = LCS_FORUPDATE; }
11411 			| FOR NO KEY UPDATE 				{ $$ = LCS_FORNOKEYUPDATE; }
11412 			| FOR SHARE 						{ $$ = LCS_FORSHARE; }
11413 			| FOR KEY SHARE 					{ $$ = LCS_FORKEYSHARE; }
11414 		;
11415 
11416 locked_rels_list:
11417 			OF qualified_name_list					{ $$ = $2; }
11418 			| /* EMPTY */							{ $$ = NIL; }
11419 		;
11420 
11421 
11422 /*
11423  * We should allow ROW '(' expr_list ')' too, but that seems to require
11424  * making VALUES a fully reserved word, which will probably break more apps
11425  * than allowing the noise-word is worth.
11426  */
11427 values_clause:
11428 			VALUES '(' expr_list ')'
11429 				{
11430 					SelectStmt *n = makeNode(SelectStmt);
11431 					n->valuesLists = list_make1($3);
11432 					$$ = (Node *) n;
11433 				}
11434 			| values_clause ',' '(' expr_list ')'
11435 				{
11436 					SelectStmt *n = (SelectStmt *) $1;
11437 					n->valuesLists = lappend(n->valuesLists, $4);
11438 					$$ = (Node *) n;
11439 				}
11440 		;
11441 
11442 
11443 /*****************************************************************************
11444  *
11445  *	clauses common to all Optimizable Stmts:
11446  *		from_clause		- allow list of both JOIN expressions and table names
11447  *		where_clause	- qualifications for joins or restrictions
11448  *
11449  *****************************************************************************/
11450 
11451 from_clause:
11452 			FROM from_list							{ $$ = $2; }
11453 			| /*EMPTY*/								{ $$ = NIL; }
11454 		;
11455 
11456 from_list:
11457 			table_ref								{ $$ = list_make1($1); }
11458 			| from_list ',' table_ref				{ $$ = lappend($1, $3); }
11459 		;
11460 
11461 /*
11462  * table_ref is where an alias clause can be attached.
11463  */
11464 table_ref:	relation_expr opt_alias_clause
11465 				{
11466 					$1->alias = $2;
11467 					$$ = (Node *) $1;
11468 				}
11469 			| relation_expr opt_alias_clause tablesample_clause
11470 				{
11471 					RangeTableSample *n = (RangeTableSample *) $3;
11472 					$1->alias = $2;
11473 					/* relation_expr goes inside the RangeTableSample node */
11474 					n->relation = (Node *) $1;
11475 					$$ = (Node *) n;
11476 				}
11477 			| func_table func_alias_clause
11478 				{
11479 					RangeFunction *n = (RangeFunction *) $1;
11480 					n->alias = linitial($2);
11481 					n->coldeflist = lsecond($2);
11482 					$$ = (Node *) n;
11483 				}
11484 			| LATERAL_P func_table func_alias_clause
11485 				{
11486 					RangeFunction *n = (RangeFunction *) $2;
11487 					n->lateral = true;
11488 					n->alias = linitial($3);
11489 					n->coldeflist = lsecond($3);
11490 					$$ = (Node *) n;
11491 				}
11492 			| xmltable opt_alias_clause
11493 				{
11494 					RangeTableFunc *n = (RangeTableFunc *) $1;
11495 					n->alias = $2;
11496 					$$ = (Node *) n;
11497 				}
11498 			| LATERAL_P xmltable opt_alias_clause
11499 				{
11500 					RangeTableFunc *n = (RangeTableFunc *) $2;
11501 					n->lateral = true;
11502 					n->alias = $3;
11503 					$$ = (Node *) n;
11504 				}
11505 			| select_with_parens opt_alias_clause
11506 				{
11507 					RangeSubselect *n = makeNode(RangeSubselect);
11508 					n->lateral = false;
11509 					n->subquery = $1;
11510 					n->alias = $2;
11511 					/*
11512 					 * The SQL spec does not permit a subselect
11513 					 * (<derived_table>) without an alias clause,
11514 					 * so we don't either.  This avoids the problem
11515 					 * of needing to invent a unique refname for it.
11516 					 * That could be surmounted if there's sufficient
11517 					 * popular demand, but for now let's just implement
11518 					 * the spec and see if anyone complains.
11519 					 * However, it does seem like a good idea to emit
11520 					 * an error message that's better than "syntax error".
11521 					 */
11522 					if ($2 == NULL)
11523 					{
11524 						if (IsA($1, SelectStmt) &&
11525 							((SelectStmt *) $1)->valuesLists)
11526 							ereport(ERROR,
11527 									(errcode(ERRCODE_SYNTAX_ERROR),
11528 									 errmsg("VALUES in FROM must have an alias"),
11529 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
11530 									 parser_errposition(@1)));
11531 						else
11532 							ereport(ERROR,
11533 									(errcode(ERRCODE_SYNTAX_ERROR),
11534 									 errmsg("subquery in FROM must have an alias"),
11535 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
11536 									 parser_errposition(@1)));
11537 					}
11538 					$$ = (Node *) n;
11539 				}
11540 			| LATERAL_P select_with_parens opt_alias_clause
11541 				{
11542 					RangeSubselect *n = makeNode(RangeSubselect);
11543 					n->lateral = true;
11544 					n->subquery = $2;
11545 					n->alias = $3;
11546 					/* same comment as above */
11547 					if ($3 == NULL)
11548 					{
11549 						if (IsA($2, SelectStmt) &&
11550 							((SelectStmt *) $2)->valuesLists)
11551 							ereport(ERROR,
11552 									(errcode(ERRCODE_SYNTAX_ERROR),
11553 									 errmsg("VALUES in FROM must have an alias"),
11554 									 errhint("For example, FROM (VALUES ...) [AS] foo."),
11555 									 parser_errposition(@2)));
11556 						else
11557 							ereport(ERROR,
11558 									(errcode(ERRCODE_SYNTAX_ERROR),
11559 									 errmsg("subquery in FROM must have an alias"),
11560 									 errhint("For example, FROM (SELECT ...) [AS] foo."),
11561 									 parser_errposition(@2)));
11562 					}
11563 					$$ = (Node *) n;
11564 				}
11565 			| joined_table
11566 				{
11567 					$$ = (Node *) $1;
11568 				}
11569 			| '(' joined_table ')' alias_clause
11570 				{
11571 					$2->alias = $4;
11572 					$$ = (Node *) $2;
11573 				}
11574 		;
11575 
11576 
11577 /*
11578  * It may seem silly to separate joined_table from table_ref, but there is
11579  * method in SQL's madness: if you don't do it this way you get reduce-
11580  * reduce conflicts, because it's not clear to the parser generator whether
11581  * to expect alias_clause after ')' or not.  For the same reason we must
11582  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
11583  * join_type to expand to empty; if we try it, the parser generator can't
11584  * figure out when to reduce an empty join_type right after table_ref.
11585  *
11586  * Note that a CROSS JOIN is the same as an unqualified
11587  * INNER JOIN, and an INNER JOIN/ON has the same shape
11588  * but a qualification expression to limit membership.
11589  * A NATURAL JOIN implicitly matches column names between
11590  * tables and the shape is determined by which columns are
11591  * in common. We'll collect columns during the later transformations.
11592  */
11593 
11594 joined_table:
11595 			'(' joined_table ')'
11596 				{
11597 					$$ = $2;
11598 				}
11599 			| table_ref CROSS JOIN table_ref
11600 				{
11601 					/* CROSS JOIN is same as unqualified inner join */
11602 					JoinExpr *n = makeNode(JoinExpr);
11603 					n->jointype = JOIN_INNER;
11604 					n->isNatural = FALSE;
11605 					n->larg = $1;
11606 					n->rarg = $4;
11607 					n->usingClause = NIL;
11608 					n->quals = NULL;
11609 					$$ = n;
11610 				}
11611 			| table_ref join_type JOIN table_ref join_qual
11612 				{
11613 					JoinExpr *n = makeNode(JoinExpr);
11614 					n->jointype = $2;
11615 					n->isNatural = FALSE;
11616 					n->larg = $1;
11617 					n->rarg = $4;
11618 					if ($5 != NULL && IsA($5, List))
11619 						n->usingClause = (List *) $5; /* USING clause */
11620 					else
11621 						n->quals = $5; /* ON clause */
11622 					$$ = n;
11623 				}
11624 			| table_ref JOIN table_ref join_qual
11625 				{
11626 					/* letting join_type reduce to empty doesn't work */
11627 					JoinExpr *n = makeNode(JoinExpr);
11628 					n->jointype = JOIN_INNER;
11629 					n->isNatural = FALSE;
11630 					n->larg = $1;
11631 					n->rarg = $3;
11632 					if ($4 != NULL && IsA($4, List))
11633 						n->usingClause = (List *) $4; /* USING clause */
11634 					else
11635 						n->quals = $4; /* ON clause */
11636 					$$ = n;
11637 				}
11638 			| table_ref NATURAL join_type JOIN table_ref
11639 				{
11640 					JoinExpr *n = makeNode(JoinExpr);
11641 					n->jointype = $3;
11642 					n->isNatural = TRUE;
11643 					n->larg = $1;
11644 					n->rarg = $5;
11645 					n->usingClause = NIL; /* figure out which columns later... */
11646 					n->quals = NULL; /* fill later */
11647 					$$ = n;
11648 				}
11649 			| table_ref NATURAL JOIN table_ref
11650 				{
11651 					/* letting join_type reduce to empty doesn't work */
11652 					JoinExpr *n = makeNode(JoinExpr);
11653 					n->jointype = JOIN_INNER;
11654 					n->isNatural = TRUE;
11655 					n->larg = $1;
11656 					n->rarg = $4;
11657 					n->usingClause = NIL; /* figure out which columns later... */
11658 					n->quals = NULL; /* fill later */
11659 					$$ = n;
11660 				}
11661 		;
11662 
11663 alias_clause:
11664 			AS ColId '(' name_list ')'
11665 				{
11666 					$$ = makeNode(Alias);
11667 					$$->aliasname = $2;
11668 					$$->colnames = $4;
11669 				}
11670 			| AS ColId
11671 				{
11672 					$$ = makeNode(Alias);
11673 					$$->aliasname = $2;
11674 				}
11675 			| ColId '(' name_list ')'
11676 				{
11677 					$$ = makeNode(Alias);
11678 					$$->aliasname = $1;
11679 					$$->colnames = $3;
11680 				}
11681 			| ColId
11682 				{
11683 					$$ = makeNode(Alias);
11684 					$$->aliasname = $1;
11685 				}
11686 		;
11687 
11688 opt_alias_clause: alias_clause						{ $$ = $1; }
11689 			| /*EMPTY*/								{ $$ = NULL; }
11690 		;
11691 
11692 /*
11693  * func_alias_clause can include both an Alias and a coldeflist, so we make it
11694  * return a 2-element list that gets disassembled by calling production.
11695  */
11696 func_alias_clause:
11697 			alias_clause
11698 				{
11699 					$$ = list_make2($1, NIL);
11700 				}
11701 			| AS '(' TableFuncElementList ')'
11702 				{
11703 					$$ = list_make2(NULL, $3);
11704 				}
11705 			| AS ColId '(' TableFuncElementList ')'
11706 				{
11707 					Alias *a = makeNode(Alias);
11708 					a->aliasname = $2;
11709 					$$ = list_make2(a, $4);
11710 				}
11711 			| ColId '(' TableFuncElementList ')'
11712 				{
11713 					Alias *a = makeNode(Alias);
11714 					a->aliasname = $1;
11715 					$$ = list_make2(a, $3);
11716 				}
11717 			| /*EMPTY*/
11718 				{
11719 					$$ = list_make2(NULL, NIL);
11720 				}
11721 		;
11722 
11723 join_type:	FULL join_outer							{ $$ = JOIN_FULL; }
11724 			| LEFT join_outer						{ $$ = JOIN_LEFT; }
11725 			| RIGHT join_outer						{ $$ = JOIN_RIGHT; }
11726 			| INNER_P								{ $$ = JOIN_INNER; }
11727 		;
11728 
11729 /* OUTER is just noise... */
11730 join_outer: OUTER_P									{ $$ = NULL; }
11731 			| /*EMPTY*/								{ $$ = NULL; }
11732 		;
11733 
11734 /* JOIN qualification clauses
11735  * Possibilities are:
11736  *	USING ( column list ) allows only unqualified column names,
11737  *						  which must match between tables.
11738  *	ON expr allows more general qualifications.
11739  *
11740  * We return USING as a List node, while an ON-expr will not be a List.
11741  */
11742 
11743 join_qual:	USING '(' name_list ')'					{ $$ = (Node *) $3; }
11744 			| ON a_expr								{ $$ = $2; }
11745 		;
11746 
11747 
11748 relation_expr:
11749 			qualified_name
11750 				{
11751 					/* inheritance query, implicitly */
11752 					$$ = $1;
11753 					$$->inh = true;
11754 					$$->alias = NULL;
11755 				}
11756 			| qualified_name '*'
11757 				{
11758 					/* inheritance query, explicitly */
11759 					$$ = $1;
11760 					$$->inh = true;
11761 					$$->alias = NULL;
11762 				}
11763 			| ONLY qualified_name
11764 				{
11765 					/* no inheritance */
11766 					$$ = $2;
11767 					$$->inh = false;
11768 					$$->alias = NULL;
11769 				}
11770 			| ONLY '(' qualified_name ')'
11771 				{
11772 					/* no inheritance, SQL99-style syntax */
11773 					$$ = $3;
11774 					$$->inh = false;
11775 					$$->alias = NULL;
11776 				}
11777 		;
11778 
11779 
11780 relation_expr_list:
11781 			relation_expr							{ $$ = list_make1($1); }
11782 			| relation_expr_list ',' relation_expr	{ $$ = lappend($1, $3); }
11783 		;
11784 
11785 
11786 /*
11787  * Given "UPDATE foo set set ...", we have to decide without looking any
11788  * further ahead whether the first "set" is an alias or the UPDATE's SET
11789  * keyword.  Since "set" is allowed as a column name both interpretations
11790  * are feasible.  We resolve the shift/reduce conflict by giving the first
11791  * relation_expr_opt_alias production a higher precedence than the SET token
11792  * has, causing the parser to prefer to reduce, in effect assuming that the
11793  * SET is not an alias.
11794  */
11795 relation_expr_opt_alias: relation_expr					%prec UMINUS
11796 				{
11797 					$$ = $1;
11798 				}
11799 			| relation_expr ColId
11800 				{
11801 					Alias *alias = makeNode(Alias);
11802 					alias->aliasname = $2;
11803 					$1->alias = alias;
11804 					$$ = $1;
11805 				}
11806 			| relation_expr AS ColId
11807 				{
11808 					Alias *alias = makeNode(Alias);
11809 					alias->aliasname = $3;
11810 					$1->alias = alias;
11811 					$$ = $1;
11812 				}
11813 		;
11814 
11815 /*
11816  * TABLESAMPLE decoration in a FROM item
11817  */
11818 tablesample_clause:
11819 			TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause
11820 				{
11821 					RangeTableSample *n = makeNode(RangeTableSample);
11822 					/* n->relation will be filled in later */
11823 					n->method = $2;
11824 					n->args = $4;
11825 					n->repeatable = $6;
11826 					n->location = @2;
11827 					$$ = (Node *) n;
11828 				}
11829 		;
11830 
11831 opt_repeatable_clause:
11832 			REPEATABLE '(' a_expr ')'	{ $$ = (Node *) $3; }
11833 			| /*EMPTY*/					{ $$ = NULL; }
11834 		;
11835 
11836 /*
11837  * func_table represents a function invocation in a FROM list. It can be
11838  * a plain function call, like "foo(...)", or a ROWS FROM expression with
11839  * one or more function calls, "ROWS FROM (foo(...), bar(...))",
11840  * optionally with WITH ORDINALITY attached.
11841  * In the ROWS FROM syntax, a column definition list can be given for each
11842  * function, for example:
11843  *     ROWS FROM (foo() AS (foo_res_a text, foo_res_b text),
11844  *                bar() AS (bar_res_a text, bar_res_b text))
11845  * It's also possible to attach a column definition list to the RangeFunction
11846  * as a whole, but that's handled by the table_ref production.
11847  */
11848 func_table: func_expr_windowless opt_ordinality
11849 				{
11850 					RangeFunction *n = makeNode(RangeFunction);
11851 					n->lateral = false;
11852 					n->ordinality = $2;
11853 					n->is_rowsfrom = false;
11854 					n->functions = list_make1(list_make2($1, NIL));
11855 					/* alias and coldeflist are set by table_ref production */
11856 					$$ = (Node *) n;
11857 				}
11858 			| ROWS FROM '(' rowsfrom_list ')' opt_ordinality
11859 				{
11860 					RangeFunction *n = makeNode(RangeFunction);
11861 					n->lateral = false;
11862 					n->ordinality = $6;
11863 					n->is_rowsfrom = true;
11864 					n->functions = $4;
11865 					/* alias and coldeflist are set by table_ref production */
11866 					$$ = (Node *) n;
11867 				}
11868 		;
11869 
11870 rowsfrom_item: func_expr_windowless opt_col_def_list
11871 				{ $$ = list_make2($1, $2); }
11872 		;
11873 
11874 rowsfrom_list:
11875 			rowsfrom_item						{ $$ = list_make1($1); }
11876 			| rowsfrom_list ',' rowsfrom_item	{ $$ = lappend($1, $3); }
11877 		;
11878 
11879 opt_col_def_list: AS '(' TableFuncElementList ')'	{ $$ = $3; }
11880 			| /*EMPTY*/								{ $$ = NIL; }
11881 		;
11882 
11883 opt_ordinality: WITH_LA ORDINALITY					{ $$ = true; }
11884 			| /*EMPTY*/								{ $$ = false; }
11885 		;
11886 
11887 
11888 where_clause:
11889 			WHERE a_expr							{ $$ = $2; }
11890 			| /*EMPTY*/								{ $$ = NULL; }
11891 		;
11892 
11893 /* variant for UPDATE and DELETE */
11894 where_or_current_clause:
11895 			WHERE a_expr							{ $$ = $2; }
11896 			| WHERE CURRENT_P OF cursor_name
11897 				{
11898 					CurrentOfExpr *n = makeNode(CurrentOfExpr);
11899 					/* cvarno is filled in by parse analysis */
11900 					n->cursor_name = $4;
11901 					n->cursor_param = 0;
11902 					$$ = (Node *) n;
11903 				}
11904 			| /*EMPTY*/								{ $$ = NULL; }
11905 		;
11906 
11907 
11908 OptTableFuncElementList:
11909 			TableFuncElementList				{ $$ = $1; }
11910 			| /*EMPTY*/							{ $$ = NIL; }
11911 		;
11912 
11913 TableFuncElementList:
11914 			TableFuncElement
11915 				{
11916 					$$ = list_make1($1);
11917 				}
11918 			| TableFuncElementList ',' TableFuncElement
11919 				{
11920 					$$ = lappend($1, $3);
11921 				}
11922 		;
11923 
11924 TableFuncElement:	ColId Typename opt_collate_clause
11925 				{
11926 					ColumnDef *n = makeNode(ColumnDef);
11927 					n->colname = $1;
11928 					n->typeName = $2;
11929 					n->inhcount = 0;
11930 					n->is_local = true;
11931 					n->is_not_null = false;
11932 					n->is_from_type = false;
11933 					n->is_from_parent = false;
11934 					n->storage = 0;
11935 					n->raw_default = NULL;
11936 					n->cooked_default = NULL;
11937 					n->collClause = (CollateClause *) $3;
11938 					n->collOid = InvalidOid;
11939 					n->constraints = NIL;
11940 					n->location = @1;
11941 					$$ = (Node *)n;
11942 				}
11943 		;
11944 
11945 /*
11946  * XMLTABLE
11947  */
11948 xmltable:
11949 			XMLTABLE '(' c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
11950 				{
11951 					RangeTableFunc *n = makeNode(RangeTableFunc);
11952 					n->rowexpr = $3;
11953 					n->docexpr = $4;
11954 					n->columns = $6;
11955 					n->namespaces = NIL;
11956 					n->location = @1;
11957 					$$ = (Node *)n;
11958 				}
11959 			| XMLTABLE '(' XMLNAMESPACES '(' xml_namespace_list ')' ','
11960 				c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
11961 				{
11962 					RangeTableFunc *n = makeNode(RangeTableFunc);
11963 					n->rowexpr = $8;
11964 					n->docexpr = $9;
11965 					n->columns = $11;
11966 					n->namespaces = $5;
11967 					n->location = @1;
11968 					$$ = (Node *)n;
11969 				}
11970 		;
11971 
11972 xmltable_column_list: xmltable_column_el					{ $$ = list_make1($1); }
11973 			| xmltable_column_list ',' xmltable_column_el	{ $$ = lappend($1, $3); }
11974 		;
11975 
11976 xmltable_column_el:
11977 			ColId Typename
11978 				{
11979 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
11980 
11981 					fc->colname = $1;
11982 					fc->for_ordinality = false;
11983 					fc->typeName = $2;
11984 					fc->is_not_null = false;
11985 					fc->colexpr = NULL;
11986 					fc->coldefexpr = NULL;
11987 					fc->location = @1;
11988 
11989 					$$ = (Node *) fc;
11990 				}
11991 			| ColId Typename xmltable_column_option_list
11992 				{
11993 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
11994 					ListCell		   *option;
11995 					bool				nullability_seen = false;
11996 
11997 					fc->colname = $1;
11998 					fc->typeName = $2;
11999 					fc->for_ordinality = false;
12000 					fc->is_not_null = false;
12001 					fc->colexpr = NULL;
12002 					fc->coldefexpr = NULL;
12003 					fc->location = @1;
12004 
foreach(option,$3)12005 					foreach(option, $3)
12006 					{
12007 						DefElem   *defel = (DefElem *) lfirst(option);
12008 
12009 						if (strcmp(defel->defname, "default") == 0)
12010 						{
12011 							if (fc->coldefexpr != NULL)
12012 								ereport(ERROR,
12013 										(errcode(ERRCODE_SYNTAX_ERROR),
12014 										 errmsg("only one DEFAULT value is allowed"),
12015 										 parser_errposition(defel->location)));
12016 							fc->coldefexpr = defel->arg;
12017 						}
12018 						else if (strcmp(defel->defname, "path") == 0)
12019 						{
12020 							if (fc->colexpr != NULL)
12021 								ereport(ERROR,
12022 										(errcode(ERRCODE_SYNTAX_ERROR),
12023 										 errmsg("only one PATH value per column is allowed"),
12024 										 parser_errposition(defel->location)));
12025 							fc->colexpr = defel->arg;
12026 						}
12027 						else if (strcmp(defel->defname, "is_not_null") == 0)
12028 						{
12029 							if (nullability_seen)
12030 								ereport(ERROR,
12031 										(errcode(ERRCODE_SYNTAX_ERROR),
12032 										 errmsg("conflicting or redundant NULL / NOT NULL declarations for column \"%s\"", fc->colname),
12033 										 parser_errposition(defel->location)));
12034 							fc->is_not_null = intVal(defel->arg);
12035 							nullability_seen = true;
12036 						}
12037 						else
12038 						{
12039 							ereport(ERROR,
12040 									(errcode(ERRCODE_SYNTAX_ERROR),
12041 									 errmsg("unrecognized column option \"%s\"",
12042 											defel->defname),
12043 									 parser_errposition(defel->location)));
12044 						}
12045 					}
12046 					$$ = (Node *) fc;
12047 				}
12048 			| ColId FOR ORDINALITY
12049 				{
12050 					RangeTableFuncCol	   *fc = makeNode(RangeTableFuncCol);
12051 
12052 					fc->colname = $1;
12053 					fc->for_ordinality = true;
12054 					/* other fields are ignored, initialized by makeNode */
12055 					fc->location = @1;
12056 
12057 					$$ = (Node *) fc;
12058 				}
12059 		;
12060 
12061 xmltable_column_option_list:
12062 			xmltable_column_option_el
12063 				{ $$ = list_make1($1); }
12064 			| xmltable_column_option_list xmltable_column_option_el
12065 				{ $$ = lappend($1, $2); }
12066 		;
12067 
12068 xmltable_column_option_el:
12069 			IDENT b_expr
12070 				{ $$ = makeDefElem($1, $2, @1); }
12071 			| DEFAULT b_expr
12072 				{ $$ = makeDefElem("default", $2, @1); }
12073 			| NOT NULL_P
12074 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(true), @1); }
12075 			| NULL_P
12076 				{ $$ = makeDefElem("is_not_null", (Node *) makeInteger(false), @1); }
12077 		;
12078 
12079 xml_namespace_list:
12080 			xml_namespace_el
12081 				{ $$ = list_make1($1); }
12082 			| xml_namespace_list ',' xml_namespace_el
12083 				{ $$ = lappend($1, $3); }
12084 		;
12085 
12086 xml_namespace_el:
12087 			b_expr AS ColLabel
12088 				{
12089 					$$ = makeNode(ResTarget);
12090 					$$->name = $3;
12091 					$$->indirection = NIL;
12092 					$$->val = $1;
12093 					$$->location = @1;
12094 				}
12095 			| DEFAULT b_expr
12096 				{
12097 					$$ = makeNode(ResTarget);
12098 					$$->name = NULL;
12099 					$$->indirection = NIL;
12100 					$$->val = $2;
12101 					$$->location = @1;
12102 				}
12103 		;
12104 
12105 /*****************************************************************************
12106  *
12107  *	Type syntax
12108  *		SQL introduces a large amount of type-specific syntax.
12109  *		Define individual clauses to handle these cases, and use
12110  *		 the generic case to handle regular type-extensible Postgres syntax.
12111  *		- thomas 1997-10-10
12112  *
12113  *****************************************************************************/
12114 
12115 Typename:	SimpleTypename opt_array_bounds
12116 				{
12117 					$$ = $1;
12118 					$$->arrayBounds = $2;
12119 				}
12120 			| SETOF SimpleTypename opt_array_bounds
12121 				{
12122 					$$ = $2;
12123 					$$->arrayBounds = $3;
12124 					$$->setof = TRUE;
12125 				}
12126 			/* SQL standard syntax, currently only one-dimensional */
12127 			| SimpleTypename ARRAY '[' Iconst ']'
12128 				{
12129 					$$ = $1;
12130 					$$->arrayBounds = list_make1(makeInteger($4));
12131 				}
12132 			| SETOF SimpleTypename ARRAY '[' Iconst ']'
12133 				{
12134 					$$ = $2;
12135 					$$->arrayBounds = list_make1(makeInteger($5));
12136 					$$->setof = TRUE;
12137 				}
12138 			| SimpleTypename ARRAY
12139 				{
12140 					$$ = $1;
12141 					$$->arrayBounds = list_make1(makeInteger(-1));
12142 				}
12143 			| SETOF SimpleTypename ARRAY
12144 				{
12145 					$$ = $2;
12146 					$$->arrayBounds = list_make1(makeInteger(-1));
12147 					$$->setof = TRUE;
12148 				}
12149 		;
12150 
12151 opt_array_bounds:
12152 			opt_array_bounds '[' ']'
12153 					{  $$ = lappend($1, makeInteger(-1)); }
12154 			| opt_array_bounds '[' Iconst ']'
12155 					{  $$ = lappend($1, makeInteger($3)); }
12156 			| /*EMPTY*/
12157 					{  $$ = NIL; }
12158 		;
12159 
12160 SimpleTypename:
12161 			GenericType								{ $$ = $1; }
12162 			| Numeric								{ $$ = $1; }
12163 			| Bit									{ $$ = $1; }
12164 			| Character								{ $$ = $1; }
12165 			| ConstDatetime							{ $$ = $1; }
12166 			| ConstInterval opt_interval
12167 				{
12168 					$$ = $1;
12169 					$$->typmods = $2;
12170 				}
12171 			| ConstInterval '(' Iconst ')'
12172 				{
12173 					$$ = $1;
12174 					$$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
12175 											 makeIntConst($3, @3));
12176 				}
12177 		;
12178 
12179 /* We have a separate ConstTypename to allow defaulting fixed-length
12180  * types such as CHAR() and BIT() to an unspecified length.
12181  * SQL9x requires that these default to a length of one, but this
12182  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
12183  * where there is an obvious better choice to make.
12184  * Note that ConstInterval is not included here since it must
12185  * be pushed up higher in the rules to accommodate the postfix
12186  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
12187  * the generic-type-name case in AExprConst to avoid premature
12188  * reduce/reduce conflicts against function names.
12189  */
12190 ConstTypename:
12191 			Numeric									{ $$ = $1; }
12192 			| ConstBit								{ $$ = $1; }
12193 			| ConstCharacter						{ $$ = $1; }
12194 			| ConstDatetime							{ $$ = $1; }
12195 		;
12196 
12197 /*
12198  * GenericType covers all type names that don't have special syntax mandated
12199  * by the standard, including qualified names.  We also allow type modifiers.
12200  * To avoid parsing conflicts against function invocations, the modifiers
12201  * have to be shown as expr_list here, but parse analysis will only accept
12202  * constants for them.
12203  */
12204 GenericType:
12205 			type_function_name opt_type_modifiers
12206 				{
12207 					$$ = makeTypeName($1);
12208 					$$->typmods = $2;
12209 					$$->location = @1;
12210 				}
12211 			| type_function_name attrs opt_type_modifiers
12212 				{
12213 					$$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
12214 					$$->typmods = $3;
12215 					$$->location = @1;
12216 				}
12217 		;
12218 
12219 opt_type_modifiers: '(' expr_list ')'				{ $$ = $2; }
12220 					| /* EMPTY */					{ $$ = NIL; }
12221 		;
12222 
12223 /*
12224  * SQL numeric data types
12225  */
12226 Numeric:	INT_P
12227 				{
12228 					$$ = SystemTypeName("int4");
12229 					$$->location = @1;
12230 				}
12231 			| INTEGER
12232 				{
12233 					$$ = SystemTypeName("int4");
12234 					$$->location = @1;
12235 				}
12236 			| SMALLINT
12237 				{
12238 					$$ = SystemTypeName("int2");
12239 					$$->location = @1;
12240 				}
12241 			| BIGINT
12242 				{
12243 					$$ = SystemTypeName("int8");
12244 					$$->location = @1;
12245 				}
12246 			| REAL
12247 				{
12248 					$$ = SystemTypeName("float4");
12249 					$$->location = @1;
12250 				}
12251 			| FLOAT_P opt_float
12252 				{
12253 					$$ = $2;
12254 					$$->location = @1;
12255 				}
12256 			| DOUBLE_P PRECISION
12257 				{
12258 					$$ = SystemTypeName("float8");
12259 					$$->location = @1;
12260 				}
12261 			| DECIMAL_P opt_type_modifiers
12262 				{
12263 					$$ = SystemTypeName("numeric");
12264 					$$->typmods = $2;
12265 					$$->location = @1;
12266 				}
12267 			| DEC opt_type_modifiers
12268 				{
12269 					$$ = SystemTypeName("numeric");
12270 					$$->typmods = $2;
12271 					$$->location = @1;
12272 				}
12273 			| NUMERIC opt_type_modifiers
12274 				{
12275 					$$ = SystemTypeName("numeric");
12276 					$$->typmods = $2;
12277 					$$->location = @1;
12278 				}
12279 			| BOOLEAN_P
12280 				{
12281 					$$ = SystemTypeName("bool");
12282 					$$->location = @1;
12283 				}
12284 		;
12285 
12286 opt_float:	'(' Iconst ')'
12287 				{
12288 					/*
12289 					 * Check FLOAT() precision limits assuming IEEE floating
12290 					 * types - thomas 1997-09-18
12291 					 */
12292 					if ($2 < 1)
12293 						ereport(ERROR,
12294 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12295 								 errmsg("precision for type float must be at least 1 bit"),
12296 								 parser_errposition(@2)));
12297 					else if ($2 <= 24)
12298 						$$ = SystemTypeName("float4");
12299 					else if ($2 <= 53)
12300 						$$ = SystemTypeName("float8");
12301 					else
12302 						ereport(ERROR,
12303 								(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12304 								 errmsg("precision for type float must be less than 54 bits"),
12305 								 parser_errposition(@2)));
12306 				}
12307 			| /*EMPTY*/
12308 				{
12309 					$$ = SystemTypeName("float8");
12310 				}
12311 		;
12312 
12313 /*
12314  * SQL bit-field data types
12315  * The following implements BIT() and BIT VARYING().
12316  */
12317 Bit:		BitWithLength
12318 				{
12319 					$$ = $1;
12320 				}
12321 			| BitWithoutLength
12322 				{
12323 					$$ = $1;
12324 				}
12325 		;
12326 
12327 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
12328 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
12329 ConstBit:	BitWithLength
12330 				{
12331 					$$ = $1;
12332 				}
12333 			| BitWithoutLength
12334 				{
12335 					$$ = $1;
12336 					$$->typmods = NIL;
12337 				}
12338 		;
12339 
12340 BitWithLength:
12341 			BIT opt_varying '(' expr_list ')'
12342 				{
12343 					char *typname;
12344 
12345 					typname = $2 ? "varbit" : "bit";
12346 					$$ = SystemTypeName(typname);
12347 					$$->typmods = $4;
12348 					$$->location = @1;
12349 				}
12350 		;
12351 
12352 BitWithoutLength:
12353 			BIT opt_varying
12354 				{
12355 					/* bit defaults to bit(1), varbit to no limit */
12356 					if ($2)
12357 					{
12358 						$$ = SystemTypeName("varbit");
12359 					}
12360 					else
12361 					{
12362 						$$ = SystemTypeName("bit");
12363 						$$->typmods = list_make1(makeIntConst(1, -1));
12364 					}
12365 					$$->location = @1;
12366 				}
12367 		;
12368 
12369 
12370 /*
12371  * SQL character data types
12372  * The following implements CHAR() and VARCHAR().
12373  */
12374 Character:  CharacterWithLength
12375 				{
12376 					$$ = $1;
12377 				}
12378 			| CharacterWithoutLength
12379 				{
12380 					$$ = $1;
12381 				}
12382 		;
12383 
12384 ConstCharacter:  CharacterWithLength
12385 				{
12386 					$$ = $1;
12387 				}
12388 			| CharacterWithoutLength
12389 				{
12390 					/* Length was not specified so allow to be unrestricted.
12391 					 * This handles problems with fixed-length (bpchar) strings
12392 					 * which in column definitions must default to a length
12393 					 * of one, but should not be constrained if the length
12394 					 * was not specified.
12395 					 */
12396 					$$ = $1;
12397 					$$->typmods = NIL;
12398 				}
12399 		;
12400 
12401 CharacterWithLength:  character '(' Iconst ')'
12402 				{
12403 					$$ = SystemTypeName($1);
12404 					$$->typmods = list_make1(makeIntConst($3, @3));
12405 					$$->location = @1;
12406 				}
12407 		;
12408 
12409 CharacterWithoutLength:	 character
12410 				{
12411 					$$ = SystemTypeName($1);
12412 					/* char defaults to char(1), varchar to no limit */
12413 					if (strcmp($1, "bpchar") == 0)
12414 						$$->typmods = list_make1(makeIntConst(1, -1));
12415 					$$->location = @1;
12416 				}
12417 		;
12418 
12419 character:	CHARACTER opt_varying
12420 										{ $$ = $2 ? "varchar": "bpchar"; }
12421 			| CHAR_P opt_varying
12422 										{ $$ = $2 ? "varchar": "bpchar"; }
12423 			| VARCHAR
12424 										{ $$ = "varchar"; }
12425 			| NATIONAL CHARACTER opt_varying
12426 										{ $$ = $3 ? "varchar": "bpchar"; }
12427 			| NATIONAL CHAR_P opt_varying
12428 										{ $$ = $3 ? "varchar": "bpchar"; }
12429 			| NCHAR opt_varying
12430 										{ $$ = $2 ? "varchar": "bpchar"; }
12431 		;
12432 
12433 opt_varying:
12434 			VARYING									{ $$ = TRUE; }
12435 			| /*EMPTY*/								{ $$ = FALSE; }
12436 		;
12437 
12438 /*
12439  * SQL date/time types
12440  */
12441 ConstDatetime:
12442 			TIMESTAMP '(' Iconst ')' opt_timezone
12443 				{
12444 					if ($5)
12445 						$$ = SystemTypeName("timestamptz");
12446 					else
12447 						$$ = SystemTypeName("timestamp");
12448 					$$->typmods = list_make1(makeIntConst($3, @3));
12449 					$$->location = @1;
12450 				}
12451 			| TIMESTAMP opt_timezone
12452 				{
12453 					if ($2)
12454 						$$ = SystemTypeName("timestamptz");
12455 					else
12456 						$$ = SystemTypeName("timestamp");
12457 					$$->location = @1;
12458 				}
12459 			| TIME '(' Iconst ')' opt_timezone
12460 				{
12461 					if ($5)
12462 						$$ = SystemTypeName("timetz");
12463 					else
12464 						$$ = SystemTypeName("time");
12465 					$$->typmods = list_make1(makeIntConst($3, @3));
12466 					$$->location = @1;
12467 				}
12468 			| TIME opt_timezone
12469 				{
12470 					if ($2)
12471 						$$ = SystemTypeName("timetz");
12472 					else
12473 						$$ = SystemTypeName("time");
12474 					$$->location = @1;
12475 				}
12476 		;
12477 
12478 ConstInterval:
12479 			INTERVAL
12480 				{
12481 					$$ = SystemTypeName("interval");
12482 					$$->location = @1;
12483 				}
12484 		;
12485 
12486 opt_timezone:
12487 			WITH_LA TIME ZONE						{ $$ = TRUE; }
12488 			| WITHOUT TIME ZONE						{ $$ = FALSE; }
12489 			| /*EMPTY*/								{ $$ = FALSE; }
12490 		;
12491 
12492 opt_interval:
12493 			YEAR_P
12494 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
12495 			| MONTH_P
12496 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
12497 			| DAY_P
12498 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
12499 			| HOUR_P
12500 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
12501 			| MINUTE_P
12502 				{ $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
12503 			| interval_second
12504 				{ $$ = $1; }
12505 			| YEAR_P TO MONTH_P
12506 				{
12507 					$$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
12508 												 INTERVAL_MASK(MONTH), @1));
12509 				}
12510 			| DAY_P TO HOUR_P
12511 				{
12512 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12513 												 INTERVAL_MASK(HOUR), @1));
12514 				}
12515 			| DAY_P TO MINUTE_P
12516 				{
12517 					$$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12518 												 INTERVAL_MASK(HOUR) |
12519 												 INTERVAL_MASK(MINUTE), @1));
12520 				}
12521 			| DAY_P TO interval_second
12522 				{
12523 					$$ = $3;
12524 					linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
12525 												INTERVAL_MASK(HOUR) |
12526 												INTERVAL_MASK(MINUTE) |
12527 												INTERVAL_MASK(SECOND), @1);
12528 				}
12529 			| HOUR_P TO MINUTE_P
12530 				{
12531 					$$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
12532 												 INTERVAL_MASK(MINUTE), @1));
12533 				}
12534 			| HOUR_P TO interval_second
12535 				{
12536 					$$ = $3;
12537 					linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
12538 												INTERVAL_MASK(MINUTE) |
12539 												INTERVAL_MASK(SECOND), @1);
12540 				}
12541 			| MINUTE_P TO interval_second
12542 				{
12543 					$$ = $3;
12544 					linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
12545 												INTERVAL_MASK(SECOND), @1);
12546 				}
12547 			| /*EMPTY*/
12548 				{ $$ = NIL; }
12549 		;
12550 
12551 interval_second:
12552 			SECOND_P
12553 				{
12554 					$$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
12555 				}
12556 			| SECOND_P '(' Iconst ')'
12557 				{
12558 					$$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
12559 									makeIntConst($3, @3));
12560 				}
12561 		;
12562 
12563 
12564 /*****************************************************************************
12565  *
12566  *	expression grammar
12567  *
12568  *****************************************************************************/
12569 
12570 /*
12571  * General expressions
12572  * This is the heart of the expression syntax.
12573  *
12574  * We have two expression types: a_expr is the unrestricted kind, and
12575  * b_expr is a subset that must be used in some places to avoid shift/reduce
12576  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
12577  * because that use of AND conflicts with AND as a boolean operator.  So,
12578  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
12579  *
12580  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
12581  * always be used by surrounding it with parens.
12582  *
12583  * c_expr is all the productions that are common to a_expr and b_expr;
12584  * it's factored out just to eliminate redundant coding.
12585  *
12586  * Be careful of productions involving more than one terminal token.
12587  * By default, bison will assign such productions the precedence of their
12588  * last terminal, but in nearly all cases you want it to be the precedence
12589  * of the first terminal instead; otherwise you will not get the behavior
12590  * you expect!  So we use %prec annotations freely to set precedences.
12591  */
12592 a_expr:		c_expr									{ $$ = $1; }
12593 			| a_expr TYPECAST Typename
12594 					{ $$ = makeTypeCast($1, $3, @2); }
12595 			| a_expr COLLATE any_name
12596 				{
12597 					CollateClause *n = makeNode(CollateClause);
12598 					n->arg = $1;
12599 					n->collname = $3;
12600 					n->location = @2;
12601 					$$ = (Node *) n;
12602 				}
12603 			| a_expr AT TIME ZONE a_expr			%prec AT
12604 				{
12605 					$$ = (Node *) makeFuncCall(SystemFuncName("timezone"),
12606 											   list_make2($5, $1),
12607 											   @2);
12608 				}
12609 		/*
12610 		 * These operators must be called out explicitly in order to make use
12611 		 * of bison's automatic operator-precedence handling.  All other
12612 		 * operator names are handled by the generic productions using "Op",
12613 		 * below; and all those operators will have the same precedence.
12614 		 *
12615 		 * If you add more explicitly-known operators, be sure to add them
12616 		 * also to b_expr and to the MathOp list below.
12617 		 */
12618 			| '+' a_expr					%prec UMINUS
12619 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
12620 			| '-' a_expr					%prec UMINUS
12621 				{ $$ = doNegate($2, @1); }
12622 			| a_expr '+' a_expr
12623 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
12624 			| a_expr '-' a_expr
12625 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
12626 			| a_expr '*' a_expr
12627 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
12628 			| a_expr '/' a_expr
12629 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
12630 			| a_expr '%' a_expr
12631 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
12632 			| a_expr '^' a_expr
12633 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
12634 			| a_expr '<' a_expr
12635 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
12636 			| a_expr '>' a_expr
12637 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
12638 			| a_expr '=' a_expr
12639 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
12640 			| a_expr LESS_EQUALS a_expr
12641 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
12642 			| a_expr GREATER_EQUALS a_expr
12643 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
12644 			| a_expr NOT_EQUALS a_expr
12645 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
12646 
12647 			| a_expr qual_Op a_expr				%prec Op
12648 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
12649 			| qual_Op a_expr					%prec Op
12650 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
12651 			| a_expr qual_Op					%prec POSTFIXOP
12652 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
12653 
12654 			| a_expr AND a_expr
12655 				{ $$ = makeAndExpr($1, $3, @2); }
12656 			| a_expr OR a_expr
12657 				{ $$ = makeOrExpr($1, $3, @2); }
12658 			| NOT a_expr
12659 				{ $$ = makeNotExpr($2, @1); }
12660 			| NOT_LA a_expr						%prec NOT
12661 				{ $$ = makeNotExpr($2, @1); }
12662 
12663 			| a_expr LIKE a_expr
12664 				{
12665 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
12666 												   $1, $3, @2);
12667 				}
12668 			| a_expr LIKE a_expr ESCAPE a_expr					%prec LIKE
12669 				{
12670 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
12671 											   list_make2($3, $5),
12672 											   @2);
12673 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
12674 												   $1, (Node *) n, @2);
12675 				}
12676 			| a_expr NOT_LA LIKE a_expr							%prec NOT_LA
12677 				{
12678 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
12679 												   $1, $4, @2);
12680 				}
12681 			| a_expr NOT_LA LIKE a_expr ESCAPE a_expr			%prec NOT_LA
12682 				{
12683 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
12684 											   list_make2($4, $6),
12685 											   @2);
12686 					$$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
12687 												   $1, (Node *) n, @2);
12688 				}
12689 			| a_expr ILIKE a_expr
12690 				{
12691 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
12692 												   $1, $3, @2);
12693 				}
12694 			| a_expr ILIKE a_expr ESCAPE a_expr					%prec ILIKE
12695 				{
12696 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
12697 											   list_make2($3, $5),
12698 											   @2);
12699 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
12700 												   $1, (Node *) n, @2);
12701 				}
12702 			| a_expr NOT_LA ILIKE a_expr						%prec NOT_LA
12703 				{
12704 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
12705 												   $1, $4, @2);
12706 				}
12707 			| a_expr NOT_LA ILIKE a_expr ESCAPE a_expr			%prec NOT_LA
12708 				{
12709 					FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
12710 											   list_make2($4, $6),
12711 											   @2);
12712 					$$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
12713 												   $1, (Node *) n, @2);
12714 				}
12715 
12716 			| a_expr SIMILAR TO a_expr							%prec SIMILAR
12717 				{
12718 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
12719 											   list_make2($4, makeNullAConst(-1)),
12720 											   @2);
12721 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
12722 												   $1, (Node *) n, @2);
12723 				}
12724 			| a_expr SIMILAR TO a_expr ESCAPE a_expr			%prec SIMILAR
12725 				{
12726 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
12727 											   list_make2($4, $6),
12728 											   @2);
12729 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
12730 												   $1, (Node *) n, @2);
12731 				}
12732 			| a_expr NOT_LA SIMILAR TO a_expr					%prec NOT_LA
12733 				{
12734 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
12735 											   list_make2($5, makeNullAConst(-1)),
12736 											   @2);
12737 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
12738 												   $1, (Node *) n, @2);
12739 				}
12740 			| a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr		%prec NOT_LA
12741 				{
12742 					FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
12743 											   list_make2($5, $7),
12744 											   @2);
12745 					$$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
12746 												   $1, (Node *) n, @2);
12747 				}
12748 
12749 			/* NullTest clause
12750 			 * Define SQL-style Null test clause.
12751 			 * Allow two forms described in the standard:
12752 			 *	a IS NULL
12753 			 *	a IS NOT NULL
12754 			 * Allow two SQL extensions
12755 			 *	a ISNULL
12756 			 *	a NOTNULL
12757 			 */
12758 			| a_expr IS NULL_P							%prec IS
12759 				{
12760 					NullTest *n = makeNode(NullTest);
12761 					n->arg = (Expr *) $1;
12762 					n->nulltesttype = IS_NULL;
12763 					n->location = @2;
12764 					$$ = (Node *)n;
12765 				}
12766 			| a_expr ISNULL
12767 				{
12768 					NullTest *n = makeNode(NullTest);
12769 					n->arg = (Expr *) $1;
12770 					n->nulltesttype = IS_NULL;
12771 					n->location = @2;
12772 					$$ = (Node *)n;
12773 				}
12774 			| a_expr IS NOT NULL_P						%prec IS
12775 				{
12776 					NullTest *n = makeNode(NullTest);
12777 					n->arg = (Expr *) $1;
12778 					n->nulltesttype = IS_NOT_NULL;
12779 					n->location = @2;
12780 					$$ = (Node *)n;
12781 				}
12782 			| a_expr NOTNULL
12783 				{
12784 					NullTest *n = makeNode(NullTest);
12785 					n->arg = (Expr *) $1;
12786 					n->nulltesttype = IS_NOT_NULL;
12787 					n->location = @2;
12788 					$$ = (Node *)n;
12789 				}
12790 			| row OVERLAPS row
12791 				{
12792 					if (list_length($1) != 2)
12793 						ereport(ERROR,
12794 								(errcode(ERRCODE_SYNTAX_ERROR),
12795 								 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
12796 								 parser_errposition(@1)));
12797 					if (list_length($3) != 2)
12798 						ereport(ERROR,
12799 								(errcode(ERRCODE_SYNTAX_ERROR),
12800 								 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
12801 								 parser_errposition(@3)));
12802 					$$ = (Node *) makeFuncCall(SystemFuncName("overlaps"),
12803 											   list_concat($1, $3),
12804 											   @2);
12805 				}
12806 			| a_expr IS TRUE_P							%prec IS
12807 				{
12808 					BooleanTest *b = makeNode(BooleanTest);
12809 					b->arg = (Expr *) $1;
12810 					b->booltesttype = IS_TRUE;
12811 					b->location = @2;
12812 					$$ = (Node *)b;
12813 				}
12814 			| a_expr IS NOT TRUE_P						%prec IS
12815 				{
12816 					BooleanTest *b = makeNode(BooleanTest);
12817 					b->arg = (Expr *) $1;
12818 					b->booltesttype = IS_NOT_TRUE;
12819 					b->location = @2;
12820 					$$ = (Node *)b;
12821 				}
12822 			| a_expr IS FALSE_P							%prec IS
12823 				{
12824 					BooleanTest *b = makeNode(BooleanTest);
12825 					b->arg = (Expr *) $1;
12826 					b->booltesttype = IS_FALSE;
12827 					b->location = @2;
12828 					$$ = (Node *)b;
12829 				}
12830 			| a_expr IS NOT FALSE_P						%prec IS
12831 				{
12832 					BooleanTest *b = makeNode(BooleanTest);
12833 					b->arg = (Expr *) $1;
12834 					b->booltesttype = IS_NOT_FALSE;
12835 					b->location = @2;
12836 					$$ = (Node *)b;
12837 				}
12838 			| a_expr IS UNKNOWN							%prec IS
12839 				{
12840 					BooleanTest *b = makeNode(BooleanTest);
12841 					b->arg = (Expr *) $1;
12842 					b->booltesttype = IS_UNKNOWN;
12843 					b->location = @2;
12844 					$$ = (Node *)b;
12845 				}
12846 			| a_expr IS NOT UNKNOWN						%prec IS
12847 				{
12848 					BooleanTest *b = makeNode(BooleanTest);
12849 					b->arg = (Expr *) $1;
12850 					b->booltesttype = IS_NOT_UNKNOWN;
12851 					b->location = @2;
12852 					$$ = (Node *)b;
12853 				}
12854 			| a_expr IS DISTINCT FROM a_expr			%prec IS
12855 				{
12856 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
12857 				}
12858 			| a_expr IS NOT DISTINCT FROM a_expr		%prec IS
12859 				{
12860                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
12861 				}
12862 			| a_expr IS OF '(' type_list ')'			%prec IS
12863 				{
12864 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
12865 				}
12866 			| a_expr IS NOT OF '(' type_list ')'		%prec IS
12867 				{
12868 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
12869 				}
12870 			| a_expr BETWEEN opt_asymmetric b_expr AND a_expr		%prec BETWEEN
12871 				{
12872 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN,
12873 												   "BETWEEN",
12874 												   $1,
12875 												   (Node *) list_make2($4, $6),
12876 												   @2);
12877 				}
12878 			| a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr %prec NOT_LA
12879 				{
12880 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN,
12881 												   "NOT BETWEEN",
12882 												   $1,
12883 												   (Node *) list_make2($5, $7),
12884 												   @2);
12885 				}
12886 			| a_expr BETWEEN SYMMETRIC b_expr AND a_expr			%prec BETWEEN
12887 				{
12888 					$$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN_SYM,
12889 												   "BETWEEN SYMMETRIC",
12890 												   $1,
12891 												   (Node *) list_make2($4, $6),
12892 												   @2);
12893 				}
12894 			| a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr		%prec NOT_LA
12895 				{
12896 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN_SYM,
12897 												   "NOT BETWEEN SYMMETRIC",
12898 												   $1,
12899 												   (Node *) list_make2($5, $7),
12900 												   @2);
12901 				}
12902 			| a_expr IN_P in_expr
12903 				{
12904 					/* in_expr returns a SubLink or a list of a_exprs */
12905 					if (IsA($3, SubLink))
12906 					{
12907 						/* generate foo = ANY (subquery) */
12908 						SubLink *n = (SubLink *) $3;
12909 						n->subLinkType = ANY_SUBLINK;
12910 						n->subLinkId = 0;
12911 						n->testexpr = $1;
12912 						n->operName = NIL;		/* show it's IN not = ANY */
12913 						n->location = @2;
12914 						$$ = (Node *)n;
12915 					}
12916 					else
12917 					{
12918 						/* generate scalar IN expression */
12919 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
12920 					}
12921 				}
12922 			| a_expr NOT_LA IN_P in_expr						%prec NOT_LA
12923 				{
12924 					/* in_expr returns a SubLink or a list of a_exprs */
12925 					if (IsA($4, SubLink))
12926 					{
12927 						/* generate NOT (foo = ANY (subquery)) */
12928 						/* Make an = ANY node */
12929 						SubLink *n = (SubLink *) $4;
12930 						n->subLinkType = ANY_SUBLINK;
12931 						n->subLinkId = 0;
12932 						n->testexpr = $1;
12933 						n->operName = NIL;		/* show it's IN not = ANY */
12934 						n->location = @2;
12935 						/* Stick a NOT on top; must have same parse location */
12936 						$$ = makeNotExpr((Node *) n, @2);
12937 					}
12938 					else
12939 					{
12940 						/* generate scalar NOT IN expression */
12941 						$$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
12942 					}
12943 				}
12944 			| a_expr subquery_Op sub_type select_with_parens	%prec Op
12945 				{
12946 					SubLink *n = makeNode(SubLink);
12947 					n->subLinkType = $3;
12948 					n->subLinkId = 0;
12949 					n->testexpr = $1;
12950 					n->operName = $2;
12951 					n->subselect = $4;
12952 					n->location = @2;
12953 					$$ = (Node *)n;
12954 				}
12955 			| a_expr subquery_Op sub_type '(' a_expr ')'		%prec Op
12956 				{
12957 					if ($3 == ANY_SUBLINK)
12958 						$$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
12959 					else
12960 						$$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
12961 				}
12962 			| UNIQUE select_with_parens
12963 				{
12964 					/* Not sure how to get rid of the parentheses
12965 					 * but there are lots of shift/reduce errors without them.
12966 					 *
12967 					 * Should be able to implement this by plopping the entire
12968 					 * select into a node, then transforming the target expressions
12969 					 * from whatever they are into count(*), and testing the
12970 					 * entire result equal to one.
12971 					 * But, will probably implement a separate node in the executor.
12972 					 */
12973 					ereport(ERROR,
12974 							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
12975 							 errmsg("UNIQUE predicate is not yet implemented"),
12976 							 parser_errposition(@1)));
12977 				}
12978 			| a_expr IS DOCUMENT_P					%prec IS
12979 				{
12980 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12981 									 list_make1($1), @2);
12982 				}
12983 			| a_expr IS NOT DOCUMENT_P				%prec IS
12984 				{
12985 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
12986 												 list_make1($1), @2),
12987 									 @2);
12988 				}
12989 			| DEFAULT
12990 				{
12991 					/*
12992 					 * The SQL spec only allows DEFAULT in "contextually typed
12993 					 * expressions", but for us, it's easier to allow it in
12994 					 * any a_expr and then throw error during parse analysis
12995 					 * if it's in an inappropriate context.  This way also
12996 					 * lets us say something smarter than "syntax error".
12997 					 */
12998 					SetToDefault *n = makeNode(SetToDefault);
12999 					/* parse analysis will fill in the rest */
13000 					n->location = @1;
13001 					$$ = (Node *)n;
13002 				}
13003 		;
13004 
13005 /*
13006  * Restricted expressions
13007  *
13008  * b_expr is a subset of the complete expression syntax defined by a_expr.
13009  *
13010  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
13011  * cause trouble in the places where b_expr is used.  For simplicity, we
13012  * just eliminate all the boolean-keyword-operator productions from b_expr.
13013  */
13014 b_expr:		c_expr
13015 				{ $$ = $1; }
13016 			| b_expr TYPECAST Typename
13017 				{ $$ = makeTypeCast($1, $3, @2); }
13018 			| '+' b_expr					%prec UMINUS
13019 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13020 			| '-' b_expr					%prec UMINUS
13021 				{ $$ = doNegate($2, @1); }
13022 			| b_expr '+' b_expr
13023 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13024 			| b_expr '-' b_expr
13025 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13026 			| b_expr '*' b_expr
13027 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13028 			| b_expr '/' b_expr
13029 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13030 			| b_expr '%' b_expr
13031 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13032 			| b_expr '^' b_expr
13033 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13034 			| b_expr '<' b_expr
13035 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13036 			| b_expr '>' b_expr
13037 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13038 			| b_expr '=' b_expr
13039 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13040 			| b_expr LESS_EQUALS b_expr
13041 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13042 			| b_expr GREATER_EQUALS b_expr
13043 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13044 			| b_expr NOT_EQUALS b_expr
13045 				{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13046 			| b_expr qual_Op b_expr				%prec Op
13047 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13048 			| qual_Op b_expr					%prec Op
13049 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13050 			| b_expr qual_Op					%prec POSTFIXOP
13051 				{ $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13052 			| b_expr IS DISTINCT FROM b_expr		%prec IS
13053 				{
13054 					$$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13055 				}
13056 			| b_expr IS NOT DISTINCT FROM b_expr	%prec IS
13057 				{
13058                     $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13059 				}
13060 			| b_expr IS OF '(' type_list ')'		%prec IS
13061 				{
13062 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13063 				}
13064 			| b_expr IS NOT OF '(' type_list ')'	%prec IS
13065 				{
13066 					$$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13067 				}
13068 			| b_expr IS DOCUMENT_P					%prec IS
13069 				{
13070 					$$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13071 									 list_make1($1), @2);
13072 				}
13073 			| b_expr IS NOT DOCUMENT_P				%prec IS
13074 				{
13075 					$$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13076 												 list_make1($1), @2),
13077 									 @2);
13078 				}
13079 		;
13080 
13081 /*
13082  * Productions that can be used in both a_expr and b_expr.
13083  *
13084  * Note: productions that refer recursively to a_expr or b_expr mostly
13085  * cannot appear here.	However, it's OK to refer to a_exprs that occur
13086  * inside parentheses, such as function arguments; that cannot introduce
13087  * ambiguity to the b_expr syntax.
13088  */
13089 c_expr:		columnref								{ $$ = $1; }
13090 			| AexprConst							{ $$ = $1; }
13091 			| PARAM opt_indirection
13092 				{
13093 					ParamRef *p = makeNode(ParamRef);
13094 					p->number = $1;
13095 					p->location = @1;
13096 					if ($2)
13097 					{
13098 						A_Indirection *n = makeNode(A_Indirection);
13099 						n->arg = (Node *) p;
13100 						n->indirection = check_indirection($2, yyscanner);
13101 						$$ = (Node *) n;
13102 					}
13103 					else
13104 						$$ = (Node *) p;
13105 				}
13106 			| '(' a_expr ')' opt_indirection
13107 				{
13108 					if ($4)
13109 					{
13110 						A_Indirection *n = makeNode(A_Indirection);
13111 						n->arg = $2;
13112 						n->indirection = check_indirection($4, yyscanner);
13113 						$$ = (Node *)n;
13114 					}
13115 					else if (operator_precedence_warning)
13116 					{
13117 						/*
13118 						 * If precedence warnings are enabled, insert
13119 						 * AEXPR_PAREN nodes wrapping all explicitly
13120 						 * parenthesized subexpressions; this prevents bogus
13121 						 * warnings from being issued when the ordering has
13122 						 * been forced by parentheses.  Take care that an
13123 						 * AEXPR_PAREN node has the same exprLocation as its
13124 						 * child, so as not to cause surprising changes in
13125 						 * error cursor positioning.
13126 						 *
13127 						 * In principle we should not be relying on a GUC to
13128 						 * decide whether to insert AEXPR_PAREN nodes.
13129 						 * However, since they have no effect except to
13130 						 * suppress warnings, it's probably safe enough; and
13131 						 * we'd just as soon not waste cycles on dummy parse
13132 						 * nodes if we don't have to.
13133 						 */
13134 						$$ = (Node *) makeA_Expr(AEXPR_PAREN, NIL, $2, NULL,
13135 												 exprLocation($2));
13136 					}
13137 					else
13138 						$$ = $2;
13139 				}
13140 			| case_expr
13141 				{ $$ = $1; }
13142 			| func_expr
13143 				{ $$ = $1; }
13144 			| select_with_parens			%prec UMINUS
13145 				{
13146 					SubLink *n = makeNode(SubLink);
13147 					n->subLinkType = EXPR_SUBLINK;
13148 					n->subLinkId = 0;
13149 					n->testexpr = NULL;
13150 					n->operName = NIL;
13151 					n->subselect = $1;
13152 					n->location = @1;
13153 					$$ = (Node *)n;
13154 				}
13155 			| select_with_parens indirection
13156 				{
13157 					/*
13158 					 * Because the select_with_parens nonterminal is designed
13159 					 * to "eat" as many levels of parens as possible, the
13160 					 * '(' a_expr ')' opt_indirection production above will
13161 					 * fail to match a sub-SELECT with indirection decoration;
13162 					 * the sub-SELECT won't be regarded as an a_expr as long
13163 					 * as there are parens around it.  To support applying
13164 					 * subscripting or field selection to a sub-SELECT result,
13165 					 * we need this redundant-looking production.
13166 					 */
13167 					SubLink *n = makeNode(SubLink);
13168 					A_Indirection *a = makeNode(A_Indirection);
13169 					n->subLinkType = EXPR_SUBLINK;
13170 					n->subLinkId = 0;
13171 					n->testexpr = NULL;
13172 					n->operName = NIL;
13173 					n->subselect = $1;
13174 					n->location = @1;
13175 					a->arg = (Node *)n;
13176 					a->indirection = check_indirection($2, yyscanner);
13177 					$$ = (Node *)a;
13178 				}
13179 			| EXISTS select_with_parens
13180 				{
13181 					SubLink *n = makeNode(SubLink);
13182 					n->subLinkType = EXISTS_SUBLINK;
13183 					n->subLinkId = 0;
13184 					n->testexpr = NULL;
13185 					n->operName = NIL;
13186 					n->subselect = $2;
13187 					n->location = @1;
13188 					$$ = (Node *)n;
13189 				}
13190 			| ARRAY select_with_parens
13191 				{
13192 					SubLink *n = makeNode(SubLink);
13193 					n->subLinkType = ARRAY_SUBLINK;
13194 					n->subLinkId = 0;
13195 					n->testexpr = NULL;
13196 					n->operName = NIL;
13197 					n->subselect = $2;
13198 					n->location = @1;
13199 					$$ = (Node *)n;
13200 				}
13201 			| ARRAY array_expr
13202 				{
13203 					A_ArrayExpr *n = castNode(A_ArrayExpr, $2);
13204 					/* point outermost A_ArrayExpr to the ARRAY keyword */
13205 					n->location = @1;
13206 					$$ = (Node *)n;
13207 				}
13208 			| explicit_row
13209 				{
13210 					RowExpr *r = makeNode(RowExpr);
13211 					r->args = $1;
13212 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13213 					r->colnames = NIL;	/* to be filled in during analysis */
13214 					r->row_format = COERCE_EXPLICIT_CALL; /* abuse */
13215 					r->location = @1;
13216 					$$ = (Node *)r;
13217 				}
13218 			| implicit_row
13219 				{
13220 					RowExpr *r = makeNode(RowExpr);
13221 					r->args = $1;
13222 					r->row_typeid = InvalidOid;	/* not analyzed yet */
13223 					r->colnames = NIL;	/* to be filled in during analysis */
13224 					r->row_format = COERCE_IMPLICIT_CAST; /* abuse */
13225 					r->location = @1;
13226 					$$ = (Node *)r;
13227 				}
13228 			| GROUPING '(' expr_list ')'
13229 			  {
13230 				  GroupingFunc *g = makeNode(GroupingFunc);
13231 				  g->args = $3;
13232 				  g->location = @1;
13233 				  $$ = (Node *)g;
13234 			  }
13235 		;
13236 
13237 func_application: func_name '(' ')'
13238 				{
13239 					$$ = (Node *) makeFuncCall($1, NIL, @1);
13240 				}
13241 			| func_name '(' func_arg_list opt_sort_clause ')'
13242 				{
13243 					FuncCall *n = makeFuncCall($1, $3, @1);
13244 					n->agg_order = $4;
13245 					$$ = (Node *)n;
13246 				}
13247 			| func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'
13248 				{
13249 					FuncCall *n = makeFuncCall($1, list_make1($4), @1);
13250 					n->func_variadic = TRUE;
13251 					n->agg_order = $5;
13252 					$$ = (Node *)n;
13253 				}
13254 			| func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'
13255 				{
13256 					FuncCall *n = makeFuncCall($1, lappend($3, $6), @1);
13257 					n->func_variadic = TRUE;
13258 					n->agg_order = $7;
13259 					$$ = (Node *)n;
13260 				}
13261 			| func_name '(' ALL func_arg_list opt_sort_clause ')'
13262 				{
13263 					FuncCall *n = makeFuncCall($1, $4, @1);
13264 					n->agg_order = $5;
13265 					/* Ideally we'd mark the FuncCall node to indicate
13266 					 * "must be an aggregate", but there's no provision
13267 					 * for that in FuncCall at the moment.
13268 					 */
13269 					$$ = (Node *)n;
13270 				}
13271 			| func_name '(' DISTINCT func_arg_list opt_sort_clause ')'
13272 				{
13273 					FuncCall *n = makeFuncCall($1, $4, @1);
13274 					n->agg_order = $5;
13275 					n->agg_distinct = TRUE;
13276 					$$ = (Node *)n;
13277 				}
13278 			| func_name '(' '*' ')'
13279 				{
13280 					/*
13281 					 * We consider AGGREGATE(*) to invoke a parameterless
13282 					 * aggregate.  This does the right thing for COUNT(*),
13283 					 * and there are no other aggregates in SQL that accept
13284 					 * '*' as parameter.
13285 					 *
13286 					 * The FuncCall node is also marked agg_star = true,
13287 					 * so that later processing can detect what the argument
13288 					 * really was.
13289 					 */
13290 					FuncCall *n = makeFuncCall($1, NIL, @1);
13291 					n->agg_star = TRUE;
13292 					$$ = (Node *)n;
13293 				}
13294 		;
13295 
13296 
13297 /*
13298  * func_expr and its cousin func_expr_windowless are split out from c_expr just
13299  * so that we have classifications for "everything that is a function call or
13300  * looks like one".  This isn't very important, but it saves us having to
13301  * document which variants are legal in places like "FROM function()" or the
13302  * backwards-compatible functional-index syntax for CREATE INDEX.
13303  * (Note that many of the special SQL functions wouldn't actually make any
13304  * sense as functional index entries, but we ignore that consideration here.)
13305  */
13306 func_expr: func_application within_group_clause filter_clause over_clause
13307 				{
13308 					FuncCall *n = (FuncCall *) $1;
13309 					/*
13310 					 * The order clause for WITHIN GROUP and the one for
13311 					 * plain-aggregate ORDER BY share a field, so we have to
13312 					 * check here that at most one is present.  We also check
13313 					 * for DISTINCT and VARIADIC here to give a better error
13314 					 * location.  Other consistency checks are deferred to
13315 					 * parse analysis.
13316 					 */
13317 					if ($2 != NIL)
13318 					{
13319 						if (n->agg_order != NIL)
13320 							ereport(ERROR,
13321 									(errcode(ERRCODE_SYNTAX_ERROR),
13322 									 errmsg("cannot use multiple ORDER BY clauses with WITHIN GROUP"),
13323 									 parser_errposition(@2)));
13324 						if (n->agg_distinct)
13325 							ereport(ERROR,
13326 									(errcode(ERRCODE_SYNTAX_ERROR),
13327 									 errmsg("cannot use DISTINCT with WITHIN GROUP"),
13328 									 parser_errposition(@2)));
13329 						if (n->func_variadic)
13330 							ereport(ERROR,
13331 									(errcode(ERRCODE_SYNTAX_ERROR),
13332 									 errmsg("cannot use VARIADIC with WITHIN GROUP"),
13333 									 parser_errposition(@2)));
13334 						n->agg_order = $2;
13335 						n->agg_within_group = TRUE;
13336 					}
13337 					n->agg_filter = $3;
13338 					n->over = $4;
13339 					$$ = (Node *) n;
13340 				}
13341 			| func_expr_common_subexpr
13342 				{ $$ = $1; }
13343 		;
13344 
13345 /*
13346  * As func_expr but does not accept WINDOW functions directly
13347  * (but they can still be contained in arguments for functions etc).
13348  * Use this when window expressions are not allowed, where needed to
13349  * disambiguate the grammar (e.g. in CREATE INDEX).
13350  */
13351 func_expr_windowless:
13352 			func_application						{ $$ = $1; }
13353 			| func_expr_common_subexpr				{ $$ = $1; }
13354 		;
13355 
13356 /*
13357  * Special expressions that are considered to be functions.
13358  */
13359 func_expr_common_subexpr:
13360 			COLLATION FOR '(' a_expr ')'
13361 				{
13362 					$$ = (Node *) makeFuncCall(SystemFuncName("pg_collation_for"),
13363 											   list_make1($4),
13364 											   @1);
13365 				}
13366 			| CURRENT_DATE
13367 				{
13368 					$$ = makeSQLValueFunction(SVFOP_CURRENT_DATE, -1, @1);
13369 				}
13370 			| CURRENT_TIME
13371 				{
13372 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME, -1, @1);
13373 				}
13374 			| CURRENT_TIME '(' Iconst ')'
13375 				{
13376 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIME_N, $3, @1);
13377 				}
13378 			| CURRENT_TIMESTAMP
13379 				{
13380 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP, -1, @1);
13381 				}
13382 			| CURRENT_TIMESTAMP '(' Iconst ')'
13383 				{
13384 					$$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP_N, $3, @1);
13385 				}
13386 			| LOCALTIME
13387 				{
13388 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME, -1, @1);
13389 				}
13390 			| LOCALTIME '(' Iconst ')'
13391 				{
13392 					$$ = makeSQLValueFunction(SVFOP_LOCALTIME_N, $3, @1);
13393 				}
13394 			| LOCALTIMESTAMP
13395 				{
13396 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP, -1, @1);
13397 				}
13398 			| LOCALTIMESTAMP '(' Iconst ')'
13399 				{
13400 					$$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP_N, $3, @1);
13401 				}
13402 			| CURRENT_ROLE
13403 				{
13404 					$$ = makeSQLValueFunction(SVFOP_CURRENT_ROLE, -1, @1);
13405 				}
13406 			| CURRENT_USER
13407 				{
13408 					$$ = makeSQLValueFunction(SVFOP_CURRENT_USER, -1, @1);
13409 				}
13410 			| SESSION_USER
13411 				{
13412 					$$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1);
13413 				}
13414 			| USER
13415 				{
13416 					$$ = makeSQLValueFunction(SVFOP_USER, -1, @1);
13417 				}
13418 			| CURRENT_CATALOG
13419 				{
13420 					$$ = makeSQLValueFunction(SVFOP_CURRENT_CATALOG, -1, @1);
13421 				}
13422 			| CURRENT_SCHEMA
13423 				{
13424 					$$ = makeSQLValueFunction(SVFOP_CURRENT_SCHEMA, -1, @1);
13425 				}
13426 			| CAST '(' a_expr AS Typename ')'
13427 				{ $$ = makeTypeCast($3, $5, @1); }
13428 			| EXTRACT '(' extract_list ')'
13429 				{
13430 					$$ = (Node *) makeFuncCall(SystemFuncName("date_part"), $3, @1);
13431 				}
13432 			| OVERLAY '(' overlay_list ')'
13433 				{
13434 					/* overlay(A PLACING B FROM C FOR D) is converted to
13435 					 * overlay(A, B, C, D)
13436 					 * overlay(A PLACING B FROM C) is converted to
13437 					 * overlay(A, B, C)
13438 					 */
13439 					$$ = (Node *) makeFuncCall(SystemFuncName("overlay"), $3, @1);
13440 				}
13441 			| POSITION '(' position_list ')'
13442 				{
13443 					/* position(A in B) is converted to position(B, A) */
13444 					$$ = (Node *) makeFuncCall(SystemFuncName("position"), $3, @1);
13445 				}
13446 			| SUBSTRING '(' substr_list ')'
13447 				{
13448 					/* substring(A from B for C) is converted to
13449 					 * substring(A, B, C) - thomas 2000-11-28
13450 					 */
13451 					$$ = (Node *) makeFuncCall(SystemFuncName("substring"), $3, @1);
13452 				}
13453 			| TREAT '(' a_expr AS Typename ')'
13454 				{
13455 					/* TREAT(expr AS target) converts expr of a particular type to target,
13456 					 * which is defined to be a subtype of the original expression.
13457 					 * In SQL99, this is intended for use with structured UDTs,
13458 					 * but let's make this a generally useful form allowing stronger
13459 					 * coercions than are handled by implicit casting.
13460 					 *
13461 					 * Convert SystemTypeName() to SystemFuncName() even though
13462 					 * at the moment they result in the same thing.
13463 					 */
13464 					$$ = (Node *) makeFuncCall(SystemFuncName(((Value *)llast($5->names))->val.str),
13465 												list_make1($3),
13466 												@1);
13467 				}
13468 			| TRIM '(' BOTH trim_list ')'
13469 				{
13470 					/* various trim expressions are defined in SQL
13471 					 * - thomas 1997-07-19
13472 					 */
13473 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $4, @1);
13474 				}
13475 			| TRIM '(' LEADING trim_list ')'
13476 				{
13477 					$$ = (Node *) makeFuncCall(SystemFuncName("ltrim"), $4, @1);
13478 				}
13479 			| TRIM '(' TRAILING trim_list ')'
13480 				{
13481 					$$ = (Node *) makeFuncCall(SystemFuncName("rtrim"), $4, @1);
13482 				}
13483 			| TRIM '(' trim_list ')'
13484 				{
13485 					$$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $3, @1);
13486 				}
13487 			| NULLIF '(' a_expr ',' a_expr ')'
13488 				{
13489 					$$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
13490 				}
13491 			| COALESCE '(' expr_list ')'
13492 				{
13493 					CoalesceExpr *c = makeNode(CoalesceExpr);
13494 					c->args = $3;
13495 					c->location = @1;
13496 					$$ = (Node *)c;
13497 				}
13498 			| GREATEST '(' expr_list ')'
13499 				{
13500 					MinMaxExpr *v = makeNode(MinMaxExpr);
13501 					v->args = $3;
13502 					v->op = IS_GREATEST;
13503 					v->location = @1;
13504 					$$ = (Node *)v;
13505 				}
13506 			| LEAST '(' expr_list ')'
13507 				{
13508 					MinMaxExpr *v = makeNode(MinMaxExpr);
13509 					v->args = $3;
13510 					v->op = IS_LEAST;
13511 					v->location = @1;
13512 					$$ = (Node *)v;
13513 				}
13514 			| XMLCONCAT '(' expr_list ')'
13515 				{
13516 					$$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
13517 				}
13518 			| XMLELEMENT '(' NAME_P ColLabel ')'
13519 				{
13520 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
13521 				}
13522 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
13523 				{
13524 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
13525 				}
13526 			| XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
13527 				{
13528 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
13529 				}
13530 			| XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
13531 				{
13532 					$$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
13533 				}
13534 			| XMLEXISTS '(' c_expr xmlexists_argument ')'
13535 				{
13536 					/* xmlexists(A PASSING [BY REF] B [BY REF]) is
13537 					 * converted to xmlexists(A, B)*/
13538 					$$ = (Node *) makeFuncCall(SystemFuncName("xmlexists"), list_make2($3, $4), @1);
13539 				}
13540 			| XMLFOREST '(' xml_attribute_list ')'
13541 				{
13542 					$$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
13543 				}
13544 			| XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
13545 				{
13546 					XmlExpr *x = (XmlExpr *)
13547 						makeXmlExpr(IS_XMLPARSE, NULL, NIL,
13548 									list_make2($4, makeBoolAConst($5, -1)),
13549 									@1);
13550 					x->xmloption = $3;
13551 					$$ = (Node *)x;
13552 				}
13553 			| XMLPI '(' NAME_P ColLabel ')'
13554 				{
13555 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
13556 				}
13557 			| XMLPI '(' NAME_P ColLabel ',' a_expr ')'
13558 				{
13559 					$$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
13560 				}
13561 			| XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
13562 				{
13563 					$$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
13564 									 list_make3($3, $5, $6), @1);
13565 				}
13566 			| XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
13567 				{
13568 					XmlSerialize *n = makeNode(XmlSerialize);
13569 					n->xmloption = $3;
13570 					n->expr = $4;
13571 					n->typeName = $6;
13572 					n->location = @1;
13573 					$$ = (Node *)n;
13574 				}
13575 		;
13576 
13577 /*
13578  * SQL/XML support
13579  */
13580 xml_root_version: VERSION_P a_expr
13581 				{ $$ = $2; }
13582 			| VERSION_P NO VALUE_P
13583 				{ $$ = makeNullAConst(-1); }
13584 		;
13585 
13586 opt_xml_root_standalone: ',' STANDALONE_P YES_P
13587 				{ $$ = makeIntConst(XML_STANDALONE_YES, -1); }
13588 			| ',' STANDALONE_P NO
13589 				{ $$ = makeIntConst(XML_STANDALONE_NO, -1); }
13590 			| ',' STANDALONE_P NO VALUE_P
13591 				{ $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
13592 			| /*EMPTY*/
13593 				{ $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
13594 		;
13595 
13596 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'	{ $$ = $3; }
13597 		;
13598 
13599 xml_attribute_list:	xml_attribute_el					{ $$ = list_make1($1); }
13600 			| xml_attribute_list ',' xml_attribute_el	{ $$ = lappend($1, $3); }
13601 		;
13602 
13603 xml_attribute_el: a_expr AS ColLabel
13604 				{
13605 					$$ = makeNode(ResTarget);
13606 					$$->name = $3;
13607 					$$->indirection = NIL;
13608 					$$->val = (Node *) $1;
13609 					$$->location = @1;
13610 				}
13611 			| a_expr
13612 				{
13613 					$$ = makeNode(ResTarget);
13614 					$$->name = NULL;
13615 					$$->indirection = NIL;
13616 					$$->val = (Node *) $1;
13617 					$$->location = @1;
13618 				}
13619 		;
13620 
13621 document_or_content: DOCUMENT_P						{ $$ = XMLOPTION_DOCUMENT; }
13622 			| CONTENT_P								{ $$ = XMLOPTION_CONTENT; }
13623 		;
13624 
13625 xml_whitespace_option: PRESERVE WHITESPACE_P		{ $$ = TRUE; }
13626 			| STRIP_P WHITESPACE_P					{ $$ = FALSE; }
13627 			| /*EMPTY*/								{ $$ = FALSE; }
13628 		;
13629 
13630 /* We allow several variants for SQL and other compatibility. */
13631 xmlexists_argument:
13632 			PASSING c_expr
13633 				{
13634 					$$ = $2;
13635 				}
13636 			| PASSING c_expr BY REF
13637 				{
13638 					$$ = $2;
13639 				}
13640 			| PASSING BY REF c_expr
13641 				{
13642 					$$ = $4;
13643 				}
13644 			| PASSING BY REF c_expr BY REF
13645 				{
13646 					$$ = $4;
13647 				}
13648 		;
13649 
13650 
13651 /*
13652  * Aggregate decoration clauses
13653  */
13654 within_group_clause:
13655 			WITHIN GROUP_P '(' sort_clause ')'		{ $$ = $4; }
13656 			| /*EMPTY*/								{ $$ = NIL; }
13657 		;
13658 
13659 filter_clause:
13660 			FILTER '(' WHERE a_expr ')'				{ $$ = $4; }
13661 			| /*EMPTY*/								{ $$ = NULL; }
13662 		;
13663 
13664 
13665 /*
13666  * Window Definitions
13667  */
13668 window_clause:
13669 			WINDOW window_definition_list			{ $$ = $2; }
13670 			| /*EMPTY*/								{ $$ = NIL; }
13671 		;
13672 
13673 window_definition_list:
13674 			window_definition						{ $$ = list_make1($1); }
13675 			| window_definition_list ',' window_definition
13676 													{ $$ = lappend($1, $3); }
13677 		;
13678 
13679 window_definition:
13680 			ColId AS window_specification
13681 				{
13682 					WindowDef *n = $3;
13683 					n->name = $1;
13684 					$$ = n;
13685 				}
13686 		;
13687 
13688 over_clause: OVER window_specification
13689 				{ $$ = $2; }
13690 			| OVER ColId
13691 				{
13692 					WindowDef *n = makeNode(WindowDef);
13693 					n->name = $2;
13694 					n->refname = NULL;
13695 					n->partitionClause = NIL;
13696 					n->orderClause = NIL;
13697 					n->frameOptions = FRAMEOPTION_DEFAULTS;
13698 					n->startOffset = NULL;
13699 					n->endOffset = NULL;
13700 					n->location = @2;
13701 					$$ = n;
13702 				}
13703 			| /*EMPTY*/
13704 				{ $$ = NULL; }
13705 		;
13706 
13707 window_specification: '(' opt_existing_window_name opt_partition_clause
13708 						opt_sort_clause opt_frame_clause ')'
13709 				{
13710 					WindowDef *n = makeNode(WindowDef);
13711 					n->name = NULL;
13712 					n->refname = $2;
13713 					n->partitionClause = $3;
13714 					n->orderClause = $4;
13715 					/* copy relevant fields of opt_frame_clause */
13716 					n->frameOptions = $5->frameOptions;
13717 					n->startOffset = $5->startOffset;
13718 					n->endOffset = $5->endOffset;
13719 					n->location = @1;
13720 					$$ = n;
13721 				}
13722 		;
13723 
13724 /*
13725  * If we see PARTITION, RANGE, or ROWS as the first token after the '('
13726  * of a window_specification, we want the assumption to be that there is
13727  * no existing_window_name; but those keywords are unreserved and so could
13728  * be ColIds.  We fix this by making them have the same precedence as IDENT
13729  * and giving the empty production here a slightly higher precedence, so
13730  * that the shift/reduce conflict is resolved in favor of reducing the rule.
13731  * These keywords are thus precluded from being an existing_window_name but
13732  * are not reserved for any other purpose.
13733  */
13734 opt_existing_window_name: ColId						{ $$ = $1; }
13735 			| /*EMPTY*/				%prec Op		{ $$ = NULL; }
13736 		;
13737 
13738 opt_partition_clause: PARTITION BY expr_list		{ $$ = $3; }
13739 			| /*EMPTY*/								{ $$ = NIL; }
13740 		;
13741 
13742 /*
13743  * For frame clauses, we return a WindowDef, but only some fields are used:
13744  * frameOptions, startOffset, and endOffset.
13745  *
13746  * This is only a subset of the full SQL:2008 frame_clause grammar.
13747  * We don't support <window frame exclusion> yet.
13748  */
13749 opt_frame_clause:
13750 			RANGE frame_extent
13751 				{
13752 					WindowDef *n = $2;
13753 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
13754 					if (n->frameOptions & (FRAMEOPTION_START_VALUE_PRECEDING |
13755 										   FRAMEOPTION_END_VALUE_PRECEDING))
13756 						ereport(ERROR,
13757 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13758 								 errmsg("RANGE PRECEDING is only supported with UNBOUNDED"),
13759 								 parser_errposition(@1)));
13760 					if (n->frameOptions & (FRAMEOPTION_START_VALUE_FOLLOWING |
13761 										   FRAMEOPTION_END_VALUE_FOLLOWING))
13762 						ereport(ERROR,
13763 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13764 								 errmsg("RANGE FOLLOWING is only supported with UNBOUNDED"),
13765 								 parser_errposition(@1)));
13766 					$$ = n;
13767 				}
13768 			| ROWS frame_extent
13769 				{
13770 					WindowDef *n = $2;
13771 					n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
13772 					$$ = n;
13773 				}
13774 			| /*EMPTY*/
13775 				{
13776 					WindowDef *n = makeNode(WindowDef);
13777 					n->frameOptions = FRAMEOPTION_DEFAULTS;
13778 					n->startOffset = NULL;
13779 					n->endOffset = NULL;
13780 					$$ = n;
13781 				}
13782 		;
13783 
13784 frame_extent: frame_bound
13785 				{
13786 					WindowDef *n = $1;
13787 					/* reject invalid cases */
13788 					if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
13789 						ereport(ERROR,
13790 								(errcode(ERRCODE_WINDOWING_ERROR),
13791 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
13792 								 parser_errposition(@1)));
13793 					if (n->frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING)
13794 						ereport(ERROR,
13795 								(errcode(ERRCODE_WINDOWING_ERROR),
13796 								 errmsg("frame starting from following row cannot end with current row"),
13797 								 parser_errposition(@1)));
13798 					n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
13799 					$$ = n;
13800 				}
13801 			| BETWEEN frame_bound AND frame_bound
13802 				{
13803 					WindowDef *n1 = $2;
13804 					WindowDef *n2 = $4;
13805 					/* form merged options */
13806 					int		frameOptions = n1->frameOptions;
13807 					/* shift converts START_ options to END_ options */
13808 					frameOptions |= n2->frameOptions << 1;
13809 					frameOptions |= FRAMEOPTION_BETWEEN;
13810 					/* reject invalid cases */
13811 					if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
13812 						ereport(ERROR,
13813 								(errcode(ERRCODE_WINDOWING_ERROR),
13814 								 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
13815 								 parser_errposition(@2)));
13816 					if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
13817 						ereport(ERROR,
13818 								(errcode(ERRCODE_WINDOWING_ERROR),
13819 								 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
13820 								 parser_errposition(@4)));
13821 					if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
13822 						(frameOptions & FRAMEOPTION_END_VALUE_PRECEDING))
13823 						ereport(ERROR,
13824 								(errcode(ERRCODE_WINDOWING_ERROR),
13825 								 errmsg("frame starting from current row cannot have preceding rows"),
13826 								 parser_errposition(@4)));
13827 					if ((frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING) &&
13828 						(frameOptions & (FRAMEOPTION_END_VALUE_PRECEDING |
13829 										 FRAMEOPTION_END_CURRENT_ROW)))
13830 						ereport(ERROR,
13831 								(errcode(ERRCODE_WINDOWING_ERROR),
13832 								 errmsg("frame starting from following row cannot have preceding rows"),
13833 								 parser_errposition(@4)));
13834 					n1->frameOptions = frameOptions;
13835 					n1->endOffset = n2->startOffset;
13836 					$$ = n1;
13837 				}
13838 		;
13839 
13840 /*
13841  * This is used for both frame start and frame end, with output set up on
13842  * the assumption it's frame start; the frame_extent productions must reject
13843  * invalid cases.
13844  */
13845 frame_bound:
13846 			UNBOUNDED PRECEDING
13847 				{
13848 					WindowDef *n = makeNode(WindowDef);
13849 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
13850 					n->startOffset = NULL;
13851 					n->endOffset = NULL;
13852 					$$ = n;
13853 				}
13854 			| UNBOUNDED FOLLOWING
13855 				{
13856 					WindowDef *n = makeNode(WindowDef);
13857 					n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
13858 					n->startOffset = NULL;
13859 					n->endOffset = NULL;
13860 					$$ = n;
13861 				}
13862 			| CURRENT_P ROW
13863 				{
13864 					WindowDef *n = makeNode(WindowDef);
13865 					n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
13866 					n->startOffset = NULL;
13867 					n->endOffset = NULL;
13868 					$$ = n;
13869 				}
13870 			| a_expr PRECEDING
13871 				{
13872 					WindowDef *n = makeNode(WindowDef);
13873 					n->frameOptions = FRAMEOPTION_START_VALUE_PRECEDING;
13874 					n->startOffset = $1;
13875 					n->endOffset = NULL;
13876 					$$ = n;
13877 				}
13878 			| a_expr FOLLOWING
13879 				{
13880 					WindowDef *n = makeNode(WindowDef);
13881 					n->frameOptions = FRAMEOPTION_START_VALUE_FOLLOWING;
13882 					n->startOffset = $1;
13883 					n->endOffset = NULL;
13884 					$$ = n;
13885 				}
13886 		;
13887 
13888 
13889 /*
13890  * Supporting nonterminals for expressions.
13891  */
13892 
13893 /* Explicit row production.
13894  *
13895  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
13896  * without conflicting with the parenthesized a_expr production.  Without the
13897  * ROW keyword, there must be more than one a_expr inside the parens.
13898  */
13899 row:		ROW '(' expr_list ')'					{ $$ = $3; }
13900 			| ROW '(' ')'							{ $$ = NIL; }
13901 			| '(' expr_list ',' a_expr ')'			{ $$ = lappend($2, $4); }
13902 		;
13903 
13904 explicit_row:	ROW '(' expr_list ')'				{ $$ = $3; }
13905 			| ROW '(' ')'							{ $$ = NIL; }
13906 		;
13907 
13908 implicit_row:	'(' expr_list ',' a_expr ')'		{ $$ = lappend($2, $4); }
13909 		;
13910 
13911 sub_type:	ANY										{ $$ = ANY_SUBLINK; }
13912 			| SOME									{ $$ = ANY_SUBLINK; }
13913 			| ALL									{ $$ = ALL_SUBLINK; }
13914 		;
13915 
13916 all_Op:		Op										{ $$ = $1; }
13917 			| MathOp								{ $$ = $1; }
13918 		;
13919 
13920 MathOp:		 '+'									{ $$ = "+"; }
13921 			| '-'									{ $$ = "-"; }
13922 			| '*'									{ $$ = "*"; }
13923 			| '/'									{ $$ = "/"; }
13924 			| '%'									{ $$ = "%"; }
13925 			| '^'									{ $$ = "^"; }
13926 			| '<'									{ $$ = "<"; }
13927 			| '>'									{ $$ = ">"; }
13928 			| '='									{ $$ = "="; }
13929 			| LESS_EQUALS							{ $$ = "<="; }
13930 			| GREATER_EQUALS						{ $$ = ">="; }
13931 			| NOT_EQUALS							{ $$ = "<>"; }
13932 		;
13933 
13934 qual_Op:	Op
13935 					{ $$ = list_make1(makeString($1)); }
13936 			| OPERATOR '(' any_operator ')'
13937 					{ $$ = $3; }
13938 		;
13939 
13940 qual_all_Op:
13941 			all_Op
13942 					{ $$ = list_make1(makeString($1)); }
13943 			| OPERATOR '(' any_operator ')'
13944 					{ $$ = $3; }
13945 		;
13946 
13947 subquery_Op:
13948 			all_Op
13949 					{ $$ = list_make1(makeString($1)); }
13950 			| OPERATOR '(' any_operator ')'
13951 					{ $$ = $3; }
13952 			| LIKE
13953 					{ $$ = list_make1(makeString("~~")); }
13954 			| NOT_LA LIKE
13955 					{ $$ = list_make1(makeString("!~~")); }
13956 			| ILIKE
13957 					{ $$ = list_make1(makeString("~~*")); }
13958 			| NOT_LA ILIKE
13959 					{ $$ = list_make1(makeString("!~~*")); }
13960 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
13961  * the regular expression is preprocessed by a function (similar_escape),
13962  * and the ~ operator for posix regular expressions is used.
13963  *        x SIMILAR TO y     ->    x ~ similar_escape(y)
13964  * this transformation is made on the fly by the parser upwards.
13965  * however the SubLink structure which handles any/some/all stuff
13966  * is not ready for such a thing.
13967  */
13968 			;
13969 
13970 expr_list:	a_expr
13971 				{
13972 					$$ = list_make1($1);
13973 				}
13974 			| expr_list ',' a_expr
13975 				{
13976 					$$ = lappend($1, $3);
13977 				}
13978 		;
13979 
13980 /* function arguments can have names */
13981 func_arg_list:  func_arg_expr
13982 				{
13983 					$$ = list_make1($1);
13984 				}
13985 			| func_arg_list ',' func_arg_expr
13986 				{
13987 					$$ = lappend($1, $3);
13988 				}
13989 		;
13990 
13991 func_arg_expr:  a_expr
13992 				{
13993 					$$ = $1;
13994 				}
13995 			| param_name COLON_EQUALS a_expr
13996 				{
13997 					NamedArgExpr *na = makeNode(NamedArgExpr);
13998 					na->name = $1;
13999 					na->arg = (Expr *) $3;
14000 					na->argnumber = -1;		/* until determined */
14001 					na->location = @1;
14002 					$$ = (Node *) na;
14003 				}
14004 			| param_name EQUALS_GREATER a_expr
14005 				{
14006 					NamedArgExpr *na = makeNode(NamedArgExpr);
14007 					na->name = $1;
14008 					na->arg = (Expr *) $3;
14009 					na->argnumber = -1;		/* until determined */
14010 					na->location = @1;
14011 					$$ = (Node *) na;
14012 				}
14013 		;
14014 
14015 type_list:	Typename								{ $$ = list_make1($1); }
14016 			| type_list ',' Typename				{ $$ = lappend($1, $3); }
14017 		;
14018 
14019 array_expr: '[' expr_list ']'
14020 				{
14021 					$$ = makeAArrayExpr($2, @1);
14022 				}
14023 			| '[' array_expr_list ']'
14024 				{
14025 					$$ = makeAArrayExpr($2, @1);
14026 				}
14027 			| '[' ']'
14028 				{
14029 					$$ = makeAArrayExpr(NIL, @1);
14030 				}
14031 		;
14032 
14033 array_expr_list: array_expr							{ $$ = list_make1($1); }
14034 			| array_expr_list ',' array_expr		{ $$ = lappend($1, $3); }
14035 		;
14036 
14037 
14038 extract_list:
14039 			extract_arg FROM a_expr
14040 				{
14041 					$$ = list_make2(makeStringConst($1, @1), $3);
14042 				}
14043 			| /*EMPTY*/								{ $$ = NIL; }
14044 		;
14045 
14046 /* Allow delimited string Sconst in extract_arg as an SQL extension.
14047  * - thomas 2001-04-12
14048  */
14049 extract_arg:
14050 			IDENT									{ $$ = $1; }
14051 			| YEAR_P								{ $$ = "year"; }
14052 			| MONTH_P								{ $$ = "month"; }
14053 			| DAY_P									{ $$ = "day"; }
14054 			| HOUR_P								{ $$ = "hour"; }
14055 			| MINUTE_P								{ $$ = "minute"; }
14056 			| SECOND_P								{ $$ = "second"; }
14057 			| Sconst								{ $$ = $1; }
14058 		;
14059 
14060 /* OVERLAY() arguments
14061  * SQL99 defines the OVERLAY() function:
14062  * o overlay(text placing text from int for int)
14063  * o overlay(text placing text from int)
14064  * and similarly for binary strings
14065  */
14066 overlay_list:
14067 			a_expr overlay_placing substr_from substr_for
14068 				{
14069 					$$ = list_make4($1, $2, $3, $4);
14070 				}
14071 			| a_expr overlay_placing substr_from
14072 				{
14073 					$$ = list_make3($1, $2, $3);
14074 				}
14075 		;
14076 
14077 overlay_placing:
14078 			PLACING a_expr
14079 				{ $$ = $2; }
14080 		;
14081 
14082 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
14083 
14084 position_list:
14085 			b_expr IN_P b_expr						{ $$ = list_make2($3, $1); }
14086 			| /*EMPTY*/								{ $$ = NIL; }
14087 		;
14088 
14089 /* SUBSTRING() arguments
14090  * SQL9x defines a specific syntax for arguments to SUBSTRING():
14091  * o substring(text from int for int)
14092  * o substring(text from int) get entire string from starting point "int"
14093  * o substring(text for int) get first "int" characters of string
14094  * o substring(text from pattern) get entire string matching pattern
14095  * o substring(text from pattern for escape) same with specified escape char
14096  * We also want to support generic substring functions which accept
14097  * the usual generic list of arguments. So we will accept both styles
14098  * here, and convert the SQL9x style to the generic list for further
14099  * processing. - thomas 2000-11-28
14100  */
14101 substr_list:
14102 			a_expr substr_from substr_for
14103 				{
14104 					$$ = list_make3($1, $2, $3);
14105 				}
14106 			| a_expr substr_for substr_from
14107 				{
14108 					/* not legal per SQL99, but might as well allow it */
14109 					$$ = list_make3($1, $3, $2);
14110 				}
14111 			| a_expr substr_from
14112 				{
14113 					$$ = list_make2($1, $2);
14114 				}
14115 			| a_expr substr_for
14116 				{
14117 					/*
14118 					 * Since there are no cases where this syntax allows
14119 					 * a textual FOR value, we forcibly cast the argument
14120 					 * to int4.  The possible matches in pg_proc are
14121 					 * substring(text,int4) and substring(text,text),
14122 					 * and we don't want the parser to choose the latter,
14123 					 * which it is likely to do if the second argument
14124 					 * is unknown or doesn't have an implicit cast to int4.
14125 					 */
14126 					$$ = list_make3($1, makeIntConst(1, -1),
14127 									makeTypeCast($2,
14128 												 SystemTypeName("int4"), -1));
14129 				}
14130 			| expr_list
14131 				{
14132 					$$ = $1;
14133 				}
14134 			| /*EMPTY*/
14135 				{ $$ = NIL; }
14136 		;
14137 
14138 substr_from:
14139 			FROM a_expr								{ $$ = $2; }
14140 		;
14141 
14142 substr_for: FOR a_expr								{ $$ = $2; }
14143 		;
14144 
14145 trim_list:	a_expr FROM expr_list					{ $$ = lappend($3, $1); }
14146 			| FROM expr_list						{ $$ = $2; }
14147 			| expr_list								{ $$ = $1; }
14148 		;
14149 
14150 in_expr:	select_with_parens
14151 				{
14152 					SubLink *n = makeNode(SubLink);
14153 					n->subselect = $1;
14154 					/* other fields will be filled later */
14155 					$$ = (Node *)n;
14156 				}
14157 			| '(' expr_list ')'						{ $$ = (Node *)$2; }
14158 		;
14159 
14160 /*
14161  * Define SQL-style CASE clause.
14162  * - Full specification
14163  *	CASE WHEN a = b THEN c ... ELSE d END
14164  * - Implicit argument
14165  *	CASE a WHEN b THEN c ... ELSE d END
14166  */
14167 case_expr:	CASE case_arg when_clause_list case_default END_P
14168 				{
14169 					CaseExpr *c = makeNode(CaseExpr);
14170 					c->casetype = InvalidOid; /* not analyzed yet */
14171 					c->arg = (Expr *) $2;
14172 					c->args = $3;
14173 					c->defresult = (Expr *) $4;
14174 					c->location = @1;
14175 					$$ = (Node *)c;
14176 				}
14177 		;
14178 
14179 when_clause_list:
14180 			/* There must be at least one */
14181 			when_clause								{ $$ = list_make1($1); }
14182 			| when_clause_list when_clause			{ $$ = lappend($1, $2); }
14183 		;
14184 
14185 when_clause:
14186 			WHEN a_expr THEN a_expr
14187 				{
14188 					CaseWhen *w = makeNode(CaseWhen);
14189 					w->expr = (Expr *) $2;
14190 					w->result = (Expr *) $4;
14191 					w->location = @1;
14192 					$$ = (Node *)w;
14193 				}
14194 		;
14195 
14196 case_default:
14197 			ELSE a_expr								{ $$ = $2; }
14198 			| /*EMPTY*/								{ $$ = NULL; }
14199 		;
14200 
14201 case_arg:	a_expr									{ $$ = $1; }
14202 			| /*EMPTY*/								{ $$ = NULL; }
14203 		;
14204 
14205 columnref:	ColId
14206 				{
14207 					$$ = makeColumnRef($1, NIL, @1, yyscanner);
14208 				}
14209 			| ColId indirection
14210 				{
14211 					$$ = makeColumnRef($1, $2, @1, yyscanner);
14212 				}
14213 		;
14214 
14215 indirection_el:
14216 			'.' attr_name
14217 				{
14218 					$$ = (Node *) makeString($2);
14219 				}
14220 			| '.' '*'
14221 				{
14222 					$$ = (Node *) makeNode(A_Star);
14223 				}
14224 			| '[' a_expr ']'
14225 				{
14226 					A_Indices *ai = makeNode(A_Indices);
14227 					ai->is_slice = false;
14228 					ai->lidx = NULL;
14229 					ai->uidx = $2;
14230 					$$ = (Node *) ai;
14231 				}
14232 			| '[' opt_slice_bound ':' opt_slice_bound ']'
14233 				{
14234 					A_Indices *ai = makeNode(A_Indices);
14235 					ai->is_slice = true;
14236 					ai->lidx = $2;
14237 					ai->uidx = $4;
14238 					$$ = (Node *) ai;
14239 				}
14240 		;
14241 
14242 opt_slice_bound:
14243 			a_expr									{ $$ = $1; }
14244 			| /*EMPTY*/								{ $$ = NULL; }
14245 		;
14246 
14247 indirection:
14248 			indirection_el							{ $$ = list_make1($1); }
14249 			| indirection indirection_el			{ $$ = lappend($1, $2); }
14250 		;
14251 
14252 opt_indirection:
14253 			/*EMPTY*/								{ $$ = NIL; }
14254 			| opt_indirection indirection_el		{ $$ = lappend($1, $2); }
14255 		;
14256 
14257 opt_asymmetric: ASYMMETRIC
14258 			| /*EMPTY*/
14259 		;
14260 
14261 
14262 /*****************************************************************************
14263  *
14264  *	target list for SELECT
14265  *
14266  *****************************************************************************/
14267 
14268 opt_target_list: target_list						{ $$ = $1; }
14269 			| /* EMPTY */							{ $$ = NIL; }
14270 		;
14271 
14272 target_list:
14273 			target_el								{ $$ = list_make1($1); }
14274 			| target_list ',' target_el				{ $$ = lappend($1, $3); }
14275 		;
14276 
14277 target_el:	a_expr AS ColLabel
14278 				{
14279 					$$ = makeNode(ResTarget);
14280 					$$->name = $3;
14281 					$$->indirection = NIL;
14282 					$$->val = (Node *)$1;
14283 					$$->location = @1;
14284 				}
14285 			/*
14286 			 * We support omitting AS only for column labels that aren't
14287 			 * any known keyword.  There is an ambiguity against postfix
14288 			 * operators: is "a ! b" an infix expression, or a postfix
14289 			 * expression and a column label?  We prefer to resolve this
14290 			 * as an infix expression, which we accomplish by assigning
14291 			 * IDENT a precedence higher than POSTFIXOP.
14292 			 */
14293 			| a_expr IDENT
14294 				{
14295 					$$ = makeNode(ResTarget);
14296 					$$->name = $2;
14297 					$$->indirection = NIL;
14298 					$$->val = (Node *)$1;
14299 					$$->location = @1;
14300 				}
14301 			| a_expr
14302 				{
14303 					$$ = makeNode(ResTarget);
14304 					$$->name = NULL;
14305 					$$->indirection = NIL;
14306 					$$->val = (Node *)$1;
14307 					$$->location = @1;
14308 				}
14309 			| '*'
14310 				{
14311 					ColumnRef *n = makeNode(ColumnRef);
14312 					n->fields = list_make1(makeNode(A_Star));
14313 					n->location = @1;
14314 
14315 					$$ = makeNode(ResTarget);
14316 					$$->name = NULL;
14317 					$$->indirection = NIL;
14318 					$$->val = (Node *)n;
14319 					$$->location = @1;
14320 				}
14321 		;
14322 
14323 
14324 /*****************************************************************************
14325  *
14326  *	Names and constants
14327  *
14328  *****************************************************************************/
14329 
14330 qualified_name_list:
14331 			qualified_name							{ $$ = list_make1($1); }
14332 			| qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
14333 		;
14334 
14335 /*
14336  * The production for a qualified relation name has to exactly match the
14337  * production for a qualified func_name, because in a FROM clause we cannot
14338  * tell which we are parsing until we see what comes after it ('(' for a
14339  * func_name, something else for a relation). Therefore we allow 'indirection'
14340  * which may contain subscripts, and reject that case in the C code.
14341  */
14342 qualified_name:
14343 			ColId
14344 				{
14345 					$$ = makeRangeVar(NULL, $1, @1);
14346 				}
14347 			| ColId indirection
14348 				{
14349 					check_qualified_name($2, yyscanner);
14350 					$$ = makeRangeVar(NULL, NULL, @1);
14351 					switch (list_length($2))
14352 					{
14353 						case 1:
14354 							$$->catalogname = NULL;
14355 							$$->schemaname = $1;
14356 							$$->relname = strVal(linitial($2));
14357 							break;
14358 						case 2:
14359 							$$->catalogname = $1;
14360 							$$->schemaname = strVal(linitial($2));
14361 							$$->relname = strVal(lsecond($2));
14362 							break;
14363 						default:
14364 							ereport(ERROR,
14365 									(errcode(ERRCODE_SYNTAX_ERROR),
14366 									 errmsg("improper qualified name (too many dotted names): %s",
14367 											NameListToString(lcons(makeString($1), $2))),
14368 									 parser_errposition(@1)));
14369 							break;
14370 					}
14371 				}
14372 		;
14373 
14374 name_list:	name
14375 					{ $$ = list_make1(makeString($1)); }
14376 			| name_list ',' name
14377 					{ $$ = lappend($1, makeString($3)); }
14378 		;
14379 
14380 
14381 name:		ColId									{ $$ = $1; };
14382 
14383 database_name:
14384 			ColId									{ $$ = $1; };
14385 
14386 access_method:
14387 			ColId									{ $$ = $1; };
14388 
14389 attr_name:	ColLabel								{ $$ = $1; };
14390 
14391 index_name: ColId									{ $$ = $1; };
14392 
14393 file_name:	Sconst									{ $$ = $1; };
14394 
14395 /*
14396  * The production for a qualified func_name has to exactly match the
14397  * production for a qualified columnref, because we cannot tell which we
14398  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
14399  * anything else for a columnref).  Therefore we allow 'indirection' which
14400  * may contain subscripts, and reject that case in the C code.  (If we
14401  * ever implement SQL99-like methods, such syntax may actually become legal!)
14402  */
14403 func_name:	type_function_name
14404 					{ $$ = list_make1(makeString($1)); }
14405 			| ColId indirection
14406 					{
14407 						$$ = check_func_name(lcons(makeString($1), $2),
14408 											 yyscanner);
14409 					}
14410 		;
14411 
14412 
14413 /*
14414  * Constants
14415  */
14416 AexprConst: Iconst
14417 				{
14418 					$$ = makeIntConst($1, @1);
14419 				}
14420 			| FCONST
14421 				{
14422 					$$ = makeFloatConst($1, @1);
14423 				}
14424 			| Sconst
14425 				{
14426 					$$ = makeStringConst($1, @1);
14427 				}
14428 			| BCONST
14429 				{
14430 					$$ = makeBitStringConst($1, @1);
14431 				}
14432 			| XCONST
14433 				{
14434 					/* This is a bit constant per SQL99:
14435 					 * Without Feature F511, "BIT data type",
14436 					 * a <general literal> shall not be a
14437 					 * <bit string literal> or a <hex string literal>.
14438 					 */
14439 					$$ = makeBitStringConst($1, @1);
14440 				}
14441 			| func_name Sconst
14442 				{
14443 					/* generic type 'literal' syntax */
14444 					TypeName *t = makeTypeNameFromNameList($1);
14445 					t->location = @1;
14446 					$$ = makeStringConstCast($2, @2, t);
14447 				}
14448 			| func_name '(' func_arg_list opt_sort_clause ')' Sconst
14449 				{
14450 					/* generic syntax with a type modifier */
14451 					TypeName *t = makeTypeNameFromNameList($1);
14452 					ListCell *lc;
14453 
14454 					/*
14455 					 * We must use func_arg_list and opt_sort_clause in the
14456 					 * production to avoid reduce/reduce conflicts, but we
14457 					 * don't actually wish to allow NamedArgExpr in this
14458 					 * context, nor ORDER BY.
14459 					 */
foreach(lc,$3)14460 					foreach(lc, $3)
14461 					{
14462 						NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
14463 
14464 						if (IsA(arg, NamedArgExpr))
14465 							ereport(ERROR,
14466 									(errcode(ERRCODE_SYNTAX_ERROR),
14467 									 errmsg("type modifier cannot have parameter name"),
14468 									 parser_errposition(arg->location)));
14469 					}
14470 					if ($4 != NIL)
14471 							ereport(ERROR,
14472 									(errcode(ERRCODE_SYNTAX_ERROR),
14473 									 errmsg("type modifier cannot have ORDER BY"),
14474 									 parser_errposition(@4)));
14475 
14476 					t->typmods = $3;
14477 					t->location = @1;
14478 					$$ = makeStringConstCast($6, @6, t);
14479 				}
14480 			| ConstTypename Sconst
14481 				{
14482 					$$ = makeStringConstCast($2, @2, $1);
14483 				}
14484 			| ConstInterval Sconst opt_interval
14485 				{
14486 					TypeName *t = $1;
14487 					t->typmods = $3;
14488 					$$ = makeStringConstCast($2, @2, t);
14489 				}
14490 			| ConstInterval '(' Iconst ')' Sconst
14491 				{
14492 					TypeName *t = $1;
14493 					t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
14494 											makeIntConst($3, @3));
14495 					$$ = makeStringConstCast($5, @5, t);
14496 				}
14497 			| TRUE_P
14498 				{
14499 					$$ = makeBoolAConst(TRUE, @1);
14500 				}
14501 			| FALSE_P
14502 				{
14503 					$$ = makeBoolAConst(FALSE, @1);
14504 				}
14505 			| NULL_P
14506 				{
14507 					$$ = makeNullAConst(@1);
14508 				}
14509 		;
14510 
14511 Iconst:		ICONST									{ $$ = $1; };
14512 Sconst:		SCONST									{ $$ = $1; };
14513 
14514 SignedIconst: Iconst								{ $$ = $1; }
14515 			| '+' Iconst							{ $$ = + $2; }
14516 			| '-' Iconst							{ $$ = - $2; }
14517 		;
14518 
14519 /* Role specifications */
14520 RoleId:		RoleSpec
14521 				{
14522 					RoleSpec *spc = (RoleSpec *) $1;
14523 					switch (spc->roletype)
14524 					{
14525 						case ROLESPEC_CSTRING:
14526 							$$ = spc->rolename;
14527 							break;
14528 						case ROLESPEC_PUBLIC:
14529 							ereport(ERROR,
14530 									(errcode(ERRCODE_RESERVED_NAME),
14531 									 errmsg("role name \"%s\" is reserved",
14532 											"public"),
14533 									 parser_errposition(@1)));
14534 						case ROLESPEC_SESSION_USER:
14535 							ereport(ERROR,
14536 									(errcode(ERRCODE_RESERVED_NAME),
14537 									 errmsg("%s cannot be used as a role name here",
14538 											"SESSION_USER"),
14539 									 parser_errposition(@1)));
14540 						case ROLESPEC_CURRENT_USER:
14541 							ereport(ERROR,
14542 									(errcode(ERRCODE_RESERVED_NAME),
14543 									 errmsg("%s cannot be used as a role name here",
14544 											"CURRENT_USER"),
14545 									 parser_errposition(@1)));
14546 					}
14547 				}
14548 			;
14549 
14550 RoleSpec:	NonReservedWord
14551 					{
14552 						/*
14553 						 * "public" and "none" are not keywords, but they must
14554 						 * be treated specially here.
14555 						 */
14556 						RoleSpec *n;
14557 						if (strcmp($1, "public") == 0)
14558 						{
14559 							n = (RoleSpec *) makeRoleSpec(ROLESPEC_PUBLIC, @1);
14560 							n->roletype = ROLESPEC_PUBLIC;
14561 						}
14562 						else if (strcmp($1, "none") == 0)
14563 						{
14564 							ereport(ERROR,
14565 									(errcode(ERRCODE_RESERVED_NAME),
14566 									 errmsg("role name \"%s\" is reserved",
14567 											"none"),
14568 									 parser_errposition(@1)));
14569 						}
14570 						else
14571 						{
14572 							n = makeRoleSpec(ROLESPEC_CSTRING, @1);
14573 							n->rolename = pstrdup($1);
14574 						}
14575 						$$ = n;
14576 					}
14577 			| CURRENT_USER
14578 					{
14579 						$$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1);
14580 					}
14581 			| SESSION_USER
14582 					{
14583 						$$ = makeRoleSpec(ROLESPEC_SESSION_USER, @1);
14584 					}
14585 		;
14586 
14587 role_list:	RoleSpec
14588 					{ $$ = list_make1($1); }
14589 			| role_list ',' RoleSpec
14590 					{ $$ = lappend($1, $3); }
14591 		;
14592 
14593 /*
14594  * Name classification hierarchy.
14595  *
14596  * IDENT is the lexeme returned by the lexer for identifiers that match
14597  * no known keyword.  In most cases, we can accept certain keywords as
14598  * names, not only IDENTs.	We prefer to accept as many such keywords
14599  * as possible to minimize the impact of "reserved words" on programmers.
14600  * So, we divide names into several possible classes.  The classification
14601  * is chosen in part to make keywords acceptable as names wherever possible.
14602  */
14603 
14604 /* Column identifier --- names that can be column, table, etc names.
14605  */
14606 ColId:		IDENT									{ $$ = $1; }
14607 			| unreserved_keyword					{ $$ = pstrdup($1); }
14608 			| col_name_keyword						{ $$ = pstrdup($1); }
14609 		;
14610 
14611 /* Type/function identifier --- names that can be type or function names.
14612  */
14613 type_function_name:	IDENT							{ $$ = $1; }
14614 			| unreserved_keyword					{ $$ = pstrdup($1); }
14615 			| type_func_name_keyword				{ $$ = pstrdup($1); }
14616 		;
14617 
14618 /* Any not-fully-reserved word --- these names can be, eg, role names.
14619  */
14620 NonReservedWord:	IDENT							{ $$ = $1; }
14621 			| unreserved_keyword					{ $$ = pstrdup($1); }
14622 			| col_name_keyword						{ $$ = pstrdup($1); }
14623 			| type_func_name_keyword				{ $$ = pstrdup($1); }
14624 		;
14625 
14626 /* Column label --- allowed labels in "AS" clauses.
14627  * This presently includes *all* Postgres keywords.
14628  */
14629 ColLabel:	IDENT									{ $$ = $1; }
14630 			| unreserved_keyword					{ $$ = pstrdup($1); }
14631 			| col_name_keyword						{ $$ = pstrdup($1); }
14632 			| type_func_name_keyword				{ $$ = pstrdup($1); }
14633 			| reserved_keyword						{ $$ = pstrdup($1); }
14634 		;
14635 
14636 
14637 /*
14638  * Keyword category lists.  Generally, every keyword present in
14639  * the Postgres grammar should appear in exactly one of these lists.
14640  *
14641  * Put a new keyword into the first list that it can go into without causing
14642  * shift or reduce conflicts.  The earlier lists define "less reserved"
14643  * categories of keywords.
14644  *
14645  * Make sure that each keyword's category in kwlist.h matches where
14646  * it is listed here.  (Someday we may be able to generate these lists and
14647  * kwlist.h's table from a common master list.)
14648  */
14649 
14650 /* "Unreserved" keywords --- available for use as any kind of name.
14651  */
14652 unreserved_keyword:
14653 			  ABORT_P
14654 			| ABSOLUTE_P
14655 			| ACCESS
14656 			| ACTION
14657 			| ADD_P
14658 			| ADMIN
14659 			| AFTER
14660 			| AGGREGATE
14661 			| ALSO
14662 			| ALTER
14663 			| ALWAYS
14664 			| ASSERTION
14665 			| ASSIGNMENT
14666 			| AT
14667 			| ATTACH
14668 			| ATTRIBUTE
14669 			| BACKWARD
14670 			| BEFORE
14671 			| BEGIN_P
14672 			| BY
14673 			| CACHE
14674 			| CALLED
14675 			| CASCADE
14676 			| CASCADED
14677 			| CATALOG_P
14678 			| CHAIN
14679 			| CHARACTERISTICS
14680 			| CHECKPOINT
14681 			| CLASS
14682 			| CLOSE
14683 			| CLUSTER
14684 			| COLUMNS
14685 			| COMMENT
14686 			| COMMENTS
14687 			| COMMIT
14688 			| COMMITTED
14689 			| CONFIGURATION
14690 			| CONFLICT
14691 			| CONNECTION
14692 			| CONSTRAINTS
14693 			| CONTENT_P
14694 			| CONTINUE_P
14695 			| CONVERSION_P
14696 			| COPY
14697 			| COST
14698 			| CSV
14699 			| CUBE
14700 			| CURRENT_P
14701 			| CURSOR
14702 			| CYCLE
14703 			| DATA_P
14704 			| DATABASE
14705 			| DAY_P
14706 			| DEALLOCATE
14707 			| DECLARE
14708 			| DEFAULTS
14709 			| DEFERRED
14710 			| DEFINER
14711 			| DELETE_P
14712 			| DELIMITER
14713 			| DELIMITERS
14714 			| DEPENDS
14715 			| DETACH
14716 			| DICTIONARY
14717 			| DISABLE_P
14718 			| DISCARD
14719 			| DOCUMENT_P
14720 			| DOMAIN_P
14721 			| DOUBLE_P
14722 			| DROP
14723 			| EACH
14724 			| ENABLE_P
14725 			| ENCODING
14726 			| ENCRYPTED
14727 			| ENUM_P
14728 			| ESCAPE
14729 			| EVENT
14730 			| EXCLUDE
14731 			| EXCLUDING
14732 			| EXCLUSIVE
14733 			| EXECUTE
14734 			| EXPLAIN
14735 			| EXTENSION
14736 			| EXTERNAL
14737 			| FAMILY
14738 			| FILTER
14739 			| FIRST_P
14740 			| FOLLOWING
14741 			| FORCE
14742 			| FORWARD
14743 			| FUNCTION
14744 			| FUNCTIONS
14745 			| GENERATED
14746 			| GLOBAL
14747 			| GRANTED
14748 			| HANDLER
14749 			| HEADER_P
14750 			| HOLD
14751 			| HOUR_P
14752 			| IDENTITY_P
14753 			| IF_P
14754 			| IMMEDIATE
14755 			| IMMUTABLE
14756 			| IMPLICIT_P
14757 			| IMPORT_P
14758 			| INCLUDING
14759 			| INCREMENT
14760 			| INDEX
14761 			| INDEXES
14762 			| INHERIT
14763 			| INHERITS
14764 			| INLINE_P
14765 			| INPUT_P
14766 			| INSENSITIVE
14767 			| INSERT
14768 			| INSTEAD
14769 			| INVOKER
14770 			| ISOLATION
14771 			| KEY
14772 			| LABEL
14773 			| LANGUAGE
14774 			| LARGE_P
14775 			| LAST_P
14776 			| LEAKPROOF
14777 			| LEVEL
14778 			| LISTEN
14779 			| LOAD
14780 			| LOCAL
14781 			| LOCATION
14782 			| LOCK_P
14783 			| LOCKED
14784 			| LOGGED
14785 			| MAPPING
14786 			| MATCH
14787 			| MATERIALIZED
14788 			| MAXVALUE
14789 			| METHOD
14790 			| MINUTE_P
14791 			| MINVALUE
14792 			| MODE
14793 			| MONTH_P
14794 			| MOVE
14795 			| NAME_P
14796 			| NAMES
14797 			| NEW
14798 			| NEXT
14799 			| NO
14800 			| NOTHING
14801 			| NOTIFY
14802 			| NOWAIT
14803 			| NULLS_P
14804 			| OBJECT_P
14805 			| OF
14806 			| OFF
14807 			| OIDS
14808 			| OLD
14809 			| OPERATOR
14810 			| OPTION
14811 			| OPTIONS
14812 			| ORDINALITY
14813 			| OVER
14814 			| OVERRIDING
14815 			| OWNED
14816 			| OWNER
14817 			| PARALLEL
14818 			| PARSER
14819 			| PARTIAL
14820 			| PARTITION
14821 			| PASSING
14822 			| PASSWORD
14823 			| PLANS
14824 			| POLICY
14825 			| PRECEDING
14826 			| PREPARE
14827 			| PREPARED
14828 			| PRESERVE
14829 			| PRIOR
14830 			| PRIVILEGES
14831 			| PROCEDURAL
14832 			| PROCEDURE
14833 			| PROGRAM
14834 			| PUBLICATION
14835 			| QUOTE
14836 			| RANGE
14837 			| READ
14838 			| REASSIGN
14839 			| RECHECK
14840 			| RECURSIVE
14841 			| REF
14842 			| REFERENCING
14843 			| REFRESH
14844 			| REINDEX
14845 			| RELATIVE_P
14846 			| RELEASE
14847 			| RENAME
14848 			| REPEATABLE
14849 			| REPLACE
14850 			| REPLICA
14851 			| RESET
14852 			| RESTART
14853 			| RESTRICT
14854 			| RETURNS
14855 			| REVOKE
14856 			| ROLE
14857 			| ROLLBACK
14858 			| ROLLUP
14859 			| ROWS
14860 			| RULE
14861 			| SAVEPOINT
14862 			| SCHEMA
14863 			| SCHEMAS
14864 			| SCROLL
14865 			| SEARCH
14866 			| SECOND_P
14867 			| SECURITY
14868 			| SEQUENCE
14869 			| SEQUENCES
14870 			| SERIALIZABLE
14871 			| SERVER
14872 			| SESSION
14873 			| SET
14874 			| SETS
14875 			| SHARE
14876 			| SHOW
14877 			| SIMPLE
14878 			| SKIP
14879 			| SNAPSHOT
14880 			| SQL_P
14881 			| STABLE
14882 			| STANDALONE_P
14883 			| START
14884 			| STATEMENT
14885 			| STATISTICS
14886 			| STDIN
14887 			| STDOUT
14888 			| STORAGE
14889 			| STRICT_P
14890 			| STRIP_P
14891 			| SUBSCRIPTION
14892 			| SYSID
14893 			| SYSTEM_P
14894 			| TABLES
14895 			| TABLESPACE
14896 			| TEMP
14897 			| TEMPLATE
14898 			| TEMPORARY
14899 			| TEXT_P
14900 			| TRANSACTION
14901 			| TRANSFORM
14902 			| TRIGGER
14903 			| TRUNCATE
14904 			| TRUSTED
14905 			| TYPE_P
14906 			| TYPES_P
14907 			| UNBOUNDED
14908 			| UNCOMMITTED
14909 			| UNENCRYPTED
14910 			| UNKNOWN
14911 			| UNLISTEN
14912 			| UNLOGGED
14913 			| UNTIL
14914 			| UPDATE
14915 			| VACUUM
14916 			| VALID
14917 			| VALIDATE
14918 			| VALIDATOR
14919 			| VALUE_P
14920 			| VARYING
14921 			| VERSION_P
14922 			| VIEW
14923 			| VIEWS
14924 			| VOLATILE
14925 			| WHITESPACE_P
14926 			| WITHIN
14927 			| WITHOUT
14928 			| WORK
14929 			| WRAPPER
14930 			| WRITE
14931 			| XML_P
14932 			| YEAR_P
14933 			| YES_P
14934 			| ZONE
14935 		;
14936 
14937 /* Column identifier --- keywords that can be column, table, etc names.
14938  *
14939  * Many of these keywords will in fact be recognized as type or function
14940  * names too; but they have special productions for the purpose, and so
14941  * can't be treated as "generic" type or function names.
14942  *
14943  * The type names appearing here are not usable as function names
14944  * because they can be followed by '(' in typename productions, which
14945  * looks too much like a function call for an LR(1) parser.
14946  */
14947 col_name_keyword:
14948 			  BETWEEN
14949 			| BIGINT
14950 			| BIT
14951 			| BOOLEAN_P
14952 			| CHAR_P
14953 			| CHARACTER
14954 			| COALESCE
14955 			| DEC
14956 			| DECIMAL_P
14957 			| EXISTS
14958 			| EXTRACT
14959 			| FLOAT_P
14960 			| GREATEST
14961 			| GROUPING
14962 			| INOUT
14963 			| INT_P
14964 			| INTEGER
14965 			| INTERVAL
14966 			| LEAST
14967 			| NATIONAL
14968 			| NCHAR
14969 			| NONE
14970 			| NULLIF
14971 			| NUMERIC
14972 			| OUT_P
14973 			| OVERLAY
14974 			| POSITION
14975 			| PRECISION
14976 			| REAL
14977 			| ROW
14978 			| SETOF
14979 			| SMALLINT
14980 			| SUBSTRING
14981 			| TIME
14982 			| TIMESTAMP
14983 			| TREAT
14984 			| TRIM
14985 			| VALUES
14986 			| VARCHAR
14987 			| XMLATTRIBUTES
14988 			| XMLCONCAT
14989 			| XMLELEMENT
14990 			| XMLEXISTS
14991 			| XMLFOREST
14992 			| XMLNAMESPACES
14993 			| XMLPARSE
14994 			| XMLPI
14995 			| XMLROOT
14996 			| XMLSERIALIZE
14997 			| XMLTABLE
14998 		;
14999 
15000 /* Type/function identifier --- keywords that can be type or function names.
15001  *
15002  * Most of these are keywords that are used as operators in expressions;
15003  * in general such keywords can't be column names because they would be
15004  * ambiguous with variables, but they are unambiguous as function identifiers.
15005  *
15006  * Do not include POSITION, SUBSTRING, etc here since they have explicit
15007  * productions in a_expr to support the goofy SQL9x argument syntax.
15008  * - thomas 2000-11-28
15009  */
15010 type_func_name_keyword:
15011 			  AUTHORIZATION
15012 			| BINARY
15013 			| COLLATION
15014 			| CONCURRENTLY
15015 			| CROSS
15016 			| CURRENT_SCHEMA
15017 			| FREEZE
15018 			| FULL
15019 			| ILIKE
15020 			| INNER_P
15021 			| IS
15022 			| ISNULL
15023 			| JOIN
15024 			| LEFT
15025 			| LIKE
15026 			| NATURAL
15027 			| NOTNULL
15028 			| OUTER_P
15029 			| OVERLAPS
15030 			| RIGHT
15031 			| SIMILAR
15032 			| TABLESAMPLE
15033 			| VERBOSE
15034 		;
15035 
15036 /* Reserved keyword --- these keywords are usable only as a ColLabel.
15037  *
15038  * Keywords appear here if they could not be distinguished from variable,
15039  * type, or function names in some contexts.  Don't put things here unless
15040  * forced to.
15041  */
15042 reserved_keyword:
15043 			  ALL
15044 			| ANALYSE
15045 			| ANALYZE
15046 			| AND
15047 			| ANY
15048 			| ARRAY
15049 			| AS
15050 			| ASC
15051 			| ASYMMETRIC
15052 			| BOTH
15053 			| CASE
15054 			| CAST
15055 			| CHECK
15056 			| COLLATE
15057 			| COLUMN
15058 			| CONSTRAINT
15059 			| CREATE
15060 			| CURRENT_CATALOG
15061 			| CURRENT_DATE
15062 			| CURRENT_ROLE
15063 			| CURRENT_TIME
15064 			| CURRENT_TIMESTAMP
15065 			| CURRENT_USER
15066 			| DEFAULT
15067 			| DEFERRABLE
15068 			| DESC
15069 			| DISTINCT
15070 			| DO
15071 			| ELSE
15072 			| END_P
15073 			| EXCEPT
15074 			| FALSE_P
15075 			| FETCH
15076 			| FOR
15077 			| FOREIGN
15078 			| FROM
15079 			| GRANT
15080 			| GROUP_P
15081 			| HAVING
15082 			| IN_P
15083 			| INITIALLY
15084 			| INTERSECT
15085 			| INTO
15086 			| LATERAL_P
15087 			| LEADING
15088 			| LIMIT
15089 			| LOCALTIME
15090 			| LOCALTIMESTAMP
15091 			| NOT
15092 			| NULL_P
15093 			| OFFSET
15094 			| ON
15095 			| ONLY
15096 			| OR
15097 			| ORDER
15098 			| PLACING
15099 			| PRIMARY
15100 			| REFERENCES
15101 			| RETURNING
15102 			| SELECT
15103 			| SESSION_USER
15104 			| SOME
15105 			| SYMMETRIC
15106 			| TABLE
15107 			| THEN
15108 			| TO
15109 			| TRAILING
15110 			| TRUE_P
15111 			| UNION
15112 			| UNIQUE
15113 			| USER
15114 			| USING
15115 			| VARIADIC
15116 			| WHEN
15117 			| WHERE
15118 			| WINDOW
15119 			| WITH
15120 		;
15121 
15122 %%
15123 
15124 /*
15125  * The signature of this function is required by bison.  However, we
15126  * ignore the passed yylloc and instead use the last token position
15127  * available from the scanner.
15128  */
15129 static void
15130 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
15131 {
15132 	parser_yyerror(msg);
15133 }
15134 
15135 static RawStmt *
makeRawStmt(Node * stmt,int stmt_location)15136 makeRawStmt(Node *stmt, int stmt_location)
15137 {
15138 	RawStmt    *rs = makeNode(RawStmt);
15139 
15140 	rs->stmt = stmt;
15141 	rs->stmt_location = stmt_location;
15142 	rs->stmt_len = 0;			/* might get changed later */
15143 	return rs;
15144 }
15145 
15146 /* Adjust a RawStmt to reflect that it doesn't run to the end of the string */
15147 static void
updateRawStmtEnd(RawStmt * rs,int end_location)15148 updateRawStmtEnd(RawStmt *rs, int end_location)
15149 {
15150 	/*
15151 	 * If we already set the length, don't change it.  This is for situations
15152 	 * like "select foo ;; select bar" where the same statement will be last
15153 	 * in the string for more than one semicolon.
15154 	 */
15155 	if (rs->stmt_len > 0)
15156 		return;
15157 
15158 	/* OK, update length of RawStmt */
15159 	rs->stmt_len = end_location - rs->stmt_location;
15160 }
15161 
15162 static Node *
makeColumnRef(char * colname,List * indirection,int location,core_yyscan_t yyscanner)15163 makeColumnRef(char *colname, List *indirection,
15164 			  int location, core_yyscan_t yyscanner)
15165 {
15166 	/*
15167 	 * Generate a ColumnRef node, with an A_Indirection node added if there
15168 	 * is any subscripting in the specified indirection list.  However,
15169 	 * any field selection at the start of the indirection list must be
15170 	 * transposed into the "fields" part of the ColumnRef node.
15171 	 */
15172 	ColumnRef  *c = makeNode(ColumnRef);
15173 	int		nfields = 0;
15174 	ListCell *l;
15175 
15176 	c->location = location;
15177 	foreach(l, indirection)
15178 	{
15179 		if (IsA(lfirst(l), A_Indices))
15180 		{
15181 			A_Indirection *i = makeNode(A_Indirection);
15182 
15183 			if (nfields == 0)
15184 			{
15185 				/* easy case - all indirection goes to A_Indirection */
15186 				c->fields = list_make1(makeString(colname));
15187 				i->indirection = check_indirection(indirection, yyscanner);
15188 			}
15189 			else
15190 			{
15191 				/* got to split the list in two */
15192 				i->indirection = check_indirection(list_copy_tail(indirection,
15193 																  nfields),
15194 												   yyscanner);
15195 				indirection = list_truncate(indirection, nfields);
15196 				c->fields = lcons(makeString(colname), indirection);
15197 			}
15198 			i->arg = (Node *) c;
15199 			return (Node *) i;
15200 		}
15201 		else if (IsA(lfirst(l), A_Star))
15202 		{
15203 			/* We only allow '*' at the end of a ColumnRef */
15204 			if (lnext(l) != NULL)
15205 				parser_yyerror("improper use of \"*\"");
15206 		}
15207 		nfields++;
15208 	}
15209 	/* No subscripting, so all indirection gets added to field list */
15210 	c->fields = lcons(makeString(colname), indirection);
15211 	return (Node *) c;
15212 }
15213 
15214 Node *
makeTypeCast(Node * arg,TypeName * typename,int location)15215 makeTypeCast(Node *arg, TypeName *typename, int location)
15216 {
15217 	TypeCast *n = makeNode(TypeCast);
15218 	n->arg = arg;
15219 	n->typeName = typename;
15220 	n->location = location;
15221 	return (Node *) n;
15222 }
15223 
15224 static Node *
makeStringConst(char * str,int location)15225 makeStringConst(char *str, int location)
15226 {
15227 	A_Const *n = makeNode(A_Const);
15228 
15229 	n->val.type = T_String;
15230 	n->val.val.str = str;
15231 	n->location = location;
15232 
15233 	return (Node *)n;
15234 }
15235 
15236 Node *
makeStringConstCast(char * str,int location,TypeName * typename)15237 makeStringConstCast(char *str, int location, TypeName *typename)
15238 {
15239 	Node *s = makeStringConst(str, location);
15240 
15241 	return makeTypeCast(s, typename, -1);
15242 }
15243 
15244 Node *
makeIntConst(int val,int location)15245 makeIntConst(int val, int location)
15246 {
15247 	A_Const *n = makeNode(A_Const);
15248 
15249 	n->val.type = T_Integer;
15250 	n->val.val.ival = val;
15251 	n->location = location;
15252 
15253 	return (Node *)n;
15254 }
15255 
15256 static Node *
makeFloatConst(char * str,int location)15257 makeFloatConst(char *str, int location)
15258 {
15259 	A_Const *n = makeNode(A_Const);
15260 
15261 	n->val.type = T_Float;
15262 	n->val.val.str = str;
15263 	n->location = location;
15264 
15265 	return (Node *)n;
15266 }
15267 
15268 static Node *
makeBitStringConst(char * str,int location)15269 makeBitStringConst(char *str, int location)
15270 {
15271 	A_Const *n = makeNode(A_Const);
15272 
15273 	n->val.type = T_BitString;
15274 	n->val.val.str = str;
15275 	n->location = location;
15276 
15277 	return (Node *)n;
15278 }
15279 
15280 static Node *
makeNullAConst(int location)15281 makeNullAConst(int location)
15282 {
15283 	A_Const *n = makeNode(A_Const);
15284 
15285 	n->val.type = T_Null;
15286 	n->location = location;
15287 
15288 	return (Node *)n;
15289 }
15290 
15291 static Node *
makeAConst(Value * v,int location)15292 makeAConst(Value *v, int location)
15293 {
15294 	Node *n;
15295 
15296 	switch (v->type)
15297 	{
15298 		case T_Float:
15299 			n = makeFloatConst(v->val.str, location);
15300 			break;
15301 
15302 		case T_Integer:
15303 			n = makeIntConst(v->val.ival, location);
15304 			break;
15305 
15306 		case T_String:
15307 		default:
15308 			n = makeStringConst(v->val.str, location);
15309 			break;
15310 	}
15311 
15312 	return n;
15313 }
15314 
15315 /* makeBoolAConst()
15316  * Create an A_Const string node and put it inside a boolean cast.
15317  */
15318 static Node *
makeBoolAConst(bool state,int location)15319 makeBoolAConst(bool state, int location)
15320 {
15321 	A_Const *n = makeNode(A_Const);
15322 
15323 	n->val.type = T_String;
15324 	n->val.val.str = (state ? "t" : "f");
15325 	n->location = location;
15326 
15327 	return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
15328 }
15329 
15330 /* makeRoleSpec
15331  * Create a RoleSpec with the given type
15332  */
15333 static RoleSpec *
makeRoleSpec(RoleSpecType type,int location)15334 makeRoleSpec(RoleSpecType type, int location)
15335 {
15336 	RoleSpec *spec = makeNode(RoleSpec);
15337 
15338 	spec->roletype = type;
15339 	spec->location = location;
15340 
15341 	return spec;
15342 }
15343 
15344 /* check_qualified_name --- check the result of qualified_name production
15345  *
15346  * It's easiest to let the grammar production for qualified_name allow
15347  * subscripts and '*', which we then must reject here.
15348  */
15349 static void
check_qualified_name(List * names,core_yyscan_t yyscanner)15350 check_qualified_name(List *names, core_yyscan_t yyscanner)
15351 {
15352 	ListCell   *i;
15353 
15354 	foreach(i, names)
15355 	{
15356 		if (!IsA(lfirst(i), String))
15357 			parser_yyerror("syntax error");
15358 	}
15359 }
15360 
15361 /* check_func_name --- check the result of func_name production
15362  *
15363  * It's easiest to let the grammar production for func_name allow subscripts
15364  * and '*', which we then must reject here.
15365  */
15366 static List *
check_func_name(List * names,core_yyscan_t yyscanner)15367 check_func_name(List *names, core_yyscan_t yyscanner)
15368 {
15369 	ListCell   *i;
15370 
15371 	foreach(i, names)
15372 	{
15373 		if (!IsA(lfirst(i), String))
15374 			parser_yyerror("syntax error");
15375 	}
15376 	return names;
15377 }
15378 
15379 /* check_indirection --- check the result of indirection production
15380  *
15381  * We only allow '*' at the end of the list, but it's hard to enforce that
15382  * in the grammar, so do it here.
15383  */
15384 static List *
check_indirection(List * indirection,core_yyscan_t yyscanner)15385 check_indirection(List *indirection, core_yyscan_t yyscanner)
15386 {
15387 	ListCell *l;
15388 
15389 	foreach(l, indirection)
15390 	{
15391 		if (IsA(lfirst(l), A_Star))
15392 		{
15393 			if (lnext(l) != NULL)
15394 				parser_yyerror("improper use of \"*\"");
15395 		}
15396 	}
15397 	return indirection;
15398 }
15399 
15400 /* extractArgTypes()
15401  * Given a list of FunctionParameter nodes, extract a list of just the
15402  * argument types (TypeNames) for input parameters only.  This is what
15403  * is needed to look up an existing function, which is what is wanted by
15404  * the productions that use this call.
15405  */
15406 static List *
extractArgTypes(List * parameters)15407 extractArgTypes(List *parameters)
15408 {
15409 	List	   *result = NIL;
15410 	ListCell   *i;
15411 
15412 	foreach(i, parameters)
15413 	{
15414 		FunctionParameter *p = (FunctionParameter *) lfirst(i);
15415 
15416 		if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
15417 			result = lappend(result, p->argType);
15418 	}
15419 	return result;
15420 }
15421 
15422 /* extractAggrArgTypes()
15423  * As above, but work from the output of the aggr_args production.
15424  */
15425 static List *
extractAggrArgTypes(List * aggrargs)15426 extractAggrArgTypes(List *aggrargs)
15427 {
15428 	Assert(list_length(aggrargs) == 2);
15429 	return extractArgTypes((List *) linitial(aggrargs));
15430 }
15431 
15432 /* makeOrderedSetArgs()
15433  * Build the result of the aggr_args production (which see the comments for).
15434  * This handles only the case where both given lists are nonempty, so that
15435  * we have to deal with multiple VARIADIC arguments.
15436  */
15437 static List *
makeOrderedSetArgs(List * directargs,List * orderedargs,core_yyscan_t yyscanner)15438 makeOrderedSetArgs(List *directargs, List *orderedargs,
15439 				   core_yyscan_t yyscanner)
15440 {
15441 	FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
15442 	int			ndirectargs;
15443 
15444 	/* No restriction unless last direct arg is VARIADIC */
15445 	if (lastd->mode == FUNC_PARAM_VARIADIC)
15446 	{
15447 		FunctionParameter *firsto = (FunctionParameter *) linitial(orderedargs);
15448 
15449 		/*
15450 		 * We ignore the names, though the aggr_arg production allows them;
15451 		 * it doesn't allow default values, so those need not be checked.
15452 		 */
15453 		if (list_length(orderedargs) != 1 ||
15454 			firsto->mode != FUNC_PARAM_VARIADIC)
15455 			ereport(ERROR,
15456 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15457 					 errmsg("an ordered-set aggregate with a VARIADIC direct argument must have one VARIADIC aggregated argument of the same data type"),
15458 					 parser_errposition(exprLocation((Node *) firsto))));
15459 
15460 		/* OK, drop the duplicate VARIADIC argument from the internal form */
15461 		orderedargs = NIL;
15462 	}
15463 
15464 	/* don't merge into the next line, as list_concat changes directargs */
15465 	ndirectargs = list_length(directargs);
15466 
15467 	return list_make2(list_concat(directargs, orderedargs),
15468 					  makeInteger(ndirectargs));
15469 }
15470 
15471 /* insertSelectOptions()
15472  * Insert ORDER BY, etc into an already-constructed SelectStmt.
15473  *
15474  * This routine is just to avoid duplicating code in SelectStmt productions.
15475  */
15476 static void
insertSelectOptions(SelectStmt * stmt,List * sortClause,List * lockingClause,Node * limitOffset,Node * limitCount,WithClause * withClause,core_yyscan_t yyscanner)15477 insertSelectOptions(SelectStmt *stmt,
15478 					List *sortClause, List *lockingClause,
15479 					Node *limitOffset, Node *limitCount,
15480 					WithClause *withClause,
15481 					core_yyscan_t yyscanner)
15482 {
15483 	Assert(IsA(stmt, SelectStmt));
15484 
15485 	/*
15486 	 * Tests here are to reject constructs like
15487 	 *	(SELECT foo ORDER BY bar) ORDER BY baz
15488 	 */
15489 	if (sortClause)
15490 	{
15491 		if (stmt->sortClause)
15492 			ereport(ERROR,
15493 					(errcode(ERRCODE_SYNTAX_ERROR),
15494 					 errmsg("multiple ORDER BY clauses not allowed"),
15495 					 parser_errposition(exprLocation((Node *) sortClause))));
15496 		stmt->sortClause = sortClause;
15497 	}
15498 	/* We can handle multiple locking clauses, though */
15499 	stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
15500 	if (limitOffset)
15501 	{
15502 		if (stmt->limitOffset)
15503 			ereport(ERROR,
15504 					(errcode(ERRCODE_SYNTAX_ERROR),
15505 					 errmsg("multiple OFFSET clauses not allowed"),
15506 					 parser_errposition(exprLocation(limitOffset))));
15507 		stmt->limitOffset = limitOffset;
15508 	}
15509 	if (limitCount)
15510 	{
15511 		if (stmt->limitCount)
15512 			ereport(ERROR,
15513 					(errcode(ERRCODE_SYNTAX_ERROR),
15514 					 errmsg("multiple LIMIT clauses not allowed"),
15515 					 parser_errposition(exprLocation(limitCount))));
15516 		stmt->limitCount = limitCount;
15517 	}
15518 	if (withClause)
15519 	{
15520 		if (stmt->withClause)
15521 			ereport(ERROR,
15522 					(errcode(ERRCODE_SYNTAX_ERROR),
15523 					 errmsg("multiple WITH clauses not allowed"),
15524 					 parser_errposition(exprLocation((Node *) withClause))));
15525 		stmt->withClause = withClause;
15526 	}
15527 }
15528 
15529 static Node *
makeSetOp(SetOperation op,bool all,Node * larg,Node * rarg)15530 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
15531 {
15532 	SelectStmt *n = makeNode(SelectStmt);
15533 
15534 	n->op = op;
15535 	n->all = all;
15536 	n->larg = (SelectStmt *) larg;
15537 	n->rarg = (SelectStmt *) rarg;
15538 	return (Node *) n;
15539 }
15540 
15541 /* SystemFuncName()
15542  * Build a properly-qualified reference to a built-in function.
15543  */
15544 List *
SystemFuncName(char * name)15545 SystemFuncName(char *name)
15546 {
15547 	return list_make2(makeString("pg_catalog"), makeString(name));
15548 }
15549 
15550 /* SystemTypeName()
15551  * Build a properly-qualified reference to a built-in type.
15552  *
15553  * typmod is defaulted, but may be changed afterwards by caller.
15554  * Likewise for the location.
15555  */
15556 TypeName *
SystemTypeName(char * name)15557 SystemTypeName(char *name)
15558 {
15559 	return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
15560 											   makeString(name)));
15561 }
15562 
15563 /* doNegate()
15564  * Handle negation of a numeric constant.
15565  *
15566  * Formerly, we did this here because the optimizer couldn't cope with
15567  * indexquals that looked like "var = -4" --- it wants "var = const"
15568  * and a unary minus operator applied to a constant didn't qualify.
15569  * As of Postgres 7.0, that problem doesn't exist anymore because there
15570  * is a constant-subexpression simplifier in the optimizer.  However,
15571  * there's still a good reason for doing this here, which is that we can
15572  * postpone committing to a particular internal representation for simple
15573  * negative constants.	It's better to leave "-123.456" in string form
15574  * until we know what the desired type is.
15575  */
15576 static Node *
doNegate(Node * n,int location)15577 doNegate(Node *n, int location)
15578 {
15579 	if (IsA(n, A_Const))
15580 	{
15581 		A_Const *con = (A_Const *)n;
15582 
15583 		/* report the constant's location as that of the '-' sign */
15584 		con->location = location;
15585 
15586 		if (con->val.type == T_Integer)
15587 		{
15588 			con->val.val.ival = -con->val.val.ival;
15589 			return n;
15590 		}
15591 		if (con->val.type == T_Float)
15592 		{
15593 			doNegateFloat(&con->val);
15594 			return n;
15595 		}
15596 	}
15597 
15598 	return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
15599 }
15600 
15601 static void
doNegateFloat(Value * v)15602 doNegateFloat(Value *v)
15603 {
15604 	char   *oldval = v->val.str;
15605 
15606 	Assert(IsA(v, Float));
15607 	if (*oldval == '+')
15608 		oldval++;
15609 	if (*oldval == '-')
15610 		v->val.str = oldval+1;	/* just strip the '-' */
15611 	else
15612 		v->val.str = psprintf("-%s", oldval);
15613 }
15614 
15615 static Node *
makeAndExpr(Node * lexpr,Node * rexpr,int location)15616 makeAndExpr(Node *lexpr, Node *rexpr, int location)
15617 {
15618 	Node	   *lexp = lexpr;
15619 
15620 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
15621 	while (IsA(lexp, A_Expr) &&
15622 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
15623 		lexp = ((A_Expr *) lexp)->lexpr;
15624 	/* Flatten "a AND b AND c ..." to a single BoolExpr on sight */
15625 	if (IsA(lexp, BoolExpr))
15626 	{
15627 		BoolExpr *blexpr = (BoolExpr *) lexp;
15628 
15629 		if (blexpr->boolop == AND_EXPR)
15630 		{
15631 			blexpr->args = lappend(blexpr->args, rexpr);
15632 			return (Node *) blexpr;
15633 		}
15634 	}
15635 	return (Node *) makeBoolExpr(AND_EXPR, list_make2(lexpr, rexpr), location);
15636 }
15637 
15638 static Node *
makeOrExpr(Node * lexpr,Node * rexpr,int location)15639 makeOrExpr(Node *lexpr, Node *rexpr, int location)
15640 {
15641 	Node	   *lexp = lexpr;
15642 
15643 	/* Look through AEXPR_PAREN nodes so they don't affect flattening */
15644 	while (IsA(lexp, A_Expr) &&
15645 		   ((A_Expr *) lexp)->kind == AEXPR_PAREN)
15646 		lexp = ((A_Expr *) lexp)->lexpr;
15647 	/* Flatten "a OR b OR c ..." to a single BoolExpr on sight */
15648 	if (IsA(lexp, BoolExpr))
15649 	{
15650 		BoolExpr *blexpr = (BoolExpr *) lexp;
15651 
15652 		if (blexpr->boolop == OR_EXPR)
15653 		{
15654 			blexpr->args = lappend(blexpr->args, rexpr);
15655 			return (Node *) blexpr;
15656 		}
15657 	}
15658 	return (Node *) makeBoolExpr(OR_EXPR, list_make2(lexpr, rexpr), location);
15659 }
15660 
15661 static Node *
makeNotExpr(Node * expr,int location)15662 makeNotExpr(Node *expr, int location)
15663 {
15664 	return (Node *) makeBoolExpr(NOT_EXPR, list_make1(expr), location);
15665 }
15666 
15667 static Node *
makeAArrayExpr(List * elements,int location)15668 makeAArrayExpr(List *elements, int location)
15669 {
15670 	A_ArrayExpr *n = makeNode(A_ArrayExpr);
15671 
15672 	n->elements = elements;
15673 	n->location = location;
15674 	return (Node *) n;
15675 }
15676 
15677 static Node *
makeSQLValueFunction(SQLValueFunctionOp op,int32 typmod,int location)15678 makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location)
15679 {
15680 	SQLValueFunction *svf = makeNode(SQLValueFunction);
15681 
15682 	svf->op = op;
15683 	/* svf->type will be filled during parse analysis */
15684 	svf->typmod = typmod;
15685 	svf->location = location;
15686 	return (Node *) svf;
15687 }
15688 
15689 static Node *
makeXmlExpr(XmlExprOp op,char * name,List * named_args,List * args,int location)15690 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
15691 			int location)
15692 {
15693 	XmlExpr		*x = makeNode(XmlExpr);
15694 
15695 	x->op = op;
15696 	x->name = name;
15697 	/*
15698 	 * named_args is a list of ResTarget; it'll be split apart into separate
15699 	 * expression and name lists in transformXmlExpr().
15700 	 */
15701 	x->named_args = named_args;
15702 	x->arg_names = NIL;
15703 	x->args = args;
15704 	/* xmloption, if relevant, must be filled in by caller */
15705 	/* type and typmod will be filled in during parse analysis */
15706 	x->type = InvalidOid;			/* marks the node as not analyzed */
15707 	x->location = location;
15708 	return (Node *) x;
15709 }
15710 
15711 /*
15712  * Merge the input and output parameters of a table function.
15713  */
15714 static List *
mergeTableFuncParameters(List * func_args,List * columns)15715 mergeTableFuncParameters(List *func_args, List *columns)
15716 {
15717 	ListCell   *lc;
15718 
15719 	/* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
15720 	foreach(lc, func_args)
15721 	{
15722 		FunctionParameter *p = (FunctionParameter *) lfirst(lc);
15723 
15724 		if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
15725 			ereport(ERROR,
15726 					(errcode(ERRCODE_SYNTAX_ERROR),
15727 					 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
15728 	}
15729 
15730 	return list_concat(func_args, columns);
15731 }
15732 
15733 /*
15734  * Determine return type of a TABLE function.  A single result column
15735  * returns setof that column's type; otherwise return setof record.
15736  */
15737 static TypeName *
TableFuncTypeName(List * columns)15738 TableFuncTypeName(List *columns)
15739 {
15740 	TypeName *result;
15741 
15742 	if (list_length(columns) == 1)
15743 	{
15744 		FunctionParameter *p = (FunctionParameter *) linitial(columns);
15745 
15746 		result = copyObject(p->argType);
15747 	}
15748 	else
15749 		result = SystemTypeName("record");
15750 
15751 	result->setof = true;
15752 
15753 	return result;
15754 }
15755 
15756 /*
15757  * Convert a list of (dotted) names to a RangeVar (like
15758  * makeRangeVarFromNameList, but with position support).  The
15759  * "AnyName" refers to the any_name production in the grammar.
15760  */
15761 static RangeVar *
makeRangeVarFromAnyName(List * names,int position,core_yyscan_t yyscanner)15762 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
15763 {
15764 	RangeVar *r = makeNode(RangeVar);
15765 
15766 	switch (list_length(names))
15767 	{
15768 		case 1:
15769 			r->catalogname = NULL;
15770 			r->schemaname = NULL;
15771 			r->relname = strVal(linitial(names));
15772 			break;
15773 		case 2:
15774 			r->catalogname = NULL;
15775 			r->schemaname = strVal(linitial(names));
15776 			r->relname = strVal(lsecond(names));
15777 			break;
15778 		case 3:
15779 			r->catalogname = strVal(linitial(names));
15780 			r->schemaname = strVal(lsecond(names));
15781 			r->relname = strVal(lthird(names));
15782 			break;
15783 		default:
15784 			ereport(ERROR,
15785 					(errcode(ERRCODE_SYNTAX_ERROR),
15786 					 errmsg("improper qualified name (too many dotted names): %s",
15787 							NameListToString(names)),
15788 					 parser_errposition(position)));
15789 			break;
15790 	}
15791 
15792 	r->relpersistence = RELPERSISTENCE_PERMANENT;
15793 	r->location = position;
15794 
15795 	return r;
15796 }
15797 
15798 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
15799 static void
SplitColQualList(List * qualList,List ** constraintList,CollateClause ** collClause,core_yyscan_t yyscanner)15800 SplitColQualList(List *qualList,
15801 				 List **constraintList, CollateClause **collClause,
15802 				 core_yyscan_t yyscanner)
15803 {
15804 	ListCell   *cell;
15805 	ListCell   *prev;
15806 	ListCell   *next;
15807 
15808 	*collClause = NULL;
15809 	prev = NULL;
15810 	for (cell = list_head(qualList); cell; cell = next)
15811 	{
15812 		Node   *n = (Node *) lfirst(cell);
15813 
15814 		next = lnext(cell);
15815 		if (IsA(n, Constraint))
15816 		{
15817 			/* keep it in list */
15818 			prev = cell;
15819 			continue;
15820 		}
15821 		if (IsA(n, CollateClause))
15822 		{
15823 			CollateClause *c = (CollateClause *) n;
15824 
15825 			if (*collClause)
15826 				ereport(ERROR,
15827 						(errcode(ERRCODE_SYNTAX_ERROR),
15828 						 errmsg("multiple COLLATE clauses not allowed"),
15829 						 parser_errposition(c->location)));
15830 			*collClause = c;
15831 		}
15832 		else
15833 			elog(ERROR, "unexpected node type %d", (int) n->type);
15834 		/* remove non-Constraint nodes from qualList */
15835 		qualList = list_delete_cell(qualList, cell, prev);
15836 	}
15837 	*constraintList = qualList;
15838 }
15839 
15840 /*
15841  * Process result of ConstraintAttributeSpec, and set appropriate bool flags
15842  * in the output command node.  Pass NULL for any flags the particular
15843  * command doesn't support.
15844  */
15845 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)15846 processCASbits(int cas_bits, int location, const char *constrType,
15847 			   bool *deferrable, bool *initdeferred, bool *not_valid,
15848 			   bool *no_inherit, core_yyscan_t yyscanner)
15849 {
15850 	/* defaults */
15851 	if (deferrable)
15852 		*deferrable = false;
15853 	if (initdeferred)
15854 		*initdeferred = false;
15855 	if (not_valid)
15856 		*not_valid = false;
15857 
15858 	if (cas_bits & (CAS_DEFERRABLE | CAS_INITIALLY_DEFERRED))
15859 	{
15860 		if (deferrable)
15861 			*deferrable = true;
15862 		else
15863 			ereport(ERROR,
15864 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15865 					 /* translator: %s is CHECK, UNIQUE, or similar */
15866 					 errmsg("%s constraints cannot be marked DEFERRABLE",
15867 							constrType),
15868 					 parser_errposition(location)));
15869 	}
15870 
15871 	if (cas_bits & CAS_INITIALLY_DEFERRED)
15872 	{
15873 		if (initdeferred)
15874 			*initdeferred = true;
15875 		else
15876 			ereport(ERROR,
15877 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15878 					 /* translator: %s is CHECK, UNIQUE, or similar */
15879 					 errmsg("%s constraints cannot be marked DEFERRABLE",
15880 							constrType),
15881 					 parser_errposition(location)));
15882 	}
15883 
15884 	if (cas_bits & CAS_NOT_VALID)
15885 	{
15886 		if (not_valid)
15887 			*not_valid = true;
15888 		else
15889 			ereport(ERROR,
15890 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15891 					 /* translator: %s is CHECK, UNIQUE, or similar */
15892 					 errmsg("%s constraints cannot be marked NOT VALID",
15893 							constrType),
15894 					 parser_errposition(location)));
15895 	}
15896 
15897 	if (cas_bits & CAS_NO_INHERIT)
15898 	{
15899 		if (no_inherit)
15900 			*no_inherit = true;
15901 		else
15902 			ereport(ERROR,
15903 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15904 					 /* translator: %s is CHECK, UNIQUE, or similar */
15905 					 errmsg("%s constraints cannot be marked NO INHERIT",
15906 							constrType),
15907 					 parser_errposition(location)));
15908 	}
15909 }
15910 
15911 /*----------
15912  * Recursive view transformation
15913  *
15914  * Convert
15915  *
15916  *     CREATE RECURSIVE VIEW relname (aliases) AS query
15917  *
15918  * to
15919  *
15920  *     CREATE VIEW relname (aliases) AS
15921  *         WITH RECURSIVE relname (aliases) AS (query)
15922  *         SELECT aliases FROM relname
15923  *
15924  * Actually, just the WITH ... part, which is then inserted into the original
15925  * view definition as the query.
15926  * ----------
15927  */
15928 static Node *
makeRecursiveViewSelect(char * relname,List * aliases,Node * query)15929 makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
15930 {
15931 	SelectStmt *s = makeNode(SelectStmt);
15932 	WithClause *w = makeNode(WithClause);
15933 	CommonTableExpr *cte = makeNode(CommonTableExpr);
15934 	List	   *tl = NIL;
15935 	ListCell   *lc;
15936 
15937 	/* create common table expression */
15938 	cte->ctename = relname;
15939 	cte->aliascolnames = aliases;
15940 	cte->ctequery = query;
15941 	cte->location = -1;
15942 
15943 	/* create WITH clause and attach CTE */
15944 	w->recursive = true;
15945 	w->ctes = list_make1(cte);
15946 	w->location = -1;
15947 
15948 	/* create target list for the new SELECT from the alias list of the
15949 	 * recursive view specification */
15950 	foreach (lc, aliases)
15951 	{
15952 		ResTarget *rt = makeNode(ResTarget);
15953 
15954 		rt->name = NULL;
15955 		rt->indirection = NIL;
15956 		rt->val = makeColumnRef(strVal(lfirst(lc)), NIL, -1, 0);
15957 		rt->location = -1;
15958 
15959 		tl = lappend(tl, rt);
15960 	}
15961 
15962 	/* create new SELECT combining WITH clause, target list, and fake FROM
15963 	 * clause */
15964 	s->withClause = w;
15965 	s->targetList = tl;
15966 	s->fromClause = list_make1(makeRangeVar(NULL, relname, -1));
15967 
15968 	return (Node *) s;
15969 }
15970 
15971 /* parser_init()
15972  * Initialize to parse one query string
15973  */
15974 void
parser_init(base_yy_extra_type * yyext)15975 parser_init(base_yy_extra_type *yyext)
15976 {
15977 	yyext->parsetree = NIL;		/* in case grammar forgets to set it */
15978 }
15979 
15980