]> granicus.if.org Git - postgresql/blob - src/backend/parser/gram.y
dd959618419032acd3df1fa14644c9b2052dd462
[postgresql] / src / backend / parser / gram.y
1 %{
2
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
5  *
6  * gram.y
7  *        POSTGRESQL BISON rules/actions
8  *
9  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  *
13  * IDENTIFICATION
14  *        src/backend/parser/gram.y
15  *
16  * HISTORY
17  *        AUTHOR                        DATE                    MAJOR EVENT
18  *        Andrew Yu                     Sept, 1994              POSTQUEL to SQL conversion
19  *        Andrew Yu                     Oct, 1994               lispy code conversion
20  *
21  * NOTES
22  *        CAPITALS are used to represent terminal symbols.
23  *        non-capitals are used to represent non-terminals.
24  *        SQL92-specific syntax is separated from plain SQL/Postgres syntax
25  *        to help isolate the non-extensible portions of the parser.
26  *
27  *        In general, nothing in this file should initiate database accesses
28  *        nor depend on changeable state (such as SET variables).  If you do
29  *        database accesses, your code will fail when we have aborted the
30  *        current transaction and are just parsing commands to find the next
31  *        ROLLBACK or COMMIT.  If you make use of SET variables, then you
32  *        will do the wrong thing in multi-query strings like this:
33  *                      SET SQL_inheritance TO off; SELECT * FROM foo;
34  *        because the entire string is parsed by gram.y before the SET gets
35  *        executed.  Anything that depends on the database or changeable state
36  *        should be handled during parse analysis so that it happens at the
37  *        right time not the wrong time.  The handling of SQL_inheritance is
38  *        a good example.
39  *
40  * WARNINGS
41  *        If you use a list, make sure the datum is a node so that the printing
42  *        routines work.
43  *
44  *        Sometimes we assign constants to makeStrings. Make sure we don't free
45  *        those.
46  *
47  *-------------------------------------------------------------------------
48  */
49 #include "postgres.h"
50
51 #include <ctype.h>
52 #include <limits.h>
53
54 #include "catalog/index.h"
55 #include "catalog/namespace.h"
56 #include "catalog/pg_trigger.h"
57 #include "commands/defrem.h"
58 #include "nodes/makefuncs.h"
59 #include "nodes/nodeFuncs.h"
60 #include "parser/gramparse.h"
61 #include "parser/parser.h"
62 #include "storage/lmgr.h"
63 #include "utils/date.h"
64 #include "utils/datetime.h"
65 #include "utils/numeric.h"
66 #include "utils/xml.h"
67
68
69 /* Location tracking support --- simpler than bison's default */
70 #define YYLLOC_DEFAULT(Current, Rhs, N) \
71         do { \
72                 if (N) \
73                         (Current) = (Rhs)[1]; \
74                 else \
75                         (Current) = (Rhs)[0]; \
76         } while (0)
77
78 /*
79  * Bison doesn't allocate anything that needs to live across parser calls,
80  * so we can easily have it use palloc instead of malloc.  This prevents
81  * memory leaks if we error out during parsing.  Note this only works with
82  * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
83  * if possible, so there's not really much problem anyhow, at least if
84  * you're building with gcc.
85  */
86 #define YYMALLOC palloc
87 #define YYFREE   pfree
88
89 /* Private struct for the result of privilege_target production */
90 typedef struct PrivTarget
91 {
92         GrantTargetType targtype;
93         GrantObjectType objtype;
94         List       *objs;
95 } PrivTarget;
96
97
98 #define parser_yyerror(msg)  scanner_yyerror(msg, yyscanner)
99 #define parser_errposition(pos)  scanner_errposition(pos, yyscanner)
100
101 static void base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner,
102                                                  const char *msg);
103 static Node *makeColumnRef(char *colname, List *indirection,
104                                                    int location, core_yyscan_t yyscanner);
105 static Node *makeTypeCast(Node *arg, TypeName *typename, int location);
106 static Node *makeStringConst(char *str, int location);
107 static Node *makeStringConstCast(char *str, int location, TypeName *typename);
108 static Node *makeIntConst(int val, int location);
109 static Node *makeFloatConst(char *str, int location);
110 static Node *makeBitStringConst(char *str, int location);
111 static Node *makeNullAConst(int location);
112 static Node *makeAConst(Value *v, int location);
113 static Node *makeBoolAConst(bool state, int location);
114 static FuncCall *makeOverlaps(List *largs, List *rargs,
115                                                           int location, core_yyscan_t yyscanner);
116 static void check_qualified_name(List *names, core_yyscan_t yyscanner);
117 static List *check_func_name(List *names, core_yyscan_t yyscanner);
118 static List *check_indirection(List *indirection, core_yyscan_t yyscanner);
119 static List *extractArgTypes(List *parameters);
120 static SelectStmt *findLeftmostSelect(SelectStmt *node);
121 static void insertSelectOptions(SelectStmt *stmt,
122                                                                 List *sortClause, List *lockingClause,
123                                                                 Node *limitOffset, Node *limitCount,
124                                                                 WithClause *withClause,
125                                                                 core_yyscan_t yyscanner);
126 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
127 static Node *doNegate(Node *n, int location);
128 static void doNegateFloat(Value *v);
129 static Node *makeAArrayExpr(List *elements, int location);
130 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
131                                                  List *args, int location);
132 static List *mergeTableFuncParameters(List *func_args, List *columns);
133 static TypeName *TableFuncTypeName(List *columns);
134 static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner);
135 static void SplitColQualList(List *qualList,
136                                                          List **constraintList, CollateClause **collClause,
137                                                          core_yyscan_t yyscanner);
138
139 %}
140
141 %pure-parser
142 %expect 0
143 %name-prefix="base_yy"
144 %locations
145
146 %parse-param {core_yyscan_t yyscanner}
147 %lex-param   {core_yyscan_t yyscanner}
148
149 %union
150 {
151         core_YYSTYPE            core_yystype;
152         /* these fields must match core_YYSTYPE: */
153         int                                     ival;
154         char                            *str;
155         const char                      *keyword;
156
157         char                            chr;
158         bool                            boolean;
159         JoinType                        jtype;
160         DropBehavior            dbehavior;
161         OnCommitAction          oncommit;
162         List                            *list;
163         Node                            *node;
164         Value                           *value;
165         ObjectType                      objtype;
166         TypeName                        *typnam;
167         FunctionParameter   *fun_param;
168         FunctionParameterMode fun_param_mode;
169         FuncWithArgs            *funwithargs;
170         DefElem                         *defelt;
171         SortBy                          *sortby;
172         WindowDef                       *windef;
173         JoinExpr                        *jexpr;
174         IndexElem                       *ielem;
175         Alias                           *alias;
176         RangeVar                        *range;
177         IntoClause                      *into;
178         WithClause                      *with;
179         A_Indices                       *aind;
180         ResTarget                       *target;
181         struct PrivTarget       *privtarget;
182         AccessPriv                      *accesspriv;
183         InsertStmt                      *istmt;
184         VariableSetStmt         *vsetstmt;
185 }
186
187 %type <node>    stmt schema_stmt
188                 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterEnumStmt
189                 AlterFdwStmt AlterForeignServerStmt AlterGroupStmt
190                 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
191                 AlterExtensionStmt AlterExtensionContentsStmt AlterForeignTableStmt
192                 AlterCompositeTypeStmt AlterUserStmt AlterUserMappingStmt AlterUserSetStmt
193                 AlterRoleStmt AlterRoleSetStmt
194                 AlterDefaultPrivilegesStmt DefACLAction
195                 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
196                 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
197                 CreateDomainStmt CreateExtensionStmt CreateGroupStmt CreateOpClassStmt
198                 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
199                 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
200                 CreateFdwStmt CreateForeignServerStmt CreateForeignTableStmt
201                 CreateAssertStmt CreateTrigStmt
202                 CreateUserStmt CreateUserMappingStmt CreateRoleStmt
203                 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
204                 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
205                 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
206                 DropUserStmt DropdbStmt DropTableSpaceStmt DropFdwStmt
207                 DropForeignServerStmt DropUserMappingStmt ExplainStmt FetchStmt
208                 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
209                 LockStmt NotifyStmt ExplainableStmt PreparableStmt
210                 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
211                 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
212                 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
213                 SecLabelStmt SelectStmt TransactionStmt TruncateStmt
214                 UnlistenStmt UpdateStmt VacuumStmt
215                 VariableResetStmt VariableSetStmt VariableShowStmt
216                 ViewStmt CheckPointStmt CreateConversionStmt
217                 DeallocateStmt PrepareStmt ExecuteStmt
218                 DropOwnedStmt ReassignOwnedStmt
219                 AlterTSConfigurationStmt AlterTSDictionaryStmt
220
221 %type <node>    select_no_parens select_with_parens select_clause
222                                 simple_select values_clause
223
224 %type <node>    alter_column_default opclass_item opclass_drop alter_using
225 %type <ival>    add_drop opt_asc_desc opt_nulls_order
226
227 %type <node>    alter_table_cmd alter_type_cmd opt_collate_clause
228 %type <list>    alter_table_cmds alter_type_cmds
229
230 %type <dbehavior>       opt_drop_behavior
231
232 %type <list>    createdb_opt_list alterdb_opt_list copy_opt_list
233                                 transaction_mode_list
234                                 create_extension_opt_list alter_extension_opt_list
235 %type <defelt>  createdb_opt_item alterdb_opt_item copy_opt_item
236                                 transaction_mode_item
237                                 create_extension_opt_item alter_extension_opt_item
238
239 %type <ival>    opt_lock lock_type cast_context
240 %type <ival>    vacuum_option_list vacuum_option_elem
241 %type <boolean> opt_force opt_or_replace
242                                 opt_grant_grant_option opt_grant_admin_option
243                                 opt_nowait opt_if_exists opt_with_data
244
245 %type <list>    OptRoleList AlterOptRoleList
246 %type <defelt>  CreateOptRoleElem AlterOptRoleElem
247
248 %type <str>             opt_type
249 %type <str>             foreign_server_version opt_foreign_server_version
250 %type <str>             auth_ident
251 %type <str>             opt_in_database
252
253 %type <str>             OptSchemaName
254 %type <list>    OptSchemaEltList
255
256 %type <boolean> TriggerForSpec TriggerForType
257 %type <ival>    TriggerActionTime
258 %type <list>    TriggerEvents TriggerOneEvent
259 %type <value>   TriggerFuncArg
260 %type <node>    TriggerWhen
261
262 %type <str>             copy_file_name
263                                 database_name access_method_clause access_method attr_name
264                                 name cursor_name file_name
265                                 index_name opt_index_name cluster_index_specification
266
267 %type <list>    func_name handler_name qual_Op qual_all_Op subquery_Op
268                                 opt_class opt_inline_handler opt_validator validator_clause
269                                 opt_collate
270
271 %type <range>   qualified_name OptConstrFromTable
272
273 %type <str>             all_Op MathOp
274
275 %type <str>             iso_level opt_encoding
276 %type <node>    grantee
277 %type <list>    grantee_list
278 %type <accesspriv> privilege
279 %type <list>    privileges privilege_list
280 %type <privtarget> privilege_target
281 %type <funwithargs> function_with_argtypes
282 %type <list>    function_with_argtypes_list
283 %type <ival>    defacl_privilege_target
284 %type <defelt>  DefACLOption
285 %type <list>    DefACLOptionList
286
287 %type <list>    stmtblock stmtmulti
288                                 OptTableElementList TableElementList OptInherit definition
289                                 OptTypedTableElementList TypedTableElementList
290                                 OptForeignTableElementList ForeignTableElementList
291                                 reloptions opt_reloptions
292                                 OptWith opt_distinct opt_definition func_args func_args_list
293                                 func_args_with_defaults func_args_with_defaults_list
294                                 func_as createfunc_opt_list alterfunc_opt_list
295                                 aggr_args old_aggr_definition old_aggr_list
296                                 oper_argtypes RuleActionList RuleActionMulti
297                                 opt_column_list columnList opt_name_list
298                                 sort_clause opt_sort_clause sortby_list index_params
299                                 name_list from_clause from_list opt_array_bounds
300                                 qualified_name_list any_name any_name_list
301                                 any_operator expr_list attrs
302                                 target_list insert_column_list set_target_list
303                                 set_clause_list set_clause multiple_set_clause
304                                 ctext_expr_list ctext_row def_list indirection opt_indirection
305                                 reloption_list group_clause TriggerFuncArgs select_limit
306                                 opt_select_limit opclass_item_list opclass_drop_list
307                                 opclass_purpose opt_opfamily transaction_mode_list_or_empty
308                                 OptTableFuncElementList TableFuncElementList opt_type_modifiers
309                                 prep_type_clause
310                                 execute_param_clause using_clause returning_clause
311                                 opt_enum_val_list enum_val_list table_func_column_list
312                                 create_generic_options alter_generic_options
313                                 relation_expr_list dostmt_opt_list
314
315 %type <list>    opt_fdw_options fdw_options
316 %type <defelt>  fdw_option
317
318 %type <range>   OptTempTableName
319 %type <into>    into_clause create_as_target
320
321 %type <defelt>  createfunc_opt_item common_func_opt_item dostmt_opt_item
322 %type <fun_param> func_arg func_arg_with_default table_func_column
323 %type <fun_param_mode> arg_class
324 %type <typnam>  func_return func_type
325
326 %type <boolean>  opt_trusted opt_restart_seqs
327 %type <ival>     OptTemp
328 %type <oncommit> OnCommitOption
329
330 %type <node>    for_locking_item
331 %type <list>    for_locking_clause opt_for_locking_clause for_locking_items
332 %type <list>    locked_rels_list
333 %type <boolean> opt_all
334
335 %type <node>    join_outer join_qual
336 %type <jtype>   join_type
337
338 %type <list>    extract_list overlay_list position_list
339 %type <list>    substr_list trim_list
340 %type <list>    opt_interval interval_second
341 %type <node>    overlay_placing substr_from substr_for
342
343 %type <boolean> opt_instead
344 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
345 %type <boolean> opt_freeze opt_default opt_recheck
346 %type <defelt>  opt_binary opt_oids copy_delimiter
347
348 %type <boolean> copy_from
349
350 %type <ival>    opt_column event cursor_options opt_hold opt_set_data
351 %type <objtype> reindex_type drop_type comment_type security_label_type
352
353 %type <node>    fetch_args limit_clause select_limit_value
354                                 offset_clause select_offset_value
355                                 select_offset_value2 opt_select_fetch_first_value
356 %type <ival>    row_or_rows first_or_next
357
358 %type <list>    OptSeqOptList SeqOptList
359 %type <defelt>  SeqOptElem
360
361 %type <istmt>   insert_rest
362
363 %type <vsetstmt> set_rest SetResetClause
364
365 %type <node>    TableElement TypedTableElement ConstraintElem TableFuncElement
366                                 ForeignTableElement
367 %type <node>    columnDef columnOptions
368 %type <defelt>  def_elem reloption_elem old_aggr_elem
369 %type <node>    def_arg columnElem where_clause where_or_current_clause
370                                 a_expr b_expr c_expr func_expr AexprConst indirection_el
371                                 columnref in_expr having_clause func_table array_expr
372                                 ExclusionWhereClause
373 %type <list>    ExclusionConstraintList ExclusionConstraintElem
374 %type <list>    func_arg_list
375 %type <node>    func_arg_expr
376 %type <list>    row type_list array_expr_list
377 %type <node>    case_expr case_arg when_clause case_default
378 %type <list>    when_clause_list
379 %type <ival>    sub_type
380 %type <list>    OptCreateAs CreateAsList
381 %type <node>    CreateAsElement ctext_expr
382 %type <value>   NumericOnly
383 %type <list>    NumericOnly_list
384 %type <alias>   alias_clause
385 %type <sortby>  sortby
386 %type <ielem>   index_elem
387 %type <node>    table_ref
388 %type <jexpr>   joined_table
389 %type <range>   relation_expr
390 %type <range>   relation_expr_opt_alias
391 %type <target>  target_el single_set_clause set_target insert_column_item
392
393 %type <str>             generic_option_name
394 %type <node>    generic_option_arg
395 %type <defelt>  generic_option_elem alter_generic_option_elem
396 %type <list>    generic_option_list alter_generic_option_list
397 %type <str>             explain_option_name
398 %type <node>    explain_option_arg
399 %type <defelt>  explain_option_elem
400 %type <list>    explain_option_list
401 %type <node>    copy_generic_opt_arg copy_generic_opt_arg_list_item
402 %type <defelt>  copy_generic_opt_elem
403 %type <list>    copy_generic_opt_list copy_generic_opt_arg_list
404 %type <list>    copy_options
405
406 %type <typnam>  Typename SimpleTypename ConstTypename
407                                 GenericType Numeric opt_float
408                                 Character ConstCharacter
409                                 CharacterWithLength CharacterWithoutLength
410                                 ConstDatetime ConstInterval
411                                 Bit ConstBit BitWithLength BitWithoutLength
412 %type <str>             character
413 %type <str>             extract_arg
414 %type <str>             opt_charset
415 %type <boolean> opt_varying opt_timezone
416
417 %type <ival>    Iconst SignedIconst
418 %type <str>             Sconst comment_text notify_payload
419 %type <str>             RoleId opt_granted_by opt_boolean_or_string ColId_or_Sconst
420 %type <list>    var_list
421 %type <str>             ColId ColLabel var_name type_function_name param_name
422 %type <node>    var_value zone_value
423
424 %type <keyword> unreserved_keyword type_func_name_keyword
425 %type <keyword> col_name_keyword reserved_keyword
426
427 %type <node>    TableConstraint TableLikeClause
428 %type <ival>    TableLikeOptionList TableLikeOption
429 %type <list>    ColQualList
430 %type <node>    ColConstraint ColConstraintElem ConstraintAttr
431 %type <ival>    key_actions key_delete key_match key_update key_action
432 %type <ival>    ConstraintAttributeSpec ConstraintDeferrabilitySpec
433                                 ConstraintTimeSpec
434 %type <str>             ExistingIndex
435
436 %type <list>    constraints_set_list
437 %type <boolean> constraints_set_mode
438 %type <str>             OptTableSpace OptConsTableSpace OptTableSpaceOwner
439 %type <list>    opt_check_option
440
441 %type <str>             opt_provider security_label
442
443 %type <target>  xml_attribute_el
444 %type <list>    xml_attribute_list xml_attributes
445 %type <node>    xml_root_version opt_xml_root_standalone
446 %type <node>    xmlexists_argument
447 %type <ival>    document_or_content
448 %type <boolean> xml_whitespace_option
449
450 %type <node>    common_table_expr
451 %type <with>    with_clause opt_with_clause
452 %type <list>    cte_list
453
454 %type <list>    window_clause window_definition_list opt_partition_clause
455 %type <windef>  window_definition over_clause window_specification
456                                 opt_frame_clause frame_extent frame_bound
457 %type <str>             opt_existing_window_name
458
459
460 /*
461  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
462  * They must be listed first so that their numeric codes do not depend on
463  * the set of keywords.  PL/pgsql depends on this so that it can share the
464  * same lexer.  If you add/change tokens here, fix PL/pgsql to match!
465  *
466  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
467  * parse errors.  It is needed by PL/pgsql.
468  */
469 %token <str>    IDENT FCONST SCONST BCONST XCONST Op
470 %token <ival>   ICONST PARAM
471 %token                  TYPECAST DOT_DOT COLON_EQUALS
472
473 /*
474  * If you want to make any keyword changes, update the keyword table in
475  * src/include/parser/kwlist.h and add new keywords to the appropriate one
476  * of the reserved-or-not-so-reserved keyword lists, below; search
477  * this file for "Keyword category lists".
478  */
479
480 /* ordinary key words in alphabetical order */
481 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
482         AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
483         ASSERTION ASSIGNMENT ASYMMETRIC AT ATTRIBUTE AUTHORIZATION
484
485         BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
486         BOOLEAN_P BOTH BY
487
488         CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
489         CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
490         CLUSTER COALESCE COLLATE COLLATION COLUMN COMMENT COMMENTS COMMIT
491         COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
492         CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE
493         CROSS CSV CURRENT_P
494         CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
495         CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
496
497         DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
498         DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
499         DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
500
501         EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT
502         EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
503         EXTENSION EXTERNAL EXTRACT
504
505         FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOLLOWING FOR FORCE FOREIGN FORWARD
506         FREEZE FROM FULL FUNCTION FUNCTIONS
507
508         GLOBAL GRANT GRANTED GREATEST GROUP_P
509
510         HANDLER HAVING HEADER_P HOLD HOUR_P
511
512         IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P
513         INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
514         INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
515         INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
516
517         JOIN
518
519         KEY
520
521         LABEL LANGUAGE LARGE_P LAST_P LC_COLLATE_P LC_CTYPE_P LEADING
522         LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP
523         LOCATION LOCK_P
524
525         MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
526
527         NAME_P NAMES NATIONAL NATURAL NCHAR NEXT NO NONE
528         NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
529         NULLS_P NUMERIC
530
531         OBJECT_P OF OFF OFFSET OIDS ON ONLY OPERATOR OPTION OPTIONS OR
532         ORDER OUT_P OUTER_P OVER OVERLAPS OVERLAY OWNED OWNER
533
534         PARSER PARTIAL PARTITION PASSING PASSWORD PLACING PLANS POSITION
535         PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
536         PRIOR PRIVILEGES PROCEDURAL PROCEDURE
537
538         QUOTE
539
540         RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REINDEX
541         RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA
542         RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK
543         ROW ROWS RULE
544
545         SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
546         SERIALIZABLE SERVER SESSION SESSION_USER SET SETOF SHARE
547         SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
548         STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING
549         SYMMETRIC SYSID SYSTEM_P
550
551         TABLE TABLES TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
552         TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
553         TRUNCATE TRUSTED TYPE_P
554
555         UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNLOGGED
556         UNTIL UPDATE USER USING
557
558         VACUUM VALID VALIDATE VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
559         VERBOSE VERSION_P VIEW VOLATILE
560
561         WHEN WHERE WHITESPACE_P WINDOW WITH WITHOUT WORK WRAPPER WRITE
562
563         XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLPARSE
564         XMLPI XMLROOT XMLSERIALIZE
565
566         YEAR_P YES_P
567
568         ZONE
569
570 /*
571  * The grammar thinks these are keywords, but they are not in the kwlist.h
572  * list and so can never be entered directly.  The filter in parser.c
573  * creates these tokens when required.
574  */
575 %token                  NULLS_FIRST NULLS_LAST WITH_TIME
576
577
578 /* Precedence: lowest to highest */
579 %nonassoc       SET                             /* see relation_expr_opt_alias */
580 %left           UNION EXCEPT
581 %left           INTERSECT
582 %left           OR
583 %left           AND
584 %right          NOT
585 %right          '='
586 %nonassoc       '<' '>'
587 %nonassoc       LIKE ILIKE SIMILAR
588 %nonassoc       ESCAPE
589 %nonassoc       OVERLAPS
590 %nonassoc       BETWEEN
591 %nonassoc       IN_P
592 %left           POSTFIXOP               /* dummy for postfix Op rules */
593 /*
594  * To support target_el without AS, we must give IDENT an explicit priority
595  * between POSTFIXOP and Op.  We can safely assign the same priority to
596  * various unreserved keywords as needed to resolve ambiguities (this can't
597  * have any bad effects since obviously the keywords will still behave the
598  * same as if they weren't keywords).  We need to do this for PARTITION,
599  * RANGE, ROWS to support opt_existing_window_name; and for RANGE, ROWS
600  * so that they can follow a_expr without creating postfix-operator problems;
601  * and for NULL so that it can follow b_expr in ColQualList without creating
602  * postfix-operator problems.
603  *
604  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
605  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
606  * there is no principled way to distinguish these from the productions
607  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
608  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
609  * appear to cause UNBOUNDED to be treated differently from other unreserved
610  * keywords anywhere else in the grammar, but it's definitely risky.  We can
611  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
612  */
613 %nonassoc       UNBOUNDED               /* ideally should have same precedence as IDENT */
614 %nonassoc       IDENT NULL_P PARTITION RANGE ROWS PRECEDING FOLLOWING
615 %left           Op OPERATOR             /* multi-character ops and user-defined operators */
616 %nonassoc       NOTNULL
617 %nonassoc       ISNULL
618 %nonassoc       IS                              /* sets precedence for IS NULL, etc */
619 %left           '+' '-'
620 %left           '*' '/' '%'
621 %left           '^'
622 /* Unary Operators */
623 %left           AT                              /* sets precedence for AT TIME ZONE */
624 %left           COLLATE
625 %right          UMINUS
626 %left           '[' ']'
627 %left           '(' ')'
628 %left           TYPECAST
629 %left           '.'
630 /*
631  * These might seem to be low-precedence, but actually they are not part
632  * of the arithmetic hierarchy at all in their use as JOIN operators.
633  * We make them high-precedence to support their use as function names.
634  * They wouldn't be given a precedence at all, were it not that we need
635  * left-associativity among the JOIN rules themselves.
636  */
637 %left           JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
638 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
639 %right          PRESERVE STRIP_P
640
641 %%
642
643 /*
644  *      The target production for the whole parse.
645  */
646 stmtblock:      stmtmulti
647                         {
648                                 pg_yyget_extra(yyscanner)->parsetree = $1;
649                         }
650                 ;
651
652 /* the thrashing around here is to discard "empty" statements... */
653 stmtmulti:      stmtmulti ';' stmt
654                                 {
655                                         if ($3 != NULL)
656                                                 $$ = lappend($1, $3);
657                                         else
658                                                 $$ = $1;
659                                 }
660                         | stmt
661                                 {
662                                         if ($1 != NULL)
663                                                 $$ = list_make1($1);
664                                         else
665                                                 $$ = NIL;
666                                 }
667                 ;
668
669 stmt :
670                         AlterDatabaseStmt
671                         | AlterDatabaseSetStmt
672                         | AlterDefaultPrivilegesStmt
673                         | AlterDomainStmt
674                         | AlterEnumStmt
675                         | AlterExtensionStmt
676                         | AlterExtensionContentsStmt
677                         | AlterFdwStmt
678                         | AlterForeignServerStmt
679                         | AlterForeignTableStmt
680                         | AlterFunctionStmt
681                         | AlterGroupStmt
682                         | AlterObjectSchemaStmt
683                         | AlterOwnerStmt
684                         | AlterSeqStmt
685                         | AlterTableStmt
686                         | AlterCompositeTypeStmt
687                         | AlterRoleSetStmt
688                         | AlterRoleStmt
689                         | AlterTSConfigurationStmt
690                         | AlterTSDictionaryStmt
691                         | AlterUserMappingStmt
692                         | AlterUserSetStmt
693                         | AlterUserStmt
694                         | AnalyzeStmt
695                         | CheckPointStmt
696                         | ClosePortalStmt
697                         | ClusterStmt
698                         | CommentStmt
699                         | ConstraintsSetStmt
700                         | CopyStmt
701                         | CreateAsStmt
702                         | CreateAssertStmt
703                         | CreateCastStmt
704                         | CreateConversionStmt
705                         | CreateDomainStmt
706                         | CreateExtensionStmt
707                         | CreateFdwStmt
708                         | CreateForeignServerStmt
709                         | CreateForeignTableStmt
710                         | CreateFunctionStmt
711                         | CreateGroupStmt
712                         | CreateOpClassStmt
713                         | CreateOpFamilyStmt
714                         | AlterOpFamilyStmt
715                         | CreatePLangStmt
716                         | CreateSchemaStmt
717                         | CreateSeqStmt
718                         | CreateStmt
719                         | CreateTableSpaceStmt
720                         | CreateTrigStmt
721                         | CreateRoleStmt
722                         | CreateUserStmt
723                         | CreateUserMappingStmt
724                         | CreatedbStmt
725                         | DeallocateStmt
726                         | DeclareCursorStmt
727                         | DefineStmt
728                         | DeleteStmt
729                         | DiscardStmt
730                         | DoStmt
731                         | DropAssertStmt
732                         | DropCastStmt
733                         | DropFdwStmt
734                         | DropForeignServerStmt
735                         | DropGroupStmt
736                         | DropOpClassStmt
737                         | DropOpFamilyStmt
738                         | DropOwnedStmt
739                         | DropPLangStmt
740                         | DropRuleStmt
741                         | DropStmt
742                         | DropTableSpaceStmt
743                         | DropTrigStmt
744                         | DropRoleStmt
745                         | DropUserStmt
746                         | DropUserMappingStmt
747                         | DropdbStmt
748                         | ExecuteStmt
749                         | ExplainStmt
750                         | FetchStmt
751                         | GrantStmt
752                         | GrantRoleStmt
753                         | IndexStmt
754                         | InsertStmt
755                         | ListenStmt
756                         | LoadStmt
757                         | LockStmt
758                         | NotifyStmt
759                         | PrepareStmt
760                         | ReassignOwnedStmt
761                         | ReindexStmt
762                         | RemoveAggrStmt
763                         | RemoveFuncStmt
764                         | RemoveOperStmt
765                         | RenameStmt
766                         | RevokeStmt
767                         | RevokeRoleStmt
768                         | RuleStmt
769                         | SecLabelStmt
770                         | SelectStmt
771                         | TransactionStmt
772                         | TruncateStmt
773                         | UnlistenStmt
774                         | UpdateStmt
775                         | VacuumStmt
776                         | VariableResetStmt
777                         | VariableSetStmt
778                         | VariableShowStmt
779                         | ViewStmt
780                         | /*EMPTY*/
781                                 { $$ = NULL; }
782                 ;
783
784 /*****************************************************************************
785  *
786  * Create a new Postgres DBMS role
787  *
788  *****************************************************************************/
789
790 CreateRoleStmt:
791                         CREATE ROLE RoleId opt_with OptRoleList
792                                 {
793                                         CreateRoleStmt *n = makeNode(CreateRoleStmt);
794                                         n->stmt_type = ROLESTMT_ROLE;
795                                         n->role = $3;
796                                         n->options = $5;
797                                         $$ = (Node *)n;
798                                 }
799                 ;
800
801
802 opt_with:       WITH                                                                    {}
803                         | /*EMPTY*/                                                             {}
804                 ;
805
806 /*
807  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
808  * for backwards compatibility).  Note: the only option required by SQL99
809  * is "WITH ADMIN name".
810  */
811 OptRoleList:
812                         OptRoleList CreateOptRoleElem                   { $$ = lappend($1, $2); }
813                         | /* EMPTY */                                                   { $$ = NIL; }
814                 ;
815
816 AlterOptRoleList:
817                         AlterOptRoleList AlterOptRoleElem               { $$ = lappend($1, $2); }
818                         | /* EMPTY */                                                   { $$ = NIL; }
819                 ;
820
821 AlterOptRoleElem:
822                         PASSWORD Sconst
823                                 {
824                                         $$ = makeDefElem("password",
825                                                                          (Node *)makeString($2));
826                                 }
827                         | PASSWORD NULL_P
828                                 {
829                                         $$ = makeDefElem("password", NULL);
830                                 }
831                         | ENCRYPTED PASSWORD Sconst
832                                 {
833                                         $$ = makeDefElem("encryptedPassword",
834                                                                          (Node *)makeString($3));
835                                 }
836                         | UNENCRYPTED PASSWORD Sconst
837                                 {
838                                         $$ = makeDefElem("unencryptedPassword",
839                                                                          (Node *)makeString($3));
840                                 }
841                         | INHERIT
842                                 {
843                                         $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
844                                 }
845                         | CONNECTION LIMIT SignedIconst
846                                 {
847                                         $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
848                                 }
849                         | VALID UNTIL Sconst
850                                 {
851                                         $$ = makeDefElem("validUntil", (Node *)makeString($3));
852                                 }
853                 /*      Supported but not documented for roles, for use by ALTER GROUP. */
854                         | USER name_list
855                                 {
856                                         $$ = makeDefElem("rolemembers", (Node *)$2);
857                                 }
858                         | IDENT
859                                 {
860                                         /*
861                                          * We handle identifiers that aren't parser keywords with
862                                          * the following special-case codes, to avoid bloating the
863                                          * size of the main parser.
864                                          */
865                                         if (strcmp($1, "superuser") == 0)
866                                                 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
867                                         else if (strcmp($1, "nosuperuser") == 0)
868                                                 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
869                                         else if (strcmp($1, "createuser") == 0)
870                                         {
871                                                 /* For backwards compatibility, synonym for SUPERUSER */
872                                                 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
873                                         }
874                                         else if (strcmp($1, "nocreateuser") == 0)
875                                         {
876                                                 /* For backwards compatibility, synonym for SUPERUSER */
877                                                 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
878                                         }
879                                         else if (strcmp($1, "createrole") == 0)
880                                                 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
881                                         else if (strcmp($1, "nocreaterole") == 0)
882                                                 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
883                                         else if (strcmp($1, "replication") == 0)
884                                                 $$ = makeDefElem("isreplication", (Node *)makeInteger(TRUE));
885                                         else if (strcmp($1, "noreplication") == 0)
886                                                 $$ = makeDefElem("isreplication", (Node *)makeInteger(FALSE));
887                                         else if (strcmp($1, "createdb") == 0)
888                                                 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
889                                         else if (strcmp($1, "nocreatedb") == 0)
890                                                 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
891                                         else if (strcmp($1, "login") == 0)
892                                                 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
893                                         else if (strcmp($1, "nologin") == 0)
894                                                 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
895                                         else if (strcmp($1, "noinherit") == 0)
896                                         {
897                                                 /*
898                                                  * Note that INHERIT is a keyword, so it's handled by main parser, but
899                                                  * NOINHERIT is handled here.
900                                                  */
901                                                 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
902                                         }
903                                         else
904                                                 ereport(ERROR,
905                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
906                                                                  errmsg("unrecognized role option \"%s\"", $1),
907                                                                          parser_errposition(@1)));
908                                 }
909                 ;
910
911 CreateOptRoleElem:
912                         AlterOptRoleElem                        { $$ = $1; }
913                         /* The following are not supported by ALTER ROLE/USER/GROUP */
914                         | SYSID Iconst
915                                 {
916                                         $$ = makeDefElem("sysid", (Node *)makeInteger($2));
917                                 }
918                         | ADMIN name_list
919                                 {
920                                         $$ = makeDefElem("adminmembers", (Node *)$2);
921                                 }
922                         | ROLE name_list
923                                 {
924                                         $$ = makeDefElem("rolemembers", (Node *)$2);
925                                 }
926                         | IN_P ROLE name_list
927                                 {
928                                         $$ = makeDefElem("addroleto", (Node *)$3);
929                                 }
930                         | IN_P GROUP_P name_list
931                                 {
932                                         $$ = makeDefElem("addroleto", (Node *)$3);
933                                 }
934                 ;
935
936
937 /*****************************************************************************
938  *
939  * Create a new Postgres DBMS user (role with implied login ability)
940  *
941  *****************************************************************************/
942
943 CreateUserStmt:
944                         CREATE USER RoleId opt_with OptRoleList
945                                 {
946                                         CreateRoleStmt *n = makeNode(CreateRoleStmt);
947                                         n->stmt_type = ROLESTMT_USER;
948                                         n->role = $3;
949                                         n->options = $5;
950                                         $$ = (Node *)n;
951                                 }
952                 ;
953
954
955 /*****************************************************************************
956  *
957  * Alter a postgresql DBMS role
958  *
959  *****************************************************************************/
960
961 AlterRoleStmt:
962                         ALTER ROLE RoleId opt_with AlterOptRoleList
963                                  {
964                                         AlterRoleStmt *n = makeNode(AlterRoleStmt);
965                                         n->role = $3;
966                                         n->action = +1; /* add, if there are members */
967                                         n->options = $5;
968                                         $$ = (Node *)n;
969                                  }
970                 ;
971
972 opt_in_database:
973                            /* EMPTY */                                  { $$ = NULL; }
974                         | IN_P DATABASE database_name   { $$ = $3; }
975                 ;
976
977 AlterRoleSetStmt:
978                         ALTER ROLE RoleId opt_in_database SetResetClause
979                                 {
980                                         AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
981                                         n->role = $3;
982                                         n->database = $4;
983                                         n->setstmt = $5;
984                                         $$ = (Node *)n;
985                                 }
986                 ;
987
988
989 /*****************************************************************************
990  *
991  * Alter a postgresql DBMS user
992  *
993  *****************************************************************************/
994
995 AlterUserStmt:
996                         ALTER USER RoleId opt_with AlterOptRoleList
997                                  {
998                                         AlterRoleStmt *n = makeNode(AlterRoleStmt);
999                                         n->role = $3;
1000                                         n->action = +1; /* add, if there are members */
1001                                         n->options = $5;
1002                                         $$ = (Node *)n;
1003                                  }
1004                 ;
1005
1006
1007 AlterUserSetStmt:
1008                         ALTER USER RoleId SetResetClause
1009                                 {
1010                                         AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
1011                                         n->role = $3;
1012                                         n->database = NULL;
1013                                         n->setstmt = $4;
1014                                         $$ = (Node *)n;
1015                                 }
1016                         ;
1017
1018
1019 /*****************************************************************************
1020  *
1021  * Drop a postgresql DBMS role
1022  *
1023  * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
1024  * might own objects in multiple databases, there is presently no way to
1025  * implement either cascading or restricting.  Caveat DBA.
1026  *****************************************************************************/
1027
1028 DropRoleStmt:
1029                         DROP ROLE name_list
1030                                 {
1031                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1032                                         n->missing_ok = FALSE;
1033                                         n->roles = $3;
1034                                         $$ = (Node *)n;
1035                                 }
1036                         | DROP ROLE IF_P EXISTS name_list
1037                                 {
1038                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1039                                         n->missing_ok = TRUE;
1040                                         n->roles = $5;
1041                                         $$ = (Node *)n;
1042                                 }
1043                         ;
1044
1045 /*****************************************************************************
1046  *
1047  * Drop a postgresql DBMS user
1048  *
1049  * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
1050  * might own objects in multiple databases, there is presently no way to
1051  * implement either cascading or restricting.  Caveat DBA.
1052  *****************************************************************************/
1053
1054 DropUserStmt:
1055                         DROP USER name_list
1056                                 {
1057                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1058                                         n->missing_ok = FALSE;
1059                                         n->roles = $3;
1060                                         $$ = (Node *)n;
1061                                 }
1062                         | DROP USER IF_P EXISTS name_list
1063                                 {
1064                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1065                                         n->roles = $5;
1066                                         n->missing_ok = TRUE;
1067                                         $$ = (Node *)n;
1068                                 }
1069                         ;
1070
1071
1072 /*****************************************************************************
1073  *
1074  * Create a postgresql group (role without login ability)
1075  *
1076  *****************************************************************************/
1077
1078 CreateGroupStmt:
1079                         CREATE GROUP_P RoleId opt_with OptRoleList
1080                                 {
1081                                         CreateRoleStmt *n = makeNode(CreateRoleStmt);
1082                                         n->stmt_type = ROLESTMT_GROUP;
1083                                         n->role = $3;
1084                                         n->options = $5;
1085                                         $$ = (Node *)n;
1086                                 }
1087                 ;
1088
1089
1090 /*****************************************************************************
1091  *
1092  * Alter a postgresql group
1093  *
1094  *****************************************************************************/
1095
1096 AlterGroupStmt:
1097                         ALTER GROUP_P RoleId add_drop USER name_list
1098                                 {
1099                                         AlterRoleStmt *n = makeNode(AlterRoleStmt);
1100                                         n->role = $3;
1101                                         n->action = $4;
1102                                         n->options = list_make1(makeDefElem("rolemembers",
1103                                                                                                                 (Node *)$6));
1104                                         $$ = (Node *)n;
1105                                 }
1106                 ;
1107
1108 add_drop:       ADD_P                                                                   { $$ = +1; }
1109                         | DROP                                                                  { $$ = -1; }
1110                 ;
1111
1112
1113 /*****************************************************************************
1114  *
1115  * Drop a postgresql group
1116  *
1117  * XXX see above notes about cascading DROP USER; groups have same problem.
1118  *****************************************************************************/
1119
1120 DropGroupStmt:
1121                         DROP GROUP_P name_list
1122                                 {
1123                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1124                                         n->missing_ok = FALSE;
1125                                         n->roles = $3;
1126                                         $$ = (Node *)n;
1127                                 }
1128                         | DROP GROUP_P IF_P EXISTS name_list
1129                                 {
1130                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1131                                         n->missing_ok = TRUE;
1132                                         n->roles = $5;
1133                                         $$ = (Node *)n;
1134                                 }
1135                 ;
1136
1137
1138 /*****************************************************************************
1139  *
1140  * Manipulate a schema
1141  *
1142  *****************************************************************************/
1143
1144 CreateSchemaStmt:
1145                         CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
1146                                 {
1147                                         CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1148                                         /* One can omit the schema name or the authorization id. */
1149                                         if ($3 != NULL)
1150                                                 n->schemaname = $3;
1151                                         else
1152                                                 n->schemaname = $5;
1153                                         n->authid = $5;
1154                                         n->schemaElts = $6;
1155                                         $$ = (Node *)n;
1156                                 }
1157                         | CREATE SCHEMA ColId OptSchemaEltList
1158                                 {
1159                                         CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1160                                         /* ...but not both */
1161                                         n->schemaname = $3;
1162                                         n->authid = NULL;
1163                                         n->schemaElts = $4;
1164                                         $$ = (Node *)n;
1165                                 }
1166                 ;
1167
1168 OptSchemaName:
1169                         ColId                                                                   { $$ = $1; }
1170                         | /* EMPTY */                                                   { $$ = NULL; }
1171                 ;
1172
1173 OptSchemaEltList:
1174                         OptSchemaEltList schema_stmt                    { $$ = lappend($1, $2); }
1175                         | /* EMPTY */                                                   { $$ = NIL; }
1176                 ;
1177
1178 /*
1179  *      schema_stmt are the ones that can show up inside a CREATE SCHEMA
1180  *      statement (in addition to by themselves).
1181  */
1182 schema_stmt:
1183                         CreateStmt
1184                         | IndexStmt
1185                         | CreateSeqStmt
1186                         | CreateTrigStmt
1187                         | GrantStmt
1188                         | ViewStmt
1189                 ;
1190
1191
1192 /*****************************************************************************
1193  *
1194  * Set PG internal variable
1195  *        SET name TO 'var_value'
1196  * Include SQL92 syntax (thomas 1997-10-22):
1197  *        SET TIME ZONE 'var_value'
1198  *
1199  *****************************************************************************/
1200
1201 VariableSetStmt:
1202                         SET set_rest
1203                                 {
1204                                         VariableSetStmt *n = $2;
1205                                         n->is_local = false;
1206                                         $$ = (Node *) n;
1207                                 }
1208                         | SET LOCAL set_rest
1209                                 {
1210                                         VariableSetStmt *n = $3;
1211                                         n->is_local = true;
1212                                         $$ = (Node *) n;
1213                                 }
1214                         | SET SESSION set_rest
1215                                 {
1216                                         VariableSetStmt *n = $3;
1217                                         n->is_local = false;
1218                                         $$ = (Node *) n;
1219                                 }
1220                 ;
1221
1222 set_rest:       /* Generic SET syntaxes: */
1223                         var_name TO var_list
1224                                 {
1225                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1226                                         n->kind = VAR_SET_VALUE;
1227                                         n->name = $1;
1228                                         n->args = $3;
1229                                         $$ = n;
1230                                 }
1231                         | var_name '=' var_list
1232                                 {
1233                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1234                                         n->kind = VAR_SET_VALUE;
1235                                         n->name = $1;
1236                                         n->args = $3;
1237                                         $$ = n;
1238                                 }
1239                         | var_name TO DEFAULT
1240                                 {
1241                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1242                                         n->kind = VAR_SET_DEFAULT;
1243                                         n->name = $1;
1244                                         $$ = n;
1245                                 }
1246                         | var_name '=' DEFAULT
1247                                 {
1248                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1249                                         n->kind = VAR_SET_DEFAULT;
1250                                         n->name = $1;
1251                                         $$ = n;
1252                                 }
1253                         | var_name FROM CURRENT_P
1254                                 {
1255                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1256                                         n->kind = VAR_SET_CURRENT;
1257                                         n->name = $1;
1258                                         $$ = n;
1259                                 }
1260                         /* Special syntaxes mandated by SQL standard: */
1261                         | TIME ZONE zone_value
1262                                 {
1263                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1264                                         n->kind = VAR_SET_VALUE;
1265                                         n->name = "timezone";
1266                                         if ($3 != NULL)
1267                                                 n->args = list_make1($3);
1268                                         else
1269                                                 n->kind = VAR_SET_DEFAULT;
1270                                         $$ = n;
1271                                 }
1272                         | TRANSACTION transaction_mode_list
1273                                 {
1274                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1275                                         n->kind = VAR_SET_MULTI;
1276                                         n->name = "TRANSACTION";
1277                                         n->args = $2;
1278                                         $$ = n;
1279                                 }
1280                         | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1281                                 {
1282                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1283                                         n->kind = VAR_SET_MULTI;
1284                                         n->name = "SESSION CHARACTERISTICS";
1285                                         n->args = $5;
1286                                         $$ = n;
1287                                 }
1288                         | CATALOG_P Sconst
1289                                 {
1290                                         ereport(ERROR,
1291                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1292                                                          errmsg("current database cannot be changed"),
1293                                                          parser_errposition(@2)));
1294                                         $$ = NULL; /*not reached*/
1295                                 }
1296                         | SCHEMA Sconst
1297                                 {
1298                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1299                                         n->kind = VAR_SET_VALUE;
1300                                         n->name = "search_path";
1301                                         n->args = list_make1(makeStringConst($2, @2));
1302                                         $$ = n;
1303                                 }
1304                         | NAMES opt_encoding
1305                                 {
1306                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1307                                         n->kind = VAR_SET_VALUE;
1308                                         n->name = "client_encoding";
1309                                         if ($2 != NULL)
1310                                                 n->args = list_make1(makeStringConst($2, @2));
1311                                         else
1312                                                 n->kind = VAR_SET_DEFAULT;
1313                                         $$ = n;
1314                                 }
1315                         | ROLE ColId_or_Sconst
1316                                 {
1317                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1318                                         n->kind = VAR_SET_VALUE;
1319                                         n->name = "role";
1320                                         n->args = list_make1(makeStringConst($2, @2));
1321                                         $$ = n;
1322                                 }
1323                         | SESSION AUTHORIZATION ColId_or_Sconst
1324                                 {
1325                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1326                                         n->kind = VAR_SET_VALUE;
1327                                         n->name = "session_authorization";
1328                                         n->args = list_make1(makeStringConst($3, @3));
1329                                         $$ = n;
1330                                 }
1331                         | SESSION AUTHORIZATION DEFAULT
1332                                 {
1333                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1334                                         n->kind = VAR_SET_DEFAULT;
1335                                         n->name = "session_authorization";
1336                                         $$ = n;
1337                                 }
1338                         | XML_P OPTION document_or_content
1339                                 {
1340                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1341                                         n->kind = VAR_SET_VALUE;
1342                                         n->name = "xmloption";
1343                                         n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1344                                         $$ = n;
1345                                 }
1346                 ;
1347
1348 var_name:       ColId                                                           { $$ = $1; }
1349                         | var_name '.' ColId
1350                                 {
1351                                         $$ = palloc(strlen($1) + strlen($3) + 2);
1352                                         sprintf($$, "%s.%s", $1, $3);
1353                                 }
1354                 ;
1355
1356 var_list:       var_value                                                               { $$ = list_make1($1); }
1357                         | var_list ',' var_value                                { $$ = lappend($1, $3); }
1358                 ;
1359
1360 var_value:      opt_boolean_or_string
1361                                 { $$ = makeStringConst($1, @1); }
1362                         | NumericOnly
1363                                 { $$ = makeAConst($1, @1); }
1364                 ;
1365
1366 iso_level:      READ UNCOMMITTED                                                { $$ = "read uncommitted"; }
1367                         | READ COMMITTED                                                { $$ = "read committed"; }
1368                         | REPEATABLE READ                                               { $$ = "repeatable read"; }
1369                         | SERIALIZABLE                                                  { $$ = "serializable"; }
1370                 ;
1371
1372 opt_boolean_or_string:
1373                         TRUE_P                                                                  { $$ = "true"; }
1374                         | FALSE_P                                                               { $$ = "false"; }
1375                         | ON                                                                    { $$ = "on"; }
1376                         /*
1377                          * OFF is also accepted as a boolean value, but is handled
1378                          * by the ColId rule below. The action for booleans and strings
1379                          * is the same, so we don't need to distinguish them here.
1380                          */
1381                         | ColId_or_Sconst                                               { $$ = $1; }
1382                 ;
1383
1384 /* Timezone values can be:
1385  * - a string such as 'pst8pdt'
1386  * - an identifier such as "pst8pdt"
1387  * - an integer or floating point number
1388  * - a time interval per SQL99
1389  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1390  * so use IDENT (meaning we reject anything that is a key word).
1391  */
1392 zone_value:
1393                         Sconst
1394                                 {
1395                                         $$ = makeStringConst($1, @1);
1396                                 }
1397                         | IDENT
1398                                 {
1399                                         $$ = makeStringConst($1, @1);
1400                                 }
1401                         | ConstInterval Sconst opt_interval
1402                                 {
1403                                         TypeName *t = $1;
1404                                         if ($3 != NIL)
1405                                         {
1406                                                 A_Const *n = (A_Const *) linitial($3);
1407                                                 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1408                                                         ereport(ERROR,
1409                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
1410                                                                          errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1411                                                                          parser_errposition(@3)));
1412                                         }
1413                                         t->typmods = $3;
1414                                         $$ = makeStringConstCast($2, @2, t);
1415                                 }
1416                         | ConstInterval '(' Iconst ')' Sconst opt_interval
1417                                 {
1418                                         TypeName *t = $1;
1419                                         if ($6 != NIL)
1420                                         {
1421                                                 A_Const *n = (A_Const *) linitial($6);
1422                                                 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1423                                                         ereport(ERROR,
1424                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
1425                                                                          errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1426                                                                          parser_errposition(@6)));
1427                                                 if (list_length($6) != 1)
1428                                                         ereport(ERROR,
1429                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
1430                                                                          errmsg("interval precision specified twice"),
1431                                                                          parser_errposition(@1)));
1432                                                 t->typmods = lappend($6, makeIntConst($3, @3));
1433                                         }
1434                                         else
1435                                                 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1436                                                                                                 makeIntConst($3, @3));
1437                                         $$ = makeStringConstCast($5, @5, t);
1438                                 }
1439                         | NumericOnly                                                   { $$ = makeAConst($1, @1); }
1440                         | DEFAULT                                                               { $$ = NULL; }
1441                         | LOCAL                                                                 { $$ = NULL; }
1442                 ;
1443
1444 opt_encoding:
1445                         Sconst                                                                  { $$ = $1; }
1446                         | DEFAULT                                                               { $$ = NULL; }
1447                         | /*EMPTY*/                                                             { $$ = NULL; }
1448                 ;
1449
1450 ColId_or_Sconst:
1451                         ColId                                                                   { $$ = $1; }
1452                         | Sconst                                                                { $$ = $1; }
1453                 ;
1454
1455 VariableResetStmt:
1456                         RESET var_name
1457                                 {
1458                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1459                                         n->kind = VAR_RESET;
1460                                         n->name = $2;
1461                                         $$ = (Node *) n;
1462                                 }
1463                         | RESET TIME ZONE
1464                                 {
1465                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1466                                         n->kind = VAR_RESET;
1467                                         n->name = "timezone";
1468                                         $$ = (Node *) n;
1469                                 }
1470                         | RESET TRANSACTION ISOLATION LEVEL
1471                                 {
1472                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1473                                         n->kind = VAR_RESET;
1474                                         n->name = "transaction_isolation";
1475                                         $$ = (Node *) n;
1476                                 }
1477                         | RESET SESSION AUTHORIZATION
1478                                 {
1479                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1480                                         n->kind = VAR_RESET;
1481                                         n->name = "session_authorization";
1482                                         $$ = (Node *) n;
1483                                 }
1484                         | RESET ALL
1485                                 {
1486                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1487                                         n->kind = VAR_RESET_ALL;
1488                                         $$ = (Node *) n;
1489                                 }
1490                 ;
1491
1492 /* SetResetClause allows SET or RESET without LOCAL */
1493 SetResetClause:
1494                         SET set_rest                                    { $$ = $2; }
1495                         | VariableResetStmt                             { $$ = (VariableSetStmt *) $1; }
1496                 ;
1497
1498
1499 VariableShowStmt:
1500                         SHOW var_name
1501                                 {
1502                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1503                                         n->name = $2;
1504                                         $$ = (Node *) n;
1505                                 }
1506                         | SHOW TIME ZONE
1507                                 {
1508                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1509                                         n->name = "timezone";
1510                                         $$ = (Node *) n;
1511                                 }
1512                         | SHOW TRANSACTION ISOLATION LEVEL
1513                                 {
1514                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1515                                         n->name = "transaction_isolation";
1516                                         $$ = (Node *) n;
1517                                 }
1518                         | SHOW SESSION AUTHORIZATION
1519                                 {
1520                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1521                                         n->name = "session_authorization";
1522                                         $$ = (Node *) n;
1523                                 }
1524                         | SHOW ALL
1525                                 {
1526                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1527                                         n->name = "all";
1528                                         $$ = (Node *) n;
1529                                 }
1530                 ;
1531
1532
1533 ConstraintsSetStmt:
1534                         SET CONSTRAINTS constraints_set_list constraints_set_mode
1535                                 {
1536                                         ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1537                                         n->constraints = $3;
1538                                         n->deferred    = $4;
1539                                         $$ = (Node *) n;
1540                                 }
1541                 ;
1542
1543 constraints_set_list:
1544                         ALL                                                                             { $$ = NIL; }
1545                         | qualified_name_list                                   { $$ = $1; }
1546                 ;
1547
1548 constraints_set_mode:
1549                         DEFERRED                                                                { $$ = TRUE; }
1550                         | IMMEDIATE                                                             { $$ = FALSE; }
1551                 ;
1552
1553
1554 /*
1555  * Checkpoint statement
1556  */
1557 CheckPointStmt:
1558                         CHECKPOINT
1559                                 {
1560                                         CheckPointStmt *n = makeNode(CheckPointStmt);
1561                                         $$ = (Node *)n;
1562                                 }
1563                 ;
1564
1565
1566 /*****************************************************************************
1567  *
1568  * DISCARD { ALL | TEMP | PLANS }
1569  *
1570  *****************************************************************************/
1571
1572 DiscardStmt:
1573                         DISCARD ALL
1574                                 {
1575                                         DiscardStmt *n = makeNode(DiscardStmt);
1576                                         n->target = DISCARD_ALL;
1577                                         $$ = (Node *) n;
1578                                 }
1579                         | DISCARD TEMP
1580                                 {
1581                                         DiscardStmt *n = makeNode(DiscardStmt);
1582                                         n->target = DISCARD_TEMP;
1583                                         $$ = (Node *) n;
1584                                 }
1585                         | DISCARD TEMPORARY
1586                                 {
1587                                         DiscardStmt *n = makeNode(DiscardStmt);
1588                                         n->target = DISCARD_TEMP;
1589                                         $$ = (Node *) n;
1590                                 }
1591                         | DISCARD PLANS
1592                                 {
1593                                         DiscardStmt *n = makeNode(DiscardStmt);
1594                                         n->target = DISCARD_PLANS;
1595                                         $$ = (Node *) n;
1596                                 }
1597                 ;
1598
1599
1600 /*****************************************************************************
1601  *
1602  *      ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
1603  *
1604  * Note: we accept all subcommands for each of the four variants, and sort
1605  * out what's really legal at execution time.
1606  *****************************************************************************/
1607
1608 AlterTableStmt:
1609                         ALTER TABLE relation_expr alter_table_cmds
1610                                 {
1611                                         AlterTableStmt *n = makeNode(AlterTableStmt);
1612                                         n->relation = $3;
1613                                         n->cmds = $4;
1614                                         n->relkind = OBJECT_TABLE;
1615                                         $$ = (Node *)n;
1616                                 }
1617                 |       ALTER INDEX qualified_name alter_table_cmds
1618                                 {
1619                                         AlterTableStmt *n = makeNode(AlterTableStmt);
1620                                         n->relation = $3;
1621                                         n->cmds = $4;
1622                                         n->relkind = OBJECT_INDEX;
1623                                         $$ = (Node *)n;
1624                                 }
1625                 |       ALTER SEQUENCE qualified_name alter_table_cmds
1626                                 {
1627                                         AlterTableStmt *n = makeNode(AlterTableStmt);
1628                                         n->relation = $3;
1629                                         n->cmds = $4;
1630                                         n->relkind = OBJECT_SEQUENCE;
1631                                         $$ = (Node *)n;
1632                                 }
1633                 |       ALTER VIEW qualified_name alter_table_cmds
1634                                 {
1635                                         AlterTableStmt *n = makeNode(AlterTableStmt);
1636                                         n->relation = $3;
1637                                         n->cmds = $4;
1638                                         n->relkind = OBJECT_VIEW;
1639                                         $$ = (Node *)n;
1640                                 }
1641                 ;
1642
1643 alter_table_cmds:
1644                         alter_table_cmd                                                 { $$ = list_make1($1); }
1645                         | alter_table_cmds ',' alter_table_cmd  { $$ = lappend($1, $3); }
1646                 ;
1647
1648 alter_table_cmd:
1649                         /* ALTER TABLE <name> ADD <coldef> */
1650                         ADD_P columnDef
1651                                 {
1652                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1653                                         n->subtype = AT_AddColumn;
1654                                         n->def = $2;
1655                                         $$ = (Node *)n;
1656                                 }
1657                         /* ALTER TABLE <name> ADD COLUMN <coldef> */
1658                         | ADD_P COLUMN columnDef
1659                                 {
1660                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1661                                         n->subtype = AT_AddColumn;
1662                                         n->def = $3;
1663                                         $$ = (Node *)n;
1664                                 }
1665                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1666                         | ALTER opt_column ColId alter_column_default
1667                                 {
1668                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1669                                         n->subtype = AT_ColumnDefault;
1670                                         n->name = $3;
1671                                         n->def = $4;
1672                                         $$ = (Node *)n;
1673                                 }
1674                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1675                         | ALTER opt_column ColId DROP NOT NULL_P
1676                                 {
1677                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1678                                         n->subtype = AT_DropNotNull;
1679                                         n->name = $3;
1680                                         $$ = (Node *)n;
1681                                 }
1682                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1683                         | ALTER opt_column ColId SET NOT NULL_P
1684                                 {
1685                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1686                                         n->subtype = AT_SetNotNull;
1687                                         n->name = $3;
1688                                         $$ = (Node *)n;
1689                                 }
1690                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
1691                         | ALTER opt_column ColId SET STATISTICS SignedIconst
1692                                 {
1693                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1694                                         n->subtype = AT_SetStatistics;
1695                                         n->name = $3;
1696                                         n->def = (Node *) makeInteger($6);
1697                                         $$ = (Node *)n;
1698                                 }
1699                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
1700                         | ALTER opt_column ColId SET reloptions
1701                                 {
1702                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1703                                         n->subtype = AT_SetOptions;
1704                                         n->name = $3;
1705                                         n->def = (Node *) $5;
1706                                         $$ = (Node *)n;
1707                                 }
1708                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
1709                         | ALTER opt_column ColId RESET reloptions
1710                                 {
1711                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1712                                         n->subtype = AT_ResetOptions;
1713                                         n->name = $3;
1714                                         n->def = (Node *) $5;
1715                                         $$ = (Node *)n;
1716                                 }
1717                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1718                         | ALTER opt_column ColId SET STORAGE ColId
1719                                 {
1720                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1721                                         n->subtype = AT_SetStorage;
1722                                         n->name = $3;
1723                                         n->def = (Node *) makeString($6);
1724                                         $$ = (Node *)n;
1725                                 }
1726                         /* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
1727                         | DROP opt_column IF_P EXISTS ColId opt_drop_behavior
1728                                 {
1729                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1730                                         n->subtype = AT_DropColumn;
1731                                         n->name = $5;
1732                                         n->behavior = $6;
1733                                         n->missing_ok = TRUE;
1734                                         $$ = (Node *)n;
1735                                 }
1736                         /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1737                         | DROP opt_column ColId opt_drop_behavior
1738                                 {
1739                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1740                                         n->subtype = AT_DropColumn;
1741                                         n->name = $3;
1742                                         n->behavior = $4;
1743                                         n->missing_ok = FALSE;
1744                                         $$ = (Node *)n;
1745                                 }
1746                         /*
1747                          * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
1748                          *              [ USING <expression> ]
1749                          */
1750                         | ALTER opt_column ColId opt_set_data TYPE_P Typename opt_collate_clause alter_using
1751                                 {
1752                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1753                                         ColumnDef *def = makeNode(ColumnDef);
1754                                         n->subtype = AT_AlterColumnType;
1755                                         n->name = $3;
1756                                         n->def = (Node *) def;
1757                                         /* We only use these three fields of the ColumnDef node */
1758                                         def->typeName = $6;
1759                                         def->collClause = (CollateClause *) $7;
1760                                         def->raw_default = $8;
1761                                         $$ = (Node *)n;
1762                                 }
1763                         /* ALTER TABLE <name> ADD CONSTRAINT ... */
1764                         | ADD_P TableConstraint
1765                                 {
1766                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1767                                         n->subtype = AT_AddConstraint;
1768                                         n->def = $2;
1769                                         $$ = (Node *)n;
1770                                 }
1771                         /* ALTER TABLE <name> VALIDATE CONSTRAINT ... */
1772                         | VALIDATE CONSTRAINT name
1773                                 {
1774                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1775                                         n->subtype = AT_ValidateConstraint;
1776                                         n->name = $3;
1777                                         $$ = (Node *)n;
1778                                 }
1779                         /* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
1780                         | DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
1781                                 {
1782                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1783                                         n->subtype = AT_DropConstraint;
1784                                         n->name = $5;
1785                                         n->behavior = $6;
1786                                         n->missing_ok = TRUE;
1787                                         $$ = (Node *)n;
1788                                 }
1789                         /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1790                         | DROP CONSTRAINT name opt_drop_behavior
1791                                 {
1792                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1793                                         n->subtype = AT_DropConstraint;
1794                                         n->name = $3;
1795                                         n->behavior = $4;
1796                                         n->missing_ok = FALSE;
1797                                         $$ = (Node *)n;
1798                                 }
1799                         /* ALTER TABLE <name> SET WITH OIDS  */
1800                         | SET WITH OIDS
1801                                 {
1802                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1803                                         n->subtype = AT_AddOids;
1804                                         $$ = (Node *)n;
1805                                 }
1806                         /* ALTER TABLE <name> SET WITHOUT OIDS  */
1807                         | SET WITHOUT OIDS
1808                                 {
1809                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1810                                         n->subtype = AT_DropOids;
1811                                         $$ = (Node *)n;
1812                                 }
1813                         /* ALTER TABLE <name> CLUSTER ON <indexname> */
1814                         | CLUSTER ON name
1815                                 {
1816                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1817                                         n->subtype = AT_ClusterOn;
1818                                         n->name = $3;
1819                                         $$ = (Node *)n;
1820                                 }
1821                         /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1822                         | SET WITHOUT CLUSTER
1823                                 {
1824                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1825                                         n->subtype = AT_DropCluster;
1826                                         n->name = NULL;
1827                                         $$ = (Node *)n;
1828                                 }
1829                         /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1830                         | ENABLE_P TRIGGER name
1831                                 {
1832                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1833                                         n->subtype = AT_EnableTrig;
1834                                         n->name = $3;
1835                                         $$ = (Node *)n;
1836                                 }
1837                         /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1838                         | ENABLE_P ALWAYS TRIGGER name
1839                                 {
1840                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1841                                         n->subtype = AT_EnableAlwaysTrig;
1842                                         n->name = $4;
1843                                         $$ = (Node *)n;
1844                                 }
1845                         /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1846                         | ENABLE_P REPLICA TRIGGER name
1847                                 {
1848                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1849                                         n->subtype = AT_EnableReplicaTrig;
1850                                         n->name = $4;
1851                                         $$ = (Node *)n;
1852                                 }
1853                         /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1854                         | ENABLE_P TRIGGER ALL
1855                                 {
1856                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1857                                         n->subtype = AT_EnableTrigAll;
1858                                         $$ = (Node *)n;
1859                                 }
1860                         /* ALTER TABLE <name> ENABLE TRIGGER USER */
1861                         | ENABLE_P TRIGGER USER
1862                                 {
1863                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1864                                         n->subtype = AT_EnableTrigUser;
1865                                         $$ = (Node *)n;
1866                                 }
1867                         /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1868                         | DISABLE_P TRIGGER name
1869                                 {
1870                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1871                                         n->subtype = AT_DisableTrig;
1872                                         n->name = $3;
1873                                         $$ = (Node *)n;
1874                                 }
1875                         /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1876                         | DISABLE_P TRIGGER ALL
1877                                 {
1878                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1879                                         n->subtype = AT_DisableTrigAll;
1880                                         $$ = (Node *)n;
1881                                 }
1882                         /* ALTER TABLE <name> DISABLE TRIGGER USER */
1883                         | DISABLE_P TRIGGER USER
1884                                 {
1885                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1886                                         n->subtype = AT_DisableTrigUser;
1887                                         $$ = (Node *)n;
1888                                 }
1889                         /* ALTER TABLE <name> ENABLE RULE <rule> */
1890                         | ENABLE_P RULE name
1891                                 {
1892                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1893                                         n->subtype = AT_EnableRule;
1894                                         n->name = $3;
1895                                         $$ = (Node *)n;
1896                                 }
1897                         /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1898                         | ENABLE_P ALWAYS RULE name
1899                                 {
1900                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1901                                         n->subtype = AT_EnableAlwaysRule;
1902                                         n->name = $4;
1903                                         $$ = (Node *)n;
1904                                 }
1905                         /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1906                         | ENABLE_P REPLICA RULE name
1907                                 {
1908                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1909                                         n->subtype = AT_EnableReplicaRule;
1910                                         n->name = $4;
1911                                         $$ = (Node *)n;
1912                                 }
1913                         /* ALTER TABLE <name> DISABLE RULE <rule> */
1914                         | DISABLE_P RULE name
1915                                 {
1916                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1917                                         n->subtype = AT_DisableRule;
1918                                         n->name = $3;
1919                                         $$ = (Node *)n;
1920                                 }
1921                         /* ALTER TABLE <name> INHERIT <parent> */
1922                         | INHERIT qualified_name
1923                                 {
1924                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1925                                         n->subtype = AT_AddInherit;
1926                                         n->def = (Node *) $2;
1927                                         $$ = (Node *)n;
1928                                 }
1929                         /* ALTER TABLE <name> NO INHERIT <parent> */
1930                         | NO INHERIT qualified_name
1931                                 {
1932                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1933                                         n->subtype = AT_DropInherit;
1934                                         n->def = (Node *) $3;
1935                                         $$ = (Node *)n;
1936                                 }
1937                         /* ALTER TABLE <name> OF <type_name> */
1938                         | OF any_name
1939                                 {
1940                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1941                                         TypeName *def = makeTypeNameFromNameList($2);
1942                                         def->location = @2;
1943                                         n->subtype = AT_AddOf;
1944                                         n->def = (Node *) def;
1945                                         $$ = (Node *)n;
1946                                 }
1947                         /* ALTER TABLE <name> NOT OF */
1948                         | NOT OF
1949                                 {
1950                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1951                                         n->subtype = AT_DropOf;
1952                                         $$ = (Node *)n;
1953                                 }
1954                         /* ALTER TABLE <name> OWNER TO RoleId */
1955                         | OWNER TO RoleId
1956                                 {
1957                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1958                                         n->subtype = AT_ChangeOwner;
1959                                         n->name = $3;
1960                                         $$ = (Node *)n;
1961                                 }
1962                         /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
1963                         | SET TABLESPACE name
1964                                 {
1965                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1966                                         n->subtype = AT_SetTableSpace;
1967                                         n->name = $3;
1968                                         $$ = (Node *)n;
1969                                 }
1970                         /* ALTER TABLE <name> SET (...) */
1971                         | SET reloptions
1972                                 {
1973                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1974                                         n->subtype = AT_SetRelOptions;
1975                                         n->def = (Node *)$2;
1976                                         $$ = (Node *)n;
1977                                 }
1978                         /* ALTER TABLE <name> RESET (...) */
1979                         | RESET reloptions
1980                                 {
1981                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1982                                         n->subtype = AT_ResetRelOptions;
1983                                         n->def = (Node *)$2;
1984                                         $$ = (Node *)n;
1985                                 }
1986                         | alter_generic_options
1987                                 {
1988                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1989                                         n->subtype = AT_GenericOptions;
1990                                         n->def = (Node *)$1;
1991                                         $$ = (Node *) n;
1992                                 }
1993                 ;
1994
1995 alter_column_default:
1996                         SET DEFAULT a_expr                      { $$ = $3; }
1997                         | DROP DEFAULT                          { $$ = NULL; }
1998                 ;
1999
2000 opt_drop_behavior:
2001                         CASCADE                                         { $$ = DROP_CASCADE; }
2002                         | RESTRICT                                      { $$ = DROP_RESTRICT; }
2003                         | /* EMPTY */                           { $$ = DROP_RESTRICT; /* default */ }
2004                 ;
2005
2006 opt_collate_clause:
2007                         COLLATE any_name
2008                                 {
2009                                         CollateClause *n = makeNode(CollateClause);
2010                                         n->arg = NULL;
2011                                         n->collname = $2;
2012                                         n->location = @1;
2013                                         $$ = (Node *) n;
2014                                 }
2015                         | /* EMPTY */                           { $$ = NULL; }
2016                 ;
2017
2018 alter_using:
2019                         USING a_expr                            { $$ = $2; }
2020                         | /* EMPTY */                           { $$ = NULL; }
2021                 ;
2022
2023 reloptions:
2024                         '(' reloption_list ')'                                  { $$ = $2; }
2025                 ;
2026
2027 opt_reloptions:         WITH reloptions                                 { $$ = $2; }
2028                          |              /* EMPTY */                                             { $$ = NIL; }
2029                 ;
2030
2031 reloption_list:
2032                         reloption_elem                                                  { $$ = list_make1($1); }
2033                         | reloption_list ',' reloption_elem             { $$ = lappend($1, $3); }
2034                 ;
2035
2036 /* This should match def_elem and also allow qualified names */
2037 reloption_elem:
2038                         ColLabel '=' def_arg
2039                                 {
2040                                         $$ = makeDefElem($1, (Node *) $3);
2041                                 }
2042                         | ColLabel
2043                                 {
2044                                         $$ = makeDefElem($1, NULL);
2045                                 }
2046                         | ColLabel '.' ColLabel '=' def_arg
2047                                 {
2048                                         $$ = makeDefElemExtended($1, $3, (Node *) $5,
2049                                                                                          DEFELEM_UNSPEC);
2050                                 }
2051                         | ColLabel '.' ColLabel
2052                                 {
2053                                         $$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC);
2054                                 }
2055                 ;
2056
2057
2058 /*****************************************************************************
2059  *
2060  *      ALTER TYPE
2061  *
2062  * really variants of the ALTER TABLE subcommands with different spellings
2063  *****************************************************************************/
2064
2065 AlterCompositeTypeStmt:
2066                         ALTER TYPE_P any_name alter_type_cmds
2067                                 {
2068                                         AlterTableStmt *n = makeNode(AlterTableStmt);
2069
2070                                         /* can't use qualified_name, sigh */
2071                                         n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
2072                                         n->cmds = $4;
2073                                         n->relkind = OBJECT_TYPE;
2074                                         $$ = (Node *)n;
2075                                 }
2076                         ;
2077
2078 alter_type_cmds:
2079                         alter_type_cmd                                                  { $$ = list_make1($1); }
2080                         | alter_type_cmds ',' alter_type_cmd    { $$ = lappend($1, $3); }
2081                 ;
2082
2083 alter_type_cmd:
2084                         /* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
2085                         ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
2086                                 {
2087                                         AlterTableCmd *n = makeNode(AlterTableCmd);
2088                                         n->subtype = AT_AddColumn;
2089                                         n->def = $3;
2090                                         n->behavior = $4;
2091                                         $$ = (Node *)n;
2092                                 }
2093                         /* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
2094                         | DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
2095                                 {
2096                                         AlterTableCmd *n = makeNode(AlterTableCmd);
2097                                         n->subtype = AT_DropColumn;
2098                                         n->name = $5;
2099                                         n->behavior = $6;
2100                                         n->missing_ok = TRUE;
2101                                         $$ = (Node *)n;
2102                                 }
2103                         /* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
2104                         | DROP ATTRIBUTE ColId opt_drop_behavior
2105                                 {
2106                                         AlterTableCmd *n = makeNode(AlterTableCmd);
2107                                         n->subtype = AT_DropColumn;
2108                                         n->name = $3;
2109                                         n->behavior = $4;
2110                                         n->missing_ok = FALSE;
2111                                         $$ = (Node *)n;
2112                                 }
2113                         /* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
2114                         | ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_collate_clause opt_drop_behavior
2115                                 {
2116                                         AlterTableCmd *n = makeNode(AlterTableCmd);
2117                                         ColumnDef *def = makeNode(ColumnDef);
2118                                         n->subtype = AT_AlterColumnType;
2119                                         n->name = $3;
2120                                         n->def = (Node *) def;
2121                                         n->behavior = $8;
2122                                         /* We only use these three fields of the ColumnDef node */
2123                                         def->typeName = $6;
2124                                         def->collClause = (CollateClause *) $7;
2125                                         def->raw_default = NULL;
2126                                         $$ = (Node *)n;
2127                                 }
2128                 ;
2129
2130
2131 /*****************************************************************************
2132  *
2133  *              QUERY :
2134  *                              close <portalname>
2135  *
2136  *****************************************************************************/
2137
2138 ClosePortalStmt:
2139                         CLOSE cursor_name
2140                                 {
2141                                         ClosePortalStmt *n = makeNode(ClosePortalStmt);
2142                                         n->portalname = $2;
2143                                         $$ = (Node *)n;
2144                                 }
2145                         | CLOSE ALL
2146                                 {
2147                                         ClosePortalStmt *n = makeNode(ClosePortalStmt);
2148                                         n->portalname = NULL;
2149                                         $$ = (Node *)n;
2150                                 }
2151                 ;
2152
2153
2154 /*****************************************************************************
2155  *
2156  *              QUERY :
2157  *                              COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
2158  *                              COPY ( SELECT ... ) TO file [WITH] [(options)]
2159  *
2160  *                              In the preferred syntax the options are comma-separated
2161  *                              and use generic identifiers instead of keywords.  The pre-9.0
2162  *                              syntax had a hard-wired, space-separated set of options.
2163  *
2164  *                              Really old syntax, from versions 7.2 and prior:
2165  *                              COPY [ BINARY ] table [ WITH OIDS ] FROM/TO file
2166  *                                      [ [ USING ] DELIMITERS 'delimiter' ] ]
2167  *                                      [ WITH NULL AS 'null string' ]
2168  *                              This option placement is not supported with COPY (SELECT...).
2169  *
2170  *****************************************************************************/
2171
2172 CopyStmt:       COPY opt_binary qualified_name opt_column_list opt_oids
2173                         copy_from copy_file_name copy_delimiter opt_with copy_options
2174                                 {
2175                                         CopyStmt *n = makeNode(CopyStmt);
2176                                         n->relation = $3;
2177                                         n->query = NULL;
2178                                         n->attlist = $4;
2179                                         n->is_from = $6;
2180                                         n->filename = $7;
2181
2182                                         n->options = NIL;
2183                                         /* Concatenate user-supplied flags */
2184                                         if ($2)
2185                                                 n->options = lappend(n->options, $2);
2186                                         if ($5)
2187                                                 n->options = lappend(n->options, $5);
2188                                         if ($8)
2189                                                 n->options = lappend(n->options, $8);
2190                                         if ($10)
2191                                                 n->options = list_concat(n->options, $10);
2192                                         $$ = (Node *)n;
2193                                 }
2194                         | COPY select_with_parens TO copy_file_name opt_with copy_options
2195                                 {
2196                                         CopyStmt *n = makeNode(CopyStmt);
2197                                         n->relation = NULL;
2198                                         n->query = $2;
2199                                         n->attlist = NIL;
2200                                         n->is_from = false;
2201                                         n->filename = $4;
2202                                         n->options = $6;
2203                                         $$ = (Node *)n;
2204                                 }
2205                 ;
2206
2207 copy_from:
2208                         FROM                                                                    { $$ = TRUE; }
2209                         | TO                                                                    { $$ = FALSE; }
2210                 ;
2211
2212 /*
2213  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
2214  * used depends on the direction. (It really doesn't make sense to copy from
2215  * stdout. We silently correct the "typo".)              - AY 9/94
2216  */
2217 copy_file_name:
2218                         Sconst                                                                  { $$ = $1; }
2219                         | STDIN                                                                 { $$ = NULL; }
2220                         | STDOUT                                                                { $$ = NULL; }
2221                 ;
2222
2223 copy_options: copy_opt_list                                                     { $$ = $1; }
2224                         | '(' copy_generic_opt_list ')'                 { $$ = $2; }
2225                 ;
2226
2227 /* old COPY option syntax */
2228 copy_opt_list:
2229                         copy_opt_list copy_opt_item                             { $$ = lappend($1, $2); }
2230                         | /* EMPTY */                                                   { $$ = NIL; }
2231                 ;
2232
2233 copy_opt_item:
2234                         BINARY
2235                                 {
2236                                         $$ = makeDefElem("format", (Node *)makeString("binary"));
2237                                 }
2238                         | OIDS
2239                                 {
2240                                         $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2241                                 }
2242                         | DELIMITER opt_as Sconst
2243                                 {
2244                                         $$ = makeDefElem("delimiter", (Node *)makeString($3));
2245                                 }
2246                         | NULL_P opt_as Sconst
2247                                 {
2248                                         $$ = makeDefElem("null", (Node *)makeString($3));
2249                                 }
2250                         | CSV
2251                                 {
2252                                         $$ = makeDefElem("format", (Node *)makeString("csv"));
2253                                 }
2254                         | HEADER_P
2255                                 {
2256                                         $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
2257                                 }
2258                         | QUOTE opt_as Sconst
2259                                 {
2260                                         $$ = makeDefElem("quote", (Node *)makeString($3));
2261                                 }
2262                         | ESCAPE opt_as Sconst
2263                                 {
2264                                         $$ = makeDefElem("escape", (Node *)makeString($3));
2265                                 }
2266                         | FORCE QUOTE columnList
2267                                 {
2268                                         $$ = makeDefElem("force_quote", (Node *)$3);
2269                                 }
2270                         | FORCE QUOTE '*'
2271                                 {
2272                                         $$ = makeDefElem("force_quote", (Node *)makeNode(A_Star));
2273                                 }
2274                         | FORCE NOT NULL_P columnList
2275                                 {
2276                                         $$ = makeDefElem("force_not_null", (Node *)$4);
2277                                 }
2278                         | ENCODING Sconst
2279                                 {
2280                                         $$ = makeDefElem("encoding", (Node *)makeString($2));
2281                                 }
2282                 ;
2283
2284 /* The following exist for backward compatibility with very old versions */
2285
2286 opt_binary:
2287                         BINARY
2288                                 {
2289                                         $$ = makeDefElem("format", (Node *)makeString("binary"));
2290                                 }
2291                         | /*EMPTY*/                                                             { $$ = NULL; }
2292                 ;
2293
2294 opt_oids:
2295                         WITH OIDS
2296                                 {
2297                                         $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2298                                 }
2299                         | /*EMPTY*/                                                             { $$ = NULL; }
2300                 ;
2301
2302 copy_delimiter:
2303                         opt_using DELIMITERS Sconst
2304                                 {
2305                                         $$ = makeDefElem("delimiter", (Node *)makeString($3));
2306                                 }
2307                         | /*EMPTY*/                                                             { $$ = NULL; }
2308                 ;
2309
2310 opt_using:
2311                         USING                                                                   {}
2312                         | /*EMPTY*/                                                             {}
2313                 ;
2314
2315 /* new COPY option syntax */
2316 copy_generic_opt_list:
2317                         copy_generic_opt_elem
2318                                 {
2319                                         $$ = list_make1($1);
2320                                 }
2321                         | copy_generic_opt_list ',' copy_generic_opt_elem
2322                                 {
2323                                         $$ = lappend($1, $3);
2324                                 }
2325                 ;
2326
2327 copy_generic_opt_elem:
2328                         ColLabel copy_generic_opt_arg
2329                                 {
2330                                         $$ = makeDefElem($1, $2);
2331                                 }
2332                 ;
2333
2334 copy_generic_opt_arg:
2335                         opt_boolean_or_string                   { $$ = (Node *) makeString($1); }
2336                         | NumericOnly                                   { $$ = (Node *) $1; }
2337                         | '*'                                                   { $$ = (Node *) makeNode(A_Star); }
2338                         | '(' copy_generic_opt_arg_list ')'             { $$ = (Node *) $2; }
2339                         | /* EMPTY */                                   { $$ = NULL; }
2340                 ;
2341
2342 copy_generic_opt_arg_list:
2343                           copy_generic_opt_arg_list_item
2344                                 {
2345                                         $$ = list_make1($1);
2346                                 }
2347                         | copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
2348                                 {
2349                                         $$ = lappend($1, $3);
2350                                 }
2351                 ;
2352
2353 /* beware of emitting non-string list elements here; see commands/define.c */
2354 copy_generic_opt_arg_list_item:
2355                         opt_boolean_or_string   { $$ = (Node *) makeString($1); }
2356                 ;
2357
2358
2359 /*****************************************************************************
2360  *
2361  *              QUERY :
2362  *                              CREATE TABLE relname
2363  *
2364  *****************************************************************************/
2365
2366 CreateStmt:     CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
2367                         OptInherit OptWith OnCommitOption OptTableSpace
2368                                 {
2369                                         CreateStmt *n = makeNode(CreateStmt);
2370                                         $4->relpersistence = $2;
2371                                         n->relation = $4;
2372                                         n->tableElts = $6;
2373                                         n->inhRelations = $8;
2374                                         n->constraints = NIL;
2375                                         n->options = $9;
2376                                         n->oncommit = $10;
2377                                         n->tablespacename = $11;
2378                                         n->if_not_exists = false;
2379                                         $$ = (Node *)n;
2380                                 }
2381                 | CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
2382                         OptTableElementList ')' OptInherit OptWith OnCommitOption
2383                         OptTableSpace
2384                                 {
2385                                         CreateStmt *n = makeNode(CreateStmt);
2386                                         $7->relpersistence = $2;
2387                                         n->relation = $7;
2388                                         n->tableElts = $9;
2389                                         n->inhRelations = $11;
2390                                         n->constraints = NIL;
2391                                         n->options = $12;
2392                                         n->oncommit = $13;
2393                                         n->tablespacename = $14;
2394                                         n->if_not_exists = true;
2395                                         $$ = (Node *)n;
2396                                 }
2397                 | CREATE OptTemp TABLE qualified_name OF any_name
2398                         OptTypedTableElementList OptWith OnCommitOption OptTableSpace
2399                                 {
2400                                         CreateStmt *n = makeNode(CreateStmt);
2401                                         $4->relpersistence = $2;
2402                                         n->relation = $4;
2403                                         n->tableElts = $7;
2404                                         n->ofTypename = makeTypeNameFromNameList($6);
2405                                         n->ofTypename->location = @6;
2406                                         n->constraints = NIL;
2407                                         n->options = $8;
2408                                         n->oncommit = $9;
2409                                         n->tablespacename = $10;
2410                                         n->if_not_exists = false;
2411                                         $$ = (Node *)n;
2412                                 }
2413                 | CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
2414                         OptTypedTableElementList OptWith OnCommitOption OptTableSpace
2415                                 {
2416                                         CreateStmt *n = makeNode(CreateStmt);
2417                                         $7->relpersistence = $2;
2418                                         n->relation = $7;
2419                                         n->tableElts = $10;
2420                                         n->ofTypename = makeTypeNameFromNameList($9);
2421                                         n->ofTypename->location = @9;
2422                                         n->constraints = NIL;
2423                                         n->options = $11;
2424                                         n->oncommit = $12;
2425                                         n->tablespacename = $13;
2426                                         n->if_not_exists = true;
2427                                         $$ = (Node *)n;
2428                                 }
2429                 ;
2430
2431 /*
2432  * Redundancy here is needed to avoid shift/reduce conflicts,
2433  * since TEMP is not a reserved word.  See also OptTempTableName.
2434  *
2435  * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
2436  * the LOCAL keyword is really meaningless.
2437  */
2438 OptTemp:        TEMPORARY                                       { $$ = RELPERSISTENCE_TEMP; }
2439                         | TEMP                                          { $$ = RELPERSISTENCE_TEMP; }
2440                         | LOCAL TEMPORARY                       { $$ = RELPERSISTENCE_TEMP; }
2441                         | LOCAL TEMP                            { $$ = RELPERSISTENCE_TEMP; }
2442                         | GLOBAL TEMPORARY                      { $$ = RELPERSISTENCE_TEMP; }
2443                         | GLOBAL TEMP                           { $$ = RELPERSISTENCE_TEMP; }
2444                         | UNLOGGED                                      { $$ = RELPERSISTENCE_UNLOGGED; }
2445                         | /*EMPTY*/                                     { $$ = RELPERSISTENCE_PERMANENT; }
2446                 ;
2447
2448 OptTableElementList:
2449                         TableElementList                                        { $$ = $1; }
2450                         | /*EMPTY*/                                                     { $$ = NIL; }
2451                 ;
2452
2453 OptTypedTableElementList:
2454                         '(' TypedTableElementList ')'           { $$ = $2; }
2455                         | /*EMPTY*/                                                     { $$ = NIL; }
2456                 ;
2457
2458 TableElementList:
2459                         TableElement
2460                                 {
2461                                         $$ = list_make1($1);
2462                                 }
2463                         | TableElementList ',' TableElement
2464                                 {
2465                                         $$ = lappend($1, $3);
2466                                 }
2467                 ;
2468
2469 TypedTableElementList:
2470                         TypedTableElement
2471                                 {
2472                                         $$ = list_make1($1);
2473                                 }
2474                         | TypedTableElementList ',' TypedTableElement
2475                                 {
2476                                         $$ = lappend($1, $3);
2477                                 }
2478                 ;
2479
2480 TableElement:
2481                         columnDef                                                       { $$ = $1; }
2482                         | TableLikeClause                                       { $$ = $1; }
2483                         | TableConstraint                                       { $$ = $1; }
2484                 ;
2485
2486 TypedTableElement:
2487                         columnOptions                                           { $$ = $1; }
2488                         | TableConstraint                                       { $$ = $1; }
2489                 ;
2490
2491 columnDef:      ColId Typename ColQualList
2492                                 {
2493                                         ColumnDef *n = makeNode(ColumnDef);
2494                                         n->colname = $1;
2495                                         n->typeName = $2;
2496                                         n->inhcount = 0;
2497                                         n->is_local = true;
2498                                         n->is_not_null = false;
2499                                         n->is_from_type = false;
2500                                         n->storage = 0;
2501                                         n->raw_default = NULL;
2502                                         n->cooked_default = NULL;
2503                                         n->collOid = InvalidOid;
2504                                         SplitColQualList($3, &n->constraints, &n->collClause,
2505                                                                          yyscanner);
2506                                         $$ = (Node *)n;
2507                                 }
2508                 ;
2509
2510 columnOptions:  ColId WITH OPTIONS ColQualList
2511                                 {
2512                                         ColumnDef *n = makeNode(ColumnDef);
2513                                         n->colname = $1;
2514                                         n->typeName = NULL;
2515                                         n->inhcount = 0;
2516                                         n->is_local = true;
2517                                         n->is_not_null = false;
2518                                         n->is_from_type = false;
2519                                         n->storage = 0;
2520                                         n->raw_default = NULL;
2521                                         n->cooked_default = NULL;
2522                                         n->collOid = InvalidOid;
2523                                         SplitColQualList($4, &n->constraints, &n->collClause,
2524                                                                          yyscanner);
2525                                         $$ = (Node *)n;
2526                                 }
2527                 ;
2528
2529 ColQualList:
2530                         ColQualList ColConstraint                               { $$ = lappend($1, $2); }
2531                         | /*EMPTY*/                                                             { $$ = NIL; }
2532                 ;
2533
2534 ColConstraint:
2535                         CONSTRAINT name ColConstraintElem
2536                                 {
2537                                         Constraint *n = (Constraint *) $3;
2538                                         Assert(IsA(n, Constraint));
2539                                         n->conname = $2;
2540                                         n->location = @1;
2541                                         $$ = (Node *) n;
2542                                 }
2543                         | ColConstraintElem                                             { $$ = $1; }
2544                         | ConstraintAttr                                                { $$ = $1; }
2545                         | COLLATE any_name
2546                                 {
2547                                         /*
2548                                          * Note: the CollateClause is momentarily included in
2549                                          * the list built by ColQualList, but we split it out
2550                                          * again in SplitColQualList.
2551                                          */
2552                                         CollateClause *n = makeNode(CollateClause);
2553                                         n->arg = NULL;
2554                                         n->collname = $2;
2555                                         n->location = @1;
2556                                         $$ = (Node *) n;
2557                                 }
2558                 ;
2559
2560 /* DEFAULT NULL is already the default for Postgres.
2561  * But define it here and carry it forward into the system
2562  * to make it explicit.
2563  * - thomas 1998-09-13
2564  *
2565  * WITH NULL and NULL are not SQL92-standard syntax elements,
2566  * so leave them out. Use DEFAULT NULL to explicitly indicate
2567  * that a column may have that value. WITH NULL leads to
2568  * shift/reduce conflicts with WITH TIME ZONE anyway.
2569  * - thomas 1999-01-08
2570  *
2571  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2572  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2573  * or be part of a_expr NOT LIKE or similar constructs).
2574  */
2575 ColConstraintElem:
2576                         NOT NULL_P
2577                                 {
2578                                         Constraint *n = makeNode(Constraint);
2579                                         n->contype = CONSTR_NOTNULL;
2580                                         n->location = @1;
2581                                         $$ = (Node *)n;
2582                                 }
2583                         | NULL_P
2584                                 {
2585                                         Constraint *n = makeNode(Constraint);
2586                                         n->contype = CONSTR_NULL;
2587                                         n->location = @1;
2588                                         $$ = (Node *)n;
2589                                 }
2590                         | UNIQUE opt_definition OptConsTableSpace
2591                                 {
2592                                         Constraint *n = makeNode(Constraint);
2593                                         n->contype = CONSTR_UNIQUE;
2594                                         n->location = @1;
2595                                         n->keys = NULL;
2596                                         n->options = $2;
2597                                         n->indexname = NULL;
2598                                         n->indexspace = $3;
2599                                         $$ = (Node *)n;
2600                                 }
2601                         | PRIMARY KEY opt_definition OptConsTableSpace
2602                                 {
2603                                         Constraint *n = makeNode(Constraint);
2604                                         n->contype = CONSTR_PRIMARY;
2605                                         n->location = @1;
2606                                         n->keys = NULL;
2607                                         n->options = $3;
2608                                         n->indexname = NULL;
2609                                         n->indexspace = $4;
2610                                         $$ = (Node *)n;
2611                                 }
2612                         | CHECK '(' a_expr ')'
2613                                 {
2614                                         Constraint *n = makeNode(Constraint);
2615                                         n->contype = CONSTR_CHECK;
2616                                         n->location = @1;
2617                                         n->raw_expr = $3;
2618                                         n->cooked_expr = NULL;
2619                                         $$ = (Node *)n;
2620                                 }
2621                         | DEFAULT b_expr
2622                                 {
2623                                         Constraint *n = makeNode(Constraint);
2624                                         n->contype = CONSTR_DEFAULT;
2625                                         n->location = @1;
2626                                         n->raw_expr = $2;
2627                                         n->cooked_expr = NULL;
2628                                         $$ = (Node *)n;
2629                                 }
2630                         | REFERENCES qualified_name opt_column_list key_match key_actions
2631                                 {
2632                                         Constraint *n = makeNode(Constraint);
2633                                         n->contype = CONSTR_FOREIGN;
2634                                         n->location = @1;
2635                                         n->pktable                      = $2;
2636                                         n->fk_attrs                     = NIL;
2637                                         n->pk_attrs                     = $3;
2638                                         n->fk_matchtype         = $4;
2639                                         n->fk_upd_action        = (char) ($5 >> 8);
2640                                         n->fk_del_action        = (char) ($5 & 0xFF);
2641                                         n->skip_validation  = FALSE;
2642                                         n->initially_valid  = true;
2643                                         $$ = (Node *)n;
2644                                 }
2645                 ;
2646
2647 /*
2648  * ConstraintAttr represents constraint attributes, which we parse as if
2649  * they were independent constraint clauses, in order to avoid shift/reduce
2650  * conflicts (since NOT might start either an independent NOT NULL clause
2651  * or an attribute).  parse_utilcmd.c is responsible for attaching the
2652  * attribute information to the preceding "real" constraint node, and for
2653  * complaining if attribute clauses appear in the wrong place or wrong
2654  * combinations.
2655  *
2656  * See also ConstraintAttributeSpec, which can be used in places where
2657  * there is no parsing conflict.
2658  */
2659 ConstraintAttr:
2660                         DEFERRABLE
2661                                 {
2662                                         Constraint *n = makeNode(Constraint);
2663                                         n->contype = CONSTR_ATTR_DEFERRABLE;
2664                                         n->location = @1;
2665                                         $$ = (Node *)n;
2666                                 }
2667                         | NOT DEFERRABLE
2668                                 {
2669                                         Constraint *n = makeNode(Constraint);
2670                                         n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2671                                         n->location = @1;
2672                                         $$ = (Node *)n;
2673                                 }
2674                         | INITIALLY DEFERRED
2675                                 {
2676                                         Constraint *n = makeNode(Constraint);
2677                                         n->contype = CONSTR_ATTR_DEFERRED;
2678                                         n->location = @1;
2679                                         $$ = (Node *)n;
2680                                 }
2681                         | INITIALLY IMMEDIATE
2682                                 {
2683                                         Constraint *n = makeNode(Constraint);
2684                                         n->contype = CONSTR_ATTR_IMMEDIATE;
2685                                         n->location = @1;
2686                                         $$ = (Node *)n;
2687                                 }
2688                 ;
2689
2690
2691 /*
2692  * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2693  * This seems to be a poor man's inheritance capability, with the resulting
2694  * tables completely decoupled except for the original commonality in definitions.
2695  *
2696  * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2697  * which is a part of SQL:2003.
2698  */
2699 TableLikeClause:
2700                         LIKE qualified_name TableLikeOptionList
2701                                 {
2702                                         InhRelation *n = makeNode(InhRelation);
2703                                         n->relation = $2;
2704                                         n->options = $3;
2705                                         $$ = (Node *)n;
2706                                 }
2707                 ;
2708
2709 TableLikeOptionList:
2710                                 TableLikeOptionList INCLUDING TableLikeOption   { $$ = $1 | $3; }
2711                                 | TableLikeOptionList EXCLUDING TableLikeOption { $$ = $1 & ~$3; }
2712                                 | /* EMPTY */                                           { $$ = 0; }
2713                 ;
2714
2715 TableLikeOption:
2716                                 DEFAULTS                        { $$ = CREATE_TABLE_LIKE_DEFAULTS; }
2717                                 | CONSTRAINTS           { $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
2718                                 | INDEXES                       { $$ = CREATE_TABLE_LIKE_INDEXES; }
2719                                 | STORAGE                       { $$ = CREATE_TABLE_LIKE_STORAGE; }
2720                                 | COMMENTS                      { $$ = CREATE_TABLE_LIKE_COMMENTS; }
2721                                 | ALL                           { $$ = CREATE_TABLE_LIKE_ALL; }
2722                 ;
2723
2724
2725 /* ConstraintElem specifies constraint syntax which is not embedded into
2726  *      a column definition. ColConstraintElem specifies the embedded form.
2727  * - thomas 1997-12-03
2728  */
2729 TableConstraint:
2730                         CONSTRAINT name ConstraintElem
2731                                 {
2732                                         Constraint *n = (Constraint *) $3;
2733                                         Assert(IsA(n, Constraint));
2734                                         n->conname = $2;
2735                                         n->location = @1;
2736                                         $$ = (Node *) n;
2737                                 }
2738                         | ConstraintElem                                                { $$ = $1; }
2739                 ;
2740
2741 ConstraintElem:
2742                         CHECK '(' a_expr ')' ConstraintAttributeSpec
2743                                 {
2744                                         Constraint *n = makeNode(Constraint);
2745                                         n->contype = CONSTR_CHECK;
2746                                         n->location = @1;
2747                                         n->raw_expr = $3;
2748                                         n->cooked_expr = NULL;
2749                                         if ($5 != 0)
2750                                                 ereport(ERROR,
2751                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2752                                                                  errmsg("CHECK constraints cannot be deferred"),
2753                                                                  parser_errposition(@5)));
2754                                         $$ = (Node *)n;
2755                                 }
2756                         | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2757                                 ConstraintAttributeSpec
2758                                 {
2759                                         Constraint *n = makeNode(Constraint);
2760                                         n->contype = CONSTR_UNIQUE;
2761                                         n->location = @1;
2762                                         n->keys = $3;
2763                                         n->options = $5;
2764                                         n->indexname = NULL;
2765                                         n->indexspace = $6;
2766                                         n->deferrable = ($7 & 1) != 0;
2767                                         n->initdeferred = ($7 & 2) != 0;
2768                                         $$ = (Node *)n;
2769                                 }
2770                         | UNIQUE ExistingIndex ConstraintAttributeSpec
2771                                 {
2772                                         Constraint *n = makeNode(Constraint);
2773                                         n->contype = CONSTR_UNIQUE;
2774                                         n->location = @1;
2775                                         n->keys = NIL;
2776                                         n->options = NIL;
2777                                         n->indexname = $2;
2778                                         n->indexspace = NULL;
2779                                         n->deferrable = ($3 & 1) != 0;
2780                                         n->initdeferred = ($3 & 2) != 0;
2781                                         $$ = (Node *)n;
2782                                 }
2783                         | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2784                                 ConstraintAttributeSpec
2785                                 {
2786                                         Constraint *n = makeNode(Constraint);
2787                                         n->contype = CONSTR_PRIMARY;
2788                                         n->location = @1;
2789                                         n->keys = $4;
2790                                         n->options = $6;
2791                                         n->indexname = NULL;
2792                                         n->indexspace = $7;
2793                                         n->deferrable = ($8 & 1) != 0;
2794                                         n->initdeferred = ($8 & 2) != 0;
2795                                         $$ = (Node *)n;
2796                                 }
2797                         | PRIMARY KEY ExistingIndex ConstraintAttributeSpec
2798                                 {
2799                                         Constraint *n = makeNode(Constraint);
2800                                         n->contype = CONSTR_PRIMARY;
2801                                         n->location = @1;
2802                                         n->keys = NIL;
2803                                         n->options = NIL;
2804                                         n->indexname = $3;
2805                                         n->indexspace = NULL;
2806                                         n->deferrable = ($4 & 1) != 0;
2807                                         n->initdeferred = ($4 & 2) != 0;
2808                                         $$ = (Node *)n;
2809                                 }
2810                         | EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
2811                                 opt_definition OptConsTableSpace ExclusionWhereClause
2812                                 ConstraintAttributeSpec
2813                                 {
2814                                         Constraint *n = makeNode(Constraint);
2815                                         n->contype = CONSTR_EXCLUSION;
2816                                         n->location = @1;
2817                                         n->access_method        = $2;
2818                                         n->exclusions           = $4;
2819                                         n->options                      = $6;
2820                                         n->indexname            = NULL;
2821                                         n->indexspace           = $7;
2822                                         n->where_clause         = $8;
2823                                         n->deferrable           = ($9 & 1) != 0;
2824                                         n->initdeferred         = ($9 & 2) != 0;
2825                                         $$ = (Node *)n;
2826                                 }
2827                         | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2828                                 opt_column_list key_match key_actions ConstraintAttributeSpec
2829                                 {
2830                                         Constraint *n = makeNode(Constraint);
2831                                         n->contype = CONSTR_FOREIGN;
2832                                         n->location = @1;
2833                                         n->pktable                      = $7;
2834                                         n->fk_attrs                     = $4;
2835                                         n->pk_attrs                     = $8;
2836                                         n->fk_matchtype         = $9;
2837                                         n->fk_upd_action        = (char) ($10 >> 8);
2838                                         n->fk_del_action        = (char) ($10 & 0xFF);
2839                                         n->deferrable           = ($11 & 1) != 0;
2840                                         n->initdeferred         = ($11 & 2) != 0;
2841                                         n->skip_validation  = false;
2842                                         n->initially_valid  = true;
2843                                         $$ = (Node *)n;
2844                                 }
2845                         | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2846                                 opt_column_list key_match key_actions
2847                                 NOT VALID
2848                                 {
2849                                         Constraint *n = makeNode(Constraint);
2850                                         n->contype = CONSTR_FOREIGN;
2851                                         n->location = @1;
2852                                         n->pktable                      = $7;
2853                                         n->fk_attrs                     = $4;
2854                                         n->pk_attrs                     = $8;
2855                                         n->fk_matchtype         = $9;
2856                                         n->fk_upd_action        = (char) ($10 >> 8);
2857                                         n->fk_del_action        = (char) ($10 & 0xFF);
2858                                         n->skip_validation  = true;
2859                                         n->initially_valid  = false;
2860                                         $$ = (Node *)n;
2861                                 }
2862                 ;
2863
2864 opt_column_list:
2865                         '(' columnList ')'                                              { $$ = $2; }
2866                         | /*EMPTY*/                                                             { $$ = NIL; }
2867                 ;
2868
2869 columnList:
2870                         columnElem                                                              { $$ = list_make1($1); }
2871                         | columnList ',' columnElem                             { $$ = lappend($1, $3); }
2872                 ;
2873
2874 columnElem: ColId
2875                                 {
2876                                         $$ = (Node *) makeString($1);
2877                                 }
2878                 ;
2879
2880 key_match:  MATCH FULL
2881                         {
2882                                 $$ = FKCONSTR_MATCH_FULL;
2883                         }
2884                 | MATCH PARTIAL
2885                         {
2886                                 ereport(ERROR,
2887                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2888                                                  errmsg("MATCH PARTIAL not yet implemented"),
2889                                                  parser_errposition(@1)));
2890                                 $$ = FKCONSTR_MATCH_PARTIAL;
2891                         }
2892                 | MATCH SIMPLE
2893                         {
2894                                 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2895                         }
2896                 | /*EMPTY*/
2897                         {
2898                                 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2899                         }
2900                 ;
2901
2902 ExclusionConstraintList:
2903                         ExclusionConstraintElem                                 { $$ = list_make1($1); }
2904                         | ExclusionConstraintList ',' ExclusionConstraintElem
2905                                                                                                         { $$ = lappend($1, $3); }
2906                 ;
2907
2908 ExclusionConstraintElem: index_elem WITH any_operator
2909                         {
2910                                 $$ = list_make2($1, $3);
2911                         }
2912                         /* allow OPERATOR() decoration for the benefit of ruleutils.c */
2913                         | index_elem WITH OPERATOR '(' any_operator ')'
2914                         {
2915                                 $$ = list_make2($1, $5);
2916                         }
2917                 ;
2918
2919 ExclusionWhereClause:
2920                         WHERE '(' a_expr ')'                                    { $$ = $3; }
2921                         | /*EMPTY*/                                                             { $$ = NULL; }
2922                 ;
2923
2924 /*
2925  * We combine the update and delete actions into one value temporarily
2926  * for simplicity of parsing, and then break them down again in the
2927  * calling production.  update is in the left 8 bits, delete in the right.
2928  * Note that NOACTION is the default.
2929  */
2930 key_actions:
2931                         key_update
2932                                 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2933                         | key_delete
2934                                 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2935                         | key_update key_delete
2936                                 { $$ = ($1 << 8) | ($2 & 0xFF); }
2937                         | key_delete key_update
2938                                 { $$ = ($2 << 8) | ($1 & 0xFF); }
2939                         | /*EMPTY*/
2940                                 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2941                 ;
2942
2943 key_update: ON UPDATE key_action                { $$ = $3; }
2944                 ;
2945
2946 key_delete: ON DELETE_P key_action              { $$ = $3; }
2947                 ;
2948
2949 key_action:
2950                         NO ACTION                                       { $$ = FKCONSTR_ACTION_NOACTION; }
2951                         | RESTRICT                                      { $$ = FKCONSTR_ACTION_RESTRICT; }
2952                         | CASCADE                                       { $$ = FKCONSTR_ACTION_CASCADE; }
2953                         | SET NULL_P                            { $$ = FKCONSTR_ACTION_SETNULL; }
2954                         | SET DEFAULT                           { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2955                 ;
2956
2957 OptInherit: INHERITS '(' qualified_name_list ')'        { $$ = $3; }
2958                         | /*EMPTY*/                                                             { $$ = NIL; }
2959                 ;
2960
2961 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2962 OptWith:
2963                         WITH reloptions                         { $$ = $2; }
2964                         | WITH OIDS                                     { $$ = list_make1(defWithOids(true)); }
2965                         | WITHOUT OIDS                          { $$ = list_make1(defWithOids(false)); }
2966                         | /*EMPTY*/                                     { $$ = NIL; }
2967                 ;
2968
2969 OnCommitOption:  ON COMMIT DROP                         { $$ = ONCOMMIT_DROP; }
2970                         | ON COMMIT DELETE_P ROWS               { $$ = ONCOMMIT_DELETE_ROWS; }
2971                         | ON COMMIT PRESERVE ROWS               { $$ = ONCOMMIT_PRESERVE_ROWS; }
2972                         | /*EMPTY*/                                             { $$ = ONCOMMIT_NOOP; }
2973                 ;
2974
2975 OptTableSpace:   TABLESPACE name                                        { $$ = $2; }
2976                         | /*EMPTY*/                                                             { $$ = NULL; }
2977                 ;
2978
2979 OptConsTableSpace:   USING INDEX TABLESPACE name        { $$ = $4; }
2980                         | /*EMPTY*/                                                             { $$ = NULL; }
2981                 ;
2982
2983 ExistingIndex:   USING INDEX index_name                         { $$ = $3; }
2984                 ;
2985
2986
2987 /*
2988  * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2989  * SELECT ... INTO.
2990  */
2991
2992 CreateAsStmt:
2993                 CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
2994                                 {
2995                                         /*
2996                                          * When the SelectStmt is a set-operation tree, we must
2997                                          * stuff the INTO information into the leftmost component
2998                                          * Select, because that's where analyze.c will expect
2999                                          * to find it.  Similarly, the output column names must
3000                                          * be attached to that Select's target list.
3001                                          */
3002                                         SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
3003                                         if (n->intoClause != NULL)
3004                                                 ereport(ERROR,
3005                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
3006                                                                  errmsg("CREATE TABLE AS cannot specify INTO"),
3007                                                                  parser_errposition(exprLocation((Node *) n->intoClause))));
3008                                         $4->rel->relpersistence = $2;
3009                                         n->intoClause = $4;
3010                                         /* Implement WITH NO DATA by forcing top-level LIMIT 0 */
3011                                         if (!$7)
3012                                                 ((SelectStmt *) $6)->limitCount = makeIntConst(0, -1);
3013                                         $$ = $6;
3014                                 }
3015                 ;
3016
3017 create_as_target:
3018                         qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
3019                                 {
3020                                         $$ = makeNode(IntoClause);
3021                                         $$->rel = $1;
3022                                         $$->colNames = $2;
3023                                         $$->options = $3;
3024                                         $$->onCommit = $4;
3025                                         $$->tableSpaceName = $5;
3026                                 }
3027                 ;
3028
3029 OptCreateAs:
3030                         '(' CreateAsList ')'                                    { $$ = $2; }
3031                         | /*EMPTY*/                                                             { $$ = NIL; }
3032                 ;
3033
3034 CreateAsList:
3035                         CreateAsElement                                                 { $$ = list_make1($1); }
3036                         | CreateAsList ',' CreateAsElement              { $$ = lappend($1, $3); }
3037                 ;
3038
3039 CreateAsElement:
3040                         ColId
3041                                 {
3042                                         ColumnDef *n = makeNode(ColumnDef);
3043                                         n->colname = $1;
3044                                         n->typeName = NULL;
3045                                         n->inhcount = 0;
3046                                         n->is_local = true;
3047                                         n->is_not_null = false;
3048                                         n->is_from_type = false;
3049                                         n->storage = 0;
3050                                         n->raw_default = NULL;
3051                                         n->cooked_default = NULL;
3052                                         n->collClause = NULL;
3053                                         n->collOid = InvalidOid;
3054                                         n->constraints = NIL;
3055                                         $$ = (Node *)n;
3056                                 }
3057                 ;
3058
3059 opt_with_data:
3060                         WITH DATA_P                                                             { $$ = TRUE; }
3061                         | WITH NO DATA_P                                                { $$ = FALSE; }
3062                         | /*EMPTY*/                                                             { $$ = TRUE; }
3063                 ;
3064
3065
3066 /*****************************************************************************
3067  *
3068  *              QUERY :
3069  *                              CREATE SEQUENCE seqname
3070  *                              ALTER SEQUENCE seqname
3071  *
3072  *****************************************************************************/
3073
3074 CreateSeqStmt:
3075                         CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
3076                                 {
3077                                         CreateSeqStmt *n = makeNode(CreateSeqStmt);
3078                                         $4->relpersistence = $2;
3079                                         n->sequence = $4;
3080                                         n->options = $5;
3081                                         n->ownerId = InvalidOid;
3082                                         $$ = (Node *)n;
3083                                 }
3084                 ;
3085
3086 AlterSeqStmt:
3087                         ALTER SEQUENCE qualified_name SeqOptList
3088                                 {
3089                                         AlterSeqStmt *n = makeNode(AlterSeqStmt);
3090                                         n->sequence = $3;
3091                                         n->options = $4;
3092                                         $$ = (Node *)n;
3093                                 }
3094                 ;
3095
3096 OptSeqOptList: SeqOptList                                                       { $$ = $1; }
3097                         | /*EMPTY*/                                                             { $$ = NIL; }
3098                 ;
3099
3100 SeqOptList: SeqOptElem                                                          { $$ = list_make1($1); }
3101                         | SeqOptList SeqOptElem                                 { $$ = lappend($1, $2); }
3102                 ;
3103
3104 SeqOptElem: CACHE NumericOnly
3105                                 {
3106                                         $$ = makeDefElem("cache", (Node *)$2);
3107                                 }
3108                         | CYCLE
3109                                 {
3110                                         $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
3111                                 }
3112                         | NO CYCLE
3113                                 {
3114                                         $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
3115                                 }
3116                         | INCREMENT opt_by NumericOnly
3117                                 {
3118                                         $$ = makeDefElem("increment", (Node *)$3);
3119                                 }
3120                         | MAXVALUE NumericOnly
3121                                 {
3122                                         $$ = makeDefElem("maxvalue", (Node *)$2);
3123                                 }
3124                         | MINVALUE NumericOnly
3125                                 {
3126                                         $$ = makeDefElem("minvalue", (Node *)$2);
3127                                 }
3128                         | NO MAXVALUE
3129                                 {
3130                                         $$ = makeDefElem("maxvalue", NULL);
3131                                 }
3132                         | NO MINVALUE
3133                                 {
3134                                         $$ = makeDefElem("minvalue", NULL);
3135                                 }
3136                         | OWNED BY any_name
3137                                 {
3138                                         $$ = makeDefElem("owned_by", (Node *)$3);
3139                                 }
3140                         | START opt_with NumericOnly
3141                                 {
3142                                         $$ = makeDefElem("start", (Node *)$3);
3143                                 }
3144                         | RESTART
3145                                 {
3146                                         $$ = makeDefElem("restart", NULL);
3147                                 }
3148                         | RESTART opt_with NumericOnly
3149                                 {
3150                                         $$ = makeDefElem("restart", (Node *)$3);
3151                                 }
3152                 ;
3153
3154 opt_by:         BY                              {}
3155                         | /* empty */   {}
3156           ;
3157
3158 NumericOnly:
3159                         FCONST                                                          { $$ = makeFloat($1); }
3160                         | '-' FCONST
3161                                 {
3162                                         $$ = makeFloat($2);
3163                                         doNegateFloat($$);
3164                                 }
3165                         | SignedIconst                                          { $$ = makeInteger($1); }
3166                 ;
3167
3168 NumericOnly_list:       NumericOnly                                             { $$ = list_make1($1); }
3169                                 | NumericOnly_list ',' NumericOnly      { $$ = lappend($1, $3); }
3170                 ;
3171
3172 /*****************************************************************************
3173  *
3174  *              QUERIES :
3175  *                              CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
3176  *                              DROP [PROCEDURAL] LANGUAGE ...
3177  *
3178  *****************************************************************************/
3179
3180 CreatePLangStmt:
3181                         CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
3182                         {
3183                                 CreatePLangStmt *n = makeNode(CreatePLangStmt);
3184                                 n->replace = $2;
3185                                 n->plname = $6;
3186                                 /* parameters are all to be supplied by system */
3187                                 n->plhandler = NIL;
3188                                 n->plinline = NIL;
3189                                 n->plvalidator = NIL;
3190                                 n->pltrusted = false;
3191                                 $$ = (Node *)n;
3192                         }
3193                         | CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
3194                           HANDLER handler_name opt_inline_handler opt_validator
3195                         {
3196                                 CreatePLangStmt *n = makeNode(CreatePLangStmt);
3197                                 n->replace = $2;
3198                                 n->plname = $6;
3199                                 n->plhandler = $8;
3200                                 n->plinline = $9;
3201                                 n->plvalidator = $10;
3202                                 n->pltrusted = $3;
3203                                 $$ = (Node *)n;
3204                         }
3205                 ;
3206
3207 opt_trusted:
3208                         TRUSTED                                                                 { $$ = TRUE; }
3209                         | /*EMPTY*/                                                             { $$ = FALSE; }
3210                 ;
3211
3212 /* This ought to be just func_name, but that causes reduce/reduce conflicts
3213  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
3214  * Work around by using simple names, instead.
3215  */
3216 handler_name:
3217                         name                                            { $$ = list_make1(makeString($1)); }
3218                         | name attrs                            { $$ = lcons(makeString($1), $2); }
3219                 ;
3220
3221 opt_inline_handler:
3222                         INLINE_P handler_name                                   { $$ = $2; }
3223                         | /*EMPTY*/                                                             { $$ = NIL; }
3224                 ;
3225
3226 validator_clause:
3227                         VALIDATOR handler_name                                  { $$ = $2; }
3228                         | NO VALIDATOR                                                  { $$ = NIL; }
3229                 ;
3230
3231 opt_validator:
3232                         validator_clause                                                { $$ = $1; }
3233                         | /*EMPTY*/                                                             { $$ = NIL; }
3234                 ;
3235
3236 DropPLangStmt:
3237                         DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
3238                                 {
3239                                         DropPLangStmt *n = makeNode(DropPLangStmt);
3240                                         n->plname = $4;
3241                                         n->behavior = $5;
3242                                         n->missing_ok = false;
3243                                         $$ = (Node *)n;
3244                                 }
3245                         | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
3246                                 {
3247                                         DropPLangStmt *n = makeNode(DropPLangStmt);
3248                                         n->plname = $6;
3249                                         n->behavior = $7;
3250                                         n->missing_ok = true;
3251                                         $$ = (Node *)n;
3252                                 }
3253                 ;
3254
3255 opt_procedural:
3256                         PROCEDURAL                                                              {}
3257                         | /*EMPTY*/                                                             {}
3258                 ;
3259
3260 /*****************************************************************************
3261  *
3262  *              QUERY:
3263  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
3264  *
3265  *****************************************************************************/
3266
3267 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
3268                                 {
3269                                         CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
3270                                         n->tablespacename = $3;
3271                                         n->owner = $4;
3272                                         n->location = $6;
3273                                         $$ = (Node *) n;
3274                                 }
3275                 ;
3276
3277 OptTableSpaceOwner: OWNER name                  { $$ = $2; }
3278                         | /*EMPTY */                            { $$ = NULL; }
3279                 ;
3280
3281 /*****************************************************************************
3282  *
3283  *              QUERY :
3284  *                              DROP TABLESPACE <tablespace>
3285  *
3286  *              No need for drop behaviour as we cannot implement dependencies for
3287  *              objects in other databases; we can only support RESTRICT.
3288  *
3289  ****************************************************************************/
3290
3291 DropTableSpaceStmt: DROP TABLESPACE name
3292                                 {
3293                                         DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
3294                                         n->tablespacename = $3;
3295                                         n->missing_ok = false;
3296                                         $$ = (Node *) n;
3297                                 }
3298                                 |  DROP TABLESPACE IF_P EXISTS name
3299                 {
3300                                         DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
3301                                         n->tablespacename = $5;
3302                                         n->missing_ok = true;
3303                                         $$ = (Node *) n;
3304                                 }
3305                 ;
3306
3307 /*****************************************************************************
3308  *
3309  *              QUERY:
3310  *             CREATE EXTENSION extension
3311  *             [ WITH ] [ SCHEMA schema ] [ VERSION version ] [ FROM oldversion ]
3312  *
3313  *****************************************************************************/
3314
3315 CreateExtensionStmt: CREATE EXTENSION name opt_with create_extension_opt_list
3316                                 {
3317                                         CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
3318                                         n->extname = $3;
3319                                         n->if_not_exists = false;
3320                                         n->options = $5;
3321                                         $$ = (Node *) n;
3322                                 }
3323                                 | CREATE EXTENSION IF_P NOT EXISTS name opt_with create_extension_opt_list
3324                                 {
3325                                         CreateExtensionStmt *n = makeNode(CreateExtensionStmt);
3326                                         n->extname = $6;
3327                                         n->if_not_exists = true;
3328                                         n->options = $8;
3329                                         $$ = (Node *) n;
3330                                 }
3331                 ;
3332
3333 create_extension_opt_list:
3334                         create_extension_opt_list create_extension_opt_item
3335                                 { $$ = lappend($1, $2); }
3336                         | /* EMPTY */
3337                                 { $$ = NIL; }
3338                 ;
3339
3340 create_extension_opt_item:
3341                         SCHEMA name
3342                                 {
3343                                         $$ = makeDefElem("schema", (Node *)makeString($2));
3344                                 }
3345                         | VERSION_P ColId_or_Sconst
3346                                 {
3347                                         $$ = makeDefElem("new_version", (Node *)makeString($2));
3348                                 }
3349                         | FROM ColId_or_Sconst
3350                                 {
3351                                         $$ = makeDefElem("old_version", (Node *)makeString($2));
3352                                 }
3353                 ;
3354
3355 /*****************************************************************************
3356  *
3357  * ALTER EXTENSION name UPDATE [ TO version ]
3358  *
3359  *****************************************************************************/
3360
3361 AlterExtensionStmt: ALTER EXTENSION name UPDATE alter_extension_opt_list
3362                                 {
3363                                         AlterExtensionStmt *n = makeNode(AlterExtensionStmt);
3364                                         n->extname = $3;
3365                                         n->options = $5;
3366                                         $$ = (Node *) n;
3367                                 }
3368                 ;
3369
3370 alter_extension_opt_list:
3371                         alter_extension_opt_list alter_extension_opt_item
3372                                 { $$ = lappend($1, $2); }
3373                         | /* EMPTY */
3374                                 { $$ = NIL; }
3375                 ;
3376
3377 alter_extension_opt_item:
3378                         TO ColId_or_Sconst
3379                                 {
3380                                         $$ = makeDefElem("new_version", (Node *)makeString($2));
3381                                 }
3382                 ;
3383
3384 /*****************************************************************************
3385  *
3386  * ALTER EXTENSION name ADD/DROP object-identifier
3387  *
3388  *****************************************************************************/
3389
3390 AlterExtensionContentsStmt:
3391                         ALTER EXTENSION name add_drop AGGREGATE func_name aggr_args
3392                                 {
3393                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3394                                         n->extname = $3;
3395                                         n->action = $4;
3396                                         n->objtype = OBJECT_AGGREGATE;
3397                                         n->objname = $6;
3398                                         n->objargs = $7;
3399                                         $$ = (Node *)n;
3400                                 }
3401                         | ALTER EXTENSION name add_drop CAST '(' Typename AS Typename ')'
3402                                 {
3403                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3404                                         n->extname = $3;
3405                                         n->action = $4;
3406                                         n->objtype = OBJECT_CAST;
3407                                         n->objname = list_make1($7);
3408                                         n->objargs = list_make1($9);
3409                                         $$ = (Node *) n;
3410                                 }
3411                         | ALTER EXTENSION name add_drop COLLATION any_name
3412                                 {
3413                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3414                                         n->extname = $3;
3415                                         n->action = $4;
3416                                         n->objtype = OBJECT_COLLATION;
3417                                         n->objname = $6;
3418                                         $$ = (Node *)n;
3419                                 }
3420                         | ALTER EXTENSION name add_drop CONVERSION_P any_name
3421                                 {
3422                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3423                                         n->extname = $3;
3424                                         n->action = $4;
3425                                         n->objtype = OBJECT_CONVERSION;
3426                                         n->objname = $6;
3427                                         $$ = (Node *)n;
3428                                 }
3429                         | ALTER EXTENSION name add_drop DOMAIN_P any_name
3430                                 {
3431                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3432                                         n->extname = $3;
3433                                         n->action = $4;
3434                                         n->objtype = OBJECT_DOMAIN;
3435                                         n->objname = $6;
3436                                         $$ = (Node *)n;
3437                                 }
3438                         | ALTER EXTENSION name add_drop FUNCTION function_with_argtypes
3439                                 {
3440                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3441                                         n->extname = $3;
3442                                         n->action = $4;
3443                                         n->objtype = OBJECT_FUNCTION;
3444                                         n->objname = $6->funcname;
3445                                         n->objargs = $6->funcargs;
3446                                         $$ = (Node *)n;
3447                                 }
3448                         | ALTER EXTENSION name add_drop opt_procedural LANGUAGE name
3449                                 {
3450                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3451                                         n->extname = $3;
3452                                         n->action = $4;
3453                                         n->objtype = OBJECT_LANGUAGE;
3454                                         n->objname = list_make1(makeString($7));
3455                                         $$ = (Node *)n;
3456                                 }
3457                         | ALTER EXTENSION name add_drop OPERATOR any_operator oper_argtypes
3458                                 {
3459                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3460                                         n->extname = $3;
3461                                         n->action = $4;
3462                                         n->objtype = OBJECT_OPERATOR;
3463                                         n->objname = $6;
3464                                         n->objargs = $7;
3465                                         $$ = (Node *)n;
3466                                 }
3467                         | ALTER EXTENSION name add_drop OPERATOR CLASS any_name USING access_method
3468                                 {
3469                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3470                                         n->extname = $3;
3471                                         n->action = $4;
3472                                         n->objtype = OBJECT_OPCLASS;
3473                                         n->objname = $7;
3474                                         n->objargs = list_make1(makeString($9));
3475                                         $$ = (Node *)n;
3476                                 }
3477                         | ALTER EXTENSION name add_drop OPERATOR FAMILY any_name USING access_method
3478                                 {
3479                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3480                                         n->extname = $3;
3481                                         n->action = $4;
3482                                         n->objtype = OBJECT_OPFAMILY;
3483                                         n->objname = $7;
3484                                         n->objargs = list_make1(makeString($9));
3485                                         $$ = (Node *)n;
3486                                 }
3487                         | ALTER EXTENSION name add_drop SCHEMA name
3488                                 {
3489                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3490                                         n->extname = $3;
3491                                         n->action = $4;
3492                                         n->objtype = OBJECT_SCHEMA;
3493                                         n->objname = list_make1(makeString($6));
3494                                         $$ = (Node *)n;
3495                                 }
3496                         | ALTER EXTENSION name add_drop TABLE any_name
3497                                 {
3498                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3499                                         n->extname = $3;
3500                                         n->action = $4;
3501                                         n->objtype = OBJECT_TABLE;
3502                                         n->objname = $6;
3503                                         $$ = (Node *)n;
3504                                 }
3505                         | ALTER EXTENSION name add_drop TEXT_P SEARCH PARSER any_name
3506                                 {
3507                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3508                                         n->extname = $3;
3509                                         n->action = $4;
3510                                         n->objtype = OBJECT_TSPARSER;
3511                                         n->objname = $8;
3512                                         $$ = (Node *)n;
3513                                 }
3514                         | ALTER EXTENSION name add_drop TEXT_P SEARCH DICTIONARY any_name
3515                                 {
3516                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3517                                         n->extname = $3;
3518                                         n->action = $4;
3519                                         n->objtype = OBJECT_TSDICTIONARY;
3520                                         n->objname = $8;
3521                                         $$ = (Node *)n;
3522                                 }
3523                         | ALTER EXTENSION name add_drop TEXT_P SEARCH TEMPLATE any_name
3524                                 {
3525                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3526                                         n->extname = $3;
3527                                         n->action = $4;
3528                                         n->objtype = OBJECT_TSTEMPLATE;
3529                                         n->objname = $8;
3530                                         $$ = (Node *)n;
3531                                 }
3532                         | ALTER EXTENSION name add_drop TEXT_P SEARCH CONFIGURATION any_name
3533                                 {
3534                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3535                                         n->extname = $3;
3536                                         n->action = $4;
3537                                         n->objtype = OBJECT_TSCONFIGURATION;
3538                                         n->objname = $8;
3539                                         $$ = (Node *)n;
3540                                 }
3541                         | ALTER EXTENSION name add_drop SEQUENCE any_name
3542                                 {
3543                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3544                                         n->extname = $3;
3545                                         n->action = $4;
3546                                         n->objtype = OBJECT_SEQUENCE;
3547                                         n->objname = $6;
3548                                         $$ = (Node *)n;
3549                                 }
3550                         | ALTER EXTENSION name add_drop VIEW any_name
3551                                 {
3552                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3553                                         n->extname = $3;
3554                                         n->action = $4;
3555                                         n->objtype = OBJECT_VIEW;
3556                                         n->objname = $6;
3557                                         $$ = (Node *)n;
3558                                 }
3559                         | ALTER EXTENSION name add_drop FOREIGN TABLE any_name
3560                                 {
3561                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3562                                         n->extname = $3;
3563                                         n->action = $4;
3564                                         n->objtype = OBJECT_FOREIGN_TABLE;
3565                                         n->objname = $7;
3566                                         $$ = (Node *)n;
3567                                 }
3568                         | ALTER EXTENSION name add_drop FOREIGN DATA_P WRAPPER name
3569                                 {
3570                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3571                                         n->extname = $3;
3572                                         n->action = $4;
3573                                         n->objtype = OBJECT_FDW;
3574                                         n->objname = list_make1(makeString($8));
3575                                         $$ = (Node *)n;
3576                                 }
3577                         | ALTER EXTENSION name add_drop SERVER name
3578                                 {
3579                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3580                                         n->extname = $3;
3581                                         n->action = $4;
3582                                         n->objtype = OBJECT_FOREIGN_SERVER;
3583                                         n->objname = list_make1(makeString($6));
3584                                         $$ = (Node *)n;
3585                                 }
3586                         | ALTER EXTENSION name add_drop TYPE_P any_name
3587                                 {
3588                                         AlterExtensionContentsStmt *n = makeNode(AlterExtensionContentsStmt);
3589                                         n->extname = $3;
3590                                         n->action = $4;
3591                                         n->objtype = OBJECT_TYPE;
3592                                         n->objname = $6;
3593                                         $$ = (Node *)n;
3594                                 }
3595                 ;
3596
3597 /*****************************************************************************
3598  *
3599  *              QUERY:
3600  *             CREATE FOREIGN DATA WRAPPER name options
3601  *
3602  *****************************************************************************/
3603
3604 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_fdw_options create_generic_options
3605                                 {
3606                                         CreateFdwStmt *n = makeNode(CreateFdwStmt);
3607                                         n->fdwname = $5;
3608                                         n->func_options = $6;
3609                                         n->options = $7;
3610                                         $$ = (Node *) n;
3611                                 }
3612                 ;
3613
3614 fdw_option:
3615                         HANDLER handler_name                            { $$ = makeDefElem("handler", (Node *)$2); }
3616                         | NO HANDLER                                            { $$ = makeDefElem("handler", NULL); }
3617                         | VALIDATOR handler_name                        { $$ = makeDefElem("validator", (Node *)$2); }
3618                         | NO VALIDATOR                                          { $$ = makeDefElem("validator", NULL); }
3619                 ;
3620
3621 fdw_options:
3622                         fdw_option                                                      { $$ = list_make1($1); }
3623                         | fdw_options fdw_option                        { $$ = lappend($1, $2); }
3624                 ;
3625
3626 opt_fdw_options:
3627                         fdw_options                                                     { $$ = $1; }
3628                         | /*EMPTY*/                                                     { $$ = NIL; }
3629                 ;
3630
3631 /*****************************************************************************
3632  *
3633  *              QUERY :
3634  *                              DROP FOREIGN DATA WRAPPER name
3635  *
3636  ****************************************************************************/
3637
3638 DropFdwStmt: DROP FOREIGN DATA_P WRAPPER name opt_drop_behavior
3639                                 {
3640                                         DropFdwStmt *n = makeNode(DropFdwStmt);
3641                                         n->fdwname = $5;
3642                                         n->missing_ok = false;
3643                                         n->behavior = $6;
3644                                         $$ = (Node *) n;
3645                                 }
3646                                 |  DROP FOREIGN DATA_P WRAPPER IF_P EXISTS name opt_drop_behavior
3647                 {
3648                                         DropFdwStmt *n = makeNode(DropFdwStmt);
3649                                         n->fdwname = $7;
3650                                         n->missing_ok = true;
3651                                         n->behavior = $8;
3652                                         $$ = (Node *) n;
3653                                 }
3654                 ;
3655
3656 /*****************************************************************************
3657  *
3658  *              QUERY :
3659  *                              ALTER FOREIGN DATA WRAPPER name options
3660  *
3661  ****************************************************************************/
3662
3663 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name opt_fdw_options alter_generic_options
3664                                 {
3665                                         AlterFdwStmt *n = makeNode(AlterFdwStmt);
3666                                         n->fdwname = $5;
3667                                         n->func_options = $6;
3668                                         n->options = $7;
3669                                         $$ = (Node *) n;
3670                                 }
3671                         | ALTER FOREIGN DATA_P WRAPPER name fdw_options
3672                                 {
3673                                         AlterFdwStmt *n = makeNode(AlterFdwStmt);
3674                                         n->fdwname = $5;
3675                                         n->func_options = $6;
3676                                         n->options = NIL;
3677                                         $$ = (Node *) n;
3678                                 }
3679                 ;
3680
3681 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
3682 create_generic_options:
3683                         OPTIONS '(' generic_option_list ')'                     { $$ = $3; }
3684                         | /*EMPTY*/                                                                     { $$ = NIL; }
3685                 ;
3686
3687 generic_option_list:
3688                         generic_option_elem
3689                                 {
3690                                         $$ = list_make1($1);
3691                                 }
3692                         | generic_option_list ',' generic_option_elem
3693                                 {
3694                                         $$ = lappend($1, $3);
3695                                 }
3696                 ;
3697
3698 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
3699 alter_generic_options:
3700                         OPTIONS '(' alter_generic_option_list ')'               { $$ = $3; }
3701                 ;
3702
3703 alter_generic_option_list:
3704                         alter_generic_option_elem
3705                                 {
3706                                         $$ = list_make1($1);
3707                                 }
3708                         | alter_generic_option_list ',' alter_generic_option_elem
3709                                 {
3710                                         $$ = lappend($1, $3);
3711                                 }
3712                 ;
3713
3714 alter_generic_option_elem:
3715                         generic_option_elem
3716                                 {
3717                                         $$ = $1;
3718                                 }
3719                         | SET generic_option_elem
3720                                 {
3721                                         $$ = $2;
3722                                         $$->defaction = DEFELEM_SET;
3723                                 }
3724                         | ADD_P generic_option_elem
3725                                 {
3726                                         $$ = $2;
3727                                         $$->defaction = DEFELEM_ADD;
3728                                 }
3729                         | DROP generic_option_name
3730                                 {
3731                                         $$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP);
3732                                 }
3733                 ;
3734
3735 generic_option_elem:
3736                         generic_option_name generic_option_arg
3737                                 {
3738                                         $$ = makeDefElem($1, $2);
3739                                 }
3740                 ;
3741
3742 generic_option_name:
3743                                 ColLabel                        { $$ = $1; }
3744                 ;
3745
3746 /* We could use def_arg here, but the spec only requires string literals */
3747 generic_option_arg:
3748                                 Sconst                          { $$ = (Node *) makeString($1); }
3749                 ;
3750
3751 /*****************************************************************************
3752  *
3753  *              QUERY:
3754  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
3755  *
3756  *****************************************************************************/
3757
3758 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
3759                                                  FOREIGN DATA_P WRAPPER name create_generic_options
3760                                 {
3761                                         CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
3762                                         n->servername = $3;
3763                                         n->servertype = $4;
3764                                         n->version = $5;
3765                                         n->fdwname = $9;
3766                                         n->options = $10;
3767                                         $$ = (Node *) n;
3768                                 }
3769                 ;
3770
3771 opt_type:
3772                         TYPE_P Sconst                   { $$ = $2; }
3773                         | /*EMPTY*/                             { $$ = NULL; }
3774                 ;
3775
3776
3777 foreign_server_version:
3778                         VERSION_P Sconst                { $$ = $2; }
3779                 |       VERSION_P NULL_P                { $$ = NULL; }
3780                 ;
3781
3782 opt_foreign_server_version:
3783                         foreign_server_version  { $$ = $1; }
3784                         | /*EMPTY*/                             { $$ = NULL; }
3785                 ;
3786
3787 /*****************************************************************************
3788  *
3789  *              QUERY :
3790  *                              DROP SERVER name
3791  *
3792  ****************************************************************************/
3793
3794 DropForeignServerStmt: DROP SERVER name opt_drop_behavior
3795                                 {
3796                                         DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
3797                                         n->servername = $3;
3798                                         n->missing_ok = false;
3799                                         n->behavior = $4;
3800                                         $$ = (Node *) n;
3801                                 }
3802                                 |  DROP SERVER IF_P EXISTS name opt_drop_behavior
3803                 {
3804                                         DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
3805                                         n->servername = $5;
3806                                         n->missing_ok = true;
3807                                         n->behavior = $6;
3808                                         $$ = (Node *) n;
3809                                 }
3810                 ;
3811
3812 /*****************************************************************************
3813  *
3814  *              QUERY :
3815  *                              ALTER SERVER name [VERSION] [OPTIONS]
3816  *
3817  ****************************************************************************/
3818
3819 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
3820                                 {
3821                                         AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3822                                         n->servername = $3;
3823                                         n->version = $4;
3824                                         n->options = $5;
3825                                         n->has_version = true;
3826                                         $$ = (Node *) n;
3827                                 }
3828                         | ALTER SERVER name foreign_server_version
3829                                 {
3830                                         AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3831                                         n->servername = $3;
3832                                         n->version = $4;
3833                                         n->has_version = true;
3834                                         $$ = (Node *) n;
3835                                 }
3836                         | ALTER SERVER name alter_generic_options
3837                                 {
3838                                         AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3839                                         n->servername = $3;
3840                                         n->options = $4;
3841                                         $$ = (Node *) n;
3842                                 }
3843                 ;
3844
3845 /*****************************************************************************
3846  *
3847  *              QUERY:
3848  *             CREATE FOREIGN TABLE relname (...) SERVER name (...)
3849  *
3850  *****************************************************************************/
3851
3852 CreateForeignTableStmt:
3853                 CREATE FOREIGN TABLE qualified_name
3854                         OptForeignTableElementList
3855                         SERVER name create_generic_options
3856                                 {
3857                                         CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
3858                                         $4->relpersistence = RELPERSISTENCE_PERMANENT;
3859                                         n->base.relation = $4;
3860                                         n->base.tableElts = $5;
3861                                         n->base.inhRelations = NIL;
3862                                         n->base.if_not_exists = false;
3863                                         /* FDW-specific data */
3864                                         n->servername = $7;
3865                                         n->options = $8;
3866                                         $$ = (Node *) n;
3867                                 }
3868                 | CREATE FOREIGN TABLE IF_P NOT EXISTS qualified_name
3869                         OptForeignTableElementList
3870                         SERVER name create_generic_options
3871                                 {
3872                                         CreateForeignTableStmt *n = makeNode(CreateForeignTableStmt);
3873                                         $7->relpersistence = RELPERSISTENCE_PERMANENT;
3874                                         n->base.relation = $7;
3875                                         n->base.tableElts = $8;
3876                                         n->base.inhRelations = NIL;
3877                                         n->base.if_not_exists = true;
3878                                         /* FDW-specific data */
3879                                         n->servername = $10;
3880                                         n->options = $11;
3881                                         $$ = (Node *) n;
3882                                 }
3883                 ;
3884
3885 OptForeignTableElementList:
3886                         '(' ForeignTableElementList ')'                 { $$ = $2; }
3887                         | '(' ')'                                                               { $$ = NIL; }
3888                 ;
3889
3890 ForeignTableElementList:
3891                         ForeignTableElement
3892                                 {
3893                                         $$ = list_make1($1);
3894                                 }
3895                         | ForeignTableElementList ',' ForeignTableElement
3896                                 {
3897                                         $$ = lappend($1, $3);
3898                                 }
3899                 ;
3900
3901 ForeignTableElement:
3902                         columnDef                                       { $$ = $1; }
3903                 ;
3904
3905 /*****************************************************************************
3906  *
3907  *              QUERY:
3908  *             ALTER FOREIGN TABLE relname [...]
3909  *
3910  *****************************************************************************/
3911
3912 AlterForeignTableStmt:
3913                         ALTER FOREIGN TABLE relation_expr alter_table_cmds
3914                                 {
3915                                         AlterTableStmt *n = makeNode(AlterTableStmt);
3916                                         n->relation = $4;
3917                                         n->cmds = $5;
3918                                         n->relkind = OBJECT_FOREIGN_TABLE;
3919                                         $$ = (Node *)n;
3920                                 }
3921                 ;
3922
3923 /*****************************************************************************
3924  *
3925  *              QUERY:
3926  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
3927  *
3928  *****************************************************************************/
3929
3930 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
3931                                 {
3932                                         CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
3933                                         n->username = $5;
3934                                         n->servername = $7;
3935                                         n->options = $8;
3936                                         $$ = (Node *) n;
3937                                 }
3938                 ;
3939
3940 /* User mapping authorization identifier */
3941 auth_ident:
3942                         CURRENT_USER    { $$ = "current_user"; }
3943                 |       USER                    { $$ = "current_user"; }
3944                 |       RoleId                  { $$ = (strcmp($1, "public") == 0) ? NULL : $1; }
3945                 ;
3946
3947 /*****************************************************************************
3948  *
3949  *              QUERY :
3950  *                              DROP USER MAPPING FOR auth_ident SERVER name
3951  *
3952  ****************************************************************************/
3953
3954 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
3955                                 {
3956                                         DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3957                                         n->username = $5;
3958                                         n->servername = $7;
3959                                         n->missing_ok = false;
3960                                         $$ = (Node *) n;
3961                                 }
3962                                 |  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
3963                 {
3964                                         DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3965                                         n->username = $7;
3966                                         n->servername = $9;
3967                                         n->missing_ok = true;
3968                                         $$ = (Node *) n;
3969                                 }
3970                 ;
3971
3972 /*****************************************************************************
3973  *
3974  *              QUERY :
3975  *                              ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
3976  *
3977  ****************************************************************************/
3978
3979 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
3980                                 {
3981                                         AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
3982                                         n->username = $5;
3983                                         n->servername = $7;
3984                                         n->options = $8;
3985                                         $$ = (Node *) n;
3986                                 }
3987                 ;
3988
3989 /*****************************************************************************
3990  *
3991  *              QUERIES :
3992  *                              CREATE TRIGGER ...
3993  *                              DROP TRIGGER ...
3994  *
3995  *****************************************************************************/
3996
3997 CreateTrigStmt:
3998                         CREATE TRIGGER name TriggerActionTime TriggerEvents ON
3999                         qualified_name TriggerForSpec TriggerWhen
4000                         EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
4001                                 {
4002                                         CreateTrigStmt *n = makeNode(CreateTrigStmt);
4003                                         n->trigname = $3;
4004                                         n->relation = $7;
4005                                         n->funcname = $12;
4006                                         n->args = $14;
4007                                         n->row = $8;
4008                                         n->timing = $4;
4009                                         n->events = intVal(linitial($5));
4010                                         n->columns = (List *) lsecond($5);
4011                                         n->whenClause = $9;
4012                                         n->isconstraint  = FALSE;
4013                                         n->deferrable    = FALSE;
4014                                         n->initdeferred  = FALSE;
4015                                         n->constrrel = NULL;
4016                                         $$ = (Node *)n;
4017                                 }
4018                         | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
4019                         qualified_name OptConstrFromTable ConstraintAttributeSpec
4020                         FOR EACH ROW TriggerWhen
4021                         EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
4022                                 {
4023                                         CreateTrigStmt *n = makeNode(CreateTrigStmt);
4024                                         n->trigname = $4;
4025                                         n->relation = $8;
4026                                         n->funcname = $17;
4027                                         n->args = $19;
4028                                         n->row = TRUE;
4029                                         n->timing = TRIGGER_TYPE_AFTER;
4030                                         n->events = intVal(linitial($6));
4031                                         n->columns = (List *) lsecond($6);
4032                                         n->whenClause = $14;
4033                                         n->isconstraint  = TRUE;
4034                                         n->deferrable = ($10 & 1) != 0;
4035                                         n->initdeferred = ($10 & 2) != 0;
4036                                         n->constrrel = $9;
4037                                         $$ = (Node *)n;
4038                                 }
4039                 ;
4040
4041 TriggerActionTime:
4042                         BEFORE                                                          { $$ = TRIGGER_TYPE_BEFORE; }
4043                         | AFTER                                                         { $$ = TRIGGER_TYPE_AFTER; }
4044                         | INSTEAD OF                                            { $$ = TRIGGER_TYPE_INSTEAD; }
4045                 ;
4046
4047 TriggerEvents:
4048                         TriggerOneEvent
4049                                 { $$ = $1; }
4050                         | TriggerEvents OR TriggerOneEvent
4051                                 {
4052                                         int             events1 = intVal(linitial($1));
4053                                         int             events2 = intVal(linitial($3));
4054                                         List   *columns1 = (List *) lsecond($1);
4055                                         List   *columns2 = (List *) lsecond($3);
4056
4057                                         if (events1 & events2)
4058                                                 parser_yyerror("duplicate trigger events specified");
4059                                         /*
4060                                          * concat'ing the columns lists loses information about
4061                                          * which columns went with which event, but so long as
4062                                          * only UPDATE carries columns and we disallow multiple
4063                                          * UPDATE items, it doesn't matter.  Command execution
4064                                          * should just ignore the columns for non-UPDATE events.
4065                                          */
4066                                         $$ = list_make2(makeInteger(events1 | events2),
4067                                                                         list_concat(columns1, columns2));
4068                                 }
4069                 ;
4070
4071 TriggerOneEvent:
4072                         INSERT
4073                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
4074                         | DELETE_P
4075                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
4076                         | UPDATE
4077                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
4078                         | UPDATE OF columnList
4079                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
4080                         | TRUNCATE
4081                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
4082                 ;
4083
4084 TriggerForSpec:
4085                         FOR TriggerForOptEach TriggerForType
4086                                 {
4087                                         $$ = $3;
4088                                 }
4089                         | /* EMPTY */
4090                                 {
4091                                         /*
4092                                          * If ROW/STATEMENT not specified, default to
4093                                          * STATEMENT, per SQL
4094                                          */
4095                                         $$ = FALSE;
4096                                 }
4097                 ;
4098
4099 TriggerForOptEach:
4100                         EACH                                                                    {}
4101                         | /*EMPTY*/                                                             {}
4102                 ;
4103
4104 TriggerForType:
4105                         ROW                                                                             { $$ = TRUE; }
4106                         | STATEMENT                                                             { $$ = FALSE; }
4107                 ;
4108
4109 TriggerWhen:
4110                         WHEN '(' a_expr ')'                                             { $$ = $3; }
4111                         | /*EMPTY*/                                                             { $$ = NULL; }
4112                 ;
4113
4114 TriggerFuncArgs:
4115                         TriggerFuncArg                                                  { $$ = list_make1($1); }
4116                         | TriggerFuncArgs ',' TriggerFuncArg    { $$ = lappend($1, $3); }
4117                         | /*EMPTY*/                                                             { $$ = NIL; }
4118                 ;
4119
4120 TriggerFuncArg:
4121                         Iconst
4122                                 {
4123                                         char buf[64];
4124                                         snprintf(buf, sizeof(buf), "%d", $1);
4125                                         $$ = makeString(pstrdup(buf));
4126                                 }
4127                         | FCONST                                                                { $$ = makeString($1); }
4128                         | Sconst                                                                { $$ = makeString($1); }
4129                         | BCONST                                                                { $$ = makeString($1); }
4130                         | XCONST                                                                { $$ = makeString($1); }
4131                         | ColId                                                                 { $$ = makeString($1); }
4132                 ;
4133
4134 OptConstrFromTable:
4135                         FROM qualified_name                                             { $$ = $2; }
4136                         | /*EMPTY*/                                                             { $$ = NULL; }
4137                 ;
4138
4139 ConstraintAttributeSpec:
4140                         ConstraintDeferrabilitySpec
4141                                 { $$ = $1; }
4142                         | ConstraintDeferrabilitySpec ConstraintTimeSpec
4143                                 {
4144                                         if ($1 == 0 && $2 != 0)
4145                                                 ereport(ERROR,
4146                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
4147                                                                  errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
4148                                                                  parser_errposition(@1)));
4149                                         $$ = $1 | $2;
4150                                 }
4151                         | ConstraintTimeSpec
4152                                 {
4153                                         if ($1 != 0)
4154                                                 $$ = 3;
4155                                         else
4156                                                 $$ = 0;
4157                                 }
4158                         | ConstraintTimeSpec ConstraintDeferrabilitySpec
4159                                 {
4160                                         if ($2 == 0 && $1 != 0)
4161                                                 ereport(ERROR,
4162                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
4163                                                                  errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
4164                                                                  parser_errposition(@1)));
4165                                         $$ = $1 | $2;
4166                                 }
4167                         | /*EMPTY*/
4168                                 { $$ = 0; }
4169                 ;
4170
4171 ConstraintDeferrabilitySpec:
4172                         NOT DEFERRABLE                                                  { $$ = 0; }
4173                         | DEFERRABLE                                                    { $$ = 1; }
4174                 ;
4175
4176 ConstraintTimeSpec:
4177                         INITIALLY IMMEDIATE                                             { $$ = 0; }
4178                         | INITIALLY DEFERRED                                    { $$ = 2; }
4179                 ;
4180
4181
4182 DropTrigStmt:
4183                         DROP TRIGGER name ON qualified_name opt_drop_behavior
4184                                 {
4185                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
4186                                         n->relation = $5;
4187                                         n->property = $3;
4188                                         n->behavior = $6;
4189                                         n->removeType = OBJECT_TRIGGER;
4190                                         n->missing_ok = false;
4191                                         $$ = (Node *) n;
4192                                 }
4193                         | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
4194                                 {
4195                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
4196                                         n->relation = $7;
4197                                         n->property = $5;
4198                                         n->behavior = $8;
4199                                         n->removeType = OBJECT_TRIGGER;
4200                                         n->missing_ok = true;
4201                                         $$ = (Node *) n;
4202                                 }
4203                 ;
4204
4205
4206 /*****************************************************************************
4207  *
4208  *              QUERIES :
4209  *                              CREATE ASSERTION ...
4210  *                              DROP ASSERTION ...
4211  *
4212  *****************************************************************************/
4213
4214 CreateAssertStmt:
4215                         CREATE ASSERTION name CHECK '(' a_expr ')'
4216                         ConstraintAttributeSpec
4217                                 {
4218                                         CreateTrigStmt *n = makeNode(CreateTrigStmt);
4219                                         n->trigname = $3;
4220                                         n->args = list_make1($6);
4221                                         n->isconstraint  = TRUE;
4222                                         n->deferrable = ($8 & 1) != 0;
4223                                         n->initdeferred = ($8 & 2) != 0;
4224
4225                                         ereport(ERROR,
4226                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4227                                                          errmsg("CREATE ASSERTION is not yet implemented")));
4228
4229                                         $$ = (Node *)n;
4230                                 }
4231                 ;
4232
4233 DropAssertStmt:
4234                         DROP ASSERTION name opt_drop_behavior
4235                                 {
4236                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
4237                                         n->relation = NULL;
4238                                         n->property = $3;
4239                                         n->behavior = $4;
4240                                         n->removeType = OBJECT_TRIGGER; /* XXX */
4241                                         ereport(ERROR,
4242                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4243                                                          errmsg("DROP ASSERTION is not yet implemented")));
4244                                         $$ = (Node *) n;
4245                                 }
4246                 ;
4247
4248
4249 /*****************************************************************************
4250  *
4251  *              QUERY :
4252  *                              define (aggregate,operator,type)
4253  *
4254  *****************************************************************************/
4255
4256 DefineStmt:
4257                         CREATE AGGREGATE func_name aggr_args definition
4258                                 {
4259                                         DefineStmt *n = makeNode(DefineStmt);
4260                                         n->kind = OBJECT_AGGREGATE;
4261                                         n->oldstyle = false;
4262                                         n->defnames = $3;
4263                                         n->args = $4;
4264                                         n->definition = $5;
4265                                         $$ = (Node *)n;
4266                                 }
4267                         | CREATE AGGREGATE func_name old_aggr_definition
4268                                 {
4269                                         /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
4270                                         DefineStmt *n = makeNode(DefineStmt);
4271                                         n->kind = OBJECT_AGGREGATE;
4272                                         n->oldstyle = true;
4273                                         n->defnames = $3;
4274                                         n->args = NIL;
4275                                         n->definition = $4;
4276                                         $$ = (Node *)n;
4277                                 }
4278                         | CREATE OPERATOR any_operator definition
4279                                 {
4280                                         DefineStmt *n = makeNode(DefineStmt);
4281                                         n->kind = OBJECT_OPERATOR;
4282                                         n->oldstyle = false;
4283                                         n->defnames = $3;
4284                                         n->args = NIL;
4285                                         n->definition = $4;
4286                                         $$ = (Node *)n;
4287                                 }
4288                         | CREATE TYPE_P any_name definition
4289                                 {
4290                                         DefineStmt *n = makeNode(DefineStmt);
4291                                         n->kind = OBJECT_TYPE;
4292                                         n->oldstyle = false;
4293                                         n->defnames = $3;
4294                                         n->args = NIL;
4295                                         n->definition = $4;
4296                                         $$ = (Node *)n;
4297                                 }
4298                         | CREATE TYPE_P any_name
4299                                 {
4300                                         /* Shell type (identified by lack of definition) */
4301                                         DefineStmt *n = makeNode(DefineStmt);
4302                                         n->kind = OBJECT_TYPE;
4303                                         n->oldstyle = false;
4304                                         n->defnames = $3;
4305                                         n->args = NIL;
4306                                         n->definition = NIL;
4307                                         $$ = (Node *)n;
4308                                 }
4309                         | CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
4310                                 {
4311                                         CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
4312
4313                                         /* can't use qualified_name, sigh */
4314                                         n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
4315                                         n->coldeflist = $6;
4316                                         $$ = (Node *)n;
4317                                 }
4318                         | CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
4319                                 {
4320                                         CreateEnumStmt *n = makeNode(CreateEnumStmt);
4321                                         n->typeName = $3;
4322                                         n->vals = $7;
4323                                         $$ = (Node *)n;
4324                                 }
4325                         | CREATE TEXT_P SEARCH PARSER any_name definition
4326                                 {
4327                                         DefineStmt *n = makeNode(DefineStmt);
4328                                         n->kind = OBJECT_TSPARSER;
4329                                         n->args = NIL;
4330                                         n->defnames = $5;
4331                                         n->definition = $6;
4332                                         $$ = (Node *)n;
4333                                 }
4334                         | CREATE TEXT_P SEARCH DICTIONARY any_name definition
4335                                 {
4336                                         DefineStmt *n = makeNode(DefineStmt);
4337                                         n->kind = OBJECT_TSDICTIONARY;
4338                                         n->args = NIL;
4339                                         n->defnames = $5;
4340                                         n->definition = $6;
4341                                         $$ = (Node *)n;
4342                                 }
4343                         | CREATE TEXT_P SEARCH TEMPLATE any_name definition
4344                                 {
4345                                         DefineStmt *n = makeNode(DefineStmt);
4346                                         n->kind = OBJECT_TSTEMPLATE;
4347                                         n->args = NIL;
4348                                         n->defnames = $5;
4349                                         n->definition = $6;
4350                                         $$ = (Node *)n;
4351                                 }
4352                         | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
4353                                 {
4354                                         DefineStmt *n = makeNode(DefineStmt);
4355                                         n->kind = OBJECT_TSCONFIGURATION;
4356                                         n->args = NIL;
4357                                         n->defnames = $5;
4358                                         n->definition = $6;
4359                                         $$ = (Node *)n;
4360                                 }
4361                         | CREATE COLLATION any_name definition
4362                                 {
4363                                         DefineStmt *n = makeNode(DefineStmt);
4364                                         n->kind = OBJECT_COLLATION;
4365                                         n->args = NIL;
4366                                         n->defnames = $3;
4367                                         n->definition = $4;
4368                                         $$ = (Node *)n;
4369                                 }
4370                         | CREATE COLLATION any_name FROM any_name
4371                                 {
4372                                         DefineStmt *n = makeNode(DefineStmt);
4373                                         n->kind = OBJECT_COLLATION;
4374                                         n->args = NIL;
4375                                         n->defnames = $3;
4376                                         n->definition = list_make1(makeDefElem("from", (Node *) $5));
4377                                         $$ = (Node *)n;
4378                                 }
4379                 ;
4380
4381 definition: '(' def_list ')'                                            { $$ = $2; }
4382                 ;
4383
4384 def_list:       def_elem                                                                { $$ = list_make1($1); }
4385                         | def_list ',' def_elem                                 { $$ = lappend($1, $3); }
4386                 ;
4387
4388 def_elem:       ColLabel '=' def_arg
4389                                 {
4390                                         $$ = makeDefElem($1, (Node *) $3);
4391                                 }
4392                         | ColLabel
4393                                 {
4394                                         $$ = makeDefElem($1, NULL);
4395                                 }
4396                 ;
4397
4398 /* Note: any simple identifier will be returned as a type name! */
4399 def_arg:        func_type                                               { $$ = (Node *)$1; }
4400                         | reserved_keyword                              { $$ = (Node *)makeString(pstrdup($1)); }
4401                         | qual_all_Op                                   { $$ = (Node *)$1; }
4402                         | NumericOnly                                   { $$ = (Node *)$1; }
4403                         | Sconst                                                { $$ = (Node *)makeString($1); }
4404                 ;
4405
4406 aggr_args:      '(' type_list ')'                                               { $$ = $2; }
4407                         | '(' '*' ')'                                                   { $$ = NIL; }
4408                 ;
4409
4410 old_aggr_definition: '(' old_aggr_list ')'                      { $$ = $2; }
4411                 ;
4412
4413 old_aggr_list: old_aggr_elem                                            { $$ = list_make1($1); }
4414                         | old_aggr_list ',' old_aggr_elem               { $$ = lappend($1, $3); }
4415                 ;
4416
4417 /*
4418  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
4419  * the item names needed in old aggregate definitions are likely to become
4420  * SQL keywords.
4421  */
4422 old_aggr_elem:  IDENT '=' def_arg
4423                                 {
4424                                         $$ = makeDefElem($1, (Node *)$3);
4425                                 }
4426                 ;
4427
4428 opt_enum_val_list:
4429                 enum_val_list                                                   { $$ = $1; }
4430                 | /*EMPTY*/                                                             { $$ = NIL; }
4431                 ;
4432
4433 enum_val_list:  Sconst
4434                                 { $$ = list_make1(makeString($1)); }
4435                         | enum_val_list ',' Sconst
4436                                 { $$ = lappend($1, makeString($3)); }
4437                 ;
4438
4439 /*****************************************************************************
4440  *
4441  *      ALTER TYPE enumtype ADD ...
4442  *
4443  *****************************************************************************/
4444
4445 AlterEnumStmt:
4446          ALTER TYPE_P any_name ADD_P VALUE_P Sconst
4447                          {
4448                                  AlterEnumStmt *n = makeNode(AlterEnumStmt);
4449                                  n->typeName = $3;
4450                                  n->newVal = $6;
4451                                  n->newValNeighbor = NULL;
4452                                  n->newValIsAfter = true;
4453                                  $$ = (Node *) n;
4454                          }
4455                  | ALTER TYPE_P any_name ADD_P VALUE_P Sconst BEFORE Sconst
4456                          {
4457                                  AlterEnumStmt *n = makeNode(AlterEnumStmt);
4458                                  n->typeName = $3;
4459                                  n->newVal = $6;
4460                                  n->newValNeighbor = $8;
4461                                  n->newValIsAfter = false;
4462                                  $$ = (Node *) n;
4463                          }
4464                  | ALTER TYPE_P any_name ADD_P VALUE_P Sconst AFTER Sconst
4465                          {
4466                                  AlterEnumStmt *n = makeNode(AlterEnumStmt);
4467                                  n->typeName = $3;
4468                                  n->newVal = $6;
4469                                  n->newValNeighbor = $8;
4470                                  n->newValIsAfter = true;
4471                                  $$ = (Node *) n;
4472                          }
4473                  ;
4474
4475
4476 /*****************************************************************************
4477  *
4478  *              QUERIES :
4479  *                              CREATE OPERATOR CLASS ...
4480  *                              CREATE OPERATOR FAMILY ...
4481  *                              ALTER OPERATOR FAMILY ...
4482  *                              DROP OPERATOR CLASS ...
4483  *                              DROP OPERATOR FAMILY ...
4484  *
4485  *****************************************************************************/
4486
4487 CreateOpClassStmt:
4488                         CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
4489                         USING access_method opt_opfamily AS opclass_item_list
4490                                 {
4491                                         CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
4492                                         n->opclassname = $4;
4493                                         n->isDefault = $5;
4494                                         n->datatype = $8;
4495                                         n->amname = $10;
4496                                         n->opfamilyname = $11;
4497                                         n->items = $13;
4498                                         $$ = (Node *) n;
4499                                 }
4500                 ;
4501
4502 opclass_item_list:
4503                         opclass_item                                                    { $$ = list_make1($1); }
4504                         | opclass_item_list ',' opclass_item    { $$ = lappend($1, $3); }
4505                 ;
4506
4507 opclass_item:
4508                         OPERATOR Iconst any_operator opclass_purpose opt_recheck
4509                                 {
4510                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
4511                                         n->itemtype = OPCLASS_ITEM_OPERATOR;
4512                                         n->name = $3;
4513                                         n->args = NIL;
4514                                         n->number = $2;
4515                                         n->order_family = $4;
4516                                         $$ = (Node *) n;
4517                                 }
4518                         | OPERATOR Iconst any_operator oper_argtypes opclass_purpose
4519                           opt_recheck
4520                                 {
4521                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
4522                                         n->itemtype = OPCLASS_ITEM_OPERATOR;
4523                                         n->name = $3;
4524                                         n->args = $4;
4525                                         n->number = $2;
4526                                         n->order_family = $5;
4527                                         $$ = (Node *) n;
4528                                 }
4529                         | FUNCTION Iconst func_name func_args
4530                                 {
4531                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
4532                                         n->itemtype = OPCLASS_ITEM_FUNCTION;
4533                                         n->name = $3;
4534                                         n->args = extractArgTypes($4);
4535                                         n->number = $2;
4536                                         $$ = (Node *) n;
4537                                 }
4538                         | FUNCTION Iconst '(' type_list ')' func_name func_args
4539                                 {
4540                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
4541                                         n->itemtype = OPCLASS_ITEM_FUNCTION;
4542                                         n->name = $6;
4543                                         n->args = extractArgTypes($7);
4544                                         n->number = $2;
4545                                         n->class_args = $4;
4546                                         $$ = (Node *) n;
4547                                 }
4548                         | STORAGE Typename
4549                                 {
4550                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
4551                                         n->itemtype = OPCLASS_ITEM_STORAGETYPE;
4552                                         n->storedtype = $2;
4553                                         $$ = (Node *) n;
4554                                 }
4555                 ;
4556
4557 opt_default:    DEFAULT                                         { $$ = TRUE; }
4558                         | /*EMPTY*/                                             { $$ = FALSE; }
4559                 ;
4560
4561 opt_opfamily:   FAMILY any_name                         { $$ = $2; }
4562                         | /*EMPTY*/                                             { $$ = NIL; }
4563                 ;
4564
4565 opclass_purpose: FOR SEARCH                                     { $$ = NIL; }
4566                         | FOR ORDER BY any_name                 { $$ = $4; }
4567                         | /*EMPTY*/                                             { $$ = NIL; }
4568                 ;
4569
4570 opt_recheck:    RECHECK
4571                                 {
4572                                         /*
4573                                          * RECHECK no longer does anything in opclass definitions,
4574                                          * but we still accept it to ease porting of old database
4575                                          * dumps.
4576                                          */
4577                                         ereport(NOTICE,
4578                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4579                                                          errmsg("RECHECK is no longer required"),
4580                                                          errhint("Update your data type."),
4581                                                          parser_errposition(@1)));
4582                                         $$ = TRUE;
4583                                 }
4584                         | /*EMPTY*/                                             { $$ = FALSE; }
4585                 ;
4586
4587
4588 CreateOpFamilyStmt:
4589                         CREATE OPERATOR FAMILY any_name USING access_method
4590                                 {
4591                                         CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
4592                                         n->opfamilyname = $4;
4593                                         n->amname = $6;
4594                                         $$ = (Node *) n;
4595                                 }
4596                 ;
4597
4598 AlterOpFamilyStmt:
4599                         ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
4600                                 {
4601                                         AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
4602                                         n->opfamilyname = $4;
4603                                         n->amname = $6;
4604                                         n->isDrop = false;
4605                                         n->items = $8;
4606                                         $$ = (Node *) n;
4607                                 }
4608                         | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
4609                                 {
4610                                         AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
4611                                         n->opfamilyname = $4;
4612                                         n->amname = $6;
4613                                         n->isDrop = true;
4614                                         n->items = $8;
4615                                         $$ = (Node *) n;
4616                                 }
4617                 ;
4618
4619 opclass_drop_list:
4620                         opclass_drop                                                    { $$ = list_make1($1); }
4621                         | opclass_drop_list ',' opclass_drop    { $$ = lappend($1, $3); }
4622                 ;
4623
4624 opclass_drop:
4625                         OPERATOR Iconst '(' type_list ')'
4626                                 {
4627                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
4628                                         n->itemtype = OPCLASS_ITEM_OPERATOR;
4629                                         n->number = $2;
4630                                         n->args = $4;
4631                                         $$ = (Node *) n;
4632                                 }
4633                         | FUNCTION Iconst '(' type_list ')'
4634                                 {
4635                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
4636                                         n->itemtype = OPCLASS_ITEM_FUNCTION;
4637                                         n->number = $2;
4638                                         n->args = $4;
4639                                         $$ = (Node *) n;
4640                                 }
4641                 ;
4642
4643
4644 DropOpClassStmt:
4645                         DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
4646                                 {
4647                                         RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
4648                                         n->opclassname = $4;
4649                                         n->amname = $6;
4650                                         n->behavior = $7;
4651                                         n->missing_ok = false;
4652                                         $$ = (Node *) n;
4653                                 }
4654                         | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
4655                                 {
4656                                         RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
4657                                         n->opclassname = $6;
4658                                         n->amname = $8;
4659                                         n->behavior = $9;
4660                                         n->missing_ok = true;
4661                                         $$ = (Node *) n;
4662                                 }
4663                 ;
4664
4665 DropOpFamilyStmt:
4666                         DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
4667                                 {
4668                                         RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
4669                                         n->opfamilyname = $4;
4670                                         n->amname = $6;
4671                                         n->behavior = $7;
4672                                         n->missing_ok = false;
4673                                         $$ = (Node *) n;
4674                                 }
4675                         | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
4676                                 {
4677                                         RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
4678                                         n->opfamilyname = $6;
4679                                         n->amname = $8;
4680                                         n->behavior = $9;
4681                                         n->missing_ok = true;
4682                                         $$ = (Node *) n;
4683                                 }
4684                 ;
4685
4686
4687 /*****************************************************************************
4688  *
4689  *              QUERY:
4690  *
4691  *              DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
4692  *              REASSIGN OWNED BY username [, username ...] TO username
4693  *
4694  *****************************************************************************/
4695 DropOwnedStmt:
4696                         DROP OWNED BY name_list opt_drop_behavior
4697                                 {
4698                                         DropOwnedStmt *n = makeNode(DropOwnedStmt);
4699                                         n->roles = $4;
4700                                         n->behavior = $5;
4701                                         $$ = (Node *)n;
4702                                 }
4703                 ;
4704
4705 ReassignOwnedStmt:
4706                         REASSIGN OWNED BY name_list TO name
4707                                 {
4708                                         ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
4709                                         n->roles = $4;
4710                                         n->newrole = $6;
4711                                         $$ = (Node *)n;
4712                                 }
4713                 ;
4714
4715 /*****************************************************************************
4716  *
4717  *              QUERY:
4718  *
4719  *              DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
4720  *           [ RESTRICT | CASCADE ]
4721  *
4722  *****************************************************************************/
4723
4724 DropStmt:       DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
4725                                 {
4726                                         DropStmt *n = makeNode(DropStmt);
4727                                         n->removeType = $2;
4728                                         n->missing_ok = TRUE;
4729                                         n->objects = $5;
4730                                         n->behavior = $6;
4731                                         $$ = (Node *)n;
4732                                 }
4733                         | DROP drop_type any_name_list opt_drop_behavior
4734                                 {
4735                                         DropStmt *n = makeNode(DropStmt);
4736                                         n->removeType = $2;
4737                                         n->missing_ok = FALSE;
4738                                         n->objects = $3;
4739                                         n->behavior = $4;
4740                                         $$ = (Node *)n;
4741                                 }
4742                 ;
4743
4744
4745 drop_type:      TABLE                                                                   { $$ = OBJECT_TABLE; }
4746                         | SEQUENCE                                                              { $$ = OBJECT_SEQUENCE; }
4747                         | VIEW                                                                  { $$ = OBJECT_VIEW; }
4748                         | INDEX                                                                 { $$ = OBJECT_INDEX; }
4749                         | FOREIGN TABLE                                                 { $$ = OBJECT_FOREIGN_TABLE; }
4750                         | TYPE_P                                                                { $$ = OBJECT_TYPE; }
4751                         | DOMAIN_P                                                              { $$ = OBJECT_DOMAIN; }
4752                         | COLLATION                                                             { $$ = OBJECT_COLLATION; }
4753                         | CONVERSION_P                                                  { $$ = OBJECT_CONVERSION; }
4754                         | SCHEMA                                                                { $$ = OBJECT_SCHEMA; }
4755                         | EXTENSION                                                             { $$ = OBJECT_EXTENSION; }
4756                         | TEXT_P SEARCH PARSER                                  { $$ = OBJECT_TSPARSER; }
4757                         | TEXT_P SEARCH DICTIONARY                              { $$ = OBJECT_TSDICTIONARY; }
4758                         | TEXT_P SEARCH TEMPLATE                                { $$ = OBJECT_TSTEMPLATE; }
4759                         | TEXT_P SEARCH CONFIGURATION                   { $$ = OBJECT_TSCONFIGURATION; }
4760                 ;
4761
4762 any_name_list:
4763                         any_name                                                                { $$ = list_make1($1); }
4764                         | any_name_list ',' any_name                    { $$ = lappend($1, $3); }
4765                 ;
4766
4767 any_name:       ColId                                           { $$ = list_make1(makeString($1)); }
4768                         | ColId attrs                           { $$ = lcons(makeString($1), $2); }
4769                 ;
4770
4771 attrs:          '.' attr_name
4772                                         { $$ = list_make1(makeString($2)); }
4773                         | attrs '.' attr_name
4774                                         { $$ = lappend($1, makeString($3)); }
4775                 ;
4776
4777
4778 /*****************************************************************************
4779  *
4780  *              QUERY:
4781  *                              truncate table relname1, relname2, ...
4782  *
4783  *****************************************************************************/
4784
4785 TruncateStmt:
4786                         TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
4787                                 {
4788                                         TruncateStmt *n = makeNode(TruncateStmt);
4789                                         n->relations = $3;
4790                                         n->restart_seqs = $4;
4791                                         n->behavior = $5;
4792                                         $$ = (Node *)n;
4793                                 }
4794                 ;
4795
4796 opt_restart_seqs:
4797                         CONTINUE_P IDENTITY_P           { $$ = false; }
4798                         | RESTART IDENTITY_P            { $$ = true; }
4799                         | /* EMPTY */                           { $$ = false; }
4800                 ;
4801
4802 /*****************************************************************************
4803  *
4804  *      The COMMENT ON statement can take different forms based upon the type of
4805  *      the object associated with the comment. The form of the statement is:
4806  *
4807  *      COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
4808  *                                 COLLATION | CONVERSION | LANGUAGE | OPERATOR CLASS |
4809  *                                 LARGE OBJECT | CAST | COLUMN | SCHEMA | TABLESPACE |
4810  *                                 EXTENSION | ROLE | TEXT SEARCH PARSER |
4811  *                                 TEXT SEARCH DICTIONARY | TEXT SEARCH TEMPLATE |
4812  *                                 TEXT SEARCH CONFIGURATION | FOREIGN TABLE |
4813  *                                 FOREIGN DATA WRAPPER | SERVER ] <objname> |
4814  *                               AGGREGATE <aggname> (arg1, ...) |
4815  *                               FUNCTION <funcname> (arg1, arg2, ...) |
4816  *                               OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
4817  *                               TRIGGER <triggername> ON <relname> |
4818  *                               CONSTRAINT <constraintname> ON <relname> |
4819  *                               RULE <rulename> ON <relname> ]
4820  *                         IS 'text'
4821  *
4822  *****************************************************************************/
4823
4824 CommentStmt:
4825                         COMMENT ON comment_type any_name IS comment_text
4826                                 {
4827                                         CommentStmt *n = makeNode(CommentStmt);
4828                                         n->objtype = $3;
4829                                         n->objname = $4;
4830                                         n->objargs = NIL;
4831                                         n->comment = $6;
4832                                         $$ = (Node *) n;
4833                                 }
4834                         | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
4835                                 {
4836                                         CommentStmt *n = makeNode(CommentStmt);
4837                                         n->objtype = OBJECT_AGGREGATE;
4838                                         n->objname = $4;
4839                                         n->objargs = $5;
4840                                         n->comment = $7;
4841                                         $$ = (Node *) n;
4842                                 }
4843                         | COMMENT ON FUNCTION func_name func_args IS comment_text
4844                                 {
4845                                         CommentStmt *n = makeNode(CommentStmt);
4846                                         n->objtype = OBJECT_FUNCTION;
4847                                         n->objname = $4;
4848                                         n->objargs = extractArgTypes($5);
4849                                         n->comment = $7;
4850                                         $$ = (Node *) n;
4851                                 }
4852                         | COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
4853                                 {
4854                                         CommentStmt *n = makeNode(CommentStmt);
4855                                         n->objtype = OBJECT_OPERATOR;
4856                                         n->objname = $4;
4857                                         n->objargs = $5;
4858                                         n->comment = $7;
4859                                         $$ = (Node *) n;
4860                                 }
4861                         | COMMENT ON CONSTRAINT name ON any_name IS comment_text
4862                                 {
4863                                         CommentStmt *n = makeNode(CommentStmt);
4864                                         n->objtype = OBJECT_CONSTRAINT;
4865                                         n->objname = lappend($6, makeString($4));
4866                                         n->objargs = NIL;
4867                                         n->comment = $8;
4868                                         $$ = (Node *) n;
4869                                 }
4870                         | COMMENT ON RULE name ON any_name IS comment_text
4871                                 {
4872                                         CommentStmt *n = makeNode(CommentStmt);
4873                                         n->objtype = OBJECT_RULE;
4874                                         n->objname = lappend($6, makeString($4));
4875                                         n->objargs = NIL;
4876                                         n->comment = $8;
4877                                         $$ = (Node *) n;
4878                                 }
4879                         | COMMENT ON RULE name IS comment_text
4880                                 {
4881                                         /* Obsolete syntax supported for awhile for compatibility */
4882                                         CommentStmt *n = makeNode(CommentStmt);
4883                                         n->objtype = OBJECT_RULE;
4884                                         n->objname = list_make1(makeString($4));
4885                                         n->objargs = NIL;
4886                                         n->comment = $6;
4887                                         $$ = (Node *) n;
4888                                 }
4889                         | COMMENT ON TRIGGER name ON any_name IS comment_text
4890                                 {
4891                                         CommentStmt *n = makeNode(CommentStmt);
4892                                         n->objtype = OBJECT_TRIGGER;
4893                                         n->objname = lappend($6, makeString($4));
4894                                         n->objargs = NIL;
4895                                         n->comment = $8;
4896                                         $$ = (Node *) n;
4897                                 }
4898                         | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
4899                                 {
4900                                         CommentStmt *n = makeNode(CommentStmt);
4901                                         n->objtype = OBJECT_OPCLASS;
4902                                         n->objname = $5;
4903                                         n->objargs = list_make1(makeString($7));
4904                                         n->comment = $9;
4905                                         $$ = (Node *) n;
4906                                 }
4907                         | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
4908                                 {
4909                                         CommentStmt *n = makeNode(CommentStmt);
4910                                         n->objtype = OBJECT_OPFAMILY;
4911                                         n->objname = $5;
4912                                         n->objargs = list_make1(makeString($7));
4913                                         n->comment = $9;
4914                                         $$ = (Node *) n;
4915                                 }
4916                         | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
4917                                 {
4918                                         CommentStmt *n = makeNode(CommentStmt);
4919                                         n->objtype = OBJECT_LARGEOBJECT;
4920                                         n->objname = list_make1($5);
4921                                         n->objargs = NIL;
4922                                         n->comment = $7;
4923                                         $$ = (Node *) n;
4924                                 }
4925                         | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
4926                                 {
4927                                         CommentStmt *n = makeNode(CommentStmt);
4928                                         n->objtype = OBJECT_CAST;
4929                                         n->objname = list_make1($5);
4930                                         n->objargs = list_make1($7);
4931                                         n->comment = $10;
4932                                         $$ = (Node *) n;
4933                                 }
4934                         | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
4935                                 {
4936                                         CommentStmt *n = makeNode(CommentStmt);
4937                                         n->objtype = OBJECT_LANGUAGE;
4938                                         n->objname = $5;
4939                                         n->objargs = NIL;
4940                                         n->comment = $7;
4941                                         $$ = (Node *) n;
4942                                 }
4943                         | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
4944                                 {
4945                                         CommentStmt *n = makeNode(CommentStmt);
4946                                         n->objtype = OBJECT_TSPARSER;
4947                                         n->objname = $6;
4948                                         n->comment = $8;
4949                                         $$ = (Node *) n;
4950                                 }
4951                         | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
4952                                 {
4953                                         CommentStmt *n = makeNode(CommentStmt);
4954                                         n->objtype = OBJECT_TSDICTIONARY;
4955                                         n->objname = $6;
4956                                         n->comment = $8;
4957                                         $$ = (Node *) n;
4958                                 }
4959                         | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
4960                                 {
4961                                         CommentStmt *n = makeNode(CommentStmt);
4962                                         n->objtype = OBJECT_TSTEMPLATE;
4963                                         n->objname = $6;
4964                                         n->comment = $8;
4965                                         $$ = (Node *) n;
4966                                 }
4967                         | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
4968                                 {
4969                                         CommentStmt *n = makeNode(CommentStmt);
4970                                         n->objtype = OBJECT_TSCONFIGURATION;
4971                                         n->objname = $6;
4972                                         n->comment = $8;
4973                                         $$ = (Node *) n;
4974                                 }
4975                 ;
4976
4977 comment_type:
4978                         COLUMN                                                          { $$ = OBJECT_COLUMN; }
4979                         | DATABASE                                                      { $$ = OBJECT_DATABASE; }
4980                         | SCHEMA                                                        { $$ = OBJECT_SCHEMA; }
4981                         | INDEX                                                         { $$ = OBJECT_INDEX; }
4982                         | SEQUENCE                                                      { $$ = OBJECT_SEQUENCE; }
4983                         | TABLE                                                         { $$ = OBJECT_TABLE; }
4984                         | DOMAIN_P                                                      { $$ = OBJECT_DOMAIN; }
4985                         | TYPE_P                                                        { $$ = OBJECT_TYPE; }
4986                         | VIEW                                                          { $$ = OBJECT_VIEW; }
4987                         | COLLATION                                                     { $$ = OBJECT_COLLATION; }
4988                         | CONVERSION_P                                          { $$ = OBJECT_CONVERSION; }
4989                         | TABLESPACE                                            { $$ = OBJECT_TABLESPACE; }
4990                         | EXTENSION                                             { $$ = OBJECT_EXTENSION; }
4991                         | ROLE                                                          { $$ = OBJECT_ROLE; }
4992                         | FOREIGN TABLE                                         { $$ = OBJECT_FOREIGN_TABLE; }
4993                         | SERVER                                                        { $$ = OBJECT_FOREIGN_SERVER; }
4994                         | FOREIGN DATA_P WRAPPER                        { $$ = OBJECT_FDW; }
4995                 ;
4996
4997 comment_text:
4998                         Sconst                                                          { $$ = $1; }
4999                         | NULL_P                                                        { $$ = NULL; }
5000                 ;
5001
5002
5003 /*****************************************************************************
5004  *
5005  *  SECURITY LABEL [FOR <provider>] ON <object> IS <label>
5006  *
5007  *  As with COMMENT ON, <object> can refer to various types of database
5008  *  objects (e.g. TABLE, COLUMN, etc.).
5009  *
5010  *****************************************************************************/
5011
5012 SecLabelStmt:
5013                         SECURITY LABEL opt_provider ON security_label_type any_name
5014                         IS security_label
5015                                 {
5016                                         SecLabelStmt *n = makeNode(SecLabelStmt);
5017                                         n->provider = $3;
5018                                         n->objtype = $5;
5019                                         n->objname = $6;
5020                                         n->objargs = NIL;
5021                                         n->label = $8;
5022                                         $$ = (Node *) n;
5023                                 }
5024                         | SECURITY LABEL opt_provider ON AGGREGATE func_name aggr_args
5025                           IS security_label
5026                                 {
5027                                         SecLabelStmt *n = makeNode(SecLabelStmt);
5028                                         n->provider = $3;
5029                                         n->objtype = OBJECT_AGGREGATE;
5030                                         n->objname = $6;
5031                                         n->objargs = $7;
5032                                         n->label = $9;
5033                                         $$ = (Node *) n;
5034                                 }
5035                         | SECURITY LABEL opt_provider ON FUNCTION func_name func_args
5036                           IS security_label
5037                                 {
5038                                         SecLabelStmt *n = makeNode(SecLabelStmt);
5039                                         n->provider = $3;
5040                                         n->objtype = OBJECT_FUNCTION;
5041                                         n->objname = $6;
5042                                         n->objargs = extractArgTypes($7);
5043                                         n->label = $9;
5044                                         $$ = (Node *) n;
5045                                 }
5046                         | SECURITY LABEL opt_provider ON LARGE_P OBJECT_P NumericOnly
5047                           IS security_label
5048                                 {
5049                                         SecLabelStmt *n = makeNode(SecLabelStmt);
5050                                         n->provider = $3;
5051                                         n->objtype = OBJECT_LARGEOBJECT;
5052                                         n->objname = list_make1($7);
5053                                         n->objargs = NIL;
5054                                         n->label = $9;
5055                                         $$ = (Node *) n;
5056                                 }
5057                         | SECURITY LABEL opt_provider ON opt_procedural LANGUAGE any_name
5058                           IS security_label
5059                                 {
5060                                         SecLabelStmt *n = makeNode(SecLabelStmt);
5061                                         n->provider = $3;
5062                                         n->objtype = OBJECT_LANGUAGE;
5063                                         n->objname = $7;
5064                                         n->objargs = NIL;
5065                                         n->label = $9;
5066                                         $$ = (Node *) n;
5067                                 }
5068                 ;
5069
5070 opt_provider:   FOR ColId_or_Sconst     { $$ = $2; }
5071                                 | /* empty */           { $$ = NULL; }
5072                 ;
5073
5074 security_label_type:
5075                         COLUMN                                                          { $$ = OBJECT_COLUMN; }
5076                         | FOREIGN TABLE                                         { $$ = OBJECT_FOREIGN_TABLE; }
5077                         | SCHEMA                                                        { $$ = OBJECT_SCHEMA; }
5078                         | SEQUENCE                                                      { $$ = OBJECT_SEQUENCE; }
5079                         | TABLE                                                         { $$ = OBJECT_TABLE; }
5080                         | DOMAIN_P                                                      { $$ = OBJECT_TYPE; }
5081                         | TYPE_P                                                        { $$ = OBJECT_TYPE; }
5082                         | VIEW                                                          { $$ = OBJECT_VIEW; }
5083                 ;
5084
5085 security_label: Sconst                          { $$ = $1; }
5086                                 | NULL_P                        { $$ = NULL; }
5087                 ;
5088
5089 /*****************************************************************************
5090  *
5091  *              QUERY:
5092  *                      fetch/move
5093  *
5094  *****************************************************************************/
5095
5096 FetchStmt:      FETCH fetch_args
5097                                 {
5098                                         FetchStmt *n = (FetchStmt *) $2;
5099                                         n->ismove = FALSE;
5100                                         $$ = (Node *)n;
5101                                 }
5102                         | MOVE fetch_args
5103                                 {
5104                                         FetchStmt *n = (FetchStmt *) $2;
5105                                         n->ismove = TRUE;
5106                                         $$ = (Node *)n;
5107                                 }
5108                 ;
5109
5110 fetch_args:     cursor_name
5111                                 {
5112                                         FetchStmt *n = makeNode(FetchStmt);
5113                                         n->portalname = $1;
5114                                         n->direction = FETCH_FORWARD;
5115                                         n->howMany = 1;
5116                                         $$ = (Node *)n;
5117                                 }
5118                         | from_in cursor_name
5119                                 {
5120                                         FetchStmt *n = makeNode(FetchStmt);
5121                                         n->portalname = $2;
5122                                         n->direction = FETCH_FORWARD;
5123                                         n->howMany = 1;
5124                                         $$ = (Node *)n;
5125                                 }
5126                         | NEXT opt_from_in cursor_name
5127                                 {
5128                                         FetchStmt *n = makeNode(FetchStmt);
5129                                         n->portalname = $3;
5130                                         n->direction = FETCH_FORWARD;
5131                                         n->howMany = 1;
5132                                         $$ = (Node *)n;
5133                                 }
5134                         | PRIOR opt_from_in cursor_name
5135                                 {
5136                                         FetchStmt *n = makeNode(FetchStmt);
5137                                         n->portalname = $3;
5138                                         n->direction = FETCH_BACKWARD;
5139                                         n->howMany = 1;
5140                                         $$ = (Node *)n;
5141                                 }
5142                         | FIRST_P opt_from_in cursor_name
5143                                 {
5144                                         FetchStmt *n = makeNode(FetchStmt);
5145                                         n->portalname = $3;
5146                                         n->direction = FETCH_ABSOLUTE;
5147                                         n->howMany = 1;
5148                                         $$ = (Node *)n;
5149                                 }
5150                         | LAST_P opt_from_in cursor_name
5151                                 {
5152                                         FetchStmt *n = makeNode(FetchStmt);
5153                                         n->portalname = $3;
5154                                         n->direction = FETCH_ABSOLUTE;
5155                                         n->howMany = -1;
5156                                         $$ = (Node *)n;
5157                                 }
5158                         | ABSOLUTE_P SignedIconst opt_from_in cursor_name
5159                                 {
5160                                         FetchStmt *n = makeNode(FetchStmt);
5161                                         n->portalname = $4;
5162                                         n->direction = FETCH_ABSOLUTE;
5163                                         n->howMany = $2;
5164                                         $$ = (Node *)n;
5165                                 }
5166                         | RELATIVE_P SignedIconst opt_from_in cursor_name
5167                                 {
5168                                         FetchStmt *n = makeNode(FetchStmt);
5169                                         n->portalname = $4;
5170                                         n->direction = FETCH_RELATIVE;
5171                                         n->howMany = $2;
5172                                         $$ = (Node *)n;
5173                                 }
5174                         | SignedIconst opt_from_in cursor_name
5175                                 {
5176                                         FetchStmt *n = makeNode(FetchStmt);
5177                                         n->portalname = $3;
5178                                         n->direction = FETCH_FORWARD;
5179                                         n->howMany = $1;
5180                                         $$ = (Node *)n;
5181                                 }
5182                         | ALL opt_from_in cursor_name
5183                                 {
5184                                         FetchStmt *n = makeNode(FetchStmt);
5185                                         n->portalname = $3;
5186                                         n->direction = FETCH_FORWARD;
5187                                         n->howMany = FETCH_ALL;
5188                                         $$ = (Node *)n;
5189                                 }
5190                         | FORWARD opt_from_in cursor_name
5191                                 {
5192                                         FetchStmt *n = makeNode(FetchStmt);
5193                                         n->portalname = $3;
5194                                         n->direction = FETCH_FORWARD;
5195                                         n->howMany = 1;
5196                                         $$ = (Node *)n;
5197                                 }
5198                         | FORWARD SignedIconst opt_from_in cursor_name
5199                                 {
5200                                         FetchStmt *n = makeNode(FetchStmt);
5201                                         n->portalname = $4;
5202                                         n->direction = FETCH_FORWARD;
5203                                         n->howMany = $2;
5204                                         $$ = (Node *)n;
5205                                 }
5206                         | FORWARD ALL opt_from_in cursor_name
5207                                 {
5208                                         FetchStmt *n = makeNode(FetchStmt);
5209                                         n->portalname = $4;
5210                                         n->direction = FETCH_FORWARD;
5211                                         n->howMany = FETCH_ALL;
5212                                         $$ = (Node *)n;
5213                                 }
5214                         | BACKWARD opt_from_in cursor_name
5215                                 {
5216                                         FetchStmt *n = makeNode(FetchStmt);
5217                                         n->portalname = $3;
5218                                         n->direction = FETCH_BACKWARD;
5219                                         n->howMany = 1;
5220                                         $$ = (Node *)n;
5221                                 }
5222                         | BACKWARD SignedIconst opt_from_in cursor_name
5223                                 {
5224                                         FetchStmt *n = makeNode(FetchStmt);
5225                                         n->portalname = $4;
5226                                         n->direction = FETCH_BACKWARD;
5227                                         n->howMany = $2;
5228                                         $$ = (Node *)n;
5229                                 }
5230                         | BACKWARD ALL opt_from_in cursor_name
5231                                 {
5232                                         FetchStmt *n = makeNode(FetchStmt);
5233                                         n->portalname = $4;
5234                                         n->direction = FETCH_BACKWARD;
5235                                         n->howMany = FETCH_ALL;
5236                                         $$ = (Node *)n;
5237                                 }
5238                 ;
5239
5240 from_in:        FROM                                                                    {}
5241                         | IN_P                                                                  {}
5242                 ;
5243
5244 opt_from_in:    from_in                                                         {}
5245                         | /* EMPTY */                                                   {}
5246                 ;
5247
5248
5249 /*****************************************************************************
5250  *
5251  * GRANT and REVOKE statements
5252  *
5253  *****************************************************************************/
5254
5255 GrantStmt:      GRANT privileges ON privilege_target TO grantee_list
5256                         opt_grant_grant_option
5257                                 {
5258                                         GrantStmt *n = makeNode(GrantStmt);
5259                                         n->is_grant = true;
5260                                         n->privileges = $2;
5261                                         n->targtype = ($4)->targtype;
5262                                         n->objtype = ($4)->objtype;
5263                                         n->objects = ($4)->objs;
5264                                         n->grantees = $6;
5265                                         n->grant_option = $7;
5266                                         $$ = (Node*)n;
5267                                 }
5268                 ;
5269
5270 RevokeStmt:
5271                         REVOKE privileges ON privilege_target
5272                         FROM grantee_list opt_drop_behavior
5273                                 {
5274                                         GrantStmt *n = makeNode(GrantStmt);
5275                                         n->is_grant = false;
5276                                         n->grant_option = false;
5277                                         n->privileges = $2;
5278                                         n->targtype = ($4)->targtype;
5279                                         n->objtype = ($4)->objtype;
5280                                         n->objects = ($4)->objs;
5281                                         n->grantees = $6;
5282                                         n->behavior = $7;
5283                                         $$ = (Node *)n;
5284                                 }
5285                         | REVOKE GRANT OPTION FOR privileges ON privilege_target
5286                         FROM grantee_list opt_drop_behavior
5287                                 {
5288                                         GrantStmt *n = makeNode(GrantStmt);
5289                                         n->is_grant = false;
5290                                         n->grant_option = true;
5291                                         n->privileges = $5;
5292                                         n->targtype = ($7)->targtype;
5293                                         n->objtype = ($7)->objtype;
5294                                         n->objects = ($7)->objs;
5295                                         n->grantees = $9;
5296                                         n->behavior = $10;
5297                                         $$ = (Node *)n;
5298                                 }
5299                 ;
5300
5301
5302 /*
5303  * Privilege names are represented as strings; the validity of the privilege
5304  * names gets checked at execution.  This is a bit annoying but we have little
5305  * choice because of the syntactic conflict with lists of role names in
5306  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
5307  * production any reserved keywords that need to be usable as privilege names.
5308  */
5309
5310 /* either ALL [PRIVILEGES] or a list of individual privileges */
5311 privileges: privilege_list
5312                                 { $$ = $1; }
5313                         | ALL
5314                                 { $$ = NIL; }
5315                         | ALL PRIVILEGES
5316                                 { $$ = NIL; }
5317                         | ALL '(' columnList ')'
5318                                 {
5319                                         AccessPriv *n = makeNode(AccessPriv);
5320                                         n->priv_name = NULL;
5321                                         n->cols = $3;
5322                                         $$ = list_make1(n);
5323                                 }
5324                         | ALL PRIVILEGES '(' columnList ')'
5325                                 {
5326                                         AccessPriv *n = makeNode(AccessPriv);
5327                                         n->priv_name = NULL;
5328                                         n->cols = $4;
5329                                         $$ = list_make1(n);
5330                                 }
5331                 ;
5332
5333 privilege_list: privilege                                                       { $$ = list_make1($1); }
5334                         | privilege_list ',' privilege                  { $$ = lappend($1, $3); }
5335                 ;
5336
5337 privilege:      SELECT opt_column_list
5338                         {
5339                                 AccessPriv *n = makeNode(AccessPriv);
5340                                 n->priv_name = pstrdup($1);
5341                                 n->cols = $2;
5342                                 $$ = n;
5343                         }
5344                 | REFERENCES opt_column_list
5345                         {
5346                                 AccessPriv *n = makeNode(AccessPriv);
5347                                 n->priv_name = pstrdup($1);
5348                                 n->cols = $2;
5349                                 $$ = n;
5350                         }
5351                 | CREATE opt_column_list
5352                         {
5353                                 AccessPriv *n = makeNode(AccessPriv);
5354                                 n->priv_name = pstrdup($1);
5355                                 n->cols = $2;
5356                                 $$ = n;
5357                         }
5358                 | ColId opt_column_list
5359                         {
5360                                 AccessPriv *n = makeNode(AccessPriv);
5361                                 n->priv_name = $1;
5362                                 n->cols = $2;
5363                                 $$ = n;
5364                         }
5365                 ;
5366
5367
5368 /* Don't bother trying to fold the first two rules into one using
5369  * opt_table.  You're going to get conflicts.
5370  */
5371 privilege_target:
5372                         qualified_name_list
5373                                 {
5374                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5375                                         n->targtype = ACL_TARGET_OBJECT;
5376                                         n->objtype = ACL_OBJECT_RELATION;
5377                                         n->objs = $1;
5378                                         $$ = n;
5379                                 }
5380                         | TABLE qualified_name_list
5381                                 {
5382                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5383                                         n->targtype = ACL_TARGET_OBJECT;
5384                                         n->objtype = ACL_OBJECT_RELATION;
5385                                         n->objs = $2;
5386                                         $$ = n;
5387                                 }
5388                         | SEQUENCE qualified_name_list
5389                                 {
5390                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5391                                         n->targtype = ACL_TARGET_OBJECT;
5392                                         n->objtype = ACL_OBJECT_SEQUENCE;
5393                                         n->objs = $2;
5394                                         $$ = n;
5395                                 }
5396                         | FOREIGN DATA_P WRAPPER name_list
5397                                 {
5398                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5399                                         n->targtype = ACL_TARGET_OBJECT;
5400                                         n->objtype = ACL_OBJECT_FDW;
5401                                         n->objs = $4;
5402                                         $$ = n;
5403                                 }
5404                         | FOREIGN SERVER name_list
5405                                 {
5406                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5407                                         n->targtype = ACL_TARGET_OBJECT;
5408                                         n->objtype = ACL_OBJECT_FOREIGN_SERVER;
5409                                         n->objs = $3;
5410                                         $$ = n;
5411                                 }
5412                         | FUNCTION function_with_argtypes_list
5413                                 {
5414                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5415                                         n->targtype = ACL_TARGET_OBJECT;
5416                                         n->objtype = ACL_OBJECT_FUNCTION;
5417                                         n->objs = $2;
5418                                         $$ = n;
5419                                 }
5420                         | DATABASE name_list
5421                                 {
5422                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5423                                         n->targtype = ACL_TARGET_OBJECT;
5424                                         n->objtype = ACL_OBJECT_DATABASE;
5425                                         n->objs = $2;
5426                                         $$ = n;
5427                                 }
5428                         | LANGUAGE name_list
5429                                 {
5430                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5431                                         n->targtype = ACL_TARGET_OBJECT;
5432                                         n->objtype = ACL_OBJECT_LANGUAGE;
5433                                         n->objs = $2;
5434                                         $$ = n;
5435                                 }
5436                         | LARGE_P OBJECT_P NumericOnly_list
5437                                 {
5438                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5439                                         n->targtype = ACL_TARGET_OBJECT;
5440                                         n->objtype = ACL_OBJECT_LARGEOBJECT;
5441                                         n->objs = $3;
5442                                         $$ = n;
5443                                 }
5444                         | SCHEMA name_list
5445                                 {
5446                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5447                                         n->targtype = ACL_TARGET_OBJECT;
5448                                         n->objtype = ACL_OBJECT_NAMESPACE;
5449                                         n->objs = $2;
5450                                         $$ = n;
5451                                 }
5452                         | TABLESPACE name_list
5453                                 {
5454                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5455                                         n->targtype = ACL_TARGET_OBJECT;
5456                                         n->objtype = ACL_OBJECT_TABLESPACE;
5457                                         n->objs = $2;
5458                                         $$ = n;
5459                                 }
5460                         | ALL TABLES IN_P SCHEMA name_list
5461                                 {
5462                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5463                                         n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
5464                                         n->objtype = ACL_OBJECT_RELATION;
5465                                         n->objs = $5;
5466                                         $$ = n;
5467                                 }
5468                         | ALL SEQUENCES IN_P SCHEMA name_list
5469                                 {
5470                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5471                                         n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
5472                                         n->objtype = ACL_OBJECT_SEQUENCE;
5473                                         n->objs = $5;
5474                                         $$ = n;
5475                                 }
5476                         | ALL FUNCTIONS IN_P SCHEMA name_list
5477                                 {
5478                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
5479                                         n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
5480                                         n->objtype = ACL_OBJECT_FUNCTION;
5481                                         n->objs = $5;
5482                                         $$ = n;
5483                                 }
5484                 ;
5485
5486
5487 grantee_list:
5488                         grantee                                                                 { $$ = list_make1($1); }
5489                         | grantee_list ',' grantee                              { $$ = lappend($1, $3); }
5490                 ;
5491
5492 grantee:        RoleId
5493                                 {
5494                                         PrivGrantee *n = makeNode(PrivGrantee);
5495                                         /* This hack lets us avoid reserving PUBLIC as a keyword*/
5496                                         if (strcmp($1, "public") == 0)
5497                                                 n->rolname = NULL;
5498                                         else
5499                                                 n->rolname = $1;
5500                                         $$ = (Node *)n;
5501                                 }
5502                         | GROUP_P RoleId
5503                                 {
5504                                         PrivGrantee *n = makeNode(PrivGrantee);
5505                                         /* Treat GROUP PUBLIC as a synonym for PUBLIC */
5506                                         if (strcmp($2, "public") == 0)
5507                                                 n->rolname = NULL;
5508                                         else
5509                                                 n->rolname = $2;
5510                                         $$ = (Node *)n;
5511                                 }
5512                 ;
5513
5514
5515 opt_grant_grant_option:
5516                         WITH GRANT OPTION { $$ = TRUE; }
5517                         | /*EMPTY*/ { $$ = FALSE; }
5518                 ;
5519
5520 function_with_argtypes_list:
5521                         function_with_argtypes                                  { $$ = list_make1($1); }
5522                         | function_with_argtypes_list ',' function_with_argtypes
5523                                                                                                         { $$ = lappend($1, $3); }
5524                 ;
5525
5526 function_with_argtypes:
5527                         func_name func_args
5528                                 {
5529                                         FuncWithArgs *n = makeNode(FuncWithArgs);
5530                                         n->funcname = $1;
5531                                         n->funcargs = extractArgTypes($2);
5532                                         $$ = n;
5533                                 }
5534                 ;
5535
5536 /*****************************************************************************
5537  *
5538  * GRANT and REVOKE ROLE statements
5539  *
5540  *****************************************************************************/
5541
5542 GrantRoleStmt:
5543                         GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
5544                                 {
5545                                         GrantRoleStmt *n = makeNode(GrantRoleStmt);
5546                                         n->is_grant = true;
5547                                         n->granted_roles = $2;
5548                                         n->grantee_roles = $4;
5549                                         n->admin_opt = $5;
5550                                         n->grantor = $6;
5551                                         $$ = (Node*)n;
5552                                 }
5553                 ;
5554
5555 RevokeRoleStmt:
5556                         REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
5557                                 {
5558                                         GrantRoleStmt *n = makeNode(GrantRoleStmt);
5559                                         n->is_grant = false;
5560                                         n->admin_opt = false;
5561                                         n->granted_roles = $2;
5562                                         n->grantee_roles = $4;
5563                                         n->behavior = $6;
5564                                         $$ = (Node*)n;
5565                                 }
5566                         | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
5567                                 {
5568                                         GrantRoleStmt *n = makeNode(GrantRoleStmt);
5569                                         n->is_grant = false;
5570                                         n->admin_opt = true;
5571                                         n->granted_roles = $5;
5572                                         n->grantee_roles = $7;
5573                                         n->behavior = $9;
5574                                         $$ = (Node*)n;
5575                                 }
5576                 ;
5577
5578 opt_grant_admin_option: WITH ADMIN OPTION                               { $$ = TRUE; }
5579                         | /*EMPTY*/                                                                     { $$ = FALSE; }
5580                 ;
5581
5582 opt_granted_by: GRANTED BY RoleId                                               { $$ = $3; }
5583                         | /*EMPTY*/                                                                     { $$ = NULL; }
5584                 ;
5585
5586 /*****************************************************************************
5587  *
5588  * ALTER DEFAULT PRIVILEGES statement
5589  *
5590  *****************************************************************************/
5591
5592 AlterDefaultPrivilegesStmt:
5593                         ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
5594                                 {
5595                                         AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
5596                                         n->options = $4;
5597                                         n->action = (GrantStmt *) $5;
5598                                         $$ = (Node*)n;
5599                                 }
5600                 ;
5601
5602 DefACLOptionList:
5603                         DefACLOptionList DefACLOption                   { $$ = lappend($1, $2); }
5604                         | /* EMPTY */                                                   { $$ = NIL; }
5605                 ;
5606
5607 DefACLOption:
5608                         IN_P SCHEMA name_list
5609                                 {
5610                                         $$ = makeDefElem("schemas", (Node *)$3);
5611                                 }
5612                         | FOR ROLE name_list
5613                                 {
5614                                         $$ = makeDefElem("roles", (Node *)$3);
5615                                 }
5616                         | FOR USER name_list
5617                                 {
5618                                         $$ = makeDefElem("roles", (Node *)$3);
5619                                 }
5620                 ;
5621
5622 /*
5623  * This should match GRANT/REVOKE, except that individual target objects
5624  * are not mentioned and we only allow a subset of object types.
5625  */
5626 DefACLAction:
5627                         GRANT privileges ON defacl_privilege_target TO grantee_list
5628                         opt_grant_grant_option
5629                                 {
5630                                         GrantStmt *n = makeNode(GrantStmt);
5631                                         n->is_grant = true;
5632                                         n->privileges = $2;
5633                                         n->targtype = ACL_TARGET_DEFAULTS;
5634                                         n->objtype = $4;
5635                                         n->objects = NIL;
5636                                         n->grantees = $6;
5637                                         n->grant_option = $7;
5638                                         $$ = (Node*)n;
5639                                 }
5640                         | REVOKE privileges ON defacl_privilege_target
5641                         FROM grantee_list opt_drop_behavior
5642                                 {
5643                                         GrantStmt *n = makeNode(GrantStmt);
5644                                         n->is_grant = false;
5645                                         n->grant_option = false;
5646                                         n->privileges = $2;
5647                                         n->targtype = ACL_TARGET_DEFAULTS;
5648                                         n->objtype = $4;
5649                                         n->objects = NIL;
5650                                         n->grantees = $6;
5651                                         n->behavior = $7;
5652                                         $$ = (Node *)n;
5653                                 }
5654                         | REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
5655                         FROM grantee_list opt_drop_behavior
5656                                 {
5657                                         GrantStmt *n = makeNode(GrantStmt);
5658                                         n->is_grant = false;
5659                                         n->grant_option = true;
5660                                         n->privileges = $5;
5661                                         n->targtype = ACL_TARGET_DEFAULTS;
5662                                         n->objtype = $7;
5663                                         n->objects = NIL;
5664                                         n->grantees = $9;
5665                                         n->behavior = $10;
5666                                         $$ = (Node *)n;
5667                                 }
5668                 ;
5669
5670 defacl_privilege_target:
5671                         TABLES                  { $$ = ACL_OBJECT_RELATION; }
5672                         | FUNCTIONS             { $$ = ACL_OBJECT_FUNCTION; }
5673                         | SEQUENCES             { $$ = ACL_OBJECT_SEQUENCE; }
5674                 ;
5675
5676
5677 /*****************************************************************************
5678  *
5679  *              QUERY: CREATE INDEX
5680  *
5681  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
5682  * willing to make TABLESPACE a fully reserved word.
5683  *****************************************************************************/
5684
5685 IndexStmt:      CREATE opt_unique INDEX opt_concurrently opt_index_name
5686                         ON qualified_name access_method_clause '(' index_params ')'
5687                         opt_reloptions OptTableSpace where_clause
5688                                 {
5689                                         IndexStmt *n = makeNode(IndexStmt);
5690                                         n->unique = $2;
5691                                         n->concurrent = $4;
5692                                         n->idxname = $5;
5693                                         n->relation = $7;
5694                                         n->accessMethod = $8;
5695                                         n->indexParams = $10;
5696                                         n->options = $12;
5697                                         n->tableSpace = $13;
5698                                         n->whereClause = $14;
5699                                         n->indexOid = InvalidOid;
5700                                         $$ = (Node *)n;
5701                                 }
5702                 ;
5703
5704 opt_unique:
5705                         UNIQUE                                                                  { $$ = TRUE; }
5706                         | /*EMPTY*/                                                             { $$ = FALSE; }
5707                 ;
5708
5709 opt_concurrently:
5710                         CONCURRENTLY                                                    { $$ = TRUE; }
5711                         | /*EMPTY*/                                                             { $$ = FALSE; }
5712                 ;
5713
5714 opt_index_name:
5715                         index_name                                                              { $$ = $1; }
5716                         | /*EMPTY*/                                                             { $$ = NULL; }
5717                 ;
5718
5719 access_method_clause:
5720                         USING access_method                                             { $$ = $2; }
5721                         | /*EMPTY*/                                                             { $$ = DEFAULT_INDEX_TYPE; }
5722                 ;
5723
5724 index_params:   index_elem                                                      { $$ = list_make1($1); }
5725                         | index_params ',' index_elem                   { $$ = lappend($1, $3); }
5726                 ;
5727
5728 /*
5729  * Index attributes can be either simple column references, or arbitrary
5730  * expressions in parens.  For backwards-compatibility reasons, we allow
5731  * an expression that's just a function call to be written without parens.
5732  */
5733 index_elem:     ColId opt_collate opt_class opt_asc_desc opt_nulls_order
5734                                 {
5735                                         $$ = makeNode(IndexElem);
5736                                         $$->name = $1;
5737                                         $$->expr = NULL;
5738                                         $$->indexcolname = NULL;
5739                                         $$->collation = $2;
5740                                         $$->opclass = $3;
5741                                         $$->ordering = $4;
5742                                         $$->nulls_ordering = $5;
5743                                 }
5744                         | func_expr opt_collate opt_class opt_asc_desc opt_nulls_order
5745                                 {
5746                                         $$ = makeNode(IndexElem);
5747                                         $$->name = NULL;
5748                                         $$->expr = $1;
5749                                         $$->indexcolname = NULL;
5750                                         $$->collation = $2;
5751                                         $$->opclass = $3;
5752                                         $$->ordering = $4;
5753                                         $$->nulls_ordering = $5;
5754                                 }
5755                         | '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order
5756                                 {
5757                                         $$ = makeNode(IndexElem);
5758                                         $$->name = NULL;
5759                                         $$->expr = $2;
5760                                         $$->indexcolname = NULL;
5761                                         $$->collation = $4;
5762                                         $$->opclass = $5;
5763                                         $$->ordering = $6;
5764                                         $$->nulls_ordering = $7;
5765                                 }
5766                 ;
5767
5768 opt_collate: COLLATE any_name                                           { $$ = $2; }
5769                         | /*EMPTY*/                                                             { $$ = NIL; }
5770                 ;
5771
5772 opt_class:      any_name                                                                { $$ = $1; }
5773                         | USING any_name                                                { $$ = $2; }
5774                         | /*EMPTY*/                                                             { $$ = NIL; }
5775                 ;
5776
5777 opt_asc_desc: ASC                                                       { $$ = SORTBY_ASC; }
5778                         | DESC                                                  { $$ = SORTBY_DESC; }
5779                         | /*EMPTY*/                                             { $$ = SORTBY_DEFAULT; }
5780                 ;
5781
5782 opt_nulls_order: NULLS_FIRST                            { $$ = SORTBY_NULLS_FIRST; }
5783                         | NULLS_LAST                                    { $$ = SORTBY_NULLS_LAST; }
5784                         | /*EMPTY*/                                             { $$ = SORTBY_NULLS_DEFAULT; }
5785                 ;
5786
5787
5788 /*****************************************************************************
5789  *
5790  *              QUERY:
5791  *                              create [or replace] function <fname>
5792  *                                              [(<type-1> { , <type-n>})]
5793  *                                              returns <type-r>
5794  *                                              as <filename or code in language as appropriate>
5795  *                                              language <lang> [with parameters]
5796  *
5797  *****************************************************************************/
5798
5799 CreateFunctionStmt:
5800                         CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
5801                         RETURNS func_return createfunc_opt_list opt_definition
5802                                 {
5803                                         CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
5804                                         n->replace = $2;
5805                                         n->funcname = $4;
5806                                         n->parameters = $5;
5807                                         n->returnType = $7;
5808                                         n->options = $8;
5809                                         n->withClause = $9;
5810                                         $$ = (Node *)n;
5811                                 }
5812                         | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
5813                           RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
5814                                 {
5815                                         CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
5816                                         n->replace = $2;
5817                                         n->funcname = $4;
5818                                         n->parameters = mergeTableFuncParameters($5, $9);
5819                                         n->returnType = TableFuncTypeName($9);
5820                                         n->returnType->location = @7;
5821                                         n->options = $11;
5822                                         n->withClause = $12;
5823                                         $$ = (Node *)n;
5824                                 }
5825                         | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
5826                           createfunc_opt_list opt_definition
5827                                 {
5828                                         CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
5829                                         n->replace = $2;
5830                                         n->funcname = $4;
5831                                         n->parameters = $5;
5832                                         n->returnType = NULL;
5833                                         n->options = $6;
5834                                         n->withClause = $7;
5835                                         $$ = (Node *)n;
5836                                 }
5837                 ;
5838
5839 opt_or_replace:
5840                         OR REPLACE                                                              { $$ = TRUE; }
5841                         | /*EMPTY*/                                                             { $$ = FALSE; }
5842                 ;
5843
5844 func_args:      '(' func_args_list ')'                                  { $$ = $2; }
5845                         | '(' ')'                                                               { $$ = NIL; }
5846                 ;
5847
5848 func_args_list:
5849                         func_arg                                                                { $$ = list_make1($1); }
5850                         | func_args_list ',' func_arg                   { $$ = lappend($1, $3); }
5851                 ;
5852
5853 /*
5854  * func_args_with_defaults is separate because we only want to accept
5855  * defaults in CREATE FUNCTION, not in ALTER etc.
5856  */
5857 func_args_with_defaults:
5858                 '(' func_args_with_defaults_list ')'            { $$ = $2; }
5859                 | '(' ')'                                                                       { $$ = NIL; }
5860                 ;
5861
5862 func_args_with_defaults_list:
5863                 func_arg_with_default                                           { $$ = list_make1($1); }
5864                 | func_args_with_defaults_list ',' func_arg_with_default
5865                                                                                                         { $$ = lappend($1, $3); }
5866                 ;
5867
5868 /*
5869  * The style with arg_class first is SQL99 standard, but Oracle puts
5870  * param_name first; accept both since it's likely people will try both
5871  * anyway.  Don't bother trying to save productions by letting arg_class
5872  * have an empty alternative ... you'll get shift/reduce conflicts.
5873  *
5874  * We can catch over-specified arguments here if we want to,
5875  * but for now better to silently swallow typmod, etc.
5876  * - thomas 2000-03-22
5877  */
5878 func_arg:
5879                         arg_class param_name func_type
5880                                 {
5881                                         FunctionParameter *n = makeNode(FunctionParameter);
5882                                         n->name = $2;
5883                                         n->argType = $3;
5884                                         n->mode = $1;
5885                                         n->defexpr = NULL;
5886                                         $$ = n;
5887                                 }
5888                         | param_name arg_class func_type
5889                                 {
5890                                         FunctionParameter *n = makeNode(FunctionParameter);
5891                                         n->name = $1;
5892                                         n->argType = $3;
5893                                         n->mode = $2;
5894                                         n->defexpr = NULL;
5895                                         $$ = n;
5896                                 }
5897                         | param_name func_type
5898                                 {
5899                                         FunctionParameter *n = makeNode(FunctionParameter);
5900                                         n->name = $1;
5901                                         n->argType = $2;
5902                                         n->mode = FUNC_PARAM_IN;
5903                                         n->defexpr = NULL;
5904                                         $$ = n;
5905                                 }
5906                         | arg_class func_type
5907                                 {
5908                                         FunctionParameter *n = makeNode(FunctionParameter);
5909                                         n->name = NULL;
5910                                         n->argType = $2;
5911                                         n->mode = $1;
5912                                         n->defexpr = NULL;
5913                                         $$ = n;
5914                                 }
5915                         | func_type
5916                                 {
5917                                         FunctionParameter *n = makeNode(FunctionParameter);
5918                                         n->name = NULL;
5919                                         n->argType = $1;
5920                                         n->mode = FUNC_PARAM_IN;
5921                                         n->defexpr = NULL;
5922                                         $$ = n;
5923                                 }
5924                 ;
5925
5926 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
5927 arg_class:      IN_P                                                            { $$ = FUNC_PARAM_IN; }
5928                         | OUT_P                                                         { $$ = FUNC_PARAM_OUT; }
5929                         | INOUT                                                         { $$ = FUNC_PARAM_INOUT; }
5930                         | IN_P OUT_P                                            { $$ = FUNC_PARAM_INOUT; }
5931                         | VARIADIC                                                      { $$ = FUNC_PARAM_VARIADIC; }
5932                 ;
5933
5934 /*
5935  * Ideally param_name should be ColId, but that causes too many conflicts.
5936  */
5937 param_name:     type_function_name
5938                 ;
5939
5940 func_return:
5941                         func_type
5942                                 {
5943                                         /* We can catch over-specified results here if we want to,
5944                                          * but for now better to silently swallow typmod, etc.
5945                                          * - thomas 2000-03-22
5946                                          */
5947                                         $$ = $1;
5948                                 }
5949                 ;
5950
5951 /*
5952  * We would like to make the %TYPE productions here be ColId attrs etc,
5953  * but that causes reduce/reduce conflicts.  type_function_name
5954  * is next best choice.
5955  */
5956 func_type:      Typename                                                                { $$ = $1; }
5957                         | type_function_name attrs '%' TYPE_P
5958                                 {
5959                                         $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
5960                                         $$->pct_type = true;
5961                                         $$->location = @1;
5962                                 }
5963                         | SETOF type_function_name attrs '%' TYPE_P
5964                                 {
5965                                         $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
5966                                         $$->pct_type = true;
5967                                         $$->setof = TRUE;
5968                                         $$->location = @2;
5969                                 }
5970                 ;
5971
5972 func_arg_with_default:
5973                 func_arg
5974                             {
5975                                     $$ = $1;
5976                             }
5977                 | func_arg DEFAULT a_expr
5978                             {
5979                                     $$ = $1;
5980                                     $$->defexpr = $3;
5981                             }
5982                 | func_arg '=' a_expr
5983                             {
5984                                     $$ = $1;
5985                                     $$->defexpr = $3;
5986                             }
5987                 ;
5988
5989
5990 createfunc_opt_list:
5991                         /* Must be at least one to prevent conflict */
5992                         createfunc_opt_item                     { $$ = list_make1($1); }
5993                         | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
5994         ;
5995
5996 /*
5997  * Options common to both CREATE FUNCTION and ALTER FUNCTION
5998  */
5999 common_func_opt_item:
6000                         CALLED ON NULL_P INPUT_P
6001                                 {
6002                                         $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
6003                                 }
6004                         | RETURNS NULL_P ON NULL_P INPUT_P
6005                                 {
6006                                         $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
6007                                 }
6008                         | STRICT_P
6009                                 {
6010                                         $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
6011                                 }
6012                         | IMMUTABLE
6013                                 {
6014                                         $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
6015                                 }
6016                         | STABLE
6017                                 {
6018                                         $$ = makeDefElem("volatility", (Node *)makeString("stable"));
6019                                 }
6020                         | VOLATILE
6021                                 {
6022                                         $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
6023                                 }
6024                         | EXTERNAL SECURITY DEFINER
6025                                 {
6026                                         $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
6027                                 }
6028                         | EXTERNAL SECURITY INVOKER
6029                                 {
6030                                         $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
6031                                 }
6032                         | SECURITY DEFINER
6033                                 {
6034                                         $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
6035                                 }
6036                         | SECURITY INVOKER
6037                                 {
6038                                         $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
6039                                 }
6040                         | COST NumericOnly
6041                                 {
6042                                         $$ = makeDefElem("cost", (Node *)$2);
6043                                 }
6044                         | ROWS NumericOnly
6045                                 {
6046                                         $$ = makeDefElem("rows", (Node *)$2);
6047                                 }
6048                         | SetResetClause
6049                                 {
6050                                         /* we abuse the normal content of a DefElem here */
6051                                         $$ = makeDefElem("set", (Node *)$1);
6052                                 }
6053                 ;
6054
6055 createfunc_opt_item:
6056                         AS func_as
6057                                 {
6058                                         $$ = makeDefElem("as", (Node *)$2);
6059                                 }
6060                         | LANGUAGE ColId_or_Sconst
6061                                 {
6062                                         $$ = makeDefElem("language", (Node *)makeString($2));
6063                                 }
6064                         | WINDOW
6065                                 {
6066                                         $$ = makeDefElem("window", (Node *)makeInteger(TRUE));
6067                                 }
6068                         | common_func_opt_item
6069                                 {
6070                                         $$ = $1;
6071                                 }
6072                 ;
6073
6074 func_as:        Sconst                                          { $$ = list_make1(makeString($1)); }
6075                         | Sconst ',' Sconst
6076                                 {
6077                                         $$ = list_make2(makeString($1), makeString($3));
6078                                 }
6079                 ;
6080
6081 opt_definition:
6082                         WITH definition                                                 { $$ = $2; }
6083                         | /*EMPTY*/                                                             { $$ = NIL; }
6084                 ;
6085
6086 table_func_column:      param_name func_type
6087                                 {
6088                                         FunctionParameter *n = makeNode(FunctionParameter);
6089                                         n->name = $1;
6090                                         n->argType = $2;
6091                                         n->mode = FUNC_PARAM_TABLE;
6092                                         n->defexpr = NULL;
6093                                         $$ = n;
6094                                 }
6095                 ;
6096
6097 table_func_column_list:
6098                         table_func_column
6099                                 {
6100                                         $$ = list_make1($1);
6101                                 }
6102                         | table_func_column_list ',' table_func_column
6103                                 {
6104                                         $$ = lappend($1, $3);
6105                                 }
6106                 ;
6107
6108 /*****************************************************************************
6109  * ALTER FUNCTION
6110  *
6111  * RENAME and OWNER subcommands are already provided by the generic
6112  * ALTER infrastructure, here we just specify alterations that can
6113  * only be applied to functions.
6114  *
6115  *****************************************************************************/
6116 AlterFunctionStmt:
6117                         ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
6118                                 {
6119                                         AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
6120                                         n->func = $3;
6121                                         n->actions = $4;
6122                                         $$ = (Node *) n;
6123                                 }
6124                 ;
6125
6126 alterfunc_opt_list:
6127                         /* At least one option must be specified */
6128                         common_func_opt_item                                    { $$ = list_make1($1); }
6129                         | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
6130                 ;
6131
6132 /* Ignored, merely for SQL compliance */
6133 opt_restrict:
6134                         RESTRICT
6135                         | /* EMPTY */
6136                 ;
6137
6138
6139 /*****************************************************************************
6140  *
6141  *              QUERY:
6142  *
6143  *              DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
6144  *              DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
6145  *              DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
6146  *
6147  *****************************************************************************/
6148
6149 RemoveFuncStmt:
6150                         DROP FUNCTION func_name func_args opt_drop_behavior
6151                                 {
6152                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
6153                                         n->kind = OBJECT_FUNCTION;
6154                                         n->name = $3;
6155                                         n->args = extractArgTypes($4);
6156                                         n->behavior = $5;
6157                                         n->missing_ok = false;
6158                                         $$ = (Node *)n;
6159                                 }
6160                         | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
6161                                 {
6162                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
6163                                         n->kind = OBJECT_FUNCTION;
6164                                         n->name = $5;
6165                                         n->args = extractArgTypes($6);
6166                                         n->behavior = $7;
6167                                         n->missing_ok = true;
6168                                         $$ = (Node *)n;
6169                                 }
6170                 ;
6171
6172 RemoveAggrStmt:
6173                         DROP AGGREGATE func_name aggr_args opt_drop_behavior
6174                                 {
6175                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
6176                                         n->kind = OBJECT_AGGREGATE;
6177                                         n->name = $3;
6178                                         n->args = $4;
6179                                         n->behavior = $5;
6180                                         n->missing_ok = false;
6181                                         $$ = (Node *)n;
6182                                 }
6183                         | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
6184                                 {
6185                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
6186                                         n->kind = OBJECT_AGGREGATE;
6187                                         n->name = $5;
6188                                         n->args = $6;
6189                                         n->behavior = $7;
6190                                         n->missing_ok = true;
6191                                         $$ = (Node *)n;
6192                                 }
6193                 ;
6194
6195 RemoveOperStmt:
6196                         DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
6197                                 {
6198                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
6199                                         n->kind = OBJECT_OPERATOR;
6200                                         n->name = $3;
6201                                         n->args = $4;
6202                                         n->behavior = $5;
6203                                         n->missing_ok = false;
6204                                         $$ = (Node *)n;
6205                                 }
6206                         | DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
6207                                 {
6208                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
6209                                         n->kind = OBJECT_OPERATOR;
6210                                         n->name = $5;
6211                                         n->args = $6;
6212                                         n->behavior = $7;
6213                                         n->missing_ok = true;
6214                                         $$ = (Node *)n;
6215                                 }
6216                 ;
6217
6218 oper_argtypes:
6219                         '(' Typename ')'
6220                                 {
6221                                    ereport(ERROR,
6222                                                    (errcode(ERRCODE_SYNTAX_ERROR),
6223                                                         errmsg("missing argument"),
6224                                                         errhint("Use NONE to denote the missing argument of a unary operator."),
6225                                                         parser_errposition(@3)));
6226                                 }
6227                         | '(' Typename ',' Typename ')'
6228                                         { $$ = list_make2($2, $4); }
6229                         | '(' NONE ',' Typename ')'                                     /* left unary */
6230                                         { $$ = list_make2(NULL, $4); }
6231                         | '(' Typename ',' NONE ')'                                     /* right unary */
6232                                         { $$ = list_make2($2, NULL); }
6233                 ;
6234
6235 any_operator:
6236                         all_Op
6237                                         { $$ = list_make1(makeString($1)); }
6238                         | ColId '.' any_operator
6239                                         { $$ = lcons(makeString($1), $3); }
6240                 ;
6241
6242 /*****************************************************************************
6243  *
6244  *              DO <anonymous code block> [ LANGUAGE language ]
6245  *
6246  * We use a DefElem list for future extensibility, and to allow flexibility
6247  * in the clause order.
6248  *
6249  *****************************************************************************/
6250
6251 DoStmt: DO dostmt_opt_list
6252                                 {
6253                                         DoStmt *n = makeNode(DoStmt);
6254                                         n->args = $2;
6255                                         $$ = (Node *)n;
6256                                 }
6257                 ;
6258
6259 dostmt_opt_list:
6260                         dostmt_opt_item                                         { $$ = list_make1($1); }
6261                         | dostmt_opt_list dostmt_opt_item       { $$ = lappend($1, $2); }
6262                 ;
6263
6264 dostmt_opt_item:
6265                         Sconst
6266                                 {
6267                                         $$ = makeDefElem("as", (Node *)makeString($1));
6268                                 }
6269                         | LANGUAGE ColId_or_Sconst
6270                                 {
6271                                         $$ = makeDefElem("language", (Node *)makeString($2));
6272                                 }
6273                 ;
6274
6275 /*****************************************************************************
6276  *
6277  *              CREATE CAST / DROP CAST
6278  *
6279  *****************************************************************************/
6280
6281 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
6282                                         WITH FUNCTION function_with_argtypes cast_context
6283                                 {
6284                                         CreateCastStmt *n = makeNode(CreateCastStmt);
6285                                         n->sourcetype = $4;
6286                                         n->targettype = $6;
6287                                         n->func = $10;
6288                                         n->context = (CoercionContext) $11;
6289                                         n->inout = false;
6290                                         $$ = (Node *)n;
6291                                 }
6292                         | CREATE CAST '(' Typename AS Typename ')'
6293                                         WITHOUT FUNCTION cast_context
6294                                 {
6295                                         CreateCastStmt *n = makeNode(CreateCastStmt);
6296                                         n->sourcetype = $4;
6297                                         n->targettype = $6;
6298                                         n->func = NULL;
6299                                         n->context = (CoercionContext) $10;
6300                                         n->inout = false;
6301                                         $$ = (Node *)n;
6302                                 }
6303                         | CREATE CAST '(' Typename AS Typename ')'
6304                                         WITH INOUT cast_context
6305                                 {
6306                                         CreateCastStmt *n = makeNode(CreateCastStmt);
6307                                         n->sourcetype = $4;
6308                                         n->targettype = $6;
6309                                         n->func = NULL;
6310                                         n->context = (CoercionContext) $10;
6311                                         n->inout = true;
6312                                         $$ = (Node *)n;
6313                                 }
6314                 ;
6315
6316 cast_context:  AS IMPLICIT_P                                    { $$ = COERCION_IMPLICIT; }
6317                 | AS ASSIGNMENT                                                 { $$ = COERCION_ASSIGNMENT; }
6318                 | /*EMPTY*/                                                             { $$ = COERCION_EXPLICIT; }
6319                 ;
6320
6321
6322 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
6323                                 {
6324                                         DropCastStmt *n = makeNode(DropCastStmt);
6325                                         n->sourcetype = $5;
6326                                         n->targettype = $7;
6327                                         n->behavior = $9;
6328                                         n->missing_ok = $3;
6329                                         $$ = (Node *)n;
6330                                 }
6331                 ;
6332
6333 opt_if_exists: IF_P EXISTS                                              { $$ = TRUE; }
6334                 | /*EMPTY*/                                                             { $$ = FALSE; }
6335                 ;
6336
6337
6338 /*****************************************************************************
6339  *
6340  *              QUERY:
6341  *
6342  *              REINDEX type <name> [FORCE]
6343  *
6344  * FORCE no longer does anything, but we accept it for backwards compatibility
6345  *****************************************************************************/
6346
6347 ReindexStmt:
6348                         REINDEX reindex_type qualified_name opt_force
6349                                 {
6350                                         ReindexStmt *n = makeNode(ReindexStmt);
6351                                         n->kind = $2;
6352                                         n->relation = $3;
6353                                         n->name = NULL;
6354                                         $$ = (Node *)n;
6355                                 }
6356                         | REINDEX SYSTEM_P name opt_force
6357                                 {
6358                                         ReindexStmt *n = makeNode(ReindexStmt);
6359                                         n->kind = OBJECT_DATABASE;
6360                                         n->name = $3;
6361                                         n->relation = NULL;
6362                                         n->do_system = true;
6363                                         n->do_user = false;
6364                                         $$ = (Node *)n;
6365                                 }
6366                         | REINDEX DATABASE name opt_force
6367                                 {
6368                                         ReindexStmt *n = makeNode(ReindexStmt);
6369                                         n->kind = OBJECT_DATABASE;
6370                                         n->name = $3;
6371                                         n->relation = NULL;
6372                                         n->do_system = true;
6373                                         n->do_user = true;
6374                                         $$ = (Node *)n;
6375                                 }
6376                 ;
6377
6378 reindex_type:
6379                         INDEX                                                                   { $$ = OBJECT_INDEX; }
6380                         | TABLE                                                                 { $$ = OBJECT_TABLE; }
6381                 ;
6382
6383 opt_force:      FORCE                                                                   {  $$ = TRUE; }
6384                         | /* EMPTY */                                                   {  $$ = FALSE; }
6385                 ;
6386
6387
6388 /*****************************************************************************
6389  *
6390  * ALTER THING name RENAME TO newname
6391  *
6392  *****************************************************************************/
6393
6394 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
6395                                 {
6396                                         RenameStmt *n = makeNode(RenameStmt);
6397                                         n->renameType = OBJECT_AGGREGATE;
6398                                         n->object = $3;
6399                                         n->objarg = $4;
6400                                         n->newname = $7;
6401                                         $$ = (Node *)n;
6402                                 }
6403                         | ALTER COLLATION any_name RENAME TO name
6404                                 {
6405                                         RenameStmt *n = makeNode(RenameStmt);
6406                                         n->renameType = OBJECT_COLLATION;
6407                                         n->object = $3;
6408                                         n->newname = $6;
6409                                         $$ = (Node *)n;
6410                                 }
6411                         | ALTER CONVERSION_P any_name RENAME TO name
6412                                 {
6413                                         RenameStmt *n = makeNode(RenameStmt);
6414                                         n->renameType = OBJECT_CONVERSION;
6415                                         n->object = $3;
6416                                         n->newname = $6;
6417                                         $$ = (Node *)n;
6418                                 }
6419                         | ALTER DATABASE database_name RENAME TO database_name
6420                                 {
6421                                         RenameStmt *n = makeNode(RenameStmt);
6422                                         n->renameType = OBJECT_DATABASE;
6423                                         n->subname = $3;
6424                                         n->newname = $6;
6425                                         $$ = (Node *)n;
6426                                 }
6427                         | ALTER FUNCTION function_with_argtypes RENAME TO name
6428                                 {
6429                                         RenameStmt *n = makeNode(RenameStmt);
6430                                         n->renameType = OBJECT_FUNCTION;
6431                                         n->object = $3->funcname;
6432                                         n->objarg = $3->funcargs;
6433                                         n->newname = $6;
6434                                         $$ = (Node *)n;
6435                                 }
6436                         | ALTER GROUP_P RoleId RENAME TO RoleId
6437                                 {
6438                                         RenameStmt *n = makeNode(RenameStmt);
6439                                         n->renameType = OBJECT_ROLE;
6440                                         n->subname = $3;
6441                                         n->newname = $6;
6442                                         $$ = (Node *)n;
6443                                 }
6444                         | ALTER opt_procedural LANGUAGE name RENAME TO name
6445                                 {
6446                                         RenameStmt *n = makeNode(RenameStmt);
6447                                         n->renameType = OBJECT_LANGUAGE;
6448                                         n->subname = $4;
6449                                         n->newname = $7;
6450                                         $$ = (Node *)n;
6451                                 }
6452                         | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
6453                                 {
6454                                         RenameStmt *n = makeNode(RenameStmt);
6455                                         n->renameType = OBJECT_OPCLASS;
6456                                         n->object = $4;
6457                                         n->subname = $6;
6458                                         n->newname = $9;
6459                                         $$ = (Node *)n;
6460                                 }
6461                         | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
6462                                 {
6463                                         RenameStmt *n = makeNode(RenameStmt);
6464                                         n->renameType = OBJECT_OPFAMILY;
6465                                         n->object = $4;
6466                                         n->subname = $6;
6467                                         n->newname = $9;
6468                                         $$ = (Node *)n;
6469                                 }
6470                         | ALTER SCHEMA name RENAME TO name
6471                                 {
6472                                         RenameStmt *n = makeNode(RenameStmt);
6473                                         n->renameType = OBJECT_SCHEMA;
6474                                         n->subname = $3;
6475                                         n->newname = $6;
6476                                         $$ = (Node *)n;
6477                                 }
6478                         | ALTER TABLE relation_expr RENAME TO name
6479                                 {
6480                                         RenameStmt *n = makeNode(RenameStmt);
6481                                         n->renameType = OBJECT_TABLE;
6482                                         n->relation = $3;
6483                                         n->subname = NULL;
6484                                         n->newname = $6;
6485                                         $$ = (Node *)n;
6486                                 }
6487                         | ALTER SEQUENCE qualified_name RENAME TO name
6488                                 {
6489                                         RenameStmt *n = makeNode(RenameStmt);
6490                                         n->renameType = OBJECT_SEQUENCE;
6491                                         n->relation = $3;
6492                                         n->subname = NULL;
6493                                         n->newname = $6;
6494                                         $$ = (Node *)n;
6495                                 }
6496                         | ALTER VIEW qualified_name RENAME TO name
6497                                 {
6498                                         RenameStmt *n = makeNode(RenameStmt);
6499                                         n->renameType = OBJECT_VIEW;
6500                                         n->relation = $3;
6501                                         n->subname = NULL;
6502                                         n->newname = $6;
6503                                         $$ = (Node *)n;
6504                                 }
6505                         | ALTER INDEX qualified_name RENAME TO name
6506                                 {
6507                                         RenameStmt *n = makeNode(RenameStmt);
6508                                         n->renameType = OBJECT_INDEX;
6509                                         n->relation = $3;
6510                                         n->subname = NULL;
6511                                         n->newname = $6;
6512                                         $$ = (Node *)n;
6513                                 }
6514                         | ALTER FOREIGN TABLE relation_expr RENAME TO name
6515                                 {
6516                                         RenameStmt *n = makeNode(RenameStmt);
6517                                         n->renameType = OBJECT_FOREIGN_TABLE;
6518                                         n->relation = $4;
6519                                         n->subname = NULL;
6520                                         n->newname = $7;
6521                                         $$ = (Node *)n;
6522                                 }
6523                         | ALTER TABLE relation_expr RENAME opt_column name TO name
6524                                 {
6525                                         RenameStmt *n = makeNode(RenameStmt);
6526                                         n->renameType = OBJECT_COLUMN;
6527                                         n->relationType = OBJECT_TABLE;
6528                                         n->relation = $3;
6529                                         n->subname = $6;
6530                                         n->newname = $8;
6531                                         $$ = (Node *)n;
6532                                 }
6533                         | ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
6534                                 {
6535                                         RenameStmt *n = makeNode(RenameStmt);
6536                                         n->renameType = OBJECT_COLUMN;
6537                                         n->relationType = OBJECT_FOREIGN_TABLE;
6538                                         n->relation = $4;
6539                                         n->subname = $7;
6540                                         n->newname = $9;
6541                                         $$ = (Node *)n;
6542                                 }
6543                         | ALTER TRIGGER name ON qualified_name RENAME TO name
6544                                 {
6545                                         RenameStmt *n = makeNode(RenameStmt);
6546                                         n->renameType = OBJECT_TRIGGER;
6547                                         n->relation = $5;
6548                                         n->subname = $3;
6549                                         n->newname = $8;
6550                                         $$ = (Node *)n;
6551                                 }
6552                         | ALTER ROLE RoleId RENAME TO RoleId
6553                                 {
6554                                         RenameStmt *n = makeNode(RenameStmt);
6555                                         n->renameType = OBJECT_ROLE;
6556                                         n->subname = $3;
6557                                         n->newname = $6;
6558                                         $$ = (Node *)n;
6559                                 }
6560                         | ALTER USER RoleId RENAME TO RoleId
6561                                 {
6562                                         RenameStmt *n = makeNode(RenameStmt);
6563                                         n->renameType = OBJECT_ROLE;
6564                                         n->subname = $3;
6565                                         n->newname = $6;
6566                                         $$ = (Node *)n;
6567                                 }
6568                         | ALTER TABLESPACE name RENAME TO name
6569                                 {
6570                                         RenameStmt *n = makeNode(RenameStmt);
6571                                         n->renameType = OBJECT_TABLESPACE;
6572                                         n->subname = $3;
6573                                         n->newname = $6;
6574                                         $$ = (Node *)n;
6575                                 }
6576                         | ALTER TABLESPACE name SET reloptions
6577                                 {
6578                                         AlterTableSpaceOptionsStmt *n =
6579                                                 makeNode(AlterTableSpaceOptionsStmt);
6580                                         n->tablespacename = $3;
6581                                         n->options = $5;
6582                                         n->isReset = FALSE;
6583                                         $$ = (Node *)n;
6584                                 }
6585                         | ALTER TABLESPACE name RESET reloptions
6586                                 {
6587                                         AlterTableSpaceOptionsStmt *n =
6588                                                 makeNode(AlterTableSpaceOptionsStmt);
6589                                         n->tablespacename = $3;
6590                                         n->options = $5;
6591                                         n->isReset = TRUE;
6592                                         $$ = (Node *)n;
6593                                 }
6594                         | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
6595                                 {
6596                                         RenameStmt *n = makeNode(RenameStmt);
6597                                         n->renameType = OBJECT_TSPARSER;
6598                                         n->object = $5;
6599                                         n->newname = $8;
6600                                         $$ = (Node *)n;
6601                                 }
6602                         | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
6603                                 {
6604                                         RenameStmt *n = makeNode(RenameStmt);
6605                                         n->renameType = OBJECT_TSDICTIONARY;
6606                                         n->object = $5;
6607                                         n->newname = $8;
6608                                         $$ = (Node *)n;
6609                                 }
6610                         | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
6611                                 {
6612                                         RenameStmt *n = makeNode(RenameStmt);
6613                                         n->renameType = OBJECT_TSTEMPLATE;
6614                                         n->object = $5;
6615                                         n->newname = $8;
6616                                         $$ = (Node *)n;
6617                                 }
6618                         | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
6619                                 {
6620                                         RenameStmt *n = makeNode(RenameStmt);
6621                                         n->renameType = OBJECT_TSCONFIGURATION;
6622                                         n->object = $5;
6623                                         n->newname = $8;
6624                                         $$ = (Node *)n;
6625                                 }
6626                         | ALTER TYPE_P any_name RENAME TO name
6627                                 {
6628                                         RenameStmt *n = makeNode(RenameStmt);
6629                                         n->renameType = OBJECT_TYPE;
6630                                         n->object = $3;
6631                                         n->newname = $6;
6632                                         $$ = (Node *)n;
6633                                 }
6634                         | ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
6635                                 {
6636                                         RenameStmt *n = makeNode(RenameStmt);
6637                                         n->renameType = OBJECT_ATTRIBUTE;
6638                                         n->relationType = OBJECT_TYPE;
6639                                         n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
6640                                         n->subname = $6;
6641                                         n->newname = $8;
6642                                         n->behavior = $9;
6643                                         $$ = (Node *)n;
6644                                 }
6645                 ;
6646
6647 opt_column: COLUMN                                                                      { $$ = COLUMN; }
6648                         | /*EMPTY*/                                                             { $$ = 0; }
6649                 ;
6650
6651 opt_set_data: SET DATA_P                                                        { $$ = 1; }
6652                         | /*EMPTY*/                                                             { $$ = 0; }
6653                 ;
6654
6655 /*****************************************************************************
6656  *
6657  * ALTER THING name SET SCHEMA name
6658  *
6659  *****************************************************************************/
6660
6661 AlterObjectSchemaStmt:
6662                         ALTER AGGREGATE func_name aggr_args SET SCHEMA name
6663                                 {
6664                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6665                                         n->objectType = OBJECT_AGGREGATE;
6666                                         n->object = $3;
6667                                         n->objarg = $4;
6668                                         n->newschema = $7;
6669                                         $$ = (Node *)n;
6670                                 }
6671                         | ALTER COLLATION any_name SET SCHEMA name
6672                                 {
6673                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6674                                         n->objectType = OBJECT_COLLATION;
6675                                         n->object = $3;
6676                                         n->newschema = $6;
6677                                         $$ = (Node *)n;
6678                                 }
6679                         | ALTER CONVERSION_P any_name SET SCHEMA name
6680                                 {
6681                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6682                                         n->objectType = OBJECT_CONVERSION;
6683                                         n->object = $3;
6684                                         n->newschema = $6;
6685                                         $$ = (Node *)n;
6686                                 }
6687                         | ALTER DOMAIN_P any_name SET SCHEMA name
6688                                 {
6689                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6690                                         n->objectType = OBJECT_DOMAIN;
6691                                         n->object = $3;
6692                                         n->newschema = $6;
6693                                         $$ = (Node *)n;
6694                                 }
6695                         | ALTER EXTENSION any_name SET SCHEMA name
6696                                 {
6697                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6698                                         n->objectType = OBJECT_EXTENSION;
6699                                         n->object = $3;
6700                                         n->newschema = $6;
6701                                         $$ = (Node *)n;
6702                                 }
6703                         | ALTER FUNCTION function_with_argtypes SET SCHEMA name
6704                                 {
6705                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6706                                         n->objectType = OBJECT_FUNCTION;
6707                                         n->object = $3->funcname;
6708                                         n->objarg = $3->funcargs;
6709                                         n->newschema = $6;
6710                                         $$ = (Node *)n;
6711                                 }
6712                         | ALTER OPERATOR any_operator oper_argtypes SET SCHEMA name
6713                                 {
6714                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6715                                         n->objectType = OBJECT_OPERATOR;
6716                                         n->object = $3;
6717                                         n->objarg = $4;
6718                                         n->newschema = $7;
6719                                         $$ = (Node *)n;
6720                                 }
6721                         | ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
6722                                 {
6723                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6724                                         n->objectType = OBJECT_OPCLASS;
6725                                         n->object = $4;
6726                                         n->addname = $6;
6727                                         n->newschema = $9;
6728                                         $$ = (Node *)n;
6729                                 }
6730                         | ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
6731                                 {
6732                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6733                                         n->objectType = OBJECT_OPFAMILY;
6734                                         n->object = $4;
6735                                         n->addname = $6;
6736                                         n->newschema = $9;
6737                                         $$ = (Node *)n;
6738                                 }
6739                         | ALTER TABLE relation_expr SET SCHEMA name
6740                                 {
6741                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6742                                         n->objectType = OBJECT_TABLE;
6743                                         n->relation = $3;
6744                                         n->newschema = $6;
6745                                         $$ = (Node *)n;
6746                                 }
6747                         | ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
6748                                 {
6749                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6750                                         n->objectType = OBJECT_TSPARSER;
6751                                         n->object = $5;
6752                                         n->newschema = $8;
6753                                         $$ = (Node *)n;
6754                                 }
6755                         | ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
6756                                 {
6757                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6758                                         n->objectType = OBJECT_TSDICTIONARY;
6759                                         n->object = $5;
6760                                         n->newschema = $8;
6761                                         $$ = (Node *)n;
6762                                 }
6763                         | ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
6764                                 {
6765                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6766                                         n->objectType = OBJECT_TSTEMPLATE;
6767                                         n->object = $5;
6768                                         n->newschema = $8;
6769                                         $$ = (Node *)n;
6770                                 }
6771                         | ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
6772                                 {
6773                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6774                                         n->objectType = OBJECT_TSCONFIGURATION;
6775                                         n->object = $5;
6776                                         n->newschema = $8;
6777                                         $$ = (Node *)n;
6778                                 }
6779                         | ALTER SEQUENCE qualified_name SET SCHEMA name
6780                                 {
6781                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6782                                         n->objectType = OBJECT_SEQUENCE;
6783                                         n->relation = $3;
6784                                         n->newschema = $6;
6785                                         $$ = (Node *)n;
6786                                 }
6787                         | ALTER VIEW qualified_name SET SCHEMA name
6788                                 {
6789                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6790                                         n->objectType = OBJECT_VIEW;
6791                                         n->relation = $3;
6792                                         n->newschema = $6;
6793                                         $$ = (Node *)n;
6794                                 }
6795                         | ALTER FOREIGN TABLE relation_expr SET SCHEMA name
6796                                 {
6797                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6798                                         n->objectType = OBJECT_FOREIGN_TABLE;
6799                                         n->relation = $4;
6800                                         n->newschema = $7;
6801                                         $$ = (Node *)n;
6802                                 }
6803                         | ALTER TYPE_P any_name SET SCHEMA name
6804                                 {
6805                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
6806                                         n->objectType = OBJECT_TYPE;
6807                                         n->object = $3;
6808                                         n->newschema = $6;
6809                                         $$ = (Node *)n;
6810                                 }
6811                 ;
6812
6813 /*****************************************************************************
6814  *
6815  * ALTER THING name OWNER TO newname
6816  *
6817  *****************************************************************************/
6818
6819 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
6820                                 {
6821                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6822                                         n->objectType = OBJECT_AGGREGATE;
6823                                         n->object = $3;
6824                                         n->objarg = $4;
6825                                         n->newowner = $7;
6826                                         $$ = (Node *)n;
6827                                 }
6828                         | ALTER COLLATION any_name OWNER TO RoleId
6829                                 {
6830                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6831                                         n->objectType = OBJECT_COLLATION;
6832                                         n->object = $3;
6833                                         n->newowner = $6;
6834                                         $$ = (Node *)n;
6835                                 }
6836                         | ALTER CONVERSION_P any_name OWNER TO RoleId
6837                                 {
6838                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6839                                         n->objectType = OBJECT_CONVERSION;
6840                                         n->object = $3;
6841                                         n->newowner = $6;
6842                                         $$ = (Node *)n;
6843                                 }
6844                         | ALTER DATABASE database_name OWNER TO RoleId
6845                                 {
6846                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6847                                         n->objectType = OBJECT_DATABASE;
6848                                         n->object = list_make1(makeString($3));
6849                                         n->newowner = $6;
6850                                         $$ = (Node *)n;
6851                                 }
6852                         | ALTER DOMAIN_P any_name OWNER TO RoleId
6853                                 {
6854                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6855                                         n->objectType = OBJECT_DOMAIN;
6856                                         n->object = $3;
6857                                         n->newowner = $6;
6858                                         $$ = (Node *)n;
6859                                 }
6860                         | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
6861                                 {
6862                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6863                                         n->objectType = OBJECT_FUNCTION;
6864                                         n->object = $3->funcname;
6865                                         n->objarg = $3->funcargs;
6866                                         n->newowner = $6;
6867                                         $$ = (Node *)n;
6868                                 }
6869                         | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
6870                                 {
6871                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6872                                         n->objectType = OBJECT_LANGUAGE;
6873                                         n->object = list_make1(makeString($4));
6874                                         n->newowner = $7;
6875                                         $$ = (Node *)n;
6876                                 }
6877                         | ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleId
6878                                 {
6879                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6880                                         n->objectType = OBJECT_LARGEOBJECT;
6881                                         n->object = list_make1($4);
6882                                         n->newowner = $7;
6883                                         $$ = (Node *)n;
6884                                 }
6885                         | ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleId
6886                                 {
6887                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6888                                         n->objectType = OBJECT_OPERATOR;
6889                                         n->object = $3;
6890                                         n->objarg = $4;
6891                                         n->newowner = $7;
6892                                         $$ = (Node *)n;
6893                                 }
6894                         | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
6895                                 {
6896                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6897                                         n->objectType = OBJECT_OPCLASS;
6898                                         n->object = $4;
6899                                         n->addname = $6;
6900                                         n->newowner = $9;
6901                                         $$ = (Node *)n;
6902                                 }
6903                         | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
6904                                 {
6905                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6906                                         n->objectType = OBJECT_OPFAMILY;
6907                                         n->object = $4;
6908                                         n->addname = $6;
6909                                         n->newowner = $9;
6910                                         $$ = (Node *)n;
6911                                 }
6912                         | ALTER SCHEMA name OWNER TO RoleId
6913                                 {
6914                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6915                                         n->objectType = OBJECT_SCHEMA;
6916                                         n->object = list_make1(makeString($3));
6917                                         n->newowner = $6;
6918                                         $$ = (Node *)n;
6919                                 }
6920                         | ALTER TYPE_P any_name OWNER TO RoleId
6921                                 {
6922                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6923                                         n->objectType = OBJECT_TYPE;
6924                                         n->object = $3;
6925                                         n->newowner = $6;
6926                                         $$ = (Node *)n;
6927                                 }
6928                         | ALTER TABLESPACE name OWNER TO RoleId
6929                                 {
6930                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6931                                         n->objectType = OBJECT_TABLESPACE;
6932                                         n->object = list_make1(makeString($3));
6933                                         n->newowner = $6;
6934                                         $$ = (Node *)n;
6935                                 }
6936                         | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
6937                                 {
6938                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6939                                         n->objectType = OBJECT_TSDICTIONARY;
6940                                         n->object = $5;
6941                                         n->newowner = $8;
6942                                         $$ = (Node *)n;
6943                                 }
6944                         | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
6945                                 {
6946                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6947                                         n->objectType = OBJECT_TSCONFIGURATION;
6948                                         n->object = $5;
6949                                         n->newowner = $8;
6950                                         $$ = (Node *)n;
6951                                 }
6952                         | ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleId
6953                                 {
6954                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6955                                         n->objectType = OBJECT_FDW;
6956                                         n->object = list_make1(makeString($5));
6957                                         n->newowner = $8;
6958                                         $$ = (Node *)n;
6959                                 }
6960                         | ALTER SERVER name OWNER TO RoleId
6961                                 {
6962                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6963                                         n->objectType = OBJECT_FOREIGN_SERVER;
6964                                         n->object = list_make1(makeString($3));
6965                                         n->newowner = $6;
6966                                         $$ = (Node *)n;
6967                                 }
6968                 ;
6969
6970
6971 /*****************************************************************************
6972  *
6973  *              QUERY:  Define Rewrite Rule
6974  *
6975  *****************************************************************************/
6976
6977 RuleStmt:       CREATE opt_or_replace RULE name AS
6978                         ON event TO qualified_name where_clause
6979                         DO opt_instead RuleActionList
6980                                 {
6981                                         RuleStmt *n = makeNode(RuleStmt);
6982                                         n->replace = $2;
6983                                         n->relation = $9;
6984                                         n->rulename = $4;
6985                                         n->whereClause = $10;
6986                                         n->event = $7;
6987                                         n->instead = $12;
6988                                         n->actions = $13;
6989                                         $$ = (Node *)n;
6990                                 }
6991                 ;
6992
6993 RuleActionList:
6994                         NOTHING                                                                 { $$ = NIL; }
6995                         | RuleActionStmt                                                { $$ = list_make1($1); }
6996                         | '(' RuleActionMulti ')'                               { $$ = $2; }
6997                 ;
6998
6999 /* the thrashing around here is to discard "empty" statements... */
7000 RuleActionMulti:
7001                         RuleActionMulti ';' RuleActionStmtOrEmpty
7002                                 { if ($3 != NULL)
7003                                         $$ = lappend($1, $3);
7004                                   else
7005                                         $$ = $1;
7006                                 }
7007                         | RuleActionStmtOrEmpty
7008                                 { if ($1 != NULL)
7009                                         $$ = list_make1($1);
7010                                   else
7011                                         $$ = NIL;
7012                                 }
7013                 ;
7014
7015 RuleActionStmt:
7016                         SelectStmt
7017                         | InsertStmt
7018                         | UpdateStmt
7019                         | DeleteStmt
7020                         | NotifyStmt
7021                 ;
7022
7023 RuleActionStmtOrEmpty:
7024                         RuleActionStmt                                                  { $$ = $1; }
7025                         |       /*EMPTY*/                                                       { $$ = NULL; }
7026                 ;
7027
7028 event:          SELECT                                                                  { $$ = CMD_SELECT; }
7029                         | UPDATE                                                                { $$ = CMD_UPDATE; }
7030                         | DELETE_P                                                              { $$ = CMD_DELETE; }
7031                         | INSERT                                                                { $$ = CMD_INSERT; }
7032                  ;
7033
7034 opt_instead:
7035                         INSTEAD                                                                 { $$ = TRUE; }
7036                         | ALSO                                                                  { $$ = FALSE; }
7037                         | /*EMPTY*/                                                             { $$ = FALSE; }
7038                 ;
7039
7040
7041 DropRuleStmt:
7042                         DROP RULE name ON qualified_name opt_drop_behavior
7043                                 {
7044                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
7045                                         n->relation = $5;
7046                                         n->property = $3;
7047                                         n->behavior = $6;
7048                                         n->removeType = OBJECT_RULE;
7049                                         n->missing_ok = false;
7050                                         $$ = (Node *) n;
7051                                 }
7052                         | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
7053                                 {
7054                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
7055                                         n->relation = $7;
7056                                         n->property = $5;
7057                                         n->behavior = $8;
7058                                         n->removeType = OBJECT_RULE;
7059                                         n->missing_ok = true;
7060                                         $$ = (Node *) n;
7061                                 }
7062                 ;
7063
7064
7065 /*****************************************************************************
7066  *
7067  *              QUERY:
7068  *                              NOTIFY <identifier> can appear both in rule bodies and
7069  *                              as a query-level command
7070  *
7071  *****************************************************************************/
7072
7073 NotifyStmt: NOTIFY ColId notify_payload
7074                                 {
7075                                         NotifyStmt *n = makeNode(NotifyStmt);
7076                                         n->conditionname = $2;
7077                                         n->payload = $3;
7078                                         $$ = (Node *)n;
7079                                 }
7080                 ;
7081
7082 notify_payload:
7083                         ',' Sconst                                                      { $$ = $2; }
7084                         | /*EMPTY*/                                                     { $$ = NULL; }
7085                 ;
7086
7087 ListenStmt: LISTEN ColId
7088                                 {
7089                                         ListenStmt *n = makeNode(ListenStmt);
7090                                         n->conditionname = $2;
7091                                         $$ = (Node *)n;
7092                                 }
7093                 ;
7094
7095 UnlistenStmt:
7096                         UNLISTEN ColId
7097                                 {
7098                                         UnlistenStmt *n = makeNode(UnlistenStmt);
7099                                         n->conditionname = $2;
7100                                         $$ = (Node *)n;
7101                                 }
7102                         | UNLISTEN '*'
7103                                 {
7104                                         UnlistenStmt *n = makeNode(UnlistenStmt);
7105                                         n->conditionname = NULL;
7106                                         $$ = (Node *)n;
7107                                 }
7108                 ;
7109
7110
7111 /*****************************************************************************
7112  *
7113  *              Transactions:
7114  *
7115  *              BEGIN / COMMIT / ROLLBACK
7116  *              (also older versions END / ABORT)
7117  *
7118  *****************************************************************************/
7119
7120 TransactionStmt:
7121                         ABORT_P opt_transaction
7122                                 {
7123                                         TransactionStmt *n = makeNode(TransactionStmt);
7124                                         n->kind = TRANS_STMT_ROLLBACK;
7125                                         n->options = NIL;
7126                                         $$ = (Node *)n;
7127                                 }
7128                         | BEGIN_P opt_transaction transaction_mode_list_or_empty
7129                                 {
7130                                         TransactionStmt *n = makeNode(TransactionStmt);
7131                                         n->kind = TRANS_STMT_BEGIN;
7132                                         n->options = $3;
7133                                         $$ = (Node *)n;
7134                                 }
7135                         | START TRANSACTION transaction_mode_list_or_empty
7136                                 {
7137                                         TransactionStmt *n = makeNode(TransactionStmt);
7138                                         n->kind = TRANS_STMT_START;
7139                                         n->options = $3;
7140                                         $$ = (Node *)n;
7141                                 }
7142                         | COMMIT opt_transaction
7143                                 {
7144                                         TransactionStmt *n = makeNode(TransactionStmt);
7145                                         n->kind = TRANS_STMT_COMMIT;
7146                                         n->options = NIL;
7147                                         $$ = (Node *)n;
7148                                 }
7149                         | END_P opt_transaction
7150                                 {
7151                                         TransactionStmt *n = makeNode(TransactionStmt);
7152                                         n->kind = TRANS_STMT_COMMIT;
7153                                         n->options = NIL;
7154                                         $$ = (Node *)n;
7155                                 }
7156                         | ROLLBACK opt_transaction
7157                                 {
7158                                         TransactionStmt *n = makeNode(TransactionStmt);
7159                                         n->kind = TRANS_STMT_ROLLBACK;
7160                                         n->options = NIL;
7161                                         $$ = (Node *)n;
7162                                 }
7163                         | SAVEPOINT ColId
7164                                 {
7165                                         TransactionStmt *n = makeNode(TransactionStmt);
7166                                         n->kind = TRANS_STMT_SAVEPOINT;
7167                                         n->options = list_make1(makeDefElem("savepoint_name",
7168                                                                                                                 (Node *)makeString($2)));
7169                                         $$ = (Node *)n;
7170                                 }
7171                         | RELEASE SAVEPOINT ColId
7172                                 {
7173                                         TransactionStmt *n = makeNode(TransactionStmt);
7174                                         n->kind = TRANS_STMT_RELEASE;
7175                                         n->options = list_make1(makeDefElem("savepoint_name",
7176                                                                                                                 (Node *)makeString($3)));
7177                                         $$ = (Node *)n;
7178                                 }
7179                         | RELEASE ColId
7180                                 {
7181                                         TransactionStmt *n = makeNode(TransactionStmt);
7182                                         n->kind = TRANS_STMT_RELEASE;
7183                                         n->options = list_make1(makeDefElem("savepoint_name",
7184                                                                                                                 (Node *)makeString($2)));
7185                                         $$ = (Node *)n;
7186                                 }
7187                         | ROLLBACK opt_transaction TO SAVEPOINT ColId
7188                                 {
7189                                         TransactionStmt *n = makeNode(TransactionStmt);
7190                                         n->kind = TRANS_STMT_ROLLBACK_TO;
7191                                         n->options = list_make1(makeDefElem("savepoint_name",
7192                                                                                                                 (Node *)makeString($5)));
7193                                         $$ = (Node *)n;
7194                                 }
7195                         | ROLLBACK opt_transaction TO ColId
7196                                 {
7197                                         TransactionStmt *n = makeNode(TransactionStmt);
7198                                         n->kind = TRANS_STMT_ROLLBACK_TO;
7199                                         n->options = list_make1(makeDefElem("savepoint_name",
7200                                                                                                                 (Node *)makeString($4)));
7201                                         $$ = (Node *)n;
7202                                 }
7203                         | PREPARE TRANSACTION Sconst
7204                                 {
7205                                         TransactionStmt *n = makeNode(TransactionStmt);
7206                                         n->kind = TRANS_STMT_PREPARE;
7207                                         n->gid = $3;
7208                                         $$ = (Node *)n;
7209                                 }
7210                         | COMMIT PREPARED Sconst
7211                                 {
7212                                         TransactionStmt *n = makeNode(TransactionStmt);
7213                                         n->kind = TRANS_STMT_COMMIT_PREPARED;
7214                                         n->gid = $3;
7215                                         $$ = (Node *)n;
7216                                 }
7217                         | ROLLBACK PREPARED Sconst
7218                                 {
7219                                         TransactionStmt *n = makeNode(TransactionStmt);
7220                                         n->kind = TRANS_STMT_ROLLBACK_PREPARED;
7221                                         n->gid = $3;
7222                                         $$ = (Node *)n;
7223                                 }
7224                 ;
7225
7226 opt_transaction:        WORK                                                    {}
7227                         | TRANSACTION                                                   {}
7228                         | /*EMPTY*/                                                             {}
7229                 ;
7230
7231 transaction_mode_item:
7232                         ISOLATION LEVEL iso_level
7233                                         { $$ = makeDefElem("transaction_isolation",
7234                                                                            makeStringConst($3, @3)); }
7235                         | READ ONLY
7236                                         { $$ = makeDefElem("transaction_read_only",
7237                                                                            makeIntConst(TRUE, @1)); }
7238                         | READ WRITE
7239                                         { $$ = makeDefElem("transaction_read_only",
7240                                                                            makeIntConst(FALSE, @1)); }
7241                         | DEFERRABLE
7242                                         { $$ = makeDefElem("transaction_deferrable",
7243                                                                            makeIntConst(TRUE, @1)); }
7244                         | NOT DEFERRABLE
7245                                         { $$ = makeDefElem("transaction_deferrable",
7246                                                                            makeIntConst(FALSE, @1)); }
7247                 ;
7248
7249 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
7250 transaction_mode_list:
7251                         transaction_mode_item
7252                                         { $$ = list_make1($1); }
7253                         | transaction_mode_list ',' transaction_mode_item
7254                                         { $$ = lappend($1, $3); }
7255                         | transaction_mode_list transaction_mode_item
7256                                         { $$ = lappend($1, $2); }
7257                 ;
7258
7259 transaction_mode_list_or_empty:
7260                         transaction_mode_list
7261                         | /* EMPTY */
7262                                         { $$ = NIL; }
7263                 ;
7264
7265
7266 /*****************************************************************************
7267  *
7268  *      QUERY:
7269  *              CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
7270  *                      AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
7271  *
7272  *****************************************************************************/
7273
7274 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
7275                                 AS SelectStmt opt_check_option
7276                                 {
7277                                         ViewStmt *n = makeNode(ViewStmt);
7278                                         n->view = $4;
7279                                         n->view->relpersistence = $2;
7280                                         n->aliases = $5;
7281                                         n->query = $7;
7282                                         n->replace = false;
7283                                         $$ = (Node *) n;
7284                                 }
7285                 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
7286                                 AS SelectStmt opt_check_option
7287                                 {
7288                                         ViewStmt *n = makeNode(ViewStmt);
7289                                         n->view = $6;
7290                                         n->view->relpersistence = $4;
7291                                         n->aliases = $7;
7292                                         n->query = $9;
7293                                         n->replace = true;
7294                                         $$ = (Node *) n;
7295                                 }
7296                 ;
7297
7298 opt_check_option:
7299                 WITH CHECK OPTION
7300                                 {
7301                                         ereport(ERROR,
7302                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7303                                                          errmsg("WITH CHECK OPTION is not implemented")));
7304                                 }
7305                 | WITH CASCADED CHECK OPTION
7306                                 {
7307                                         ereport(ERROR,
7308                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7309                                                          errmsg("WITH CHECK OPTION is not implemented")));
7310                                 }
7311                 | WITH LOCAL CHECK OPTION
7312                                 {
7313                                         ereport(ERROR,
7314                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7315                                                          errmsg("WITH CHECK OPTION is not implemented")));
7316                                 }
7317                 | /* EMPTY */                                                   { $$ = NIL; }
7318                 ;
7319
7320 /*****************************************************************************
7321  *
7322  *              QUERY:
7323  *                              LOAD "filename"
7324  *
7325  *****************************************************************************/
7326
7327 LoadStmt:       LOAD file_name
7328                                 {
7329                                         LoadStmt *n = makeNode(LoadStmt);
7330                                         n->filename = $2;
7331                                         $$ = (Node *)n;
7332                                 }
7333                 ;
7334
7335
7336 /*****************************************************************************
7337  *
7338  *              CREATE DATABASE
7339  *
7340  *****************************************************************************/
7341
7342 CreatedbStmt:
7343                         CREATE DATABASE database_name opt_with createdb_opt_list
7344                                 {
7345                                         CreatedbStmt *n = makeNode(CreatedbStmt);
7346                                         n->dbname = $3;
7347                                         n->options = $5;
7348                                         $$ = (Node *)n;
7349                                 }
7350                 ;
7351
7352 createdb_opt_list:
7353                         createdb_opt_list createdb_opt_item             { $$ = lappend($1, $2); }
7354                         | /* EMPTY */                                                   { $$ = NIL; }
7355                 ;
7356
7357 createdb_opt_item:
7358                         TABLESPACE opt_equal name
7359                                 {
7360                                         $$ = makeDefElem("tablespace", (Node *)makeString($3));
7361                                 }
7362                         | TABLESPACE opt_equal DEFAULT
7363                                 {
7364                                         $$ = makeDefElem("tablespace", NULL);
7365                                 }
7366                         | LOCATION opt_equal Sconst
7367                                 {
7368                                         $$ = makeDefElem("location", (Node *)makeString($3));
7369                                 }
7370                         | LOCATION opt_equal DEFAULT
7371                                 {
7372                                         $$ = makeDefElem("location", NULL);
7373                                 }
7374                         | TEMPLATE opt_equal name
7375                                 {
7376                                         $$ = makeDefElem("template", (Node *)makeString($3));
7377                                 }
7378                         | TEMPLATE opt_equal DEFAULT
7379                                 {
7380                                         $$ = makeDefElem("template", NULL);
7381                                 }
7382                         | ENCODING opt_equal Sconst
7383                                 {
7384                                         $$ = makeDefElem("encoding", (Node *)makeString($3));
7385                                 }
7386                         | ENCODING opt_equal Iconst
7387                                 {
7388                                         $$ = makeDefElem("encoding", (Node *)makeInteger($3));
7389                                 }
7390                         | ENCODING opt_equal DEFAULT
7391                                 {
7392                                         $$ = makeDefElem("encoding", NULL);
7393                                 }
7394                         | LC_COLLATE_P opt_equal Sconst
7395                                 {
7396                                         $$ = makeDefElem("lc_collate", (Node *)makeString($3));
7397                                 }
7398                         | LC_COLLATE_P opt_equal DEFAULT
7399                                 {
7400                                         $$ = makeDefElem("lc_collate", NULL);
7401                                 }
7402                         | LC_CTYPE_P opt_equal Sconst
7403                                 {
7404                                         $$ = makeDefElem("lc_ctype", (Node *)makeString($3));
7405                                 }
7406                         | LC_CTYPE_P opt_equal DEFAULT
7407                                 {
7408                                         $$ = makeDefElem("lc_ctype", NULL);
7409                                 }
7410                         | CONNECTION LIMIT opt_equal SignedIconst
7411                                 {
7412                                         $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
7413                                 }
7414                         | OWNER opt_equal name
7415                                 {
7416                                         $$ = makeDefElem("owner", (Node *)makeString($3));
7417                                 }
7418                         | OWNER opt_equal DEFAULT
7419                                 {
7420                                         $$ = makeDefElem("owner", NULL);
7421                                 }
7422                 ;
7423
7424 /*
7425  *      Though the equals sign doesn't match other WITH options, pg_dump uses
7426  *      equals for backward compatibility, and it doesn't seem worth removing it.
7427  */
7428 opt_equal:      '='                                                                             {}
7429                         | /*EMPTY*/                                                             {}
7430                 ;
7431
7432
7433 /*****************************************************************************
7434  *
7435  *              ALTER DATABASE
7436  *
7437  *****************************************************************************/
7438
7439 AlterDatabaseStmt:
7440                         ALTER DATABASE database_name opt_with alterdb_opt_list
7441                                  {
7442                                         AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
7443                                         n->dbname = $3;
7444                                         n->options = $5;
7445                                         $$ = (Node *)n;
7446                                  }
7447                         | ALTER DATABASE database_name SET TABLESPACE name
7448                                  {
7449                                         AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
7450                                         n->dbname = $3;
7451                                         n->options = list_make1(makeDefElem("tablespace",
7452                                                                                                         (Node *)makeString($6)));
7453                                         $$ = (Node *)n;
7454                                  }
7455                 ;
7456
7457 AlterDatabaseSetStmt:
7458                         ALTER DATABASE database_name SetResetClause
7459                                 {
7460                                         AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
7461                                         n->dbname = $3;
7462                                         n->setstmt = $4;
7463                                         $$ = (Node *)n;
7464                                 }
7465                 ;
7466
7467
7468 alterdb_opt_list:
7469                         alterdb_opt_list alterdb_opt_item               { $$ = lappend($1, $2); }
7470                         | /* EMPTY */                                                   { $$ = NIL; }
7471                 ;
7472
7473 alterdb_opt_item:
7474                         CONNECTION LIMIT opt_equal SignedIconst
7475                                 {
7476                                         $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
7477                                 }
7478                 ;
7479
7480
7481 /*****************************************************************************
7482  *
7483  *              DROP DATABASE [ IF EXISTS ]
7484  *
7485  * This is implicitly CASCADE, no need for drop behavior
7486  *****************************************************************************/
7487
7488 DropdbStmt: DROP DATABASE database_name
7489                                 {
7490                                         DropdbStmt *n = makeNode(DropdbStmt);
7491                                         n->dbname = $3;
7492                                         n->missing_ok = FALSE;
7493                                         $$ = (Node *)n;
7494                                 }
7495                         | DROP DATABASE IF_P EXISTS database_name
7496                                 {
7497                                         DropdbStmt *n = makeNode(DropdbStmt);
7498                                         n->dbname = $5;
7499                                         n->missing_ok = TRUE;
7500                                         $$ = (Node *)n;
7501                                 }
7502                 ;
7503
7504
7505 /*****************************************************************************
7506  *
7507  * Manipulate a domain
7508  *
7509  *****************************************************************************/
7510
7511 CreateDomainStmt:
7512                         CREATE DOMAIN_P any_name opt_as Typename ColQualList
7513                                 {
7514                                         CreateDomainStmt *n = makeNode(CreateDomainStmt);
7515                                         n->domainname = $3;
7516                                         n->typeName = $5;
7517                                         SplitColQualList($6, &n->constraints, &n->collClause,
7518                                                                          yyscanner);
7519                                         $$ = (Node *)n;
7520                                 }
7521                 ;
7522
7523 AlterDomainStmt:
7524                         /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
7525                         ALTER DOMAIN_P any_name alter_column_default
7526                                 {
7527                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
7528                                         n->subtype = 'T';
7529                                         n->typeName = $3;
7530                                         n->def = $4;
7531                                         $$ = (Node *)n;
7532                                 }
7533                         /* ALTER DOMAIN <domain> DROP NOT NULL */
7534                         | ALTER DOMAIN_P any_name DROP NOT NULL_P
7535                                 {
7536                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
7537                                         n->subtype = 'N';
7538                                         n->typeName = $3;
7539                                         $$ = (Node *)n;
7540                                 }
7541                         /* ALTER DOMAIN <domain> SET NOT NULL */
7542                         | ALTER DOMAIN_P any_name SET NOT NULL_P
7543                                 {
7544                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
7545                                         n->subtype = 'O';
7546                                         n->typeName = $3;
7547                                         $$ = (Node *)n;
7548                                 }
7549                         /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
7550                         | ALTER DOMAIN_P any_name ADD_P TableConstraint
7551                                 {
7552                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
7553                                         n->subtype = 'C';
7554                                         n->typeName = $3;
7555                                         n->def = $5;
7556                                         $$ = (Node *)n;
7557                                 }
7558                         /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
7559                         | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
7560                                 {
7561                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
7562                                         n->subtype = 'X';
7563                                         n->typeName = $3;
7564                                         n->name = $6;
7565                                         n->behavior = $7;
7566                                         $$ = (Node *)n;
7567                                 }
7568                         ;
7569
7570 opt_as:         AS                                                                              {}
7571                         | /* EMPTY */                                                   {}
7572                 ;
7573
7574
7575 /*****************************************************************************
7576  *
7577  * Manipulate a text search dictionary or configuration
7578  *
7579  *****************************************************************************/
7580
7581 AlterTSDictionaryStmt:
7582                         ALTER TEXT_P SEARCH DICTIONARY any_name definition
7583                                 {
7584                                         AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
7585                                         n->dictname = $5;
7586                                         n->options = $6;
7587                                         $$ = (Node *)n;
7588                                 }
7589                 ;
7590
7591 AlterTSConfigurationStmt:
7592                         ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
7593                                 {
7594                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
7595                                         n->cfgname = $5;
7596                                         n->tokentype = $9;
7597                                         n->dicts = $11;
7598                                         n->override = false;
7599                                         n->replace = false;
7600                                         $$ = (Node*)n;
7601                                 }
7602                         | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
7603                                 {
7604                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
7605                                         n->cfgname = $5;
7606                                         n->tokentype = $9;
7607                                         n->dicts = $11;
7608                                         n->override = true;
7609                                         n->replace = false;
7610                                         $$ = (Node*)n;
7611                                 }
7612                         | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
7613                                 {
7614                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
7615                                         n->cfgname = $5;
7616                                         n->tokentype = NIL;
7617                                         n->dicts = list_make2($9,$11);
7618                                         n->override = false;
7619                                         n->replace = true;
7620                                         $$ = (Node*)n;
7621                                 }
7622                         | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
7623                                 {
7624                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
7625                                         n->cfgname = $5;
7626                                         n->tokentype = $9;
7627                                         n->dicts = list_make2($11,$13);
7628                                         n->override = false;
7629                                         n->replace = true;
7630                                         $$ = (Node*)n;
7631                                 }
7632                         | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
7633                                 {
7634                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
7635                                         n->cfgname = $5;
7636                                         n->tokentype = $9;
7637                                         n->missing_ok = false;
7638                                         $$ = (Node*)n;
7639                                 }
7640                         | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
7641                                 {
7642                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
7643                                         n->cfgname = $5;
7644                                         n->tokentype = $11;
7645                                         n->missing_ok = true;
7646                                         $$ = (Node*)n;
7647                                 }
7648                 ;
7649
7650
7651 /*****************************************************************************
7652  *
7653  * Manipulate a conversion
7654  *
7655  *              CREATE [DEFAULT] CONVERSION <conversion_name>
7656  *              FOR <encoding_name> TO <encoding_name> FROM <func_name>
7657  *
7658  *****************************************************************************/
7659
7660 CreateConversionStmt:
7661                         CREATE opt_default CONVERSION_P any_name FOR Sconst
7662                         TO Sconst FROM any_name
7663                         {
7664                           CreateConversionStmt *n = makeNode(CreateConversionStmt);
7665                           n->conversion_name = $4;
7666                           n->for_encoding_name = $6;
7667                           n->to_encoding_name = $8;
7668                           n->func_name = $10;
7669                           n->def = $2;
7670                           $$ = (Node *)n;
7671                         }
7672                 ;
7673
7674 /*****************************************************************************
7675  *
7676  *              QUERY:
7677  *                              CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
7678  *                              CLUSTER [VERBOSE]
7679  *                              CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
7680  *
7681  *****************************************************************************/
7682
7683 ClusterStmt:
7684                         CLUSTER opt_verbose qualified_name cluster_index_specification
7685                                 {
7686                                ClusterStmt *n = makeNode(ClusterStmt);
7687                                    n->relation = $3;
7688                                    n->indexname = $4;
7689                                    n->verbose = $2;
7690                                    $$ = (Node*)n;
7691                                 }
7692                         | CLUSTER opt_verbose
7693                             {
7694                                    ClusterStmt *n = makeNode(ClusterStmt);
7695                                    n->relation = NULL;
7696                                    n->indexname = NULL;
7697                                    n->verbose = $2;
7698                                    $$ = (Node*)n;
7699                                 }
7700                         /* kept for pre-8.3 compatibility */
7701                         | CLUSTER opt_verbose index_name ON qualified_name
7702                                 {
7703                                    ClusterStmt *n = makeNode(ClusterStmt);
7704                                    n->relation = $5;
7705                                    n->indexname = $3;
7706                                    n->verbose = $2;
7707                                    $$ = (Node*)n;
7708                                 }
7709                 ;
7710
7711 cluster_index_specification:
7712                         USING index_name                { $$ = $2; }
7713                         | /*EMPTY*/                             { $$ = NULL; }
7714                 ;
7715
7716
7717 /*****************************************************************************
7718  *
7719  *              QUERY:
7720  *                              VACUUM
7721  *                              ANALYZE
7722  *
7723  *****************************************************************************/
7724
7725 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
7726                                 {
7727                                         VacuumStmt *n = makeNode(VacuumStmt);
7728                                         n->options = VACOPT_VACUUM;
7729                                         if ($2)
7730                                                 n->options |= VACOPT_FULL;
7731                                         if ($4)
7732                                                 n->options |= VACOPT_VERBOSE;
7733                                         n->freeze_min_age = $3 ? 0 : -1;
7734                                         n->freeze_table_age = $3 ? 0 : -1;
7735                                         n->relation = NULL;
7736                                         n->va_cols = NIL;
7737                                         $$ = (Node *)n;
7738                                 }
7739                         | VACUUM opt_full opt_freeze opt_verbose qualified_name
7740                                 {
7741                                         VacuumStmt *n = makeNode(VacuumStmt);
7742                                         n->options = VACOPT_VACUUM;
7743                                         if ($2)
7744                                                 n->options |= VACOPT_FULL;
7745                                         if ($4)
7746                                                 n->options |= VACOPT_VERBOSE;
7747                                         n->freeze_min_age = $3 ? 0 : -1;
7748                                         n->freeze_table_age = $3 ? 0 : -1;
7749                                         n->relation = $5;
7750                                         n->va_cols = NIL;
7751                                         $$ = (Node *)n;
7752                                 }
7753                         | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
7754                                 {
7755                                         VacuumStmt *n = (VacuumStmt *) $5;
7756                                         n->options |= VACOPT_VACUUM;
7757                                         if ($2)
7758                                                 n->options |= VACOPT_FULL;
7759                                         if ($4)
7760                                                 n->options |= VACOPT_VERBOSE;
7761                                         n->freeze_min_age = $3 ? 0 : -1;
7762                                         n->freeze_table_age = $3 ? 0 : -1;
7763                                         $$ = (Node *)n;
7764                                 }
7765                         | VACUUM '(' vacuum_option_list ')'
7766                                 {
7767                                         VacuumStmt *n = makeNode(VacuumStmt);
7768                                         n->options = VACOPT_VACUUM | $3;
7769                                         if (n->options & VACOPT_FREEZE)
7770                                                 n->freeze_min_age = n->freeze_table_age = 0;
7771                                         else
7772                                                 n->freeze_min_age = n->freeze_table_age = -1;
7773                                         n->relation = NULL;
7774                                         n->va_cols = NIL;
7775                                         $$ = (Node *) n;
7776                                 }
7777                         | VACUUM '(' vacuum_option_list ')' qualified_name opt_name_list
7778                                 {
7779                                         VacuumStmt *n = makeNode(VacuumStmt);
7780                                         n->options = VACOPT_VACUUM | $3;
7781                                         if (n->options & VACOPT_FREEZE)
7782                                                 n->freeze_min_age = n->freeze_table_age = 0;
7783                                         else
7784                                                 n->freeze_min_age = n->freeze_table_age = -1;
7785                                         n->relation = $5;
7786                                         n->va_cols = $6;
7787                                         if (n->va_cols != NIL)  /* implies analyze */
7788                                                 n->options |= VACOPT_ANALYZE;
7789                                         $$ = (Node *) n;
7790                                 }
7791                 ;
7792
7793 vacuum_option_list:
7794                         vacuum_option_elem                                                              { $$ = $1; }
7795                         | vacuum_option_list ',' vacuum_option_elem             { $$ = $1 | $3; }
7796                 ;
7797
7798 vacuum_option_elem:
7799                         analyze_keyword         { $$ = VACOPT_ANALYZE; }
7800                         | VERBOSE                       { $$ = VACOPT_VERBOSE; }
7801                         | FREEZE                        { $$ = VACOPT_FREEZE; }
7802                         | FULL                          { $$ = VACOPT_FULL; }
7803                 ;
7804
7805 AnalyzeStmt:
7806                         analyze_keyword opt_verbose
7807                                 {
7808                                         VacuumStmt *n = makeNode(VacuumStmt);
7809                                         n->options = VACOPT_ANALYZE;
7810                                         if ($2)
7811                                                 n->options |= VACOPT_VERBOSE;
7812                                         n->freeze_min_age = -1;
7813                                         n->freeze_table_age = -1;
7814                                         n->relation = NULL;
7815                                         n->va_cols = NIL;
7816                                         $$ = (Node *)n;
7817                                 }
7818                         | analyze_keyword opt_verbose qualified_name opt_name_list
7819                                 {
7820                                         VacuumStmt *n = makeNode(VacuumStmt);
7821                                         n->options = VACOPT_ANALYZE;
7822                                         if ($2)
7823                                                 n->options |= VACOPT_VERBOSE;
7824                                         n->freeze_min_age = -1;
7825                                         n->freeze_table_age = -1;
7826                                         n->relation = $3;
7827                                         n->va_cols = $4;
7828                                         $$ = (Node *)n;
7829                                 }
7830                 ;
7831
7832 analyze_keyword:
7833                         ANALYZE                                                                 {}
7834                         | ANALYSE /* British */                                 {}
7835                 ;
7836
7837 opt_verbose:
7838                         VERBOSE                                                                 { $$ = TRUE; }
7839                         | /*EMPTY*/                                                             { $$ = FALSE; }
7840                 ;
7841
7842 opt_full:       FULL                                                                    { $$ = TRUE; }
7843                         | /*EMPTY*/                                                             { $$ = FALSE; }
7844                 ;
7845
7846 opt_freeze: FREEZE                                                                      { $$ = TRUE; }
7847                         | /*EMPTY*/                                                             { $$ = FALSE; }
7848                 ;
7849
7850 opt_name_list:
7851                         '(' name_list ')'                                               { $$ = $2; }
7852                         | /*EMPTY*/                                                             { $$ = NIL; }
7853                 ;
7854
7855
7856 /*****************************************************************************
7857  *
7858  *              QUERY:
7859  *                              EXPLAIN [ANALYZE] [VERBOSE] query
7860  *                              EXPLAIN ( options ) query
7861  *
7862  *****************************************************************************/
7863
7864 ExplainStmt:
7865                 EXPLAIN ExplainableStmt
7866                                 {
7867                                         ExplainStmt *n = makeNode(ExplainStmt);
7868                                         n->query = $2;
7869                                         n->options = NIL;
7870                                         $$ = (Node *) n;
7871                                 }
7872                 | EXPLAIN analyze_keyword opt_verbose ExplainableStmt
7873                                 {
7874                                         ExplainStmt *n = makeNode(ExplainStmt);
7875                                         n->query = $4;
7876                                         n->options = list_make1(makeDefElem("analyze", NULL));
7877                                         if ($3)
7878                                                 n->options = lappend(n->options,
7879                                                                                          makeDefElem("verbose", NULL));
7880                                         $$ = (Node *) n;
7881                                 }
7882                 | EXPLAIN VERBOSE ExplainableStmt
7883                                 {
7884                                         ExplainStmt *n = makeNode(ExplainStmt);
7885                                         n->query = $3;
7886                                         n->options = list_make1(makeDefElem("verbose", NULL));
7887                                         $$ = (Node *) n;
7888                                 }
7889                 | EXPLAIN '(' explain_option_list ')' ExplainableStmt
7890                                 {
7891                                         ExplainStmt *n = makeNode(ExplainStmt);
7892                                         n->query = $5;
7893                                         n->options = $3;
7894                                         $$ = (Node *) n;
7895                                 }
7896                 ;
7897
7898 ExplainableStmt:
7899                         SelectStmt
7900                         | InsertStmt
7901                         | UpdateStmt
7902                         | DeleteStmt
7903                         | DeclareCursorStmt
7904                         | CreateAsStmt
7905                         | ExecuteStmt                                   /* by default all are $$=$1 */
7906                 ;
7907
7908 explain_option_list:
7909                         explain_option_elem
7910                                 {
7911                                         $$ = list_make1($1);
7912                                 }
7913                         | explain_option_list ',' explain_option_elem
7914                                 {
7915                                         $$ = lappend($1, $3);
7916                                 }
7917                 ;
7918
7919 explain_option_elem:
7920                         explain_option_name explain_option_arg
7921                                 {
7922                                         $$ = makeDefElem($1, $2);
7923                                 }
7924                 ;
7925
7926 explain_option_name:
7927                         ColId                                   { $$ = $1; }
7928                         | analyze_keyword               { $$ = "analyze"; }
7929                         | VERBOSE                               { $$ = "verbose"; }
7930                 ;
7931
7932 explain_option_arg:
7933                         opt_boolean_or_string   { $$ = (Node *) makeString($1); }
7934                         | NumericOnly                   { $$ = (Node *) $1; }
7935                         | /* EMPTY */                   { $$ = NULL; }
7936                 ;
7937
7938 /*****************************************************************************
7939  *
7940  *              QUERY:
7941  *                              PREPARE <plan_name> [(args, ...)] AS <query>
7942  *
7943  *****************************************************************************/
7944
7945 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
7946                                 {
7947                                         PrepareStmt *n = makeNode(PrepareStmt);
7948                                         n->name = $2;
7949                                         n->argtypes = $3;
7950                                         n->query = $5;
7951                                         $$ = (Node *) n;
7952                                 }
7953                 ;
7954
7955 prep_type_clause: '(' type_list ')'                     { $$ = $2; }
7956                                 | /* EMPTY */                           { $$ = NIL; }
7957                 ;
7958
7959 PreparableStmt:
7960                         SelectStmt
7961                         | InsertStmt
7962                         | UpdateStmt
7963                         | DeleteStmt                                    /* by default all are $$=$1 */
7964                 ;
7965
7966 /*****************************************************************************
7967  *
7968  * EXECUTE <plan_name> [(params, ...)]
7969  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
7970  *
7971  *****************************************************************************/
7972
7973 ExecuteStmt: EXECUTE name execute_param_clause
7974                                 {
7975                                         ExecuteStmt *n = makeNode(ExecuteStmt);
7976                                         n->name = $2;
7977                                         n->params = $3;
7978                                         n->into = NULL;
7979                                         $$ = (Node *) n;
7980                                 }
7981                         | CREATE OptTemp TABLE create_as_target AS
7982                                 EXECUTE name execute_param_clause
7983                                 {
7984                                         ExecuteStmt *n = makeNode(ExecuteStmt);
7985                                         n->name = $7;
7986                                         n->params = $8;
7987                                         $4->rel->relpersistence = $2;
7988                                         n->into = $4;
7989                                         if ($4->colNames)
7990                                                 ereport(ERROR,
7991                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7992                                                                  errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
7993                                         /* ... because it's not implemented, but it could be */
7994                                         $$ = (Node *) n;
7995                                 }
7996                 ;
7997
7998 execute_param_clause: '(' expr_list ')'                         { $$ = $2; }
7999                                         | /* EMPTY */                                   { $$ = NIL; }
8000                                         ;
8001
8002 /*****************************************************************************
8003  *
8004  *              QUERY:
8005  *                              DEALLOCATE [PREPARE] <plan_name>
8006  *
8007  *****************************************************************************/
8008
8009 DeallocateStmt: DEALLOCATE name
8010                                         {
8011                                                 DeallocateStmt *n = makeNode(DeallocateStmt);
8012                                                 n->name = $2;
8013                                                 $$ = (Node *) n;
8014                                         }
8015                                 | DEALLOCATE PREPARE name
8016                                         {
8017                                                 DeallocateStmt *n = makeNode(DeallocateStmt);
8018                                                 n->name = $3;
8019                                                 $$ = (Node *) n;
8020                                         }
8021                                 | DEALLOCATE ALL
8022                                         {
8023                                                 DeallocateStmt *n = makeNode(DeallocateStmt);
8024                                                 n->name = NULL;
8025                                                 $$ = (Node *) n;
8026                                         }
8027                                 | DEALLOCATE PREPARE ALL
8028                                         {
8029                                                 DeallocateStmt *n = makeNode(DeallocateStmt);
8030                                                 n->name = NULL;
8031                                                 $$ = (Node *) n;
8032                                         }
8033                 ;
8034
8035 /*****************************************************************************
8036  *
8037  *              QUERY:
8038  *                              INSERT STATEMENTS
8039  *
8040  *****************************************************************************/
8041
8042 InsertStmt:
8043                         opt_with_clause INSERT INTO qualified_name insert_rest returning_clause
8044                                 {
8045                                         $5->relation = $4;
8046                                         $5->returningList = $6;
8047                                         $5->withClause = $1;
8048                                         $$ = (Node *) $5;
8049                                 }
8050                 ;
8051
8052 insert_rest:
8053                         SelectStmt
8054                                 {
8055                                         $$ = makeNode(InsertStmt);
8056                                         $$->cols = NIL;
8057                                         $$->selectStmt = $1;
8058                                 }
8059                         | '(' insert_column_list ')' SelectStmt
8060                                 {
8061                                         $$ = makeNode(InsertStmt);
8062                                         $$->cols = $2;
8063                                         $$->selectStmt = $4;
8064                                 }
8065                         | DEFAULT VALUES
8066                                 {
8067                                         $$ = makeNode(InsertStmt);
8068                                         $$->cols = NIL;
8069                                         $$->selectStmt = NULL;
8070                                 }
8071                 ;
8072
8073 insert_column_list:
8074                         insert_column_item
8075                                         { $$ = list_make1($1); }
8076                         | insert_column_list ',' insert_column_item
8077                                         { $$ = lappend($1, $3); }
8078                 ;
8079
8080 insert_column_item:
8081                         ColId opt_indirection
8082                                 {
8083                                         $$ = makeNode(ResTarget);
8084                                         $$->name = $1;
8085                                         $$->indirection = check_indirection($2, yyscanner);
8086                                         $$->val = NULL;
8087                                         $$->location = @1;
8088                                 }
8089                 ;
8090
8091 returning_clause:
8092                         RETURNING target_list           { $$ = $2; }
8093                         | /* EMPTY */                           { $$ = NIL; }
8094                 ;
8095
8096
8097 /*****************************************************************************
8098  *
8099  *              QUERY:
8100  *                              DELETE STATEMENTS
8101  *
8102  *****************************************************************************/
8103
8104 DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
8105                         using_clause where_or_current_clause returning_clause
8106                                 {
8107                                         DeleteStmt *n = makeNode(DeleteStmt);
8108                                         n->relation = $4;
8109                                         n->usingClause = $5;
8110                                         n->whereClause = $6;
8111                                         n->returningList = $7;
8112                                         n->withClause = $1;
8113                                         $$ = (Node *)n;
8114                                 }
8115                 ;
8116
8117 using_clause:
8118                         USING from_list                                         { $$ = $2; }
8119                         | /*EMPTY*/                                                             { $$ = NIL; }
8120                 ;
8121
8122
8123 /*****************************************************************************
8124  *
8125  *              QUERY:
8126  *                              LOCK TABLE
8127  *
8128  *****************************************************************************/
8129
8130 LockStmt:       LOCK_P opt_table relation_expr_list opt_lock opt_nowait
8131                                 {
8132                                         LockStmt *n = makeNode(LockStmt);
8133
8134                                         n->relations = $3;
8135                                         n->mode = $4;
8136                                         n->nowait = $5;
8137                                         $$ = (Node *)n;
8138                                 }
8139                 ;
8140
8141 opt_lock:       IN_P lock_type MODE                     { $$ = $2; }
8142                         | /*EMPTY*/                                             { $$ = AccessExclusiveLock; }
8143                 ;
8144
8145 lock_type:      ACCESS SHARE                                    { $$ = AccessShareLock; }
8146                         | ROW SHARE                                             { $$ = RowShareLock; }
8147                         | ROW EXCLUSIVE                                 { $$ = RowExclusiveLock; }
8148                         | SHARE UPDATE EXCLUSIVE                { $$ = ShareUpdateExclusiveLock; }
8149                         | SHARE                                                 { $$ = ShareLock; }
8150                         | SHARE ROW EXCLUSIVE                   { $$ = ShareRowExclusiveLock; }
8151                         | EXCLUSIVE                                             { $$ = ExclusiveLock; }
8152                         | ACCESS EXCLUSIVE                              { $$ = AccessExclusiveLock; }
8153                 ;
8154
8155 opt_nowait:     NOWAIT                                                  { $$ = TRUE; }
8156                         | /*EMPTY*/                                             { $$ = FALSE; }
8157                 ;
8158
8159
8160 /*****************************************************************************
8161  *
8162  *              QUERY:
8163  *                              UpdateStmt (UPDATE)
8164  *
8165  *****************************************************************************/
8166
8167 UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
8168                         SET set_clause_list
8169                         from_clause
8170                         where_or_current_clause
8171                         returning_clause
8172                                 {
8173                                         UpdateStmt *n = makeNode(UpdateStmt);
8174                                         n->relation = $3;
8175                                         n->targetList = $5;
8176                                         n->fromClause = $6;
8177                                         n->whereClause = $7;
8178                                         n->returningList = $8;
8179                                         n->withClause = $1;
8180                                         $$ = (Node *)n;
8181                                 }
8182                 ;
8183
8184 set_clause_list:
8185                         set_clause                                                      { $$ = $1; }
8186                         | set_clause_list ',' set_clause        { $$ = list_concat($1,$3); }
8187                 ;
8188
8189 set_clause:
8190                         single_set_clause                                               { $$ = list_make1($1); }
8191                         | multiple_set_clause                                   { $$ = $1; }
8192                 ;
8193
8194 single_set_clause:
8195                         set_target '=' ctext_expr
8196                                 {
8197                                         $$ = $1;
8198                                         $$->val = (Node *) $3;
8199                                 }
8200                 ;
8201
8202 multiple_set_clause:
8203                         '(' set_target_list ')' '=' ctext_row
8204                                 {
8205                                         ListCell *col_cell;
8206                                         ListCell *val_cell;
8207
8208                                         /*
8209                                          * Break the ctext_row apart, merge individual expressions
8210                                          * into the destination ResTargets.  XXX this approach
8211                                          * cannot work for general row expressions as sources.
8212                                          */
8213                                         if (list_length($2) != list_length($5))
8214                                                 ereport(ERROR,
8215                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
8216                                                                  errmsg("number of columns does not match number of values"),
8217                                                                  parser_errposition(@1)));
8218                                         forboth(col_cell, $2, val_cell, $5)
8219                                         {
8220                                                 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
8221                                                 Node *res_val = (Node *) lfirst(val_cell);
8222
8223                                                 res_col->val = res_val;
8224                                         }
8225
8226                                         $$ = $2;
8227                                 }
8228                 ;
8229
8230 set_target:
8231                         ColId opt_indirection
8232                                 {
8233                                         $$ = makeNode(ResTarget);
8234                                         $$->name = $1;
8235                                         $$->indirection = check_indirection($2, yyscanner);
8236                                         $$->val = NULL; /* upper production sets this */
8237                                         $$->location = @1;
8238                                 }
8239                 ;
8240
8241 set_target_list:
8242                         set_target                                                              { $$ = list_make1($1); }
8243                         | set_target_list ',' set_target                { $$ = lappend($1,$3); }
8244                 ;
8245
8246
8247 /*****************************************************************************
8248  *
8249  *              QUERY:
8250  *                              CURSOR STATEMENTS
8251  *
8252  *****************************************************************************/
8253 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
8254                                 {
8255                                         DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
8256                                         n->portalname = $2;
8257                                         /* currently we always set FAST_PLAN option */
8258                                         n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
8259                                         n->query = $7;
8260                                         $$ = (Node *)n;
8261                                 }
8262                 ;
8263
8264 cursor_name:    name                                            { $$ = $1; }
8265                 ;
8266
8267 cursor_options: /*EMPTY*/                                       { $$ = 0; }
8268                         | cursor_options NO SCROLL              { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
8269                         | cursor_options SCROLL                 { $$ = $1 | CURSOR_OPT_SCROLL; }
8270                         | cursor_options BINARY                 { $$ = $1 | CURSOR_OPT_BINARY; }
8271                         | cursor_options INSENSITIVE    { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
8272                 ;
8273
8274 opt_hold: /* EMPTY */                                           { $$ = 0; }
8275                         | WITH HOLD                                             { $$ = CURSOR_OPT_HOLD; }
8276                         | WITHOUT HOLD                                  { $$ = 0; }
8277                 ;
8278
8279 /*****************************************************************************
8280  *
8281  *              QUERY:
8282  *                              SELECT STATEMENTS
8283  *
8284  *****************************************************************************/
8285
8286 /* A complete SELECT statement looks like this.
8287  *
8288  * The rule returns either a single SelectStmt node or a tree of them,
8289  * representing a set-operation tree.
8290  *
8291  * There is an ambiguity when a sub-SELECT is within an a_expr and there
8292  * are excess parentheses: do the parentheses belong to the sub-SELECT or
8293  * to the surrounding a_expr?  We don't really care, but bison wants to know.
8294  * To resolve the ambiguity, we are careful to define the grammar so that
8295  * the decision is staved off as long as possible: as long as we can keep
8296  * absorbing parentheses into the sub-SELECT, we will do so, and only when
8297  * it's no longer possible to do that will we decide that parens belong to
8298  * the expression.      For example, in "SELECT (((SELECT 2)) + 3)" the extra
8299  * parentheses are treated as part of the sub-select.  The necessity of doing
8300  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".      Had we
8301  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
8302  * SELECT viewpoint when we see the UNION.
8303  *
8304  * This approach is implemented by defining a nonterminal select_with_parens,
8305  * which represents a SELECT with at least one outer layer of parentheses,
8306  * and being careful to use select_with_parens, never '(' SelectStmt ')',
8307  * in the expression grammar.  We will then have shift-reduce conflicts
8308  * which we can resolve in favor of always treating '(' <select> ')' as
8309  * a select_with_parens.  To resolve the conflicts, the productions that
8310  * conflict with the select_with_parens productions are manually given
8311  * precedences lower than the precedence of ')', thereby ensuring that we
8312  * shift ')' (and then reduce to select_with_parens) rather than trying to
8313  * reduce the inner <select> nonterminal to something else.  We use UMINUS
8314  * precedence for this, which is a fairly arbitrary choice.
8315  *
8316  * To be able to define select_with_parens itself without ambiguity, we need
8317  * a nonterminal select_no_parens that represents a SELECT structure with no
8318  * outermost parentheses.  This is a little bit tedious, but it works.
8319  *
8320  * In non-expression contexts, we use SelectStmt which can represent a SELECT
8321  * with or without outer parentheses.
8322  */
8323
8324 SelectStmt: select_no_parens                    %prec UMINUS
8325                         | select_with_parens            %prec UMINUS
8326                 ;
8327
8328 select_with_parens:
8329                         '(' select_no_parens ')'                                { $$ = $2; }
8330                         | '(' select_with_parens ')'                    { $$ = $2; }
8331                 ;
8332
8333 /*
8334  * This rule parses the equivalent of the standard's <query expression>.
8335  * The duplicative productions are annoying, but hard to get rid of without
8336  * creating shift/reduce conflicts.
8337  *
8338  *      FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
8339  *      In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
8340  *      We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
8341  *      2002-08-28 bjm
8342  */
8343 select_no_parens:
8344                         simple_select                                           { $$ = $1; }
8345                         | select_clause sort_clause
8346                                 {
8347                                         insertSelectOptions((SelectStmt *) $1, $2, NIL,
8348                                                                                 NULL, NULL, NULL,
8349                                                                                 yyscanner);
8350                                         $$ = $1;
8351                                 }
8352                         | select_clause opt_sort_clause for_locking_clause opt_select_limit
8353                                 {
8354                                         insertSelectOptions((SelectStmt *) $1, $2, $3,
8355                                                                                 list_nth($4, 0), list_nth($4, 1),
8356                                                                                 NULL,
8357                                                                                 yyscanner);
8358                                         $$ = $1;
8359                                 }
8360                         | select_clause opt_sort_clause select_limit opt_for_locking_clause
8361                                 {
8362                                         insertSelectOptions((SelectStmt *) $1, $2, $4,
8363                                                                                 list_nth($3, 0), list_nth($3, 1),
8364                                                                                 NULL,
8365                                                                                 yyscanner);
8366                                         $$ = $1;
8367                                 }
8368                         | with_clause select_clause
8369                                 {
8370                                         insertSelectOptions((SelectStmt *) $2, NULL, NIL,
8371                                                                                 NULL, NULL,
8372                                                                                 $1,
8373                                                                                 yyscanner);
8374                                         $$ = $2;
8375                                 }
8376                         | with_clause select_clause sort_clause
8377                                 {
8378                                         insertSelectOptions((SelectStmt *) $2, $3, NIL,
8379                                                                                 NULL, NULL,
8380                                                                                 $1,
8381                                                                                 yyscanner);
8382                                         $$ = $2;
8383                                 }
8384                         | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
8385                                 {
8386                                         insertSelectOptions((SelectStmt *) $2, $3, $4,
8387                                                                                 list_nth($5, 0), list_nth($5, 1),
8388                                                                                 $1,
8389                                                                                 yyscanner);
8390                                         $$ = $2;
8391                                 }
8392                         | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
8393                                 {
8394                                         insertSelectOptions((SelectStmt *) $2, $3, $5,
8395                                                                                 list_nth($4, 0), list_nth($4, 1),
8396                                                                                 $1,
8397                                                                                 yyscanner);
8398                                         $$ = $2;
8399                                 }
8400                 ;
8401
8402 select_clause:
8403                         simple_select                                                   { $$ = $1; }
8404                         | select_with_parens                                    { $$ = $1; }
8405                 ;
8406
8407 /*
8408  * This rule parses SELECT statements that can appear within set operations,
8409  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
8410  * the ordering of the set operations.  Without '(' and ')' we want the
8411  * operations to be ordered per the precedence specs at the head of this file.
8412  *
8413  * As with select_no_parens, simple_select cannot have outer parentheses,
8414  * but can have parenthesized subclauses.
8415  *
8416  * Note that sort clauses cannot be included at this level --- SQL92 requires
8417  *              SELECT foo UNION SELECT bar ORDER BY baz
8418  * to be parsed as
8419  *              (SELECT foo UNION SELECT bar) ORDER BY baz
8420  * not
8421  *              SELECT foo UNION (SELECT bar ORDER BY baz)
8422  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
8423  * described as part of the select_no_parens production, not simple_select.
8424  * This does not limit functionality, because you can reintroduce these
8425  * clauses inside parentheses.
8426  *
8427  * NOTE: only the leftmost component SelectStmt should have INTO.
8428  * However, this is not checked by the grammar; parse analysis must check it.
8429  */
8430 simple_select:
8431                         SELECT opt_distinct target_list
8432                         into_clause from_clause where_clause
8433                         group_clause having_clause window_clause
8434                                 {
8435                                         SelectStmt *n = makeNode(SelectStmt);
8436                                         n->distinctClause = $2;
8437                                         n->targetList = $3;
8438                                         n->intoClause = $4;
8439                                         n->fromClause = $5;
8440                                         n->whereClause = $6;
8441                                         n->groupClause = $7;
8442                                         n->havingClause = $8;
8443                                         n->windowClause = $9;
8444                                         $$ = (Node *)n;
8445                                 }
8446                         | values_clause                                                 { $$ = $1; }
8447                         | TABLE relation_expr
8448                                 {
8449                                         /* same as SELECT * FROM relation_expr */
8450                                         ColumnRef *cr = makeNode(ColumnRef);
8451                                         ResTarget *rt = makeNode(ResTarget);
8452                                         SelectStmt *n = makeNode(SelectStmt);
8453
8454                                         cr->fields = list_make1(makeNode(A_Star));
8455                                         cr->location = -1;
8456
8457                                         rt->name = NULL;
8458                                         rt->indirection = NIL;
8459                                         rt->val = (Node *)cr;
8460                                         rt->location = -1;
8461
8462                                         n->targetList = list_make1(rt);
8463                                         n->fromClause = list_make1($2);
8464                                         $$ = (Node *)n;
8465                                 }
8466                         | select_clause UNION opt_all select_clause
8467                                 {
8468                                         $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
8469                                 }
8470                         | select_clause INTERSECT opt_all select_clause
8471                                 {
8472                                         $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
8473                                 }
8474                         | select_clause EXCEPT opt_all select_clause
8475                                 {
8476                                         $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
8477                                 }
8478                 ;
8479
8480 /*
8481  * SQL standard WITH clause looks like:
8482  *
8483  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
8484  *              AS (query) [ SEARCH or CYCLE clause ]
8485  *
8486  * We don't currently support the SEARCH or CYCLE clause.
8487  */
8488 with_clause:
8489                 WITH cte_list
8490                         {
8491                                 $$ = makeNode(WithClause);
8492                                 $$->ctes = $2;
8493                                 $$->recursive = false;
8494                                 $$->location = @1;
8495                         }
8496                 | WITH RECURSIVE cte_list
8497                         {
8498                                 $$ = makeNode(WithClause);
8499                                 $$->ctes = $3;
8500                                 $$->recursive = true;
8501                                 $$->location = @1;
8502                         }
8503                 ;
8504
8505 cte_list:
8506                 common_table_expr                                               { $$ = list_make1($1); }
8507                 | cte_list ',' common_table_expr                { $$ = lappend($1, $3); }
8508                 ;
8509
8510 common_table_expr:  name opt_name_list AS '(' PreparableStmt ')'
8511                         {
8512                                 CommonTableExpr *n = makeNode(CommonTableExpr);
8513                                 n->ctename = $1;
8514                                 n->aliascolnames = $2;
8515                                 n->ctequery = $5;
8516                                 n->location = @1;
8517                                 $$ = (Node *) n;
8518                         }
8519                 ;
8520
8521 opt_with_clause:
8522                 with_clause                                                             { $$ = $1; }
8523                 | /*EMPTY*/                                                             { $$ = NULL; }
8524                 ;
8525
8526 into_clause:
8527                         INTO OptTempTableName
8528                                 {
8529                                         $$ = makeNode(IntoClause);
8530                                         $$->rel = $2;
8531                                         $$->colNames = NIL;
8532                                         $$->options = NIL;
8533                                         $$->onCommit = ONCOMMIT_NOOP;
8534                                         $$->tableSpaceName = NULL;
8535                                 }
8536                         | /*EMPTY*/
8537                                 { $$ = NULL; }
8538                 ;
8539
8540 /*
8541  * Redundancy here is needed to avoid shift/reduce conflicts,
8542  * since TEMP is not a reserved word.  See also OptTemp.
8543  */
8544 OptTempTableName:
8545                         TEMPORARY opt_table qualified_name
8546                                 {
8547                                         $$ = $3;
8548                                         $$->relpersistence = RELPERSISTENCE_TEMP;
8549                                 }
8550                         | TEMP opt_table qualified_name
8551                                 {
8552                                         $$ = $3;
8553                                         $$->relpersistence = RELPERSISTENCE_TEMP;
8554                                 }
8555                         | LOCAL TEMPORARY opt_table qualified_name
8556                                 {
8557                                         $$ = $4;
8558                                         $$->relpersistence = RELPERSISTENCE_TEMP;
8559                                 }
8560                         | LOCAL TEMP opt_table qualified_name
8561                                 {
8562                                         $$ = $4;
8563                                         $$->relpersistence = RELPERSISTENCE_TEMP;
8564                                 }
8565                         | GLOBAL TEMPORARY opt_table qualified_name
8566                                 {
8567                                         $$ = $4;
8568                                         $$->relpersistence = RELPERSISTENCE_TEMP;
8569                                 }
8570                         | GLOBAL TEMP opt_table qualified_name
8571                                 {
8572                                         $$ = $4;
8573                                         $$->relpersistence = RELPERSISTENCE_TEMP;
8574                                 }
8575                         | UNLOGGED opt_table qualified_name
8576                                 {
8577                                         $$ = $3;
8578                                         $$->relpersistence = RELPERSISTENCE_UNLOGGED;
8579                                 }
8580                         | TABLE qualified_name
8581                                 {
8582                                         $$ = $2;
8583                                         $$->relpersistence = RELPERSISTENCE_PERMANENT;
8584                                 }
8585                         | qualified_name
8586                                 {
8587                                         $$ = $1;
8588                                         $$->relpersistence = RELPERSISTENCE_PERMANENT;
8589                                 }
8590                 ;
8591
8592 opt_table:      TABLE                                                                   {}
8593                         | /*EMPTY*/                                                             {}
8594                 ;
8595
8596 opt_all:        ALL                                                                             { $$ = TRUE; }
8597                         | DISTINCT                                                              { $$ = FALSE; }
8598                         | /*EMPTY*/                                                             { $$ = FALSE; }
8599                 ;
8600
8601 /* We use (NIL) as a placeholder to indicate that all target expressions
8602  * should be placed in the DISTINCT list during parsetree analysis.
8603  */
8604 opt_distinct:
8605                         DISTINCT                                                                { $$ = list_make1(NIL); }
8606                         | DISTINCT ON '(' expr_list ')'                 { $$ = $4; }
8607                         | ALL                                                                   { $$ = NIL; }
8608                         | /*EMPTY*/                                                             { $$ = NIL; }
8609                 ;
8610
8611 opt_sort_clause:
8612                         sort_clause                                                             { $$ = $1;}
8613                         | /*EMPTY*/                                                             { $$ = NIL; }
8614                 ;
8615
8616 sort_clause:
8617                         ORDER BY sortby_list                                    { $$ = $3; }
8618                 ;
8619
8620 sortby_list:
8621                         sortby                                                                  { $$ = list_make1($1); }
8622                         | sortby_list ',' sortby                                { $$ = lappend($1, $3); }
8623                 ;
8624
8625 sortby:         a_expr USING qual_all_Op opt_nulls_order
8626                                 {
8627                                         $$ = makeNode(SortBy);
8628                                         $$->node = $1;
8629                                         $$->sortby_dir = SORTBY_USING;
8630                                         $$->sortby_nulls = $4;
8631                                         $$->useOp = $3;
8632                                         $$->location = @3;
8633                                 }
8634                         | a_expr opt_asc_desc opt_nulls_order
8635                                 {
8636                                         $$ = makeNode(SortBy);
8637                                         $$->node = $1;
8638                                         $$->sortby_dir = $2;
8639                                         $$->sortby_nulls = $3;
8640                                         $$->useOp = NIL;
8641                                         $$->location = -1;              /* no operator */
8642                                 }
8643                 ;
8644
8645
8646 select_limit:
8647                         limit_clause offset_clause                      { $$ = list_make2($2, $1); }
8648                         | offset_clause limit_clause            { $$ = list_make2($1, $2); }
8649                         | limit_clause                                          { $$ = list_make2(NULL, $1); }
8650                         | offset_clause                                         { $$ = list_make2($1, NULL); }
8651                 ;
8652
8653 opt_select_limit:
8654                         select_limit                                            { $$ = $1; }
8655                         | /* EMPTY */                                           { $$ = list_make2(NULL,NULL); }
8656                 ;
8657
8658 limit_clause:
8659                         LIMIT select_limit_value
8660                                 { $$ = $2; }
8661                         | LIMIT select_limit_value ',' select_offset_value
8662                                 {
8663                                         /* Disabled because it was too confusing, bjm 2002-02-18 */
8664                                         ereport(ERROR,
8665                                                         (errcode(ERRCODE_SYNTAX_ERROR),
8666                                                          errmsg("LIMIT #,# syntax is not supported"),
8667                                                          errhint("Use separate LIMIT and OFFSET clauses."),
8668                                                          parser_errposition(@1)));
8669                                 }
8670                         /* SQL:2008 syntax */
8671                         | FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
8672                                 { $$ = $3; }
8673                 ;
8674
8675 offset_clause:
8676                         OFFSET select_offset_value
8677                                 { $$ = $2; }
8678                         /* SQL:2008 syntax */
8679                         | OFFSET select_offset_value2 row_or_rows
8680                                 { $$ = $2; }
8681                 ;
8682
8683 select_limit_value:
8684                         a_expr                                                                  { $$ = $1; }
8685                         | ALL
8686                                 {
8687                                         /* LIMIT ALL is represented as a NULL constant */
8688                                         $$ = makeNullAConst(@1);
8689                                 }
8690                 ;
8691
8692 select_offset_value:
8693                         a_expr                                                                  { $$ = $1; }
8694                 ;
8695
8696 /*
8697  * Allowing full expressions without parentheses causes various parsing
8698  * problems with the trailing ROW/ROWS key words.  SQL only calls for
8699  * constants, so we allow the rest only with parentheses.  If omitted,
8700  * default to 1.
8701  */
8702 opt_select_fetch_first_value:
8703                         SignedIconst                                            { $$ = makeIntConst($1, @1); }
8704                         | '(' a_expr ')'                                        { $$ = $2; }
8705                         | /*EMPTY*/                                                     { $$ = makeIntConst(1, -1); }
8706                 ;
8707
8708 /*
8709  * Again, the trailing ROW/ROWS in this case prevent the full expression
8710  * syntax.  c_expr is the best we can do.
8711  */
8712 select_offset_value2:
8713                         c_expr                                                                  { $$ = $1; }
8714                 ;
8715
8716 /* noise words */
8717 row_or_rows: ROW                                                                        { $$ = 0; }
8718                         | ROWS                                                                  { $$ = 0; }
8719                 ;
8720
8721 first_or_next: FIRST_P                                                          { $$ = 0; }
8722                         | NEXT                                                                  { $$ = 0; }
8723                 ;
8724
8725
8726 group_clause:
8727                         GROUP_P BY expr_list                                    { $$ = $3; }
8728                         | /*EMPTY*/                                                             { $$ = NIL; }
8729                 ;
8730
8731 having_clause:
8732                         HAVING a_expr                                                   { $$ = $2; }
8733                         | /*EMPTY*/                                                             { $$ = NULL; }
8734                 ;
8735
8736 for_locking_clause:
8737                         for_locking_items                                               { $$ = $1; }
8738                         | FOR READ ONLY                                                 { $$ = NIL; }
8739                 ;
8740
8741 opt_for_locking_clause:
8742                         for_locking_clause                                              { $$ = $1; }
8743                         | /* EMPTY */                                                   { $$ = NIL; }
8744                 ;
8745
8746 for_locking_items:
8747                         for_locking_item                                                { $$ = list_make1($1); }
8748                         | for_locking_items for_locking_item    { $$ = lappend($1, $2); }
8749                 ;
8750
8751 for_locking_item:
8752                         FOR UPDATE locked_rels_list opt_nowait
8753                                 {
8754                                         LockingClause *n = makeNode(LockingClause);
8755                                         n->lockedRels = $3;
8756                                         n->forUpdate = TRUE;
8757                                         n->noWait = $4;
8758                                         $$ = (Node *) n;
8759                                 }
8760                         | FOR SHARE locked_rels_list opt_nowait
8761                                 {
8762                                         LockingClause *n = makeNode(LockingClause);
8763                                         n->lockedRels = $3;
8764                                         n->forUpdate = FALSE;
8765                                         n->noWait = $4;
8766                                         $$ = (Node *) n;
8767                                 }
8768                 ;
8769
8770 locked_rels_list:
8771                         OF qualified_name_list                                  { $$ = $2; }
8772                         | /* EMPTY */                                                   { $$ = NIL; }
8773                 ;
8774
8775
8776 values_clause:
8777                         VALUES ctext_row
8778                                 {
8779                                         SelectStmt *n = makeNode(SelectStmt);
8780                                         n->valuesLists = list_make1($2);
8781                                         $$ = (Node *) n;
8782                                 }
8783                         | values_clause ',' ctext_row
8784                                 {
8785                                         SelectStmt *n = (SelectStmt *) $1;
8786                                         n->valuesLists = lappend(n->valuesLists, $3);
8787                                         $$ = (Node *) n;
8788                                 }
8789                 ;
8790
8791
8792 /*****************************************************************************
8793  *
8794  *      clauses common to all Optimizable Stmts:
8795  *              from_clause             - allow list of both JOIN expressions and table names
8796  *              where_clause    - qualifications for joins or restrictions
8797  *
8798  *****************************************************************************/
8799
8800 from_clause:
8801                         FROM from_list                                                  { $$ = $2; }
8802                         | /*EMPTY*/                                                             { $$ = NIL; }
8803                 ;
8804
8805 from_list:
8806                         table_ref                                                               { $$ = list_make1($1); }
8807                         | from_list ',' table_ref                               { $$ = lappend($1, $3); }
8808                 ;
8809
8810 /*
8811  * table_ref is where an alias clause can be attached.  Note we cannot make
8812  * alias_clause have an empty production because that causes parse conflicts
8813  * between table_ref := '(' joined_table ')' alias_clause
8814  * and joined_table := '(' joined_table ')'.  So, we must have the
8815  * redundant-looking productions here instead.
8816  */
8817 table_ref:      relation_expr
8818                                 {
8819                                         $$ = (Node *) $1;
8820                                 }
8821                         | relation_expr alias_clause
8822                                 {
8823                                         $1->alias = $2;
8824                                         $$ = (Node *) $1;
8825                                 }
8826                         | func_table
8827                                 {
8828                                         RangeFunction *n = makeNode(RangeFunction);
8829                                         n->funccallnode = $1;
8830                                         n->coldeflist = NIL;
8831                                         $$ = (Node *) n;
8832                                 }
8833                         | func_table alias_clause
8834                                 {
8835                                         RangeFunction *n = makeNode(RangeFunction);
8836                                         n->funccallnode = $1;
8837                                         n->alias = $2;
8838                                         n->coldeflist = NIL;
8839                                         $$ = (Node *) n;
8840                                 }
8841                         | func_table AS '(' TableFuncElementList ')'
8842                                 {
8843                                         RangeFunction *n = makeNode(RangeFunction);
8844                                         n->funccallnode = $1;
8845                                         n->coldeflist = $4;
8846                                         $$ = (Node *) n;
8847                                 }
8848                         | func_table AS ColId '(' TableFuncElementList ')'
8849                                 {
8850                                         RangeFunction *n = makeNode(RangeFunction);
8851                                         Alias *a = makeNode(Alias);
8852                                         n->funccallnode = $1;
8853                                         a->aliasname = $3;
8854                                         n->alias = a;
8855                                         n->coldeflist = $5;
8856                                         $$ = (Node *) n;
8857                                 }
8858                         | func_table ColId '(' TableFuncElementList ')'
8859                                 {
8860                                         RangeFunction *n = makeNode(RangeFunction);
8861                                         Alias *a = makeNode(Alias);
8862                                         n->funccallnode = $1;
8863                                         a->aliasname = $2;
8864                                         n->alias = a;
8865                                         n->coldeflist = $4;
8866                                         $$ = (Node *) n;
8867                                 }
8868                         | select_with_parens
8869                                 {
8870                                         /*
8871                                          * The SQL spec does not permit a subselect
8872                                          * (<derived_table>) without an alias clause,
8873                                          * so we don't either.  This avoids the problem
8874                                          * of needing to invent a unique refname for it.
8875                                          * That could be surmounted if there's sufficient
8876                                          * popular demand, but for now let's just implement
8877                                          * the spec and see if anyone complains.
8878                                          * However, it does seem like a good idea to emit
8879                                          * an error message that's better than "syntax error".
8880                                          */
8881                                         if (IsA($1, SelectStmt) &&
8882                                                 ((SelectStmt *) $1)->valuesLists)
8883                                                 ereport(ERROR,
8884                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
8885                                                                  errmsg("VALUES in FROM must have an alias"),
8886                                                                  errhint("For example, FROM (VALUES ...) [AS] foo."),
8887                                                                  parser_errposition(@1)));
8888                                         else
8889                                                 ereport(ERROR,
8890                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
8891                                                                  errmsg("subquery in FROM must have an alias"),
8892                                                                  errhint("For example, FROM (SELECT ...) [AS] foo."),
8893                                                                  parser_errposition(@1)));
8894                                         $$ = NULL;
8895                                 }
8896                         | select_with_parens alias_clause
8897                                 {
8898                                         RangeSubselect *n = makeNode(RangeSubselect);
8899                                         n->subquery = $1;
8900                                         n->alias = $2;
8901                                         $$ = (Node *) n;
8902                                 }
8903                         | joined_table
8904                                 {
8905                                         $$ = (Node *) $1;
8906                                 }
8907                         | '(' joined_table ')' alias_clause
8908                                 {
8909                                         $2->alias = $4;
8910                                         $$ = (Node *) $2;
8911                                 }
8912                 ;
8913
8914
8915 /*
8916  * It may seem silly to separate joined_table from table_ref, but there is
8917  * method in SQL92's madness: if you don't do it this way you get reduce-
8918  * reduce conflicts, because it's not clear to the parser generator whether
8919  * to expect alias_clause after ')' or not.  For the same reason we must
8920  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
8921  * join_type to expand to empty; if we try it, the parser generator can't
8922  * figure out when to reduce an empty join_type right after table_ref.
8923  *
8924  * Note that a CROSS JOIN is the same as an unqualified
8925  * INNER JOIN, and an INNER JOIN/ON has the same shape
8926  * but a qualification expression to limit membership.
8927  * A NATURAL JOIN implicitly matches column names between
8928  * tables and the shape is determined by which columns are
8929  * in common. We'll collect columns during the later transformations.
8930  */
8931
8932 joined_table:
8933                         '(' joined_table ')'
8934                                 {
8935                                         $$ = $2;
8936                                 }
8937                         | table_ref CROSS JOIN table_ref
8938                                 {
8939                                         /* CROSS JOIN is same as unqualified inner join */
8940                                         JoinExpr *n = makeNode(JoinExpr);
8941                                         n->jointype = JOIN_INNER;
8942                                         n->isNatural = FALSE;
8943                                         n->larg = $1;
8944                                         n->rarg = $4;
8945                                         n->usingClause = NIL;
8946                                         n->quals = NULL;
8947                                         $$ = n;
8948                                 }
8949                         | table_ref join_type JOIN table_ref join_qual
8950                                 {
8951                                         JoinExpr *n = makeNode(JoinExpr);
8952                                         n->jointype = $2;
8953                                         n->isNatural = FALSE;
8954                                         n->larg = $1;
8955                                         n->rarg = $4;
8956                                         if ($5 != NULL && IsA($5, List))
8957                                                 n->usingClause = (List *) $5; /* USING clause */
8958                                         else
8959                                                 n->quals = $5; /* ON clause */
8960                                         $$ = n;
8961                                 }
8962                         | table_ref JOIN table_ref join_qual
8963                                 {
8964                                         /* letting join_type reduce to empty doesn't work */
8965                                         JoinExpr *n = makeNode(JoinExpr);
8966                                         n->jointype = JOIN_INNER;
8967                                         n->isNatural = FALSE;
8968                                         n->larg = $1;
8969                                         n->rarg = $3;
8970                                         if ($4 != NULL && IsA($4, List))
8971                                                 n->usingClause = (List *) $4; /* USING clause */
8972                                         else
8973                                                 n->quals = $4; /* ON clause */
8974                                         $$ = n;
8975                                 }
8976                         | table_ref NATURAL join_type JOIN table_ref
8977                                 {
8978                                         JoinExpr *n = makeNode(JoinExpr);
8979                                         n->jointype = $3;
8980                                         n->isNatural = TRUE;
8981                                         n->larg = $1;
8982                                         n->rarg = $5;
8983                                         n->usingClause = NIL; /* figure out which columns later... */
8984                                         n->quals = NULL; /* fill later */
8985                                         $$ = n;
8986                                 }
8987                         | table_ref NATURAL JOIN table_ref
8988                                 {
8989                                         /* letting join_type reduce to empty doesn't work */
8990                                         JoinExpr *n = makeNode(JoinExpr);
8991                                         n->jointype = JOIN_INNER;
8992                                         n->isNatural = TRUE;
8993                                         n->larg = $1;
8994                                         n->rarg = $4;
8995                                         n->usingClause = NIL; /* figure out which columns later... */
8996                                         n->quals = NULL; /* fill later */
8997                                         $$ = n;
8998                                 }
8999                 ;
9000
9001 alias_clause:
9002                         AS ColId '(' name_list ')'
9003                                 {
9004                                         $$ = makeNode(Alias);
9005                                         $$->aliasname = $2;
9006                                         $$->colnames = $4;
9007                                 }
9008                         | AS ColId
9009                                 {
9010                                         $$ = makeNode(Alias);
9011                                         $$->aliasname = $2;
9012                                 }
9013                         | ColId '(' name_list ')'
9014                                 {
9015                                         $$ = makeNode(Alias);
9016                                         $$->aliasname = $1;
9017                                         $$->colnames = $3;
9018                                 }
9019                         | ColId
9020                                 {
9021                                         $$ = makeNode(Alias);
9022                                         $$->aliasname = $1;
9023                                 }
9024                 ;
9025
9026 join_type:      FULL join_outer                                                 { $$ = JOIN_FULL; }
9027                         | LEFT join_outer                                               { $$ = JOIN_LEFT; }
9028                         | RIGHT join_outer                                              { $$ = JOIN_RIGHT; }
9029                         | INNER_P                                                               { $$ = JOIN_INNER; }
9030                 ;
9031
9032 /* OUTER is just noise... */
9033 join_outer: OUTER_P                                                                     { $$ = NULL; }
9034                         | /*EMPTY*/                                                             { $$ = NULL; }
9035                 ;
9036
9037 /* JOIN qualification clauses
9038  * Possibilities are:
9039  *      USING ( column list ) allows only unqualified column names,
9040  *                                                which must match between tables.
9041  *      ON expr allows more general qualifications.
9042  *
9043  * We return USING as a List node, while an ON-expr will not be a List.
9044  */
9045
9046 join_qual:      USING '(' name_list ')'                                 { $$ = (Node *) $3; }
9047                         | ON a_expr                                                             { $$ = $2; }
9048                 ;
9049
9050
9051 relation_expr:
9052                         qualified_name
9053                                 {
9054                                         /* default inheritance */
9055                                         $$ = $1;
9056                                         $$->inhOpt = INH_DEFAULT;
9057                                         $$->alias = NULL;
9058                                 }
9059                         | qualified_name '*'
9060                                 {
9061                                         /* inheritance query */
9062                                         $$ = $1;
9063                                         $$->inhOpt = INH_YES;
9064                                         $$->alias = NULL;
9065                                 }
9066                         | ONLY qualified_name
9067                                 {
9068                                         /* no inheritance */
9069                                         $$ = $2;
9070                                         $$->inhOpt = INH_NO;
9071                                         $$->alias = NULL;
9072                                 }
9073                         | ONLY '(' qualified_name ')'
9074                                 {
9075                                         /* no inheritance, SQL99-style syntax */
9076                                         $$ = $3;
9077                                         $$->inhOpt = INH_NO;
9078                                         $$->alias = NULL;
9079                                 }
9080                 ;
9081
9082
9083 relation_expr_list:
9084                         relation_expr                                                   { $$ = list_make1($1); }
9085                         | relation_expr_list ',' relation_expr  { $$ = lappend($1, $3); }
9086                 ;
9087
9088
9089 /*
9090  * Given "UPDATE foo set set ...", we have to decide without looking any
9091  * further ahead whether the first "set" is an alias or the UPDATE's SET
9092  * keyword.  Since "set" is allowed as a column name both interpretations
9093  * are feasible.  We resolve the shift/reduce conflict by giving the first
9094  * relation_expr_opt_alias production a higher precedence than the SET token
9095  * has, causing the parser to prefer to reduce, in effect assuming that the
9096  * SET is not an alias.
9097  */
9098 relation_expr_opt_alias: relation_expr                                  %prec UMINUS
9099                                 {
9100                                         $$ = $1;
9101                                 }
9102                         | relation_expr ColId
9103                                 {
9104                                         Alias *alias = makeNode(Alias);
9105                                         alias->aliasname = $2;
9106                                         $1->alias = alias;
9107                                         $$ = $1;
9108                                 }
9109                         | relation_expr AS ColId
9110                                 {
9111                                         Alias *alias = makeNode(Alias);
9112                                         alias->aliasname = $3;
9113                                         $1->alias = alias;
9114                                         $$ = $1;
9115                                 }
9116                 ;
9117
9118
9119 func_table: func_expr                                                           { $$ = $1; }
9120                 ;
9121
9122
9123 where_clause:
9124                         WHERE a_expr                                                    { $$ = $2; }
9125                         | /*EMPTY*/                                                             { $$ = NULL; }
9126                 ;
9127
9128 /* variant for UPDATE and DELETE */
9129 where_or_current_clause:
9130                         WHERE a_expr                                                    { $$ = $2; }
9131                         | WHERE CURRENT_P OF cursor_name
9132                                 {
9133                                         CurrentOfExpr *n = makeNode(CurrentOfExpr);
9134                                         /* cvarno is filled in by parse analysis */
9135                                         n->cursor_name = $4;
9136                                         n->cursor_param = 0;
9137                                         $$ = (Node *) n;
9138                                 }
9139                         | /*EMPTY*/                                                             { $$ = NULL; }
9140                 ;
9141
9142
9143 OptTableFuncElementList:
9144                         TableFuncElementList                            { $$ = $1; }
9145                         | /*EMPTY*/                                                     { $$ = NIL; }
9146                 ;
9147
9148 TableFuncElementList:
9149                         TableFuncElement
9150                                 {
9151                                         $$ = list_make1($1);
9152                                 }
9153                         | TableFuncElementList ',' TableFuncElement
9154                                 {
9155                                         $$ = lappend($1, $3);
9156                                 }
9157                 ;
9158
9159 TableFuncElement:       ColId Typename opt_collate_clause
9160                                 {
9161                                         ColumnDef *n = makeNode(ColumnDef);
9162                                         n->colname = $1;
9163                                         n->typeName = $2;
9164                                         n->inhcount = 0;
9165                                         n->is_local = true;
9166                                         n->is_not_null = false;
9167                                         n->is_from_type = false;
9168                                         n->storage = 0;
9169                                         n->raw_default = NULL;
9170                                         n->cooked_default = NULL;
9171                                         n->collClause = (CollateClause *) $3;
9172                                         n->collOid = InvalidOid;
9173                                         n->constraints = NIL;
9174                                         $$ = (Node *)n;
9175                                 }
9176                 ;
9177
9178 /*****************************************************************************
9179  *
9180  *      Type syntax
9181  *              SQL92 introduces a large amount of type-specific syntax.
9182  *              Define individual clauses to handle these cases, and use
9183  *               the generic case to handle regular type-extensible Postgres syntax.
9184  *              - thomas 1997-10-10
9185  *
9186  *****************************************************************************/
9187
9188 Typename:       SimpleTypename opt_array_bounds
9189                                 {
9190                                         $$ = $1;
9191                                         $$->arrayBounds = $2;
9192                                 }
9193                         | SETOF SimpleTypename opt_array_bounds
9194                                 {
9195                                         $$ = $2;
9196                                         $$->arrayBounds = $3;
9197                                         $$->setof = TRUE;
9198                                 }
9199                         /* SQL standard syntax, currently only one-dimensional */
9200                         | SimpleTypename ARRAY '[' Iconst ']'
9201                                 {
9202                                         $$ = $1;
9203                                         $$->arrayBounds = list_make1(makeInteger($4));
9204                                 }
9205                         | SETOF SimpleTypename ARRAY '[' Iconst ']'
9206                                 {
9207                                         $$ = $2;
9208                                         $$->arrayBounds = list_make1(makeInteger($5));
9209                                         $$->setof = TRUE;
9210                                 }
9211                         | SimpleTypename ARRAY
9212                                 {
9213                                         $$ = $1;
9214                                         $$->arrayBounds = list_make1(makeInteger(-1));
9215                                 }
9216                         | SETOF SimpleTypename ARRAY
9217                                 {
9218                                         $$ = $2;
9219                                         $$->arrayBounds = list_make1(makeInteger(-1));
9220                                         $$->setof = TRUE;
9221                                 }
9222                 ;
9223
9224 opt_array_bounds:
9225                         opt_array_bounds '[' ']'
9226                                         {  $$ = lappend($1, makeInteger(-1)); }
9227                         | opt_array_bounds '[' Iconst ']'
9228                                         {  $$ = lappend($1, makeInteger($3)); }
9229                         | /*EMPTY*/
9230                                         {  $$ = NIL; }
9231                 ;
9232
9233 SimpleTypename:
9234                         GenericType                                                             { $$ = $1; }
9235                         | Numeric                                                               { $$ = $1; }
9236                         | Bit                                                                   { $$ = $1; }
9237                         | Character                                                             { $$ = $1; }
9238                         | ConstDatetime                                                 { $$ = $1; }
9239                         | ConstInterval opt_interval
9240                                 {
9241                                         $$ = $1;
9242                                         $$->typmods = $2;
9243                                 }
9244                         | ConstInterval '(' Iconst ')' opt_interval
9245                                 {
9246                                         $$ = $1;
9247                                         if ($5 != NIL)
9248                                         {
9249                                                 if (list_length($5) != 1)
9250                                                         ereport(ERROR,
9251                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
9252                                                                          errmsg("interval precision specified twice"),
9253                                                                          parser_errposition(@1)));
9254                                                 $$->typmods = lappend($5, makeIntConst($3, @3));
9255                                         }
9256                                         else
9257                                                 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
9258                                                                                                  makeIntConst($3, @3));
9259                                 }
9260                 ;
9261
9262 /* We have a separate ConstTypename to allow defaulting fixed-length
9263  * types such as CHAR() and BIT() to an unspecified length.
9264  * SQL9x requires that these default to a length of one, but this
9265  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
9266  * where there is an obvious better choice to make.
9267  * Note that ConstInterval is not included here since it must
9268  * be pushed up higher in the rules to accomodate the postfix
9269  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
9270  * the generic-type-name case in AExprConst to avoid premature
9271  * reduce/reduce conflicts against function names.
9272  */
9273 ConstTypename:
9274                         Numeric                                                                 { $$ = $1; }
9275                         | ConstBit                                                              { $$ = $1; }
9276                         | ConstCharacter                                                { $$ = $1; }
9277                         | ConstDatetime                                                 { $$ = $1; }
9278                 ;
9279
9280 /*
9281  * GenericType covers all type names that don't have special syntax mandated
9282  * by the standard, including qualified names.  We also allow type modifiers.
9283  * To avoid parsing conflicts against function invocations, the modifiers
9284  * have to be shown as expr_list here, but parse analysis will only accept
9285  * constants for them.
9286  */
9287 GenericType:
9288                         type_function_name opt_type_modifiers
9289                                 {
9290                                         $$ = makeTypeName($1);
9291                                         $$->typmods = $2;
9292                                         $$->location = @1;
9293                                 }
9294                         | type_function_name attrs opt_type_modifiers
9295                                 {
9296                                         $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
9297                                         $$->typmods = $3;
9298                                         $$->location = @1;
9299                                 }
9300                 ;
9301
9302 opt_type_modifiers: '(' expr_list ')'                           { $$ = $2; }
9303                                         | /* EMPTY */                                   { $$ = NIL; }
9304                 ;
9305
9306 /*
9307  * SQL92 numeric data types
9308  */
9309 Numeric:        INT_P
9310                                 {
9311                                         $$ = SystemTypeName("int4");
9312                                         $$->location = @1;
9313                                 }
9314                         | INTEGER
9315                                 {
9316                                         $$ = SystemTypeName("int4");
9317                                         $$->location = @1;
9318                                 }
9319                         | SMALLINT
9320                                 {
9321                                         $$ = SystemTypeName("int2");
9322                                         $$->location = @1;
9323                                 }
9324                         | BIGINT
9325                                 {
9326                                         $$ = SystemTypeName("int8");
9327                                         $$->location = @1;
9328                                 }
9329                         | REAL
9330                                 {
9331                                         $$ = SystemTypeName("float4");
9332                                         $$->location = @1;
9333                                 }
9334                         | FLOAT_P opt_float
9335                                 {
9336                                         $$ = $2;
9337                                         $$->location = @1;
9338                                 }
9339                         | DOUBLE_P PRECISION
9340                                 {
9341                                         $$ = SystemTypeName("float8");
9342                                         $$->location = @1;
9343                                 }
9344                         | DECIMAL_P opt_type_modifiers
9345                                 {
9346                                         $$ = SystemTypeName("numeric");
9347                                         $$->typmods = $2;
9348                                         $$->location = @1;
9349                                 }
9350                         | DEC opt_type_modifiers
9351                                 {
9352                                         $$ = SystemTypeName("numeric");
9353                                         $$->typmods = $2;
9354                                         $$->location = @1;
9355                                 }
9356                         | NUMERIC opt_type_modifiers
9357                                 {
9358                                         $$ = SystemTypeName("numeric");
9359                                         $$->typmods = $2;
9360                                         $$->location = @1;
9361                                 }
9362                         | BOOLEAN_P
9363                                 {
9364                                         $$ = SystemTypeName("bool");
9365                                         $$->location = @1;
9366                                 }
9367                 ;
9368
9369 opt_float:      '(' Iconst ')'
9370                                 {
9371                                         /*
9372                                          * Check FLOAT() precision limits assuming IEEE floating
9373                                          * types - thomas 1997-09-18
9374                                          */
9375                                         if ($2 < 1)
9376                                                 ereport(ERROR,
9377                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
9378                                                                  errmsg("precision for type float must be at least 1 bit"),
9379                                                                  parser_errposition(@2)));
9380                                         else if ($2 <= 24)
9381                                                 $$ = SystemTypeName("float4");
9382                                         else if ($2 <= 53)
9383                                                 $$ = SystemTypeName("float8");
9384                                         else
9385                                                 ereport(ERROR,
9386                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
9387                                                                  errmsg("precision for type float must be less than 54 bits"),
9388                                                                  parser_errposition(@2)));
9389                                 }
9390                         | /*EMPTY*/
9391                                 {
9392                                         $$ = SystemTypeName("float8");
9393                                 }
9394                 ;
9395
9396 /*
9397  * SQL92 bit-field data types
9398  * The following implements BIT() and BIT VARYING().
9399  */
9400 Bit:            BitWithLength
9401                                 {
9402                                         $$ = $1;
9403                                 }
9404                         | BitWithoutLength
9405                                 {
9406                                         $$ = $1;
9407                                 }
9408                 ;
9409
9410 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
9411 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
9412 ConstBit:       BitWithLength
9413                                 {
9414                                         $$ = $1;
9415                                 }
9416                         | BitWithoutLength
9417                                 {
9418                                         $$ = $1;
9419                                         $$->typmods = NIL;
9420                                 }
9421                 ;
9422
9423 BitWithLength:
9424                         BIT opt_varying '(' expr_list ')'
9425                                 {
9426                                         char *typname;
9427
9428                                         typname = $2 ? "varbit" : "bit";
9429                                         $$ = SystemTypeName(typname);
9430                                         $$->typmods = $4;
9431                                         $$->location = @1;
9432                                 }
9433                 ;
9434
9435 BitWithoutLength:
9436                         BIT opt_varying
9437                                 {
9438                                         /* bit defaults to bit(1), varbit to no limit */
9439                                         if ($2)
9440                                         {
9441                                                 $$ = SystemTypeName("varbit");
9442                                         }
9443                                         else
9444                                         {
9445                                                 $$ = SystemTypeName("bit");
9446                                                 $$->typmods = list_make1(makeIntConst(1, -1));
9447                                         }
9448                                         $$->location = @1;
9449                                 }
9450                 ;
9451
9452
9453 /*
9454  * SQL92 character data types
9455  * The following implements CHAR() and VARCHAR().
9456  */
9457 Character:  CharacterWithLength
9458                                 {
9459                                         $$ = $1;
9460                                 }
9461                         | CharacterWithoutLength
9462                                 {
9463                                         $$ = $1;
9464                                 }
9465                 ;
9466
9467 ConstCharacter:  CharacterWithLength
9468                                 {
9469                                         $$ = $1;
9470                                 }
9471                         | CharacterWithoutLength
9472                                 {
9473                                         /* Length was not specified so allow to be unrestricted.
9474                                          * This handles problems with fixed-length (bpchar) strings
9475                                          * which in column definitions must default to a length
9476                                          * of one, but should not be constrained if the length
9477                                          * was not specified.
9478                                          */
9479                                         $$ = $1;
9480                                         $$->typmods = NIL;
9481                                 }
9482                 ;
9483
9484 CharacterWithLength:  character '(' Iconst ')' opt_charset
9485                                 {
9486                                         if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
9487                                         {
9488                                                 char *type;
9489
9490                                                 type = palloc(strlen($1) + 1 + strlen($5) + 1);
9491                                                 strcpy(type, $1);
9492                                                 strcat(type, "_");
9493                                                 strcat(type, $5);
9494                                                 $1 = type;
9495                                         }
9496
9497                                         $$ = SystemTypeName($1);
9498                                         $$->typmods = list_make1(makeIntConst($3, @3));
9499                                         $$->location = @1;
9500                                 }
9501                 ;
9502
9503 CharacterWithoutLength:  character opt_charset
9504                                 {
9505                                         if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
9506                                         {
9507                                                 char *type;
9508
9509                                                 type = palloc(strlen($1) + 1 + strlen($2) + 1);
9510                                                 strcpy(type, $1);
9511                                                 strcat(type, "_");
9512                                                 strcat(type, $2);
9513                                                 $1 = type;
9514                                         }
9515
9516                                         $$ = SystemTypeName($1);
9517
9518                                         /* char defaults to char(1), varchar to no limit */
9519                                         if (strcmp($1, "bpchar") == 0)
9520                                                 $$->typmods = list_make1(makeIntConst(1, -1));
9521
9522                                         $$->location = @1;
9523                                 }
9524                 ;
9525
9526 character:      CHARACTER opt_varying
9527                                                                                 { $$ = $2 ? "varchar": "bpchar"; }
9528                         | CHAR_P opt_varying
9529                                                                                 { $$ = $2 ? "varchar": "bpchar"; }
9530                         | VARCHAR
9531                                                                                 { $$ = "varchar"; }
9532                         | NATIONAL CHARACTER opt_varying
9533                                                                                 { $$ = $3 ? "varchar": "bpchar"; }
9534                         | NATIONAL CHAR_P opt_varying
9535                                                                                 { $$ = $3 ? "varchar": "bpchar"; }
9536                         | NCHAR opt_varying
9537                                                                                 { $$ = $2 ? "varchar": "bpchar"; }
9538                 ;
9539
9540 opt_varying:
9541                         VARYING                                                                 { $$ = TRUE; }
9542                         | /*EMPTY*/                                                             { $$ = FALSE; }
9543                 ;
9544
9545 opt_charset:
9546                         CHARACTER SET ColId                                             { $$ = $3; }
9547                         | /*EMPTY*/                                                             { $$ = NULL; }
9548                 ;
9549
9550 /*
9551  * SQL92 date/time types
9552  */
9553 ConstDatetime:
9554                         TIMESTAMP '(' Iconst ')' opt_timezone
9555                                 {
9556                                         if ($5)
9557                                                 $$ = SystemTypeName("timestamptz");
9558                                         else
9559                                                 $$ = SystemTypeName("timestamp");
9560                                         $$->typmods = list_make1(makeIntConst($3, @3));
9561                                         $$->location = @1;
9562                                 }
9563                         | TIMESTAMP opt_timezone
9564                                 {
9565                                         if ($2)
9566                                                 $$ = SystemTypeName("timestamptz");
9567                                         else
9568                                                 $$ = SystemTypeName("timestamp");
9569                                         $$->location = @1;
9570                                 }
9571                         | TIME '(' Iconst ')' opt_timezone
9572                                 {
9573                                         if ($5)
9574                                                 $$ = SystemTypeName("timetz");
9575                                         else
9576                                                 $$ = SystemTypeName("time");
9577                                         $$->typmods = list_make1(makeIntConst($3, @3));
9578                                         $$->location = @1;
9579                                 }
9580                         | TIME opt_timezone
9581                                 {
9582                                         if ($2)
9583                                                 $$ = SystemTypeName("timetz");
9584                                         else
9585                                                 $$ = SystemTypeName("time");
9586                                         $$->location = @1;
9587                                 }
9588                 ;
9589
9590 ConstInterval:
9591                         INTERVAL
9592                                 {
9593                                         $$ = SystemTypeName("interval");
9594                                         $$->location = @1;
9595                                 }
9596                 ;
9597
9598 opt_timezone:
9599                         WITH_TIME ZONE                                                  { $$ = TRUE; }
9600                         | WITHOUT TIME ZONE                                             { $$ = FALSE; }
9601                         | /*EMPTY*/                                                             { $$ = FALSE; }
9602                 ;
9603
9604 opt_interval:
9605                         YEAR_P
9606                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
9607                         | MONTH_P
9608                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
9609                         | DAY_P
9610                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
9611                         | HOUR_P
9612                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
9613                         | MINUTE_P
9614                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
9615                         | interval_second
9616                                 { $$ = $1; }
9617                         | YEAR_P TO MONTH_P
9618                                 {
9619                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
9620                                                                                                  INTERVAL_MASK(MONTH), @1));
9621                                 }
9622                         | DAY_P TO HOUR_P
9623                                 {
9624                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
9625                                                                                                  INTERVAL_MASK(HOUR), @1));
9626                                 }
9627                         | DAY_P TO MINUTE_P
9628                                 {
9629                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
9630                                                                                                  INTERVAL_MASK(HOUR) |
9631                                                                                                  INTERVAL_MASK(MINUTE), @1));
9632                                 }
9633                         | DAY_P TO interval_second
9634                                 {
9635                                         $$ = $3;
9636                                         linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
9637                                                                                                 INTERVAL_MASK(HOUR) |
9638                                                                                                 INTERVAL_MASK(MINUTE) |
9639                                                                                                 INTERVAL_MASK(SECOND), @1);
9640                                 }
9641                         | HOUR_P TO MINUTE_P
9642                                 {
9643                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
9644                                                                                                  INTERVAL_MASK(MINUTE), @1));
9645                                 }
9646                         | HOUR_P TO interval_second
9647                                 {
9648                                         $$ = $3;
9649                                         linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
9650                                                                                                 INTERVAL_MASK(MINUTE) |
9651                                                                                                 INTERVAL_MASK(SECOND), @1);
9652                                 }
9653                         | MINUTE_P TO interval_second
9654                                 {
9655                                         $$ = $3;
9656                                         linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
9657                                                                                                 INTERVAL_MASK(SECOND), @1);
9658                                 }
9659                         | /*EMPTY*/
9660                                 { $$ = NIL; }
9661                 ;
9662
9663 interval_second:
9664                         SECOND_P
9665                                 {
9666                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
9667                                 }
9668                         | SECOND_P '(' Iconst ')'
9669                                 {
9670                                         $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
9671                                                                         makeIntConst($3, @3));
9672                                 }
9673                 ;
9674
9675
9676 /*****************************************************************************
9677  *
9678  *      expression grammar
9679  *
9680  *****************************************************************************/
9681
9682 /*
9683  * General expressions
9684  * This is the heart of the expression syntax.
9685  *
9686  * We have two expression types: a_expr is the unrestricted kind, and
9687  * b_expr is a subset that must be used in some places to avoid shift/reduce
9688  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
9689  * because that use of AND conflicts with AND as a boolean operator.  So,
9690  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
9691  *
9692  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
9693  * always be used by surrounding it with parens.
9694  *
9695  * c_expr is all the productions that are common to a_expr and b_expr;
9696  * it's factored out just to eliminate redundant coding.
9697  */
9698 a_expr:         c_expr                                                                  { $$ = $1; }
9699                         | a_expr TYPECAST Typename
9700                                         { $$ = makeTypeCast($1, $3, @2); }
9701                         | a_expr COLLATE any_name
9702                                 {
9703                                         CollateClause *n = makeNode(CollateClause);
9704                                         n->arg = $1;
9705                                         n->collname = $3;
9706                                         n->location = @2;
9707                                         $$ = (Node *) n;
9708                                 }
9709                         | a_expr AT TIME ZONE a_expr                    %prec AT
9710                                 {
9711                                         FuncCall *n = makeNode(FuncCall);
9712                                         n->funcname = SystemFuncName("timezone");
9713                                         n->args = list_make2($5, $1);
9714                                         n->agg_order = NIL;
9715                                         n->agg_star = FALSE;
9716                                         n->agg_distinct = FALSE;
9717                                         n->func_variadic = FALSE;
9718                                         n->over = NULL;
9719                                         n->location = @2;
9720                                         $$ = (Node *) n;
9721                                 }
9722                 /*
9723                  * These operators must be called out explicitly in order to make use
9724                  * of bison's automatic operator-precedence handling.  All other
9725                  * operator names are handled by the generic productions using "Op",
9726                  * below; and all those operators will have the same precedence.
9727                  *
9728                  * If you add more explicitly-known operators, be sure to add them
9729                  * also to b_expr and to the MathOp list above.
9730                  */
9731                         | '+' a_expr                                    %prec UMINUS
9732                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
9733                         | '-' a_expr                                    %prec UMINUS
9734                                 { $$ = doNegate($2, @1); }
9735                         | a_expr '+' a_expr
9736                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
9737                         | a_expr '-' a_expr
9738                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
9739                         | a_expr '*' a_expr
9740                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
9741                         | a_expr '/' a_expr
9742                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
9743                         | a_expr '%' a_expr
9744                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
9745                         | a_expr '^' a_expr
9746                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
9747                         | a_expr '<' a_expr
9748                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
9749                         | a_expr '>' a_expr
9750                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
9751                         | a_expr '=' a_expr
9752                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
9753
9754                         | a_expr qual_Op a_expr                         %prec Op
9755                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
9756                         | qual_Op a_expr                                        %prec Op
9757                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
9758                         | a_expr qual_Op                                        %prec POSTFIXOP
9759                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
9760
9761                         | a_expr AND a_expr
9762                                 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
9763                         | a_expr OR a_expr
9764                                 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
9765                         | NOT a_expr
9766                                 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
9767
9768                         | a_expr LIKE a_expr
9769                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
9770                         | a_expr LIKE a_expr ESCAPE a_expr
9771                                 {
9772                                         FuncCall *n = makeNode(FuncCall);
9773                                         n->funcname = SystemFuncName("like_escape");
9774                                         n->args = list_make2($3, $5);
9775                                         n->agg_order = NIL;
9776                                         n->agg_star = FALSE;
9777                                         n->agg_distinct = FALSE;
9778                                         n->func_variadic = FALSE;
9779                                         n->over = NULL;
9780                                         n->location = @2;
9781                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
9782                                 }
9783                         | a_expr NOT LIKE a_expr
9784                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
9785                         | a_expr NOT LIKE a_expr ESCAPE a_expr
9786                                 {
9787                                         FuncCall *n = makeNode(FuncCall);
9788                                         n->funcname = SystemFuncName("like_escape");
9789                                         n->args = list_make2($4, $6);
9790                                         n->agg_order = NIL;
9791                                         n->agg_star = FALSE;
9792                                         n->agg_distinct = FALSE;
9793                                         n->func_variadic = FALSE;
9794                                         n->over = NULL;
9795                                         n->location = @2;
9796                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
9797                                 }
9798                         | a_expr ILIKE a_expr
9799                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
9800                         | a_expr ILIKE a_expr ESCAPE a_expr
9801                                 {
9802                                         FuncCall *n = makeNode(FuncCall);
9803                                         n->funcname = SystemFuncName("like_escape");
9804                                         n->args = list_make2($3, $5);
9805                                         n->agg_order = NIL;
9806                                         n->agg_star = FALSE;
9807                                         n->agg_distinct = FALSE;
9808                                         n->func_variadic = FALSE;
9809                                         n->over = NULL;
9810                                         n->location = @2;
9811                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
9812                                 }
9813                         | a_expr NOT ILIKE a_expr
9814                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
9815                         | a_expr NOT ILIKE a_expr ESCAPE a_expr
9816                                 {
9817                                         FuncCall *n = makeNode(FuncCall);
9818                                         n->funcname = SystemFuncName("like_escape");
9819                                         n->args = list_make2($4, $6);
9820                                         n->agg_order = NIL;
9821                                         n->agg_star = FALSE;
9822                                         n->agg_distinct = FALSE;
9823                                         n->func_variadic = FALSE;
9824                                         n->over = NULL;
9825                                         n->location = @2;
9826                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
9827                                 }
9828
9829                         | a_expr SIMILAR TO a_expr                              %prec SIMILAR
9830                                 {
9831                                         FuncCall *n = makeNode(FuncCall);
9832                                         n->funcname = SystemFuncName("similar_escape");
9833                                         n->args = list_make2($4, makeNullAConst(-1));
9834                                         n->agg_order = NIL;
9835                                         n->agg_star = FALSE;
9836                                         n->agg_distinct = FALSE;
9837                                         n->func_variadic = FALSE;
9838                                         n->over = NULL;
9839                                         n->location = @2;
9840                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
9841                                 }
9842                         | a_expr SIMILAR TO a_expr ESCAPE a_expr
9843                                 {
9844                                         FuncCall *n = makeNode(FuncCall);
9845                                         n->funcname = SystemFuncName("similar_escape");
9846                                         n->args = list_make2($4, $6);
9847                                         n->agg_order = NIL;
9848                                         n->agg_star = FALSE;
9849                                         n->agg_distinct = FALSE;
9850                                         n->func_variadic = FALSE;
9851                                         n->over = NULL;
9852                                         n->location = @2;
9853                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
9854                                 }
9855                         | a_expr NOT SIMILAR TO a_expr                  %prec SIMILAR
9856                                 {
9857                                         FuncCall *n = makeNode(FuncCall);
9858                                         n->funcname = SystemFuncName("similar_escape");
9859                                         n->args = list_make2($5, makeNullAConst(-1));
9860                                         n->agg_order = NIL;
9861                                         n->agg_star = FALSE;
9862                                         n->agg_distinct = FALSE;
9863                                         n->func_variadic = FALSE;
9864                                         n->over = NULL;
9865                                         n->location = @2;
9866                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
9867                                 }
9868                         | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
9869                                 {
9870                                         FuncCall *n = makeNode(FuncCall);
9871                                         n->funcname = SystemFuncName("similar_escape");
9872                                         n->args = list_make2($5, $7);
9873                                         n->agg_order = NIL;
9874                                         n->agg_star = FALSE;
9875                                         n->agg_distinct = FALSE;
9876                                         n->func_variadic = FALSE;
9877                                         n->over = NULL;
9878                                         n->location = @2;
9879                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
9880                                 }
9881
9882                         /* NullTest clause
9883                          * Define SQL92-style Null test clause.
9884                          * Allow two forms described in the standard:
9885                          *      a IS NULL
9886                          *      a IS NOT NULL
9887                          * Allow two SQL extensions
9888                          *      a ISNULL
9889                          *      a NOTNULL
9890                          */
9891                         | a_expr IS NULL_P                                                      %prec IS
9892                                 {
9893                                         NullTest *n = makeNode(NullTest);
9894                                         n->arg = (Expr *) $1;
9895                                         n->nulltesttype = IS_NULL;
9896                                         $$ = (Node *)n;
9897                                 }
9898                         | a_expr ISNULL
9899                                 {
9900                                         NullTest *n = makeNode(NullTest);
9901                                         n->arg = (Expr *) $1;
9902                                         n->nulltesttype = IS_NULL;
9903                                         $$ = (Node *)n;
9904                                 }
9905                         | a_expr IS NOT NULL_P                                          %prec IS
9906                                 {
9907                                         NullTest *n = makeNode(NullTest);
9908                                         n->arg = (Expr *) $1;
9909                                         n->nulltesttype = IS_NOT_NULL;
9910                                         $$ = (Node *)n;
9911                                 }
9912                         | a_expr NOTNULL
9913                                 {
9914                                         NullTest *n = makeNode(NullTest);
9915                                         n->arg = (Expr *) $1;
9916                                         n->nulltesttype = IS_NOT_NULL;
9917                                         $$ = (Node *)n;
9918                                 }
9919                         | row OVERLAPS row
9920                                 {
9921                                         $$ = (Node *)makeOverlaps($1, $3, @2, yyscanner);
9922                                 }
9923                         | a_expr IS TRUE_P                                                      %prec IS
9924                                 {
9925                                         BooleanTest *b = makeNode(BooleanTest);
9926                                         b->arg = (Expr *) $1;
9927                                         b->booltesttype = IS_TRUE;
9928                                         $$ = (Node *)b;
9929                                 }
9930                         | a_expr IS NOT TRUE_P                                          %prec IS
9931                                 {
9932                                         BooleanTest *b = makeNode(BooleanTest);
9933                                         b->arg = (Expr *) $1;
9934                                         b->booltesttype = IS_NOT_TRUE;
9935                                         $$ = (Node *)b;
9936                                 }
9937                         | a_expr IS FALSE_P                                                     %prec IS
9938                                 {
9939                                         BooleanTest *b = makeNode(BooleanTest);
9940                                         b->arg = (Expr *) $1;
9941                                         b->booltesttype = IS_FALSE;
9942                                         $$ = (Node *)b;
9943                                 }
9944                         | a_expr IS NOT FALSE_P                                         %prec IS
9945                                 {
9946                                         BooleanTest *b = makeNode(BooleanTest);
9947                                         b->arg = (Expr *) $1;
9948                                         b->booltesttype = IS_NOT_FALSE;
9949                                         $$ = (Node *)b;
9950                                 }
9951                         | a_expr IS UNKNOWN                                                     %prec IS
9952                                 {
9953                                         BooleanTest *b = makeNode(BooleanTest);
9954                                         b->arg = (Expr *) $1;
9955                                         b->booltesttype = IS_UNKNOWN;
9956                                         $$ = (Node *)b;
9957                                 }
9958                         | a_expr IS NOT UNKNOWN                                         %prec IS
9959                                 {
9960                                         BooleanTest *b = makeNode(BooleanTest);
9961                                         b->arg = (Expr *) $1;
9962                                         b->booltesttype = IS_NOT_UNKNOWN;
9963                                         $$ = (Node *)b;
9964                                 }
9965                         | a_expr IS DISTINCT FROM a_expr                        %prec IS
9966                                 {
9967                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
9968                                 }
9969                         | a_expr IS NOT DISTINCT FROM a_expr            %prec IS
9970                                 {
9971                                         $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
9972                                                                         (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
9973                                                                                                                           "=", $1, $6, @2),
9974                                                                                          @2);
9975
9976                                 }
9977                         | a_expr IS OF '(' type_list ')'                        %prec IS
9978                                 {
9979                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
9980                                 }
9981                         | a_expr IS NOT OF '(' type_list ')'            %prec IS
9982                                 {
9983                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
9984                                 }
9985                         /*
9986                          *      Ideally we would not use hard-wired operators below but
9987                          *      instead use opclasses.  However, mixed data types and other
9988                          *      issues make this difficult:
9989                          *      http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
9990                          */
9991                         | a_expr BETWEEN opt_asymmetric b_expr AND b_expr               %prec BETWEEN
9992                                 {
9993                                         $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
9994                                                 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
9995                                                 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
9996                                                                                          @2);
9997                                 }
9998                         | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr   %prec BETWEEN
9999                                 {
10000                                         $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
10001                                                 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
10002                                                 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
10003                                                                                          @2);
10004                                 }
10005                         | a_expr BETWEEN SYMMETRIC b_expr AND b_expr                    %prec BETWEEN
10006                                 {
10007                                         $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
10008                                                 (Node *) makeA_Expr(AEXPR_AND, NIL,
10009                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
10010                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
10011                                                                                         @2),
10012                                                 (Node *) makeA_Expr(AEXPR_AND, NIL,
10013                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
10014                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
10015                                                                                         @2),
10016                                                                                          @2);
10017                                 }
10018                         | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr                %prec BETWEEN
10019                                 {
10020                                         $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
10021                                                 (Node *) makeA_Expr(AEXPR_OR, NIL,
10022                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
10023                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
10024                                                                                         @2),
10025                                                 (Node *) makeA_Expr(AEXPR_OR, NIL,
10026                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
10027                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
10028                                                                                         @2),
10029                                                                                          @2);
10030                                 }
10031                         | a_expr IN_P in_expr
10032                                 {
10033                                         /* in_expr returns a SubLink or a list of a_exprs */
10034                                         if (IsA($3, SubLink))
10035                                         {
10036                                                 /* generate foo = ANY (subquery) */
10037                                                 SubLink *n = (SubLink *) $3;
10038                                                 n->subLinkType = ANY_SUBLINK;
10039                                                 n->testexpr = $1;
10040                                                 n->operName = list_make1(makeString("="));
10041                                                 n->location = @2;
10042                                                 $$ = (Node *)n;
10043                                         }
10044                                         else
10045                                         {
10046                                                 /* generate scalar IN expression */
10047                                                 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
10048                                         }
10049                                 }
10050                         | a_expr NOT IN_P in_expr
10051                                 {
10052                                         /* in_expr returns a SubLink or a list of a_exprs */
10053                                         if (IsA($4, SubLink))
10054                                         {
10055                                                 /* generate NOT (foo = ANY (subquery)) */
10056                                                 /* Make an = ANY node */
10057                                                 SubLink *n = (SubLink *) $4;
10058                                                 n->subLinkType = ANY_SUBLINK;
10059                                                 n->testexpr = $1;
10060                                                 n->operName = list_make1(makeString("="));
10061                                                 n->location = @3;
10062                                                 /* Stick a NOT on top */
10063                                                 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
10064                                         }
10065                                         else
10066                                         {
10067                                                 /* generate scalar NOT IN expression */
10068                                                 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
10069                                         }
10070                                 }
10071                         | a_expr subquery_Op sub_type select_with_parens        %prec Op
10072                                 {
10073                                         SubLink *n = makeNode(SubLink);
10074                                         n->subLinkType = $3;
10075                                         n->testexpr = $1;
10076                                         n->operName = $2;
10077                                         n->subselect = $4;
10078                                         n->location = @2;
10079                                         $$ = (Node *)n;
10080                                 }
10081                         | a_expr subquery_Op sub_type '(' a_expr ')'            %prec Op
10082                                 {
10083                                         if ($3 == ANY_SUBLINK)
10084                                                 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
10085                                         else
10086                                                 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
10087                                 }
10088                         | UNIQUE select_with_parens
10089                                 {
10090                                         /* Not sure how to get rid of the parentheses
10091                                          * but there are lots of shift/reduce errors without them.
10092                                          *
10093                                          * Should be able to implement this by plopping the entire
10094                                          * select into a node, then transforming the target expressions
10095                                          * from whatever they are into count(*), and testing the
10096                                          * entire result equal to one.
10097                                          * But, will probably implement a separate node in the executor.
10098                                          */
10099                                         ereport(ERROR,
10100                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10101                                                          errmsg("UNIQUE predicate is not yet implemented"),
10102                                                          parser_errposition(@1)));
10103                                 }
10104                         | a_expr IS DOCUMENT_P                                  %prec IS
10105                                 {
10106                                         $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
10107                                                                          list_make1($1), @2);
10108                                 }
10109                         | a_expr IS NOT DOCUMENT_P                              %prec IS
10110                                 {
10111                                         $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
10112                                                                                          makeXmlExpr(IS_DOCUMENT, NULL, NIL,
10113                                                                                                                  list_make1($1), @2),
10114                                                                                          @2);
10115                                 }
10116                 ;
10117
10118 /*
10119  * Restricted expressions
10120  *
10121  * b_expr is a subset of the complete expression syntax defined by a_expr.
10122  *
10123  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
10124  * cause trouble in the places where b_expr is used.  For simplicity, we
10125  * just eliminate all the boolean-keyword-operator productions from b_expr.
10126  */
10127 b_expr:         c_expr
10128                                 { $$ = $1; }
10129                         | b_expr TYPECAST Typename
10130                                 { $$ = makeTypeCast($1, $3, @2); }
10131                         | '+' b_expr                                    %prec UMINUS
10132                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
10133                         | '-' b_expr                                    %prec UMINUS
10134                                 { $$ = doNegate($2, @1); }
10135                         | b_expr '+' b_expr
10136                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
10137                         | b_expr '-' b_expr
10138                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
10139                         | b_expr '*' b_expr
10140                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
10141                         | b_expr '/' b_expr
10142                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
10143                         | b_expr '%' b_expr
10144                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
10145                         | b_expr '^' b_expr
10146                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
10147                         | b_expr '<' b_expr
10148                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
10149                         | b_expr '>' b_expr
10150                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
10151                         | b_expr '=' b_expr
10152                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
10153                         | b_expr qual_Op b_expr                         %prec Op
10154                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
10155                         | qual_Op b_expr                                        %prec Op
10156                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
10157                         | b_expr qual_Op                                        %prec POSTFIXOP
10158                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
10159                         | b_expr IS DISTINCT FROM b_expr                %prec IS
10160                                 {
10161                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
10162                                 }
10163                         | b_expr IS NOT DISTINCT FROM b_expr    %prec IS
10164                                 {
10165                                         $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
10166                                                 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
10167                                 }
10168                         | b_expr IS OF '(' type_list ')'                %prec IS
10169                                 {
10170                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
10171                                 }
10172                         | b_expr IS NOT OF '(' type_list ')'    %prec IS
10173                                 {
10174                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
10175                                 }
10176                         | b_expr IS DOCUMENT_P                                  %prec IS
10177                                 {
10178                                         $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
10179                                                                          list_make1($1), @2);
10180                                 }
10181                         | b_expr IS NOT DOCUMENT_P                              %prec IS
10182                                 {
10183                                         $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
10184                                                                                          makeXmlExpr(IS_DOCUMENT, NULL, NIL,
10185                                                                                                                  list_make1($1), @2),
10186                                                                                          @2);
10187                                 }
10188                 ;
10189
10190 /*
10191  * Productions that can be used in both a_expr and b_expr.
10192  *
10193  * Note: productions that refer recursively to a_expr or b_expr mostly
10194  * cannot appear here.  However, it's OK to refer to a_exprs that occur
10195  * inside parentheses, such as function arguments; that cannot introduce
10196  * ambiguity to the b_expr syntax.
10197  */
10198 c_expr:         columnref                                                               { $$ = $1; }
10199                         | AexprConst                                                    { $$ = $1; }
10200                         | PARAM opt_indirection
10201                                 {
10202                                         ParamRef *p = makeNode(ParamRef);
10203                                         p->number = $1;
10204                                         p->location = @1;
10205                                         if ($2)
10206                                         {
10207                                                 A_Indirection *n = makeNode(A_Indirection);
10208                                                 n->arg = (Node *) p;
10209                                                 n->indirection = check_indirection($2, yyscanner);
10210                                                 $$ = (Node *) n;
10211                                         }
10212                                         else
10213                                                 $$ = (Node *) p;
10214                                 }
10215                         | '(' a_expr ')' opt_indirection
10216                                 {
10217                                         if ($4)
10218                                         {
10219                                                 A_Indirection *n = makeNode(A_Indirection);
10220                                                 n->arg = $2;
10221                                                 n->indirection = check_indirection($4, yyscanner);
10222                                                 $$ = (Node *)n;
10223                                         }
10224                                         else
10225                                                 $$ = $2;
10226                                 }
10227                         | case_expr
10228                                 { $$ = $1; }
10229                         | func_expr
10230                                 { $$ = $1; }
10231                         | select_with_parens                    %prec UMINUS
10232                                 {
10233                                         SubLink *n = makeNode(SubLink);
10234                                         n->subLinkType = EXPR_SUBLINK;
10235                                         n->testexpr = NULL;
10236                                         n->operName = NIL;
10237                                         n->subselect = $1;
10238                                         n->location = @1;
10239                                         $$ = (Node *)n;
10240                                 }
10241                         | EXISTS select_with_parens
10242                                 {
10243                                         SubLink *n = makeNode(SubLink);
10244                                         n->subLinkType = EXISTS_SUBLINK;
10245                                         n->testexpr = NULL;
10246                                         n->operName = NIL;
10247                                         n->subselect = $2;
10248                                         n->location = @1;
10249                                         $$ = (Node *)n;
10250                                 }
10251                         | ARRAY select_with_parens
10252                                 {
10253                                         SubLink *n = makeNode(SubLink);
10254                                         n->subLinkType = ARRAY_SUBLINK;
10255                                         n->testexpr = NULL;
10256                                         n->operName = NIL;
10257                                         n->subselect = $2;
10258                                         n->location = @1;
10259                                         $$ = (Node *)n;
10260                                 }
10261                         | ARRAY array_expr
10262                                 {
10263                                         A_ArrayExpr *n = (A_ArrayExpr *) $2;
10264                                         Assert(IsA(n, A_ArrayExpr));
10265                                         /* point outermost A_ArrayExpr to the ARRAY keyword */
10266                                         n->location = @1;
10267                                         $$ = (Node *)n;
10268                                 }
10269                         | row
10270                                 {
10271                                         RowExpr *r = makeNode(RowExpr);
10272                                         r->args = $1;
10273                                         r->row_typeid = InvalidOid;     /* not analyzed yet */
10274                                         r->location = @1;
10275                                         $$ = (Node *)r;
10276                                 }
10277                 ;
10278
10279 /*
10280  * func_expr is split out from c_expr just so that we have a classification
10281  * for "everything that is a function call or looks like one".  This isn't
10282  * very important, but it saves us having to document which variants are
10283  * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
10284  * (Note that many of the special SQL functions wouldn't actually make any
10285  * sense as functional index entries, but we ignore that consideration here.)
10286  */
10287 func_expr:      func_name '(' ')' over_clause
10288                                 {
10289                                         FuncCall *n = makeNode(FuncCall);
10290                                         n->funcname = $1;
10291                                         n->args = NIL;
10292                                         n->agg_order = NIL;
10293                                         n->agg_star = FALSE;
10294                                         n->agg_distinct = FALSE;
10295                                         n->func_variadic = FALSE;
10296                                         n->over = $4;
10297                                         n->location = @1;
10298                                         $$ = (Node *)n;
10299                                 }
10300                         | func_name '(' func_arg_list ')' over_clause
10301                                 {
10302                                         FuncCall *n = makeNode(FuncCall);
10303                                         n->funcname = $1;
10304                                         n->args = $3;
10305                                         n->agg_order = NIL;
10306                                         n->agg_star = FALSE;
10307                                         n->agg_distinct = FALSE;
10308                                         n->func_variadic = FALSE;
10309                                         n->over = $5;
10310                                         n->location = @1;
10311                                         $$ = (Node *)n;
10312                                 }
10313                         | func_name '(' VARIADIC func_arg_expr ')' over_clause
10314                                 {
10315                                         FuncCall *n = makeNode(FuncCall);
10316                                         n->funcname = $1;
10317                                         n->args = list_make1($4);
10318                                         n->agg_order = NIL;
10319                                         n->agg_star = FALSE;
10320                                         n->agg_distinct = FALSE;
10321                                         n->func_variadic = TRUE;
10322                                         n->over = $6;
10323                                         n->location = @1;
10324                                         $$ = (Node *)n;
10325                                 }
10326                         | func_name '(' func_arg_list ',' VARIADIC func_arg_expr ')' over_clause
10327                                 {
10328                                         FuncCall *n = makeNode(FuncCall);
10329                                         n->funcname = $1;
10330                                         n->args = lappend($3, $6);
10331                                         n->agg_order = NIL;
10332                                         n->agg_star = FALSE;
10333                                         n->agg_distinct = FALSE;
10334                                         n->func_variadic = TRUE;
10335                                         n->over = $8;
10336                                         n->location = @1;
10337                                         $$ = (Node *)n;
10338                                 }
10339                         | func_name '(' func_arg_list sort_clause ')' over_clause
10340                                 {
10341                                         FuncCall *n = makeNode(FuncCall);
10342                                         n->funcname = $1;
10343                                         n->args = $3;
10344                                         n->agg_order = $4;
10345                                         n->agg_star = FALSE;
10346                                         n->agg_distinct = FALSE;
10347                                         n->func_variadic = FALSE;
10348                                         n->over = $6;
10349                                         n->location = @1;
10350                                         $$ = (Node *)n;
10351                                 }
10352                         | func_name '(' ALL func_arg_list opt_sort_clause ')' over_clause
10353                                 {
10354                                         FuncCall *n = makeNode(FuncCall);
10355                                         n->funcname = $1;
10356                                         n->args = $4;
10357                                         n->agg_order = $5;
10358                                         n->agg_star = FALSE;
10359                                         n->agg_distinct = FALSE;
10360                                         /* Ideally we'd mark the FuncCall node to indicate
10361                                          * "must be an aggregate", but there's no provision
10362                                          * for that in FuncCall at the moment.
10363                                          */
10364                                         n->func_variadic = FALSE;
10365                                         n->over = $7;
10366                                         n->location = @1;
10367                                         $$ = (Node *)n;
10368                                 }
10369                         | func_name '(' DISTINCT func_arg_list opt_sort_clause ')' over_clause
10370                                 {
10371                                         FuncCall *n = makeNode(FuncCall);
10372                                         n->funcname = $1;
10373                                         n->args = $4;
10374                                         n->agg_order = $5;
10375                                         n->agg_star = FALSE;
10376                                         n->agg_distinct = TRUE;
10377                                         n->func_variadic = FALSE;
10378                                         n->over = $7;
10379                                         n->location = @1;
10380                                         $$ = (Node *)n;
10381                                 }
10382                         | func_name '(' '*' ')' over_clause
10383                                 {
10384                                         /*
10385                                          * We consider AGGREGATE(*) to invoke a parameterless
10386                                          * aggregate.  This does the right thing for COUNT(*),
10387                                          * and there are no other aggregates in SQL92 that accept
10388                                          * '*' as parameter.
10389                                          *
10390                                          * The FuncCall node is also marked agg_star = true,
10391                                          * so that later processing can detect what the argument
10392                                          * really was.
10393                                          */
10394                                         FuncCall *n = makeNode(FuncCall);
10395                                         n->funcname = $1;
10396                                         n->args = NIL;
10397                                         n->agg_order = NIL;
10398                                         n->agg_star = TRUE;
10399                                         n->agg_distinct = FALSE;
10400                                         n->func_variadic = FALSE;
10401                                         n->over = $5;
10402                                         n->location = @1;
10403                                         $$ = (Node *)n;
10404                                 }
10405                         | CURRENT_DATE
10406                                 {
10407                                         /*
10408                                          * Translate as "'now'::text::date".
10409                                          *
10410                                          * We cannot use "'now'::date" because coerce_type() will
10411                                          * immediately reduce that to a constant representing
10412                                          * today's date.  We need to delay the conversion until
10413                                          * runtime, else the wrong things will happen when
10414                                          * CURRENT_DATE is used in a column default value or rule.
10415                                          *
10416                                          * This could be simplified if we had a way to generate
10417                                          * an expression tree representing runtime application
10418                                          * of type-input conversion functions.  (As of PG 7.3
10419                                          * that is actually possible, but not clear that we want
10420                                          * to rely on it.)
10421                                          */
10422                                         Node *n;
10423                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
10424                                         $$ = makeTypeCast(n, SystemTypeName("date"), -1);
10425                                 }
10426                         | CURRENT_TIME
10427                                 {
10428                                         /*
10429                                          * Translate as "'now'::text::timetz".
10430                                          * See comments for CURRENT_DATE.
10431                                          */
10432                                         Node *n;
10433                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
10434                                         $$ = makeTypeCast(n, SystemTypeName("timetz"), -1);
10435                                 }
10436                         | CURRENT_TIME '(' Iconst ')'
10437                                 {
10438                                         /*
10439                                          * Translate as "'now'::text::timetz(n)".
10440                                          * See comments for CURRENT_DATE.
10441                                          */
10442                                         Node *n;
10443                                         TypeName *d;
10444                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
10445                                         d = SystemTypeName("timetz");
10446                                         d->typmods = list_make1(makeIntConst($3, @3));
10447                                         $$ = makeTypeCast(n, d, -1);
10448                                 }
10449                         | CURRENT_TIMESTAMP
10450                                 {
10451                                         /*
10452                                          * Translate as "now()", since we have a function that
10453                                          * does exactly what is needed.
10454                                          */
10455                                         FuncCall *n = makeNode(FuncCall);
10456                                         n->funcname = SystemFuncName("now");
10457                                         n->args = NIL;
10458                                         n->agg_order = NIL;
10459                                         n->agg_star = FALSE;
10460                                         n->agg_distinct = FALSE;
10461                                         n->func_variadic = FALSE;
10462                                         n->over = NULL;
10463                                         n->location = @1;
10464                                         $$ = (Node *)n;
10465                                 }
10466                         | CURRENT_TIMESTAMP '(' Iconst ')'
10467                                 {
10468                                         /*
10469                                          * Translate as "'now'::text::timestamptz(n)".
10470                                          * See comments for CURRENT_DATE.
10471                                          */
10472                                         Node *n;
10473                                         TypeName *d;
10474                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
10475                                         d = SystemTypeName("timestamptz");
10476                                         d->typmods = list_make1(makeIntConst($3, @3));
10477                                         $$ = makeTypeCast(n, d, -1);
10478                                 }
10479                         | LOCALTIME
10480                                 {
10481                                         /*
10482                                          * Translate as "'now'::text::time".
10483                                          * See comments for CURRENT_DATE.
10484                                          */
10485                                         Node *n;
10486                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
10487                                         $$ = makeTypeCast((Node *)n, SystemTypeName("time"), -1);
10488                                 }
10489                         | LOCALTIME '(' Iconst ')'
10490                                 {
10491                                         /*
10492                                          * Translate as "'now'::text::time(n)".
10493                                          * See comments for CURRENT_DATE.
10494                                          */
10495                                         Node *n;
10496                                         TypeName *d;
10497                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
10498                                         d = SystemTypeName("time");
10499                                         d->typmods = list_make1(makeIntConst($3, @3));
10500                                         $$ = makeTypeCast((Node *)n, d, -1);
10501                                 }
10502                         | LOCALTIMESTAMP
10503                                 {
10504                                         /*
10505                                          * Translate as "'now'::text::timestamp".
10506                                          * See comments for CURRENT_DATE.
10507                                          */
10508                                         Node *n;
10509                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
10510                                         $$ = makeTypeCast(n, SystemTypeName("timestamp"), -1);
10511                                 }
10512                         | LOCALTIMESTAMP '(' Iconst ')'
10513                                 {
10514                                         /*
10515                                          * Translate as "'now'::text::timestamp(n)".
10516                                          * See comments for CURRENT_DATE.
10517                                          */
10518                                         Node *n;
10519                                         TypeName *d;
10520                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
10521                                         d = SystemTypeName("timestamp");
10522                                         d->typmods = list_make1(makeIntConst($3, @3));
10523                                         $$ = makeTypeCast(n, d, -1);
10524                                 }
10525                         | CURRENT_ROLE
10526                                 {
10527                                         FuncCall *n = makeNode(FuncCall);
10528                                         n->funcname = SystemFuncName("current_user");
10529                                         n->args = NIL;
10530                                         n->agg_order = NIL;
10531                                         n->agg_star = FALSE;
10532                                         n->agg_distinct = FALSE;
10533                                         n->func_variadic = FALSE;
10534                                         n->over = NULL;
10535                                         n->location = @1;
10536                                         $$ = (Node *)n;
10537                                 }
10538                         | CURRENT_USER
10539                                 {
10540                                         FuncCall *n = makeNode(FuncCall);
10541                                         n->funcname = SystemFuncName("current_user");
10542                                         n->args = NIL;
10543                                         n->agg_order = NIL;
10544                                         n->agg_star = FALSE;
10545                                         n->agg_distinct = FALSE;
10546                                         n->func_variadic = FALSE;
10547                                         n->over = NULL;
10548                                         n->location = @1;
10549                                         $$ = (Node *)n;
10550                                 }
10551                         | SESSION_USER
10552                                 {
10553                                         FuncCall *n = makeNode(FuncCall);
10554                                         n->funcname = SystemFuncName("session_user");
10555                                         n->args = NIL;
10556                                         n->agg_order = NIL;
10557                                         n->agg_star = FALSE;
10558                                         n->agg_distinct = FALSE;
10559                                         n->func_variadic = FALSE;
10560                                         n->over = NULL;
10561                                         n->location = @1;
10562                                         $$ = (Node *)n;
10563                                 }
10564                         | USER
10565                                 {
10566                                         FuncCall *n = makeNode(FuncCall);
10567                                         n->funcname = SystemFuncName("current_user");
10568                                         n->args = NIL;
10569                                         n->agg_order = NIL;
10570                                         n->agg_star = FALSE;
10571                                         n->agg_distinct = FALSE;
10572                                         n->func_variadic = FALSE;
10573                                         n->over = NULL;
10574                                         n->location = @1;
10575                                         $$ = (Node *)n;
10576                                 }
10577                         | CURRENT_CATALOG
10578                                 {
10579                                         FuncCall *n = makeNode(FuncCall);
10580                                         n->funcname = SystemFuncName("current_database");
10581                                         n->args = NIL;
10582                                         n->agg_order = NIL;
10583                                         n->agg_star = FALSE;
10584                                         n->agg_distinct = FALSE;
10585                                         n->func_variadic = FALSE;
10586                                         n->over = NULL;
10587                                         n->location = @1;
10588                                         $$ = (Node *)n;
10589                                 }
10590                         | CURRENT_SCHEMA
10591                                 {
10592                                         FuncCall *n = makeNode(FuncCall);
10593                                         n->funcname = SystemFuncName("current_schema");
10594                                         n->args = NIL;
10595                                         n->agg_order = NIL;
10596                                         n->agg_star = FALSE;
10597                                         n->agg_distinct = FALSE;
10598                                         n->func_variadic = FALSE;
10599                                         n->over = NULL;
10600                                         n->location = @1;
10601                                         $$ = (Node *)n;
10602                                 }
10603                         | CAST '(' a_expr AS Typename ')'
10604                                 { $$ = makeTypeCast($3, $5, @1); }
10605                         | EXTRACT '(' extract_list ')'
10606                                 {
10607                                         FuncCall *n = makeNode(FuncCall);
10608                                         n->funcname = SystemFuncName("date_part");
10609                                         n->args = $3;
10610                                         n->agg_order = NIL;
10611                                         n->agg_star = FALSE;
10612                                         n->agg_distinct = FALSE;
10613                                         n->func_variadic = FALSE;
10614                                         n->over = NULL;
10615                                         n->location = @1;
10616                                         $$ = (Node *)n;
10617                                 }
10618                         | OVERLAY '(' overlay_list ')'
10619                                 {
10620                                         /* overlay(A PLACING B FROM C FOR D) is converted to
10621                                          * overlay(A, B, C, D)
10622                                          * overlay(A PLACING B FROM C) is converted to
10623                                          * overlay(A, B, C)
10624                                          */
10625                                         FuncCall *n = makeNode(FuncCall);
10626                                         n->funcname = SystemFuncName("overlay");
10627                                         n->args = $3;
10628                                         n->agg_order = NIL;
10629                                         n->agg_star = FALSE;
10630                                         n->agg_distinct = FALSE;
10631                                         n->func_variadic = FALSE;
10632                                         n->over = NULL;
10633                                         n->location = @1;
10634                                         $$ = (Node *)n;
10635                                 }
10636                         | POSITION '(' position_list ')'
10637                                 {
10638                                         /* position(A in B) is converted to position(B, A) */
10639                                         FuncCall *n = makeNode(FuncCall);
10640                                         n->funcname = SystemFuncName("position");
10641                                         n->args = $3;
10642                                         n->agg_order = NIL;
10643                                         n->agg_star = FALSE;
10644                                         n->agg_distinct = FALSE;
10645                                         n->func_variadic = FALSE;
10646                                         n->over = NULL;
10647                                         n->location = @1;
10648                                         $$ = (Node *)n;
10649                                 }
10650                         | SUBSTRING '(' substr_list ')'
10651                                 {
10652                                         /* substring(A from B for C) is converted to
10653                                          * substring(A, B, C) - thomas 2000-11-28
10654                                          */
10655                                         FuncCall *n = makeNode(FuncCall);
10656                                         n->funcname = SystemFuncName("substring");
10657                                         n->args = $3;
10658                                         n->agg_order = NIL;
10659                                         n->agg_star = FALSE;
10660                                         n->agg_distinct = FALSE;
10661                                         n->func_variadic = FALSE;
10662                                         n->over = NULL;
10663                                         n->location = @1;
10664                                         $$ = (Node *)n;
10665                                 }
10666                         | TREAT '(' a_expr AS Typename ')'
10667                                 {
10668                                         /* TREAT(expr AS target) converts expr of a particular type to target,
10669                                          * which is defined to be a subtype of the original expression.
10670                                          * In SQL99, this is intended for use with structured UDTs,
10671                                          * but let's make this a generally useful form allowing stronger
10672                                          * coercions than are handled by implicit casting.
10673                                          */
10674                                         FuncCall *n = makeNode(FuncCall);
10675                                         /* Convert SystemTypeName() to SystemFuncName() even though
10676                                          * at the moment they result in the same thing.
10677                                          */
10678                                         n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
10679                                         n->args = list_make1($3);
10680                                         n->agg_order = NIL;
10681                                         n->agg_star = FALSE;
10682                                         n->agg_distinct = FALSE;
10683                                         n->func_variadic = FALSE;
10684                                         n->over = NULL;
10685                                         n->location = @1;
10686                                         $$ = (Node *)n;
10687                                 }
10688                         | TRIM '(' BOTH trim_list ')'
10689                                 {
10690                                         /* various trim expressions are defined in SQL92
10691                                          * - thomas 1997-07-19
10692                                          */
10693                                         FuncCall *n = makeNode(FuncCall);
10694                                         n->funcname = SystemFuncName("btrim");
10695                                         n->args = $4;
10696                                         n->agg_order = NIL;
10697                                         n->agg_star = FALSE;
10698                                         n->agg_distinct = FALSE;
10699                                         n->func_variadic = FALSE;
10700                                         n->over = NULL;
10701                                         n->location = @1;
10702                                         $$ = (Node *)n;
10703                                 }
10704                         | TRIM '(' LEADING trim_list ')'
10705                                 {
10706                                         FuncCall *n = makeNode(FuncCall);
10707                                         n->funcname = SystemFuncName("ltrim");
10708                                         n->args = $4;
10709                                         n->agg_order = NIL;
10710                                         n->agg_star = FALSE;
10711                                         n->agg_distinct = FALSE;
10712                                         n->func_variadic = FALSE;
10713                                         n->over = NULL;
10714                                         n->location = @1;
10715                                         $$ = (Node *)n;
10716                                 }
10717                         | TRIM '(' TRAILING trim_list ')'
10718                                 {
10719                                         FuncCall *n = makeNode(FuncCall);
10720                                         n->funcname = SystemFuncName("rtrim");
10721                                         n->args = $4;
10722                                         n->agg_order = NIL;
10723                                         n->agg_star = FALSE;
10724                                         n->agg_distinct = FALSE;
10725                                         n->func_variadic = FALSE;
10726                                         n->over = NULL;
10727                                         n->location = @1;
10728                                         $$ = (Node *)n;
10729                                 }
10730                         | TRIM '(' trim_list ')'
10731                                 {
10732                                         FuncCall *n = makeNode(FuncCall);
10733                                         n->funcname = SystemFuncName("btrim");
10734                                         n->args = $3;
10735                                         n->agg_order = NIL;
10736                                         n->agg_star = FALSE;
10737                                         n->agg_distinct = FALSE;
10738                                         n->func_variadic = FALSE;
10739                                         n->over = NULL;
10740                                         n->location = @1;
10741                                         $$ = (Node *)n;
10742                                 }
10743                         | NULLIF '(' a_expr ',' a_expr ')'
10744                                 {
10745                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
10746                                 }
10747                         | COALESCE '(' expr_list ')'
10748                                 {
10749                                         CoalesceExpr *c = makeNode(CoalesceExpr);
10750                                         c->args = $3;
10751                                         c->location = @1;
10752                                         $$ = (Node *)c;
10753                                 }
10754                         | GREATEST '(' expr_list ')'
10755                                 {
10756                                         MinMaxExpr *v = makeNode(MinMaxExpr);
10757                                         v->args = $3;
10758                                         v->op = IS_GREATEST;
10759                                         v->location = @1;
10760                                         $$ = (Node *)v;
10761                                 }
10762                         | LEAST '(' expr_list ')'
10763                                 {
10764                                         MinMaxExpr *v = makeNode(MinMaxExpr);
10765                                         v->args = $3;
10766                                         v->op = IS_LEAST;
10767                                         v->location = @1;
10768                                         $$ = (Node *)v;
10769                                 }
10770                         | XMLCONCAT '(' expr_list ')'
10771                                 {
10772                                         $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
10773                                 }
10774                         | XMLELEMENT '(' NAME_P ColLabel ')'
10775                                 {
10776                                         $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
10777                                 }
10778                         | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
10779                                 {
10780                                         $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
10781                                 }
10782                         | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
10783                                 {
10784                                         $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
10785                                 }
10786                         | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
10787                                 {
10788                                         $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
10789                                 }
10790                         | XMLEXISTS '(' c_expr xmlexists_argument ')'
10791                                 {
10792                                         /* xmlexists(A PASSING [BY REF] B [BY REF]) is
10793                                          * converted to xmlexists(A, B)*/
10794                                         FuncCall *n = makeNode(FuncCall);
10795                                         n->funcname = SystemFuncName("xmlexists");
10796                                         n->args = list_make2($3, $4);
10797                                         n->agg_order = NIL;
10798                                         n->agg_star = FALSE;
10799                                         n->agg_distinct = FALSE;
10800                                         n->func_variadic = FALSE;
10801                                         n->over = NULL;
10802                                         n->location = @1;
10803                                         $$ = (Node *)n;
10804                                 }
10805                         | XMLFOREST '(' xml_attribute_list ')'
10806                                 {
10807                                         $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
10808                                 }
10809                         | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
10810                                 {
10811                                         XmlExpr *x = (XmlExpr *)
10812                                                 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
10813                                                                         list_make2($4, makeBoolAConst($5, -1)),
10814                                                                         @1);
10815                                         x->xmloption = $3;
10816                                         $$ = (Node *)x;
10817                                 }
10818                         | XMLPI '(' NAME_P ColLabel ')'
10819                                 {
10820                                         $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
10821                                 }
10822                         | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
10823                                 {
10824                                         $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
10825                                 }
10826                         | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
10827                                 {
10828                                         $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
10829                                                                          list_make3($3, $5, $6), @1);
10830                                 }
10831                         | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
10832                                 {
10833                                         XmlSerialize *n = makeNode(XmlSerialize);
10834                                         n->xmloption = $3;
10835                                         n->expr = $4;
10836                                         n->typeName = $6;
10837                                         n->location = @1;
10838                                         $$ = (Node *)n;
10839                                 }
10840                 ;
10841
10842 /*
10843  * SQL/XML support
10844  */
10845 xml_root_version: VERSION_P a_expr
10846                                 { $$ = $2; }
10847                         | VERSION_P NO VALUE_P
10848                                 { $$ = makeNullAConst(-1); }
10849                 ;
10850
10851 opt_xml_root_standalone: ',' STANDALONE_P YES_P
10852                                 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
10853                         | ',' STANDALONE_P NO
10854                                 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
10855                         | ',' STANDALONE_P NO VALUE_P
10856                                 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
10857                         | /*EMPTY*/
10858                                 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
10859                 ;
10860
10861 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'        { $$ = $3; }
10862                 ;
10863
10864 xml_attribute_list:     xml_attribute_el                                        { $$ = list_make1($1); }
10865                         | xml_attribute_list ',' xml_attribute_el       { $$ = lappend($1, $3); }
10866                 ;
10867
10868 xml_attribute_el: a_expr AS ColLabel
10869                                 {
10870                                         $$ = makeNode(ResTarget);
10871                                         $$->name = $3;
10872                                         $$->indirection = NIL;
10873                                         $$->val = (Node *) $1;
10874                                         $$->location = @1;
10875                                 }
10876                         | a_expr
10877                                 {
10878                                         $$ = makeNode(ResTarget);
10879                                         $$->name = NULL;
10880                                         $$->indirection = NIL;
10881                                         $$->val = (Node *) $1;
10882                                         $$->location = @1;
10883                                 }
10884                 ;
10885
10886 document_or_content: DOCUMENT_P                                         { $$ = XMLOPTION_DOCUMENT; }
10887                         | CONTENT_P                                                             { $$ = XMLOPTION_CONTENT; }
10888                 ;
10889
10890 xml_whitespace_option: PRESERVE WHITESPACE_P            { $$ = TRUE; }
10891                         | STRIP_P WHITESPACE_P                                  { $$ = FALSE; }
10892                         | /*EMPTY*/                                                             { $$ = FALSE; }
10893                 ;
10894
10895 /* We allow several variants for SQL and other compatibility. */
10896 xmlexists_argument:
10897                         PASSING c_expr
10898                                 {
10899                                         $$ = $2;
10900                                 }
10901                         | PASSING c_expr BY REF
10902                                 {
10903                                         $$ = $2;
10904                                 }
10905                         | PASSING BY REF c_expr
10906                                 {
10907                                         $$ = $4;
10908                                 }
10909                         | PASSING BY REF c_expr BY REF
10910                                 {
10911                                         $$ = $4;
10912                                 }
10913                 ;
10914
10915
10916 /*
10917  * Window Definitions
10918  */
10919 window_clause:
10920                         WINDOW window_definition_list                   { $$ = $2; }
10921                         | /*EMPTY*/                                                             { $$ = NIL; }
10922                 ;
10923
10924 window_definition_list:
10925                         window_definition                                               { $$ = list_make1($1); }
10926                         | window_definition_list ',' window_definition
10927                                                                                                         { $$ = lappend($1, $3); }
10928                 ;
10929
10930 window_definition:
10931                         ColId AS window_specification
10932                                 {
10933                                         WindowDef *n = $3;
10934                                         n->name = $1;
10935                                         $$ = n;
10936                                 }
10937                 ;
10938
10939 over_clause: OVER window_specification
10940                                 { $$ = $2; }
10941                         | OVER ColId
10942                                 {
10943                                         WindowDef *n = makeNode(WindowDef);
10944                                         n->name = $2;
10945                                         n->refname = NULL;
10946                                         n->partitionClause = NIL;
10947                                         n->orderClause = NIL;
10948                                         n->frameOptions = FRAMEOPTION_DEFAULTS;
10949                                         n->startOffset = NULL;
10950                                         n->endOffset = NULL;
10951                                         n->location = @2;
10952                                         $$ = n;
10953                                 }
10954                         | /*EMPTY*/
10955                                 { $$ = NULL; }
10956                 ;
10957
10958 window_specification: '(' opt_existing_window_name opt_partition_clause
10959                                                 opt_sort_clause opt_frame_clause ')'
10960                                 {
10961                                         WindowDef *n = makeNode(WindowDef);
10962                                         n->name = NULL;
10963                                         n->refname = $2;
10964                                         n->partitionClause = $3;
10965                                         n->orderClause = $4;
10966                                         /* copy relevant fields of opt_frame_clause */
10967                                         n->frameOptions = $5->frameOptions;
10968                                         n->startOffset = $5->startOffset;
10969                                         n->endOffset = $5->endOffset;
10970                                         n->location = @1;
10971                                         $$ = n;
10972                                 }
10973                 ;
10974
10975 /*
10976  * If we see PARTITION, RANGE, or ROWS as the first token after the '('
10977  * of a window_specification, we want the assumption to be that there is
10978  * no existing_window_name; but those keywords are unreserved and so could
10979  * be ColIds.  We fix this by making them have the same precedence as IDENT
10980  * and giving the empty production here a slightly higher precedence, so
10981  * that the shift/reduce conflict is resolved in favor of reducing the rule.
10982  * These keywords are thus precluded from being an existing_window_name but
10983  * are not reserved for any other purpose.
10984  */
10985 opt_existing_window_name: ColId                                         { $$ = $1; }
10986                         | /*EMPTY*/                             %prec Op                { $$ = NULL; }
10987                 ;
10988
10989 opt_partition_clause: PARTITION BY expr_list            { $$ = $3; }
10990                         | /*EMPTY*/                                                             { $$ = NIL; }
10991                 ;
10992
10993 /*
10994  * For frame clauses, we return a WindowDef, but only some fields are used:
10995  * frameOptions, startOffset, and endOffset.
10996  *
10997  * This is only a subset of the full SQL:2008 frame_clause grammar.
10998  * We don't support <window frame exclusion> yet.
10999  */
11000 opt_frame_clause:
11001                         RANGE frame_extent
11002                                 {
11003                                         WindowDef *n = $2;
11004                                         n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
11005                                         if (n->frameOptions & (FRAMEOPTION_START_VALUE_PRECEDING |
11006                                                                                    FRAMEOPTION_END_VALUE_PRECEDING))
11007                                                 ereport(ERROR,
11008                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
11009                                                                  errmsg("RANGE PRECEDING is only supported with UNBOUNDED"),
11010                                                                  parser_errposition(@1)));
11011                                         if (n->frameOptions & (FRAMEOPTION_START_VALUE_FOLLOWING |
11012                                                                                    FRAMEOPTION_END_VALUE_FOLLOWING))
11013                                                 ereport(ERROR,
11014                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
11015                                                                  errmsg("RANGE FOLLOWING is only supported with UNBOUNDED"),
11016                                                                  parser_errposition(@1)));
11017                                         $$ = n;
11018                                 }
11019                         | ROWS frame_extent
11020                                 {
11021                                         WindowDef *n = $2;
11022                                         n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
11023                                         $$ = n;
11024                                 }
11025                         | /*EMPTY*/
11026                                 {
11027                                         WindowDef *n = makeNode(WindowDef);
11028                                         n->frameOptions = FRAMEOPTION_DEFAULTS;
11029                                         n->startOffset = NULL;
11030                                         n->endOffset = NULL;
11031                                         $$ = n;
11032                                 }
11033                 ;
11034
11035 frame_extent: frame_bound
11036                                 {
11037                                         WindowDef *n = $1;
11038                                         /* reject invalid cases */
11039                                         if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
11040                                                 ereport(ERROR,
11041                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
11042                                                                  errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
11043                                                                  parser_errposition(@1)));
11044                                         if (n->frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING)
11045                                                 ereport(ERROR,
11046                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
11047                                                                  errmsg("frame starting from following row cannot end with current row"),
11048                                                                  parser_errposition(@1)));
11049                                         n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
11050                                         $$ = n;
11051                                 }
11052                         | BETWEEN frame_bound AND frame_bound
11053                                 {
11054                                         WindowDef *n1 = $2;
11055                                         WindowDef *n2 = $4;
11056                                         /* form merged options */
11057                                         int             frameOptions = n1->frameOptions;
11058                                         /* shift converts START_ options to END_ options */
11059                                         frameOptions |= n2->frameOptions << 1;
11060                                         frameOptions |= FRAMEOPTION_BETWEEN;
11061                                         /* reject invalid cases */
11062                                         if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
11063                                                 ereport(ERROR,
11064                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
11065                                                                  errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
11066                                                                  parser_errposition(@2)));
11067                                         if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
11068                                                 ereport(ERROR,
11069                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
11070                                                                  errmsg("frame end cannot be UNBOUNDED PRECEDING"),
11071                                                                  parser_errposition(@4)));
11072                                         if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
11073                                                 (frameOptions & FRAMEOPTION_END_VALUE_PRECEDING))
11074                                                 ereport(ERROR,
11075                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
11076                                                                  errmsg("frame starting from current row cannot have preceding rows"),
11077                                                                  parser_errposition(@4)));
11078                                         if ((frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING) &&
11079                                                 (frameOptions & (FRAMEOPTION_END_VALUE_PRECEDING |
11080                                                                                  FRAMEOPTION_END_CURRENT_ROW)))
11081                                                 ereport(ERROR,
11082                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
11083                                                                  errmsg("frame starting from following row cannot have preceding rows"),
11084                                                                  parser_errposition(@4)));
11085                                         n1->frameOptions = frameOptions;
11086                                         n1->endOffset = n2->startOffset;
11087                                         $$ = n1;
11088                                 }
11089                 ;
11090
11091 /*
11092  * This is used for both frame start and frame end, with output set up on
11093  * the assumption it's frame start; the frame_extent productions must reject
11094  * invalid cases.
11095  */
11096 frame_bound:
11097                         UNBOUNDED PRECEDING
11098                                 {
11099                                         WindowDef *n = makeNode(WindowDef);
11100                                         n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
11101                                         n->startOffset = NULL;
11102                                         n->endOffset = NULL;
11103                                         $$ = n;
11104                                 }
11105                         | UNBOUNDED FOLLOWING
11106                                 {
11107                                         WindowDef *n = makeNode(WindowDef);
11108                                         n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
11109                                         n->startOffset = NULL;
11110                                         n->endOffset = NULL;
11111                                         $$ = n;
11112                                 }
11113                         | CURRENT_P ROW
11114                                 {
11115                                         WindowDef *n = makeNode(WindowDef);
11116                                         n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
11117                                         n->startOffset = NULL;
11118                                         n->endOffset = NULL;
11119                                         $$ = n;
11120                                 }
11121                         | a_expr PRECEDING
11122                                 {
11123                                         WindowDef *n = makeNode(WindowDef);
11124                                         n->frameOptions = FRAMEOPTION_START_VALUE_PRECEDING;
11125                                         n->startOffset = $1;
11126                                         n->endOffset = NULL;
11127                                         $$ = n;
11128                                 }
11129                         | a_expr FOLLOWING
11130                                 {
11131                                         WindowDef *n = makeNode(WindowDef);
11132                                         n->frameOptions = FRAMEOPTION_START_VALUE_FOLLOWING;
11133                                         n->startOffset = $1;
11134                                         n->endOffset = NULL;
11135                                         $$ = n;
11136                                 }
11137                 ;
11138
11139
11140 /*
11141  * Supporting nonterminals for expressions.
11142  */
11143
11144 /* Explicit row production.
11145  *
11146  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
11147  * without conflicting with the parenthesized a_expr production.  Without the
11148  * ROW keyword, there must be more than one a_expr inside the parens.
11149  */
11150 row:            ROW '(' expr_list ')'                                   { $$ = $3; }
11151                         | ROW '(' ')'                                                   { $$ = NIL; }
11152                         | '(' expr_list ',' a_expr ')'                  { $$ = lappend($2, $4); }
11153                 ;
11154
11155 sub_type:       ANY                                                                             { $$ = ANY_SUBLINK; }
11156                         | SOME                                                                  { $$ = ANY_SUBLINK; }
11157                         | ALL                                                                   { $$ = ALL_SUBLINK; }
11158                 ;
11159
11160 all_Op:         Op                                                                              { $$ = $1; }
11161                         | MathOp                                                                { $$ = $1; }
11162                 ;
11163
11164 MathOp:          '+'                                                                    { $$ = "+"; }
11165                         | '-'                                                                   { $$ = "-"; }
11166                         | '*'                                                                   { $$ = "*"; }
11167                         | '/'                                                                   { $$ = "/"; }
11168                         | '%'                                                                   { $$ = "%"; }
11169                         | '^'                                                                   { $$ = "^"; }
11170                         | '<'                                                                   { $$ = "<"; }
11171                         | '>'                                                                   { $$ = ">"; }
11172                         | '='                                                                   { $$ = "="; }
11173                 ;
11174
11175 qual_Op:        Op
11176                                         { $$ = list_make1(makeString($1)); }
11177                         | OPERATOR '(' any_operator ')'
11178                                         { $$ = $3; }
11179                 ;
11180
11181 qual_all_Op:
11182                         all_Op
11183                                         { $$ = list_make1(makeString($1)); }
11184                         | OPERATOR '(' any_operator ')'
11185                                         { $$ = $3; }
11186                 ;
11187
11188 subquery_Op:
11189                         all_Op
11190                                         { $$ = list_make1(makeString($1)); }
11191                         | OPERATOR '(' any_operator ')'
11192                                         { $$ = $3; }
11193                         | LIKE
11194                                         { $$ = list_make1(makeString("~~")); }
11195                         | NOT LIKE
11196                                         { $$ = list_make1(makeString("!~~")); }
11197                         | ILIKE
11198                                         { $$ = list_make1(makeString("~~*")); }
11199                         | NOT ILIKE
11200                                         { $$ = list_make1(makeString("!~~*")); }
11201 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
11202  * the regular expression is preprocessed by a function (similar_escape),
11203  * and the ~ operator for posix regular expressions is used.
11204  *        x SIMILAR TO y     ->    x ~ similar_escape(y)
11205  * this transformation is made on the fly by the parser upwards.
11206  * however the SubLink structure which handles any/some/all stuff
11207  * is not ready for such a thing.
11208  */
11209                         ;
11210
11211 expr_list:      a_expr
11212                                 {
11213                                         $$ = list_make1($1);
11214                                 }
11215                         | expr_list ',' a_expr
11216                                 {
11217                                         $$ = lappend($1, $3);
11218                                 }
11219                 ;
11220
11221 /* function arguments can have names */
11222 func_arg_list:  func_arg_expr
11223                                 {
11224                                         $$ = list_make1($1);
11225                                 }
11226                         | func_arg_list ',' func_arg_expr
11227                                 {
11228                                         $$ = lappend($1, $3);
11229                                 }
11230                 ;
11231
11232 func_arg_expr:  a_expr
11233                                 {
11234                                         $$ = $1;
11235                                 }
11236                         | param_name COLON_EQUALS a_expr
11237                                 {
11238                                         NamedArgExpr *na = makeNode(NamedArgExpr);
11239                                         na->name = $1;
11240                                         na->arg = (Expr *) $3;
11241                                         na->argnumber = -1;             /* until determined */
11242                                         na->location = @1;
11243                                         $$ = (Node *) na;
11244                                 }
11245                 ;
11246
11247 type_list:      Typename                                                                { $$ = list_make1($1); }
11248                         | type_list ',' Typename                                { $$ = lappend($1, $3); }
11249                 ;
11250
11251 array_expr: '[' expr_list ']'
11252                                 {
11253                                         $$ = makeAArrayExpr($2, @1);
11254                                 }
11255                         | '[' array_expr_list ']'
11256                                 {
11257                                         $$ = makeAArrayExpr($2, @1);
11258                                 }
11259                         | '[' ']'
11260                                 {
11261                                         $$ = makeAArrayExpr(NIL, @1);
11262                                 }
11263                 ;
11264
11265 array_expr_list: array_expr                                                     { $$ = list_make1($1); }
11266                         | array_expr_list ',' array_expr                { $$ = lappend($1, $3); }
11267                 ;
11268
11269
11270 extract_list:
11271                         extract_arg FROM a_expr
11272                                 {
11273                                         $$ = list_make2(makeStringConst($1, @1), $3);
11274                                 }
11275                         | /*EMPTY*/                                                             { $$ = NIL; }
11276                 ;
11277
11278 /* Allow delimited string Sconst in extract_arg as an SQL extension.
11279  * - thomas 2001-04-12
11280  */
11281 extract_arg:
11282                         IDENT                                                                   { $$ = $1; }
11283                         | YEAR_P                                                                { $$ = "year"; }
11284                         | MONTH_P                                                               { $$ = "month"; }
11285                         | DAY_P                                                                 { $$ = "day"; }
11286                         | HOUR_P                                                                { $$ = "hour"; }
11287                         | MINUTE_P                                                              { $$ = "minute"; }
11288                         | SECOND_P                                                              { $$ = "second"; }
11289                         | Sconst                                                                { $$ = $1; }
11290                 ;
11291
11292 /* OVERLAY() arguments
11293  * SQL99 defines the OVERLAY() function:
11294  * o overlay(text placing text from int for int)
11295  * o overlay(text placing text from int)
11296  * and similarly for binary strings
11297  */
11298 overlay_list:
11299                         a_expr overlay_placing substr_from substr_for
11300                                 {
11301                                         $$ = list_make4($1, $2, $3, $4);
11302                                 }
11303                         | a_expr overlay_placing substr_from
11304                                 {
11305                                         $$ = list_make3($1, $2, $3);
11306                                 }
11307                 ;
11308
11309 overlay_placing:
11310                         PLACING a_expr
11311                                 { $$ = $2; }
11312                 ;
11313
11314 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
11315
11316 position_list:
11317                         b_expr IN_P b_expr                                              { $$ = list_make2($3, $1); }
11318                         | /*EMPTY*/                                                             { $$ = NIL; }
11319                 ;
11320
11321 /* SUBSTRING() arguments
11322  * SQL9x defines a specific syntax for arguments to SUBSTRING():
11323  * o substring(text from int for int)
11324  * o substring(text from int) get entire string from starting point "int"
11325  * o substring(text for int) get first "int" characters of string
11326  * o substring(text from pattern) get entire string matching pattern
11327  * o substring(text from pattern for escape) same with specified escape char
11328  * We also want to support generic substring functions which accept
11329  * the usual generic list of arguments. So we will accept both styles
11330  * here, and convert the SQL9x style to the generic list for further
11331  * processing. - thomas 2000-11-28
11332  */
11333 substr_list:
11334                         a_expr substr_from substr_for
11335                                 {
11336                                         $$ = list_make3($1, $2, $3);
11337                                 }
11338                         | a_expr substr_for substr_from
11339                                 {
11340                                         /* not legal per SQL99, but might as well allow it */
11341                                         $$ = list_make3($1, $3, $2);
11342                                 }
11343                         | a_expr substr_from
11344                                 {
11345                                         $$ = list_make2($1, $2);
11346                                 }
11347                         | a_expr substr_for
11348                                 {
11349                                         /*
11350                                          * Since there are no cases where this syntax allows
11351                                          * a textual FOR value, we forcibly cast the argument
11352                                          * to int4.  The possible matches in pg_proc are
11353                                          * substring(text,int4) and substring(text,text),
11354                                          * and we don't want the parser to choose the latter,
11355                                          * which it is likely to do if the second argument
11356                                          * is unknown or doesn't have an implicit cast to int4.
11357                                          */
11358                                         $$ = list_make3($1, makeIntConst(1, -1),
11359                                                                         makeTypeCast($2,
11360                                                                                                  SystemTypeName("int4"), -1));
11361                                 }
11362                         | expr_list
11363                                 {
11364                                         $$ = $1;
11365                                 }
11366                         | /*EMPTY*/
11367                                 { $$ = NIL; }
11368                 ;
11369
11370 substr_from:
11371                         FROM a_expr                                                             { $$ = $2; }
11372                 ;
11373
11374 substr_for: FOR a_expr                                                          { $$ = $2; }
11375                 ;
11376
11377 trim_list:      a_expr FROM expr_list                                   { $$ = lappend($3, $1); }
11378                         | FROM expr_list                                                { $$ = $2; }
11379                         | expr_list                                                             { $$ = $1; }
11380                 ;
11381
11382 in_expr:        select_with_parens
11383                                 {
11384                                         SubLink *n = makeNode(SubLink);
11385                                         n->subselect = $1;
11386                                         /* other fields will be filled later */
11387                                         $$ = (Node *)n;
11388                                 }
11389                         | '(' expr_list ')'                                             { $$ = (Node *)$2; }
11390                 ;
11391
11392 /*
11393  * Define SQL92-style case clause.
11394  * - Full specification
11395  *      CASE WHEN a = b THEN c ... ELSE d END
11396  * - Implicit argument
11397  *      CASE a WHEN b THEN c ... ELSE d END
11398  */
11399 case_expr:      CASE case_arg when_clause_list case_default END_P
11400                                 {
11401                                         CaseExpr *c = makeNode(CaseExpr);
11402                                         c->casetype = InvalidOid; /* not analyzed yet */
11403                                         c->arg = (Expr *) $2;
11404                                         c->args = $3;
11405                                         c->defresult = (Expr *) $4;
11406                                         c->location = @1;
11407                                         $$ = (Node *)c;
11408                                 }
11409                 ;
11410
11411 when_clause_list:
11412                         /* There must be at least one */
11413                         when_clause                                                             { $$ = list_make1($1); }
11414                         | when_clause_list when_clause                  { $$ = lappend($1, $2); }
11415                 ;
11416
11417 when_clause:
11418                         WHEN a_expr THEN a_expr
11419                                 {
11420                                         CaseWhen *w = makeNode(CaseWhen);
11421                                         w->expr = (Expr *) $2;
11422                                         w->result = (Expr *) $4;
11423                                         w->location = @1;
11424                                         $$ = (Node *)w;
11425                                 }
11426                 ;
11427
11428 case_default:
11429                         ELSE a_expr                                                             { $$ = $2; }
11430                         | /*EMPTY*/                                                             { $$ = NULL; }
11431                 ;
11432
11433 case_arg:       a_expr                                                                  { $$ = $1; }
11434                         | /*EMPTY*/                                                             { $$ = NULL; }
11435                 ;
11436
11437 columnref:      ColId
11438                                 {
11439                                         $$ = makeColumnRef($1, NIL, @1, yyscanner);
11440                                 }
11441                         | ColId indirection
11442                                 {
11443                                         $$ = makeColumnRef($1, $2, @1, yyscanner);
11444                                 }
11445                 ;
11446
11447 indirection_el:
11448                         '.' attr_name
11449                                 {
11450                                         $$ = (Node *) makeString($2);
11451                                 }
11452                         | '.' '*'
11453                                 {
11454                                         $$ = (Node *) makeNode(A_Star);
11455                                 }
11456                         | '[' a_expr ']'
11457                                 {
11458                                         A_Indices *ai = makeNode(A_Indices);
11459                                         ai->lidx = NULL;
11460                                         ai->uidx = $2;
11461                                         $$ = (Node *) ai;
11462                                 }
11463                         | '[' a_expr ':' a_expr ']'
11464                                 {
11465                                         A_Indices *ai = makeNode(A_Indices);
11466                                         ai->lidx = $2;
11467                                         ai->uidx = $4;
11468                                         $$ = (Node *) ai;
11469                                 }
11470                 ;
11471
11472 indirection:
11473                         indirection_el                                                  { $$ = list_make1($1); }
11474                         | indirection indirection_el                    { $$ = lappend($1, $2); }
11475                 ;
11476
11477 opt_indirection:
11478                         /*EMPTY*/                                                               { $$ = NIL; }
11479                         | opt_indirection indirection_el                { $$ = lappend($1, $2); }
11480                 ;
11481
11482 opt_asymmetric: ASYMMETRIC
11483                         | /*EMPTY*/
11484                 ;
11485
11486 /*
11487  * The SQL spec defines "contextually typed value expressions" and
11488  * "contextually typed row value constructors", which for our purposes
11489  * are the same as "a_expr" and "row" except that DEFAULT can appear at
11490  * the top level.
11491  */
11492
11493 ctext_expr:
11494                         a_expr                                  { $$ = (Node *) $1; }
11495                         | DEFAULT
11496                                 {
11497                                         SetToDefault *n = makeNode(SetToDefault);
11498                                         n->location = @1;
11499                                         $$ = (Node *) n;
11500                                 }
11501                 ;
11502
11503 ctext_expr_list:
11504                         ctext_expr                                                              { $$ = list_make1($1); }
11505                         | ctext_expr_list ',' ctext_expr                { $$ = lappend($1, $3); }
11506                 ;
11507
11508 /*
11509  * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
11510  * making VALUES a fully reserved word, which will probably break more apps
11511  * than allowing the noise-word is worth.
11512  */
11513 ctext_row: '(' ctext_expr_list ')'                                      { $$ = $2; }
11514                 ;
11515
11516
11517 /*****************************************************************************
11518  *
11519  *      target list for SELECT
11520  *
11521  *****************************************************************************/
11522
11523 target_list:
11524                         target_el                                                               { $$ = list_make1($1); }
11525                         | target_list ',' target_el                             { $$ = lappend($1, $3); }
11526                 ;
11527
11528 target_el:      a_expr AS ColLabel
11529                                 {
11530                                         $$ = makeNode(ResTarget);
11531                                         $$->name = $3;
11532                                         $$->indirection = NIL;
11533                                         $$->val = (Node *)$1;
11534                                         $$->location = @1;
11535                                 }
11536                         /*
11537                          * We support omitting AS only for column labels that aren't
11538                          * any known keyword.  There is an ambiguity against postfix
11539                          * operators: is "a ! b" an infix expression, or a postfix
11540                          * expression and a column label?  We prefer to resolve this
11541                          * as an infix expression, which we accomplish by assigning
11542                          * IDENT a precedence higher than POSTFIXOP.
11543                          */
11544                         | a_expr IDENT
11545                                 {
11546                                         $$ = makeNode(ResTarget);
11547                                         $$->name = $2;
11548                                         $$->indirection = NIL;
11549                                         $$->val = (Node *)$1;
11550                                         $$->location = @1;
11551                                 }
11552                         | a_expr
11553                                 {
11554                                         $$ = makeNode(ResTarget);
11555                                         $$->name = NULL;
11556                                         $$->indirection = NIL;
11557                                         $$->val = (Node *)$1;
11558                                         $$->location = @1;
11559                                 }
11560                         | '*'
11561                                 {
11562                                         ColumnRef *n = makeNode(ColumnRef);
11563                                         n->fields = list_make1(makeNode(A_Star));
11564                                         n->location = @1;
11565
11566                                         $$ = makeNode(ResTarget);
11567                                         $$->name = NULL;
11568                                         $$->indirection = NIL;
11569                                         $$->val = (Node *)n;
11570                                         $$->location = @1;
11571                                 }
11572                 ;
11573
11574
11575 /*****************************************************************************
11576  *
11577  *      Names and constants
11578  *
11579  *****************************************************************************/
11580
11581 qualified_name_list:
11582                         qualified_name                                                  { $$ = list_make1($1); }
11583                         | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
11584                 ;
11585
11586 /*
11587  * The production for a qualified relation name has to exactly match the
11588  * production for a qualified func_name, because in a FROM clause we cannot
11589  * tell which we are parsing until we see what comes after it ('(' for a
11590  * func_name, something else for a relation). Therefore we allow 'indirection'
11591  * which may contain subscripts, and reject that case in the C code.
11592  */
11593 qualified_name:
11594                         ColId
11595                                 {
11596                                         $$ = makeRangeVar(NULL, $1, @1);
11597                                 }
11598                         | ColId indirection
11599                                 {
11600                                         check_qualified_name($2, yyscanner);
11601                                         $$ = makeRangeVar(NULL, NULL, @1);
11602                                         switch (list_length($2))
11603                                         {
11604                                                 case 1:
11605                                                         $$->catalogname = NULL;
11606                                                         $$->schemaname = $1;
11607                                                         $$->relname = strVal(linitial($2));
11608                                                         break;
11609                                                 case 2:
11610                                                         $$->catalogname = $1;
11611                                                         $$->schemaname = strVal(linitial($2));
11612                                                         $$->relname = strVal(lsecond($2));
11613                                                         break;
11614                                                 default:
11615                                                         ereport(ERROR,
11616                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
11617                                                                          errmsg("improper qualified name (too many dotted names): %s",
11618                                                                                         NameListToString(lcons(makeString($1), $2))),
11619                                                                          parser_errposition(@1)));
11620                                                         break;
11621                                         }
11622                                 }
11623                 ;
11624
11625 name_list:      name
11626                                         { $$ = list_make1(makeString($1)); }
11627                         | name_list ',' name
11628                                         { $$ = lappend($1, makeString($3)); }
11629                 ;
11630
11631
11632 name:           ColId                                                                   { $$ = $1; };
11633
11634 database_name:
11635                         ColId                                                                   { $$ = $1; };
11636
11637 access_method:
11638                         ColId                                                                   { $$ = $1; };
11639
11640 attr_name:      ColLabel                                                                { $$ = $1; };
11641
11642 index_name: ColId                                                                       { $$ = $1; };
11643
11644 file_name:      Sconst                                                                  { $$ = $1; };
11645
11646 /*
11647  * The production for a qualified func_name has to exactly match the
11648  * production for a qualified columnref, because we cannot tell which we
11649  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
11650  * anything else for a columnref).  Therefore we allow 'indirection' which
11651  * may contain subscripts, and reject that case in the C code.  (If we
11652  * ever implement SQL99-like methods, such syntax may actually become legal!)
11653  */
11654 func_name:      type_function_name
11655                                         { $$ = list_make1(makeString($1)); }
11656                         | ColId indirection
11657                                         {
11658                                                 $$ = check_func_name(lcons(makeString($1), $2),
11659                                                                                          yyscanner);
11660                                         }
11661                 ;
11662
11663
11664 /*
11665  * Constants
11666  */
11667 AexprConst: Iconst
11668                                 {
11669                                         $$ = makeIntConst($1, @1);
11670                                 }
11671                         | FCONST
11672                                 {
11673                                         $$ = makeFloatConst($1, @1);
11674                                 }
11675                         | Sconst
11676                                 {
11677                                         $$ = makeStringConst($1, @1);
11678                                 }
11679                         | BCONST
11680                                 {
11681                                         $$ = makeBitStringConst($1, @1);
11682                                 }
11683                         | XCONST
11684                                 {
11685                                         /* This is a bit constant per SQL99:
11686                                          * Without Feature F511, "BIT data type",
11687                                          * a <general literal> shall not be a
11688                                          * <bit string literal> or a <hex string literal>.
11689                                          */
11690                                         $$ = makeBitStringConst($1, @1);
11691                                 }
11692                         | func_name Sconst
11693                                 {
11694                                         /* generic type 'literal' syntax */
11695                                         TypeName *t = makeTypeNameFromNameList($1);
11696                                         t->location = @1;
11697                                         $$ = makeStringConstCast($2, @2, t);
11698                                 }
11699                         | func_name '(' func_arg_list ')' Sconst
11700                                 {
11701                                         /* generic syntax with a type modifier */
11702                                         TypeName *t = makeTypeNameFromNameList($1);
11703                                         ListCell *lc;
11704
11705                                         /*
11706                                          * We must use func_arg_list in the production to avoid
11707                                          * reduce/reduce conflicts, but we don't actually wish
11708                                          * to allow NamedArgExpr in this context.
11709                                          */
11710                                         foreach(lc, $3)
11711                                         {
11712                                                 NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
11713
11714                                                 if (IsA(arg, NamedArgExpr))
11715                                                         ereport(ERROR,
11716                                                                     (errcode(ERRCODE_SYNTAX_ERROR),
11717                                                                      errmsg("type modifier cannot have parameter name"),
11718                                                                      parser_errposition(arg->location)));
11719                                         }
11720                                         t->typmods = $3;
11721                                         t->location = @1;
11722                                         $$ = makeStringConstCast($5, @5, t);
11723                                 }
11724                         | ConstTypename Sconst
11725                                 {
11726                                         $$ = makeStringConstCast($2, @2, $1);
11727                                 }
11728                         | ConstInterval Sconst opt_interval
11729                                 {
11730                                         TypeName *t = $1;
11731                                         t->typmods = $3;
11732                                         $$ = makeStringConstCast($2, @2, t);
11733                                 }
11734                         | ConstInterval '(' Iconst ')' Sconst opt_interval
11735                                 {
11736                                         TypeName *t = $1;
11737                                         if ($6 != NIL)
11738                                         {
11739                                                 if (list_length($6) != 1)
11740                                                         ereport(ERROR,
11741                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
11742                                                                          errmsg("interval precision specified twice"),
11743                                                                          parser_errposition(@1)));
11744                                                 t->typmods = lappend($6, makeIntConst($3, @3));
11745                                         }
11746                                         else
11747                                                 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
11748                                                                                                 makeIntConst($3, @3));
11749                                         $$ = makeStringConstCast($5, @5, t);
11750                                 }
11751                         | TRUE_P
11752                                 {
11753                                         $$ = makeBoolAConst(TRUE, @1);
11754                                 }
11755                         | FALSE_P
11756                                 {
11757                                         $$ = makeBoolAConst(FALSE, @1);
11758                                 }
11759                         | NULL_P
11760                                 {
11761                                         $$ = makeNullAConst(@1);
11762                                 }
11763                 ;
11764
11765 Iconst:         ICONST                                                                  { $$ = $1; };
11766 Sconst:         SCONST                                                                  { $$ = $1; };
11767 RoleId:         ColId                                                                   { $$ = $1; };
11768
11769 SignedIconst: Iconst                                                            { $$ = $1; }
11770                         | '+' Iconst                                                    { $$ = + $2; }
11771                         | '-' Iconst                                                    { $$ = - $2; }
11772                 ;
11773
11774 /*
11775  * Name classification hierarchy.
11776  *
11777  * IDENT is the lexeme returned by the lexer for identifiers that match
11778  * no known keyword.  In most cases, we can accept certain keywords as
11779  * names, not only IDENTs.      We prefer to accept as many such keywords
11780  * as possible to minimize the impact of "reserved words" on programmers.
11781  * So, we divide names into several possible classes.  The classification
11782  * is chosen in part to make keywords acceptable as names wherever possible.
11783  */
11784
11785 /* Column identifier --- names that can be column, table, etc names.
11786  */
11787 ColId:          IDENT                                                                   { $$ = $1; }
11788                         | unreserved_keyword                                    { $$ = pstrdup($1); }
11789                         | col_name_keyword                                              { $$ = pstrdup($1); }
11790                 ;
11791
11792 /* Type/function identifier --- names that can be type or function names.
11793  */
11794 type_function_name:     IDENT                                                   { $$ = $1; }
11795                         | unreserved_keyword                                    { $$ = pstrdup($1); }
11796                         | type_func_name_keyword                                { $$ = pstrdup($1); }
11797                 ;
11798
11799 /* Column label --- allowed labels in "AS" clauses.
11800  * This presently includes *all* Postgres keywords.
11801  */
11802 ColLabel:       IDENT                                                                   { $$ = $1; }
11803                         | unreserved_keyword                                    { $$ = pstrdup($1); }
11804                         | col_name_keyword                                              { $$ = pstrdup($1); }
11805                         | type_func_name_keyword                                { $$ = pstrdup($1); }
11806                         | reserved_keyword                                              { $$ = pstrdup($1); }
11807                 ;
11808
11809
11810 /*
11811  * Keyword category lists.  Generally, every keyword present in
11812  * the Postgres grammar should appear in exactly one of these lists.
11813  *
11814  * Put a new keyword into the first list that it can go into without causing
11815  * shift or reduce conflicts.  The earlier lists define "less reserved"
11816  * categories of keywords.
11817  *
11818  * Make sure that each keyword's category in kwlist.h matches where
11819  * it is listed here.  (Someday we may be able to generate these lists and
11820  * kwlist.h's table from a common master list.)
11821  */
11822
11823 /* "Unreserved" keywords --- available for use as any kind of name.
11824  */
11825 unreserved_keyword:
11826                           ABORT_P
11827                         | ABSOLUTE_P
11828                         | ACCESS
11829                         | ACTION
11830                         | ADD_P
11831                         | ADMIN
11832                         | AFTER
11833                         | AGGREGATE
11834                         | ALSO
11835                         | ALTER
11836                         | ALWAYS
11837                         | ASSERTION
11838                         | ASSIGNMENT
11839                         | AT
11840                         | ATTRIBUTE
11841                         | BACKWARD
11842                         | BEFORE
11843                         | BEGIN_P
11844                         | BY
11845                         | CACHE
11846                         | CALLED
11847                         | CASCADE
11848                         | CASCADED
11849                         | CATALOG_P
11850                         | CHAIN
11851                         | CHARACTERISTICS
11852                         | CHECKPOINT
11853                         | CLASS
11854                         | CLOSE
11855                         | CLUSTER
11856                         | COLLATION
11857                         | COMMENT
11858                         | COMMENTS
11859                         | COMMIT
11860                         | COMMITTED
11861                         | CONFIGURATION
11862                         | CONNECTION
11863                         | CONSTRAINTS
11864                         | CONTENT_P
11865                         | CONTINUE_P
11866                         | CONVERSION_P
11867                         | COPY
11868                         | COST
11869                         | CSV
11870                         | CURRENT_P
11871                         | CURSOR
11872                         | CYCLE
11873                         | DATA_P
11874                         | DATABASE
11875                         | DAY_P
11876                         | DEALLOCATE
11877                         | DECLARE
11878                         | DEFAULTS
11879                         | DEFERRED
11880                         | DEFINER
11881                         | DELETE_P
11882                         | DELIMITER
11883                         | DELIMITERS
11884                         | DICTIONARY
11885                         | DISABLE_P
11886                         | DISCARD
11887                         | DOCUMENT_P
11888                         | DOMAIN_P
11889                         | DOUBLE_P
11890                         | DROP
11891                         | EACH
11892                         | ENABLE_P
11893                         | ENCODING
11894                         | ENCRYPTED
11895                         | ENUM_P
11896                         | ESCAPE
11897                         | EXCLUDE
11898                         | EXCLUDING
11899                         | EXCLUSIVE
11900                         | EXECUTE
11901                         | EXPLAIN
11902                         | EXTENSION
11903                         | EXTERNAL
11904                         | FAMILY
11905                         | FIRST_P
11906                         | FOLLOWING
11907                         | FORCE
11908                         | FORWARD
11909                         | FUNCTION
11910                         | FUNCTIONS
11911                         | GLOBAL
11912                         | GRANTED
11913                         | HANDLER
11914                         | HEADER_P
11915                         | HOLD
11916                         | HOUR_P
11917                         | IDENTITY_P
11918                         | IF_P
11919                         | IMMEDIATE
11920                         | IMMUTABLE
11921                         | IMPLICIT_P
11922                         | INCLUDING
11923                         | INCREMENT
11924                         | INDEX
11925                         | INDEXES
11926                         | INHERIT
11927                         | INHERITS
11928                         | INLINE_P
11929                         | INPUT_P
11930                         | INSENSITIVE
11931                         | INSERT
11932                         | INSTEAD
11933                         | INVOKER
11934                         | ISOLATION
11935                         | KEY
11936                         | LABEL
11937                         | LANGUAGE
11938                         | LARGE_P
11939                         | LAST_P
11940                         | LC_COLLATE_P
11941                         | LC_CTYPE_P
11942                         | LEVEL
11943                         | LISTEN
11944                         | LOAD
11945                         | LOCAL
11946                         | LOCATION
11947                         | LOCK_P
11948                         | MAPPING
11949                         | MATCH
11950                         | MAXVALUE
11951                         | MINUTE_P
11952                         | MINVALUE
11953                         | MODE
11954                         | MONTH_P
11955                         | MOVE
11956                         | NAME_P
11957                         | NAMES
11958                         | NEXT
11959                         | NO
11960                         | NOTHING
11961                         | NOTIFY
11962                         | NOWAIT
11963                         | NULLS_P
11964                         | OBJECT_P
11965                         | OF
11966                         | OFF
11967                         | OIDS
11968                         | OPERATOR
11969                         | OPTION
11970                         | OPTIONS
11971                         | OWNED
11972                         | OWNER
11973                         | PARSER
11974                         | PARTIAL
11975                         | PARTITION
11976                         | PASSING
11977                         | PASSWORD
11978                         | PLANS
11979                         | PRECEDING
11980                         | PREPARE
11981                         | PREPARED
11982                         | PRESERVE
11983                         | PRIOR
11984                         | PRIVILEGES
11985                         | PROCEDURAL
11986                         | PROCEDURE
11987                         | QUOTE
11988                         | RANGE
11989                         | READ
11990                         | REASSIGN
11991                         | RECHECK
11992                         | RECURSIVE
11993                         | REF
11994                         | REINDEX
11995                         | RELATIVE_P
11996                         | RELEASE
11997                         | RENAME
11998                         | REPEATABLE
11999                         | REPLACE
12000                         | REPLICA
12001                         | RESET
12002                         | RESTART
12003                         | RESTRICT
12004                         | RETURNS
12005                         | REVOKE
12006                         | ROLE
12007                         | ROLLBACK
12008                         | ROWS
12009                         | RULE
12010                         | SAVEPOINT
12011                         | SCHEMA
12012                         | SCROLL
12013                         | SEARCH
12014                         | SECOND_P
12015                         | SECURITY
12016                         | SEQUENCE
12017                         | SEQUENCES
12018                         | SERIALIZABLE
12019                         | SERVER
12020                         | SESSION
12021                         | SET
12022                         | SHARE
12023                         | SHOW
12024                         | SIMPLE
12025                         | STABLE
12026                         | STANDALONE_P
12027                         | START
12028                         | STATEMENT
12029                         | STATISTICS
12030                         | STDIN
12031                         | STDOUT
12032                         | STORAGE
12033                         | STRICT_P
12034                         | STRIP_P
12035                         | SYSID
12036                         | SYSTEM_P
12037                         | TABLES
12038                         | TABLESPACE
12039                         | TEMP
12040                         | TEMPLATE
12041                         | TEMPORARY
12042                         | TEXT_P
12043                         | TRANSACTION
12044                         | TRIGGER
12045                         | TRUNCATE
12046                         | TRUSTED
12047                         | TYPE_P
12048                         | UNBOUNDED
12049                         | UNCOMMITTED
12050                         | UNENCRYPTED
12051                         | UNKNOWN
12052                         | UNLISTEN
12053                         | UNLOGGED
12054                         | UNTIL
12055                         | UPDATE
12056                         | VACUUM
12057                         | VALID
12058                         | VALIDATE
12059                         | VALIDATOR
12060                         | VALUE_P
12061                         | VARYING
12062                         | VERSION_P
12063                         | VIEW
12064                         | VOLATILE
12065                         | WHITESPACE_P
12066                         | WITHOUT
12067                         | WORK
12068                         | WRAPPER
12069                         | WRITE
12070                         | XML_P
12071                         | YEAR_P
12072                         | YES_P
12073                         | ZONE
12074                 ;
12075
12076 /* Column identifier --- keywords that can be column, table, etc names.
12077  *
12078  * Many of these keywords will in fact be recognized as type or function
12079  * names too; but they have special productions for the purpose, and so
12080  * can't be treated as "generic" type or function names.
12081  *
12082  * The type names appearing here are not usable as function names
12083  * because they can be followed by '(' in typename productions, which
12084  * looks too much like a function call for an LR(1) parser.
12085  */
12086 col_name_keyword:
12087                           BETWEEN
12088                         | BIGINT
12089                         | BIT
12090                         | BOOLEAN_P
12091                         | CHAR_P
12092                         | CHARACTER
12093                         | COALESCE
12094                         | DEC
12095                         | DECIMAL_P
12096                         | EXISTS
12097                         | EXTRACT
12098                         | FLOAT_P
12099                         | GREATEST
12100                         | INOUT
12101                         | INT_P
12102                         | INTEGER
12103                         | INTERVAL
12104                         | LEAST
12105                         | NATIONAL
12106                         | NCHAR
12107                         | NONE
12108                         | NULLIF
12109                         | NUMERIC
12110                         | OUT_P
12111                         | OVERLAY
12112                         | POSITION
12113                         | PRECISION
12114                         | REAL
12115                         | ROW
12116                         | SETOF
12117                         | SMALLINT
12118                         | SUBSTRING
12119                         | TIME
12120                         | TIMESTAMP
12121                         | TREAT
12122                         | TRIM
12123                         | VALUES
12124                         | VARCHAR
12125                         | XMLATTRIBUTES
12126                         | XMLCONCAT
12127                         | XMLELEMENT
12128                         | XMLEXISTS
12129                         | XMLFOREST
12130                         | XMLPARSE
12131                         | XMLPI
12132                         | XMLROOT
12133                         | XMLSERIALIZE
12134                 ;
12135
12136 /* Type/function identifier --- keywords that can be type or function names.
12137  *
12138  * Most of these are keywords that are used as operators in expressions;
12139  * in general such keywords can't be column names because they would be
12140  * ambiguous with variables, but they are unambiguous as function identifiers.
12141  *
12142  * Do not include POSITION, SUBSTRING, etc here since they have explicit
12143  * productions in a_expr to support the goofy SQL9x argument syntax.
12144  * - thomas 2000-11-28
12145  */
12146 type_func_name_keyword:
12147                           AUTHORIZATION
12148                         | BINARY
12149                         | CONCURRENTLY
12150                         | CROSS
12151                         | CURRENT_SCHEMA
12152                         | FREEZE
12153                         | FULL
12154                         | ILIKE
12155                         | INNER_P
12156                         | IS
12157                         | ISNULL
12158                         | JOIN
12159                         | LEFT
12160                         | LIKE
12161                         | NATURAL
12162                         | NOTNULL
12163                         | OUTER_P
12164                         | OVER
12165                         | OVERLAPS
12166                         | RIGHT
12167                         | SIMILAR
12168                         | VERBOSE
12169                 ;
12170
12171 /* Reserved keyword --- these keywords are usable only as a ColLabel.
12172  *
12173  * Keywords appear here if they could not be distinguished from variable,
12174  * type, or function names in some contexts.  Don't put things here unless
12175  * forced to.
12176  */
12177 reserved_keyword:
12178                           ALL
12179                         | ANALYSE
12180                         | ANALYZE
12181                         | AND
12182                         | ANY
12183                         | ARRAY
12184                         | AS
12185                         | ASC
12186                         | ASYMMETRIC
12187                         | BOTH
12188                         | CASE
12189                         | CAST
12190                         | CHECK
12191                         | COLLATE
12192                         | COLUMN
12193                         | CONSTRAINT
12194                         | CREATE
12195                         | CURRENT_CATALOG
12196                         | CURRENT_DATE
12197                         | CURRENT_ROLE
12198                         | CURRENT_TIME
12199                         | CURRENT_TIMESTAMP
12200                         | CURRENT_USER
12201                         | DEFAULT
12202                         | DEFERRABLE
12203                         | DESC
12204                         | DISTINCT
12205                         | DO
12206                         | ELSE
12207                         | END_P
12208                         | EXCEPT
12209                         | FALSE_P
12210                         | FETCH
12211                         | FOR
12212                         | FOREIGN
12213                         | FROM
12214                         | GRANT
12215                         | GROUP_P
12216                         | HAVING
12217                         | IN_P
12218                         | INITIALLY
12219                         | INTERSECT
12220                         | INTO
12221                         | LEADING
12222                         | LIMIT
12223                         | LOCALTIME
12224                         | LOCALTIMESTAMP
12225                         | NOT
12226                         | NULL_P
12227                         | OFFSET
12228                         | ON
12229                         | ONLY
12230                         | OR
12231                         | ORDER
12232                         | PLACING
12233                         | PRIMARY
12234                         | REFERENCES
12235                         | RETURNING
12236                         | SELECT
12237                         | SESSION_USER
12238                         | SOME
12239                         | SYMMETRIC
12240                         | TABLE
12241                         | THEN
12242                         | TO
12243                         | TRAILING
12244                         | TRUE_P
12245                         | UNION
12246                         | UNIQUE
12247                         | USER
12248                         | USING
12249                         | VARIADIC
12250                         | WHEN
12251                         | WHERE
12252                         | WINDOW
12253                         | WITH
12254                 ;
12255
12256 %%
12257
12258 /*
12259  * The signature of this function is required by bison.  However, we
12260  * ignore the passed yylloc and instead use the last token position
12261  * available from the scanner.
12262  */
12263 static void
12264 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
12265 {
12266         parser_yyerror(msg);
12267 }
12268
12269 static Node *
12270 makeColumnRef(char *colname, List *indirection,
12271                           int location, core_yyscan_t yyscanner)
12272 {
12273         /*
12274          * Generate a ColumnRef node, with an A_Indirection node added if there
12275          * is any subscripting in the specified indirection list.  However,
12276          * any field selection at the start of the indirection list must be
12277          * transposed into the "fields" part of the ColumnRef node.
12278          */
12279         ColumnRef  *c = makeNode(ColumnRef);
12280         int             nfields = 0;
12281         ListCell *l;
12282
12283         c->location = location;
12284         foreach(l, indirection)
12285         {
12286                 if (IsA(lfirst(l), A_Indices))
12287                 {
12288                         A_Indirection *i = makeNode(A_Indirection);
12289
12290                         if (nfields == 0)
12291                         {
12292                                 /* easy case - all indirection goes to A_Indirection */
12293                                 c->fields = list_make1(makeString(colname));
12294                                 i->indirection = check_indirection(indirection, yyscanner);
12295                         }
12296                         else
12297                         {
12298                                 /* got to split the list in two */
12299                                 i->indirection = check_indirection(list_copy_tail(indirection,
12300                                                                                                                                   nfields),
12301                                                                                                    yyscanner);
12302                                 indirection = list_truncate(indirection, nfields);
12303                                 c->fields = lcons(makeString(colname), indirection);
12304                         }
12305                         i->arg = (Node *) c;
12306                         return (Node *) i;
12307                 }
12308                 else if (IsA(lfirst(l), A_Star))
12309                 {
12310                         /* We only allow '*' at the end of a ColumnRef */
12311                         if (lnext(l) != NULL)
12312                                 parser_yyerror("improper use of \"*\"");
12313                 }
12314                 nfields++;
12315         }
12316         /* No subscripting, so all indirection gets added to field list */
12317         c->fields = lcons(makeString(colname), indirection);
12318         return (Node *) c;
12319 }
12320
12321 static Node *
12322 makeTypeCast(Node *arg, TypeName *typename, int location)
12323 {
12324         TypeCast *n = makeNode(TypeCast);
12325         n->arg = arg;
12326         n->typeName = typename;
12327         n->location = location;
12328         return (Node *) n;
12329 }
12330
12331 static Node *
12332 makeStringConst(char *str, int location)
12333 {
12334         A_Const *n = makeNode(A_Const);
12335
12336         n->val.type = T_String;
12337         n->val.val.str = str;
12338         n->location = location;
12339
12340         return (Node *)n;
12341 }
12342
12343 static Node *
12344 makeStringConstCast(char *str, int location, TypeName *typename)
12345 {
12346         Node *s = makeStringConst(str, location);
12347
12348         return makeTypeCast(s, typename, -1);
12349 }
12350
12351 static Node *
12352 makeIntConst(int val, int location)
12353 {
12354         A_Const *n = makeNode(A_Const);
12355
12356         n->val.type = T_Integer;
12357         n->val.val.ival = val;
12358         n->location = location;
12359
12360         return (Node *)n;
12361 }
12362
12363 static Node *
12364 makeFloatConst(char *str, int location)
12365 {
12366         A_Const *n = makeNode(A_Const);
12367
12368         n->val.type = T_Float;
12369         n->val.val.str = str;
12370         n->location = location;
12371
12372         return (Node *)n;
12373 }
12374
12375 static Node *
12376 makeBitStringConst(char *str, int location)
12377 {
12378         A_Const *n = makeNode(A_Const);
12379
12380         n->val.type = T_BitString;
12381         n->val.val.str = str;
12382         n->location = location;
12383
12384         return (Node *)n;
12385 }
12386
12387 static Node *
12388 makeNullAConst(int location)
12389 {
12390         A_Const *n = makeNode(A_Const);
12391
12392         n->val.type = T_Null;
12393         n->location = location;
12394
12395         return (Node *)n;
12396 }
12397
12398 static Node *
12399 makeAConst(Value *v, int location)
12400 {
12401         Node *n;
12402
12403         switch (v->type)
12404         {
12405                 case T_Float:
12406                         n = makeFloatConst(v->val.str, location);
12407                         break;
12408
12409                 case T_Integer:
12410                         n = makeIntConst(v->val.ival, location);
12411                         break;
12412
12413                 case T_String:
12414                 default:
12415                         n = makeStringConst(v->val.str, location);
12416                         break;
12417         }
12418
12419         return n;
12420 }
12421
12422 /* makeBoolAConst()
12423  * Create an A_Const string node and put it inside a boolean cast.
12424  */
12425 static Node *
12426 makeBoolAConst(bool state, int location)
12427 {
12428         A_Const *n = makeNode(A_Const);
12429
12430         n->val.type = T_String;
12431         n->val.val.str = (state ? "t" : "f");
12432         n->location = location;
12433
12434         return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
12435 }
12436
12437 /* makeOverlaps()
12438  * Create and populate a FuncCall node to support the OVERLAPS operator.
12439  */
12440 static FuncCall *
12441 makeOverlaps(List *largs, List *rargs, int location, core_yyscan_t yyscanner)
12442 {
12443         FuncCall *n = makeNode(FuncCall);
12444
12445         n->funcname = SystemFuncName("overlaps");
12446         if (list_length(largs) == 1)
12447                 largs = lappend(largs, largs);
12448         else if (list_length(largs) != 2)
12449                 ereport(ERROR,
12450                                 (errcode(ERRCODE_SYNTAX_ERROR),
12451                                  errmsg("wrong number of parameters on left side of OVERLAPS expression"),
12452                                  parser_errposition(location)));
12453         if (list_length(rargs) == 1)
12454                 rargs = lappend(rargs, rargs);
12455         else if (list_length(rargs) != 2)
12456                 ereport(ERROR,
12457                                 (errcode(ERRCODE_SYNTAX_ERROR),
12458                                  errmsg("wrong number of parameters on right side of OVERLAPS expression"),
12459                                  parser_errposition(location)));
12460         n->args = list_concat(largs, rargs);
12461         n->agg_order = NIL;
12462         n->agg_star = FALSE;
12463         n->agg_distinct = FALSE;
12464         n->func_variadic = FALSE;
12465         n->over = NULL;
12466         n->location = location;
12467         return n;
12468 }
12469
12470 /* check_qualified_name --- check the result of qualified_name production
12471  *
12472  * It's easiest to let the grammar production for qualified_name allow
12473  * subscripts and '*', which we then must reject here.
12474  */
12475 static void
12476 check_qualified_name(List *names, core_yyscan_t yyscanner)
12477 {
12478         ListCell   *i;
12479
12480         foreach(i, names)
12481         {
12482                 if (!IsA(lfirst(i), String))
12483                         parser_yyerror("syntax error");
12484         }
12485 }
12486
12487 /* check_func_name --- check the result of func_name production
12488  *
12489  * It's easiest to let the grammar production for func_name allow subscripts
12490  * and '*', which we then must reject here.
12491  */
12492 static List *
12493 check_func_name(List *names, core_yyscan_t yyscanner)
12494 {
12495         ListCell   *i;
12496
12497         foreach(i, names)
12498         {
12499                 if (!IsA(lfirst(i), String))
12500                         parser_yyerror("syntax error");
12501         }
12502         return names;
12503 }
12504
12505 /* check_indirection --- check the result of indirection production
12506  *
12507  * We only allow '*' at the end of the list, but it's hard to enforce that
12508  * in the grammar, so do it here.
12509  */
12510 static List *
12511 check_indirection(List *indirection, core_yyscan_t yyscanner)
12512 {
12513         ListCell *l;
12514
12515         foreach(l, indirection)
12516         {
12517                 if (IsA(lfirst(l), A_Star))
12518                 {
12519                         if (lnext(l) != NULL)
12520                                 parser_yyerror("improper use of \"*\"");
12521                 }
12522         }
12523         return indirection;
12524 }
12525
12526 /* extractArgTypes()
12527  * Given a list of FunctionParameter nodes, extract a list of just the
12528  * argument types (TypeNames) for input parameters only.  This is what
12529  * is needed to look up an existing function, which is what is wanted by
12530  * the productions that use this call.
12531  */
12532 static List *
12533 extractArgTypes(List *parameters)
12534 {
12535         List       *result = NIL;
12536         ListCell   *i;
12537
12538         foreach(i, parameters)
12539         {
12540                 FunctionParameter *p = (FunctionParameter *) lfirst(i);
12541
12542                 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
12543                         result = lappend(result, p->argType);
12544         }
12545         return result;
12546 }
12547
12548 /* findLeftmostSelect()
12549  * Find the leftmost component SelectStmt in a set-operation parsetree.
12550  */
12551 static SelectStmt *
12552 findLeftmostSelect(SelectStmt *node)
12553 {
12554         while (node && node->op != SETOP_NONE)
12555                 node = node->larg;
12556         Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
12557         return node;
12558 }
12559
12560 /* insertSelectOptions()
12561  * Insert ORDER BY, etc into an already-constructed SelectStmt.
12562  *
12563  * This routine is just to avoid duplicating code in SelectStmt productions.
12564  */
12565 static void
12566 insertSelectOptions(SelectStmt *stmt,
12567                                         List *sortClause, List *lockingClause,
12568                                         Node *limitOffset, Node *limitCount,
12569                                         WithClause *withClause,
12570                                         core_yyscan_t yyscanner)
12571 {
12572         Assert(IsA(stmt, SelectStmt));
12573
12574         /*
12575          * Tests here are to reject constructs like
12576          *      (SELECT foo ORDER BY bar) ORDER BY baz
12577          */
12578         if (sortClause)
12579         {
12580                 if (stmt->sortClause)
12581                         ereport(ERROR,
12582                                         (errcode(ERRCODE_SYNTAX_ERROR),
12583                                          errmsg("multiple ORDER BY clauses not allowed"),
12584                                          parser_errposition(exprLocation((Node *) sortClause))));
12585                 stmt->sortClause = sortClause;
12586         }
12587         /* We can handle multiple locking clauses, though */
12588         stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
12589         if (limitOffset)
12590         {
12591                 if (stmt->limitOffset)
12592                         ereport(ERROR,
12593                                         (errcode(ERRCODE_SYNTAX_ERROR),
12594                                          errmsg("multiple OFFSET clauses not allowed"),
12595                                          parser_errposition(exprLocation(limitOffset))));
12596                 stmt->limitOffset = limitOffset;
12597         }
12598         if (limitCount)
12599         {
12600                 if (stmt->limitCount)
12601                         ereport(ERROR,
12602                                         (errcode(ERRCODE_SYNTAX_ERROR),
12603                                          errmsg("multiple LIMIT clauses not allowed"),
12604                                          parser_errposition(exprLocation(limitCount))));
12605                 stmt->limitCount = limitCount;
12606         }
12607         if (withClause)
12608         {
12609                 if (stmt->withClause)
12610                         ereport(ERROR,
12611                                         (errcode(ERRCODE_SYNTAX_ERROR),
12612                                          errmsg("multiple WITH clauses not allowed"),
12613                                          parser_errposition(exprLocation((Node *) withClause))));
12614                 stmt->withClause = withClause;
12615         }
12616 }
12617
12618 static Node *
12619 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
12620 {
12621         SelectStmt *n = makeNode(SelectStmt);
12622
12623         n->op = op;
12624         n->all = all;
12625         n->larg = (SelectStmt *) larg;
12626         n->rarg = (SelectStmt *) rarg;
12627         return (Node *) n;
12628 }
12629
12630 /* SystemFuncName()
12631  * Build a properly-qualified reference to a built-in function.
12632  */
12633 List *
12634 SystemFuncName(char *name)
12635 {
12636         return list_make2(makeString("pg_catalog"), makeString(name));
12637 }
12638
12639 /* SystemTypeName()
12640  * Build a properly-qualified reference to a built-in type.
12641  *
12642  * typmod is defaulted, but may be changed afterwards by caller.
12643  * Likewise for the location.
12644  */
12645 TypeName *
12646 SystemTypeName(char *name)
12647 {
12648         return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
12649                                                                                            makeString(name)));
12650 }
12651
12652 /* doNegate()
12653  * Handle negation of a numeric constant.
12654  *
12655  * Formerly, we did this here because the optimizer couldn't cope with
12656  * indexquals that looked like "var = -4" --- it wants "var = const"
12657  * and a unary minus operator applied to a constant didn't qualify.
12658  * As of Postgres 7.0, that problem doesn't exist anymore because there
12659  * is a constant-subexpression simplifier in the optimizer.  However,
12660  * there's still a good reason for doing this here, which is that we can
12661  * postpone committing to a particular internal representation for simple
12662  * negative constants.  It's better to leave "-123.456" in string form
12663  * until we know what the desired type is.
12664  */
12665 static Node *
12666 doNegate(Node *n, int location)
12667 {
12668         if (IsA(n, A_Const))
12669         {
12670                 A_Const *con = (A_Const *)n;
12671
12672                 /* report the constant's location as that of the '-' sign */
12673                 con->location = location;
12674
12675                 if (con->val.type == T_Integer)
12676                 {
12677                         con->val.val.ival = -con->val.val.ival;
12678                         return n;
12679                 }
12680                 if (con->val.type == T_Float)
12681                 {
12682                         doNegateFloat(&con->val);
12683                         return n;
12684                 }
12685         }
12686
12687         return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
12688 }
12689
12690 static void
12691 doNegateFloat(Value *v)
12692 {
12693         char   *oldval = v->val.str;
12694
12695         Assert(IsA(v, Float));
12696         if (*oldval == '+')
12697                 oldval++;
12698         if (*oldval == '-')
12699                 v->val.str = oldval+1;  /* just strip the '-' */
12700         else
12701         {
12702                 char   *newval = (char *) palloc(strlen(oldval) + 2);
12703
12704                 *newval = '-';
12705                 strcpy(newval+1, oldval);
12706                 v->val.str = newval;
12707         }
12708 }
12709
12710 static Node *
12711 makeAArrayExpr(List *elements, int location)
12712 {
12713         A_ArrayExpr *n = makeNode(A_ArrayExpr);
12714
12715         n->elements = elements;
12716         n->location = location;
12717         return (Node *) n;
12718 }
12719
12720 static Node *
12721 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
12722                         int location)
12723 {
12724         XmlExpr    *x = makeNode(XmlExpr);
12725
12726         x->op = op;
12727         x->name = name;
12728         /*
12729          * named_args is a list of ResTarget; it'll be split apart into separate
12730          * expression and name lists in transformXmlExpr().
12731          */
12732         x->named_args = named_args;
12733         x->arg_names = NIL;
12734         x->args = args;
12735         /* xmloption, if relevant, must be filled in by caller */
12736         /* type and typmod will be filled in during parse analysis */
12737         x->location = location;
12738         return (Node *) x;
12739 }
12740
12741 /*
12742  * Merge the input and output parameters of a table function.
12743  */
12744 static List *
12745 mergeTableFuncParameters(List *func_args, List *columns)
12746 {
12747         ListCell   *lc;
12748
12749         /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
12750         foreach(lc, func_args)
12751         {
12752                 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
12753
12754                 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
12755                         ereport(ERROR,
12756                                         (errcode(ERRCODE_SYNTAX_ERROR),
12757                                          errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
12758         }
12759
12760         return list_concat(func_args, columns);
12761 }
12762
12763 /*
12764  * Determine return type of a TABLE function.  A single result column
12765  * returns setof that column's type; otherwise return setof record.
12766  */
12767 static TypeName *
12768 TableFuncTypeName(List *columns)
12769 {
12770         TypeName *result;
12771
12772         if (list_length(columns) == 1)
12773         {
12774                 FunctionParameter *p = (FunctionParameter *) linitial(columns);
12775
12776                 result = (TypeName *) copyObject(p->argType);
12777         }
12778         else
12779                 result = SystemTypeName("record");
12780
12781         result->setof = true;
12782
12783         return result;
12784 }
12785
12786 /*
12787  * Convert a list of (dotted) names to a RangeVar (like
12788  * makeRangeVarFromNameList, but with position support).  The
12789  * "AnyName" refers to the any_name production in the grammar.
12790  */
12791 static RangeVar *
12792 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
12793 {
12794         RangeVar *r = makeNode(RangeVar);
12795
12796         switch (list_length(names))
12797         {
12798                 case 1:
12799                         r->catalogname = NULL;
12800                         r->schemaname = NULL;
12801                         r->relname = strVal(linitial(names));
12802                         break;
12803                 case 2:
12804                         r->catalogname = NULL;
12805                         r->schemaname = strVal(linitial(names));
12806                         r->relname = strVal(lsecond(names));
12807                         break;
12808                 case 3:
12809                         r->catalogname = strVal(linitial(names));;
12810                         r->schemaname = strVal(lsecond(names));
12811                         r->relname = strVal(lthird(names));
12812                         break;
12813                 default:
12814                         ereport(ERROR,
12815                                         (errcode(ERRCODE_SYNTAX_ERROR),
12816                                          errmsg("improper qualified name (too many dotted names): %s",
12817                                                         NameListToString(names)),
12818                                          parser_errposition(position)));
12819                         break;
12820         }
12821
12822         r->relpersistence = RELPERSISTENCE_PERMANENT;
12823         r->location = position;
12824
12825         return r;
12826 }
12827
12828 /* Separate Constraint nodes from COLLATE clauses in a ColQualList */
12829 static void
12830 SplitColQualList(List *qualList,
12831                                  List **constraintList, CollateClause **collClause,
12832                                  core_yyscan_t yyscanner)
12833 {
12834         ListCell   *cell;
12835         ListCell   *prev;
12836         ListCell   *next;
12837
12838         *collClause = NULL;
12839         prev = NULL;
12840         for (cell = list_head(qualList); cell; cell = next)
12841         {
12842                 Node   *n = (Node *) lfirst(cell);
12843
12844                 next = lnext(cell);
12845                 if (IsA(n, Constraint))
12846                 {
12847                         /* keep it in list */
12848                         prev = cell;
12849                         continue;
12850                 }
12851                 if (IsA(n, CollateClause))
12852                 {
12853                         CollateClause *c = (CollateClause *) n;
12854
12855                         if (*collClause)
12856                                 ereport(ERROR,
12857                                                 (errcode(ERRCODE_SYNTAX_ERROR),
12858                                                  errmsg("multiple COLLATE clauses not allowed"),
12859                                                  parser_errposition(c->location)));
12860                         *collClause = c;
12861                 }
12862                 else
12863                         elog(ERROR, "unexpected node type %d", (int) n->type);
12864                 /* remove non-Constraint nodes from qualList */
12865                 qualList = list_delete_cell(qualList, cell, prev);
12866         }
12867         *constraintList = qualList;
12868 }
12869
12870 /* parser_init()
12871  * Initialize to parse one query string
12872  */
12873 void
12874 parser_init(base_yy_extra_type *yyext)
12875 {
12876         yyext->parsetree = NIL;         /* in case grammar forgets to set it */
12877 }
12878
12879 /*
12880  * Must undefine this stuff before including scan.c, since it has different
12881  * definitions for these macros.
12882  */
12883 #undef yyerror
12884 #undef yylval
12885 #undef yylloc
12886
12887 #include "scan.c"