1 %{
2
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5 *
6 * gram.y
7 * POSTGRESQL BISON rules/actions
8 *
9 * Portions Copyright (c) 2003-2019, PgPool Global Development Group
10 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
12 *
13 *
14 * IDENTIFICATION
15 * src/backend/parser/gram.y
16 *
17 * HISTORY
18 * AUTHOR DATE MAJOR EVENT
19 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
20 * Andrew Yu Oct, 1994 lispy code conversion
21 *
22 * NOTES
23 * CAPITALS are used to represent terminal symbols.
24 * non-capitals are used to represent non-terminals.
25 *
26 * In general, nothing in this file should initiate database accesses
27 * nor depend on changeable state (such as SET variables). If you do
28 * database accesses, your code will fail when we have aborted the
29 * current transaction and are just parsing commands to find the next
30 * ROLLBACK or COMMIT. If you make use of SET variables, then you
31 * will do the wrong thing in multi-query strings like this:
32 * SET constraint_exclusion TO off; SELECT * FROM foo;
33 * because the entire string is parsed by gram.y before the SET gets
34 * executed. Anything that depends on the database or changeable state
35 * should be handled during parse analysis so that it happens at the
36 * right time not the wrong time.
37 *
38 * WARNINGS
39 * If you use a list, make sure the datum is a node so that the printing
40 * routines work.
41 *
42 * Sometimes we assign constants to makeStrings. Make sure we don't free
43 * those.
44 *
45 *-------------------------------------------------------------------------
46 */
47
48 #include "pool_parser.h"
49 #include "utils/elog.h"
50 #include "utils/palloc.h"
51 #include <ctype.h>
52 #include <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56
57 #include "nodes.h"
58 #include "keywords.h"
59 #include "gramparse.h"
60 #include "makefuncs.h"
61 #include "pool_string.h"
62 #include "parser.h"
63 #include "pg_class.h"
64 #include "pg_trigger.h"
65
66 /* This is a configuration parameter since PostgreSQL 9.5.
67 * We set this false in pgpool-II. This is default in PostgreSQL.
68 */
69 static bool operator_precedence_warning = false;
70
71 /*
72 * Definition taken from
73 * postgreSQL source code file: src/include/commands/trigger.h
74 */
75 #define TRIGGER_FIRES_ON_ORIGIN 'O'
76 #define TRIGGER_FIRES_ALWAYS 'A'
77 #define TRIGGER_FIRES_ON_REPLICA 'R'
78 #define TRIGGER_DISABLED 'D'
79
80 /*
81 * Definition taken from
82 * postgreSQL source code file: src/include/catalog/pg_class.h
83 */
84
85 #define REPLICA_IDENTITY_DEFAULT 'd'
86 #define REPLICA_IDENTITY_NOTHING 'n'
87 #define REPLICA_IDENTITY_FULL 'f'
88 #define REPLICA_IDENTITY_INDEX 'i'
89
90 /*
91 * Definition taken from
92 * postgreSQL source code file: src/include/catalog/pg_attribute.h
93 */
94 #define ATTRIBUTE_IDENTITY_ALWAYS 'a'
95 #define ATTRIBUTE_IDENTITY_BY_DEFAULT 'd'
96
97 /*
98 * Definition taken from
99 * postgreSQL source code file: src/include/utils/xml.h
100 */
101 typedef enum
102 {
103 XML_STANDALONE_YES,
104 XML_STANDALONE_NO,
105 XML_STANDALONE_NO_VALUE,
106 XML_STANDALONE_OMITTED
107 } XmlStandaloneType;
108
109 /*
110 * Location tracking support --- simpler than bison's default, since we only
111 * want to track the start position not the end position of each nonterminal.
112 */
113 #define YYLLOC_DEFAULT(Current, Rhs, N) \
114 do { \
115 if ((N) > 0) \
116 (Current) = (Rhs)[1]; \
117 else \
118 (Current) = (-1); \
119 } while (0)
120
121 /*
122 * The above macro assigns -1 (unknown) as the parse location of any
123 * nonterminal that was reduced from an empty rule, or whose leftmost
124 * component was reduced from an empty rule. This is problematic
125 * for nonterminals defined like
126 * OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ;
127 * because we'll set -1 as the location during the first reduction and then
128 * copy it during each subsequent reduction, leaving us with -1 for the
129 * location even when the list is not empty. To fix that, do this in the
130 * action for the nonempty rule(s):
131 * if (@$ < 0) @$ = @2;
132 * (Although we have many nonterminals that follow this pattern, we only
133 * bother with fixing @$ like this when the nonterminal's parse location
134 * is actually referenced in some rule.)
135 *
136 * A cleaner answer would be to make YYLLOC_DEFAULT scan all the Rhs
137 * locations until it's found one that's not -1. Then we'd get a correct
138 * location for any nonterminal that isn't entirely empty. But this way
139 * would add overhead to every rule reduction, and so far there's not been
140 * a compelling reason to pay that overhead.
141 */
142
143 /*
144 * Bison doesn't allocate anything that needs to live across parser calls,
145 * so we can easily have it use palloc instead of malloc. This prevents
146 * memory leaks if we error out during parsing. Note this only works with
147 * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
148 * if possible, so there's not really much problem anyhow, at least if
149 * you're building with gcc.
150 */
151 #define YYMALLOC palloc
152 #define YYFREE pfree
153
154 /* Private struct for the result of privilege_target production */
155 typedef struct PrivTarget
156 {
157 GrantTargetType targtype;
158 ObjectType objtype;
159 List *objs;
160 } PrivTarget;
161
162 /* Private struct for the result of import_qualification production */
163 typedef struct ImportQual
164 {
165 ImportForeignSchemaType type;
166 List *table_names;
167 } ImportQual;
168
169 /* ConstraintAttributeSpec yields an integer bitmask of these flags: */
170 #define CAS_NOT_DEFERRABLE 0x01
171 #define CAS_DEFERRABLE 0x02
172 #define CAS_INITIALLY_IMMEDIATE 0x04
173 #define CAS_INITIALLY_DEFERRED 0x08
174 #define CAS_NOT_VALID 0x10
175 #define CAS_NO_INHERIT 0x20
176
177
178 #define parser_yyerror(msg) scanner_yyerror(msg, yyscanner)
179 #define parser_errposition(pos) scanner_errposition(pos, yyscanner)
180
181 static void 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 CallStmt ClosePortalStmt ClusterStmt CommentStmt
294 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
295 CreateDomainStmt CreateExtensionStmt CreateGroupStmt CreateOpClassStmt
296 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
297 CreateSchemaStmt CreateSeqStmt CreateStmt CreateStatsStmt CreateTableSpaceStmt
298 CreateFdwStmt CreateForeignServerStmt CreateForeignTableStmt
299 CreateAssertionStmt CreateTransformStmt CreateTrigStmt CreateEventTrigStmt
300 CreateUserStmt CreateUserMappingStmt CreateRoleStmt CreatePolicyStmt
301 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
302 DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
303 DropCastStmt DropRoleStmt
304 DropdbStmt DropTableSpaceStmt
305 DropTransformStmt
306 DropUserMappingStmt ExplainStmt FetchStmt
307 GrantStmt GrantRoleStmt ImportForeignSchemaStmt IndexStmt InsertStmt
308 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
314 VacuumStmt
315 VariableResetStmt VariableSetStmt VariableShowStmt
316 ViewStmt CheckPointStmt CreateConversionStmt
317 DeallocateStmt PrepareStmt ExecuteStmt
318 DropOwnedStmt ReassignOwnedStmt
319 AlterTSConfigurationStmt AlterTSDictionaryStmt
320 CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
321 CreatePublicationStmt AlterPublicationStmt
322 CreateSubscriptionStmt AlterSubscriptionStmt DropSubscriptionStmt
323
324 %type <node> select_no_parens select_with_parens select_clause
325 simple_select values_clause
326
327 %type <node> alter_column_default opclass_item opclass_drop alter_using
328 %type <ival> add_drop opt_asc_desc opt_nulls_order
329
330 %type <node> alter_table_cmd alter_type_cmd opt_collate_clause
331 replica_identity partition_cmd index_partition_cmd
332 %type <list> alter_table_cmds alter_type_cmds
333 %type <list> alter_identity_column_option_list
334 %type <defelt> alter_identity_column_option
335
336 %type <dbehavior> opt_drop_behavior
337
338 %type <list> createdb_opt_list createdb_opt_items copy_opt_list
339 transaction_mode_list
340 create_extension_opt_list alter_extension_opt_list
341 %type <defelt> createdb_opt_item copy_opt_item
342 transaction_mode_item
343 create_extension_opt_item alter_extension_opt_item
344
345 %type <ival> opt_lock lock_type cast_context
346 %type <str> vac_analyze_option_name
347 %type <defelt> vac_analyze_option_elem
348 %type <list> vac_analyze_option_list
349 %type <node> vac_analyze_option_arg
350 %type <boolean> opt_or_replace
351 opt_grant_grant_option opt_grant_admin_option
352 opt_nowait opt_if_exists opt_with_data
353 opt_transaction_chain
354 %type <ival> opt_nowait_or_skip
355
356 %type <list> OptRoleList AlterOptRoleList
357 %type <defelt> CreateOptRoleElem AlterOptRoleElem
358
359 %type <str> opt_type
360 %type <str> foreign_server_version opt_foreign_server_version
361 %type <str> opt_in_database
362
363 %type <str> OptSchemaName
364 %type <list> OptSchemaEltList
365
366 %type <chr> am_type
367
368 %type <boolean> TriggerForSpec TriggerForType
369 %type <ival> TriggerActionTime
370 %type <list> TriggerEvents TriggerOneEvent
371 %type <value> TriggerFuncArg
372 %type <node> TriggerWhen
373 %type <str> TransitionRelName
374 %type <boolean> TransitionRowOrTable TransitionOldOrNew
375 %type <node> TriggerTransition
376
377 %type <list> event_trigger_when_list event_trigger_value_list
378 %type <defelt> event_trigger_when_item
379 %type <chr> enable_trigger
380
381 %type <str> copy_file_name
382 database_name access_method_clause access_method attr_name
383 table_access_method_clause name cursor_name file_name
384 index_name opt_index_name cluster_index_specification
385
386 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
387 opt_class opt_inline_handler opt_validator validator_clause
388 opt_collate
389
390 %type <range> qualified_name insert_target OptConstrFromTable
391
392 %type <str> all_Op MathOp
393
394 %type <str> row_security_cmd RowSecurityDefaultForCmd
395 %type <boolean> RowSecurityDefaultPermissive
396 %type <node> RowSecurityOptionalWithCheck RowSecurityOptionalExpr
397 %type <list> RowSecurityDefaultToRole RowSecurityOptionalToRole
398
399 %type <str> iso_level opt_encoding
400 %type <rolespec> grantee
401 %type <list> grantee_list
402 %type <accesspriv> privilege
403 %type <list> privileges privilege_list
404 %type <privtarget> privilege_target
405 %type <objwithargs> function_with_argtypes aggregate_with_argtypes operator_with_argtypes
406 %type <list> function_with_argtypes_list aggregate_with_argtypes_list operator_with_argtypes_list
407 %type <ival> defacl_privilege_target
408 %type <defelt> DefACLOption
409 %type <list> DefACLOptionList
410 %type <ival> import_qualification_type
411 %type <importqual> import_qualification
412 %type <node> vacuum_relation
413
414 %type <list> stmtblock stmtmulti
415 OptTableElementList TableElementList OptInherit definition
416 OptTypedTableElementList TypedTableElementList
417 reloptions opt_reloptions
418 OptWith distinct_clause opt_all_clause opt_definition func_args func_args_list
419 func_args_with_defaults func_args_with_defaults_list
420 aggr_args aggr_args_list
421 func_as createfunc_opt_list alterfunc_opt_list
422 old_aggr_definition old_aggr_list
423 oper_argtypes RuleActionList RuleActionMulti
424 opt_column_list columnList opt_name_list
425 sort_clause opt_sort_clause sortby_list index_params
426 opt_include opt_c_include index_including_params
427 name_list role_list from_clause from_list opt_array_bounds
428 qualified_name_list any_name any_name_list type_name_list
429 any_operator expr_list attrs
430 target_list opt_target_list insert_column_list set_target_list
431 set_clause_list set_clause
432 def_list operator_def_list indirection opt_indirection
433 reloption_list group_clause TriggerFuncArgs select_limit
434 opt_select_limit opclass_item_list opclass_drop_list
435 opclass_purpose opt_opfamily transaction_mode_list_or_empty
436 OptTableFuncElementList TableFuncElementList opt_type_modifiers
437 prep_type_clause
438 execute_param_clause using_clause returning_clause
439 opt_enum_val_list enum_val_list table_func_column_list
440 create_generic_options alter_generic_options
441 relation_expr_list dostmt_opt_list
442 transform_element_list transform_type_list
443 TriggerTransitions TriggerReferencing
444 publication_name_list
445 vacuum_relation_list opt_vacuum_relation_list
446
447 %type <list> group_by_list
448 %type <node> group_by_item empty_grouping_set rollup_clause cube_clause
449 %type <node> grouping_sets_clause
450 %type <node> opt_publication_for_tables publication_for_tables
451 %type <value> publication_name_item
452
453 %type <list> opt_fdw_options fdw_options
454 %type <defelt> fdw_option
455
456 %type <range> OptTempTableName
457 %type <into> into_clause create_as_target create_mv_target
458
459 %type <defelt> createfunc_opt_item common_func_opt_item dostmt_opt_item
460 %type <fun_param> func_arg func_arg_with_default table_func_column aggr_arg
461 %type <fun_param_mode> arg_class
462 %type <typnam> func_return func_type
463
464 %type <boolean> opt_trusted opt_restart_seqs
465 %type <ival> OptTemp
466 %type <ival> OptNoLog
467 %type <oncommit> OnCommitOption
468
469 %type <ival> for_locking_strength
470 %type <node> for_locking_item
471 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
472 %type <list> locked_rels_list
473 %type <boolean> all_or_distinct
474
475 %type <node> join_outer join_qual
476 %type <jtype> join_type
477
478 %type <list> extract_list overlay_list position_list
479 %type <list> substr_list trim_list
480 %type <list> opt_interval interval_second
481 %type <node> overlay_placing substr_from substr_for
482
483 %type <boolean> opt_instead
484 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
485 %type <boolean> opt_freeze opt_analyze opt_default opt_recheck
486 %type <defelt> opt_binary copy_delimiter
487
488 %type <boolean> copy_from opt_program
489
490 %type <ival> opt_column event cursor_options opt_hold opt_set_data
491 %type <objtype> drop_type_any_name drop_type_name drop_type_name_on_any_name
492 comment_type_any_name comment_type_name
493 security_label_type_any_name security_label_type_name
494
495 %type <node> fetch_args limit_clause select_limit_value
496 offset_clause select_offset_value
497 select_fetch_first_value I_or_F_const
498 %type <ival> row_or_rows first_or_next
499
500 %type <list> OptSeqOptList SeqOptList OptParenthesizedSeqOptList
501 %type <defelt> SeqOptElem
502
503 %type <istmt> insert_rest
504 %type <infer> opt_conf_expr
505 %type <onconflict> opt_on_conflict
506
507 %type <vsetstmt> generic_set set_rest set_rest_more generic_reset reset_rest
508 SetResetClause FunctionSetResetClause
509
510 %type <node> TableElement TypedTableElement ConstraintElem TableFuncElement
511 %type <node> columnDef columnOptions
512 %type <defelt> def_elem reloption_elem old_aggr_elem operator_def_elem
513 %type <node> def_arg columnElem where_clause where_or_current_clause
514 a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
515 columnref in_expr having_clause func_table xmltable array_expr
516 ExclusionWhereClause operator_def_arg
517 %type <list> rowsfrom_item rowsfrom_list opt_col_def_list
518 %type <boolean> opt_ordinality
519 %type <list> ExclusionConstraintList ExclusionConstraintElem
520 %type <list> func_arg_list
521 %type <node> func_arg_expr
522 %type <list> row explicit_row implicit_row type_list array_expr_list
523 %type <node> case_expr case_arg when_clause case_default
524 %type <list> when_clause_list
525 %type <ival> sub_type opt_materialized
526 %type <value> NumericOnly
527 %type <list> NumericOnly_list
528 %type <alias> alias_clause opt_alias_clause
529 %type <list> func_alias_clause
530 %type <sortby> sortby
531 %type <ielem> index_elem
532 %type <node> table_ref
533 %type <jexpr> joined_table
534 %type <range> relation_expr
535 %type <range> relation_expr_opt_alias
536 %type <node> tablesample_clause opt_repeatable_clause
537 %type <target> target_el set_target insert_column_item
538
539 %type <str> generic_option_name
540 %type <node> generic_option_arg
541 %type <defelt> generic_option_elem alter_generic_option_elem
542 %type <list> generic_option_list alter_generic_option_list
543 %type <str> explain_option_name
544 %type <node> explain_option_arg
545 %type <defelt> explain_option_elem
546 %type <list> explain_option_list
547
548 %type <ival> reindex_target_type reindex_target_multitable
549 %type <ival> reindex_option_list reindex_option_elem
550
551 %type <node> copy_generic_opt_arg copy_generic_opt_arg_list_item
552 %type <defelt> copy_generic_opt_elem
553 %type <list> copy_generic_opt_list copy_generic_opt_arg_list
554 %type <list> copy_options
555
556 %type <typnam> Typename SimpleTypename ConstTypename
557 GenericType Numeric opt_float
558 Character ConstCharacter
559 CharacterWithLength CharacterWithoutLength
560 ConstDatetime ConstInterval
561 Bit ConstBit BitWithLength BitWithoutLength
562 %type <str> character
563 %type <str> extract_arg
564 %type <boolean> opt_varying opt_timezone opt_no_inherit
565
566 %type <ival> Iconst SignedIconst
567 %type <str> Sconst comment_text notify_payload
568 %type <str> RoleId opt_boolean_or_string
569 %type <list> var_list
570 %type <str> ColId ColLabel var_name type_function_name param_name
571 %type <str> NonReservedWord NonReservedWord_or_Sconst
572 %type <str> createdb_opt_name
573 %type <node> var_value zone_value
574 %type <rolespec> auth_ident RoleSpec opt_granted_by
575
576 %type <keyword> unreserved_keyword type_func_name_keyword
577 %type <keyword> col_name_keyword reserved_keyword
578
579 %type <node> TableConstraint TableLikeClause
580 %type <ival> TableLikeOptionList TableLikeOption
581 %type <list> ColQualList
582 %type <node> ColConstraint ColConstraintElem ConstraintAttr
583 %type <ival> key_actions key_delete key_match key_update key_action
584 %type <ival> ConstraintAttributeSpec ConstraintAttributeElem
585 %type <str> ExistingIndex
586
587 %type <list> constraints_set_list
588 %type <boolean> constraints_set_mode
589 %type <str> OptTableSpace OptConsTableSpace
590 %type <rolespec> OptTableSpaceOwner
591 %type <ival> opt_check_option
592
593 %type <str> opt_provider security_label
594
595 %type <target> xml_attribute_el
596 %type <list> xml_attribute_list xml_attributes
597 %type <node> xml_root_version opt_xml_root_standalone
598 %type <node> xmlexists_argument
599 %type <ival> document_or_content
600 %type <boolean> xml_whitespace_option
601 %type <list> xmltable_column_list xmltable_column_option_list
602 %type <node> xmltable_column_el
603 %type <defelt> xmltable_column_option_el
604 %type <list> xml_namespace_list
605 %type <target> xml_namespace_el
606
607 %type <node> func_application func_expr_common_subexpr
608 %type <node> func_expr func_expr_windowless
609 %type <node> common_table_expr
610 %type <with> with_clause opt_with_clause
611 %type <list> cte_list
612
613 %type <list> within_group_clause
614 %type <node> filter_clause
615 %type <list> window_clause window_definition_list opt_partition_clause
616 %type <windef> window_definition over_clause window_specification
617 opt_frame_clause frame_extent frame_bound
618 %type <ival> opt_window_exclusion_clause
619 %type <str> opt_existing_window_name
620 %type <boolean> opt_if_not_exists
621 %type <ival> generated_when override_kind
622 %type <partspec> PartitionSpec OptPartitionSpec
623 %type <str> part_strategy
624 %type <partelem> part_elem
625 %type <list> part_params
626 %type <partboundspec> PartitionBoundSpec
627 %type <list> hash_partbound
628 %type <defelt> hash_partbound_elem
629
630 /*
631 * Non-keyword token types. These are hard-wired into the "flex" lexer.
632 * They must be listed first so that their numeric codes do not depend on
633 * the set of keywords. PL/pgSQL depends on this so that it can share the
634 * same lexer. If you add/change tokens here, fix PL/pgSQL to match!
635 *
636 * DOT_DOT is unused in the core SQL grammar, and so will always provoke
637 * parse errors. It is needed by PL/pgSQL.
638 */
639 %token <str> IDENT FCONST SCONST BCONST XCONST Op
640 %token <ival> ICONST PARAM
641 %token TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
642 %token LESS_EQUALS GREATER_EQUALS NOT_EQUALS
643
644 /*
645 * If you want to make any keyword changes, update the keyword table in
646 * src/include/parser/kwlist.h and add new keywords to the appropriate one
647 * of the reserved-or-not-so-reserved keyword lists, below; search
648 * this file for "Keyword category lists".
649 */
650
651 /* ordinary key words in alphabetical order */
652 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
653 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
654 ASSERTION ASSIGNMENT ASYMMETRIC AT ATTACH ATTRIBUTE AUTHORIZATION
655
656 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
657 BOOLEAN_P BOTH BY
658
659 CACHE CALL CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
660 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
661 CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMENT COMMENTS COMMIT
662 COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION CONSTRAINT
663 CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE
664 CROSS CSV CUBE CURRENT_P
665 CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
666 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
667
668 DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
669 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DEPENDS DESC
670 DETACH DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P
671 DOUBLE_P DROP
672
673 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EVENT EXCEPT
674 EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
675 EXTENSION EXTERNAL EXTRACT
676
677 FALSE_P FAMILY FETCH FILTER FIRST_P FLOAT_P FOLLOWING FOR
678 FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
679
680 GENERATED GLOBAL GRANT GRANTED GREATEST GROUP_P GROUPING GROUPS
681
682 HANDLER HAVING HEADER_P HOLD HOUR_P
683
684 IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P INCLUDE
685 INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
686 INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
687 INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
688
689 JOIN
690
691 KEY
692
693 LABEL LANGUAGE LARGE_P LAST_P LATERAL_P
694 LEADING LEAKPROOF LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL
695 LOCALTIME LOCALTIMESTAMP LOCATION LOCK_P LOCKED LOGGED
696
697 MAPPING MATCH MATERIALIZED MAXVALUE METHOD MINUTE_P MINVALUE MODE MONTH_P MOVE
698
699 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NONE
700 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
701 NULLS_P NUMERIC
702
703 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
704 ORDER ORDINALITY OTHERS OUT_P OUTER_P
705 OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
706
707 PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PGPOOL PLACING PLANS POLICY
708 POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
709 PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROCEDURES PROGRAM PUBLICATION
710
711 QUOTE
712
713 RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REFERENCING
714 REFRESH REINDEX RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
715 RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLUP
716 ROUTINE ROUTINES ROW ROWS RULE
717
718 SAVEPOINT SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
719 SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
720 SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P
721 START STATEMENT STATISTICS STDIN STDOUT STORAGE STORED STRICT_P STRIP_P
722 SUBSCRIPTION SUBSTRING SUPPORT SYMMETRIC SYSID SYSTEM_P
723
724 TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
725 TIES TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM
726 TREAT TRIGGER TRIM TRUE_P
727 TRUNCATE TRUSTED TYPE_P TYPES_P
728
729 UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNLOGGED
730 UNTIL UPDATE USER USING
731
732 VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
733 VERBOSE VERSION_P VIEW VIEWS VOLATILE
734
735 WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE
736
737 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLNAMESPACES
738 XMLPARSE XMLPI XMLROOT XMLSERIALIZE XMLTABLE
739
740 YEAR_P YES_P
741
742 ZONE
743
744 /*
745 * The grammar thinks these are keywords, but they are not in the kwlist.h
746 * list and so can never be entered directly. The filter in parser.c
747 * creates these tokens when required (based on looking one token ahead).
748 *
749 * NOT_LA exists so that productions such as NOT LIKE can be given the same
750 * precedence as LIKE; otherwise they'd effectively have the same precedence
751 * as NOT, at least with respect to their left-hand subexpression.
752 * NULLS_LA and WITH_LA are needed to make the grammar LALR(1).
753 */
754 %token NOT_LA NULLS_LA WITH_LA
755
756
757 /* Precedence: lowest to highest */
758 %nonassoc SET /* see relation_expr_opt_alias */
759 %left UNION EXCEPT
760 %left INTERSECT
761 %left OR
762 %left AND
763 %right NOT
764 %nonassoc IS ISNULL NOTNULL /* IS sets precedence for IS NULL, etc */
765 %nonassoc '<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
766 %nonassoc BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA
767 %nonassoc ESCAPE /* ESCAPE must be just above LIKE/ILIKE/SIMILAR */
768 %left POSTFIXOP /* dummy for postfix Op rules */
769 /*
770 * To support target_el without AS, we must give IDENT an explicit priority
771 * between POSTFIXOP and Op. We can safely assign the same priority to
772 * various unreserved keywords as needed to resolve ambiguities (this can't
773 * have any bad effects since obviously the keywords will still behave the
774 * same as if they weren't keywords). We need to do this:
775 * for PARTITION, RANGE, ROWS, GROUPS to support opt_existing_window_name;
776 * for RANGE, ROWS, GROUPS so that they can follow a_expr without creating
777 * postfix-operator problems;
778 * for GENERATED so that it can follow b_expr;
779 * and for NULL so that it can follow b_expr in ColQualList without creating
780 * postfix-operator problems.
781 *
782 * To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
783 * an explicit priority lower than '(', so that a rule with CUBE '(' will shift
784 * rather than reducing a conflicting rule that takes CUBE as a function name.
785 * Using the same precedence as IDENT seems right for the reasons given above.
786 *
787 * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
788 * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
789 * there is no principled way to distinguish these from the productions
790 * a_expr PRECEDING/FOLLOWING. We hack this up by giving UNBOUNDED slightly
791 * lower precedence than PRECEDING and FOLLOWING. At present this doesn't
792 * appear to cause UNBOUNDED to be treated differently from other unreserved
793 * keywords anywhere else in the grammar, but it's definitely risky. We can
794 * blame any funny behavior of UNBOUNDED on the SQL standard, though.
795 */
796 %nonassoc UNBOUNDED /* ideally should have same precedence as IDENT */
797 %nonassoc IDENT GENERATED NULL_P PARTITION RANGE ROWS GROUPS PRECEDING FOLLOWING CUBE ROLLUP
798 %left Op OPERATOR /* multi-character ops and user-defined operators */
799 %left '+' '-'
800 %left '*' '/' '%'
801 %left '^'
802 /* Unary Operators */
803 %left AT /* sets precedence for AT TIME ZONE */
804 %left COLLATE
805 %right UMINUS
806 %left '[' ']'
807 %left '(' ')'
808 %left TYPECAST
809 %left '.'
810 /*
811 * These might seem to be low-precedence, but actually they are not part
812 * of the arithmetic hierarchy at all in their use as JOIN operators.
813 * We make them high-precedence to support their use as function names.
814 * They wouldn't be given a precedence at all, were it not that we need
815 * left-associativity among the JOIN rules themselves.
816 */
817 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
818 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
819 %right PRESERVE STRIP_P
820
821 %%
822
823 /*
824 * The target production for the whole parse.
825 */
826 stmtblock: stmtmulti
827 {
828 pg_yyget_extra(yyscanner)->parsetree = $1;
829 }
830 ;
831
832 /*
833 * At top level, we wrap each stmt with a RawStmt node carrying start location
834 * and length of the stmt's text. Notice that the start loc/len are driven
835 * entirely from semicolon locations (@2). It would seem natural to use
836 * @1 or @3 to get the true start location of a stmt, but that doesn't work
837 * for statements that can start with empty nonterminals (opt_with_clause is
838 * the main offender here); as noted in the comments for YYLLOC_DEFAULT,
839 * we'd get -1 for the location in such cases.
840 * We also take care to discard empty statements entirely.
841 */
842 stmtmulti: stmtmulti ';' stmt
843 {
844 if ($1 != NIL)
845 {
846 /* update length of previous stmt */
847 updateRawStmtEnd(llast_node(RawStmt, $1), @2);
848 }
849 if ($3 != NULL)
850 $$ = lappend($1, makeRawStmt($3, @2 + 1));
851 else
852 $$ = $1;
853 }
854 | stmt
855 {
856 if ($1 != NULL)
857 $$ = list_make1(makeRawStmt($1, 0));
858 else
859 $$ = NIL;
860 }
861 ;
862
863 stmt :
864 AlterEventTrigStmt
865 | AlterCollationStmt
866 | AlterDatabaseStmt
867 | AlterDatabaseSetStmt
868 | AlterDefaultPrivilegesStmt
869 | AlterDomainStmt
870 | AlterEnumStmt
871 | AlterExtensionStmt
872 | AlterExtensionContentsStmt
873 | AlterFdwStmt
874 | AlterForeignServerStmt
875 | AlterForeignTableStmt
876 | AlterFunctionStmt
877 | AlterGroupStmt
878 | AlterObjectDependsStmt
879 | AlterObjectSchemaStmt
880 | AlterOwnerStmt
881 | AlterOperatorStmt
882 | AlterPolicyStmt
883 | AlterSeqStmt
884 | AlterSystemStmt
885 | AlterTableStmt
886 | AlterTblSpcStmt
887 | AlterCompositeTypeStmt
888 | AlterPublicationStmt
889 | AlterRoleSetStmt
890 | AlterRoleStmt
891 | AlterSubscriptionStmt
892 | AlterTSConfigurationStmt
893 | AlterTSDictionaryStmt
894 | AlterUserMappingStmt
895 | AnalyzeStmt
896 | CallStmt
897 | CheckPointStmt
898 | ClosePortalStmt
899 | ClusterStmt
900 | CommentStmt
901 | ConstraintsSetStmt
902 | CopyStmt
903 | CreateAmStmt
904 | CreateAsStmt
905 | CreateAssertionStmt
906 | CreateCastStmt
907 | CreateConversionStmt
908 | CreateDomainStmt
909 | CreateExtensionStmt
910 | CreateFdwStmt
911 | CreateForeignServerStmt
912 | CreateForeignTableStmt
913 | CreateFunctionStmt
914 | CreateGroupStmt
915 | CreateMatViewStmt
916 | CreateOpClassStmt
917 | CreateOpFamilyStmt
918 | CreatePublicationStmt
919 | AlterOpFamilyStmt
920 | CreatePolicyStmt
921 | CreatePLangStmt
922 | CreateSchemaStmt
923 | CreateSeqStmt
924 | CreateStmt
925 | CreateSubscriptionStmt
926 | CreateStatsStmt
927 | CreateTableSpaceStmt
928 | CreateTransformStmt
929 | CreateTrigStmt
930 | CreateEventTrigStmt
931 | CreateRoleStmt
932 | CreateUserStmt
933 | CreateUserMappingStmt
934 | CreatedbStmt
935 | DeallocateStmt
936 | DeclareCursorStmt
937 | DefineStmt
938 | DeleteStmt
939 | DiscardStmt
940 | DoStmt
941 | DropCastStmt
942 | DropOpClassStmt
943 | DropOpFamilyStmt
944 | DropOwnedStmt
945 | DropPLangStmt
946 | DropStmt
947 | DropSubscriptionStmt
948 | DropTableSpaceStmt
949 | DropTransformStmt
950 | DropRoleStmt
951 | DropUserMappingStmt
952 | DropdbStmt
953 | ExecuteStmt
954 | ExplainStmt
955 | FetchStmt
956 | GrantStmt
957 | GrantRoleStmt
958 | ImportForeignSchemaStmt
959 | IndexStmt
960 | InsertStmt
961 | ListenStmt
962 | RefreshMatViewStmt
963 | LoadStmt
964 | LockStmt
965 | NotifyStmt
966 | PrepareStmt
967 | ReassignOwnedStmt
968 | ReindexStmt
969 | RemoveAggrStmt
970 | RemoveFuncStmt
971 | RemoveOperStmt
972 | RenameStmt
973 | RevokeStmt
974 | RevokeRoleStmt
975 | RuleStmt
976 | SecLabelStmt
977 | SelectStmt
978 | TransactionStmt
979 | TruncateStmt
980 | UnlistenStmt
981 | UpdateStmt
982 | VacuumStmt
983 | VariableResetStmt
984 | VariableSetStmt
985 | VariableShowStmt
986 | ViewStmt
987 | /*EMPTY*/
988 { $$ = NULL; }
989 ;
990
991 /*****************************************************************************
992 *
993 * CALL statement
994 *
995 *****************************************************************************/
996
997 CallStmt: CALL func_application
998 {
999 CallStmt *n = makeNode(CallStmt);
1000 n->funccall = castNode(FuncCall, $2);
1001 $$ = (Node *)n;
1002 }
1003 ;
1004
1005 /*****************************************************************************
1006 *
1007 * Create a new Postgres DBMS role
1008 *
1009 *****************************************************************************/
1010
1011 CreateRoleStmt:
1012 CREATE ROLE RoleId opt_with OptRoleList
1013 {
1014 CreateRoleStmt *n = makeNode(CreateRoleStmt);
1015 n->stmt_type = ROLESTMT_ROLE;
1016 n->role = $3;
1017 n->options = $5;
1018 $$ = (Node *)n;
1019 }
1020 ;
1021
1022
1023 opt_with: WITH {}
1024 | WITH_LA {}
1025 | /*EMPTY*/ {}
1026 ;
1027
1028 /*
1029 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
1030 * for backwards compatibility). Note: the only option required by SQL99
1031 * is "WITH ADMIN name".
1032 */
1033 OptRoleList:
1034 OptRoleList CreateOptRoleElem { $$ = lappend($1, $2); }
1035 | /* EMPTY */ { $$ = NIL; }
1036 ;
1037
1038 AlterOptRoleList:
1039 AlterOptRoleList AlterOptRoleElem { $$ = lappend($1, $2); }
1040 | /* EMPTY */ { $$ = NIL; }
1041 ;
1042
1043 AlterOptRoleElem:
1044 PASSWORD Sconst
1045 {
1046 $$ = makeDefElem("password",
1047 (Node *)makeString($2), @1);
1048 }
1049 | PASSWORD NULL_P
1050 {
1051 $$ = makeDefElem("password", NULL, @1);
1052 }
1053 | ENCRYPTED PASSWORD Sconst
1054 {
1055 /*
1056 * These days, passwords are always stored in encrypted
1057 * form, so there is no difference between PASSWORD and
1058 * ENCRYPTED PASSWORD.
1059 */
1060 $$ = makeDefElem("password",
1061 (Node *)makeString($3), @1);
1062 }
1063 | UNENCRYPTED PASSWORD Sconst
1064 {
1065 ereport(ERROR,
1066 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1067 errmsg("UNENCRYPTED PASSWORD is no longer supported"),
1068 errhint("Remove UNENCRYPTED to store the password in encrypted form instead."),
1069 parser_errposition(@1)));
1070 }
1071 | INHERIT
1072 {
1073 $$ = makeDefElem("inherit", (Node *)makeInteger(true), @1);
1074 }
1075 | CONNECTION LIMIT SignedIconst
1076 {
1077 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3), @1);
1078 }
1079 | VALID UNTIL Sconst
1080 {
1081 $$ = makeDefElem("validUntil", (Node *)makeString($3), @1);
1082 }
1083 /* Supported but not documented for roles, for use by ALTER GROUP. */
1084 | USER role_list
1085 {
1086 $$ = makeDefElem("rolemembers", (Node *)$2, @1);
1087 }
1088 | IDENT
1089 {
1090 /*
1091 * We handle identifiers that aren't parser keywords with
1092 * the following special-case codes, to avoid bloating the
1093 * size of the main parser.
1094 */
1095 if (strcmp($1, "superuser") == 0)
1096 $$ = makeDefElem("superuser", (Node *)makeInteger(true), @1);
1097 else if (strcmp($1, "nosuperuser") == 0)
1098 $$ = makeDefElem("superuser", (Node *)makeInteger(false), @1);
1099 else if (strcmp($1, "createrole") == 0)
1100 $$ = makeDefElem("createrole", (Node *)makeInteger(true), @1);
1101 else if (strcmp($1, "nocreaterole") == 0)
1102 $$ = makeDefElem("createrole", (Node *)makeInteger(false), @1);
1103 else if (strcmp($1, "replication") == 0)
1104 $$ = makeDefElem("isreplication", (Node *)makeInteger(true), @1);
1105 else if (strcmp($1, "noreplication") == 0)
1106 $$ = makeDefElem("isreplication", (Node *)makeInteger(false), @1);
1107 else if (strcmp($1, "createdb") == 0)
1108 $$ = makeDefElem("createdb", (Node *)makeInteger(true), @1);
1109 else if (strcmp($1, "nocreatedb") == 0)
1110 $$ = makeDefElem("createdb", (Node *)makeInteger(false), @1);
1111 else if (strcmp($1, "login") == 0)
1112 $$ = makeDefElem("canlogin", (Node *)makeInteger(true), @1);
1113 else if (strcmp($1, "nologin") == 0)
1114 $$ = makeDefElem("canlogin", (Node *)makeInteger(false), @1);
1115 else if (strcmp($1, "bypassrls") == 0)
1116 $$ = makeDefElem("bypassrls", (Node *)makeInteger(true), @1);
1117 else if (strcmp($1, "nobypassrls") == 0)
1118 $$ = makeDefElem("bypassrls", (Node *)makeInteger(false), @1);
1119 else if (strcmp($1, "noinherit") == 0)
1120 {
1121 /*
1122 * Note that INHERIT is a keyword, so it's handled by main parser, but
1123 * NOINHERIT is handled here.
1124 */
1125 $$ = makeDefElem("inherit", (Node *)makeInteger(false), @1);
1126 }
1127 else
1128 ereport(ERROR,
1129 (errcode(ERRCODE_SYNTAX_ERROR),
1130 errmsg("unrecognized role option \"%s\"", $1),
1131 parser_errposition(@1)));
1132 }
1133 ;
1134
1135 CreateOptRoleElem:
1136 AlterOptRoleElem { $$ = $1; }
1137 /* The following are not supported by ALTER ROLE/USER/GROUP */
1138 | SYSID Iconst
1139 {
1140 $$ = makeDefElem("sysid", (Node *)makeInteger($2), @1);
1141 }
1142 | ADMIN role_list
1143 {
1144 $$ = makeDefElem("adminmembers", (Node *)$2, @1);
1145 }
1146 | ROLE role_list
1147 {
1148 $$ = makeDefElem("rolemembers", (Node *)$2, @1);
1149 }
1150 | IN_P ROLE role_list
1151 {
1152 $$ = makeDefElem("addroleto", (Node *)$3, @1);
1153 }
1154 | IN_P GROUP_P role_list
1155 {
1156 $$ = makeDefElem("addroleto", (Node *)$3, @1);
1157 }
1158 ;
1159
1160
1161 /*****************************************************************************
1162 *
1163 * Create a new Postgres DBMS user (role with implied login ability)
1164 *
1165 *****************************************************************************/
1166
1167 CreateUserStmt:
1168 CREATE USER RoleId opt_with OptRoleList
1169 {
1170 CreateRoleStmt *n = makeNode(CreateRoleStmt);
1171 n->stmt_type = ROLESTMT_USER;
1172 n->role = $3;
1173 n->options = $5;
1174 $$ = (Node *)n;
1175 }
1176 ;
1177
1178
1179 /*****************************************************************************
1180 *
1181 * Alter a postgresql DBMS role
1182 *
1183 *****************************************************************************/
1184
1185 AlterRoleStmt:
1186 ALTER ROLE RoleSpec opt_with AlterOptRoleList
1187 {
1188 AlterRoleStmt *n = makeNode(AlterRoleStmt);
1189 n->role = $3;
1190 n->action = +1; /* add, if there are members */
1191 n->options = $5;
1192 $$ = (Node *)n;
1193 }
1194 | ALTER USER RoleSpec opt_with AlterOptRoleList
1195 {
1196 AlterRoleStmt *n = makeNode(AlterRoleStmt);
1197 n->role = $3;
1198 n->action = +1; /* add, if there are members */
1199 n->options = $5;
1200 $$ = (Node *)n;
1201 }
1202 ;
1203
1204 opt_in_database:
1205 /* EMPTY */ { $$ = NULL; }
1206 | IN_P DATABASE database_name { $$ = $3; }
1207 ;
1208
1209 AlterRoleSetStmt:
1210 ALTER ROLE RoleSpec opt_in_database SetResetClause
1211 {
1212 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1213 n->role = $3;
1214 n->database = $4;
1215 n->setstmt = $5;
1216 $$ = (Node *)n;
1217 }
1218 | ALTER ROLE ALL opt_in_database SetResetClause
1219 {
1220 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1221 n->role = NULL;
1222 n->database = $4;
1223 n->setstmt = $5;
1224 $$ = (Node *)n;
1225 }
1226 | ALTER USER RoleSpec opt_in_database SetResetClause
1227 {
1228 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1229 n->role = $3;
1230 n->database = $4;
1231 n->setstmt = $5;
1232 $$ = (Node *)n;
1233 }
1234 | ALTER USER ALL opt_in_database SetResetClause
1235 {
1236 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1237 n->role = NULL;
1238 n->database = $4;
1239 n->setstmt = $5;
1240 $$ = (Node *)n;
1241 }
1242 ;
1243
1244
1245 /*****************************************************************************
1246 *
1247 * Drop a postgresql DBMS role
1248 *
1249 * XXX Ideally this would have CASCADE/RESTRICT options, but a role
1250 * might own objects in multiple databases, and there is presently no way to
1251 * implement cascading to other databases. So we always behave as RESTRICT.
1252 *****************************************************************************/
1253
1254 DropRoleStmt:
1255 DROP ROLE role_list
1256 {
1257 DropRoleStmt *n = makeNode(DropRoleStmt);
1258 n->missing_ok = false;
1259 n->roles = $3;
1260 $$ = (Node *)n;
1261 }
1262 | DROP ROLE 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 | DROP USER role_list
1270 {
1271 DropRoleStmt *n = makeNode(DropRoleStmt);
1272 n->missing_ok = false;
1273 n->roles = $3;
1274 $$ = (Node *)n;
1275 }
1276 | DROP USER IF_P EXISTS role_list
1277 {
1278 DropRoleStmt *n = makeNode(DropRoleStmt);
1279 n->roles = $5;
1280 n->missing_ok = true;
1281 $$ = (Node *)n;
1282 }
1283 | DROP GROUP_P role_list
1284 {
1285 DropRoleStmt *n = makeNode(DropRoleStmt);
1286 n->missing_ok = false;
1287 n->roles = $3;
1288 $$ = (Node *)n;
1289 }
1290 | DROP GROUP_P IF_P EXISTS role_list
1291 {
1292 DropRoleStmt *n = makeNode(DropRoleStmt);
1293 n->missing_ok = true;
1294 n->roles = $5;
1295 $$ = (Node *)n;
1296 }
1297 ;
1298
1299
1300 /*****************************************************************************
1301 *
1302 * Create a postgresql group (role without login ability)
1303 *
1304 *****************************************************************************/
1305
1306 CreateGroupStmt:
1307 CREATE GROUP_P RoleId opt_with OptRoleList
1308 {
1309 CreateRoleStmt *n = makeNode(CreateRoleStmt);
1310 n->stmt_type = ROLESTMT_GROUP;
1311 n->role = $3;
1312 n->options = $5;
1313 $$ = (Node *)n;
1314 }
1315 ;
1316
1317
1318 /*****************************************************************************
1319 *
1320 * Alter a postgresql group
1321 *
1322 *****************************************************************************/
1323
1324 AlterGroupStmt:
1325 ALTER GROUP_P RoleSpec add_drop USER role_list
1326 {
1327 AlterRoleStmt *n = makeNode(AlterRoleStmt);
1328 n->role = $3;
1329 n->action = $4;
1330 n->options = list_make1(makeDefElem("rolemembers",
1331 (Node *)$6, @6));
1332 $$ = (Node *)n;
1333 }
1334 ;
1335
1336 add_drop: ADD_P { $$ = +1; }
1337 | DROP { $$ = -1; }
1338 ;
1339
1340
1341 /*****************************************************************************
1342 *
1343 * Manipulate a schema
1344 *
1345 *****************************************************************************/
1346
1347 CreateSchemaStmt:
1348 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1349 {
1350 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1351 /* One can omit the schema name or the authorization id. */
1352 n->schemaname = $3;
1353 n->authrole = $5;
1354 n->schemaElts = $6;
1355 n->if_not_exists = false;
1356 $$ = (Node *)n;
1357 }
1358 | CREATE SCHEMA ColId OptSchemaEltList
1359 {
1360 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1361 /* ...but not both */
1362 n->schemaname = $3;
1363 n->authrole = NULL;
1364 n->schemaElts = $4;
1365 n->if_not_exists = false;
1366 $$ = (Node *)n;
1367 }
1368 | CREATE SCHEMA IF_P NOT EXISTS OptSchemaName AUTHORIZATION RoleSpec OptSchemaEltList
1369 {
1370 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1371 /* schema name can be omitted here, too */
1372 n->schemaname = $6;
1373 n->authrole = $8;
1374 if ($9 != NIL)
1375 ereport(ERROR,
1376 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1377 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1378 parser_errposition(@9)));
1379 n->schemaElts = $9;
1380 n->if_not_exists = true;
1381 $$ = (Node *)n;
1382 }
1383 | CREATE SCHEMA IF_P NOT EXISTS ColId OptSchemaEltList
1384 {
1385 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1386 /* ...but not here */
1387 n->schemaname = $6;
1388 n->authrole = NULL;
1389 if ($7 != NIL)
1390 ereport(ERROR,
1391 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1392 errmsg("CREATE SCHEMA IF NOT EXISTS cannot include schema elements"),
1393 parser_errposition(@7)));
1394 n->schemaElts = $7;
1395 n->if_not_exists = true;
1396 $$ = (Node *)n;
1397 }
1398 ;
1399
1400 OptSchemaName:
1401 ColId { $$ = $1; }
1402 | /* EMPTY */ { $$ = NULL; }
1403 ;
1404
1405 OptSchemaEltList:
1406 OptSchemaEltList schema_stmt
1407 {
1408 if (@$ < 0) /* see comments for YYLLOC_DEFAULT */
1409 @$ = @2;
1410 $$ = lappend($1, $2);
1411 }
1412 | /* EMPTY */
1413 { $$ = NIL; }
1414 ;
1415
1416 /*
1417 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1418 * statement (in addition to by themselves).
1419 */
1420 schema_stmt:
1421 CreateStmt
1422 | IndexStmt
1423 | CreateSeqStmt
1424 | CreateTrigStmt
1425 | GrantStmt
1426 | ViewStmt
1427 ;
1428
1429
1430 /*****************************************************************************
1431 *
1432 * Set PG internal variable
1433 * SET name TO 'var_value'
1434 * Include SQL syntax (thomas 1997-10-22):
1435 * SET TIME ZONE 'var_value'
1436 *
1437 *****************************************************************************/
1438
1439 VariableSetStmt:
1440 PGPOOL SET generic_set
1441 {
1442 VariableSetStmt *n = $3;
1443 n->type = T_PgpoolVariableSetStmt; /* Hack to keep changes minumum */
1444 n->is_local = false;
1445 $$ = (Node *) n;
1446 }
1447 | SET set_rest
1448 {
1449 VariableSetStmt *n = $2;
1450 n->is_local = false;
1451 $$ = (Node *) n;
1452 }
1453 | SET LOCAL set_rest
1454 {
1455 VariableSetStmt *n = $3;
1456 n->is_local = true;
1457 $$ = (Node *) n;
1458 }
1459 | SET SESSION set_rest
1460 {
1461 VariableSetStmt *n = $3;
1462 n->is_local = false;
1463 $$ = (Node *) n;
1464 }
1465 ;
1466
1467 set_rest:
1468 TRANSACTION transaction_mode_list
1469 {
1470 VariableSetStmt *n = makeNode(VariableSetStmt);
1471 n->kind = VAR_SET_MULTI;
1472 n->name = "TRANSACTION";
1473 n->args = $2;
1474 $$ = n;
1475 }
1476 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1477 {
1478 VariableSetStmt *n = makeNode(VariableSetStmt);
1479 n->kind = VAR_SET_MULTI;
1480 n->name = "SESSION CHARACTERISTICS";
1481 n->args = $5;
1482 $$ = n;
1483 }
1484 | set_rest_more
1485 ;
1486
1487 generic_set:
1488 var_name TO var_list
1489 {
1490 VariableSetStmt *n = makeNode(VariableSetStmt);
1491 n->kind = VAR_SET_VALUE;
1492 n->name = $1;
1493 n->args = $3;
1494 $$ = n;
1495 }
1496 | var_name '=' var_list
1497 {
1498 VariableSetStmt *n = makeNode(VariableSetStmt);
1499 n->kind = VAR_SET_VALUE;
1500 n->name = $1;
1501 n->args = $3;
1502 $$ = n;
1503 }
1504 | var_name TO DEFAULT
1505 {
1506 VariableSetStmt *n = makeNode(VariableSetStmt);
1507 n->kind = VAR_SET_DEFAULT;
1508 n->name = $1;
1509 $$ = n;
1510 }
1511 | var_name '=' DEFAULT
1512 {
1513 VariableSetStmt *n = makeNode(VariableSetStmt);
1514 n->kind = VAR_SET_DEFAULT;
1515 n->name = $1;
1516 $$ = n;
1517 }
1518 ;
1519
1520 set_rest_more: /* Generic SET syntaxes: */
1521 generic_set {$$ = $1;}
1522 | var_name FROM CURRENT_P
1523 {
1524 VariableSetStmt *n = makeNode(VariableSetStmt);
1525 n->kind = VAR_SET_CURRENT;
1526 n->name = $1;
1527 $$ = n;
1528 }
1529 /* Special syntaxes mandated by SQL standard: */
1530 | TIME ZONE zone_value
1531 {
1532 VariableSetStmt *n = makeNode(VariableSetStmt);
1533 n->kind = VAR_SET_VALUE;
1534 n->name = "timezone";
1535 if ($3 != NULL)
1536 n->args = list_make1($3);
1537 else
1538 n->kind = VAR_SET_DEFAULT;
1539 $$ = n;
1540 }
1541 | CATALOG_P Sconst
1542 {
1543 ereport(ERROR,
1544 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1545 errmsg("current database cannot be changed"),
1546 parser_errposition(@2)));
1547 $$ = NULL; /*not reached*/
1548 }
1549 | SCHEMA Sconst
1550 {
1551 VariableSetStmt *n = makeNode(VariableSetStmt);
1552 n->kind = VAR_SET_VALUE;
1553 n->name = "search_path";
1554 n->args = list_make1(makeStringConst($2, @2));
1555 $$ = n;
1556 }
1557 | NAMES opt_encoding
1558 {
1559 VariableSetStmt *n = makeNode(VariableSetStmt);
1560 n->kind = VAR_SET_VALUE;
1561 n->name = "client_encoding";
1562 if ($2 != NULL)
1563 n->args = list_make1(makeStringConst($2, @2));
1564 else
1565 n->kind = VAR_SET_DEFAULT;
1566 $$ = n;
1567 }
1568 | ROLE NonReservedWord_or_Sconst
1569 {
1570 VariableSetStmt *n = makeNode(VariableSetStmt);
1571 n->kind = VAR_SET_VALUE;
1572 n->name = "role";
1573 n->args = list_make1(makeStringConst($2, @2));
1574 $$ = n;
1575 }
1576 | SESSION AUTHORIZATION NonReservedWord_or_Sconst
1577 {
1578 VariableSetStmt *n = makeNode(VariableSetStmt);
1579 n->kind = VAR_SET_VALUE;
1580 n->name = "session_authorization";
1581 n->args = list_make1(makeStringConst($3, @3));
1582 $$ = n;
1583 }
1584 | SESSION AUTHORIZATION DEFAULT
1585 {
1586 VariableSetStmt *n = makeNode(VariableSetStmt);
1587 n->kind = VAR_SET_DEFAULT;
1588 n->name = "session_authorization";
1589 $$ = n;
1590 }
1591 | XML_P OPTION document_or_content
1592 {
1593 VariableSetStmt *n = makeNode(VariableSetStmt);
1594 n->kind = VAR_SET_VALUE;
1595 n->name = "xmloption";
1596 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1597 $$ = n;
1598 }
1599 /* Special syntaxes invented by PostgreSQL: */
1600 | TRANSACTION SNAPSHOT Sconst
1601 {
1602 VariableSetStmt *n = makeNode(VariableSetStmt);
1603 n->kind = VAR_SET_MULTI;
1604 n->name = "TRANSACTION SNAPSHOT";
1605 n->args = list_make1(makeStringConst($3, @3));
1606 $$ = n;
1607 }
1608 ;
1609
1610 var_name: ColId { $$ = $1; }
1611 | var_name '.' ColId
1612 { $$ = psprintf("%s.%s", $1, $3); }
1613 ;
1614
1615 var_list: var_value { $$ = list_make1($1); }
1616 | var_list ',' var_value { $$ = lappend($1, $3); }
1617 ;
1618
1619 var_value: opt_boolean_or_string
1620 { $$ = makeStringConst($1, @1); }
1621 | NumericOnly
1622 { $$ = makeAConst($1, @1); }
1623 ;
1624
1625 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1626 | READ COMMITTED { $$ = "read committed"; }
1627 | REPEATABLE READ { $$ = "repeatable read"; }
1628 | SERIALIZABLE { $$ = "serializable"; }
1629 ;
1630
1631 opt_boolean_or_string:
1632 TRUE_P { $$ = "true"; }
1633 | FALSE_P { $$ = "false"; }
1634 | ON { $$ = "on"; }
1635 /*
1636 * OFF is also accepted as a boolean value, but is handled by
1637 * the NonReservedWord rule. The action for booleans and strings
1638 * is the same, so we don't need to distinguish them here.
1639 */
1640 | NonReservedWord_or_Sconst { $$ = $1; }
1641 ;
1642
1643 /* Timezone values can be:
1644 * - a string such as 'pst8pdt'
1645 * - an identifier such as "pst8pdt"
1646 * - an integer or floating point number
1647 * - a time interval per SQL99
1648 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1649 * so use IDENT (meaning we reject anything that is a key word).
1650 */
1651 zone_value:
1652 Sconst
1653 {
1654 $$ = makeStringConst($1, @1);
1655 }
1656 | IDENT
1657 {
1658 $$ = makeStringConst($1, @1);
1659 }
1660 | ConstInterval Sconst opt_interval
1661 {
1662 TypeName *t = $1;
1663 if ($3 != NIL)
1664 {
1665 A_Const *n = (A_Const *) linitial($3);
1666 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1667 ereport(ERROR,
1668 (errcode(ERRCODE_SYNTAX_ERROR),
1669 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1670 parser_errposition(@3)));
1671 }
1672 t->typmods = $3;
1673 $$ = makeStringConstCast($2, @2, t);
1674 }
1675 | ConstInterval '(' Iconst ')' Sconst
1676 {
1677 TypeName *t = $1;
1678 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1679 makeIntConst($3, @3));
1680 $$ = makeStringConstCast($5, @5, t);
1681 }
1682 | NumericOnly { $$ = makeAConst($1, @1); }
1683 | DEFAULT { $$ = NULL; }
1684 | LOCAL { $$ = NULL; }
1685 ;
1686
1687 opt_encoding:
1688 Sconst { $$ = $1; }
1689 | DEFAULT { $$ = NULL; }
1690 | /*EMPTY*/ { $$ = NULL; }
1691 ;
1692
1693 NonReservedWord_or_Sconst:
1694 NonReservedWord { $$ = $1; }
1695 | Sconst { $$ = $1; }
1696 ;
1697
1698 VariableResetStmt:
1699 RESET reset_rest { $$ = (Node *) $2; }
1700 | PGPOOL RESET generic_reset
1701 {
1702 VariableSetStmt *n = $3;
1703 n->type = T_PgpoolVariableSetStmt; /* Hack to keep the changes minumum */
1704 $$ = (Node *) n;
1705 }
1706 ;
1707
1708 reset_rest:
1709 generic_reset { $$ = $1; }
1710 | TIME ZONE
1711 {
1712 VariableSetStmt *n = makeNode(VariableSetStmt);
1713 n->kind = VAR_RESET;
1714 n->name = "timezone";
1715 $$ = n;
1716 }
1717 | TRANSACTION ISOLATION LEVEL
1718 {
1719 VariableSetStmt *n = makeNode(VariableSetStmt);
1720 n->kind = VAR_RESET;
1721 n->name = "transaction_isolation";
1722 $$ = n;
1723 }
1724 | SESSION AUTHORIZATION
1725 {
1726 VariableSetStmt *n = makeNode(VariableSetStmt);
1727 n->kind = VAR_RESET;
1728 n->name = "session_authorization";
1729 $$ = n;
1730 }
1731 ;
1732
1733 generic_reset:
1734 var_name
1735 {
1736 VariableSetStmt *n = makeNode(VariableSetStmt);
1737 n->kind = VAR_RESET;
1738 n->name = $1;
1739 $$ = n;
1740 }
1741 | ALL
1742 {
1743 VariableSetStmt *n = makeNode(VariableSetStmt);
1744 n->kind = VAR_RESET_ALL;
1745 $$ = n;
1746 }
1747 ;
1748
1749 /* SetResetClause allows SET or RESET without LOCAL */
1750 SetResetClause:
1751 SET set_rest { $$ = $2; }
1752 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1753 ;
1754
1755 /* SetResetClause allows SET or RESET without LOCAL */
1756 FunctionSetResetClause:
1757 SET set_rest_more { $$ = $2; }
1758 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1759 ;
1760
1761
1762 VariableShowStmt:
1763 /* pgpool extension */
1764 PGPOOL SHOW var_name
1765 {
1766 VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1767 n->name = $3;
1768 $$ = (Node *) n;
1769 }
1770 | PGPOOL SHOW ALL
1771 {
1772 VariableShowStmt *n = (VariableShowStmt *)newNode(sizeof(VariableShowStmt),T_PgpoolVariableShowStmt);
1773 n->name = "all";
1774 $$ = (Node *) n;
1775 }
1776 | SHOW var_name
1777 {
1778 VariableShowStmt *n = makeNode(VariableShowStmt);
1779 n->name = $2;
1780 $$ = (Node *) n;
1781 }
1782 | SHOW TIME ZONE
1783 {
1784 VariableShowStmt *n = makeNode(VariableShowStmt);
1785 n->name = "timezone";
1786 $$ = (Node *) n;
1787 }
1788 | SHOW TRANSACTION ISOLATION LEVEL
1789 {
1790 VariableShowStmt *n = makeNode(VariableShowStmt);
1791 n->name = "transaction_isolation";
1792 $$ = (Node *) n;
1793 }
1794 | SHOW SESSION AUTHORIZATION
1795 {
1796 VariableShowStmt *n = makeNode(VariableShowStmt);
1797 n->name = "session_authorization";
1798 $$ = (Node *) n;
1799 }
1800 | SHOW ALL
1801 {
1802 VariableShowStmt *n = makeNode(VariableShowStmt);
1803 n->name = "all";
1804 $$ = (Node *) n;
1805 }
1806 ;
1807
1808
1809 ConstraintsSetStmt:
1810 SET CONSTRAINTS constraints_set_list constraints_set_mode
1811 {
1812 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1813 n->constraints = $3;
1814 n->deferred = $4;
1815 $$ = (Node *) n;
1816 }
1817 ;
1818
1819 constraints_set_list:
1820 ALL { $$ = NIL; }
1821 | qualified_name_list { $$ = $1; }
1822 ;
1823
1824 constraints_set_mode:
1825 DEFERRED { $$ = true; }
1826 | IMMEDIATE { $$ = false; }
1827 ;
1828
1829
1830 /*
1831 * Checkpoint statement
1832 */
1833 CheckPointStmt:
1834 CHECKPOINT
1835 {
1836 CheckPointStmt *n = makeNode(CheckPointStmt);
1837 $$ = (Node *)n;
1838 }
1839 ;
1840
1841
1842 /*****************************************************************************
1843 *
1844 * DISCARD { ALL | TEMP | PLANS | SEQUENCES }
1845 *
1846 *****************************************************************************/
1847
1848 DiscardStmt:
1849 DISCARD ALL
1850 {
1851 DiscardStmt *n = makeNode(DiscardStmt);
1852 n->target = DISCARD_ALL;
1853 $$ = (Node *) n;
1854 }
1855 | DISCARD TEMP
1856 {
1857 DiscardStmt *n = makeNode(DiscardStmt);
1858 n->target = DISCARD_TEMP;
1859 $$ = (Node *) n;
1860 }
1861 | DISCARD TEMPORARY
1862 {
1863 DiscardStmt *n = makeNode(DiscardStmt);
1864 n->target = DISCARD_TEMP;
1865 $$ = (Node *) n;
1866 }
1867 | DISCARD PLANS
1868 {
1869 DiscardStmt *n = makeNode(DiscardStmt);
1870 n->target = DISCARD_PLANS;
1871 $$ = (Node *) n;
1872 }
1873 | DISCARD SEQUENCES
1874 {
1875 DiscardStmt *n = makeNode(DiscardStmt);
1876 n->target = DISCARD_SEQUENCES;
1877 $$ = (Node *) n;
1878 }
1879
1880 ;
1881
1882
1883 /*****************************************************************************
1884 *
1885 * ALTER [ TABLE | INDEX | SEQUENCE | VIEW | MATERIALIZED VIEW ] variations
1886 *
1887 * Note: we accept all subcommands for each of the five variants, and sort
1888 * out what's really legal at execution time.
1889 *****************************************************************************/
1890
1891 AlterTableStmt:
1892 ALTER TABLE relation_expr alter_table_cmds
1893 {
1894 AlterTableStmt *n = makeNode(AlterTableStmt);
1895 n->relation = $3;
1896 n->cmds = $4;
1897 n->relkind = OBJECT_TABLE;
1898 n->missing_ok = false;
1899 $$ = (Node *)n;
1900 }
1901 | ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
1902 {
1903 AlterTableStmt *n = makeNode(AlterTableStmt);
1904 n->relation = $5;
1905 n->cmds = $6;
1906 n->relkind = OBJECT_TABLE;
1907 n->missing_ok = true;
1908 $$ = (Node *)n;
1909 }
1910 | ALTER TABLE relation_expr partition_cmd
1911 {
1912 AlterTableStmt *n = makeNode(AlterTableStmt);
1913 n->relation = $3;
1914 n->cmds = list_make1($4);
1915 n->relkind = OBJECT_TABLE;
1916 n->missing_ok = false;
1917 $$ = (Node *)n;
1918 }
1919 | ALTER TABLE IF_P EXISTS relation_expr partition_cmd
1920 {
1921 AlterTableStmt *n = makeNode(AlterTableStmt);
1922 n->relation = $5;
1923 n->cmds = list_make1($6);
1924 n->relkind = OBJECT_TABLE;
1925 n->missing_ok = true;
1926 $$ = (Node *)n;
1927 }
1928 | ALTER TABLE ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1929 {
1930 AlterTableMoveAllStmt *n =
1931 makeNode(AlterTableMoveAllStmt);
1932 n->orig_tablespacename = $6;
1933 n->objtype = OBJECT_TABLE;
1934 n->roles = NIL;
1935 n->new_tablespacename = $9;
1936 n->nowait = $10;
1937 $$ = (Node *)n;
1938 }
1939 | ALTER TABLE ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1940 {
1941 AlterTableMoveAllStmt *n =
1942 makeNode(AlterTableMoveAllStmt);
1943 n->orig_tablespacename = $6;
1944 n->objtype = OBJECT_TABLE;
1945 n->roles = $9;
1946 n->new_tablespacename = $12;
1947 n->nowait = $13;
1948 $$ = (Node *)n;
1949 }
1950 | ALTER INDEX qualified_name alter_table_cmds
1951 {
1952 AlterTableStmt *n = makeNode(AlterTableStmt);
1953 n->relation = $3;
1954 n->cmds = $4;
1955 n->relkind = OBJECT_INDEX;
1956 n->missing_ok = false;
1957 $$ = (Node *)n;
1958 }
1959 | ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
1960 {
1961 AlterTableStmt *n = makeNode(AlterTableStmt);
1962 n->relation = $5;
1963 n->cmds = $6;
1964 n->relkind = OBJECT_INDEX;
1965 n->missing_ok = true;
1966 $$ = (Node *)n;
1967 }
1968 | ALTER INDEX qualified_name index_partition_cmd
1969 {
1970 AlterTableStmt *n = makeNode(AlterTableStmt);
1971 n->relation = $3;
1972 n->cmds = list_make1($4);
1973 n->relkind = OBJECT_INDEX;
1974 n->missing_ok = false;
1975 $$ = (Node *)n;
1976 }
1977 | ALTER INDEX ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
1978 {
1979 AlterTableMoveAllStmt *n =
1980 makeNode(AlterTableMoveAllStmt);
1981 n->orig_tablespacename = $6;
1982 n->objtype = OBJECT_INDEX;
1983 n->roles = NIL;
1984 n->new_tablespacename = $9;
1985 n->nowait = $10;
1986 $$ = (Node *)n;
1987 }
1988 | ALTER INDEX ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
1989 {
1990 AlterTableMoveAllStmt *n =
1991 makeNode(AlterTableMoveAllStmt);
1992 n->orig_tablespacename = $6;
1993 n->objtype = OBJECT_INDEX;
1994 n->roles = $9;
1995 n->new_tablespacename = $12;
1996 n->nowait = $13;
1997 $$ = (Node *)n;
1998 }
1999 | ALTER SEQUENCE qualified_name alter_table_cmds
2000 {
2001 AlterTableStmt *n = makeNode(AlterTableStmt);
2002 n->relation = $3;
2003 n->cmds = $4;
2004 n->relkind = OBJECT_SEQUENCE;
2005 n->missing_ok = false;
2006 $$ = (Node *)n;
2007 }
2008 | ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
2009 {
2010 AlterTableStmt *n = makeNode(AlterTableStmt);
2011 n->relation = $5;
2012 n->cmds = $6;
2013 n->relkind = OBJECT_SEQUENCE;
2014 n->missing_ok = true;
2015 $$ = (Node *)n;
2016 }
2017 | ALTER VIEW qualified_name alter_table_cmds
2018 {
2019 AlterTableStmt *n = makeNode(AlterTableStmt);
2020 n->relation = $3;
2021 n->cmds = $4;
2022 n->relkind = OBJECT_VIEW;
2023 n->missing_ok = false;
2024 $$ = (Node *)n;
2025 }
2026 | ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
2027 {
2028 AlterTableStmt *n = makeNode(AlterTableStmt);
2029 n->relation = $5;
2030 n->cmds = $6;
2031 n->relkind = OBJECT_VIEW;
2032 n->missing_ok = true;
2033 $$ = (Node *)n;
2034 }
2035 | ALTER MATERIALIZED VIEW qualified_name alter_table_cmds
2036 {
2037 AlterTableStmt *n = makeNode(AlterTableStmt);
2038 n->relation = $4;
2039 n->cmds = $5;
2040 n->relkind = OBJECT_MATVIEW;
2041 n->missing_ok = false;
2042 $$ = (Node *)n;
2043 }
2044 | ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds
2045 {
2046 AlterTableStmt *n = makeNode(AlterTableStmt);
2047 n->relation = $6;
2048 n->cmds = $7;
2049 n->relkind = OBJECT_MATVIEW;
2050 n->missing_ok = true;
2051 $$ = (Node *)n;
2052 }
2053 | ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name SET TABLESPACE name opt_nowait
2054 {
2055 AlterTableMoveAllStmt *n =
2056 makeNode(AlterTableMoveAllStmt);
2057 n->orig_tablespacename = $7;
2058 n->objtype = OBJECT_MATVIEW;
2059 n->roles = NIL;
2060 n->new_tablespacename = $10;
2061 n->nowait = $11;
2062 $$ = (Node *)n;
2063 }
2064 | ALTER MATERIALIZED VIEW ALL IN_P TABLESPACE name OWNED BY role_list SET TABLESPACE name opt_nowait
2065 {
2066 AlterTableMoveAllStmt *n =
2067 makeNode(AlterTableMoveAllStmt);
2068 n->orig_tablespacename = $7;
2069 n->objtype = OBJECT_MATVIEW;
2070 n->roles = $10;
2071 n->new_tablespacename = $13;
2072 n->nowait = $14;
2073 $$ = (Node *)n;
2074 }
2075 ;
2076
2077 alter_table_cmds:
2078 alter_table_cmd { $$ = list_make1($1); }
2079 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
2080 ;
2081
2082 partition_cmd:
2083 /* ALTER TABLE <name> ATTACH PARTITION <table_name> FOR VALUES */
2084 ATTACH PARTITION qualified_name PartitionBoundSpec
2085 {
2086 AlterTableCmd *n = makeNode(AlterTableCmd);
2087 PartitionCmd *cmd = makeNode(PartitionCmd);
2088
2089 n->subtype = AT_AttachPartition;
2090 cmd->name = $3;
2091 cmd->bound = $4;
2092 n->def = (Node *) cmd;
2093
2094 $$ = (Node *) n;
2095 }
2096 /* ALTER TABLE <name> DETACH PARTITION <partition_name> */
2097 | DETACH PARTITION qualified_name
2098 {
2099 AlterTableCmd *n = makeNode(AlterTableCmd);
2100 PartitionCmd *cmd = makeNode(PartitionCmd);
2101
2102 n->subtype = AT_DetachPartition;
2103 cmd->name = $3;
2104 cmd->bound = NULL;
2105 n->def = (Node *) cmd;
2106
2107 $$ = (Node *) n;
2108 }
2109 ;
2110
2111 index_partition_cmd:
2112 /* ALTER INDEX <name> ATTACH PARTITION <index_name> */
2113 ATTACH PARTITION qualified_name
2114 {
2115 AlterTableCmd *n = makeNode(AlterTableCmd);
2116 PartitionCmd *cmd = makeNode(PartitionCmd);
2117
2118 n->subtype = AT_AttachPartition;
2119 cmd->name = $3;
2120 cmd->bound = NULL;
2121 n->def = (Node *) cmd;
2122
2123 $$ = (Node *) n;
2124 }
2125 ;
2126
2127 alter_table_cmd:
2128 /* ALTER TABLE <name> ADD <coldef> */
2129 ADD_P columnDef
2130 {
2131 AlterTableCmd *n = makeNode(AlterTableCmd);
2132 n->subtype = AT_AddColumn;
2133 n->def = $2;
2134 n->missing_ok = false;
2135 $$ = (Node *)n;
2136 }
2137 /* ALTER TABLE <name> ADD IF NOT EXISTS <coldef> */
2138 | ADD_P IF_P NOT EXISTS columnDef
2139 {
2140 AlterTableCmd *n = makeNode(AlterTableCmd);
2141 n->subtype = AT_AddColumn;
2142 n->def = $5;
2143 n->missing_ok = true;
2144 $$ = (Node *)n;
2145 }
2146 /* ALTER TABLE <name> ADD COLUMN <coldef> */
2147 | ADD_P COLUMN columnDef
2148 {
2149 AlterTableCmd *n = makeNode(AlterTableCmd);
2150 n->subtype = AT_AddColumn;
2151 n->def = $3;
2152 n->missing_ok = false;
2153 $$ = (Node *)n;
2154 }
2155 /* ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef> */
2156 | ADD_P COLUMN IF_P NOT EXISTS columnDef
2157 {
2158 AlterTableCmd *n = makeNode(AlterTableCmd);
2159 n->subtype = AT_AddColumn;
2160 n->def = $6;
2161 n->missing_ok = true;
2162 $$ = (Node *)n;
2163 }
2164 /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
2165 | ALTER opt_column ColId alter_column_default
2166 {
2167 AlterTableCmd *n = makeNode(AlterTableCmd);
2168 n->subtype = AT_ColumnDefault;
2169 n->name = $3;
2170 n->def = $4;
2171 $$ = (Node *)n;
2172 }
2173 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
2174 | ALTER opt_column ColId DROP NOT NULL_P
2175 {
2176 AlterTableCmd *n = makeNode(AlterTableCmd);
2177 n->subtype = AT_DropNotNull;
2178 n->name = $3;
2179 $$ = (Node *)n;
2180 }
2181 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
2182 | ALTER opt_column ColId SET NOT NULL_P
2183 {
2184 AlterTableCmd *n = makeNode(AlterTableCmd);
2185 n->subtype = AT_SetNotNull;
2186 n->name = $3;
2187 $$ = (Node *)n;
2188 }
2189 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
2190 | ALTER opt_column ColId SET STATISTICS SignedIconst
2191 {
2192 AlterTableCmd *n = makeNode(AlterTableCmd);
2193 n->subtype = AT_SetStatistics;
2194 n->name = $3;
2195 n->def = (Node *) makeInteger($6);
2196 $$ = (Node *)n;
2197 }
2198 /* ALTER TABLE <name> ALTER [COLUMN] <colnum> SET STATISTICS <SignedIconst> */
2199 | ALTER opt_column Iconst SET STATISTICS SignedIconst
2200 {
2201 AlterTableCmd *n = makeNode(AlterTableCmd);
2202
2203 if ($3 <= 0 || $3 > PG_INT16_MAX)
2204 ereport(ERROR,
2205 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2206 errmsg("column number must be in range from 1 to %d", PG_INT16_MAX),
2207 parser_errposition(@3)));
2208
2209 n->subtype = AT_SetStatistics;
2210 n->num = (int16) $3;
2211 n->def = (Node *) makeInteger($6);
2212 $$ = (Node *)n;
2213 }
2214 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
2215 | ALTER opt_column ColId SET reloptions
2216 {
2217 AlterTableCmd *n = makeNode(AlterTableCmd);
2218 n->subtype = AT_SetOptions;
2219 n->name = $3;
2220 n->def = (Node *) $5;
2221 $$ = (Node *)n;
2222 }
2223 /* ALTER TABLE <name> ALTER [COLUMN] <colname> RESET ( column_parameter = value [, ... ] ) */
2224 | ALTER opt_column ColId RESET reloptions
2225 {
2226 AlterTableCmd *n = makeNode(AlterTableCmd);
2227 n->subtype = AT_ResetOptions;
2228 n->name = $3;
2229 n->def = (Node *) $5;
2230 $$ = (Node *)n;
2231 }
2232 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
2233 | ALTER opt_column ColId SET STORAGE ColId
2234 {
2235 AlterTableCmd *n = makeNode(AlterTableCmd);
2236 n->subtype = AT_SetStorage;
2237 n->name = $3;
2238 n->def = (Node *) makeString($6);
2239 $$ = (Node *)n;
2240 }
2241 /* ALTER TABLE <name> ALTER [COLUMN] <colname> ADD GENERATED ... AS IDENTITY ... */
2242 | ALTER opt_column ColId ADD_P GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
2243 {
2244 AlterTableCmd *n = makeNode(AlterTableCmd);
2245 Constraint *c = makeNode(Constraint);
2246
2247 c->contype = CONSTR_IDENTITY;
2248 c->generated_when = $6;
2249 c->options = $9;
2250 c->location = @5;
2251
2252 n->subtype = AT_AddIdentity;
2253 n->name = $3;
2254 n->def = (Node *) c;
2255
2256 $$ = (Node *)n;
2257 }
2258 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET <sequence options>/RESET */
2259 | ALTER opt_column ColId alter_identity_column_option_list
2260 {
2261 AlterTableCmd *n = makeNode(AlterTableCmd);
2262 n->subtype = AT_SetIdentity;
2263 n->name = $3;
2264 n->def = (Node *) $4;
2265 $$ = (Node *)n;
2266 }
2267 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY */
2268 | ALTER opt_column ColId DROP IDENTITY_P
2269 {
2270 AlterTableCmd *n = makeNode(AlterTableCmd);
2271 n->subtype = AT_DropIdentity;
2272 n->name = $3;
2273 n->missing_ok = false;
2274 $$ = (Node *)n;
2275 }
2276 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP IDENTITY IF EXISTS */
2277 | ALTER opt_column ColId DROP IDENTITY_P IF_P EXISTS
2278 {
2279 AlterTableCmd *n = makeNode(AlterTableCmd);
2280 n->subtype = AT_DropIdentity;
2281 n->name = $3;
2282 n->missing_ok = true;
2283 $$ = (Node *)n;
2284 }
2285 /* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
2286 | DROP opt_column IF_P EXISTS ColId opt_drop_behavior
2287 {
2288 AlterTableCmd *n = makeNode(AlterTableCmd);
2289 n->subtype = AT_DropColumn;
2290 n->name = $5;
2291 n->behavior = $6;
2292 n->missing_ok = true;
2293 $$ = (Node *)n;
2294 }
2295 /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
2296 | DROP opt_column ColId opt_drop_behavior
2297 {
2298 AlterTableCmd *n = makeNode(AlterTableCmd);
2299 n->subtype = AT_DropColumn;
2300 n->name = $3;
2301 n->behavior = $4;
2302 n->missing_ok = false;
2303 $$ = (Node *)n;
2304 }
2305 /*
2306 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
2307 * [ USING <expression> ]
2308 */
2309 | ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
2310 {
2311 AlterTableCmd *n = makeNode(AlterTableCmd);
2312 ColumnDef *def = makeNode(ColumnDef);
2313 n->subtype = AT_AlterColumnType;
2314 n->name = $3;
2315 n->def = (Node *) def;
2316 /* We only use these fields of the ColumnDef node */
2317 def->typeName = $6;
2318 def->collClause = (CollateClause *) $7;
2319 def->raw_default = $8;
2320 def->location = @3;
2321 $$ = (Node *)n;
2322 }
2323 /* ALTER FOREIGN TABLE <name> ALTER [COLUMN] <colname> OPTIONS */
2324 | ALTER opt_column ColId alter_generic_options
2325 {
2326 AlterTableCmd *n = makeNode(AlterTableCmd);
2327 n->subtype = AT_AlterColumnGenericOptions;
2328 n->name = $3;
2329 n->def = (Node *) $4;
2330 $$ = (Node *)n;
2331 }
2332 /* ALTER TABLE <name> ADD CONSTRAINT ... */
2333 | ADD_P TableConstraint
2334 {
2335 AlterTableCmd *n = makeNode(AlterTableCmd);
2336 n->subtype = AT_AddConstraint;
2337 n->def = $2;
2338 n->missing_ok = false;
2339 $$ = (Node *)n;
2340 }
2341 /* ALTER TABLE <name> ALTER CONSTRAINT ... */
2342 | ALTER CONSTRAINT name ConstraintAttributeSpec
2343 {
2344 AlterTableCmd *n = makeNode(AlterTableCmd);
2345 Constraint *c = makeNode(Constraint);
2346 n->subtype = AT_AlterConstraint;
2347 n->def = (Node *) c;
2348 c->contype = CONSTR_FOREIGN; /* others not supported, yet */
2349 c->conname = $3;
2350 processCASbits($4, @4, "ALTER CONSTRAINT statement",
2351 &c->deferrable,
2352 &c->initdeferred,
2353 NULL, NULL, yyscanner);
2354 $$ = (Node *)n;
2355 }
2356 /* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
2357 | VALIDATE CONSTRAINT name
2358 {
2359 AlterTableCmd *n = makeNode(AlterTableCmd);
2360 n->subtype = AT_ValidateConstraint;
2361 n->name = $3;
2362 $$ = (Node *)n;
2363 }
2364 /* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
2365 | DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
2366 {
2367 AlterTableCmd *n = makeNode(AlterTableCmd);
2368 n->subtype = AT_DropConstraint;
2369 n->name = $5;
2370 n->behavior = $6;
2371 n->missing_ok = true;
2372 $$ = (Node *)n;
2373 }
2374 /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
2375 | DROP CONSTRAINT name opt_drop_behavior
2376 {
2377 AlterTableCmd *n = makeNode(AlterTableCmd);
2378 n->subtype = AT_DropConstraint;
2379 n->name = $3;
2380 n->behavior = $4;
2381 n->missing_ok = false;
2382 $$ = (Node *)n;
2383 }
2384 /* ALTER TABLE <name> SET WITHOUT OIDS, for backward compat */
2385 | SET WITHOUT OIDS
2386 {
2387 AlterTableCmd *n = makeNode(AlterTableCmd);
2388 n->subtype = AT_DropOids;
2389 $$ = (Node *)n;
2390 }
2391 /* ALTER TABLE <name> CLUSTER ON <indexname> */
2392 | CLUSTER ON name
2393 {
2394 AlterTableCmd *n = makeNode(AlterTableCmd);
2395 n->subtype = AT_ClusterOn;
2396 n->name = $3;
2397 $$ = (Node *)n;
2398 }
2399 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
2400 | SET WITHOUT CLUSTER
2401 {
2402 AlterTableCmd *n = makeNode(AlterTableCmd);
2403 n->subtype = AT_DropCluster;
2404 n->name = NULL;
2405 $$ = (Node *)n;
2406 }
2407 /* ALTER TABLE <name> SET LOGGED */
2408 | SET LOGGED
2409 {
2410 AlterTableCmd *n = makeNode(AlterTableCmd);
2411 n->subtype = AT_SetLogged;
2412 $$ = (Node *)n;
2413 }
2414 /* ALTER TABLE <name> SET UNLOGGED */
2415 | SET UNLOGGED
2416 {
2417 AlterTableCmd *n = makeNode(AlterTableCmd);
2418 n->subtype = AT_SetUnLogged;
2419 $$ = (Node *)n;
2420 }
2421 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
2422 | ENABLE_P TRIGGER name
2423 {
2424 AlterTableCmd *n = makeNode(AlterTableCmd);
2425 n->subtype = AT_EnableTrig;
2426 n->name = $3;
2427 $$ = (Node *)n;
2428 }
2429 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
2430 | ENABLE_P ALWAYS TRIGGER name
2431 {
2432 AlterTableCmd *n = makeNode(AlterTableCmd);
2433 n->subtype = AT_EnableAlwaysTrig;
2434 n->name = $4;
2435 $$ = (Node *)n;
2436 }
2437 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
2438 | ENABLE_P REPLICA TRIGGER name
2439 {
2440 AlterTableCmd *n = makeNode(AlterTableCmd);
2441 n->subtype = AT_EnableReplicaTrig;
2442 n->name = $4;
2443 $$ = (Node *)n;
2444 }
2445 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
2446 | ENABLE_P TRIGGER ALL
2447 {
2448 AlterTableCmd *n = makeNode(AlterTableCmd);
2449 n->subtype = AT_EnableTrigAll;
2450 $$ = (Node *)n;
2451 }
2452 /* ALTER TABLE <name> ENABLE TRIGGER USER */
2453 | ENABLE_P TRIGGER USER
2454 {
2455 AlterTableCmd *n = makeNode(AlterTableCmd);
2456 n->subtype = AT_EnableTrigUser;
2457 $$ = (Node *)n;
2458 }
2459 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
2460 | DISABLE_P TRIGGER name
2461 {
2462 AlterTableCmd *n = makeNode(AlterTableCmd);
2463 n->subtype = AT_DisableTrig;
2464 n->name = $3;
2465 $$ = (Node *)n;
2466 }
2467 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
2468 | DISABLE_P TRIGGER ALL
2469 {
2470 AlterTableCmd *n = makeNode(AlterTableCmd);
2471 n->subtype = AT_DisableTrigAll;
2472 $$ = (Node *)n;
2473 }
2474 /* ALTER TABLE <name> DISABLE TRIGGER USER */
2475 | DISABLE_P TRIGGER USER
2476 {
2477 AlterTableCmd *n = makeNode(AlterTableCmd);
2478 n->subtype = AT_DisableTrigUser;
2479 $$ = (Node *)n;
2480 }
2481 /* ALTER TABLE <name> ENABLE RULE <rule> */
2482 | ENABLE_P RULE name
2483 {
2484 AlterTableCmd *n = makeNode(AlterTableCmd);
2485 n->subtype = AT_EnableRule;
2486 n->name = $3;
2487 $$ = (Node *)n;
2488 }
2489 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
2490 | ENABLE_P ALWAYS RULE name
2491 {
2492 AlterTableCmd *n = makeNode(AlterTableCmd);
2493 n->subtype = AT_EnableAlwaysRule;
2494 n->name = $4;
2495 $$ = (Node *)n;
2496 }
2497 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
2498 | ENABLE_P REPLICA RULE name
2499 {
2500 AlterTableCmd *n = makeNode(AlterTableCmd);
2501 n->subtype = AT_EnableReplicaRule;
2502 n->name = $4;
2503 $$ = (Node *)n;
2504 }
2505 /* ALTER TABLE <name> DISABLE RULE <rule> */
2506 | DISABLE_P RULE name
2507 {
2508 AlterTableCmd *n = makeNode(AlterTableCmd);
2509 n->subtype = AT_DisableRule;
2510 n->name = $3;
2511 $$ = (Node *)n;
2512 }
2513 /* ALTER TABLE <name> INHERIT <parent> */
2514 | INHERIT qualified_name
2515 {
2516 AlterTableCmd *n = makeNode(AlterTableCmd);
2517 n->subtype = AT_AddInherit;
2518 n->def = (Node *) $2;
2519 $$ = (Node *)n;
2520 }
2521 /* ALTER TABLE <name> NO INHERIT <parent> */
2522 | NO INHERIT qualified_name
2523 {
2524 AlterTableCmd *n = makeNode(AlterTableCmd);
2525 n->subtype = AT_DropInherit;
2526 n->def = (Node *) $3;
2527 $$ = (Node *)n;
2528 }
2529 /* ALTER TABLE <name> OF <type_name> */
2530 | OF any_name
2531 {
2532 AlterTableCmd *n = makeNode(AlterTableCmd);
2533 TypeName *def = makeTypeNameFromNameList($2);
2534 def->location = @2;
2535 n->subtype = AT_AddOf;
2536 n->def = (Node *) def;
2537 $$ = (Node *)n;
2538 }
2539 /* ALTER TABLE <name> NOT OF */
2540 | NOT OF
2541 {
2542 AlterTableCmd *n = makeNode(AlterTableCmd);
2543 n->subtype = AT_DropOf;
2544 $$ = (Node *)n;
2545 }
2546 /* ALTER TABLE <name> OWNER TO RoleSpec */
2547 | OWNER TO RoleSpec
2548 {
2549 AlterTableCmd *n = makeNode(AlterTableCmd);
2550 n->subtype = AT_ChangeOwner;
2551 n->newowner = $3;
2552 $$ = (Node *)n;
2553 }
2554 /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
2555 | SET TABLESPACE name
2556 {
2557 AlterTableCmd *n = makeNode(AlterTableCmd);
2558 n->subtype = AT_SetTableSpace;
2559 n->name = $3;
2560 $$ = (Node *)n;
2561 }
2562 /* ALTER TABLE <name> SET (...) */
2563 | SET reloptions
2564 {
2565 AlterTableCmd *n = makeNode(AlterTableCmd);
2566 n->subtype = AT_SetRelOptions;
2567 n->def = (Node *)$2;
2568 $$ = (Node *)n;
2569 }
2570 /* ALTER TABLE <name> RESET (...) */
2571 | RESET reloptions
2572 {
2573 AlterTableCmd *n = makeNode(AlterTableCmd);
2574 n->subtype = AT_ResetRelOptions;
2575 n->def = (Node *)$2;
2576 $$ = (Node *)n;
2577 }
2578 /* ALTER TABLE <name> REPLICA IDENTITY */
2579 | REPLICA IDENTITY_P replica_identity
2580 {
2581 AlterTableCmd *n = makeNode(AlterTableCmd);
2582 n->subtype = AT_ReplicaIdentity;
2583 n->def = $3;
2584 $$ = (Node *)n;
2585 }
2586 /* ALTER TABLE <name> ENABLE ROW LEVEL SECURITY */
2587 | ENABLE_P ROW LEVEL SECURITY
2588 {
2589 AlterTableCmd *n = makeNode(AlterTableCmd);
2590 n->subtype = AT_EnableRowSecurity;
2591 $$ = (Node *)n;
2592 }
2593 /* ALTER TABLE <name> DISABLE ROW LEVEL SECURITY */
2594 | DISABLE_P ROW LEVEL SECURITY
2595 {
2596 AlterTableCmd *n = makeNode(AlterTableCmd);
2597 n->subtype = AT_DisableRowSecurity;
2598 $$ = (Node *)n;
2599 }
2600 /* ALTER TABLE <name> FORCE ROW LEVEL SECURITY */
2601 | FORCE ROW LEVEL SECURITY
2602 {
2603 AlterTableCmd *n = makeNode(AlterTableCmd);
2604 n->subtype = AT_ForceRowSecurity;
2605 $$ = (Node *)n;
2606 }
2607 /* ALTER TABLE <name> NO FORCE ROW LEVEL SECURITY */
2608 | NO FORCE ROW LEVEL SECURITY
2609 {
2610 AlterTableCmd *n = makeNode(AlterTableCmd);
2611 n->subtype = AT_NoForceRowSecurity;
2612 $$ = (Node *)n;
2613 }
2614 | alter_generic_options
2615 {
2616 AlterTableCmd *n = makeNode(AlterTableCmd);
2617 n->subtype = AT_GenericOptions;
2618 n->def = (Node *)$1;
2619 $$ = (Node *) n;
2620 }
2621 ;
2622
2623 alter_column_default:
2624 SET DEFAULT a_expr { $$ = $3; }
2625 | DROP DEFAULT { $$ = NULL; }
2626 ;
2627
2628 opt_drop_behavior:
2629 CASCADE { $$ = DROP_CASCADE; }
2630 | RESTRICT { $$ = DROP_RESTRICT; }
2631 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
2632 ;
2633
2634 opt_collate_clause:
2635 COLLATE any_name
2636 {
2637 CollateClause *n = makeNode(CollateClause);
2638 n->arg = NULL;
2639 n->collname = $2;
2640 n->location = @1;
2641 $$ = (Node *) n;
2642 }
2643 | /* EMPTY */ { $$ = NULL; }
2644 ;
2645
2646 alter_using:
2647 USING a_expr { $$ = $2; }
2648 | /* EMPTY */ { $$ = NULL; }
2649 ;
2650
2651 replica_identity:
2652 NOTHING
2653 {
2654 ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2655 n->identity_type = REPLICA_IDENTITY_NOTHING;
2656 n->name = NULL;
2657 $$ = (Node *) n;
2658 }
2659 | FULL
2660 {
2661 ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2662 n->identity_type = REPLICA_IDENTITY_FULL;
2663 n->name = NULL;
2664 $$ = (Node *) n;
2665 }
2666 | DEFAULT
2667 {
2668 ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2669 n->identity_type = REPLICA_IDENTITY_DEFAULT;
2670 n->name = NULL;
2671 $$ = (Node *) n;
2672 }
2673 | USING INDEX name
2674 {
2675 ReplicaIdentityStmt *n = makeNode(ReplicaIdentityStmt);
2676 n->identity_type = REPLICA_IDENTITY_INDEX;
2677 n->name = $3;
2678 $$ = (Node *) n;
2679 }
2680 ;
2681
2682 reloptions:
2683 '(' reloption_list ')' { $$ = $2; }
2684 ;
2685
2686 opt_reloptions: WITH reloptions { $$ = $2; }
2687 | /* EMPTY */ { $$ = NIL; }
2688 ;
2689
2690 reloption_list:
2691 reloption_elem { $$ = list_make1($1); }
2692 | reloption_list ',' reloption_elem { $$ = lappend($1, $3); }
2693 ;
2694
2695 /* This should match def_elem and also allow qualified names */
2696 reloption_elem:
2697 ColLabel '=' def_arg
2698 {
2699 $$ = makeDefElem($1, (Node *) $3, @1);
2700 }
2701 | ColLabel
2702 {
2703 $$ = makeDefElem($1, NULL, @1);
2704 }
2705 | ColLabel '.' ColLabel '=' def_arg
2706 {
2707 $$ = makeDefElemExtended($1, $3, (Node *) $5,
2708 DEFELEM_UNSPEC, @1);
2709 }
2710 | ColLabel '.' ColLabel
2711 {
2712 $$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC, @1);
2713 }
2714 ;
2715
2716 alter_identity_column_option_list:
2717 alter_identity_column_option
2718 { $$ = list_make1($1); }
2719 | alter_identity_column_option_list alter_identity_column_option
2720 { $$ = lappend($1, $2); }
2721 ;
2722
2723 alter_identity_column_option:
2724 RESTART
2725 {
2726 $$ = makeDefElem("restart", NULL, @1);
2727 }
2728 | RESTART opt_with NumericOnly
2729 {
2730 $$ = makeDefElem("restart", (Node *)$3, @1);
2731 }
2732 | SET SeqOptElem
2733 {
2734 if (strcmp($2->defname, "as") == 0 ||
2735 strcmp($2->defname, "restart") == 0 ||
2736 strcmp($2->defname, "owned_by") == 0)
2737 ereport(ERROR,
2738 (errcode(ERRCODE_SYNTAX_ERROR),
2739 errmsg("sequence option \"%s\" not supported here", $2->defname),
2740 parser_errposition(@2)));
2741 $$ = $2;
2742 }
2743 | SET GENERATED generated_when
2744 {
2745 $$ = makeDefElem("generated", (Node *) makeInteger($3), @1);
2746 }
2747 ;
2748
2749 PartitionBoundSpec:
2750 /* a HASH partition */
2751 FOR VALUES WITH '(' hash_partbound ')'
2752 {
2753 ListCell *lc;
2754 PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2755
2756 n->strategy = PARTITION_STRATEGY_HASH;
2757 n->modulus = n->remainder = -1;
2758
foreach(lc,$5)2759 foreach (lc, $5)
2760 {
2761 DefElem *opt = lfirst_node(DefElem, lc);
2762
2763 if (strcmp(opt->defname, "modulus") == 0)
2764 {
2765 if (n->modulus != -1)
2766 ereport(ERROR,
2767 (errcode(ERRCODE_DUPLICATE_OBJECT),
2768 errmsg("modulus for hash partition provided more than once"),
2769 parser_errposition(opt->location)));
2770 n->modulus = defGetInt32(opt);
2771 }
2772 else if (strcmp(opt->defname, "remainder") == 0)
2773 {
2774 if (n->remainder != -1)
2775 ereport(ERROR,
2776 (errcode(ERRCODE_DUPLICATE_OBJECT),
2777 errmsg("remainder for hash partition provided more than once"),
2778 parser_errposition(opt->location)));
2779 n->remainder = defGetInt32(opt);
2780 }
2781 else
2782 ereport(ERROR,
2783 (errcode(ERRCODE_SYNTAX_ERROR),
2784 errmsg("unrecognized hash partition bound specification \"%s\"",
2785 opt->defname),
2786 parser_errposition(opt->location)));
2787 }
2788
2789 if (n->modulus == -1)
2790 ereport(ERROR,
2791 (errcode(ERRCODE_SYNTAX_ERROR),
2792 errmsg("modulus for hash partition must be specified")));
2793 if (n->remainder == -1)
2794 ereport(ERROR,
2795 (errcode(ERRCODE_SYNTAX_ERROR),
2796 errmsg("remainder for hash partition must be specified")));
2797
2798 n->location = @3;
2799
2800 $$ = n;
2801 }
2802
2803 /* a LIST partition */
2804 | FOR VALUES IN_P '(' expr_list ')'
2805 {
2806 PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2807
2808 n->strategy = PARTITION_STRATEGY_LIST;
2809 n->is_default = false;
2810 n->listdatums = $5;
2811 n->location = @3;
2812
2813 $$ = n;
2814 }
2815
2816 /* a RANGE partition */
2817 | FOR VALUES FROM '(' expr_list ')' TO '(' expr_list ')'
2818 {
2819 PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2820
2821 n->strategy = PARTITION_STRATEGY_RANGE;
2822 n->is_default = false;
2823 n->lowerdatums = $5;
2824 n->upperdatums = $9;
2825 n->location = @3;
2826
2827 $$ = n;
2828 }
2829
2830 /* a DEFAULT partition */
2831 | DEFAULT
2832 {
2833 PartitionBoundSpec *n = makeNode(PartitionBoundSpec);
2834
2835 n->is_default = true;
2836 n->location = @1;
2837
2838 $$ = n;
2839 }
2840 ;
2841
2842 hash_partbound_elem:
2843 NonReservedWord Iconst
2844 {
2845 $$ = makeDefElem($1, (Node *)makeInteger($2), @1);
2846 }
2847 ;
2848
2849 hash_partbound:
2850 hash_partbound_elem
2851 {
2852 $$ = list_make1($1);
2853 }
2854 | hash_partbound ',' hash_partbound_elem
2855 {
2856 $$ = lappend($1, $3);
2857 }
2858 ;
2859
2860 /*****************************************************************************
2861 *
2862 * ALTER TYPE
2863 *
2864 * really variants of the ALTER TABLE subcommands with different spellings
2865 *****************************************************************************/
2866
2867 AlterCompositeTypeStmt:
2868 ALTER TYPE_P any_name alter_type_cmds
2869 {
2870 AlterTableStmt *n = makeNode(AlterTableStmt);
2871
2872 /* can't use qualified_name, sigh */
2873 n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
2874 n->cmds = $4;
2875 n->relkind = OBJECT_TYPE;
2876 $$ = (Node *)n;
2877 }
2878 ;
2879
2880 alter_type_cmds:
2881 alter_type_cmd { $$ = list_make1($1); }
2882 | alter_type_cmds ',' alter_type_cmd { $$ = lappend($1, $3); }
2883 ;
2884
2885 alter_type_cmd:
2886 /* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
2887 ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
2888 {
2889 AlterTableCmd *n = makeNode(AlterTableCmd);
2890 n->subtype = AT_AddColumn;
2891 n->def = $3;
2892 n->behavior = $4;
2893 $$ = (Node *)n;
2894 }
2895 /* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
2896 | DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
2897 {
2898 AlterTableCmd *n = makeNode(AlterTableCmd);
2899 n->subtype = AT_DropColumn;
2900 n->name = $5;
2901 n->behavior = $6;
2902 n->missing_ok = true;
2903 $$ = (Node *)n;
2904 }
2905 /* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
2906 | DROP ATTRIBUTE ColId opt_drop_behavior
2907 {
2908 AlterTableCmd *n = makeNode(AlterTableCmd);
2909 n->subtype = AT_DropColumn;
2910 n->name = $3;
2911 n->behavior = $4;
2912 n->missing_ok = false;
2913 $$ = (Node *)n;
2914 }
2915 /* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
2916 | ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
2917 {
2918 AlterTableCmd *n = makeNode(AlterTableCmd);
2919 ColumnDef *def = makeNode(ColumnDef);
2920 n->subtype = AT_AlterColumnType;
2921 n->name = $3;
2922 n->def = (Node *) def;
2923 n->behavior = $8;
2924 /* We only use these fields of the ColumnDef node */
2925 def->typeName = $6;
2926 def->collClause = (CollateClause *) $7;
2927 def->raw_default = NULL;
2928 def->location = @3;
2929 $$ = (Node *)n;
2930 }
2931 ;
2932
2933
2934 /*****************************************************************************
2935 *
2936 * QUERY :
2937 * close <portalname>
2938 *
2939 *****************************************************************************/
2940
2941 ClosePortalStmt:
2942 CLOSE cursor_name
2943 {
2944 ClosePortalStmt *n = makeNode(ClosePortalStmt);
2945 n->portalname = $2;
2946 $$ = (Node *)n;
2947 }
2948 | CLOSE ALL
2949 {
2950 ClosePortalStmt *n = makeNode(ClosePortalStmt);
2951 n->portalname = NULL;
2952 $$ = (Node *)n;
2953 }
2954 ;
2955
2956
2957 /*****************************************************************************
2958 *
2959 * QUERY :
2960 * COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
2961 * COPY ( query ) TO file [WITH] [(options)]
2962 *
2963 * where 'query' can be one of:
2964 * { SELECT | UPDATE | INSERT | DELETE }
2965 *
2966 * and 'file' can be one of:
2967 * { PROGRAM 'command' | STDIN | STDOUT | 'filename' }
2968 *
2969 * In the preferred syntax the options are comma-separated
2970 * and use generic identifiers instead of keywords. The pre-9.0
2971 * syntax had a hard-wired, space-separated set of options.
2972 *
2973 * Really old syntax, from versions 7.2 and prior:
2974 * COPY [ BINARY ] table FROM/TO file
2975 * [ [ USING ] DELIMITERS 'delimiter' ] ]
2976 * [ WITH NULL AS 'null string' ]
2977 * This option placement is not supported with COPY (query...).
2978 *
2979 *****************************************************************************/
2980
2981 CopyStmt: COPY opt_binary qualified_name opt_column_list
2982 copy_from opt_program copy_file_name copy_delimiter opt_with
2983 copy_options where_clause
2984 {
2985 CopyStmt *n = makeNode(CopyStmt);
2986 n->relation = $3;
2987 n->query = NULL;
2988 n->attlist = $4;
2989 n->is_from = $5;
2990 n->is_program = $6;
2991 n->filename = $7;
2992 n->whereClause = $11;
2993
2994 if (n->is_program && n->filename == NULL)
2995 ereport(ERROR,
2996 (errcode(ERRCODE_SYNTAX_ERROR),
2997 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
2998 parser_errposition(@8)));
2999
3000 if (!n->is_from && n->whereClause != NULL)
3001 ereport(ERROR,
3002 (errcode(ERRCODE_SYNTAX_ERROR),
3003 errmsg("WHERE clause not allowed with COPY TO"),
3004 parser_errposition(@11)));
3005
3006 n->options = NIL;
3007 /* Concatenate user-supplied flags */
3008 if ($2)
3009 n->options = lappend(n->options, $2);
3010 if ($8)
3011 n->options = lappend(n->options, $8);
3012 if ($10)
3013 n->options = list_concat(n->options, $10);
3014 $$ = (Node *)n;
3015 }
3016 | COPY '(' PreparableStmt ')' TO opt_program copy_file_name opt_with copy_options
3017 {
3018 CopyStmt *n = makeNode(CopyStmt);
3019 n->relation = NULL;
3020 n->query = $3;
3021 n->attlist = NIL;
3022 n->is_from = false;
3023 n->is_program = $6;
3024 n->filename = $7;
3025 n->options = $9;
3026
3027 if (n->is_program && n->filename == NULL)
3028 ereport(ERROR,
3029 (errcode(ERRCODE_SYNTAX_ERROR),
3030 errmsg("STDIN/STDOUT not allowed with PROGRAM"),
3031 parser_errposition(@5)));
3032
3033 $$ = (Node *)n;
3034 }
3035 ;
3036
3037 copy_from:
3038 FROM { $$ = true; }
3039 | TO { $$ = false; }
3040 ;
3041
3042 opt_program:
3043 PROGRAM { $$ = true; }
3044 | /* EMPTY */ { $$ = false; }
3045 ;
3046
3047 /*
3048 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
3049 * used depends on the direction. (It really doesn't make sense to copy from
3050 * stdout. We silently correct the "typo".) - AY 9/94
3051 */
3052 copy_file_name:
3053 Sconst { $$ = $1; }
3054 | STDIN { $$ = NULL; }
3055 | STDOUT { $$ = NULL; }
3056 ;
3057
3058 copy_options: copy_opt_list { $$ = $1; }
3059 | '(' copy_generic_opt_list ')' { $$ = $2; }
3060 ;
3061
3062 /* old COPY option syntax */
3063 copy_opt_list:
3064 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
3065 | /* EMPTY */ { $$ = NIL; }
3066 ;
3067
3068 copy_opt_item:
3069 BINARY
3070 {
3071 $$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3072 }
3073 | FREEZE
3074 {
3075 $$ = makeDefElem("freeze", (Node *)makeInteger(true), @1);
3076 }
3077 | DELIMITER opt_as Sconst
3078 {
3079 $$ = makeDefElem("delimiter", (Node *)makeString($3), @1);
3080 }
3081 | NULL_P opt_as Sconst
3082 {
3083 $$ = makeDefElem("null", (Node *)makeString($3), @1);
3084 }
3085 | CSV
3086 {
3087 $$ = makeDefElem("format", (Node *)makeString("csv"), @1);
3088 }
3089 | HEADER_P
3090 {
3091 $$ = makeDefElem("header", (Node *)makeInteger(true), @1);
3092 }
3093 | QUOTE opt_as Sconst
3094 {
3095 $$ = makeDefElem("quote", (Node *)makeString($3), @1);
3096 }
3097 | ESCAPE opt_as Sconst
3098 {
3099 $$ = makeDefElem("escape", (Node *)makeString($3), @1);
3100 }
3101 | FORCE QUOTE columnList
3102 {
3103 $$ = makeDefElem("force_quote", (Node *)$3, @1);
3104 }
3105 | FORCE QUOTE '*'
3106 {
3107 $$ = makeDefElem("force_quote", (Node *)makeNode(A_Star), @1);
3108 }
3109 | FORCE NOT NULL_P columnList
3110 {
3111 $$ = makeDefElem("force_not_null", (Node *)$4, @1);
3112 }
3113 | FORCE NULL_P columnList
3114 {
3115 $$ = makeDefElem("force_null", (Node *)$3, @1);
3116 }
3117 | ENCODING Sconst
3118 {
3119 $$ = makeDefElem("encoding", (Node *)makeString($2), @1);
3120 }
3121 ;
3122
3123 /* The following exist for backward compatibility with very old versions */
3124
3125 opt_binary:
3126 BINARY
3127 {
3128 $$ = makeDefElem("format", (Node *)makeString("binary"), @1);
3129 }
3130 | /*EMPTY*/ { $$ = NULL; }
3131 ;
3132
3133 copy_delimiter:
3134 opt_using DELIMITERS Sconst
3135 {
3136 $$ = makeDefElem("delimiter", (Node *)makeString($3), @2);
3137 }
3138 | /*EMPTY*/ { $$ = NULL; }
3139 ;
3140
3141 opt_using:
3142 USING {}
3143 | /*EMPTY*/ {}
3144 ;
3145
3146 /* new COPY option syntax */
3147 copy_generic_opt_list:
3148 copy_generic_opt_elem
3149 {
3150 $$ = list_make1($1);
3151 }
3152 | copy_generic_opt_list ',' copy_generic_opt_elem
3153 {
3154 $$ = lappend($1, $3);
3155 }
3156 ;
3157
3158 copy_generic_opt_elem:
3159 ColLabel copy_generic_opt_arg
3160 {
3161 $$ = makeDefElem($1, $2, @1);
3162 }
3163 ;
3164
3165 copy_generic_opt_arg:
3166 opt_boolean_or_string { $$ = (Node *) makeString($1); }
3167 | NumericOnly { $$ = (Node *) $1; }
3168 | '*' { $$ = (Node *) makeNode(A_Star); }
3169 | '(' copy_generic_opt_arg_list ')' { $$ = (Node *) $2; }
3170 | /* EMPTY */ { $$ = NULL; }
3171 ;
3172
3173 copy_generic_opt_arg_list:
3174 copy_generic_opt_arg_list_item
3175 {
3176 $$ = list_make1($1);
3177 }
3178 | copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
3179 {
3180 $$ = lappend($1, $3);
3181 }
3182 ;
3183
3184 /* beware of emitting non-string list elements here; see commands/define.c */
3185 copy_generic_opt_arg_list_item:
3186 opt_boolean_or_string { $$ = (Node *) makeString($1); }
3187 ;
3188
3189
3190 /*****************************************************************************
3191 *
3192 * QUERY :
3193 * CREATE TABLE relname
3194 *
3195 *****************************************************************************/
3196
3197 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
3198 OptInherit OptPartitionSpec table_access_method_clause OptWith
3199 OnCommitOption OptTableSpace
3200 {
3201 CreateStmt *n = makeNode(CreateStmt);
3202 $4->relpersistence = $2;
3203 n->relation = $4;
3204 n->tableElts = $6;
3205 n->inhRelations = $8;
3206 n->partspec = $9;
3207 n->ofTypename = NULL;
3208 n->constraints = NIL;
3209 n->accessMethod = $10;
3210 n->options = $11;
3211 n->oncommit = $12;
3212 n->tablespacename = $13;
3213 n->if_not_exists = false;
3214 $$ = (Node *)n;
3215 }
3216 | CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
3217 OptTableElementList ')' OptInherit OptPartitionSpec table_access_method_clause
3218 OptWith OnCommitOption OptTableSpace
3219 {
3220 CreateStmt *n = makeNode(CreateStmt);
3221 $7->relpersistence = $2;
3222 n->relation = $7;
3223 n->tableElts = $9;
3224 n->inhRelations = $11;
3225 n->partspec = $12;
3226 n->ofTypename = NULL;
3227 n->constraints = NIL;
3228 n->accessMethod = $13;
3229 n->options = $14;
3230 n->oncommit = $15;
3231 n->tablespacename = $16;
3232 n->if_not_exists = true;
3233 $$ = (Node *)n;
3234 }
3235 | CREATE OptTemp TABLE qualified_name OF any_name
3236 OptTypedTableElementList OptPartitionSpec table_access_method_clause
3237 OptWith OnCommitOption OptTableSpace
3238 {
3239 CreateStmt *n = makeNode(CreateStmt);
3240 $4->relpersistence = $2;
3241 n->relation = $4;
3242 n->tableElts = $7;
3243 n->inhRelations = NIL;
3244 n->partspec = $8;
3245 n->ofTypename = makeTypeNameFromNameList($6);
3246 n->ofTypename->location = @6;
3247 n->constraints = NIL;
3248 n->accessMethod = $9;
3249 n->options = $10;
3250 n->oncommit = $11;
3251 n->tablespacename = $12;
3252 n->if_not_exists = false;
3253 $$ = (Node *)n;
3254 }
3255 | CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
3256 OptTypedTableElementList OptPartitionSpec table_access_method_clause
3257 OptWith OnCommitOption OptTableSpace
3258 {
3259 CreateStmt *n = makeNode(CreateStmt);
3260 $7->relpersistence = $2;
3261 n->relation = $7;
3262 n->tableElts = $10;
3263 n->inhRelations = NIL;
3264 n->partspec = $11;
3265 n->ofTypename = makeTypeNameFromNameList($9);
3266 n->ofTypename->location = @9;
3267 n->constraints = NIL;
3268 n->accessMethod = $12;
3269 n->options = $13;
3270 n->oncommit = $14;
3271 n->tablespacename = $15;
3272 n->if_not_exists = true;
3273 $$ = (Node *)n;
3274 }
3275 | CREATE OptTemp TABLE qualified_name PARTITION OF qualified_name
3276 OptTypedTableElementList PartitionBoundSpec OptPartitionSpec
3277 table_access_method_clause OptWith OnCommitOption OptTableSpace
3278 {
3279 CreateStmt *n = makeNode(CreateStmt);
3280 $4->relpersistence = $2;
3281 n->relation = $4;
3282 n->tableElts = $8;
3283 n->inhRelations = list_make1($7);
3284 n->partbound = $9;
3285 n->partspec = $10;
3286 n->ofTypename = NULL;
3287 n->constraints = NIL;
3288 n->accessMethod = $11;
3289 n->options = $12;
3290 n->oncommit = $13;
3291 n->tablespacename = $14;
3292 n->if_not_exists = false;
3293 $$ = (Node *)n;
3294 }
3295 | CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name PARTITION OF
3296 qualified_name OptTypedTableElementList PartitionBoundSpec OptPartitionSpec
3297 table_access_method_clause OptWith OnCommitOption OptTableSpace
3298 {
3299 CreateStmt *n = makeNode(CreateStmt);
3300 $7->relpersistence = $2;
3301 n->relation = $7;
3302 n->tableElts = $11;
3303 n->inhRelations = list_make1($10);
3304 n->partbound = $12;
3305 n->partspec = $13;
3306 n->ofTypename = NULL;
3307 n->constraints = NIL;
3308 n->accessMethod = $14;
3309 n->options = $15;
3310 n->oncommit = $16;
3311 n->tablespacename = $17;
3312 n->if_not_exists = true;
3313 $$ = (Node *)n;
3314 }
3315 ;
3316
3317 /*
3318 * Redundancy here is needed to avoid shift/reduce conflicts,
3319 * since TEMP is not a reserved word. See also OptTempTableName.
3320 *
3321 * NOTE: we accept both GLOBAL and LOCAL options. They currently do nothing,
3322 * but future versions might consider GLOBAL to request SQL-spec-compliant
3323 * temp table behavior, so warn about that. Since we have no modules the
3324 * LOCAL keyword is really meaningless; furthermore, some other products
3325 * implement LOCAL as meaning the same as our default temp table behavior,
3326 * so we'll probably continue to treat LOCAL as a noise word.
3327 */
3328 OptTemp: TEMPORARY { $$ = RELPERSISTENCE_TEMP; }
3329 | TEMP { $$ = RELPERSISTENCE_TEMP; }
3330 | LOCAL TEMPORARY { $$ = RELPERSISTENCE_TEMP; }
3331 | LOCAL TEMP { $$ = RELPERSISTENCE_TEMP; }
3332 | GLOBAL TEMPORARY
3333 {
3334 ereport(WARNING,
3335 (errmsg("GLOBAL is deprecated in temporary table creation"),
3336 parser_errposition(@1)));
3337 $$ = RELPERSISTENCE_TEMP;
3338 }
3339 | GLOBAL TEMP
3340 {
3341 ereport(WARNING,
3342 (errmsg("GLOBAL is deprecated in temporary table creation"),
3343 parser_errposition(@1)));
3344 $$ = RELPERSISTENCE_TEMP;
3345 }
3346 | UNLOGGED { $$ = RELPERSISTENCE_UNLOGGED; }
3347 | /*EMPTY*/ { $$ = RELPERSISTENCE_PERMANENT; }
3348 ;
3349
3350 OptTableElementList:
3351 TableElementList { $$ = $1; }
3352 | /*EMPTY*/ { $$ = NIL; }
3353 ;
3354
3355 OptTypedTableElementList:
3356 '(' TypedTableElementList ')' { $$ = $2; }
3357 | /*EMPTY*/ { $$ = NIL; }
3358 ;
3359
3360 TableElementList:
3361 TableElement
3362 {
3363 $$ = list_make1($1);
3364 }
3365 | TableElementList ',' TableElement
3366 {
3367 $$ = lappend($1, $3);
3368 }
3369 ;
3370
3371 TypedTableElementList:
3372 TypedTableElement
3373 {
3374 $$ = list_make1($1);
3375 }
3376 | TypedTableElementList ',' TypedTableElement
3377 {
3378 $$ = lappend($1, $3);
3379 }
3380 ;
3381
3382 TableElement:
3383 columnDef { $$ = $1; }
3384 | TableLikeClause { $$ = $1; }
3385 | TableConstraint { $$ = $1; }
3386 ;
3387
3388 TypedTableElement:
3389 columnOptions { $$ = $1; }
3390 | TableConstraint { $$ = $1; }
3391 ;
3392
3393 columnDef: ColId Typename create_generic_options ColQualList
3394 {
3395 ColumnDef *n = makeNode(ColumnDef);
3396 n->colname = $1;
3397 n->typeName = $2;
3398 n->inhcount = 0;
3399 n->is_local = true;
3400 n->is_not_null = false;
3401 n->is_from_type = false;
3402 n->storage = 0;
3403 n->raw_default = NULL;
3404 n->cooked_default = NULL;
3405 n->collOid = InvalidOid;
3406 n->fdwoptions = $3;
3407 SplitColQualList($4, &n->constraints, &n->collClause,
3408 yyscanner);
3409 n->location = @1;
3410 $$ = (Node *)n;
3411 }
3412 ;
3413
3414 columnOptions: ColId ColQualList
3415 {
3416 ColumnDef *n = makeNode(ColumnDef);
3417 n->colname = $1;
3418 n->typeName = NULL;
3419 n->inhcount = 0;
3420 n->is_local = true;
3421 n->is_not_null = false;
3422 n->is_from_type = false;
3423 n->storage = 0;
3424 n->raw_default = NULL;
3425 n->cooked_default = NULL;
3426 n->collOid = InvalidOid;
3427 SplitColQualList($2, &n->constraints, &n->collClause,
3428 yyscanner);
3429 n->location = @1;
3430 $$ = (Node *)n;
3431 }
3432 | ColId WITH OPTIONS ColQualList
3433 {
3434 ColumnDef *n = makeNode(ColumnDef);
3435 n->colname = $1;
3436 n->typeName = NULL;
3437 n->inhcount = 0;
3438 n->is_local = true;
3439 n->is_not_null = false;
3440 n->is_from_type = false;
3441 n->storage = 0;
3442 n->raw_default = NULL;
3443 n->cooked_default = NULL;
3444 n->collOid = InvalidOid;
3445 SplitColQualList($4, &n->constraints, &n->collClause,
3446 yyscanner);
3447 n->location = @1;
3448 $$ = (Node *)n;
3449 }
3450 ;
3451
3452 ColQualList:
3453 ColQualList ColConstraint { $$ = lappend($1, $2); }
3454 | /*EMPTY*/ { $$ = NIL; }
3455 ;
3456
3457 ColConstraint:
3458 CONSTRAINT name ColConstraintElem
3459 {
3460 Constraint *n = castNode(Constraint, $3);
3461 n->conname = $2;
3462 n->location = @1;
3463 $$ = (Node *) n;
3464 }
3465 | ColConstraintElem { $$ = $1; }
3466 | ConstraintAttr { $$ = $1; }
3467 | COLLATE any_name
3468 {
3469 /*
3470 * Note: the CollateClause is momentarily included in
3471 * the list built by ColQualList, but we split it out
3472 * again in SplitColQualList.
3473 */
3474 CollateClause *n = makeNode(CollateClause);
3475 n->arg = NULL;
3476 n->collname = $2;
3477 n->location = @1;
3478 $$ = (Node *) n;
3479 }
3480 ;
3481
3482 /* DEFAULT NULL is already the default for Postgres.
3483 * But define it here and carry it forward into the system
3484 * to make it explicit.
3485 * - thomas 1998-09-13
3486 *
3487 * WITH NULL and NULL are not SQL-standard syntax elements,
3488 * so leave them out. Use DEFAULT NULL to explicitly indicate
3489 * that a column may have that value. WITH NULL leads to
3490 * shift/reduce conflicts with WITH TIME ZONE anyway.
3491 * - thomas 1999-01-08
3492 *
3493 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
3494 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
3495 * or be part of a_expr NOT LIKE or similar constructs).
3496 */
3497 ColConstraintElem:
3498 NOT NULL_P
3499 {
3500 Constraint *n = makeNode(Constraint);
3501 n->contype = CONSTR_NOTNULL;
3502 n->location = @1;
3503 $$ = (Node *)n;
3504 }
3505 | NULL_P
3506 {
3507 Constraint *n = makeNode(Constraint);
3508 n->contype = CONSTR_NULL;
3509 n->location = @1;
3510 $$ = (Node *)n;
3511 }
3512 | UNIQUE opt_definition OptConsTableSpace
3513 {
3514 Constraint *n = makeNode(Constraint);
3515 n->contype = CONSTR_UNIQUE;
3516 n->location = @1;
3517 n->keys = NULL;
3518 n->options = $2;
3519 n->indexname = NULL;
3520 n->indexspace = $3;
3521 $$ = (Node *)n;
3522 }
3523 | PRIMARY KEY opt_definition OptConsTableSpace
3524 {
3525 Constraint *n = makeNode(Constraint);
3526 n->contype = CONSTR_PRIMARY;
3527 n->location = @1;
3528 n->keys = NULL;
3529 n->options = $3;
3530 n->indexname = NULL;
3531 n->indexspace = $4;
3532 $$ = (Node *)n;
3533 }
3534 | CHECK '(' a_expr ')' opt_no_inherit
3535 {
3536 Constraint *n = makeNode(Constraint);
3537 n->contype = CONSTR_CHECK;
3538 n->location = @1;
3539 n->is_no_inherit = $5;
3540 n->raw_expr = $3;
3541 n->cooked_expr = NULL;
3542 n->skip_validation = false;
3543 n->initially_valid = true;
3544 $$ = (Node *)n;
3545 }
3546 | DEFAULT b_expr
3547 {
3548 Constraint *n = makeNode(Constraint);
3549 n->contype = CONSTR_DEFAULT;
3550 n->location = @1;
3551 n->raw_expr = $2;
3552 n->cooked_expr = NULL;
3553 n->skip_validation = false;
3554 n->initially_valid = true;
3555 $$ = (Node *)n;
3556 }
3557 | GENERATED generated_when AS IDENTITY_P OptParenthesizedSeqOptList
3558 {
3559 Constraint *n = makeNode(Constraint);
3560 n->contype = CONSTR_IDENTITY;
3561 n->generated_when = $2;
3562 n->options = $5;
3563 n->location = @1;
3564 $$ = (Node *)n;
3565 }
3566 | GENERATED generated_when AS '(' a_expr ')' STORED
3567 {
3568 Constraint *n = makeNode(Constraint);
3569 n->contype = CONSTR_GENERATED;
3570 n->generated_when = $2;
3571 n->raw_expr = $5;
3572 n->cooked_expr = NULL;
3573 n->location = @1;
3574
3575 /*
3576 * Can't do this in the grammar because of shift/reduce
3577 * conflicts. (IDENTITY allows both ALWAYS and BY
3578 * DEFAULT, but generated columns only allow ALWAYS.) We
3579 * can also give a more useful error message and location.
3580 */
3581 if ($2 != ATTRIBUTE_IDENTITY_ALWAYS)
3582 ereport(ERROR,
3583 (errcode(ERRCODE_SYNTAX_ERROR),
3584 errmsg("for a generated column, GENERATED ALWAYS must be specified"),
3585 parser_errposition(@2)));
3586
3587 $$ = (Node *)n;
3588 }
3589 | REFERENCES qualified_name opt_column_list key_match key_actions
3590 {
3591 Constraint *n = makeNode(Constraint);
3592 n->contype = CONSTR_FOREIGN;
3593 n->location = @1;
3594 n->pktable = $2;
3595 n->fk_attrs = NIL;
3596 n->pk_attrs = $3;
3597 n->fk_matchtype = $4;
3598 n->fk_upd_action = (char) ($5 >> 8);
3599 n->fk_del_action = (char) ($5 & 0xFF);
3600 n->skip_validation = false;
3601 n->initially_valid = true;
3602 $$ = (Node *)n;
3603 }
3604 ;
3605
3606 generated_when:
3607 ALWAYS { $$ = ATTRIBUTE_IDENTITY_ALWAYS; }
3608 | BY DEFAULT { $$ = ATTRIBUTE_IDENTITY_BY_DEFAULT; }
3609 ;
3610
3611 /*
3612 * ConstraintAttr represents constraint attributes, which we parse as if
3613 * they were independent constraint clauses, in order to avoid shift/reduce
3614 * conflicts (since NOT might start either an independent NOT NULL clause
3615 * or an attribute). parse_utilcmd.c is responsible for attaching the
3616 * attribute information to the preceding "real" constraint node, and for
3617 * complaining if attribute clauses appear in the wrong place or wrong
3618 * combinations.
3619 *
3620 * See also ConstraintAttributeSpec, which can be used in places where
3621 * there is no parsing conflict. (Note: currently, NOT VALID and NO INHERIT
3622 * are allowed clauses in ConstraintAttributeSpec, but not here. Someday we
3623 * might need to allow them here too, but for the moment it doesn't seem
3624 * useful in the statements that use ConstraintAttr.)
3625 */
3626 ConstraintAttr:
3627 DEFERRABLE
3628 {
3629 Constraint *n = makeNode(Constraint);
3630 n->contype = CONSTR_ATTR_DEFERRABLE;
3631 n->location = @1;
3632 $$ = (Node *)n;
3633 }
3634 | NOT DEFERRABLE
3635 {
3636 Constraint *n = makeNode(Constraint);
3637 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
3638 n->location = @1;
3639 $$ = (Node *)n;
3640 }
3641 | INITIALLY DEFERRED
3642 {
3643 Constraint *n = makeNode(Constraint);
3644 n->contype = CONSTR_ATTR_DEFERRED;
3645 n->location = @1;
3646 $$ = (Node *)n;
3647 }
3648 | INITIALLY IMMEDIATE
3649 {
3650 Constraint *n = makeNode(Constraint);
3651 n->contype = CONSTR_ATTR_IMMEDIATE;
3652 n->location = @1;
3653 $$ = (Node *)n;
3654 }
3655 ;
3656
3657
3658 TableLikeClause:
3659 LIKE qualified_name TableLikeOptionList
3660 {
3661 TableLikeClause *n = makeNode(TableLikeClause);
3662 n->relation = $2;
3663 n->options = $3;
3664 $$ = (Node *)n;
3665 }
3666 ;
3667
3668 TableLikeOptionList:
3669 TableLikeOptionList INCLUDING TableLikeOption { $$ = $1 | $3; }
3670 | TableLikeOptionList EXCLUDING TableLikeOption { $$ = $1 & ~$3; }
3671 | /* EMPTY */ { $$ = 0; }
3672 ;
3673
3674 TableLikeOption:
3675 COMMENTS { $$ = CREATE_TABLE_LIKE_COMMENTS; }
3676 | CONSTRAINTS { $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
3677 | DEFAULTS { $$ = CREATE_TABLE_LIKE_DEFAULTS; }
3678 | IDENTITY_P { $$ = CREATE_TABLE_LIKE_IDENTITY; }
3679 | GENERATED { $$ = CREATE_TABLE_LIKE_GENERATED; }
3680 | INDEXES { $$ = CREATE_TABLE_LIKE_INDEXES; }
3681 | STATISTICS { $$ = CREATE_TABLE_LIKE_STATISTICS; }
3682 | STORAGE { $$ = CREATE_TABLE_LIKE_STORAGE; }
3683 | ALL { $$ = CREATE_TABLE_LIKE_ALL; }
3684 ;
3685
3686
3687 /* ConstraintElem specifies constraint syntax which is not embedded into
3688 * a column definition. ColConstraintElem specifies the embedded form.
3689 * - thomas 1997-12-03
3690 */
3691 TableConstraint:
3692 CONSTRAINT name ConstraintElem
3693 {
3694 Constraint *n = castNode(Constraint, $3);
3695 n->conname = $2;
3696 n->location = @1;
3697 $$ = (Node *) n;
3698 }
3699 | ConstraintElem { $$ = $1; }
3700 ;
3701
3702 ConstraintElem:
3703 CHECK '(' a_expr ')' ConstraintAttributeSpec
3704 {
3705 Constraint *n = makeNode(Constraint);
3706 n->contype = CONSTR_CHECK;
3707 n->location = @1;
3708 n->raw_expr = $3;
3709 n->cooked_expr = NULL;
3710 processCASbits($5, @5, "CHECK",
3711 NULL, NULL, &n->skip_validation,
3712 &n->is_no_inherit, yyscanner);
3713 n->initially_valid = !n->skip_validation;
3714 $$ = (Node *)n;
3715 }
3716 | UNIQUE '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3717 ConstraintAttributeSpec
3718 {
3719 Constraint *n = makeNode(Constraint);
3720 n->contype = CONSTR_UNIQUE;
3721 n->location = @1;
3722 n->keys = $3;
3723 n->including = $5;
3724 n->options = $6;
3725 n->indexname = NULL;
3726 n->indexspace = $7;
3727 processCASbits($8, @8, "UNIQUE",
3728 &n->deferrable, &n->initdeferred, NULL,
3729 NULL, yyscanner);
3730 $$ = (Node *)n;
3731 }
3732 | UNIQUE ExistingIndex ConstraintAttributeSpec
3733 {
3734 Constraint *n = makeNode(Constraint);
3735 n->contype = CONSTR_UNIQUE;
3736 n->location = @1;
3737 n->keys = NIL;
3738 n->including = NIL;
3739 n->options = NIL;
3740 n->indexname = $2;
3741 n->indexspace = NULL;
3742 processCASbits($3, @3, "UNIQUE",
3743 &n->deferrable, &n->initdeferred, NULL,
3744 NULL, yyscanner);
3745 $$ = (Node *)n;
3746 }
3747 | PRIMARY KEY '(' columnList ')' opt_c_include opt_definition OptConsTableSpace
3748 ConstraintAttributeSpec
3749 {
3750 Constraint *n = makeNode(Constraint);
3751 n->contype = CONSTR_PRIMARY;
3752 n->location = @1;
3753 n->keys = $4;
3754 n->including = $6;
3755 n->options = $7;
3756 n->indexname = NULL;
3757 n->indexspace = $8;
3758 processCASbits($9, @9, "PRIMARY KEY",
3759 &n->deferrable, &n->initdeferred, NULL,
3760 NULL, yyscanner);
3761 $$ = (Node *)n;
3762 }
3763 | PRIMARY KEY ExistingIndex ConstraintAttributeSpec
3764 {
3765 Constraint *n = makeNode(Constraint);
3766 n->contype = CONSTR_PRIMARY;
3767 n->location = @1;
3768 n->keys = NIL;
3769 n->including = NIL;
3770 n->options = NIL;
3771 n->indexname = $3;
3772 n->indexspace = NULL;
3773 processCASbits($4, @4, "PRIMARY KEY",
3774 &n->deferrable, &n->initdeferred, NULL,
3775 NULL, yyscanner);
3776 $$ = (Node *)n;
3777 }
3778 | EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
3779 opt_c_include opt_definition OptConsTableSpace ExclusionWhereClause
3780 ConstraintAttributeSpec
3781 {
3782 Constraint *n = makeNode(Constraint);
3783 n->contype = CONSTR_EXCLUSION;
3784 n->location = @1;
3785 n->access_method = $2;
3786 n->exclusions = $4;
3787 n->including = $6;
3788 n->options = $7;
3789 n->indexname = NULL;
3790 n->indexspace = $8;
3791 n->where_clause = $9;
3792 processCASbits($10, @10, "EXCLUDE",
3793 &n->deferrable, &n->initdeferred, NULL,
3794 NULL, yyscanner);
3795 $$ = (Node *)n;
3796 }
3797 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
3798 opt_column_list key_match key_actions ConstraintAttributeSpec
3799 {
3800 Constraint *n = makeNode(Constraint);
3801 n->contype = CONSTR_FOREIGN;
3802 n->location = @1;
3803 n->pktable = $7;
3804 n->fk_attrs = $4;
3805 n->pk_attrs = $8;
3806 n->fk_matchtype = $9;
3807 n->fk_upd_action = (char) ($10 >> 8);
3808 n->fk_del_action = (char) ($10 & 0xFF);
3809 processCASbits($11, @11, "FOREIGN KEY",
3810 &n->deferrable, &n->initdeferred,
3811 &n->skip_validation, NULL,
3812 yyscanner);
3813 n->initially_valid = !n->skip_validation;
3814 $$ = (Node *)n;
3815 }
3816 ;
3817
3818 opt_no_inherit: NO INHERIT { $$ = true; }
3819 | /* EMPTY */ { $$ = false; }
3820 ;
3821
3822 opt_column_list:
3823 '(' columnList ')' { $$ = $2; }
3824 | /*EMPTY*/ { $$ = NIL; }
3825 ;
3826
3827 columnList:
3828 columnElem { $$ = list_make1($1); }
3829 | columnList ',' columnElem { $$ = lappend($1, $3); }
3830 ;
3831
3832 columnElem: ColId
3833 {
3834 $$ = (Node *) makeString($1);
3835 }
3836 ;
3837
3838 opt_c_include: INCLUDE '(' columnList ')' { $$ = $3; }
3839 | /* EMPTY */ { $$ = NIL; }
3840 ;
3841
3842 key_match: MATCH FULL
3843 {
3844 $$ = FKCONSTR_MATCH_FULL;
3845 }
3846 | MATCH PARTIAL
3847 {
3848 ereport(ERROR,
3849 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3850 errmsg("MATCH PARTIAL not yet implemented"),
3851 parser_errposition(@1)));
3852 $$ = FKCONSTR_MATCH_PARTIAL;
3853 }
3854 | MATCH SIMPLE
3855 {
3856 $$ = FKCONSTR_MATCH_SIMPLE;
3857 }
3858 | /*EMPTY*/
3859 {
3860 $$ = FKCONSTR_MATCH_SIMPLE;
3861 }
3862 ;
3863
3864 ExclusionConstraintList:
3865 ExclusionConstraintElem { $$ = list_make1($1); }
3866 | ExclusionConstraintList ',' ExclusionConstraintElem
3867 { $$ = lappend($1, $3); }
3868 ;
3869
3870 ExclusionConstraintElem: index_elem WITH any_operator
3871 {
3872 $$ = list_make2($1, $3);
3873 }
3874 /* allow OPERATOR() decoration for the benefit of ruleutils.c */
3875 | index_elem WITH OPERATOR '(' any_operator ')'
3876 {
3877 $$ = list_make2($1, $5);
3878 }
3879 ;
3880
3881 ExclusionWhereClause:
3882 WHERE '(' a_expr ')' { $$ = $3; }
3883 | /*EMPTY*/ { $$ = NULL; }
3884 ;
3885
3886 /*
3887 * We combine the update and delete actions into one value temporarily
3888 * for simplicity of parsing, and then break them down again in the
3889 * calling production. update is in the left 8 bits, delete in the right.
3890 * Note that NOACTION is the default.
3891 */
3892 key_actions:
3893 key_update
3894 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3895 | key_delete
3896 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
3897 | key_update key_delete
3898 { $$ = ($1 << 8) | ($2 & 0xFF); }
3899 | key_delete key_update
3900 { $$ = ($2 << 8) | ($1 & 0xFF); }
3901 | /*EMPTY*/
3902 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
3903 ;
3904
3905 key_update: ON UPDATE key_action { $$ = $3; }
3906 ;
3907
3908 key_delete: ON DELETE_P key_action { $$ = $3; }
3909 ;
3910
3911 key_action:
3912 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
3913 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
3914 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
3915 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
3916 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
3917 ;
3918
3919 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
3920 | /*EMPTY*/ { $$ = NIL; }
3921 ;
3922
3923 /* Optional partition key specification */
3924 OptPartitionSpec: PartitionSpec { $$ = $1; }
3925 | /*EMPTY*/ { $$ = NULL; }
3926 ;
3927
3928 PartitionSpec: PARTITION BY part_strategy '(' part_params ')'
3929 {
3930 PartitionSpec *n = makeNode(PartitionSpec);
3931
3932 n->strategy = $3;
3933 n->partParams = $5;
3934 n->location = @1;
3935
3936 $$ = n;
3937 }
3938 ;
3939
3940 part_strategy: IDENT { $$ = $1; }
3941 | unreserved_keyword { $$ = pstrdup($1); }
3942 ;
3943
3944 part_params: part_elem { $$ = list_make1($1); }
3945 | part_params ',' part_elem { $$ = lappend($1, $3); }
3946 ;
3947
3948 part_elem: ColId opt_collate opt_class
3949 {
3950 PartitionElem *n = makeNode(PartitionElem);
3951
3952 n->name = $1;
3953 n->expr = NULL;
3954 n->collation = $2;
3955 n->opclass = $3;
3956 n->location = @1;
3957 $$ = n;
3958 }
3959 | func_expr_windowless opt_collate opt_class
3960 {
3961 PartitionElem *n = makeNode(PartitionElem);
3962
3963 n->name = NULL;
3964 n->expr = $1;
3965 n->collation = $2;
3966 n->opclass = $3;
3967 n->location = @1;
3968 $$ = n;
3969 }
3970 | '(' a_expr ')' opt_collate opt_class
3971 {
3972 PartitionElem *n = makeNode(PartitionElem);
3973
3974 n->name = NULL;
3975 n->expr = $2;
3976 n->collation = $4;
3977 n->opclass = $5;
3978 n->location = @1;
3979 $$ = n;
3980 }
3981 ;
3982
3983 table_access_method_clause:
3984 USING access_method { $$ = $2; }
3985 | /*EMPTY*/ { $$ = NULL; }
3986 ;
3987
3988 /* WITHOUT OIDS is legacy only */
3989 OptWith:
3990 WITH reloptions { $$ = $2; }
3991 | WITHOUT OIDS { $$ = NIL; }
3992 | /*EMPTY*/ { $$ = NIL; }
3993 ;
3994
3995 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
3996 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
3997 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
3998 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
3999 ;
4000
4001 OptTableSpace: TABLESPACE name { $$ = $2; }
4002 | /*EMPTY*/ { $$ = NULL; }
4003 ;
4004
4005 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
4006 | /*EMPTY*/ { $$ = NULL; }
4007 ;
4008
4009 ExistingIndex: USING INDEX index_name { $$ = $3; }
4010 ;
4011
4012 /*****************************************************************************
4013 *
4014 * QUERY :
4015 * CREATE STATISTICS [IF NOT EXISTS] stats_name [(stat types)]
4016 * ON expression-list FROM from_list
4017 *
4018 * Note: the expectation here is that the clauses after ON are a subset of
4019 * SELECT syntax, allowing for expressions and joined tables, and probably
4020 * someday a WHERE clause. Much less than that is currently implemented,
4021 * but the grammar accepts it and then we'll throw FEATURE_NOT_SUPPORTED
4022 * errors as necessary at execution.
4023 *
4024 *****************************************************************************/
4025
4026 CreateStatsStmt:
4027 CREATE STATISTICS any_name
4028 opt_name_list ON expr_list FROM from_list
4029 {
4030 CreateStatsStmt *n = makeNode(CreateStatsStmt);
4031 n->defnames = $3;
4032 n->stat_types = $4;
4033 n->exprs = $6;
4034 n->relations = $8;
4035 n->stxcomment = NULL;
4036 n->if_not_exists = false;
4037 $$ = (Node *)n;
4038 }
4039 | CREATE STATISTICS IF_P NOT EXISTS any_name
4040 opt_name_list ON expr_list FROM from_list
4041 {
4042 CreateStatsStmt *n = makeNode(CreateStatsStmt);
4043 n->defnames = $6;
4044 n->stat_types = $7;
4045 n->exprs = $9;
4046 n->relations = $11;
4047 n->stxcomment = NULL;
4048 n->if_not_exists = true;
4049 $$ = (Node *)n;
4050 }
4051 ;
4052
4053 /*****************************************************************************
4054 *
4055 * QUERY :
4056 * CREATE TABLE relname AS SelectStmt [ WITH [NO] DATA ]
4057 *
4058 *
4059 * Note: SELECT ... INTO is a now-deprecated alternative for this.
4060 *
4061 *****************************************************************************/
4062
4063 CreateAsStmt:
4064 CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
4065 {
4066 CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4067 ctas->query = $6;
4068 ctas->into = $4;
4069 ctas->relkind = OBJECT_TABLE;
4070 ctas->is_select_into = false;
4071 ctas->if_not_exists = false;
4072 /* cram additional flags into the IntoClause */
4073 $4->rel->relpersistence = $2;
4074 $4->skipData = !($7);
4075 $$ = (Node *) ctas;
4076 }
4077 | CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS SelectStmt opt_with_data
4078 {
4079 CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4080 ctas->query = $9;
4081 ctas->into = $7;
4082 ctas->relkind = OBJECT_TABLE;
4083 ctas->is_select_into = false;
4084 ctas->if_not_exists = true;
4085 /* cram additional flags into the IntoClause */
4086 $7->rel->relpersistence = $2;
4087 $7->skipData = !($10);
4088 $$ = (Node *) ctas;
4089 }
4090 ;
4091
4092 create_as_target:
4093 qualified_name opt_column_list table_access_method_clause
4094 OptWith OnCommitOption OptTableSpace
4095 {
4096 $$ = makeNode(IntoClause);
4097 $$->rel = $1;
4098 $$->colNames = $2;
4099 $$->accessMethod = $3;
4100 $$->options = $4;
4101 $$->onCommit = $5;
4102 $$->tableSpaceName = $6;
4103 $$->viewQuery = NULL;
4104 $$->skipData = false; /* might get changed later */
4105 }
4106 ;
4107
4108 opt_with_data:
4109 WITH DATA_P { $$ = true; }
4110 | WITH NO DATA_P { $$ = false; }
4111 | /*EMPTY*/ { $$ = true; }
4112 ;
4113
4114
4115 /*****************************************************************************
4116 *
4117 * QUERY :
4118 * CREATE MATERIALIZED VIEW relname AS SelectStmt
4119 *
4120 *****************************************************************************/
4121
4122 CreateMatViewStmt:
4123 CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data
4124 {
4125 CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4126 ctas->query = $7;
4127 ctas->into = $5;
4128 ctas->relkind = OBJECT_MATVIEW;
4129 ctas->is_select_into = false;
4130 ctas->if_not_exists = false;
4131 /* cram additional flags into the IntoClause */
4132 $5->rel->relpersistence = $2;
4133 $5->skipData = !($8);
4134 $$ = (Node *) ctas;
4135 }
4136 | CREATE OptNoLog MATERIALIZED VIEW IF_P NOT EXISTS create_mv_target AS SelectStmt opt_with_data
4137 {
4138 CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
4139 ctas->query = $10;
4140 ctas->into = $8;
4141 ctas->relkind = OBJECT_MATVIEW;
4142 ctas->is_select_into = false;
4143 ctas->if_not_exists = true;
4144 /* cram additional flags into the IntoClause */
4145 $8->rel->relpersistence = $2;
4146 $8->skipData = !($11);
4147 $$ = (Node *) ctas;
4148 }
4149 ;
4150
4151 create_mv_target:
4152 qualified_name opt_column_list table_access_method_clause opt_reloptions OptTableSpace
4153 {
4154 $$ = makeNode(IntoClause);
4155 $$->rel = $1;
4156 $$->colNames = $2;
4157 $$->accessMethod = $3;
4158 $$->options = $4;
4159 $$->onCommit = ONCOMMIT_NOOP;
4160 $$->tableSpaceName = $5;
4161 $$->viewQuery = NULL; /* filled at analysis time */
4162 $$->skipData = false; /* might get changed later */
4163 }
4164 ;
4165
4166 OptNoLog: UNLOGGED { $$ = RELPERSISTENCE_UNLOGGED; }
4167 | /*EMPTY*/ { $$ = RELPERSISTENCE_PERMANENT; }
4168 ;
4169
4170
4171 /*****************************************************************************
4172 *
4173 * QUERY :
4174 * REFRESH MATERIALIZED VIEW qualified_name
4175 *
4176 *****************************************************************************/
4177
4178 RefreshMatViewStmt:
4179 REFRESH MATERIALIZED VIEW opt_concurrently qualified_name opt_with_data
4180 {
4181 RefreshMatViewStmt *n = makeNode(RefreshMatViewStmt);
4182 n->concurrent = $4;
4183 n->relation = $5;
4184 n->skipData = !($6);
4185 $$ = (Node *) n;
4186 }
4187 ;
4188
4189
4190 /*****************************************************************************
4191 *
4192 * QUERY :
4193 * CREATE SEQUENCE seqname
4194 * ALTER SEQUENCE seqname
4195 *
4196 *****************************************************************************/
4197
4198 CreateSeqStmt:
4199 CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
4200 {
4201 CreateSeqStmt *n = makeNode(CreateSeqStmt);
4202 $4->relpersistence = $2;
4203 n->sequence = $4;
4204 n->options = $5;
4205 n->ownerId = InvalidOid;
4206 n->if_not_exists = false;
4207 $$ = (Node *)n;
4208 }
4209 | CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
4210 {
4211 CreateSeqStmt *n = makeNode(CreateSeqStmt);
4212 $7->relpersistence = $2;
4213 n->sequence = $7;
4214 n->options = $8;
4215 n->ownerId = InvalidOid;
4216 n->if_not_exists = true;
4217 $$ = (Node *)n;
4218 }
4219 ;
4220
4221 AlterSeqStmt:
4222 ALTER SEQUENCE qualified_name SeqOptList
4223 {
4224 AlterSeqStmt *n = makeNode(AlterSeqStmt);
4225 n->sequence = $3;
4226 n->options = $4;
4227 n->missing_ok = false;
4228 $$ = (Node *)n;
4229 }
4230 | ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
4231 {
4232 AlterSeqStmt *n = makeNode(AlterSeqStmt);
4233 n->sequence = $5;
4234 n->options = $6;
4235 n->missing_ok = true;
4236 $$ = (Node *)n;
4237 }
4238
4239 ;
4240
4241 OptSeqOptList: SeqOptList { $$ = $1; }
4242 | /*EMPTY*/ { $$ = NIL; }
4243 ;
4244
4245 OptParenthesizedSeqOptList: '(' SeqOptList ')' { $$ = $2; }
4246 | /*EMPTY*/ { $$ = NIL; }
4247 ;
4248
4249 SeqOptList: SeqOptElem { $$ = list_make1($1); }
4250 | SeqOptList SeqOptElem { $$ = lappend($1, $2); }
4251 ;
4252
4253 SeqOptElem: AS SimpleTypename
4254 {
4255 $$ = makeDefElem("as", (Node *)$2, @1);
4256 }
4257 | CACHE NumericOnly
4258 {
4259 $$ = makeDefElem("cache", (Node *)$2, @1);
4260 }
4261 | CYCLE
4262 {
4263 $$ = makeDefElem("cycle", (Node *)makeInteger(true), @1);
4264 }
4265 | NO CYCLE
4266 {
4267 $$ = makeDefElem("cycle", (Node *)makeInteger(false), @1);
4268 }
4269 | INCREMENT opt_by NumericOnly
4270 {
4271 $$ = makeDefElem("increment", (Node *)$3, @1);
4272 }
4273 | MAXVALUE NumericOnly
4274 {
4275 $$ = makeDefElem("maxvalue", (Node *)$2, @1);
4276 }
4277 | MINVALUE NumericOnly
4278 {
4279 $$ = makeDefElem("minvalue", (Node *)$2, @1);
4280 }
4281 | NO MAXVALUE
4282 {
4283 $$ = makeDefElem("maxvalue", NULL, @1);
4284 }
4285 | NO MINVALUE
4286 {
4287 $$ = makeDefElem("minvalue", NULL, @1);
4288 }
4289 | OWNED BY any_name
4290 {
4291 $$ = makeDefElem("owned_by", (Node *)$3, @1);
4292 }
4293 | SEQUENCE NAME_P any_name
4294 {
4295 /* not documented, only used by pg_dump */
4296 $$ = makeDefElem("sequence_name", (Node *)$3, @1);
4297 }
4298 | START opt_with NumericOnly
4299 {
4300 $$ = makeDefElem("start", (Node *)$3, @1);
4301 }
4302 | RESTART
4303 {
4304 $$ = makeDefElem("restart", NULL, @1);
4305 }
4306 | RESTART opt_with NumericOnly
4307 {
4308 $$ = makeDefElem("restart", (Node *)$3, @1);
4309 }
4310 ;
4311
4312 opt_by: BY {}
4313 | /* empty */ {}
4314 ;
4315
4316 NumericOnly:
4317 FCONST { $$ = makeFloat($1); }
4318 | '+' FCONST { $$ = makeFloat($2); }
4319 | '-' FCONST
4320 {
4321 $$ = makeFloat($2);
4322 doNegateFloat($$);
4323 }
4324 | SignedIconst { $$ = makeInteger($1); }
4325 ;
4326
4327 NumericOnly_list: NumericOnly { $$ = list_make1($1); }
4328 | NumericOnly_list ',' NumericOnly { $$ = lappend($1, $3); }
4329 ;
4330
4331 /*****************************************************************************
4332 *
4333 * QUERIES :
4334 * CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
4335 * DROP [PROCEDURAL] LANGUAGE ...
4336 *
4337 *****************************************************************************/
4338
4339 CreatePLangStmt:
4340 CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4341 {
4342 CreatePLangStmt *n = makeNode(CreatePLangStmt);
4343 n->replace = $2;
4344 n->plname = $6;
4345 /* parameters are all to be supplied by system */
4346 n->plhandler = NIL;
4347 n->plinline = NIL;
4348 n->plvalidator = NIL;
4349 n->pltrusted = false;
4350 $$ = (Node *)n;
4351 }
4352 | CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE NonReservedWord_or_Sconst
4353 HANDLER handler_name opt_inline_handler opt_validator
4354 {
4355 CreatePLangStmt *n = makeNode(CreatePLangStmt);
4356 n->replace = $2;
4357 n->plname = $6;
4358 n->plhandler = $8;
4359 n->plinline = $9;
4360 n->plvalidator = $10;
4361 n->pltrusted = $3;
4362 $$ = (Node *)n;
4363 }
4364 ;
4365
4366 opt_trusted:
4367 TRUSTED { $$ = true; }
4368 | /*EMPTY*/ { $$ = false; }
4369 ;
4370
4371 /* This ought to be just func_name, but that causes reduce/reduce conflicts
4372 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
4373 * Work around by using simple names, instead.
4374 */
4375 handler_name:
4376 name { $$ = list_make1(makeString($1)); }
4377 | name attrs { $$ = lcons(makeString($1), $2); }
4378 ;
4379
4380 opt_inline_handler:
4381 INLINE_P handler_name { $$ = $2; }
4382 | /*EMPTY*/ { $$ = NIL; }
4383 ;
4384
4385 validator_clause:
4386 VALIDATOR handler_name { $$ = $2; }
4387 | NO VALIDATOR { $$ = NIL; }
4388 ;
4389
4390 opt_validator:
4391 validator_clause { $$ = $1; }
4392 | /*EMPTY*/ { $$ = NIL; }
4393 ;
4394
4395 DropPLangStmt:
4396 DROP opt_procedural LANGUAGE NonReservedWord_or_Sconst opt_drop_behavior
4397 {
4398 DropStmt *n = makeNode(DropStmt);
4399 n->removeType = OBJECT_LANGUAGE;
4400 n->objects = list_make1(makeString($4));
4401 n->behavior = $5;
4402 n->missing_ok = false;
4403 n->concurrent = false;
4404 $$ = (Node *)n;
4405 }
4406 | DROP opt_procedural LANGUAGE IF_P EXISTS NonReservedWord_or_Sconst opt_drop_behavior
4407 {
4408 DropStmt *n = makeNode(DropStmt);
4409 n->removeType = OBJECT_LANGUAGE;
4410 n->objects = list_make1(makeString($6));
4411 n->behavior = $7;
4412 n->missing_ok = true;
4413 n->concurrent = false;
4414 $$ = (Node *)n;
4415 }
4416 ;
4417
4418 opt_procedural:
4419 PROCEDURAL {}
4420 | /*EMPTY*/ {}
4421 ;
4422
4423 /*****************************************************************************
4424 *
4425 * QUERY:
4426 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
4427 *
4428 *****************************************************************************/
4429
4430 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst opt_reloptions
4431 {
4432 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
4433 n->tablespacename = $3;
4434 n->owner = $4;
4435 n->location = $6;
4436 n->options = $7;
4437 $$ = (Node *) n;
4438 }
4439 ;
4440
4441 OptTableSpaceOwner: OWNER RoleSpec { $$ = $2; }
4442 | /*EMPTY */ { $$ = NULL; }
4443 ;
4444
4445 /*****************************************************************************
4446 *
4447 * QUERY :
4448 * DROP TABLESPACE <tablespace>
4449 *
4450 * No need for drop behaviour as we cannot implement dependencies for
4451 * objects in other databases; we can only support RESTRICT.
4452 *
4453 ****************************************************************************/
4454
4455 DropTableSpaceStmt: DROP TABLESPACE name
4456 {
4457 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4458 n->tablespacename = $3;
4459 n->missing_ok = false;
4460 $$ = (Node *) n;
4461 }
4462 | DROP TABLESPACE IF_P EXISTS name
4463 {
4464 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
4465 n->tablespacename = $5;
4466 n->missing_ok = true;
4467 $$ = (Node *) n;
4468 }
4469 ;
4470
4471 /*****************************************************************************
4472 *
4473 * QUERY:
4474 * CREATE EXTENSION extension
4475 * [ WITH ] [ SCHEMA schema ] [ VERSION version ] [ FROM oldversion ]
4476 *
4477 *****************************************************************************/
4478
4479 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
4480 {
4481 CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4482 n->extname = $3;
4483 n->if_not_exists = false;
4484 n->options = $5;
4485 $$ = (Node *) n;
4486 }
4487 | CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
4488 {
4489 CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
4490 n->extname = $6;
4491 n->if_not_exists = true;
4492 n->options = $8;
4493 $$ = (Node *) n;
4494 }
4495 ;
4496
4497 create_extension_opt_list:
4498 create_extension_opt_list create_extension_opt_item
4499 { $$ = lappend($1, $2); }
4500 | /* EMPTY */
4501 { $$ = NIL; }
4502 ;
4503
4504 create_extension_opt_item:
4505 SCHEMA name
4506 {
4507 $$ = makeDefElem("schema", (Node *)makeString($2), @1);
4508 }
4509 | VERSION_P NonReservedWord_or_Sconst
4510 {
4511 $$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4512 }
4513 | FROM NonReservedWord_or_Sconst
4514 {
4515 $$ = makeDefElem("old_version", (Node *)makeString($2), @1);
4516 }
4517 | CASCADE
4518 {
4519 $$ = makeDefElem("cascade", (Node *)makeInteger(true), @1);
4520 }
4521 ;
4522
4523 /*****************************************************************************
4524 *
4525 * ALTER EXTENSION name UPDATE [ TO version ]
4526 *
4527 *****************************************************************************/
4528
4529 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
4530 {
4531 AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
4532 n->extname = $3;
4533 n->options = $5;
4534 $$ = (Node *) n;
4535 }
4536 ;
4537
4538 alter_extension_opt_list:
4539 alter_extension_opt_list alter_extension_opt_item
4540 { $$ = lappend($1, $2); }
4541 | /* EMPTY */
4542 { $$ = NIL; }
4543 ;
4544
4545 alter_extension_opt_item:
4546 TO NonReservedWord_or_Sconst
4547 {
4548 $$ = makeDefElem("new_version", (Node *)makeString($2), @1);
4549 }
4550 ;
4551
4552 /*****************************************************************************
4553 *
4554 * ALTER EXTENSION name ADD/DROP object-identifier
4555 *
4556 *****************************************************************************/
4557
4558 AlterExtensionContentsStmt:
4559 ALTER EXTENSION name add_drop ACCESS METHOD name
4560 {
4561 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4562 n->extname = $3;
4563 n->action = $4;
4564 n->objtype = OBJECT_ACCESS_METHOD;
4565 n->object = (Node *) makeString($7);
4566 $$ = (Node *)n;
4567 }
4568 | ALTER EXTENSION name add_drop AGGREGATE aggregate_with_argtypes
4569 {
4570 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4571 n->extname = $3;
4572 n->action = $4;
4573 n->objtype = OBJECT_AGGREGATE;
4574 n->object = (Node *) $6;
4575 $$ = (Node *)n;
4576 }
4577 | ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
4578 {
4579 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4580 n->extname = $3;
4581 n->action = $4;
4582 n->objtype = OBJECT_CAST;
4583 n->object = (Node *) list_make2($7, $9);
4584 $$ = (Node *) n;
4585 }
4586 | ALTER EXTENSION name add_drop COLLATION any_name
4587 {
4588 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4589 n->extname = $3;
4590 n->action = $4;
4591 n->objtype = OBJECT_COLLATION;
4592 n->object = (Node *) $6;
4593 $$ = (Node *)n;
4594 }
4595 | ALTER EXTENSION name add_drop CONVERSION_P any_name
4596 {
4597 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4598 n->extname = $3;
4599 n->action = $4;
4600 n->objtype = OBJECT_CONVERSION;
4601 n->object = (Node *) $6;
4602 $$ = (Node *)n;
4603 }
4604 | ALTER EXTENSION name add_drop DOMAIN_P Typename
4605 {
4606 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4607 n->extname = $3;
4608 n->action = $4;
4609 n->objtype = OBJECT_DOMAIN;
4610 n->object = (Node *) $6;
4611 $$ = (Node *)n;
4612 }
4613 | ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
4614 {
4615 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4616 n->extname = $3;
4617 n->action = $4;
4618 n->objtype = OBJECT_FUNCTION;
4619 n->object = (Node *) $6;
4620 $$ = (Node *)n;
4621 }
4622 | ALTER EXTENSION name add_drop opt_procedural LANGUAGE name
4623 {
4624 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4625 n->extname = $3;
4626 n->action = $4;
4627 n->objtype = OBJECT_LANGUAGE;
4628 n->object = (Node *) makeString($7);
4629 $$ = (Node *)n;
4630 }
4631 | ALTER EXTENSION name add_drop OPERATOR operator_with_argtypes
4632 {
4633 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4634 n->extname = $3;
4635 n->action = $4;
4636 n->objtype = OBJECT_OPERATOR;
4637 n->object = (Node *) $6;
4638 $$ = (Node *)n;
4639 }
4640 | ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING access_method
4641 {
4642 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4643 n->extname = $3;
4644 n->action = $4;
4645 n->objtype = OBJECT_OPCLASS;
4646 n->object = (Node *) lcons(makeString($9), $7);
4647 $$ = (Node *)n;
4648 }
4649 | ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING access_method
4650 {
4651 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4652 n->extname = $3;
4653 n->action = $4;
4654 n->objtype = OBJECT_OPFAMILY;
4655 n->object = (Node *) lcons(makeString($9), $7);
4656 $$ = (Node *)n;
4657 }
4658 | ALTER EXTENSION name add_drop PROCEDURE function_with_argtypes
4659 {
4660 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4661 n->extname = $3;
4662 n->action = $4;
4663 n->objtype = OBJECT_PROCEDURE;
4664 n->object = (Node *) $6;
4665 $$ = (Node *)n;
4666 }
4667 | ALTER EXTENSION name add_drop ROUTINE function_with_argtypes
4668 {
4669 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4670 n->extname = $3;
4671 n->action = $4;
4672 n->objtype = OBJECT_ROUTINE;
4673 n->object = (Node *) $6;
4674 $$ = (Node *)n;
4675 }
4676 | ALTER EXTENSION name add_drop SCHEMA name
4677 {
4678 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4679 n->extname = $3;
4680 n->action = $4;
4681 n->objtype = OBJECT_SCHEMA;
4682 n->object = (Node *) makeString($6);
4683 $$ = (Node *)n;
4684 }
4685 | ALTER EXTENSION name add_drop EVENT TRIGGER name
4686 {
4687 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4688 n->extname = $3;
4689 n->action = $4;
4690 n->objtype = OBJECT_EVENT_TRIGGER;
4691 n->object = (Node *) makeString($7);
4692 $$ = (Node *)n;
4693 }
4694 | ALTER EXTENSION name add_drop TABLE any_name
4695 {
4696 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4697 n->extname = $3;
4698 n->action = $4;
4699 n->objtype = OBJECT_TABLE;
4700 n->object = (Node *) $6;
4701 $$ = (Node *)n;
4702 }
4703 | ALTER EXTENSION name add_drop TEXT_P SEARCH PARSER any_name
4704 {
4705 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4706 n->extname = $3;
4707 n->action = $4;
4708 n->objtype = OBJECT_TSPARSER;
4709 n->object = (Node *) $8;
4710 $$ = (Node *)n;
4711 }
4712 | ALTER EXTENSION name add_drop TEXT_P SEARCH DICTIONARY any_name
4713 {
4714 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4715 n->extname = $3;
4716 n->action = $4;
4717 n->objtype = OBJECT_TSDICTIONARY;
4718 n->object = (Node *) $8;
4719 $$ = (Node *)n;
4720 }
4721 | ALTER EXTENSION name add_drop TEXT_P SEARCH TEMPLATE any_name
4722 {
4723 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4724 n->extname = $3;
4725 n->action = $4;
4726 n->objtype = OBJECT_TSTEMPLATE;
4727 n->object = (Node *) $8;
4728 $$ = (Node *)n;
4729 }
4730 | ALTER EXTENSION name add_drop TEXT_P SEARCH CONFIGURATION any_name
4731 {
4732 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4733 n->extname = $3;
4734 n->action = $4;
4735 n->objtype = OBJECT_TSCONFIGURATION;
4736 n->object = (Node *) $8;
4737 $$ = (Node *)n;
4738 }
4739 | ALTER EXTENSION name add_drop SEQUENCE any_name
4740 {
4741 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4742 n->extname = $3;
4743 n->action = $4;
4744 n->objtype = OBJECT_SEQUENCE;
4745 n->object = (Node *) $6;
4746 $$ = (Node *)n;
4747 }
4748 | ALTER EXTENSION name add_drop VIEW any_name
4749 {
4750 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4751 n->extname = $3;
4752 n->action = $4;
4753 n->objtype = OBJECT_VIEW;
4754 n->object = (Node *) $6;
4755 $$ = (Node *)n;
4756 }
4757 | ALTER EXTENSION name add_drop MATERIALIZED VIEW any_name
4758 {
4759 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4760 n->extname = $3;
4761 n->action = $4;
4762 n->objtype = OBJECT_MATVIEW;
4763 n->object = (Node *) $7;
4764 $$ = (Node *)n;
4765 }
4766 | ALTER EXTENSION name add_drop FOREIGN TABLE any_name
4767 {
4768 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4769 n->extname = $3;
4770 n->action = $4;
4771 n->objtype = OBJECT_FOREIGN_TABLE;
4772 n->object = (Node *) $7;
4773 $$ = (Node *)n;
4774 }
4775 | ALTER EXTENSION name add_drop FOREIGN DATA_P WRAPPER name
4776 {
4777 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4778 n->extname = $3;
4779 n->action = $4;
4780 n->objtype = OBJECT_FDW;
4781 n->object = (Node *) makeString($8);
4782 $$ = (Node *)n;
4783 }
4784 | ALTER EXTENSION name add_drop SERVER name
4785 {
4786 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4787 n->extname = $3;
4788 n->action = $4;
4789 n->objtype = OBJECT_FOREIGN_SERVER;
4790 n->object = (Node *) makeString($6);
4791 $$ = (Node *)n;
4792 }
4793 | ALTER EXTENSION name add_drop TRANSFORM FOR Typename LANGUAGE name
4794 {
4795 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4796 n->extname = $3;
4797 n->action = $4;
4798 n->objtype = OBJECT_TRANSFORM;
4799 n->object = (Node *) list_make2($7, makeString($9));
4800 $$ = (Node *)n;
4801 }
4802 | ALTER EXTENSION name add_drop TYPE_P Typename
4803 {
4804 AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
4805 n->extname = $3;
4806 n->action = $4;
4807 n->objtype = OBJECT_TYPE;
4808 n->object = (Node *) $6;
4809 $$ = (Node *)n;
4810 }
4811 ;
4812
4813 /*****************************************************************************
4814 *
4815 * QUERY:
4816 * CREATE FOREIGN DATA WRAPPER name options
4817 *
4818 *****************************************************************************/
4819
4820 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
4821 {
4822 CreateFdwStmt *n = makeNode(CreateFdwStmt);
4823 n->fdwname = $5;
4824 n->func_options = $6;
4825 n->options = $7;
4826 $$ = (Node *) n;
4827 }
4828 ;
4829
4830 fdw_option:
4831 HANDLER handler_name { $$ = makeDefElem("handler", (Node *)$2, @1); }
4832 | NO HANDLER { $$ = makeDefElem("handler", NULL, @1); }
4833 | VALIDATOR handler_name { $$ = makeDefElem("validator", (Node *)$2, @1); }
4834 | NO VALIDATOR { $$ = makeDefElem("validator", NULL, @1); }
4835 ;
4836
4837 fdw_options:
4838 fdw_option { $$ = list_make1($1); }
4839 | fdw_options fdw_option { $$ = lappend($1, $2); }
4840 ;
4841
4842 opt_fdw_options:
4843 fdw_options { $$ = $1; }
4844 | /*EMPTY*/ { $$ = NIL; }
4845 ;
4846
4847 /*****************************************************************************
4848 *
4849 * QUERY :
4850 * ALTER FOREIGN DATA WRAPPER name options
4851 *
4852 ****************************************************************************/
4853
4854 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
4855 {
4856 AlterFdwStmt *n = makeNode(AlterFdwStmt);
4857 n->fdwname = $5;
4858 n->func_options = $6;
4859 n->options = $7;
4860 $$ = (Node *) n;
4861 }
4862 | ALTER FOREIGN DATA_P WRAPPER name fdw_options
4863 {
4864 AlterFdwStmt *n = makeNode(AlterFdwStmt);
4865 n->fdwname = $5;
4866 n->func_options = $6;
4867 n->options = NIL;
4868 $$ = (Node *) n;
4869 }
4870 ;
4871
4872 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
4873 create_generic_options:
4874 OPTIONS '(' generic_option_list ')' { $$ = $3; }
4875 | /*EMPTY*/ { $$ = NIL; }
4876 ;
4877
4878 generic_option_list:
4879 generic_option_elem
4880 {
4881 $$ = list_make1($1);
4882 }
4883 | generic_option_list ',' generic_option_elem
4884 {
4885 $$ = lappend($1, $3);
4886 }
4887 ;
4888
4889 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
4890 alter_generic_options:
4891 OPTIONS '(' alter_generic_option_list ')' { $$ = $3; }
4892 ;
4893
4894 alter_generic_option_list:
4895 alter_generic_option_elem
4896 {
4897 $$ = list_make1($1);
4898 }
4899 | alter_generic_option_list ',' alter_generic_option_elem
4900 {
4901 $$ = lappend($1, $3);
4902 }
4903 ;
4904
4905 alter_generic_option_elem:
4906 generic_option_elem
4907 {
4908 $$ = $1;
4909 }
4910 | SET generic_option_elem
4911 {
4912 $$ = $2;
4913 $$->defaction = DEFELEM_SET;
4914 }
4915 | ADD_P generic_option_elem
4916 {
4917 $$ = $2;
4918 $$->defaction = DEFELEM_ADD;
4919 }
4920 | DROP generic_option_name
4921 {
4922 $$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP, @2);
4923 }
4924 ;
4925
4926 generic_option_elem:
4927 generic_option_name generic_option_arg
4928 {
4929 $$ = makeDefElem($1, $2, @1);
4930 }
4931 ;
4932
4933 generic_option_name:
4934 ColLabel { $$ = $1; }
4935 ;
4936
4937 /* We could use def_arg here, but the spec only requires string literals */
4938 generic_option_arg:
4939 Sconst { $$ = (Node *) makeString($1); }
4940 ;
4941
4942 /*****************************************************************************
4943 *
4944 * QUERY:
4945 * CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
4946 *
4947 *****************************************************************************/
4948
4949 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
4950 FOREIGN DATA_P WRAPPER name create_generic_options
4951 {
4952 CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4953 n->servername = $3;
4954 n->servertype = $4;
4955 n->version = $5;
4956 n->fdwname = $9;
4957 n->options = $10;
4958 n->if_not_exists = false;
4959 $$ = (Node *) n;
4960 }
4961 | CREATE SERVER IF_P NOT EXISTS name opt_type opt_foreign_server_version
4962 FOREIGN DATA_P WRAPPER name create_generic_options
4963 {
4964 CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
4965 n->servername = $6;
4966 n->servertype = $7;
4967 n->version = $8;
4968 n->fdwname = $12;
4969 n->options = $13;
4970 n->if_not_exists = true;
4971 $$ = (Node *) n;
4972 }
4973 ;
4974
4975 opt_type:
4976 TYPE_P Sconst { $$ = $2; }
4977 | /*EMPTY*/ { $$ = NULL; }
4978 ;
4979
4980
4981 foreign_server_version:
4982 VERSION_P Sconst { $$ = $2; }
4983 | VERSION_P NULL_P { $$ = NULL; }
4984 ;
4985
4986 opt_foreign_server_version:
4987 foreign_server_version { $$ = $1; }
4988 | /*EMPTY*/ { $$ = NULL; }
4989 ;
4990
4991 /*****************************************************************************
4992 *
4993 * QUERY :
4994 * ALTER SERVER name [VERSION] [OPTIONS]
4995 *
4996 ****************************************************************************/
4997
4998 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
4999 {
5000 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5001 n->servername = $3;
5002 n->version = $4;
5003 n->options = $5;
5004 n->has_version = true;
5005 $$ = (Node *) n;
5006 }
5007 | ALTER SERVER name foreign_server_version
5008 {
5009 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5010 n->servername = $3;
5011 n->version = $4;
5012 n->has_version = true;
5013 $$ = (Node *) n;
5014 }
5015 | ALTER SERVER name alter_generic_options
5016 {
5017 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
5018 n->servername = $3;
5019 n->options = $4;
5020 $$ = (Node *) n;
5021 }
5022 ;
5023
5024 /*****************************************************************************
5025 *
5026 * QUERY:
5027 * CREATE FOREIGN TABLE relname (...) SERVER name (...)
5028 *
5029 *****************************************************************************/
5030
5031 CreateForeignTableStmt:
5032 CREATE FOREIGN TABLE qualified_name
5033 '(' OptTableElementList ')'
5034 OptInherit SERVER name create_generic_options
5035 {
5036 CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5037 $4->relpersistence = RELPERSISTENCE_PERMANENT;
5038 n->base.relation = $4;
5039 n->base.tableElts = $6;
5040 n->base.inhRelations = $8;
5041 n->base.ofTypename = NULL;
5042 n->base.constraints = NIL;
5043 n->base.options = NIL;
5044 n->base.oncommit = ONCOMMIT_NOOP;
5045 n->base.tablespacename = NULL;
5046 n->base.if_not_exists = false;
5047 /* FDW-specific data */
5048 n->servername = $10;
5049 n->options = $11;
5050 $$ = (Node *) n;
5051 }
5052 | CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5053 '(' OptTableElementList ')'
5054 OptInherit SERVER name create_generic_options
5055 {
5056 CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5057 $7->relpersistence = RELPERSISTENCE_PERMANENT;
5058 n->base.relation = $7;
5059 n->base.tableElts = $9;
5060 n->base.inhRelations = $11;
5061 n->base.ofTypename = NULL;
5062 n->base.constraints = NIL;
5063 n->base.options = NIL;
5064 n->base.oncommit = ONCOMMIT_NOOP;
5065 n->base.tablespacename = NULL;
5066 n->base.if_not_exists = true;
5067 /* FDW-specific data */
5068 n->servername = $13;
5069 n->options = $14;
5070 $$ = (Node *) n;
5071 }
5072 | CREATE FOREIGN TABLE qualified_name
5073 PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5074 SERVER name create_generic_options
5075 {
5076 CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5077 $4->relpersistence = RELPERSISTENCE_PERMANENT;
5078 n->base.relation = $4;
5079 n->base.inhRelations = list_make1($7);
5080 n->base.tableElts = $8;
5081 n->base.partbound = $9;
5082 n->base.ofTypename = NULL;
5083 n->base.constraints = NIL;
5084 n->base.options = NIL;
5085 n->base.oncommit = ONCOMMIT_NOOP;
5086 n->base.tablespacename = NULL;
5087 n->base.if_not_exists = false;
5088 /* FDW-specific data */
5089 n->servername = $11;
5090 n->options = $12;
5091 $$ = (Node *) n;
5092 }
5093 | CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
5094 PARTITION OF qualified_name OptTypedTableElementList PartitionBoundSpec
5095 SERVER name create_generic_options
5096 {
5097 CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
5098 $7->relpersistence = RELPERSISTENCE_PERMANENT;
5099 n->base.relation = $7;
5100 n->base.inhRelations = list_make1($10);
5101 n->base.tableElts = $11;
5102 n->base.partbound = $12;
5103 n->base.ofTypename = NULL;
5104 n->base.constraints = NIL;
5105 n->base.options = NIL;
5106 n->base.oncommit = ONCOMMIT_NOOP;
5107 n->base.tablespacename = NULL;
5108 n->base.if_not_exists = true;
5109 /* FDW-specific data */
5110 n->servername = $14;
5111 n->options = $15;
5112 $$ = (Node *) n;
5113 }
5114 ;
5115
5116 /*****************************************************************************
5117 *
5118 * QUERY:
5119 * ALTER FOREIGN TABLE relname [...]
5120 *
5121 *****************************************************************************/
5122
5123 AlterForeignTableStmt:
5124 ALTER FOREIGN TABLE relation_expr alter_table_cmds
5125 {
5126 AlterTableStmt *n = makeNode(AlterTableStmt);
5127 n->relation = $4;
5128 n->cmds = $5;
5129 n->relkind = OBJECT_FOREIGN_TABLE;
5130 n->missing_ok = false;
5131 $$ = (Node *)n;
5132 }
5133 | ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
5134 {
5135 AlterTableStmt *n = makeNode(AlterTableStmt);
5136 n->relation = $6;
5137 n->cmds = $7;
5138 n->relkind = OBJECT_FOREIGN_TABLE;
5139 n->missing_ok = true;
5140 $$ = (Node *)n;
5141 }
5142 ;
5143
5144 /*****************************************************************************
5145 *
5146 * QUERY:
5147 * IMPORT FOREIGN SCHEMA remote_schema
5148 * [ { LIMIT TO | EXCEPT } ( table_list ) ]
5149 * FROM SERVER server_name INTO local_schema [ OPTIONS (...) ]
5150 *
5151 ****************************************************************************/
5152
5153 ImportForeignSchemaStmt:
5154 IMPORT_P FOREIGN SCHEMA name import_qualification
5155 FROM SERVER name INTO name create_generic_options
5156 {
5157 ImportForeignSchemaStmt *n = makeNode(ImportForeignSchemaStmt);
5158 n->server_name = $8;
5159 n->remote_schema = $4;
5160 n->local_schema = $10;
5161 n->list_type = $5->type;
5162 n->table_list = $5->table_names;
5163 n->options = $11;
5164 $$ = (Node *) n;
5165 }
5166 ;
5167
5168 import_qualification_type:
5169 LIMIT TO { $$ = FDW_IMPORT_SCHEMA_LIMIT_TO; }
5170 | EXCEPT { $$ = FDW_IMPORT_SCHEMA_EXCEPT; }
5171 ;
5172
5173 import_qualification:
5174 import_qualification_type '(' relation_expr_list ')'
5175 {
5176 ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5177 n->type = $1;
5178 n->table_names = $3;
5179 $$ = n;
5180 }
5181 | /*EMPTY*/
5182 {
5183 ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual));
5184 n->type = FDW_IMPORT_SCHEMA_ALL;
5185 n->table_names = NIL;
5186 $$ = n;
5187 }
5188 ;
5189
5190 /*****************************************************************************
5191 *
5192 * QUERY:
5193 * CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
5194 *
5195 *****************************************************************************/
5196
5197 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
5198 {
5199 CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5200 n->user = $5;
5201 n->servername = $7;
5202 n->options = $8;
5203 n->if_not_exists = false;
5204 $$ = (Node *) n;
5205 }
5206 | CREATE USER MAPPING IF_P NOT EXISTS FOR auth_ident SERVER name create_generic_options
5207 {
5208 CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
5209 n->user = $8;
5210 n->servername = $10;
5211 n->options = $11;
5212 n->if_not_exists = true;
5213 $$ = (Node *) n;
5214 }
5215 ;
5216
5217 /* User mapping authorization identifier */
5218 auth_ident: RoleSpec { $$ = $1; }
5219 | USER { $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1); }
5220 ;
5221
5222 /*****************************************************************************
5223 *
5224 * QUERY :
5225 * DROP USER MAPPING FOR auth_ident SERVER name
5226 *
5227 * XXX you'd think this should have a CASCADE/RESTRICT option, even if it's
5228 * only pro forma; but the SQL standard doesn't show one.
5229 ****************************************************************************/
5230
5231 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
5232 {
5233 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5234 n->user = $5;
5235 n->servername = $7;
5236 n->missing_ok = false;
5237 $$ = (Node *) n;
5238 }
5239 | DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
5240 {
5241 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
5242 n->user = $7;
5243 n->servername = $9;
5244 n->missing_ok = true;
5245 $$ = (Node *) n;
5246 }
5247 ;
5248
5249 /*****************************************************************************
5250 *
5251 * QUERY :
5252 * ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
5253 *
5254 ****************************************************************************/
5255
5256 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
5257 {
5258 AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
5259 n->user = $5;
5260 n->servername = $7;
5261 n->options = $8;
5262 $$ = (Node *) n;
5263 }
5264 ;
5265
5266 /*****************************************************************************
5267 *
5268 * QUERIES:
5269 * CREATE POLICY name ON table
5270 * [AS { PERMISSIVE | RESTRICTIVE } ]
5271 * [FOR { SELECT | INSERT | UPDATE | DELETE } ]
5272 * [TO role, ...]
5273 * [USING (qual)] [WITH CHECK (with check qual)]
5274 * ALTER POLICY name ON table [TO role, ...]
5275 * [USING (qual)] [WITH CHECK (with check qual)]
5276 *
5277 *****************************************************************************/
5278
5279 CreatePolicyStmt:
5280 CREATE POLICY name ON qualified_name RowSecurityDefaultPermissive
5281 RowSecurityDefaultForCmd RowSecurityDefaultToRole
5282 RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5283 {
5284 CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
5285 n->policy_name = $3;
5286 n->table = $5;
5287 n->permissive = $6;
5288 n->cmd_name = $7;
5289 n->roles = $8;
5290 n->qual = $9;
5291 n->with_check = $10;
5292 $$ = (Node *) n;
5293 }
5294 ;
5295
5296 AlterPolicyStmt:
5297 ALTER POLICY name ON qualified_name RowSecurityOptionalToRole
5298 RowSecurityOptionalExpr RowSecurityOptionalWithCheck
5299 {
5300 AlterPolicyStmt *n = makeNode(AlterPolicyStmt);
5301 n->policy_name = $3;
5302 n->table = $5;
5303 n->roles = $6;
5304 n->qual = $7;
5305 n->with_check = $8;
5306 $$ = (Node *) n;
5307 }
5308 ;
5309
5310 RowSecurityOptionalExpr:
5311 USING '(' a_expr ')' { $$ = $3; }
5312 | /* EMPTY */ { $$ = NULL; }
5313 ;
5314
5315 RowSecurityOptionalWithCheck:
5316 WITH CHECK '(' a_expr ')' { $$ = $4; }
5317 | /* EMPTY */ { $$ = NULL; }
5318 ;
5319
5320 RowSecurityDefaultToRole:
5321 TO role_list { $$ = $2; }
5322 | /* EMPTY */ { $$ = list_make1(makeRoleSpec(ROLESPEC_PUBLIC, -1)); }
5323 ;
5324
5325 RowSecurityOptionalToRole:
5326 TO role_list { $$ = $2; }
5327 | /* EMPTY */ { $$ = NULL; }
5328 ;
5329
5330 RowSecurityDefaultPermissive:
5331 AS IDENT
5332 {
5333 if (strcmp($2, "permissive") == 0)
5334 $$ = true;
5335 else if (strcmp($2, "restrictive") == 0)
5336 $$ = false;
5337 else
5338 ereport(ERROR,
5339 (errcode(ERRCODE_SYNTAX_ERROR),
5340 errmsg("unrecognized row security option \"%s\"", $2),
5341 errhint("Only PERMISSIVE or RESTRICTIVE policies are supported currently."),
5342 parser_errposition(@2)));
5343
5344 }
5345 | /* EMPTY */ { $$ = true; }
5346 ;
5347
5348 RowSecurityDefaultForCmd:
5349 FOR row_security_cmd { $$ = $2; }
5350 | /* EMPTY */ { $$ = "all"; }
5351 ;
5352
5353 row_security_cmd:
5354 ALL { $$ = "all"; }
5355 | SELECT { $$ = "select"; }
5356 | INSERT { $$ = "insert"; }
5357 | UPDATE { $$ = "update"; }
5358 | DELETE_P { $$ = "delete"; }
5359 ;
5360
5361 /*****************************************************************************
5362 *
5363 * QUERY:
5364 * CREATE ACCESS METHOD name HANDLER handler_name
5365 *
5366 *****************************************************************************/
5367
5368 CreateAmStmt: CREATE ACCESS METHOD name TYPE_P am_type HANDLER handler_name
5369 {
5370 CreateAmStmt *n = makeNode(CreateAmStmt);
5371 n->amname = $4;
5372 n->handler_name = $8;
5373 n->amtype = $6;
5374 $$ = (Node *) n;
5375 }
5376 ;
5377
5378 am_type:
5379 INDEX { $$ = AMTYPE_INDEX; }
5380 | TABLE { $$ = AMTYPE_TABLE; }
5381 ;
5382
5383 /*****************************************************************************
5384 *
5385 * QUERIES :
5386 * CREATE TRIGGER ...
5387 *
5388 *****************************************************************************/
5389
5390 CreateTrigStmt:
5391 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
5392 qualified_name TriggerReferencing TriggerForSpec TriggerWhen
5393 EXECUTE FUNCTION_or_PROCEDURE func_name '(' TriggerFuncArgs ')'
5394 {
5395 CreateTrigStmt *n = makeNode(CreateTrigStmt);
5396 n->trigname = $3;
5397 n->relation = $7;
5398 n->funcname = $13;
5399 n->args = $15;
5400 n->row = $9;
5401 n->timing = $4;
5402 n->events = intVal(linitial($5));
5403 n->columns = (List *) lsecond($5);
5404 n->whenClause = $10;
5405 n->transitionRels = $8;
5406 n->isconstraint = false;
5407 n->deferrable = false;
5408 n->initdeferred = false;
5409 n->constrrel = NULL;
5410 $$ = (Node *)n;
5411 }
5412 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
5413 qualified_name OptConstrFromTable ConstraintAttributeSpec
5414 FOR EACH ROW TriggerWhen
5415 EXECUTE FUNCTION_or_PROCEDURE func_name '(' TriggerFuncArgs ')'
5416 {
5417 CreateTrigStmt *n = makeNode(CreateTrigStmt);
5418 n->trigname = $4;
5419 n->relation = $8;
5420 n->funcname = $17;
5421 n->args = $19;
5422 n->row = true;
5423 n->timing = TRIGGER_TYPE_AFTER;
5424 n->events = intVal(linitial($6));
5425 n->columns = (List *) lsecond($6);
5426 n->whenClause = $14;
5427 n->transitionRels = NIL;
5428 n->isconstraint = true;
5429 processCASbits($10, @10, "TRIGGER",
5430 &n->deferrable, &n->initdeferred, NULL,
5431 NULL, yyscanner);
5432 n->constrrel = $9;
5433 $$ = (Node *)n;
5434 }
5435 ;
5436
5437 TriggerActionTime:
5438 BEFORE { $$ = TRIGGER_TYPE_BEFORE; }
5439 | AFTER { $$ = TRIGGER_TYPE_AFTER; }
5440 | INSTEAD OF { $$ = TRIGGER_TYPE_INSTEAD; }
5441 ;
5442
5443 TriggerEvents:
5444 TriggerOneEvent
5445 { $$ = $1; }
5446 | TriggerEvents OR TriggerOneEvent
5447 {
5448 int events1 = intVal(linitial($1));
5449 int events2 = intVal(linitial($3));
5450 List *columns1 = (List *) lsecond($1);
5451 List *columns2 = (List *) lsecond($3);
5452
5453 if (events1 & events2)
5454 parser_yyerror("duplicate trigger events specified");
5455 /*
5456 * concat'ing the columns lists loses information about
5457 * which columns went with which event, but so long as
5458 * only UPDATE carries columns and we disallow multiple
5459 * UPDATE items, it doesn't matter. Command execution
5460 * should just ignore the columns for non-UPDATE events.
5461 */
5462 $$ = list_make2(makeInteger(events1 | events2),
5463 list_concat(columns1, columns2));
5464 }
5465 ;
5466
5467 TriggerOneEvent:
5468 INSERT
5469 { $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
5470 | DELETE_P
5471 { $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
5472 | UPDATE
5473 { $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
5474 | UPDATE OF columnList
5475 { $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
5476 | TRUNCATE
5477 { $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
5478 ;
5479
5480 TriggerReferencing:
5481 REFERENCING TriggerTransitions { $$ = $2; }
5482 | /*EMPTY*/ { $$ = NIL; }
5483 ;
5484
5485 TriggerTransitions:
5486 TriggerTransition { $$ = list_make1($1); }
5487 | TriggerTransitions TriggerTransition { $$ = lappend($1, $2); }
5488 ;
5489
5490 TriggerTransition:
5491 TransitionOldOrNew TransitionRowOrTable opt_as TransitionRelName
5492 {
5493 TriggerTransition *n = makeNode(TriggerTransition);
5494 n->name = $4;
5495 n->isNew = $1;
5496 n->isTable = $2;
5497 $$ = (Node *)n;
5498 }
5499 ;
5500
5501 TransitionOldOrNew:
5502 NEW { $$ = true; }
5503 | OLD { $$ = false; }
5504 ;
5505
5506 TransitionRowOrTable:
5507 TABLE { $$ = true; }
5508 /*
5509 * According to the standard, lack of a keyword here implies ROW.
5510 * Support for that would require prohibiting ROW entirely here,
5511 * reserving the keyword ROW, and/or requiring AS (instead of
5512 * allowing it to be optional, as the standard specifies) as the
5513 * next token. Requiring ROW seems cleanest and easiest to
5514 * explain.
5515 */
5516 | ROW { $$ = false; }
5517 ;
5518
5519 TransitionRelName:
5520 ColId { $$ = $1; }
5521 ;
5522
5523 TriggerForSpec:
5524 FOR TriggerForOptEach TriggerForType
5525 {
5526 $$ = $3;
5527 }
5528 | /* EMPTY */
5529 {
5530 /*
5531 * If ROW/STATEMENT not specified, default to
5532 * STATEMENT, per SQL
5533 */
5534 $$ = false;
5535 }
5536 ;
5537
5538 TriggerForOptEach:
5539 EACH {}
5540 | /*EMPTY*/ {}
5541 ;
5542
5543 TriggerForType:
5544 ROW { $$ = true; }
5545 | STATEMENT { $$ = false; }
5546 ;
5547
5548 TriggerWhen:
5549 WHEN '(' a_expr ')' { $$ = $3; }
5550 | /*EMPTY*/ { $$ = NULL; }
5551 ;
5552
5553 FUNCTION_or_PROCEDURE:
5554 FUNCTION
5555 | PROCEDURE
5556 ;
5557
5558 TriggerFuncArgs:
5559 TriggerFuncArg { $$ = list_make1($1); }
5560 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
5561 | /*EMPTY*/ { $$ = NIL; }
5562 ;
5563
5564 TriggerFuncArg:
5565 Iconst
5566 {
5567 $$ = makeString(psprintf("%d", $1));
5568 }
5569 | FCONST { $$ = makeString($1); }
5570 | Sconst { $$ = makeString($1); }
5571 | ColLabel { $$ = makeString($1); }
5572 ;
5573
5574 OptConstrFromTable:
5575 FROM qualified_name { $$ = $2; }
5576 | /*EMPTY*/ { $$ = NULL; }
5577 ;
5578
5579 ConstraintAttributeSpec:
5580 /*EMPTY*/
5581 { $$ = 0; }
5582 | ConstraintAttributeSpec ConstraintAttributeElem
5583 {
5584 /*
5585 * We must complain about conflicting options.
5586 * We could, but choose not to, complain about redundant
5587 * options (ie, where $2's bit is already set in $1).
5588 */
5589 int newspec = $1 | $2;
5590
5591 /* special message for this case */
5592 if ((newspec & (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED)) == (CAS_NOT_DEFERRABLE | CAS_INITIALLY_DEFERRED))
5593 ereport(ERROR,
5594 (errcode(ERRCODE_SYNTAX_ERROR),
5595 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
5596 parser_errposition(@2)));
5597 /* generic message for other conflicts */
5598 if ((newspec & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE)) == (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE) ||
5599 (newspec & (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED)) == (CAS_INITIALLY_IMMEDIATE | CAS_INITIALLY_DEFERRED))
5600 ereport(ERROR,
5601 (errcode(ERRCODE_SYNTAX_ERROR),
5602 errmsg("conflicting constraint properties"),
5603 parser_errposition(@2)));
5604 $$ = newspec;
5605 }
5606 ;
5607
5608 ConstraintAttributeElem:
5609 NOT DEFERRABLE { $$ = CAS_NOT_DEFERRABLE; }
5610 | DEFERRABLE { $$ = CAS_DEFERRABLE; }
5611 | INITIALLY IMMEDIATE { $$ = CAS_INITIALLY_IMMEDIATE; }
5612 | INITIALLY DEFERRED { $$ = CAS_INITIALLY_DEFERRED; }
5613 | NOT VALID { $$ = CAS_NOT_VALID; }
5614 | NO INHERIT { $$ = CAS_NO_INHERIT; }
5615 ;
5616
5617
5618 /*****************************************************************************
5619 *
5620 * QUERIES :
5621 * CREATE EVENT TRIGGER ...
5622 * ALTER EVENT TRIGGER ...
5623 *
5624 *****************************************************************************/
5625
5626 CreateEventTrigStmt:
5627 CREATE EVENT TRIGGER name ON ColLabel
5628 EXECUTE FUNCTION_or_PROCEDURE func_name '(' ')'
5629 {
5630 CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5631 n->trigname = $4;
5632 n->eventname = $6;
5633 n->whenclause = NULL;
5634 n->funcname = $9;
5635 $$ = (Node *)n;
5636 }
5637 | CREATE EVENT TRIGGER name ON ColLabel
5638 WHEN event_trigger_when_list
5639 EXECUTE FUNCTION_or_PROCEDURE func_name '(' ')'
5640 {
5641 CreateEventTrigStmt *n = makeNode(CreateEventTrigStmt);
5642 n->trigname = $4;
5643 n->eventname = $6;
5644 n->whenclause = $8;
5645 n->funcname = $11;
5646 $$ = (Node *)n;
5647 }
5648 ;
5649
5650 event_trigger_when_list:
5651 event_trigger_when_item
5652 { $$ = list_make1($1); }
5653 | event_trigger_when_list AND event_trigger_when_item
5654 { $$ = lappend($1, $3); }
5655 ;
5656
5657 event_trigger_when_item:
5658 ColId IN_P '(' event_trigger_value_list ')'
5659 { $$ = makeDefElem($1, (Node *) $4, @1); }
5660 ;
5661
5662 event_trigger_value_list:
5663 SCONST
5664 { $$ = list_make1(makeString($1)); }
5665 | event_trigger_value_list ',' SCONST
5666 { $$ = lappend($1, makeString($3)); }
5667 ;
5668
5669 AlterEventTrigStmt:
5670 ALTER EVENT TRIGGER name enable_trigger
5671 {
5672 AlterEventTrigStmt *n = makeNode(AlterEventTrigStmt);
5673 n->trigname = $4;
5674 n->tgenabled = $5;
5675 $$ = (Node *) n;
5676 }
5677 ;
5678
5679 enable_trigger:
5680 ENABLE_P { $$ = TRIGGER_FIRES_ON_ORIGIN; }
5681 | ENABLE_P REPLICA { $$ = TRIGGER_FIRES_ON_REPLICA; }
5682 | ENABLE_P ALWAYS { $$ = TRIGGER_FIRES_ALWAYS; }
5683 | DISABLE_P { $$ = TRIGGER_DISABLED; }
5684 ;
5685
5686 /*****************************************************************************
5687 *
5688 * QUERY :
5689 * CREATE ASSERTION ...
5690 *
5691 *****************************************************************************/
5692
5693 CreateAssertionStmt:
5694 CREATE ASSERTION any_name CHECK '(' a_expr ')' ConstraintAttributeSpec
5695 {
5696 ereport(ERROR,
5697 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5698 errmsg("CREATE ASSERTION is not yet implemented")));
5699
5700 $$ = NULL;
5701 }
5702 ;
5703
5704
5705 /*****************************************************************************
5706 *
5707 * QUERY :
5708 * define (aggregate,operator,type)
5709 *
5710 *****************************************************************************/
5711
5712 DefineStmt:
5713 CREATE opt_or_replace AGGREGATE func_name aggr_args definition
5714 {
5715 DefineStmt *n = makeNode(DefineStmt);
5716 n->kind = OBJECT_AGGREGATE;
5717 n->oldstyle = false;
5718 n->replace = $2;
5719 n->defnames = $4;
5720 n->args = $5;
5721 n->definition = $6;
5722 $$ = (Node *)n;
5723 }
5724 | CREATE opt_or_replace AGGREGATE func_name old_aggr_definition
5725 {
5726 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
5727 DefineStmt *n = makeNode(DefineStmt);
5728 n->kind = OBJECT_AGGREGATE;
5729 n->oldstyle = true;
5730 n->replace = $2;
5731 n->defnames = $4;
5732 n->args = NIL;
5733 n->definition = $5;
5734 $$ = (Node *)n;
5735 }
5736 | CREATE OPERATOR any_operator definition
5737 {
5738 DefineStmt *n = makeNode(DefineStmt);
5739 n->kind = OBJECT_OPERATOR;
5740 n->oldstyle = false;
5741 n->defnames = $3;
5742 n->args = NIL;
5743 n->definition = $4;
5744 $$ = (Node *)n;
5745 }
5746 | CREATE TYPE_P any_name definition
5747 {
5748 DefineStmt *n = makeNode(DefineStmt);
5749 n->kind = OBJECT_TYPE;
5750 n->oldstyle = false;
5751 n->defnames = $3;
5752 n->args = NIL;
5753 n->definition = $4;
5754 $$ = (Node *)n;
5755 }
5756 | CREATE TYPE_P any_name
5757 {
5758 /* Shell type (identified by lack of definition) */
5759 DefineStmt *n = makeNode(DefineStmt);
5760 n->kind = OBJECT_TYPE;
5761 n->oldstyle = false;
5762 n->defnames = $3;
5763 n->args = NIL;
5764 n->definition = NIL;
5765 $$ = (Node *)n;
5766 }
5767 | CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
5768 {
5769 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
5770
5771 /* can't use qualified_name, sigh */
5772 n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
5773 n->coldeflist = $6;
5774 $$ = (Node *)n;
5775 }
5776 | CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
5777 {
5778 CreateEnumStmt *n = makeNode(CreateEnumStmt);
5779 n->typeName = $3;
5780 n->vals = $7;
5781 $$ = (Node *)n;
5782 }
5783 | CREATE TYPE_P any_name AS RANGE definition
5784 {
5785 CreateRangeStmt *n = makeNode(CreateRangeStmt);
5786 n->typeName = $3;
5787 n->params = $6;
5788 $$ = (Node *)n;
5789 }
5790 | CREATE TEXT_P SEARCH PARSER any_name definition
5791 {
5792 DefineStmt *n = makeNode(DefineStmt);
5793 n->kind = OBJECT_TSPARSER;
5794 n->args = NIL;
5795 n->defnames = $5;
5796 n->definition = $6;
5797 $$ = (Node *)n;
5798 }
5799 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
5800 {
5801 DefineStmt *n = makeNode(DefineStmt);
5802 n->kind = OBJECT_TSDICTIONARY;
5803 n->args = NIL;
5804 n->defnames = $5;
5805 n->definition = $6;
5806 $$ = (Node *)n;
5807 }
5808 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
5809 {
5810 DefineStmt *n = makeNode(DefineStmt);
5811 n->kind = OBJECT_TSTEMPLATE;
5812 n->args = NIL;
5813 n->defnames = $5;
5814 n->definition = $6;
5815 $$ = (Node *)n;
5816 }
5817 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
5818 {
5819 DefineStmt *n = makeNode(DefineStmt);
5820 n->kind = OBJECT_TSCONFIGURATION;
5821 n->args = NIL;
5822 n->defnames = $5;
5823 n->definition = $6;
5824 $$ = (Node *)n;
5825 }
5826 | CREATE COLLATION any_name definition
5827 {
5828 DefineStmt *n = makeNode(DefineStmt);
5829 n->kind = OBJECT_COLLATION;
5830 n->args = NIL;
5831 n->defnames = $3;
5832 n->definition = $4;
5833 $$ = (Node *)n;
5834 }
5835 | CREATE COLLATION IF_P NOT EXISTS any_name definition
5836 {
5837 DefineStmt *n = makeNode(DefineStmt);
5838 n->kind = OBJECT_COLLATION;
5839 n->args = NIL;
5840 n->defnames = $6;
5841 n->definition = $7;
5842 n->if_not_exists = true;
5843 $$ = (Node *)n;
5844 }
5845 | CREATE COLLATION any_name FROM any_name
5846 {
5847 DefineStmt *n = makeNode(DefineStmt);
5848 n->kind = OBJECT_COLLATION;
5849 n->args = NIL;
5850 n->defnames = $3;
5851 n->definition = list_make1(makeDefElem("from", (Node *) $5, @5));
5852 $$ = (Node *)n;
5853 }
5854 | CREATE COLLATION IF_P NOT EXISTS any_name FROM any_name
5855 {
5856 DefineStmt *n = makeNode(DefineStmt);
5857 n->kind = OBJECT_COLLATION;
5858 n->args = NIL;
5859 n->defnames = $6;
5860 n->definition = list_make1(makeDefElem("from", (Node *) $8, @8));
5861 n->if_not_exists = true;
5862 $$ = (Node *)n;
5863 }
5864 ;
5865
5866 definition: '(' def_list ')' { $$ = $2; }
5867 ;
5868
5869 def_list: def_elem { $$ = list_make1($1); }
5870 | def_list ',' def_elem { $$ = lappend($1, $3); }
5871 ;
5872
5873 def_elem: ColLabel '=' def_arg
5874 {
5875 $$ = makeDefElem($1, (Node *) $3, @1);
5876 }
5877 | ColLabel
5878 {
5879 $$ = makeDefElem($1, NULL, @1);
5880 }
5881 ;
5882
5883 /* Note: any simple identifier will be returned as a type name! */
5884 def_arg: func_type { $$ = (Node *)$1; }
5885 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
5886 | qual_all_Op { $$ = (Node *)$1; }
5887 | NumericOnly { $$ = (Node *)$1; }
5888 | Sconst { $$ = (Node *)makeString($1); }
5889 | NONE { $$ = (Node *)makeString(pstrdup($1)); }
5890 ;
5891
5892 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
5893 ;
5894
5895 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
5896 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
5897 ;
5898
5899 /*
5900 * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
5901 * the item names needed in old aggregate definitions are likely to become
5902 * SQL keywords.
5903 */
5904 old_aggr_elem: IDENT '=' def_arg
5905 {
5906 $$ = makeDefElem($1, (Node *)$3, @1);
5907 }
5908 ;
5909
5910 opt_enum_val_list:
5911 enum_val_list { $$ = $1; }
5912 | /*EMPTY*/ { $$ = NIL; }
5913 ;
5914
5915 enum_val_list: Sconst
5916 { $$ = list_make1(makeString($1)); }
5917 | enum_val_list ',' Sconst
5918 { $$ = lappend($1, makeString($3)); }
5919 ;
5920
5921 /*****************************************************************************
5922 *
5923 * ALTER TYPE enumtype ADD ...
5924 *
5925 *****************************************************************************/
5926
5927 AlterEnumStmt:
5928 ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst
5929 {
5930 AlterEnumStmt *n = makeNode(AlterEnumStmt);
5931 n->typeName = $3;
5932 n->oldVal = NULL;
5933 n->newVal = $7;
5934 n->newValNeighbor = NULL;
5935 n->newValIsAfter = true;
5936 n->skipIfNewValExists = $6;
5937 $$ = (Node *) n;
5938 }
5939 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst BEFORE Sconst
5940 {
5941 AlterEnumStmt *n = makeNode(AlterEnumStmt);
5942 n->typeName = $3;
5943 n->oldVal = NULL;
5944 n->newVal = $7;
5945 n->newValNeighbor = $9;
5946 n->newValIsAfter = false;
5947 n->skipIfNewValExists = $6;
5948 $$ = (Node *) n;
5949 }
5950 | ALTER TYPE_P any_name ADD_P VALUE_P opt_if_not_exists Sconst AFTER Sconst
5951 {
5952 AlterEnumStmt *n = makeNode(AlterEnumStmt);
5953 n->typeName = $3;
5954 n->oldVal = NULL;
5955 n->newVal = $7;
5956 n->newValNeighbor = $9;
5957 n->newValIsAfter = true;
5958 n->skipIfNewValExists = $6;
5959 $$ = (Node *) n;
5960 }
5961 | ALTER TYPE_P any_name RENAME VALUE_P Sconst TO Sconst
5962 {
5963 AlterEnumStmt *n = makeNode(AlterEnumStmt);
5964 n->typeName = $3;
5965 n->oldVal = $6;
5966 n->newVal = $8;
5967 n->newValNeighbor = NULL;
5968 n->newValIsAfter = false;
5969 n->skipIfNewValExists = false;
5970 $$ = (Node *) n;
5971 }
5972 ;
5973
5974 opt_if_not_exists: IF_P NOT EXISTS { $$ = true; }
5975 | /* empty */ { $$ = false; }
5976 ;
5977
5978
5979 /*****************************************************************************
5980 *
5981 * QUERIES :
5982 * CREATE OPERATOR CLASS ...
5983 * CREATE OPERATOR FAMILY ...
5984 * ALTER OPERATOR FAMILY ...
5985 * DROP OPERATOR CLASS ...
5986 * DROP OPERATOR FAMILY ...
5987 *
5988 *****************************************************************************/
5989
5990 CreateOpClassStmt:
5991 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
5992 USING access_method opt_opfamily AS opclass_item_list
5993 {
5994 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
5995 n->opclassname = $4;
5996 n->isDefault = $5;
5997 n->datatype = $8;
5998 n->amname = $10;
5999 n->opfamilyname = $11;
6000 n->items = $13;
6001 $$ = (Node *) n;
6002 }
6003 ;
6004
6005 opclass_item_list:
6006 opclass_item { $$ = list_make1($1); }
6007 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
6008 ;
6009
6010 opclass_item:
6011 OPERATOR Iconst any_operator opclass_purpose opt_recheck
6012 {
6013 CreateOpClassItem *n = makeNode(CreateOpClassItem);
6014 ObjectWithArgs *owa = makeNode(ObjectWithArgs);
6015 owa->objname = $3;
6016 owa->objargs = NIL;
6017 n->itemtype = OPCLASS_ITEM_OPERATOR;
6018 n->name = owa;
6019 n->number = $2;
6020 n->order_family = $4;
6021 $$ = (Node *) n;
6022 }
6023 | OPERATOR Iconst operator_with_argtypes opclass_purpose
6024 opt_recheck
6025 {
6026 CreateOpClassItem *n = makeNode(CreateOpClassItem);
6027 n->itemtype = OPCLASS_ITEM_OPERATOR;
6028 n->name = $3;
6029 n->number = $2;
6030 n->order_family = $4;
6031 $$ = (Node *) n;
6032 }
6033 | FUNCTION Iconst function_with_argtypes
6034 {
6035 CreateOpClassItem *n = makeNode(CreateOpClassItem);
6036 n->itemtype = OPCLASS_ITEM_FUNCTION;
6037 n->name = $3;
6038 n->number = $2;
6039 $$ = (Node *) n;
6040 }
6041 | FUNCTION Iconst '(' type_list ')' function_with_argtypes
6042 {
6043 CreateOpClassItem *n = makeNode(CreateOpClassItem);
6044 n->itemtype = OPCLASS_ITEM_FUNCTION;
6045 n->name = $6;
6046 n->number = $2;
6047 n->class_args = $4;
6048 $$ = (Node *) n;
6049 }
6050 | STORAGE Typename
6051 {
6052 CreateOpClassItem *n = makeNode(CreateOpClassItem);
6053 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
6054 n->storedtype = $2;
6055 $$ = (Node *) n;
6056 }
6057 ;
6058
6059 opt_default: DEFAULT { $$ = true; }
6060 | /*EMPTY*/ { $$ = false; }
6061 ;
6062
6063 opt_opfamily: FAMILY any_name { $$ = $2; }
6064 | /*EMPTY*/ { $$ = NIL; }
6065 ;
6066
6067 opclass_purpose: FOR SEARCH { $$ = NIL; }
6068 | FOR ORDER BY any_name { $$ = $4; }
6069 | /*EMPTY*/ { $$ = NIL; }
6070 ;
6071
6072 opt_recheck: RECHECK
6073 {
6074 /*
6075 * RECHECK no longer does anything in opclass definitions,
6076 * but we still accept it to ease porting of old database
6077 * dumps.
6078 */
6079 ereport(NOTICE,
6080 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6081 errmsg("RECHECK is no longer required"),
6082 errhint("Update your data type."),
6083 parser_errposition(@1)));
6084 $$ = true;
6085 }
6086 | /*EMPTY*/ { $$ = false; }
6087 ;
6088
6089
6090 CreateOpFamilyStmt:
6091 CREATE OPERATOR FAMILY any_name USING access_method
6092 {
6093 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
6094 n->opfamilyname = $4;
6095 n->amname = $6;
6096 $$ = (Node *) n;
6097 }
6098 ;
6099
6100 AlterOpFamilyStmt:
6101 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
6102 {
6103 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6104 n->opfamilyname = $4;
6105 n->amname = $6;
6106 n->isDrop = false;
6107 n->items = $8;
6108 $$ = (Node *) n;
6109 }
6110 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
6111 {
6112 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
6113 n->opfamilyname = $4;
6114 n->amname = $6;
6115 n->isDrop = true;
6116 n->items = $8;
6117 $$ = (Node *) n;
6118 }
6119 ;
6120
6121 opclass_drop_list:
6122 opclass_drop { $$ = list_make1($1); }
6123 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
6124 ;
6125
6126 opclass_drop:
6127 OPERATOR Iconst '(' type_list ')'
6128 {
6129 CreateOpClassItem *n = makeNode(CreateOpClassItem);
6130 n->itemtype = OPCLASS_ITEM_OPERATOR;
6131 n->number = $2;
6132 n->class_args = $4;
6133 $$ = (Node *) n;
6134 }
6135 | FUNCTION Iconst '(' type_list ')'
6136 {
6137 CreateOpClassItem *n = makeNode(CreateOpClassItem);
6138 n->itemtype = OPCLASS_ITEM_FUNCTION;
6139 n->number = $2;
6140 n->class_args = $4;
6141 $$ = (Node *) n;
6142 }
6143 ;
6144
6145
6146 DropOpClassStmt:
6147 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
6148 {
6149 DropStmt *n = makeNode(DropStmt);
6150 n->objects = list_make1(lcons(makeString($6), $4));
6151 n->removeType = OBJECT_OPCLASS;
6152 n->behavior = $7;
6153 n->missing_ok = false;
6154 n->concurrent = false;
6155 $$ = (Node *) n;
6156 }
6157 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
6158 {
6159 DropStmt *n = makeNode(DropStmt);
6160 n->objects = list_make1(lcons(makeString($8), $6));
6161 n->removeType = OBJECT_OPCLASS;
6162 n->behavior = $9;
6163 n->missing_ok = true;
6164 n->concurrent = false;
6165 $$ = (Node *) n;
6166 }
6167 ;
6168
6169 DropOpFamilyStmt:
6170 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
6171 {
6172 DropStmt *n = makeNode(DropStmt);
6173 n->objects = list_make1(lcons(makeString($6), $4));
6174 n->removeType = OBJECT_OPFAMILY;
6175 n->behavior = $7;
6176 n->missing_ok = false;
6177 n->concurrent = false;
6178 $$ = (Node *) n;
6179 }
6180 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
6181 {
6182 DropStmt *n = makeNode(DropStmt);
6183 n->objects = list_make1(lcons(makeString($8), $6));
6184 n->removeType = OBJECT_OPFAMILY;
6185 n->behavior = $9;
6186 n->missing_ok = true;
6187 n->concurrent = false;
6188 $$ = (Node *) n;
6189 }
6190 ;
6191
6192
6193 /*****************************************************************************
6194 *
6195 * QUERY:
6196 *
6197 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
6198 * REASSIGN OWNED BY username [, username ...] TO username
6199 *
6200 *****************************************************************************/
6201 DropOwnedStmt:
6202 DROP OWNED BY role_list opt_drop_behavior
6203 {
6204 DropOwnedStmt *n = makeNode(DropOwnedStmt);
6205 n->roles = $4;
6206 n->behavior = $5;
6207 $$ = (Node *)n;
6208 }
6209 ;
6210
6211 ReassignOwnedStmt:
6212 REASSIGN OWNED BY role_list TO RoleSpec
6213 {
6214 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
6215 n->roles = $4;
6216 n->newrole = $6;
6217 $$ = (Node *)n;
6218 }
6219 ;
6220
6221 /*****************************************************************************
6222 *
6223 * QUERY:
6224 *
6225 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
6226 * [ RESTRICT | CASCADE ]
6227 *
6228 *****************************************************************************/
6229
6230 DropStmt: DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
6231 {
6232 DropStmt *n = makeNode(DropStmt);
6233 n->removeType = $2;
6234 n->missing_ok = true;
6235 n->objects = $5;
6236 n->behavior = $6;
6237 n->concurrent = false;
6238 $$ = (Node *)n;
6239 }
6240 | DROP drop_type_any_name any_name_list opt_drop_behavior
6241 {
6242 DropStmt *n = makeNode(DropStmt);
6243 n->removeType = $2;
6244 n->missing_ok = false;
6245 n->objects = $3;
6246 n->behavior = $4;
6247 n->concurrent = false;
6248 $$ = (Node *)n;
6249 }
6250 | DROP drop_type_name IF_P EXISTS name_list opt_drop_behavior
6251 {
6252 DropStmt *n = makeNode(DropStmt);
6253 n->removeType = $2;
6254 n->missing_ok = true;
6255 n->objects = $5;
6256 n->behavior = $6;
6257 n->concurrent = false;
6258 $$ = (Node *)n;
6259 }
6260 | DROP drop_type_name name_list opt_drop_behavior
6261 {
6262 DropStmt *n = makeNode(DropStmt);
6263 n->removeType = $2;
6264 n->missing_ok = false;
6265 n->objects = $3;
6266 n->behavior = $4;
6267 n->concurrent = false;
6268 $$ = (Node *)n;
6269 }
6270 | DROP drop_type_name_on_any_name name ON any_name opt_drop_behavior
6271 {
6272 DropStmt *n = makeNode(DropStmt);
6273 n->removeType = $2;
6274 n->objects = list_make1(lappend($5, makeString($3)));
6275 n->behavior = $6;
6276 n->missing_ok = false;
6277 n->concurrent = false;
6278 $$ = (Node *) n;
6279 }
6280 | DROP drop_type_name_on_any_name IF_P EXISTS name ON any_name opt_drop_behavior
6281 {
6282 DropStmt *n = makeNode(DropStmt);
6283 n->removeType = $2;
6284 n->objects = list_make1(lappend($7, makeString($5)));
6285 n->behavior = $8;
6286 n->missing_ok = true;
6287 n->concurrent = false;
6288 $$ = (Node *) n;
6289 }
6290 | DROP TYPE_P type_name_list opt_drop_behavior
6291 {
6292 DropStmt *n = makeNode(DropStmt);
6293 n->removeType = OBJECT_TYPE;
6294 n->missing_ok = false;
6295 n->objects = $3;
6296 n->behavior = $4;
6297 n->concurrent = false;
6298 $$ = (Node *) n;
6299 }
6300 | DROP TYPE_P IF_P EXISTS type_name_list opt_drop_behavior
6301 {
6302 DropStmt *n = makeNode(DropStmt);
6303 n->removeType = OBJECT_TYPE;
6304 n->missing_ok = true;
6305 n->objects = $5;
6306 n->behavior = $6;
6307 n->concurrent = false;
6308 $$ = (Node *) n;
6309 }
6310 | DROP DOMAIN_P type_name_list opt_drop_behavior
6311 {
6312 DropStmt *n = makeNode(DropStmt);
6313 n->removeType = OBJECT_DOMAIN;
6314 n->missing_ok = false;
6315 n->objects = $3;
6316 n->behavior = $4;
6317 n->concurrent = false;
6318 $$ = (Node *) n;
6319 }
6320 | DROP DOMAIN_P IF_P EXISTS type_name_list opt_drop_behavior
6321 {
6322 DropStmt *n = makeNode(DropStmt);
6323 n->removeType = OBJECT_DOMAIN;
6324 n->missing_ok = true;
6325 n->objects = $5;
6326 n->behavior = $6;
6327 n->concurrent = false;
6328 $$ = (Node *) n;
6329 }
6330 | DROP INDEX CONCURRENTLY any_name_list opt_drop_behavior
6331 {
6332 DropStmt *n = makeNode(DropStmt);
6333 n->removeType = OBJECT_INDEX;
6334 n->missing_ok = false;
6335 n->objects = $4;
6336 n->behavior = $5;
6337 n->concurrent = true;
6338 $$ = (Node *)n;
6339 }
6340 | DROP INDEX CONCURRENTLY IF_P EXISTS any_name_list opt_drop_behavior
6341 {
6342 DropStmt *n = makeNode(DropStmt);
6343 n->removeType = OBJECT_INDEX;
6344 n->missing_ok = true;
6345 n->objects = $6;
6346 n->behavior = $7;
6347 n->concurrent = true;
6348 $$ = (Node *)n;
6349 }
6350 ;
6351
6352 /* object types taking any_name_list */
6353 drop_type_any_name:
6354 TABLE { $$ = OBJECT_TABLE; }
6355 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
6356 | VIEW { $$ = OBJECT_VIEW; }
6357 | MATERIALIZED VIEW { $$ = OBJECT_MATVIEW; }
6358 | INDEX { $$ = OBJECT_INDEX; }
6359 | FOREIGN TABLE { $$ = OBJECT_FOREIGN_TABLE; }
6360 | COLLATION { $$ = OBJECT_COLLATION; }
6361 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
6362 | STATISTICS { $$ = OBJECT_STATISTIC_EXT; }
6363 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
6364 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
6365 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
6366 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
6367 ;
6368
6369 /* object types taking name_list */
6370 drop_type_name:
6371 ACCESS METHOD { $$ = OBJECT_ACCESS_METHOD; }
6372 | EVENT TRIGGER { $$ = OBJECT_EVENT_TRIGGER; }
6373 | EXTENSION { $$ = OBJECT_EXTENSION; }
6374 | FOREIGN DATA_P WRAPPER { $$ = OBJECT_FDW; }
6375 | PUBLICATION { $$ = OBJECT_PUBLICATION; }
6376 | SCHEMA { $$ = OBJECT_SCHEMA; }
6377 | SERVER { $$ = OBJECT_FOREIGN_SERVER; }
6378 ;
6379
6380 /* object types attached to a table */
6381 drop_type_name_on_any_name:
6382 POLICY { $$ = OBJECT_POLICY; }
6383 | RULE { $$ = OBJECT_RULE; }
6384 | TRIGGER { $$ = OBJECT_TRIGGER; }
6385 ;
6386
6387 any_name_list:
6388 any_name { $$ = list_make1($1); }
6389 | any_name_list ',' any_name { $$ = lappend($1, $3); }
6390 ;
6391
6392 any_name: ColId { $$ = list_make1(makeString($1)); }
6393 | ColId attrs { $$ = lcons(makeString($1), $2); }
6394 ;
6395
6396 attrs: '.' attr_name
6397 { $$ = list_make1(makeString($2)); }
6398 | attrs '.' attr_name
6399 { $$ = lappend($1, makeString($3)); }
6400 ;
6401
6402 type_name_list:
6403 Typename { $$ = list_make1($1); }
6404 | type_name_list ',' Typename { $$ = lappend($1, $3); }
6405 ;
6406
6407 /*****************************************************************************
6408 *
6409 * QUERY:
6410 * truncate table relname1, relname2, ...
6411 *
6412 *****************************************************************************/
6413
6414 TruncateStmt:
6415 TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
6416 {
6417 TruncateStmt *n = makeNode(TruncateStmt);
6418 n->relations = $3;
6419 n->restart_seqs = $4;
6420 n->behavior = $5;
6421 $$ = (Node *)n;
6422 }
6423 ;
6424
6425 opt_restart_seqs:
6426 CONTINUE_P IDENTITY_P { $$ = false; }
6427 | RESTART IDENTITY_P { $$ = true; }
6428 | /* EMPTY */ { $$ = false; }
6429 ;
6430
6431 /*****************************************************************************
6432 *
6433 * The COMMENT ON statement can take different forms based upon the type of
6434 * the object associated with the comment. The form of the statement is:
6435 *
6436 * COMMENT ON [ [ ACCESS METHOD | CONVERSION | COLLATION |
6437 * DATABASE | DOMAIN |
6438 * EXTENSION | EVENT TRIGGER | FOREIGN DATA WRAPPER |
6439 * FOREIGN TABLE | INDEX | [PROCEDURAL] LANGUAGE |
6440 * MATERIALIZED VIEW | POLICY | ROLE | SCHEMA | SEQUENCE |
6441 * SERVER | TABLE | TABLESPACE |
6442 * TEXT SEARCH CONFIGURATION | TEXT SEARCH DICTIONARY |
6443 * TEXT SEARCH PARSER | TEXT SEARCH TEMPLATE | TYPE |
6444 * VIEW] <objname> |
6445 * AGGREGATE <aggname> (arg1, ...) |
6446 * CAST (<src type> AS <dst type>) |
6447 * COLUMN <relname>.<colname> |
6448 * CONSTRAINT <constraintname> ON <relname> |
6449 * CONSTRAINT <constraintname> ON DOMAIN <domainname> |
6450 * FUNCTION <funcname> (arg1, arg2, ...) |
6451 * LARGE OBJECT <oid> |
6452 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
6453 * OPERATOR CLASS <name> USING <access-method> |
6454 * OPERATOR FAMILY <name> USING <access-method> |
6455 * RULE <rulename> ON <relname> |
6456 * TRIGGER <triggername> ON <relname> ]
6457 * IS { 'text' | NULL }
6458 *
6459 *****************************************************************************/
6460
6461 CommentStmt:
6462 COMMENT ON comment_type_any_name any_name IS comment_text
6463 {
6464 CommentStmt *n = makeNode(CommentStmt);
6465 n->objtype = $3;
6466 n->object = (Node *) $4;
6467 n->comment = $6;
6468 $$ = (Node *) n;
6469 }
6470 | COMMENT ON comment_type_name name IS comment_text
6471 {
6472 CommentStmt *n = makeNode(CommentStmt);
6473 n->objtype = $3;
6474 n->object = (Node *) makeString($4);
6475 n->comment = $6;
6476 $$ = (Node *) n;
6477 }
6478 | COMMENT ON TYPE_P Typename IS comment_text
6479 {
6480 CommentStmt *n = makeNode(CommentStmt);
6481 n->objtype = OBJECT_TYPE;
6482 n->object = (Node *) $4;
6483 n->comment = $6;
6484 $$ = (Node *) n;
6485 }
6486 | COMMENT ON DOMAIN_P Typename IS comment_text
6487 {
6488 CommentStmt *n = makeNode(CommentStmt);
6489 n->objtype = OBJECT_DOMAIN;
6490 n->object = (Node *) $4;
6491 n->comment = $6;
6492 $$ = (Node *) n;
6493 }
6494 | COMMENT ON AGGREGATE aggregate_with_argtypes IS comment_text
6495 {
6496 CommentStmt *n = makeNode(CommentStmt);
6497 n->objtype = OBJECT_AGGREGATE;
6498 n->object = (Node *) $4;
6499 n->comment = $6;
6500 $$ = (Node *) n;
6501 }
6502 | COMMENT ON FUNCTION function_with_argtypes IS comment_text
6503 {
6504 CommentStmt *n = makeNode(CommentStmt);
6505 n->objtype = OBJECT_FUNCTION;
6506 n->object = (Node *) $4;
6507 n->comment = $6;
6508 $$ = (Node *) n;
6509 }
6510 | COMMENT ON OPERATOR operator_with_argtypes IS comment_text
6511 {
6512 CommentStmt *n = makeNode(CommentStmt);
6513 n->objtype = OBJECT_OPERATOR;
6514 n->object = (Node *) $4;
6515 n->comment = $6;
6516 $$ = (Node *) n;
6517 }
6518 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
6519 {
6520 CommentStmt *n = makeNode(CommentStmt);
6521 n->objtype = OBJECT_TABCONSTRAINT;
6522 n->object = (Node *) lappend($6, makeString($4));
6523 n->comment = $8;
6524 $$ = (Node *) n;
6525 }
6526 | COMMENT ON CONSTRAINT name ON DOMAIN_P any_name IS comment_text
6527 {
6528 CommentStmt *n = makeNode(CommentStmt);
6529 n->objtype = OBJECT_DOMCONSTRAINT;
6530 /*
6531 * should use Typename not any_name in the production, but
6532 * there's a shift/reduce conflict if we do that, so fix it
6533 * up here.
6534 */
6535 n->object = (Node *) list_make2(makeTypeNameFromNameList($7), makeString($4));
6536 n->comment = $9;
6537 $$ = (Node *) n;
6538 }
6539 | COMMENT ON POLICY name ON any_name IS comment_text
6540 {
6541 CommentStmt *n = makeNode(CommentStmt);
6542 n->objtype = OBJECT_POLICY;
6543 n->object = (Node *) lappend($6, makeString($4));
6544 n->comment = $8;
6545 $$ = (Node *) n;
6546 }
6547 | COMMENT ON PROCEDURE function_with_argtypes IS comment_text
6548 {
6549 CommentStmt *n = makeNode(CommentStmt);
6550 n->objtype = OBJECT_PROCEDURE;
6551 n->object = (Node *) $4;
6552 n->comment = $6;
6553 $$ = (Node *) n;
6554 }
6555 | COMMENT ON ROUTINE function_with_argtypes IS comment_text
6556 {
6557 CommentStmt *n = makeNode(CommentStmt);
6558 n->objtype = OBJECT_ROUTINE;
6559 n->object = (Node *) $4;
6560 n->comment = $6;
6561 $$ = (Node *) n;
6562 }
6563 | COMMENT ON RULE name ON any_name IS comment_text
6564 {
6565 CommentStmt *n = makeNode(CommentStmt);
6566 n->objtype = OBJECT_RULE;
6567 n->object = (Node *) lappend($6, makeString($4));
6568 n->comment = $8;
6569 $$ = (Node *) n;
6570 }
6571 | COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
6572 {
6573 CommentStmt *n = makeNode(CommentStmt);
6574 n->objtype = OBJECT_TRANSFORM;
6575 n->object = (Node *) list_make2($5, makeString($7));
6576 n->comment = $9;
6577 $$ = (Node *) n;
6578 }
6579 | COMMENT ON TRIGGER name ON any_name IS comment_text
6580 {
6581 CommentStmt *n = makeNode(CommentStmt);
6582 n->objtype = OBJECT_TRIGGER;
6583 n->object = (Node *) lappend($6, makeString($4));
6584 n->comment = $8;
6585 $$ = (Node *) n;
6586 }
6587 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
6588 {
6589 CommentStmt *n = makeNode(CommentStmt);
6590 n->objtype = OBJECT_OPCLASS;
6591 n->object = (Node *) lcons(makeString($7), $5);
6592 n->comment = $9;
6593 $$ = (Node *) n;
6594 }
6595 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
6596 {
6597 CommentStmt *n = makeNode(CommentStmt);
6598 n->objtype = OBJECT_OPFAMILY;
6599 n->object = (Node *) lcons(makeString($7), $5);
6600 n->comment = $9;
6601 $$ = (Node *) n;
6602 }
6603 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
6604 {
6605 CommentStmt *n = makeNode(CommentStmt);
6606 n->objtype = OBJECT_LARGEOBJECT;
6607 n->object = (Node *) $5;
6608 n->comment = $7;
6609 $$ = (Node *) n;
6610 }
6611 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
6612 {
6613 CommentStmt *n = makeNode(CommentStmt);
6614 n->objtype = OBJECT_CAST;
6615 n->object = (Node *) list_make2($5, $7);
6616 n->comment = $10;
6617 $$ = (Node *) n;
6618 }
6619 ;
6620
6621 /* object types taking any_name */
6622 comment_type_any_name:
6623 COLUMN { $$ = OBJECT_COLUMN; }
6624 | INDEX { $$ = OBJECT_INDEX; }
6625 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
6626 | STATISTICS { $$ = OBJECT_STATISTIC_EXT; }
6627 | TABLE { $$ = OBJECT_TABLE; }
6628 | VIEW { $$ = OBJECT_VIEW; }
6629 | MATERIALIZED VIEW { $$ = OBJECT_MATVIEW; }
6630 | COLLATION { $$ = OBJECT_COLLATION; }
6631 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
6632 | FOREIGN TABLE { $$ = OBJECT_FOREIGN_TABLE; }
6633 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
6634 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
6635 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
6636 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
6637 ;
6638
6639 /* object types taking name */
6640 comment_type_name:
6641 ACCESS METHOD { $$ = OBJECT_ACCESS_METHOD; }
6642 | DATABASE { $$ = OBJECT_DATABASE; }
6643 | EVENT TRIGGER { $$ = OBJECT_EVENT_TRIGGER; }
6644 | EXTENSION { $$ = OBJECT_EXTENSION; }
6645 | FOREIGN DATA_P WRAPPER { $$ = OBJECT_FDW; }
6646 | opt_procedural LANGUAGE { $$ = OBJECT_LANGUAGE; }
6647 | PUBLICATION { $$ = OBJECT_PUBLICATION; }
6648 | ROLE { $$ = OBJECT_ROLE; }
6649 | SCHEMA { $$ = OBJECT_SCHEMA; }
6650 | SERVER { $$ = OBJECT_FOREIGN_SERVER; }
6651 | SUBSCRIPTION { $$ = OBJECT_SUBSCRIPTION; }
6652 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
6653 ;
6654
6655 comment_text:
6656 Sconst { $$ = $1; }
6657 | NULL_P { $$ = NULL; }
6658 ;
6659
6660
6661 /*****************************************************************************
6662 *
6663 * SECURITY LABEL [FOR <provider>] ON <object> IS <label>
6664 *
6665 * As with COMMENT ON, <object> can refer to various types of database
6666 * objects (e.g. TABLE, COLUMN, etc.).
6667 *
6668 *****************************************************************************/
6669
6670 SecLabelStmt:
6671 SECURITY LABEL opt_provider ON security_label_type_any_name any_name
6672 IS security_label
6673 {
6674 SecLabelStmt *n = makeNode(SecLabelStmt);
6675 n->provider = $3;
6676 n->objtype = $5;
6677 n->object = (Node *) $6;
6678 n->label = $8;
6679 $$ = (Node *) n;
6680 }
6681 | SECURITY LABEL opt_provider ON security_label_type_name name
6682 IS security_label
6683 {
6684 SecLabelStmt *n = makeNode(SecLabelStmt);
6685 n->provider = $3;
6686 n->objtype = $5;
6687 n->object = (Node *) makeString($6);
6688 n->label = $8;
6689 $$ = (Node *) n;
6690 }
6691 | SECURITY LABEL opt_provider ON TYPE_P Typename
6692 IS security_label
6693 {
6694 SecLabelStmt *n = makeNode(SecLabelStmt);
6695 n->provider = $3;
6696 n->objtype = OBJECT_TYPE;
6697 n->object = (Node *) $6;
6698 n->label = $8;
6699 $$ = (Node *) n;
6700 }
6701 | SECURITY LABEL opt_provider ON DOMAIN_P Typename
6702 IS security_label
6703 {
6704 SecLabelStmt *n = makeNode(SecLabelStmt);
6705 n->provider = $3;
6706 n->objtype = OBJECT_DOMAIN;
6707 n->object = (Node *) $6;
6708 n->label = $8;
6709 $$ = (Node *) n;
6710 }
6711 | SECURITY LABEL opt_provider ON AGGREGATE aggregate_with_argtypes
6712 IS security_label
6713 {
6714 SecLabelStmt *n = makeNode(SecLabelStmt);
6715 n->provider = $3;
6716 n->objtype = OBJECT_AGGREGATE;
6717 n->object = (Node *) $6;
6718 n->label = $8;
6719 $$ = (Node *) n;
6720 }
6721 | SECURITY LABEL opt_provider ON FUNCTION function_with_argtypes
6722 IS security_label
6723 {
6724 SecLabelStmt *n = makeNode(SecLabelStmt);
6725 n->provider = $3;
6726 n->objtype = OBJECT_FUNCTION;
6727 n->object = (Node *) $6;
6728 n->label = $8;
6729 $$ = (Node *) n;
6730 }
6731 | SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
6732 IS security_label
6733 {
6734 SecLabelStmt *n = makeNode(SecLabelStmt);
6735 n->provider = $3;
6736 n->objtype = OBJECT_LARGEOBJECT;
6737 n->object = (Node *) $7;
6738 n->label = $9;
6739 $$ = (Node *) n;
6740 }
6741 | SECURITY LABEL opt_provider ON PROCEDURE function_with_argtypes
6742 IS security_label
6743 {
6744 SecLabelStmt *n = makeNode(SecLabelStmt);
6745 n->provider = $3;
6746 n->objtype = OBJECT_PROCEDURE;
6747 n->object = (Node *) $6;
6748 n->label = $8;
6749 $$ = (Node *) n;
6750 }
6751 | SECURITY LABEL opt_provider ON ROUTINE function_with_argtypes
6752 IS security_label
6753 {
6754 SecLabelStmt *n = makeNode(SecLabelStmt);
6755 n->provider = $3;
6756 n->objtype = OBJECT_ROUTINE;
6757 n->object = (Node *) $6;
6758 n->label = $8;
6759 $$ = (Node *) n;
6760 }
6761 ;
6762
6763 opt_provider: FOR NonReservedWord_or_Sconst { $$ = $2; }
6764 | /* empty */ { $$ = NULL; }
6765 ;
6766
6767 /* object types taking any_name */
6768 security_label_type_any_name:
6769 COLUMN { $$ = OBJECT_COLUMN; }
6770 | FOREIGN TABLE { $$ = OBJECT_FOREIGN_TABLE; }
6771 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
6772 | TABLE { $$ = OBJECT_TABLE; }
6773 | VIEW { $$ = OBJECT_VIEW; }
6774 | MATERIALIZED VIEW { $$ = OBJECT_MATVIEW; }
6775 ;
6776
6777 /* object types taking name */
6778 security_label_type_name:
6779 DATABASE { $$ = OBJECT_DATABASE; }
6780 | EVENT TRIGGER { $$ = OBJECT_EVENT_TRIGGER; }
6781 | opt_procedural LANGUAGE { $$ = OBJECT_LANGUAGE; }
6782 | PUBLICATION { $$ = OBJECT_PUBLICATION; }
6783 | ROLE { $$ = OBJECT_ROLE; }
6784 | SCHEMA { $$ = OBJECT_SCHEMA; }
6785 | SUBSCRIPTION { $$ = OBJECT_SUBSCRIPTION; }
6786 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
6787 ;
6788
6789 security_label: Sconst { $$ = $1; }
6790 | NULL_P { $$ = NULL; }
6791 ;
6792
6793 /*****************************************************************************
6794 *
6795 * QUERY:
6796 * fetch/move
6797 *
6798 *****************************************************************************/
6799
6800 FetchStmt: FETCH fetch_args
6801 {
6802 FetchStmt *n = (FetchStmt *) $2;
6803 n->ismove = false;
6804 $$ = (Node *)n;
6805 }
6806 | MOVE fetch_args
6807 {
6808 FetchStmt *n = (FetchStmt *) $2;
6809 n->ismove = true;
6810 $$ = (Node *)n;
6811 }
6812 ;
6813
6814 fetch_args: cursor_name
6815 {
6816 FetchStmt *n = makeNode(FetchStmt);
6817 n->portalname = $1;
6818 n->direction = FETCH_FORWARD;
6819 n->howMany = 1;
6820 $$ = (Node *)n;
6821 }
6822 | from_in cursor_name
6823 {
6824 FetchStmt *n = makeNode(FetchStmt);
6825 n->portalname = $2;
6826 n->direction = FETCH_FORWARD;
6827 n->howMany = 1;
6828 $$ = (Node *)n;
6829 }
6830 | NEXT opt_from_in cursor_name
6831 {
6832 FetchStmt *n = makeNode(FetchStmt);
6833 n->portalname = $3;
6834 n->direction = FETCH_FORWARD;
6835 n->howMany = 1;
6836 $$ = (Node *)n;
6837 }
6838 | PRIOR opt_from_in cursor_name
6839 {
6840 FetchStmt *n = makeNode(FetchStmt);
6841 n->portalname = $3;
6842 n->direction = FETCH_BACKWARD;
6843 n->howMany = 1;
6844 $$ = (Node *)n;
6845 }
6846 | FIRST_P opt_from_in cursor_name
6847 {
6848 FetchStmt *n = makeNode(FetchStmt);
6849 n->portalname = $3;
6850 n->direction = FETCH_ABSOLUTE;
6851 n->howMany = 1;
6852 $$ = (Node *)n;
6853 }
6854 | LAST_P opt_from_in cursor_name
6855 {
6856 FetchStmt *n = makeNode(FetchStmt);
6857 n->portalname = $3;
6858 n->direction = FETCH_ABSOLUTE;
6859 n->howMany = -1;
6860 $$ = (Node *)n;
6861 }
6862 | ABSOLUTE_P SignedIconst opt_from_in cursor_name
6863 {
6864 FetchStmt *n = makeNode(FetchStmt);
6865 n->portalname = $4;
6866 n->direction = FETCH_ABSOLUTE;
6867 n->howMany = $2;
6868 $$ = (Node *)n;
6869 }
6870 | RELATIVE_P SignedIconst opt_from_in cursor_name
6871 {
6872 FetchStmt *n = makeNode(FetchStmt);
6873 n->portalname = $4;
6874 n->direction = FETCH_RELATIVE;
6875 n->howMany = $2;
6876 $$ = (Node *)n;
6877 }
6878 | SignedIconst opt_from_in cursor_name
6879 {
6880 FetchStmt *n = makeNode(FetchStmt);
6881 n->portalname = $3;
6882 n->direction = FETCH_FORWARD;
6883 n->howMany = $1;
6884 $$ = (Node *)n;
6885 }
6886 | ALL opt_from_in cursor_name
6887 {
6888 FetchStmt *n = makeNode(FetchStmt);
6889 n->portalname = $3;
6890 n->direction = FETCH_FORWARD;
6891 n->howMany = FETCH_ALL;
6892 $$ = (Node *)n;
6893 }
6894 | FORWARD opt_from_in cursor_name
6895 {
6896 FetchStmt *n = makeNode(FetchStmt);
6897 n->portalname = $3;
6898 n->direction = FETCH_FORWARD;
6899 n->howMany = 1;
6900 $$ = (Node *)n;
6901 }
6902 | FORWARD SignedIconst opt_from_in cursor_name
6903 {
6904 FetchStmt *n = makeNode(FetchStmt);
6905 n->portalname = $4;
6906 n->direction = FETCH_FORWARD;
6907 n->howMany = $2;
6908 $$ = (Node *)n;
6909 }
6910 | FORWARD ALL opt_from_in cursor_name
6911 {
6912 FetchStmt *n = makeNode(FetchStmt);
6913 n->portalname = $4;
6914 n->direction = FETCH_FORWARD;
6915 n->howMany = FETCH_ALL;
6916 $$ = (Node *)n;
6917 }
6918 | BACKWARD opt_from_in cursor_name
6919 {
6920 FetchStmt *n = makeNode(FetchStmt);
6921 n->portalname = $3;
6922 n->direction = FETCH_BACKWARD;
6923 n->howMany = 1;
6924 $$ = (Node *)n;
6925 }
6926 | BACKWARD SignedIconst opt_from_in cursor_name
6927 {
6928 FetchStmt *n = makeNode(FetchStmt);
6929 n->portalname = $4;
6930 n->direction = FETCH_BACKWARD;
6931 n->howMany = $2;
6932 $$ = (Node *)n;
6933 }
6934 | BACKWARD ALL opt_from_in cursor_name
6935 {
6936 FetchStmt *n = makeNode(FetchStmt);
6937 n->portalname = $4;
6938 n->direction = FETCH_BACKWARD;
6939 n->howMany = FETCH_ALL;
6940 $$ = (Node *)n;
6941 }
6942 ;
6943
6944 from_in: FROM {}
6945 | IN_P {}
6946 ;
6947
6948 opt_from_in: from_in {}
6949 | /* EMPTY */ {}
6950 ;
6951
6952
6953 /*****************************************************************************
6954 *
6955 * GRANT and REVOKE statements
6956 *
6957 *****************************************************************************/
6958
6959 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
6960 opt_grant_grant_option
6961 {
6962 GrantStmt *n = makeNode(GrantStmt);
6963 n->is_grant = true;
6964 n->privileges = $2;
6965 n->targtype = ($4)->targtype;
6966 n->objtype = ($4)->objtype;
6967 n->objects = ($4)->objs;
6968 n->grantees = $6;
6969 n->grant_option = $7;
6970 $$ = (Node*)n;
6971 }
6972 ;
6973
6974 RevokeStmt:
6975 REVOKE privileges ON privilege_target
6976 FROM grantee_list opt_drop_behavior
6977 {
6978 GrantStmt *n = makeNode(GrantStmt);
6979 n->is_grant = false;
6980 n->grant_option = false;
6981 n->privileges = $2;
6982 n->targtype = ($4)->targtype;
6983 n->objtype = ($4)->objtype;
6984 n->objects = ($4)->objs;
6985 n->grantees = $6;
6986 n->behavior = $7;
6987 $$ = (Node *)n;
6988 }
6989 | REVOKE GRANT OPTION FOR privileges ON privilege_target
6990 FROM grantee_list opt_drop_behavior
6991 {
6992 GrantStmt *n = makeNode(GrantStmt);
6993 n->is_grant = false;
6994 n->grant_option = true;
6995 n->privileges = $5;
6996 n->targtype = ($7)->targtype;
6997 n->objtype = ($7)->objtype;
6998 n->objects = ($7)->objs;
6999 n->grantees = $9;
7000 n->behavior = $10;
7001 $$ = (Node *)n;
7002 }
7003 ;
7004
7005
7006 /*
7007 * Privilege names are represented as strings; the validity of the privilege
7008 * names gets checked at execution. This is a bit annoying but we have little
7009 * choice because of the syntactic conflict with lists of role names in
7010 * GRANT/REVOKE. What's more, we have to call out in the "privilege"
7011 * production any reserved keywords that need to be usable as privilege names.
7012 */
7013
7014 /* either ALL [PRIVILEGES] or a list of individual privileges */
7015 privileges: privilege_list
7016 { $$ = $1; }
7017 | ALL
7018 { $$ = NIL; }
7019 | ALL PRIVILEGES
7020 { $$ = NIL; }
7021 | ALL '(' columnList ')'
7022 {
7023 AccessPriv *n = makeNode(AccessPriv);
7024 n->priv_name = NULL;
7025 n->cols = $3;
7026 $$ = list_make1(n);
7027 }
7028 | ALL PRIVILEGES '(' columnList ')'
7029 {
7030 AccessPriv *n = makeNode(AccessPriv);
7031 n->priv_name = NULL;
7032 n->cols = $4;
7033 $$ = list_make1(n);
7034 }
7035 ;
7036
7037 privilege_list: privilege { $$ = list_make1($1); }
7038 | privilege_list ',' privilege { $$ = lappend($1, $3); }
7039 ;
7040
7041 privilege: SELECT opt_column_list
7042 {
7043 AccessPriv *n = makeNode(AccessPriv);
7044 n->priv_name = pstrdup($1);
7045 n->cols = $2;
7046 $$ = n;
7047 }
7048 | REFERENCES opt_column_list
7049 {
7050 AccessPriv *n = makeNode(AccessPriv);
7051 n->priv_name = pstrdup($1);
7052 n->cols = $2;
7053 $$ = n;
7054 }
7055 | CREATE opt_column_list
7056 {
7057 AccessPriv *n = makeNode(AccessPriv);
7058 n->priv_name = pstrdup($1);
7059 n->cols = $2;
7060 $$ = n;
7061 }
7062 | ColId opt_column_list
7063 {
7064 AccessPriv *n = makeNode(AccessPriv);
7065 n->priv_name = $1;
7066 n->cols = $2;
7067 $$ = n;
7068 }
7069 ;
7070
7071
7072 /* Don't bother trying to fold the first two rules into one using
7073 * opt_table. You're going to get conflicts.
7074 */
7075 privilege_target:
7076 qualified_name_list
7077 {
7078 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7079 n->targtype = ACL_TARGET_OBJECT;
7080 n->objtype = OBJECT_TABLE;
7081 n->objs = $1;
7082 $$ = n;
7083 }
7084 | TABLE qualified_name_list
7085 {
7086 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7087 n->targtype = ACL_TARGET_OBJECT;
7088 n->objtype = OBJECT_TABLE;
7089 n->objs = $2;
7090 $$ = n;
7091 }
7092 | SEQUENCE qualified_name_list
7093 {
7094 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7095 n->targtype = ACL_TARGET_OBJECT;
7096 n->objtype = OBJECT_SEQUENCE;
7097 n->objs = $2;
7098 $$ = n;
7099 }
7100 | FOREIGN DATA_P WRAPPER name_list
7101 {
7102 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7103 n->targtype = ACL_TARGET_OBJECT;
7104 n->objtype = OBJECT_FDW;
7105 n->objs = $4;
7106 $$ = n;
7107 }
7108 | FOREIGN SERVER name_list
7109 {
7110 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7111 n->targtype = ACL_TARGET_OBJECT;
7112 n->objtype = OBJECT_FOREIGN_SERVER;
7113 n->objs = $3;
7114 $$ = n;
7115 }
7116 | FUNCTION function_with_argtypes_list
7117 {
7118 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7119 n->targtype = ACL_TARGET_OBJECT;
7120 n->objtype = OBJECT_FUNCTION;
7121 n->objs = $2;
7122 $$ = n;
7123 }
7124 | PROCEDURE function_with_argtypes_list
7125 {
7126 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7127 n->targtype = ACL_TARGET_OBJECT;
7128 n->objtype = OBJECT_PROCEDURE;
7129 n->objs = $2;
7130 $$ = n;
7131 }
7132 | ROUTINE function_with_argtypes_list
7133 {
7134 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7135 n->targtype = ACL_TARGET_OBJECT;
7136 n->objtype = OBJECT_ROUTINE;
7137 n->objs = $2;
7138 $$ = n;
7139 }
7140 | DATABASE name_list
7141 {
7142 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7143 n->targtype = ACL_TARGET_OBJECT;
7144 n->objtype = OBJECT_DATABASE;
7145 n->objs = $2;
7146 $$ = n;
7147 }
7148 | DOMAIN_P any_name_list
7149 {
7150 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7151 n->targtype = ACL_TARGET_OBJECT;
7152 n->objtype = OBJECT_DOMAIN;
7153 n->objs = $2;
7154 $$ = n;
7155 }
7156 | LANGUAGE name_list
7157 {
7158 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7159 n->targtype = ACL_TARGET_OBJECT;
7160 n->objtype = OBJECT_LANGUAGE;
7161 n->objs = $2;
7162 $$ = n;
7163 }
7164 | LARGE_P OBJECT_P NumericOnly_list
7165 {
7166 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7167 n->targtype = ACL_TARGET_OBJECT;
7168 n->objtype = OBJECT_LARGEOBJECT;
7169 n->objs = $3;
7170 $$ = n;
7171 }
7172 | SCHEMA name_list
7173 {
7174 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7175 n->targtype = ACL_TARGET_OBJECT;
7176 n->objtype = OBJECT_SCHEMA;
7177 n->objs = $2;
7178 $$ = n;
7179 }
7180 | TABLESPACE name_list
7181 {
7182 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7183 n->targtype = ACL_TARGET_OBJECT;
7184 n->objtype = OBJECT_TABLESPACE;
7185 n->objs = $2;
7186 $$ = n;
7187 }
7188 | TYPE_P any_name_list
7189 {
7190 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7191 n->targtype = ACL_TARGET_OBJECT;
7192 n->objtype = OBJECT_TYPE;
7193 n->objs = $2;
7194 $$ = n;
7195 }
7196 | ALL TABLES IN_P SCHEMA name_list
7197 {
7198 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7199 n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7200 n->objtype = OBJECT_TABLE;
7201 n->objs = $5;
7202 $$ = n;
7203 }
7204 | ALL SEQUENCES IN_P SCHEMA name_list
7205 {
7206 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7207 n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7208 n->objtype = OBJECT_SEQUENCE;
7209 n->objs = $5;
7210 $$ = n;
7211 }
7212 | ALL FUNCTIONS IN_P SCHEMA name_list
7213 {
7214 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7215 n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7216 n->objtype = OBJECT_FUNCTION;
7217 n->objs = $5;
7218 $$ = n;
7219 }
7220 | ALL PROCEDURES IN_P SCHEMA name_list
7221 {
7222 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7223 n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7224 n->objtype = OBJECT_PROCEDURE;
7225 n->objs = $5;
7226 $$ = n;
7227 }
7228 | ALL ROUTINES IN_P SCHEMA name_list
7229 {
7230 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
7231 n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
7232 n->objtype = OBJECT_ROUTINE;
7233 n->objs = $5;
7234 $$ = n;
7235 }
7236 ;
7237
7238
7239 grantee_list:
7240 grantee { $$ = list_make1($1); }
7241 | grantee_list ',' grantee { $$ = lappend($1, $3); }
7242 ;
7243
7244 grantee:
7245 RoleSpec { $$ = $1; }
7246 | GROUP_P RoleSpec { $$ = $2; }
7247 ;
7248
7249
7250 opt_grant_grant_option:
7251 WITH GRANT OPTION { $$ = true; }
7252 | /*EMPTY*/ { $$ = false; }
7253 ;
7254
7255 /*****************************************************************************
7256 *
7257 * GRANT and REVOKE ROLE statements
7258 *
7259 *****************************************************************************/
7260
7261 GrantRoleStmt:
7262 GRANT privilege_list TO role_list opt_grant_admin_option opt_granted_by
7263 {
7264 GrantRoleStmt *n = makeNode(GrantRoleStmt);
7265 n->is_grant = true;
7266 n->granted_roles = $2;
7267 n->grantee_roles = $4;
7268 n->admin_opt = $5;
7269 n->grantor = $6;
7270 $$ = (Node*)n;
7271 }
7272 ;
7273
7274 RevokeRoleStmt:
7275 REVOKE privilege_list FROM role_list opt_granted_by opt_drop_behavior
7276 {
7277 GrantRoleStmt *n = makeNode(GrantRoleStmt);
7278 n->is_grant = false;
7279 n->admin_opt = false;
7280 n->granted_roles = $2;
7281 n->grantee_roles = $4;
7282 n->behavior = $6;
7283 $$ = (Node*)n;
7284 }
7285 | REVOKE ADMIN OPTION FOR privilege_list FROM role_list opt_granted_by opt_drop_behavior
7286 {
7287 GrantRoleStmt *n = makeNode(GrantRoleStmt);
7288 n->is_grant = false;
7289 n->admin_opt = true;
7290 n->granted_roles = $5;
7291 n->grantee_roles = $7;
7292 n->behavior = $9;
7293 $$ = (Node*)n;
7294 }
7295 ;
7296
7297 opt_grant_admin_option: WITH ADMIN OPTION { $$ = true; }
7298 | /*EMPTY*/ { $$ = false; }
7299 ;
7300
7301 opt_granted_by: GRANTED BY RoleSpec { $$ = $3; }
7302 | /*EMPTY*/ { $$ = NULL; }
7303 ;
7304
7305 /*****************************************************************************
7306 *
7307 * ALTER DEFAULT PRIVILEGES statement
7308 *
7309 *****************************************************************************/
7310
7311 AlterDefaultPrivilegesStmt:
7312 ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
7313 {
7314 AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
7315 n->options = $4;
7316 n->action = (GrantStmt *) $5;
7317 $$ = (Node*)n;
7318 }
7319 ;
7320
7321 DefACLOptionList:
7322 DefACLOptionList DefACLOption { $$ = lappend($1, $2); }
7323 | /* EMPTY */ { $$ = NIL; }
7324 ;
7325
7326 DefACLOption:
7327 IN_P SCHEMA name_list
7328 {
7329 $$ = makeDefElem("schemas", (Node *)$3, @1);
7330 }
7331 | FOR ROLE role_list
7332 {
7333 $$ = makeDefElem("roles", (Node *)$3, @1);
7334 }
7335 | FOR USER role_list
7336 {
7337 $$ = makeDefElem("roles", (Node *)$3, @1);
7338 }
7339 ;
7340
7341 /*
7342 * This should match GRANT/REVOKE, except that individual target objects
7343 * are not mentioned and we only allow a subset of object types.
7344 */
7345 DefACLAction:
7346 GRANT privileges ON defacl_privilege_target TO grantee_list
7347 opt_grant_grant_option
7348 {
7349 GrantStmt *n = makeNode(GrantStmt);
7350 n->is_grant = true;
7351 n->privileges = $2;
7352 n->targtype = ACL_TARGET_DEFAULTS;
7353 n->objtype = $4;
7354 n->objects = NIL;
7355 n->grantees = $6;
7356 n->grant_option = $7;
7357 $$ = (Node*)n;
7358 }
7359 | REVOKE privileges ON defacl_privilege_target
7360 FROM grantee_list opt_drop_behavior
7361 {
7362 GrantStmt *n = makeNode(GrantStmt);
7363 n->is_grant = false;
7364 n->grant_option = false;
7365 n->privileges = $2;
7366 n->targtype = ACL_TARGET_DEFAULTS;
7367 n->objtype = $4;
7368 n->objects = NIL;
7369 n->grantees = $6;
7370 n->behavior = $7;
7371 $$ = (Node *)n;
7372 }
7373 | REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
7374 FROM grantee_list opt_drop_behavior
7375 {
7376 GrantStmt *n = makeNode(GrantStmt);
7377 n->is_grant = false;
7378 n->grant_option = true;
7379 n->privileges = $5;
7380 n->targtype = ACL_TARGET_DEFAULTS;
7381 n->objtype = $7;
7382 n->objects = NIL;
7383 n->grantees = $9;
7384 n->behavior = $10;
7385 $$ = (Node *)n;
7386 }
7387 ;
7388
7389 defacl_privilege_target:
7390 TABLES { $$ = OBJECT_TABLE; }
7391 | FUNCTIONS { $$ = OBJECT_FUNCTION; }
7392 | ROUTINES { $$ = OBJECT_FUNCTION; }
7393 | SEQUENCES { $$ = OBJECT_SEQUENCE; }
7394 | TYPES_P { $$ = OBJECT_TYPE; }
7395 | SCHEMAS { $$ = OBJECT_SCHEMA; }
7396 ;
7397
7398
7399 /*****************************************************************************
7400 *
7401 * QUERY: CREATE INDEX
7402 *
7403 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
7404 * willing to make TABLESPACE a fully reserved word.
7405 *****************************************************************************/
7406
7407 IndexStmt: CREATE opt_unique INDEX opt_concurrently opt_index_name
7408 ON relation_expr access_method_clause '(' index_params ')'
7409 opt_include opt_reloptions OptTableSpace where_clause
7410 {
7411 IndexStmt *n = makeNode(IndexStmt);
7412 n->unique = $2;
7413 n->concurrent = $4;
7414 n->idxname = $5;
7415 n->relation = $7;
7416 n->accessMethod = $8;
7417 n->indexParams = $10;
7418 n->indexIncludingParams = $12;
7419 n->options = $13;
7420 n->tableSpace = $14;
7421 n->whereClause = $15;
7422 n->excludeOpNames = NIL;
7423 n->idxcomment = NULL;
7424 n->indexOid = InvalidOid;
7425 n->oldNode = InvalidOid;
7426 n->primary = false;
7427 n->isconstraint = false;
7428 n->deferrable = false;
7429 n->initdeferred = false;
7430 n->transformed = false;
7431 n->if_not_exists = false;
7432 n->reset_default_tblspc = false;
7433 $$ = (Node *)n;
7434 }
7435 | CREATE opt_unique INDEX opt_concurrently IF_P NOT EXISTS index_name
7436 ON relation_expr access_method_clause '(' index_params ')'
7437 opt_include opt_reloptions OptTableSpace where_clause
7438 {
7439 IndexStmt *n = makeNode(IndexStmt);
7440 n->unique = $2;
7441 n->concurrent = $4;
7442 n->idxname = $8;
7443 n->relation = $10;
7444 n->accessMethod = $11;
7445 n->indexParams = $13;
7446 n->indexIncludingParams = $15;
7447 n->options = $16;
7448 n->tableSpace = $17;
7449 n->whereClause = $18;
7450 n->excludeOpNames = NIL;
7451 n->idxcomment = NULL;
7452 n->indexOid = InvalidOid;
7453 n->oldNode = InvalidOid;
7454 n->primary = false;
7455 n->isconstraint = false;
7456 n->deferrable = false;
7457 n->initdeferred = false;
7458 n->transformed = false;
7459 n->if_not_exists = true;
7460 n->reset_default_tblspc = false;
7461 $$ = (Node *)n;
7462 }
7463 ;
7464
7465 opt_unique:
7466 UNIQUE { $$ = true; }
7467 | /*EMPTY*/ { $$ = false; }
7468 ;
7469
7470 opt_concurrently:
7471 CONCURRENTLY { $$ = true; }
7472 | /*EMPTY*/ { $$ = false; }
7473 ;
7474
7475 opt_index_name:
7476 index_name { $$ = $1; }
7477 | /*EMPTY*/ { $$ = NULL; }
7478 ;
7479
7480 access_method_clause:
7481 USING access_method { $$ = $2; }
7482 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
7483 ;
7484
7485 index_params: index_elem { $$ = list_make1($1); }
7486 | index_params ',' index_elem { $$ = lappend($1, $3); }
7487 ;
7488
7489 /*
7490 * Index attributes can be either simple column references, or arbitrary
7491 * expressions in parens. For backwards-compatibility reasons, we allow
7492 * an expression that's just a function call to be written without parens.
7493 */
7494 index_elem: ColId opt_collate opt_class opt_asc_desc opt_nulls_order
7495 {
7496 $$ = makeNode(IndexElem);
7497 $$->name = $1;
7498 $$->expr = NULL;
7499 $$->indexcolname = NULL;
7500 $$->collation = $2;
7501 $$->opclass = $3;
7502 $$->ordering = $4;
7503 $$->nulls_ordering = $5;
7504 }
7505 | func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order
7506 {
7507 $$ = makeNode(IndexElem);
7508 $$->name = NULL;
7509 $$->expr = $1;
7510 $$->indexcolname = NULL;
7511 $$->collation = $2;
7512 $$->opclass = $3;
7513 $$->ordering = $4;
7514 $$->nulls_ordering = $5;
7515 }
7516 | '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order
7517 {
7518 $$ = makeNode(IndexElem);
7519 $$->name = NULL;
7520 $$->expr = $2;
7521 $$->indexcolname = NULL;
7522 $$->collation = $4;
7523 $$->opclass = $5;
7524 $$->ordering = $6;
7525 $$->nulls_ordering = $7;
7526 }
7527 ;
7528
7529 opt_include: INCLUDE '(' index_including_params ')' { $$ = $3; }
7530 | /* EMPTY */ { $$ = NIL; }
7531 ;
7532
7533 index_including_params: index_elem { $$ = list_make1($1); }
7534 | index_including_params ',' index_elem { $$ = lappend($1, $3); }
7535 ;
7536
7537 opt_collate: COLLATE any_name { $$ = $2; }
7538 | /*EMPTY*/ { $$ = NIL; }
7539 ;
7540
7541 opt_class: any_name { $$ = $1; }
7542 | /*EMPTY*/ { $$ = NIL; }
7543 ;
7544
7545 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
7546 | DESC { $$ = SORTBY_DESC; }
7547 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
7548 ;
7549
7550 opt_nulls_order: NULLS_LA FIRST_P { $$ = SORTBY_NULLS_FIRST; }
7551 | NULLS_LA LAST_P { $$ = SORTBY_NULLS_LAST; }
7552 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
7553 ;
7554
7555
7556 /*****************************************************************************
7557 *
7558 * QUERY:
7559 * create [or replace] function <fname>
7560 * [(<type-1> { , <type-n>})]
7561 * returns <type-r>
7562 * as <filename or code in language as appropriate>
7563 * language <lang> [with parameters]
7564 *
7565 *****************************************************************************/
7566
7567 CreateFunctionStmt:
7568 CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7569 RETURNS func_return createfunc_opt_list
7570 {
7571 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7572 n->is_procedure = false;
7573 n->replace = $2;
7574 n->funcname = $4;
7575 n->parameters = $5;
7576 n->returnType = $7;
7577 n->options = $8;
7578 $$ = (Node *)n;
7579 }
7580 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7581 RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list
7582 {
7583 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7584 n->is_procedure = false;
7585 n->replace = $2;
7586 n->funcname = $4;
7587 n->parameters = mergeTableFuncParameters($5, $9);
7588 n->returnType = TableFuncTypeName($9);
7589 n->returnType->location = @7;
7590 n->options = $11;
7591 $$ = (Node *)n;
7592 }
7593 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
7594 createfunc_opt_list
7595 {
7596 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7597 n->is_procedure = false;
7598 n->replace = $2;
7599 n->funcname = $4;
7600 n->parameters = $5;
7601 n->returnType = NULL;
7602 n->options = $6;
7603 $$ = (Node *)n;
7604 }
7605 | CREATE opt_or_replace PROCEDURE func_name func_args_with_defaults
7606 createfunc_opt_list
7607 {
7608 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
7609 n->is_procedure = true;
7610 n->replace = $2;
7611 n->funcname = $4;
7612 n->parameters = $5;
7613 n->returnType = NULL;
7614 n->options = $6;
7615 $$ = (Node *)n;
7616 }
7617 ;
7618
7619 opt_or_replace:
7620 OR REPLACE { $$ = true; }
7621 | /*EMPTY*/ { $$ = false; }
7622 ;
7623
7624 func_args: '(' func_args_list ')' { $$ = $2; }
7625 | '(' ')' { $$ = NIL; }
7626 ;
7627
7628 func_args_list:
7629 func_arg { $$ = list_make1($1); }
7630 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
7631 ;
7632
7633 function_with_argtypes_list:
7634 function_with_argtypes { $$ = list_make1($1); }
7635 | function_with_argtypes_list ',' function_with_argtypes
7636 { $$ = lappend($1, $3); }
7637 ;
7638
7639 function_with_argtypes:
7640 func_name func_args
7641 {
7642 ObjectWithArgs *n = makeNode(ObjectWithArgs);
7643 n->objname = $1;
7644 n->objargs = extractArgTypes($2);
7645 $$ = n;
7646 }
7647 /*
7648 * Because of reduce/reduce conflicts, we can't use func_name
7649 * below, but we can write it out the long way, which actually
7650 * allows more cases.
7651 */
7652 | type_func_name_keyword
7653 {
7654 ObjectWithArgs *n = makeNode(ObjectWithArgs);
7655 n->objname = list_make1(makeString(pstrdup($1)));
7656 n->args_unspecified = true;
7657 $$ = n;
7658 }
7659 | ColId
7660 {
7661 ObjectWithArgs *n = makeNode(ObjectWithArgs);
7662 n->objname = list_make1(makeString($1));
7663 n->args_unspecified = true;
7664 $$ = n;
7665 }
7666 | ColId indirection
7667 {
7668 ObjectWithArgs *n = makeNode(ObjectWithArgs);
7669 n->objname = check_func_name(lcons(makeString($1), $2),
7670 yyscanner);
7671 n->args_unspecified = true;
7672 $$ = n;
7673 }
7674 ;
7675
7676 /*
7677 * func_args_with_defaults is separate because we only want to accept
7678 * defaults in CREATE FUNCTION, not in ALTER etc.
7679 */
7680 func_args_with_defaults:
7681 '(' func_args_with_defaults_list ')' { $$ = $2; }
7682 | '(' ')' { $$ = NIL; }
7683 ;
7684
7685 func_args_with_defaults_list:
7686 func_arg_with_default { $$ = list_make1($1); }
7687 | func_args_with_defaults_list ',' func_arg_with_default
7688 { $$ = lappend($1, $3); }
7689 ;
7690
7691 /*
7692 * The style with arg_class first is SQL99 standard, but Oracle puts
7693 * param_name first; accept both since it's likely people will try both
7694 * anyway. Don't bother trying to save productions by letting arg_class
7695 * have an empty alternative ... you'll get shift/reduce conflicts.
7696 *
7697 * We can catch over-specified arguments here if we want to,
7698 * but for now better to silently swallow typmod, etc.
7699 * - thomas 2000-03-22
7700 */
7701 func_arg:
7702 arg_class param_name func_type
7703 {
7704 FunctionParameter *n = makeNode(FunctionParameter);
7705 n->name = $2;
7706 n->argType = $3;
7707 n->mode = $1;
7708 n->defexpr = NULL;
7709 $$ = n;
7710 }
7711 | param_name arg_class func_type
7712 {
7713 FunctionParameter *n = makeNode(FunctionParameter);
7714 n->name = $1;
7715 n->argType = $3;
7716 n->mode = $2;
7717 n->defexpr = NULL;
7718 $$ = n;
7719 }
7720 | param_name func_type
7721 {
7722 FunctionParameter *n = makeNode(FunctionParameter);
7723 n->name = $1;
7724 n->argType = $2;
7725 n->mode = FUNC_PARAM_IN;
7726 n->defexpr = NULL;
7727 $$ = n;
7728 }
7729 | arg_class func_type
7730 {
7731 FunctionParameter *n = makeNode(FunctionParameter);
7732 n->name = NULL;
7733 n->argType = $2;
7734 n->mode = $1;
7735 n->defexpr = NULL;
7736 $$ = n;
7737 }
7738 | func_type
7739 {
7740 FunctionParameter *n = makeNode(FunctionParameter);
7741 n->name = NULL;
7742 n->argType = $1;
7743 n->mode = FUNC_PARAM_IN;
7744 n->defexpr = NULL;
7745 $$ = n;
7746 }
7747 ;
7748
7749 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
7750 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
7751 | OUT_P { $$ = FUNC_PARAM_OUT; }
7752 | INOUT { $$ = FUNC_PARAM_INOUT; }
7753 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
7754 | VARIADIC { $$ = FUNC_PARAM_VARIADIC; }
7755 ;
7756
7757 /*
7758 * Ideally param_name should be ColId, but that causes too many conflicts.
7759 */
7760 param_name: type_function_name
7761 ;
7762
7763 func_return:
7764 func_type
7765 {
7766 /* We can catch over-specified results here if we want to,
7767 * but for now better to silently swallow typmod, etc.
7768 * - thomas 2000-03-22
7769 */
7770 $$ = $1;
7771 }
7772 ;
7773
7774 /*
7775 * We would like to make the %TYPE productions here be ColId attrs etc,
7776 * but that causes reduce/reduce conflicts. type_function_name
7777 * is next best choice.
7778 */
7779 func_type: Typename { $$ = $1; }
7780 | type_function_name attrs '%' TYPE_P
7781 {
7782 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7783 $$->pct_type = true;
7784 $$->location = @1;
7785 }
7786 | SETOF type_function_name attrs '%' TYPE_P
7787 {
7788 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
7789 $$->pct_type = true;
7790 $$->setof = true;
7791 $$->location = @2;
7792 }
7793 ;
7794
7795 func_arg_with_default:
7796 func_arg
7797 {
7798 $$ = $1;
7799 }
7800 | func_arg DEFAULT a_expr
7801 {
7802 $$ = $1;
7803 $$->defexpr = $3;
7804 }
7805 | func_arg '=' a_expr
7806 {
7807 $$ = $1;
7808 $$->defexpr = $3;
7809 }
7810 ;
7811
7812 /* Aggregate args can be most things that function args can be */
7813 aggr_arg: func_arg
7814 {
7815 if (!($1->mode == FUNC_PARAM_IN ||
7816 $1->mode == FUNC_PARAM_VARIADIC))
7817 ereport(ERROR,
7818 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7819 errmsg("aggregates cannot have output arguments"),
7820 parser_errposition(@1)));
7821 $$ = $1;
7822 }
7823 ;
7824
7825 /*
7826 * The SQL standard offers no guidance on how to declare aggregate argument
7827 * lists, since it doesn't have CREATE AGGREGATE etc. We accept these cases:
7828 *
7829 * (*) - normal agg with no args
7830 * (aggr_arg,...) - normal agg with args
7831 * (ORDER BY aggr_arg,...) - ordered-set agg with no direct args
7832 * (aggr_arg,... ORDER BY aggr_arg,...) - ordered-set agg with direct args
7833 *
7834 * The zero-argument case is spelled with '*' for consistency with COUNT(*).
7835 *
7836 * An additional restriction is that if the direct-args list ends in a
7837 * VARIADIC item, the ordered-args list must contain exactly one item that
7838 * is also VARIADIC with the same type. This allows us to collapse the two
7839 * VARIADIC items into one, which is necessary to represent the aggregate in
7840 * pg_proc. We check this at the grammar stage so that we can return a list
7841 * in which the second VARIADIC item is already discarded, avoiding extra work
7842 * in cases such as DROP AGGREGATE.
7843 *
7844 * The return value of this production is a two-element list, in which the
7845 * first item is a sublist of FunctionParameter nodes (with any duplicate
7846 * VARIADIC item already dropped, as per above) and the second is an integer
7847 * Value node, containing -1 if there was no ORDER BY and otherwise the number
7848 * of argument declarations before the ORDER BY. (If this number is equal
7849 * to the first sublist's length, then we dropped a duplicate VARIADIC item.)
7850 * This representation is passed as-is to CREATE AGGREGATE; for operations
7851 * on existing aggregates, we can just apply extractArgTypes to the first
7852 * sublist.
7853 */
7854 aggr_args: '(' '*' ')'
7855 {
7856 $$ = list_make2(NIL, makeInteger(-1));
7857 }
7858 | '(' aggr_args_list ')'
7859 {
7860 $$ = list_make2($2, makeInteger(-1));
7861 }
7862 | '(' ORDER BY aggr_args_list ')'
7863 {
7864 $$ = list_make2($4, makeInteger(0));
7865 }
7866 | '(' aggr_args_list ORDER BY aggr_args_list ')'
7867 {
7868 /* this is the only case requiring consistency checking */
7869 $$ = makeOrderedSetArgs($2, $5, yyscanner);
7870 }
7871 ;
7872
7873 aggr_args_list:
7874 aggr_arg { $$ = list_make1($1); }
7875 | aggr_args_list ',' aggr_arg { $$ = lappend($1, $3); }
7876 ;
7877
7878 aggregate_with_argtypes:
7879 func_name aggr_args
7880 {
7881 ObjectWithArgs *n = makeNode(ObjectWithArgs);
7882 n->objname = $1;
7883 n->objargs = extractAggrArgTypes($2);
7884 $$ = n;
7885 }
7886 ;
7887
7888 aggregate_with_argtypes_list:
7889 aggregate_with_argtypes { $$ = list_make1($1); }
7890 | aggregate_with_argtypes_list ',' aggregate_with_argtypes
7891 { $$ = lappend($1, $3); }
7892 ;
7893
7894 createfunc_opt_list:
7895 /* Must be at least one to prevent conflict */
7896 createfunc_opt_item { $$ = list_make1($1); }
7897 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
7898 ;
7899
7900 /*
7901 * Options common to both CREATE FUNCTION and ALTER FUNCTION
7902 */
7903 common_func_opt_item:
7904 CALLED ON NULL_P INPUT_P
7905 {
7906 $$ = makeDefElem("strict", (Node *)makeInteger(false), @1);
7907 }
7908 | RETURNS NULL_P ON NULL_P INPUT_P
7909 {
7910 $$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7911 }
7912 | STRICT_P
7913 {
7914 $$ = makeDefElem("strict", (Node *)makeInteger(true), @1);
7915 }
7916 | IMMUTABLE
7917 {
7918 $$ = makeDefElem("volatility", (Node *)makeString("immutable"), @1);
7919 }
7920 | STABLE
7921 {
7922 $$ = makeDefElem("volatility", (Node *)makeString("stable"), @1);
7923 }
7924 | VOLATILE
7925 {
7926 $$ = makeDefElem("volatility", (Node *)makeString("volatile"), @1);
7927 }
7928 | EXTERNAL SECURITY DEFINER
7929 {
7930 $$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7931 }
7932 | EXTERNAL SECURITY INVOKER
7933 {
7934 $$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7935 }
7936 | SECURITY DEFINER
7937 {
7938 $$ = makeDefElem("security", (Node *)makeInteger(true), @1);
7939 }
7940 | SECURITY INVOKER
7941 {
7942 $$ = makeDefElem("security", (Node *)makeInteger(false), @1);
7943 }
7944 | LEAKPROOF
7945 {
7946 $$ = makeDefElem("leakproof", (Node *)makeInteger(true), @1);
7947 }
7948 | NOT LEAKPROOF
7949 {
7950 $$ = makeDefElem("leakproof", (Node *)makeInteger(false), @1);
7951 }
7952 | COST NumericOnly
7953 {
7954 $$ = makeDefElem("cost", (Node *)$2, @1);
7955 }
7956 | ROWS NumericOnly
7957 {
7958 $$ = makeDefElem("rows", (Node *)$2, @1);
7959 }
7960 | SUPPORT any_name
7961 {
7962 $$ = makeDefElem("support", (Node *)$2, @1);
7963 }
7964 | FunctionSetResetClause
7965 {
7966 /* we abuse the normal content of a DefElem here */
7967 $$ = makeDefElem("set", (Node *)$1, @1);
7968 }
7969 | PARALLEL ColId
7970 {
7971 $$ = makeDefElem("parallel", (Node *)makeString($2), @1);
7972 }
7973 ;
7974
7975 createfunc_opt_item:
7976 AS func_as
7977 {
7978 $$ = makeDefElem("as", (Node *)$2, @1);
7979 }
7980 | LANGUAGE NonReservedWord_or_Sconst
7981 {
7982 $$ = makeDefElem("language", (Node *)makeString($2), @1);
7983 }
7984 | TRANSFORM transform_type_list
7985 {
7986 $$ = makeDefElem("transform", (Node *)$2, @1);
7987 }
7988 | WINDOW
7989 {
7990 $$ = makeDefElem("window", (Node *)makeInteger(true), @1);
7991 }
7992 | common_func_opt_item
7993 {
7994 $$ = $1;
7995 }
7996 ;
7997
7998 func_as: Sconst { $$ = list_make1(makeString($1)); }
7999 | Sconst ',' Sconst
8000 {
8001 $$ = list_make2(makeString($1), makeString($3));
8002 }
8003 ;
8004
8005 transform_type_list:
8006 FOR TYPE_P Typename { $$ = list_make1($3); }
8007 | transform_type_list ',' FOR TYPE_P Typename { $$ = lappend($1, $5); }
8008 ;
8009
8010 opt_definition:
8011 WITH definition { $$ = $2; }
8012 | /*EMPTY*/ { $$ = NIL; }
8013 ;
8014
8015 table_func_column: param_name func_type
8016 {
8017 FunctionParameter *n = makeNode(FunctionParameter);
8018 n->name = $1;
8019 n->argType = $2;
8020 n->mode = FUNC_PARAM_TABLE;
8021 n->defexpr = NULL;
8022 $$ = n;
8023 }
8024 ;
8025
8026 table_func_column_list:
8027 table_func_column
8028 {
8029 $$ = list_make1($1);
8030 }
8031 | table_func_column_list ',' table_func_column
8032 {
8033 $$ = lappend($1, $3);
8034 }
8035 ;
8036
8037 /*****************************************************************************
8038 * ALTER FUNCTION / ALTER PROCEDURE / ALTER ROUTINE
8039 *
8040 * RENAME and OWNER subcommands are already provided by the generic
8041 * ALTER infrastructure, here we just specify alterations that can
8042 * only be applied to functions.
8043 *
8044 *****************************************************************************/
8045 AlterFunctionStmt:
8046 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
8047 {
8048 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8049 n->objtype = OBJECT_FUNCTION;
8050 n->func = $3;
8051 n->actions = $4;
8052 $$ = (Node *) n;
8053 }
8054 | ALTER PROCEDURE function_with_argtypes alterfunc_opt_list opt_restrict
8055 {
8056 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8057 n->objtype = OBJECT_PROCEDURE;
8058 n->func = $3;
8059 n->actions = $4;
8060 $$ = (Node *) n;
8061 }
8062 | ALTER ROUTINE function_with_argtypes alterfunc_opt_list opt_restrict
8063 {
8064 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
8065 n->objtype = OBJECT_ROUTINE;
8066 n->func = $3;
8067 n->actions = $4;
8068 $$ = (Node *) n;
8069 }
8070 ;
8071
8072 alterfunc_opt_list:
8073 /* At least one option must be specified */
8074 common_func_opt_item { $$ = list_make1($1); }
8075 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
8076 ;
8077
8078 /* Ignored, merely for SQL compliance */
8079 opt_restrict:
8080 RESTRICT
8081 | /* EMPTY */
8082 ;
8083
8084
8085 /*****************************************************************************
8086 *
8087 * QUERY:
8088 *
8089 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8090 * DROP PROCEDURE procname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8091 * DROP ROUTINE routname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
8092 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
8093 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
8094 *
8095 *****************************************************************************/
8096
8097 RemoveFuncStmt:
8098 DROP FUNCTION function_with_argtypes_list opt_drop_behavior
8099 {
8100 DropStmt *n = makeNode(DropStmt);
8101 n->removeType = OBJECT_FUNCTION;
8102 n->objects = $3;
8103 n->behavior = $4;
8104 n->missing_ok = false;
8105 n->concurrent = false;
8106 $$ = (Node *)n;
8107 }
8108 | DROP FUNCTION IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8109 {
8110 DropStmt *n = makeNode(DropStmt);
8111 n->removeType = OBJECT_FUNCTION;
8112 n->objects = $5;
8113 n->behavior = $6;
8114 n->missing_ok = true;
8115 n->concurrent = false;
8116 $$ = (Node *)n;
8117 }
8118 | DROP PROCEDURE function_with_argtypes_list opt_drop_behavior
8119 {
8120 DropStmt *n = makeNode(DropStmt);
8121 n->removeType = OBJECT_PROCEDURE;
8122 n->objects = $3;
8123 n->behavior = $4;
8124 n->missing_ok = false;
8125 n->concurrent = false;
8126 $$ = (Node *)n;
8127 }
8128 | DROP PROCEDURE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8129 {
8130 DropStmt *n = makeNode(DropStmt);
8131 n->removeType = OBJECT_PROCEDURE;
8132 n->objects = $5;
8133 n->behavior = $6;
8134 n->missing_ok = true;
8135 n->concurrent = false;
8136 $$ = (Node *)n;
8137 }
8138 | DROP ROUTINE function_with_argtypes_list opt_drop_behavior
8139 {
8140 DropStmt *n = makeNode(DropStmt);
8141 n->removeType = OBJECT_ROUTINE;
8142 n->objects = $3;
8143 n->behavior = $4;
8144 n->missing_ok = false;
8145 n->concurrent = false;
8146 $$ = (Node *)n;
8147 }
8148 | DROP ROUTINE IF_P EXISTS function_with_argtypes_list opt_drop_behavior
8149 {
8150 DropStmt *n = makeNode(DropStmt);
8151 n->removeType = OBJECT_ROUTINE;
8152 n->objects = $5;
8153 n->behavior = $6;
8154 n->missing_ok = true;
8155 n->concurrent = false;
8156 $$ = (Node *)n;
8157 }
8158 ;
8159
8160 RemoveAggrStmt:
8161 DROP AGGREGATE aggregate_with_argtypes_list opt_drop_behavior
8162 {
8163 DropStmt *n = makeNode(DropStmt);
8164 n->removeType = OBJECT_AGGREGATE;
8165 n->objects = $3;
8166 n->behavior = $4;
8167 n->missing_ok = false;
8168 n->concurrent = false;
8169 $$ = (Node *)n;
8170 }
8171 | DROP AGGREGATE IF_P EXISTS aggregate_with_argtypes_list opt_drop_behavior
8172 {
8173 DropStmt *n = makeNode(DropStmt);
8174 n->removeType = OBJECT_AGGREGATE;
8175 n->objects = $5;
8176 n->behavior = $6;
8177 n->missing_ok = true;
8178 n->concurrent = false;
8179 $$ = (Node *)n;
8180 }
8181 ;
8182
8183 RemoveOperStmt:
8184 DROP OPERATOR operator_with_argtypes_list opt_drop_behavior
8185 {
8186 DropStmt *n = makeNode(DropStmt);
8187 n->removeType = OBJECT_OPERATOR;
8188 n->objects = $3;
8189 n->behavior = $4;
8190 n->missing_ok = false;
8191 n->concurrent = false;
8192 $$ = (Node *)n;
8193 }
8194 | DROP OPERATOR IF_P EXISTS operator_with_argtypes_list opt_drop_behavior
8195 {
8196 DropStmt *n = makeNode(DropStmt);
8197 n->removeType = OBJECT_OPERATOR;
8198 n->objects = $5;
8199 n->behavior = $6;
8200 n->missing_ok = true;
8201 n->concurrent = false;
8202 $$ = (Node *)n;
8203 }
8204 ;
8205
8206 oper_argtypes:
8207 '(' Typename ')'
8208 {
8209 ereport(ERROR,
8210 (errcode(ERRCODE_SYNTAX_ERROR),
8211 errmsg("missing argument"),
8212 errhint("Use NONE to denote the missing argument of a unary operator."),
8213 parser_errposition(@3)));
8214 }
8215 | '(' Typename ',' Typename ')'
8216 { $$ = list_make2($2, $4); }
8217 | '(' NONE ',' Typename ')' /* left unary */
8218 { $$ = list_make2(NULL, $4); }
8219 | '(' Typename ',' NONE ')' /* right unary */
8220 { $$ = list_make2($2, NULL); }
8221 ;
8222
8223 any_operator:
8224 all_Op
8225 { $$ = list_make1(makeString($1)); }
8226 | ColId '.' any_operator
8227 { $$ = lcons(makeString($1), $3); }
8228 ;
8229
8230 operator_with_argtypes_list:
8231 operator_with_argtypes { $$ = list_make1($1); }
8232 | operator_with_argtypes_list ',' operator_with_argtypes
8233 { $$ = lappend($1, $3); }
8234 ;
8235
8236 operator_with_argtypes:
8237 any_operator oper_argtypes
8238 {
8239 ObjectWithArgs *n = makeNode(ObjectWithArgs);
8240 n->objname = $1;
8241 n->objargs = $2;
8242 $$ = n;
8243 }
8244 ;
8245
8246 /*****************************************************************************
8247 *
8248 * DO <anonymous code block> [ LANGUAGE language ]
8249 *
8250 * We use a DefElem list for future extensibility, and to allow flexibility
8251 * in the clause order.
8252 *
8253 *****************************************************************************/
8254
8255 DoStmt: DO dostmt_opt_list
8256 {
8257 DoStmt *n = makeNode(DoStmt);
8258 n->args = $2;
8259 $$ = (Node *)n;
8260 }
8261 ;
8262
8263 dostmt_opt_list:
8264 dostmt_opt_item { $$ = list_make1($1); }
8265 | dostmt_opt_list dostmt_opt_item { $$ = lappend($1, $2); }
8266 ;
8267
8268 dostmt_opt_item:
8269 Sconst
8270 {
8271 $$ = makeDefElem("as", (Node *)makeString($1), @1);
8272 }
8273 | LANGUAGE NonReservedWord_or_Sconst
8274 {
8275 $$ = makeDefElem("language", (Node *)makeString($2), @1);
8276 }
8277 ;
8278
8279 /*****************************************************************************
8280 *
8281 * CREATE CAST / DROP CAST
8282 *
8283 *****************************************************************************/
8284
8285 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
8286 WITH FUNCTION function_with_argtypes cast_context
8287 {
8288 CreateCastStmt *n = makeNode(CreateCastStmt);
8289 n->sourcetype = $4;
8290 n->targettype = $6;
8291 n->func = $10;
8292 n->context = (CoercionContext) $11;
8293 n->inout = false;
8294 $$ = (Node *)n;
8295 }
8296 | CREATE CAST '(' Typename AS Typename ')'
8297 WITHOUT FUNCTION cast_context
8298 {
8299 CreateCastStmt *n = makeNode(CreateCastStmt);
8300 n->sourcetype = $4;
8301 n->targettype = $6;
8302 n->func = NULL;
8303 n->context = (CoercionContext) $10;
8304 n->inout = false;
8305 $$ = (Node *)n;
8306 }
8307 | CREATE CAST '(' Typename AS Typename ')'
8308 WITH INOUT cast_context
8309 {
8310 CreateCastStmt *n = makeNode(CreateCastStmt);
8311 n->sourcetype = $4;
8312 n->targettype = $6;
8313 n->func = NULL;
8314 n->context = (CoercionContext) $10;
8315 n->inout = true;
8316 $$ = (Node *)n;
8317 }
8318 ;
8319
8320 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
8321 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
8322 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
8323 ;
8324
8325
8326 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
8327 {
8328 DropStmt *n = makeNode(DropStmt);
8329 n->removeType = OBJECT_CAST;
8330 n->objects = list_make1(list_make2($5, $7));
8331 n->behavior = $9;
8332 n->missing_ok = $3;
8333 n->concurrent = false;
8334 $$ = (Node *)n;
8335 }
8336 ;
8337
8338 opt_if_exists: IF_P EXISTS { $$ = true; }
8339 | /*EMPTY*/ { $$ = false; }
8340 ;
8341
8342
8343 /*****************************************************************************
8344 *
8345 * CREATE TRANSFORM / DROP TRANSFORM
8346 *
8347 *****************************************************************************/
8348
8349 CreateTransformStmt: CREATE opt_or_replace TRANSFORM FOR Typename LANGUAGE name '(' transform_element_list ')'
8350 {
8351 CreateTransformStmt *n = makeNode(CreateTransformStmt);
8352 n->replace = $2;
8353 n->type_name = $5;
8354 n->lang = $7;
8355 n->fromsql = linitial($9);
8356 n->tosql = lsecond($9);
8357 $$ = (Node *)n;
8358 }
8359 ;
8360
8361 transform_element_list: FROM SQL_P WITH FUNCTION function_with_argtypes ',' TO SQL_P WITH FUNCTION function_with_argtypes
8362 {
8363 $$ = list_make2($5, $11);
8364 }
8365 | TO SQL_P WITH FUNCTION function_with_argtypes ',' FROM SQL_P WITH FUNCTION function_with_argtypes
8366 {
8367 $$ = list_make2($11, $5);
8368 }
8369 | FROM SQL_P WITH FUNCTION function_with_argtypes
8370 {
8371 $$ = list_make2($5, NULL);
8372 }
8373 | TO SQL_P WITH FUNCTION function_with_argtypes
8374 {
8375 $$ = list_make2(NULL, $5);
8376 }
8377 ;
8378
8379
8380 DropTransformStmt: DROP TRANSFORM opt_if_exists FOR Typename LANGUAGE name opt_drop_behavior
8381 {
8382 DropStmt *n = makeNode(DropStmt);
8383 n->removeType = OBJECT_TRANSFORM;
8384 n->objects = list_make1(list_make2($5, makeString($7)));
8385 n->behavior = $8;
8386 n->missing_ok = $3;
8387 $$ = (Node *)n;
8388 }
8389 ;
8390
8391
8392 /*****************************************************************************
8393 *
8394 * QUERY:
8395 *
8396 * REINDEX [ (options) ] type [CONCURRENTLY] <name>
8397 *****************************************************************************/
8398
8399 ReindexStmt:
8400 REINDEX reindex_target_type opt_concurrently qualified_name
8401 {
8402 ReindexStmt *n = makeNode(ReindexStmt);
8403 n->kind = $2;
8404 n->concurrent = $3;
8405 n->relation = $4;
8406 n->name = NULL;
8407 n->options = 0;
8408 $$ = (Node *)n;
8409 }
8410 | REINDEX reindex_target_multitable opt_concurrently name
8411 {
8412 ReindexStmt *n = makeNode(ReindexStmt);
8413 n->kind = $2;
8414 n->concurrent = $3;
8415 n->name = $4;
8416 n->relation = NULL;
8417 n->options = 0;
8418 $$ = (Node *)n;
8419 }
8420 | REINDEX '(' reindex_option_list ')' reindex_target_type opt_concurrently qualified_name
8421 {
8422 ReindexStmt *n = makeNode(ReindexStmt);
8423 n->kind = $5;
8424 n->concurrent = $6;
8425 n->relation = $7;
8426 n->name = NULL;
8427 n->options = $3;
8428 $$ = (Node *)n;
8429 }
8430 | REINDEX '(' reindex_option_list ')' reindex_target_multitable opt_concurrently name
8431 {
8432 ReindexStmt *n = makeNode(ReindexStmt);
8433 n->kind = $5;
8434 n->concurrent = $6;
8435 n->name = $7;
8436 n->relation = NULL;
8437 n->options = $3;
8438 $$ = (Node *)n;
8439 }
8440 ;
8441 reindex_target_type:
8442 INDEX { $$ = REINDEX_OBJECT_INDEX; }
8443 | TABLE { $$ = REINDEX_OBJECT_TABLE; }
8444 ;
8445 reindex_target_multitable:
8446 SCHEMA { $$ = REINDEX_OBJECT_SCHEMA; }
8447 | SYSTEM_P { $$ = REINDEX_OBJECT_SYSTEM; }
8448 | DATABASE { $$ = REINDEX_OBJECT_DATABASE; }
8449 ;
8450 reindex_option_list:
8451 reindex_option_elem { $$ = $1; }
8452 | reindex_option_list ',' reindex_option_elem { $$ = $1 | $3; }
8453 ;
8454 reindex_option_elem:
8455 VERBOSE { $$ = REINDEXOPT_VERBOSE; }
8456 ;
8457
8458 /*****************************************************************************
8459 *
8460 * ALTER TABLESPACE
8461 *
8462 *****************************************************************************/
8463
8464 AlterTblSpcStmt:
8465 ALTER TABLESPACE name SET reloptions
8466 {
8467 AlterTableSpaceOptionsStmt *n =
8468 makeNode(AlterTableSpaceOptionsStmt);
8469 n->tablespacename = $3;
8470 n->options = $5;
8471 n->isReset = false;
8472 $$ = (Node *)n;
8473 }
8474 | ALTER TABLESPACE name RESET reloptions
8475 {
8476 AlterTableSpaceOptionsStmt *n =
8477 makeNode(AlterTableSpaceOptionsStmt);
8478 n->tablespacename = $3;
8479 n->options = $5;
8480 n->isReset = true;
8481 $$ = (Node *)n;
8482 }
8483 ;
8484
8485 /*****************************************************************************
8486 *
8487 * ALTER THING name RENAME TO newname
8488 *
8489 *****************************************************************************/
8490
8491 RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
8492 {
8493 RenameStmt *n = makeNode(RenameStmt);
8494 n->renameType = OBJECT_AGGREGATE;
8495 n->object = (Node *) $3;
8496 n->newname = $6;
8497 n->missing_ok = false;
8498 $$ = (Node *)n;
8499 }
8500 | ALTER COLLATION any_name RENAME TO name
8501 {
8502 RenameStmt *n = makeNode(RenameStmt);
8503 n->renameType = OBJECT_COLLATION;
8504 n->object = (Node *) $3;
8505 n->newname = $6;
8506 n->missing_ok = false;
8507 $$ = (Node *)n;
8508 }
8509 | ALTER CONVERSION_P any_name RENAME TO name
8510 {
8511 RenameStmt *n = makeNode(RenameStmt);
8512 n->renameType = OBJECT_CONVERSION;
8513 n->object = (Node *) $3;
8514 n->newname = $6;
8515 n->missing_ok = false;
8516 $$ = (Node *)n;
8517 }
8518 | ALTER DATABASE database_name RENAME TO database_name
8519 {
8520 RenameStmt *n = makeNode(RenameStmt);
8521 n->renameType = OBJECT_DATABASE;
8522 n->subname = $3;
8523 n->newname = $6;
8524 n->missing_ok = false;
8525 $$ = (Node *)n;
8526 }
8527 | ALTER DOMAIN_P any_name RENAME TO name
8528 {
8529 RenameStmt *n = makeNode(RenameStmt);
8530 n->renameType = OBJECT_DOMAIN;
8531 n->object = (Node *) $3;
8532 n->newname = $6;
8533 n->missing_ok = false;
8534 $$ = (Node *)n;
8535 }
8536 | ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
8537 {
8538 RenameStmt *n = makeNode(RenameStmt);
8539 n->renameType = OBJECT_DOMCONSTRAINT;
8540 n->object = (Node *) $3;
8541 n->subname = $6;
8542 n->newname = $8;
8543 $$ = (Node *)n;
8544 }
8545 | ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
8546 {
8547 RenameStmt *n = makeNode(RenameStmt);
8548 n->renameType = OBJECT_FDW;
8549 n->object = (Node *) makeString($5);
8550 n->newname = $8;
8551 n->missing_ok = false;
8552 $$ = (Node *)n;
8553 }
8554 | ALTER FUNCTION function_with_argtypes RENAME TO name
8555 {
8556 RenameStmt *n = makeNode(RenameStmt);
8557 n->renameType = OBJECT_FUNCTION;
8558 n->object = (Node *) $3;
8559 n->newname = $6;
8560 n->missing_ok = false;
8561 $$ = (Node *)n;
8562 }
8563 | ALTER GROUP_P RoleId RENAME TO RoleId
8564 {
8565 RenameStmt *n = makeNode(RenameStmt);
8566 n->renameType = OBJECT_ROLE;
8567 n->subname = $3;
8568 n->newname = $6;
8569 n->missing_ok = false;
8570 $$ = (Node *)n;
8571 }
8572 | ALTER opt_procedural LANGUAGE name RENAME TO name
8573 {
8574 RenameStmt *n = makeNode(RenameStmt);
8575 n->renameType = OBJECT_LANGUAGE;
8576 n->object = (Node *) makeString($4);
8577 n->newname = $7;
8578 n->missing_ok = false;
8579 $$ = (Node *)n;
8580 }
8581 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
8582 {
8583 RenameStmt *n = makeNode(RenameStmt);
8584 n->renameType = OBJECT_OPCLASS;
8585 n->object = (Node *) lcons(makeString($6), $4);
8586 n->newname = $9;
8587 n->missing_ok = false;
8588 $$ = (Node *)n;
8589 }
8590 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
8591 {
8592 RenameStmt *n = makeNode(RenameStmt);
8593 n->renameType = OBJECT_OPFAMILY;
8594 n->object = (Node *) lcons(makeString($6), $4);
8595 n->newname = $9;
8596 n->missing_ok = false;
8597 $$ = (Node *)n;
8598 }
8599 | ALTER POLICY name ON qualified_name RENAME TO name
8600 {
8601 RenameStmt *n = makeNode(RenameStmt);
8602 n->renameType = OBJECT_POLICY;
8603 n->relation = $5;
8604 n->subname = $3;
8605 n->newname = $8;
8606 n->missing_ok = false;
8607 $$ = (Node *)n;
8608 }
8609 | ALTER POLICY IF_P EXISTS name ON qualified_name RENAME TO name
8610 {
8611 RenameStmt *n = makeNode(RenameStmt);
8612 n->renameType = OBJECT_POLICY;
8613 n->relation = $7;
8614 n->subname = $5;
8615 n->newname = $10;
8616 n->missing_ok = true;
8617 $$ = (Node *)n;
8618 }
8619 | ALTER PROCEDURE function_with_argtypes RENAME TO name
8620 {
8621 RenameStmt *n = makeNode(RenameStmt);
8622 n->renameType = OBJECT_PROCEDURE;
8623 n->object = (Node *) $3;
8624 n->newname = $6;
8625 n->missing_ok = false;
8626 $$ = (Node *)n;
8627 }
8628 | ALTER PUBLICATION name RENAME TO name
8629 {
8630 RenameStmt *n = makeNode(RenameStmt);
8631 n->renameType = OBJECT_PUBLICATION;
8632 n->object = (Node *) makeString($3);
8633 n->newname = $6;
8634 n->missing_ok = false;
8635 $$ = (Node *)n;
8636 }
8637 | ALTER ROUTINE function_with_argtypes RENAME TO name
8638 {
8639 RenameStmt *n = makeNode(RenameStmt);
8640 n->renameType = OBJECT_ROUTINE;
8641 n->object = (Node *) $3;
8642 n->newname = $6;
8643 n->missing_ok = false;
8644 $$ = (Node *)n;
8645 }
8646 | ALTER SCHEMA name RENAME TO name
8647 {
8648 RenameStmt *n = makeNode(RenameStmt);
8649 n->renameType = OBJECT_SCHEMA;
8650 n->subname = $3;
8651 n->newname = $6;
8652 n->missing_ok = false;
8653 $$ = (Node *)n;
8654 }
8655 | ALTER SERVER name RENAME TO name
8656 {
8657 RenameStmt *n = makeNode(RenameStmt);
8658 n->renameType = OBJECT_FOREIGN_SERVER;
8659 n->object = (Node *) makeString($3);
8660 n->newname = $6;
8661 n->missing_ok = false;
8662 $$ = (Node *)n;
8663 }
8664 | ALTER SUBSCRIPTION name RENAME TO name
8665 {
8666 RenameStmt *n = makeNode(RenameStmt);
8667 n->renameType = OBJECT_SUBSCRIPTION;
8668 n->object = (Node *) makeString($3);
8669 n->newname = $6;
8670 n->missing_ok = false;
8671 $$ = (Node *)n;
8672 }
8673 | ALTER TABLE relation_expr RENAME TO name
8674 {
8675 RenameStmt *n = makeNode(RenameStmt);
8676 n->renameType = OBJECT_TABLE;
8677 n->relation = $3;
8678 n->subname = NULL;
8679 n->newname = $6;
8680 n->missing_ok = false;
8681 $$ = (Node *)n;
8682 }
8683 | ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
8684 {
8685 RenameStmt *n = makeNode(RenameStmt);
8686 n->renameType = OBJECT_TABLE;
8687 n->relation = $5;
8688 n->subname = NULL;
8689 n->newname = $8;
8690 n->missing_ok = true;
8691 $$ = (Node *)n;
8692 }
8693 | ALTER SEQUENCE qualified_name RENAME TO name
8694 {
8695 RenameStmt *n = makeNode(RenameStmt);
8696 n->renameType = OBJECT_SEQUENCE;
8697 n->relation = $3;
8698 n->subname = NULL;
8699 n->newname = $6;
8700 n->missing_ok = false;
8701 $$ = (Node *)n;
8702 }
8703 | ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
8704 {
8705 RenameStmt *n = makeNode(RenameStmt);
8706 n->renameType = OBJECT_SEQUENCE;
8707 n->relation = $5;
8708 n->subname = NULL;
8709 n->newname = $8;
8710 n->missing_ok = true;
8711 $$ = (Node *)n;
8712 }
8713 | ALTER VIEW qualified_name RENAME TO name
8714 {
8715 RenameStmt *n = makeNode(RenameStmt);
8716 n->renameType = OBJECT_VIEW;
8717 n->relation = $3;
8718 n->subname = NULL;
8719 n->newname = $6;
8720 n->missing_ok = false;
8721 $$ = (Node *)n;
8722 }
8723 | ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
8724 {
8725 RenameStmt *n = makeNode(RenameStmt);
8726 n->renameType = OBJECT_VIEW;
8727 n->relation = $5;
8728 n->subname = NULL;
8729 n->newname = $8;
8730 n->missing_ok = true;
8731 $$ = (Node *)n;
8732 }
8733 | ALTER MATERIALIZED VIEW qualified_name RENAME TO name
8734 {
8735 RenameStmt *n = makeNode(RenameStmt);
8736 n->renameType = OBJECT_MATVIEW;
8737 n->relation = $4;
8738 n->subname = NULL;
8739 n->newname = $7;
8740 n->missing_ok = false;
8741 $$ = (Node *)n;
8742 }
8743 | ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name
8744 {
8745 RenameStmt *n = makeNode(RenameStmt);
8746 n->renameType = OBJECT_MATVIEW;
8747 n->relation = $6;
8748 n->subname = NULL;
8749 n->newname = $9;
8750 n->missing_ok = true;
8751 $$ = (Node *)n;
8752 }
8753 | ALTER INDEX qualified_name RENAME TO name
8754 {
8755 RenameStmt *n = makeNode(RenameStmt);
8756 n->renameType = OBJECT_INDEX;
8757 n->relation = $3;
8758 n->subname = NULL;
8759 n->newname = $6;
8760 n->missing_ok = false;
8761 $$ = (Node *)n;
8762 }
8763 | ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
8764 {
8765 RenameStmt *n = makeNode(RenameStmt);
8766 n->renameType = OBJECT_INDEX;
8767 n->relation = $5;
8768 n->subname = NULL;
8769 n->newname = $8;
8770 n->missing_ok = true;
8771 $$ = (Node *)n;
8772 }
8773 | ALTER FOREIGN TABLE relation_expr RENAME TO name
8774 {
8775 RenameStmt *n = makeNode(RenameStmt);
8776 n->renameType = OBJECT_FOREIGN_TABLE;
8777 n->relation = $4;
8778 n->subname = NULL;
8779 n->newname = $7;
8780 n->missing_ok = false;
8781 $$ = (Node *)n;
8782 }
8783 | ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
8784 {
8785 RenameStmt *n = makeNode(RenameStmt);
8786 n->renameType = OBJECT_FOREIGN_TABLE;
8787 n->relation = $6;
8788 n->subname = NULL;
8789 n->newname = $9;
8790 n->missing_ok = true;
8791 $$ = (Node *)n;
8792 }
8793 | ALTER TABLE relation_expr RENAME opt_column name TO name
8794 {
8795 RenameStmt *n = makeNode(RenameStmt);
8796 n->renameType = OBJECT_COLUMN;
8797 n->relationType = OBJECT_TABLE;
8798 n->relation = $3;
8799 n->subname = $6;
8800 n->newname = $8;
8801 n->missing_ok = false;
8802 $$ = (Node *)n;
8803 }
8804 | ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8805 {
8806 RenameStmt *n = makeNode(RenameStmt);
8807 n->renameType = OBJECT_COLUMN;
8808 n->relationType = OBJECT_TABLE;
8809 n->relation = $5;
8810 n->subname = $8;
8811 n->newname = $10;
8812 n->missing_ok = true;
8813 $$ = (Node *)n;
8814 }
8815 | ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name
8816 {
8817 RenameStmt *n = makeNode(RenameStmt);
8818 n->renameType = OBJECT_COLUMN;
8819 n->relationType = OBJECT_MATVIEW;
8820 n->relation = $4;
8821 n->subname = $7;
8822 n->newname = $9;
8823 n->missing_ok = false;
8824 $$ = (Node *)n;
8825 }
8826 | ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name
8827 {
8828 RenameStmt *n = makeNode(RenameStmt);
8829 n->renameType = OBJECT_COLUMN;
8830 n->relationType = OBJECT_MATVIEW;
8831 n->relation = $6;
8832 n->subname = $9;
8833 n->newname = $11;
8834 n->missing_ok = true;
8835 $$ = (Node *)n;
8836 }
8837 | ALTER TABLE relation_expr RENAME CONSTRAINT name TO name
8838 {
8839 RenameStmt *n = makeNode(RenameStmt);
8840 n->renameType = OBJECT_TABCONSTRAINT;
8841 n->relation = $3;
8842 n->subname = $6;
8843 n->newname = $8;
8844 n->missing_ok = false;
8845 $$ = (Node *)n;
8846 }
8847 | ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
8848 {
8849 RenameStmt *n = makeNode(RenameStmt);
8850 n->renameType = OBJECT_TABCONSTRAINT;
8851 n->relation = $5;
8852 n->subname = $8;
8853 n->newname = $10;
8854 n->missing_ok = true;
8855 $$ = (Node *)n;
8856 }
8857 | ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
8858 {
8859 RenameStmt *n = makeNode(RenameStmt);
8860 n->renameType = OBJECT_COLUMN;
8861 n->relationType = OBJECT_FOREIGN_TABLE;
8862 n->relation = $4;
8863 n->subname = $7;
8864 n->newname = $9;
8865 n->missing_ok = false;
8866 $$ = (Node *)n;
8867 }
8868 | ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
8869 {
8870 RenameStmt *n = makeNode(RenameStmt);
8871 n->renameType = OBJECT_COLUMN;
8872 n->relationType = OBJECT_FOREIGN_TABLE;
8873 n->relation = $6;
8874 n->subname = $9;
8875 n->newname = $11;
8876 n->missing_ok = true;
8877 $$ = (Node *)n;
8878 }
8879 | ALTER RULE name ON qualified_name RENAME TO name
8880 {
8881 RenameStmt *n = makeNode(RenameStmt);
8882 n->renameType = OBJECT_RULE;
8883 n->relation = $5;
8884 n->subname = $3;
8885 n->newname = $8;
8886 n->missing_ok = false;
8887 $$ = (Node *)n;
8888 }
8889 | ALTER TRIGGER name ON qualified_name RENAME TO name
8890 {
8891 RenameStmt *n = makeNode(RenameStmt);
8892 n->renameType = OBJECT_TRIGGER;
8893 n->relation = $5;
8894 n->subname = $3;
8895 n->newname = $8;
8896 n->missing_ok = false;
8897 $$ = (Node *)n;
8898 }
8899 | ALTER EVENT TRIGGER name RENAME TO name
8900 {
8901 RenameStmt *n = makeNode(RenameStmt);
8902 n->renameType = OBJECT_EVENT_TRIGGER;
8903 n->object = (Node *) makeString($4);
8904 n->newname = $7;
8905 $$ = (Node *)n;
8906 }
8907 | ALTER ROLE RoleId RENAME TO RoleId
8908 {
8909 RenameStmt *n = makeNode(RenameStmt);
8910 n->renameType = OBJECT_ROLE;
8911 n->subname = $3;
8912 n->newname = $6;
8913 n->missing_ok = false;
8914 $$ = (Node *)n;
8915 }
8916 | ALTER USER RoleId RENAME TO RoleId
8917 {
8918 RenameStmt *n = makeNode(RenameStmt);
8919 n->renameType = OBJECT_ROLE;
8920 n->subname = $3;
8921 n->newname = $6;
8922 n->missing_ok = false;
8923 $$ = (Node *)n;
8924 }
8925 | ALTER TABLESPACE name RENAME TO name
8926 {
8927 RenameStmt *n = makeNode(RenameStmt);
8928 n->renameType = OBJECT_TABLESPACE;
8929 n->subname = $3;
8930 n->newname = $6;
8931 n->missing_ok = false;
8932 $$ = (Node *)n;
8933 }
8934 | ALTER STATISTICS any_name RENAME TO name
8935 {
8936 RenameStmt *n = makeNode(RenameStmt);
8937 n->renameType = OBJECT_STATISTIC_EXT;
8938 n->object = (Node *) $3;
8939 n->newname = $6;
8940 n->missing_ok = false;
8941 $$ = (Node *)n;
8942 }
8943 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
8944 {
8945 RenameStmt *n = makeNode(RenameStmt);
8946 n->renameType = OBJECT_TSPARSER;
8947 n->object = (Node *) $5;
8948 n->newname = $8;
8949 n->missing_ok = false;
8950 $$ = (Node *)n;
8951 }
8952 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
8953 {
8954 RenameStmt *n = makeNode(RenameStmt);
8955 n->renameType = OBJECT_TSDICTIONARY;
8956 n->object = (Node *) $5;
8957 n->newname = $8;
8958 n->missing_ok = false;
8959 $$ = (Node *)n;
8960 }
8961 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
8962 {
8963 RenameStmt *n = makeNode(RenameStmt);
8964 n->renameType = OBJECT_TSTEMPLATE;
8965 n->object = (Node *) $5;
8966 n->newname = $8;
8967 n->missing_ok = false;
8968 $$ = (Node *)n;
8969 }
8970 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
8971 {
8972 RenameStmt *n = makeNode(RenameStmt);
8973 n->renameType = OBJECT_TSCONFIGURATION;
8974 n->object = (Node *) $5;
8975 n->newname = $8;
8976 n->missing_ok = false;
8977 $$ = (Node *)n;
8978 }
8979 | ALTER TYPE_P any_name RENAME TO name
8980 {
8981 RenameStmt *n = makeNode(RenameStmt);
8982 n->renameType = OBJECT_TYPE;
8983 n->object = (Node *) $3;
8984 n->newname = $6;
8985 n->missing_ok = false;
8986 $$ = (Node *)n;
8987 }
8988 | ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
8989 {
8990 RenameStmt *n = makeNode(RenameStmt);
8991 n->renameType = OBJECT_ATTRIBUTE;
8992 n->relationType = OBJECT_TYPE;
8993 n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
8994 n->subname = $6;
8995 n->newname = $8;
8996 n->behavior = $9;
8997 n->missing_ok = false;
8998 $$ = (Node *)n;
8999 }
9000 ;
9001
9002 opt_column: COLUMN { $$ = COLUMN; }
9003 | /*EMPTY*/ { $$ = 0; }
9004 ;
9005
9006 opt_set_data: SET DATA_P { $$ = 1; }
9007 | /*EMPTY*/ { $$ = 0; }
9008 ;
9009
9010 /*****************************************************************************
9011 *
9012 * ALTER THING name DEPENDS ON EXTENSION name
9013 *
9014 *****************************************************************************/
9015
9016 AlterObjectDependsStmt:
9017 ALTER FUNCTION function_with_argtypes DEPENDS ON EXTENSION name
9018 {
9019 AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9020 n->objectType = OBJECT_FUNCTION;
9021 n->object = (Node *) $3;
9022 n->extname = makeString($7);
9023 $$ = (Node *)n;
9024 }
9025 | ALTER PROCEDURE function_with_argtypes DEPENDS ON EXTENSION name
9026 {
9027 AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9028 n->objectType = OBJECT_PROCEDURE;
9029 n->object = (Node *) $3;
9030 n->extname = makeString($7);
9031 $$ = (Node *)n;
9032 }
9033 | ALTER ROUTINE function_with_argtypes DEPENDS ON EXTENSION name
9034 {
9035 AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9036 n->objectType = OBJECT_ROUTINE;
9037 n->object = (Node *) $3;
9038 n->extname = makeString($7);
9039 $$ = (Node *)n;
9040 }
9041 | ALTER TRIGGER name ON qualified_name DEPENDS ON EXTENSION name
9042 {
9043 AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9044 n->objectType = OBJECT_TRIGGER;
9045 n->relation = $5;
9046 n->object = (Node *) list_make1(makeString($3));
9047 n->extname = makeString($9);
9048 $$ = (Node *)n;
9049 }
9050 | ALTER MATERIALIZED VIEW qualified_name DEPENDS ON EXTENSION name
9051 {
9052 AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9053 n->objectType = OBJECT_MATVIEW;
9054 n->relation = $4;
9055 n->extname = makeString($8);
9056 $$ = (Node *)n;
9057 }
9058 | ALTER INDEX qualified_name DEPENDS ON EXTENSION name
9059 {
9060 AlterObjectDependsStmt *n = makeNode(AlterObjectDependsStmt);
9061 n->objectType = OBJECT_INDEX;
9062 n->relation = $3;
9063 n->extname = makeString($7);
9064 $$ = (Node *)n;
9065 }
9066 ;
9067
9068 /*****************************************************************************
9069 *
9070 * ALTER THING name SET SCHEMA name
9071 *
9072 *****************************************************************************/
9073
9074 AlterObjectSchemaStmt:
9075 ALTER AGGREGATE aggregate_with_argtypes SET SCHEMA name
9076 {
9077 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9078 n->objectType = OBJECT_AGGREGATE;
9079 n->object = (Node *) $3;
9080 n->newschema = $6;
9081 n->missing_ok = false;
9082 $$ = (Node *)n;
9083 }
9084 | ALTER COLLATION any_name SET SCHEMA name
9085 {
9086 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9087 n->objectType = OBJECT_COLLATION;
9088 n->object = (Node *) $3;
9089 n->newschema = $6;
9090 n->missing_ok = false;
9091 $$ = (Node *)n;
9092 }
9093 | ALTER CONVERSION_P any_name SET SCHEMA name
9094 {
9095 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9096 n->objectType = OBJECT_CONVERSION;
9097 n->object = (Node *) $3;
9098 n->newschema = $6;
9099 n->missing_ok = false;
9100 $$ = (Node *)n;
9101 }
9102 | ALTER DOMAIN_P any_name SET SCHEMA name
9103 {
9104 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9105 n->objectType = OBJECT_DOMAIN;
9106 n->object = (Node *) $3;
9107 n->newschema = $6;
9108 n->missing_ok = false;
9109 $$ = (Node *)n;
9110 }
9111 | ALTER EXTENSION name SET SCHEMA name
9112 {
9113 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9114 n->objectType = OBJECT_EXTENSION;
9115 n->object = (Node *) makeString($3);
9116 n->newschema = $6;
9117 n->missing_ok = false;
9118 $$ = (Node *)n;
9119 }
9120 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
9121 {
9122 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9123 n->objectType = OBJECT_FUNCTION;
9124 n->object = (Node *) $3;
9125 n->newschema = $6;
9126 n->missing_ok = false;
9127 $$ = (Node *)n;
9128 }
9129 | ALTER OPERATOR operator_with_argtypes SET SCHEMA name
9130 {
9131 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9132 n->objectType = OBJECT_OPERATOR;
9133 n->object = (Node *) $3;
9134 n->newschema = $6;
9135 n->missing_ok = false;
9136 $$ = (Node *)n;
9137 }
9138 | ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
9139 {
9140 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9141 n->objectType = OBJECT_OPCLASS;
9142 n->object = (Node *) lcons(makeString($6), $4);
9143 n->newschema = $9;
9144 n->missing_ok = false;
9145 $$ = (Node *)n;
9146 }
9147 | ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
9148 {
9149 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9150 n->objectType = OBJECT_OPFAMILY;
9151 n->object = (Node *) lcons(makeString($6), $4);
9152 n->newschema = $9;
9153 n->missing_ok = false;
9154 $$ = (Node *)n;
9155 }
9156 | ALTER PROCEDURE function_with_argtypes SET SCHEMA name
9157 {
9158 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9159 n->objectType = OBJECT_PROCEDURE;
9160 n->object = (Node *) $3;
9161 n->newschema = $6;
9162 n->missing_ok = false;
9163 $$ = (Node *)n;
9164 }
9165 | ALTER ROUTINE function_with_argtypes SET SCHEMA name
9166 {
9167 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9168 n->objectType = OBJECT_ROUTINE;
9169 n->object = (Node *) $3;
9170 n->newschema = $6;
9171 n->missing_ok = false;
9172 $$ = (Node *)n;
9173 }
9174 | ALTER TABLE relation_expr SET SCHEMA name
9175 {
9176 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9177 n->objectType = OBJECT_TABLE;
9178 n->relation = $3;
9179 n->newschema = $6;
9180 n->missing_ok = false;
9181 $$ = (Node *)n;
9182 }
9183 | ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
9184 {
9185 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9186 n->objectType = OBJECT_TABLE;
9187 n->relation = $5;
9188 n->newschema = $8;
9189 n->missing_ok = true;
9190 $$ = (Node *)n;
9191 }
9192 | ALTER STATISTICS any_name SET SCHEMA name
9193 {
9194 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9195 n->objectType = OBJECT_STATISTIC_EXT;
9196 n->object = (Node *) $3;
9197 n->newschema = $6;
9198 n->missing_ok = false;
9199 $$ = (Node *)n;
9200 }
9201 | ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
9202 {
9203 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9204 n->objectType = OBJECT_TSPARSER;
9205 n->object = (Node *) $5;
9206 n->newschema = $8;
9207 n->missing_ok = false;
9208 $$ = (Node *)n;
9209 }
9210 | ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
9211 {
9212 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9213 n->objectType = OBJECT_TSDICTIONARY;
9214 n->object = (Node *) $5;
9215 n->newschema = $8;
9216 n->missing_ok = false;
9217 $$ = (Node *)n;
9218 }
9219 | ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
9220 {
9221 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9222 n->objectType = OBJECT_TSTEMPLATE;
9223 n->object = (Node *) $5;
9224 n->newschema = $8;
9225 n->missing_ok = false;
9226 $$ = (Node *)n;
9227 }
9228 | ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
9229 {
9230 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9231 n->objectType = OBJECT_TSCONFIGURATION;
9232 n->object = (Node *) $5;
9233 n->newschema = $8;
9234 n->missing_ok = false;
9235 $$ = (Node *)n;
9236 }
9237 | ALTER SEQUENCE qualified_name SET SCHEMA name
9238 {
9239 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9240 n->objectType = OBJECT_SEQUENCE;
9241 n->relation = $3;
9242 n->newschema = $6;
9243 n->missing_ok = false;
9244 $$ = (Node *)n;
9245 }
9246 | ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
9247 {
9248 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9249 n->objectType = OBJECT_SEQUENCE;
9250 n->relation = $5;
9251 n->newschema = $8;
9252 n->missing_ok = true;
9253 $$ = (Node *)n;
9254 }
9255 | ALTER VIEW qualified_name SET SCHEMA name
9256 {
9257 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9258 n->objectType = OBJECT_VIEW;
9259 n->relation = $3;
9260 n->newschema = $6;
9261 n->missing_ok = false;
9262 $$ = (Node *)n;
9263 }
9264 | ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
9265 {
9266 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9267 n->objectType = OBJECT_VIEW;
9268 n->relation = $5;
9269 n->newschema = $8;
9270 n->missing_ok = true;
9271 $$ = (Node *)n;
9272 }
9273 | ALTER MATERIALIZED VIEW qualified_name SET SCHEMA name
9274 {
9275 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9276 n->objectType = OBJECT_MATVIEW;
9277 n->relation = $4;
9278 n->newschema = $7;
9279 n->missing_ok = false;
9280 $$ = (Node *)n;
9281 }
9282 | ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name SET SCHEMA name
9283 {
9284 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9285 n->objectType = OBJECT_MATVIEW;
9286 n->relation = $6;
9287 n->newschema = $9;
9288 n->missing_ok = true;
9289 $$ = (Node *)n;
9290 }
9291 | ALTER FOREIGN TABLE relation_expr SET SCHEMA name
9292 {
9293 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9294 n->objectType = OBJECT_FOREIGN_TABLE;
9295 n->relation = $4;
9296 n->newschema = $7;
9297 n->missing_ok = false;
9298 $$ = (Node *)n;
9299 }
9300 | ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
9301 {
9302 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9303 n->objectType = OBJECT_FOREIGN_TABLE;
9304 n->relation = $6;
9305 n->newschema = $9;
9306 n->missing_ok = true;
9307 $$ = (Node *)n;
9308 }
9309 | ALTER TYPE_P any_name SET SCHEMA name
9310 {
9311 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
9312 n->objectType = OBJECT_TYPE;
9313 n->object = (Node *) $3;
9314 n->newschema = $6;
9315 n->missing_ok = false;
9316 $$ = (Node *)n;
9317 }
9318 ;
9319
9320 /*****************************************************************************
9321 *
9322 * ALTER OPERATOR name SET define
9323 *
9324 *****************************************************************************/
9325
9326 AlterOperatorStmt:
9327 ALTER OPERATOR operator_with_argtypes SET '(' operator_def_list ')'
9328 {
9329 AlterOperatorStmt *n = makeNode(AlterOperatorStmt);
9330 n->opername = $3;
9331 n->options = $6;
9332 $$ = (Node *)n;
9333 }
9334 ;
9335
9336 operator_def_list: operator_def_elem { $$ = list_make1($1); }
9337 | operator_def_list ',' operator_def_elem { $$ = lappend($1, $3); }
9338 ;
9339
9340 operator_def_elem: ColLabel '=' NONE
9341 { $$ = makeDefElem($1, NULL, @1); }
9342 | ColLabel '=' operator_def_arg
9343 { $$ = makeDefElem($1, (Node *) $3, @1); }
9344 ;
9345
9346 /* must be similar enough to def_arg to avoid reduce/reduce conflicts */
9347 operator_def_arg:
9348 func_type { $$ = (Node *)$1; }
9349 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
9350 | qual_all_Op { $$ = (Node *)$1; }
9351 | NumericOnly { $$ = (Node *)$1; }
9352 | Sconst { $$ = (Node *)makeString($1); }
9353 ;
9354
9355 /*****************************************************************************
9356 *
9357 * ALTER THING name OWNER TO newname
9358 *
9359 *****************************************************************************/
9360
9361 AlterOwnerStmt: ALTER AGGREGATE aggregate_with_argtypes OWNER TO RoleSpec
9362 {
9363 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9364 n->objectType = OBJECT_AGGREGATE;
9365 n->object = (Node *) $3;
9366 n->newowner = $6;
9367 $$ = (Node *)n;
9368 }
9369 | ALTER COLLATION any_name OWNER TO RoleSpec
9370 {
9371 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9372 n->objectType = OBJECT_COLLATION;
9373 n->object = (Node *) $3;
9374 n->newowner = $6;
9375 $$ = (Node *)n;
9376 }
9377 | ALTER CONVERSION_P any_name OWNER TO RoleSpec
9378 {
9379 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9380 n->objectType = OBJECT_CONVERSION;
9381 n->object = (Node *) $3;
9382 n->newowner = $6;
9383 $$ = (Node *)n;
9384 }
9385 | ALTER DATABASE database_name OWNER TO RoleSpec
9386 {
9387 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9388 n->objectType = OBJECT_DATABASE;
9389 n->object = (Node *) makeString($3);
9390 n->newowner = $6;
9391 $$ = (Node *)n;
9392 }
9393 | ALTER DOMAIN_P any_name OWNER TO RoleSpec
9394 {
9395 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9396 n->objectType = OBJECT_DOMAIN;
9397 n->object = (Node *) $3;
9398 n->newowner = $6;
9399 $$ = (Node *)n;
9400 }
9401 | ALTER FUNCTION function_with_argtypes OWNER TO RoleSpec
9402 {
9403 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9404 n->objectType = OBJECT_FUNCTION;
9405 n->object = (Node *) $3;
9406 n->newowner = $6;
9407 $$ = (Node *)n;
9408 }
9409 | ALTER opt_procedural LANGUAGE name OWNER TO RoleSpec
9410 {
9411 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9412 n->objectType = OBJECT_LANGUAGE;
9413 n->object = (Node *) makeString($4);
9414 n->newowner = $7;
9415 $$ = (Node *)n;
9416 }
9417 | ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleSpec
9418 {
9419 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9420 n->objectType = OBJECT_LARGEOBJECT;
9421 n->object = (Node *) $4;
9422 n->newowner = $7;
9423 $$ = (Node *)n;
9424 }
9425 | ALTER OPERATOR operator_with_argtypes OWNER TO RoleSpec
9426 {
9427 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9428 n->objectType = OBJECT_OPERATOR;
9429 n->object = (Node *) $3;
9430 n->newowner = $6;
9431 $$ = (Node *)n;
9432 }
9433 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleSpec
9434 {
9435 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9436 n->objectType = OBJECT_OPCLASS;
9437 n->object = (Node *) lcons(makeString($6), $4);
9438 n->newowner = $9;
9439 $$ = (Node *)n;
9440 }
9441 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleSpec
9442 {
9443 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9444 n->objectType = OBJECT_OPFAMILY;
9445 n->object = (Node *) lcons(makeString($6), $4);
9446 n->newowner = $9;
9447 $$ = (Node *)n;
9448 }
9449 | ALTER PROCEDURE function_with_argtypes OWNER TO RoleSpec
9450 {
9451 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9452 n->objectType = OBJECT_PROCEDURE;
9453 n->object = (Node *) $3;
9454 n->newowner = $6;
9455 $$ = (Node *)n;
9456 }
9457 | ALTER ROUTINE function_with_argtypes OWNER TO RoleSpec
9458 {
9459 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9460 n->objectType = OBJECT_ROUTINE;
9461 n->object = (Node *) $3;
9462 n->newowner = $6;
9463 $$ = (Node *)n;
9464 }
9465 | ALTER SCHEMA name OWNER TO RoleSpec
9466 {
9467 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9468 n->objectType = OBJECT_SCHEMA;
9469 n->object = (Node *) makeString($3);
9470 n->newowner = $6;
9471 $$ = (Node *)n;
9472 }
9473 | ALTER TYPE_P any_name OWNER TO RoleSpec
9474 {
9475 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9476 n->objectType = OBJECT_TYPE;
9477 n->object = (Node *) $3;
9478 n->newowner = $6;
9479 $$ = (Node *)n;
9480 }
9481 | ALTER TABLESPACE name OWNER TO RoleSpec
9482 {
9483 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9484 n->objectType = OBJECT_TABLESPACE;
9485 n->object = (Node *) makeString($3);
9486 n->newowner = $6;
9487 $$ = (Node *)n;
9488 }
9489 | ALTER STATISTICS any_name OWNER TO RoleSpec
9490 {
9491 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9492 n->objectType = OBJECT_STATISTIC_EXT;
9493 n->object = (Node *) $3;
9494 n->newowner = $6;
9495 $$ = (Node *)n;
9496 }
9497 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleSpec
9498 {
9499 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9500 n->objectType = OBJECT_TSDICTIONARY;
9501 n->object = (Node *) $5;
9502 n->newowner = $8;
9503 $$ = (Node *)n;
9504 }
9505 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleSpec
9506 {
9507 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9508 n->objectType = OBJECT_TSCONFIGURATION;
9509 n->object = (Node *) $5;
9510 n->newowner = $8;
9511 $$ = (Node *)n;
9512 }
9513 | ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleSpec
9514 {
9515 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9516 n->objectType = OBJECT_FDW;
9517 n->object = (Node *) makeString($5);
9518 n->newowner = $8;
9519 $$ = (Node *)n;
9520 }
9521 | ALTER SERVER name OWNER TO RoleSpec
9522 {
9523 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9524 n->objectType = OBJECT_FOREIGN_SERVER;
9525 n->object = (Node *) makeString($3);
9526 n->newowner = $6;
9527 $$ = (Node *)n;
9528 }
9529 | ALTER EVENT TRIGGER name OWNER TO RoleSpec
9530 {
9531 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9532 n->objectType = OBJECT_EVENT_TRIGGER;
9533 n->object = (Node *) makeString($4);
9534 n->newowner = $7;
9535 $$ = (Node *)n;
9536 }
9537 | ALTER PUBLICATION name OWNER TO RoleSpec
9538 {
9539 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9540 n->objectType = OBJECT_PUBLICATION;
9541 n->object = (Node *) makeString($3);
9542 n->newowner = $6;
9543 $$ = (Node *)n;
9544 }
9545 | ALTER SUBSCRIPTION name OWNER TO RoleSpec
9546 {
9547 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
9548 n->objectType = OBJECT_SUBSCRIPTION;
9549 n->object = (Node *) makeString($3);
9550 n->newowner = $6;
9551 $$ = (Node *)n;
9552 }
9553 ;
9554
9555
9556 /*****************************************************************************
9557 *
9558 * CREATE PUBLICATION name [ FOR TABLE ] [ WITH options ]
9559 *
9560 *****************************************************************************/
9561
9562 CreatePublicationStmt:
9563 CREATE PUBLICATION name opt_publication_for_tables opt_definition
9564 {
9565 CreatePublicationStmt *n = makeNode(CreatePublicationStmt);
9566 n->pubname = $3;
9567 n->options = $5;
9568 if ($4 != NULL)
9569 {
9570 /* FOR TABLE */
9571 if (IsA($4, List))
9572 n->tables = (List *)$4;
9573 /* FOR ALL TABLES */
9574 else
9575 n->for_all_tables = true;
9576 }
9577 $$ = (Node *)n;
9578 }
9579 ;
9580
9581 opt_publication_for_tables:
9582 publication_for_tables { $$ = $1; }
9583 | /* EMPTY */ { $$ = NULL; }
9584 ;
9585
9586 publication_for_tables:
9587 FOR TABLE relation_expr_list
9588 {
9589 $$ = (Node *) $3;
9590 }
9591 | FOR ALL TABLES
9592 {
9593 $$ = (Node *) makeInteger(true);
9594 }
9595 ;
9596
9597
9598 /*****************************************************************************
9599 *
9600 * ALTER PUBLICATION name SET ( options )
9601 *
9602 * ALTER PUBLICATION name ADD TABLE table [, table2]
9603 *
9604 * ALTER PUBLICATION name DROP TABLE table [, table2]
9605 *
9606 * ALTER PUBLICATION name SET TABLE table [, table2]
9607 *
9608 *****************************************************************************/
9609
9610 AlterPublicationStmt:
9611 ALTER PUBLICATION name SET definition
9612 {
9613 AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9614 n->pubname = $3;
9615 n->options = $5;
9616 $$ = (Node *)n;
9617 }
9618 | ALTER PUBLICATION name ADD_P TABLE relation_expr_list
9619 {
9620 AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9621 n->pubname = $3;
9622 n->tables = $6;
9623 n->tableAction = DEFELEM_ADD;
9624 $$ = (Node *)n;
9625 }
9626 | ALTER PUBLICATION name SET TABLE relation_expr_list
9627 {
9628 AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9629 n->pubname = $3;
9630 n->tables = $6;
9631 n->tableAction = DEFELEM_SET;
9632 $$ = (Node *)n;
9633 }
9634 | ALTER PUBLICATION name DROP TABLE relation_expr_list
9635 {
9636 AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
9637 n->pubname = $3;
9638 n->tables = $6;
9639 n->tableAction = DEFELEM_DROP;
9640 $$ = (Node *)n;
9641 }
9642 ;
9643
9644 /*****************************************************************************
9645 *
9646 * CREATE SUBSCRIPTION name ...
9647 *
9648 *****************************************************************************/
9649
9650 CreateSubscriptionStmt:
9651 CREATE SUBSCRIPTION name CONNECTION Sconst PUBLICATION publication_name_list opt_definition
9652 {
9653 CreateSubscriptionStmt *n =
9654 makeNode(CreateSubscriptionStmt);
9655 n->subname = $3;
9656 n->conninfo = $5;
9657 n->publication = $7;
9658 n->options = $8;
9659 $$ = (Node *)n;
9660 }
9661 ;
9662
9663 publication_name_list:
9664 publication_name_item
9665 {
9666 $$ = list_make1($1);
9667 }
9668 | publication_name_list ',' publication_name_item
9669 {
9670 $$ = lappend($1, $3);
9671 }
9672 ;
9673
9674 publication_name_item:
9675 ColLabel { $$ = makeString($1); };
9676
9677 /*****************************************************************************
9678 *
9679 * ALTER SUBSCRIPTION name ...
9680 *
9681 *****************************************************************************/
9682
9683 AlterSubscriptionStmt:
9684 ALTER SUBSCRIPTION name SET definition
9685 {
9686 AlterSubscriptionStmt *n =
9687 makeNode(AlterSubscriptionStmt);
9688 n->kind = ALTER_SUBSCRIPTION_OPTIONS;
9689 n->subname = $3;
9690 n->options = $5;
9691 $$ = (Node *)n;
9692 }
9693 | ALTER SUBSCRIPTION name CONNECTION Sconst
9694 {
9695 AlterSubscriptionStmt *n =
9696 makeNode(AlterSubscriptionStmt);
9697 n->kind = ALTER_SUBSCRIPTION_CONNECTION;
9698 n->subname = $3;
9699 n->conninfo = $5;
9700 $$ = (Node *)n;
9701 }
9702 | ALTER SUBSCRIPTION name REFRESH PUBLICATION opt_definition
9703 {
9704 AlterSubscriptionStmt *n =
9705 makeNode(AlterSubscriptionStmt);
9706 n->kind = ALTER_SUBSCRIPTION_REFRESH;
9707 n->subname = $3;
9708 n->options = $6;
9709 $$ = (Node *)n;
9710 }
9711 | ALTER SUBSCRIPTION name SET PUBLICATION publication_name_list opt_definition
9712 {
9713 AlterSubscriptionStmt *n =
9714 makeNode(AlterSubscriptionStmt);
9715 n->kind = ALTER_SUBSCRIPTION_PUBLICATION;
9716 n->subname = $3;
9717 n->publication = $6;
9718 n->options = $7;
9719 $$ = (Node *)n;
9720 }
9721 | ALTER SUBSCRIPTION name ENABLE_P
9722 {
9723 AlterSubscriptionStmt *n =
9724 makeNode(AlterSubscriptionStmt);
9725 n->kind = ALTER_SUBSCRIPTION_ENABLED;
9726 n->subname = $3;
9727 n->options = list_make1(makeDefElem("enabled",
9728 (Node *)makeInteger(true), @1));
9729 $$ = (Node *)n;
9730 }
9731 | ALTER SUBSCRIPTION name DISABLE_P
9732 {
9733 AlterSubscriptionStmt *n =
9734 makeNode(AlterSubscriptionStmt);
9735 n->kind = ALTER_SUBSCRIPTION_ENABLED;
9736 n->subname = $3;
9737 n->options = list_make1(makeDefElem("enabled",
9738 (Node *)makeInteger(false), @1));
9739 $$ = (Node *)n;
9740 }
9741 ;
9742
9743 /*****************************************************************************
9744 *
9745 * DROP SUBSCRIPTION [ IF EXISTS ] name
9746 *
9747 *****************************************************************************/
9748
9749 DropSubscriptionStmt: DROP SUBSCRIPTION name opt_drop_behavior
9750 {
9751 DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9752 n->subname = $3;
9753 n->missing_ok = false;
9754 n->behavior = $4;
9755 $$ = (Node *) n;
9756 }
9757 | DROP SUBSCRIPTION IF_P EXISTS name opt_drop_behavior
9758 {
9759 DropSubscriptionStmt *n = makeNode(DropSubscriptionStmt);
9760 n->subname = $5;
9761 n->missing_ok = true;
9762 n->behavior = $6;
9763 $$ = (Node *) n;
9764 }
9765 ;
9766
9767 /*****************************************************************************
9768 *
9769 * QUERY: Define Rewrite Rule
9770 *
9771 *****************************************************************************/
9772
9773 RuleStmt: CREATE opt_or_replace RULE name AS
9774 ON event TO qualified_name where_clause
9775 DO opt_instead RuleActionList
9776 {
9777 RuleStmt *n = makeNode(RuleStmt);
9778 n->replace = $2;
9779 n->relation = $9;
9780 n->rulename = $4;
9781 n->whereClause = $10;
9782 n->event = $7;
9783 n->instead = $12;
9784 n->actions = $13;
9785 $$ = (Node *)n;
9786 }
9787 ;
9788
9789 RuleActionList:
9790 NOTHING { $$ = NIL; }
9791 | RuleActionStmt { $$ = list_make1($1); }
9792 | '(' RuleActionMulti ')' { $$ = $2; }
9793 ;
9794
9795 /* the thrashing around here is to discard "empty" statements... */
9796 RuleActionMulti:
9797 RuleActionMulti ';' RuleActionStmtOrEmpty
9798 { if ($3 != NULL)
9799 $$ = lappend($1, $3);
9800 else
9801 $$ = $1;
9802 }
9803 | RuleActionStmtOrEmpty
9804 { if ($1 != NULL)
9805 $$ = list_make1($1);
9806 else
9807 $$ = NIL;
9808 }
9809 ;
9810
9811 RuleActionStmt:
9812 SelectStmt
9813 | InsertStmt
9814 | UpdateStmt
9815 | DeleteStmt
9816 | NotifyStmt
9817 ;
9818
9819 RuleActionStmtOrEmpty:
9820 RuleActionStmt { $$ = $1; }
9821 | /*EMPTY*/ { $$ = NULL; }
9822 ;
9823
9824 event: SELECT { $$ = CMD_SELECT; }
9825 | UPDATE { $$ = CMD_UPDATE; }
9826 | DELETE_P { $$ = CMD_DELETE; }
9827 | INSERT { $$ = CMD_INSERT; }
9828 ;
9829
9830 opt_instead:
9831 INSTEAD { $$ = true; }
9832 | ALSO { $$ = false; }
9833 | /*EMPTY*/ { $$ = false; }
9834 ;
9835
9836
9837 /*****************************************************************************
9838 *
9839 * QUERY:
9840 * NOTIFY <identifier> can appear both in rule bodies and
9841 * as a query-level command
9842 *
9843 *****************************************************************************/
9844
9845 NotifyStmt: NOTIFY ColId notify_payload
9846 {
9847 NotifyStmt *n = makeNode(NotifyStmt);
9848 n->conditionname = $2;
9849 n->payload = $3;
9850 $$ = (Node *)n;
9851 }
9852 ;
9853
9854 notify_payload:
9855 ',' Sconst { $$ = $2; }
9856 | /*EMPTY*/ { $$ = NULL; }
9857 ;
9858
9859 ListenStmt: LISTEN ColId
9860 {
9861 ListenStmt *n = makeNode(ListenStmt);
9862 n->conditionname = $2;
9863 $$ = (Node *)n;
9864 }
9865 ;
9866
9867 UnlistenStmt:
9868 UNLISTEN ColId
9869 {
9870 UnlistenStmt *n = makeNode(UnlistenStmt);
9871 n->conditionname = $2;
9872 $$ = (Node *)n;
9873 }
9874 | UNLISTEN '*'
9875 {
9876 UnlistenStmt *n = makeNode(UnlistenStmt);
9877 n->conditionname = NULL;
9878 $$ = (Node *)n;
9879 }
9880 ;
9881
9882
9883 /*****************************************************************************
9884 *
9885 * Transactions:
9886 *
9887 * BEGIN / COMMIT / ROLLBACK
9888 * (also older versions END / ABORT)
9889 *
9890 *****************************************************************************/
9891
9892 TransactionStmt:
9893 ABORT_P opt_transaction opt_transaction_chain
9894 {
9895 TransactionStmt *n = makeNode(TransactionStmt);
9896 n->kind = TRANS_STMT_ROLLBACK;
9897 n->options = NIL;
9898 n->chain = $3;
9899 $$ = (Node *)n;
9900 }
9901 | BEGIN_P opt_transaction transaction_mode_list_or_empty
9902 {
9903 TransactionStmt *n = makeNode(TransactionStmt);
9904 n->kind = TRANS_STMT_BEGIN;
9905 n->options = $3;
9906 $$ = (Node *)n;
9907 }
9908 | START TRANSACTION transaction_mode_list_or_empty
9909 {
9910 TransactionStmt *n = makeNode(TransactionStmt);
9911 n->kind = TRANS_STMT_START;
9912 n->options = $3;
9913 $$ = (Node *)n;
9914 }
9915 | COMMIT opt_transaction opt_transaction_chain
9916 {
9917 TransactionStmt *n = makeNode(TransactionStmt);
9918 n->kind = TRANS_STMT_COMMIT;
9919 n->options = NIL;
9920 n->chain = $3;
9921 $$ = (Node *)n;
9922 }
9923 | END_P opt_transaction opt_transaction_chain
9924 {
9925 TransactionStmt *n = makeNode(TransactionStmt);
9926 n->kind = TRANS_STMT_COMMIT;
9927 n->options = NIL;
9928 n->chain = $3;
9929 $$ = (Node *)n;
9930 }
9931 | ROLLBACK opt_transaction opt_transaction_chain
9932 {
9933 TransactionStmt *n = makeNode(TransactionStmt);
9934 n->kind = TRANS_STMT_ROLLBACK;
9935 n->options = NIL;
9936 n->chain = $3;
9937 $$ = (Node *)n;
9938 }
9939 | SAVEPOINT ColId
9940 {
9941 TransactionStmt *n = makeNode(TransactionStmt);
9942 n->kind = TRANS_STMT_SAVEPOINT;
9943 n->savepoint_name = $2;
9944 $$ = (Node *)n;
9945 }
9946 | RELEASE SAVEPOINT ColId
9947 {
9948 TransactionStmt *n = makeNode(TransactionStmt);
9949 n->kind = TRANS_STMT_RELEASE;
9950 n->savepoint_name = $3;
9951 $$ = (Node *)n;
9952 }
9953 | RELEASE ColId
9954 {
9955 TransactionStmt *n = makeNode(TransactionStmt);
9956 n->kind = TRANS_STMT_RELEASE;
9957 n->savepoint_name = $2;
9958 $$ = (Node *)n;
9959 }
9960 | ROLLBACK opt_transaction TO SAVEPOINT ColId
9961 {
9962 TransactionStmt *n = makeNode(TransactionStmt);
9963 n->kind = TRANS_STMT_ROLLBACK_TO;
9964 n->savepoint_name = $5;
9965 $$ = (Node *)n;
9966 }
9967 | ROLLBACK opt_transaction TO ColId
9968 {
9969 TransactionStmt *n = makeNode(TransactionStmt);
9970 n->kind = TRANS_STMT_ROLLBACK_TO;
9971 n->savepoint_name = $4;
9972 $$ = (Node *)n;
9973 }
9974 | PREPARE TRANSACTION Sconst
9975 {
9976 TransactionStmt *n = makeNode(TransactionStmt);
9977 n->kind = TRANS_STMT_PREPARE;
9978 n->gid = $3;
9979 $$ = (Node *)n;
9980 }
9981 | COMMIT PREPARED Sconst
9982 {
9983 TransactionStmt *n = makeNode(TransactionStmt);
9984 n->kind = TRANS_STMT_COMMIT_PREPARED;
9985 n->gid = $3;
9986 $$ = (Node *)n;
9987 }
9988 | ROLLBACK PREPARED Sconst
9989 {
9990 TransactionStmt *n = makeNode(TransactionStmt);
9991 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
9992 n->gid = $3;
9993 $$ = (Node *)n;
9994 }
9995 ;
9996
9997 opt_transaction: WORK {}
9998 | TRANSACTION {}
9999 | /*EMPTY*/ {}
10000 ;
10001
10002 transaction_mode_item:
10003 ISOLATION LEVEL iso_level
10004 { $$ = makeDefElem("transaction_isolation",
10005 makeStringConst($3, @3), @1); }
10006 | READ ONLY
10007 { $$ = makeDefElem("transaction_read_only",
10008 makeIntConst(true, @1), @1); }
10009 | READ WRITE
10010 { $$ = makeDefElem("transaction_read_only",
10011 makeIntConst(false, @1), @1); }
10012 | DEFERRABLE
10013 { $$ = makeDefElem("transaction_deferrable",
10014 makeIntConst(true, @1), @1); }
10015 | NOT DEFERRABLE
10016 { $$ = makeDefElem("transaction_deferrable",
10017 makeIntConst(false, @1), @1); }
10018 ;
10019
10020 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
10021 transaction_mode_list:
10022 transaction_mode_item
10023 { $$ = list_make1($1); }
10024 | transaction_mode_list ',' transaction_mode_item
10025 { $$ = lappend($1, $3); }
10026 | transaction_mode_list transaction_mode_item
10027 { $$ = lappend($1, $2); }
10028 ;
10029
10030 transaction_mode_list_or_empty:
10031 transaction_mode_list
10032 | /* EMPTY */
10033 { $$ = NIL; }
10034 ;
10035
10036 opt_transaction_chain:
10037 AND CHAIN { $$ = true; }
10038 | AND NO CHAIN { $$ = false; }
10039 | /* EMPTY */ { $$ = false; }
10040 ;
10041
10042
10043 /*****************************************************************************
10044 *
10045 * QUERY:
10046 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
10047 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
10048 *
10049 *****************************************************************************/
10050
10051 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10052 AS SelectStmt opt_check_option
10053 {
10054 ViewStmt *n = makeNode(ViewStmt);
10055 n->view = $4;
10056 n->view->relpersistence = $2;
10057 n->aliases = $5;
10058 n->query = $8;
10059 n->replace = false;
10060 n->options = $6;
10061 n->withCheckOption = $9;
10062 $$ = (Node *) n;
10063 }
10064 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list opt_reloptions
10065 AS SelectStmt opt_check_option
10066 {
10067 ViewStmt *n = makeNode(ViewStmt);
10068 n->view = $6;
10069 n->view->relpersistence = $4;
10070 n->aliases = $7;
10071 n->query = $10;
10072 n->replace = true;
10073 n->options = $8;
10074 n->withCheckOption = $11;
10075 $$ = (Node *) n;
10076 }
10077 | CREATE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10078 AS SelectStmt opt_check_option
10079 {
10080 ViewStmt *n = makeNode(ViewStmt);
10081 n->view = $5;
10082 n->view->relpersistence = $2;
10083 n->aliases = $7;
10084 n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $11);
10085 n->replace = false;
10086 n->options = $9;
10087 n->withCheckOption = $12;
10088 if (n->withCheckOption != NO_CHECK_OPTION)
10089 ereport(ERROR,
10090 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10091 errmsg("WITH CHECK OPTION not supported on recursive views"),
10092 parser_errposition(@12)));
10093 $$ = (Node *) n;
10094 }
10095 | CREATE OR REPLACE OptTemp RECURSIVE VIEW qualified_name '(' columnList ')' opt_reloptions
10096 AS SelectStmt opt_check_option
10097 {
10098 ViewStmt *n = makeNode(ViewStmt);
10099 n->view = $7;
10100 n->view->relpersistence = $4;
10101 n->aliases = $9;
10102 n->query = makeRecursiveViewSelect(n->view->relname, n->aliases, $13);
10103 n->replace = true;
10104 n->options = $11;
10105 n->withCheckOption = $14;
10106 if (n->withCheckOption != NO_CHECK_OPTION)
10107 ereport(ERROR,
10108 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10109 errmsg("WITH CHECK OPTION not supported on recursive views"),
10110 parser_errposition(@14)));
10111 $$ = (Node *) n;
10112 }
10113 ;
10114
10115 opt_check_option:
10116 WITH CHECK OPTION { $$ = CASCADED_CHECK_OPTION; }
10117 | WITH CASCADED CHECK OPTION { $$ = CASCADED_CHECK_OPTION; }
10118 | WITH LOCAL CHECK OPTION { $$ = LOCAL_CHECK_OPTION; }
10119 | /* EMPTY */ { $$ = NO_CHECK_OPTION; }
10120 ;
10121
10122 /*****************************************************************************
10123 *
10124 * QUERY:
10125 * LOAD "filename"
10126 *
10127 *****************************************************************************/
10128
10129 LoadStmt: LOAD file_name
10130 {
10131 LoadStmt *n = makeNode(LoadStmt);
10132 n->filename = $2;
10133 $$ = (Node *)n;
10134 }
10135 ;
10136
10137
10138 /*****************************************************************************
10139 *
10140 * CREATE DATABASE
10141 *
10142 *****************************************************************************/
10143
10144 CreatedbStmt:
10145 CREATE DATABASE database_name opt_with createdb_opt_list
10146 {
10147 CreatedbStmt *n = makeNode(CreatedbStmt);
10148 n->dbname = $3;
10149 n->options = $5;
10150 $$ = (Node *)n;
10151 }
10152 ;
10153
10154 createdb_opt_list:
10155 createdb_opt_items { $$ = $1; }
10156 | /* EMPTY */ { $$ = NIL; }
10157 ;
10158
10159 createdb_opt_items:
10160 createdb_opt_item { $$ = list_make1($1); }
10161 | createdb_opt_items createdb_opt_item { $$ = lappend($1, $2); }
10162 ;
10163
10164 createdb_opt_item:
10165 createdb_opt_name opt_equal SignedIconst
10166 {
10167 $$ = makeDefElem($1, (Node *)makeInteger($3), @1);
10168 }
10169 | createdb_opt_name opt_equal opt_boolean_or_string
10170 {
10171 $$ = makeDefElem($1, (Node *)makeString($3), @1);
10172 }
10173 | createdb_opt_name opt_equal DEFAULT
10174 {
10175 $$ = makeDefElem($1, NULL, @1);
10176 }
10177 ;
10178
10179 /*
10180 * Ideally we'd use ColId here, but that causes shift/reduce conflicts against
10181 * the ALTER DATABASE SET/RESET syntaxes. Instead call out specific keywords
10182 * we need, and allow IDENT so that database option names don't have to be
10183 * parser keywords unless they are already keywords for other reasons.
10184 *
10185 * XXX this coding technique is fragile since if someone makes a formerly
10186 * non-keyword option name into a keyword and forgets to add it here, the
10187 * option will silently break. Best defense is to provide a regression test
10188 * exercising every such option, at least at the syntax level.
10189 */
10190 createdb_opt_name:
10191 IDENT { $$ = $1; }
10192 | CONNECTION LIMIT { $$ = pstrdup("connection_limit"); }
10193 | ENCODING { $$ = pstrdup($1); }
10194 | LOCATION { $$ = pstrdup($1); }
10195 | OWNER { $$ = pstrdup($1); }
10196 | TABLESPACE { $$ = pstrdup($1); }
10197 | TEMPLATE { $$ = pstrdup($1); }
10198 ;
10199
10200 /*
10201 * Though the equals sign doesn't match other WITH options, pg_dump uses
10202 * equals for backward compatibility, and it doesn't seem worth removing it.
10203 */
10204 opt_equal: '=' {}
10205 | /*EMPTY*/ {}
10206 ;
10207
10208
10209 /*****************************************************************************
10210 *
10211 * ALTER DATABASE
10212 *
10213 *****************************************************************************/
10214
10215 AlterDatabaseStmt:
10216 ALTER DATABASE database_name WITH createdb_opt_list
10217 {
10218 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10219 n->dbname = $3;
10220 n->options = $5;
10221 $$ = (Node *)n;
10222 }
10223 | ALTER DATABASE database_name createdb_opt_list
10224 {
10225 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10226 n->dbname = $3;
10227 n->options = $4;
10228 $$ = (Node *)n;
10229 }
10230 | ALTER DATABASE database_name SET TABLESPACE name
10231 {
10232 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
10233 n->dbname = $3;
10234 n->options = list_make1(makeDefElem("tablespace",
10235 (Node *)makeString($6), @6));
10236 $$ = (Node *)n;
10237 }
10238 ;
10239
10240 AlterDatabaseSetStmt:
10241 ALTER DATABASE database_name SetResetClause
10242 {
10243 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
10244 n->dbname = $3;
10245 n->setstmt = $4;
10246 $$ = (Node *)n;
10247 }
10248 ;
10249
10250
10251 /*****************************************************************************
10252 *
10253 * DROP DATABASE [ IF EXISTS ]
10254 *
10255 * This is implicitly CASCADE, no need for drop behavior
10256 *****************************************************************************/
10257
10258 DropdbStmt: DROP DATABASE database_name
10259 {
10260 DropdbStmt *n = makeNode(DropdbStmt);
10261 n->dbname = $3;
10262 n->missing_ok = false;
10263 $$ = (Node *)n;
10264 }
10265 | DROP DATABASE IF_P EXISTS database_name
10266 {
10267 DropdbStmt *n = makeNode(DropdbStmt);
10268 n->dbname = $5;
10269 n->missing_ok = true;
10270 $$ = (Node *)n;
10271 }
10272 ;
10273
10274
10275 /*****************************************************************************
10276 *
10277 * ALTER COLLATION
10278 *
10279 *****************************************************************************/
10280
10281 AlterCollationStmt: ALTER COLLATION any_name REFRESH VERSION_P
10282 {
10283 AlterCollationStmt *n = makeNode(AlterCollationStmt);
10284 n->collname = $3;
10285 $$ = (Node *)n;
10286 }
10287 ;
10288
10289
10290 /*****************************************************************************
10291 *
10292 * ALTER SYSTEM
10293 *
10294 * This is used to change configuration parameters persistently.
10295 *****************************************************************************/
10296
10297 AlterSystemStmt:
10298 ALTER SYSTEM_P SET generic_set
10299 {
10300 AlterSystemStmt *n = makeNode(AlterSystemStmt);
10301 n->setstmt = $4;
10302 $$ = (Node *)n;
10303 }
10304 | ALTER SYSTEM_P RESET generic_reset
10305 {
10306 AlterSystemStmt *n = makeNode(AlterSystemStmt);
10307 n->setstmt = $4;
10308 $$ = (Node *)n;
10309 }
10310 ;
10311
10312
10313 /*****************************************************************************
10314 *
10315 * Manipulate a domain
10316 *
10317 *****************************************************************************/
10318
10319 CreateDomainStmt:
10320 CREATE DOMAIN_P any_name opt_as Typename ColQualList
10321 {
10322 CreateDomainStmt *n = makeNode(CreateDomainStmt);
10323 n->domainname = $3;
10324 n->typeName = $5;
10325 SplitColQualList($6, &n->constraints, &n->collClause,
10326 yyscanner);
10327 $$ = (Node *)n;
10328 }
10329 ;
10330
10331 AlterDomainStmt:
10332 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
10333 ALTER DOMAIN_P any_name alter_column_default
10334 {
10335 AlterDomainStmt *n = makeNode(AlterDomainStmt);
10336 n->subtype = 'T';
10337 n->typeName = $3;
10338 n->def = $4;
10339 $$ = (Node *)n;
10340 }
10341 /* ALTER DOMAIN <domain> DROP NOT NULL */
10342 | ALTER DOMAIN_P any_name DROP NOT NULL_P
10343 {
10344 AlterDomainStmt *n = makeNode(AlterDomainStmt);
10345 n->subtype = 'N';
10346 n->typeName = $3;
10347 $$ = (Node *)n;
10348 }
10349 /* ALTER DOMAIN <domain> SET NOT NULL */
10350 | ALTER DOMAIN_P any_name SET NOT NULL_P
10351 {
10352 AlterDomainStmt *n = makeNode(AlterDomainStmt);
10353 n->subtype = 'O';
10354 n->typeName = $3;
10355 $$ = (Node *)n;
10356 }
10357 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
10358 | ALTER DOMAIN_P any_name ADD_P TableConstraint
10359 {
10360 AlterDomainStmt *n = makeNode(AlterDomainStmt);
10361 n->subtype = 'C';
10362 n->typeName = $3;
10363 n->def = $5;
10364 $$ = (Node *)n;
10365 }
10366 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
10367 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
10368 {
10369 AlterDomainStmt *n = makeNode(AlterDomainStmt);
10370 n->subtype = 'X';
10371 n->typeName = $3;
10372 n->name = $6;
10373 n->behavior = $7;
10374 n->missing_ok = false;
10375 $$ = (Node *)n;
10376 }
10377 /* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
10378 | ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
10379 {
10380 AlterDomainStmt *n = makeNode(AlterDomainStmt);
10381 n->subtype = 'X';
10382 n->typeName = $3;
10383 n->name = $8;
10384 n->behavior = $9;
10385 n->missing_ok = true;
10386 $$ = (Node *)n;
10387 }
10388 /* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
10389 | ALTER DOMAIN_P any_name VALIDATE CONSTRAINT name
10390 {
10391 AlterDomainStmt *n = makeNode(AlterDomainStmt);
10392 n->subtype = 'V';
10393 n->typeName = $3;
10394 n->name = $6;
10395 $$ = (Node *)n;
10396 }
10397 ;
10398
10399 opt_as: AS {}
10400 | /* EMPTY */ {}
10401 ;
10402
10403
10404 /*****************************************************************************
10405 *
10406 * Manipulate a text search dictionary or configuration
10407 *
10408 *****************************************************************************/
10409
10410 AlterTSDictionaryStmt:
10411 ALTER TEXT_P SEARCH DICTIONARY any_name definition
10412 {
10413 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
10414 n->dictname = $5;
10415 n->options = $6;
10416 $$ = (Node *)n;
10417 }
10418 ;
10419
10420 AlterTSConfigurationStmt:
10421 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list any_with any_name_list
10422 {
10423 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10424 n->kind = ALTER_TSCONFIG_ADD_MAPPING;
10425 n->cfgname = $5;
10426 n->tokentype = $9;
10427 n->dicts = $11;
10428 n->override = false;
10429 n->replace = false;
10430 $$ = (Node*)n;
10431 }
10432 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list any_with any_name_list
10433 {
10434 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10435 n->kind = ALTER_TSCONFIG_ALTER_MAPPING_FOR_TOKEN;
10436 n->cfgname = $5;
10437 n->tokentype = $9;
10438 n->dicts = $11;
10439 n->override = true;
10440 n->replace = false;
10441 $$ = (Node*)n;
10442 }
10443 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name any_with any_name
10444 {
10445 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10446 n->kind = ALTER_TSCONFIG_REPLACE_DICT;
10447 n->cfgname = $5;
10448 n->tokentype = NIL;
10449 n->dicts = list_make2($9,$11);
10450 n->override = false;
10451 n->replace = true;
10452 $$ = (Node*)n;
10453 }
10454 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name any_with any_name
10455 {
10456 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10457 n->kind = ALTER_TSCONFIG_REPLACE_DICT_FOR_TOKEN;
10458 n->cfgname = $5;
10459 n->tokentype = $9;
10460 n->dicts = list_make2($11,$13);
10461 n->override = false;
10462 n->replace = true;
10463 $$ = (Node*)n;
10464 }
10465 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
10466 {
10467 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10468 n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10469 n->cfgname = $5;
10470 n->tokentype = $9;
10471 n->missing_ok = false;
10472 $$ = (Node*)n;
10473 }
10474 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
10475 {
10476 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
10477 n->kind = ALTER_TSCONFIG_DROP_MAPPING;
10478 n->cfgname = $5;
10479 n->tokentype = $11;
10480 n->missing_ok = true;
10481 $$ = (Node*)n;
10482 }
10483 ;
10484
10485 /* Use this if TIME or ORDINALITY after WITH should be taken as an identifier */
10486 any_with: WITH {}
10487 | WITH_LA {}
10488 ;
10489
10490
10491 /*****************************************************************************
10492 *
10493 * Manipulate a conversion
10494 *
10495 * CREATE [DEFAULT] CONVERSION <conversion_name>
10496 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
10497 *
10498 *****************************************************************************/
10499
10500 CreateConversionStmt:
10501 CREATE opt_default CONVERSION_P any_name FOR Sconst
10502 TO Sconst FROM any_name
10503 {
10504 CreateConversionStmt *n = makeNode(CreateConversionStmt);
10505 n->conversion_name = $4;
10506 n->for_encoding_name = $6;
10507 n->to_encoding_name = $8;
10508 n->func_name = $10;
10509 n->def = $2;
10510 $$ = (Node *)n;
10511 }
10512 ;
10513
10514 /*****************************************************************************
10515 *
10516 * QUERY:
10517 * CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
10518 * CLUSTER [VERBOSE]
10519 * CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
10520 *
10521 *****************************************************************************/
10522
10523 ClusterStmt:
10524 CLUSTER opt_verbose qualified_name cluster_index_specification
10525 {
10526 ClusterStmt *n = makeNode(ClusterStmt);
10527 n->relation = $3;
10528 n->indexname = $4;
10529 n->options = 0;
10530 if ($2)
10531 n->options |= CLUOPT_VERBOSE;
10532 $$ = (Node*)n;
10533 }
10534 | CLUSTER opt_verbose
10535 {
10536 ClusterStmt *n = makeNode(ClusterStmt);
10537 n->relation = NULL;
10538 n->indexname = NULL;
10539 n->options = 0;
10540 if ($2)
10541 n->options |= CLUOPT_VERBOSE;
10542 $$ = (Node*)n;
10543 }
10544 /* kept for pre-8.3 compatibility */
10545 | CLUSTER opt_verbose index_name ON qualified_name
10546 {
10547 ClusterStmt *n = makeNode(ClusterStmt);
10548 n->relation = $5;
10549 n->indexname = $3;
10550 n->options = 0;
10551 if ($2)
10552 n->options |= CLUOPT_VERBOSE;
10553 $$ = (Node*)n;
10554 }
10555 ;
10556
10557 cluster_index_specification:
10558 USING index_name { $$ = $2; }
10559 | /*EMPTY*/ { $$ = NULL; }
10560 ;
10561
10562
10563 /*****************************************************************************
10564 *
10565 * QUERY:
10566 * VACUUM
10567 * ANALYZE
10568 *
10569 *****************************************************************************/
10570
10571 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose opt_analyze opt_vacuum_relation_list
10572 {
10573 VacuumStmt *n = makeNode(VacuumStmt);
10574 n->options = NIL;
10575 if ($2)
10576 n->options = lappend(n->options,
10577 makeDefElem("full", NULL, @2));
10578 if ($3)
10579 n->options = lappend(n->options,
10580 makeDefElem("freeze", NULL, @3));
10581 if ($4)
10582 n->options = lappend(n->options,
10583 makeDefElem("verbose", NULL, @4));
10584 if ($5)
10585 n->options = lappend(n->options,
10586 makeDefElem("analyze", NULL, @5));
10587 n->rels = $6;
10588 n->is_vacuumcmd = true;
10589 $$ = (Node *)n;
10590 }
10591 | VACUUM '(' vac_analyze_option_list ')' opt_vacuum_relation_list
10592 {
10593 VacuumStmt *n = makeNode(VacuumStmt);
10594 n->options = $3;
10595 n->rels = $5;
10596 n->is_vacuumcmd = true;
10597 $$ = (Node *) n;
10598 }
10599 ;
10600
10601 AnalyzeStmt: analyze_keyword opt_verbose opt_vacuum_relation_list
10602 {
10603 VacuumStmt *n = makeNode(VacuumStmt);
10604 n->options = NIL;
10605 if ($2)
10606 n->options = lappend(n->options,
10607 makeDefElem("verbose", NULL, @2));
10608 n->rels = $3;
10609 n->is_vacuumcmd = false;
10610 $$ = (Node *)n;
10611 }
10612 | analyze_keyword '(' vac_analyze_option_list ')' opt_vacuum_relation_list
10613 {
10614 VacuumStmt *n = makeNode(VacuumStmt);
10615 n->options = $3;
10616 n->rels = $5;
10617 n->is_vacuumcmd = false;
10618 $$ = (Node *) n;
10619 }
10620 ;
10621
10622 vac_analyze_option_list:
10623 vac_analyze_option_elem
10624 {
10625 $$ = list_make1($1);
10626 }
10627 | vac_analyze_option_list ',' vac_analyze_option_elem
10628 {
10629 $$ = lappend($1, $3);
10630 }
10631 ;
10632
10633 analyze_keyword:
10634 ANALYZE {}
10635 | ANALYSE /* British */ {}
10636 ;
10637
10638 vac_analyze_option_elem:
10639 vac_analyze_option_name vac_analyze_option_arg
10640 {
10641 $$ = makeDefElem($1, $2, @1);
10642 }
10643 ;
10644
10645 vac_analyze_option_name:
10646 NonReservedWord { $$ = $1; }
10647 | analyze_keyword { $$ = "analyze"; }
10648 ;
10649
10650 vac_analyze_option_arg:
10651 opt_boolean_or_string { $$ = (Node *) makeString($1); }
10652 | NumericOnly { $$ = (Node *) $1; }
10653 | /* EMPTY */ { $$ = NULL; }
10654 ;
10655
10656 opt_analyze:
10657 analyze_keyword { $$ = true; }
10658 | /*EMPTY*/ { $$ = false; }
10659 ;
10660
10661 opt_verbose:
10662 VERBOSE { $$ = true; }
10663 | /*EMPTY*/ { $$ = false; }
10664 ;
10665
10666 opt_full: FULL { $$ = true; }
10667 | /*EMPTY*/ { $$ = false; }
10668 ;
10669
10670 opt_freeze: FREEZE { $$ = true; }
10671 | /*EMPTY*/ { $$ = false; }
10672 ;
10673
10674 opt_name_list:
10675 '(' name_list ')' { $$ = $2; }
10676 | /*EMPTY*/ { $$ = NIL; }
10677 ;
10678
10679 vacuum_relation:
10680 qualified_name opt_name_list
10681 {
10682 $$ = (Node *) makeVacuumRelation($1, InvalidOid, $2);
10683 }
10684 ;
10685
10686 vacuum_relation_list:
10687 vacuum_relation
10688 { $$ = list_make1($1); }
10689 | vacuum_relation_list ',' vacuum_relation
10690 { $$ = lappend($1, $3); }
10691 ;
10692
10693 opt_vacuum_relation_list:
10694 vacuum_relation_list { $$ = $1; }
10695 | /*EMPTY*/ { $$ = NIL; }
10696 ;
10697
10698
10699 /*****************************************************************************
10700 *
10701 * QUERY:
10702 * EXPLAIN [ANALYZE] [VERBOSE] query
10703 * EXPLAIN ( options ) query
10704 *
10705 *****************************************************************************/
10706
10707 ExplainStmt:
10708 EXPLAIN ExplainableStmt
10709 {
10710 ExplainStmt *n = makeNode(ExplainStmt);
10711 n->query = $2;
10712 n->options = NIL;
10713 $$ = (Node *) n;
10714 }
10715 | EXPLAIN analyze_keyword opt_verbose ExplainableStmt
10716 {
10717 ExplainStmt *n = makeNode(ExplainStmt);
10718 n->query = $4;
10719 n->options = list_make1(makeDefElem("analyze", NULL, @2));
10720 if ($3)
10721 n->options = lappend(n->options,
10722 makeDefElem("verbose", NULL, @3));
10723 $$ = (Node *) n;
10724 }
10725 | EXPLAIN VERBOSE ExplainableStmt
10726 {
10727 ExplainStmt *n = makeNode(ExplainStmt);
10728 n->query = $3;
10729 n->options = list_make1(makeDefElem("verbose", NULL, @2));
10730 $$ = (Node *) n;
10731 }
10732 | EXPLAIN '(' explain_option_list ')' ExplainableStmt
10733 {
10734 ExplainStmt *n = makeNode(ExplainStmt);
10735 n->query = $5;
10736 n->options = $3;
10737 $$ = (Node *) n;
10738 }
10739 ;
10740
10741 ExplainableStmt:
10742 SelectStmt
10743 | InsertStmt
10744 | UpdateStmt
10745 | DeleteStmt
10746 | DeclareCursorStmt
10747 | CreateAsStmt
10748 | CreateMatViewStmt
10749 | RefreshMatViewStmt
10750 | ExecuteStmt /* by default all are $$=$1 */
10751 ;
10752
10753 explain_option_list:
10754 explain_option_elem
10755 {
10756 $$ = list_make1($1);
10757 }
10758 | explain_option_list ',' explain_option_elem
10759 {
10760 $$ = lappend($1, $3);
10761 }
10762 ;
10763
10764 explain_option_elem:
10765 explain_option_name explain_option_arg
10766 {
10767 $$ = makeDefElem($1, $2, @1);
10768 }
10769 ;
10770
10771 explain_option_name:
10772 NonReservedWord { $$ = $1; }
10773 | analyze_keyword { $$ = "analyze"; }
10774 ;
10775
10776 explain_option_arg:
10777 opt_boolean_or_string { $$ = (Node *) makeString($1); }
10778 | NumericOnly { $$ = (Node *) $1; }
10779 | /* EMPTY */ { $$ = NULL; }
10780 ;
10781
10782 /*****************************************************************************
10783 *
10784 * QUERY:
10785 * PREPARE <plan_name> [(args, ...)] AS <query>
10786 *
10787 *****************************************************************************/
10788
10789 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
10790 {
10791 PrepareStmt *n = makeNode(PrepareStmt);
10792 n->name = $2;
10793 n->argtypes = $3;
10794 n->query = $5;
10795 $$ = (Node *) n;
10796 }
10797 ;
10798
10799 prep_type_clause: '(' type_list ')' { $$ = $2; }
10800 | /* EMPTY */ { $$ = NIL; }
10801 ;
10802
10803 PreparableStmt:
10804 SelectStmt
10805 | InsertStmt
10806 | UpdateStmt
10807 | DeleteStmt /* by default all are $$=$1 */
10808 ;
10809
10810 /*****************************************************************************
10811 *
10812 * EXECUTE <plan_name> [(params, ...)]
10813 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
10814 *
10815 *****************************************************************************/
10816
10817 ExecuteStmt: EXECUTE name execute_param_clause
10818 {
10819 ExecuteStmt *n = makeNode(ExecuteStmt);
10820 n->name = $2;
10821 n->params = $3;
10822 $$ = (Node *) n;
10823 }
10824 | CREATE OptTemp TABLE create_as_target AS
10825 EXECUTE name execute_param_clause opt_with_data
10826 {
10827 CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10828 ExecuteStmt *n = makeNode(ExecuteStmt);
10829 n->name = $7;
10830 n->params = $8;
10831 ctas->query = (Node *) n;
10832 ctas->into = $4;
10833 ctas->relkind = OBJECT_TABLE;
10834 ctas->is_select_into = false;
10835 ctas->if_not_exists = false;
10836 /* cram additional flags into the IntoClause */
10837 $4->rel->relpersistence = $2;
10838 $4->skipData = !($9);
10839 $$ = (Node *) ctas;
10840 }
10841 | CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS
10842 EXECUTE name execute_param_clause opt_with_data
10843 {
10844 CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10845 ExecuteStmt *n = makeNode(ExecuteStmt);
10846 n->name = $10;
10847 n->params = $11;
10848 ctas->query = (Node *) n;
10849 ctas->into = $7;
10850 ctas->relkind = OBJECT_TABLE;
10851 ctas->is_select_into = false;
10852 ctas->if_not_exists = true;
10853 /* cram additional flags into the IntoClause */
10854 $7->rel->relpersistence = $2;
10855 $7->skipData = !($12);
10856 $$ = (Node *) ctas;
10857 }
10858 ;
10859
10860 execute_param_clause: '(' expr_list ')' { $$ = $2; }
10861 | /* EMPTY */ { $$ = NIL; }
10862 ;
10863
10864 /*****************************************************************************
10865 *
10866 * QUERY:
10867 * DEALLOCATE [PREPARE] <plan_name>
10868 *
10869 *****************************************************************************/
10870
10871 DeallocateStmt: DEALLOCATE name
10872 {
10873 DeallocateStmt *n = makeNode(DeallocateStmt);
10874 n->name = $2;
10875 $$ = (Node *) n;
10876 }
10877 | DEALLOCATE PREPARE name
10878 {
10879 DeallocateStmt *n = makeNode(DeallocateStmt);
10880 n->name = $3;
10881 $$ = (Node *) n;
10882 }
10883 | DEALLOCATE ALL
10884 {
10885 DeallocateStmt *n = makeNode(DeallocateStmt);
10886 n->name = NULL;
10887 $$ = (Node *) n;
10888 }
10889 | DEALLOCATE PREPARE ALL
10890 {
10891 DeallocateStmt *n = makeNode(DeallocateStmt);
10892 n->name = NULL;
10893 $$ = (Node *) n;
10894 }
10895 ;
10896
10897 /*****************************************************************************
10898 *
10899 * QUERY:
10900 * INSERT STATEMENTS
10901 *
10902 *****************************************************************************/
10903
10904 InsertStmt:
10905 opt_with_clause INSERT INTO insert_target insert_rest
10906 opt_on_conflict returning_clause
10907 {
10908 $5->relation = $4;
10909 $5->onConflictClause = $6;
10910 $5->returningList = $7;
10911 $5->withClause = $1;
10912 $$ = (Node *) $5;
10913 }
10914 ;
10915
10916 /*
10917 * Can't easily make AS optional here, because VALUES in insert_rest would
10918 * have a shift/reduce conflict with VALUES as an optional alias. We could
10919 * easily allow unreserved_keywords as optional aliases, but that'd be an odd
10920 * divergence from other places. So just require AS for now.
10921 */
10922 insert_target:
10923 qualified_name
10924 {
10925 $$ = $1;
10926 }
10927 | qualified_name AS ColId
10928 {
10929 $1->alias = makeAlias($3, NIL);
10930 $$ = $1;
10931 }
10932 ;
10933
10934 insert_rest:
10935 SelectStmt
10936 {
10937 $$ = makeNode(InsertStmt);
10938 $$->cols = NIL;
10939 $$->selectStmt = $1;
10940 }
10941 | OVERRIDING override_kind VALUE_P SelectStmt
10942 {
10943 $$ = makeNode(InsertStmt);
10944 $$->cols = NIL;
10945 $$->override = $2;
10946 $$->selectStmt = $4;
10947 }
10948 | '(' insert_column_list ')' SelectStmt
10949 {
10950 $$ = makeNode(InsertStmt);
10951 $$->cols = $2;
10952 $$->selectStmt = $4;
10953 }
10954 | '(' insert_column_list ')' OVERRIDING override_kind VALUE_P SelectStmt
10955 {
10956 $$ = makeNode(InsertStmt);
10957 $$->cols = $2;
10958 $$->override = $5;
10959 $$->selectStmt = $7;
10960 }
10961 | DEFAULT VALUES
10962 {
10963 $$ = makeNode(InsertStmt);
10964 $$->cols = NIL;
10965 $$->selectStmt = NULL;
10966 }
10967 ;
10968
10969 override_kind:
10970 USER { $$ = OVERRIDING_USER_VALUE; }
10971 | SYSTEM_P { $$ = OVERRIDING_SYSTEM_VALUE; }
10972 ;
10973
10974 insert_column_list:
10975 insert_column_item
10976 { $$ = list_make1($1); }
10977 | insert_column_list ',' insert_column_item
10978 { $$ = lappend($1, $3); }
10979 ;
10980
10981 insert_column_item:
10982 ColId opt_indirection
10983 {
10984 $$ = makeNode(ResTarget);
10985 $$->name = $1;
10986 $$->indirection = check_indirection($2, yyscanner);
10987 $$->val = NULL;
10988 $$->location = @1;
10989 }
10990 ;
10991
10992 opt_on_conflict:
10993 ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list where_clause
10994 {
10995 $$ = makeNode(OnConflictClause);
10996 $$->action = ONCONFLICT_UPDATE;
10997 $$->infer = $3;
10998 $$->targetList = $7;
10999 $$->whereClause = $8;
11000 $$->location = @1;
11001 }
11002 |
11003 ON CONFLICT opt_conf_expr DO NOTHING
11004 {
11005 $$ = makeNode(OnConflictClause);
11006 $$->action = ONCONFLICT_NOTHING;
11007 $$->infer = $3;
11008 $$->targetList = NIL;
11009 $$->whereClause = NULL;
11010 $$->location = @1;
11011 }
11012 | /*EMPTY*/
11013 {
11014 $$ = NULL;
11015 }
11016 ;
11017
11018 opt_conf_expr:
11019 '(' index_params ')' where_clause
11020 {
11021 $$ = makeNode(InferClause);
11022 $$->indexElems = $2;
11023 $$->whereClause = $4;
11024 $$->conname = NULL;
11025 $$->location = @1;
11026 }
11027 |
11028 ON CONSTRAINT name
11029 {
11030 $$ = makeNode(InferClause);
11031 $$->indexElems = NIL;
11032 $$->whereClause = NULL;
11033 $$->conname = $3;
11034 $$->location = @1;
11035 }
11036 | /*EMPTY*/
11037 {
11038 $$ = NULL;
11039 }
11040 ;
11041
11042 returning_clause:
11043 RETURNING target_list { $$ = $2; }
11044 | /* EMPTY */ { $$ = NIL; }
11045 ;
11046
11047
11048 /*****************************************************************************
11049 *
11050 * QUERY:
11051 * DELETE STATEMENTS
11052 *
11053 *****************************************************************************/
11054
11055 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
11056 using_clause where_or_current_clause returning_clause
11057 {
11058 DeleteStmt *n = makeNode(DeleteStmt);
11059 n->relation = $4;
11060 n->usingClause = $5;
11061 n->whereClause = $6;
11062 n->returningList = $7;
11063 n->withClause = $1;
11064 $$ = (Node *)n;
11065 }
11066 ;
11067
11068 using_clause:
11069 USING from_list { $$ = $2; }
11070 | /*EMPTY*/ { $$ = NIL; }
11071 ;
11072
11073
11074 /*****************************************************************************
11075 *
11076 * QUERY:
11077 * LOCK TABLE
11078 *
11079 *****************************************************************************/
11080
11081 LockStmt: LOCK_P opt_table relation_expr_list opt_lock opt_nowait
11082 {
11083 LockStmt *n = makeNode(LockStmt);
11084
11085 n->relations = $3;
11086 n->mode = $4;
11087 n->nowait = $5;
11088 $$ = (Node *)n;
11089 }
11090 ;
11091
11092 opt_lock: IN_P lock_type MODE { $$ = $2; }
11093 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
11094 ;
11095
11096 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
11097 | ROW SHARE { $$ = RowShareLock; }
11098 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
11099 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
11100 | SHARE { $$ = ShareLock; }
11101 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
11102 | EXCLUSIVE { $$ = ExclusiveLock; }
11103 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
11104 ;
11105
11106 opt_nowait: NOWAIT { $$ = true; }
11107 | /*EMPTY*/ { $$ = false; }
11108 ;
11109
11110 opt_nowait_or_skip:
11111 NOWAIT { $$ = LockWaitError; }
11112 | SKIP LOCKED { $$ = LockWaitSkip; }
11113 | /*EMPTY*/ { $$ = LockWaitBlock; }
11114 ;
11115
11116
11117 /*****************************************************************************
11118 *
11119 * QUERY:
11120 * UpdateStmt (UPDATE)
11121 *
11122 *****************************************************************************/
11123
11124 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
11125 SET set_clause_list
11126 from_clause
11127 where_or_current_clause
11128 returning_clause
11129 {
11130 UpdateStmt *n = makeNode(UpdateStmt);
11131 n->relation = $3;
11132 n->targetList = $5;
11133 n->fromClause = $6;
11134 n->whereClause = $7;
11135 n->returningList = $8;
11136 n->withClause = $1;
11137 $$ = (Node *)n;
11138 }
11139 ;
11140 set_clause_list:
11141 set_clause { $$ = $1; }
11142 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
11143 ;
11144
11145 set_clause:
11146 set_target '=' a_expr
11147 {
11148 $1->val = (Node *) $3;
11149 $$ = list_make1($1);
11150 }
11151 | '(' set_target_list ')' '=' a_expr
11152 {
11153 int ncolumns = list_length($2);
11154 int i = 1;
11155 ListCell *col_cell;
11156
11157 /* Create a MultiAssignRef source for each target */
foreach(col_cell,$2)11158 foreach(col_cell, $2)
11159 {
11160 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
11161 MultiAssignRef *r = makeNode(MultiAssignRef);
11162
11163 r->source = (Node *) $5;
11164 r->colno = i;
11165 r->ncolumns = ncolumns;
11166 res_col->val = (Node *) r;
11167 i++;
11168 }
11169
11170 $$ = $2;
11171 }
11172 ;
11173
11174 set_target:
11175 ColId opt_indirection
11176 {
11177 $$ = makeNode(ResTarget);
11178 $$->name = $1;
11179 $$->indirection = check_indirection($2, yyscanner);
11180 $$->val = NULL; /* upper production sets this */
11181 $$->location = @1;
11182 }
11183 ;
11184
11185 set_target_list:
11186 set_target { $$ = list_make1($1); }
11187 | set_target_list ',' set_target { $$ = lappend($1,$3); }
11188 ;
11189
11190
11191 /*****************************************************************************
11192 *
11193 * QUERY:
11194 * CURSOR STATEMENTS
11195 *
11196 *****************************************************************************/
11197 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
11198 {
11199 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
11200 n->portalname = $2;
11201 /* currently we always set FAST_PLAN option */
11202 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
11203 n->query = $7;
11204 $$ = (Node *)n;
11205 }
11206 ;
11207
11208 cursor_name: name { $$ = $1; }
11209 ;
11210
11211 cursor_options: /*EMPTY*/ { $$ = 0; }
11212 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
11213 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
11214 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
11215 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
11216 ;
11217
11218 opt_hold: /* EMPTY */ { $$ = 0; }
11219 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
11220 | WITHOUT HOLD { $$ = 0; }
11221 ;
11222
11223 /*****************************************************************************
11224 *
11225 * QUERY:
11226 * SELECT STATEMENTS
11227 *
11228 *****************************************************************************/
11229
11230 /* A complete SELECT statement looks like this.
11231 *
11232 * The rule returns either a single SelectStmt node or a tree of them,
11233 * representing a set-operation tree.
11234 *
11235 * There is an ambiguity when a sub-SELECT is within an a_expr and there
11236 * are excess parentheses: do the parentheses belong to the sub-SELECT or
11237 * to the surrounding a_expr? We don't really care, but bison wants to know.
11238 * To resolve the ambiguity, we are careful to define the grammar so that
11239 * the decision is staved off as long as possible: as long as we can keep
11240 * absorbing parentheses into the sub-SELECT, we will do so, and only when
11241 * it's no longer possible to do that will we decide that parens belong to
11242 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
11243 * parentheses are treated as part of the sub-select. The necessity of doing
11244 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
11245 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
11246 * SELECT viewpoint when we see the UNION.
11247 *
11248 * This approach is implemented by defining a nonterminal select_with_parens,
11249 * which represents a SELECT with at least one outer layer of parentheses,
11250 * and being careful to use select_with_parens, never '(' SelectStmt ')',
11251 * in the expression grammar. We will then have shift-reduce conflicts
11252 * which we can resolve in favor of always treating '(' <select> ')' as
11253 * a select_with_parens. To resolve the conflicts, the productions that
11254 * conflict with the select_with_parens productions are manually given
11255 * precedences lower than the precedence of ')', thereby ensuring that we
11256 * shift ')' (and then reduce to select_with_parens) rather than trying to
11257 * reduce the inner <select> nonterminal to something else. We use UMINUS
11258 * precedence for this, which is a fairly arbitrary choice.
11259 *
11260 * To be able to define select_with_parens itself without ambiguity, we need
11261 * a nonterminal select_no_parens that represents a SELECT structure with no
11262 * outermost parentheses. This is a little bit tedious, but it works.
11263 *
11264 * In non-expression contexts, we use SelectStmt which can represent a SELECT
11265 * with or without outer parentheses.
11266 */
11267
11268 SelectStmt: select_no_parens %prec UMINUS
11269 | select_with_parens %prec UMINUS
11270 ;
11271
11272 select_with_parens:
11273 '(' select_no_parens ')' { $$ = $2; }
11274 | '(' select_with_parens ')' { $$ = $2; }
11275 ;
11276
11277 /*
11278 * This rule parses the equivalent of the standard's <query expression>.
11279 * The duplicative productions are annoying, but hard to get rid of without
11280 * creating shift/reduce conflicts.
11281 *
11282 * The locking clause (FOR UPDATE etc) may be before or after LIMIT/OFFSET.
11283 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
11284 * We now support both orderings, but prefer LIMIT/OFFSET before the locking
11285 * clause.
11286 * 2002-08-28 bjm
11287 */
11288 select_no_parens:
11289 simple_select { $$ = $1; }
11290 | select_clause sort_clause
11291 {
11292 insertSelectOptions((SelectStmt *) $1, $2, NIL,
11293 NULL, NULL, NULL,
11294 yyscanner);
11295 $$ = $1;
11296 }
11297 | select_clause opt_sort_clause for_locking_clause opt_select_limit
11298 {
11299 insertSelectOptions((SelectStmt *) $1, $2, $3,
11300 list_nth($4, 0), list_nth($4, 1),
11301 NULL,
11302 yyscanner);
11303 $$ = $1;
11304 }
11305 | select_clause opt_sort_clause select_limit opt_for_locking_clause
11306 {
11307 insertSelectOptions((SelectStmt *) $1, $2, $4,
11308 list_nth($3, 0), list_nth($3, 1),
11309 NULL,
11310 yyscanner);
11311 $$ = $1;
11312 }
11313 | with_clause select_clause
11314 {
11315 insertSelectOptions((SelectStmt *) $2, NULL, NIL,
11316 NULL, NULL,
11317 $1,
11318 yyscanner);
11319 $$ = $2;
11320 }
11321 | with_clause select_clause sort_clause
11322 {
11323 insertSelectOptions((SelectStmt *) $2, $3, NIL,
11324 NULL, NULL,
11325 $1,
11326 yyscanner);
11327 $$ = $2;
11328 }
11329 | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
11330 {
11331 insertSelectOptions((SelectStmt *) $2, $3, $4,
11332 list_nth($5, 0), list_nth($5, 1),
11333 $1,
11334 yyscanner);
11335 $$ = $2;
11336 }
11337 | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
11338 {
11339 insertSelectOptions((SelectStmt *) $2, $3, $5,
11340 list_nth($4, 0), list_nth($4, 1),
11341 $1,
11342 yyscanner);
11343 $$ = $2;
11344 }
11345 ;
11346
11347 select_clause:
11348 simple_select { $$ = $1; }
11349 | select_with_parens { $$ = $1; }
11350 ;
11351
11352 /*
11353 * This rule parses SELECT statements that can appear within set operations,
11354 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
11355 * the ordering of the set operations. Without '(' and ')' we want the
11356 * operations to be ordered per the precedence specs at the head of this file.
11357 *
11358 * As with select_no_parens, simple_select cannot have outer parentheses,
11359 * but can have parenthesized subclauses.
11360 *
11361 * Note that sort clauses cannot be included at this level --- SQL requires
11362 * SELECT foo UNION SELECT bar ORDER BY baz
11363 * to be parsed as
11364 * (SELECT foo UNION SELECT bar) ORDER BY baz
11365 * not
11366 * SELECT foo UNION (SELECT bar ORDER BY baz)
11367 * Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
11368 * described as part of the select_no_parens production, not simple_select.
11369 * This does not limit functionality, because you can reintroduce these
11370 * clauses inside parentheses.
11371 *
11372 * NOTE: only the leftmost component SelectStmt should have INTO.
11373 * However, this is not checked by the grammar; parse analysis must check it.
11374 */
11375 simple_select:
11376 SELECT opt_all_clause opt_target_list
11377 into_clause from_clause where_clause
11378 group_clause having_clause window_clause
11379 {
11380 SelectStmt *n = makeNode(SelectStmt);
11381 n->targetList = $3;
11382 n->intoClause = $4;
11383 n->fromClause = $5;
11384 n->whereClause = $6;
11385 n->groupClause = $7;
11386 n->havingClause = $8;
11387 n->windowClause = $9;
11388 $$ = (Node *)n;
11389 }
11390 | SELECT distinct_clause target_list
11391 into_clause from_clause where_clause
11392 group_clause having_clause window_clause
11393 {
11394 SelectStmt *n = makeNode(SelectStmt);
11395 n->distinctClause = $2;
11396 n->targetList = $3;
11397 n->intoClause = $4;
11398 n->fromClause = $5;
11399 n->whereClause = $6;
11400 n->groupClause = $7;
11401 n->havingClause = $8;
11402 n->windowClause = $9;
11403 $$ = (Node *)n;
11404 }
11405 | values_clause { $$ = $1; }
11406 | TABLE relation_expr
11407 {
11408 /* same as SELECT * FROM relation_expr */
11409 ColumnRef *cr = makeNode(ColumnRef);
11410 ResTarget *rt = makeNode(ResTarget);
11411 SelectStmt *n = makeNode(SelectStmt);
11412
11413 cr->fields = list_make1(makeNode(A_Star));
11414 cr->location = -1;
11415
11416 rt->name = NULL;
11417 rt->indirection = NIL;
11418 rt->val = (Node *)cr;
11419 rt->location = -1;
11420
11421 n->targetList = list_make1(rt);
11422 n->fromClause = list_make1($2);
11423 $$ = (Node *)n;
11424 }
11425 | select_clause UNION all_or_distinct select_clause
11426 {
11427 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
11428 }
11429 | select_clause INTERSECT all_or_distinct select_clause
11430 {
11431 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
11432 }
11433 | select_clause EXCEPT all_or_distinct select_clause
11434 {
11435 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
11436 }
11437 ;
11438
11439 /*
11440 * SQL standard WITH clause looks like:
11441 *
11442 * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
11443 * AS (query) [ SEARCH or CYCLE clause ]
11444 *
11445 * We don't currently support the SEARCH or CYCLE clause.
11446 *
11447 * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY.
11448 */
11449 with_clause:
11450 WITH cte_list
11451 {
11452 $$ = makeNode(WithClause);
11453 $$->ctes = $2;
11454 $$->recursive = false;
11455 $$->location = @1;
11456 }
11457 | WITH_LA cte_list
11458 {
11459 $$ = makeNode(WithClause);
11460 $$->ctes = $2;
11461 $$->recursive = false;
11462 $$->location = @1;
11463 }
11464 | WITH RECURSIVE cte_list
11465 {
11466 $$ = makeNode(WithClause);
11467 $$->ctes = $3;
11468 $$->recursive = true;
11469 $$->location = @1;
11470 }
11471 ;
11472
11473 cte_list:
11474 common_table_expr { $$ = list_make1($1); }
11475 | cte_list ',' common_table_expr { $$ = lappend($1, $3); }
11476 ;
11477
11478 common_table_expr: name opt_name_list AS opt_materialized '(' PreparableStmt ')'
11479 {
11480 CommonTableExpr *n = makeNode(CommonTableExpr);
11481 n->ctename = $1;
11482 n->aliascolnames = $2;
11483 n->ctematerialized = $4;
11484 n->ctequery = $6;
11485 n->location = @1;
11486 $$ = (Node *) n;
11487 }
11488 ;
11489
11490 opt_materialized:
11491 MATERIALIZED { $$ = CTEMaterializeAlways; }
11492 | NOT MATERIALIZED { $$ = CTEMaterializeNever; }
11493 | /*EMPTY*/ { $$ = CTEMaterializeDefault; }
11494 ;
11495
11496 opt_with_clause:
11497 with_clause { $$ = $1; }
11498 | /*EMPTY*/ { $$ = NULL; }
11499 ;
11500
11501 into_clause:
11502 INTO OptTempTableName
11503 {
11504 $$ = makeNode(IntoClause);
11505 $$->rel = $2;
11506 $$->colNames = NIL;
11507 $$->options = NIL;
11508 $$->onCommit = ONCOMMIT_NOOP;
11509 $$->tableSpaceName = NULL;
11510 $$->viewQuery = NULL;
11511 $$->skipData = false;
11512 }
11513 | /*EMPTY*/
11514 { $$ = NULL; }
11515 ;
11516
11517 /*
11518 * Redundancy here is needed to avoid shift/reduce conflicts,
11519 * since TEMP is not a reserved word. See also OptTemp.
11520 */
11521 OptTempTableName:
11522 TEMPORARY opt_table qualified_name
11523 {
11524 $$ = $3;
11525 $$->relpersistence = RELPERSISTENCE_TEMP;
11526 }
11527 | TEMP opt_table qualified_name
11528 {
11529 $$ = $3;
11530 $$->relpersistence = RELPERSISTENCE_TEMP;
11531 }
11532 | LOCAL TEMPORARY opt_table qualified_name
11533 {
11534 $$ = $4;
11535 $$->relpersistence = RELPERSISTENCE_TEMP;
11536 }
11537 | LOCAL TEMP opt_table qualified_name
11538 {
11539 $$ = $4;
11540 $$->relpersistence = RELPERSISTENCE_TEMP;
11541 }
11542 | GLOBAL TEMPORARY opt_table qualified_name
11543 {
11544 ereport(WARNING,
11545 (errmsg("GLOBAL is deprecated in temporary table creation"),
11546 parser_errposition(@1)));
11547 $$ = $4;
11548 $$->relpersistence = RELPERSISTENCE_TEMP;
11549 }
11550 | GLOBAL TEMP opt_table qualified_name
11551 {
11552 ereport(WARNING,
11553 (errmsg("GLOBAL is deprecated in temporary table creation"),
11554 parser_errposition(@1)));
11555 $$ = $4;
11556 $$->relpersistence = RELPERSISTENCE_TEMP;
11557 }
11558 | UNLOGGED opt_table qualified_name
11559 {
11560 $$ = $3;
11561 $$->relpersistence = RELPERSISTENCE_UNLOGGED;
11562 }
11563 | TABLE qualified_name
11564 {
11565 $$ = $2;
11566 $$->relpersistence = RELPERSISTENCE_PERMANENT;
11567 }
11568 | qualified_name
11569 {
11570 $$ = $1;
11571 $$->relpersistence = RELPERSISTENCE_PERMANENT;
11572 }
11573 ;
11574
11575 opt_table: TABLE {}
11576 | /*EMPTY*/ {}
11577 ;
11578
11579 all_or_distinct:
11580 ALL { $$ = true; }
11581 | DISTINCT { $$ = false; }
11582 | /*EMPTY*/ { $$ = false; }
11583 ;
11584
11585 /* We use (NIL) as a placeholder to indicate that all target expressions
11586 * should be placed in the DISTINCT list during parsetree analysis.
11587 */
11588 distinct_clause:
11589 DISTINCT { $$ = list_make1(NIL); }
11590 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
11591 ;
11592
11593 opt_all_clause:
11594 ALL { $$ = NIL;}
11595 | /*EMPTY*/ { $$ = NIL; }
11596 ;
11597
11598 opt_sort_clause:
11599 sort_clause { $$ = $1;}
11600 | /*EMPTY*/ { $$ = NIL; }
11601 ;
11602
11603 sort_clause:
11604 ORDER BY sortby_list { $$ = $3; }
11605 ;
11606
11607 sortby_list:
11608 sortby { $$ = list_make1($1); }
11609 | sortby_list ',' sortby { $$ = lappend($1, $3); }
11610 ;
11611
11612 sortby: a_expr USING qual_all_Op opt_nulls_order
11613 {
11614 $$ = makeNode(SortBy);
11615 $$->node = $1;
11616 $$->sortby_dir = SORTBY_USING;
11617 $$->sortby_nulls = $4;
11618 $$->useOp = $3;
11619 $$->location = @3;
11620 }
11621 | a_expr opt_asc_desc opt_nulls_order
11622 {
11623 $$ = makeNode(SortBy);
11624 $$->node = $1;
11625 $$->sortby_dir = $2;
11626 $$->sortby_nulls = $3;
11627 $$->useOp = NIL;
11628 $$->location = -1; /* no operator */
11629 }
11630 ;
11631
11632
11633 select_limit:
11634 limit_clause offset_clause { $$ = list_make2($2, $1); }
11635 | offset_clause limit_clause { $$ = list_make2($1, $2); }
11636 | limit_clause { $$ = list_make2(NULL, $1); }
11637 | offset_clause { $$ = list_make2($1, NULL); }
11638 ;
11639
11640 opt_select_limit:
11641 select_limit { $$ = $1; }
11642 | /* EMPTY */ { $$ = list_make2(NULL,NULL); }
11643 ;
11644
11645 limit_clause:
11646 LIMIT select_limit_value
11647 { $$ = $2; }
11648 | LIMIT select_limit_value ',' select_offset_value
11649 {
11650 /* Disabled because it was too confusing, bjm 2002-02-18 */
11651 ereport(ERROR,
11652 (errcode(ERRCODE_SYNTAX_ERROR),
11653 errmsg("LIMIT #,# syntax is not supported"),
11654 errhint("Use separate LIMIT and OFFSET clauses."),
11655 parser_errposition(@1)));
11656 }
11657 /* SQL:2008 syntax */
11658 /* to avoid shift/reduce conflicts, handle the optional value with
11659 * a separate production rather than an opt_ expression. The fact
11660 * that ONLY is fully reserved means that this way, we defer any
11661 * decision about what rule reduces ROW or ROWS to the point where
11662 * we can see the ONLY token in the lookahead slot.
11663 */
11664 | FETCH first_or_next select_fetch_first_value row_or_rows ONLY
11665 { $$ = $3; }
11666 | FETCH first_or_next row_or_rows ONLY
11667 { $$ = makeIntConst(1, -1); }
11668 ;
11669
11670 offset_clause:
11671 OFFSET select_offset_value
11672 { $$ = $2; }
11673 /* SQL:2008 syntax */
11674 | OFFSET select_fetch_first_value row_or_rows
11675 { $$ = $2; }
11676 ;
11677
11678 select_limit_value:
11679 a_expr { $$ = $1; }
11680 | ALL
11681 {
11682 /* LIMIT ALL is represented as a NULL constant */
11683 $$ = makeNullAConst(@1);
11684 }
11685 ;
11686
11687 select_offset_value:
11688 a_expr { $$ = $1; }
11689 ;
11690
11691 /*
11692 * Allowing full expressions without parentheses causes various parsing
11693 * problems with the trailing ROW/ROWS key words. SQL spec only calls for
11694 * <simple value specification>, which is either a literal or a parameter (but
11695 * an <SQL parameter reference> could be an identifier, bringing up conflicts
11696 * with ROW/ROWS). We solve this by leveraging the presence of ONLY (see above)
11697 * to determine whether the expression is missing rather than trying to make it
11698 * optional in this rule.
11699 *
11700 * c_expr covers almost all the spec-required cases (and more), but it doesn't
11701 * cover signed numeric literals, which are allowed by the spec. So we include
11702 * those here explicitly. We need FCONST as well as ICONST because values that
11703 * don't fit in the platform's "long", but do fit in bigint, should still be
11704 * accepted here. (This is possible in 64-bit Windows as well as all 32-bit
11705 * builds.)
11706 */
11707 select_fetch_first_value:
11708 c_expr { $$ = $1; }
11709 | '+' I_or_F_const
11710 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
11711 | '-' I_or_F_const
11712 { $$ = doNegate($2, @1); }
11713 ;
11714
11715 I_or_F_const:
11716 Iconst { $$ = makeIntConst($1,@1); }
11717 | FCONST { $$ = makeFloatConst($1,@1); }
11718 ;
11719
11720 /* noise words */
11721 row_or_rows: ROW { $$ = 0; }
11722 | ROWS { $$ = 0; }
11723 ;
11724
11725 first_or_next: FIRST_P { $$ = 0; }
11726 | NEXT { $$ = 0; }
11727 ;
11728
11729
11730 /*
11731 * This syntax for group_clause tries to follow the spec quite closely.
11732 * However, the spec allows only column references, not expressions,
11733 * which introduces an ambiguity between implicit row constructors
11734 * (a,b) and lists of column references.
11735 *
11736 * We handle this by using the a_expr production for what the spec calls
11737 * <ordinary grouping set>, which in the spec represents either one column
11738 * reference or a parenthesized list of column references. Then, we check the
11739 * top node of the a_expr to see if it's an implicit RowExpr, and if so, just
11740 * grab and use the list, discarding the node. (this is done in parse analysis,
11741 * not here)
11742 *
11743 * (we abuse the row_format field of RowExpr to distinguish implicit and
11744 * explicit row constructors; it's debatable if anyone sanely wants to use them
11745 * in a group clause, but if they have a reason to, we make it possible.)
11746 *
11747 * Each item in the group_clause list is either an expression tree or a
11748 * GroupingSet node of some type.
11749 */
11750 group_clause:
11751 GROUP_P BY group_by_list { $$ = $3; }
11752 | /*EMPTY*/ { $$ = NIL; }
11753 ;
11754
11755 group_by_list:
11756 group_by_item { $$ = list_make1($1); }
11757 | group_by_list ',' group_by_item { $$ = lappend($1,$3); }
11758 ;
11759
11760 group_by_item:
11761 a_expr { $$ = $1; }
11762 | empty_grouping_set { $$ = $1; }
11763 | cube_clause { $$ = $1; }
11764 | rollup_clause { $$ = $1; }
11765 | grouping_sets_clause { $$ = $1; }
11766 ;
11767
11768 empty_grouping_set:
11769 '(' ')'
11770 {
11771 $$ = (Node *) makeGroupingSet(GROUPING_SET_EMPTY, NIL, @1);
11772 }
11773 ;
11774
11775 /*
11776 * These hacks rely on setting precedence of CUBE and ROLLUP below that of '(',
11777 * so that they shift in these rules rather than reducing the conflicting
11778 * unreserved_keyword rule.
11779 */
11780
11781 rollup_clause:
11782 ROLLUP '(' expr_list ')'
11783 {
11784 $$ = (Node *) makeGroupingSet(GROUPING_SET_ROLLUP, $3, @1);
11785 }
11786 ;
11787
11788 cube_clause:
11789 CUBE '(' expr_list ')'
11790 {
11791 $$ = (Node *) makeGroupingSet(GROUPING_SET_CUBE, $3, @1);
11792 }
11793 ;
11794
11795 grouping_sets_clause:
11796 GROUPING SETS '(' group_by_list ')'
11797 {
11798 $$ = (Node *) makeGroupingSet(GROUPING_SET_SETS, $4, @1);
11799 }
11800 ;
11801
11802 having_clause:
11803 HAVING a_expr { $$ = $2; }
11804 | /*EMPTY*/ { $$ = NULL; }
11805 ;
11806
11807 for_locking_clause:
11808 for_locking_items { $$ = $1; }
11809 | FOR READ ONLY { $$ = NIL; }
11810 ;
11811
11812 opt_for_locking_clause:
11813 for_locking_clause { $$ = $1; }
11814 | /* EMPTY */ { $$ = NIL; }
11815 ;
11816
11817 for_locking_items:
11818 for_locking_item { $$ = list_make1($1); }
11819 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
11820 ;
11821
11822 for_locking_item:
11823 for_locking_strength locked_rels_list opt_nowait_or_skip
11824 {
11825 LockingClause *n = makeNode(LockingClause);
11826 n->lockedRels = $2;
11827 n->strength = $1;
11828 n->waitPolicy = $3;
11829 $$ = (Node *) n;
11830 }
11831 ;
11832
11833 for_locking_strength:
11834 FOR UPDATE { $$ = LCS_FORUPDATE; }
11835 | FOR NO KEY UPDATE { $$ = LCS_FORNOKEYUPDATE; }
11836 | FOR SHARE { $$ = LCS_FORSHARE; }
11837 | FOR KEY SHARE { $$ = LCS_FORKEYSHARE; }
11838 ;
11839
11840 locked_rels_list:
11841 OF qualified_name_list { $$ = $2; }
11842 | /* EMPTY */ { $$ = NIL; }
11843 ;
11844
11845
11846 /*
11847 * We should allow ROW '(' expr_list ')' too, but that seems to require
11848 * making VALUES a fully reserved word, which will probably break more apps
11849 * than allowing the noise-word is worth.
11850 */
11851 values_clause:
11852 VALUES '(' expr_list ')'
11853 {
11854 SelectStmt *n = makeNode(SelectStmt);
11855 n->valuesLists = list_make1($3);
11856 $$ = (Node *) n;
11857 }
11858 | values_clause ',' '(' expr_list ')'
11859 {
11860 SelectStmt *n = (SelectStmt *) $1;
11861 n->valuesLists = lappend(n->valuesLists, $4);
11862 $$ = (Node *) n;
11863 }
11864 ;
11865
11866
11867 /*****************************************************************************
11868 *
11869 * clauses common to all Optimizable Stmts:
11870 * from_clause - allow list of both JOIN expressions and table names
11871 * where_clause - qualifications for joins or restrictions
11872 *
11873 *****************************************************************************/
11874
11875 from_clause:
11876 FROM from_list { $$ = $2; }
11877 | /*EMPTY*/ { $$ = NIL; }
11878 ;
11879
11880 from_list:
11881 table_ref { $$ = list_make1($1); }
11882 | from_list ',' table_ref { $$ = lappend($1, $3); }
11883 ;
11884
11885 /*
11886 * table_ref is where an alias clause can be attached.
11887 */
11888 table_ref: relation_expr opt_alias_clause
11889 {
11890 $1->alias = $2;
11891 $$ = (Node *) $1;
11892 }
11893 | relation_expr opt_alias_clause tablesample_clause
11894 {
11895 RangeTableSample *n = (RangeTableSample *) $3;
11896 $1->alias = $2;
11897 /* relation_expr goes inside the RangeTableSample node */
11898 n->relation = (Node *) $1;
11899 $$ = (Node *) n;
11900 }
11901 | func_table func_alias_clause
11902 {
11903 RangeFunction *n = (RangeFunction *) $1;
11904 n->alias = linitial($2);
11905 n->coldeflist = lsecond($2);
11906 $$ = (Node *) n;
11907 }
11908 | LATERAL_P func_table func_alias_clause
11909 {
11910 RangeFunction *n = (RangeFunction *) $2;
11911 n->lateral = true;
11912 n->alias = linitial($3);
11913 n->coldeflist = lsecond($3);
11914 $$ = (Node *) n;
11915 }
11916 | xmltable opt_alias_clause
11917 {
11918 RangeTableFunc *n = (RangeTableFunc *) $1;
11919 n->alias = $2;
11920 $$ = (Node *) n;
11921 }
11922 | LATERAL_P xmltable opt_alias_clause
11923 {
11924 RangeTableFunc *n = (RangeTableFunc *) $2;
11925 n->lateral = true;
11926 n->alias = $3;
11927 $$ = (Node *) n;
11928 }
11929 | select_with_parens opt_alias_clause
11930 {
11931 RangeSubselect *n = makeNode(RangeSubselect);
11932 n->lateral = false;
11933 n->subquery = $1;
11934 n->alias = $2;
11935 /*
11936 * The SQL spec does not permit a subselect
11937 * (<derived_table>) without an alias clause,
11938 * so we don't either. This avoids the problem
11939 * of needing to invent a unique refname for it.
11940 * That could be surmounted if there's sufficient
11941 * popular demand, but for now let's just implement
11942 * the spec and see if anyone complains.
11943 * However, it does seem like a good idea to emit
11944 * an error message that's better than "syntax error".
11945 */
11946 if ($2 == NULL)
11947 {
11948 if (IsA($1, SelectStmt) &&
11949 ((SelectStmt *) $1)->valuesLists)
11950 ereport(ERROR,
11951 (errcode(ERRCODE_SYNTAX_ERROR),
11952 errmsg("VALUES in FROM must have an alias"),
11953 errhint("For example, FROM (VALUES ...) [AS] foo."),
11954 parser_errposition(@1)));
11955 else
11956 ereport(ERROR,
11957 (errcode(ERRCODE_SYNTAX_ERROR),
11958 errmsg("subquery in FROM must have an alias"),
11959 errhint("For example, FROM (SELECT ...) [AS] foo."),
11960 parser_errposition(@1)));
11961 }
11962 $$ = (Node *) n;
11963 }
11964 | LATERAL_P select_with_parens opt_alias_clause
11965 {
11966 RangeSubselect *n = makeNode(RangeSubselect);
11967 n->lateral = true;
11968 n->subquery = $2;
11969 n->alias = $3;
11970 /* same comment as above */
11971 if ($3 == NULL)
11972 {
11973 if (IsA($2, SelectStmt) &&
11974 ((SelectStmt *) $2)->valuesLists)
11975 ereport(ERROR,
11976 (errcode(ERRCODE_SYNTAX_ERROR),
11977 errmsg("VALUES in FROM must have an alias"),
11978 errhint("For example, FROM (VALUES ...) [AS] foo."),
11979 parser_errposition(@2)));
11980 else
11981 ereport(ERROR,
11982 (errcode(ERRCODE_SYNTAX_ERROR),
11983 errmsg("subquery in FROM must have an alias"),
11984 errhint("For example, FROM (SELECT ...) [AS] foo."),
11985 parser_errposition(@2)));
11986 }
11987 $$ = (Node *) n;
11988 }
11989 | joined_table
11990 {
11991 $$ = (Node *) $1;
11992 }
11993 | '(' joined_table ')' alias_clause
11994 {
11995 $2->alias = $4;
11996 $$ = (Node *) $2;
11997 }
11998 ;
11999
12000
12001 /*
12002 * It may seem silly to separate joined_table from table_ref, but there is
12003 * method in SQL's madness: if you don't do it this way you get reduce-
12004 * reduce conflicts, because it's not clear to the parser generator whether
12005 * to expect alias_clause after ')' or not. For the same reason we must
12006 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
12007 * join_type to expand to empty; if we try it, the parser generator can't
12008 * figure out when to reduce an empty join_type right after table_ref.
12009 *
12010 * Note that a CROSS JOIN is the same as an unqualified
12011 * INNER JOIN, and an INNER JOIN/ON has the same shape
12012 * but a qualification expression to limit membership.
12013 * A NATURAL JOIN implicitly matches column names between
12014 * tables and the shape is determined by which columns are
12015 * in common. We'll collect columns during the later transformations.
12016 */
12017
12018 joined_table:
12019 '(' joined_table ')'
12020 {
12021 $$ = $2;
12022 }
12023 | table_ref CROSS JOIN table_ref
12024 {
12025 /* CROSS JOIN is same as unqualified inner join */
12026 JoinExpr *n = makeNode(JoinExpr);
12027 n->jointype = JOIN_INNER;
12028 n->isNatural = false;
12029 n->larg = $1;
12030 n->rarg = $4;
12031 n->usingClause = NIL;
12032 n->quals = NULL;
12033 $$ = n;
12034 }
12035 | table_ref join_type JOIN table_ref join_qual
12036 {
12037 JoinExpr *n = makeNode(JoinExpr);
12038 n->jointype = $2;
12039 n->isNatural = false;
12040 n->larg = $1;
12041 n->rarg = $4;
12042 if ($5 != NULL && IsA($5, List))
12043 n->usingClause = (List *) $5; /* USING clause */
12044 else
12045 n->quals = $5; /* ON clause */
12046 $$ = n;
12047 }
12048 | table_ref JOIN table_ref join_qual
12049 {
12050 /* letting join_type reduce to empty doesn't work */
12051 JoinExpr *n = makeNode(JoinExpr);
12052 n->jointype = JOIN_INNER;
12053 n->isNatural = false;
12054 n->larg = $1;
12055 n->rarg = $3;
12056 if ($4 != NULL && IsA($4, List))
12057 n->usingClause = (List *) $4; /* USING clause */
12058 else
12059 n->quals = $4; /* ON clause */
12060 $$ = n;
12061 }
12062 | table_ref NATURAL join_type JOIN table_ref
12063 {
12064 JoinExpr *n = makeNode(JoinExpr);
12065 n->jointype = $3;
12066 n->isNatural = true;
12067 n->larg = $1;
12068 n->rarg = $5;
12069 n->usingClause = NIL; /* figure out which columns later... */
12070 n->quals = NULL; /* fill later */
12071 $$ = n;
12072 }
12073 | table_ref NATURAL JOIN table_ref
12074 {
12075 /* letting join_type reduce to empty doesn't work */
12076 JoinExpr *n = makeNode(JoinExpr);
12077 n->jointype = JOIN_INNER;
12078 n->isNatural = true;
12079 n->larg = $1;
12080 n->rarg = $4;
12081 n->usingClause = NIL; /* figure out which columns later... */
12082 n->quals = NULL; /* fill later */
12083 $$ = n;
12084 }
12085 ;
12086
12087 alias_clause:
12088 AS ColId '(' name_list ')'
12089 {
12090 $$ = makeNode(Alias);
12091 $$->aliasname = $2;
12092 $$->colnames = $4;
12093 }
12094 | AS ColId
12095 {
12096 $$ = makeNode(Alias);
12097 $$->aliasname = $2;
12098 }
12099 | ColId '(' name_list ')'
12100 {
12101 $$ = makeNode(Alias);
12102 $$->aliasname = $1;
12103 $$->colnames = $3;
12104 }
12105 | ColId
12106 {
12107 $$ = makeNode(Alias);
12108 $$->aliasname = $1;
12109 }
12110 ;
12111
12112 opt_alias_clause: alias_clause { $$ = $1; }
12113 | /*EMPTY*/ { $$ = NULL; }
12114 ;
12115
12116 /*
12117 * func_alias_clause can include both an Alias and a coldeflist, so we make it
12118 * return a 2-element list that gets disassembled by calling production.
12119 */
12120 func_alias_clause:
12121 alias_clause
12122 {
12123 $$ = list_make2($1, NIL);
12124 }
12125 | AS '(' TableFuncElementList ')'
12126 {
12127 $$ = list_make2(NULL, $3);
12128 }
12129 | AS ColId '(' TableFuncElementList ')'
12130 {
12131 Alias *a = makeNode(Alias);
12132 a->aliasname = $2;
12133 $$ = list_make2(a, $4);
12134 }
12135 | ColId '(' TableFuncElementList ')'
12136 {
12137 Alias *a = makeNode(Alias);
12138 a->aliasname = $1;
12139 $$ = list_make2(a, $3);
12140 }
12141 | /*EMPTY*/
12142 {
12143 $$ = list_make2(NULL, NIL);
12144 }
12145 ;
12146
12147 join_type: FULL join_outer { $$ = JOIN_FULL; }
12148 | LEFT join_outer { $$ = JOIN_LEFT; }
12149 | RIGHT join_outer { $$ = JOIN_RIGHT; }
12150 | INNER_P { $$ = JOIN_INNER; }
12151 ;
12152
12153 /* OUTER is just noise... */
12154 join_outer: OUTER_P { $$ = NULL; }
12155 | /*EMPTY*/ { $$ = NULL; }
12156 ;
12157
12158 /* JOIN qualification clauses
12159 * Possibilities are:
12160 * USING ( column list ) allows only unqualified column names,
12161 * which must match between tables.
12162 * ON expr allows more general qualifications.
12163 *
12164 * We return USING as a List node, while an ON-expr will not be a List.
12165 */
12166
12167 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
12168 | ON a_expr { $$ = $2; }
12169 ;
12170
12171
12172 relation_expr:
12173 qualified_name
12174 {
12175 /* inheritance query, implicitly */
12176 $$ = $1;
12177 $$->inh = true;
12178 $$->alias = NULL;
12179 }
12180 | qualified_name '*'
12181 {
12182 /* inheritance query, explicitly */
12183 $$ = $1;
12184 $$->inh = true;
12185 $$->alias = NULL;
12186 }
12187 | ONLY qualified_name
12188 {
12189 /* no inheritance */
12190 $$ = $2;
12191 $$->inh = false;
12192 $$->alias = NULL;
12193 }
12194 | ONLY '(' qualified_name ')'
12195 {
12196 /* no inheritance, SQL99-style syntax */
12197 $$ = $3;
12198 $$->inh = false;
12199 $$->alias = NULL;
12200 }
12201 ;
12202
12203
12204 relation_expr_list:
12205 relation_expr { $$ = list_make1($1); }
12206 | relation_expr_list ',' relation_expr { $$ = lappend($1, $3); }
12207 ;
12208
12209
12210 /*
12211 * Given "UPDATE foo set set ...", we have to decide without looking any
12212 * further ahead whether the first "set" is an alias or the UPDATE's SET
12213 * keyword. Since "set" is allowed as a column name both interpretations
12214 * are feasible. We resolve the shift/reduce conflict by giving the first
12215 * relation_expr_opt_alias production a higher precedence than the SET token
12216 * has, causing the parser to prefer to reduce, in effect assuming that the
12217 * SET is not an alias.
12218 */
12219 relation_expr_opt_alias: relation_expr %prec UMINUS
12220 {
12221 $$ = $1;
12222 }
12223 | relation_expr ColId
12224 {
12225 Alias *alias = makeNode(Alias);
12226 alias->aliasname = $2;
12227 $1->alias = alias;
12228 $$ = $1;
12229 }
12230 | relation_expr AS ColId
12231 {
12232 Alias *alias = makeNode(Alias);
12233 alias->aliasname = $3;
12234 $1->alias = alias;
12235 $$ = $1;
12236 }
12237 ;
12238
12239 /*
12240 * TABLESAMPLE decoration in a FROM item
12241 */
12242 tablesample_clause:
12243 TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause
12244 {
12245 RangeTableSample *n = makeNode(RangeTableSample);
12246 /* n->relation will be filled in later */
12247 n->method = $2;
12248 n->args = $4;
12249 n->repeatable = $6;
12250 n->location = @2;
12251 $$ = (Node *) n;
12252 }
12253 ;
12254
12255 opt_repeatable_clause:
12256 REPEATABLE '(' a_expr ')' { $$ = (Node *) $3; }
12257 | /*EMPTY*/ { $$ = NULL; }
12258 ;
12259
12260 /*
12261 * func_table represents a function invocation in a FROM list. It can be
12262 * a plain function call, like "foo(...)", or a ROWS FROM expression with
12263 * one or more function calls, "ROWS FROM (foo(...), bar(...))",
12264 * optionally with WITH ORDINALITY attached.
12265 * In the ROWS FROM syntax, a column definition list can be given for each
12266 * function, for example:
12267 * ROWS FROM (foo() AS (foo_res_a text, foo_res_b text),
12268 * bar() AS (bar_res_a text, bar_res_b text))
12269 * It's also possible to attach a column definition list to the RangeFunction
12270 * as a whole, but that's handled by the table_ref production.
12271 */
12272 func_table: func_expr_windowless opt_ordinality
12273 {
12274 RangeFunction *n = makeNode(RangeFunction);
12275 n->lateral = false;
12276 n->ordinality = $2;
12277 n->is_rowsfrom = false;
12278 n->functions = list_make1(list_make2($1, NIL));
12279 /* alias and coldeflist are set by table_ref production */
12280 $$ = (Node *) n;
12281 }
12282 | ROWS FROM '(' rowsfrom_list ')' opt_ordinality
12283 {
12284 RangeFunction *n = makeNode(RangeFunction);
12285 n->lateral = false;
12286 n->ordinality = $6;
12287 n->is_rowsfrom = true;
12288 n->functions = $4;
12289 /* alias and coldeflist are set by table_ref production */
12290 $$ = (Node *) n;
12291 }
12292 ;
12293
12294 rowsfrom_item: func_expr_windowless opt_col_def_list
12295 { $$ = list_make2($1, $2); }
12296 ;
12297
12298 rowsfrom_list:
12299 rowsfrom_item { $$ = list_make1($1); }
12300 | rowsfrom_list ',' rowsfrom_item { $$ = lappend($1, $3); }
12301 ;
12302
12303 opt_col_def_list: AS '(' TableFuncElementList ')' { $$ = $3; }
12304 | /*EMPTY*/ { $$ = NIL; }
12305 ;
12306
12307 opt_ordinality: WITH_LA ORDINALITY { $$ = true; }
12308 | /*EMPTY*/ { $$ = false; }
12309 ;
12310
12311
12312 where_clause:
12313 WHERE a_expr { $$ = $2; }
12314 | /*EMPTY*/ { $$ = NULL; }
12315 ;
12316
12317 /* variant for UPDATE and DELETE */
12318 where_or_current_clause:
12319 WHERE a_expr { $$ = $2; }
12320 | WHERE CURRENT_P OF cursor_name
12321 {
12322 CurrentOfExpr *n = makeNode(CurrentOfExpr);
12323 /* cvarno is filled in by parse analysis */
12324 n->cursor_name = $4;
12325 n->cursor_param = 0;
12326 $$ = (Node *) n;
12327 }
12328 | /*EMPTY*/ { $$ = NULL; }
12329 ;
12330
12331
12332 OptTableFuncElementList:
12333 TableFuncElementList { $$ = $1; }
12334 | /*EMPTY*/ { $$ = NIL; }
12335 ;
12336
12337 TableFuncElementList:
12338 TableFuncElement
12339 {
12340 $$ = list_make1($1);
12341 }
12342 | TableFuncElementList ',' TableFuncElement
12343 {
12344 $$ = lappend($1, $3);
12345 }
12346 ;
12347
12348 TableFuncElement: ColId Typename opt_collate_clause
12349 {
12350 ColumnDef *n = makeNode(ColumnDef);
12351 n->colname = $1;
12352 n->typeName = $2;
12353 n->inhcount = 0;
12354 n->is_local = true;
12355 n->is_not_null = false;
12356 n->is_from_type = false;
12357 n->storage = 0;
12358 n->raw_default = NULL;
12359 n->cooked_default = NULL;
12360 n->collClause = (CollateClause *) $3;
12361 n->collOid = InvalidOid;
12362 n->constraints = NIL;
12363 n->location = @1;
12364 $$ = (Node *)n;
12365 }
12366 ;
12367
12368 /*
12369 * XMLTABLE
12370 */
12371 xmltable:
12372 XMLTABLE '(' c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12373 {
12374 RangeTableFunc *n = makeNode(RangeTableFunc);
12375 n->rowexpr = $3;
12376 n->docexpr = $4;
12377 n->columns = $6;
12378 n->namespaces = NIL;
12379 n->location = @1;
12380 $$ = (Node *)n;
12381 }
12382 | XMLTABLE '(' XMLNAMESPACES '(' xml_namespace_list ')' ','
12383 c_expr xmlexists_argument COLUMNS xmltable_column_list ')'
12384 {
12385 RangeTableFunc *n = makeNode(RangeTableFunc);
12386 n->rowexpr = $8;
12387 n->docexpr = $9;
12388 n->columns = $11;
12389 n->namespaces = $5;
12390 n->location = @1;
12391 $$ = (Node *)n;
12392 }
12393 ;
12394
12395 xmltable_column_list: xmltable_column_el { $$ = list_make1($1); }
12396 | xmltable_column_list ',' xmltable_column_el { $$ = lappend($1, $3); }
12397 ;
12398
12399 xmltable_column_el:
12400 ColId Typename
12401 {
12402 RangeTableFuncCol *fc = makeNode(RangeTableFuncCol);
12403
12404 fc->colname = $1;
12405 fc->for_ordinality = false;
12406 fc->typeName = $2;
12407 fc->is_not_null = false;
12408 fc->colexpr = NULL;
12409 fc->coldefexpr = NULL;
12410 fc->location = @1;
12411
12412 $$ = (Node *) fc;
12413 }
12414 | ColId Typename xmltable_column_option_list
12415 {
12416 RangeTableFuncCol *fc = makeNode(RangeTableFuncCol);
12417 ListCell *option;
12418 bool nullability_seen = false;
12419
12420 fc->colname = $1;
12421 fc->typeName = $2;
12422 fc->for_ordinality = false;
12423 fc->is_not_null = false;
12424 fc->colexpr = NULL;
12425 fc->coldefexpr = NULL;
12426 fc->location = @1;
12427
foreach(option,$3)12428 foreach(option, $3)
12429 {
12430 DefElem *defel = (DefElem *) lfirst(option);
12431
12432 if (strcmp(defel->defname, "default") == 0)
12433 {
12434 if (fc->coldefexpr != NULL)
12435 ereport(ERROR,
12436 (errcode(ERRCODE_SYNTAX_ERROR),
12437 errmsg("only one DEFAULT value is allowed"),
12438 parser_errposition(defel->location)));
12439 fc->coldefexpr = defel->arg;
12440 }
12441 else if (strcmp(defel->defname, "path") == 0)
12442 {
12443 if (fc->colexpr != NULL)
12444 ereport(ERROR,
12445 (errcode(ERRCODE_SYNTAX_ERROR),
12446 errmsg("only one PATH value per column is allowed"),
12447 parser_errposition(defel->location)));
12448 fc->colexpr = defel->arg;
12449 }
12450 else if (strcmp(defel->defname, "is_not_null") == 0)
12451 {
12452 if (nullability_seen)
12453 ereport(ERROR,
12454 (errcode(ERRCODE_SYNTAX_ERROR),
12455 errmsg("conflicting or redundant NULL / NOT NULL declarations for column \"%s\"", fc->colname),
12456 parser_errposition(defel->location)));
12457 fc->is_not_null = intVal(defel->arg);
12458 nullability_seen = true;
12459 }
12460 else
12461 {
12462 ereport(ERROR,
12463 (errcode(ERRCODE_SYNTAX_ERROR),
12464 errmsg("unrecognized column option \"%s\"",
12465 defel->defname),
12466 parser_errposition(defel->location)));
12467 }
12468 }
12469 $$ = (Node *) fc;
12470 }
12471 | ColId FOR ORDINALITY
12472 {
12473 RangeTableFuncCol *fc = makeNode(RangeTableFuncCol);
12474
12475 fc->colname = $1;
12476 fc->for_ordinality = true;
12477 /* other fields are ignored, initialized by makeNode */
12478 fc->location = @1;
12479
12480 $$ = (Node *) fc;
12481 }
12482 ;
12483
12484 xmltable_column_option_list:
12485 xmltable_column_option_el
12486 { $$ = list_make1($1); }
12487 | xmltable_column_option_list xmltable_column_option_el
12488 { $$ = lappend($1, $2); }
12489 ;
12490
12491 xmltable_column_option_el:
12492 IDENT b_expr
12493 { $$ = makeDefElem($1, $2, @1); }
12494 | DEFAULT b_expr
12495 { $$ = makeDefElem("default", $2, @1); }
12496 | NOT NULL_P
12497 { $$ = makeDefElem("is_not_null", (Node *) makeInteger(true), @1); }
12498 | NULL_P
12499 { $$ = makeDefElem("is_not_null", (Node *) makeInteger(false), @1); }
12500 ;
12501
12502 xml_namespace_list:
12503 xml_namespace_el
12504 { $$ = list_make1($1); }
12505 | xml_namespace_list ',' xml_namespace_el
12506 { $$ = lappend($1, $3); }
12507 ;
12508
12509 xml_namespace_el:
12510 b_expr AS ColLabel
12511 {
12512 $$ = makeNode(ResTarget);
12513 $$->name = $3;
12514 $$->indirection = NIL;
12515 $$->val = $1;
12516 $$->location = @1;
12517 }
12518 | DEFAULT b_expr
12519 {
12520 $$ = makeNode(ResTarget);
12521 $$->name = NULL;
12522 $$->indirection = NIL;
12523 $$->val = $2;
12524 $$->location = @1;
12525 }
12526 ;
12527
12528 /*****************************************************************************
12529 *
12530 * Type syntax
12531 * SQL introduces a large amount of type-specific syntax.
12532 * Define individual clauses to handle these cases, and use
12533 * the generic case to handle regular type-extensible Postgres syntax.
12534 * - thomas 1997-10-10
12535 *
12536 *****************************************************************************/
12537
12538 Typename: SimpleTypename opt_array_bounds
12539 {
12540 $$ = $1;
12541 $$->arrayBounds = $2;
12542 }
12543 | SETOF SimpleTypename opt_array_bounds
12544 {
12545 $$ = $2;
12546 $$->arrayBounds = $3;
12547 $$->setof = true;
12548 }
12549 /* SQL standard syntax, currently only one-dimensional */
12550 | SimpleTypename ARRAY '[' Iconst ']'
12551 {
12552 $$ = $1;
12553 $$->arrayBounds = list_make1(makeInteger($4));
12554 }
12555 | SETOF SimpleTypename ARRAY '[' Iconst ']'
12556 {
12557 $$ = $2;
12558 $$->arrayBounds = list_make1(makeInteger($5));
12559 $$->setof = true;
12560 }
12561 | SimpleTypename ARRAY
12562 {
12563 $$ = $1;
12564 $$->arrayBounds = list_make1(makeInteger(-1));
12565 }
12566 | SETOF SimpleTypename ARRAY
12567 {
12568 $$ = $2;
12569 $$->arrayBounds = list_make1(makeInteger(-1));
12570 $$->setof = true;
12571 }
12572 ;
12573
12574 opt_array_bounds:
12575 opt_array_bounds '[' ']'
12576 { $$ = lappend($1, makeInteger(-1)); }
12577 | opt_array_bounds '[' Iconst ']'
12578 { $$ = lappend($1, makeInteger($3)); }
12579 | /*EMPTY*/
12580 { $$ = NIL; }
12581 ;
12582
12583 SimpleTypename:
12584 GenericType { $$ = $1; }
12585 | Numeric { $$ = $1; }
12586 | Bit { $$ = $1; }
12587 | Character { $$ = $1; }
12588 | ConstDatetime { $$ = $1; }
12589 | ConstInterval opt_interval
12590 {
12591 $$ = $1;
12592 $$->typmods = $2;
12593 }
12594 | ConstInterval '(' Iconst ')'
12595 {
12596 $$ = $1;
12597 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
12598 makeIntConst($3, @3));
12599 }
12600 ;
12601
12602 /* We have a separate ConstTypename to allow defaulting fixed-length
12603 * types such as CHAR() and BIT() to an unspecified length.
12604 * SQL9x requires that these default to a length of one, but this
12605 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
12606 * where there is an obvious better choice to make.
12607 * Note that ConstInterval is not included here since it must
12608 * be pushed up higher in the rules to accommodate the postfix
12609 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
12610 * the generic-type-name case in AExprConst to avoid premature
12611 * reduce/reduce conflicts against function names.
12612 */
12613 ConstTypename:
12614 Numeric { $$ = $1; }
12615 | ConstBit { $$ = $1; }
12616 | ConstCharacter { $$ = $1; }
12617 | ConstDatetime { $$ = $1; }
12618 ;
12619
12620 /*
12621 * GenericType covers all type names that don't have special syntax mandated
12622 * by the standard, including qualified names. We also allow type modifiers.
12623 * To avoid parsing conflicts against function invocations, the modifiers
12624 * have to be shown as expr_list here, but parse analysis will only accept
12625 * constants for them.
12626 */
12627 GenericType:
12628 type_function_name opt_type_modifiers
12629 {
12630 $$ = makeTypeName($1);
12631 $$->typmods = $2;
12632 $$->location = @1;
12633 }
12634 | type_function_name attrs opt_type_modifiers
12635 {
12636 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
12637 $$->typmods = $3;
12638 $$->location = @1;
12639 }
12640 ;
12641
12642 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
12643 | /* EMPTY */ { $$ = NIL; }
12644 ;
12645
12646 /*
12647 * SQL numeric data types
12648 */
12649 Numeric: INT_P
12650 {
12651 $$ = SystemTypeName("int4");
12652 $$->location = @1;
12653 }
12654 | INTEGER
12655 {
12656 $$ = SystemTypeName("int4");
12657 $$->location = @1;
12658 }
12659 | SMALLINT
12660 {
12661 $$ = SystemTypeName("int2");
12662 $$->location = @1;
12663 }
12664 | BIGINT
12665 {
12666 $$ = SystemTypeName("int8");
12667 $$->location = @1;
12668 }
12669 | REAL
12670 {
12671 $$ = SystemTypeName("float4");
12672 $$->location = @1;
12673 }
12674 | FLOAT_P opt_float
12675 {
12676 $$ = $2;
12677 $$->location = @1;
12678 }
12679 | DOUBLE_P PRECISION
12680 {
12681 $$ = SystemTypeName("float8");
12682 $$->location = @1;
12683 }
12684 | DECIMAL_P opt_type_modifiers
12685 {
12686 $$ = SystemTypeName("numeric");
12687 $$->typmods = $2;
12688 $$->location = @1;
12689 }
12690 | DEC opt_type_modifiers
12691 {
12692 $$ = SystemTypeName("numeric");
12693 $$->typmods = $2;
12694 $$->location = @1;
12695 }
12696 | NUMERIC opt_type_modifiers
12697 {
12698 $$ = SystemTypeName("numeric");
12699 $$->typmods = $2;
12700 $$->location = @1;
12701 }
12702 | BOOLEAN_P
12703 {
12704 $$ = SystemTypeName("bool");
12705 $$->location = @1;
12706 }
12707 ;
12708
12709 opt_float: '(' Iconst ')'
12710 {
12711 /*
12712 * Check FLOAT() precision limits assuming IEEE floating
12713 * types - thomas 1997-09-18
12714 */
12715 if ($2 < 1)
12716 ereport(ERROR,
12717 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12718 errmsg("precision for type float must be at least 1 bit"),
12719 parser_errposition(@2)));
12720 else if ($2 <= 24)
12721 $$ = SystemTypeName("float4");
12722 else if ($2 <= 53)
12723 $$ = SystemTypeName("float8");
12724 else
12725 ereport(ERROR,
12726 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
12727 errmsg("precision for type float must be less than 54 bits"),
12728 parser_errposition(@2)));
12729 }
12730 | /*EMPTY*/
12731 {
12732 $$ = SystemTypeName("float8");
12733 }
12734 ;
12735
12736 /*
12737 * SQL bit-field data types
12738 * The following implements BIT() and BIT VARYING().
12739 */
12740 Bit: BitWithLength
12741 {
12742 $$ = $1;
12743 }
12744 | BitWithoutLength
12745 {
12746 $$ = $1;
12747 }
12748 ;
12749
12750 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
12751 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
12752 ConstBit: BitWithLength
12753 {
12754 $$ = $1;
12755 }
12756 | BitWithoutLength
12757 {
12758 $$ = $1;
12759 $$->typmods = NIL;
12760 }
12761 ;
12762
12763 BitWithLength:
12764 BIT opt_varying '(' expr_list ')'
12765 {
12766 char *typname;
12767
12768 typname = $2 ? "varbit" : "bit";
12769 $$ = SystemTypeName(typname);
12770 $$->typmods = $4;
12771 $$->location = @1;
12772 }
12773 ;
12774
12775 BitWithoutLength:
12776 BIT opt_varying
12777 {
12778 /* bit defaults to bit(1), varbit to no limit */
12779 if ($2)
12780 {
12781 $$ = SystemTypeName("varbit");
12782 }
12783 else
12784 {
12785 $$ = SystemTypeName("bit");
12786 $$->typmods = list_make1(makeIntConst(1, -1));
12787 }
12788 $$->location = @1;
12789 }
12790 ;
12791
12792
12793 /*
12794 * SQL character data types
12795 * The following implements CHAR() and VARCHAR().
12796 */
12797 Character: CharacterWithLength
12798 {
12799 $$ = $1;
12800 }
12801 | CharacterWithoutLength
12802 {
12803 $$ = $1;
12804 }
12805 ;
12806
12807 ConstCharacter: CharacterWithLength
12808 {
12809 $$ = $1;
12810 }
12811 | CharacterWithoutLength
12812 {
12813 /* Length was not specified so allow to be unrestricted.
12814 * This handles problems with fixed-length (bpchar) strings
12815 * which in column definitions must default to a length
12816 * of one, but should not be constrained if the length
12817 * was not specified.
12818 */
12819 $$ = $1;
12820 $$->typmods = NIL;
12821 }
12822 ;
12823
12824 CharacterWithLength: character '(' Iconst ')'
12825 {
12826 $$ = SystemTypeName($1);
12827 $$->typmods = list_make1(makeIntConst($3, @3));
12828 $$->location = @1;
12829 }
12830 ;
12831
12832 CharacterWithoutLength: character
12833 {
12834 $$ = SystemTypeName($1);
12835 /* char defaults to char(1), varchar to no limit */
12836 if (strcmp($1, "bpchar") == 0)
12837 $$->typmods = list_make1(makeIntConst(1, -1));
12838 $$->location = @1;
12839 }
12840 ;
12841
12842 character: CHARACTER opt_varying
12843 { $$ = $2 ? "varchar": "bpchar"; }
12844 | CHAR_P opt_varying
12845 { $$ = $2 ? "varchar": "bpchar"; }
12846 | VARCHAR
12847 { $$ = "varchar"; }
12848 | NATIONAL CHARACTER opt_varying
12849 { $$ = $3 ? "varchar": "bpchar"; }
12850 | NATIONAL CHAR_P opt_varying
12851 { $$ = $3 ? "varchar": "bpchar"; }
12852 | NCHAR opt_varying
12853 { $$ = $2 ? "varchar": "bpchar"; }
12854 ;
12855
12856 opt_varying:
12857 VARYING { $$ = true; }
12858 | /*EMPTY*/ { $$ = false; }
12859 ;
12860
12861 /*
12862 * SQL date/time types
12863 */
12864 ConstDatetime:
12865 TIMESTAMP '(' Iconst ')' opt_timezone
12866 {
12867 if ($5)
12868 $$ = SystemTypeName("timestamptz");
12869 else
12870 $$ = SystemTypeName("timestamp");
12871 $$->typmods = list_make1(makeIntConst($3, @3));
12872 $$->location = @1;
12873 }
12874 | TIMESTAMP opt_timezone
12875 {
12876 if ($2)
12877 $$ = SystemTypeName("timestamptz");
12878 else
12879 $$ = SystemTypeName("timestamp");
12880 $$->location = @1;
12881 }
12882 | TIME '(' Iconst ')' opt_timezone
12883 {
12884 if ($5)
12885 $$ = SystemTypeName("timetz");
12886 else
12887 $$ = SystemTypeName("time");
12888 $$->typmods = list_make1(makeIntConst($3, @3));
12889 $$->location = @1;
12890 }
12891 | TIME opt_timezone
12892 {
12893 if ($2)
12894 $$ = SystemTypeName("timetz");
12895 else
12896 $$ = SystemTypeName("time");
12897 $$->location = @1;
12898 }
12899 ;
12900
12901 ConstInterval:
12902 INTERVAL
12903 {
12904 $$ = SystemTypeName("interval");
12905 $$->location = @1;
12906 }
12907 ;
12908
12909 opt_timezone:
12910 WITH_LA TIME ZONE { $$ = true; }
12911 | WITHOUT TIME ZONE { $$ = false; }
12912 | /*EMPTY*/ { $$ = false; }
12913 ;
12914
12915 opt_interval:
12916 YEAR_P
12917 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
12918 | MONTH_P
12919 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
12920 | DAY_P
12921 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
12922 | HOUR_P
12923 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
12924 | MINUTE_P
12925 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
12926 | interval_second
12927 { $$ = $1; }
12928 | YEAR_P TO MONTH_P
12929 {
12930 $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
12931 INTERVAL_MASK(MONTH), @1));
12932 }
12933 | DAY_P TO HOUR_P
12934 {
12935 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12936 INTERVAL_MASK(HOUR), @1));
12937 }
12938 | DAY_P TO MINUTE_P
12939 {
12940 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
12941 INTERVAL_MASK(HOUR) |
12942 INTERVAL_MASK(MINUTE), @1));
12943 }
12944 | DAY_P TO interval_second
12945 {
12946 $$ = $3;
12947 linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
12948 INTERVAL_MASK(HOUR) |
12949 INTERVAL_MASK(MINUTE) |
12950 INTERVAL_MASK(SECOND), @1);
12951 }
12952 | HOUR_P TO MINUTE_P
12953 {
12954 $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
12955 INTERVAL_MASK(MINUTE), @1));
12956 }
12957 | HOUR_P TO interval_second
12958 {
12959 $$ = $3;
12960 linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
12961 INTERVAL_MASK(MINUTE) |
12962 INTERVAL_MASK(SECOND), @1);
12963 }
12964 | MINUTE_P TO interval_second
12965 {
12966 $$ = $3;
12967 linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
12968 INTERVAL_MASK(SECOND), @1);
12969 }
12970 | /*EMPTY*/
12971 { $$ = NIL; }
12972 ;
12973
12974 interval_second:
12975 SECOND_P
12976 {
12977 $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
12978 }
12979 | SECOND_P '(' Iconst ')'
12980 {
12981 $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
12982 makeIntConst($3, @3));
12983 }
12984 ;
12985
12986
12987 /*****************************************************************************
12988 *
12989 * expression grammar
12990 *
12991 *****************************************************************************/
12992
12993 /*
12994 * General expressions
12995 * This is the heart of the expression syntax.
12996 *
12997 * We have two expression types: a_expr is the unrestricted kind, and
12998 * b_expr is a subset that must be used in some places to avoid shift/reduce
12999 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
13000 * because that use of AND conflicts with AND as a boolean operator. So,
13001 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
13002 *
13003 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
13004 * always be used by surrounding it with parens.
13005 *
13006 * c_expr is all the productions that are common to a_expr and b_expr;
13007 * it's factored out just to eliminate redundant coding.
13008 *
13009 * Be careful of productions involving more than one terminal token.
13010 * By default, bison will assign such productions the precedence of their
13011 * last terminal, but in nearly all cases you want it to be the precedence
13012 * of the first terminal instead; otherwise you will not get the behavior
13013 * you expect! So we use %prec annotations freely to set precedences.
13014 */
13015 a_expr: c_expr { $$ = $1; }
13016 | a_expr TYPECAST Typename
13017 { $$ = makeTypeCast($1, $3, @2); }
13018 | a_expr COLLATE any_name
13019 {
13020 CollateClause *n = makeNode(CollateClause);
13021 n->arg = $1;
13022 n->collname = $3;
13023 n->location = @2;
13024 $$ = (Node *) n;
13025 }
13026 | a_expr AT TIME ZONE a_expr %prec AT
13027 {
13028 $$ = (Node *) makeFuncCall(SystemFuncName("timezone"),
13029 list_make2($5, $1),
13030 @2);
13031 }
13032 /*
13033 * These operators must be called out explicitly in order to make use
13034 * of bison's automatic operator-precedence handling. All other
13035 * operator names are handled by the generic productions using "Op",
13036 * below; and all those operators will have the same precedence.
13037 *
13038 * If you add more explicitly-known operators, be sure to add them
13039 * also to b_expr and to the MathOp list below.
13040 */
13041 | '+' a_expr %prec UMINUS
13042 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13043 | '-' a_expr %prec UMINUS
13044 { $$ = doNegate($2, @1); }
13045 | a_expr '+' a_expr
13046 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13047 | a_expr '-' a_expr
13048 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13049 | a_expr '*' a_expr
13050 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13051 | a_expr '/' a_expr
13052 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13053 | a_expr '%' a_expr
13054 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13055 | a_expr '^' a_expr
13056 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13057 | a_expr '<' a_expr
13058 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13059 | a_expr '>' a_expr
13060 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13061 | a_expr '=' a_expr
13062 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13063 | a_expr LESS_EQUALS a_expr
13064 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13065 | a_expr GREATER_EQUALS a_expr
13066 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13067 | a_expr NOT_EQUALS a_expr
13068 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13069
13070 | a_expr qual_Op a_expr %prec Op
13071 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13072 | qual_Op a_expr %prec Op
13073 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13074 | a_expr qual_Op %prec POSTFIXOP
13075 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13076
13077 | a_expr AND a_expr
13078 { $$ = makeAndExpr($1, $3, @2); }
13079 | a_expr OR a_expr
13080 { $$ = makeOrExpr($1, $3, @2); }
13081 | NOT a_expr
13082 { $$ = makeNotExpr($2, @1); }
13083 | NOT_LA a_expr %prec NOT
13084 { $$ = makeNotExpr($2, @1); }
13085
13086 | a_expr LIKE a_expr
13087 {
13088 $$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13089 $1, $3, @2);
13090 }
13091 | a_expr LIKE a_expr ESCAPE a_expr %prec LIKE
13092 {
13093 FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13094 list_make2($3, $5),
13095 @2);
13096 $$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "~~",
13097 $1, (Node *) n, @2);
13098 }
13099 | a_expr NOT_LA LIKE a_expr %prec NOT_LA
13100 {
13101 $$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13102 $1, $4, @2);
13103 }
13104 | a_expr NOT_LA LIKE a_expr ESCAPE a_expr %prec NOT_LA
13105 {
13106 FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13107 list_make2($4, $6),
13108 @2);
13109 $$ = (Node *) makeSimpleA_Expr(AEXPR_LIKE, "!~~",
13110 $1, (Node *) n, @2);
13111 }
13112 | a_expr ILIKE a_expr
13113 {
13114 $$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13115 $1, $3, @2);
13116 }
13117 | a_expr ILIKE a_expr ESCAPE a_expr %prec ILIKE
13118 {
13119 FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13120 list_make2($3, $5),
13121 @2);
13122 $$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "~~*",
13123 $1, (Node *) n, @2);
13124 }
13125 | a_expr NOT_LA ILIKE a_expr %prec NOT_LA
13126 {
13127 $$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13128 $1, $4, @2);
13129 }
13130 | a_expr NOT_LA ILIKE a_expr ESCAPE a_expr %prec NOT_LA
13131 {
13132 FuncCall *n = makeFuncCall(SystemFuncName("like_escape"),
13133 list_make2($4, $6),
13134 @2);
13135 $$ = (Node *) makeSimpleA_Expr(AEXPR_ILIKE, "!~~*",
13136 $1, (Node *) n, @2);
13137 }
13138
13139 | a_expr SIMILAR TO a_expr %prec SIMILAR
13140 {
13141 FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13142 list_make2($4, makeNullAConst(-1)),
13143 @2);
13144 $$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13145 $1, (Node *) n, @2);
13146 }
13147 | a_expr SIMILAR TO a_expr ESCAPE a_expr %prec SIMILAR
13148 {
13149 FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13150 list_make2($4, $6),
13151 @2);
13152 $$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "~",
13153 $1, (Node *) n, @2);
13154 }
13155 | a_expr NOT_LA SIMILAR TO a_expr %prec NOT_LA
13156 {
13157 FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13158 list_make2($5, makeNullAConst(-1)),
13159 @2);
13160 $$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13161 $1, (Node *) n, @2);
13162 }
13163 | a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr %prec NOT_LA
13164 {
13165 FuncCall *n = makeFuncCall(SystemFuncName("similar_escape"),
13166 list_make2($5, $7),
13167 @2);
13168 $$ = (Node *) makeSimpleA_Expr(AEXPR_SIMILAR, "!~",
13169 $1, (Node *) n, @2);
13170 }
13171
13172 /* NullTest clause
13173 * Define SQL-style Null test clause.
13174 * Allow two forms described in the standard:
13175 * a IS NULL
13176 * a IS NOT NULL
13177 * Allow two SQL extensions
13178 * a ISNULL
13179 * a NOTNULL
13180 */
13181 | a_expr IS NULL_P %prec IS
13182 {
13183 NullTest *n = makeNode(NullTest);
13184 n->arg = (Expr *) $1;
13185 n->nulltesttype = IS_NULL;
13186 n->location = @2;
13187 $$ = (Node *)n;
13188 }
13189 | a_expr ISNULL
13190 {
13191 NullTest *n = makeNode(NullTest);
13192 n->arg = (Expr *) $1;
13193 n->nulltesttype = IS_NULL;
13194 n->location = @2;
13195 $$ = (Node *)n;
13196 }
13197 | a_expr IS NOT NULL_P %prec IS
13198 {
13199 NullTest *n = makeNode(NullTest);
13200 n->arg = (Expr *) $1;
13201 n->nulltesttype = IS_NOT_NULL;
13202 n->location = @2;
13203 $$ = (Node *)n;
13204 }
13205 | a_expr NOTNULL
13206 {
13207 NullTest *n = makeNode(NullTest);
13208 n->arg = (Expr *) $1;
13209 n->nulltesttype = IS_NOT_NULL;
13210 n->location = @2;
13211 $$ = (Node *)n;
13212 }
13213 | row OVERLAPS row
13214 {
13215 if (list_length($1) != 2)
13216 ereport(ERROR,
13217 (errcode(ERRCODE_SYNTAX_ERROR),
13218 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
13219 parser_errposition(@1)));
13220 if (list_length($3) != 2)
13221 ereport(ERROR,
13222 (errcode(ERRCODE_SYNTAX_ERROR),
13223 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
13224 parser_errposition(@3)));
13225 $$ = (Node *) makeFuncCall(SystemFuncName("overlaps"),
13226 list_concat($1, $3),
13227 @2);
13228 }
13229 | a_expr IS TRUE_P %prec IS
13230 {
13231 BooleanTest *b = makeNode(BooleanTest);
13232 b->arg = (Expr *) $1;
13233 b->booltesttype = IS_TRUE;
13234 b->location = @2;
13235 $$ = (Node *)b;
13236 }
13237 | a_expr IS NOT TRUE_P %prec IS
13238 {
13239 BooleanTest *b = makeNode(BooleanTest);
13240 b->arg = (Expr *) $1;
13241 b->booltesttype = IS_NOT_TRUE;
13242 b->location = @2;
13243 $$ = (Node *)b;
13244 }
13245 | a_expr IS FALSE_P %prec IS
13246 {
13247 BooleanTest *b = makeNode(BooleanTest);
13248 b->arg = (Expr *) $1;
13249 b->booltesttype = IS_FALSE;
13250 b->location = @2;
13251 $$ = (Node *)b;
13252 }
13253 | a_expr IS NOT FALSE_P %prec IS
13254 {
13255 BooleanTest *b = makeNode(BooleanTest);
13256 b->arg = (Expr *) $1;
13257 b->booltesttype = IS_NOT_FALSE;
13258 b->location = @2;
13259 $$ = (Node *)b;
13260 }
13261 | a_expr IS UNKNOWN %prec IS
13262 {
13263 BooleanTest *b = makeNode(BooleanTest);
13264 b->arg = (Expr *) $1;
13265 b->booltesttype = IS_UNKNOWN;
13266 b->location = @2;
13267 $$ = (Node *)b;
13268 }
13269 | a_expr IS NOT UNKNOWN %prec IS
13270 {
13271 BooleanTest *b = makeNode(BooleanTest);
13272 b->arg = (Expr *) $1;
13273 b->booltesttype = IS_NOT_UNKNOWN;
13274 b->location = @2;
13275 $$ = (Node *)b;
13276 }
13277 | a_expr IS DISTINCT FROM a_expr %prec IS
13278 {
13279 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13280 }
13281 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
13282 {
13283 $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13284 }
13285 | a_expr IS OF '(' type_list ')' %prec IS
13286 {
13287 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13288 }
13289 | a_expr IS NOT OF '(' type_list ')' %prec IS
13290 {
13291 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13292 }
13293 | a_expr BETWEEN opt_asymmetric b_expr AND a_expr %prec BETWEEN
13294 {
13295 $$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN,
13296 "BETWEEN",
13297 $1,
13298 (Node *) list_make2($4, $6),
13299 @2);
13300 }
13301 | a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr %prec NOT_LA
13302 {
13303 $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN,
13304 "NOT BETWEEN",
13305 $1,
13306 (Node *) list_make2($5, $7),
13307 @2);
13308 }
13309 | a_expr BETWEEN SYMMETRIC b_expr AND a_expr %prec BETWEEN
13310 {
13311 $$ = (Node *) makeSimpleA_Expr(AEXPR_BETWEEN_SYM,
13312 "BETWEEN SYMMETRIC",
13313 $1,
13314 (Node *) list_make2($4, $6),
13315 @2);
13316 }
13317 | a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr %prec NOT_LA
13318 {
13319 $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_BETWEEN_SYM,
13320 "NOT BETWEEN SYMMETRIC",
13321 $1,
13322 (Node *) list_make2($5, $7),
13323 @2);
13324 }
13325 | a_expr IN_P in_expr
13326 {
13327 /* in_expr returns a SubLink or a list of a_exprs */
13328 if (IsA($3, SubLink))
13329 {
13330 /* generate foo = ANY (subquery) */
13331 SubLink *n = (SubLink *) $3;
13332 n->subLinkType = ANY_SUBLINK;
13333 n->subLinkId = 0;
13334 n->testexpr = $1;
13335 n->operName = NIL; /* show it's IN not = ANY */
13336 n->location = @2;
13337 $$ = (Node *)n;
13338 }
13339 else
13340 {
13341 /* generate scalar IN expression */
13342 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
13343 }
13344 }
13345 | a_expr NOT_LA IN_P in_expr %prec NOT_LA
13346 {
13347 /* in_expr returns a SubLink or a list of a_exprs */
13348 if (IsA($4, SubLink))
13349 {
13350 /* generate NOT (foo = ANY (subquery)) */
13351 /* Make an = ANY node */
13352 SubLink *n = (SubLink *) $4;
13353 n->subLinkType = ANY_SUBLINK;
13354 n->subLinkId = 0;
13355 n->testexpr = $1;
13356 n->operName = NIL; /* show it's IN not = ANY */
13357 n->location = @2;
13358 /* Stick a NOT on top; must have same parse location */
13359 $$ = makeNotExpr((Node *) n, @2);
13360 }
13361 else
13362 {
13363 /* generate scalar NOT IN expression */
13364 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
13365 }
13366 }
13367 | a_expr subquery_Op sub_type select_with_parens %prec Op
13368 {
13369 SubLink *n = makeNode(SubLink);
13370 n->subLinkType = $3;
13371 n->subLinkId = 0;
13372 n->testexpr = $1;
13373 n->operName = $2;
13374 n->subselect = $4;
13375 n->location = @2;
13376 $$ = (Node *)n;
13377 }
13378 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
13379 {
13380 if ($3 == ANY_SUBLINK)
13381 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
13382 else
13383 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
13384 }
13385 | UNIQUE select_with_parens
13386 {
13387 /* Not sure how to get rid of the parentheses
13388 * but there are lots of shift/reduce errors without them.
13389 *
13390 * Should be able to implement this by plopping the entire
13391 * select into a node, then transforming the target expressions
13392 * from whatever they are into count(*), and testing the
13393 * entire result equal to one.
13394 * But, will probably implement a separate node in the executor.
13395 */
13396 ereport(ERROR,
13397 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13398 errmsg("UNIQUE predicate is not yet implemented"),
13399 parser_errposition(@1)));
13400 }
13401 | a_expr IS DOCUMENT_P %prec IS
13402 {
13403 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13404 list_make1($1), @2);
13405 }
13406 | a_expr IS NOT DOCUMENT_P %prec IS
13407 {
13408 $$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13409 list_make1($1), @2),
13410 @2);
13411 }
13412 | DEFAULT
13413 {
13414 /*
13415 * The SQL spec only allows DEFAULT in "contextually typed
13416 * expressions", but for us, it's easier to allow it in
13417 * any a_expr and then throw error during parse analysis
13418 * if it's in an inappropriate context. This way also
13419 * lets us say something smarter than "syntax error".
13420 */
13421 SetToDefault *n = makeNode(SetToDefault);
13422 /* parse analysis will fill in the rest */
13423 n->location = @1;
13424 $$ = (Node *)n;
13425 }
13426 ;
13427
13428 /*
13429 * Restricted expressions
13430 *
13431 * b_expr is a subset of the complete expression syntax defined by a_expr.
13432 *
13433 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
13434 * cause trouble in the places where b_expr is used. For simplicity, we
13435 * just eliminate all the boolean-keyword-operator productions from b_expr.
13436 */
13437 b_expr: c_expr
13438 { $$ = $1; }
13439 | b_expr TYPECAST Typename
13440 { $$ = makeTypeCast($1, $3, @2); }
13441 | '+' b_expr %prec UMINUS
13442 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
13443 | '-' b_expr %prec UMINUS
13444 { $$ = doNegate($2, @1); }
13445 | b_expr '+' b_expr
13446 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
13447 | b_expr '-' b_expr
13448 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
13449 | b_expr '*' b_expr
13450 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
13451 | b_expr '/' b_expr
13452 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
13453 | b_expr '%' b_expr
13454 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
13455 | b_expr '^' b_expr
13456 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
13457 | b_expr '<' b_expr
13458 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
13459 | b_expr '>' b_expr
13460 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
13461 | b_expr '=' b_expr
13462 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
13463 | b_expr LESS_EQUALS b_expr
13464 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $3, @2); }
13465 | b_expr GREATER_EQUALS b_expr
13466 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3, @2); }
13467 | b_expr NOT_EQUALS b_expr
13468 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, $3, @2); }
13469 | b_expr qual_Op b_expr %prec Op
13470 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
13471 | qual_Op b_expr %prec Op
13472 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
13473 | b_expr qual_Op %prec POSTFIXOP
13474 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
13475 | b_expr IS DISTINCT FROM b_expr %prec IS
13476 {
13477 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
13478 }
13479 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
13480 {
13481 $$ = (Node *) makeSimpleA_Expr(AEXPR_NOT_DISTINCT, "=", $1, $6, @2);
13482 }
13483 | b_expr IS OF '(' type_list ')' %prec IS
13484 {
13485 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
13486 }
13487 | b_expr IS NOT OF '(' type_list ')' %prec IS
13488 {
13489 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
13490 }
13491 | b_expr IS DOCUMENT_P %prec IS
13492 {
13493 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13494 list_make1($1), @2);
13495 }
13496 | b_expr IS NOT DOCUMENT_P %prec IS
13497 {
13498 $$ = makeNotExpr(makeXmlExpr(IS_DOCUMENT, NULL, NIL,
13499 list_make1($1), @2),
13500 @2);
13501 }
13502 ;
13503
13504 /*
13505 * Productions that can be used in both a_expr and b_expr.
13506 *
13507 * Note: productions that refer recursively to a_expr or b_expr mostly
13508 * cannot appear here. However, it's OK to refer to a_exprs that occur
13509 * inside parentheses, such as function arguments; that cannot introduce
13510 * ambiguity to the b_expr syntax.
13511 */
13512 c_expr: columnref { $$ = $1; }
13513 | AexprConst { $$ = $1; }
13514 | PARAM opt_indirection
13515 {
13516 ParamRef *p = makeNode(ParamRef);
13517 p->number = $1;
13518 p->location = @1;
13519 if ($2)
13520 {
13521 A_Indirection *n = makeNode(A_Indirection);
13522 n->arg = (Node *) p;
13523 n->indirection = check_indirection($2, yyscanner);
13524 $$ = (Node *) n;
13525 }
13526 else
13527 $$ = (Node *) p;
13528 }
13529 | '(' a_expr ')' opt_indirection
13530 {
13531 if ($4)
13532 {
13533 A_Indirection *n = makeNode(A_Indirection);
13534 n->arg = $2;
13535 n->indirection = check_indirection($4, yyscanner);
13536 $$ = (Node *)n;
13537 }
13538 else if (operator_precedence_warning)
13539 {
13540 /*
13541 * If precedence warnings are enabled, insert
13542 * AEXPR_PAREN nodes wrapping all explicitly
13543 * parenthesized subexpressions; this prevents bogus
13544 * warnings from being issued when the ordering has
13545 * been forced by parentheses. Take care that an
13546 * AEXPR_PAREN node has the same exprLocation as its
13547 * child, so as not to cause surprising changes in
13548 * error cursor positioning.
13549 *
13550 * In principle we should not be relying on a GUC to
13551 * decide whether to insert AEXPR_PAREN nodes.
13552 * However, since they have no effect except to
13553 * suppress warnings, it's probably safe enough; and
13554 * we'd just as soon not waste cycles on dummy parse
13555 * nodes if we don't have to.
13556 */
13557 $$ = (Node *) makeA_Expr(AEXPR_PAREN, NIL, $2, NULL,
13558 exprLocation($2));
13559 }
13560 else
13561 $$ = $2;
13562 }
13563 | case_expr
13564 { $$ = $1; }
13565 | func_expr
13566 { $$ = $1; }
13567 | select_with_parens %prec UMINUS
13568 {
13569 SubLink *n = makeNode(SubLink);
13570 n->subLinkType = EXPR_SUBLINK;
13571 n->subLinkId = 0;
13572 n->testexpr = NULL;
13573 n->operName = NIL;
13574 n->subselect = $1;
13575 n->location = @1;
13576 $$ = (Node *)n;
13577 }
13578 | select_with_parens indirection
13579 {
13580 /*
13581 * Because the select_with_parens nonterminal is designed
13582 * to "eat" as many levels of parens as possible, the
13583 * '(' a_expr ')' opt_indirection production above will
13584 * fail to match a sub-SELECT with indirection decoration;
13585 * the sub-SELECT won't be regarded as an a_expr as long
13586 * as there are parens around it. To support applying
13587 * subscripting or field selection to a sub-SELECT result,
13588 * we need this redundant-looking production.
13589 */
13590 SubLink *n = makeNode(SubLink);
13591 A_Indirection *a = makeNode(A_Indirection);
13592 n->subLinkType = EXPR_SUBLINK;
13593 n->subLinkId = 0;
13594 n->testexpr = NULL;
13595 n->operName = NIL;
13596 n->subselect = $1;
13597 n->location = @1;
13598 a->arg = (Node *)n;
13599 a->indirection = check_indirection($2, yyscanner);
13600 $$ = (Node *)a;
13601 }
13602 | EXISTS select_with_parens
13603 {
13604 SubLink *n = makeNode(SubLink);
13605 n->subLinkType = EXISTS_SUBLINK;
13606 n->subLinkId = 0;
13607 n->testexpr = NULL;
13608 n->operName = NIL;
13609 n->subselect = $2;
13610 n->location = @1;
13611 $$ = (Node *)n;
13612 }
13613 | ARRAY select_with_parens
13614 {
13615 SubLink *n = makeNode(SubLink);
13616 n->subLinkType = ARRAY_SUBLINK;
13617 n->subLinkId = 0;
13618 n->testexpr = NULL;
13619 n->operName = NIL;
13620 n->subselect = $2;
13621 n->location = @1;
13622 $$ = (Node *)n;
13623 }
13624 | ARRAY array_expr
13625 {
13626 A_ArrayExpr *n = castNode(A_ArrayExpr, $2);
13627 /* point outermost A_ArrayExpr to the ARRAY keyword */
13628 n->location = @1;
13629 $$ = (Node *)n;
13630 }
13631 | explicit_row
13632 {
13633 RowExpr *r = makeNode(RowExpr);
13634 r->args = $1;
13635 r->row_typeid = InvalidOid; /* not analyzed yet */
13636 r->colnames = NIL; /* to be filled in during analysis */
13637 r->row_format = COERCE_EXPLICIT_CALL; /* abuse */
13638 r->location = @1;
13639 $$ = (Node *)r;
13640 }
13641 | implicit_row
13642 {
13643 RowExpr *r = makeNode(RowExpr);
13644 r->args = $1;
13645 r->row_typeid = InvalidOid; /* not analyzed yet */
13646 r->colnames = NIL; /* to be filled in during analysis */
13647 r->row_format = COERCE_IMPLICIT_CAST; /* abuse */
13648 r->location = @1;
13649 $$ = (Node *)r;
13650 }
13651 | GROUPING '(' expr_list ')'
13652 {
13653 GroupingFunc *g = makeNode(GroupingFunc);
13654 g->args = $3;
13655 g->location = @1;
13656 $$ = (Node *)g;
13657 }
13658 ;
13659
13660 func_application: func_name '(' ')'
13661 {
13662 $$ = (Node *) makeFuncCall($1, NIL, @1);
13663 }
13664 | func_name '(' func_arg_list opt_sort_clause ')'
13665 {
13666 FuncCall *n = makeFuncCall($1, $3, @1);
13667 n->agg_order = $4;
13668 $$ = (Node *)n;
13669 }
13670 | func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'
13671 {
13672 FuncCall *n = makeFuncCall($1, list_make1($4), @1);
13673 n->func_variadic = true;
13674 n->agg_order = $5;
13675 $$ = (Node *)n;
13676 }
13677 | func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'
13678 {
13679 FuncCall *n = makeFuncCall($1, lappend($3, $6), @1);
13680 n->func_variadic = true;
13681 n->agg_order = $7;
13682 $$ = (Node *)n;
13683 }
13684 | func_name '(' ALL func_arg_list opt_sort_clause ')'
13685 {
13686 FuncCall *n = makeFuncCall($1, $4, @1);
13687 n->agg_order = $5;
13688 /* Ideally we'd mark the FuncCall node to indicate
13689 * "must be an aggregate", but there's no provision
13690 * for that in FuncCall at the moment.
13691 */
13692 $$ = (Node *)n;
13693 }
13694 | func_name '(' DISTINCT func_arg_list opt_sort_clause ')'
13695 {
13696 FuncCall *n = makeFuncCall($1, $4, @1);
13697 n->agg_order = $5;
13698 n->agg_distinct = true;
13699 $$ = (Node *)n;
13700 }
13701 | func_name '(' '*' ')'
13702 {
13703 /*
13704 * We consider AGGREGATE(*) to invoke a parameterless
13705 * aggregate. This does the right thing for COUNT(*),
13706 * and there are no other aggregates in SQL that accept
13707 * '*' as parameter.
13708 *
13709 * The FuncCall node is also marked agg_star = true,
13710 * so that later processing can detect what the argument
13711 * really was.
13712 */
13713 FuncCall *n = makeFuncCall($1, NIL, @1);
13714 n->agg_star = true;
13715 $$ = (Node *)n;
13716 }
13717 ;
13718
13719
13720 /*
13721 * func_expr and its cousin func_expr_windowless are split out from c_expr just
13722 * so that we have classifications for "everything that is a function call or
13723 * looks like one". This isn't very important, but it saves us having to
13724 * document which variants are legal in places like "FROM function()" or the
13725 * backwards-compatible functional-index syntax for CREATE INDEX.
13726 * (Note that many of the special SQL functions wouldn't actually make any
13727 * sense as functional index entries, but we ignore that consideration here.)
13728 */
13729 func_expr: func_application within_group_clause filter_clause over_clause
13730 {
13731 FuncCall *n = (FuncCall *) $1;
13732 /*
13733 * The order clause for WITHIN GROUP and the one for
13734 * plain-aggregate ORDER BY share a field, so we have to
13735 * check here that at most one is present. We also check
13736 * for DISTINCT and VARIADIC here to give a better error
13737 * location. Other consistency checks are deferred to
13738 * parse analysis.
13739 */
13740 if ($2 != NIL)
13741 {
13742 if (n->agg_order != NIL)
13743 ereport(ERROR,
13744 (errcode(ERRCODE_SYNTAX_ERROR),
13745 errmsg("cannot use multiple ORDER BY clauses with WITHIN GROUP"),
13746 parser_errposition(@2)));
13747 if (n->agg_distinct)
13748 ereport(ERROR,
13749 (errcode(ERRCODE_SYNTAX_ERROR),
13750 errmsg("cannot use DISTINCT with WITHIN GROUP"),
13751 parser_errposition(@2)));
13752 if (n->func_variadic)
13753 ereport(ERROR,
13754 (errcode(ERRCODE_SYNTAX_ERROR),
13755 errmsg("cannot use VARIADIC with WITHIN GROUP"),
13756 parser_errposition(@2)));
13757 n->agg_order = $2;
13758 n->agg_within_group = true;
13759 }
13760 n->agg_filter = $3;
13761 n->over = $4;
13762 $$ = (Node *) n;
13763 }
13764 | func_expr_common_subexpr
13765 { $$ = $1; }
13766 ;
13767
13768 /*
13769 * As func_expr but does not accept WINDOW functions directly
13770 * (but they can still be contained in arguments for functions etc).
13771 * Use this when window expressions are not allowed, where needed to
13772 * disambiguate the grammar (e.g. in CREATE INDEX).
13773 */
13774 func_expr_windowless:
13775 func_application { $$ = $1; }
13776 | func_expr_common_subexpr { $$ = $1; }
13777 ;
13778
13779 /*
13780 * Special expressions that are considered to be functions.
13781 */
13782 func_expr_common_subexpr:
13783 COLLATION FOR '(' a_expr ')'
13784 {
13785 $$ = (Node *) makeFuncCall(SystemFuncName("pg_collation_for"),
13786 list_make1($4),
13787 @1);
13788 }
13789 | CURRENT_DATE
13790 {
13791 $$ = makeSQLValueFunction(SVFOP_CURRENT_DATE, -1, @1);
13792 }
13793 | CURRENT_TIME
13794 {
13795 $$ = makeSQLValueFunction(SVFOP_CURRENT_TIME, -1, @1);
13796 }
13797 | CURRENT_TIME '(' Iconst ')'
13798 {
13799 $$ = makeSQLValueFunction(SVFOP_CURRENT_TIME_N, $3, @1);
13800 }
13801 | CURRENT_TIMESTAMP
13802 {
13803 $$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP, -1, @1);
13804 }
13805 | CURRENT_TIMESTAMP '(' Iconst ')'
13806 {
13807 $$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP_N, $3, @1);
13808 }
13809 | LOCALTIME
13810 {
13811 $$ = makeSQLValueFunction(SVFOP_LOCALTIME, -1, @1);
13812 }
13813 | LOCALTIME '(' Iconst ')'
13814 {
13815 $$ = makeSQLValueFunction(SVFOP_LOCALTIME_N, $3, @1);
13816 }
13817 | LOCALTIMESTAMP
13818 {
13819 $$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP, -1, @1);
13820 }
13821 | LOCALTIMESTAMP '(' Iconst ')'
13822 {
13823 $$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP_N, $3, @1);
13824 }
13825 | CURRENT_ROLE
13826 {
13827 $$ = makeSQLValueFunction(SVFOP_CURRENT_ROLE, -1, @1);
13828 }
13829 | CURRENT_USER
13830 {
13831 $$ = makeSQLValueFunction(SVFOP_CURRENT_USER, -1, @1);
13832 }
13833 | SESSION_USER
13834 {
13835 $$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1);
13836 }
13837 | USER
13838 {
13839 $$ = makeSQLValueFunction(SVFOP_USER, -1, @1);
13840 }
13841 | CURRENT_CATALOG
13842 {
13843 $$ = makeSQLValueFunction(SVFOP_CURRENT_CATALOG, -1, @1);
13844 }
13845 | CURRENT_SCHEMA
13846 {
13847 $$ = makeSQLValueFunction(SVFOP_CURRENT_SCHEMA, -1, @1);
13848 }
13849 | CAST '(' a_expr AS Typename ')'
13850 { $$ = makeTypeCast($3, $5, @1); }
13851 | EXTRACT '(' extract_list ')'
13852 {
13853 $$ = (Node *) makeFuncCall(SystemFuncName("date_part"), $3, @1);
13854 }
13855 | OVERLAY '(' overlay_list ')'
13856 {
13857 /* overlay(A PLACING B FROM C FOR D) is converted to
13858 * overlay(A, B, C, D)
13859 * overlay(A PLACING B FROM C) is converted to
13860 * overlay(A, B, C)
13861 */
13862 $$ = (Node *) makeFuncCall(SystemFuncName("overlay"), $3, @1);
13863 }
13864 | POSITION '(' position_list ')'
13865 {
13866 /* position(A in B) is converted to position(B, A) */
13867 $$ = (Node *) makeFuncCall(SystemFuncName("position"), $3, @1);
13868 }
13869 | SUBSTRING '(' substr_list ')'
13870 {
13871 /* substring(A from B for C) is converted to
13872 * substring(A, B, C) - thomas 2000-11-28
13873 */
13874 $$ = (Node *) makeFuncCall(SystemFuncName("substring"), $3, @1);
13875 }
13876 | TREAT '(' a_expr AS Typename ')'
13877 {
13878 /* TREAT(expr AS target) converts expr of a particular type to target,
13879 * which is defined to be a subtype of the original expression.
13880 * In SQL99, this is intended for use with structured UDTs,
13881 * but let's make this a generally useful form allowing stronger
13882 * coercions than are handled by implicit casting.
13883 *
13884 * Convert SystemTypeName() to SystemFuncName() even though
13885 * at the moment they result in the same thing.
13886 */
13887 $$ = (Node *) makeFuncCall(SystemFuncName(((Value *)llast($5->names))->val.str),
13888 list_make1($3),
13889 @1);
13890 }
13891 | TRIM '(' BOTH trim_list ')'
13892 {
13893 /* various trim expressions are defined in SQL
13894 * - thomas 1997-07-19
13895 */
13896 $$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $4, @1);
13897 }
13898 | TRIM '(' LEADING trim_list ')'
13899 {
13900 $$ = (Node *) makeFuncCall(SystemFuncName("ltrim"), $4, @1);
13901 }
13902 | TRIM '(' TRAILING trim_list ')'
13903 {
13904 $$ = (Node *) makeFuncCall(SystemFuncName("rtrim"), $4, @1);
13905 }
13906 | TRIM '(' trim_list ')'
13907 {
13908 $$ = (Node *) makeFuncCall(SystemFuncName("btrim"), $3, @1);
13909 }
13910 | NULLIF '(' a_expr ',' a_expr ')'
13911 {
13912 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
13913 }
13914 | COALESCE '(' expr_list ')'
13915 {
13916 CoalesceExpr *c = makeNode(CoalesceExpr);
13917 c->args = $3;
13918 c->location = @1;
13919 $$ = (Node *)c;
13920 }
13921 | GREATEST '(' expr_list ')'
13922 {
13923 MinMaxExpr *v = makeNode(MinMaxExpr);
13924 v->args = $3;
13925 v->op = IS_GREATEST;
13926 v->location = @1;
13927 $$ = (Node *)v;
13928 }
13929 | LEAST '(' expr_list ')'
13930 {
13931 MinMaxExpr *v = makeNode(MinMaxExpr);
13932 v->args = $3;
13933 v->op = IS_LEAST;
13934 v->location = @1;
13935 $$ = (Node *)v;
13936 }
13937 | XMLCONCAT '(' expr_list ')'
13938 {
13939 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
13940 }
13941 | XMLELEMENT '(' NAME_P ColLabel ')'
13942 {
13943 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
13944 }
13945 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
13946 {
13947 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
13948 }
13949 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
13950 {
13951 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
13952 }
13953 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
13954 {
13955 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
13956 }
13957 | XMLEXISTS '(' c_expr xmlexists_argument ')'
13958 {
13959 /* xmlexists(A PASSING [BY REF] B [BY REF]) is
13960 * converted to xmlexists(A, B)*/
13961 $$ = (Node *) makeFuncCall(SystemFuncName("xmlexists"), list_make2($3, $4), @1);
13962 }
13963 | XMLFOREST '(' xml_attribute_list ')'
13964 {
13965 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
13966 }
13967 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
13968 {
13969 XmlExpr *x = (XmlExpr *)
13970 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
13971 list_make2($4, makeBoolAConst($5, -1)),
13972 @1);
13973 x->xmloption = $3;
13974 $$ = (Node *)x;
13975 }
13976 | XMLPI '(' NAME_P ColLabel ')'
13977 {
13978 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
13979 }
13980 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
13981 {
13982 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
13983 }
13984 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
13985 {
13986 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
13987 list_make3($3, $5, $6), @1);
13988 }
13989 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
13990 {
13991 XmlSerialize *n = makeNode(XmlSerialize);
13992 n->xmloption = $3;
13993 n->expr = $4;
13994 n->typeName = $6;
13995 n->location = @1;
13996 $$ = (Node *)n;
13997 }
13998 ;
13999
14000 /*
14001 * SQL/XML support
14002 */
14003 xml_root_version: VERSION_P a_expr
14004 { $$ = $2; }
14005 | VERSION_P NO VALUE_P
14006 { $$ = makeNullAConst(-1); }
14007 ;
14008
14009 opt_xml_root_standalone: ',' STANDALONE_P YES_P
14010 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
14011 | ',' STANDALONE_P NO
14012 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
14013 | ',' STANDALONE_P NO VALUE_P
14014 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
14015 | /*EMPTY*/
14016 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
14017 ;
14018
14019 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
14020 ;
14021
14022 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
14023 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
14024 ;
14025
14026 xml_attribute_el: a_expr AS ColLabel
14027 {
14028 $$ = makeNode(ResTarget);
14029 $$->name = $3;
14030 $$->indirection = NIL;
14031 $$->val = (Node *) $1;
14032 $$->location = @1;
14033 }
14034 | a_expr
14035 {
14036 $$ = makeNode(ResTarget);
14037 $$->name = NULL;
14038 $$->indirection = NIL;
14039 $$->val = (Node *) $1;
14040 $$->location = @1;
14041 }
14042 ;
14043
14044 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
14045 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
14046 ;
14047
14048 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = true; }
14049 | STRIP_P WHITESPACE_P { $$ = false; }
14050 | /*EMPTY*/ { $$ = false; }
14051 ;
14052
14053 /* We allow several variants for SQL and other compatibility. */
14054 xmlexists_argument:
14055 PASSING c_expr
14056 {
14057 $$ = $2;
14058 }
14059 | PASSING c_expr xml_passing_mech
14060 {
14061 $$ = $2;
14062 }
14063 | PASSING xml_passing_mech c_expr
14064 {
14065 $$ = $3;
14066 }
14067 | PASSING xml_passing_mech c_expr xml_passing_mech
14068 {
14069 $$ = $3;
14070 }
14071 ;
14072
14073 xml_passing_mech:
14074 BY REF
14075 | BY VALUE_P
14076 ;
14077
14078
14079 /*
14080 * Aggregate decoration clauses
14081 */
14082 within_group_clause:
14083 WITHIN GROUP_P '(' sort_clause ')' { $$ = $4; }
14084 | /*EMPTY*/ { $$ = NIL; }
14085 ;
14086
14087 filter_clause:
14088 FILTER '(' WHERE a_expr ')' { $$ = $4; }
14089 | /*EMPTY*/ { $$ = NULL; }
14090 ;
14091
14092
14093 /*
14094 * Window Definitions
14095 */
14096 window_clause:
14097 WINDOW window_definition_list { $$ = $2; }
14098 | /*EMPTY*/ { $$ = NIL; }
14099 ;
14100
14101 window_definition_list:
14102 window_definition { $$ = list_make1($1); }
14103 | window_definition_list ',' window_definition
14104 { $$ = lappend($1, $3); }
14105 ;
14106
14107 window_definition:
14108 ColId AS window_specification
14109 {
14110 WindowDef *n = $3;
14111 n->name = $1;
14112 $$ = n;
14113 }
14114 ;
14115
14116 over_clause: OVER window_specification
14117 { $$ = $2; }
14118 | OVER ColId
14119 {
14120 WindowDef *n = makeNode(WindowDef);
14121 n->name = $2;
14122 n->refname = NULL;
14123 n->partitionClause = NIL;
14124 n->orderClause = NIL;
14125 n->frameOptions = FRAMEOPTION_DEFAULTS;
14126 n->startOffset = NULL;
14127 n->endOffset = NULL;
14128 n->location = @2;
14129 $$ = n;
14130 }
14131 | /*EMPTY*/
14132 { $$ = NULL; }
14133 ;
14134
14135 window_specification: '(' opt_existing_window_name opt_partition_clause
14136 opt_sort_clause opt_frame_clause ')'
14137 {
14138 WindowDef *n = makeNode(WindowDef);
14139 n->name = NULL;
14140 n->refname = $2;
14141 n->partitionClause = $3;
14142 n->orderClause = $4;
14143 /* copy relevant fields of opt_frame_clause */
14144 n->frameOptions = $5->frameOptions;
14145 n->startOffset = $5->startOffset;
14146 n->endOffset = $5->endOffset;
14147 n->location = @1;
14148 $$ = n;
14149 }
14150 ;
14151
14152 /*
14153 * If we see PARTITION, RANGE, ROWS or GROUPS as the first token after the '('
14154 * of a window_specification, we want the assumption to be that there is
14155 * no existing_window_name; but those keywords are unreserved and so could
14156 * be ColIds. We fix this by making them have the same precedence as IDENT
14157 * and giving the empty production here a slightly higher precedence, so
14158 * that the shift/reduce conflict is resolved in favor of reducing the rule.
14159 * These keywords are thus precluded from being an existing_window_name but
14160 * are not reserved for any other purpose.
14161 */
14162 opt_existing_window_name: ColId { $$ = $1; }
14163 | /*EMPTY*/ %prec Op { $$ = NULL; }
14164 ;
14165
14166 opt_partition_clause: PARTITION BY expr_list { $$ = $3; }
14167 | /*EMPTY*/ { $$ = NIL; }
14168 ;
14169
14170 /*
14171 * For frame clauses, we return a WindowDef, but only some fields are used:
14172 * frameOptions, startOffset, and endOffset.
14173 */
14174 opt_frame_clause:
14175 RANGE frame_extent opt_window_exclusion_clause
14176 {
14177 WindowDef *n = $2;
14178 n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
14179 n->frameOptions |= $3;
14180 $$ = n;
14181 }
14182 | ROWS frame_extent opt_window_exclusion_clause
14183 {
14184 WindowDef *n = $2;
14185 n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
14186 n->frameOptions |= $3;
14187 $$ = n;
14188 }
14189 | GROUPS frame_extent opt_window_exclusion_clause
14190 {
14191 WindowDef *n = $2;
14192 n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_GROUPS;
14193 n->frameOptions |= $3;
14194 $$ = n;
14195 }
14196 | /*EMPTY*/
14197 {
14198 WindowDef *n = makeNode(WindowDef);
14199 n->frameOptions = FRAMEOPTION_DEFAULTS;
14200 n->startOffset = NULL;
14201 n->endOffset = NULL;
14202 $$ = n;
14203 }
14204 ;
14205
14206 frame_extent: frame_bound
14207 {
14208 WindowDef *n = $1;
14209 /* reject invalid cases */
14210 if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14211 ereport(ERROR,
14212 (errcode(ERRCODE_WINDOWING_ERROR),
14213 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14214 parser_errposition(@1)));
14215 if (n->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
14216 ereport(ERROR,
14217 (errcode(ERRCODE_WINDOWING_ERROR),
14218 errmsg("frame starting from following row cannot end with current row"),
14219 parser_errposition(@1)));
14220 n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
14221 $$ = n;
14222 }
14223 | BETWEEN frame_bound AND frame_bound
14224 {
14225 WindowDef *n1 = $2;
14226 WindowDef *n2 = $4;
14227 /* form merged options */
14228 int frameOptions = n1->frameOptions;
14229 /* shift converts START_ options to END_ options */
14230 frameOptions |= n2->frameOptions << 1;
14231 frameOptions |= FRAMEOPTION_BETWEEN;
14232 /* reject invalid cases */
14233 if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
14234 ereport(ERROR,
14235 (errcode(ERRCODE_WINDOWING_ERROR),
14236 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
14237 parser_errposition(@2)));
14238 if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
14239 ereport(ERROR,
14240 (errcode(ERRCODE_WINDOWING_ERROR),
14241 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
14242 parser_errposition(@4)));
14243 if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
14244 (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING))
14245 ereport(ERROR,
14246 (errcode(ERRCODE_WINDOWING_ERROR),
14247 errmsg("frame starting from current row cannot have preceding rows"),
14248 parser_errposition(@4)));
14249 if ((frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING) &&
14250 (frameOptions & (FRAMEOPTION_END_OFFSET_PRECEDING |
14251 FRAMEOPTION_END_CURRENT_ROW)))
14252 ereport(ERROR,
14253 (errcode(ERRCODE_WINDOWING_ERROR),
14254 errmsg("frame starting from following row cannot have preceding rows"),
14255 parser_errposition(@4)));
14256 n1->frameOptions = frameOptions;
14257 n1->endOffset = n2->startOffset;
14258 $$ = n1;
14259 }
14260 ;
14261
14262 /*
14263 * This is used for both frame start and frame end, with output set up on
14264 * the assumption it's frame start; the frame_extent productions must reject
14265 * invalid cases.
14266 */
14267 frame_bound:
14268 UNBOUNDED PRECEDING
14269 {
14270 WindowDef *n = makeNode(WindowDef);
14271 n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
14272 n->startOffset = NULL;
14273 n->endOffset = NULL;
14274 $$ = n;
14275 }
14276 | UNBOUNDED FOLLOWING
14277 {
14278 WindowDef *n = makeNode(WindowDef);
14279 n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
14280 n->startOffset = NULL;
14281 n->endOffset = NULL;
14282 $$ = n;
14283 }
14284 | CURRENT_P ROW
14285 {
14286 WindowDef *n = makeNode(WindowDef);
14287 n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
14288 n->startOffset = NULL;
14289 n->endOffset = NULL;
14290 $$ = n;
14291 }
14292 | a_expr PRECEDING
14293 {
14294 WindowDef *n = makeNode(WindowDef);
14295 n->frameOptions = FRAMEOPTION_START_OFFSET_PRECEDING;
14296 n->startOffset = $1;
14297 n->endOffset = NULL;
14298 $$ = n;
14299 }
14300 | a_expr FOLLOWING
14301 {
14302 WindowDef *n = makeNode(WindowDef);
14303 n->frameOptions = FRAMEOPTION_START_OFFSET_FOLLOWING;
14304 n->startOffset = $1;
14305 n->endOffset = NULL;
14306 $$ = n;
14307 }
14308 ;
14309
14310 opt_window_exclusion_clause:
14311 EXCLUDE CURRENT_P ROW { $$ = FRAMEOPTION_EXCLUDE_CURRENT_ROW; }
14312 | EXCLUDE GROUP_P { $$ = FRAMEOPTION_EXCLUDE_GROUP; }
14313 | EXCLUDE TIES { $$ = FRAMEOPTION_EXCLUDE_TIES; }
14314 | EXCLUDE NO OTHERS { $$ = 0; }
14315 | /*EMPTY*/ { $$ = 0; }
14316 ;
14317
14318
14319 /*
14320 * Supporting nonterminals for expressions.
14321 */
14322
14323 /* Explicit row production.
14324 *
14325 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
14326 * without conflicting with the parenthesized a_expr production. Without the
14327 * ROW keyword, there must be more than one a_expr inside the parens.
14328 */
14329 row: ROW '(' expr_list ')' { $$ = $3; }
14330 | ROW '(' ')' { $$ = NIL; }
14331 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
14332 ;
14333
14334 explicit_row: ROW '(' expr_list ')' { $$ = $3; }
14335 | ROW '(' ')' { $$ = NIL; }
14336 ;
14337
14338 implicit_row: '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
14339 ;
14340
14341 sub_type: ANY { $$ = ANY_SUBLINK; }
14342 | SOME { $$ = ANY_SUBLINK; }
14343 | ALL { $$ = ALL_SUBLINK; }
14344 ;
14345
14346 all_Op: Op { $$ = $1; }
14347 | MathOp { $$ = $1; }
14348 ;
14349
14350 MathOp: '+' { $$ = "+"; }
14351 | '-' { $$ = "-"; }
14352 | '*' { $$ = "*"; }
14353 | '/' { $$ = "/"; }
14354 | '%' { $$ = "%"; }
14355 | '^' { $$ = "^"; }
14356 | '<' { $$ = "<"; }
14357 | '>' { $$ = ">"; }
14358 | '=' { $$ = "="; }
14359 | LESS_EQUALS { $$ = "<="; }
14360 | GREATER_EQUALS { $$ = ">="; }
14361 | NOT_EQUALS { $$ = "<>"; }
14362 ;
14363
14364 qual_Op: Op
14365 { $$ = list_make1(makeString($1)); }
14366 | OPERATOR '(' any_operator ')'
14367 { $$ = $3; }
14368 ;
14369
14370 qual_all_Op:
14371 all_Op
14372 { $$ = list_make1(makeString($1)); }
14373 | OPERATOR '(' any_operator ')'
14374 { $$ = $3; }
14375 ;
14376
14377 subquery_Op:
14378 all_Op
14379 { $$ = list_make1(makeString($1)); }
14380 | OPERATOR '(' any_operator ')'
14381 { $$ = $3; }
14382 | LIKE
14383 { $$ = list_make1(makeString("~~")); }
14384 | NOT_LA LIKE
14385 { $$ = list_make1(makeString("!~~")); }
14386 | ILIKE
14387 { $$ = list_make1(makeString("~~*")); }
14388 | NOT_LA ILIKE
14389 { $$ = list_make1(makeString("!~~*")); }
14390 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
14391 * the regular expression is preprocessed by a function (similar_escape),
14392 * and the ~ operator for posix regular expressions is used.
14393 * x SIMILAR TO y -> x ~ similar_escape(y)
14394 * this transformation is made on the fly by the parser upwards.
14395 * however the SubLink structure which handles any/some/all stuff
14396 * is not ready for such a thing.
14397 */
14398 ;
14399
14400 expr_list: a_expr
14401 {
14402 $$ = list_make1($1);
14403 }
14404 | expr_list ',' a_expr
14405 {
14406 $$ = lappend($1, $3);
14407 }
14408 ;
14409
14410 /* function arguments can have names */
14411 func_arg_list: func_arg_expr
14412 {
14413 $$ = list_make1($1);
14414 }
14415 | func_arg_list ',' func_arg_expr
14416 {
14417 $$ = lappend($1, $3);
14418 }
14419 ;
14420
14421 func_arg_expr: a_expr
14422 {
14423 $$ = $1;
14424 }
14425 | param_name COLON_EQUALS a_expr
14426 {
14427 NamedArgExpr *na = makeNode(NamedArgExpr);
14428 na->name = $1;
14429 na->arg = (Expr *) $3;
14430 na->argnumber = -1; /* until determined */
14431 na->location = @1;
14432 $$ = (Node *) na;
14433 }
14434 | param_name EQUALS_GREATER a_expr
14435 {
14436 NamedArgExpr *na = makeNode(NamedArgExpr);
14437 na->name = $1;
14438 na->arg = (Expr *) $3;
14439 na->argnumber = -1; /* until determined */
14440 na->location = @1;
14441 $$ = (Node *) na;
14442 }
14443 ;
14444
14445 type_list: Typename { $$ = list_make1($1); }
14446 | type_list ',' Typename { $$ = lappend($1, $3); }
14447 ;
14448
14449 array_expr: '[' expr_list ']'
14450 {
14451 $$ = makeAArrayExpr($2, @1);
14452 }
14453 | '[' array_expr_list ']'
14454 {
14455 $$ = makeAArrayExpr($2, @1);
14456 }
14457 | '[' ']'
14458 {
14459 $$ = makeAArrayExpr(NIL, @1);
14460 }
14461 ;
14462
14463 array_expr_list: array_expr { $$ = list_make1($1); }
14464 | array_expr_list ',' array_expr { $$ = lappend($1, $3); }
14465 ;
14466
14467
14468 extract_list:
14469 extract_arg FROM a_expr
14470 {
14471 $$ = list_make2(makeStringConst($1, @1), $3);
14472 }
14473 | /*EMPTY*/ { $$ = NIL; }
14474 ;
14475
14476 /* Allow delimited string Sconst in extract_arg as an SQL extension.
14477 * - thomas 2001-04-12
14478 */
14479 extract_arg:
14480 IDENT { $$ = $1; }
14481 | YEAR_P { $$ = "year"; }
14482 | MONTH_P { $$ = "month"; }
14483 | DAY_P { $$ = "day"; }
14484 | HOUR_P { $$ = "hour"; }
14485 | MINUTE_P { $$ = "minute"; }
14486 | SECOND_P { $$ = "second"; }
14487 | Sconst { $$ = $1; }
14488 ;
14489
14490 /* OVERLAY() arguments
14491 * SQL99 defines the OVERLAY() function:
14492 * o overlay(text placing text from int for int)
14493 * o overlay(text placing text from int)
14494 * and similarly for binary strings
14495 */
14496 overlay_list:
14497 a_expr overlay_placing substr_from substr_for
14498 {
14499 $$ = list_make4($1, $2, $3, $4);
14500 }
14501 | a_expr overlay_placing substr_from
14502 {
14503 $$ = list_make3($1, $2, $3);
14504 }
14505 ;
14506
14507 overlay_placing:
14508 PLACING a_expr
14509 { $$ = $2; }
14510 ;
14511
14512 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
14513
14514 position_list:
14515 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
14516 | /*EMPTY*/ { $$ = NIL; }
14517 ;
14518
14519 /* SUBSTRING() arguments
14520 * SQL9x defines a specific syntax for arguments to SUBSTRING():
14521 * o substring(text from int for int)
14522 * o substring(text from int) get entire string from starting point "int"
14523 * o substring(text for int) get first "int" characters of string
14524 * o substring(text from pattern) get entire string matching pattern
14525 * o substring(text from pattern for escape) same with specified escape char
14526 * We also want to support generic substring functions which accept
14527 * the usual generic list of arguments. So we will accept both styles
14528 * here, and convert the SQL9x style to the generic list for further
14529 * processing. - thomas 2000-11-28
14530 */
14531 substr_list:
14532 a_expr substr_from substr_for
14533 {
14534 $$ = list_make3($1, $2, $3);
14535 }
14536 | a_expr substr_for substr_from
14537 {
14538 /* not legal per SQL99, but might as well allow it */
14539 $$ = list_make3($1, $3, $2);
14540 }
14541 | a_expr substr_from
14542 {
14543 $$ = list_make2($1, $2);
14544 }
14545 | a_expr substr_for
14546 {
14547 /*
14548 * Since there are no cases where this syntax allows
14549 * a textual FOR value, we forcibly cast the argument
14550 * to int4. The possible matches in pg_proc are
14551 * substring(text,int4) and substring(text,text),
14552 * and we don't want the parser to choose the latter,
14553 * which it is likely to do if the second argument
14554 * is unknown or doesn't have an implicit cast to int4.
14555 */
14556 $$ = list_make3($1, makeIntConst(1, -1),
14557 makeTypeCast($2,
14558 SystemTypeName("int4"), -1));
14559 }
14560 | expr_list
14561 {
14562 $$ = $1;
14563 }
14564 | /*EMPTY*/
14565 { $$ = NIL; }
14566 ;
14567
14568 substr_from:
14569 FROM a_expr { $$ = $2; }
14570 ;
14571
14572 substr_for: FOR a_expr { $$ = $2; }
14573 ;
14574
14575 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
14576 | FROM expr_list { $$ = $2; }
14577 | expr_list { $$ = $1; }
14578 ;
14579
14580 in_expr: select_with_parens
14581 {
14582 SubLink *n = makeNode(SubLink);
14583 n->subselect = $1;
14584 /* other fields will be filled later */
14585 $$ = (Node *)n;
14586 }
14587 | '(' expr_list ')' { $$ = (Node *)$2; }
14588 ;
14589
14590 /*
14591 * Define SQL-style CASE clause.
14592 * - Full specification
14593 * CASE WHEN a = b THEN c ... ELSE d END
14594 * - Implicit argument
14595 * CASE a WHEN b THEN c ... ELSE d END
14596 */
14597 case_expr: CASE case_arg when_clause_list case_default END_P
14598 {
14599 CaseExpr *c = makeNode(CaseExpr);
14600 c->casetype = InvalidOid; /* not analyzed yet */
14601 c->arg = (Expr *) $2;
14602 c->args = $3;
14603 c->defresult = (Expr *) $4;
14604 c->location = @1;
14605 $$ = (Node *)c;
14606 }
14607 ;
14608
14609 when_clause_list:
14610 /* There must be at least one */
14611 when_clause { $$ = list_make1($1); }
14612 | when_clause_list when_clause { $$ = lappend($1, $2); }
14613 ;
14614
14615 when_clause:
14616 WHEN a_expr THEN a_expr
14617 {
14618 CaseWhen *w = makeNode(CaseWhen);
14619 w->expr = (Expr *) $2;
14620 w->result = (Expr *) $4;
14621 w->location = @1;
14622 $$ = (Node *)w;
14623 }
14624 ;
14625
14626 case_default:
14627 ELSE a_expr { $$ = $2; }
14628 | /*EMPTY*/ { $$ = NULL; }
14629 ;
14630
14631 case_arg: a_expr { $$ = $1; }
14632 | /*EMPTY*/ { $$ = NULL; }
14633 ;
14634
14635 columnref: ColId
14636 {
14637 $$ = makeColumnRef($1, NIL, @1, yyscanner);
14638 }
14639 | ColId indirection
14640 {
14641 $$ = makeColumnRef($1, $2, @1, yyscanner);
14642 }
14643 ;
14644
14645 indirection_el:
14646 '.' attr_name
14647 {
14648 $$ = (Node *) makeString($2);
14649 }
14650 | '.' '*'
14651 {
14652 $$ = (Node *) makeNode(A_Star);
14653 }
14654 | '[' a_expr ']'
14655 {
14656 A_Indices *ai = makeNode(A_Indices);
14657 ai->is_slice = false;
14658 ai->lidx = NULL;
14659 ai->uidx = $2;
14660 $$ = (Node *) ai;
14661 }
14662 | '[' opt_slice_bound ':' opt_slice_bound ']'
14663 {
14664 A_Indices *ai = makeNode(A_Indices);
14665 ai->is_slice = true;
14666 ai->lidx = $2;
14667 ai->uidx = $4;
14668 $$ = (Node *) ai;
14669 }
14670 ;
14671
14672 opt_slice_bound:
14673 a_expr { $$ = $1; }
14674 | /*EMPTY*/ { $$ = NULL; }
14675 ;
14676
14677 indirection:
14678 indirection_el { $$ = list_make1($1); }
14679 | indirection indirection_el { $$ = lappend($1, $2); }
14680 ;
14681
14682 opt_indirection:
14683 /*EMPTY*/ { $$ = NIL; }
14684 | opt_indirection indirection_el { $$ = lappend($1, $2); }
14685 ;
14686
14687 opt_asymmetric: ASYMMETRIC
14688 | /*EMPTY*/
14689 ;
14690
14691
14692 /*****************************************************************************
14693 *
14694 * target list for SELECT
14695 *
14696 *****************************************************************************/
14697
14698 opt_target_list: target_list { $$ = $1; }
14699 | /* EMPTY */ { $$ = NIL; }
14700 ;
14701
14702 target_list:
14703 target_el { $$ = list_make1($1); }
14704 | target_list ',' target_el { $$ = lappend($1, $3); }
14705 ;
14706
14707 target_el: a_expr AS ColLabel
14708 {
14709 $$ = makeNode(ResTarget);
14710 $$->name = $3;
14711 $$->indirection = NIL;
14712 $$->val = (Node *)$1;
14713 $$->location = @1;
14714 }
14715 /*
14716 * We support omitting AS only for column labels that aren't
14717 * any known keyword. There is an ambiguity against postfix
14718 * operators: is "a ! b" an infix expression, or a postfix
14719 * expression and a column label? We prefer to resolve this
14720 * as an infix expression, which we accomplish by assigning
14721 * IDENT a precedence higher than POSTFIXOP.
14722 */
14723 | a_expr IDENT
14724 {
14725 $$ = makeNode(ResTarget);
14726 $$->name = $2;
14727 $$->indirection = NIL;
14728 $$->val = (Node *)$1;
14729 $$->location = @1;
14730 }
14731 | a_expr
14732 {
14733 $$ = makeNode(ResTarget);
14734 $$->name = NULL;
14735 $$->indirection = NIL;
14736 $$->val = (Node *)$1;
14737 $$->location = @1;
14738 }
14739 | '*'
14740 {
14741 ColumnRef *n = makeNode(ColumnRef);
14742 n->fields = list_make1(makeNode(A_Star));
14743 n->location = @1;
14744
14745 $$ = makeNode(ResTarget);
14746 $$->name = NULL;
14747 $$->indirection = NIL;
14748 $$->val = (Node *)n;
14749 $$->location = @1;
14750 }
14751 ;
14752
14753
14754 /*****************************************************************************
14755 *
14756 * Names and constants
14757 *
14758 *****************************************************************************/
14759
14760 qualified_name_list:
14761 qualified_name { $$ = list_make1($1); }
14762 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
14763 ;
14764
14765 /*
14766 * The production for a qualified relation name has to exactly match the
14767 * production for a qualified func_name, because in a FROM clause we cannot
14768 * tell which we are parsing until we see what comes after it ('(' for a
14769 * func_name, something else for a relation). Therefore we allow 'indirection'
14770 * which may contain subscripts, and reject that case in the C code.
14771 */
14772 qualified_name:
14773 ColId
14774 {
14775 $$ = makeRangeVar(NULL, $1, @1);
14776 }
14777 | ColId indirection
14778 {
14779 check_qualified_name($2, yyscanner);
14780 $$ = makeRangeVar(NULL, NULL, @1);
14781 switch (list_length($2))
14782 {
14783 case 1:
14784 $$->catalogname = NULL;
14785 $$->schemaname = $1;
14786 $$->relname = strVal(linitial($2));
14787 break;
14788 case 2:
14789 $$->catalogname = $1;
14790 $$->schemaname = strVal(linitial($2));
14791 $$->relname = strVal(lsecond($2));
14792 break;
14793 default:
14794 ereport(ERROR,
14795 (errcode(ERRCODE_SYNTAX_ERROR),
14796 errmsg("improper qualified name (too many dotted names): %s",
14797 NameListToString(lcons(makeString($1), $2))),
14798 parser_errposition(@1)));
14799 break;
14800 }
14801 }
14802 ;
14803
14804 name_list: name
14805 { $$ = list_make1(makeString($1)); }
14806 | name_list ',' name
14807 { $$ = lappend($1, makeString($3)); }
14808 ;
14809
14810
14811 name: ColId { $$ = $1; };
14812
14813 database_name:
14814 ColId { $$ = $1; };
14815
14816 access_method:
14817 ColId { $$ = $1; };
14818
14819 attr_name: ColLabel { $$ = $1; };
14820
14821 index_name: ColId { $$ = $1; };
14822
14823 file_name: Sconst { $$ = $1; };
14824
14825 /*
14826 * The production for a qualified func_name has to exactly match the
14827 * production for a qualified columnref, because we cannot tell which we
14828 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
14829 * anything else for a columnref). Therefore we allow 'indirection' which
14830 * may contain subscripts, and reject that case in the C code. (If we
14831 * ever implement SQL99-like methods, such syntax may actually become legal!)
14832 */
14833 func_name: type_function_name
14834 { $$ = list_make1(makeString($1)); }
14835 | ColId indirection
14836 {
14837 $$ = check_func_name(lcons(makeString($1), $2),
14838 yyscanner);
14839 }
14840 ;
14841
14842
14843 /*
14844 * Constants
14845 */
14846 AexprConst: Iconst
14847 {
14848 $$ = makeIntConst($1, @1);
14849 }
14850 | FCONST
14851 {
14852 $$ = makeFloatConst($1, @1);
14853 }
14854 | Sconst
14855 {
14856 $$ = makeStringConst($1, @1);
14857 }
14858 | BCONST
14859 {
14860 $$ = makeBitStringConst($1, @1);
14861 }
14862 | XCONST
14863 {
14864 /* This is a bit constant per SQL99:
14865 * Without Feature F511, "BIT data type",
14866 * a <general literal> shall not be a
14867 * <bit string literal> or a <hex string literal>.
14868 */
14869 $$ = makeBitStringConst($1, @1);
14870 }
14871 | func_name Sconst
14872 {
14873 /* generic type 'literal' syntax */
14874 TypeName *t = makeTypeNameFromNameList($1);
14875 t->location = @1;
14876 $$ = makeStringConstCast($2, @2, t);
14877 }
14878 | func_name '(' func_arg_list opt_sort_clause ')' Sconst
14879 {
14880 /* generic syntax with a type modifier */
14881 TypeName *t = makeTypeNameFromNameList($1);
14882 ListCell *lc;
14883
14884 /*
14885 * We must use func_arg_list and opt_sort_clause in the
14886 * production to avoid reduce/reduce conflicts, but we
14887 * don't actually wish to allow NamedArgExpr in this
14888 * context, nor ORDER BY.
14889 */
foreach(lc,$3)14890 foreach(lc, $3)
14891 {
14892 NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
14893
14894 if (IsA(arg, NamedArgExpr))
14895 ereport(ERROR,
14896 (errcode(ERRCODE_SYNTAX_ERROR),
14897 errmsg("type modifier cannot have parameter name"),
14898 parser_errposition(arg->location)));
14899 }
14900 if ($4 != NIL)
14901 ereport(ERROR,
14902 (errcode(ERRCODE_SYNTAX_ERROR),
14903 errmsg("type modifier cannot have ORDER BY"),
14904 parser_errposition(@4)));
14905
14906 t->typmods = $3;
14907 t->location = @1;
14908 $$ = makeStringConstCast($6, @6, t);
14909 }
14910 | ConstTypename Sconst
14911 {
14912 $$ = makeStringConstCast($2, @2, $1);
14913 }
14914 | ConstInterval Sconst opt_interval
14915 {
14916 TypeName *t = $1;
14917 t->typmods = $3;
14918 $$ = makeStringConstCast($2, @2, t);
14919 }
14920 | ConstInterval '(' Iconst ')' Sconst
14921 {
14922 TypeName *t = $1;
14923 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
14924 makeIntConst($3, @3));
14925 $$ = makeStringConstCast($5, @5, t);
14926 }
14927 | TRUE_P
14928 {
14929 $$ = makeBoolAConst(true, @1);
14930 }
14931 | FALSE_P
14932 {
14933 $$ = makeBoolAConst(false, @1);
14934 }
14935 | NULL_P
14936 {
14937 $$ = makeNullAConst(@1);
14938 }
14939 ;
14940
14941 Iconst: ICONST { $$ = $1; };
14942 Sconst: SCONST { $$ = $1; };
14943
14944 SignedIconst: Iconst { $$ = $1; }
14945 | '+' Iconst { $$ = + $2; }
14946 | '-' Iconst { $$ = - $2; }
14947 ;
14948
14949 /* Role specifications */
14950 RoleId: RoleSpec
14951 {
14952 RoleSpec *spc = (RoleSpec *) $1;
14953 switch (spc->roletype)
14954 {
14955 case ROLESPEC_CSTRING:
14956 $$ = spc->rolename;
14957 break;
14958 case ROLESPEC_PUBLIC:
14959 ereport(ERROR,
14960 (errcode(ERRCODE_RESERVED_NAME),
14961 errmsg("role name \"%s\" is reserved",
14962 "public"),
14963 parser_errposition(@1)));
14964 break;
14965 case ROLESPEC_SESSION_USER:
14966 ereport(ERROR,
14967 (errcode(ERRCODE_RESERVED_NAME),
14968 errmsg("%s cannot be used as a role name here",
14969 "SESSION_USER"),
14970 parser_errposition(@1)));
14971 break;
14972 case ROLESPEC_CURRENT_USER:
14973 ereport(ERROR,
14974 (errcode(ERRCODE_RESERVED_NAME),
14975 errmsg("%s cannot be used as a role name here",
14976 "CURRENT_USER"),
14977 parser_errposition(@1)));
14978 break;
14979 }
14980 }
14981 ;
14982
14983 RoleSpec: NonReservedWord
14984 {
14985 /*
14986 * "public" and "none" are not keywords, but they must
14987 * be treated specially here.
14988 */
14989 RoleSpec *n;
14990 if (strcmp($1, "public") == 0)
14991 {
14992 n = (RoleSpec *) makeRoleSpec(ROLESPEC_PUBLIC, @1);
14993 n->roletype = ROLESPEC_PUBLIC;
14994 }
14995 else if (strcmp($1, "none") == 0)
14996 {
14997 ereport(ERROR,
14998 (errcode(ERRCODE_RESERVED_NAME),
14999 errmsg("role name \"%s\" is reserved",
15000 "none"),
15001 parser_errposition(@1)));
15002 }
15003 else
15004 {
15005 n = makeRoleSpec(ROLESPEC_CSTRING, @1);
15006 n->rolename = pstrdup($1);
15007 }
15008 $$ = n;
15009 }
15010 | CURRENT_USER
15011 {
15012 $$ = makeRoleSpec(ROLESPEC_CURRENT_USER, @1);
15013 }
15014 | SESSION_USER
15015 {
15016 $$ = makeRoleSpec(ROLESPEC_SESSION_USER, @1);
15017 }
15018 ;
15019
15020 role_list: RoleSpec
15021 { $$ = list_make1($1); }
15022 | role_list ',' RoleSpec
15023 { $$ = lappend($1, $3); }
15024 ;
15025
15026 /*
15027 * Name classification hierarchy.
15028 *
15029 * IDENT is the lexeme returned by the lexer for identifiers that match
15030 * no known keyword. In most cases, we can accept certain keywords as
15031 * names, not only IDENTs. We prefer to accept as many such keywords
15032 * as possible to minimize the impact of "reserved words" on programmers.
15033 * So, we divide names into several possible classes. The classification
15034 * is chosen in part to make keywords acceptable as names wherever possible.
15035 */
15036
15037 /* Column identifier --- names that can be column, table, etc names.
15038 */
15039 ColId: IDENT { $$ = $1; }
15040 | unreserved_keyword { $$ = pstrdup($1); }
15041 | col_name_keyword { $$ = pstrdup($1); }
15042 ;
15043
15044 /* Type/function identifier --- names that can be type or function names.
15045 */
15046 type_function_name: IDENT { $$ = $1; }
15047 | unreserved_keyword { $$ = pstrdup($1); }
15048 | type_func_name_keyword { $$ = pstrdup($1); }
15049 ;
15050
15051 /* Any not-fully-reserved word --- these names can be, eg, role names.
15052 */
15053 NonReservedWord: IDENT { $$ = $1; }
15054 | unreserved_keyword { $$ = pstrdup($1); }
15055 | col_name_keyword { $$ = pstrdup($1); }
15056 | type_func_name_keyword { $$ = pstrdup($1); }
15057 ;
15058
15059 /* Column label --- allowed labels in "AS" clauses.
15060 * This presently includes *all* Postgres keywords.
15061 */
15062 ColLabel: IDENT { $$ = $1; }
15063 | unreserved_keyword { $$ = pstrdup($1); }
15064 | col_name_keyword { $$ = pstrdup($1); }
15065 | type_func_name_keyword { $$ = pstrdup($1); }
15066 | reserved_keyword { $$ = pstrdup($1); }
15067 ;
15068
15069
15070 /*
15071 * Keyword category lists. Generally, every keyword present in
15072 * the Postgres grammar should appear in exactly one of these lists.
15073 *
15074 * Put a new keyword into the first list that it can go into without causing
15075 * shift or reduce conflicts. The earlier lists define "less reserved"
15076 * categories of keywords.
15077 *
15078 * Make sure that each keyword's category in kwlist.h matches where
15079 * it is listed here. (Someday we may be able to generate these lists and
15080 * kwlist.h's table from a common master list.)
15081 */
15082
15083 /* "Unreserved" keywords --- available for use as any kind of name.
15084 */
15085 unreserved_keyword:
15086 ABORT_P
15087 | ABSOLUTE_P
15088 | ACCESS
15089 | ACTION
15090 | ADD_P
15091 | ADMIN
15092 | AFTER
15093 | AGGREGATE
15094 | ALSO
15095 | ALTER
15096 | ALWAYS
15097 | ASSERTION
15098 | ASSIGNMENT
15099 | AT
15100 | ATTACH
15101 | ATTRIBUTE
15102 | BACKWARD
15103 | BEFORE
15104 | BEGIN_P
15105 | BY
15106 | CACHE
15107 | CALL
15108 | CALLED
15109 | CASCADE
15110 | CASCADED
15111 | CATALOG_P
15112 | CHAIN
15113 | CHARACTERISTICS
15114 | CHECKPOINT
15115 | CLASS
15116 | CLOSE
15117 | CLUSTER
15118 | COLUMNS
15119 | COMMENT
15120 | COMMENTS
15121 | COMMIT
15122 | COMMITTED
15123 | CONFIGURATION
15124 | CONFLICT
15125 | CONNECTION
15126 | CONSTRAINTS
15127 | CONTENT_P
15128 | CONTINUE_P
15129 | CONVERSION_P
15130 | COPY
15131 | COST
15132 | CSV
15133 | CUBE
15134 | CURRENT_P
15135 | CURSOR
15136 | CYCLE
15137 | DATA_P
15138 | DATABASE
15139 | DAY_P
15140 | DEALLOCATE
15141 | DECLARE
15142 | DEFAULTS
15143 | DEFERRED
15144 | DEFINER
15145 | DELETE_P
15146 | DELIMITER
15147 | DELIMITERS
15148 | DEPENDS
15149 | DETACH
15150 | DICTIONARY
15151 | DISABLE_P
15152 | DISCARD
15153 | DOCUMENT_P
15154 | DOMAIN_P
15155 | DOUBLE_P
15156 | DROP
15157 | EACH
15158 | ENABLE_P
15159 | ENCODING
15160 | ENCRYPTED
15161 | ENUM_P
15162 | ESCAPE
15163 | EVENT
15164 | EXCLUDE
15165 | EXCLUDING
15166 | EXCLUSIVE
15167 | EXECUTE
15168 | EXPLAIN
15169 | EXTENSION
15170 | EXTERNAL
15171 | FAMILY
15172 | FILTER
15173 | FIRST_P
15174 | FOLLOWING
15175 | FORCE
15176 | FORWARD
15177 | FUNCTION
15178 | FUNCTIONS
15179 | GENERATED
15180 | GLOBAL
15181 | GRANTED
15182 | GROUPS
15183 | HANDLER
15184 | HEADER_P
15185 | HOLD
15186 | HOUR_P
15187 | IDENTITY_P
15188 | IF_P
15189 | IMMEDIATE
15190 | IMMUTABLE
15191 | IMPLICIT_P
15192 | IMPORT_P
15193 | INCLUDE
15194 | INCLUDING
15195 | INCREMENT
15196 | INDEX
15197 | INDEXES
15198 | INHERIT
15199 | INHERITS
15200 | INLINE_P
15201 | INPUT_P
15202 | INSENSITIVE
15203 | INSERT
15204 | INSTEAD
15205 | INVOKER
15206 | ISOLATION
15207 | KEY
15208 | LABEL
15209 | LANGUAGE
15210 | LARGE_P
15211 | LAST_P
15212 | LEAKPROOF
15213 | LEVEL
15214 | LISTEN
15215 | LOAD
15216 | LOCAL
15217 | LOCATION
15218 | LOCK_P
15219 | LOCKED
15220 | LOGGED
15221 | MAPPING
15222 | MATCH
15223 | MATERIALIZED
15224 | MAXVALUE
15225 | METHOD
15226 | MINUTE_P
15227 | MINVALUE
15228 | MODE
15229 | MONTH_P
15230 | MOVE
15231 | NAME_P
15232 | NAMES
15233 | NEW
15234 | NEXT
15235 | NO
15236 | NOTHING
15237 | NOTIFY
15238 | NOWAIT
15239 | NULLS_P
15240 | OBJECT_P
15241 | OF
15242 | OFF
15243 | OIDS
15244 | OLD
15245 | OPERATOR
15246 | OPTION
15247 | OPTIONS
15248 | ORDINALITY
15249 | OTHERS
15250 | OVER
15251 | OVERRIDING
15252 | OWNED
15253 | OWNER
15254 | PARALLEL
15255 | PARSER
15256 | PARTIAL
15257 | PARTITION
15258 | PASSING
15259 | PASSWORD
15260 | PLANS
15261 | POLICY
15262 | PRECEDING
15263 | PREPARE
15264 | PREPARED
15265 | PRESERVE
15266 | PRIOR
15267 | PRIVILEGES
15268 | PROCEDURAL
15269 | PROCEDURE
15270 | PROCEDURES
15271 | PROGRAM
15272 | PUBLICATION
15273 | QUOTE
15274 | RANGE
15275 | READ
15276 | REASSIGN
15277 | RECHECK
15278 | RECURSIVE
15279 | REF
15280 | REFERENCING
15281 | REFRESH
15282 | REINDEX
15283 | RELATIVE_P
15284 | RELEASE
15285 | RENAME
15286 | REPEATABLE
15287 | REPLACE
15288 | REPLICA
15289 | RESET
15290 | RESTART
15291 | RESTRICT
15292 | RETURNS
15293 | REVOKE
15294 | ROLE
15295 | ROLLBACK
15296 | ROLLUP
15297 | ROUTINE
15298 | ROUTINES
15299 | ROWS
15300 | RULE
15301 | SAVEPOINT
15302 | SCHEMA
15303 | SCHEMAS
15304 | SCROLL
15305 | SEARCH
15306 | SECOND_P
15307 | SECURITY
15308 | SEQUENCE
15309 | SEQUENCES
15310 | SERIALIZABLE
15311 | SERVER
15312 | SESSION
15313 | SET
15314 | SETS
15315 | SHARE
15316 | SHOW
15317 | SIMPLE
15318 | SKIP
15319 | SNAPSHOT
15320 | SQL_P
15321 | STABLE
15322 | STANDALONE_P
15323 | START
15324 | STATEMENT
15325 | STATISTICS
15326 | STDIN
15327 | STDOUT
15328 | STORAGE
15329 | STORED
15330 | STRICT_P
15331 | STRIP_P
15332 | SUBSCRIPTION
15333 | SUPPORT
15334 | SYSID
15335 | SYSTEM_P
15336 | TABLES
15337 | TABLESPACE
15338 | TEMP
15339 | TEMPLATE
15340 | TEMPORARY
15341 | TEXT_P
15342 | TIES
15343 | TRANSACTION
15344 | TRANSFORM
15345 | TRIGGER
15346 | TRUNCATE
15347 | TRUSTED
15348 | TYPE_P
15349 | TYPES_P
15350 | UNBOUNDED
15351 | UNCOMMITTED
15352 | UNENCRYPTED
15353 | UNKNOWN
15354 | UNLISTEN
15355 | UNLOGGED
15356 | UNTIL
15357 | UPDATE
15358 | VACUUM
15359 | VALID
15360 | VALIDATE
15361 | VALIDATOR
15362 | VALUE_P
15363 | VARYING
15364 | VERSION_P
15365 | VIEW
15366 | VIEWS
15367 | VOLATILE
15368 | WHITESPACE_P
15369 | WITHIN
15370 | WITHOUT
15371 | WORK
15372 | WRAPPER
15373 | WRITE
15374 | XML_P
15375 | YEAR_P
15376 | YES_P
15377 | ZONE
15378 ;
15379
15380 /* Column identifier --- keywords that can be column, table, etc names.
15381 *
15382 * Many of these keywords will in fact be recognized as type or function
15383 * names too; but they have special productions for the purpose, and so
15384 * can't be treated as "generic" type or function names.
15385 *
15386 * The type names appearing here are not usable as function names
15387 * because they can be followed by '(' in typename productions, which
15388 * looks too much like a function call for an LR(1) parser.
15389 */
15390 col_name_keyword:
15391 BETWEEN
15392 | BIGINT
15393 | BIT
15394 | BOOLEAN_P
15395 | CHAR_P
15396 | CHARACTER
15397 | COALESCE
15398 | DEC
15399 | DECIMAL_P
15400 | EXISTS
15401 | EXTRACT
15402 | FLOAT_P
15403 | GREATEST
15404 | GROUPING
15405 | INOUT
15406 | INT_P
15407 | INTEGER
15408 | INTERVAL
15409 | LEAST
15410 | NATIONAL
15411 | NCHAR
15412 | NONE
15413 | NULLIF
15414 | NUMERIC
15415 | OUT_P
15416 | OVERLAY
15417 | POSITION
15418 | PRECISION
15419 | REAL
15420 | ROW
15421 | SETOF
15422 | SMALLINT
15423 | SUBSTRING
15424 | TIME
15425 | TIMESTAMP
15426 | TREAT
15427 | TRIM
15428 | VALUES
15429 | VARCHAR
15430 | XMLATTRIBUTES
15431 | XMLCONCAT
15432 | XMLELEMENT
15433 | XMLEXISTS
15434 | XMLFOREST
15435 | XMLNAMESPACES
15436 | XMLPARSE
15437 | XMLPI
15438 | XMLROOT
15439 | XMLSERIALIZE
15440 | XMLTABLE
15441 ;
15442
15443 /* Type/function identifier --- keywords that can be type or function names.
15444 *
15445 * Most of these are keywords that are used as operators in expressions;
15446 * in general such keywords can't be column names because they would be
15447 * ambiguous with variables, but they are unambiguous as function identifiers.
15448 *
15449 * Do not include POSITION, SUBSTRING, etc here since they have explicit
15450 * productions in a_expr to support the goofy SQL9x argument syntax.
15451 * - thomas 2000-11-28
15452 */
15453 type_func_name_keyword:
15454 AUTHORIZATION
15455 | BINARY
15456 | COLLATION
15457 | CONCURRENTLY
15458 | CROSS
15459 | CURRENT_SCHEMA
15460 | FREEZE
15461 | FULL
15462 | ILIKE
15463 | INNER_P
15464 | IS
15465 | ISNULL
15466 | JOIN
15467 | LEFT
15468 | LIKE
15469 | NATURAL
15470 | NOTNULL
15471 | OUTER_P
15472 | OVERLAPS
15473 | RIGHT
15474 | SIMILAR
15475 | TABLESAMPLE
15476 | VERBOSE
15477 ;
15478
15479 /* Reserved keyword --- these keywords are usable only as a ColLabel.
15480 *
15481 * Keywords appear here if they could not be distinguished from variable,
15482 * type, or function names in some contexts. Don't put things here unless
15483 * forced to.
15484 */
15485 reserved_keyword:
15486 ALL
15487 | ANALYSE
15488 | ANALYZE
15489 | AND
15490 | ANY
15491 | ARRAY
15492 | AS
15493 | ASC
15494 | ASYMMETRIC
15495 | BOTH
15496 | CASE
15497 | CAST
15498 | CHECK
15499 | COLLATE
15500 | COLUMN
15501 | CONSTRAINT
15502 | CREATE
15503 | CURRENT_CATALOG
15504 | CURRENT_DATE
15505 | CURRENT_ROLE
15506 | CURRENT_TIME
15507 | CURRENT_TIMESTAMP
15508 | CURRENT_USER
15509 | DEFAULT
15510 | DEFERRABLE
15511 | DESC
15512 | DISTINCT
15513 | DO
15514 | ELSE
15515 | END_P
15516 | EXCEPT
15517 | FALSE_P
15518 | FETCH
15519 | FOR
15520 | FOREIGN
15521 | FROM
15522 | GRANT
15523 | GROUP_P
15524 | HAVING
15525 | IN_P
15526 | INITIALLY
15527 | INTERSECT
15528 | INTO
15529 | LATERAL_P
15530 | LEADING
15531 | LIMIT
15532 | LOCALTIME
15533 | LOCALTIMESTAMP
15534 | NOT
15535 | NULL_P
15536 | OFFSET
15537 | ON
15538 | ONLY
15539 | OR
15540 | ORDER
15541 | PLACING
15542 | PRIMARY
15543 | REFERENCES
15544 | RETURNING
15545 | SELECT
15546 | SESSION_USER
15547 | SOME
15548 | SYMMETRIC
15549 | TABLE
15550 | THEN
15551 | TO
15552 | TRAILING
15553 | TRUE_P
15554 | UNION
15555 | UNIQUE
15556 | USER
15557 | USING
15558 | VARIADIC
15559 | WHEN
15560 | WHERE
15561 | WINDOW
15562 | WITH
15563 ;
15564
15565 %%
15566
15567 /*
15568 * The signature of this function is required by bison. However, we
15569 * ignore the passed yylloc and instead use the last token position
15570 * available from the scanner.
15571 */
15572 static void
15573 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
15574 {
15575 parser_yyerror(msg);
15576 }
15577
15578 static RawStmt *
makeRawStmt(Node * stmt,int stmt_location)15579 makeRawStmt(Node *stmt, int stmt_location)
15580 {
15581 RawStmt *rs = makeNode(RawStmt);
15582
15583 rs->stmt = stmt;
15584 rs->stmt_location = stmt_location;
15585 rs->stmt_len = 0; /* might get changed later */
15586 return rs;
15587 }
15588
15589 /* Adjust a RawStmt to reflect that it doesn't run to the end of the string */
15590 static void
updateRawStmtEnd(RawStmt * rs,int end_location)15591 updateRawStmtEnd(RawStmt *rs, int end_location)
15592 {
15593 /*
15594 * If we already set the length, don't change it. This is for situations
15595 * like "select foo ;; select bar" where the same statement will be last
15596 * in the string for more than one semicolon.
15597 */
15598 if (rs->stmt_len > 0)
15599 return;
15600
15601 /* OK, update length of RawStmt */
15602 rs->stmt_len = end_location - rs->stmt_location;
15603 }
15604
15605 static Node *
makeColumnRef(char * colname,List * indirection,int location,core_yyscan_t yyscanner)15606 makeColumnRef(char *colname, List *indirection,
15607 int location, core_yyscan_t yyscanner)
15608 {
15609 /*
15610 * Generate a ColumnRef node, with an A_Indirection node added if there
15611 * is any subscripting in the specified indirection list. However,
15612 * any field selection at the start of the indirection list must be
15613 * transposed into the "fields" part of the ColumnRef node.
15614 */
15615 ColumnRef *c = makeNode(ColumnRef);
15616 int nfields = 0;
15617 ListCell *l;
15618
15619 c->location = location;
15620 foreach(l, indirection)
15621 {
15622 if (IsA(lfirst(l), A_Indices))
15623 {
15624 A_Indirection *i = makeNode(A_Indirection);
15625
15626 if (nfields == 0)
15627 {
15628 /* easy case - all indirection goes to A_Indirection */
15629 c->fields = list_make1(makeString(colname));
15630 i->indirection = check_indirection(indirection, yyscanner);
15631 }
15632 else
15633 {
15634 /* got to split the list in two */
15635 i->indirection = check_indirection(list_copy_tail(indirection,
15636 nfields),
15637 yyscanner);
15638 indirection = list_truncate(indirection, nfields);
15639 c->fields = lcons(makeString(colname), indirection);
15640 }
15641 i->arg = (Node *) c;
15642 return (Node *) i;
15643 }
15644 else if (IsA(lfirst(l), A_Star))
15645 {
15646 /* We only allow '*' at the end of a ColumnRef */
15647 if (lnext(l) != NULL)
15648 parser_yyerror("improper use of \"*\"");
15649 }
15650 nfields++;
15651 }
15652 /* No subscripting, so all indirection gets added to field list */
15653 c->fields = lcons(makeString(colname), indirection);
15654 return (Node *) c;
15655 }
15656
15657 Node *
makeTypeCast(Node * arg,TypeName * typename,int location)15658 makeTypeCast(Node *arg, TypeName *typename, int location)
15659 {
15660 TypeCast *n = makeNode(TypeCast);
15661 n->arg = arg;
15662 n->typeName = typename;
15663 n->location = location;
15664 return (Node *) n;
15665 }
15666
15667 static Node *
makeStringConst(char * str,int location)15668 makeStringConst(char *str, int location)
15669 {
15670 A_Const *n = makeNode(A_Const);
15671
15672 n->val.type = T_String;
15673 n->val.val.str = str;
15674 n->location = location;
15675
15676 return (Node *)n;
15677 }
15678
15679 Node *
makeStringConstCast(char * str,int location,TypeName * typename)15680 makeStringConstCast(char *str, int location, TypeName *typename)
15681 {
15682 Node *s = makeStringConst(str, location);
15683
15684 return makeTypeCast(s, typename, -1);
15685 }
15686
15687 Node *
makeIntConst(int val,int location)15688 makeIntConst(int val, int location)
15689 {
15690 A_Const *n = makeNode(A_Const);
15691
15692 n->val.type = T_Integer;
15693 n->val.val.ival = val;
15694 n->location = location;
15695
15696 return (Node *)n;
15697 }
15698
15699 static Node *
makeFloatConst(char * str,int location)15700 makeFloatConst(char *str, int location)
15701 {
15702 A_Const *n = makeNode(A_Const);
15703
15704 n->val.type = T_Float;
15705 n->val.val.str = str;
15706 n->location = location;
15707
15708 return (Node *)n;
15709 }
15710
15711 static Node *
makeBitStringConst(char * str,int location)15712 makeBitStringConst(char *str, int location)
15713 {
15714 A_Const *n = makeNode(A_Const);
15715
15716 n->val.type = T_BitString;
15717 n->val.val.str = str;
15718 n->location = location;
15719
15720 return (Node *)n;
15721 }
15722
15723 static Node *
makeNullAConst(int location)15724 makeNullAConst(int location)
15725 {
15726 A_Const *n = makeNode(A_Const);
15727
15728 n->val.type = T_Null;
15729 n->location = location;
15730
15731 return (Node *)n;
15732 }
15733
15734 static Node *
makeAConst(Value * v,int location)15735 makeAConst(Value *v, int location)
15736 {
15737 Node *n;
15738
15739 switch (v->type)
15740 {
15741 case T_Float:
15742 n = makeFloatConst(v->val.str, location);
15743 break;
15744
15745 case T_Integer:
15746 n = makeIntConst(v->val.ival, location);
15747 break;
15748
15749 case T_String:
15750 default:
15751 n = makeStringConst(v->val.str, location);
15752 break;
15753 }
15754
15755 return n;
15756 }
15757
15758 /* makeBoolAConst()
15759 * Create an A_Const string node and put it inside a boolean cast.
15760 */
15761 static Node *
makeBoolAConst(bool state,int location)15762 makeBoolAConst(bool state, int location)
15763 {
15764 A_Const *n = makeNode(A_Const);
15765
15766 n->val.type = T_String;
15767 n->val.val.str = (state ? "t" : "f");
15768 n->location = location;
15769
15770 return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
15771 }
15772
15773 /* makeRoleSpec
15774 * Create a RoleSpec with the given type
15775 */
15776 static RoleSpec *
makeRoleSpec(RoleSpecType type,int location)15777 makeRoleSpec(RoleSpecType type, int location)
15778 {
15779 RoleSpec *spec = makeNode(RoleSpec);
15780
15781 spec->roletype = type;
15782 spec->location = location;
15783
15784 return spec;
15785 }
15786
15787 /* check_qualified_name --- check the result of qualified_name production
15788 *
15789 * It's easiest to let the grammar production for qualified_name allow
15790 * subscripts and '*', which we then must reject here.
15791 */
15792 static void
check_qualified_name(List * names,core_yyscan_t yyscanner)15793 check_qualified_name(List *names, core_yyscan_t yyscanner)
15794 {
15795 ListCell *i;
15796
15797 foreach(i, names)
15798 {
15799 if (!IsA(lfirst(i), String))
15800 parser_yyerror("syntax error");
15801 }
15802 }
15803
15804 /* check_func_name --- check the result of func_name production
15805 *
15806 * It's easiest to let the grammar production for func_name allow subscripts
15807 * and '*', which we then must reject here.
15808 */
15809 static List *
check_func_name(List * names,core_yyscan_t yyscanner)15810 check_func_name(List *names, core_yyscan_t yyscanner)
15811 {
15812 ListCell *i;
15813
15814 foreach(i, names)
15815 {
15816 if (!IsA(lfirst(i), String))
15817 parser_yyerror("syntax error");
15818 }
15819 return names;
15820 }
15821
15822 /* check_indirection --- check the result of indirection production
15823 *
15824 * We only allow '*' at the end of the list, but it's hard to enforce that
15825 * in the grammar, so do it here.
15826 */
15827 static List *
check_indirection(List * indirection,core_yyscan_t yyscanner)15828 check_indirection(List *indirection, core_yyscan_t yyscanner)
15829 {
15830 ListCell *l;
15831
15832 foreach(l, indirection)
15833 {
15834 if (IsA(lfirst(l), A_Star))
15835 {
15836 if (lnext(l) != NULL)
15837 parser_yyerror("improper use of \"*\"");
15838 }
15839 }
15840 return indirection;
15841 }
15842
15843 /* extractArgTypes()
15844 * Given a list of FunctionParameter nodes, extract a list of just the
15845 * argument types (TypeNames) for input parameters only. This is what
15846 * is needed to look up an existing function, which is what is wanted by
15847 * the productions that use this call.
15848 */
15849 static List *
extractArgTypes(List * parameters)15850 extractArgTypes(List *parameters)
15851 {
15852 List *result = NIL;
15853 ListCell *i;
15854
15855 foreach(i, parameters)
15856 {
15857 FunctionParameter *p = (FunctionParameter *) lfirst(i);
15858
15859 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
15860 result = lappend(result, p->argType);
15861 }
15862 return result;
15863 }
15864
15865 /* extractAggrArgTypes()
15866 * As above, but work from the output of the aggr_args production.
15867 */
15868 static List *
extractAggrArgTypes(List * aggrargs)15869 extractAggrArgTypes(List *aggrargs)
15870 {
15871 Assert(list_length(aggrargs) == 2);
15872 return extractArgTypes((List *) linitial(aggrargs));
15873 }
15874
15875 /* makeOrderedSetArgs()
15876 * Build the result of the aggr_args production (which see the comments for).
15877 * This handles only the case where both given lists are nonempty, so that
15878 * we have to deal with multiple VARIADIC arguments.
15879 */
15880 static List *
makeOrderedSetArgs(List * directargs,List * orderedargs,core_yyscan_t yyscanner)15881 makeOrderedSetArgs(List *directargs, List *orderedargs,
15882 core_yyscan_t yyscanner)
15883 {
15884 FunctionParameter *lastd = (FunctionParameter *) llast(directargs);
15885 int ndirectargs;
15886
15887 /* No restriction unless last direct arg is VARIADIC */
15888 if (lastd->mode == FUNC_PARAM_VARIADIC)
15889 {
15890 FunctionParameter *firsto = (FunctionParameter *) linitial(orderedargs);
15891
15892 /*
15893 * We ignore the names, though the aggr_arg production allows them;
15894 * it doesn't allow default values, so those need not be checked.
15895 */
15896 if (list_length(orderedargs) != 1 ||
15897 firsto->mode != FUNC_PARAM_VARIADIC)
15898 ereport(ERROR,
15899 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15900 errmsg("an ordered-set aggregate with a VARIADIC direct argument must have one VARIADIC aggregated argument of the same data type"),
15901 parser_errposition(exprLocation((Node *) firsto))));
15902
15903 /* OK, drop the duplicate VARIADIC argument from the internal form */
15904 orderedargs = NIL;
15905 }
15906
15907 /* don't merge into the next line, as list_concat changes directargs */
15908 ndirectargs = list_length(directargs);
15909
15910 return list_make2(list_concat(directargs, orderedargs),
15911 makeInteger(ndirectargs));
15912 }
15913
15914 /* insertSelectOptions()
15915 * Insert ORDER BY, etc into an already-constructed SelectStmt.
15916 *
15917 * This routine is just to avoid duplicating code in SelectStmt productions.
15918 */
15919 static void
insertSelectOptions(SelectStmt * stmt,List * sortClause,List * lockingClause,Node * limitOffset,Node * limitCount,WithClause * withClause,core_yyscan_t yyscanner)15920 insertSelectOptions(SelectStmt *stmt,
15921 List *sortClause, List *lockingClause,
15922 Node *limitOffset, Node *limitCount,
15923 WithClause *withClause,
15924 core_yyscan_t yyscanner)
15925 {
15926 Assert(IsA(stmt, SelectStmt));
15927
15928 /*
15929 * Tests here are to reject constructs like
15930 * (SELECT foo ORDER BY bar) ORDER BY baz
15931 */
15932 if (sortClause)
15933 {
15934 if (stmt->sortClause)
15935 ereport(ERROR,
15936 (errcode(ERRCODE_SYNTAX_ERROR),
15937 errmsg("multiple ORDER BY clauses not allowed"),
15938 parser_errposition(exprLocation((Node *) sortClause))));
15939 stmt->sortClause = sortClause;
15940 }
15941 /* We can handle multiple locking clauses, though */
15942 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
15943 if (limitOffset)
15944 {
15945 if (stmt->limitOffset)
15946 ereport(ERROR,
15947 (errcode(ERRCODE_SYNTAX_ERROR),
15948 errmsg("multiple OFFSET clauses not allowed"),
15949 parser_errposition(exprLocation(limitOffset))));
15950 stmt->limitOffset = limitOffset;
15951 }
15952 if (limitCount)
15953 {
15954 if (stmt->limitCount)
15955 ereport(ERROR,
15956 (errcode(ERRCODE_SYNTAX_ERROR),
15957 errmsg("multiple LIMIT clauses not allowed"),
15958 parser_errposition(exprLocation(limitCount))));
15959 stmt->limitCount = limitCount;
15960 }
15961 if (withClause)
15962 {
15963 if (stmt->withClause)
15964 ereport(ERROR,
15965 (errcode(ERRCODE_SYNTAX_ERROR),
15966 errmsg("multiple WITH clauses not allowed"),
15967 parser_errposition(exprLocation((Node *) withClause))));
15968 stmt->withClause = withClause;
15969 }
15970 }
15971
15972 static Node *
makeSetOp(SetOperation op,bool all,Node * larg,Node * rarg)15973 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
15974 {
15975 SelectStmt *n = makeNode(SelectStmt);
15976
15977 n->op = op;
15978 n->all = all;
15979 n->larg = (SelectStmt *) larg;
15980 n->rarg = (SelectStmt *) rarg;
15981 return (Node *) n;
15982 }
15983
15984 /* SystemFuncName()
15985 * Build a properly-qualified reference to a built-in function.
15986 */
15987 List *
SystemFuncName(char * name)15988 SystemFuncName(char *name)
15989 {
15990 return list_make2(makeString("pg_catalog"), makeString(name));
15991 }
15992
15993 /* SystemTypeName()
15994 * Build a properly-qualified reference to a built-in type.
15995 *
15996 * typmod is defaulted, but may be changed afterwards by caller.
15997 * Likewise for the location.
15998 */
15999 TypeName *
SystemTypeName(char * name)16000 SystemTypeName(char *name)
16001 {
16002 return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
16003 makeString(name)));
16004 }
16005 /* doNegate()
16006 * Handle negation of a numeric constant.
16007 *
16008 * Formerly, we did this here because the optimizer couldn't cope with
16009 * indexquals that looked like "var = -4" --- it wants "var = const"
16010 * and a unary minus operator applied to a constant didn't qualify.
16011 * As of Postgres 7.0, that problem doesn't exist anymore because there
16012 * is a constant-subexpression simplifier in the optimizer. However,
16013 * there's still a good reason for doing this here, which is that we can
16014 * postpone committing to a particular internal representation for simple
16015 * negative constants. It's better to leave "-123.456" in string form
16016 * until we know what the desired type is.
16017 */
16018 static Node *
doNegate(Node * n,int location)16019 doNegate(Node *n, int location)
16020 {
16021 if (IsA(n, A_Const))
16022 {
16023 A_Const *con = (A_Const *)n;
16024
16025 /* report the constant's location as that of the '-' sign */
16026 con->location = location;
16027
16028 if (con->val.type == T_Integer)
16029 {
16030 con->val.val.ival = -con->val.val.ival;
16031 return n;
16032 }
16033 if (con->val.type == T_Float)
16034 {
16035 doNegateFloat(&con->val);
16036 return n;
16037 }
16038 }
16039
16040 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
16041 }
16042
16043 static void
doNegateFloat(Value * v)16044 doNegateFloat(Value *v)
16045 {
16046 char *oldval = v->val.str;
16047
16048 Assert(IsA(v, Float));
16049 if (*oldval == '+')
16050 oldval++;
16051 if (*oldval == '-')
16052 v->val.str = oldval+1; /* just strip the '-' */
16053 else
16054 v->val.str = psprintf("-%s", oldval);
16055 }
16056
16057 static Node *
makeAndExpr(Node * lexpr,Node * rexpr,int location)16058 makeAndExpr(Node *lexpr, Node *rexpr, int location)
16059 {
16060 Node *lexp = lexpr;
16061
16062 /* Look through AEXPR_PAREN nodes so they don't affect flattening */
16063 while (IsA(lexp, A_Expr) &&
16064 ((A_Expr *) lexp)->kind == AEXPR_PAREN)
16065 lexp = ((A_Expr *) lexp)->lexpr;
16066 /* Flatten "a AND b AND c ..." to a single BoolExpr on sight */
16067 if (IsA(lexp, BoolExpr))
16068 {
16069 BoolExpr *blexpr = (BoolExpr *) lexp;
16070
16071 if (blexpr->boolop == AND_EXPR)
16072 {
16073 blexpr->args = lappend(blexpr->args, rexpr);
16074 return (Node *) blexpr;
16075 }
16076 }
16077 return (Node *) makeBoolExpr(AND_EXPR, list_make2(lexpr, rexpr), location);
16078 }
16079
16080 static Node *
makeOrExpr(Node * lexpr,Node * rexpr,int location)16081 makeOrExpr(Node *lexpr, Node *rexpr, int location)
16082 {
16083 Node *lexp = lexpr;
16084
16085 /* Look through AEXPR_PAREN nodes so they don't affect flattening */
16086 while (IsA(lexp, A_Expr) &&
16087 ((A_Expr *) lexp)->kind == AEXPR_PAREN)
16088 lexp = ((A_Expr *) lexp)->lexpr;
16089 /* Flatten "a OR b OR c ..." to a single BoolExpr on sight */
16090 if (IsA(lexp, BoolExpr))
16091 {
16092 BoolExpr *blexpr = (BoolExpr *) lexp;
16093
16094 if (blexpr->boolop == OR_EXPR)
16095 {
16096 blexpr->args = lappend(blexpr->args, rexpr);
16097 return (Node *) blexpr;
16098 }
16099 }
16100 return (Node *) makeBoolExpr(OR_EXPR, list_make2(lexpr, rexpr), location);
16101 }
16102
16103 static Node *
makeNotExpr(Node * expr,int location)16104 makeNotExpr(Node *expr, int location)
16105 {
16106 return (Node *) makeBoolExpr(NOT_EXPR, list_make1(expr), location);
16107 }
16108
16109 static Node *
makeAArrayExpr(List * elements,int location)16110 makeAArrayExpr(List *elements, int location)
16111 {
16112 A_ArrayExpr *n = makeNode(A_ArrayExpr);
16113
16114 n->elements = elements;
16115 n->location = location;
16116 return (Node *) n;
16117 }
16118
16119 static Node *
makeSQLValueFunction(SQLValueFunctionOp op,int32 typmod,int location)16120 makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location)
16121 {
16122 SQLValueFunction *svf = makeNode(SQLValueFunction);
16123
16124 svf->op = op;
16125 /* svf->type will be filled during parse analysis */
16126 svf->typmod = typmod;
16127 svf->location = location;
16128 return (Node *) svf;
16129 }
16130
16131 static Node *
makeXmlExpr(XmlExprOp op,char * name,List * named_args,List * args,int location)16132 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
16133 int location)
16134 {
16135 XmlExpr *x = makeNode(XmlExpr);
16136
16137 x->op = op;
16138 x->name = name;
16139 /*
16140 * named_args is a list of ResTarget; it'll be split apart into separate
16141 * expression and name lists in transformXmlExpr().
16142 */
16143 x->named_args = named_args;
16144 x->arg_names = NIL;
16145 x->args = args;
16146 /* xmloption, if relevant, must be filled in by caller */
16147 /* type and typmod will be filled in during parse analysis */
16148 x->type = InvalidOid; /* marks the node as not analyzed */
16149 x->location = location;
16150 return (Node *) x;
16151 }
16152
16153 /*
16154 * Merge the input and output parameters of a table function.
16155 */
16156 static List *
mergeTableFuncParameters(List * func_args,List * columns)16157 mergeTableFuncParameters(List *func_args, List *columns)
16158 {
16159 ListCell *lc;
16160
16161 /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
16162 foreach(lc, func_args)
16163 {
16164 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
16165
16166 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
16167 ereport(ERROR,
16168 (errcode(ERRCODE_SYNTAX_ERROR),
16169 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
16170 }
16171
16172 return list_concat(func_args, columns);
16173 }
16174
16175 /*
16176 * Determine return type of a TABLE function. A single result column
16177 * returns setof that column's type; otherwise return setof record.
16178 */
16179 static TypeName *
TableFuncTypeName(List * columns)16180 TableFuncTypeName(List *columns)
16181 {
16182 TypeName *result;
16183
16184 if (list_length(columns) == 1)
16185 {
16186 FunctionParameter *p = (FunctionParameter *) linitial(columns);
16187
16188 result = copyObject(p->argType);
16189 }
16190 else
16191 result = SystemTypeName("record");
16192
16193 result->setof = true;
16194
16195 return result;
16196 }
16197
16198 /*
16199 * Convert a list of (dotted) names to a RangeVar (like
16200 * makeRangeVarFromNameList, but with position support). The
16201 * "AnyName" refers to the any_name production in the grammar.
16202 */
16203 static RangeVar *
makeRangeVarFromAnyName(List * names,int position,core_yyscan_t yyscanner)16204 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
16205 {
16206 RangeVar *r = makeNode(RangeVar);
16207
16208 switch (list_length(names))
16209 {
16210 case 1:
16211 r->catalogname = NULL;
16212 r->schemaname = NULL;
16213 r->relname = strVal(linitial(names));
16214 break;
16215 case 2:
16216 r->catalogname = NULL;
16217 r->schemaname = strVal(linitial(names));
16218 r->relname = strVal(lsecond(names));
16219 break;
16220 case 3:
16221 r->catalogname = strVal(linitial(names));
16222 r->schemaname = strVal(lsecond(names));
16223 r->relname = strVal(lthird(names));
16224 break;
16225 default:
16226 ereport(ERROR,
16227 (errcode(ERRCODE_SYNTAX_ERROR),
16228 errmsg("improper qualified name (too many dotted names): %s",
16229 NameListToString(names)),
16230 parser_errposition(position)));
16231 break;
16232 }
16233
16234 r->relpersistence = RELPERSISTENCE_PERMANENT;
16235 r->location = position;
16236
16237 return r;
16238 }
16239
16240 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
16241 static void
SplitColQualList(List * qualList,List ** constraintList,CollateClause ** collClause,core_yyscan_t yyscanner)16242 SplitColQualList(List *qualList,
16243 List **constraintList, CollateClause **collClause,
16244 core_yyscan_t yyscanner)
16245 {
16246 ListCell *cell;
16247 ListCell *prev;
16248 ListCell *next;
16249
16250 *collClause = NULL;
16251 prev = NULL;
16252 for (cell = list_head(qualList); cell; cell = next)
16253 {
16254 Node *n = (Node *) lfirst(cell);
16255
16256 next = lnext(cell);
16257 if (IsA(n, Constraint))
16258 {
16259 /* keep it in list */
16260 prev = cell;
16261 continue;
16262 }
16263 if (IsA(n, CollateClause))
16264 {
16265 CollateClause *c = (CollateClause *) n;
16266
16267 if (*collClause)
16268 ereport(ERROR,
16269 (errcode(ERRCODE_SYNTAX_ERROR),
16270 errmsg("multiple COLLATE clauses not allowed"),
16271 parser_errposition(c->location)));
16272 *collClause = c;
16273 }
16274 else
16275 elog(ERROR, "unexpected node type %d", (int) n->type);
16276 /* remove non-Constraint nodes from qualList */
16277 qualList = list_delete_cell(qualList, cell, prev);
16278 }
16279 *constraintList = qualList;
16280 }
16281
16282 /*
16283 * Process result of ConstraintAttributeSpec, and set appropriate bool flags
16284 * in the output command node. Pass NULL for any flags the particular
16285 * command doesn't support.
16286 */
16287 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)16288 processCASbits(int cas_bits, int location, const char *constrType,
16289 bool *deferrable, bool *initdeferred, bool *not_valid,
16290 bool *no_inherit, core_yyscan_t yyscanner)
16291 {
16292 /* defaults */
16293 if (deferrable)
16294 *deferrable = false;
16295 if (initdeferred)
16296 *initdeferred = false;
16297 if (not_valid)
16298 *not_valid = false;
16299
16300 if (cas_bits & (CAS_DEFERRABLE | CAS_INITIALLY_DEFERRED))
16301 {
16302 if (deferrable)
16303 *deferrable = true;
16304 else
16305 ereport(ERROR,
16306 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16307 /* translator: %s is CHECK, UNIQUE, or similar */
16308 errmsg("%s constraints cannot be marked DEFERRABLE",
16309 constrType),
16310 parser_errposition(location)));
16311 }
16312
16313 if (cas_bits & CAS_INITIALLY_DEFERRED)
16314 {
16315 if (initdeferred)
16316 *initdeferred = true;
16317 else
16318 ereport(ERROR,
16319 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16320 /* translator: %s is CHECK, UNIQUE, or similar */
16321 errmsg("%s constraints cannot be marked DEFERRABLE",
16322 constrType),
16323 parser_errposition(location)));
16324 }
16325
16326 if (cas_bits & CAS_NOT_VALID)
16327 {
16328 if (not_valid)
16329 *not_valid = true;
16330 else
16331 ereport(ERROR,
16332 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16333 /* translator: %s is CHECK, UNIQUE, or similar */
16334 errmsg("%s constraints cannot be marked NOT VALID",
16335 constrType),
16336 parser_errposition(location)));
16337 }
16338
16339 if (cas_bits & CAS_NO_INHERIT)
16340 {
16341 if (no_inherit)
16342 *no_inherit = true;
16343 else
16344 ereport(ERROR,
16345 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
16346 /* translator: %s is CHECK, UNIQUE, or similar */
16347 errmsg("%s constraints cannot be marked NO INHERIT",
16348 constrType),
16349 parser_errposition(location)));
16350 }
16351 }
16352
16353 /*----------
16354 * Recursive view transformation
16355 *
16356 * Convert
16357 *
16358 * CREATE RECURSIVE VIEW relname (aliases) AS query
16359 *
16360 * to
16361 *
16362 * CREATE VIEW relname (aliases) AS
16363 * WITH RECURSIVE relname (aliases) AS (query)
16364 * SELECT aliases FROM relname
16365 *
16366 * Actually, just the WITH ... part, which is then inserted into the original
16367 * view definition as the query.
16368 * ----------
16369 */
16370 static Node *
makeRecursiveViewSelect(char * relname,List * aliases,Node * query)16371 makeRecursiveViewSelect(char *relname, List *aliases, Node *query)
16372 {
16373 SelectStmt *s = makeNode(SelectStmt);
16374 WithClause *w = makeNode(WithClause);
16375 CommonTableExpr *cte = makeNode(CommonTableExpr);
16376 List *tl = NIL;
16377 ListCell *lc;
16378
16379 /* create common table expression */
16380 cte->ctename = relname;
16381 cte->aliascolnames = aliases;
16382 cte->ctematerialized = CTEMaterializeDefault;
16383 cte->ctequery = query;
16384 cte->location = -1;
16385
16386 /* create WITH clause and attach CTE */
16387 w->recursive = true;
16388 w->ctes = list_make1(cte);
16389 w->location = -1;
16390
16391 /* create target list for the new SELECT from the alias list of the
16392 * recursive view specification */
16393 foreach (lc, aliases)
16394 {
16395 ResTarget *rt = makeNode(ResTarget);
16396
16397 rt->name = NULL;
16398 rt->indirection = NIL;
16399 rt->val = makeColumnRef(strVal(lfirst(lc)), NIL, -1, 0);
16400 rt->location = -1;
16401
16402 tl = lappend(tl, rt);
16403 }
16404
16405 /* create new SELECT combining WITH clause, target list, and fake FROM
16406 * clause */
16407 s->withClause = w;
16408 s->targetList = tl;
16409 s->fromClause = list_make1(makeRangeVar(NULL, relname, -1));
16410
16411 return (Node *) s;
16412 }
16413
16414 /* parser_init()
16415 * Initialize to parse one query string
16416 */
16417 void
parser_init(base_yy_extra_type * yyext)16418 parser_init(base_yy_extra_type *yyext)
16419 {
16420 yyext->parsetree = NIL; /* in case grammar forgets to set it */
16421 }
16422
16423